This patch adds a function to mkimage.c called full_write() which calls write() in a loop until all the data has been written or write errors.
I was getting an error with mkimage running out of space on the device it was writing to, but instead of printing a useful error message it was printing: mkimage: Write error on /tmp/flash.img: Success Which is kinda confusing. It was printing this because write() was writing as many bytes as it could and then returning success. It needs to be called a second time to set errno. Also write(fd, data, count) isn't guranteed to write the full count bytes in one go anyway, so it needs to be called in a loop (that or use fwrite instead).
diff --git a/tools/mkimage.c b/tools/mkimage.c index 123d0c7..bb32ea8 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -29,6 +29,20 @@ struct image_tool_params params = { .imagename2 = "", }; +static ssize_t full_write(int fd, const void *buf, size_t count) { + ssize_t remaining = (ssize_t)count; + for(;;) { + ssize_t written; + written = write(fd, buf, remaining); + if(written < 0) + return written; + remaining -= written; + buf += written; + if(remaining <= 0) + return count - remaining;; + }; +}; + /* * mkimage_register - * @@ -382,8 +396,8 @@ NXTARG: ; else memset(tparams->hdr, 0, tparams->header_size); - if (write(ifd, tparams->hdr, tparams->header_size) - != tparams->header_size) { + int bytes_written = full_write(ifd, tparams->hdr, tparams->header_size); + if (bytes_written != tparams->header_size) { fprintf (stderr, "%s: Write error on %s: %s\n", params.cmdname, params.imagefile, strerror(errno)); exit (EXIT_FAILURE); @@ -413,10 +427,12 @@ NXTARG: ; size = 0; } - if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) { + bytes_written = full_write(ifd, (char *)&size, sizeof(size)); + if (bytes_written != sizeof(size)) { fprintf (stderr, "%s: Write error on %s: %s\n", params.cmdname, params.imagefile, strerror(errno)); + exit (EXIT_FAILURE); } @@ -588,7 +604,8 @@ copy_file (int ifd, const char *datafile, int pad) } size = sbuf.st_size - offset; - if (write(ifd, ptr + offset, size) != size) { + int bytes_written = full_write(ifd, ptr + offset, size); + if (bytes_written != size) { fprintf (stderr, "%s: Write error on %s: %s\n", params.cmdname, params.imagefile, strerror(errno)); exit (EXIT_FAILURE); @@ -597,14 +614,16 @@ copy_file (int ifd, const char *datafile, int pad) tail = size % 4; if ((pad == 1) && (tail != 0)) { - if (write(ifd, (char *)&zero, 4-tail) != 4-tail) { + bytes_written = full_write(ifd, (char *)&zero, 4-tail); + if (bytes_written != 4-tail) { fprintf (stderr, "%s: Write error on %s: %s\n", params.cmdname, params.imagefile, strerror(errno)); exit (EXIT_FAILURE); } } else if (pad > 1) { - if (write(ifd, (char *)&zeros, pad) != pad) { + bytes_written = full_write(ifd, (char *)&zeros, pad); + if (bytes_written != pad) { fprintf(stderr, "%s: Write error on %s: %s\n", params.cmdname, params.imagefile, strerror(errno));
_______________________________________________ U-Boot mailing list U-Boot@lists.denx.de http://lists.denx.de/mailman/listinfo/u-boot