[openwrt/openwrt] bcm4908img: store offset of tail data

LEDE Commits lede-commits at lists.infradead.org
Sat Oct 2 11:27:04 PDT 2021


rmilecki pushed a commit to openwrt/openwrt.git, branch master:
https://git.openwrt.org/063038bcef39802aa65c196a162067f66e5c8621

commit 063038bcef39802aa65c196a162067f66e5c8621
Author: Rafał Miłecki <rafal at milecki.pl>
AuthorDate: Fri Sep 10 16:34:47 2021 +0200

    bcm4908img: store offset of tail data
    
    This simplifies some operations as it doesn't have to be caculated over
    and over. It will also allow adding support for more vendor formats.
    
    Signed-off-by: Rafał Miłecki <rafal at milecki.pl>
---
 package/utils/bcm4908img/Makefile         |  2 +-
 package/utils/bcm4908img/src/bcm4908img.c | 23 +++++++++++++----------
 2 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/package/utils/bcm4908img/Makefile b/package/utils/bcm4908img/Makefile
index c18ac958f8..ff504a8078 100644
--- a/package/utils/bcm4908img/Makefile
+++ b/package/utils/bcm4908img/Makefile
@@ -3,7 +3,7 @@
 include $(TOPDIR)/rules.mk
 
 PKG_NAME:=bcm4908img
-PKG_RELEASE:=1
+PKG_RELEASE:=2
 
 PKG_FLAGS:=nonshared
 
diff --git a/package/utils/bcm4908img/src/bcm4908img.c b/package/utils/bcm4908img/src/bcm4908img.c
index 402d317190..f2023ced9f 100644
--- a/package/utils/bcm4908img/src/bcm4908img.c
+++ b/package/utils/bcm4908img/src/bcm4908img.c
@@ -79,11 +79,11 @@ struct bcm4908img_tail {
  * 6. BCM4908 tail
  */
 struct bcm4908img_info {
-	size_t file_size;
 	size_t cferom_offset;
 	size_t bootfs_offset;
 	size_t padding_offset;
 	size_t rootfs_offset;
+	size_t tail_offset;
 	uint32_t crc32;			/* Calculated checksum */
 	struct bcm4908img_tail tail;
 };
@@ -219,7 +219,7 @@ static int bcm4908img_calc_crc32(FILE *fp, struct bcm4908img_info *info) {
 	fseek(fp, info->cferom_offset, SEEK_SET);
 
 	info->crc32 = 0xffffffff;
-	length = info->file_size - info->cferom_offset - sizeof(struct bcm4908img_tail);
+	length = info->tail_offset - info->cferom_offset;
 	while (length && (bytes = fread(buf, 1, bcm4908img_min(sizeof(buf), length), fp)) > 0) {
 		info->crc32 = bcm4908img_crc32(info->crc32, buf, bytes);
 		length -= bytes;
@@ -267,6 +267,7 @@ static int bcm4908img_parse(FILE *fp, struct bcm4908img_info *info) {
 	struct chk_header *chk;
 	struct stat st;
 	uint8_t buf[1024];
+	size_t file_size;
 	uint16_t tmp16;
 	size_t length;
 	size_t bytes;
@@ -281,7 +282,9 @@ static int bcm4908img_parse(FILE *fp, struct bcm4908img_info *info) {
 		fprintf(stderr, "Failed to fstat: %d\n", err);
 		return err;
 	}
-	info->file_size = st.st_size;
+	file_size = st.st_size;
+
+	info->tail_offset = file_size - sizeof(*tail);
 
 	/* Vendor formats */
 
@@ -297,7 +300,7 @@ static int bcm4908img_parse(FILE *fp, struct bcm4908img_info *info) {
 	/* Offsets */
 
 	for (info->bootfs_offset = info->cferom_offset;
-	     info->bootfs_offset < info->file_size;
+	     info->bootfs_offset < info->tail_offset;
 	     info->bootfs_offset += 0x20000) {
 		if (fseek(fp, info->bootfs_offset, SEEK_SET)) {
 			err = -errno;
@@ -311,13 +314,13 @@ static int bcm4908img_parse(FILE *fp, struct bcm4908img_info *info) {
 		if (be16_to_cpu(tmp16) == 0x8519)
 			break;
 	}
-	if (info->bootfs_offset >= info->file_size) {
+	if (info->bootfs_offset >= info->tail_offset) {
 		fprintf(stderr, "Failed to find bootfs offset\n");
 		return -EPROTO;
 	}
 
 	for (info->rootfs_offset = info->bootfs_offset;
-	     info->rootfs_offset < info->file_size;
+	     info->rootfs_offset < info->tail_offset;
 	     info->rootfs_offset += 0x20000) {
 		uint32_t *magic = (uint32_t *)&buf[0];
 
@@ -340,7 +343,7 @@ static int bcm4908img_parse(FILE *fp, struct bcm4908img_info *info) {
 		if (be32_to_cpu(*magic) == UBI_EC_HDR_MAGIC)
 			break;
 	}
-	if (info->rootfs_offset >= info->file_size) {
+	if (info->rootfs_offset >= info->tail_offset) {
 		fprintf(stderr, "Failed to find rootfs offset\n");
 		return -EPROTO;
 	}
@@ -351,7 +354,7 @@ static int bcm4908img_parse(FILE *fp, struct bcm4908img_info *info) {
 	fseek(fp, info->cferom_offset, SEEK_SET);
 
 	info->crc32 = 0xffffffff;
-	length = info->file_size - info->cferom_offset - sizeof(*tail);
+	length = info->tail_offset - info->cferom_offset;
 	while (length && (bytes = fread(buf, 1, bcm4908img_min(sizeof(buf), length), fp)) > 0) {
 		info->crc32 = bcm4908img_crc32(info->crc32, buf, bytes);
 		length -= bytes;
@@ -617,10 +620,10 @@ static int bcm4908img_extract(int argc, char **argv) {
 		length = (info.padding_offset ? info.padding_offset : info.rootfs_offset) - offset;
 	} else if (!strcmp(type, "rootfs")) {
 		offset = info.rootfs_offset;
-		length = info.file_size - offset - sizeof(struct bcm4908img_tail);
+		length = info.tail_offset - offset;
 	} else if (!strcmp(type, "firmware")) {
 		offset = info.bootfs_offset;
-		length = info.file_size - offset - sizeof(struct bcm4908img_tail);
+		length = info.tail_offset - offset;
 	} else {
 		err = -EINVAL;
 		fprintf(stderr, "Unsupported extract type: %s\n", type);



More information about the lede-commits mailing list