Skip to contentSkip to conversation

boot: android: fix booting without a ramdisk

PATCH In review Michael Walle 3 participants 6 messages Jul 29, 2024 – Aug 6, 2024

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 boot: android: fix booting without a ramdisk

android_image_get_ramdisk() will return an error if there is no ramdisk. Using the android image without a ramdisk worked until commit 1ce8e10f3b4b ("image: Fix up ANDROID_BOOT_IMAGE ramdisk code") because the return code wasn't checked until then. Return -ENOENT in case there is no ramdisk and translate that into -ENOPKG in the calling code, which will then indicate "no ramdisk" to its caller (boot_get_ramdisk()).

This way, we can get rid of the "*rd_data = *rd_len = 0;" in the error path, too.

With this, I'm able to boot a linux kernel using fastboot again:

  fastboot --base 0x41000000 --header-version 2 --dtb /path/to/dtb \
  --cmdline "root=/dev/mmcblk0p1 rootwait" boot path/to/Image
---
 boot/image-android.c | 7 +++----
 boot/image-board.c   | 4 +++-
 include/image.h      | 2 +-
 3 files changed, 7 insertions(+), 6 deletions(-)
boot/image-android.c +3 -4
index 09c7a44e058..774565fd1fe 100644
@@ -393,10 +393,9 @@ int android_image_get_ramdisk(const void *hdr, const void *vendor_boot_img,
 	if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
 		return -EINVAL;
 
-	if (!img_data.ramdisk_size) {
-		*rd_data = *rd_len = 0;
-		return -1;
-	}
+	if (!img_data.ramdisk_size)
+		return -ENOENT;
+
 	if (img_data.header_version > 2) {
 		ramdisk_ptr = img_data.ramdisk_addr;
 		memcpy((void *)(ramdisk_ptr), (void *)img_data.vendor_ramdisk_ptr,
boot/image-board.c +3 -1
index f2124013046..eca1b1d2bff 100644
@@ -427,7 +427,9 @@ static int select_ramdisk(struct bootm_headers *images, const char *select, u8 a
 				unmap_sysmem(ptr);
 			}
 
-			if (ret)
+			if (ret == -ENOENT)
+				return -ENOPKG;
+			else if (ret)
 				return ret;
 			done = true;
 		}
include/image.h +1 -1
index dd4042d1bd9..2ab17075287 100644
@@ -1858,7 +1858,7 @@ int android_image_get_kernel(const void *hdr,
  * @vendor_boot_img : Pointer to vendor boot image header
  * @rd_data:	Pointer to a ulong variable, will hold ramdisk address
  * @rd_len:	Pointer to a ulong variable, will hold ramdisk length
- * Return: 0 if succeeded, -1 if ramdisk size is 0
+ * Return: 0 if OK, -ENOPKG if no ramdisk, -EINVAL if invalid image
  */
 int android_image_get_ramdisk(const void *hdr, const void *vendor_boot_img,
 			      ulong *rd_data, ulong *rd_len);
Signature · 1 line
2.39.2
Reviewed-by on v2 1/1
#1 lore raw

Hi Michael,

Thank you for the patch.

Quoted 11 lines · Michael Walle

android_image_get_ramdisk() will return an error if there is no ramdisk. Using the android image without a ramdisk worked until commit 1ce8e10f3b4b ("image: Fix up ANDROID_BOOT_IMAGE ramdisk code") because the return code wasn't checked until then. Return -ENOENT in case there is no ramdisk and translate that into -ENOPKG in the calling code, which will then indicate "no ramdisk" to its caller (boot_get_ramdisk()).

This way, we can get rid of the "*rd_data = *rd_len = 0;" in the error path, too.

Quoted 6 lines

With this, I'm able to boot a linux kernel using fastboot again:

  fastboot --base 0x41000000 --header-version 2 --dtb /path/to/dtb \
  --cmdline "root=/dev/mmcblk0p1 rootwait" boot path/to/Image
Quoted 54 lines
---
 boot/image-android.c | 7 +++----
 boot/image-board.c   | 4 +++-
 include/image.h      | 2 +-
 3 files changed, 7 insertions(+), 6 deletions(-)
boot/image-android.c +3 -4
index 09c7a44e058..774565fd1fe 100644
@@ -393,10 +393,9 @@ int android_image_get_ramdisk(const void *hdr, const void *vendor_boot_img,
 	if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
 		return -EINVAL;
 
-	if (!img_data.ramdisk_size) {
-		*rd_data = *rd_len = 0;
-		return -1;
-	}
+	if (!img_data.ramdisk_size)
+		return -ENOENT;
+
 	if (img_data.header_version > 2) {
 		ramdisk_ptr = img_data.ramdisk_addr;
 		memcpy((void *)(ramdisk_ptr), (void *)img_data.vendor_ramdisk_ptr,
boot/image-board.c +3 -1
index f2124013046..eca1b1d2bff 100644
@@ -427,7 +427,9 @@ static int select_ramdisk(struct bootm_headers *images, const char *select, u8 a
 				unmap_sysmem(ptr);
 			}
 
-			if (ret)
+			if (ret == -ENOENT)
+				return -ENOPKG;
+			else if (ret)
 				return ret;
 			done = true;
 		}
include/image.h +1 -1
index dd4042d1bd9..2ab17075287 100644
@@ -1858,7 +1858,7 @@ int android_image_get_kernel(const void *hdr,
  * @vendor_boot_img : Pointer to vendor boot image header
  * @rd_data:	Pointer to a ulong variable, will hold ramdisk address
  * @rd_len:	Pointer to a ulong variable, will hold ramdisk length
- * Return: 0 if succeeded, -1 if ramdisk size is 0
+ * Return: 0 if OK, -ENOPKG if no ramdisk, -EINVAL if invalid image
  */
 int android_image_get_ramdisk(const void *hdr, const void *vendor_boot_img,
 			      ulong *rd_data, ulong *rd_len);
Signature · 1 line
2.39.2
Reviewed-by
#2 lore raw
Quoted 18 lines · Mattijs Korpershoek

Hi Michael,

Thank you for the patch.

Quoted 11 lines · Michael Walle

android_image_get_ramdisk() will return an error if there is no ramdisk. Using the android image without a ramdisk worked until commit 1ce8e10f3b4b ("image: Fix up ANDROID_BOOT_IMAGE ramdisk code") because the return code wasn't checked until then. Return -ENOENT in case there is no ramdisk and translate that into -ENOPKG in the calling code, which will then indicate "no ramdisk" to its caller (boot_get_ramdisk()).

This way, we can get rid of the "*rd_data = *rd_len = 0;" in the error path, too.

Quoted 8 lines
Quoted 6 lines

With this, I'm able to boot a linux kernel using fastboot again:

  fastboot --base 0x41000000 --header-version 2 --dtb /path/to/dtb \
  --cmdline "root=/dev/mmcblk0p1 rootwait" boot path/to/Image

But see below

Quoted 33 lines
Quoted 33 lines
---
 boot/image-android.c | 7 +++----
 boot/image-board.c   | 4 +++-
 include/image.h      | 2 +-
 3 files changed, 7 insertions(+), 6 deletions(-)
boot/image-android.c +3 -4
index 09c7a44e058..774565fd1fe 100644
@@ -393,10 +393,9 @@ int android_image_get_ramdisk(const void *hdr, const void *vendor_boot_img,
      if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
              return -EINVAL;
 
-     if (!img_data.ramdisk_size) {
-             *rd_data = *rd_len = 0;
-             return -1;
-     }
+     if (!img_data.ramdisk_size)
+             return -ENOENT;
+
      if (img_data.header_version > 2) {
              ramdisk_ptr = img_data.ramdisk_addr;
              memcpy((void *)(ramdisk_ptr), (void *)img_data.vendor_ramdisk_ptr,
boot/image-board.c +0 -0
index f2124013046..eca1b1d2bff 100644
@@ -427,7 +427,9 @@ static int select_ramdisk(struct bootm_headers *images, const char *select, u8 a
                              unmap_sysmem(ptr);
                      }
 
Quoted 3 lines

boot/image-board.c static int select_ramdisk(struct bootm_headers *images, const char *select, u8 a · line 431

Quoted 3 lines
boot/image-board.c +2 -1
-                     if (ret)
+                     if (ret == -ENOENT)
+                             return -ENOPKG;

We normally use -ENOENT for this sort of thing.

Quoted 11 lines
Quoted 11 lines
boot/image-board.c +1 -0
+                     else if (ret)
                              return ret;
                      done = true;
              }
include/image.h +0 -0
index dd4042d1bd9..2ab17075287 100644
@@ -1858,7 +1858,7 @@ int android_image_get_kernel(const void *hdr,
  * @vendor_boot_img : Pointer to vendor boot image header
  * @rd_data: Pointer to a ulong variable, will hold ramdisk address
Quoted 8 lines
Quoted 8 lines
include/image.h +1 -2
  * @rd_len:  Pointer to a ulong variable, will hold ramdisk length
- * Return: 0 if succeeded, -1 if ramdisk size is 0
+ * Return: 0 if OK, -ENOPKG if no ramdisk, -EINVAL if invalid image
  */
 int android_image_get_ramdisk(const void *hdr, const void *vendor_boot_img,
                            ulong *rd_data, ulong *rd_len);
--

2.39.2

Regards,
Simon

#3 lore raw

Hi,

Quoted 5 lines
Quoted 3 lines

boot/image-board.c static int select_ramdisk(struct bootm_headers *images, const char *select, u8 a · line 431

Quoted 3 lines
boot/image-board.c +2 -1
-                     if (ret)
+                     if (ret == -ENOENT)
+                             return -ENOPKG;

We normally use -ENOENT for this sort of thing.

That's the way select_ramdisk() is documented. It was actually introduced by yourself in commit e4c928792e93 ("image: Split up boot_get_ramdisk()") :)

-michael

#4 lore raw

Hi Michael,

Quoted 7 lines · Michael Walle

Hi,

Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
boot/image-board.c +2 -1
-                     if (ret)
+                     if (ret == -ENOENT)
+                             return -ENOPKG;
Quoted 5 lines
Quoted 1 line

We normally use -ENOENT for this sort of thing.

That's the way select_ramdisk() is documented. It was actually introduced by yourself in commit e4c928792e93 ("image: Split up boot_get_ramdisk()") :)

Hmm, OK. Perhaps I was worried about conflicting with a file-not-found error in bootstd. Anyway, let's stick with ENOPKG.

Regards,
Simon

#5 lore raw
In reply to Michael Walle

Hi,

Quoted 9 lines · Michael Walle

android_image_get_ramdisk() will return an error if there is no ramdisk. Using the android image without a ramdisk worked until commit 1ce8e10f3b4b ("image: Fix up ANDROID_BOOT_IMAGE ramdisk code") because the return code wasn't checked until then. Return -ENOENT in case there is no ramdisk and translate that into -ENOPKG in the calling code, which will then indicate "no ramdisk" to its caller (boot_get_ramdisk()).

[...]

Thanks, Applied to https://source.denx.de/u-boot/custodians/u-boot-dfu (u-boot-dfu)

[1/1] boot: android: fix booting without a ramdisk

      https://source.denx.de/u-boot/custodians/u-boot-dfu/-/commit/2a75793bc55ec1ace0a66e96102beec3cb7ab64d

--
Mattijs