mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH v2 11/13] bootm: factor out file detection into helper
Date: Mon,  9 Feb 2026 19:10:50 +0100	[thread overview]
Message-ID: <20260209181138.2023065-12-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20260209181138.2023065-1-a.fatoum@pengutronix.de>

Having this function callable on its own will come in handy once we add
boot.image override support as that would need to recompute the file
type.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/bootm.c  | 39 ++++++++++++++++++++++++++++++---------
 include/bootm.h |  2 ++
 2 files changed, 32 insertions(+), 9 deletions(-)

diff --git a/common/bootm.c b/common/bootm.c
index 3c3523644b57..d43079bb81da 100644
--- a/common/bootm.c
+++ b/common/bootm.c
@@ -542,6 +542,33 @@ static int bootm_image_name_and_part(const char *name, char **filename, char **p
 	return 0;
 }
 
+/**
+ * file_read_and_detect_boot_image_type - read file header and detect image type
+ * @os_file: path to the boot image file
+ * @os_header: returns a PAGE_SIZE buffer with the file header (caller frees)
+ *
+ * Return: detected filetype (enum filetype) on success, negative error code
+ *         on failure. On error, *os_header is set to NULL.
+ */
+int file_read_and_detect_boot_image_type(const char *os_file, void **os_header)
+{
+	size_t size;
+	int ret;
+
+	ret = read_file_2(os_file, &size, os_header, PAGE_SIZE);
+	if (ret < 0 && ret != -EFBIG) {
+		pr_err("could not open %s: %pe\n", os_file, ERR_PTR(ret));
+		return ret;
+	}
+	if (size < PAGE_SIZE) {
+		free(*os_header);
+		*os_header = NULL;
+		return -ENODATA;
+	}
+
+	return file_detect_boot_image_type(*os_header, PAGE_SIZE);
+}
+
 /*
  * bootm_boot - Boot an application image described by bootm_data
  */
@@ -550,7 +577,6 @@ int bootm_boot(struct bootm_data *bootm_data)
 	struct image_data *data;
 	struct image_handler *handler;
 	int ret;
-	size_t size;
 	const char *image_type_str;
 
 	if (!bootm_data->os_file) {
@@ -574,15 +600,10 @@ int bootm_boot(struct bootm_data *bootm_data)
 	data->os_entry = bootm_data->os_entry;
 	data->efi_boot = bootm_data->efi_boot;
 
-	ret = read_file_2(data->os_file, &size, &data->os_header, PAGE_SIZE);
-	if (ret < 0 && ret != -EFBIG) {
-		pr_err("could not open %s: %pe\n", data->os_file, ERR_PTR(ret));
+	ret = file_read_and_detect_boot_image_type(data->os_file, &data->os_header);
+	if (ret < 0)
 		goto err_out;
-	}
-	if (size < PAGE_SIZE)
-		goto err_out;
-
-	data->image_type = file_detect_boot_image_type(data->os_header, PAGE_SIZE);
+	data->image_type = ret;
 
 	if (!data->force && data->image_type == filetype_unknown) {
 		pr_err("Unknown OS filetype (try -f)\n");
diff --git a/include/bootm.h b/include/bootm.h
index ef979807505d..21feb1ca98ae 100644
--- a/include/bootm.h
+++ b/include/bootm.h
@@ -186,4 +186,6 @@ bool bootm_efi_check_image(struct image_handler *handler,
 			   struct image_data *data,
 			   enum filetype detected_filetype);
 
+int file_read_and_detect_boot_image_type(const char *os_file, void **os_header);
+
 #endif /* __BOOTM_H */
-- 
2.47.3




  parent reply	other threads:[~2026-02-09 18:17 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-09 18:10 [PATCH v2 00/13] bootm: prepare loadable abstraction rework Ahmad Fatoum
2026-02-09 18:10 ` [PATCH v2 01/13] FIT: implement fit_count_images Ahmad Fatoum
2026-02-09 18:10 ` [PATCH v2 02/13] FIT: add image index argument to fit_open_image Ahmad Fatoum
2026-02-09 18:10 ` [PATCH v2 03/13] resource: implement gap-aware lookup_region Ahmad Fatoum
2026-02-09 18:10 ` [PATCH v2 04/13] bootm: give bootm_load_ functions an end address Ahmad Fatoum
2026-02-09 18:10 ` [PATCH v2 05/13] bootm: store separate image_type and kernel_type Ahmad Fatoum
2026-02-09 18:10 ` [PATCH v2 06/13] bootm: fit: move length calculation into fit_open Ahmad Fatoum
2026-02-09 18:10 ` [PATCH v2 07/13] libfile: factor out zero-page resistant read_file as __read_full_anywhere Ahmad Fatoum
2026-02-09 18:10 ` [PATCH v2 08/13] resource: implement resize_region Ahmad Fatoum
2026-02-09 18:10 ` [PATCH v2 09/13] bootm: rename image_data::os/initrd with _uimage suffix Ahmad Fatoum
2026-02-09 18:10 ` [PATCH v2 10/13] uimage: record original file name in uimage_handle Ahmad Fatoum
2026-02-09 18:10 ` Ahmad Fatoum [this message]
2026-02-09 18:10 ` [PATCH v2 12/13] efi: payload: bootm: add dry run support Ahmad Fatoum
2026-02-09 18:10 ` [PATCH v2 13/13] efi: initrd: make efi_initrd_register initrd pointer param const Ahmad Fatoum
2026-02-13 14:07 ` [PATCH v2 00/13] bootm: prepare loadable abstraction rework Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260209181138.2023065-12-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox