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>
27 * |-Write to address command block
30 * |-ORR address with mask command block
32 * |-Write to address command block
36 #define SB_HAB_DCD_WRITE 0xccUL
37 #define SB_HAB_DCD_CHECK 0xcfUL
38 #define SB_HAB_DCD_NOOP 0xc0UL
39 #define SB_HAB_DCD_MASK_BIT (1 << 3)
40 #define SB_HAB_DCD_SET_BIT (1 << 4)
42 /* Addr.n = Value.n */
43 #define SB_DCD_WRITE \
44 (SB_HAB_DCD_WRITE << 24)
45 /* Addr.n &= ~Value.n */
47 ((SB_HAB_DCD_WRITE << 24) | SB_HAB_DCD_SET_BIT)
48 /* Addr.n |= Value.n */
50 ((SB_HAB_DCD_WRITE << 24) | SB_HAB_DCD_SET_BIT | SB_HAB_DCD_MASK_BIT)
51 /* (Addr.n & Value.n) == 0 */
52 #define SB_DCD_CHK_EQZ \
53 (SB_HAB_DCD_CHECK << 24)
54 /* (Addr.n & Value.n) == Value.n */
55 #define SB_DCD_CHK_EQ \
56 ((SB_HAB_DCD_CHECK << 24) | SB_HAB_DCD_SET_BIT)
57 /* (Addr.n & Value.n) != Value.n */
58 #define SB_DCD_CHK_NEQ \
59 ((SB_HAB_DCD_CHECK << 24) | SB_HAB_DCD_MASK_BIT)
60 /* (Addr.n & Value.n) != 0 */
61 #define SB_DCD_CHK_NEZ \
62 ((SB_HAB_DCD_CHECK << 24) | SB_HAB_DCD_SET_BIT | SB_HAB_DCD_MASK_BIT)
65 (SB_HAB_DCD_NOOP << 24)
68 struct sb_dcd_ctx
*dcd
;
74 /* Size of the whole DCD block. */
77 /* Pointer to previous DCD command block. */
78 uint32_t *prev_dcd_head
;
100 struct sb_cmd_ctx
*cmd
;
105 struct sb_command payload
;
106 struct sb_command c_payload
;
109 struct sb_section_ctx
{
115 struct sb_section_ctx
*sect
;
117 struct sb_cmd_ctx
*cmd_head
;
118 struct sb_cmd_ctx
*cmd_tail
;
120 struct sb_sections_header payload
;
123 struct sb_image_ctx
{
124 unsigned int in_section
:1;
125 unsigned int in_dcd
:1;
126 /* Image configuration */
127 unsigned int verbose_boot
:1;
128 unsigned int silent_dump
:1;
129 char *input_filename
;
130 char *output_filename
;
132 uint8_t image_key
[16];
134 /* Number of section in the image */
135 unsigned int sect_count
;
136 /* Bootable section */
137 unsigned int sect_boot
;
138 unsigned int sect_boot_found
:1;
140 struct sb_section_ctx
*sect_head
;
141 struct sb_section_ctx
*sect_tail
;
143 struct sb_dcd_ctx
*dcd_head
;
144 struct sb_dcd_ctx
*dcd_tail
;
146 EVP_CIPHER_CTX cipher_ctx
;
149 struct sb_key_dictionary_key sb_dict_key
;
151 struct sb_boot_image_header payload
;
155 * Instruction semantics:
159 * LOAD IVT address IVT_entry_point
160 * FILL address pattern length
161 * JUMP [HAB] address [r0_arg]
162 * CALL [HAB] address [r0_arg]
164 * For i.MX23, mode = USB/I2C/SPI1_FLASH/SPI2_FLASH/NAND_BCH
165 * JTAG/SPI3_EEPROM/SD_SSP0/SD_SSP1
166 * For i.MX28, mode = USB/I2C/SPI2_FLASH/SPI3_FLASH/NAND_BCH
167 * JTAG/SPI2_EEPROM/SD_SSP0/SD_SSP1
173 static int sb_aes_init(struct sb_image_ctx
*ictx
, uint8_t *iv
, int enc
)
175 EVP_CIPHER_CTX
*ctx
= &ictx
->cipher_ctx
;
178 /* If there is no init vector, init vector is all zeroes. */
180 iv
= ictx
->image_key
;
182 EVP_CIPHER_CTX_init(ctx
);
183 ret
= EVP_CipherInit(ctx
, EVP_aes_128_cbc(), ictx
->image_key
, iv
, enc
);
185 EVP_CIPHER_CTX_set_padding(ctx
, 0);
189 static int sb_aes_crypt(struct sb_image_ctx
*ictx
, uint8_t *in_data
,
190 uint8_t *out_data
, int in_len
)
192 EVP_CIPHER_CTX
*ctx
= &ictx
->cipher_ctx
;
196 outbuf
= malloc(in_len
);
199 memset(outbuf
, 0, sizeof(in_len
));
201 ret
= EVP_CipherUpdate(ctx
, outbuf
, &outlen
, in_data
, in_len
);
208 memcpy(out_data
, outbuf
, outlen
);
215 static int sb_aes_deinit(EVP_CIPHER_CTX
*ctx
)
217 return EVP_CIPHER_CTX_cleanup(ctx
);
220 static int sb_aes_reinit(struct sb_image_ctx
*ictx
, int enc
)
223 EVP_CIPHER_CTX
*ctx
= &ictx
->cipher_ctx
;
224 struct sb_boot_image_header
*sb_header
= &ictx
->payload
;
225 uint8_t *iv
= sb_header
->iv
;
227 ret
= sb_aes_deinit(ctx
);
230 return sb_aes_init(ictx
, iv
, enc
);
236 static uint32_t crc32(uint8_t *data
, uint32_t len
)
238 const uint32_t poly
= 0x04c11db7;
239 uint32_t crc32
= 0xffffffff;
240 unsigned int byte
, bit
;
242 for (byte
= 0; byte
< len
; byte
++) {
243 crc32
^= data
[byte
] << 24;
245 for (bit
= 8; bit
> 0; bit
--) {
246 if (crc32
& (1UL << 31))
247 crc32
= (crc32
<< 1) ^ poly
;
249 crc32
= (crc32
<< 1);
259 static void soprintf(struct sb_image_ctx
*ictx
, const char *fmt
, ...)
263 if (ictx
->silent_dump
)
267 vfprintf(stdout
, fmt
, ap
);
274 static time_t sb_get_timestamp(void)
276 struct tm time_2000
= {
277 .tm_yday
= 1, /* Jan. 1st */
278 .tm_year
= 100, /* 2000 */
280 time_t seconds_to_2000
= mktime(&time_2000
);
281 time_t seconds_to_now
= time(NULL
);
283 return seconds_to_now
- seconds_to_2000
;
286 static int sb_get_time(time_t time
, struct tm
*tm
)
288 struct tm time_2000
= {
289 .tm_yday
= 1, /* Jan. 1st */
290 .tm_year
= 0, /* 1900 */
292 const time_t seconds_to_2000
= mktime(&time_2000
);
293 const time_t seconds_to_now
= seconds_to_2000
+ time
;
295 ret
= gmtime_r(&seconds_to_now
, tm
);
296 return ret
? 0 : -EINVAL
;
299 static void sb_encrypt_sb_header(struct sb_image_ctx
*ictx
)
301 EVP_MD_CTX
*md_ctx
= &ictx
->md_ctx
;
302 struct sb_boot_image_header
*sb_header
= &ictx
->payload
;
303 uint8_t *sb_header_ptr
= (uint8_t *)sb_header
;
305 /* Encrypt the header, compute the digest. */
306 sb_aes_crypt(ictx
, sb_header_ptr
, NULL
, sizeof(*sb_header
));
307 EVP_DigestUpdate(md_ctx
, sb_header_ptr
, sizeof(*sb_header
));
310 static void sb_encrypt_sb_sections_header(struct sb_image_ctx
*ictx
)
312 EVP_MD_CTX
*md_ctx
= &ictx
->md_ctx
;
313 struct sb_section_ctx
*sctx
= ictx
->sect_head
;
314 struct sb_sections_header
*shdr
;
315 uint8_t *sb_sections_header_ptr
;
316 const int size
= sizeof(*shdr
);
319 shdr
= &sctx
->payload
;
320 sb_sections_header_ptr
= (uint8_t *)shdr
;
322 sb_aes_crypt(ictx
, sb_sections_header_ptr
,
323 ictx
->sb_dict_key
.cbc_mac
, size
);
324 EVP_DigestUpdate(md_ctx
, sb_sections_header_ptr
, size
);
330 static void sb_encrypt_key_dictionary_key(struct sb_image_ctx
*ictx
)
332 EVP_MD_CTX
*md_ctx
= &ictx
->md_ctx
;
334 sb_aes_crypt(ictx
, ictx
->image_key
, ictx
->sb_dict_key
.key
,
335 sizeof(ictx
->sb_dict_key
.key
));
336 EVP_DigestUpdate(md_ctx
, &ictx
->sb_dict_key
, sizeof(ictx
->sb_dict_key
));
339 static void sb_decrypt_key_dictionary_key(struct sb_image_ctx
*ictx
)
341 EVP_MD_CTX
*md_ctx
= &ictx
->md_ctx
;
343 EVP_DigestUpdate(md_ctx
, &ictx
->sb_dict_key
, sizeof(ictx
->sb_dict_key
));
344 sb_aes_crypt(ictx
, ictx
->sb_dict_key
.key
, ictx
->image_key
,
345 sizeof(ictx
->sb_dict_key
.key
));
348 static void sb_encrypt_tag(struct sb_image_ctx
*ictx
,
349 struct sb_cmd_ctx
*cctx
)
351 EVP_MD_CTX
*md_ctx
= &ictx
->md_ctx
;
352 struct sb_command
*cmd
= &cctx
->payload
;
354 sb_aes_crypt(ictx
, (uint8_t *)cmd
,
355 (uint8_t *)&cctx
->c_payload
, sizeof(*cmd
));
356 EVP_DigestUpdate(md_ctx
, &cctx
->c_payload
, sizeof(*cmd
));
359 static int sb_encrypt_image(struct sb_image_ctx
*ictx
)
361 /* Start image-wide crypto. */
362 EVP_MD_CTX_init(&ictx
->md_ctx
);
363 EVP_DigestInit(&ictx
->md_ctx
, EVP_sha1());
368 sb_aes_init(ictx
, NULL
, 1);
369 sb_encrypt_sb_header(ictx
);
372 * SB sections header.
374 sb_encrypt_sb_sections_header(ictx
);
379 sb_aes_reinit(ictx
, 1);
380 sb_encrypt_key_dictionary_key(ictx
);
385 struct sb_cmd_ctx
*cctx
;
386 struct sb_command
*ccmd
;
387 struct sb_section_ctx
*sctx
= ictx
->sect_head
;
390 cctx
= sctx
->cmd_head
;
392 sb_aes_reinit(ictx
, 1);
395 ccmd
= &cctx
->payload
;
397 sb_encrypt_tag(ictx
, cctx
);
399 if (ccmd
->header
.tag
== ROM_TAG_CMD
) {
400 sb_aes_reinit(ictx
, 1);
401 } else if (ccmd
->header
.tag
== ROM_LOAD_CMD
) {
402 sb_aes_crypt(ictx
, cctx
->data
, cctx
->data
,
404 EVP_DigestUpdate(&ictx
->md_ctx
, cctx
->data
,
415 * Dump the SHA1 of the whole image.
417 sb_aes_reinit(ictx
, 1);
419 EVP_DigestFinal(&ictx
->md_ctx
, ictx
->digest
, NULL
);
420 sb_aes_crypt(ictx
, ictx
->digest
, ictx
->digest
, sizeof(ictx
->digest
));
422 /* Stop the encryption session. */
423 sb_aes_deinit(&ictx
->cipher_ctx
);
428 static int sb_load_file(struct sb_cmd_ctx
*cctx
, char *filename
)
430 long real_size
, roundup_size
;
437 fprintf(stderr
, "ERR: Missing filename!\n");
441 fp
= fopen(filename
, "r");
445 ret
= fseek(fp
, 0, SEEK_END
);
449 real_size
= ftell(fp
);
453 ret
= fseek(fp
, 0, SEEK_SET
);
457 roundup_size
= roundup(real_size
, SB_BLOCK_SIZE
);
458 data
= calloc(1, roundup_size
);
462 size
= fread(data
, 1, real_size
, fp
);
463 if (size
!= (unsigned long)real_size
)
467 cctx
->length
= roundup_size
;
477 fprintf(stderr
, "ERR: Failed to load file \"%s\"\n", filename
);
481 static uint8_t sb_command_checksum(struct sb_command
*inst
)
483 uint8_t *inst_ptr
= (uint8_t *)inst
;
487 for (i
= 0; i
< sizeof(struct sb_command
); i
++)
493 static int sb_token_to_long(char *tok
, uint32_t *rid
)
498 if (tok
[0] != '0' || tok
[1] != 'x') {
499 fprintf(stderr
, "ERR: Invalid hexadecimal number!\n");
505 id
= strtoul(tok
, &endptr
, 16);
506 if ((errno
== ERANGE
&& id
== ULONG_MAX
) || (errno
!= 0 && id
== 0)) {
507 fprintf(stderr
, "ERR: Value can't be decoded!\n");
511 /* Check for 32-bit overflow. */
512 if (id
> 0xffffffff) {
513 fprintf(stderr
, "ERR: Value too big!\n");
518 fprintf(stderr
, "ERR: Deformed value!\n");
526 static int sb_grow_dcd(struct sb_dcd_ctx
*dctx
, unsigned int inc_size
)
533 dctx
->size
+= inc_size
;
534 tmp
= realloc(dctx
->payload
, dctx
->size
);
540 /* Assemble and update the HAB DCD header. */
541 dctx
->payload
[0] = htonl((SB_HAB_DCD_TAG
<< 24) |
548 static int sb_build_dcd(struct sb_image_ctx
*ictx
, struct sb_cmd_list
*cmd
)
550 struct sb_dcd_ctx
*dctx
;
556 dctx
= calloc(1, sizeof(*dctx
));
560 ret
= sb_grow_dcd(dctx
, 4);
564 /* Read DCD block number. */
565 tok
= strtok(cmd
->cmd
, " ");
567 fprintf(stderr
, "#%i ERR: DCD block without number!\n",
573 /* Parse the DCD block number. */
574 ret
= sb_token_to_long(tok
, &id
);
576 fprintf(stderr
, "#%i ERR: Malformed DCD block number!\n",
584 * The DCD block is now constructed. Append it to the list.
585 * WARNING: The DCD size is still not computed and will be
586 * updated while parsing it's commands.
588 if (!ictx
->dcd_head
) {
589 ictx
->dcd_head
= dctx
;
590 ictx
->dcd_tail
= dctx
;
592 ictx
->dcd_tail
->dcd
= dctx
;
593 ictx
->dcd_tail
= dctx
;
604 static int sb_build_dcd_block(struct sb_image_ctx
*ictx
,
605 struct sb_cmd_list
*cmd
,
609 uint32_t address
, value
, length
;
612 struct sb_dcd_ctx
*dctx
= ictx
->dcd_tail
;
615 if (dctx
->prev_dcd_head
&& (type
!= SB_DCD_NOOP
) &&
616 ((dctx
->prev_dcd_head
[0] & 0xff0000ff) == type
)) {
617 /* Same instruction as before, just append it. */
618 ret
= sb_grow_dcd(dctx
, 8);
621 } else if (type
== SB_DCD_NOOP
) {
622 ret
= sb_grow_dcd(dctx
, 4);
626 /* Update DCD command block pointer. */
627 dctx
->prev_dcd_head
= dctx
->payload
+
628 dctx
->size
/ sizeof(*dctx
->payload
) - 1;
630 /* NOOP has only 4 bytes and no payload. */
634 * Either a different instruction block started now
635 * or this is the first instruction block.
637 ret
= sb_grow_dcd(dctx
, 12);
641 /* Update DCD command block pointer. */
642 dctx
->prev_dcd_head
= dctx
->payload
+
643 dctx
->size
/ sizeof(*dctx
->payload
) - 3;
646 dcd
= dctx
->payload
+ dctx
->size
/ sizeof(*dctx
->payload
) - 2;
649 * Prepare the command.
651 tok
= strtok(cmd
->cmd
, " ");
653 fprintf(stderr
, "#%i ERR: Missing DCD address!\n",
659 /* Read DCD destination address. */
660 ret
= sb_token_to_long(tok
, &address
);
662 fprintf(stderr
, "#%i ERR: Incorrect DCD address!\n",
667 tok
= strtok(NULL
, " ");
669 fprintf(stderr
, "#%i ERR: Missing DCD value!\n",
675 /* Read DCD operation value. */
676 ret
= sb_token_to_long(tok
, &value
);
678 fprintf(stderr
, "#%i ERR: Incorrect DCD value!\n",
683 /* Fill in the new DCD entry. */
684 dcd
[0] = htonl(address
);
685 dcd
[1] = htonl(value
);
688 /* Update the DCD command block. */
689 length
= dctx
->size
-
690 ((dctx
->prev_dcd_head
- dctx
->payload
) *
691 sizeof(*dctx
->payload
));
692 dctx
->prev_dcd_head
[0] = htonl(type
| (length
<< 8));
698 static int sb_build_section(struct sb_image_ctx
*ictx
, struct sb_cmd_list
*cmd
)
700 struct sb_section_ctx
*sctx
;
701 struct sb_sections_header
*shdr
;
703 uint32_t bootable
= 0;
707 sctx
= calloc(1, sizeof(*sctx
));
711 /* Read section number. */
712 tok
= strtok(cmd
->cmd
, " ");
714 fprintf(stderr
, "#%i ERR: Section without number!\n",
720 /* Parse the section number. */
721 ret
= sb_token_to_long(tok
, &id
);
723 fprintf(stderr
, "#%i ERR: Malformed section number!\n",
728 /* Read section's BOOTABLE flag. */
729 tok
= strtok(NULL
, " ");
730 if (tok
&& (strlen(tok
) == 8) && !strncmp(tok
, "BOOTABLE", 8))
731 bootable
= SB_SECTION_FLAG_BOOTABLE
;
733 sctx
->boot
= bootable
;
735 shdr
= &sctx
->payload
;
736 shdr
->section_number
= id
;
737 shdr
->section_flags
= bootable
;
740 * The section is now constructed. Append it to the list.
741 * WARNING: The section size is still not computed and will
742 * be updated while parsing it's commands.
746 /* Mark that this section is bootable one. */
748 if (ictx
->sect_boot_found
) {
750 "#%i WARN: Multiple bootable section!\n",
753 ictx
->sect_boot
= id
;
754 ictx
->sect_boot_found
= 1;
758 if (!ictx
->sect_head
) {
759 ictx
->sect_head
= sctx
;
760 ictx
->sect_tail
= sctx
;
762 ictx
->sect_tail
->sect
= sctx
;
763 ictx
->sect_tail
= sctx
;
773 static int sb_build_command_nop(struct sb_image_ctx
*ictx
)
775 struct sb_section_ctx
*sctx
= ictx
->sect_tail
;
776 struct sb_cmd_ctx
*cctx
;
777 struct sb_command
*ccmd
;
779 cctx
= calloc(1, sizeof(*cctx
));
783 ccmd
= &cctx
->payload
;
786 * Construct the command.
788 ccmd
->header
.checksum
= 0x5a;
789 ccmd
->header
.tag
= ROM_NOP_CMD
;
791 cctx
->size
= sizeof(*ccmd
);
794 * Append the command to the last section.
796 if (!sctx
->cmd_head
) {
797 sctx
->cmd_head
= cctx
;
798 sctx
->cmd_tail
= cctx
;
800 sctx
->cmd_tail
->cmd
= cctx
;
801 sctx
->cmd_tail
= cctx
;
807 static int sb_build_command_tag(struct sb_image_ctx
*ictx
,
808 struct sb_cmd_list
*cmd
)
810 struct sb_section_ctx
*sctx
= ictx
->sect_tail
;
811 struct sb_cmd_ctx
*cctx
;
812 struct sb_command
*ccmd
;
815 cctx
= calloc(1, sizeof(*cctx
));
819 ccmd
= &cctx
->payload
;
822 * Prepare the command.
824 /* Check for the LAST keyword. */
825 tok
= strtok(cmd
->cmd
, " ");
826 if (tok
&& !strcmp(tok
, "LAST"))
827 ccmd
->header
.flags
= ROM_TAG_CMD_FLAG_ROM_LAST_TAG
;
830 * Construct the command.
832 ccmd
->header
.checksum
= 0x5a;
833 ccmd
->header
.tag
= ROM_TAG_CMD
;
835 cctx
->size
= sizeof(*ccmd
);
838 * Append the command to the last section.
840 if (!sctx
->cmd_head
) {
841 sctx
->cmd_head
= cctx
;
842 sctx
->cmd_tail
= cctx
;
844 sctx
->cmd_tail
->cmd
= cctx
;
845 sctx
->cmd_tail
= cctx
;
851 static int sb_build_command_load(struct sb_image_ctx
*ictx
,
852 struct sb_cmd_list
*cmd
)
854 struct sb_section_ctx
*sctx
= ictx
->sect_tail
;
855 struct sb_cmd_ctx
*cctx
;
856 struct sb_command
*ccmd
;
858 int ret
, is_ivt
= 0, is_dcd
= 0;
859 uint32_t dest
, dcd
= 0;
861 cctx
= calloc(1, sizeof(*cctx
));
865 ccmd
= &cctx
->payload
;
868 * Prepare the command.
870 tok
= strtok(cmd
->cmd
, " ");
872 fprintf(stderr
, "#%i ERR: Missing LOAD address or 'IVT'!\n",
878 /* Check for "IVT" flag. */
879 if (!strcmp(tok
, "IVT"))
881 if (!strcmp(tok
, "DCD"))
883 if (is_ivt
|| is_dcd
) {
884 tok
= strtok(NULL
, " ");
886 fprintf(stderr
, "#%i ERR: Missing LOAD address!\n",
893 /* Read load destination address. */
894 ret
= sb_token_to_long(tok
, &dest
);
896 fprintf(stderr
, "#%i ERR: Incorrect LOAD address!\n",
901 /* Read filename or IVT entrypoint or DCD block ID. */
902 tok
= strtok(NULL
, " ");
905 "#%i ERR: Missing LOAD filename or IVT ep or DCD block ID!\n",
913 struct sb_ivt_header
*ivt
;
915 ret
= sb_token_to_long(tok
, &ivtep
);
919 "#%i ERR: Incorrect IVT entry point!\n",
924 ivt
= calloc(1, sizeof(*ivt
));
930 ivt
->header
= sb_hab_ivt_header();
934 cctx
->data
= (uint8_t *)ivt
;
935 cctx
->length
= sizeof(*ivt
);
937 struct sb_dcd_ctx
*dctx
= ictx
->dcd_head
;
941 ret
= sb_token_to_long(tok
, &dcdid
);
945 "#%i ERR: Incorrect DCD block ID!\n",
951 if (dctx
->id
== dcdid
)
957 fprintf(stderr
, "#%i ERR: DCD block %08x not found!\n",
962 asize
= roundup(dctx
->size
, SB_BLOCK_SIZE
);
963 payload
= calloc(1, asize
);
969 memcpy(payload
, dctx
->payload
, dctx
->size
);
971 cctx
->data
= payload
;
972 cctx
->length
= asize
;
974 /* Set the Load DCD flag. */
975 dcd
= ROM_LOAD_CMD_FLAG_DCD_LOAD
;
977 /* Regular LOAD of a file. */
978 ret
= sb_load_file(cctx
, tok
);
980 fprintf(stderr
, "#%i ERR: Cannot load '%s'!\n",
986 if (cctx
->length
& (SB_BLOCK_SIZE
- 1)) {
987 fprintf(stderr
, "#%i ERR: Unaligned payload!\n",
992 * Construct the command.
994 ccmd
->header
.checksum
= 0x5a;
995 ccmd
->header
.tag
= ROM_LOAD_CMD
;
996 ccmd
->header
.flags
= dcd
;
998 ccmd
->load
.address
= dest
;
999 ccmd
->load
.count
= cctx
->length
;
1000 ccmd
->load
.crc32
= crc32(cctx
->data
, cctx
->length
);
1002 cctx
->size
= sizeof(*ccmd
) + cctx
->length
;
1005 * Append the command to the last section.
1007 if (!sctx
->cmd_head
) {
1008 sctx
->cmd_head
= cctx
;
1009 sctx
->cmd_tail
= cctx
;
1011 sctx
->cmd_tail
->cmd
= cctx
;
1012 sctx
->cmd_tail
= cctx
;
1022 static int sb_build_command_fill(struct sb_image_ctx
*ictx
,
1023 struct sb_cmd_list
*cmd
)
1025 struct sb_section_ctx
*sctx
= ictx
->sect_tail
;
1026 struct sb_cmd_ctx
*cctx
;
1027 struct sb_command
*ccmd
;
1029 uint32_t address
, pattern
, length
;
1032 cctx
= calloc(1, sizeof(*cctx
));
1036 ccmd
= &cctx
->payload
;
1039 * Prepare the command.
1041 tok
= strtok(cmd
->cmd
, " ");
1043 fprintf(stderr
, "#%i ERR: Missing FILL address!\n",
1049 /* Read fill destination address. */
1050 ret
= sb_token_to_long(tok
, &address
);
1052 fprintf(stderr
, "#%i ERR: Incorrect FILL address!\n",
1057 tok
= strtok(NULL
, " ");
1059 fprintf(stderr
, "#%i ERR: Missing FILL pattern!\n",
1065 /* Read fill pattern address. */
1066 ret
= sb_token_to_long(tok
, &pattern
);
1068 fprintf(stderr
, "#%i ERR: Incorrect FILL pattern!\n",
1073 tok
= strtok(NULL
, " ");
1075 fprintf(stderr
, "#%i ERR: Missing FILL length!\n",
1081 /* Read fill pattern address. */
1082 ret
= sb_token_to_long(tok
, &length
);
1084 fprintf(stderr
, "#%i ERR: Incorrect FILL length!\n",
1090 * Construct the command.
1092 ccmd
->header
.checksum
= 0x5a;
1093 ccmd
->header
.tag
= ROM_FILL_CMD
;
1095 ccmd
->fill
.address
= address
;
1096 ccmd
->fill
.count
= length
;
1097 ccmd
->fill
.pattern
= pattern
;
1099 cctx
->size
= sizeof(*ccmd
);
1102 * Append the command to the last section.
1104 if (!sctx
->cmd_head
) {
1105 sctx
->cmd_head
= cctx
;
1106 sctx
->cmd_tail
= cctx
;
1108 sctx
->cmd_tail
->cmd
= cctx
;
1109 sctx
->cmd_tail
= cctx
;
1119 static int sb_build_command_jump_call(struct sb_image_ctx
*ictx
,
1120 struct sb_cmd_list
*cmd
,
1121 unsigned int is_call
)
1123 struct sb_section_ctx
*sctx
= ictx
->sect_tail
;
1124 struct sb_cmd_ctx
*cctx
;
1125 struct sb_command
*ccmd
;
1127 uint32_t dest
, arg
= 0x0;
1130 const char *cmdname
= is_call
? "CALL" : "JUMP";
1132 cctx
= calloc(1, sizeof(*cctx
));
1136 ccmd
= &cctx
->payload
;
1139 * Prepare the command.
1141 tok
= strtok(cmd
->cmd
, " ");
1144 "#%i ERR: Missing %s address or 'HAB'!\n",
1145 cmd
->lineno
, cmdname
);
1150 /* Check for "HAB" flag. */
1151 if (!strcmp(tok
, "HAB")) {
1152 hab
= is_call
? ROM_CALL_CMD_FLAG_HAB
: ROM_JUMP_CMD_FLAG_HAB
;
1153 tok
= strtok(NULL
, " ");
1155 fprintf(stderr
, "#%i ERR: Missing %s address!\n",
1156 cmd
->lineno
, cmdname
);
1161 /* Read load destination address. */
1162 ret
= sb_token_to_long(tok
, &dest
);
1164 fprintf(stderr
, "#%i ERR: Incorrect %s address!\n",
1165 cmd
->lineno
, cmdname
);
1169 tok
= strtok(NULL
, " ");
1171 ret
= sb_token_to_long(tok
, &arg
);
1174 "#%i ERR: Incorrect %s argument!\n",
1175 cmd
->lineno
, cmdname
);
1181 * Construct the command.
1183 ccmd
->header
.checksum
= 0x5a;
1184 ccmd
->header
.tag
= is_call
? ROM_CALL_CMD
: ROM_JUMP_CMD
;
1185 ccmd
->header
.flags
= hab
;
1187 ccmd
->call
.address
= dest
;
1188 ccmd
->call
.argument
= arg
;
1190 cctx
->size
= sizeof(*ccmd
);
1193 * Append the command to the last section.
1195 if (!sctx
->cmd_head
) {
1196 sctx
->cmd_head
= cctx
;
1197 sctx
->cmd_tail
= cctx
;
1199 sctx
->cmd_tail
->cmd
= cctx
;
1200 sctx
->cmd_tail
= cctx
;
1210 static int sb_build_command_jump(struct sb_image_ctx
*ictx
,
1211 struct sb_cmd_list
*cmd
)
1213 return sb_build_command_jump_call(ictx
, cmd
, 0);
1216 static int sb_build_command_call(struct sb_image_ctx
*ictx
,
1217 struct sb_cmd_list
*cmd
)
1219 return sb_build_command_jump_call(ictx
, cmd
, 1);
1222 static int sb_build_command_mode(struct sb_image_ctx
*ictx
,
1223 struct sb_cmd_list
*cmd
)
1225 struct sb_section_ctx
*sctx
= ictx
->sect_tail
;
1226 struct sb_cmd_ctx
*cctx
;
1227 struct sb_command
*ccmd
;
1231 uint32_t mode
= 0xffffffff;
1233 cctx
= calloc(1, sizeof(*cctx
));
1237 ccmd
= &cctx
->payload
;
1240 * Prepare the command.
1242 tok
= strtok(cmd
->cmd
, " ");
1244 fprintf(stderr
, "#%i ERR: Missing MODE boot mode argument!\n",
1250 for (i
= 0; i
< ARRAY_SIZE(modetable
); i
++) {
1251 if (!strcmp(tok
, modetable
[i
].name
)) {
1252 mode
= modetable
[i
].mode
;
1256 if (!modetable
[i
].altname
)
1259 if (!strcmp(tok
, modetable
[i
].altname
)) {
1260 mode
= modetable
[i
].mode
;
1265 if (mode
== 0xffffffff) {
1266 fprintf(stderr
, "#%i ERR: Invalid MODE boot mode argument!\n",
1273 * Construct the command.
1275 ccmd
->header
.checksum
= 0x5a;
1276 ccmd
->header
.tag
= ROM_MODE_CMD
;
1278 ccmd
->mode
.mode
= mode
;
1280 cctx
->size
= sizeof(*ccmd
);
1283 * Append the command to the last section.
1285 if (!sctx
->cmd_head
) {
1286 sctx
->cmd_head
= cctx
;
1287 sctx
->cmd_tail
= cctx
;
1289 sctx
->cmd_tail
->cmd
= cctx
;
1290 sctx
->cmd_tail
= cctx
;
1300 static int sb_prefill_image_header(struct sb_image_ctx
*ictx
)
1302 struct sb_boot_image_header
*hdr
= &ictx
->payload
;
1304 /* Fill signatures */
1305 memcpy(hdr
->signature1
, "STMP", 4);
1306 memcpy(hdr
->signature2
, "sgtl", 4);
1308 /* SB Image version 1.1 */
1309 hdr
->major_version
= SB_VERSION_MAJOR
;
1310 hdr
->minor_version
= SB_VERSION_MINOR
;
1312 /* Boot image major version */
1313 hdr
->product_version
.major
= htons(0x999);
1314 hdr
->product_version
.minor
= htons(0x999);
1315 hdr
->product_version
.revision
= htons(0x999);
1316 /* Boot image major version */
1317 hdr
->component_version
.major
= htons(0x999);
1318 hdr
->component_version
.minor
= htons(0x999);
1319 hdr
->component_version
.revision
= htons(0x999);
1321 /* Drive tag must be 0x0 for i.MX23 */
1324 hdr
->header_blocks
=
1325 sizeof(struct sb_boot_image_header
) / SB_BLOCK_SIZE
;
1326 hdr
->section_header_size
=
1327 sizeof(struct sb_sections_header
) / SB_BLOCK_SIZE
;
1328 hdr
->timestamp_us
= sb_get_timestamp() * 1000000;
1330 /* FIXME -- add proper config option */
1331 hdr
->flags
= ictx
->verbose_boot
? SB_IMAGE_FLAG_VERBOSE
: 0,
1333 /* FIXME -- We support only default key */
1339 static int sb_postfill_image_header(struct sb_image_ctx
*ictx
)
1341 struct sb_boot_image_header
*hdr
= &ictx
->payload
;
1342 struct sb_section_ctx
*sctx
= ictx
->sect_head
;
1343 uint32_t kd_size
, sections_blocks
;
1346 /* The main SB header size in blocks. */
1347 hdr
->image_blocks
= hdr
->header_blocks
;
1349 /* Size of the key dictionary, which has single zero entry. */
1350 kd_size
= hdr
->key_count
* sizeof(struct sb_key_dictionary_key
);
1351 hdr
->image_blocks
+= kd_size
/ SB_BLOCK_SIZE
;
1353 /* Now count the payloads. */
1354 hdr
->section_count
= ictx
->sect_count
;
1356 hdr
->image_blocks
+= sctx
->size
/ SB_BLOCK_SIZE
;
1360 if (!ictx
->sect_boot_found
) {
1361 fprintf(stderr
, "ERR: No bootable section selected!\n");
1364 hdr
->first_boot_section_id
= ictx
->sect_boot
;
1366 /* The n * SB section size in blocks. */
1367 sections_blocks
= hdr
->section_count
* hdr
->section_header_size
;
1368 hdr
->image_blocks
+= sections_blocks
;
1370 /* Key dictionary offset. */
1371 hdr
->key_dictionary_block
= hdr
->header_blocks
+ sections_blocks
;
1373 /* Digest of the whole image. */
1374 hdr
->image_blocks
+= 2;
1376 /* Pointer past the dictionary. */
1377 hdr
->first_boot_tag_block
=
1378 hdr
->key_dictionary_block
+ kd_size
/ SB_BLOCK_SIZE
;
1380 /* Compute header digest. */
1381 EVP_MD_CTX_init(&md_ctx
);
1383 EVP_DigestInit(&md_ctx
, EVP_sha1());
1384 EVP_DigestUpdate(&md_ctx
, hdr
->signature1
,
1385 sizeof(struct sb_boot_image_header
) -
1386 sizeof(hdr
->digest
));
1387 EVP_DigestFinal(&md_ctx
, hdr
->digest
, NULL
);
1392 static int sb_fixup_sections_and_tags(struct sb_image_ctx
*ictx
)
1394 /* Fixup the placement of sections. */
1395 struct sb_boot_image_header
*ihdr
= &ictx
->payload
;
1396 struct sb_section_ctx
*sctx
= ictx
->sect_head
;
1397 struct sb_sections_header
*shdr
;
1398 struct sb_cmd_ctx
*cctx
;
1399 struct sb_command
*ccmd
;
1400 uint32_t offset
= ihdr
->first_boot_tag_block
;
1403 shdr
= &sctx
->payload
;
1405 /* Fill in the section TAG offset. */
1406 shdr
->section_offset
= offset
+ 1;
1407 offset
+= shdr
->section_size
;
1409 /* Section length is measured from the TAG block. */
1410 shdr
->section_size
--;
1412 /* Fixup the TAG command. */
1413 cctx
= sctx
->cmd_head
;
1415 ccmd
= &cctx
->payload
;
1416 if (ccmd
->header
.tag
== ROM_TAG_CMD
) {
1417 ccmd
->tag
.section_number
= shdr
->section_number
;
1418 ccmd
->tag
.section_length
= shdr
->section_size
;
1419 ccmd
->tag
.section_flags
= shdr
->section_flags
;
1422 /* Update the command checksum. */
1423 ccmd
->header
.checksum
= sb_command_checksum(ccmd
);
1434 static int sb_parse_line(struct sb_image_ctx
*ictx
, struct sb_cmd_list
*cmd
)
1437 char *line
= cmd
->cmd
;
1441 /* Analyze the identifier on this line first. */
1442 tok
= strtok_r(line
, " ", &rptr
);
1443 if (!tok
|| (strlen(tok
) == 0)) {
1444 fprintf(stderr
, "#%i ERR: Invalid line!\n", cmd
->lineno
);
1451 if (!strcmp(tok
, "DCD")) {
1452 ictx
->in_section
= 0;
1454 sb_build_dcd(ictx
, cmd
);
1459 if (!strcmp(tok
, "SECTION")) {
1460 ictx
->in_section
= 1;
1462 sb_build_section(ictx
, cmd
);
1466 if (!ictx
->in_section
&& !ictx
->in_dcd
) {
1467 fprintf(stderr
, "#%i ERR: Data outside of a section!\n",
1472 if (ictx
->in_section
) {
1473 /* Section commands */
1474 if (!strcmp(tok
, "NOP")) {
1475 ret
= sb_build_command_nop(ictx
);
1476 } else if (!strcmp(tok
, "TAG")) {
1477 ret
= sb_build_command_tag(ictx
, cmd
);
1478 } else if (!strcmp(tok
, "LOAD")) {
1479 ret
= sb_build_command_load(ictx
, cmd
);
1480 } else if (!strcmp(tok
, "FILL")) {
1481 ret
= sb_build_command_fill(ictx
, cmd
);
1482 } else if (!strcmp(tok
, "JUMP")) {
1483 ret
= sb_build_command_jump(ictx
, cmd
);
1484 } else if (!strcmp(tok
, "CALL")) {
1485 ret
= sb_build_command_call(ictx
, cmd
);
1486 } else if (!strcmp(tok
, "MODE")) {
1487 ret
= sb_build_command_mode(ictx
, cmd
);
1490 "#%i ERR: Unsupported instruction '%s'!\n",
1494 } else if (ictx
->in_dcd
) {
1496 uint32_t ilen
= '1';
1498 tok
= strtok_r(tok
, ".", &lptr
);
1499 if (!tok
|| (strlen(tok
) == 0) || (lptr
&& strlen(lptr
) != 1)) {
1500 fprintf(stderr
, "#%i ERR: Invalid line!\n",
1506 (lptr
[0] != '1' && lptr
[0] != '2' && lptr
[0] != '4')) {
1507 fprintf(stderr
, "#%i ERR: Invalid instruction width!\n",
1513 ilen
= lptr
[0] - '1';
1516 if (!strcmp(tok
, "WRITE")) {
1517 ret
= sb_build_dcd_block(ictx
, cmd
,
1518 SB_DCD_WRITE
| ilen
);
1519 } else if (!strcmp(tok
, "ANDC")) {
1520 ret
= sb_build_dcd_block(ictx
, cmd
,
1521 SB_DCD_ANDC
| ilen
);
1522 } else if (!strcmp(tok
, "ORR")) {
1523 ret
= sb_build_dcd_block(ictx
, cmd
,
1525 } else if (!strcmp(tok
, "EQZ")) {
1526 ret
= sb_build_dcd_block(ictx
, cmd
,
1527 SB_DCD_CHK_EQZ
| ilen
);
1528 } else if (!strcmp(tok
, "EQ")) {
1529 ret
= sb_build_dcd_block(ictx
, cmd
,
1530 SB_DCD_CHK_EQ
| ilen
);
1531 } else if (!strcmp(tok
, "NEQ")) {
1532 ret
= sb_build_dcd_block(ictx
, cmd
,
1533 SB_DCD_CHK_NEQ
| ilen
);
1534 } else if (!strcmp(tok
, "NEZ")) {
1535 ret
= sb_build_dcd_block(ictx
, cmd
,
1536 SB_DCD_CHK_NEZ
| ilen
);
1537 } else if (!strcmp(tok
, "NOOP")) {
1538 ret
= sb_build_dcd_block(ictx
, cmd
, SB_DCD_NOOP
);
1541 "#%i ERR: Unsupported instruction '%s'!\n",
1546 fprintf(stderr
, "#%i ERR: Unsupported instruction '%s'!\n",
1552 * Here we have at least one section with one command, otherwise we
1553 * would have failed already higher above.
1555 * FIXME -- should the updating happen here ?
1557 if (ictx
->in_section
&& !ret
) {
1558 ictx
->sect_tail
->size
+= ictx
->sect_tail
->cmd_tail
->size
;
1559 ictx
->sect_tail
->payload
.section_size
=
1560 ictx
->sect_tail
->size
/ SB_BLOCK_SIZE
;
1566 static int sb_load_cmdfile(struct sb_image_ctx
*ictx
)
1568 struct sb_cmd_list cmd
;
1575 fp
= fopen(ictx
->cfg_filename
, "r");
1579 while ((rlen
= getline(&line
, &len
, fp
)) > 0) {
1580 memset(&cmd
, 0, sizeof(cmd
));
1582 /* Strip the trailing newline. */
1583 line
[rlen
- 1] = '\0';
1587 cmd
.lineno
= lineno
++;
1589 sb_parse_line(ictx
, &cmd
);
1600 fprintf(stderr
, "ERR: Failed to load file \"%s\"\n",
1601 ictx
->cfg_filename
);
1605 static int sb_build_tree_from_cfg(struct sb_image_ctx
*ictx
)
1609 ret
= sb_load_cmdfile(ictx
);
1613 ret
= sb_prefill_image_header(ictx
);
1617 ret
= sb_postfill_image_header(ictx
);
1621 ret
= sb_fixup_sections_and_tags(ictx
);
1628 static int sb_verify_image_header(struct sb_image_ctx
*ictx
,
1629 FILE *fp
, long fsize
)
1631 /* Verify static fields in the image header. */
1632 struct sb_boot_image_header
*hdr
= &ictx
->payload
;
1633 const char *stat
[2] = { "[PASS]", "[FAIL]" };
1636 unsigned char digest
[20];
1640 /* Start image-wide crypto. */
1641 EVP_MD_CTX_init(&ictx
->md_ctx
);
1642 EVP_DigestInit(&ictx
->md_ctx
, EVP_sha1());
1644 soprintf(ictx
, "---------- Verifying SB Image Header ----------\n");
1646 size
= fread(&ictx
->payload
, 1, sizeof(ictx
->payload
), fp
);
1647 if (size
!= sizeof(ictx
->payload
)) {
1648 fprintf(stderr
, "ERR: SB image header too short!\n");
1652 /* Compute header digest. */
1653 EVP_MD_CTX_init(&md_ctx
);
1654 EVP_DigestInit(&md_ctx
, EVP_sha1());
1655 EVP_DigestUpdate(&md_ctx
, hdr
->signature1
,
1656 sizeof(struct sb_boot_image_header
) -
1657 sizeof(hdr
->digest
));
1658 EVP_DigestFinal(&md_ctx
, digest
, NULL
);
1660 sb_aes_init(ictx
, NULL
, 1);
1661 sb_encrypt_sb_header(ictx
);
1663 if (memcmp(digest
, hdr
->digest
, 20))
1665 soprintf(ictx
, "%s Image header checksum: %s\n", stat
[!!ret
],
1666 ret
? "BAD" : "OK");
1670 if (memcmp(hdr
->signature1
, "STMP", 4) ||
1671 memcmp(hdr
->signature2
, "sgtl", 4))
1673 soprintf(ictx
, "%s Signatures: '%.4s' '%.4s'\n",
1674 stat
[!!ret
], hdr
->signature1
, hdr
->signature2
);
1678 if ((hdr
->major_version
!= SB_VERSION_MAJOR
) ||
1679 ((hdr
->minor_version
!= 1) && (hdr
->minor_version
!= 2)))
1681 soprintf(ictx
, "%s Image version: v%i.%i\n", stat
[!!ret
],
1682 hdr
->major_version
, hdr
->minor_version
);
1686 ret
= sb_get_time(hdr
->timestamp_us
/ 1000000, &tm
);
1688 "%s Creation time: %02i:%02i:%02i %02i/%02i/%04i\n",
1689 stat
[!!ret
], tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
,
1690 tm
.tm_mday
, tm
.tm_mon
, tm
.tm_year
+ 2000);
1694 soprintf(ictx
, "%s Product version: %x.%x.%x\n", stat
[0],
1695 ntohs(hdr
->product_version
.major
),
1696 ntohs(hdr
->product_version
.minor
),
1697 ntohs(hdr
->product_version
.revision
));
1698 soprintf(ictx
, "%s Component version: %x.%x.%x\n", stat
[0],
1699 ntohs(hdr
->component_version
.major
),
1700 ntohs(hdr
->component_version
.minor
),
1701 ntohs(hdr
->component_version
.revision
));
1703 if (hdr
->flags
& ~SB_IMAGE_FLAG_VERBOSE
)
1705 soprintf(ictx
, "%s Image flags: %s\n", stat
[!!ret
],
1706 hdr
->flags
& SB_IMAGE_FLAG_VERBOSE
? "Verbose_boot" : "");
1710 if (hdr
->drive_tag
!= 0)
1712 soprintf(ictx
, "%s Drive tag: %i\n", stat
[!!ret
],
1717 sz
= sizeof(struct sb_boot_image_header
) / SB_BLOCK_SIZE
;
1718 if (hdr
->header_blocks
!= sz
)
1720 soprintf(ictx
, "%s Image header size (blocks): %i\n", stat
[!!ret
],
1721 hdr
->header_blocks
);
1725 sz
= sizeof(struct sb_sections_header
) / SB_BLOCK_SIZE
;
1726 if (hdr
->section_header_size
!= sz
)
1728 soprintf(ictx
, "%s Section header size (blocks): %i\n", stat
[!!ret
],
1729 hdr
->section_header_size
);
1733 soprintf(ictx
, "%s Sections count: %i\n", stat
[!!ret
],
1734 hdr
->section_count
);
1735 soprintf(ictx
, "%s First bootable section %i\n", stat
[!!ret
],
1736 hdr
->first_boot_section_id
);
1738 if (hdr
->image_blocks
!= fsize
/ SB_BLOCK_SIZE
)
1740 soprintf(ictx
, "%s Image size (blocks): %i\n", stat
[!!ret
],
1745 sz
= hdr
->header_blocks
+ hdr
->section_header_size
* hdr
->section_count
;
1746 if (hdr
->key_dictionary_block
!= sz
)
1748 soprintf(ictx
, "%s Key dict offset (blocks): %i\n", stat
[!!ret
],
1749 hdr
->key_dictionary_block
);
1753 if (hdr
->key_count
!= 1)
1755 soprintf(ictx
, "%s Number of encryption keys: %i\n", stat
[!!ret
],
1760 sz
= hdr
->header_blocks
+ hdr
->section_header_size
* hdr
->section_count
;
1761 sz
+= hdr
->key_count
*
1762 sizeof(struct sb_key_dictionary_key
) / SB_BLOCK_SIZE
;
1763 if (hdr
->first_boot_tag_block
!= (unsigned)sz
)
1765 soprintf(ictx
, "%s First TAG block (blocks): %i\n", stat
[!!ret
],
1766 hdr
->first_boot_tag_block
);
1773 static void sb_decrypt_tag(struct sb_image_ctx
*ictx
,
1774 struct sb_cmd_ctx
*cctx
)
1776 EVP_MD_CTX
*md_ctx
= &ictx
->md_ctx
;
1777 struct sb_command
*cmd
= &cctx
->payload
;
1779 sb_aes_crypt(ictx
, (uint8_t *)&cctx
->c_payload
,
1780 (uint8_t *)&cctx
->payload
, sizeof(*cmd
));
1781 EVP_DigestUpdate(md_ctx
, &cctx
->c_payload
, sizeof(*cmd
));
1784 static int sb_verify_command(struct sb_image_ctx
*ictx
,
1785 struct sb_cmd_ctx
*cctx
, FILE *fp
,
1786 unsigned long *tsize
)
1788 struct sb_command
*ccmd
= &cctx
->payload
;
1789 unsigned long size
, asize
;
1790 char *csum
, *flag
= "";
1793 uint8_t csn
, csc
= ccmd
->header
.checksum
;
1794 ccmd
->header
.checksum
= 0x5a;
1795 csn
= sb_command_checksum(ccmd
);
1796 ccmd
->header
.checksum
= csc
;
1802 csum
= ret
? "checksum BAD" : "checksum OK";
1804 switch (ccmd
->header
.tag
) {
1806 soprintf(ictx
, " NOOP # %s\n", csum
);
1809 if (ccmd
->header
.flags
& ROM_TAG_CMD_FLAG_ROM_LAST_TAG
)
1811 soprintf(ictx
, " TAG %s # %s\n", flag
, csum
);
1812 sb_aes_reinit(ictx
, 0);
1815 soprintf(ictx
, " LOAD addr=0x%08x length=0x%08x # %s\n",
1816 ccmd
->load
.address
, ccmd
->load
.count
, csum
);
1818 cctx
->length
= ccmd
->load
.count
;
1819 asize
= roundup(cctx
->length
, SB_BLOCK_SIZE
);
1820 cctx
->data
= malloc(asize
);
1824 size
= fread(cctx
->data
, 1, asize
, fp
);
1825 if (size
!= asize
) {
1827 "ERR: SB LOAD command payload too short!\n");
1833 EVP_DigestUpdate(&ictx
->md_ctx
, cctx
->data
, asize
);
1834 sb_aes_crypt(ictx
, cctx
->data
, cctx
->data
, asize
);
1836 if (ccmd
->load
.crc32
!= crc32(cctx
->data
, asize
)) {
1838 "ERR: SB LOAD command payload CRC32 invalid!\n");
1844 " FILL addr=0x%08x length=0x%08x pattern=0x%08x # %s\n",
1845 ccmd
->fill
.address
, ccmd
->fill
.count
,
1846 ccmd
->fill
.pattern
, csum
);
1849 if (ccmd
->header
.flags
& ROM_JUMP_CMD_FLAG_HAB
)
1852 " JUMP%s addr=0x%08x r0_arg=0x%08x # %s\n",
1853 flag
, ccmd
->fill
.address
, ccmd
->jump
.argument
, csum
);
1856 if (ccmd
->header
.flags
& ROM_CALL_CMD_FLAG_HAB
)
1859 " CALL%s addr=0x%08x r0_arg=0x%08x # %s\n",
1860 flag
, ccmd
->fill
.address
, ccmd
->jump
.argument
, csum
);
1863 for (i
= 0; i
< ARRAY_SIZE(modetable
); i
++) {
1864 if (ccmd
->mode
.mode
== modetable
[i
].mode
) {
1865 soprintf(ictx
, " MODE %s # %s\n",
1866 modetable
[i
].name
, csum
);
1870 fprintf(stderr
, " MODE !INVALID! # %s\n", csum
);
1877 static int sb_verify_commands(struct sb_image_ctx
*ictx
,
1878 struct sb_section_ctx
*sctx
, FILE *fp
)
1880 unsigned long size
, tsize
= 0;
1881 struct sb_cmd_ctx
*cctx
;
1884 sb_aes_reinit(ictx
, 0);
1886 while (tsize
< sctx
->size
) {
1887 cctx
= calloc(1, sizeof(*cctx
));
1890 if (!sctx
->cmd_head
) {
1891 sctx
->cmd_head
= cctx
;
1892 sctx
->cmd_tail
= cctx
;
1894 sctx
->cmd_tail
->cmd
= cctx
;
1895 sctx
->cmd_tail
= cctx
;
1898 size
= fread(&cctx
->c_payload
, 1, sizeof(cctx
->c_payload
), fp
);
1899 if (size
!= sizeof(cctx
->c_payload
)) {
1900 fprintf(stderr
, "ERR: SB command header too short!\n");
1906 sb_decrypt_tag(ictx
, cctx
);
1908 ret
= sb_verify_command(ictx
, cctx
, fp
, &tsize
);
1916 static int sb_verify_sections_cmds(struct sb_image_ctx
*ictx
, FILE *fp
)
1918 struct sb_boot_image_header
*hdr
= &ictx
->payload
;
1919 struct sb_sections_header
*shdr
;
1922 struct sb_section_ctx
*sctx
;
1924 char *bootable
= "";
1926 soprintf(ictx
, "----- Verifying SB Sections and Commands -----\n");
1928 for (i
= 0; i
< hdr
->section_count
; i
++) {
1929 sctx
= calloc(1, sizeof(*sctx
));
1932 if (!ictx
->sect_head
) {
1933 ictx
->sect_head
= sctx
;
1934 ictx
->sect_tail
= sctx
;
1936 ictx
->sect_tail
->sect
= sctx
;
1937 ictx
->sect_tail
= sctx
;
1940 size
= fread(&sctx
->payload
, 1, sizeof(sctx
->payload
), fp
);
1941 if (size
!= sizeof(sctx
->payload
)) {
1942 fprintf(stderr
, "ERR: SB section header too short!\n");
1947 size
= fread(&ictx
->sb_dict_key
, 1, sizeof(ictx
->sb_dict_key
), fp
);
1948 if (size
!= sizeof(ictx
->sb_dict_key
)) {
1949 fprintf(stderr
, "ERR: SB key dictionary too short!\n");
1953 sb_encrypt_sb_sections_header(ictx
);
1954 sb_aes_reinit(ictx
, 0);
1955 sb_decrypt_key_dictionary_key(ictx
);
1957 sb_aes_reinit(ictx
, 0);
1959 sctx
= ictx
->sect_head
;
1961 shdr
= &sctx
->payload
;
1963 if (shdr
->section_flags
& SB_SECTION_FLAG_BOOTABLE
) {
1965 bootable
= " BOOTABLE";
1968 sctx
->size
= (shdr
->section_size
* SB_BLOCK_SIZE
) +
1969 sizeof(struct sb_command
);
1970 soprintf(ictx
, "SECTION 0x%x%s # size = %i bytes\n",
1971 shdr
->section_number
, bootable
, sctx
->size
);
1973 if (shdr
->section_flags
& ~SB_SECTION_FLAG_BOOTABLE
)
1974 fprintf(stderr
, " WARN: Unknown section flag(s) %08x\n",
1975 shdr
->section_flags
);
1977 if ((shdr
->section_flags
& SB_SECTION_FLAG_BOOTABLE
) &&
1978 (hdr
->first_boot_section_id
!= shdr
->section_number
)) {
1980 " WARN: Bootable section does ID not match image header ID!\n");
1983 ret
= sb_verify_commands(ictx
, sctx
, fp
);
1992 * check if the first TAG command is at sctx->section_offset
1997 static int sb_verify_image_end(struct sb_image_ctx
*ictx
,
1998 FILE *fp
, off_t filesz
)
2005 soprintf(ictx
, "------------- Verifying image end -------------\n");
2007 size
= fread(digest
, 1, sizeof(digest
), fp
);
2008 if (size
!= sizeof(digest
)) {
2009 fprintf(stderr
, "ERR: SB key dictionary too short!\n");
2014 if (pos
!= filesz
) {
2015 fprintf(stderr
, "ERR: Trailing data past the image!\n");
2019 /* Check the image digest. */
2020 EVP_DigestFinal(&ictx
->md_ctx
, ictx
->digest
, NULL
);
2022 /* Decrypt the image digest from the input image. */
2023 sb_aes_reinit(ictx
, 0);
2024 sb_aes_crypt(ictx
, digest
, digest
, sizeof(digest
));
2026 /* Check all of 20 bytes of the SHA1 hash. */
2027 ret
= memcmp(digest
, ictx
->digest
, 20) ? -EINVAL
: 0;
2030 soprintf(ictx
, "[FAIL] Full-image checksum: BAD\n");
2032 soprintf(ictx
, "[PASS] Full-image checksum: OK\n");
2038 static int sb_build_tree_from_img(struct sb_image_ctx
*ictx
)
2044 if (!ictx
->input_filename
) {
2045 fprintf(stderr
, "ERR: Missing filename!\n");
2049 fp
= fopen(ictx
->input_filename
, "r");
2053 ret
= fseek(fp
, 0, SEEK_END
);
2057 filesize
= ftell(fp
);
2061 ret
= fseek(fp
, 0, SEEK_SET
);
2065 if (filesize
< (signed)sizeof(ictx
->payload
)) {
2066 fprintf(stderr
, "ERR: File too short!\n");
2070 if (filesize
& (SB_BLOCK_SIZE
- 1)) {
2071 fprintf(stderr
, "ERR: The file is not aligned!\n");
2075 /* Load and verify image header */
2076 ret
= sb_verify_image_header(ictx
, fp
, filesize
);
2080 /* Load and verify sections and commands */
2081 ret
= sb_verify_sections_cmds(ictx
, fp
);
2085 ret
= sb_verify_image_end(ictx
, fp
, filesize
);
2092 soprintf(ictx
, "-------------------- Result -------------------\n");
2093 soprintf(ictx
, "Verification %s\n", ret
? "FAILED" : "PASSED");
2095 /* Stop the encryption session. */
2096 sb_aes_deinit(&ictx
->cipher_ctx
);
2104 fprintf(stderr
, "ERR: Failed to load file \"%s\"\n",
2105 ictx
->input_filename
);
2109 static void sb_free_image(struct sb_image_ctx
*ictx
)
2111 struct sb_section_ctx
*sctx
= ictx
->sect_head
, *s_head
;
2112 struct sb_dcd_ctx
*dctx
= ictx
->dcd_head
, *d_head
;
2113 struct sb_cmd_ctx
*cctx
, *c_head
;
2117 c_head
= sctx
->cmd_head
;
2121 c_head
= c_head
->cmd
;
2134 free(d_head
->payload
);
2140 * MXSSB-MKIMAGE glue code.
2142 static int mxsimage_check_image_types(uint8_t type
)
2144 if (type
== IH_TYPE_MXSIMAGE
)
2145 return EXIT_SUCCESS
;
2147 return EXIT_FAILURE
;
2150 static void mxsimage_set_header(void *ptr
, struct stat
*sbuf
, int ifd
,
2151 struct mkimage_params
*params
)
2155 int mxsimage_check_params(struct mkimage_params
*params
)
2159 if (!strlen(params
->imagename
)) {
2161 "Error: %s - Configuration file not specified, it is needed for mxsimage generation\n",
2168 * XIP is not allowed and verify that incompatible
2169 * parameters are not sent at the same time
2170 * For example, if list is required a data image must not be provided
2172 return (params
->dflag
&& (params
->fflag
|| params
->lflag
)) ||
2173 (params
->fflag
&& (params
->dflag
|| params
->lflag
)) ||
2174 (params
->lflag
&& (params
->dflag
|| params
->fflag
)) ||
2175 (params
->xflag
) || !(strlen(params
->imagename
));
2178 static int mxsimage_verify_print_header(char *file
, int silent
)
2181 struct sb_image_ctx ctx
;
2183 memset(&ctx
, 0, sizeof(ctx
));
2185 ctx
.input_filename
= file
;
2186 ctx
.silent_dump
= silent
;
2188 ret
= sb_build_tree_from_img(&ctx
);
2189 sb_free_image(&ctx
);
2195 static int mxsimage_verify_header(unsigned char *ptr
, int image_size
,
2196 struct mkimage_params
*params
)
2198 struct sb_boot_image_header
*hdr
;
2203 hdr
= (struct sb_boot_image_header
*)ptr
;
2206 * Check if the header contains the MXS image signatures,
2207 * if so, do a full-image verification.
2209 if (memcmp(hdr
->signature1
, "STMP", 4) ||
2210 memcmp(hdr
->signature2
, "sgtl", 4))
2213 imagefile
= params
->imagefile
;
2215 return mxsimage_verify_print_header(params
->imagefile
, 1);
2218 static void mxsimage_print_header(const void *hdr
)
2221 mxsimage_verify_print_header(imagefile
, 0);
2224 static int sb_build_image(struct sb_image_ctx
*ictx
,
2225 struct image_type_params
*tparams
)
2227 struct sb_boot_image_header
*sb_header
= &ictx
->payload
;
2228 struct sb_section_ctx
*sctx
;
2229 struct sb_cmd_ctx
*cctx
;
2230 struct sb_command
*ccmd
;
2231 struct sb_key_dictionary_key
*sb_dict_key
= &ictx
->sb_dict_key
;
2233 uint8_t *image
, *iptr
;
2235 /* Calculate image size. */
2236 uint32_t size
= sizeof(*sb_header
) +
2237 ictx
->sect_count
* sizeof(struct sb_sections_header
) +
2238 sizeof(*sb_dict_key
) + sizeof(ictx
->digest
);
2240 sctx
= ictx
->sect_head
;
2246 image
= malloc(size
);
2251 memcpy(iptr
, sb_header
, sizeof(*sb_header
));
2252 iptr
+= sizeof(*sb_header
);
2254 sctx
= ictx
->sect_head
;
2256 memcpy(iptr
, &sctx
->payload
, sizeof(struct sb_sections_header
));
2257 iptr
+= sizeof(struct sb_sections_header
);
2261 memcpy(iptr
, sb_dict_key
, sizeof(*sb_dict_key
));
2262 iptr
+= sizeof(*sb_dict_key
);
2264 sctx
= ictx
->sect_head
;
2266 cctx
= sctx
->cmd_head
;
2268 ccmd
= &cctx
->payload
;
2270 memcpy(iptr
, &cctx
->c_payload
, sizeof(cctx
->payload
));
2271 iptr
+= sizeof(cctx
->payload
);
2273 if (ccmd
->header
.tag
== ROM_LOAD_CMD
) {
2274 memcpy(iptr
, cctx
->data
, cctx
->length
);
2275 iptr
+= cctx
->length
;
2284 memcpy(iptr
, ictx
->digest
, sizeof(ictx
->digest
));
2285 iptr
+= sizeof(ictx
->digest
);
2287 /* Configure the mkimage */
2288 tparams
->hdr
= image
;
2289 tparams
->header_size
= size
;
2294 static int mxsimage_generate(struct mkimage_params
*params
,
2295 struct image_type_params
*tparams
)
2298 struct sb_image_ctx ctx
;
2300 /* Do not copy the U-Boot image! */
2301 params
->skipcpy
= 1;
2303 memset(&ctx
, 0, sizeof(ctx
));
2305 ctx
.cfg_filename
= params
->imagename
;
2306 ctx
.output_filename
= params
->imagefile
;
2307 ctx
.verbose_boot
= 1;
2309 ret
= sb_build_tree_from_cfg(&ctx
);
2313 ret
= sb_encrypt_image(&ctx
);
2315 ret
= sb_build_image(&ctx
, tparams
);
2318 sb_free_image(&ctx
);
2324 * mxsimage parameters
2326 static struct image_type_params mxsimage_params
= {
2327 .name
= "Freescale MXS Boot Image support",
2330 .check_image_type
= mxsimage_check_image_types
,
2331 .verify_header
= mxsimage_verify_header
,
2332 .print_header
= mxsimage_print_header
,
2333 .set_header
= mxsimage_set_header
,
2334 .check_params
= mxsimage_check_params
,
2335 .vrec_header
= mxsimage_generate
,
2338 void init_mxs_image_type(void)
2340 mkimage_register(&mxsimage_params
);
2344 void init_mxs_image_type(void)