Skip to contentSkip to conversation

Android image support

PATCH Applied Sascha Hauer 2 participants 7 messages Jan 20 – Jan 23

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Android image support

Android images are particularly useful with fastboot. The fastboot host tool encapsulates boot images in its own rudimentary image format which we can't boot directly. This series replaces the existing ARM32/zImage only Android image handler with a generic handler which only passes the extracted Kernel/initrd image on to bootm.

Sascha

---
Sascha Hauer (2):
libfile: copy_fd: add size argument
bootm: add generic android image handler

 arch/arm/lib32/bootm.c       | 174 -------------------------------------------
 common/Makefile              |   1 +
 common/bootm-android-image.c | 154 ++++++++++++++++++++++++++++++++++++++
 common/firmware.c            |   2 +-
 include/libfile.h            |   2 +-
 lib/libfile.c                |  37 ++++++++-
 6 files changed, 190 insertions(+), 180 deletions(-)

---
base-commit: 0024921364eb4c8bc8089fdc198440b0d67a239f
change-id: 20260120-android-image-dca2d5bc8e40

Best regards,

Signature · 1 line
Sascha Hauer <[email protected]>
#1 lore raw

PATCH 1/2 libfile: copy_fd: add size argument

Add a size argument to copy_fd() to allow copying a specified number of bytes. While at it add some function documentation.

---
 common/firmware.c |  2 +-
 include/libfile.h |  2 +-
 lib/libfile.c     | 37 +++++++++++++++++++++++++++++++++----
 3 files changed, 35 insertions(+), 6 deletions(-)
common/firmware.c +1 -1
index 264fc9d66d..cdc55467cd 100644
@@ -292,7 +292,7 @@ int firmwaremgr_load_file(struct firmware_mgr *mgr, const char *firmware)
 		ret = uncompress_fd_to_fd(firmwarefd, devicefd,
 					  uncompress_err_stdout);
 	else
-		ret = copy_fd(firmwarefd, devicefd);
+		ret = copy_fd(firmwarefd, devicefd, 0);
 
 out:
 	free(dst);
include/libfile.h +1 -1
index dc292e422e..370f8b9725 100644
@@ -11,7 +11,7 @@ int pread_full(int fd, void *buf, size_t size, loff_t offset);
 int pwrite_full(int fd, const void *buf, size_t size, loff_t offset);
 int write_full(int fd, const void *buf, size_t size);
 int read_full(int fd, void *buf, size_t size);
-int copy_fd(int in, int out);
+int copy_fd(int in, int out, size_t size);
 
 ssize_t read_file_into_buf(const char *filename, void *buf, size_t size);
 
lib/libfile.c +33 -4
index 01189773b7..32b0c27eba 100644
@@ -124,25 +124,54 @@ int read_full(int fd, void *buf, size_t size)
 }
 EXPORT_SYMBOL(read_full);
 
-int copy_fd(int in, int out)
+/*
+ * copy_fd - copy one file descriptor to another
+ * @in: input file descriptor
+ * @out: output file descriptor
+ * @size: size to copy
+ *
+ * This copies at @size bytes from @in file descriptor to @out
+ * file descriptor. If @size is zero then all data is copied until
+ * EOF is reached.
+ *
+ * @return: 0 for success and negatibe error code otherwise. If @size
+ * is non zero and not all data could be read from @in then -ENODATA
+ * is returned.
+ */
+int copy_fd(int in, int out, size_t size)
 {
-	int bs = 4096, ret;
+	size_t copied = 0, bs = 4096;
 	void *buf = malloc(bs);
+	int ret;
 
 	if (!buf)
 		return -ENOMEM;
 
 	while (1) {
-		ret = read(in, buf, bs);
+		size_t now = min(bs, size - copied);
+
+		if (!now) {
+			ret = 0;
+			break;
+		}
+
+		ret = read(in, buf, now);
 		if (ret <= 0)
 			break;
 
+		now = ret;
+
 		ret = write_full(out, buf, ret);
 		if (ret < 0)
 			break;
+
+		copied += now;
 	}
 
-        free(buf);
+	free(buf);
+
+	if (size && copied < size)
+		ret = -ENODATA;
 
 	return ret;
 }
Signature · 1 line
2.47.3
#2 lore raw

PATCH 2/2 bootm: add generic android image handler

In reply to Sascha Hauer

An Android image handler is particularly useful for use with fastboot. The fastboot protocol supports a "boot" command which can be used to boot a kernel via fastboot. The fastboot host tool encapsulates the Kernel binary in a Android image format which is then transferred.

This patch brings us an image handler which extracts the Kernel binary and optionally an initrd and passes this on to bootm.

