Commit 5b9d44df2307fc882b2ae666fd28fb2c7c2d5b11

Authored by Simon Glass
1 parent a3c3cff06c

mkimage: Display a better list of available image types

Offer to display the available image types in help. Also, rather than
hacking the genimg_get_type_id() function to display a list of types,
do this in the tool. Also, sort the list.

The list of image types is quite long, and hard to discover. Print it out
when we show help information.

Signed-off-by: Simon Glass <sjg@chromium.org>

Showing 3 changed files with 95 additions and 33 deletions Side-by-side Diff

... ... @@ -543,6 +543,15 @@
543 543 }
544 544 #endif
545 545  
  546 +const table_entry_t *get_table_entry(const table_entry_t *table, int id)
  547 +{
  548 + for (; table->id >= 0; ++table) {
  549 + if (table->id == id)
  550 + return table;
  551 + }
  552 + return NULL;
  553 +}
  554 +
546 555 /**
547 556 * get_table_entry_name - translate entry id to long name
548 557 * @table: pointer to a translation table for entries of a specific type
549 558  
550 559  
551 560  
... ... @@ -559,15 +568,14 @@
559 568 */
560 569 char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
561 570 {
562   - for (; table->id >= 0; ++table) {
563   - if (table->id == id)
  571 + table = get_table_entry(table, id);
  572 + if (!table)
  573 + return msg;
564 574 #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
565   - return table->lname;
  575 + return table->lname;
566 576 #else
567   - return table->lname + gd->reloc_off;
  577 + return table->lname + gd->reloc_off;
568 578 #endif
569   - }
570   - return (msg);
571 579 }
572 580  
573 581 const char *genimg_get_os_name(uint8_t os)
... ... @@ -586,6 +594,20 @@
586 594 return (get_table_entry_name(uimage_type, "Unknown Image", type));
587 595 }
588 596  
  597 +const char *genimg_get_type_short_name(uint8_t type)
  598 +{
  599 + const table_entry_t *table;
  600 +
  601 + table = get_table_entry(uimage_type, type);
  602 + if (!table)
  603 + return "unknown";
  604 +#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
  605 + return table->sname;
  606 +#else
  607 + return table->sname + gd->reloc_off;
  608 +#endif
  609 +}
  610 +
