1 /*
2  * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3  */
4 #ifndef __TOOL_MSG_H__
5 #define __TOOL_MSG_H__
6 
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10 
11 #define BOOT_MAGIC_NUMBER                   0xBE57EC1C
12 #define BOOT_HASH_TYPE_MD5                  1
13 #define BOOT_HASH_TYPE_SHA256               2
14 #define BOOT_KEY_TYPE_RSA2048               1
15 #define BOOT_KEY_TYPE_ECDSA192              2
16 #define BOOT_KEY_TYPE_ECDSA256              3
17 
18 #define SEC_KEY_VERSION                     0
19 #define BOOT_STRUCT_VERSION                 0
20 
21 #define PREFIX_CHAR                         0xBE
22 
23 #define KEY_LEN                             (4 + 256 + 256)
24 #define SIG_LEN                             256
25 #define HASH_LEN                            32
26 
27 #ifndef MAX_TOOL_MSG_DATA_LEN
28 // Max msg: flash cmd msg with 4-byte address and 16-byte data (1 + 4 + 16)
29 #define MAX_TOOL_MSG_DATA_LEN               21
30 #endif
31 
32 #define MAX_READ_DATA_LEN                   16
33 #define MAX_WRITE_DATA_LEN                  16
34 #define MAX_FLASH_CMD_DATA_LEN              16
35 
36 #define MIN_SIGNED_CODE_SIZE                32
37 
38 #ifdef SECURE_BOOT_V1
39 #define BOOT_STRUCT_V1_OFFSET_TO_SIGN(b)    \
40     ((unsigned char *)&((struct boot_struct_v1_t *)(b))->hdr.security)
41 #define BOOT_STRUCT_V1_LEN_TO_SIGN          \
42     ((unsigned long)&((struct boot_struct_v1_t *)0)->sig[0] - \
43     (unsigned long)&((struct boot_struct_v1_t *)0)->hdr.security)
44 #endif
45 
46 #define SEC_KEY_OFFSET_TO_HASH(b)           \
47     ((unsigned char *)&((struct sec_key_struct_t *)(b))->version)
48 #define SEC_KEY_LEN_TO_HASH                 \
49     ((unsigned long)&((struct sec_key_struct_t *)0)->hash[0] - \
50     (unsigned long)&((struct sec_key_struct_t *)0)->version)
51 
52 #define SIG_MSG_OVERHEAD                    8
53 #define SIG_MSG_V1_EXTRA_DATA_LEN           (sizeof(struct boot_struct_v1_t) + sizeof(struct code_sig_struct_t))
54 #define SIG_MSG_V1_TOTAL_LEN                (SIG_MSG_OVERHEAD + SIG_MSG_V1_EXTRA_DATA_LEN)
55 #define SIG_MSG_EXTRA_DATA_LEN              (sizeof(struct boot_struct_t) + sizeof(struct code_sig_struct_t))
56 #define SIG_MSG_TOTAL_LEN                   (SIG_MSG_OVERHEAD + SIG_MSG_EXTRA_DATA_LEN)
57 
58 #define CODE_MSG_OVERHEAD                   8
59 #define BURN_DATA_MSG_OVERHEAD              16
60 
61 #define SECTOR_SIZE_64K                     (1 << 16)
62 #define SECTOR_SIZE_32K                     (1 << 15)
63 #define SECTOR_SIZE_16K                     (1 << 14)
64 #define SECTOR_SIZE_4K                      (1 << 12)
65 
66 #define MSG_TOTAL_LEN(msg)                  (sizeof(struct msg_hdr_t) + ((struct msg_hdr_t *)(msg))->len + 1)
67 
68 enum MSG_TYPE {
69     TYPE_SYS = 0x00,
70     TYPE_READ = 0x01,
71     TYPE_WRITE = 0x02,
72     TYPE_BULK_READ = 0x03,
73 #if defined(TEST_OVER_THE_AIR_ENANBLED)
74     TYPE_EXT_WRITE = 0x04,
75     TYPE_BULK_WRITE_START = 0x05,
76     TYPE_BULK_WRITE_DATA = 0x06,
77 #endif
78     TYPE_NOTIF = 0x10,
79     TYPE_SYNC = 0x50,
80     TYPE_SIG_INFO = 0x51,
81     TYPE_SIG = 0x52,
82     TYPE_CODE_INFO = 0x53,
83     TYPE_CODE = 0x54,
84     TYPE_RUN = 0x55,
85     TYPE_SECTOR_SIZE = 0x60,
86     TYPE_ERASE_BURN_START = 0x61,
87     TYPE_ERASE_BURN_DATA = 0x62,
88     TYPE_OBSOLETED_63 = 0x63,
89     TYPE_OBSOLETED_64 = 0x64,
90     TYPE_FLASH_CMD = 0x65,
91     TYPE_GET_SECTOR_INFO = 0x66,
92     TYPE_SEC_REG_ERASE_BURN_START = 0x67,
93     TYPE_SEC_REG_ERASE_BURN_DATA = 0x68,
94 
95     // Extended types
96     TYPE_PROD_TEST = 0x81,
97     TYPE_RUNTIME_CMD = 0x82,
98     TYPE_BT_CALIB_CMD = 0x83,
99     TYPE_PROTO_EL = 0xA0,
100 
101     TYPE_INVALID = 0xFF,
102 };
103 
104 enum SYS_CMD_TYPE {
105     SYS_CMD_REBOOT = 0xF1,
106     SYS_CMD_SHUTDOWN = 0xF2,
107     SYS_CMD_FLASH_BOOT = 0xF3,
108     SYS_CMD_SET_BOOTMODE = 0xE1,
109     SYS_CMD_CLR_BOOTMODE = 0xE2,
110     SYS_CMD_GET_BOOTMODE = 0xE3,
111 };
112 
113 enum ERR_CODE {
114     ERR_NONE = 0x00,
115     ERR_LEN = 0x01,
116     ERR_CHECKSUM = 0x02,
117     ERR_NOT_SYNC = 0x03,
118     ERR_NOT_SEC = 0x04,
119     ERR_SYNC_WORD = 0x05,
120     ERR_SYS_CMD = 0x06,
121     ERR_DATA_ADDR = 0x07,
122     ERR_DATA_LEN = 0x08,
123     ERR_ACCESS_RIGHT = 0x09,
124 
125     ERR_TYPE_INVALID = 0x0F,
126 
127     //ERR_BOOT_OK = 0x10,
128     ERR_BOOT_MAGIC = 0x11,
129     ERR_BOOT_SEC = 0x12,
130     ERR_BOOT_HASH_TYPE = 0x13,
131     ERR_BOOT_KEY_TYPE = 0x14,
132     ERR_BOOT_KEY_LEN = 0x15,
133     ERR_BOOT_SIG_LEN = 0x16,
134     ERR_BOOT_SIG = 0x17,
135     ERR_BOOT_CRC = 0x18,
136     ERR_BOOT_LEN = 0x19,
137     ERR_SIG_CODE_SIZE = 0x1A,
138     ERR_SIG_SIG_LEN = 0x1B,
139     ERR_SIG_INFO_MISSING = 0x1C,
140     ERR_BOOT_KEY_ID = 0x1D,
141     ERR_BOOT_HASH = 0x1E,
142 
143     ERR_CODE_OK = 0x20,
144     ERR_BOOT_MISSING = 0x21,
145     ERR_CODE_SIZE_SIG = 0x22,
146     ERR_CODE_ADDR_SIZE = 0x23,
147     ERR_CODE_INFO_MISSING = 0x24,
148     ERR_CODE_CRC = 0x25,
149     ERR_CODE_SIG = 0x26,
150 
151     ERR_CODE_MISSING = 0x31,
152     ERR_VERSION = 0x32,
153 
154     ERR_BURN_OK = 0x60,
155     ERR_SECTOR_SIZE = 0x61,
156     ERR_SECTOR_SEQ_OVERFLOW = 0x62,
157     ERR_BURN_INFO_MISSING = 0x63,
158     ERR_SECTOR_DATA_LEN = 0x64,
159     ERR_SECTOR_DATA_CRC = 0x65,
160     ERR_SECTOR_SEQ = 0x66,
161     ERR_ERASE_FLSH = 0x67,
162     ERR_BURN_FLSH = 0x68,
163     ERR_VERIFY_FLSH = 0x69,
164     ERR_FLASH_CMD = 0x6A,
165 
166     ERR_TYPE_MISMATCHED = 0xE1,
167     ERR_SEQ_MISMATCHED = 0xE2,
168     ERR_BUF_TOO_SMALL = 0xE3,
169 
170     ERR_INTERNAL = 0xFF,
171 };
172 
173 enum PARSE_STATE {
174     PARSE_HEADER,
175     PARSE_DATA,
176     PARSE_EXTRA,
177 };
178 
179 struct msg_hdr_t {
180     unsigned char prefix;
181     unsigned char type;
182     unsigned char seq;
183     unsigned char len;
184 };
185 
186 struct message_t {
187     struct msg_hdr_t hdr;
188     // data and 1-byte check_sum
189     unsigned char data[MAX_TOOL_MSG_DATA_LEN + 1];
190 };
191 
192 struct sec_key_struct_t {
193     unsigned int magic;
194     unsigned short version;
195     unsigned char hash_type;
196     unsigned char key_type;
197     unsigned short key_len;
198     unsigned short sig_len;
199     unsigned int reserved[5];
200     unsigned char key[KEY_LEN];
201     unsigned char hash[HASH_LEN];
202 };
203 
204 struct boot_hdr_v1_t {
205     unsigned int magic;
206     unsigned short security;
207     unsigned char hash_type;
208     unsigned char key_type;
209     unsigned short key_len;
210     unsigned short sig_len;
211     unsigned int build_info_start;
212 };
213 
214 struct boot_struct_v1_t {
215     struct boot_hdr_v1_t hdr;
216     unsigned char key[KEY_LEN];
217     unsigned char sig[SIG_LEN];
218 };
219 
220 struct boot_hdr_t {
221     unsigned int magic;
222     unsigned short security;
223     unsigned short version;
224     unsigned int reserved;
225     unsigned int build_info_start;
226 };
227 
228 struct boot_struct_t {
229     struct boot_hdr_t hdr;
230 #if HAAS_OTA_ENABLED //OTA user add it for debug.
231     unsigned char ver[16];
232 #else
233     unsigned int reserved[4];
234 #endif
235 };
236 
237 struct code_sig_struct_t {
238     unsigned int code_size;
239     unsigned short sig_len;
240     unsigned short reserved;
241     unsigned char sig[SIG_LEN];
242 };
243 
244 struct exec_struct_t {
245     unsigned int entry;
246     unsigned int param;
247     unsigned int sp;
248     unsigned int exec_addr;
249 };
250 
251 #ifdef __cplusplus
252 }
253 #endif
254 
255 #endif
256 
257