]> git.sur5r.net Git - u-boot/blobdiff - tools/fit_image.c
tools, rsa: Further minor cleanups on top of c236ebd and 2b9ec7
[u-boot] / tools / fit_image.c
index 06b561dde66bd0ea138d376072e913a147f6b74c..10fd6d4929374728732efd63e327cab36275766e 100644 (file)
@@ -51,8 +51,10 @@ static int fit_add_file_data(struct image_tool_params *params, size_t size_inc,
        }
 
        /* for first image creation, add a timestamp at offset 0 i.e., root  */
-       if (params->datafile)
-               ret = fit_set_timestamp(ptr, 0, sbuf.st_mtime);
+       if (params->datafile) {
+               time_t time = imagetool_get_source_date(params, sbuf.st_mtime);
+               ret = fit_set_timestamp(ptr, 0, time);
+       }
 
        if (!ret) {
                ret = fit_add_verification_data(params->keydir, dest_blob, ptr,
@@ -123,13 +125,14 @@ static int fdt_property_file(struct image_tool_params *params,
 
        ret = fdt_property_placeholder(fdt, "data", sbuf.st_size, &ptr);
        if (ret)
-               return ret;
+               goto err;
        ret = read(fd, ptr, sbuf.st_size);
        if (ret != sbuf.st_size) {
                fprintf(stderr, "%s: Can't read %s: %s\n",
                        params->cmdname, fname, strerror(errno));
                goto err;
        }
+       close(fd);
 
        return 0;
 err:
@@ -192,7 +195,8 @@ static int fit_write_images(struct image_tool_params *params, char *fdt)
        fdt_begin_node(fdt, str);
        fdt_property_string(fdt, "description", params->imagename);
        fdt_property_string(fdt, "type", typename);
-       fdt_property_string(fdt, "arch", genimg_get_arch_name(params->arch));
+       fdt_property_string(fdt, "arch",
+                           genimg_get_arch_short_name(params->arch));
        fdt_property_string(fdt, "os", genimg_get_os_short_name(params->os));
        fdt_property_string(fdt, "compression",
                            genimg_get_comp_short_name(params->comp));
@@ -329,7 +333,7 @@ static int fit_build(struct image_tool_params *params, const char *fname)
        if (ret < 0) {
                fprintf(stderr, "%s: Failed to build FIT image\n",
                        params->cmdname);
-               goto err;
+               goto err_buf;
        }
        size = ret;
        fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
@@ -342,13 +346,15 @@ static int fit_build(struct image_tool_params *params, const char *fname)
        if (ret != size) {
                fprintf(stderr, "%s: Can't write %s: %s\n",
                        params->cmdname, fname, strerror(errno));
-               close(fd);
                goto err;
        }
        close(fd);
+       free(buf);
 
        return 0;
 err:
+       close(fd);
+err_buf:
        free(buf);
        return -1;
 }
@@ -385,7 +391,7 @@ static int fit_extract_data(struct image_tool_params *params, const char *fname)
        buf = malloc(fit_size);
        if (!buf) {
                ret = -ENOMEM;
-               goto err;
+               goto err_munmap;
        }
        buf_ptr = 0;
 
@@ -393,7 +399,7 @@ static int fit_extract_data(struct image_tool_params *params, const char *fname)
        if (images < 0) {
                debug("%s: Cannot find /images node: %d\n", __func__, images);
                ret = -EINVAL;
-               goto err;
+               goto err_munmap;
        }
 
        for (node = fdt_first_subnode(fdt, images);
@@ -411,9 +417,15 @@ static int fit_extract_data(struct image_tool_params *params, const char *fname)
                ret = fdt_delprop(fdt, node, "data");
                if (ret) {
                        ret = -EPERM;
-                       goto err;
+                       goto err_munmap;
+               }
+               if (params->external_offset > 0) {
+                       /* An external offset positions the data absolutely. */
+                       fdt_setprop_u32(fdt, node, "data-position",
+                                       params->external_offset + buf_ptr);
+               } else {
+                       fdt_setprop_u32(fdt, node, "data-offset", buf_ptr);
                }
-               fdt_setprop_u32(fdt, node, "data-offset", buf_ptr);
                fdt_setprop_u32(fdt, node, "data-size", len);
 
                buf_ptr += (len + 3) & ~3;
@@ -434,6 +446,17 @@ static int fit_extract_data(struct image_tool_params *params, const char *fname)
                ret = -EIO;
                goto err;
        }
+
+       /* Check if an offset for the external data was set. */
+       if (params->external_offset > 0) {
+               if (params->external_offset < new_size) {
+                       debug("External offset %x overlaps FIT length %x",
+                             params->external_offset, new_size);
+                       ret = -EINVAL;
+                       goto err;
+               }
+               new_size = params->external_offset;
+       }
        if (lseek(fd, new_size, SEEK_SET) < 0) {
                debug("%s: Failed to seek to end of file: %s\n", __func__,
                      strerror(errno));
@@ -447,10 +470,106 @@ static int fit_extract_data(struct image_tool_params *params, const char *fname)
                goto err;
        }
        close(fd);
+       return 0;
+
+err_munmap:
+       munmap(fdt, sbuf.st_size);
+err:
+       if (buf)
+               free(buf);
+       close(fd);
+       return ret;
+}
+
+static int fit_import_data(struct image_tool_params *params, const char *fname)
+{
+       void *fdt, *old_fdt;
+       int fit_size, new_size, size, data_base;
+       int fd;
+       struct stat sbuf;
+       int ret;
+       int images;
+       int node;
+
+       fd = mmap_fdt(params->cmdname, fname, 0, &old_fdt, &sbuf, false);
+       if (fd < 0)
+               return -EIO;
+       fit_size = fdt_totalsize(old_fdt);
+       data_base = (fit_size + 3) & ~3;
+
+       /* Allocate space to hold the new FIT */
+       size = sbuf.st_size + 16384;
+       fdt = malloc(size);
+       if (!fdt) {
+               fprintf(stderr, "%s: Failed to allocate memory (%d bytes)\n",
+                       __func__, size);
+               ret = -ENOMEM;
+               goto err;
+       }
+       ret = fdt_open_into(old_fdt, fdt, size);
+       if (ret) {
+               debug("%s: Failed to expand FIT: %s\n", __func__,
+                     fdt_strerror(errno));
+               ret = -EINVAL;
+               goto err;
+       }
+
+       images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
+       if (images < 0) {
+               debug("%s: Cannot find /images node: %d\n", __func__, images);
+               ret = -EINVAL;
+               goto err;
+       }
+
+       for (node = fdt_first_subnode(fdt, images);
+            node >= 0;
+            node = fdt_next_subnode(fdt, node)) {
+               int buf_ptr;
+               int len;
+
+               buf_ptr = fdtdec_get_int(fdt, node, "data-offset", -1);
+               len = fdtdec_get_int(fdt, node, "data-size", -1);
+               if (buf_ptr == -1 || len == -1)
+                       continue;
+               debug("Importing data size %x\n", len);
+
+               ret = fdt_setprop(fdt, node, "data", fdt + data_base + buf_ptr,
+                                 len);
+               if (ret) {
+                       debug("%s: Failed to write property: %s\n", __func__,
+                             fdt_strerror(ret));
+                       ret = -EINVAL;
+                       goto err;
+               }
+       }
+
+       munmap(old_fdt, sbuf.st_size);
+       close(fd);
+
+       /* Pack the FDT and place the data after it */
+       fdt_pack(fdt);
+
+       new_size = fdt_totalsize(fdt);
+       debug("Size expanded from %x to %x\n", fit_size, new_size);
+
+       fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
+       if (fd < 0) {
+               fprintf(stderr, "%s: Can't open %s: %s\n",
+                       params->cmdname, fname, strerror(errno));
+               free(fdt);
+               return -EIO;
+       }
+       if (write(fd, fdt, new_size) != new_size) {
+               debug("%s: Failed to write external data to file %s\n",
+                     __func__, strerror(errno));
+               ret = -EIO;
+               goto err;
+       }
 
        ret = 0;
 
 err:
+       free(fdt);
        close(fd);
        return ret;
 }
@@ -510,6 +629,11 @@ static int fit_handle_file(struct image_tool_params *params)
                goto err_system;
        }
 
+       /* Move the data so it is internal to the FIT, if needed */
+       ret = fit_import_data(params, tmpfile);
+       if (ret)
+               goto err_system;
+
        /*
         * Set hashes for images in the blob. Unfortunately we may need more
         * space in either FDT, so keep trying until we succeed.
@@ -527,8 +651,8 @@ static int fit_handle_file(struct image_tool_params *params)
        }
 
        if (ret) {
-               fprintf(stderr, "%s Can't add hashes to FIT blob\n",
-                       params->cmdname);
+               fprintf(stderr, "%s Can't add hashes to FIT blob: %d\n",
+                       params->cmdname, ret);
                goto err_system;
        }