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