1 // SPDX-License-Identifier: GPL-2.0+
3 * Freescale i.MX23/i.MX28 SB image generator
5 * Copyright (C) 2012-2013 Marek Vasut <marex@denx.de>
17 #include <openssl/evp.h>
19 #include "imagetool.h"
21 #include "pbl_crc32.h"
25 * OpenSSL 1.1.0 and newer compatibility functions:
26 * https://wiki.openssl.org/index.php/1.1_API_Changes
28 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
29 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
30 static void *OPENSSL_zalloc(size_t num)
32 void *ret = OPENSSL_malloc(num);
39 EVP_MD_CTX *EVP_MD_CTX_new(void)
41 return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
44 void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
46 EVP_MD_CTX_cleanup(ctx);
50 int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx)
52 return EVP_CIPHER_CTX_cleanup(ctx);
58 * |-Write to address command block
61 * |-ORR address with mask command block
63 * |-Write to address command block
67 #define SB_HAB_DCD_WRITE 0xccUL
68 #define SB_HAB_DCD_CHECK 0xcfUL
69 #define SB_HAB_DCD_NOOP 0xc0UL
70 #define SB_HAB_DCD_MASK_BIT (1 << 3)
71 #define SB_HAB_DCD_SET_BIT (1 << 4)
73 /* Addr.n = Value.n */
74 #define SB_DCD_WRITE \
75 (SB_HAB_DCD_WRITE << 24)
76 /* Addr.n &= ~Value.n */
78 ((SB_HAB_DCD_WRITE << 24) | SB_HAB_DCD_SET_BIT)
79 /* Addr.n |= Value.n */
81 ((SB_HAB_DCD_WRITE << 24) | SB_HAB_DCD_SET_BIT | SB_HAB_DCD_MASK_BIT)
82 /* (Addr.n & Value.n) == 0 */
83 #define SB_DCD_CHK_EQZ \
84 (SB_HAB_DCD_CHECK << 24)
85 /* (Addr.n & Value.n) == Value.n */
86 #define SB_DCD_CHK_EQ \
87 ((SB_HAB_DCD_CHECK << 24) | SB_HAB_DCD_SET_BIT)
88 /* (Addr.n & Value.n) != Value.n */
89 #define SB_DCD_CHK_NEQ \
90 ((SB_HAB_DCD_CHECK << 24) | SB_HAB_DCD_MASK_BIT)
91 /* (Addr.n & Value.n) != 0 */
92 #define SB_DCD_CHK_NEZ \
93 ((SB_HAB_DCD_CHECK << 24) | SB_HAB_DCD_SET_BIT | SB_HAB_DCD_MASK_BIT)
96 (SB_HAB_DCD_NOOP << 24)
99 struct sb_dcd_ctx *dcd;
105 /* Size of the whole DCD block. */
108 /* Pointer to previous DCD command block. */
109 uint32_t *prev_dcd_head;
131 struct sb_cmd_ctx *cmd;
136 struct sb_command payload;
137 struct sb_command c_payload;
140 struct sb_section_ctx {
146 struct sb_section_ctx *sect;
148 struct sb_cmd_ctx *cmd_head;
149 struct sb_cmd_ctx *cmd_tail;
151 struct sb_sections_header payload;
154 struct sb_image_ctx {
155 unsigned int in_section:1;
156 unsigned int in_dcd:1;
157 /* Image configuration */
158 unsigned int display_progress:1;
159 unsigned int silent_dump:1;
160 char *input_filename;
161 char *output_filename;
163 uint8_t image_key[16];
165 /* Number of section in the image */
166 unsigned int sect_count;
167 /* Bootable section */
168 unsigned int sect_boot;
169 unsigned int sect_boot_found:1;
171 struct sb_section_ctx *sect_head;
172 struct sb_section_ctx *sect_tail;
174 struct sb_dcd_ctx *dcd_head;
175 struct sb_dcd_ctx *dcd_tail;
177 EVP_CIPHER_CTX *cipher_ctx;
180 struct sb_key_dictionary_key sb_dict_key;
182 struct sb_boot_image_header payload;
186 * Instruction semantics:
190 * LOAD IVT address IVT_entry_point
191 * FILL address pattern length
192 * JUMP [HAB] address [r0_arg]
193 * CALL [HAB] address [r0_arg]
195 * For i.MX23, mode = USB/I2C/SPI1_FLASH/SPI2_FLASH/NAND_BCH
196 * JTAG/SPI3_EEPROM/SD_SSP0/SD_SSP1
197 * For i.MX28, mode = USB/I2C/SPI2_FLASH/SPI3_FLASH/NAND_BCH
198 * JTAG/SPI2_EEPROM/SD_SSP0/SD_SSP1
204 static int sb_aes_init(struct sb_image_ctx *ictx, uint8_t *iv, int enc)
209 /* If there is no init vector, init vector is all zeroes. */
211 iv = ictx->image_key;
213 ctx = EVP_CIPHER_CTX_new();
214 ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), ictx->image_key, iv, enc);
216 EVP_CIPHER_CTX_set_padding(ctx, 0);
217 ictx->cipher_ctx = ctx;
222 static int sb_aes_crypt(struct sb_image_ctx *ictx, uint8_t *in_data,
223 uint8_t *out_data, int in_len)
225 EVP_CIPHER_CTX *ctx = ictx->cipher_ctx;
229 outbuf = malloc(in_len);
232 memset(outbuf, 0, sizeof(in_len));
234 ret = EVP_CipherUpdate(ctx, outbuf, &outlen, in_data, in_len);
241 memcpy(out_data, outbuf, outlen);
248 static int sb_aes_deinit(EVP_CIPHER_CTX *ctx)
250 return EVP_CIPHER_CTX_reset(ctx);
253 static int sb_aes_reinit(struct sb_image_ctx *ictx, int enc)
256 EVP_CIPHER_CTX *ctx = ictx->cipher_ctx;
257 struct sb_boot_image_header *sb_header = &ictx->payload;
258 uint8_t *iv = sb_header->iv;
260 ret = sb_aes_deinit(ctx);
263 return sb_aes_init(ictx, iv, enc);
269 static void soprintf(struct sb_image_ctx *ictx, const char *fmt, ...)
273 if (ictx->silent_dump)
277 vfprintf(stdout, fmt, ap);
284 static time_t sb_get_timestamp(void)
286 struct tm time_2000 = {
287 .tm_yday = 1, /* Jan. 1st */
288 .tm_year = 100, /* 2000 */
290 time_t seconds_to_2000 = mktime(&time_2000);
291 time_t seconds_to_now = time(NULL);
293 return seconds_to_now - seconds_to_2000;
296 static int sb_get_time(time_t time, struct tm *tm)
298 struct tm time_2000 = {
299 .tm_yday = 1, /* Jan. 1st */
300 .tm_year = 0, /* 1900 */
302 const time_t seconds_to_2000 = mktime(&time_2000);
303 const time_t seconds_to_now = seconds_to_2000 + time;
305 ret = gmtime_r(&seconds_to_now, tm);
306 return ret ? 0 : -EINVAL;
309 static void sb_encrypt_sb_header(struct sb_image_ctx *ictx)
311 EVP_MD_CTX *md_ctx = ictx->md_ctx;
312 struct sb_boot_image_header *sb_header = &ictx->payload;
313 uint8_t *sb_header_ptr = (uint8_t *)sb_header;
315 /* Encrypt the header, compute the digest. */
316 sb_aes_crypt(ictx, sb_header_ptr, NULL, sizeof(*sb_header));
317 EVP_DigestUpdate(md_ctx, sb_header_ptr, sizeof(*sb_header));
320 static void sb_encrypt_sb_sections_header(struct sb_image_ctx *ictx)
322 EVP_MD_CTX *md_ctx = ictx->md_ctx;
323 struct sb_section_ctx *sctx = ictx->sect_head;
324 struct sb_sections_header *shdr;
325 uint8_t *sb_sections_header_ptr;
326 const int size = sizeof(*shdr);
329 shdr = &sctx->payload;
330 sb_sections_header_ptr = (uint8_t *)shdr;
332 sb_aes_crypt(ictx, sb_sections_header_ptr,
333 ictx->sb_dict_key.cbc_mac, size);
334 EVP_DigestUpdate(md_ctx, sb_sections_header_ptr, size);
340 static void sb_encrypt_key_dictionary_key(struct sb_image_ctx *ictx)
342 EVP_MD_CTX *md_ctx = ictx->md_ctx;
344 sb_aes_crypt(ictx, ictx->image_key, ictx->sb_dict_key.key,
345 sizeof(ictx->sb_dict_key.key));
346 EVP_DigestUpdate(md_ctx, &ictx->sb_dict_key, sizeof(ictx->sb_dict_key));
349 static void sb_decrypt_key_dictionary_key(struct sb_image_ctx *ictx)
351 EVP_MD_CTX *md_ctx = ictx->md_ctx;
353 EVP_DigestUpdate(md_ctx, &ictx->sb_dict_key, sizeof(ictx->sb_dict_key));
354 sb_aes_crypt(ictx, ictx->sb_dict_key.key, ictx->image_key,
355 sizeof(ictx->sb_dict_key.key));
358 static void sb_encrypt_tag(struct sb_image_ctx *ictx,
359 struct sb_cmd_ctx *cctx)
361 EVP_MD_CTX *md_ctx = ictx->md_ctx;
362 struct sb_command *cmd = &cctx->payload;
364 sb_aes_crypt(ictx, (uint8_t *)cmd,
365 (uint8_t *)&cctx->c_payload, sizeof(*cmd));
366 EVP_DigestUpdate(md_ctx, &cctx->c_payload, sizeof(*cmd));
369 static int sb_encrypt_image(struct sb_image_ctx *ictx)
371 /* Start image-wide crypto. */
372 ictx->md_ctx = EVP_MD_CTX_new();
373 EVP_DigestInit(ictx->md_ctx, EVP_sha1());
378 sb_aes_init(ictx, NULL, 1);
379 sb_encrypt_sb_header(ictx);
382 * SB sections header.
384 sb_encrypt_sb_sections_header(ictx);
389 sb_aes_reinit(ictx, 1);
390 sb_encrypt_key_dictionary_key(ictx);
395 struct sb_cmd_ctx *cctx;
396 struct sb_command *ccmd;
397 struct sb_section_ctx *sctx = ictx->sect_head;
400 cctx = sctx->cmd_head;
402 sb_aes_reinit(ictx, 1);
405 ccmd = &cctx->payload;
407 sb_encrypt_tag(ictx, cctx);
409 if (ccmd->header.tag == ROM_TAG_CMD) {
410 sb_aes_reinit(ictx, 1);
411 } else if (ccmd->header.tag == ROM_LOAD_CMD) {
412 sb_aes_crypt(ictx, cctx->data, cctx->data,
414 EVP_DigestUpdate(ictx->md_ctx, cctx->data,
425 * Dump the SHA1 of the whole image.
427 sb_aes_reinit(ictx, 1);
429 EVP_DigestFinal(ictx->md_ctx, ictx->digest, NULL);
430 EVP_MD_CTX_free(ictx->md_ctx);
431 sb_aes_crypt(ictx, ictx->digest, ictx->digest, sizeof(ictx->digest));
433 /* Stop the encryption session. */
434 sb_aes_deinit(ictx->cipher_ctx);
439 static int sb_load_file(struct sb_cmd_ctx *cctx, char *filename)
441 long real_size, roundup_size;
448 fprintf(stderr, "ERR: Missing filename!\n");
452 fp = fopen(filename, "r");
456 ret = fseek(fp, 0, SEEK_END);
460 real_size = ftell(fp);
464 ret = fseek(fp, 0, SEEK_SET);
468 roundup_size = roundup(real_size, SB_BLOCK_SIZE);
469 data = calloc(1, roundup_size);
473 size = fread(data, 1, real_size, fp);
474 if (size != (unsigned long)real_size)
478 cctx->length = roundup_size;
488 fprintf(stderr, "ERR: Failed to load file \"%s\"\n", filename);
492 static uint8_t sb_command_checksum(struct sb_command *inst)
494 uint8_t *inst_ptr = (uint8_t *)inst;
498 for (i = 0; i < sizeof(struct sb_command); i++)
504 static int sb_token_to_long(char *tok, uint32_t *rid)
509 if (tok[0] != '0' || tok[1] != 'x') {
510 fprintf(stderr, "ERR: Invalid hexadecimal number!\n");
517 id = strtoul(tok, &endptr, 16);
518 if ((errno == ERANGE && id == ULONG_MAX) || (errno != 0 && id == 0)) {
519 fprintf(stderr, "ERR: Value can't be decoded!\n");
523 /* Check for 32-bit overflow. */
524 if (id > 0xffffffff) {
525 fprintf(stderr, "ERR: Value too big!\n");
530 fprintf(stderr, "ERR: Deformed value!\n");
538 static int sb_grow_dcd(struct sb_dcd_ctx *dctx, unsigned int inc_size)
545 dctx->size += inc_size;
546 tmp = realloc(dctx->payload, dctx->size);
552 /* Assemble and update the HAB DCD header. */
553 dctx->payload[0] = htonl((SB_HAB_DCD_TAG << 24) |
560 static int sb_build_dcd(struct sb_image_ctx *ictx, struct sb_cmd_list *cmd)
562 struct sb_dcd_ctx *dctx;
568 dctx = calloc(1, sizeof(*dctx));
572 ret = sb_grow_dcd(dctx, 4);
576 /* Read DCD block number. */
577 tok = strtok(cmd->cmd, " ");
579 fprintf(stderr, "#%i ERR: DCD block without number!\n",
585 /* Parse the DCD block number. */
586 ret = sb_token_to_long(tok, &id);
588 fprintf(stderr, "#%i ERR: Malformed DCD block number!\n",
596 * The DCD block is now constructed. Append it to the list.
597 * WARNING: The DCD size is still not computed and will be
598 * updated while parsing it's commands.
600 if (!ictx->dcd_head) {
601 ictx->dcd_head = dctx;
602 ictx->dcd_tail = dctx;
604 ictx->dcd_tail->dcd = dctx;
605 ictx->dcd_tail = dctx;
616 static int sb_build_dcd_block(struct sb_image_ctx *ictx,
617 struct sb_cmd_list *cmd,
621 uint32_t address, value, length;
624 struct sb_dcd_ctx *dctx = ictx->dcd_tail;
627 if (dctx->prev_dcd_head && (type != SB_DCD_NOOP) &&
628 ((dctx->prev_dcd_head[0] & 0xff0000ff) == type)) {
629 /* Same instruction as before, just append it. */
630 ret = sb_grow_dcd(dctx, 8);
633 } else if (type == SB_DCD_NOOP) {
634 ret = sb_grow_dcd(dctx, 4);
638 /* Update DCD command block pointer. */
639 dctx->prev_dcd_head = dctx->payload +
640 dctx->size / sizeof(*dctx->payload) - 1;
642 /* NOOP has only 4 bytes and no payload. */
646 * Either a different instruction block started now
647 * or this is the first instruction block.
649 ret = sb_grow_dcd(dctx, 12);
653 /* Update DCD command block pointer. */
654 dctx->prev_dcd_head = dctx->payload +
655 dctx->size / sizeof(*dctx->payload) - 3;
658 dcd = dctx->payload + dctx->size / sizeof(*dctx->payload) - 2;
661 * Prepare the command.
663 tok = strtok(cmd->cmd, " ");
665 fprintf(stderr, "#%i ERR: Missing DCD address!\n",
671 /* Read DCD destination address. */
672 ret = sb_token_to_long(tok, &address);
674 fprintf(stderr, "#%i ERR: Incorrect DCD address!\n",
679 tok = strtok(NULL, " ");
681 fprintf(stderr, "#%i ERR: Missing DCD value!\n",
687 /* Read DCD operation value. */
688 ret = sb_token_to_long(tok, &value);
690 fprintf(stderr, "#%i ERR: Incorrect DCD value!\n",
695 /* Fill in the new DCD entry. */
696 dcd[0] = htonl(address);
697 dcd[1] = htonl(value);
700 /* Update the DCD command block. */
701 length = dctx->size -
702 ((dctx->prev_dcd_head - dctx->payload) *
703 sizeof(*dctx->payload));
704 dctx->prev_dcd_head[0] = htonl(type | (length << 8));
710 static int sb_build_section(struct sb_image_ctx *ictx, struct sb_cmd_list *cmd)
712 struct sb_section_ctx *sctx;
713 struct sb_sections_header *shdr;
715 uint32_t bootable = 0;
719 sctx = calloc(1, sizeof(*sctx));
723 /* Read section number. */
724 tok = strtok(cmd->cmd, " ");
726 fprintf(stderr, "#%i ERR: Section without number!\n",
732 /* Parse the section number. */
733 ret = sb_token_to_long(tok, &id);
735 fprintf(stderr, "#%i ERR: Malformed section number!\n",
740 /* Read section's BOOTABLE flag. */
741 tok = strtok(NULL, " ");
742 if (tok && (strlen(tok) == 8) && !strncmp(tok, "BOOTABLE", 8))
743 bootable = SB_SECTION_FLAG_BOOTABLE;
745 sctx->boot = bootable;
747 shdr = &sctx->payload;
748 shdr->section_number = id;
749 shdr->section_flags = bootable;
752 * The section is now constructed. Append it to the list.
753 * WARNING: The section size is still not computed and will
754 * be updated while parsing it's commands.
758 /* Mark that this section is bootable one. */
760 if (ictx->sect_boot_found) {
762 "#%i WARN: Multiple bootable section!\n",
765 ictx->sect_boot = id;
766 ictx->sect_boot_found = 1;
770 if (!ictx->sect_head) {
771 ictx->sect_head = sctx;
772 ictx->sect_tail = sctx;
774 ictx->sect_tail->sect = sctx;
775 ictx->sect_tail = sctx;
785 static int sb_build_command_nop(struct sb_image_ctx *ictx)
787 struct sb_section_ctx *sctx = ictx->sect_tail;
788 struct sb_cmd_ctx *cctx;
789 struct sb_command *ccmd;
791 cctx = calloc(1, sizeof(*cctx));
795 ccmd = &cctx->payload;
798 * Construct the command.
800 ccmd->header.checksum = 0x5a;
801 ccmd->header.tag = ROM_NOP_CMD;
803 cctx->size = sizeof(*ccmd);
806 * Append the command to the last section.
808 if (!sctx->cmd_head) {
809 sctx->cmd_head = cctx;
810 sctx->cmd_tail = cctx;
812 sctx->cmd_tail->cmd = cctx;
813 sctx->cmd_tail = cctx;
819 static int sb_build_command_tag(struct sb_image_ctx *ictx,
820 struct sb_cmd_list *cmd)
822 struct sb_section_ctx *sctx = ictx->sect_tail;
823 struct sb_cmd_ctx *cctx;
824 struct sb_command *ccmd;
827 cctx = calloc(1, sizeof(*cctx));
831 ccmd = &cctx->payload;
834 * Prepare the command.
836 /* Check for the LAST keyword. */
837 tok = strtok(cmd->cmd, " ");
838 if (tok && !strcmp(tok, "LAST"))
839 ccmd->header.flags = ROM_TAG_CMD_FLAG_ROM_LAST_TAG;
842 * Construct the command.
844 ccmd->header.checksum = 0x5a;
845 ccmd->header.tag = ROM_TAG_CMD;
847 cctx->size = sizeof(*ccmd);
850 * Append the command to the last section.
852 if (!sctx->cmd_head) {
853 sctx->cmd_head = cctx;
854 sctx->cmd_tail = cctx;
856 sctx->cmd_tail->cmd = cctx;
857 sctx->cmd_tail = cctx;
863 static int sb_build_command_load(struct sb_image_ctx *ictx,
864 struct sb_cmd_list *cmd)
866 struct sb_section_ctx *sctx = ictx->sect_tail;
867 struct sb_cmd_ctx *cctx;
868 struct sb_command *ccmd;
870 int ret, is_ivt = 0, is_dcd = 0;
871 uint32_t dest, dcd = 0;
873 cctx = calloc(1, sizeof(*cctx));
877 ccmd = &cctx->payload;
880 * Prepare the command.
882 tok = strtok(cmd->cmd, " ");
884 fprintf(stderr, "#%i ERR: Missing LOAD address or 'IVT'!\n",
890 /* Check for "IVT" flag. */
891 if (!strcmp(tok, "IVT"))
893 if (!strcmp(tok, "DCD"))
895 if (is_ivt || is_dcd) {
896 tok = strtok(NULL, " ");
898 fprintf(stderr, "#%i ERR: Missing LOAD address!\n",
905 /* Read load destination address. */
906 ret = sb_token_to_long(tok, &dest);
908 fprintf(stderr, "#%i ERR: Incorrect LOAD address!\n",
913 /* Read filename or IVT entrypoint or DCD block ID. */
914 tok = strtok(NULL, " ");
917 "#%i ERR: Missing LOAD filename or IVT ep or DCD block ID!\n",
925 struct sb_ivt_header *ivt;
927 ret = sb_token_to_long(tok, &ivtep);
931 "#%i ERR: Incorrect IVT entry point!\n",
936 ivt = calloc(1, sizeof(*ivt));
942 ivt->header = sb_hab_ivt_header();
946 cctx->data = (uint8_t *)ivt;
947 cctx->length = sizeof(*ivt);
949 struct sb_dcd_ctx *dctx = ictx->dcd_head;
953 ret = sb_token_to_long(tok, &dcdid);
957 "#%i ERR: Incorrect DCD block ID!\n",
963 if (dctx->id == dcdid)
969 fprintf(stderr, "#%i ERR: DCD block %08x not found!\n",
974 asize = roundup(dctx->size, SB_BLOCK_SIZE);
975 payload = calloc(1, asize);
981 memcpy(payload, dctx->payload, dctx->size);
983 cctx->data = payload;
984 cctx->length = asize;
986 /* Set the Load DCD flag. */
987 dcd = ROM_LOAD_CMD_FLAG_DCD_LOAD;
989 /* Regular LOAD of a file. */
990 ret = sb_load_file(cctx, tok);
992 fprintf(stderr, "#%i ERR: Cannot load '%s'!\n",
998 if (cctx->length & (SB_BLOCK_SIZE - 1)) {
999 fprintf(stderr, "#%i ERR: Unaligned payload!\n",
1004 * Construct the command.
1006 ccmd->header.checksum = 0x5a;
1007 ccmd->header.tag = ROM_LOAD_CMD;
1008 ccmd->header.flags = dcd;
1010 ccmd->load.address = dest;
1011 ccmd->load.count = cctx->length;
1012 ccmd->load.crc32 = pbl_crc32(0,
1013 (const char *)cctx->data,
1016 cctx->size = sizeof(*ccmd) + cctx->length;
1019 * Append the command to the last section.
1021 if (!sctx->cmd_head) {
1022 sctx->cmd_head = cctx;
1023 sctx->cmd_tail = cctx;
1025 sctx->cmd_tail->cmd = cctx;
1026 sctx->cmd_tail = cctx;
1036 static int sb_build_command_fill(struct sb_image_ctx *ictx,
1037 struct sb_cmd_list *cmd)
1039 struct sb_section_ctx *sctx = ictx->sect_tail;
1040 struct sb_cmd_ctx *cctx;
1041 struct sb_command *ccmd;
1043 uint32_t address, pattern, length;
1046 cctx = calloc(1, sizeof(*cctx));
1050 ccmd = &cctx->payload;
1053 * Prepare the command.
1055 tok = strtok(cmd->cmd, " ");
1057 fprintf(stderr, "#%i ERR: Missing FILL address!\n",
1063 /* Read fill destination address. */
1064 ret = sb_token_to_long(tok, &address);
1066 fprintf(stderr, "#%i ERR: Incorrect FILL address!\n",
1071 tok = strtok(NULL, " ");
1073 fprintf(stderr, "#%i ERR: Missing FILL pattern!\n",
1079 /* Read fill pattern address. */
1080 ret = sb_token_to_long(tok, &pattern);
1082 fprintf(stderr, "#%i ERR: Incorrect FILL pattern!\n",
1087 tok = strtok(NULL, " ");
1089 fprintf(stderr, "#%i ERR: Missing FILL length!\n",
1095 /* Read fill pattern address. */
1096 ret = sb_token_to_long(tok, &length);
1098 fprintf(stderr, "#%i ERR: Incorrect FILL length!\n",
1104 * Construct the command.
1106 ccmd->header.checksum = 0x5a;
1107 ccmd->header.tag = ROM_FILL_CMD;
1109 ccmd->fill.address = address;
1110 ccmd->fill.count = length;
1111 ccmd->fill.pattern = pattern;
1113 cctx->size = sizeof(*ccmd);
1116 * Append the command to the last section.
1118 if (!sctx->cmd_head) {
1119 sctx->cmd_head = cctx;
1120 sctx->cmd_tail = cctx;
1122 sctx->cmd_tail->cmd = cctx;
1123 sctx->cmd_tail = cctx;
1133 static int sb_build_command_jump_call(struct sb_image_ctx *ictx,
1134 struct sb_cmd_list *cmd,
1135 unsigned int is_call)
1137 struct sb_section_ctx *sctx = ictx->sect_tail;
1138 struct sb_cmd_ctx *cctx;
1139 struct sb_command *ccmd;
1141 uint32_t dest, arg = 0x0;
1144 const char *cmdname = is_call ? "CALL" : "JUMP";
1146 cctx = calloc(1, sizeof(*cctx));
1150 ccmd = &cctx->payload;
1153 * Prepare the command.
1155 tok = strtok(cmd->cmd, " ");
1158 "#%i ERR: Missing %s address or 'HAB'!\n",
1159 cmd->lineno, cmdname);
1164 /* Check for "HAB" flag. */
1165 if (!strcmp(tok, "HAB")) {
1166 hab = is_call ? ROM_CALL_CMD_FLAG_HAB : ROM_JUMP_CMD_FLAG_HAB;
1167 tok = strtok(NULL, " ");
1169 fprintf(stderr, "#%i ERR: Missing %s address!\n",
1170 cmd->lineno, cmdname);
1175 /* Read load destination address. */
1176 ret = sb_token_to_long(tok, &dest);
1178 fprintf(stderr, "#%i ERR: Incorrect %s address!\n",
1179 cmd->lineno, cmdname);
1183 tok = strtok(NULL, " ");
1185 ret = sb_token_to_long(tok, &arg);
1188 "#%i ERR: Incorrect %s argument!\n",
1189 cmd->lineno, cmdname);
1195 * Construct the command.
1197 ccmd->header.checksum = 0x5a;
1198 ccmd->header.tag = is_call ? ROM_CALL_CMD : ROM_JUMP_CMD;
1199 ccmd->header.flags = hab;
1201 ccmd->call.address = dest;
1202 ccmd->call.argument = arg;
1204 cctx->size = sizeof(*ccmd);
1207 * Append the command to the last section.
1209 if (!sctx->cmd_head) {
1210 sctx->cmd_head = cctx;
1211 sctx->cmd_tail = cctx;
1213 sctx->cmd_tail->cmd = cctx;
1214 sctx->cmd_tail = cctx;
1224 static int sb_build_command_jump(struct sb_image_ctx *ictx,
1225 struct sb_cmd_list *cmd)
1227 return sb_build_command_jump_call(ictx, cmd, 0);
1230 static int sb_build_command_call(struct sb_image_ctx *ictx,
1231 struct sb_cmd_list *cmd)
1233 return sb_build_command_jump_call(ictx, cmd, 1);
1236 static int sb_build_command_mode(struct sb_image_ctx *ictx,
1237 struct sb_cmd_list *cmd)
1239 struct sb_section_ctx *sctx = ictx->sect_tail;
1240 struct sb_cmd_ctx *cctx;
1241 struct sb_command *ccmd;
1245 uint32_t mode = 0xffffffff;
1247 cctx = calloc(1, sizeof(*cctx));
1251 ccmd = &cctx->payload;
1254 * Prepare the command.
1256 tok = strtok(cmd->cmd, " ");
1258 fprintf(stderr, "#%i ERR: Missing MODE boot mode argument!\n",
1264 for (i = 0; i < ARRAY_SIZE(modetable); i++) {
1265 if (!strcmp(tok, modetable[i].name)) {
1266 mode = modetable[i].mode;
1270 if (!modetable[i].altname)
1273 if (!strcmp(tok, modetable[i].altname)) {
1274 mode = modetable[i].mode;
1279 if (mode == 0xffffffff) {
1280 fprintf(stderr, "#%i ERR: Invalid MODE boot mode argument!\n",
1287 * Construct the command.
1289 ccmd->header.checksum = 0x5a;
1290 ccmd->header.tag = ROM_MODE_CMD;
1292 ccmd->mode.mode = mode;
1294 cctx->size = sizeof(*ccmd);
1297 * Append the command to the last section.
1299 if (!sctx->cmd_head) {
1300 sctx->cmd_head = cctx;
1301 sctx->cmd_tail = cctx;
1303 sctx->cmd_tail->cmd = cctx;
1304 sctx->cmd_tail = cctx;
1314 static int sb_prefill_image_header(struct sb_image_ctx *ictx)
1316 struct sb_boot_image_header *hdr = &ictx->payload;
1318 /* Fill signatures */
1319 memcpy(hdr->signature1, "STMP", 4);
1320 memcpy(hdr->signature2, "sgtl", 4);
1322 /* SB Image version 1.1 */
1323 hdr->major_version = SB_VERSION_MAJOR;
1324 hdr->minor_version = SB_VERSION_MINOR;
1326 /* Boot image major version */
1327 hdr->product_version.major = htons(0x999);
1328 hdr->product_version.minor = htons(0x999);
1329 hdr->product_version.revision = htons(0x999);
1330 /* Boot image major version */
1331 hdr->component_version.major = htons(0x999);
1332 hdr->component_version.minor = htons(0x999);
1333 hdr->component_version.revision = htons(0x999);
1335 /* Drive tag must be 0x0 for i.MX23 */
1338 hdr->header_blocks =
1339 sizeof(struct sb_boot_image_header) / SB_BLOCK_SIZE;
1340 hdr->section_header_size =
1341 sizeof(struct sb_sections_header) / SB_BLOCK_SIZE;
1342 hdr->timestamp_us = sb_get_timestamp() * 1000000;
1344 hdr->flags = ictx->display_progress ?
1345 SB_IMAGE_FLAG_DISPLAY_PROGRESS : 0;
1347 /* FIXME -- We support only default key */
1353 static int sb_postfill_image_header(struct sb_image_ctx *ictx)
1355 struct sb_boot_image_header *hdr = &ictx->payload;
1356 struct sb_section_ctx *sctx = ictx->sect_head;
1357 uint32_t kd_size, sections_blocks;
1360 /* The main SB header size in blocks. */
1361 hdr->image_blocks = hdr->header_blocks;
1363 /* Size of the key dictionary, which has single zero entry. */
1364 kd_size = hdr->key_count * sizeof(struct sb_key_dictionary_key);
1365 hdr->image_blocks += kd_size / SB_BLOCK_SIZE;
1367 /* Now count the payloads. */
1368 hdr->section_count = ictx->sect_count;
1370 hdr->image_blocks += sctx->size / SB_BLOCK_SIZE;
1374 if (!ictx->sect_boot_found) {
1375 fprintf(stderr, "ERR: No bootable section selected!\n");
1378 hdr->first_boot_section_id = ictx->sect_boot;
1380 /* The n * SB section size in blocks. */
1381 sections_blocks = hdr->section_count * hdr->section_header_size;
1382 hdr->image_blocks += sections_blocks;
1384 /* Key dictionary offset. */
1385 hdr->key_dictionary_block = hdr->header_blocks + sections_blocks;
1387 /* Digest of the whole image. */
1388 hdr->image_blocks += 2;
1390 /* Pointer past the dictionary. */
1391 hdr->first_boot_tag_block =
1392 hdr->key_dictionary_block + kd_size / SB_BLOCK_SIZE;
1394 /* Compute header digest. */
1395 md_ctx = EVP_MD_CTX_new();
1397 EVP_DigestInit(md_ctx, EVP_sha1());
1398 EVP_DigestUpdate(md_ctx, hdr->signature1,
1399 sizeof(struct sb_boot_image_header) -
1400 sizeof(hdr->digest));
1401 EVP_DigestFinal(md_ctx, hdr->digest, NULL);
1402 EVP_MD_CTX_free(md_ctx);
1407 static int sb_fixup_sections_and_tags(struct sb_image_ctx *ictx)
1409 /* Fixup the placement of sections. */
1410 struct sb_boot_image_header *ihdr = &ictx->payload;
1411 struct sb_section_ctx *sctx = ictx->sect_head;
1412 struct sb_sections_header *shdr;
1413 struct sb_cmd_ctx *cctx;
1414 struct sb_command *ccmd;
1415 uint32_t offset = ihdr->first_boot_tag_block;
1418 shdr = &sctx->payload;
1420 /* Fill in the section TAG offset. */
1421 shdr->section_offset = offset + 1;
1422 offset += shdr->section_size;
1424 /* Section length is measured from the TAG block. */
1425 shdr->section_size--;
1427 /* Fixup the TAG command. */
1428 cctx = sctx->cmd_head;
1430 ccmd = &cctx->payload;
1431 if (ccmd->header.tag == ROM_TAG_CMD) {
1432 ccmd->tag.section_number = shdr->section_number;
1433 ccmd->tag.section_length = shdr->section_size;
1434 ccmd->tag.section_flags = shdr->section_flags;
1437 /* Update the command checksum. */
1438 ccmd->header.checksum = sb_command_checksum(ccmd);
1449 static int sb_parse_line(struct sb_image_ctx *ictx, struct sb_cmd_list *cmd)
1452 char *line = cmd->cmd;
1456 /* Analyze the identifier on this line first. */
1457 tok = strtok_r(line, " ", &rptr);
1458 if (!tok || (strlen(tok) == 0)) {
1459 fprintf(stderr, "#%i ERR: Invalid line!\n", cmd->lineno);
1465 /* set DISPLAY_PROGRESS flag */
1466 if (!strcmp(tok, "DISPLAYPROGRESS")) {
1467 ictx->display_progress = 1;
1472 if (!strcmp(tok, "DCD")) {
1473 ictx->in_section = 0;
1475 sb_build_dcd(ictx, cmd);
1480 if (!strcmp(tok, "SECTION")) {
1481 ictx->in_section = 1;
1483 sb_build_section(ictx, cmd);
1487 if (!ictx->in_section && !ictx->in_dcd) {
1488 fprintf(stderr, "#%i ERR: Data outside of a section!\n",
1493 if (ictx->in_section) {
1494 /* Section commands */
1495 if (!strcmp(tok, "NOP")) {
1496 ret = sb_build_command_nop(ictx);
1497 } else if (!strcmp(tok, "TAG")) {
1498 ret = sb_build_command_tag(ictx, cmd);
1499 } else if (!strcmp(tok, "LOAD")) {
1500 ret = sb_build_command_load(ictx, cmd);
1501 } else if (!strcmp(tok, "FILL")) {
1502 ret = sb_build_command_fill(ictx, cmd);
1503 } else if (!strcmp(tok, "JUMP")) {
1504 ret = sb_build_command_jump(ictx, cmd);
1505 } else if (!strcmp(tok, "CALL")) {
1506 ret = sb_build_command_call(ictx, cmd);
1507 } else if (!strcmp(tok, "MODE")) {
1508 ret = sb_build_command_mode(ictx, cmd);
1511 "#%i ERR: Unsupported instruction '%s'!\n",
1515 } else if (ictx->in_dcd) {
1517 uint32_t ilen = '1';
1519 tok = strtok_r(tok, ".", &lptr);
1520 if (!tok || (strlen(tok) == 0) || (lptr && strlen(lptr) != 1)) {
1521 fprintf(stderr, "#%i ERR: Invalid line!\n",
1527 (lptr[0] != '1' && lptr[0] != '2' && lptr[0] != '4')) {
1528 fprintf(stderr, "#%i ERR: Invalid instruction width!\n",
1534 ilen = lptr[0] - '1';
1537 if (!strcmp(tok, "WRITE")) {
1538 ret = sb_build_dcd_block(ictx, cmd,
1539 SB_DCD_WRITE | ilen);
1540 } else if (!strcmp(tok, "ANDC")) {
1541 ret = sb_build_dcd_block(ictx, cmd,
1542 SB_DCD_ANDC | ilen);
1543 } else if (!strcmp(tok, "ORR")) {
1544 ret = sb_build_dcd_block(ictx, cmd,
1546 } else if (!strcmp(tok, "EQZ")) {
1547 ret = sb_build_dcd_block(ictx, cmd,
1548 SB_DCD_CHK_EQZ | ilen);
1549 } else if (!strcmp(tok, "EQ")) {
1550 ret = sb_build_dcd_block(ictx, cmd,
1551 SB_DCD_CHK_EQ | ilen);
1552 } else if (!strcmp(tok, "NEQ")) {
1553 ret = sb_build_dcd_block(ictx, cmd,
1554 SB_DCD_CHK_NEQ | ilen);
1555 } else if (!strcmp(tok, "NEZ")) {
1556 ret = sb_build_dcd_block(ictx, cmd,
1557 SB_DCD_CHK_NEZ | ilen);
1558 } else if (!strcmp(tok, "NOOP")) {
1559 ret = sb_build_dcd_block(ictx, cmd, SB_DCD_NOOP);
1562 "#%i ERR: Unsupported instruction '%s'!\n",
1567 fprintf(stderr, "#%i ERR: Unsupported instruction '%s'!\n",
1573 * Here we have at least one section with one command, otherwise we
1574 * would have failed already higher above.
1576 * FIXME -- should the updating happen here ?
1578 if (ictx->in_section && !ret) {
1579 ictx->sect_tail->size += ictx->sect_tail->cmd_tail->size;
1580 ictx->sect_tail->payload.section_size =
1581 ictx->sect_tail->size / SB_BLOCK_SIZE;
1587 static int sb_load_cmdfile(struct sb_image_ctx *ictx)
1589 struct sb_cmd_list cmd;
1596 fp = fopen(ictx->cfg_filename, "r");
1600 while ((rlen = getline(&line, &len, fp)) > 0) {
1601 memset(&cmd, 0, sizeof(cmd));
1603 /* Strip the trailing newline. */
1604 line[rlen - 1] = '\0';
1608 cmd.lineno = lineno++;
1610 sb_parse_line(ictx, &cmd);
1621 fprintf(stderr, "ERR: Failed to load file \"%s\"\n",
1622 ictx->cfg_filename);
1626 static int sb_build_tree_from_cfg(struct sb_image_ctx *ictx)
1630 ret = sb_load_cmdfile(ictx);
1634 ret = sb_prefill_image_header(ictx);
1638 ret = sb_postfill_image_header(ictx);
1642 ret = sb_fixup_sections_and_tags(ictx);
1649 static int sb_verify_image_header(struct sb_image_ctx *ictx,
1650 FILE *fp, long fsize)
1652 /* Verify static fields in the image header. */
1653 struct sb_boot_image_header *hdr = &ictx->payload;
1654 const char *stat[2] = { "[PASS]", "[FAIL]" };
1657 unsigned char digest[20];
1661 /* Start image-wide crypto. */
1662 ictx->md_ctx = EVP_MD_CTX_new();
1663 EVP_DigestInit(ictx->md_ctx, EVP_sha1());
1665 soprintf(ictx, "---------- Verifying SB Image Header ----------\n");
1667 size = fread(&ictx->payload, 1, sizeof(ictx->payload), fp);
1668 if (size != sizeof(ictx->payload)) {
1669 fprintf(stderr, "ERR: SB image header too short!\n");
1673 /* Compute header digest. */
1674 md_ctx = EVP_MD_CTX_new();
1675 EVP_DigestInit(md_ctx, EVP_sha1());
1676 EVP_DigestUpdate(md_ctx, hdr->signature1,
1677 sizeof(struct sb_boot_image_header) -
1678 sizeof(hdr->digest));
1679 EVP_DigestFinal(md_ctx, digest, NULL);
1680 EVP_MD_CTX_free(md_ctx);
1682 sb_aes_init(ictx, NULL, 1);
1683 sb_encrypt_sb_header(ictx);
1685 if (memcmp(digest, hdr->digest, 20))
1687 soprintf(ictx, "%s Image header checksum: %s\n", stat[!!ret],
1688 ret ? "BAD" : "OK");
1692 if (memcmp(hdr->signature1, "STMP", 4) ||
1693 memcmp(hdr->signature2, "sgtl", 4))
1695 soprintf(ictx, "%s Signatures: '%.4s' '%.4s'\n",
1696 stat[!!ret], hdr->signature1, hdr->signature2);
1700 if ((hdr->major_version != SB_VERSION_MAJOR) ||
1701 ((hdr->minor_version != 1) && (hdr->minor_version != 2)))
1703 soprintf(ictx, "%s Image version: v%i.%i\n", stat[!!ret],
1704 hdr->major_version, hdr->minor_version);
1708 ret = sb_get_time(hdr->timestamp_us / 1000000, &tm);
1710 "%s Creation time: %02i:%02i:%02i %02i/%02i/%04i\n",
1711 stat[!!ret], tm.tm_hour, tm.tm_min, tm.tm_sec,
1712 tm.tm_mday, tm.tm_mon, tm.tm_year + 2000);
1716 soprintf(ictx, "%s Product version: %x.%x.%x\n", stat[0],
1717 ntohs(hdr->product_version.major),
1718 ntohs(hdr->product_version.minor),
1719 ntohs(hdr->product_version.revision));
1720 soprintf(ictx, "%s Component version: %x.%x.%x\n", stat[0],
1721 ntohs(hdr->component_version.major),
1722 ntohs(hdr->component_version.minor),
1723 ntohs(hdr->component_version.revision));
1725 if (hdr->flags & ~SB_IMAGE_FLAGS_MASK)
1727 soprintf(ictx, "%s Image flags: %s\n", stat[!!ret],
1728 hdr->flags & SB_IMAGE_FLAG_DISPLAY_PROGRESS ?
1729 "Display_progress" : "");
1733 if (hdr->drive_tag != 0)
1735 soprintf(ictx, "%s Drive tag: %i\n", stat[!!ret],
1740 sz = sizeof(struct sb_boot_image_header) / SB_BLOCK_SIZE;
1741 if (hdr->header_blocks != sz)
1743 soprintf(ictx, "%s Image header size (blocks): %i\n", stat[!!ret],
1744 hdr->header_blocks);
1748 sz = sizeof(struct sb_sections_header) / SB_BLOCK_SIZE;
1749 if (hdr->section_header_size != sz)
1751 soprintf(ictx, "%s Section header size (blocks): %i\n", stat[!!ret],
1752 hdr->section_header_size);
1756 soprintf(ictx, "%s Sections count: %i\n", stat[!!ret],
1757 hdr->section_count);
1758 soprintf(ictx, "%s First bootable section %i\n", stat[!!ret],
1759 hdr->first_boot_section_id);
1761 if (hdr->image_blocks != fsize / SB_BLOCK_SIZE)
1763 soprintf(ictx, "%s Image size (blocks): %i\n", stat[!!ret],
1768 sz = hdr->header_blocks + hdr->section_header_size * hdr->section_count;
1769 if (hdr->key_dictionary_block != sz)
1771 soprintf(ictx, "%s Key dict offset (blocks): %i\n", stat[!!ret],
1772 hdr->key_dictionary_block);
1776 if (hdr->key_count != 1)
1778 soprintf(ictx, "%s Number of encryption keys: %i\n", stat[!!ret],
1783 sz = hdr->header_blocks + hdr->section_header_size * hdr->section_count;
1784 sz += hdr->key_count *
1785 sizeof(struct sb_key_dictionary_key) / SB_BLOCK_SIZE;
1786 if (hdr->first_boot_tag_block != (unsigned)sz)
1788 soprintf(ictx, "%s First TAG block (blocks): %i\n", stat[!!ret],
1789 hdr->first_boot_tag_block);
1796 static void sb_decrypt_tag(struct sb_image_ctx *ictx,
1797 struct sb_cmd_ctx *cctx)
1799 EVP_MD_CTX *md_ctx = ictx->md_ctx;
1800 struct sb_command *cmd = &cctx->payload;
1802 sb_aes_crypt(ictx, (uint8_t *)&cctx->c_payload,
1803 (uint8_t *)&cctx->payload, sizeof(*cmd));
1804 EVP_DigestUpdate(md_ctx, &cctx->c_payload, sizeof(*cmd));
1807 static int sb_verify_command(struct sb_image_ctx *ictx,
1808 struct sb_cmd_ctx *cctx, FILE *fp,
1809 unsigned long *tsize)
1811 struct sb_command *ccmd = &cctx->payload;
1812 unsigned long size, asize;
1813 char *csum, *flag = "";
1816 uint8_t csn, csc = ccmd->header.checksum;
1817 ccmd->header.checksum = 0x5a;
1818 csn = sb_command_checksum(ccmd);
1819 ccmd->header.checksum = csc;
1825 csum = ret ? "checksum BAD" : "checksum OK";
1827 switch (ccmd->header.tag) {
1829 soprintf(ictx, " NOOP # %s\n", csum);
1832 if (ccmd->header.flags & ROM_TAG_CMD_FLAG_ROM_LAST_TAG)
1834 soprintf(ictx, " TAG %s # %s\n", flag, csum);
1835 sb_aes_reinit(ictx, 0);
1838 soprintf(ictx, " LOAD addr=0x%08x length=0x%08x # %s\n",
1839 ccmd->load.address, ccmd->load.count, csum);
1841 cctx->length = ccmd->load.count;
1842 asize = roundup(cctx->length, SB_BLOCK_SIZE);
1843 cctx->data = malloc(asize);
1847 size = fread(cctx->data, 1, asize, fp);
1848 if (size != asize) {
1850 "ERR: SB LOAD command payload too short!\n");
1856 EVP_DigestUpdate(ictx->md_ctx, cctx->data, asize);
1857 sb_aes_crypt(ictx, cctx->data, cctx->data, asize);
1859 if (ccmd->load.crc32 != pbl_crc32(0,
1860 (const char *)cctx->data,
1863 "ERR: SB LOAD command payload CRC32 invalid!\n");
1869 " FILL addr=0x%08x length=0x%08x pattern=0x%08x # %s\n",
1870 ccmd->fill.address, ccmd->fill.count,
1871 ccmd->fill.pattern, csum);
1874 if (ccmd->header.flags & ROM_JUMP_CMD_FLAG_HAB)
1877 " JUMP%s addr=0x%08x r0_arg=0x%08x # %s\n",
1878 flag, ccmd->fill.address, ccmd->jump.argument, csum);
1881 if (ccmd->header.flags & ROM_CALL_CMD_FLAG_HAB)
1884 " CALL%s addr=0x%08x r0_arg=0x%08x # %s\n",
1885 flag, ccmd->fill.address, ccmd->jump.argument, csum);
1888 for (i = 0; i < ARRAY_SIZE(modetable); i++) {
1889 if (ccmd->mode.mode == modetable[i].mode) {
1890 soprintf(ictx, " MODE %s # %s\n",
1891 modetable[i].name, csum);
1895 fprintf(stderr, " MODE !INVALID! # %s\n", csum);
1902 static int sb_verify_commands(struct sb_image_ctx *ictx,
1903 struct sb_section_ctx *sctx, FILE *fp)
1905 unsigned long size, tsize = 0;
1906 struct sb_cmd_ctx *cctx;
1909 sb_aes_reinit(ictx, 0);
1911 while (tsize < sctx->size) {
1912 cctx = calloc(1, sizeof(*cctx));
1915 if (!sctx->cmd_head) {
1916 sctx->cmd_head = cctx;
1917 sctx->cmd_tail = cctx;
1919 sctx->cmd_tail->cmd = cctx;
1920 sctx->cmd_tail = cctx;
1923 size = fread(&cctx->c_payload, 1, sizeof(cctx->c_payload), fp);
1924 if (size != sizeof(cctx->c_payload)) {
1925 fprintf(stderr, "ERR: SB command header too short!\n");
1931 sb_decrypt_tag(ictx, cctx);
1933 ret = sb_verify_command(ictx, cctx, fp, &tsize);
1941 static int sb_verify_sections_cmds(struct sb_image_ctx *ictx, FILE *fp)
1943 struct sb_boot_image_header *hdr = &ictx->payload;
1944 struct sb_sections_header *shdr;
1947 struct sb_section_ctx *sctx;
1949 char *bootable = "";
1951 soprintf(ictx, "----- Verifying SB Sections and Commands -----\n");
1953 for (i = 0; i < hdr->section_count; i++) {
1954 sctx = calloc(1, sizeof(*sctx));
1957 if (!ictx->sect_head) {
1958 ictx->sect_head = sctx;
1959 ictx->sect_tail = sctx;
1961 ictx->sect_tail->sect = sctx;
1962 ictx->sect_tail = sctx;
1965 size = fread(&sctx->payload, 1, sizeof(sctx->payload), fp);
1966 if (size != sizeof(sctx->payload)) {
1967 fprintf(stderr, "ERR: SB section header too short!\n");
1972 size = fread(&ictx->sb_dict_key, 1, sizeof(ictx->sb_dict_key), fp);
1973 if (size != sizeof(ictx->sb_dict_key)) {
1974 fprintf(stderr, "ERR: SB key dictionary too short!\n");
1978 sb_encrypt_sb_sections_header(ictx);
1979 sb_aes_reinit(ictx, 0);
1980 sb_decrypt_key_dictionary_key(ictx);
1982 sb_aes_reinit(ictx, 0);
1984 sctx = ictx->sect_head;
1986 shdr = &sctx->payload;
1988 if (shdr->section_flags & SB_SECTION_FLAG_BOOTABLE) {
1990 bootable = " BOOTABLE";
1993 sctx->size = (shdr->section_size * SB_BLOCK_SIZE) +
1994 sizeof(struct sb_command);
1995 soprintf(ictx, "SECTION 0x%x%s # size = %i bytes\n",
1996 shdr->section_number, bootable, sctx->size);
1998 if (shdr->section_flags & ~SB_SECTION_FLAG_BOOTABLE)
1999 fprintf(stderr, " WARN: Unknown section flag(s) %08x\n",
2000 shdr->section_flags);
2002 if ((shdr->section_flags & SB_SECTION_FLAG_BOOTABLE) &&
2003 (hdr->first_boot_section_id != shdr->section_number)) {
2005 " WARN: Bootable section does ID not match image header ID!\n");
2008 ret = sb_verify_commands(ictx, sctx, fp);
2017 * check if the first TAG command is at sctx->section_offset
2022 static int sb_verify_image_end(struct sb_image_ctx *ictx,
2023 FILE *fp, off_t filesz)
2030 soprintf(ictx, "------------- Verifying image end -------------\n");
2032 size = fread(digest, 1, sizeof(digest), fp);
2033 if (size != sizeof(digest)) {
2034 fprintf(stderr, "ERR: SB key dictionary too short!\n");
2039 if (pos != filesz) {
2040 fprintf(stderr, "ERR: Trailing data past the image!\n");
2044 /* Check the image digest. */
2045 EVP_DigestFinal(ictx->md_ctx, ictx->digest, NULL);
2046 EVP_MD_CTX_free(ictx->md_ctx);
2048 /* Decrypt the image digest from the input image. */
2049 sb_aes_reinit(ictx, 0);
2050 sb_aes_crypt(ictx, digest, digest, sizeof(digest));
2052 /* Check all of 20 bytes of the SHA1 hash. */
2053 ret = memcmp(digest, ictx->digest, 20) ? -EINVAL : 0;
2056 soprintf(ictx, "[FAIL] Full-image checksum: BAD\n");
2058 soprintf(ictx, "[PASS] Full-image checksum: OK\n");
2064 static int sb_build_tree_from_img(struct sb_image_ctx *ictx)
2070 if (!ictx->input_filename) {
2071 fprintf(stderr, "ERR: Missing filename!\n");
2075 fp = fopen(ictx->input_filename, "r");
2079 ret = fseek(fp, 0, SEEK_END);
2083 filesize = ftell(fp);
2087 ret = fseek(fp, 0, SEEK_SET);
2091 if (filesize < (signed)sizeof(ictx->payload)) {
2092 fprintf(stderr, "ERR: File too short!\n");
2096 if (filesize & (SB_BLOCK_SIZE - 1)) {
2097 fprintf(stderr, "ERR: The file is not aligned!\n");
2101 /* Load and verify image header */
2102 ret = sb_verify_image_header(ictx, fp, filesize);
2106 /* Load and verify sections and commands */
2107 ret = sb_verify_sections_cmds(ictx, fp);
2111 ret = sb_verify_image_end(ictx, fp, filesize);
2118 soprintf(ictx, "-------------------- Result -------------------\n");
2119 soprintf(ictx, "Verification %s\n", ret ? "FAILED" : "PASSED");
2121 /* Stop the encryption session. */
2122 sb_aes_deinit(ictx->cipher_ctx);
2130 fprintf(stderr, "ERR: Failed to load file \"%s\"\n",
2131 ictx->input_filename);
2135 static void sb_free_image(struct sb_image_ctx *ictx)
2137 struct sb_section_ctx *sctx = ictx->sect_head, *s_head;
2138 struct sb_dcd_ctx *dctx = ictx->dcd_head, *d_head;
2139 struct sb_cmd_ctx *cctx, *c_head;
2143 c_head = sctx->cmd_head;
2147 c_head = c_head->cmd;
2160 free(d_head->payload);
2166 * MXSSB-MKIMAGE glue code.
2168 static int mxsimage_check_image_types(uint8_t type)
2170 if (type == IH_TYPE_MXSIMAGE)
2171 return EXIT_SUCCESS;
2173 return EXIT_FAILURE;
2176 static void mxsimage_set_header(void *ptr, struct stat *sbuf, int ifd,
2177 struct image_tool_params *params)
2181 int mxsimage_check_params(struct image_tool_params *params)
2185 if (!strlen(params->imagename)) {
2187 "Error: %s - Configuration file not specified, it is needed for mxsimage generation\n",
2194 * XIP is not allowed and verify that incompatible
2195 * parameters are not sent at the same time
2196 * For example, if list is required a data image must not be provided
2198 return (params->dflag && (params->fflag || params->lflag)) ||
2199 (params->fflag && (params->dflag || params->lflag)) ||
2200 (params->lflag && (params->dflag || params->fflag)) ||
2201 (params->xflag) || !(strlen(params->imagename));
2204 static int mxsimage_verify_print_header(char *file, int silent)
2207 struct sb_image_ctx ctx;
2209 memset(&ctx, 0, sizeof(ctx));
2211 ctx.input_filename = file;
2212 ctx.silent_dump = silent;
2214 ret = sb_build_tree_from_img(&ctx);
2215 sb_free_image(&ctx);
2221 static int mxsimage_verify_header(unsigned char *ptr, int image_size,
2222 struct image_tool_params *params)
2224 struct sb_boot_image_header *hdr;
2229 hdr = (struct sb_boot_image_header *)ptr;
2232 * Check if the header contains the MXS image signatures,
2233 * if so, do a full-image verification.
2235 if (memcmp(hdr->signature1, "STMP", 4) ||
2236 memcmp(hdr->signature2, "sgtl", 4))
2239 imagefile = params->imagefile;
2241 return mxsimage_verify_print_header(params->imagefile, 1);
2244 static void mxsimage_print_header(const void *hdr)
2247 mxsimage_verify_print_header(imagefile, 0);
2250 static int sb_build_image(struct sb_image_ctx *ictx,
2251 struct image_type_params *tparams)
2253 struct sb_boot_image_header *sb_header = &ictx->payload;
2254 struct sb_section_ctx *sctx;
2255 struct sb_cmd_ctx *cctx;
2256 struct sb_command *ccmd;
2257 struct sb_key_dictionary_key *sb_dict_key = &ictx->sb_dict_key;
2259 uint8_t *image, *iptr;
2261 /* Calculate image size. */
2262 uint32_t size = sizeof(*sb_header) +
2263 ictx->sect_count * sizeof(struct sb_sections_header) +
2264 sizeof(*sb_dict_key) + sizeof(ictx->digest);
2266 sctx = ictx->sect_head;
2272 image = malloc(size);
2277 memcpy(iptr, sb_header, sizeof(*sb_header));
2278 iptr += sizeof(*sb_header);
2280 sctx = ictx->sect_head;
2282 memcpy(iptr, &sctx->payload, sizeof(struct sb_sections_header));
2283 iptr += sizeof(struct sb_sections_header);
2287 memcpy(iptr, sb_dict_key, sizeof(*sb_dict_key));
2288 iptr += sizeof(*sb_dict_key);
2290 sctx = ictx->sect_head;
2292 cctx = sctx->cmd_head;
2294 ccmd = &cctx->payload;
2296 memcpy(iptr, &cctx->c_payload, sizeof(cctx->payload));
2297 iptr += sizeof(cctx->payload);
2299 if (ccmd->header.tag == ROM_LOAD_CMD) {
2300 memcpy(iptr, cctx->data, cctx->length);
2301 iptr += cctx->length;
2310 memcpy(iptr, ictx->digest, sizeof(ictx->digest));
2311 iptr += sizeof(ictx->digest);
2313 /* Configure the mkimage */
2314 tparams->hdr = image;
2315 tparams->header_size = size;
2320 static int mxsimage_generate(struct image_tool_params *params,
2321 struct image_type_params *tparams)
2324 struct sb_image_ctx ctx;
2326 /* Do not copy the U-Boot image! */
2327 params->skipcpy = 1;
2329 memset(&ctx, 0, sizeof(ctx));
2331 ctx.cfg_filename = params->imagename;
2332 ctx.output_filename = params->imagefile;
2334 ret = sb_build_tree_from_cfg(&ctx);
2338 ret = sb_encrypt_image(&ctx);
2340 ret = sb_build_image(&ctx, tparams);
2343 sb_free_image(&ctx);
2349 * mxsimage parameters
2353 "Freescale MXS Boot Image support",
2356 mxsimage_check_params,
2357 mxsimage_verify_header,
2358 mxsimage_print_header,
2359 mxsimage_set_header,
2361 mxsimage_check_image_types,