1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef NVRAM_MESSAGES_NVRAM_MESSAGES_H_
18 #define NVRAM_MESSAGES_NVRAM_MESSAGES_H_
19 
20 #include <hardware/nvram_defs.h>
21 
22 #include <nvram/messages/blob.h>
23 #include <nvram/messages/compiler.h>
24 #include <nvram/messages/struct.h>
25 #include <nvram/messages/tagged_union.h>
26 #include <nvram/messages/vector.h>
27 
28 namespace nvram {
29 
30 enum Command {
31   // Commands corresponding to the API defined in the access-controlled NVRAM
32   // HAL spec. Note that some commands service multiple HAL API calls.
33   COMMAND_GET_INFO = 1,
34   COMMAND_CREATE_SPACE = 2,
35   COMMAND_GET_SPACE_INFO = 3,
36   COMMAND_DELETE_SPACE = 4,
37   COMMAND_DISABLE_CREATE = 5,
38   COMMAND_WRITE_SPACE = 6,
39   COMMAND_READ_SPACE = 7,
40   COMMAND_LOCK_SPACE_WRITE = 8,
41   COMMAND_LOCK_SPACE_READ = 9,
42 
43   // The wipe commands are provided as a utility for clearing NVRAM during
44   // hardware reset. These are not accessible via the HAL API, but may be used
45   // by implementations to implement NVRAM clearing on full device reset.
46   COMMAND_WIPE_STORAGE = 10,
47   COMMAND_DISABLE_WIPE = 11,
48 };
49 
50 // COMMAND_GET_INFO request/response.
51 struct GetInfoRequest {};
52 
53 struct GetInfoResponse {
54   uint64_t total_size = 0;
55   uint64_t available_size = 0;
56   uint64_t max_space_size = 0;
57   uint32_t max_spaces = 0;
58   Vector<uint32_t> space_list;
59   bool wipe_disabled = false;
60 };
61 
62 // COMMAND_CREATE_SPACE request/response.
63 struct CreateSpaceRequest {
64   uint32_t index = 0;
65   uint64_t size = 0;
66   Vector<nvram_control_t> controls;
67   Blob authorization_value;
68 };
69 
70 struct CreateSpaceResponse {};
71 
72 // COMMAND_GET_SPACE_INFO request/response.
73 struct GetSpaceInfoRequest {
74   uint32_t index = 0;
75 };
76 
77 struct GetSpaceInfoResponse {
78   uint64_t size = 0;
79   Vector<nvram_control_t> controls;
80   bool read_locked = false;
81   bool write_locked = false;
82 };
83 
84 // COMMAND_DELETE_SPACE request/response.
85 struct DeleteSpaceRequest {
86   uint32_t index = 0;
87   Blob authorization_value;
88 };
89 
90 struct DeleteSpaceResponse {};
91 
92 // COMMAND_DISABLE_CREATE request/response.
93 struct DisableCreateRequest {};
94 
95 struct DisableCreateResponse {};
96 
97 // COMMAND_WRITE_SPACE request/response.
98 struct WriteSpaceRequest {
99   uint32_t index = 0;
100   Blob buffer;
101   Blob authorization_value;
102 };
103 
104 struct WriteSpaceResponse {};
105 
106 // COMMAND_READ_SPACE request/response.
107 struct ReadSpaceRequest {
108   uint32_t index = 0;
109   Blob authorization_value;
110 };
111 
112 struct ReadSpaceResponse {
113   Blob buffer;
114 };
115 
116 // COMMAND_LOCK_SPACE_WRITE request/response.
117 struct LockSpaceWriteRequest {
118   uint32_t index = 0;
119   Blob authorization_value;
120 };
121 
122 struct LockSpaceWriteResponse {};
123 
124 // COMMAND_LOCK_SPACE_READ request/response.
125 struct LockSpaceReadRequest {
126   uint32_t index = 0;
127   Blob authorization_value;
128 };
129 
130 struct LockSpaceReadResponse {};
131 
132 // COMMAND_WIPE request/response.
133 struct WipeStorageRequest {};
134 struct WipeStorageResponse {};
135 
136 // COMMAND_DISABLE_WIPE request/response.
137 struct DisableWipeRequest {};
138 struct DisableWipeResponse {};
139 
140 // Generic request message, carrying command-specific payload. The slot set in
141 // the payload determines the requested command.
142 using RequestUnion = TaggedUnion<
143     Command,
144     TaggedUnionMember<COMMAND_GET_INFO, GetInfoRequest>,
145     TaggedUnionMember<COMMAND_CREATE_SPACE, CreateSpaceRequest>,
146     TaggedUnionMember<COMMAND_GET_SPACE_INFO, GetSpaceInfoRequest>,
147     TaggedUnionMember<COMMAND_DELETE_SPACE, DeleteSpaceRequest>,
148     TaggedUnionMember<COMMAND_DISABLE_CREATE, DisableCreateRequest>,
149     TaggedUnionMember<COMMAND_WRITE_SPACE, WriteSpaceRequest>,
150     TaggedUnionMember<COMMAND_READ_SPACE, ReadSpaceRequest>,
151     TaggedUnionMember<COMMAND_LOCK_SPACE_WRITE, LockSpaceWriteRequest>,
152     TaggedUnionMember<COMMAND_LOCK_SPACE_READ, LockSpaceReadRequest>,
153     TaggedUnionMember<COMMAND_WIPE_STORAGE, WipeStorageRequest>,
154     TaggedUnionMember<COMMAND_DISABLE_WIPE, DisableWipeRequest>>;
155 struct Request {
156   RequestUnion payload;
157 };
158 
159 // Generic response message, carrying a result code and command-specific
160 // payload.
161 using ResponseUnion = TaggedUnion<
162     Command,
163     TaggedUnionMember<COMMAND_GET_INFO, GetInfoResponse>,
164     TaggedUnionMember<COMMAND_CREATE_SPACE, CreateSpaceResponse>,
165     TaggedUnionMember<COMMAND_GET_SPACE_INFO, GetSpaceInfoResponse>,
166     TaggedUnionMember<COMMAND_DELETE_SPACE, DeleteSpaceResponse>,
167     TaggedUnionMember<COMMAND_DISABLE_CREATE, DisableCreateResponse>,
168     TaggedUnionMember<COMMAND_WRITE_SPACE, WriteSpaceResponse>,
169     TaggedUnionMember<COMMAND_READ_SPACE, ReadSpaceResponse>,
170     TaggedUnionMember<COMMAND_LOCK_SPACE_WRITE, LockSpaceWriteResponse>,
171     TaggedUnionMember<COMMAND_LOCK_SPACE_READ, LockSpaceReadResponse>,
172     TaggedUnionMember<COMMAND_WIPE_STORAGE, WipeStorageResponse>,
173     TaggedUnionMember<COMMAND_DISABLE_WIPE, DisableWipeResponse>>;
174 struct Response {
175   nvram_result_t result = NV_RESULT_SUCCESS;
176   ResponseUnion payload;
177 };
178 
179 // Encoding and decoding functions. Template instantiations are provided for the
180 // |Request| and |Response| wrapper types declared above.
181 
182 // Encode |msg| to |blob|. Returns true if successful.
183 template <typename Message>
184 bool Encode(const Message& msg, Blob* blob);
185 
186 // Encode |msg| to |buffer|, which is of size |*size|. Updates |*size| to
187 // indicate the number of bytes written. Returns true on success.
188 template <typename Message>
189 bool Encode(const Message& msg, void* buffer, size_t* size);
190 
191 // Decode |msg| from the |data| buffer, which contains |size| bytes. Returns
192 // true if successful.
193 template <typename Message>
194 bool Decode(const uint8_t* data, size_t size, Message* msg);
195 
196 }  // namespace nvram
197 
198 #endif  // NVRAM_MESSAGES_NVRAM_MESSAGES_H_
199