Skip to contentSkip to conversation

fastboot: block: Add GPT/MBR and device selection

PATCH In review Balaji Selvanathan 4 participants 18 messages Nov 15 – Nov 19

lore
Share & tools
lore
#0 lore raw

v3 cover fastboot: block: Add GPT/MBR and device selection

This series adds GPT and MBR partition table flashing to the fastboot block backend and lets the host select which device on the configured interface to use. For example, with FASTBOOT_FLASH_BLOCK:

  fastboot flash gpt gpt.img        # GPT on the default device
  fastboot flash 1#gpt gpt.img      # GPT on device 1
  fastboot flash 1#mbr mbr.img      # MBR on device 1
  fastboot flash 1#boot boot.img    # partition "boot" on device 1
  fastboot erase 1:3                # partition 3 on device 1

A name without a device prefix, such as "fastboot flash boot boot.img", works as before with CONFIG_FASTBOOT_FLASH_BLOCK_DEVICE_ID.

---
Changes in v3:
- Rebase on master, which now has SPL fastboot support
- Split the blk_get_dev() NULL check fix into its own patch
- Use '#' to select a partition by name, as the rest of the disk layer
does, and reuse part_get_info_by_dev_and_name_or_num() instead of a
local parser. This also fixes "foo:boot" being taken as device 0
- Pass a block descriptor to the GPT/MBR helpers, so fb_mmc.c keeps
its DEV_TYPE_UNKNOWN check, and share the validate/write code between
them. Move them to fb_block.c
- Check the interface name and the device prefix in the block caller,
and report an invalid prefix instead of ignoring it
- Add a sandbox test for the partition table target lookup
- Doc: use sentence case headings, cut the duplicated GPT/MBR text down
to one example and mention erase and partition-size
- Link to v2: https://lore.kernel.org/u-boot/[email protected]/

Changes in v2:
- Separate adding device selection syntax and GPT/MBR flashing support
into separate patches.
- Introduce common helper functions such as
fastboot_flash_gpt_partition_table and
fastboot_flash_mbr_partition_table functions that can be used both
in fb_mmc.c and fb_block.c for GPT and MBR flashing.
- Link to v1: https://lore.kernel.org/u-boot/[email protected]/

---
Balaji Selvanathan (6):
fastboot: block: Fix NULL check on blk_get_dev() result
fastboot: block: Add device selection syntax
fastboot: Add GPT/MBR partition table flashing helper functions
fastboot: block: Add GPT/MBR partition table flashing support
test: fastboot: Add test for block partition table targets
doc: fastboot: Document block device selection syntax

 doc/android/fastboot.rst    |  32 +++++++++
 drivers/fastboot/Kconfig    |   4 +-
 drivers/fastboot/fb_block.c | 158 +++++++++++++++++++++++++++++++++++++++++---
 drivers/fastboot/fb_mmc.c   |  45 ++-----------
 include/fb_block.h          |  52 +++++++++++++++
 test/dm/fastboot.c          |  61 +++++++++++++++++
 6 files changed, 301 insertions(+), 51 deletions(-)

---
base-commit: 6487c50b8a40f2668ab98a892b9300ad5c181f38
change-id: 20260427-fb_gpt-86d7d6595dad

Best regards,

Signature · 1 line
Balaji Selvanathan <[email protected]>
#1 lore raw

PATCH v3 1/6 fastboot: block: Fix NULL check on blk_get_dev() result

fastboot_block_get_part_info() stores the result of blk_get_dev() in *dev_desc but then tests dev_desc, which is the caller's pointer and never NULL. If the configured device does not exist, the error is missed and part_get_info_by_name() is called with a NULL descriptor.

Test *dev_desc instead.

Fixes 4bf92b541af6 ("fastboot: blk: introduce fastboot block flashing support")

---
Changes in v3:
- New patch, split out of the device selection patch

---
 drivers/fastboot/fb_block.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/fastboot/fb_block.c +1 -1
index 68593244ab9..16b94b8a0b2 100644
@@ -147,7 +147,7 @@ int fastboot_block_get_part_info(const char *part_name,
 	}
 
 	*dev_desc = blk_get_dev(interface, device);
-	if (!dev_desc) {
+	if (!*dev_desc) {
 		fastboot_fail("no such device", response);
 		return -ENODEV;
 	}
Signature · 1 line
2.34.1
Reviewed-by on v3 1/6
#2 lore raw

Re: [PATCH v3 1/6] fastboot: block: Fix NULL check on blk_get_dev() result

Quoted 8 lines · Balaji Selvanathan

fastboot_block_get_part_info() stores the result of blk_get_dev() in *dev_desc but then tests dev_desc, which is the caller's pointer and never NULL. If the configured device does not exist, the error is missed and part_get_info_by_name() is called with a NULL descriptor.

Test *dev_desc instead.

[...]

Signature · 1 line
Guillaume La Roque <[email protected]>
#3 lore raw

PATCH v3 2/6 fastboot: block: Add device selection syntax

In reply to Balaji Selvanathan

The block backend can only look up partitions by name on the device set by CONFIG_FASTBOOT_FLASH_BLOCK_DEVICE_ID, so a board with more than one device on the configured interface cannot flash or erase partitions on the others.

Do the same as the MMC backend: look up the name on the default device first, and if that fails, hand it to part_get_info_by_dev_and_name_or_num(). This accepts "<dev>#<partname>" to select a partition by name and "<dev>:<partnum>" to select it by number, the same syntax used elsewhere in the disk layer. Since fastboot_block_get_part_info() is shared by flash, erase and getvar, all of them accept the new forms.

Example usage:

  fastboot flash 1#boot boot.img  # partition "boot" on device 1
  fastboot erase 1:3              # partition 3 on device 1
  fastboot flash boot boot.img    # partition "boot" on the default device

---
Changes in v3:
- Use '#' for name lookup to match the disk layer, instead of ':'
- Use part_get_info_by_dev_and_name_or_num() instead of a local parser, as fb_mmc.c does
- Move the NULL check fix to a separate patch
- Mention erase and getvar in the commit message

Changes in v2:
- Newly created in v2
- Has only device selection syntax related changes

---
 drivers/fastboot/fb_block.c | 12 ++++++++----
 include/fb_block.h          |  4 ++++
 2 files changed, 12 insertions(+), 4 deletions(-)
drivers/fastboot/fb_block.c +8 -4
index 16b94b8a0b2..fb0face0429 100644
@@ -146,13 +146,17 @@ int fastboot_block_get_part_info(const char *part_name,
 		return -EINVAL;
 	}
 
+	/* First try a partition name on the default device */
 	*dev_desc = blk_get_dev(interface, device);
-	if (!*dev_desc) {
-		fastboot_fail("no such device", response);
-		return -ENODEV;
+	if (*dev_desc) {
+		ret = part_get_info_by_name(*dev_desc, part_name, part_info);
+		if (ret >= 0)
+			return ret;
 	}
 
-	ret = part_get_info_by_name(*dev_desc, part_name, part_info);
+	/* Then try <dev>#<partname> or <dev>:<partnum> */
+	ret = part_get_info_by_dev_and_name_or_num(interface, part_name,
+						   dev_desc, part_info, true);
 	if (ret < 0)
 		fastboot_fail("failed to get partition info", response);
 
include/fb_block.h +4 -0
index 189c708e2f0..f60cd3d9635 100644
@@ -12,6 +12,10 @@ struct disk_partition;
 /**
  * fastboot_block_get_part_info() - Lookup block partition by name
  *
+ * The partition is first looked up by name on the default device. If that
+ * fails, @part_name is parsed as "<dev>#<partname>" or "<dev>:<partnum>",
+ * as done by part_get_info_by_dev_and_name_or_num().
+ *
  * @part_name: Named partition to lookup
  * @dev_desc: Pointer to returned blk_desc pointer
  * @part_info: Pointer to returned struct disk_partition
Signature · 1 line
2.34.1
Reviewed-by on v3 2/6
#4 lore raw

Re: [PATCH v3 2/6] fastboot: block: Add device selection syntax

Quoted 13 lines · Balaji Selvanathan

The block backend can only look up partitions by name on the device set by CONFIG_FASTBOOT_FLASH_BLOCK_DEVICE_ID, so a board with more than one device on the configured interface cannot flash or erase partitions on the others.

Do the same as the MMC backend: look up the name on the default device first, and if that fails, hand it to part_get_info_by_dev_and_name_or_num(). This accepts "<dev>#<partname>" to select a partition by name and "<dev>:<partnum>" to select it by number, the same syntax used elsewhere in the disk layer. Since fastboot_block_get_part_info() is shared by flash, erase and getvar, all of them accept the new forms.

Quoted 1 line

[...]

Signature · 1 line
Guillaume La Roque <[email protected]>
#5 lore raw

Re: [PATCH v3 2/6] fastboot: block: Add device selection syntax

In reply to Balaji Selvanathan
Quoted 48 lines

The block backend can only look up partitions by name on the device set by CONFIG_FASTBOOT_FLASH_BLOCK_DEVICE_ID, so a board with more than one device on the configured interface cannot flash or erase partitions on the others.

Do the same as the MMC backend: look up the name on the default device first, and if that fails, hand it to part_get_info_by_dev_and_name_or_num(). This accepts "<dev>#<partname>" to select a partition by name and "<dev>:<partnum>" to select it by number, the same syntax used elsewhere in the disk layer. Since fastboot_block_get_part_info() is shared by flash, erase and getvar, all of them accept the new forms.

Example usage:

  fastboot flash 1#boot boot.img  # partition "boot" on device 1
  fastboot erase 1:3              # partition 3 on device 1
  fastboot flash boot boot.img    # partition "boot" on the default device

---
Changes in v3:
- Use '#' for name lookup to match the disk layer, instead of ':'
- Use part_get_info_by_dev_and_name_or_num() instead of a local parser, as fb_mmc.c does
- Move the NULL check fix to a separate patch
- Mention erase and getvar in the commit message

Changes in v2:
- Newly created in v2
- Has only device selection syntax related changes

---
 drivers/fastboot/fb_block.c | 12 ++++++++----
 include/fb_block.h          |  4 ++++
 2 files changed, 12 insertions(+), 4 deletions(-)
drivers/fastboot/fb_block.c +1 -3
index 16b94b8a0b2..fb0face0429 100644
@@ -146,13 +146,17 @@ int fastboot_block_get_part_info(const char *part_name,
 		return -EINVAL;
 	}
 
+	/* First try a partition name on the default device */
 	*dev_desc = blk_get_dev(interface, device);
-	if (!*dev_desc) {
-		fastboot_fail("no such device", response);
-		return -ENODEV;

drivers/fastboot/fb_block.c int fastboot_block_get_part_info(const char *part_name, · line 159

Quoted 10 lines
drivers/fastboot/fb_block.c +7 -1
+	if (*dev_desc) {
+		ret = part_get_info_by_name(*dev_desc, part_name, part_info);
+		if (ret >= 0)
+			return ret;
 	}
 
-	ret = part_get_info_by_name(*dev_desc, part_name, part_info);
+	/* Then try <dev>#<partname> or <dev>:<partnum> */
+	ret = part_get_info_by_dev_and_name_or_num(interface, part_name,
+						   dev_desc, part_info, true);

I have been using this patch for some work I am doing and it is quite noisy. part_get_info_by_dev_and_name_or_num() prints for every partition:

** Bad device specification scsi foo_a **
** Bad device specification scsi foo_a **
Couldn't find partition scsi foo_a

even though everything works fine. Should we only be calling this if e.g. the name contains '#' or ':' to avoid the irrelevant error messages?

And second point on the same line of code. Passing true for allow_whole_dev means that "<dev>:0" selects the whole device, not just the first partition. None of the commit message, documentation (patch 6), or code comments mention this. I assume this was intentional so should just be documented.

Signature · 1 line
David Lechner <[email protected]>
#6 lore raw

PATCH v3 3/6 fastboot: Add GPT/MBR partition table flashing helper functions

In reply to Balaji Selvanathan

Move the GPT and MBR flashing code out of fastboot_mmc_flash_write() into fastboot_block_flash_gpt() and fastboot_block_flash_mbr() in fb_block.c, so that the block backend can use it too. fb_block.c is already built for both the MMC and block backends.

Both helpers take a block descriptor and share one function for validating the image, writing it and rescanning the partitions. Only the validate and write callbacks differ.

Convert fastboot_mmc_flash_write() to the new helpers.

---
Changes in v3:
- Pass a block descriptor to the helpers rather than an interface name and device number. fb_mmc.c keeps using fastboot_mmc_get_dev(), so the DEV_TYPE_UNKNOWN check is no longer lost
- Share one validate/write/rescan function between GPT and MBR
- Move the helpers from fb_common.c to fb_block.c, which already includes <blk.h> and <part.h>, and rename them to match
- Drop the interface check from the helpers
- Drop the __maybe_unused on fastboot_mmc_get_dev(), no longer needed

Changes in v2:
- Newly created in v2
- Adds fastboot_flash_gpt_partition_table() and fastboot_flash_mbr_partition_table() helper functions
- Update fb_mmc.c to use these functions

---
 drivers/fastboot/fb_block.c | 64 +++++++++++++++++++++++++++++++++++++++++++++
 drivers/fastboot/fb_mmc.c   | 45 +++++--------------------------
 include/fb_block.h          | 26 ++++++++++++++++++
 3 files changed, 96 insertions(+), 39 deletions(-)
drivers/fastboot/fb_block.c +64 -0
index fb0face0429..cf03c600772 100644
@@ -315,6 +315,70 @@ void fastboot_block_write_sparse_image(struct blk_desc *dev_desc, struct disk_pa
 		fastboot_okay(NULL, response);
 }
 
+#if CONFIG_IS_ENABLED(EFI_PARTITION) || CONFIG_IS_ENABLED(DOS_PARTITION)
+/**
+ * fb_block_flash_part_table() - Validate and write a partition table
+ *
+ * @dev_desc: Block device to write to
+ * @type: Partition table type, used in messages
+ * @validate: Checks that @buffer holds a valid partition table
+ * @write: Writes the partition table in @buffer to @dev_desc
+ * @buffer: Downloaded partition table image
+ * @response: Pointer to fastboot response buffer
+ */
+static void fb_block_flash_part_table(struct blk_desc *dev_desc,
+				      const char *type,
+				      int (*validate)(struct blk_desc *, void *),
+				      int (*write)(struct blk_desc *, void *),
+				      void *buffer, char *response)
+{
+	printf("%s: updating %s on device %d\n", __func__, type,
+	       dev_desc->devnum);
+
+	if (validate(dev_desc, buffer)) {
+		printf("%s: invalid %s - refusing to write to flash\n",
+		       __func__, type);
+		fastboot_response("FAIL", response, "invalid %s partition",
+				  type);
+		return;
+	}
+
+	if (write(dev_desc, buffer)) {
+		printf("%s: writing %s failed\n", __func__, type);
+		fastboot_response("FAIL", response, "writing %s failed", type);
+		return;
+	}
+
+	part_init(dev_desc);
+	printf("........ success\n");
+	fastboot_okay(NULL, response);
+}
+#endif
+
+#if CONFIG_IS_ENABLED(EFI_PARTITION)
+void fastboot_block_flash_gpt(struct blk_desc *dev_desc, void *buffer,
+			      char *response)
+{
+	fb_block_flash_part_table(dev_desc, "GPT", is_valid_gpt_buf,
+				  write_mbr_and_gpt_partitions, buffer,
+				  response);
+}
+#endif
+
+#if CONFIG_IS_ENABLED(DOS_PARTITION)
+static int fb_block_valid_mbr(struct blk_desc *dev_desc, void *buffer)
+{
+	return is_valid_dos_buf(buffer);
+}
+
+void fastboot_block_flash_mbr(struct blk_desc *dev_desc, void *buffer,
+			      char *response)
+{
+	fb_block_flash_part_table(dev_desc, "MBR", fb_block_valid_mbr,
+				  write_mbr_sector, buffer, response);
+}
+#endif
+
 void fastboot_block_flash_write(const char *part_name, void *download_buffer,
 				u32 download_bytes, char *response)
 {
drivers/fastboot/fb_mmc.c +6 -39

57 lines not on this page · raw message

include/fb_block.h +26 -0
index f60cd3d9635..bee069a1dff 100644
@@ -95,6 +95,32 @@ void fastboot_block_write_raw_image(struct blk_desc *dev_desc,
 void fastboot_block_write_sparse_image(struct blk_desc *dev_desc, struct disk_partition *info,
 				       const char *part_name, void *buffer, char *response);
 
+/**
+ * fastboot_block_flash_gpt() - Write a GPT to a block device
+ *
+ * Checks the downloaded image, writes the protective MBR, the primary GPT
+ * and the backup GPT, then rescans the partitions on @dev_desc.
+ *
+ * @dev_desc: Block device to write to
+ * @buffer: Downloaded GPT image
+ * @response: Pointer to fastboot response buffer
+ */
+void fastboot_block_flash_gpt(struct blk_desc *dev_desc, void *buffer,
+			      char *response);
+
+/**
+ * fastboot_block_flash_mbr() - Write an MBR to a block device
+ *
+ * Checks the downloaded image, writes the MBR, then rescans the partitions
+ * on @dev_desc.
+ *
+ * @dev_desc: Block device to write to
+ * @buffer: Downloaded MBR image
+ * @response: Pointer to fastboot response buffer
+ */
+void fastboot_block_flash_mbr(struct blk_desc *dev_desc, void *buffer,
+			      char *response);
+
 /**
  * fastboot_block_flash_write() - Write image to block device for fastboot
  *
Signature · 1 line
2.34.1
Reviewed-by on v3 3/6
#7 lore raw

Re: [PATCH v3 3/6] fastboot: Add GPT/MBR partition table flashing helper functions

Quoted 10 lines · Balaji Selvanathan

Move the GPT and MBR flashing code out of fastboot_mmc_flash_write() into fastboot_block_flash_gpt() and fastboot_block_flash_mbr() in fb_block.c, so that the block backend can use it too. fb_block.c is already built for both the MMC and block backends.

Both helpers take a block descriptor and share one function for validating the image, writing it and rescanning the partitions. Only the validate and write callbacks differ.

[...]

Signature · 1 line
Guillaume La Roque <[email protected]>
#8 lore raw

Re: [PATCH v3 3/6] fastboot: Add GPT/MBR partition table flashing helper functions

In reply to Balaji Selvanathan
Quoted 65 lines

Move the GPT and MBR flashing code out of fastboot_mmc_flash_write() into fastboot_block_flash_gpt() and fastboot_block_flash_mbr() in fb_block.c, so that the block backend can use it too. fb_block.c is already built for both the MMC and block backends.

Both helpers take a block descriptor and share one function for validating the image, writing it and rescanning the partitions. Only the validate and write callbacks differ.

Convert fastboot_mmc_flash_write() to the new helpers.

---
Changes in v3:
- Pass a block descriptor to the helpers rather than an interface name and device number. fb_mmc.c keeps using fastboot_mmc_get_dev(), so the DEV_TYPE_UNKNOWN check is no longer lost
- Share one validate/write/rescan function between GPT and MBR
- Move the helpers from fb_common.c to fb_block.c, which already includes <blk.h> and <part.h>, and rename them to match
- Drop the interface check from the helpers
- Drop the __maybe_unused on fastboot_mmc_get_dev(), no longer needed

Changes in v2:
- Newly created in v2
- Adds fastboot_flash_gpt_partition_table() and fastboot_flash_mbr_partition_table() helper functions
- Update fb_mmc.c to use these functions

---
 drivers/fastboot/fb_block.c | 64 +++++++++++++++++++++++++++++++++++++++++++++
 drivers/fastboot/fb_mmc.c   | 45 +++++--------------------------
 include/fb_block.h          | 26 ++++++++++++++++++
 3 files changed, 96 insertions(+), 39 deletions(-)
drivers/fastboot/fb_block.c +23 -0
index fb0face0429..cf03c600772 100644
@@ -315,6 +315,70 @@ void fastboot_block_write_sparse_image(struct blk_desc *dev_desc, struct disk_pa
 		fastboot_okay(NULL, response);
 }
 
+#if CONFIG_IS_ENABLED(EFI_PARTITION) || CONFIG_IS_ENABLED(DOS_PARTITION)
+/**
+ * fb_block_flash_part_table() - Validate and write a partition table
+ *
+ * @dev_desc: Block device to write to
+ * @type: Partition table type, used in messages
+ * @validate: Checks that @buffer holds a valid partition table
+ * @write: Writes the partition table in @buffer to @dev_desc
+ * @buffer: Downloaded partition table image
+ * @response: Pointer to fastboot response buffer
+ */
+static void fb_block_flash_part_table(struct blk_desc *dev_desc,
+				      const char *type,
+				      int (*validate)(struct blk_desc *, void *),
+				      int (*write)(struct blk_desc *, void *),
+				      void *buffer, char *response)
+{
+	printf("%s: updating %s on device %d\n", __func__, type,
+	       dev_desc->devnum);
+
+	if (validate(dev_desc, buffer)) {
+		printf("%s: invalid %s - refusing to write to flash\n",
+		       __func__, type);

drivers/fastboot/fb_block.c void fastboot_block_write_sparse_image(struct blk_desc *dev_desc, struct disk_pa · line 350

Quoted 10 lines
drivers/fastboot/fb_block.c +10 -0
+		fastboot_response("FAIL", response, "invalid %s partition",
+				  type);
+		return;
+	}
+
+	if (write(dev_desc, buffer)) {
+		printf("%s: writing %s failed\n", __func__, type);
+		fastboot_response("FAIL", response, "writing %s failed", type);
+		return;
+	}

There are changes in addition to just moving the code like changing what the FAIL messages print. The commit message should mention these changes. Or perhaps even better, do the moving in one patch and the changes in a separate patch to make it easier to see what actually changed.

Signature · 1 line
David Lechner <[email protected]>
#9 lore raw

PATCH v3 4/6 fastboot: block: Add GPT/MBR partition table flashing support

In reply to Balaji Selvanathan

Add support for flashing GPT and MBR partition tables to the fastboot block backend. This enables operations like "fastboot flash gpt gpt.img" and "fastboot flash mbr mbr.img" for block devices.

A device can be selected with "<dev>#gpt" or "<dev>#mbr", matching the "<dev>#<partname>" form used for partitions. The device is parsed by blk_get_device_by_str(). A missing device number, as in "#gpt", is rejected.

Update Kconfig dependencies to allow FASTBOOT_GPT_NAME and FASTBOOT_MBR_NAME to work with both MMC and block backends.

---
Changes in v3:
- Use "<dev>#gpt" instead of "<dev>:gpt"
- Merge is_partition_table_name() and the device lookup into fastboot_block_get_table_dev(), which reports an invalid prefix instead of ignoring the parse result
- Share the interface and default device lookup with fastboot_block_get_part_info()
- Drop stray backtick in the commit message

Changes in v2:
- Newly created in v2
- Adds GPT and MBR flash support to fastboot block codes

---
 drivers/fastboot/Kconfig    |  4 +--
 drivers/fastboot/fb_block.c | 82 +++++++++++++++++++++++++++++++++++++++++----
 include/fb_block.h          | 22 ++++++++++++
 3 files changed, 100 insertions(+), 8 deletions(-)
drivers/fastboot/Kconfig +2 -2
index 9a5251d4b60..9099db33907 100644
@@ -261,7 +261,7 @@ config FASTBOOT_FLASH_BLOCK_DEVICE_ID
 
 config FASTBOOT_GPT_NAME
 	string "Target name for updating GPT"
-	depends on FASTBOOT_FLASH_MMC && EFI_PARTITION
+	depends on (FASTBOOT_FLASH_MMC || FASTBOOT_FLASH_BLOCK) && EFI_PARTITION
 	default "gpt"
 	help
 	  The fastboot "flash" command supports writing the downloaded
@@ -274,7 +274,7 @@ config FASTBOOT_GPT_NAME
 
 config FASTBOOT_MBR_NAME
 	string "Target name for updating MBR"
-	depends on FASTBOOT_FLASH_MMC && DOS_PARTITION
+	depends on (FASTBOOT_FLASH_MMC || FASTBOOT_FLASH_BLOCK) && DOS_PARTITION
 	default "mbr"
 	help
 	  The fastboot "flash" command allows to write the downloaded image
drivers/fastboot/fb_block.c +76 -6

106 lines not on this page · raw message

include/fb_block.h +22 -0
index bee069a1dff..031cd7fbdae 100644
@@ -27,6 +27,28 @@ int fastboot_block_get_part_info(const char *part_name,
 				 struct disk_partition *part_info,
 				 char *response);
 
+/**
+ * fastboot_block_get_table_dev() - Look up the device for a partition table
+ *
+ * Checks whether @part_name names the partition table target @table_name,
+ * either as "<table>" for the default device or as "<dev>#<table>". The
+ * device is parsed by blk_get_device_by_str(), so it is a hex number with an
+ * optional ".<hwpart>" suffix, as elsewhere in the disk layer. An empty
+ * device, as in "#gpt", is rejected.
+ *
+ * @interface: Block interface name, e.g. "scsi"
+ * @default_dev: Device to use when @part_name has no device prefix
+ * @part_name: Name sent by the host
+ * @table_name: Partition table target name, e.g. "gpt"
+ * @dev_desc: Returns the block device
+ * @response: Pointer to fastboot response buffer, set on error
+ * Return: 1 if @part_name names @table_name and @dev_desc is set, 0 if it
+ *	does not name @table_name, -ve on error
+ */
+int fastboot_block_get_table_dev(const char *interface, int default_dev,
+				 const char *part_name, const char *table_name,
+				 struct blk_desc **dev_desc, char *response);
+
 /**
  * fastboot_block_raw_erase() - Erase raw block device partition
  *
Signature · 1 line
2.34.1
Reviewed-by on v3 4/6
#10 lore raw

Re: [PATCH v3 4/6] fastboot: block: Add GPT/MBR partition table flashing support

Quoted 10 lines · Balaji Selvanathan

Add support for flashing GPT and MBR partition tables to the fastboot block backend. This enables operations like "fastboot flash gpt gpt.img" and "fastboot flash mbr mbr.img" for block devices.

A device can be selected with "<dev>#gpt" or "<dev>#mbr", matching the "<dev>#<partname>" form used for partitions. The device is parsed by blk_get_device_by_str(). A missing device number, as in "#gpt", is rejected.

[...]

Signature · 1 line
Guillaume La Roque <[email protected]>
#11 lore raw

Re: [PATCH v3 4/6] fastboot: block: Add GPT/MBR partition table flashing support

In reply to Balaji Selvanathan
Quoted 4 lines

Add support for flashing GPT and MBR partition tables to the fastboot block backend. This enables operations like "fastboot flash gpt gpt.img" and "fastboot flash mbr mbr.img" for block devices.

...

Quoted 30 lines
drivers/fastboot/fb_block.c +30 -0
+int fastboot_block_get_table_dev(const char *interface, int default_dev,
+				 const char *part_name, const char *table_name,
+				 struct blk_desc **dev_desc, char *response)
+{
+	char dev_str[16];
+	const char *sep;
+	size_t len;
+
+	if (!strcmp(part_name, table_name)) {
+		len = 0;
+	} else {
+		sep = strchr(part_name, '#');
+		if (!sep || strcmp(sep + 1, table_name))
+			return 0;
+		len = sep - part_name;
+		if (!len || len >= sizeof(dev_str)) {
+			fastboot_fail("invalid device", response);
+			return -EINVAL;
+		}
+	}
+
+	if (!interface || !strcmp(interface, "")) {
+		fastboot_fail("block interface isn't provided", response);
+		return -EINVAL;
+	}
+
+	if (!len) {
+		*dev_desc = blk_get_dev(interface, default_dev);
+		if (!*dev_desc || (*dev_desc)->type == DEV_TYPE_UNKNOWN) {
+			fastboot_fail("no such device", response);

drivers/fastboot/fb_block.c int fastboot_block_get_part_info(const char *part_name, · line 208

Quoted 10 lines
drivers/fastboot/fb_block.c +10 -0
+			return -ENODEV;
+		}
+		return 1;
+	}
+
+	strlcpy(dev_str, part_name, len + 1);
+	if (blk_get_device_by_str(interface, dev_str, dev_desc) < 0) {
+		fastboot_fail("no such device", response);
+		return -ENODEV;
+	}

The default device goes through blk_get_dev() while "<dev>#gpt" goes through blk_get_device_by_str(), which also selects hwpart 0. So a previous "<dev>.<hwpart>#..." command can leave the default device on a boot hwpart, and a plain "gpt" would then write there. Calling blk_dselect_hwpart(*dev_desc, 0) after blk_get_dev() would avoid that.

The same applies to the default device lookup in fastboot_block_get_part_info() now that patch 2 allows selecting a hwpart.

Signature · 1 line
David Lechner <[email protected]>
#12 lore raw

PATCH v3 5/6 test: fastboot: Add test for block partition table targets

In reply to Balaji Selvanathan

Add a test for fastboot_block_get_table_dev(), covering the default device, an explicit "<dev>#" prefix, names that are not a partition table target, an empty or invalid device prefix and a missing interface name.

Sandbox uses the MMC backend, so the block backend cannot be tested through fastboot_block_flash_write() there. The test calls the lookup function directly with the "mmc" interface. Lookup of ordinary partitions with "<dev>#<partname>" goes through part_get_info_by_dev_and_name_or_num(), which is already covered by dm_test_fastboot_mmc_part().

---
Changes in v3:
- New patch

---
 test/dm/fastboot.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)
test/dm/fastboot.c +61 -0
index 5b51b6bf9dd..6c008d20df6 100644
@@ -6,6 +6,7 @@
 #include <dm.h>
 #include <env.h>
 #include <fastboot.h>
+#include <fb_block.h>
 #include <fb_mmc.h>
 #include <mmc.h>
 #include <part.h>
@@ -93,3 +94,63 @@ static int dm_test_fastboot_mmc_part(struct unit_test_state *uts)
 	return 0;
 }
 DM_TEST(dm_test_fastboot_mmc_part, UTF_SCAN_PDATA | UTF_SCAN_FDT);
+
+static int dm_test_fastboot_block_table_dev(struct unit_test_state *uts)
+{
+	char response[FASTBOOT_RESPONSE_LEN] = {0};
+	struct blk_desc *desc;
+
+	/* No device prefix selects the default device */
+	ut_asserteq(1, fastboot_block_get_table_dev("mmc", 0, "gpt", "gpt",
+						    &desc, response));
+	ut_asserteq(0, desc->devnum);
+	ut_asserteq(1, fastboot_block_get_table_dev("mmc", 2, "gpt", "gpt",
+						    &desc, response));
+	ut_asserteq(2, desc->devnum);
+
+	/* An explicit device overrides the default */
+	ut_asserteq(1, fastboot_block_get_table_dev("mmc", 0, "2#gpt", "gpt",
+						    &desc, response));
+	ut_asserteq(2, desc->devnum);
+	ut_asserteq(1, fastboot_block_get_table_dev("mmc", 2, "0#mbr", "mbr",
+						    &desc, response));
+	ut_asserteq(0, desc->devnum);
+
+	/* Names which are not the table target are left to the caller */
+	ut_asserteq(0, fastboot_block_get_table_dev("mmc", 0, "boot", "gpt",
+						    &desc, response));
+	ut_asserteq(0, fastboot_block_get_table_dev("mmc", 0, "0#boot", "gpt",
+						    &desc, response));
+	ut_asserteq(0, fastboot_block_get_table_dev("mmc", 0, "gpt2", "gpt",
+						    &desc, response));
+	ut_asserteq(0, fastboot_block_get_table_dev("mmc", 0, "0#gpt2", "gpt",
+						    &desc, response));
+	ut_asserteq(0, fastboot_block_get_table_dev("mmc", 0, "mbr", "gpt",
+						    &desc, response));
+
+	/* Bad or missing device prefix */
+	ut_asserteq(-EINVAL, fastboot_block_get_table_dev("mmc", 0, "#gpt",
+							  "gpt", &desc,
+							  response));
+	ut_asserteq_str("FAILinvalid device", response);
+	ut_asserteq(-ENODEV, fastboot_block_get_table_dev("mmc", 0, "foo#gpt",
+							  "gpt", &desc,
+							  response));
+	ut_asserteq_str("FAILno such device", response);
+	ut_asserteq(-ENODEV, fastboot_block_get_table_dev("mmc", 0, "ff#gpt",
+							  "gpt", &desc,
+							  response));
+	ut_asserteq(-ENODEV, fastboot_block_get_table_dev("mmc", 0xff, "gpt",
+							  "gpt", &desc,
+							  response));
+
+	/* Missing interface name */
+	ut_asserteq(-EINVAL, fastboot_block_get_table_dev("", 0, "gpt", "gpt",
+							  &desc, response));
+	ut_asserteq_str("FAILblock interface isn't provided", response);
+	ut_asserteq(0, fastboot_block_get_table_dev("", 0, "boot", "gpt",
+						    &desc, response));
+
+	return 0;
+}
+DM_TEST(dm_test_fastboot_block_table_dev, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Signature · 1 line
2.34.1
#13 lore raw

Re: [PATCH v3 5/6] test: fastboot: Add test for block partition table targets

On Sun, 15 Nov 2026 16:35:12 +0530, Balaji Selvanathan <[email protected]> wrote:

Hi,

your tests no really cover all new feature you add, in sandbox FASTBOOT_FLASH_BLOCK is not set so you test part of your change not all.

i don't see some equal to fastboot flash or erase command , i don't know if it's possible to do it in sandbox test.

Regards,
Guillaume

Signature · 1 line
Guillaume La Roque <[email protected]>
#14 lore raw

Re: [PATCH v3 5/6] test: fastboot: Add test for block partition table targets

Quoted 9 lines · Guillaume La Roque

On Sun, 15 Nov 2026 16:35:12 +0530, Balaji Selvanathan <[email protected]> wrote:

Hi,

your tests no really cover all new feature you add, in sandbox FASTBOOT_FLASH_BLOCK is not set so you test part of your change not all.

i don't see some equal to fastboot flash or erase command , i don't know if it's possible to do it in sandbox test.

It's possible. Igor has submitted a series recently which does so. It's a clever trick but it works.

See:
https://lore.kernel.org/all/20261107-upstream-avb_tests_b4-v1-5-9177277458f9@oss.qualcomm.com/

Quoted 6 lines

Regards,
Guillaume

Signature · 1 line
Guillaume La Roque <[email protected]>
#15 lore raw

PATCH v3 6/6 doc: fastboot: Document block device selection syntax

In reply to Balaji Selvanathan

Document how the block backend selects a device: a partition name is looked up on CONFIG_FASTBOOT_FLASH_BLOCK_DEVICE_ID first, then parsed as "<dev>#<partname>" or "<dev>:<partnum>". Add examples for flash and erase, and note that "<dev>#gpt" and "<dev>#mbr" select the device for partition table updates.

---
Changes in v3:
- Document "<dev>#<partname>" and "<dev>:<partnum>" instead of "<dev>:<partname>"
- Use sentence case for the new headings
- Cut the duplicated GPT/MBR text down to one "1#gpt" example
- Mention erase and the partition-size variable, which use the same lookup
- Drop Mattijs's Reviewed-by since the documented syntax changed

Changes in v2:
- No changes

---
 doc/android/fastboot.rst | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
doc/android/fastboot.rst +32 -0
index 8e15f287f9b..404ebfe5f54 100644
@@ -195,6 +195,33 @@ The Fastboot implementation in U-Boot allows to write images into disk
 partitions. Target partitions are referred on the host computer by
 their names.
 
+Device selection for block devices
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+When using ``FASTBOOT_FLASH_BLOCK``, a partition is first looked up by name on
+the device set by ``CONFIG_FASTBOOT_FLASH_BLOCK_DEVICE_ID``. If it is not found
+there, the name is parsed in the same way as by other U-Boot disk commands::
+
+    <dev>#<partition name>      # partition selected by name
+    <dev>:<partition number>    # partition selected by number
+
+``<dev>`` is the device number on the interface set by
+``CONFIG_FASTBOOT_FLASH_BLOCK_INTERFACE_NAME``. It is parsed as hexadecimal, as
+in other disk commands, so ``10`` is device 16. It can be followed by
+``.<hwpart>`` to select a hardware partition, e.g. an eMMC boot partition.
+
+Examples::
+
+    fastboot flash boot boot.img          # partition "boot" on the default device
+    fastboot flash 1#boot boot.img        # partition "boot" on device 1
+    fastboot erase 1:3                    # partition 3 on device 1
+
+The same syntax applies to ``flash``, ``erase`` and the ``partition-size``
+variable.
+
+Partition name formats
+^^^^^^^^^^^^^^^^^^^^^^
+
 For GPT/EFI the respective partition name is used.
 
 For MBR the partitions are referred by generic names according to the
@@ -234,6 +261,11 @@ configuration options:
    CONFIG_FASTBOOT_GPT_NAME
    CONFIG_FASTBOOT_MBR_NAME
 
+When using ``FASTBOOT_FLASH_BLOCK``, the table can be written to a device other
+than the default by prefixing it with ``<dev>#``, e.g.
+``fastboot flash 1#gpt gpt.img``. ``<dev>`` is the same as in
+`Device selection for block devices`_.
+
 In Action
 ---------
 
Signature · 1 line
2.34.1
#16 lore raw

Re: [PATCH v3 6/6] doc: fastboot: Document block device selection syntax

On Sun, 15 Nov 2026 16:35:13 +0530, Balaji Selvanathan <[email protected]> wrote:

Hi,

doc/android/fastboot.rst The Fastboot implementation in U-Boot allows to write images into disk · line 212

Quoted 13 lines
doc/android/fastboot.rst +0 -0
index 8e15f287f9b4..404ebfe5f54c 100644
@@ -195,6 +195,33 @@ The Fastboot implementation in U-Boot allows to write images into disk

[ ... skip 12 lines ... ]

doc/android/fastboot.rst +6 -0
+
+``<dev>`` is the device number on the interface set by
+``CONFIG_FASTBOOT_FLASH_BLOCK_INTERFACE_NAME``. It is parsed as hexadecimal, as
+in other disk commands, so ``10`` is device 16. It can be followed by
+``.<hwpart>`` to select a hardware partition, e.g. an eMMC boot partition.
+

Partition number was in Hexa decimal too, perhasp it could be good to update this part.

Signature · 1 line
Guillaume La Roque <[email protected]>
#17 lore raw

Re: [PATCH v3 6/6] doc: fastboot: Document block device selection syntax

In reply to Balaji Selvanathan
Quoted 51 lines

Document how the block backend selects a device: a partition name is looked up on CONFIG_FASTBOOT_FLASH_BLOCK_DEVICE_ID first, then parsed as "<dev>#<partname>" or "<dev>:<partnum>". Add examples for flash and erase, and note that "<dev>#gpt" and "<dev>#mbr" select the device for partition table updates.

---
Changes in v3:
- Document "<dev>#<partname>" and "<dev>:<partnum>" instead of "<dev>:<partname>"
- Use sentence case for the new headings
- Cut the duplicated GPT/MBR text down to one "1#gpt" example
- Mention erase and the partition-size variable, which use the same lookup
- Drop Mattijs's Reviewed-by since the documented syntax changed

Changes in v2:
- No changes

---
 doc/android/fastboot.rst | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
doc/android/fastboot.rst +20 -0
index 8e15f287f9b..404ebfe5f54 100644
@@ -195,6 +195,33 @@ The Fastboot implementation in U-Boot allows to write images into disk
 partitions. Target partitions are referred on the host computer by
 their names.
 
+Device selection for block devices
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+When using ``FASTBOOT_FLASH_BLOCK``, a partition is first looked up by name on
+the device set by ``CONFIG_FASTBOOT_FLASH_BLOCK_DEVICE_ID``. If it is not found
+there, the name is parsed in the same way as by other U-Boot disk commands::
+
+    <dev>#<partition name>      # partition selected by name
+    <dev>:<partition number>    # partition selected by number
+
+``<dev>`` is the device number on the interface set by
+``CONFIG_FASTBOOT_FLASH_BLOCK_INTERFACE_NAME``. It is parsed as hexadecimal, as
+in other disk commands, so ``10`` is device 16. It can be followed by
+``.<hwpart>`` to select a hardware partition, e.g. an eMMC boot partition.
+
+Examples::
+
+    fastboot flash boot boot.img          # partition "boot" on the default device
+    fastboot flash 1#boot boot.img        # partition "boot" on device 1
+    fastboot erase 1:3                    # partition 3 on device 1

doc/android/fastboot.rst The Fastboot implementation in U-Boot allows to write images into disk · line 227

Quoted 10 lines
doc/android/fastboot.rst +7 -0
+
+The same syntax applies to ``flash``, ``erase`` and the ``partition-size``
+variable.
+
+Partition name formats
+^^^^^^^^^^^^^^^^^^^^^^
+
 For GPT/EFI the respective partition name is used.
 
 For MBR the partitions are referred by generic names according to the

The end of this "Partition name formats" section says:

The interface is always ``mmc``.

I don't think that is true now, so we should fix up that part too.

Signature · 1 line
David Lechner <[email protected]>