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@milecki.pl>
This commit is contained in:
Rafał Miłecki 2021-09-10 16:34:47 +02:00
parent 8ff8323335
commit 063038bcef
2 changed files with 14 additions and 11 deletions

View File

@ -3,7 +3,7 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=bcm4908img
PKG_RELEASE:=1
PKG_RELEASE:=2
PKG_FLAGS:=nonshared

View File

@ -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);