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 __NANOHUBPACKET_H
18 #define __NANOHUBPACKET_H
19 
20 /**
21  * Formats and constants related to nanohub packets.  This header is intended
22  * to be shared between the host Linux kernel and the nanohub implementation.
23  */
24 #include "toolchain.h"
25 
26 #ifdef __KERNEL__
27 #include <linux/types.h>
28 #else
29 #include <hostIntf.h>
30 #include <stdint.h>
31 
32 typedef uint16_t __le16;
33 typedef uint16_t __be16;
34 typedef uint32_t __le32;
35 typedef uint32_t __be32;
36 typedef uint64_t __le64;
37 typedef uint64_t __be64;
38 #endif
39 
40 SET_PACKED_STRUCT_MODE_ON
41 struct NanohubPacket {
42     uint8_t sync;
43     __le32 seq;
44     __le32 reason;
45     uint8_t len;
46     uint8_t data[0];
47 } ATTRIBUTE_PACKED;
48 SET_PACKED_STRUCT_MODE_OFF
49 
50 SET_PACKED_STRUCT_MODE_ON
51 struct NanohubPacketFooter {
52     __le32 crc;
53 } ATTRIBUTE_PACKED;
54 SET_PACKED_STRUCT_MODE_OFF
55 
nanohubGetPacketFooter(struct NanohubPacket * packet)56 static inline struct NanohubPacketFooter *nanohubGetPacketFooter(struct NanohubPacket *packet)
57 {
58     return (struct NanohubPacketFooter *)(packet->data + packet->len);
59 }
60 
61 #define NANOHUB_PACKET_SIZE(len) \
62     (sizeof(struct NanohubPacket) + (len) + sizeof(struct NanohubPacketFooter))
63 
64 #define NANOHUB_PACKET_PAYLOAD_MAX    255
65 #define NANOHUB_PACKET_SIZE_MAX       NANOHUB_PACKET_SIZE(NANOHUB_PACKET_PAYLOAD_MAX)
66 
67 #define NANOHUB_SYNC_BYTE             0x31
68 
69 #define NANOHUB_PREAMBLE_BYTE         0xFF
70 #define NANOHUB_ACK_PREAMBLE_LEN      16
71 #define NANOHUB_PAYLOAD_PREAMBLE_LEN  512
72 #define NANOHUB_RSA_KEY_CHUNK_LEN     64
73 
74 #define NANOHUB_INT_BOOT_COMPLETE     0
75 #define NANOHUB_INT_WAKE_COMPLETE     0
76 #define NANOHUB_INT_WAKEUP            1
77 #define NANOHUB_INT_NONWAKEUP         2
78 #define NANOHUB_INT_CMD_WAIT          3
79 
80 #define NANOHUB_REASON_ACK                    0x00000000
81 #define NANOHUB_REASON_NAK                    0x00000001
82 #define NANOHUB_REASON_NAK_BUSY               0x00000002
83 
84 /**
85  * INFORMATIONAL
86  */
87 
88 #define NANOHUB_REASON_GET_OS_HW_VERSIONS     0x00001000
89 #if defined(__GNUC__)
90 SET_PACKED_STRUCT_MODE_ON
91 struct NanohubOsHwVersionsRequest {
92 } ATTRIBUTE_PACKED;
93 SET_PACKED_STRUCT_MODE_OFF
94 #endif
95 
96 SET_PACKED_STRUCT_MODE_ON
97 struct NanohubOsHwVersionsResponse {
98     __le16 hwType;
99     __le16 hwVer;
100     __le16 blVer;
101     __le16 osVer;
102     __le32 variantVer;
103 } ATTRIBUTE_PACKED;
104 SET_PACKED_STRUCT_MODE_OFF
105 
106 #define NANOHUB_REASON_GET_APP_VERSIONS       0x00001001
107 
108 SET_PACKED_STRUCT_MODE_ON
109 struct NanohubAppVersionsRequest {
110     __le64 appId;
111 } ATTRIBUTE_PACKED;
112 SET_PACKED_STRUCT_MODE_OFF
113 
114 SET_PACKED_STRUCT_MODE_ON
115 struct NanohubAppVersionsResponse {
116     __le32 appVer;
117 } ATTRIBUTE_PACKED;
118 SET_PACKED_STRUCT_MODE_OFF
119 
120 #define NANOHUB_REASON_QUERY_APP_INFO         0x00001002
121 
122 SET_PACKED_STRUCT_MODE_ON
123 struct NanohubAppInfoRequest {
124     __le32 appIdx;
125 } ATTRIBUTE_PACKED;
126 SET_PACKED_STRUCT_MODE_OFF
127 
128 SET_PACKED_STRUCT_MODE_ON
129 struct NanohubAppInfoResponse {
130     __le64 appId;
131     __le32 appVer;
132     __le32 appSize;
133 } ATTRIBUTE_PACKED;
134 SET_PACKED_STRUCT_MODE_OFF
135 
136 #define NANOHUB_REASON_START_FIRMWARE_UPLOAD  0x00001040
137 
138 SET_PACKED_STRUCT_MODE_ON
139 struct NanohubStartFirmwareUploadRequest {
140     __le32 size;
141     __le32 crc;
142     uint8_t type;
143 } ATTRIBUTE_PACKED;
144 SET_PACKED_STRUCT_MODE_OFF
145 
146 SET_PACKED_STRUCT_MODE_ON
147 struct NanohubStartFirmwareUploadResponse {
148     uint8_t accepted;
149 } ATTRIBUTE_PACKED;
150 SET_PACKED_STRUCT_MODE_OFF
151 
152 #define NANOHUB_REASON_FIRMWARE_CHUNK         0x00001041
153 
154 SET_PACKED_STRUCT_MODE_ON
155 struct NanohubFirmwareChunkRequest {
156     __le32 offset;
157     uint8_t data[NANOHUB_PACKET_PAYLOAD_MAX-sizeof(__le32)];
158 } ATTRIBUTE_PACKED;
159 SET_PACKED_STRUCT_MODE_OFF
160 
161 enum NanohubFirmwareChunkReply {
162     NANOHUB_FIRMWARE_CHUNK_REPLY_ACCEPTED = 0,
163     NANOHUB_FIRMWARE_CHUNK_REPLY_WAIT,
164     NANOHUB_FIRMWARE_CHUNK_REPLY_RESEND,
165     NANOHUB_FIRMWARE_CHUNK_REPLY_RESTART,
166     NANOHUB_FIRMWARE_CHUNK_REPLY_CANCEL,
167     NANOHUB_FIRMWARE_CHUNK_REPLY_CANCEL_NO_RETRY,
168 };
169 
170 SET_PACKED_STRUCT_MODE_ON
171 struct NanohubFirmwareChunkResponse {
172     uint8_t chunkReply;
173 } ATTRIBUTE_PACKED;
174 SET_PACKED_STRUCT_MODE_OFF
175 
176 #define NANOHUB_REASON_FINISH_FIRMWARE_UPLOAD 0x00001042
177 
178 #if defined(__GNUC__)
179 SET_PACKED_STRUCT_MODE_ON
180 struct NanohubFinishFirmwareUploadRequest {
181 } ATTRIBUTE_PACKED;
182 SET_PACKED_STRUCT_MODE_OFF
183 #endif
184 
185 enum NanohubFirmwareUploadReply {
186     NANOHUB_FIRMWARE_UPLOAD_SUCCESS = 0,
187     NANOHUB_FIRMWARE_UPLOAD_PROCESSING,
188     NANOHUB_FIRMWARE_UPLOAD_WAITING_FOR_DATA,
189     NANOHUB_FIRMWARE_UPLOAD_APP_SEC_KEY_NOT_FOUND,
190     NANOHUB_FIRMWARE_UPLOAD_APP_SEC_HEADER_ERROR,
191     NANOHUB_FIRMWARE_UPLOAD_APP_SEC_TOO_MUCH_DATA,
192     NANOHUB_FIRMWARE_UPLOAD_APP_SEC_TOO_LITTLE_DATA,
193     NANOHUB_FIRMWARE_UPLOAD_APP_SEC_SIG_VERIFY_FAIL,
194     NANOHUB_FIRMWARE_UPLOAD_APP_SEC_SIG_DECODE_FAIL,
195     NANOHUB_FIRMWARE_UPLOAD_APP_SEC_SIG_ROOT_UNKNOWN,
196     NANOHUB_FIRMWARE_UPLOAD_APP_SEC_MEMORY_ERROR,
197     NANOHUB_FIRMWARE_UPLOAD_APP_SEC_INVALID_DATA,
198     NANOHUB_FIRMWARE_UPLOAD_APP_SEC_VERIFY_FAILED,
199     NANOHUB_FIRMWARE_UPLOAD_APP_SEC_BAD,
200 };
201 
202 SET_PACKED_STRUCT_MODE_ON
203 struct NanohubFinishFirmwareUploadResponse {
204    uint8_t uploadReply;
205 } ATTRIBUTE_PACKED;
206 SET_PACKED_STRUCT_MODE_OFF
207 
208 #define NANOHUB_REASON_GET_INTERRUPT          0x00001080
209 
210 SET_PACKED_STRUCT_MODE_ON
211 struct NanohubGetInterruptRequest {
212     uint32_t clear[HOSTINTF_MAX_INTERRUPTS/(32*sizeof(uint8_t))];
213 } ATTRIBUTE_PACKED;
214 SET_PACKED_STRUCT_MODE_OFF
215 
216 SET_PACKED_STRUCT_MODE_ON
217 struct NanohubGetInterruptResponse {
218     uint32_t interrupts[HOSTINTF_MAX_INTERRUPTS/(32*sizeof(uint8_t))];
219 } ATTRIBUTE_PACKED;
220 SET_PACKED_STRUCT_MODE_OFF
221 
222 #define NANOHUB_REASON_MASK_INTERRUPT         0x00001081
223 
224 SET_PACKED_STRUCT_MODE_ON
225 struct NanohubMaskInterruptRequest {
226     uint8_t interrupt;
227 } ATTRIBUTE_PACKED;
228 SET_PACKED_STRUCT_MODE_OFF
229 
230 SET_PACKED_STRUCT_MODE_ON
231 struct NanohubMaskInterruptResponse {
232     uint8_t accepted;
233 } ATTRIBUTE_PACKED;
234 SET_PACKED_STRUCT_MODE_OFF
235 
236 #define NANOHUB_REASON_UNMASK_INTERRUPT       0x00001082
237 
238 SET_PACKED_STRUCT_MODE_ON
239 struct NanohubUnmaskInterruptRequest {
240     uint8_t interrupt;
241 } ATTRIBUTE_PACKED;
242 SET_PACKED_STRUCT_MODE_OFF
243 
244 SET_PACKED_STRUCT_MODE_ON
245 struct NanohubUnmaskInterruptResponse {
246     uint8_t accepted;
247 } ATTRIBUTE_PACKED;
248 SET_PACKED_STRUCT_MODE_OFF
249 
250 #define NANOHUB_REASON_READ_EVENT             0x00001090
251 
252 SET_PACKED_STRUCT_MODE_ON
253 struct NanohubReadEventRequest {
254     __le64 apBootTime;
255 } ATTRIBUTE_PACKED;
256 SET_PACKED_STRUCT_MODE_OFF
257 
258 SET_PACKED_STRUCT_MODE_ON
259 struct NanohubReadEventResponse {
260     __le32 evtType;
261     uint8_t evtData[NANOHUB_PACKET_PAYLOAD_MAX - sizeof(__le32)];
262 } ATTRIBUTE_PACKED;
263 SET_PACKED_STRUCT_MODE_OFF
264 
265 #define NANOHUB_REASON_WRITE_EVENT            0x00001091
266 
267 SET_PACKED_STRUCT_MODE_ON
268 struct NanohubWriteEventRequest {
269     __le32 evtType;
270     uint8_t evtData[NANOHUB_PACKET_PAYLOAD_MAX - sizeof(__le32)];
271 } ATTRIBUTE_PACKED;
272 SET_PACKED_STRUCT_MODE_OFF
273 
274 SET_PACKED_STRUCT_MODE_ON
275 struct NanohubWriteEventResponse {
276     uint8_t accepted;
277 } ATTRIBUTE_PACKED;
278 SET_PACKED_STRUCT_MODE_OFF
279 
280 SET_PACKED_STRUCT_MODE_ON
281 struct NanohubHalHdr {
282     uint64_t appId;
283     uint8_t len;
284     uint8_t msg;
285 } ATTRIBUTE_PACKED;
286 SET_PACKED_STRUCT_MODE_OFF
287 
288 #define NANOHUB_HAL_EXT_APPS_ON     0
289 #define NANOHUB_HAL_EXT_APPS_OFF    1
290 #define NANOHUB_HAL_EXT_APP_DELETE  2
291 
292 // this behaves more stable w.r.t. endianness than bit field
293 // this is setting byte fields in MgmtStatus response
294 // the high-order bit, if set, is indication of counter overflow
295 #define SET_COUNTER(counter, val) (counter = (val & 0x7F) | (val > 0x7F ? 0x80 : 0))
296 
297 SET_PACKED_STRUCT_MODE_ON
298 struct MgmtStatus {
299     union {
300         __le32 value;
301         // NOTE: union fields are accessed in CPU native mode
302         struct {
303             uint8_t app;
304             uint8_t task;
305             uint8_t op;
306             uint8_t erase;
307         } ATTRIBUTE_PACKED;
308     };
309 } ATTRIBUTE_PACKED;
310 SET_PACKED_STRUCT_MODE_OFF
311 
312 SET_PACKED_STRUCT_MODE_ON
313 struct NanohubHalMgmtRx {
314     __le64 appId;
315     struct MgmtStatus stat;
316 } ATTRIBUTE_PACKED;
317 SET_PACKED_STRUCT_MODE_OFF
318 
319 SET_PACKED_STRUCT_MODE_ON
320 struct NanohubHalMgmtTx {
321     struct NanohubHalHdr hdr;
322     __le32 status;
323 } ATTRIBUTE_PACKED;
324 SET_PACKED_STRUCT_MODE_OFF
325 
326 #define NANOHUB_HAL_QUERY_MEMINFO   3
327 #define NANOHUB_HAL_QUERY_APPS      4
328 
329 SET_PACKED_STRUCT_MODE_ON
330 struct NanohubHalQueryAppsRx {
331     __le32 idx;
332 } ATTRIBUTE_PACKED;
333 SET_PACKED_STRUCT_MODE_OFF
334 
335 SET_PACKED_STRUCT_MODE_ON
336 struct NanohubHalQueryAppsTx {
337     struct NanohubHalHdr hdr;
338     __le64 appId;
339     __le32 version;
340     __le32 flashUse;
341     __le32 ramUse;
342 } ATTRIBUTE_PACKED;
343 SET_PACKED_STRUCT_MODE_OFF
344 
345 #define NANOHUB_HAL_QUERY_RSA_KEYS  5
346 
347 SET_PACKED_STRUCT_MODE_ON
348 struct NanohubHalQueryRsaKeysRx {
349     __le32 offset;
350 } ATTRIBUTE_PACKED;
351 SET_PACKED_STRUCT_MODE_OFF
352 
353 SET_PACKED_STRUCT_MODE_ON
354 struct NanohubHalQueryRsaKeysTx {
355     struct NanohubHalHdr hdr;
356     uint8_t data[];
357 } ATTRIBUTE_PACKED;
358 SET_PACKED_STRUCT_MODE_OFF
359 
360 #define NANOHUB_HAL_START_UPLOAD    6
361 
362 SET_PACKED_STRUCT_MODE_ON
363 struct NanohubHalStartUploadRx {
364     uint8_t isOs;
365     __le32 length;
366 } ATTRIBUTE_PACKED;
367 SET_PACKED_STRUCT_MODE_OFF
368 
369 SET_PACKED_STRUCT_MODE_ON
370 struct NanohubHalStartUploadTx {
371     struct NanohubHalHdr hdr;
372     uint8_t success;
373 } ATTRIBUTE_PACKED;
374 SET_PACKED_STRUCT_MODE_OFF
375 
376 #define NANOHUB_HAL_CONT_UPLOAD     7
377 
378 SET_PACKED_STRUCT_MODE_ON
379 struct NanohubHalContUploadRx {
380     __le32 offset;
381     uint8_t data[];
382 } ATTRIBUTE_PACKED;
383 SET_PACKED_STRUCT_MODE_OFF
384 
385 SET_PACKED_STRUCT_MODE_ON
386 struct NanohubHalContUploadTx {
387     struct NanohubHalHdr hdr;
388     uint8_t success;
389 } ATTRIBUTE_PACKED;
390 SET_PACKED_STRUCT_MODE_OFF
391 
392 #define NANOHUB_HAL_FINISH_UPLOAD   8
393 
394 SET_PACKED_STRUCT_MODE_ON
395 struct NanohubHalFinishUploadTx {
396     struct NanohubHalHdr hdr;
397     uint8_t success;
398 } ATTRIBUTE_PACKED;
399 SET_PACKED_STRUCT_MODE_OFF
400 
401 #define NANOHUB_HAL_REBOOT          9
402 
403 SET_PACKED_STRUCT_MODE_ON
404 struct NanohubHalRebootTx {
405     struct NanohubHalHdr hdr;
406     __le32 reason;
407 } ATTRIBUTE_PACKED;
408 SET_PACKED_STRUCT_MODE_OFF
409 
410 #endif /* __NANOHUBPACKET_H */
411