Commit 46371cc1 authored by Marek Vasut's avatar Marek Vasut
Browse files

mxssb: Add silent verification mode



In this mode, the file contents will not be dumped and so will not
be presented any further output. The tool will only return a return
value depending on whether the verification passed or failed. This
is useful in scripts.
Signed-off-by: Marek Vasut's avatarMarek Vasut <marex@denx.de>
parent aa89efd4
......@@ -70,6 +70,7 @@ struct sb_section_ctx {
struct sb_image_ctx {
/* Image configuration */
unsigned int verbose_boot:1;
unsigned int silent_dump:1;
char *input_filename;
char *output_filename;
char *cfg_filename;
......@@ -194,6 +195,21 @@ static uint32_t crc32(uint8_t *data, uint32_t len)
return crc32;
}
/*
* Debug
*/
static void soprintf(struct sb_image_ctx *ictx, const char *fmt, ...)
{
va_list ap;
if (ictx->silent_dump)
return;
va_start(ap, fmt);
vfprintf(stdout, fmt, ap);
va_end(ap);
}
/*
* Code
*/
......@@ -1335,7 +1351,7 @@ static int sb_verify_image_header(struct sb_image_ctx *ictx,
EVP_MD_CTX_init(&ictx->md_ctx);
EVP_DigestInit(&ictx->md_ctx, EVP_sha1());
fprintf(stdout, "---------- Verifying SB Image Header ----------\n");
soprintf(ictx, "---------- Verifying SB Image Header ----------\n");
size = fread(&ictx->payload, 1, sizeof(ictx->payload), fp);
if (size != sizeof(ictx->payload)) {
......@@ -1356,7 +1372,7 @@ static int sb_verify_image_header(struct sb_image_ctx *ictx,
if (memcmp(digest, hdr->digest, 20))
ret = -EINVAL;
fprintf(stdout, "%s Image header checksum: %s\n", stat[!!ret],
soprintf(ictx, "%s Image header checksum: %s\n", stat[!!ret],
ret ? "BAD" : "OK");
if (ret)
return ret;
......@@ -1364,7 +1380,7 @@ static int sb_verify_image_header(struct sb_image_ctx *ictx,
if (memcmp(hdr->signature1, "STMP", 4) ||
memcmp(hdr->signature2, "sgtl", 4))
ret = -EINVAL;
fprintf(stdout, "%s Signatures: '%.4s' '%.4s'\n",
soprintf(ictx, "%s Signatures: '%.4s' '%.4s'\n",
stat[!!ret], hdr->signature1, hdr->signature2);
if (ret)
return ret;
......@@ -1372,38 +1388,38 @@ static int sb_verify_image_header(struct sb_image_ctx *ictx,
if ((hdr->major_version != SB_VERSION_MAJOR) ||
((hdr->minor_version != 1) && (hdr->minor_version != 2)))
ret = -EINVAL;
fprintf(stdout, "%s Image version: v%i.%i\n", stat[!!ret],
soprintf(ictx, "%s Image version: v%i.%i\n", stat[!!ret],
hdr->major_version, hdr->minor_version);
if (ret)
return ret;
ret = sb_get_time(hdr->timestamp_us / 1000000, &tm);
fprintf(stdout, "%s Creation time: "
soprintf(ictx, "%s Creation time: "
"%02i:%02i:%02i %02i/%02i/%04i\n",
stat[!!ret], tm.tm_hour, tm.tm_min, tm.tm_sec,
tm.tm_mday, tm.tm_mon, tm.tm_year + 2000);
if (ret)
return ret;
fprintf(stdout, "%s Product version: %x.%x.%x\n", stat[0],
soprintf(ictx, "%s Product version: %x.%x.%x\n", stat[0],
ntohs(hdr->product_version.major),
ntohs(hdr->product_version.minor),
ntohs(hdr->product_version.revision));
fprintf(stdout, "%s Component version: %x.%x.%x\n", stat[0],
soprintf(ictx, "%s Component version: %x.%x.%x\n", stat[0],
ntohs(hdr->component_version.major),
ntohs(hdr->component_version.minor),
ntohs(hdr->component_version.revision));
if (hdr->flags & ~SB_IMAGE_FLAG_VERBOSE)
ret = -EINVAL;
fprintf(stdout, "%s Image flags: %s\n", stat[!!ret],
soprintf(ictx, "%s Image flags: %s\n", stat[!!ret],
hdr->flags & SB_IMAGE_FLAG_VERBOSE ? "Verbose_boot" : "");
if (ret)
return ret;
if (hdr->drive_tag != 0)
ret = -EINVAL;
fprintf(stdout, "%s Drive tag: %i\n", stat[!!ret],
soprintf(ictx, "%s Drive tag: %i\n", stat[!!ret],
hdr->drive_tag);
if (ret)
return ret;
......@@ -1411,7 +1427,7 @@ static int sb_verify_image_header(struct sb_image_ctx *ictx,
sz = sizeof(struct sb_boot_image_header) / SB_BLOCK_SIZE;
if (hdr->header_blocks != sz)
ret = -EINVAL;
fprintf(stdout, "%s Image header size (blocks): %i\n", stat[!!ret],
soprintf(ictx, "%s Image header size (blocks): %i\n", stat[!!ret],
hdr->header_blocks);
if (ret)
return ret;
......@@ -1419,19 +1435,19 @@ static int sb_verify_image_header(struct sb_image_ctx *ictx,
sz = sizeof(struct sb_sections_header) / SB_BLOCK_SIZE;
if (hdr->section_header_size != sz)
ret = -EINVAL;
fprintf(stdout, "%s Section header size (blocks): %i\n", stat[!!ret],
soprintf(ictx, "%s Section header size (blocks): %i\n", stat[!!ret],
hdr->section_header_size);
if (ret)
return ret;
fprintf(stdout, "%s Sections count: %i\n", stat[!!ret],
soprintf(ictx, "%s Sections count: %i\n", stat[!!ret],
hdr->section_count);
fprintf(stdout, "%s First bootable section %i\n", stat[!!ret],
soprintf(ictx, "%s First bootable section %i\n", stat[!!ret],
hdr->first_boot_section_id);
if (hdr->image_blocks != fsize / SB_BLOCK_SIZE)
ret = -EINVAL;
fprintf(stdout, "%s Image size (blocks): %i\n", stat[!!ret],
soprintf(ictx, "%s Image size (blocks): %i\n", stat[!!ret],
hdr->image_blocks);
if (ret)
return ret;
......@@ -1439,14 +1455,14 @@ static int sb_verify_image_header(struct sb_image_ctx *ictx,
sz = hdr->header_blocks + hdr->section_header_size * hdr->section_count;
if (hdr->key_dictionary_block != sz)
ret = -EINVAL;
fprintf(stdout, "%s Key dict offset (blocks): %i\n", stat[!!ret],
soprintf(ictx, "%s Key dict offset (blocks): %i\n", stat[!!ret],
hdr->key_dictionary_block);
if (ret)
return ret;
if (hdr->key_count != 1)
ret = -EINVAL;
fprintf(stdout, "%s Number of encryption keys: %i\n", stat[!!ret],
soprintf(ictx, "%s Number of encryption keys: %i\n", stat[!!ret],
hdr->key_count);
if (ret)
return ret;
......@@ -1455,7 +1471,7 @@ static int sb_verify_image_header(struct sb_image_ctx *ictx,
sz += hdr->key_count * sizeof(struct sb_key_dictionary_key) / SB_BLOCK_SIZE;
if (hdr->first_boot_tag_block != (unsigned)sz)
ret = -EINVAL;
fprintf(stdout, "%s First TAG block (blocks): %i\n", stat[!!ret],
soprintf(ictx, "%s First TAG block (blocks): %i\n", stat[!!ret],
hdr->first_boot_tag_block);
if (ret)
return ret;
......@@ -1496,16 +1512,16 @@ static int sb_verify_command(struct sb_image_ctx *ictx,
switch (ccmd->header.tag) {
case ROM_NOP_CMD:
fprintf(stdout, " NOOP # %s\n", csum);
soprintf(ictx, " NOOP # %s\n", csum);
return ret;
case ROM_TAG_CMD:
if (ccmd->header.flags & ROM_TAG_CMD_FLAG_ROM_LAST_TAG)
flag = "LAST";
fprintf(stdout, " TAG %s # %s\n", flag, csum);
soprintf(ictx, " TAG %s # %s\n", flag, csum);
sb_aes_reinit(ictx, 0);
return ret;
case ROM_LOAD_CMD:
fprintf(stdout, " LOAD addr=0x%08x length=0x%08x # %s\n",
soprintf(ictx, " LOAD addr=0x%08x length=0x%08x # %s\n",
ccmd->load.address, ccmd->load.count, csum);
cctx->length = ccmd->load.count;
......@@ -1533,7 +1549,7 @@ static int sb_verify_command(struct sb_image_ctx *ictx,
}
return 0;
case ROM_FILL_CMD:
fprintf(stdout,
soprintf(ictx,
" FILL addr=0x%08x length=0x%08x pattern=0x%08x # %s\n",
ccmd->fill.address, ccmd->fill.count,
ccmd->fill.pattern, csum);
......@@ -1541,21 +1557,21 @@ static int sb_verify_command(struct sb_image_ctx *ictx,
case ROM_JUMP_CMD:
if (ccmd->header.flags & ROM_JUMP_CMD_FLAG_HAB)
flag = " HAB";
fprintf(stdout,
soprintf(ictx,
" JUMP%s addr=0x%08x r0_arg=0x%08x # %s\n",
flag, ccmd->fill.address, ccmd->jump.argument, csum);
return 0;
case ROM_CALL_CMD:
if (ccmd->header.flags & ROM_CALL_CMD_FLAG_HAB)
flag = " HAB";
fprintf(stdout,
soprintf(ictx,
" CALL%s addr=0x%08x r0_arg=0x%08x # %s\n",
flag, ccmd->fill.address, ccmd->jump.argument, csum);
return 0;
case ROM_MODE_CMD:
for (i = 0; i < ARRAY_SIZE(modetable); i++) {
if (ccmd->mode.mode == modetable[i].mode) {
fprintf(stdout, " MODE %s # %s\n",
soprintf(ictx, " MODE %s # %s\n",
modetable[i].name, csum);
break;
}
......@@ -1616,7 +1632,7 @@ static int sb_verify_sections_cmds(struct sb_image_ctx *ictx, FILE *fp)
unsigned long size;
char *bootable = "";
fprintf(stdout, "----- Verifying SB Sections and Commands -----\n");
soprintf(ictx, "----- Verifying SB Sections and Commands -----\n");
for (i = 0; i < hdr->section_count; i++) {
sctx = calloc(1, sizeof(*sctx));
......@@ -1660,7 +1676,7 @@ static int sb_verify_sections_cmds(struct sb_image_ctx *ictx, FILE *fp)
sctx->size = (shdr->section_size * SB_BLOCK_SIZE) +
sizeof(struct sb_command);
fprintf(stdout, "SECTION 0x%x%s # size = %i bytes\n",
soprintf(ictx, "SECTION 0x%x%s # size = %i bytes\n",
shdr->section_number, bootable, sctx->size);
if (shdr->section_flags & ~SB_SECTION_FLAG_BOOTABLE)
......@@ -1692,7 +1708,7 @@ static int sb_verify_image_end(struct sb_image_ctx *ictx, FILE *fp, off_t filesz
off_t pos;
int ret;
fprintf(stdout, "------------- Verifying image end -------------\n");
soprintf(ictx, "------------- Verifying image end -------------\n");
size = fread(digest, 1, sizeof(digest), fp);
if (size != sizeof(digest)) {
......@@ -1717,9 +1733,9 @@ static int sb_verify_image_end(struct sb_image_ctx *ictx, FILE *fp, off_t filesz
ret = memcmp(digest, ictx->digest, 20) ? -EINVAL : 0;
if (ret)
fprintf(stdout, "[FAIL] Full-image checksum: BAD\n");
soprintf(ictx, "[FAIL] Full-image checksum: BAD\n");
else
fprintf(stdout, "[PASS] Full-image checksum: OK\n");
soprintf(ictx, "[PASS] Full-image checksum: OK\n");
return ret;
}
......@@ -1779,8 +1795,8 @@ static int sb_build_tree_from_img(struct sb_image_ctx *ictx)
ret = 0;
err_verify:
fprintf(stdout, "-------------------- Result -------------------\n");
fprintf(stdout, "Verification %s\n", ret ? "FAILED" : "PASSED");
soprintf(ictx, "-------------------- Result -------------------\n");
soprintf(ictx, "Verification %s\n", ret ? "FAILED" : "PASSED");
/* Stop the encryption session. */
sb_aes_deinit(&ictx->cipher_ctx);
......@@ -1821,10 +1837,11 @@ static void print_help(const char *pn)
{
printf(
"Usage: %s -c FILE -o FILE [-v] [-h]\n"
" %s -i FILE [-h]\n"
" %s -i FILE [-s] [-h]\n"
" -c FILE ..... path to command file (mxsimage.cfg)\n"
" -o FILE ..... path to output SB file (u-boot.sb)\n"
" -i FILE ..... path to input SB file (u-boot.sb)\n"
" -s .......... verify the file without any output\n"
" -v .......... verbose boot output from BootROM\n"
" -h .......... print help\n", pn, pn);
}
......@@ -1837,7 +1854,7 @@ int main(int argc, char **argv)
struct sb_image_ctx ctx;
memset(&ctx, 0, sizeof(ctx));
while ((opt = getopt(argc, argv, "c:o:i:vh")) != -1) {
while ((opt = getopt(argc, argv, "c:o:i:svh")) != -1) {
switch (opt) {
case 'c':
ctx.cfg_filename = optarg;
......@@ -1848,6 +1865,9 @@ int main(int argc, char **argv)
case 'i':
ctx.input_filename = optarg;
break;
case 's':
ctx.silent_dump = 1;
break;
case 'v':
ctx.verbose_boot = 1;
break;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment