1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2011 Amaury Pouly
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
31 /* All fields are in big-endian BCD */
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 */
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
));
65 struct sb_section_header_t
68 uint32_t offset
; /* In blocks */
69 uint32_t size
; /* In blocks */
71 } __attribute__((packed
));
73 struct sb_key_dictionary_entry_t
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
));
79 #define IMAGE_MAJOR_VERSION 1
80 #define IMAGE_MINOR_VERSION 1
82 #define SECTION_BOOTABLE (1 << 0)
83 #define SECTION_CLEARTEXT (1 << 1)
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
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 */
101 struct sb_instruction_header_t
106 } __attribute__((packed
));
108 struct sb_instruction_common_t
110 struct sb_instruction_header_t hdr
;
114 } __attribute__((packed
));
116 struct sb_instruction_load_t
118 struct sb_instruction_header_t hdr
;
122 } __attribute__((packed
));
124 struct sb_instruction_fill_t
126 struct sb_instruction_header_t hdr
;
130 } __attribute__((packed
));
132 struct sb_instruction_mode_t
134 struct sb_instruction_header_t hdr
;
138 } __attribute__((packed
));
140 struct sb_instruction_call_t
142 struct sb_instruction_header_t hdr
;
146 } __attribute__((packed
));
148 struct sb_instruction_tag_t
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
));
160 #define SB_INST_DATA 0xff
164 uint8_t inst
; /* SB_INST_* */
171 uint32_t argument
; // for call, jump and mode
172 /* for production use */
173 uint32_t padding_size
;
183 // data sections are handled as one or more SB_INST_DATA virtual instruction
185 struct sb_inst_t
*insts
;
186 /* for production use */
187 uint32_t file_offset
; /* in blocks */
188 uint32_t sec_size
; /* in blocks */
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];
202 uint32_t first_boot_sec_id
;
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 */
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
,
226 enum sb_error_t
sb_write_file(struct sb_file_t
*sb
, const char *filename
);
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
);
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_instruction(struct sb_inst_t inst
);
235 void sb_free_section(struct sb_section_t file
);
236 void sb_free(struct sb_file_t
*file
);
238 #endif /* __SB_H__ */