[PATCH v2 2/4] fastboot: Add GPT/MBR partition table flashing helper functions

Balaji Selvanathan balaji.selvanathan at oss.qualcomm.com
Mon Apr 27 14:06:43 CEST 2026


Add fastboot_flash_gpt_partition_table() and
fastboot_flash_mbr_partition_table() helper functions that handle
flashing of GPT and MBR partition tables to block devices.

The MMC backend now uses these helper functions for GPT and MBR
operations, simplifying the code while maintaining the same
functionality.

Signed-off-by: Balaji Selvanathan <balaji.selvanathan at oss.qualcomm.com>
---
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_common.c | 94 ++++++++++++++++++++++++++++++++++++++++++++
 drivers/fastboot/fb_mmc.c    | 49 ++++-------------------
 include/fastboot-internal.h  | 30 ++++++++++++++
 3 files changed, 131 insertions(+), 42 deletions(-)

diff --git a/drivers/fastboot/fb_common.c b/drivers/fastboot/fb_common.c
index dac5528f809..b20bc728910 100644
--- a/drivers/fastboot/fb_common.c
+++ b/drivers/fastboot/fb_common.c
@@ -245,3 +245,97 @@ void fastboot_init(void *buf_addr, u32 buf_size)
 	fastboot_set_progress_callback(NULL);
 
 }
+
+#if CONFIG_IS_ENABLED(EFI_PARTITION)
+/**
+ * fastboot_flash_gpt_partition_table() - Flash GPT partition table
+ * @interface: Block interface name (e.g., "mmc", "scsi")
+ * @device: Device number
+ * @download_buffer: Buffer containing GPT data
+ * @response: Fastboot response buffer
+ */
+void fastboot_flash_gpt_partition_table(const char *interface,
+					int device,
+					void *download_buffer,
+					char *response)
+{
+	struct blk_desc *dev_desc;
+
+	if (!interface || !strcmp(interface, "")) {
+		fastboot_fail("block interface isn't provided", response);
+		return;
+	}
+
+	dev_desc = blk_get_dev(interface, device);
+	if (!dev_desc) {
+		fastboot_fail("no such device", response);
+		return;
+	}
+
+	printf("%s: updating MBR, Primary and Backup GPT(s) on %s device %d\n",
+	       __func__, interface, dev_desc->devnum);
+
+	if (is_valid_gpt_buf(dev_desc, download_buffer)) {
+		printf("%s: invalid GPT - refusing to write to flash\n", __func__);
+		fastboot_fail("invalid GPT partition", response);
+		return;
+	}
+
+	if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
+		printf("%s: writing GPT partitions failed\n", __func__);
+		fastboot_fail("writing GPT partitions failed", response);
+		return;
+	}
+
+	part_init(dev_desc);
+	printf("........ success\n");
+	fastboot_okay(NULL, response);
+}
+#endif
+
+#if CONFIG_IS_ENABLED(DOS_PARTITION)
+/**
+ * fastboot_flash_mbr_partition_table() - Flash MBR partition table
+ * @interface: Block interface name (e.g., "mmc", "scsi")
+ * @device: Device number
+ * @download_buffer: Buffer containing MBR data
+ * @response: Fastboot response buffer
+ */
+void fastboot_flash_mbr_partition_table(const char *interface,
+					int device,
+					void *download_buffer,
+					char *response)
+{
+	struct blk_desc *dev_desc;
+
+	if (!interface || !strcmp(interface, "")) {
+		fastboot_fail("block interface isn't provided", response);
+		return;
+	}
+
+	dev_desc = blk_get_dev(interface, device);
+	if (!dev_desc) {
+		fastboot_fail("no such device", response);
+		return;
+	}
+
+	printf("%s: updating MBR on %s device %d\n", __func__, interface,
+	       dev_desc->devnum);
+
+	if (is_valid_dos_buf(download_buffer)) {
+		printf("%s: invalid MBR - refusing to write to flash\n", __func__);
+		fastboot_fail("invalid MBR partition", response);
+		return;
+	}
+
+	if (write_mbr_sector(dev_desc, download_buffer)) {
+		printf("%s: writing MBR partition failed\n", __func__);
+		fastboot_fail("writing MBR partition failed", response);
+		return;
+	}
+
+	part_init(dev_desc);
+	printf("........ success\n");
+	fastboot_okay(NULL, response);
+}
+#endif
diff --git a/drivers/fastboot/fb_mmc.c b/drivers/fastboot/fb_mmc.c
index 11d9c8e8460..c85853915c8 100644
--- a/drivers/fastboot/fb_mmc.c
+++ b/drivers/fastboot/fb_mmc.c
@@ -343,7 +343,7 @@ int fastboot_mmc_get_part_info(const char *part_name,
 	return ret;
 }
 
