2 * Freescale i.MX23/i.MX28 SB image generator
4 * Copyright (C) 2012-2013 Marek Vasut <marex@denx.de>
6 * SPDX-License-Identifier: GPL-2.0+
18 #include <openssl/evp.h>
20 #include "imagetool.h"
22 #include "pbl_crc32.h"
28 * |-Write to address command block
31 * |-ORR address with mask command block
33 * |-Write to address command block
37 #define SB_HAB_DCD_WRITE 0xccUL
38 #define SB_HAB_DCD_CHECK 0xcfUL
39 #define SB_HAB_DCD_NOOP 0xc0UL
40 #define SB_HAB_DCD_MASK_BIT (1 << 3)
41 #define SB_HAB_DCD_SET_BIT (1 << 4)
43 /* Addr.n = Value.n */
44 #define SB_DCD_WRITE \
45 (SB_HAB_DCD_WRITE << 24)
46 /* Addr.n &= ~Value.n */
48 ((SB_HAB_DCD_WRITE << 24) | SB_HAB_DCD_SET_BIT)
49 /* Addr.n |= Value.n */
51 ((SB_HAB_DCD_WRITE << 24) | SB_HAB_DCD_SET_BIT | SB_HAB_DCD_MASK_BIT)
52 /* (Addr.n & Value.n) == 0 */
53 #define SB_DCD_CHK_EQZ \
54 (SB_HAB_DCD_CHECK << 24)
55 /* (Addr.n & Value.n) == Value.n */
56 #define SB_DCD_CHK_EQ \
57 ((SB_HAB_DCD_CHECK << 24) | SB_HAB_DCD_SET_BIT)
58 /* (Addr.n & Value.n) != Value.n */
59 #define SB_DCD_CHK_NEQ \
60 ((SB_HAB_DCD_CHECK << 24) | SB_HAB_DCD_MASK_BIT)
61 /* (Addr.n & Value.n) != 0 */
62 #define SB_DCD_CHK_NEZ \
63 ((SB_HAB_DCD_CHECK << 24) | SB_HAB_DCD_SET_BIT | SB_HAB_DCD_MASK_BIT)
66 (SB_HAB_DCD_NOOP << 24)
69 struct sb_dcd_ctx *dcd;
75 /* Size of the whole DCD block. */
78 /* Pointer to previous DCD command block. */
79 uint32_t *prev_dcd_head;
101 struct sb_cmd_ctx *cmd;
106 struct sb_command payload;
107 struct sb_command c_payload;
110 struct sb_section_ctx {
116 struct sb_section_ctx *sect;
118 struct sb_cmd_ctx *cmd_head;
119 struct sb_cmd_ctx *cmd_tail;
121 struct sb_sections_header payload;
124 struct sb_image_ctx {
125 unsigned int in_section:1;
126 unsigned int in_dcd:1;
127 /* Image configuration */
128 unsigned int display_progress:1;
129 unsigned int silent_dump:1;
130 char *input_filename;
131 char *output_filename;
133 uint8_t image_key[16];
135 /* Number of section in the image */
136 unsigned int sect_count;
137 /* Bootable section */
138 unsigned int sect_boot;
139 unsigned int sect_boot_found:1;
141 struct sb_section_ctx *sect_head;
142 struct sb_section_ctx *sect_tail;
144 struct sb_dcd_ctx *dcd_head;
145 struct sb_dcd_ctx *dcd_tail;
147 EVP_CIPHER_CTX cipher_ctx;
150 struct sb_key_dictionary_key sb_dict_key;
152 struct sb_boot_image_header payload;
156 * Instruction semantics:
160 * LOAD IVT address IVT_entry_point
161 * FILL address pattern length
162 * JUMP [HAB] address [r0_arg]
163 * CALL [HAB] address [r0_arg]
165 * For i.MX23, mode = USB/I2C/SPI1_FLASH/SPI2_FLASH/NAND_BCH
166 * JTAG/SPI3_EEPROM/SD_SSP0/SD_SSP1
167 * For i.MX28, mode = USB/I2C/SPI2_FLASH/SPI3_FLASH/NAND_BCH
168 * JTAG/SPI2_EEPROM/SD_SSP0/SD_SSP1
174 static int sb_aes_init(struct sb_image_ctx *ictx, uint8_t *iv, int enc)
176 EVP_CIPHER_CTX *ctx = &ictx->cipher_ctx;
179 /* If there is no init vector, init vector is all zeroes. */
181 iv = ictx->image_key;
183 EVP_CIPHER_CTX_init(ctx);
184 ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), ictx->image_key, iv, enc);
186 EVP_CIPHER_CTX_set_padding(ctx, 0);
190 static int sb_aes_crypt(struct sb_image_ctx *ictx, uint8_t *in_data,
191 uint8_t *out_data, int in_len)
193 EVP_CIPHER_CTX *ctx = &ictx->cipher_ctx;
197 outbuf = malloc(in_len);
200 memset(outbuf, 0, sizeof(in_len));
202 ret = EVP_CipherUpdate(ctx, outbuf, &outlen, in_data, in_len);
209 memcpy(out_data, outbuf, outlen);
216 static int sb_aes_deinit(EVP_CIPHER_CTX *ctx)
218 return EVP_CIPHER_CTX_cleanup(ctx);
221 static int sb_aes_reinit(struct sb_image_ctx *ictx, int enc)
224 EVP_CIPHER_CTX *ctx = &ictx->cipher_ctx;
225 struct sb_boot_image_header *sb_header = &ictx->payload;
226 uint8_t *iv = sb_header->iv;
228 ret = sb_aes_deinit(ctx);
231 return sb_aes_init(ictx, iv, enc);
237 static void soprintf(struct sb_image_ctx *ictx, const char *fmt, ...)
241 if (ictx->silent_dump)
245 vfprintf(stdout, fmt, ap);
252 static time_t sb_get_timestamp(void)
254 struct tm time_2000 = {
255 .tm_yday = 1, /* Jan. 1st */
256 .tm_year = 100, /* 2000 */
258 time_t seconds_to_2000 = mktime(&time_2000);
259 time_t seconds_to_now = time(NULL);
261 return seconds_to_now - seconds_to_2000;
264 static int sb_get_time(time_t time, struct tm *tm)
266 struct tm time_2000 = {
267 .tm_yday = 1, /* Jan. 1st */
268 .tm_year = 0, /* 1900 */
270 const time_t seconds_to_2000 = mktime(&time_2000);
271 const time_t seconds_to_now = seconds_to_2000 + time;
273 ret = gmtime_r(&seconds_to_now, tm);
274 return ret ? 0 : -EINVAL;
277 static void sb_encrypt_sb_header(struct sb_image_ctx *ictx)
279 EVP_MD_CTX *md_ctx = &ictx->md_ctx;
280 struct sb_boot_image_header *sb_header = &ictx->payload;
281 uint8_t *sb_header_ptr = (uint8_t *)sb_header;
283 /* Encrypt the header, compute the digest. */
284 sb_aes_crypt(ictx, sb_header_ptr, NULL, sizeof(*sb_header));
285 EVP_DigestUpdate(md_ctx, sb_header_ptr, sizeof(*sb_header));
288 static void sb_encrypt_sb_sections_header(struct sb_image_ctx *ictx)
290 EVP_MD_CTX *md_ctx = &ictx->md_ctx;
291 struct sb_section_ctx *sctx = ictx->sect_head;
292 struct sb_sections_header *shdr;
293 uint8_t *sb_sections_header_ptr;
294 const int size = sizeof(*shdr);
297 shdr = &sctx->payload;
298 sb_sections_header_ptr = (uint8_t *)shdr;
300 sb_aes_crypt(ictx, sb_sections_header_ptr,
301 ictx->sb_dict_key.cbc_mac, size);
302 EVP_DigestUpdate(md_ctx, sb_sections_header_ptr, size);
308 static void sb_encrypt_key_dictionary_key(struct sb_image_ctx *ictx)
310 EVP_MD_CTX *md_ctx = &ictx->md_ctx;
312 sb_aes_crypt(ictx, ictx->image_key, ictx->sb_dict_key.key,
313 sizeof(ictx->sb_dict_key.key));
314 EVP_DigestUpdate(md_ctx, &ictx->sb_dict_key, sizeof(ictx->sb_dict_key));
317 static void sb_decrypt_key_dictionary_key(struct sb_image_ctx *ictx)
319 EVP_MD_CTX *md_ctx = &ictx->md_ctx;
321 EVP_DigestUpdate(md_ctx, &ictx->sb_dict_key, sizeof(ictx->sb_dict_key));
322 sb_aes_crypt(ictx, ictx->sb_dict_key.key, ictx->image_key,
323 sizeof(ictx->sb_dict_key.key));
326 static void sb_encrypt_tag(struct sb_image_ctx *ictx,
327 struct sb_cmd_ctx *cctx)
329 EVP_MD_CTX *md_ctx = &ictx->md_ctx;
330 struct sb_command *cmd = &cctx->payload;
332 sb_aes_crypt(ictx, (uint8_t *)cmd,
333 (uint8_t *)&cctx->c_payload, sizeof(*cmd));
334 EVP_DigestUpdate(md_ctx, &cctx->c_payload, sizeof(*cmd));
337 static int sb_encrypt_image(struct sb_image_ctx *ictx)
339 /* Start image-wide crypto. */
340 EVP_MD_CTX_init(&ictx->md_ctx);
341 EVP_DigestInit(&ictx->md_ctx, EVP_sha1());
346 sb_aes_init(ictx, NULL, 1);
347 sb_encrypt_sb_header(ictx);
350 * SB sections header.
352 sb_encrypt_sb_sections_header(ictx);
357 sb_aes_reinit(ictx, 1);
358 sb_encrypt_key_dictionary_key(ictx);
363 struct sb_cmd_ctx *cctx;
364 struct sb_command *ccmd;
365 struct sb_section_ctx *sctx = ictx->sect_head;
368 cctx = sctx->cmd_head;
370 sb_aes_reinit(ictx, 1);
373 ccmd = &cctx->payload;
375 sb_encrypt_tag(ictx, cctx);
377 if (ccmd->header.tag == ROM_TAG_CMD) {
378 sb_aes_reinit(ictx, 1);
379 } else if (ccmd->header.tag == ROM_LOAD_CMD) {
380 sb_aes_crypt(ictx, cctx->data, cctx->data,
382 EVP_DigestUpdate(&ictx->md_ctx, cctx->data,
393 * Dump the SHA1 of the whole image.
395 sb_aes_reinit(ictx, 1);
397 EVP_DigestFinal(&ictx->md_ctx, ictx->digest, NULL);
398 sb_aes_crypt(ictx, ictx->digest, ictx->digest, sizeof(ictx->digest));
400 /* Stop the encryption session. */
401 sb_aes_deinit(&ictx->cipher_ctx);
406 static int sb_load_file(struct sb_cmd_ctx *cctx, char *filename)
408 long real_size, roundup_size;
415 fprintf(stderr, "ERR: Missing filename!\n");
419 fp = fopen(filename, "r");
423 ret = fseek(fp, 0, SEEK_END);
427 real_size = ftell(fp);
431 ret = fseek(fp, 0, SEEK_SET);
435 roundup_size = roundup(real_size, SB_BLOCK_SIZE);
436 data = calloc(1, roundup_size);
440 size = fread(data, 1, real_size, fp);
441 if (size != (unsigned long)real_size)
445 cctx->length = roundup_size;
455 fprintf(stderr, "ERR: Failed to load file \"%s\"\n", filename);
459 static uint8_t sb_command_checksum(struct sb_command *inst)
461 uint8_t *inst_ptr = (uint8_t *)inst;
465 for (i = 0; i < sizeof(struct sb_command); i++)
471 static int sb_token_to_long(char *tok, uint32_t *rid)
476 if (tok[0] != '0' || tok[1] != 'x') {
477 fprintf(stderr, "ERR: Invalid hexadecimal number!\n");
484 id = strtoul(tok, &endptr, 16);
485 if ((errno == ERANGE && id == ULONG_MAX) || (errno != 0 && id == 0)) {
486 fprintf(stderr, "ERR: Value can't be decoded!\n");
490 /* Check for 32-bit overflow. */
491 if (id > 0xffffffff) {
492 fprintf(stderr, "ERR: Value too big!\n");
497 fprintf(stderr, "ERR: Deformed value!\n");
505 static int sb_grow_dcd(struct sb_dcd_ctx *dctx, unsigned int inc_size)
512 dctx->size += inc_size;
513 tmp = realloc(dctx->payload, dctx->size);
519 /* Assemble and update the HAB DCD header. */
520 dctx->payload[0] = htonl((SB_HAB_DCD_TAG << 24) |
527 static int sb_build_dcd(struct sb_image_ctx *ictx, struct sb_cmd_list *cmd)
529 struct sb_dcd_ctx *dctx;
535 dctx = calloc(1, sizeof(*dctx));
539 ret = sb_grow_dcd(dctx, 4);
543 /* Read DCD block number. */
544 tok = strtok(cmd->cmd, " ");
546 fprintf(stderr, "#%i ERR: DCD block without number!\n",
552 /* Parse the DCD block number. */
553 ret = sb_token_to_long(tok, &id);
555 fprintf(stderr, "#%i ERR: Malformed DCD block number!\n",
563 * The DCD block is now constructed. Append it to the list.
564 * WARNING: The DCD size is still not computed and will be
565 * updated while parsing it's commands.
567 if (!ictx->dcd_head) {
568 ictx->dcd_head = dctx;
569 ictx->dcd_tail = dctx;
571 ictx->dcd_tail->dcd = dctx;
572 ictx->dcd_tail = dctx;
583 static int sb_build_dcd_block(struct sb_image_ctx *ictx,
584 struct sb_cmd_list *cmd,
588 uint32_t address, value, length;
591 struct sb_dcd_ctx *dctx = ictx->dcd_tail;
594 if (dctx->prev_dcd_head && (type != SB_DCD_NOOP) &&
595 ((dctx->prev_dcd_head[0] & 0xff0000ff) == type)) {
596 /* Same instruction as before, just append it. */
597 ret = sb_grow_dcd(dctx, 8);
600 } else if (type == SB_DCD_NOOP) {
601 ret = sb_grow_dcd(dctx, 4);
605 /* Update DCD command block pointer. */
606 dctx->prev_dcd_head = dctx->payload +
607 dctx->size / sizeof(*dctx->payload) - 1;
609 /* NOOP has only 4 bytes and no payload. */
613 * Either a different instruction block started now
614 * or this is the first instruction block.
616 ret = sb_grow_dcd(dctx, 12);
620 /* Update DCD command block pointer. */
621 dctx->prev_dcd_head = dctx->payload +
622 dctx->size / sizeof(*dctx->payload) - 3;
625 dcd = dctx->payload + dctx->size / sizeof(*dctx->payload) - 2;
628 * Prepare the command.
630 tok = strtok(cmd->cmd, " ");
632 fprintf(stderr, "#%i ERR: Missing DCD address!\n",
638 /* Read DCD destination address. */
639 ret = sb_token_to_long(tok, &address);
641 fprintf(stderr, "#%i ERR: Incorrect DCD address!\n",
646 tok = strtok(NULL, " ");
648 fprintf(stderr, "#%i ERR: Missing DCD value!\n",
654 /* Read DCD operation value. */
655 ret = sb_token_to_long(tok, &value);
657 fprintf(stderr, "#%i ERR: Incorrect DCD value!\n",
662 /* Fill in the new DCD entry. */
663 dcd[0] = htonl(address);
664 dcd[1] = htonl(value);
667 /* Update the DCD command block. */
668 length = dctx->size -
669 ((dctx->prev_dcd_head - dctx->payload) *
670 sizeof(*dctx->payload));
671 dctx->prev_dcd_head[0] = htonl(type | (length << 8));
677 static int sb_build_section(struct sb_image_ctx *ictx, struct sb_cmd_list *cmd)
679 struct sb_section_ctx *sctx;
680 struct sb_sections_header *shdr;
682 uint32_t bootable = 0;
686 sctx = calloc(1, sizeof(*sctx));
690 /* Read section number. */
691 tok = strtok(cmd->cmd, " ");
693 fprintf(stderr, "#%i ERR: Section without number!\n",
699 /* Parse the section number. */
700 ret = sb_token_to_long(tok, &id);
702 fprintf(stderr, "#%i ERR: Malformed section number!\n",
707 /* Read section's BOOTABLE flag. */
708 tok = strtok(NULL, " ");
709 if (tok && (strlen(tok) == 8) && !strncmp(tok, "BOOTABLE", 8))
710 bootable = SB_SECTION_FLAG_BOOTABLE;
712 sctx->boot = bootable;
714 shdr = &sctx->payload;
715 shdr->section_number = id;
716 shdr->section_flags = bootable;
719 * The section is now constructed. Append it to the list.
720 * WARNING: The section size is still not computed and will
721 * be updated while parsing it's commands.
725 /* Mark that this section is bootable one. */
727 if (ictx->sect_boot_found) {
729 "#%i WARN: Multiple bootable section!\n",
732 ictx->sect_boot = id;
733 ictx->sect_boot_found = 1;
737 if (!ictx->sect_head) {
738 ictx->sect_head = sctx;
739 ictx->sect_tail = sctx;
741 ictx->sect_tail->sect = sctx;
742 ictx->sect_tail = sctx;
752 static int sb_build_command_nop(struct sb_image_ctx *ictx)
754 struct sb_section_ctx *sctx = ictx->sect_tail;
755 struct sb_cmd_ctx *cctx;
756 struct sb_command *ccmd;
758 cctx = calloc(1, sizeof(*cctx));
762 ccmd = &cctx->payload;
765 * Construct the command.
767 ccmd->header.checksum = 0x5a;
768 ccmd->header.tag = ROM_NOP_CMD;
770 cctx->size = sizeof(*ccmd);
773 * Append the command to the last section.
775 if (!sctx->cmd_head) {
776 sctx->cmd_head = cctx;
777 sctx->cmd_tail = cctx;
779 sctx->cmd_tail->cmd = cctx;
780 sctx->cmd_tail = cctx;
786 static int sb_build_command_tag(struct sb_image_ctx *ictx,
787 struct sb_cmd_list *cmd)
789 struct sb_section_ctx *sctx = ictx->sect_tail;
790 struct sb_cmd_ctx *cctx;
791 struct sb_command *ccmd;
794 cctx = calloc(1, sizeof(*cctx));
798 ccmd = &cctx->payload;
801 * Prepare the command.
803 /* Check for the LAST keyword. */
804 tok = strtok(cmd->cmd, " ");
805 if (tok && !strcmp(tok, "LAST"))
806 ccmd->header.flags = ROM_TAG_CMD_FLAG_ROM_LAST_TAG;
809 * Construct the command.
811 ccmd->header.checksum = 0x5a;
812 ccmd->header.tag = ROM_TAG_CMD;
814 cctx->size = sizeof(*ccmd);
817 * Append the command to the last section.
819 if (!sctx->cmd_head) {
820 sctx->cmd_head = cctx;
821 sctx->cmd_tail = cctx;
823 sctx->cmd_tail->cmd = cctx;
824 sctx->cmd_tail = cctx;
830 static int sb_build_command_load(struct sb_image_ctx *ictx,
831 struct sb_cmd_list *cmd)
833 struct sb_section_ctx *sctx = ictx->sect_tail;
834 struct sb_cmd_ctx *cctx;
835 struct sb_command *ccmd;
837 int ret, is_ivt = 0, is_dcd = 0;
838 uint32_t dest, dcd = 0;
840 cctx = calloc(1, sizeof(*cctx));
844 ccmd = &cctx->payload;
847 * Prepare the command.
849 tok = strtok(cmd->cmd, " ");
851 fprintf(stderr, "#%i ERR: Missing LOAD address or 'IVT'!\n",
857 /* Check for "IVT" flag. */
858 if (!strcmp(tok, "IVT"))
860 if (!strcmp(tok, "DCD"))
862 if (is_ivt || is_dcd) {
863 tok = strtok(NULL, " ");
865 fprintf(stderr, "#%i ERR: Missing LOAD address!\n",
872 /* Read load destination address. */
873 ret = sb_token_to_long(tok, &dest);
875 fprintf(stderr, "#%i ERR: Incorrect LOAD address!\n",
880 /* Read filename or IVT entrypoint or DCD block ID. */
881 tok = strtok(NULL, " ");
884 "#%i ERR: Missing LOAD filename or IVT ep or DCD block ID!\n",
892 struct sb_ivt_header *ivt;
894 ret = sb_token_to_long(tok, &ivtep);
898 "#%i ERR: Incorrect IVT entry point!\n",
903 ivt = calloc(1, sizeof(*ivt));
909 ivt->header = sb_hab_ivt_header();
913 cctx->data = (uint8_t *)ivt;
914 cctx->length = sizeof(*ivt);
916 struct sb_dcd_ctx *dctx = ictx->dcd_head;
920 ret = sb_token_to_long(tok, &dcdid);
924 "#%i ERR: Incorrect DCD block ID!\n",
930 if (dctx->id == dcdid)
936 fprintf(stderr, "#%i ERR: DCD block %08x not found!\n",
941 asize = roundup(dctx->size, SB_BLOCK_SIZE);
942 payload = calloc(1, asize);
948 memcpy(payload, dctx->payload, dctx->size);
950 cctx->data = payload;
951 cctx->length = asize;
953 /* Set the Load DCD flag. */
954 dcd = ROM_LOAD_CMD_FLAG_DCD_LOAD;
956 /* Regular LOAD of a file. */
957 ret = sb_load_file(cctx, tok);
959 fprintf(stderr, "#%i ERR: Cannot load '%s'!\n",
965 if (cctx->length & (SB_BLOCK_SIZE - 1)) {
966 fprintf(stderr, "#%i ERR: Unaligned payload!\n",
971 * Construct the command.
973 ccmd->header.checksum = 0x5a;
974 ccmd->header.tag = ROM_LOAD_CMD;
975 ccmd->header.flags = dcd;
977 ccmd->load.address = dest;
978 ccmd->load.count = cctx->length;
979 ccmd->load.crc32 = pbl_crc32(0,
980 (const char *)cctx->data,
983 cctx->size = sizeof(*ccmd) + cctx->length;
986 * Append the command to the last section.
988 if (!sctx->cmd_head) {
989 sctx->cmd_head = cctx;
990 sctx->cmd_tail = cctx;
992 sctx->cmd_tail->cmd = cctx;
993 sctx->cmd_tail = cctx;
1003 static int sb_build_command_fill(struct sb_image_ctx *ictx,
1004 struct sb_cmd_list *cmd)
1006 struct sb_section_ctx *sctx = ictx->sect_tail;
1007 struct sb_cmd_ctx *cctx;
1008 struct sb_command *ccmd;
1010 uint32_t address, pattern, length;
1013 cctx = calloc(1, sizeof(*cctx));
1017 ccmd = &cctx->payload;
1020 * Prepare the command.
1022 tok = strtok(cmd->cmd, " ");
1024 fprintf(stderr, "#%i ERR: Missing FILL address!\n",
1030 /* Read fill destination address. */
1031 ret = sb_token_to_long(tok, &address);
1033 fprintf(stderr, "#%i ERR: Incorrect FILL address!\n",
1038 tok = strtok(NULL, " ");
1040 fprintf(stderr, "#%i ERR: Missing FILL pattern!\n",
1046 /* Read fill pattern address. */
1047 ret = sb_token_to_long(tok, &pattern);
1049 fprintf(stderr, "#%i ERR: Incorrect FILL pattern!\n",
1054 tok = strtok(NULL, " ");
1056 fprintf(stderr, "#%i ERR: Missing FILL length!\n",
1062 /* Read fill pattern address. */
1063 ret = sb_token_to_long(tok, &length);
1065 fprintf(stderr, "#%i ERR: Incorrect FILL length!\n",
1071 * Construct the command.
1073 ccmd->header.checksum = 0x5a;
1074 ccmd->header.tag = ROM_FILL_CMD;
1076 ccmd->fill.address = address;
1077 ccmd->fill.count = length;
1078 ccmd->fill.pattern = pattern;
1080 cctx->size = sizeof(*ccmd);
1083 * Append the command to the last section.
1085 if (!sctx->cmd_head) {
1086 sctx->cmd_head = cctx;
1087 sctx->cmd_tail = cctx;
1089 sctx->cmd_tail->cmd = cctx;
1090 sctx->cmd_tail = cctx;
1100 static int sb_build_command_jump_call(struct sb_image_ctx *ictx,
1101 struct sb_cmd_list *cmd,
1102 unsigned int is_call)
1104 struct sb_section_ctx *sctx = ictx->sect_tail;
1105 struct sb_cmd_ctx *cctx;
1106 struct sb_command *ccmd;
1108 uint32_t dest, arg = 0x0;
1111 const char *cmdname = is_call ? "CALL" : "JUMP";
1113 cctx = calloc(1, sizeof(*cctx));
1117 ccmd = &cctx->payload;
1120 * Prepare the command.
1122 tok = strtok(cmd->cmd, " ");
1125 "#%i ERR: Missing %s address or 'HAB'!\n",
1126 cmd->lineno, cmdname);
1131 /* Check for "HAB" flag. */
1132 if (!strcmp(tok, "HAB")) {
1133 hab = is_call ? ROM_CALL_CMD_FLAG_HAB : ROM_JUMP_CMD_FLAG_HAB;
1134 tok = strtok(NULL, " ");
1136 fprintf(stderr, "#%i ERR: Missing %s address!\n",
1137 cmd->lineno, cmdname);
1142 /* Read load destination address. */
1143 ret = sb_token_to_long(tok, &dest);
1145 fprintf(stderr, "#%i ERR: Incorrect %s address!\n",
1146 cmd->lineno, cmdname);
1150 tok = strtok(NULL, " ");
1152 ret = sb_token_to_long(tok, &arg);
1155 "#%i ERR: Incorrect %s argument!\n",
1156 cmd->lineno, cmdname);
1162 * Construct the command.
1164 ccmd->header.checksum = 0x5a;
1165 ccmd->header.tag = is_call ? ROM_CALL_CMD : ROM_JUMP_CMD;
1166 ccmd->header.flags = hab;
1168 ccmd->call.address = dest;
1169 ccmd->call.argument = arg;
1171 cctx->size = sizeof(*ccmd);
1174 * Append the command to the last section.
1176 if (!sctx->cmd_head) {
1177 sctx->cmd_head = cctx;
1178 sctx->cmd_tail = cctx;
1180 sctx->cmd_tail->cmd = cctx;
1181 sctx->cmd_tail = cctx;
1191 static int sb_build_command_jump(struct sb_image_ctx *ictx,
1192 struct sb_cmd_list *cmd)
1194 return sb_build_command_jump_call(ictx, cmd, 0);
1197 static int sb_build_command_call(struct sb_image_ctx *ictx,
1198 struct sb_cmd_list *cmd)
1200 return sb_build_command_jump_call(ictx, cmd, 1);
1203 static int sb_build_command_mode(struct sb_image_ctx *ictx,
1204 struct sb_cmd_list *cmd)
1206 struct sb_section_ctx *sctx = ictx->sect_tail;
1207 struct sb_cmd_ctx *cctx;
1208 struct sb_command *ccmd;
1212 uint32_t mode = 0xffffffff;
1214 cctx = calloc(1, sizeof(*cctx));
1218 ccmd = &cctx->payload;
1221 * Prepare the command.
1223 tok = strtok(cmd->cmd, " ");
1225 fprintf(stderr, "#%i ERR: Missing MODE boot mode argument!\n",
1231 for (i = 0; i < ARRAY_SIZE(modetable); i++) {
1232 if (!strcmp(tok, modetable[i].name)) {
1233 mode = modetable[i].mode;
1237 if (!modetable[i].altname)
1240 if (!strcmp(tok, modetable[i].altname)) {
1241 mode = modetable[i].mode;
1246 if (mode == 0xffffffff) {
1247 fprintf(stderr, "#%i ERR: Invalid MODE boot mode argument!\n",
1254 * Construct the command.
1256 ccmd->header.checksum = 0x5a;
1257 ccmd->header.tag = ROM_MODE_CMD;
1259 ccmd->mode.mode = mode;
1261 cctx->size = sizeof(*ccmd);
1264 * Append the command to the last section.
1266 if (!sctx->cmd_head) {
1267 sctx->cmd_head = cctx;
1268 sctx->cmd_tail = cctx;
1270 sctx->cmd_tail->cmd = cctx;
1271 sctx->cmd_tail = cctx;
1281 static int sb_prefill_image_header(struct sb_image_ctx *ictx)
1283 struct sb_boot_image_header *hdr = &ictx->payload;
1285 /* Fill signatures */
1286 memcpy(hdr->signature1, "STMP", 4);
1287 memcpy(hdr->signature2, "sgtl", 4);
1289 /* SB Image version 1.1 */
1290 hdr->major_version = SB_VERSION_MAJOR;
1291 hdr->minor_version = SB_VERSION_MINOR;
1293 /* Boot image major version */
1294 hdr->product_version.major = htons(0x999);
1295 hdr->product_version.minor = htons(0x999);
1296 hdr->product_version.revision = htons(0x999);
1297 /* Boot image major version */
1298 hdr->component_version.major = htons(0x999);
1299 hdr->component_version.minor = htons(0x999);
1300 hdr->component_version.revision = htons(0x999);
1302 /* Drive tag must be 0x0 for i.MX23 */
1305 hdr->header_blocks =
1306 sizeof(struct sb_boot_image_header) / SB_BLOCK_SIZE;
1307 hdr->section_header_size =
1308 sizeof(struct sb_sections_header) / SB_BLOCK_SIZE;
1309 hdr->timestamp_us = sb_get_timestamp() * 1000000;
1311 hdr->flags = ictx->display_progress ?
1312 SB_IMAGE_FLAG_DISPLAY_PROGRESS : 0;
1314 /* FIXME -- We support only default key */
1320 static int sb_postfill_image_header(struct sb_image_ctx *ictx)
1322 struct sb_boot_image_header *hdr = &ictx->payload;
1323 struct sb_section_ctx *sctx = ictx->sect_head;
1324 uint32_t kd_size, sections_blocks;
1327 /* The main SB header size in blocks. */
1328 hdr->image_blocks = hdr->header_blocks;
1330 /* Size of the key dictionary, which has single zero entry. */
1331 kd_size = hdr->key_count * sizeof(struct sb_key_dictionary_key);
1332 hdr->image_blocks += kd_size / SB_BLOCK_SIZE;
1334 /* Now count the payloads. */
1335 hdr->section_count = ictx->sect_count;
1337 hdr->image_blocks += sctx->size / SB_BLOCK_SIZE;
1341 if (!ictx->sect_boot_found) {
1342 fprintf(stderr, "ERR: No bootable section selected!\n");
1345 hdr->first_boot_section_id = ictx->sect_boot;
1347 /* The n * SB section size in blocks. */
1348 sections_blocks = hdr->section_count * hdr->section_header_size;
1349 hdr->image_blocks += sections_blocks;
1351 /* Key dictionary offset. */
1352 hdr->key_dictionary_block = hdr->header_blocks + sections_blocks;
1354 /* Digest of the whole image. */
1355 hdr->image_blocks += 2;
1357 /* Pointer past the dictionary. */
1358 hdr->first_boot_tag_block =
1359 hdr->key_dictionary_block + kd_size / SB_BLOCK_SIZE;
1361 /* Compute header digest. */
1362 EVP_MD_CTX_init(&md_ctx);
1364 EVP_DigestInit(&md_ctx, EVP_sha1());
1365 EVP_DigestUpdate(&md_ctx, hdr->signature1,
1366 sizeof(struct sb_boot_image_header) -
1367 sizeof(hdr->digest));
1368 EVP_DigestFinal(&md_ctx, hdr->digest, NULL);
1373 static int sb_fixup_sections_and_tags(struct sb_image_ctx *ictx)
1375 /* Fixup the placement of sections. */
1376 struct sb_boot_image_header *ihdr = &ictx->payload;
1377 struct sb_section_ctx *sctx = ictx->sect_head;
1378 struct sb_sections_header *shdr;
1379 struct sb_cmd_ctx *cctx;
1380 struct sb_command *ccmd;
1381 uint32_t offset = ihdr->first_boot_tag_block;
1384 shdr = &sctx->payload;
1386 /* Fill in the section TAG offset. */
1387 shdr->section_offset = offset + 1;
1388 offset += shdr->section_size;
1390 /* Section length is measured from the TAG block. */
1391 shdr->section_size--;
1393 /* Fixup the TAG command. */
1394 cctx = sctx->cmd_head;
1396 ccmd = &cctx->payload;
1397 if (ccmd->header.tag == ROM_TAG_CMD) {
1398 ccmd->tag.section_number = shdr->section_number;
1399 ccmd->tag.section_length = shdr->section_size;
1400 ccmd->tag.section_flags = shdr->section_flags;
1403 /* Update the command checksum. */
1404 ccmd->header.checksum = sb_command_checksum(ccmd);
1415 static int sb_parse_line(struct sb_image_ctx *ictx, struct sb_cmd_list *cmd)
1418 char *line = cmd->cmd;
1422 /* Analyze the identifier on this line first. */
1423 tok = strtok_r(line, " ", &rptr);
1424 if (!tok || (strlen(tok) == 0)) {
1425 fprintf(stderr, "#%i ERR: Invalid line!\n", cmd->lineno);
1431 /* set DISPLAY_PROGRESS flag */
1432 if (!strcmp(tok, "DISPLAYPROGRESS")) {
1433 ictx->display_progress = 1;
1438 if (!strcmp(tok, "DCD")) {
1439 ictx->in_section = 0;
1441 sb_build_dcd(ictx, cmd);
1446 if (!strcmp(tok, "SECTION")) {
1447 ictx->in_section = 1;
1449 sb_build_section(ictx, cmd);
1453 if (!ictx->in_section && !ictx->in_dcd) {
1454 fprintf(stderr, "#%i ERR: Data outside of a section!\n",
1459 if (ictx->in_section) {
1460 /* Section commands */
1461 if (!strcmp(tok, "NOP")) {
1462 ret = sb_build_command_nop(ictx);
1463 } else if (!strcmp(tok, "TAG")) {
1464 ret = sb_build_command_tag(ictx, cmd);
1465 } else if (!strcmp(tok, "LOAD")) {
1466 ret = sb_build_command_load(ictx, cmd);
1467 } else if (!strcmp(tok, "FILL")) {
1468 ret = sb_build_command_fill(ictx, cmd);
1469 } else if (!strcmp(tok, "JUMP")) {
1470 ret = sb_build_command_jump(ictx, cmd);
1471 } else if (!strcmp(tok, "CALL")) {
1472 ret = sb_build_command_call(ictx, cmd);
1473 } else if (!strcmp(tok, "MODE")) {
1474 ret = sb_build_command_mode(ictx, cmd);
1477 "#%i ERR: Unsupported instruction '%s'!\n",
1481 } else if (ictx->in_dcd) {
1483 uint32_t ilen = '1';
1485 tok = strtok_r(tok, ".", &lptr);
1486 if (!tok || (strlen(tok) == 0) || (lptr && strlen(lptr) != 1)) {
1487 fprintf(stderr, "#%i ERR: Invalid line!\n",
1493 (lptr[0] != '1' && lptr[0] != '2' && lptr[0] != '4')) {
1494 fprintf(stderr, "#%i ERR: Invalid instruction width!\n",
1500 ilen = lptr[0] - '1';
1503 if (!strcmp(tok, "WRITE")) {
1504 ret = sb_build_dcd_block(ictx, cmd,
1505 SB_DCD_WRITE | ilen);
1506 } else if (!strcmp(tok, "ANDC")) {
1507 ret = sb_build_dcd_block(ictx, cmd,
1508 SB_DCD_ANDC | ilen);
1509 } else if (!strcmp(tok, "ORR")) {
1510 ret = sb_build_dcd_block(ictx, cmd,
1512 } else if (!strcmp(tok, "EQZ")) {
1513 ret = sb_build_dcd_block(ictx, cmd,
1514 SB_DCD_CHK_EQZ | ilen);
1515 } else if (!strcmp(tok, "EQ")) {
1516 ret = sb_build_dcd_block(ictx, cmd,
1517 SB_DCD_CHK_EQ | ilen);
1518 } else if (!strcmp(tok, "NEQ")) {
1519 ret = sb_build_dcd_block(ictx, cmd,
1520 SB_DCD_CHK_NEQ | ilen);
1521 } else if (!strcmp(tok, "NEZ")) {
1522 ret = sb_build_dcd_block(ictx, cmd,
1523 SB_DCD_CHK_NEZ | ilen);
1524 } else if (!strcmp(tok, "NOOP")) {
1525 ret = sb_build_dcd_block(ictx, cmd, SB_DCD_NOOP);
1528 "#%i ERR: Unsupported instruction '%s'!\n",
1533 fprintf(stderr, "#%i ERR: Unsupported instruction '%s'!\n",
1539 * Here we have at least one section with one command, otherwise we
1540 * would have failed already higher above.
1542 * FIXME -- should the updating happen here ?
1544 if (ictx->in_section && !ret) {
1545 ictx->sect_tail->size += ictx->sect_tail->cmd_tail->size;
1546 ictx->sect_tail->payload.section_size =
1547 ictx->sect_tail->size / SB_BLOCK_SIZE;
1553 static int sb_load_cmdfile(struct sb_image_ctx *ictx)
1555 struct sb_cmd_list cmd;
1562 fp = fopen(ictx->cfg_filename, "r");
1566 while ((rlen = getline(&line, &len, fp)) > 0) {
1567 memset(&cmd, 0, sizeof(cmd));
1569 /* Strip the trailing newline. */
1570 line[rlen - 1] = '\0';
1574 cmd.lineno = lineno++;
1576 sb_parse_line(ictx, &cmd);
1587 fprintf(stderr, "ERR: Failed to load file \"%s\"\n",
1588 ictx->cfg_filename);
1592 static int sb_build_tree_from_cfg(struct sb_image_ctx *ictx)
1596 ret = sb_load_cmdfile(ictx);
1600 ret = sb_prefill_image_header(ictx);
1604 ret = sb_postfill_image_header(ictx);
1608 ret = sb_fixup_sections_and_tags(ictx);
1615 static int sb_verify_image_header(struct sb_image_ctx *ictx,
1616 FILE *fp, long fsize)
1618 /* Verify static fields in the image header. */
1619 struct sb_boot_image_header *hdr = &ictx->payload;
1620 const char *stat[2] = { "[PASS]", "[FAIL]" };
1623 unsigned char digest[20];
1627 /* Start image-wide crypto. */
1628 EVP_MD_CTX_init(&ictx->md_ctx);
1629 EVP_DigestInit(&ictx->md_ctx, EVP_sha1());
1631 soprintf(ictx, "---------- Verifying SB Image Header ----------\n");
1633 size = fread(&ictx->payload, 1, sizeof(ictx->payload), fp);
1634 if (size != sizeof(ictx->payload)) {
1635 fprintf(stderr, "ERR: SB image header too short!\n");
1639 /* Compute header digest. */
1640 EVP_MD_CTX_init(&md_ctx);
1641 EVP_DigestInit(&md_ctx, EVP_sha1());
1642 EVP_DigestUpdate(&md_ctx, hdr->signature1,
1643 sizeof(struct sb_boot_image_header) -
1644 sizeof(hdr->digest));
1645 EVP_DigestFinal(&md_ctx, digest, NULL);
1647 sb_aes_init(ictx, NULL, 1);
1648 sb_encrypt_sb_header(ictx);
1650 if (memcmp(digest, hdr->digest, 20))
1652 soprintf(ictx, "%s Image header checksum: %s\n", stat[!!ret],
1653 ret ? "BAD" : "OK");
1657 if (memcmp(hdr->signature1, "STMP", 4) ||
1658 memcmp(hdr->signature2, "sgtl", 4))
1660 soprintf(ictx, "%s Signatures: '%.4s' '%.4s'\n",
1661 stat[!!ret], hdr->signature1, hdr->signature2);
1665 if ((hdr->major_version != SB_VERSION_MAJOR) ||
1666 ((hdr->minor_version != 1) && (hdr->minor_version != 2)))
1668 soprintf(ictx, "%s Image version: v%i.%i\n", stat[!!ret],
1669 hdr->major_version, hdr->minor_version);
1673 ret = sb_get_time(hdr->timestamp_us / 1000000, &tm);
1675 "%s Creation time: %02i:%02i:%02i %02i/%02i/%04i\n",
1676 stat[!!ret], tm.tm_hour, tm.tm_min, tm.tm_sec,
1677 tm.tm_mday, tm.tm_mon, tm.tm_year + 2000);
1681 soprintf(ictx, "%s Product version: %x.%x.%x\n", stat[0],
1682 ntohs(hdr->product_version.major),
1683 ntohs(hdr->product_version.minor),
1684 ntohs(hdr->product_version.revision));
1685 soprintf(ictx, "%s Component version: %x.%x.%x\n", stat[0],
1686 ntohs(hdr->component_version.major),
1687 ntohs(hdr->component_version.minor),
1688 ntohs(hdr->component_version.revision));
1690 if (hdr->flags & ~SB_IMAGE_FLAGS_MASK)
1692 soprintf(ictx, "%s Image flags: %s\n", stat[!!ret],
1693 hdr->flags & SB_IMAGE_FLAG_DISPLAY_PROGRESS ?
1694 "Display_progress" : "");
1698 if (hdr->drive_tag != 0)
1700 soprintf(ictx, "%s Drive tag: %i\n", stat[!!ret],
1705 sz = sizeof(struct sb_boot_image_header) / SB_BLOCK_SIZE;
1706 if (hdr->header_blocks != sz)
1708 soprintf(ictx, "%s Image header size (blocks): %i\n", stat[!!ret],
1709 hdr->header_blocks);
1713 sz = sizeof(struct sb_sections_header) / SB_BLOCK_SIZE;
1714 if (hdr->section_header_size != sz)
1716 soprintf(ictx, "%s Section header size (blocks): %i\n", stat[!!ret],
1717 hdr->section_header_size);
1721 soprintf(ictx, "%s Sections count: %i\n", stat[!!ret],
1722 hdr->section_count);
1723 soprintf(ictx, "%s First bootable section %i\n", stat[!!ret],
1724 hdr->first_boot_section_id);
1726 if (hdr->image_blocks != fsize / SB_BLOCK_SIZE)
1728 soprintf(ictx, "%s Image size (blocks): %i\n", stat[!!ret],
1733 sz = hdr->header_blocks + hdr->section_header_size * hdr->section_count;
1734 if (hdr->key_dictionary_block != sz)
1736 soprintf(ictx, "%s Key dict offset (blocks): %i\n", stat[!!ret],
1737 hdr->key_dictionary_block);
1741 if (hdr->key_count != 1)
1743 soprintf(ictx, "%s Number of encryption keys: %i\n", stat[!!ret],
1748 sz = hdr->header_blocks + hdr->section_header_size * hdr->section_count;
1749 sz += hdr->key_count *
1750 sizeof(struct sb_key_dictionary_key) / SB_BLOCK_SIZE;
1751 if (hdr->first_boot_tag_block != (unsigned)sz)
1753 soprintf(ictx, "%s First TAG block (blocks): %i\n", stat[!!ret],
1754 hdr->first_boot_tag_block);
1761 static void sb_decrypt_tag(struct sb_image_ctx *ictx,
1762 struct sb_cmd_ctx *cctx)
1764 EVP_MD_CTX *md_ctx = &ictx->md_ctx;
1765 struct sb_command *cmd = &cctx->payload;
1767 sb_aes_crypt(ictx, (uint8_t *)&cctx->c_payload,
1768 (uint8_t *)&cctx->payload, sizeof(*cmd));
1769 EVP_DigestUpdate(md_ctx, &cctx->c_payload, sizeof(*cmd));
1772 static int sb_verify_command(struct sb_image_ctx *ictx,
1773 struct sb_cmd_ctx *cctx, FILE *fp,
1774 unsigned long *tsize)
1776 struct sb_command *ccmd = &cctx->payload;
1777 unsigned long size, asize;
1778 char *csum, *flag = "";
1781 uint8_t csn, csc = ccmd->header.checksum;
1782 ccmd->header.checksum = 0x5a;
1783 csn = sb_command_checksum(ccmd);
1784 ccmd->header.checksum = csc;
1790 csum = ret ? "checksum BAD" : "checksum OK";
1792 switch (ccmd->header.tag) {
1794 soprintf(ictx, " NOOP # %s\n", csum);
1797 if (ccmd->header.flags & ROM_TAG_CMD_FLAG_ROM_LAST_TAG)
1799 soprintf(ictx, " TAG %s # %s\n", flag, csum);
1800 sb_aes_reinit(ictx, 0);
1803 soprintf(ictx, " LOAD addr=0x%08x length=0x%08x # %s\n",
1804 ccmd->load.address, ccmd->load.count, csum);
1806 cctx->length = ccmd->load.count;
1807 asize = roundup(cctx->length, SB_BLOCK_SIZE);
1808 cctx->data = malloc(asize);
1812 size = fread(cctx->data, 1, asize, fp);
1813 if (size != asize) {
1815 "ERR: SB LOAD command payload too short!\n");
1821 EVP_DigestUpdate(&ictx->md_ctx, cctx->data, asize);
1822 sb_aes_crypt(ictx, cctx->data, cctx->data, asize);
1824 if (ccmd->load.crc32 != pbl_crc32(0,
1825 (const char *)cctx->data,
1828 "ERR: SB LOAD command payload CRC32 invalid!\n");
1834 " FILL addr=0x%08x length=0x%08x pattern=0x%08x # %s\n",
1835 ccmd->fill.address, ccmd->fill.count,
1836 ccmd->fill.pattern, csum);
1839 if (ccmd->header.flags & ROM_JUMP_CMD_FLAG_HAB)
1842 " JUMP%s addr=0x%08x r0_arg=0x%08x # %s\n",
1843 flag, ccmd->fill.address, ccmd->jump.argument, csum);
1846 if (ccmd->header.flags & ROM_CALL_CMD_FLAG_HAB)
1849 " CALL%s addr=0x%08x r0_arg=0x%08x # %s\n",
1850 flag, ccmd->fill.address, ccmd->jump.argument, csum);
1853 for (i = 0; i < ARRAY_SIZE(modetable); i++) {
1854 if (ccmd->mode.mode == modetable[i].mode) {
1855 soprintf(ictx, " MODE %s # %s\n",
1856 modetable[i].name, csum);
1860 fprintf(stderr, " MODE !INVALID! # %s\n", csum);
1867 static int sb_verify_commands(struct sb_image_ctx *ictx,
1868 struct sb_section_ctx *sctx, FILE *fp)
1870 unsigned long size, tsize = 0;
1871 struct sb_cmd_ctx *cctx;
1874 sb_aes_reinit(ictx, 0);
1876 while (tsize < sctx->size) {
1877 cctx = calloc(1, sizeof(*cctx));
1880 if (!sctx->cmd_head) {
1881 sctx->cmd_head = cctx;
1882 sctx->cmd_tail = cctx;
1884 sctx->cmd_tail->cmd = cctx;
1885 sctx->cmd_tail = cctx;
1888 size = fread(&cctx->c_payload, 1, sizeof(cctx->c_payload), fp);
1889 if (size != sizeof(cctx->c_payload)) {
1890 fprintf(stderr, "ERR: SB command header too short!\n");
1896 sb_decrypt_tag(ictx, cctx);
1898 ret = sb_verify_command(ictx, cctx, fp, &tsize);
1906 static int sb_verify_sections_cmds(struct sb_image_ctx *ictx, FILE *fp)
1908 struct sb_boot_image_header *hdr = &ictx->payload;
1909 struct sb_sections_header *shdr;
1912 struct sb_section_ctx *sctx;
1914 char *bootable = "";
1916 soprintf(ictx, "----- Verifying SB Sections and Commands -----\n");
1918 for (i = 0; i < hdr->section_count; i++) {
1919 sctx = calloc(1, sizeof(*sctx));
1922 if (!ictx->sect_head) {
1923 ictx->sect_head = sctx;
1924 ictx->sect_tail = sctx;
1926 ictx->sect_tail->sect = sctx;
1927 ictx->sect_tail = sctx;
1930 size = fread(&sctx->payload, 1, sizeof(sctx->payload), fp);
1931 if (size != sizeof(sctx->payload)) {
1932 fprintf(stderr, "ERR: SB section header too short!\n");
1937 size = fread(&ictx->sb_dict_key, 1, sizeof(ictx->sb_dict_key), fp);
1938 if (size != sizeof(ictx->sb_dict_key)) {
1939 fprintf(stderr, "ERR: SB key dictionary too short!\n");
1943 sb_encrypt_sb_sections_header(ictx);
1944 sb_aes_reinit(ictx, 0);
1945 sb_decrypt_key_dictionary_key(ictx);
1947 sb_aes_reinit(ictx, 0);
1949 sctx = ictx->sect_head;
1951 shdr = &sctx->payload;
1953 if (shdr->section_flags & SB_SECTION_FLAG_BOOTABLE) {
1955 bootable = " BOOTABLE";
1958 sctx->size = (shdr->section_size * SB_BLOCK_SIZE) +
1959 sizeof(struct sb_command);
1960 soprintf(ictx, "SECTION 0x%x%s # size = %i bytes\n",
1961 shdr->section_number, bootable, sctx->size);
1963 if (shdr->section_flags & ~SB_SECTION_FLAG_BOOTABLE)
1964 fprintf(stderr, " WARN: Unknown section flag(s) %08x\n",
1965 shdr->section_flags);
1967 if ((shdr->section_flags & SB_SECTION_FLAG_BOOTABLE) &&
1968 (hdr->first_boot_section_id != shdr->section_number)) {
1970 " WARN: Bootable section does ID not match image header ID!\n");
1973 ret = sb_verify_commands(ictx, sctx, fp);
1982 * check if the first TAG command is at sctx->section_offset
1987 static int sb_verify_image_end(struct sb_image_ctx *ictx,
1988 FILE *fp, off_t filesz)
1995 soprintf(ictx, "------------- Verifying image end -------------\n");
1997 size = fread(digest, 1, sizeof(digest), fp);
1998 if (size != sizeof(digest)) {
1999 fprintf(stderr, "ERR: SB key dictionary too short!\n");
2004 if (pos != filesz) {
2005 fprintf(stderr, "ERR: Trailing data past the image!\n");
2009 /* Check the image digest. */
2010 EVP_DigestFinal(&ictx->md_ctx, ictx->digest, NULL);
2012 /* Decrypt the image digest from the input image. */
2013 sb_aes_reinit(ictx, 0);
2014 sb_aes_crypt(ictx, digest, digest, sizeof(digest));
2016 /* Check all of 20 bytes of the SHA1 hash. */
2017 ret = memcmp(digest, ictx->digest, 20) ? -EINVAL : 0;
2020 soprintf(ictx, "[FAIL] Full-image checksum: BAD\n");
2022 soprintf(ictx, "[PASS] Full-image checksum: OK\n");
2028 static int sb_build_tree_from_img(struct sb_image_ctx *ictx)
2034 if (!ictx->input_filename) {
2035 fprintf(stderr, "ERR: Missing filename!\n");
2039 fp = fopen(ictx->input_filename, "r");
2043 ret = fseek(fp, 0, SEEK_END);
2047 filesize = ftell(fp);
2051 ret = fseek(fp, 0, SEEK_SET);
2055 if (filesize < (signed)sizeof(ictx->payload)) {
2056 fprintf(stderr, "ERR: File too short!\n");
2060 if (filesize & (SB_BLOCK_SIZE - 1)) {
2061 fprintf(stderr, "ERR: The file is not aligned!\n");
2065 /* Load and verify image header */
2066 ret = sb_verify_image_header(ictx, fp, filesize);
2070 /* Load and verify sections and commands */
2071 ret = sb_verify_sections_cmds(ictx, fp);
2075 ret = sb_verify_image_end(ictx, fp, filesize);
2082 soprintf(ictx, "-------------------- Result -------------------\n");
2083 soprintf(ictx, "Verification %s\n", ret ? "FAILED" : "PASSED");
2085 /* Stop the encryption session. */
2086 sb_aes_deinit(&ictx->cipher_ctx);
2094 fprintf(stderr, "ERR: Failed to load file \"%s\"\n",
2095 ictx->input_filename);
2099 static void sb_free_image(struct sb_image_ctx *ictx)
2101 struct sb_section_ctx *sctx = ictx->sect_head, *s_head;
2102 struct sb_dcd_ctx *dctx = ictx->dcd_head, *d_head;
2103 struct sb_cmd_ctx *cctx, *c_head;
2107 c_head = sctx->cmd_head;
2111 c_head = c_head->cmd;
2124 free(d_head->payload);
2130 * MXSSB-MKIMAGE glue code.
2132 static int mxsimage_check_image_types(uint8_t type)
2134 if (type == IH_TYPE_MXSIMAGE)
2135 return EXIT_SUCCESS;
2137 return EXIT_FAILURE;
2140 static void mxsimage_set_header(void *ptr, struct stat *sbuf, int ifd,
2141 struct image_tool_params *params)
2145 int mxsimage_check_params(struct image_tool_params *params)
2149 if (!strlen(params->imagename)) {
2151 "Error: %s - Configuration file not specified, it is needed for mxsimage generation\n",
2158 * XIP is not allowed and verify that incompatible
2159 * parameters are not sent at the same time
2160 * For example, if list is required a data image must not be provided
2162 return (params->dflag && (params->fflag || params->lflag)) ||
2163 (params->fflag && (params->dflag || params->lflag)) ||
2164 (params->lflag && (params->dflag || params->fflag)) ||
2165 (params->xflag) || !(strlen(params->imagename));
2168 static int mxsimage_verify_print_header(char *file, int silent)
2171 struct sb_image_ctx ctx;
2173 memset(&ctx, 0, sizeof(ctx));
2175 ctx.input_filename = file;
2176 ctx.silent_dump = silent;
2178 ret = sb_build_tree_from_img(&ctx);
2179 sb_free_image(&ctx);
2185 static int mxsimage_verify_header(unsigned char *ptr, int image_size,
2186 struct image_tool_params *params)
2188 struct sb_boot_image_header *hdr;
2193 hdr = (struct sb_boot_image_header *)ptr;
2196 * Check if the header contains the MXS image signatures,
2197 * if so, do a full-image verification.
2199 if (memcmp(hdr->signature1, "STMP", 4) ||
2200 memcmp(hdr->signature2, "sgtl", 4))
2203 imagefile = params->imagefile;
2205 return mxsimage_verify_print_header(params->imagefile, 1);
2208 static void mxsimage_print_header(const void *hdr)
2211 mxsimage_verify_print_header(imagefile, 0);
2214 static int sb_build_image(struct sb_image_ctx *ictx,
2215 struct image_type_params *tparams)
2217 struct sb_boot_image_header *sb_header = &ictx->payload;
2218 struct sb_section_ctx *sctx;
2219 struct sb_cmd_ctx *cctx;
2220 struct sb_command *ccmd;
2221 struct sb_key_dictionary_key *sb_dict_key = &ictx->sb_dict_key;
2223 uint8_t *image, *iptr;
2225 /* Calculate image size. */
2226 uint32_t size = sizeof(*sb_header) +
2227 ictx->sect_count * sizeof(struct sb_sections_header) +
2228 sizeof(*sb_dict_key) + sizeof(ictx->digest);
2230 sctx = ictx->sect_head;
2236 image = malloc(size);
2241 memcpy(iptr, sb_header, sizeof(*sb_header));
2242 iptr += sizeof(*sb_header);
2244 sctx = ictx->sect_head;
2246 memcpy(iptr, &sctx->payload, sizeof(struct sb_sections_header));
2247 iptr += sizeof(struct sb_sections_header);
2251 memcpy(iptr, sb_dict_key, sizeof(*sb_dict_key));
2252 iptr += sizeof(*sb_dict_key);
2254 sctx = ictx->sect_head;
2256 cctx = sctx->cmd_head;
2258 ccmd = &cctx->payload;
2260 memcpy(iptr, &cctx->c_payload, sizeof(cctx->payload));
2261 iptr += sizeof(cctx->payload);
2263 if (ccmd->header.tag == ROM_LOAD_CMD) {
2264 memcpy(iptr, cctx->data, cctx->length);
2265 iptr += cctx->length;
2274 memcpy(iptr, ictx->digest, sizeof(ictx->digest));
2275 iptr += sizeof(ictx->digest);
2277 /* Configure the mkimage */
2278 tparams->hdr = image;
2279 tparams->header_size = size;
2284 static int mxsimage_generate(struct image_tool_params *params,
2285 struct image_type_params *tparams)
2288 struct sb_image_ctx ctx;
2290 /* Do not copy the U-Boot image! */
2291 params->skipcpy = 1;
2293 memset(&ctx, 0, sizeof(ctx));
2295 ctx.cfg_filename = params->imagename;
2296 ctx.output_filename = params->imagefile;
2298 ret = sb_build_tree_from_cfg(&ctx);
2302 ret = sb_encrypt_image(&ctx);
2304 ret = sb_build_image(&ctx, tparams);
2307 sb_free_image(&ctx);
2313 * mxsimage parameters
2315 static struct image_type_params mxsimage_params = {
2316 .name = "Freescale MXS Boot Image support",
2319 .check_image_type = mxsimage_check_image_types,
2320 .verify_header = mxsimage_verify_header,
2321 .print_header = mxsimage_print_header,
2322 .set_header = mxsimage_set_header,
2323 .check_params = mxsimage_check_params,
2324 .vrec_header = mxsimage_generate,
2327 void init_mxs_image_type(void)
2329 register_image_type(&mxsimage_params);
2333 void init_mxs_image_type(void)