X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=tools%2Ffit_image.c;h=5aca634b5e935e6baf95312eef48f028f7cd8f2e;hb=d3d212b6240f75006c18e4c59b3b28d81eedb7d3;hp=58aa8e27db3ee19db661bde16ef4675c6ee775ba;hpb=2f6b47061d1979d241930843ff154c6f1f1ece58;p=oweals%2Fu-boot.git diff --git a/tools/fit_image.c b/tools/fit_image.c index 58aa8e27db..5aca634b5e 100644 --- a/tools/fit_image.c +++ b/tools/fit_image.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0+ /* * (C) Copyright 2008 Semihalf * @@ -10,8 +11,6 @@ * some functions added to address abstraction * * All rights reserved. - * - * SPDX-License-Identifier: GPL-2.0+ */ #include "imagetool.h" @@ -34,7 +33,8 @@ static int fit_add_file_data(struct image_tool_params *params, size_t size_inc, void *ptr; int ret = 0; - tfd = mmap_fdt(params->cmdname, tmpfile, size_inc, &ptr, &sbuf, true); + tfd = mmap_fdt(params->cmdname, tmpfile, size_inc, &ptr, &sbuf, true, + false); if (tfd < 0) return -EIO; @@ -42,7 +42,8 @@ static int fit_add_file_data(struct image_tool_params *params, size_t size_inc, struct stat dest_sbuf; destfd = mmap_fdt(params->cmdname, params->keydest, size_inc, - &dest_blob, &dest_sbuf, false); + &dest_blob, &dest_sbuf, false, + false); if (destfd < 0) { ret = -EIO; goto err_keydest; @@ -52,14 +53,17 @@ 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) { - time_t time = imagetool_get_source_date(params, sbuf.st_mtime); + time_t time = imagetool_get_source_date(params->cmdname, + sbuf.st_mtime); ret = fit_set_timestamp(ptr, 0, time); } if (!ret) { ret = fit_add_verification_data(params->keydir, dest_blob, ptr, params->comment, - params->require_keys); + params->require_keys, + params->engine_id, + params->cmdname); } if (dest_blob) { @@ -85,8 +89,15 @@ static int fit_calc_size(struct image_tool_params *params) size = imagetool_get_filesize(params, params->datafile); if (size < 0) return -1; - total_size = size; + + if (params->fit_ramdisk) { + size = imagetool_get_filesize(params, params->fit_ramdisk); + if (size < 0) + return -1; + total_size += size; + } + for (cont = params->content_head; cont; cont = cont->next) { size = imagetool_get_filesize(params, cont->fname); if (size < 0) @@ -177,7 +188,7 @@ static void get_basename(char *str, int size, const char *fname) * fit_write_images() - Write out a list of images to the FIT * * We always include the main image (params->datafile). If there are device - * tree files, we include an fdt@ node for each of those too. + * tree files, we include an fdt- node for each of those too. */ static int fit_write_images(struct image_tool_params *params, char *fdt) { @@ -191,22 +202,24 @@ static int fit_write_images(struct image_tool_params *params, char *fdt) /* First the main image */ typename = genimg_get_type_short_name(params->fit_image_type); - snprintf(str, sizeof(str), "%s@1", typename); + snprintf(str, sizeof(str), "%s-1", typename); 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, "os", genimg_get_os_short_name(params->os)); - fdt_property_string(fdt, "compression", + fdt_property_string(fdt, FIT_DESC_PROP, params->imagename); + fdt_property_string(fdt, FIT_TYPE_PROP, typename); + fdt_property_string(fdt, FIT_ARCH_PROP, + genimg_get_arch_short_name(params->arch)); + fdt_property_string(fdt, FIT_OS_PROP, + genimg_get_os_short_name(params->os)); + fdt_property_string(fdt, FIT_COMP_PROP, genimg_get_comp_short_name(params->comp)); - fdt_property_u32(fdt, "load", params->addr); - fdt_property_u32(fdt, "entry", params->ep); + fdt_property_u32(fdt, FIT_LOAD_PROP, params->addr); + fdt_property_u32(fdt, FIT_ENTRY_PROP, params->ep); /* * Put data last since it is large. SPL may only load the first part * of the DT, so this way it can access all the above fields. */ - ret = fdt_property_file(params, fdt, "data", params->datafile); + ret = fdt_property_file(params, fdt, FIT_DATA_PROP, params->datafile); if (ret) return ret; fdt_end_node(fdt); @@ -216,22 +229,39 @@ static int fit_write_images(struct image_tool_params *params, char *fdt) for (cont = params->content_head; cont; cont = cont->next) { if (cont->type != IH_TYPE_FLATDT) continue; - snprintf(str, sizeof(str), "%s@%d", FIT_FDT_PROP, ++upto); + snprintf(str, sizeof(str), "%s-%d", FIT_FDT_PROP, ++upto); fdt_begin_node(fdt, str); get_basename(str, sizeof(str), cont->fname); - fdt_property_string(fdt, "description", str); - ret = fdt_property_file(params, fdt, "data", cont->fname); + fdt_property_string(fdt, FIT_DESC_PROP, str); + ret = fdt_property_file(params, fdt, FIT_DATA_PROP, + cont->fname); if (ret) return ret; - fdt_property_string(fdt, "type", typename); - fdt_property_string(fdt, "arch", + fdt_property_string(fdt, FIT_TYPE_PROP, typename); + fdt_property_string(fdt, FIT_ARCH_PROP, genimg_get_arch_short_name(params->arch)); - fdt_property_string(fdt, "compression", + fdt_property_string(fdt, FIT_COMP_PROP, genimg_get_comp_short_name(IH_COMP_NONE)); fdt_end_node(fdt); } + /* And a ramdisk file if available */ + if (params->fit_ramdisk) { + fdt_begin_node(fdt, FIT_RAMDISK_PROP "-1"); + + fdt_property_string(fdt, FIT_TYPE_PROP, FIT_RAMDISK_PROP); + fdt_property_string(fdt, FIT_OS_PROP, + genimg_get_os_short_name(params->os)); + + ret = fdt_property_file(params, fdt, FIT_DATA_PROP, + params->fit_ramdisk); + if (ret) + return ret; + + fdt_end_node(fdt); + } + fdt_end_node(fdt); return 0; @@ -254,32 +284,43 @@ static void fit_write_configs(struct image_tool_params *params, char *fdt) int upto; fdt_begin_node(fdt, "configurations"); - fdt_property_string(fdt, "default", "conf@1"); + fdt_property_string(fdt, FIT_DEFAULT_PROP, "conf-1"); upto = 0; for (cont = params->content_head; cont; cont = cont->next) { if (cont->type != IH_TYPE_FLATDT) continue; typename = genimg_get_type_short_name(cont->type); - snprintf(str, sizeof(str), "conf@%d", ++upto); + snprintf(str, sizeof(str), "conf-%d", ++upto); fdt_begin_node(fdt, str); get_basename(str, sizeof(str), cont->fname); - fdt_property_string(fdt, "description", str); + fdt_property_string(fdt, FIT_DESC_PROP, str); typename = genimg_get_type_short_name(params->fit_image_type); - snprintf(str, sizeof(str), "%s@1", typename); + snprintf(str, sizeof(str), "%s-1", typename); fdt_property_string(fdt, typename, str); + fdt_property_string(fdt, FIT_LOADABLE_PROP, str); + + if (params->fit_ramdisk) + fdt_property_string(fdt, FIT_RAMDISK_PROP, + FIT_RAMDISK_PROP "-1"); - snprintf(str, sizeof(str), FIT_FDT_PROP "@%d", upto); + snprintf(str, sizeof(str), FIT_FDT_PROP "-%d", upto); fdt_property_string(fdt, FIT_FDT_PROP, str); fdt_end_node(fdt); } + if (!upto) { - fdt_begin_node(fdt, "conf@1"); + fdt_begin_node(fdt, "conf-1"); typename = genimg_get_type_short_name(params->fit_image_type); - snprintf(str, sizeof(str), "%s@1", typename); + snprintf(str, sizeof(str), "%s-1", typename); fdt_property_string(fdt, typename, str); + + if (params->fit_ramdisk) + fdt_property_string(fdt, FIT_RAMDISK_PROP, + FIT_RAMDISK_PROP "-1"); + fdt_end_node(fdt); } @@ -295,7 +336,7 @@ static int fit_build_fdt(struct image_tool_params *params, char *fdt, int size) return ret; fdt_finish_reservemap(fdt); fdt_begin_node(fdt, ""); - fdt_property_strf(fdt, "description", + fdt_property_strf(fdt, FIT_DESC_PROP, "%s image with one or more FDT blobs", genimg_get_type_name(params->fit_image_type)); fdt_property_strf(fdt, "creator", "U-Boot mkimage %s", PLAIN_VERSION); @@ -339,7 +380,7 @@ static int fit_build(struct image_tool_params *params, const char *fname) if (fd < 0) { fprintf(stderr, "%s: Can't open %s: %s\n", params->cmdname, fname, strerror(errno)); - goto err; + goto err_buf; } ret = write(fd, buf, size); if (ret != size) { @@ -381,7 +422,7 @@ static int fit_extract_data(struct image_tool_params *params, const char *fname) int images; int node; - fd = mmap_fdt(params->cmdname, fname, 0, &fdt, &sbuf, false); + fd = mmap_fdt(params->cmdname, fname, 0, &fdt, &sbuf, false, false); if (fd < 0) return -EIO; fit_size = fdt_totalsize(fdt); @@ -407,25 +448,26 @@ static int fit_extract_data(struct image_tool_params *params, const char *fname) const char *data; int len; - data = fdt_getprop(fdt, node, "data", &len); + data = fdt_getprop(fdt, node, FIT_DATA_PROP, &len); if (!data) continue; memcpy(buf + buf_ptr, data, len); debug("Extracting data size %x\n", len); - ret = fdt_delprop(fdt, node, "data"); + ret = fdt_delprop(fdt, node, FIT_DATA_PROP); if (ret) { ret = -EPERM; goto err_munmap; } if (params->external_offset > 0) { /* An external offset positions the data absolutely. */ - fdt_setprop_u32(fdt, node, "data-position", + fdt_setprop_u32(fdt, node, FIT_DATA_POSITION_PROP, params->external_offset + buf_ptr); } else { - fdt_setprop_u32(fdt, node, "data-offset", buf_ptr); + fdt_setprop_u32(fdt, node, FIT_DATA_OFFSET_PROP, + buf_ptr); } - fdt_setprop_u32(fdt, node, "data-size", len); + fdt_setprop_u32(fdt, node, FIT_DATA_SIZE_PROP, len); buf_ptr += (len + 3) & ~3; } @@ -468,6 +510,7 @@ static int fit_extract_data(struct image_tool_params *params, const char *fname) ret = -EIO; goto err; } + free(buf); close(fd); return 0; @@ -490,7 +533,7 @@ static int fit_import_data(struct image_tool_params *params, const char *fname) int images; int node; - fd = mmap_fdt(params->cmdname, fname, 0, &old_fdt, &sbuf, false); + fd = mmap_fdt(params->cmdname, fname, 0, &old_fdt, &sbuf, false, false); if (fd < 0) return -EIO; fit_size = fdt_totalsize(old_fdt); @@ -503,21 +546,21 @@ static int fit_import_data(struct image_tool_params *params, const char *fname) fprintf(stderr, "%s: Failed to allocate memory (%d bytes)\n", __func__, size); ret = -ENOMEM; - goto err; + goto err_has_fd; } 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; + goto err_has_fd; } 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; + goto err_has_fd; } for (node = fdt_first_subnode(fdt, images); @@ -538,11 +581,11 @@ static int fit_import_data(struct image_tool_params *params, const char *fname) debug("%s: Failed to write property: %s\n", __func__, fdt_strerror(ret)); ret = -EINVAL; - goto err; + goto err_has_fd; } } - munmap(old_fdt, sbuf.st_size); + /* Close the old fd so we can re-use it. */ close(fd); /* Pack the FDT and place the data after it */ @@ -555,21 +598,23 @@ static int fit_import_data(struct image_tool_params *params, const char *fname) if (fd < 0) { fprintf(stderr, "%s: Can't open %s: %s\n", params->cmdname, fname, strerror(errno)); - free(fdt); - return -EIO; + ret = -EIO; + goto err_no_fd; } 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; + goto err_has_fd; } ret = 0; -err: - free(fdt); +err_has_fd: close(fd); +err_no_fd: + munmap(old_fdt, sbuf.st_size); + free(fdt); return ret; } @@ -614,12 +659,12 @@ static int fit_handle_file(struct image_tool_params *params) } *cmd = '\0'; } else if (params->datafile) { - /* dtc -I dts -O dtb -p 500 datafile > tmpfile */ - snprintf(cmd, sizeof(cmd), "%s %s %s > %s", - MKIMAGE_DTC, params->dtc, params->datafile, tmpfile); + /* dtc -I dts -O dtb -p 500 -o tmpfile datafile */ + snprintf(cmd, sizeof(cmd), "%s %s -o \"%s\" \"%s\"", + MKIMAGE_DTC, params->dtc, tmpfile, params->datafile); debug("Trying to execute \"%s\"\n", cmd); } else { - snprintf(cmd, sizeof(cmd), "cp %s %s", + snprintf(cmd, sizeof(cmd), "cp \"%s\" \"%s\"", params->imagefile, tmpfile); } if (*cmd && system(cmd) == -1) { @@ -650,8 +695,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; }