-static struct blk_desc *fastboot_mmc_get_dev(char *response)
+static struct blk_desc __maybe_unused *fastboot_mmc_get_dev(char *response)
 {
 	struct blk_desc *ret = blk_get_dev("mmc",
 					   CONFIG_FASTBOOT_FLASH_MMC_DEV);
@@ -389,53 +389,18 @@ void fastboot_mmc_flash_write(const char *cmd, void *download_buffer,
 
 #if CONFIG_IS_ENABLED(EFI_PARTITION)
 	if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
-		dev_desc = fastboot_mmc_get_dev(response);
-		if (!dev_desc)
-			return;
-
-		printf("%s: updating MBR, Primary and Backup GPT(s)\n",
-		       __func__);
-		if (is_valid_gpt_buf(dev_desc, download_buffer)) {
-			printf("%s: invalid GPT - refusing to write to flash\n",
-			       __func__);
-			fastboot_fail("invalid GPT partition", response);
-			return;
-		}
-		if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
-			printf("%s: writing GPT partitions failed\n", __func__);
-			fastboot_fail("writing GPT partitions failed",
-				      response);
-			return;
-		}
-		part_init(dev_desc);
-		printf("........ success\n");
-		fastboot_okay(NULL, response);
+		fastboot_flash_gpt_partition_table("mmc",
+						   CONFIG_FASTBOOT_FLASH_MMC_DEV,
+						   download_buffer, response);
 		return;
 	}
 #endif
 
 #if CONFIG_IS_ENABLED(DOS_PARTITION)
 	if (strcmp(cmd, CONFIG_FASTBOOT_MBR_NAME) == 0) {
-		dev_desc = fastboot_mmc_get_dev(response);
-		if (!dev_desc)
-			return;
-
-		printf("%s: updating MBR\n", __func__);
-		if (is_valid_dos_buf(download_buffer)) {
-			printf("%s: invalid MBR - refusing to write to flash\n",
-			       __func__);
-			fastboot_fail("invalid MBR partition", response);
-			return;
-		}
-		if (write_mbr_sector(dev_desc, download_buffer)) {
-			printf("%s: writing MBR partition failed\n", __func__);
-			fastboot_fail("writing MBR partition failed",
-				      response);
-			return;
-		}
-		part_init(dev_desc);
-		printf("........ success\n");
-		fastboot_okay(NULL, response);
+		fastboot_flash_mbr_partition_table("mmc",
+						   CONFIG_FASTBOOT_FLASH_MMC_DEV,
+						   download_buffer, response);
 		return;
 	}
 #endif
diff --git a/include/fastboot-internal.h b/include/fastboot-internal.h
index 610d4f91414..35683cdb642 100644
--- a/include/fastboot-internal.h
+++ b/include/fastboot-internal.h
@@ -40,4 +40,34 @@ void fastboot_getvar_all(char *response);
  */
 void fastboot_getvar(char *cmd_parameter, char *response);
 
+#if CONFIG_IS_ENABLED(EFI_PARTITION)
+/**
+ * fastboot_flash_gpt_partition_table() - Flash GPT partition table
+ *
+ * @interface: Block interface name (e.g., "mmc", "scsi")
+ * @device: Device number
+ * @download_buffer: Buffer containing GPT data
+ * @response: Pointer to fastboot response buffer
+ */
+void fastboot_flash_gpt_partition_table(const char *interface,
+					int device,
+					void *download_buffer,
+					char *response);
+#endif
+
+#if CONFIG_IS_ENABLED(DOS_PARTITION)
+/**
+ * fastboot_flash_mbr_partition_table() - Flash MBR partition table
+ *
+ * @interface: Block interface name (e.g., "mmc", "scsi")
+ * @device: Device number
+ * @download_buffer: Buffer containing MBR data
+ * @response: Pointer to fastboot response buffer
+ */
+void fastboot_flash_mbr_partition_table(const char *interface,
+					int device,
+					void *download_buffer,
+					char *response);
+#endif
+
 #endif

-- 
2.34.1



More information about the U-Boot mailing list