]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - tools/mkimage.c
ARM: keystone2: Cleanup PLL init code
[karo-tx-uboot.git] / tools / mkimage.c
index 5ccd951048cef587686d264575aae6ea7623e90f..e81d4550835c6763bcab21e0b2b8ab2bf46baba3 100644 (file)
@@ -26,8 +26,48 @@ struct image_tool_params params = {
        .imagename2 = "",
 };
 
-int
-main (int argc, char **argv)
+static int h_compare_image_name(const void *vtype1, const void *vtype2)
+{
+       const int *type1 = vtype1;
+       const int *type2 = vtype2;
+       const char *name1 = genimg_get_type_short_name(*type1);
+       const char *name2 = genimg_get_type_short_name(*type2);
+
+       return strcmp(name1, name2);
+}
+
+/* Show all image types supported by mkimage */
+static void show_image_types(void)
+{
+       struct image_type_params *tparams;
+       int order[IH_TYPE_COUNT];
+       int count;
+       int type;
+       int i;
+
+       /* Sort the names in order of short name for easier reading */
+       memset(order, '\0', sizeof(order));
+       for (count = 0, type = 0; type < IH_TYPE_COUNT; type++) {
+               tparams = imagetool_get_type(type);
+               if (tparams)
+                       order[count++] = type;
+       }
+       qsort(order, count, sizeof(int), h_compare_image_name);
+
+       fprintf(stderr, "\nInvalid image type. Supported image types:\n");
+       for (i = 0; i < count; i++) {
+               type = order[i];
+               tparams = imagetool_get_type(type);
+               if (tparams) {
+                       fprintf(stderr, "\t%-15s  %s\n",
+                               genimg_get_type_short_name(type),
+                               genimg_get_type_name(type));
+               }
+       }
+       fprintf(stderr, "\n");
+}
+
+int main(int argc, char **argv)
 {
        int ifd = -1;
        struct stat sbuf;
@@ -35,6 +75,7 @@ main (int argc, char **argv)
        int retval = 0;
        struct image_type_params *tparams = NULL;
        int pad_len = 0;
+       int dfd;
 
        params.cmdname = *argv;
        params.addr = params.ep = 0;
@@ -75,12 +116,16 @@ main (int argc, char **argv)
                                        usage ();
                                goto NXTARG;
                        case 'T':
-                               if ((--argc <= 0) ||
-                                       (params.type =
-                                       genimg_get_type_id (*++argv)) < 0)
-                                       usage ();
+                               params.type = -1;
+                               if (--argc >= 0 && argv[1]) {
+                                       params.type =
+                                               genimg_get_type_id(*++argv);
+                               }
+                               if (params.type < 0) {
+                                       show_image_types();
+                                       usage();
+                               }
                                goto NXTARG;
-
                        case 'a':
                                if (--argc <= 0)
                                        usage ();
@@ -266,6 +311,22 @@ NXTARG:            ;
                exit (retval);
        }
 
+       dfd = open(params.datafile, O_RDONLY | O_BINARY);
+       if (dfd < 0) {
+               fprintf(stderr, "%s: Can't open %s: %s\n",
+                       params.cmdname, params.datafile, strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       if (fstat(dfd, &sbuf) < 0) {
+               fprintf(stderr, "%s: Can't stat %s: %s\n",
+                       params.cmdname, params.datafile, strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       params.file_size = sbuf.st_size + tparams->header_size;
+       close(dfd);
+
        /*
         * In case there an header with a variable
         * length will be added, the corresponding
@@ -365,6 +426,7 @@ NXTARG:             ;
                        params.cmdname, params.imagefile, strerror(errno));
                exit (EXIT_FAILURE);
        }
+       params.file_size = sbuf.st_size;
 
        ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
        if (ptr == MAP_FAILED) {
@@ -546,6 +608,7 @@ static void usage(void)
 #endif
        fprintf (stderr, "       %s -V ==> print version information and exit\n",
                params.cmdname);
+       fprintf(stderr, "Use -T to see a list of available image types\n");
 
        exit (EXIT_FAILURE);
 }