We previously already had an Android image handler, but this is very limited and replaced with this patch. The existing handler was only supported on ARM32 and only zImages. It hasn't seen any active changes for more than 10 years.

---
 arch/arm/lib32/bootm.c       | 174 -------------------------------------------
 common/Makefile              |   1 +
 common/bootm-android-image.c | 154 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 155 insertions(+), 174 deletions(-)
arch/arm/lib32/bootm.c +0 -174
index dca4fec020..2ae132d435 100644
@@ -567,180 +567,6 @@ static struct image_handler socfpga_xload_handler = {
 	.filetype = filetype_socfpga_xload,
 };
 
-#include <aimage.h>
-
-static int aimage_load_resource(int fd, struct resource *r, void* buf, int ps)
-{
-	int ret;
-	void *image = (void *)r->start;
-	unsigned to_read = ps - resource_size(r) % ps;
-
-	ret = read_full(fd, image, resource_size(r));
-	if (ret < 0)
-		return ret;
-
-	ret = read_full(fd, buf, to_read);
-	if (ret < 0)
-		printf("could not read dummy %u\n", to_read);
-
-	return ret;
-}
-
-static int do_bootm_aimage(struct image_data *data)
-{
-	struct resource *snd_stage_res;
-	int fd, ret;
-	struct android_header __header, *header;
-	void *buf;
-	int to_read;
-	struct android_header_comp *cmp;
-	unsigned long mem_free;
-	unsigned long mem_start, mem_size;
-
-	ret = sdram_start_and_size(&mem_start, &mem_size);
-	if (ret)
-		return ret;
-
-	fd = open(data->os_file, O_RDONLY);
-	if (fd < 0) {
-		perror("open");
-		return 1;
-	}
-
-	header = &__header;
-	ret = read(fd, header, sizeof(*header));
-	if (ret < sizeof(*header)) {
-		printf("could not read %s\n", data->os_file);
-		goto err_out;
-	}
-
-	printf("Android Image for '%s'\n", header->name);
-
-	/*
-	 * As on tftp we do not support lseek and we will just have to seek
-	 * for the size of a page - 1 max just buffer instead to read to dummy
-	 * data
-	 */
-	buf = xmalloc(header->page_size);
-
-	to_read = header->page_size - sizeof(*header);
-	ret = read_full(fd, buf, to_read);
-	if (ret < 0) {
-		printf("could not read dummy %d from %s\n", to_read, data->os_file);
-		goto err_out;
-	}
-
-	cmp = &header->kernel;
-	data->os_res = request_sdram_region("akernel", cmp->load_addr, cmp->size,
-					    MEMTYPE_LOADER_CODE, MEMATTRS_RWX);
-	if (!data->os_res) {
-		pr_err("Cannot request region 0x%08x - 0x%08x, using default load address\n",
-				cmp->load_addr, cmp->size);
-
-		data->os_address = mem_start + PAGE_ALIGN(cmp->size * 4);
-		data->os_res = request_sdram_region("akernel", data->os_address, cmp->size,
-						    MEMTYPE_LOADER_CODE, MEMATTRS_RWX);
-		if (!data->os_res) {
-			pr_err("Cannot request region 0x%08x - 0x%08x\n",
-					cmp->load_addr, cmp->size);
-			ret = -ENOMEM;
-			goto err_out;
-		}
-	}
-
-	ret = aimage_load_resource(fd, data->os_res, buf, header->page_size);
-	if (ret < 0) {
-		perror("could not read kernel");
-		goto err_out;
-	}
-
-	/*
-	 * fastboot always expect a ramdisk
-	 * in barebox we can be less restrictive
-	 */
-	cmp = &header->ramdisk;
-	if (cmp->size) {
-		data->initrd_res = request_sdram_region("ainitrd", cmp->load_addr, cmp->size,
-							MEMTYPE_LOADER_DATA, MEMATTRS_RW);
-		if (!data->initrd_res) {
-			ret = -ENOMEM;
-			goto err_out;
-		}
-
-		ret = aimage_load_resource(fd, data->initrd_res, buf, header->page_size);
-		if (ret < 0) {
-			perror("could not read initrd");
-			goto err_out;
-		}
-	}
-
-	if (!getenv("aimage_noverwrite_bootargs"))
-		linux_bootargs_overwrite(header->cmdline);
-
-	if (!getenv("aimage_noverwrite_tags"))
-		armlinux_set_bootparams((void *)(unsigned long)header->tags_addr);
-
-	cmp = &header->second_stage;
-	if (cmp->size) {
-		void (*second)(void);
-
-		snd_stage_res = request_sdram_region("asecond", cmp->load_addr, cmp->size,
-						     MEMTYPE_LOADER_CODE, MEMATTRS_RWX);
-		if (!snd_stage_res) {
-			ret = -ENOMEM;
-			goto err_out;
-		}
-
-		ret = aimage_load_resource(fd, snd_stage_res, buf, header->page_size);
-		if (ret < 0) {
-			perror("could not read initrd");
-			goto err_out;
-		}
-
-		second = (void*)snd_stage_res->start;
-		shutdown_barebox();
-
-		second();
-
-		restart_machine(0);
-	}
-
-	close(fd);
-
-	/*
-	 * Put devicetree right after initrd if present or after the kernel
-	 * if not.
-	 */
-	if (data->initrd_res)
-		mem_free = PAGE_ALIGN(data->initrd_res->end);
-	else
-		mem_free = PAGE_ALIGN(data->os_res->end + SZ_1M);
-
-	return __do_bootm_linux(data, mem_free, 0, NULL);
-
-err_out:
-	linux_bootargs_overwrite(NULL);
-	close(fd);
-
-	return ret;
-}
-
-static struct image_handler aimage_handler = {
-	.name = "ARM Android Image",
-	.bootm = do_bootm_aimage,
-	.filetype = filetype_aimage,
-};
-
-#ifdef CONFIG_BOOTM_AIMAGE
-BAREBOX_MAGICVAR(aimage_noverwrite_bootargs, "Disable overwrite of the bootargs with the one present in aimage");
-BAREBOX_MAGICVAR(aimage_noverwrite_tags, "Disable overwrite of the tags addr with the one present in aimage");
-#endif
-
-static struct binfmt_hook binfmt_aimage_hook = {
-	.type = filetype_aimage,
-	.exec = "bootm",
-};
-
 static struct binfmt_hook binfmt_arm_zimage_hook = {
 	.type = filetype_arm_zimage,
 	.exec = "bootm",
common/Makefile +1 -0
index 36dee5f7a9..54120d3d8a 100644
@@ -28,6 +28,7 @@ obj-$(CONFIG_BINFMT)		+= binfmt.o
 obj-$(CONFIG_BLOCK)		+= block.o
 obj-$(CONFIG_BLSPEC)		+= blspec.o
 obj-$(CONFIG_BOOTM)		+= bootm.o booti.o
+obj-$(CONFIG_BOOTM_AIMAGE)	+= bootm-android-image.o
 obj-$(CONFIG_CMD_LOADS)		+= s_record.o
 obj-$(CONFIG_MEMTEST)		+= memtest.o
 obj-$(CONFIG_COMMAND_SUPPORT)	+= command.o
common/bootm-android-image.c new file +154 -0

154 lines not on this page · raw message

Signature · 1 line
2.47.3
#3 lore raw

Re: [PATCH 2/2] bootm: add generic android image handler

Hello Sascha,

common/bootm-android-image.c line 129

Quoted 9 lines · Sascha Hauer
common/bootm-android-image.c new file +9 -0
+	if (kernel)
+		bootm_data.os_file = kernel;
+
+	if (initrd)
+		bootm_data.initrd_file = initrd;
+
+	close(fd);
+
+	ret = bootm_boot(&bootm_data);

Two quick comments:

I think this feature will not be usable when CONFIG_BOOTM_OFTREE_FALLBACK is disabled, which is a shame, because I think normal deployments should disable that option to avoid unexpected fallback behavior.

Here the fallback is expected though. Would it be possible to change this, so the barebox oftree is passed along explicitly to avoid falling into the fallback?

On a related note, this will probably trigger a warning on every boot if the barebox device tree has a state node. Maybe we could mark dupes of the barebox device tree as such (maybe set device_node::dev to a special value?!), so we can skip the warning? This also bothered me with EFI boot, which always passes the barebox DT along and leaves it to grub or whatever to decide whether it wants to use it or if it wants to use another DT it finds itself.

Thanks!
Ahmad

Quoted 26 lines
common/bootm-android-image.c new file +25 -0
+
+err_close:
+	close(fd);
+
+	if (kernel)
+		unlink(kernel);
+	if (initrd)
+		unlink(initrd);
+err:
+	free(hdr);
+
+	return ret;
+}
+
+static struct image_handler aimage_bootm_handler = {
+	.name = "Android boot image",
+	.bootm = do_bootm_aimage,
+	.filetype = filetype_aimage,
+};
+
+static int bootm_aimage_register(void)
+{
+	return register_image_handler(&aimage_bootm_handler);
+}
+late_initcall(bootm_aimage_register);
Signature · 4 lines
Pengutronix e.K.                  |                             |
Steuerwalder Str. 21              | http://www.pengutronix.de/  |
31137 Hildesheim, Germany         | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686  | Fax:   +49-5121-206917-5555 |
#4 lore raw

