diff options
Diffstat (limited to 'utils/imxtools/sb.h')
-rw-r--r-- | utils/imxtools/sb.h | 237 |
1 files changed, 237 insertions, 0 deletions
diff --git a/utils/imxtools/sb.h b/utils/imxtools/sb.h new file mode 100644 index 0000000000..ced6481a80 --- /dev/null +++ b/utils/imxtools/sb.h | |||
@@ -0,0 +1,237 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2011 Amaury Pouly | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | #ifndef __SB_H__ | ||
22 | #define __SB_H__ | ||
23 | |||
24 | #include <stdint.h> | ||
25 | #include <stdbool.h> | ||
26 | |||
27 | #include "misc.h" | ||
28 | |||
29 | #define BLOCK_SIZE 16 | ||
30 | |||
31 | /* All fields are in big-endian BCD */ | ||
32 | struct sb_version_t | ||
33 | { | ||
34 | uint16_t major; | ||
35 | uint16_t pad0; | ||
36 | uint16_t minor; | ||
37 | uint16_t pad1; | ||
38 | uint16_t revision; | ||
39 | uint16_t pad2; | ||
40 | }; | ||
41 | |||
42 | struct sb_header_t | ||
43 | { | ||
44 | uint8_t sha1_header[20]; /* SHA-1 of the rest of the header */ | ||
45 | uint8_t signature[4]; /* Signature "STMP" */ | ||
46 | uint8_t major_ver; /* Should be 1 */ | ||
47 | uint8_t minor_ver; /* Should be 1 */ | ||
48 | uint16_t flags; | ||
49 | uint32_t image_size; /* In blocks (=16bytes) */ | ||
50 | uint32_t first_boot_tag_off; /* Offset in blocks */ | ||
51 | uint32_t first_boot_sec_id; /* First bootable section ID */ | ||
52 | uint16_t nr_keys; /* Number of encryption keys */ | ||
53 | uint16_t key_dict_off; /* Offset to key dictionary (in blocks) */ | ||
54 | uint16_t header_size; /* In blocks */ | ||
55 | uint16_t nr_sections; /* Number of sections */ | ||
56 | uint16_t sec_hdr_size; /* Section header size (in blocks) */ | ||
57 | uint8_t rand_pad0[6]; /* Random padding */ | ||
58 | uint64_t timestamp; /* In microseconds since 2000/1/1 00:00:00 */ | ||
59 | struct sb_version_t product_ver; | ||
60 | struct sb_version_t component_ver; | ||
61 | uint16_t drive_tag; /* first tag to boot ? */ | ||
62 | uint8_t rand_pad1[6]; /* Random padding */ | ||
63 | } __attribute__((packed)); | ||
64 | |||
65 | struct sb_section_header_t | ||
66 | { | ||
67 | uint32_t identifier; | ||
68 | uint32_t offset; /* In blocks */ | ||
69 | uint32_t size; /* In blocks */ | ||
70 | uint32_t flags; | ||
71 | } __attribute__((packed)); | ||
72 | |||
73 | struct sb_key_dictionary_entry_t | ||
74 | { | ||
75 | uint8_t hdr_cbc_mac[16]; /* CBC-MAC of the header */ | ||
76 | uint8_t key[16]; /* Actual AES Key (encrypted by the global key) */ | ||
77 | } __attribute__((packed)); | ||
78 | |||
79 | #define IMAGE_MAJOR_VERSION 1 | ||
80 | #define IMAGE_MINOR_VERSION 1 | ||
81 | |||
82 | #define SECTION_BOOTABLE (1 << 0) | ||
83 | #define SECTION_CLEARTEXT (1 << 1) | ||
84 | |||
85 | #define SB_INST_NOP 0x0 | ||
86 | #define SB_INST_TAG 0x1 | ||
87 | #define SB_INST_LOAD 0x2 | ||
88 | #define SB_INST_FILL 0x3 | ||
89 | #define SB_INST_JUMP 0x4 | ||
90 | #define SB_INST_CALL 0x5 | ||
91 | #define SB_INST_MODE 0x6 | ||
92 | |||
93 | /* flags */ | ||
94 | #define SB_INST_LAST_TAG 1 /* for TAG */ | ||
95 | #define SB_INST_LOAD_DCD 1 /* for LOAD */ | ||
96 | #define SB_INST_FILL_BYTE 0 /* for FILL */ | ||
97 | #define SB_INST_FILL_HWORD 1 /* for FILL */ | ||
98 | #define SB_INST_FILL_WORD 2 /* for FILL */ | ||
99 | #define SB_INST_HAB_EXEC 1 /* for JUMP/CALL */ | ||
100 | |||
101 | struct sb_instruction_header_t | ||
102 | { | ||
103 | uint8_t checksum; | ||
104 | uint8_t opcode; | ||
105 | uint16_t flags; | ||
106 | } __attribute__((packed)); | ||
107 | |||
108 | struct sb_instruction_common_t | ||
109 | { | ||
110 | struct sb_instruction_header_t hdr; | ||
111 | uint32_t addr; | ||
112 | uint32_t len; | ||
113 | uint32_t data; | ||
114 | } __attribute__((packed)); | ||
115 | |||
116 | struct sb_instruction_load_t | ||
117 | { | ||
118 | struct sb_instruction_header_t hdr; | ||
119 | uint32_t addr; | ||
120 | uint32_t len; | ||
121 | uint32_t crc; | ||
122 | } __attribute__((packed)); | ||
123 | |||
124 | struct sb_instruction_fill_t | ||
125 | { | ||
126 | struct sb_instruction_header_t hdr; | ||
127 | uint32_t addr; | ||
128 | uint32_t len; | ||
129 | uint32_t pattern; | ||
130 | } __attribute__((packed)); | ||
131 | |||
132 | struct sb_instruction_mode_t | ||
133 | { | ||
134 | struct sb_instruction_header_t hdr; | ||
135 | uint32_t zero1; | ||
136 | uint32_t zero2; | ||
137 | uint32_t mode; | ||
138 | } __attribute__((packed)); | ||
139 | |||
140 | struct sb_instruction_call_t | ||
141 | { | ||
142 | struct sb_instruction_header_t hdr; | ||
143 | uint32_t addr; | ||
144 | uint32_t zero; | ||
145 | uint32_t arg; | ||
146 | } __attribute__((packed)); | ||
147 | |||
148 | struct sb_instruction_tag_t | ||
149 | { | ||
150 | struct sb_instruction_header_t hdr; | ||
151 | uint32_t identifier; /* section identifier */ | ||
152 | uint32_t len; /* length of the section */ | ||
153 | uint32_t flags; /* section flags */ | ||
154 | } __attribute__((packed)); | ||
155 | |||
156 | /******* | ||
157 | * API * | ||
158 | *******/ | ||
159 | |||
160 | #define SB_INST_DATA 0xff | ||
161 | |||
162 | struct sb_inst_t | ||
163 | { | ||
164 | uint8_t inst; /* SB_INST_* */ | ||
165 | uint32_t size; | ||
166 | uint32_t addr; | ||
167 | // <union> | ||
168 | void *data; | ||
169 | uint32_t pattern; | ||
170 | // </union> | ||
171 | uint32_t argument; // for call, jump and mode | ||
172 | /* for production use */ | ||
173 | uint32_t padding_size; | ||
174 | uint8_t *padding; | ||
175 | }; | ||
176 | |||
177 | struct sb_section_t | ||
178 | { | ||
179 | uint32_t identifier; | ||
180 | bool is_data; | ||
181 | bool is_cleartext; | ||
182 | uint32_t alignment; | ||
183 | // data sections are handled as one or more SB_INST_DATA virtual instruction | ||
184 | int nr_insts; | ||
185 | struct sb_inst_t *insts; | ||
186 | /* for production use */ | ||
187 | uint32_t file_offset; /* in blocks */ | ||
188 | uint32_t sec_size; /* in blocks */ | ||
189 | }; | ||
190 | |||
191 | struct sb_file_t | ||
192 | { | ||
193 | /* override real, otherwise it is randomly generated */ | ||
194 | bool override_real_key; | ||
195 | uint8_t real_key[16]; | ||
196 | /* override crypto IV, use with caution ! Use NULL to generate it */ | ||
197 | bool override_crypto_iv; | ||
198 | uint8_t crypto_iv[16]; | ||
199 | |||
200 | int nr_sections; | ||
201 | uint16_t drive_tag; | ||
202 | uint32_t first_boot_sec_id; | ||
203 | uint16_t flags; | ||
204 | uint8_t minor_version; | ||
205 | struct sb_section_t *sections; | ||
206 | struct sb_version_t product_ver; | ||
207 | struct sb_version_t component_ver; | ||
208 | /* for production use */ | ||
209 | uint32_t image_size; /* in blocks */ | ||
210 | }; | ||
211 | |||
212 | enum sb_error_t | ||
213 | { | ||
214 | SB_SUCCESS = 0, | ||
215 | SB_ERROR = -1, | ||
216 | SB_OPEN_ERROR = -2, | ||
217 | SB_READ_ERROR = -3, | ||
218 | SB_WRITE_ERROR = -4, | ||
219 | SB_FORMAT_ERROR = -5, | ||
220 | SB_CHECKSUM_ERROR = -6, | ||
221 | SB_NO_VALID_KEY = -7, | ||
222 | SB_FIRST_CRYPTO_ERROR = -8, | ||
223 | SB_LAST_CRYPTO_ERROR = SB_FIRST_CRYPTO_ERROR - CRYPTO_NUM_ERRORS, | ||
224 | }; | ||
225 | |||
226 | enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename); | ||
227 | |||
228 | typedef void (*sb_color_printf)(void *u, bool err, color_t c, const char *f, ...); | ||
229 | struct sb_file_t *sb_read_file(const char *filename, bool raw_mode, void *u, | ||
230 | sb_color_printf printf, enum sb_error_t *err); | ||
231 | |||
232 | void sb_fill_section_name(char name[5], uint32_t identifier); | ||
233 | void sb_dump(struct sb_file_t *file, void *u, sb_color_printf printf); | ||
234 | void sb_free_section(struct sb_section_t file); | ||
235 | void sb_free(struct sb_file_t *file); | ||
236 | |||
237 | #endif /* __SB_H__ */ | ||