>From 1e9e0c9515e644abeda066c5387d1338d927cbda Mon Sep 17 00:00:00 2001 From: Marco Felsch Date: Wed, 26 Apr 2023 21:51:26 +0200 Subject: [PATCH] fixup! sunxi: add image support --- scripts/sunxi_egon.c | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/scripts/sunxi_egon.c b/scripts/sunxi_egon.c index 6d74a83ad8..b339955295 100644 --- a/scripts/sunxi_egon.c +++ b/scripts/sunxi_egon.c @@ -29,11 +29,12 @@ static int write_image(int infd, int outfd, enum sunxi_arch arch, unsigned long pblsize, unsigned long ofs) { struct boot_file_head *header; + unsigned int header_sz; uint32_t checksum = 0; - void *buf, *origbuf; unsigned long size; uint32_t *buf32; uint32_t value; + void *buf; int ret; int i; @@ -43,8 +44,10 @@ static int write_image(int infd, int outfd, enum sunxi_arch arch, if (!buf) return 1; + memset(buf, 0, size); + header = buf; - buf32 = buf; + header_sz = sizeof(*header); /* * Different architectures need different first instruction to @@ -53,7 +56,7 @@ static int write_image(int infd, int outfd, enum sunxi_arch arch, switch (arch) { case SUNXI_ARCH_ARM: /* Generate an ARM branch instruction to jump over the header. */ - value = 0xea000000 | (sizeof(*header) / 4 - 2); + value = 0xea000000 | (header_sz / 4 - 2); header->b_instruction = cpu_to_le32(value); break; case SUNXI_ARCH_RISCV: @@ -68,10 +71,10 @@ static int write_image(int infd, int outfd, enum sunxi_arch arch, * is not allowed). */ value = 0x0000006f | - ((sizeof(*header) & 0x00100000) << 11) | - ((sizeof(*header) & 0x000007fe) << 20) | - ((sizeof(*header) & 0x00000800) << 9) | - ((sizeof(*header) & 0x000ff000) << 0); + ((header_sz & 0x00100000) << 11) | + ((header_sz & 0x000007fe) << 20) | + ((header_sz & 0x00000800) << 9) | + ((header_sz & 0x000ff000) << 0); header->b_instruction = cpu_to_le32(value); break; default: @@ -85,33 +88,32 @@ static int write_image(int infd, int outfd, enum sunxi_arch arch, memcpy(header->spl_signature, SPL_SIGNATURE, 3); header->spl_signature[3] = SPL_ENV_HEADER_VERSION; - /* Calculate the checksum. Yes, it's that simple. */ - for (i = 0; i < size / 4; i++) - checksum += le32_to_cpu(buf32[i]); - header->check_sum = cpu_to_le32(checksum); - - origbuf = buf; - buf += sizeof(*header); - ret = read(infd, buf, pblsize); + ret = read(infd, buf + header_sz, pblsize); if (ret > pblsize) { printf("Error: While read: 0x%d > 0x%ld bytes!\n", ret, pblsize); - free(origbuf); + free(buf); return 1; } + /* Calculate the checksum. Yes, it's that simple. */ + buf32 = buf; + for (i = 0; i < size / 4; i++) + checksum += le32_to_cpu(buf32[i]); + header->check_sum = cpu_to_le32(checksum); + if (ofs) lseek(outfd, ofs, SEEK_SET); - ret = write(outfd, origbuf, size); + ret = write(outfd, buf, size); if (ret != size) { printf("Error: While write: 0x%d != 0x%ld bytes!\n", ret, size); - free(origbuf); + free(buf); return 1; } - free(origbuf); + free(buf); return 0; } -- 2.39.2