Re: [PATCH 2/2] bootm: add generic android image handler

Quoted 15 lines · Ahmad Fatoum

Hello Sascha,

common/bootm-android-image.c line 129

Quoted 9 lines · Sascha Hauer
common/bootm-android-image.c new file +9 -0
+	if (kernel)
+		bootm_data.os_file = kernel;
+
+	if (initrd)
+		bootm_data.initrd_file = initrd;
+
+	close(fd);
+
+	ret = bootm_boot(&bootm_data);

Two quick comments:

Quoted 8 lines

I think this feature will not be usable when CONFIG_BOOTM_OFTREE_FALLBACK is disabled, which is a shame, because I think normal deployments should disable that option to avoid unexpected fallback behavior.

Here the fallback is expected though. Would it be possible to change this, so the barebox oftree is passed along explicitly to avoid falling into the fallback?

When a user chooses to disable CONFIG_BOOTM_OFTREE_FALLBACK to avoid using the internal device tree as a fallback then this decision should be universally honoured. Explicitly inserting the internal device tree in special cases contradicts this.

Note that I have used this feature with a FIT image which carries its own device tree, so I wasn't affected.

We currently only support version 1 of the Android image format. Since version 2 the image can also embed a device tree which can be passed with the --dtb option to the fastboot command, so the solution to address your concerns should be adding support for version 2.

