1 #ifndef __TOOL_MSG_H__
2 #define __TOOL_MSG_H__
3 
4 #include "communication_cmd_msg.h"
5 
6 #ifdef __cplusplus
7 extern "C" {
8 #endif
9 
10 #define BOOT_MAGIC_NUMBER           0xBE57EC1C
11 #define BOOT_HASH_TYPE_MD5          1
12 #define BOOT_HASH_TYPE_SHA256       2
13 #define BOOT_KEY_TYPE_RSA2048       1
14 #define BOOT_KEY_TYPE_ECDSA192      2
15 #define BOOT_KEY_TYPE_ECDSA256      3
16 
17 #ifdef __PC_CMD_UART__
18 #define PREFIX_CHAR                 0x7b//{
19 #else
20 #define PREFIX_CHAR                 0xBE
21 #endif
22 
23 #define KEY_LEN                     (4 + 256 + 256)
24 #define SIG_LEN                     256
25 
26 #define BOOT_STRUCT_OFFSET_TO_SIGN(b)  \
27     ((unsigned char *)&((struct boot_struct_t *)(b))->hdr.security)
28 #define BOOT_STRUCT_LEN_TO_SIGN     \
29     ((unsigned int)&((struct boot_struct_t *)0)->sig[0] - \
30     (unsigned int)&((struct boot_struct_t *)0)->hdr.security)
31 
32 #define SIG_MSG_OVERHEAD            8
33 #define SIG_MSG_EXTRA_DATA_LEN      (sizeof(struct boot_struct_t) + sizeof(struct code_sig_struct_t))
34 #define SIG_MSG_TOTAL_LEN           (SIG_MSG_OVERHEAD + SIG_MSG_EXTRA_DATA_LEN)
35 
36 #define CODE_MSG_OVERHEAD           8
37 #define BURN_DATA_MSG_OVERHEAD      16
38 
39 #define SECTOR_SIZE_64K             (1 << 16)
40 #define SECTOR_SIZE_32K             (1 << 15)
41 #define SECTOR_SIZE_16K             (1 << 14)
42 #define SECTOR_SIZE_4K              (1 << 12)
43 
44 #define MSG_TOTAL_LEN(msg)          (sizeof((msg)->hdr) + (msg)->hdr.len + 1)
45 
46 enum MSG_TYPE {
47     TYPE_SYS = 0x00,
48     TYPE_READ = 0x01,
49     TYPE_WRITE = 0x02,
50     TYPE_BULK_READ = 0x03,
51     TYPE_SYNC = 0x50,
52     TYPE_SIG_INFO = 0x51,
53     TYPE_SIG = 0x52,
54     TYPE_CODE_INFO = 0x53,
55     TYPE_CODE = 0x54,
56     TYPE_RUN = 0x55,
57     TYPE_SECTOR_SIZE = 0x60,
58     TYPE_ERASE_BURN_START = 0x61,
59     TYPE_ERASE_BURN_DATA = 0x62,
60     TYPE_BURN_START = 0x63,
61     TYPE_BURN_DATA = 0x64,
62     TYPE_BURN_CMD = 0x65,
63     TYPE_GET_SECTOR_INFO = 0x66,
64 #if defined(__EXT_CMD_SUPPORT__)
65     TYPE_EXTEND_CMD = 0x67,
66 #endif
67     TYPE_COMMUNICATION_CMD = 0x68,
68 };
69 
70 enum SYS_CMD_TYPE {
71     SYS_CMD_REBOOT = 0xF1,
72     SYS_CMD_SHUTDOWN = 0xF2,
73     SYS_CMD_FLASH_BOOT = 0xF3,
74     SYS_CMD_SET_BOOTMODE = 0xE1,
75     SYS_CMD_CLR_BOOTMODE = 0xE2,
76     SYS_CMD_GET_BOOTMODE = 0xE3,
77 };
78 
79 enum ERR_CODE {
80     ERR_NONE = 0x00,
81     ERR_LEN = 0x01,
82     ERR_CHECKSUM = 0x02,
83     ERR_NOT_SYNC = 0x03,
84     ERR_NOT_SEC = 0x04,
85     ERR_SYNC_WORD = 0x05,
86     ERR_SYS_CMD = 0x06,
87     ERR_DATA_ADDR = 0x07,
88     ERR_DATA_LEN = 0x08,
89     ERR_ACCESS_RIGHT = 0x09,
90 
91     ERR_TYPE_INVALID = 0x0F,
92 
93     //ERR_BOOT_OK = 0x10,
94     ERR_BOOT_MAGIC = 0x11,
95     ERR_BOOT_SEC = 0x12,
96     ERR_BOOT_HASH_TYPE = 0x13,
97     ERR_BOOT_KEY_TYPE = 0x14,
98     ERR_BOOT_KEY_LEN = 0x15,
99     ERR_BOOT_SIG_LEN = 0x16,
100     ERR_BOOT_SIG = 0x17,
101     ERR_BOOT_CRC = 0x18,
102     ERR_BOOT_LEN = 0x19,
103     ERR_SIG_CODE_SIZE = 0x1A,
104     ERR_SIG_SIG_LEN = 0x1B,
105     ERR_SIG_INFO_MISSING = 0x1C,
106 
107     ERR_CODE_OK = 0x20,
108     ERR_BOOT_MISSING = 0x21,
109     ERR_CODE_SIZE_SIG = 0x22,
110     ERR_CODE_ADDR_SIZE = 0x23,
111     ERR_CODE_INFO_MISSING = 0x24,
112     ERR_CODE_CRC = 0x25,
113     ERR_CODE_SIG = 0x26,
114 
115     ERR_CODE_MISSING = 0x31,
116 
117     ERR_BURN_OK = 0x60,
118     ERR_SECTOR_SIZE = 0x61,
119     ERR_SECTOR_SEQ_OVERFLOW = 0x62,
120     ERR_BURN_INFO_MISSING = 0x63,
121     ERR_SECTOR_DATA_LEN = 0x64,
122     ERR_SECTOR_DATA_CRC = 0x65,
123     ERR_SECTOR_SEQ = 0x66,
124     ERR_ERASE_FLSH = 0x67,
125     ERR_BURN_FLSH = 0x68,
126     ERR_VERIFY_FLSH = 0x69,
127     ERR_BURN_CMD = 0x6A,
128 
129     ERR_TYPE_MISMATCHED = 0xE1,
130     ERR_SEQ_MISMATCHED = 0xE2,
131     ERR_BUF_TOO_SMALL = 0xE3,
132 
133     ERR_INTERNAL = 0xFF,
134 };
135 
136 enum PARSE_STATE {
137     PARSE_HEADER,
138     PARSE_DATA,
139     PARSE_EXTRA,
140 };
141 
142 struct message_t {
143     struct msg_hdr_t {
144         unsigned char prefix;
145         unsigned char type;
146         unsigned char seq;
147         unsigned char len;
148     } hdr;
149     unsigned char data[255];
150 };
151 
152 struct boot_struct_t {
153     struct boot_hdr_t {
154         unsigned int magic;
155         unsigned short security;
156         unsigned char hash_type;
157         unsigned char key_type;
158         unsigned short key_len;
159         unsigned short sig_len;
160         unsigned int build_info_start;
161     } hdr;
162     unsigned char key[KEY_LEN];
163     unsigned char sig[SIG_LEN];
164 };
165 
166 struct code_sig_struct_t {
167     unsigned int code_size;
168     unsigned short sig_len;
169     unsigned short reserved;
170     unsigned char sig[SIG_LEN];
171 };
172 
173 struct exec_struct_t {
174     unsigned int entry;
175     unsigned int param;
176     unsigned int sp;
177     unsigned int reserved;
178 };
179 
180 #ifdef __cplusplus
181 }
182 #endif
183 
184 #endif
185 
186