589 611 const char *genimg_get_comp_name(uint8_t comp)
590 612 {
591 613 return (get_table_entry_name(uimage_comp, "Unknown Compression",
592 614  
593 615  
594 616  
595 617  
... ... @@ -610,34 +632,18 @@
610 632 const char *table_name, const char *name)
611 633 {
612 634 const table_entry_t *t;
613   -#ifdef USE_HOSTCC
614   - int first = 1;
615 635  
616 636 for (t = table; t->id >= 0; ++t) {
617   - if (t->sname && strcasecmp(t->sname, name) == 0)
618   - return(t->id);
619   - }
620   -
621   - fprintf(stderr, "\nInvalid %s Type - valid names are", table_name);
622   - for (t = table; t->id >= 0; ++t) {
623   - if (t->sname == NULL)
624   - continue;
625   - fprintf(stderr, "%c %s", (first) ? ':' : ',', t->sname);
626   - first = 0;
627   - }
628   - fprintf(stderr, "\n");
629   -#else
630   - for (t = table; t->id >= 0; ++t) {
631 637 #ifdef CONFIG_NEEDS_MANUAL_RELOC
632   - if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
  638 + if (t->sname && strcasecmp(t->sname + gd->reloc_off, name) == 0)
633 639 #else
634   - if (t->sname && strcmp(t->sname, name) == 0)
  640 + if (t->sname && strcasecmp(t->sname, name) == 0)
635 641 #endif
636 642 return (t->id);
637 643 }
638 644 debug("Invalid %s Type: %s\n", table_name, name);
639   -#endif /* USE_HOSTCC */
640   - return (-1);
  645 +
  646 + return -1;
641 647 }
642 648  
643 649 int genimg_get_os_id(const char *name)
... ... @@ -246,6 +246,8 @@
246 246 #define IH_TYPE_LPC32XXIMAGE 21 /* x86 setup.bin Image */
247 247 #define IH_TYPE_LOADABLE 22 /* A list of typeless images */
248 248  
  249 +#define IH_TYPE_COUNT 23 /* Number of image types */
  250 +
249 251 /*
250 252 * Compression Types
251 253 */
... ... @@ -411,6 +413,15 @@
411 413 const char *genimg_get_os_name(uint8_t os);
412 414 const char *genimg_get_arch_name(uint8_t arch);
413 415 const char *genimg_get_type_name(uint8_t type);
  416 +
  417 +/**
  418 + * genimg_get_type_short_name() - get the short name for an image type
  419 + *
  420 + * @param type Image type (IH_TYPE_...)
  421 + * @return image short name, or "unknown" if unknown
  422 + */
  423 +const char *genimg_get_type_short_name(uint8_t type);
  424 +
414 425 const char *genimg_get_comp_name(uint8_t comp);
415 426 int genimg_get_os_id(const char *name);
416 427 int genimg_get_arch_id(const char *name);
... ... @@ -26,9 +26,49 @@
26 26 .imagename2 = "",
27 27 };
28 28  
29   -int
30   -main (int argc, char **argv)
  29 +static int h_compare_image_name(const void *vtype1, const void *vtype2)
31 30 {
  31 + const int *type1 = vtype1;
  32 + const int *type2 = vtype2;
  33 + const char *name1 = genimg_get_type_short_name(*type1);
  34 + const char *name2 = genimg_get_type_short_name(*type2);
  35 +
  36 + return strcmp(name1, name2);
  37 +}
  38 +
  39 +/* Show all image types supported by mkimage */
  40 +static void show_image_types(void)
  41 +{
  42 + struct image_type_params *tparams;
  43 + int order[IH_TYPE_COUNT];
  44 + int count;
  45 + int type;
  46 + int i;
  47 +
  48 + /* Sort the names in order of short name for easier reading */
  49 + memset(order, '\0', sizeof(order));
  50 + for (count = 0, type = 0; type < IH_TYPE_COUNT; type++) {
  51 + tparams = imagetool_get_type(type);
  52 + if (tparams)
  53 + order[count++] = type;
  54 + }
  55 + qsort(order, count, sizeof(int), h_compare_image_name);
  56 +
  57 + fprintf(stderr, "\nInvalid image type. Supported image types:\n");
  58 + for (i = 0; i < count; i++) {
  59 + type = order[i];
  60 + tparams = imagetool_get_type(type);
  61 + if (tparams) {
  62 + fprintf(stderr, "\t%-15s %s\n",
  63 + genimg_get_type_short_name(type),
  64 + genimg_get_type_name(type));
  65 + }
  66 + }
  67 + fprintf(stderr, "\n");
  68 +}
  69 +
  70 +int main(int argc, char **argv)
  71 +{
32 72 int ifd = -1;
33 73 struct stat sbuf;
34 74 char *ptr;
35 75  
... ... @@ -75,12 +115,16 @@
75 115 usage ();
76 116 goto NXTARG;
77 117 case 'T':
78   - if ((--argc <= 0) ||
79   - (params.type =
80   - genimg_get_type_id (*++argv)) < 0)
81   - usage ();
  118 + params.type = -1;
  119 + if (--argc >= 0 && argv[1]) {
  120 + params.type =
  121 + genimg_get_type_id(*++argv);
  122 + }
  123 + if (params.type < 0) {
  124 + show_image_types();
  125 + usage();
  126 + }
82 127 goto NXTARG;
83   -
84 128 case 'a':
85 129 if (--argc <= 0)
86 130 usage ();
... ... @@ -546,6 +590,7 @@
546 590 #endif
547 591 fprintf (stderr, " %s -V ==> print version information and exit\n",
548 592 params.cmdname);
  593 + fprintf(stderr, "Use -T to see a list of available image types\n");
549 594  
550 595 exit (EXIT_FAILURE);
551 596 }