I would consider this a future enhancement though.

Quoted 5 lines

On a related note, this will probably trigger a warning on every boot if the barebox device tree has a state node. Maybe we could mark dupes of the barebox device tree as such (maybe set device_node::dev to a special value?!), so we can skip the warning?

You mean this warning?

dev_warn(&state->dev, "Warning: Kernel devicetree contains state node, replacing it\n");

We could mark it as duplicate as you suggested. Another possibility would be to delete state nodes while duplicating the internal device tree.

Sascha

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#5 lore raw

Re: [PATCH 2/2] bootm: add generic android image handler

Hi Sascha,

Quoted 33 lines · Sascha Hauer
Quoted 15 lines · Ahmad Fatoum

Hello Sascha,

common/bootm-android-image.c line 129

Quoted 9 lines · Sascha Hauer
common/bootm-android-image.c new file +9 -0
+	if (kernel)
+		bootm_data.os_file = kernel;
+
+	if (initrd)
+		bootm_data.initrd_file = initrd;
+
+	close(fd);
+
+	ret = bootm_boot(&bootm_data);

Two quick comments:

Quoted 8 lines

I think this feature will not be usable when CONFIG_BOOTM_OFTREE_FALLBACK is disabled, which is a shame, because I think normal deployments should disable that option to avoid unexpected fallback behavior.

Here the fallback is expected though. Would it be possible to change this, so the barebox oftree is passed along explicitly to avoid falling into the fallback?

When a user chooses to disable CONFIG_BOOTM_OFTREE_FALLBACK to avoid using the internal device tree as a fallback then this decision should be universally honoured. Explicitly inserting the internal device tree in special cases contradicts this.

Note that I have used this feature with a FIT image which carries its own device tree, so I wasn't affected.

Quoted 6 lines

We currently only support version 1 of the Android image format. Since version 2 the image can also embed a device tree which can be passed with the --dtb option to the fastboot command, so the solution to address your concerns should be adding support for version 2.

I would consider this a future enhancement though.

Ah, didn't consider that you can just boot FIT images this way. Then I also think it's ok keeping it as-is.

Quoted 11 lines
Quoted 4 lines

On a related note, this will probably trigger a warning on every boot if the barebox device tree has a state node. Maybe we could mark dupes of the barebox device tree as such (maybe set device_node::dev to a special value?!), so we can skip the warning?

You mean this warning?

dev_warn(&state->dev, "Warning: Kernel devicetree contains state node, replacing it\n");

We could mark it as duplicate as you suggested. Another possibility would be to delete state nodes while duplicating the internal device tree.

Oh, I like the second suggestion, thanks.

Cheers,
Ahmad

Quoted 4 lines

Sascha

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
author Applied
#6 lore raw
In reply to Sascha Hauer
Quoted 9 lines · Sascha Hauer

Android images are particularly useful with fastboot. The fastboot host tool encapsulates boot images in its own rudimentary image format which we can't boot directly. This series replaces the existing ARM32/zImage only Android image handler with a generic handler which only passes the extracted Kernel/initrd image on to bootm.

Sascha

[...]

Applied, thanks!

[1/2] libfile: copy_fd: add size argument
https://git.pengutronix.de/cgit/barebox/commit/?id=ecf516d2de4c (link may not be stable)
[2/2] bootm: add generic android image handler

      https://git.pengutronix.de/cgit/barebox/commit/?id=5747df949877 (link may not be stable)

Best regards,

Signature · 1 line
Sascha Hauer <[email protected]>