From patchwork Fri Sep 10 14:34:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= X-Patchwork-Id: 1526552 X-Patchwork-Delegate: zajec5@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=apI1k++0; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20210112 header.b=e5VaZV8U; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4H5dmB74Cbz9sQt for ; Sat, 11 Sep 2021 00:38:50 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=n8CbmAtHm/MXucIXFGQEeTCxqBY6+ps00qNRJXzJFDs=; b=apI1k++0DddUxy VGon2QB8JSX7MGHPYhby2gceaZGEYpMKQyxaYlyipM8+FNGHrG4w20uv2OYGCnTccm31m8ZK5N8tk hlhZX9HKvcAdfAvo/GvGMh8OOMMVnJ0lVD+0iiYuZU4qkrlnYpELa13kK2y9XjLozJiVe3lDYQaMW aJOam4jLo3m5KL3Xtr26ZPxzpeDMPkOji91GJc+06xE7ju+L+I/9co+njrgokQNmyzOkGwGTYgMn6 eeJ9IzBgF7icpflSiTJO7Kh3jiLQVTYTCTxW7X2+1/7UzIAniDBSi4RHiqQSc27DGDRuri/7BffNV 8s7eZ3YgB106807B1qOg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mOhcI-00CxGI-8d; Fri, 10 Sep 2021 14:34:58 +0000 Received: from mail-lf1-x12b.google.com ([2a00:1450:4864:20::12b]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mOhcE-00CxEe-EI for openwrt-devel@lists.openwrt.org; Fri, 10 Sep 2021 14:34:56 +0000 Received: by mail-lf1-x12b.google.com with SMTP id k13so4499219lfv.2 for ; Fri, 10 Sep 2021 07:34:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Wl/7SggDzbhcyyf3PySVBDmDU+Y2EMTBC8BlymGUslg=; b=e5VaZV8UW8CDJy6embZtno4qzPwwyU2buVq2EbH8HajgY/pePSlK/PxxW83m9SFIQe xcnmfdYcRQuGcCQd8D5m6VKfUfZ4ElNndlXV2/U7teBTXcYrQ6vS4pXUEwEKHNVmreOV Lq9jws95C1D3vixlQUxxoMXrBbgQpgjORz4vGr9FNikxSFl2VLk5I6A20CRsxCW5WjbY lT21QgRDd+2BfBwOGSBQ/UgQPQnRO6FnXNXXghq563GUR6+cM2vX8EKVy2vcL4t8aCFZ l+qzpgZ4BH5+KBiFI2CrEE0uAsTAVOfroc4oU1HL7h7A1OCIB4t+mn9jGgJk20uFruuf k6Fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Wl/7SggDzbhcyyf3PySVBDmDU+Y2EMTBC8BlymGUslg=; b=Qb0QX1+/HGaNqKX5sua9XswOrk1jjOIUU7sIRBKxoWyszFUK4CfANhasn1nc7QM8Bj Hv7CcIpFhufQ9phS0+IrHgdU7L6t3Aj0//3J2sgD2KPYzGcfbLHMAU+I8NOCCDxFKS+q e8Kvj08GbSAQcsE/bZ2UvY7j07QJNgZUJencH6etVr9J3sN/MeMhnkHek7MiBh4qA+VB WXzdoc6xeuuJI8qht0tO0Eke+CpWbw4l9TABBinOSNkVLZNswYPnKtYEiT43pXv5D75Z JUzDDMCigVBRdy6xBew4vJh44+AMKliyDebOr2PZc35tcPoj3/Krj+Aq/htGFYDtbKcZ RdKw== X-Gm-Message-State: AOAM530cdGtOnuX7kvjb4uux0LzsT+6/jNDkICc1P7xHdvLv1HP2RCFY mhbLGs8+RAqK5E+ryk2VVUhmopsbxL0= X-Google-Smtp-Source: ABdhPJzwsVy0OhRYFtDw8cqeZmbo6c43fjQYkDLs6Mpq3KwjvXHeoynvRt7gNusMlm9AfKnriwE/Iw== X-Received: by 2002:a05:6512:1383:: with SMTP id p3mr4147791lfa.648.1631284492302; Fri, 10 Sep 2021 07:34:52 -0700 (PDT) Received: from localhost.lan (ip-194-187-74-233.konfederacka.maverick.com.pl. [194.187.74.233]) by smtp.gmail.com with ESMTPSA id 25sm592734ljw.31.2021.09.10.07.34.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Sep 2021 07:34:51 -0700 (PDT) From: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= To: openwrt-devel@lists.openwrt.org Cc: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= Subject: [PATCH 1/2] bcm4908img: store offset of tail data Date: Fri, 10 Sep 2021 16:34:47 +0200 Message-Id: <20210910143448.1776-1-zajec5@gmail.com> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210910_073454_532680_45478EEF X-CRM114-Status: GOOD ( 15.36 ) X-Spam-Score: 0.1 (/) X-Spam-Report: =?unknown-8bit?q?Spam_detection_software=2C_running_on_the_sy?= =?unknown-8bit?q?stem_=22bombadil=2Einfradead=2Eorg=22=2C?= =?unknown-8bit?q?_has_NOT_identified_this_incoming_email_as_spam=2E__The_ori?= =?unknown-8bit?q?ginal?= =?unknown-8bit?q?_message_has_been_attached_to_this_so_you_can_view_it_or_la?= =?unknown-8bit?q?bel?= =?unknown-8bit?q?_similar_future_email=2E__If_you_have_any_questions=2C_see?= =?unknown-8bit?q?_the_administrator_of_that_system_for_details=2E?= =?unknown-8bit?q?_?= =?unknown-8bit?q?_Content_preview=3A__From=3A_Rafa=C5=82_Mi=C5=82ecki_This_s?= =?unknown-8bit?q?implifies_some_operations_as_it?= =?unknown-8bit?q?_doesn=27t_have_to_be_caculated_over_and_over=2E_It_will_al?= =?unknown-8bit?q?so_allow_adding_support?= =?unknown-8bit?q?_for_more_vendor_formats=2E_Signed-off-by=3A_Rafa=C5=82_Mi?= =?unknown-8bit?q?=C5=82ecki_---_package/utils/bcm4908img/Makefile?= =?unknown-8bit?q?_=7C_2_+-_package/utils/bcm4908img/src/bcm4908img=2Ec_=7C_2?= =?unknown-8bit?q?3_+++++++++++++_2_files?= =?unknown-8bit?q?_changed=2C_14_insertions=28+=29=2C_11_deletions=28-=29_?= =?unknown-8bit?q?_?= =?unknown-8bit?q?_Content_analysis_details=3A___=280=2E1_points=2C_5=2E0_req?= =?unknown-8bit?q?uired=29?= =?unknown-8bit?q?_?= =?unknown-8bit?q?_pts_rule_name______________description?= =?unknown-8bit?q?_----_----------------------_------------------------------?= =?unknown-8bit?q?--------------------?= =?unknown-8bit?q?_-0=2E0_RCVD=5FIN=5FDNSWL=5FNONE_____RBL=3A_Sender_listed_a?= =?unknown-8bit?q?t_https=3A//www=2Ednswl=2Eorg/=2C?= =?unknown-8bit?q?_no_trust?= =?unknown-8bit?b?IFsyYTAwOjE0NTA6NDg2NDoyMDowOjA6MDoxMmIgbGlzdGVkIGluXQ==?= =?unknown-8bit?b?IFtsaXN0LmRuc3dsLm9yZ10=?= =?unknown-8bit?q?_-0=2E0_SPF=5FPASS_______________SPF=3A_sender_matches_SPF_?= =?unknown-8bit?q?record?= =?unknown-8bit?q?_0=2E0_SPF=5FHELO=5FNONE__________SPF=3A_HELO_does_not_publ?= =?unknown-8bit?q?ish_an_SPF_Record?= =?unknown-8bit?q?_0=2E0_FREEMAIL=5FFROM__________Sender_email_is_commonly_ab?= =?unknown-8bit?q?used_enduser_mail?= =?unknown-8bit?q?_provider?= =?unknown-8bit?q?_=5Bzajec5=5Bat=5Dgmail=2Ecom=5D?= =?unknown-8bit?q?_0=2E2_FREEMAIL=5FENVFROM=5FEND=5FDIGIT_Envelope-from_freem?= =?unknown-8bit?q?ail_username_ends?= =?unknown-8bit?q?_in_digit?= =?unknown-8bit?q?_=5Bzajec5=5Bat=5Dgmail=2Ecom=5D?= =?unknown-8bit?q?_-0=2E1_DKIM=5FVALID=5FAU__________Message_has_a_valid_DKIM?= =?unknown-8bit?q?_or_DK_signature_from?= =?unknown-8bit?q?_author=27s_domain?= =?unknown-8bit?q?_-0=2E1_DKIM=5FVALID_____________Message_has_at_least_one_v?= =?unknown-8bit?q?alid_DKIM_or_DK_signature?= =?unknown-8bit?q?_0=2E1_DKIM=5FSIGNED____________Message_has_a_DKIM_or_DK_si?= =?unknown-8bit?q?gnature=2C_not_necessarily?= =?unknown-8bit?q?_valid?= =?unknown-8bit?q?_-0=2E1_DKIM=5FVALID=5FEF__________Message_has_a_valid_DKIM?= =?unknown-8bit?q?_or_DK_signature_from?= =?unknown-8bit?q?_envelope-from_domain?= X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org From: Rafał Miłecki 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 --- 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);