Amaury Pouly | d9b050e | 2011-04-16 18:22:37 +0000 | [diff] [blame] | 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 | ****************************************************************************/ |
Amaury Pouly | 9d7df9a | 2011-09-15 16:10:31 +0000 | [diff] [blame] | 21 | #ifndef __SB_H__ |
| 22 | #define __SB_H__ |
Amaury Pouly | d9b050e | 2011-04-16 18:22:37 +0000 | [diff] [blame] | 23 | |
Amaury Pouly | e2f8fbf | 2011-04-17 01:43:48 +0000 | [diff] [blame] | 24 | #include <stdint.h> |
Amaury Pouly | d2a58f3 | 2011-10-29 17:01:47 +0000 | [diff] [blame] | 25 | #include <stdbool.h> |
Amaury Pouly | d9b050e | 2011-04-16 18:22:37 +0000 | [diff] [blame] | 26 | |
Amaury Pouly | e36471d | 2011-11-01 11:23:43 +0000 | [diff] [blame] | 27 | #include "misc.h" |
| 28 | |
Amaury Pouly | e4dd514 | 2011-04-16 18:22:42 +0000 | [diff] [blame] | 29 | #define BLOCK_SIZE 16 |
| 30 | |
Amaury Pouly | de13803 | 2011-07-24 19:12:18 +0000 | [diff] [blame] | 31 | /* All fields are in big-endian BCD */ |
Amaury Pouly | d9b050e | 2011-04-16 18:22:37 +0000 | [diff] [blame] | 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; |
Amaury Pouly | 8a8d77b | 2011-04-17 18:37:23 +0000 | [diff] [blame] | 61 | uint16_t drive_tag; /* first tag to boot ? */ |
Amaury Pouly | d9b050e | 2011-04-16 18:22:37 +0000 | [diff] [blame] | 62 | uint8_t rand_pad1[6]; /* Random padding */ |
| 63 | } __attribute__((packed)); |
| 64 | |
Amaury Pouly | e4dd514 | 2011-04-16 18:22:42 +0000 | [diff] [blame] | 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 | |
Amaury Pouly | 7098566 | 2011-04-17 01:43:30 +0000 | [diff] [blame] | 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) |
Amaury Pouly | e4dd514 | 2011-04-16 18:22:42 +0000 | [diff] [blame] | 84 | |
Amaury Pouly | d9b050e | 2011-04-16 18:22:37 +0000 | [diff] [blame] | 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 | |
Amaury Pouly | 8a8d77b | 2011-04-17 18:37:23 +0000 | [diff] [blame] | 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 | |
Amaury Pouly | d9b050e | 2011-04-16 18:22:37 +0000 | [diff] [blame] | 101 | struct sb_instruction_header_t |
| 102 | { |
| 103 | uint8_t checksum; |
| 104 | uint8_t opcode; |
Amaury Pouly | 8a8d77b | 2011-04-17 18:37:23 +0000 | [diff] [blame] | 105 | uint16_t flags; |
Amaury Pouly | d9b050e | 2011-04-16 18:22:37 +0000 | [diff] [blame] | 106 | } __attribute__((packed)); |
| 107 | |
Amaury Pouly | b2c5954 | 2011-04-17 22:30:09 +0000 | [diff] [blame] | 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 | |
Amaury Pouly | d9b050e | 2011-04-16 18:22:37 +0000 | [diff] [blame] | 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 | |
Amaury Pouly | 50be1a8 | 2011-06-13 21:46:29 +0000 | [diff] [blame] | 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 | |
Amaury Pouly | d9b050e | 2011-04-16 18:22:37 +0000 | [diff] [blame] | 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)); |
Amaury Pouly | 8a8d77b | 2011-04-17 18:37:23 +0000 | [diff] [blame] | 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)); |
Amaury Pouly | 9d7df9a | 2011-09-15 16:10:31 +0000 | [diff] [blame] | 155 | |
Amaury Pouly | d2a58f3 | 2011-10-29 17:01:47 +0000 | [diff] [blame] | 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; |
Amaury Pouly | e36471d | 2011-11-01 11:23:43 +0000 | [diff] [blame] | 166 | uint32_t addr; |
Amaury Pouly | d2a58f3 | 2011-10-29 17:01:47 +0000 | [diff] [blame] | 167 | // <union> |
| 168 | void *data; |
| 169 | uint32_t pattern; |
Amaury Pouly | d2a58f3 | 2011-10-29 17:01:47 +0000 | [diff] [blame] | 170 | // </union> |
Amaury Pouly | e36471d | 2011-11-01 11:23:43 +0000 | [diff] [blame] | 171 | uint32_t argument; // for call, jump and mode |
Amaury Pouly | d2a58f3 | 2011-10-29 17:01:47 +0000 | [diff] [blame] | 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 */ |
Amaury Pouly | 5827937 | 2011-11-06 01:49:13 +0000 | [diff] [blame] | 194 | bool override_real_key; |
| 195 | uint8_t real_key[16]; |
Amaury Pouly | d2a58f3 | 2011-10-29 17:01:47 +0000 | [diff] [blame] | 196 | /* override crypto IV, use with caution ! Use NULL to generate it */ |
Amaury Pouly | 5827937 | 2011-11-06 01:49:13 +0000 | [diff] [blame] | 197 | bool override_crypto_iv; |
| 198 | uint8_t crypto_iv[16]; |
Amaury Pouly | d2a58f3 | 2011-10-29 17:01:47 +0000 | [diff] [blame] | 199 | |
| 200 | int nr_sections; |
Amaury Pouly | e36471d | 2011-11-01 11:23:43 +0000 | [diff] [blame] | 201 | uint16_t drive_tag; |
| 202 | uint32_t first_boot_sec_id; |
| 203 | uint16_t flags; |
| 204 | uint8_t minor_version; |
Amaury Pouly | d2a58f3 | 2011-10-29 17:01:47 +0000 | [diff] [blame] | 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 | |
Amaury Pouly | 5827937 | 2011-11-06 01:49:13 +0000 | [diff] [blame] | 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); |
Amaury Pouly | e36471d | 2011-11-01 11:23:43 +0000 | [diff] [blame] | 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, |
Amaury Pouly | 5827937 | 2011-11-06 01:49:13 +0000 | [diff] [blame] | 230 | sb_color_printf printf, enum sb_error_t *err); |
Amaury Pouly | e36471d | 2011-11-01 11:23:43 +0000 | [diff] [blame] | 231 | |
Amaury Pouly | dd0fffe | 2011-11-01 11:26:16 +0000 | [diff] [blame] | 232 | void sb_fill_section_name(char name[5], uint32_t identifier); |
Amaury Pouly | e36471d | 2011-11-01 11:23:43 +0000 | [diff] [blame] | 233 | void sb_dump(struct sb_file_t *file, void *u, sb_color_printf printf); |
Amaury Pouly | cd832bd | 2011-11-06 19:41:29 +0000 | [diff] [blame] | 234 | void sb_free_instruction(struct sb_inst_t inst); |
Amaury Pouly | 5827937 | 2011-11-06 01:49:13 +0000 | [diff] [blame] | 235 | void sb_free_section(struct sb_section_t file); |
| 236 | void sb_free(struct sb_file_t *file); |
Amaury Pouly | d2a58f3 | 2011-10-29 17:01:47 +0000 | [diff] [blame] | 237 | |
Amaury Pouly | 9d7df9a | 2011-09-15 16:10:31 +0000 | [diff] [blame] | 238 | #endif /* __SB_H__ */ |