From mboxrd@z Thu Jan 1 00:00:00 1970 Delivery-date: Fri, 19 May 2023 14:11:55 +0200 Received: from metis.ext.pengutronix.de ([2001:67c:670:201:290:27ff:fe1d:cc33]) by lore.white.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1pzyxg-006dyv-9i for lore@lore.pengutronix.de; Fri, 19 May 2023 14:11:54 +0200 Received: from bombadil.infradead.org ([2607:7c80:54:3::133]) by metis.ext.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pzyxd-00049X-Ur for lore@pengutronix.de; Fri, 19 May 2023 14:11:54 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: Content-Type: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=5ECzXTOqtzG+JBewNs1V+C8yre3HNg6c0lsyn+Q//FQ=; b=2EL26RFrMVLV8EVmcUPU3hEUGD CjV56t19hTe2HHKK/IRQDkhPogh6G9XWBx4Rv/HZUhNNz6Y1jafb0HJmDbB+rN1CHz7xM/Ae9Lii9 JZutZ4CkrZ93hQmoXVNyVlYhL9i0lx6W5ZbY4+haPNPU3dVhZxas1+lvkonwkEJbYDkNN4G/rNYKe SjE0ZDxBTvfJ7LCdhD90REvY64CEdRBhMYax/HOv4D6/hgjJSQuB05OWWOZWinC8XIfzG/JcWo4d3 IAAZ+gIdbP0PVx7rPJjYHVdWMXJkCF9Z/OR4mmB5AlP7+TyODCGUov7MuFejSSCJqnETbTe/QEoFG s97/V07g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pzywR-00GBvW-26; Fri, 19 May 2023 12:10:39 +0000 Received: from metis.ext.pengutronix.de ([2001:67c:670:201:290:27ff:fe1d:cc33]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pzywN-00GBuI-3B for barebox@lists.infradead.org; Fri, 19 May 2023 12:10:37 +0000 Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.ext.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pzywJ-0003zV-8H; Fri, 19 May 2023 14:10:31 +0200 Received: from [2a0a:edc0:0:1101:1d::54] (helo=dude05.red.stw.pengutronix.de) by drehscheibe.grey.stw.pengutronix.de with esmtp (Exim 4.94.2) (envelope-from ) id 1pzywI-001IhB-K6; Fri, 19 May 2023 14:10:30 +0200 Received: from afa by dude05.red.stw.pengutronix.de with local (Exim 4.94.2) (envelope-from ) id 1pzywH-00AO52-PP; Fri, 19 May 2023 14:10:29 +0200 From: Ahmad Fatoum To: barebox@lists.infradead.org Cc: =?UTF-8?q?Daniel=20Br=C3=A1t?= , Ahmad Fatoum Date: Fri, 19 May 2023 14:10:28 +0200 Message-Id: <20230519121028.2475832-1-a.fatoum@pengutronix.de> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230519_051036_022116_38BE89F1 X-CRM114-Status: GOOD ( 17.51 ) X-BeenThere: barebox@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "barebox" X-SA-Exim-Connect-IP: 2607:7c80:54:3::133 X-SA-Exim-Mail-From: barebox-bounces+lore=pengutronix.de@lists.infradead.org X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on metis.ext.pengutronix.de X-Spam-Level: X-Spam-Status: No, score=-4.8 required=4.0 tests=AWL,BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.2 Subject: [PATCH v3] barebox: Fix excessive loading of FIT images X-SA-Exim-Version: 4.2.1 (built Wed, 08 May 2019 21:11:16 +0000) X-SA-Exim-Scanned: Yes (on metis.ext.pengutronix.de) From: Christian Melki Barebox doesn't use the FIT image size from the header when loading FIT images. It bluntly assumes that the FIT image is equal to the file size. Which would be true if the FIT image is a file. But if it's situated on a raw device, then barebox proceeds to load the entire contents of that raw device, only to conclude that it only needed parts of it. Fix it. Cc: Daniel BrĂ¡t Signed-off-by: Christian Melki Signed-off-by: Ahmad Fatoum --- v3 was here: https://lore.barebox.org/barebox/20220729205441.9512-1-danek.brat@gmail.com/ v2 -> v3: - restrict change to bootm_open_fit - use cached data in struct image_data v1 -> v2: - use fdt32_to_cpu to read the totalsize from header --- common/bootm.c | 9 ++++++++- common/image-fit.c | 7 ++++--- include/image-fit.h | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/common/bootm.c b/common/bootm.c index fb1ed36a26dc..91a6e1688674 100644 --- a/common/bootm.c +++ b/common/bootm.c @@ -522,13 +522,20 @@ static int bootm_open_os_uimage(struct image_data *data) static int bootm_open_fit(struct image_data *data) { struct fit_handle *fit; + struct fdt_header *header; static const char *kernel_img = "kernel"; + size_t flen, hlen; int ret; if (!IS_ENABLED(CONFIG_FITIMAGE)) return 0; - fit = fit_open(data->os_file, data->verbose, data->verify); + header = (struct fdt_header *)data->os_header; + flen = bootm_get_os_size(data); + hlen = fdt32_to_cpu(header->totalsize); + + fit = fit_open(data->os_file, data->verbose, data->verify, + min(flen, hlen)); if (IS_ERR(fit)) { pr_err("Loading FIT image %s failed with: %pe\n", data->os_file, fit); return PTR_ERR(fit); diff --git a/common/image-fit.c b/common/image-fit.c index 3e6e7fbd6d12..9bea62bb34a0 100644 --- a/common/image-fit.c +++ b/common/image-fit.c @@ -827,6 +827,7 @@ struct fit_handle *fit_open_buf(const void *buf, size_t size, bool verbose, * @filename: The filename of the FIT image * @verbose: If true, be more verbose * @verify: The verify mode + * @max_size: maximum length to read from file * * This opens a FIT image found in @filename. The returned handle is used as * context for the other FIT functions. @@ -834,7 +835,7 @@ struct fit_handle *fit_open_buf(const void *buf, size_t size, bool verbose, * Return: A handle to a FIT image or a ERR_PTR */ struct fit_handle *fit_open(const char *filename, bool verbose, - enum bootm_verify verify) + enum bootm_verify verify, loff_t max_size) { struct fit_handle *handle; int ret; @@ -845,8 +846,8 @@ struct fit_handle *fit_open(const char *filename, bool verbose, handle->verify = verify; ret = read_file_2(filename, &handle->size, &handle->fit_alloc, - FILESIZE_MAX); - if (ret) { + max_size); + if (ret && ret != -EFBIG) { pr_err("unable to read %s: %s\n", filename, strerror(-ret)); return ERR_PTR(ret); } diff --git a/include/image-fit.h b/include/image-fit.h index f21545988e16..0b8e94bf4635 100644 --- a/include/image-fit.h +++ b/include/image-fit.h @@ -23,7 +23,7 @@ struct fit_handle { }; struct fit_handle *fit_open(const char *filename, bool verbose, - enum bootm_verify verify); + enum bootm_verify verify, loff_t max_size); struct fit_handle *fit_open_buf(const void *buf, size_t len, bool verbose, enum bootm_verify verify); void *fit_open_configuration(struct fit_handle *handle, const char *name); -- 2.39.2