X-Git-Url: https://git.librecmc.org/?p=oweals%2Fu-boot.git;a=blobdiff_plain;f=tools%2Ffit_image.c;h=88ff093d05bed6f5da152dbbad8428eec219e4d7;hp=0201cc44d8fff52ad92cf3c1dd357bdef4be4eac;hb=9452b7496f8b85ca3bdda1014495df1a6235a8de;hpb=a8c184663349cbaf029f97d306c63b1ffa7eb544 diff --git a/tools/fit_image.c b/tools/fit_image.c index 0201cc44d8..88ff093d05 100644 --- a/tools/fit_image.c +++ b/tools/fit_image.c @@ -17,6 +17,7 @@ #include "fit_common.h" #include "mkimage.h" #include +#include #include #include #include @@ -58,6 +59,14 @@ static int fit_add_file_data(struct image_tool_params *params, size_t size_inc, ret = fit_set_timestamp(ptr, 0, time); } + if (!ret) { + ret = fit_cipher_data(params->keydir, dest_blob, ptr, + params->comment, + params->require_keys, + params->engine_id, + params->cmdname); + } + if (!ret) { ret = fit_add_verification_data(params->keydir, dest_blob, ptr, params->comment, @@ -74,7 +83,6 @@ static int fit_add_file_data(struct image_tool_params *params, size_t size_inc, err_keydest: munmap(ptr, sbuf.st_size); close(tfd); - return ret; } @@ -415,7 +423,7 @@ err_buf: */ static int fit_extract_data(struct image_tool_params *params, const char *fname) { - void *buf; + void *buf = NULL; int buf_ptr; int fit_size, new_size; int fd; @@ -424,26 +432,33 @@ static int fit_extract_data(struct image_tool_params *params, const char *fname) int ret; int images; int node; + int image_number; + int align_size; + align_size = params->bl_len ? params->bl_len : 4; fd = mmap_fdt(params->cmdname, fname, 0, &fdt, &sbuf, false, false); if (fd < 0) return -EIO; fit_size = fdt_totalsize(fdt); - /* Allocate space to hold the image data we will extract */ - buf = malloc(fit_size); - if (!buf) { - ret = -ENOMEM; - goto err_munmap; - } - buf_ptr = 0; - 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_munmap; } + image_number = fdtdec_get_child_count(fdt, images); + + /* + * Allocate space to hold the image data we will extract, + * extral space allocate for image alignment to prevent overflow. + */ + buf = malloc(fit_size + (align_size * image_number)); + if (!buf) { + ret = -ENOMEM; + goto err_munmap; + } + buf_ptr = 0; for (node = fdt_first_subnode(fdt, images); node >= 0; @@ -471,17 +486,17 @@ static int fit_extract_data(struct image_tool_params *params, const char *fname) buf_ptr); } fdt_setprop_u32(fdt, node, FIT_DATA_SIZE_PROP, len); - - buf_ptr += (len + 3) & ~3; + buf_ptr += ALIGN(len, align_size); } /* Pack the FDT and place the data after it */ fdt_pack(fdt); + new_size = fdt_totalsize(fdt); + new_size = ALIGN(new_size, align_size); + fdt_set_totalsize(fdt, new_size); debug("Size reduced from %x to %x\n", fit_size, fdt_totalsize(fdt)); debug("External data size %x\n", buf_ptr); - new_size = fdt_totalsize(fdt); - new_size = (new_size + 3) & ~3; munmap(fdt, sbuf.st_size); if (ftruncate(fd, new_size)) { @@ -520,8 +535,7 @@ static int fit_extract_data(struct image_tool_params *params, const char *fname) err_munmap: munmap(fdt, sbuf.st_size); err: - if (buf) - free(buf); + free(buf); close(fd); return ret; } @@ -540,7 +554,7 @@ static int fit_import_data(struct image_tool_params *params, const char *fname) if (fd < 0) return -EIO; fit_size = fdt_totalsize(old_fdt); - data_base = (fit_size + 3) & ~3; + data_base = ALIGN(fit_size, 4); /* Allocate space to hold the new FIT */ size = sbuf.st_size + 16384; @@ -549,21 +563,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_has_fd; + goto err_munmap; } 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_has_fd; + goto err_munmap; } 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_has_fd; + goto err_munmap; } for (node = fdt_first_subnode(fdt, images); @@ -584,10 +598,12 @@ 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_has_fd; + goto err_munmap; } } + munmap(old_fdt, sbuf.st_size); + /* Close the old fd so we can re-use it. */ close(fd); @@ -602,22 +618,80 @@ static int fit_import_data(struct image_tool_params *params, const char *fname) fprintf(stderr, "%s: Can't open %s: %s\n", params->cmdname, fname, strerror(errno)); ret = -EIO; - goto err_no_fd; + goto err; } 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_has_fd; + goto err; } - ret = 0; - -err_has_fd: + free(fdt); close(fd); -err_no_fd: + return 0; + +err_munmap: munmap(old_fdt, sbuf.st_size); +err: free(fdt); + close(fd); + return ret; +} + +static int copyfile(const char *src, const char *dst) +{ + int fd_src = -1, fd_dst = -1; + void *buf = NULL; + ssize_t size; + size_t count; + int ret = -1; + + fd_src = open(src, O_RDONLY); + if (fd_src < 0) { + printf("Can't open file %s (%s)\n", src, strerror(errno)); + goto out; + } + + fd_dst = open(dst, O_WRONLY | O_CREAT, 0666); + if (fd_dst < 0) { + printf("Can't open file %s (%s)\n", dst, strerror(errno)); + goto out; + } + + buf = malloc(512); + if (!buf) { + printf("Can't allocate buffer to copy file\n"); + goto out; + } + + while (1) { + size = read(fd_src, buf, 512); + if (size < 0) { + printf("Can't read file %s\n", src); + goto out; + } + if (!size) + break; + + count = size; + size = write(fd_dst, buf, count); + if (size < 0) { + printf("Can't write file %s\n", dst); + goto out; + } + } + + ret = 0; + + out: + if (fd_src >= 0) + close(fd_src); + if (fd_dst >= 0) + close(fd_dst); + if (buf) + free(buf); + return ret; } @@ -636,6 +710,7 @@ err_no_fd: static int fit_handle_file(struct image_tool_params *params) { char tmpfile[MKIMAGE_MAX_TMPFILE_LEN]; + char bakfile[MKIMAGE_MAX_TMPFILE_LEN + 4] = {0}; char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN]; size_t size_inc; int ret; @@ -670,6 +745,10 @@ static int fit_handle_file(struct image_tool_params *params) snprintf(cmd, sizeof(cmd), "cp \"%s\" \"%s\"", params->imagefile, tmpfile); } + if (strlen(cmd) >= MKIMAGE_MAX_DTC_CMDLINE_LEN - 1) { + fprintf(stderr, "WARNING: command-line for FIT creation might be truncated and will probably fail.\n"); + } + if (*cmd && system(cmd) == -1) { fprintf (stderr, "%s: system(%s) failed: %s\n", params->cmdname, cmd, strerror(errno)); @@ -681,6 +760,14 @@ static int fit_handle_file(struct image_tool_params *params) if (ret) goto err_system; + /* + * Copy the tmpfile to bakfile, then in the following loop + * we copy bakfile to tmpfile. So we always start from the + * beginning. + */ + sprintf(bakfile, "%s%s", tmpfile, ".bak"); + rename(tmpfile, bakfile); + /* * Set hashes for images in the blob. Unfortunately we may need more * space in either FDT, so keep trying until we succeed. @@ -692,6 +779,11 @@ static int fit_handle_file(struct image_tool_params *params) * steps of this loop is enough to sign with several keys. */ for (size_inc = 0; size_inc < 64 * 1024; size_inc += 1024) { + if (copyfile(bakfile, tmpfile) < 0) { + printf("Can't copy %s to %s\n", bakfile, tmpfile); + ret = -EIO; + break; + } ret = fit_add_file_data(params, size_inc, tmpfile); if (!ret || ret != -ENOSPC) break; @@ -715,13 +807,16 @@ static int fit_handle_file(struct image_tool_params *params) params->cmdname, tmpfile, params->imagefile, strerror (errno)); unlink (tmpfile); + unlink(bakfile); unlink (params->imagefile); return EXIT_FAILURE; } + unlink(bakfile); return EXIT_SUCCESS; err_system: unlink(tmpfile); + unlink(bakfile); return -1; } @@ -741,9 +836,14 @@ static int fit_image_extract( { const void *file_data; size_t file_size = 0; + int ret; - /* get the "data" property of component at offset "image_noffset" */ - fit_image_get_data(fit, image_noffset, &file_data, &file_size); + /* get the data address and size of component at offset "image_noffset" */ + ret = fit_image_get_data_and_size(fit, image_noffset, &file_data, &file_size); + if (ret) { + fprintf(stderr, "Could not get component information\n"); + return ret; + } /* save the "file_data" into the file specified by "file_name" */ return imagetool_save_subimage(file_name, (ulong) file_data, file_size); @@ -820,9 +920,9 @@ static int fit_check_params(struct image_tool_params *params) { if (params->auto_its) return 0; - return ((params->dflag && (params->fflag || params->lflag)) || - (params->fflag && (params->dflag || params->lflag)) || - (params->lflag && (params->dflag || params->fflag))); + return ((params->dflag && params->fflag) || + (params->fflag && params->lflag) || + (params->lflag && params->dflag)); } U_BOOT_IMAGE_TYPE(