X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=tools%2Ffit_image.c;h=ddefa72f3479ffacf746d9b5d46a3b017b92937a;hb=59fa089b2b191c87bc1ad81b5ddd553de665001c;hp=3ecc88fb56589e6e666c0e782059096f980c2e6b;hpb=9f0f432c0aea1e70959a0c06938459d3175a36b0;p=oweals%2Fu-boot.git diff --git a/tools/fit_image.c b/tools/fit_image.c index 3ecc88fb56..ddefa72f34 100644 --- a/tools/fit_image.c +++ b/tools/fit_image.c @@ -123,13 +123,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: @@ -329,7 +330,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); @@ -346,9 +347,12 @@ static int fit_build(struct image_tool_params *params, const char *fname) goto err; } close(fd); + free(buf); return 0; err: + close(fd); +err_buf: free(buf); return -1; } @@ -385,7 +389,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 +397,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,7 +415,7 @@ 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; } fdt_setprop_u32(fdt, node, "data-offset", buf_ptr); fdt_setprop_u32(fdt, node, "data-size", len); @@ -447,10 +451,13 @@ static int fit_extract_data(struct image_tool_params *params, const char *fname) goto err; } close(fd); + return 0; - ret = 0; - +err_munmap: + munmap(fdt, sbuf.st_size); err: + if (buf) + free(buf); close(fd); return ret; } @@ -517,7 +524,7 @@ static int fit_import_data(struct image_tool_params *params, const char *fname) } } - munmap(fdt, sbuf.st_size); + munmap(old_fdt, sbuf.st_size); close(fd); /* Pack the FDT and place the data after it */ @@ -530,7 +537,8 @@ 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)); - goto err; + free(fdt); + return -EIO; } if (write(fd, fdt, new_size) != new_size) { debug("%s: Failed to write external data to file %s\n", @@ -538,11 +546,11 @@ static int fit_import_data(struct image_tool_params *params, const char *fname) ret = -EIO; goto err; } - close(fd); ret = 0; err: + free(fdt); close(fd); return ret; }