]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
tools: kwbimage: Simplify aligning and calculating checksum
authorPali Rohár <pali@kernel.org>
Fri, 23 Jul 2021 09:13:56 +0000 (11:13 +0200)
committerStefan Roese <sr@denx.de>
Sat, 31 Jul 2021 07:49:31 +0000 (09:49 +0200)
The return value of kwbimage_generate() is used for aligning the data
part of kwbimage. Use it for calculating proper 4 byte alignment as is
required by BootROM and also use it for allocating additional 4 bytes
for the 32-bit data checksum.

This simplifies the alignment code to be only at one place (in function
kwbimage_generate) and also simplifies setting checksum as it can be
directly updated in memory.

Signed-off-by: Pali Rohár <pali@kernel.org>
Reviewed-by: Marek Behún <marek.behun@nic.cz>
Reviewed-by: Stefan Roese <sr@denx.de>
Reviewed-by: Chris Packham <judge.packham@gmail.com>
Tested-by: Chris Packham <judge.packham@gmail.com>
tools/kwbimage.c

index 02fd0c949fff6dbb94224bf783ef4d6f1e247d92..c775abf0e3d21135a697743e303f25cc600d9b71 100644 (file)
@@ -891,7 +891,7 @@ static void *image_create_v0(size_t *imagesz, struct image_tool_params *params,
 
        /* Fill in the main header */
        main_hdr->blocksize =
-               cpu_to_le32(payloadsz + sizeof(uint32_t) - headersz);
+               cpu_to_le32(payloadsz - headersz);
        main_hdr->srcaddr   = cpu_to_le32(headersz);
        main_hdr->ext       = has_ext;
        main_hdr->destaddr  = cpu_to_le32(params->addr);
@@ -1249,7 +1249,7 @@ static void *image_create_v1(size_t *imagesz, struct image_tool_params *params,
 
        /* Fill the main header */
        main_hdr->blocksize    =
-               cpu_to_le32(payloadsz - headersz + sizeof(uint32_t));
+               cpu_to_le32(payloadsz - headersz);
        main_hdr->headersz_lsb = cpu_to_le16(headersz & 0xFFFF);
        main_hdr->headersz_msb = (headersz & 0xFFFF0000) >> 16;
        main_hdr->destaddr     = cpu_to_le32(params->addr)
@@ -1519,7 +1519,6 @@ static void kwbimage_set_header(void *ptr, struct stat *sbuf, int ifd,
        size_t headersz = 0;
        uint32_t checksum;
        int ret;
-       int size;
 
        fcfg = fopen(params->imagename, "r");
        if (!fcfg) {
@@ -1547,9 +1546,6 @@ static void kwbimage_set_header(void *ptr, struct stat *sbuf, int ifd,
                exit(EXIT_FAILURE);
        }
 
-       /* The MVEBU BootROM does not allow non word aligned payloads */
-       sbuf->st_size = ALIGN(sbuf->st_size, 4);
-
        version = image_get_version();
        switch (version) {
                /*
@@ -1580,16 +1576,10 @@ static void kwbimage_set_header(void *ptr, struct stat *sbuf, int ifd,
        free(image_cfg);
 
        /* Build and add image checksum header */
-       checksum =
-               cpu_to_le32(image_checksum32((uint32_t *)ptr, sbuf->st_size));
-       size = write(ifd, &checksum, sizeof(uint32_t));
-       if (size != sizeof(uint32_t)) {
-               fprintf(stderr, "Error:%s - Checksum write %d bytes %s\n",
-                       params->cmdname, size, params->imagefile);
-               exit(EXIT_FAILURE);
-       }
-
-       sbuf->st_size += sizeof(uint32_t);
+       checksum = cpu_to_le32(image_checksum32((uint8_t *)ptr + headersz,
+                               sbuf->st_size - headersz - sizeof(uint32_t)));
+       memcpy((uint8_t *)ptr + sbuf->st_size - sizeof(uint32_t), &checksum,
+               sizeof(uint32_t));
 
        /* Finally copy the header into the image area */
        memcpy(ptr, image, headersz);
@@ -1650,6 +1640,7 @@ static int kwbimage_generate(struct image_tool_params *params,
                             struct image_type_params *tparams)
 {
        FILE *fcfg;
+       struct stat s;
        int alloc_len;
        int version;
        void *hdr;
@@ -1662,6 +1653,12 @@ static int kwbimage_generate(struct image_tool_params *params,
                exit(EXIT_FAILURE);
        }
 
+       if (stat(params->datafile, &s)) {
+               fprintf(stderr, "Could not stat data file %s: %s\n",
+                       params->datafile, strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
        image_cfg = malloc(IMAGE_CFG_ELEMENT_MAX *
                           sizeof(struct image_cfg_element));
        if (!image_cfg) {
@@ -1719,12 +1716,9 @@ static int kwbimage_generate(struct image_tool_params *params,
        /*
         * The resulting image needs to be 4-byte aligned. At least
         * the Marvell hdrparser tool complains if its unaligned.
-        * By returning 1 here in this function, called via
-        * tparams->vrec_header() in mkimage.c, mkimage will
-        * automatically pad the the resulting image to a 4-byte
-        * size if necessary.
+        * After the image data is stored 4-byte checksum.
         */
-       return 1;
+       return 4 + (4 - s.st_size % 4) % 4;
 }
 
 /*