Skip to contentSkip to conversation

fastboot: Fix getvar "has-slot" and cleanup

PATCH New Sam Protsenko 2 participants 7 messages Jun 13, 2019 – Jun 14, 2019

lore
Share & tools
lore
#0 lore raw

v3 cover fastboot: Fix getvar "has-slot" and cleanup

This patch series fixes "has-slot" fastboot variable and provides associated refactoring, so that related code is not cluttered.

Changes in v3:
- fix build on platforms where CONFIG_FASTBOOT defined but CONFIG_FASTBOOT_FLASH is not defined
- rework logic for "has-slot" variable (handle "no" case properly)

Igor Opaniuk (1):
fastboot: Check if partition really exist in getvar_has_slot()

Sam Protsenko (2):
fastboot: Use const qualifier for char *part_name
fastboot: getvar: Refactor fastboot_*_get_part_info() usage

 drivers/fastboot/fb_getvar.c | 97 ++++++++++++++++++++++++++++--------
 drivers/fastboot/fb_mmc.c    |  3 +-
 drivers/fastboot/fb_nand.c   |  4 +-
 include/fb_mmc.h             |  3 +-
 include/fb_nand.h            |  4 +-
 5 files changed, 84 insertions(+), 27 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

PATCH v3 1/3 fastboot: Use const qualifier for char *part_name

In fastboot_*_get_part_info() functions we can use stronger typing by expecting const strings.

---
Changes in v3: none
Changes in v2: add this patch to series

 drivers/fastboot/fb_mmc.c  | 3 ++-
 drivers/fastboot/fb_nand.c | 4 ++--
 include/fb_mmc.h           | 3 ++-
 include/fb_nand.h          | 4 ++--
 4 files changed, 8 insertions(+), 6 deletions(-)
drivers/fastboot/fb_mmc.c +2 -1
index 90ca81da9b..0a335db3a6 100644
@@ -298,7 +298,8 @@ static int fb_mmc_update_zimage(struct blk_desc *dev_desc,
  * @part_info: Pointer to returned disk_partition_t
  * @response: Pointer to fastboot response buffer
  */
-int fastboot_mmc_get_part_info(char *part_name, struct blk_desc **dev_desc,
+int fastboot_mmc_get_part_info(const char *part_name,
+			       struct blk_desc **dev_desc,
 			       disk_partition_t *part_info, char *response)
 {
 	int r;
drivers/fastboot/fb_nand.c +2 -2
index 526bc12307..6756ea769f 100644
@@ -152,8 +152,8 @@ static lbaint_t fb_nand_sparse_reserve(struct sparse_storage *info,
  * @part_info: Pointer to returned part_info pointer
  * @response: Pointer to fastboot response buffer
  */
-int fastboot_nand_get_part_info(char *part_name, struct part_info **part_info,
-				char *response)
+int fastboot_nand_get_part_info(const char *part_name,
+				struct part_info **part_info, char *response)
 {
 	struct mtd_info *mtd = NULL;
 
include/fb_mmc.h +2 -1
index fd5db9eac8..95db001bee 100644
@@ -14,7 +14,8 @@
  * @part_info: Pointer to returned disk_partition_t
  * @response: Pointer to fastboot response buffer
  */
-int fastboot_mmc_get_part_info(char *part_name, struct blk_desc **dev_desc,
+int fastboot_mmc_get_part_info(const char *part_name,
+			       struct blk_desc **dev_desc,
 			       disk_partition_t *part_info, char *response);
 
 /**
include/fb_nand.h +2 -2
index 08ab0e28a6..6d7999f262 100644
@@ -16,8 +16,8 @@
  * @part_info: Pointer to returned part_info pointer
  * @response: Pointer to fastboot response buffer
  */
-int fastboot_nand_get_part_info(char *part_name, struct part_info **part_info,
-				char *response);
+int fastboot_nand_get_part_info(const char *part_name,
+				struct part_info **part_info, char *response);
 
 /**
  * fastboot_nand_flash_write() - Write image to NAND for fastboot
Signature · 1 line
2.20.1
#2 lore raw

PATCH v3 2/3 fastboot: getvar: Refactor fastboot_*_get_part_info() usage

In reply to Sam Protsenko

Extract fastboot_*_get_part_info() usage for MMC and NAND into getvar_get_part_info() function, as it will be needed further in other functions. This way we can avoid code duplication and mess with preprocessor directives across all points of usage.

---
Changes in v3:
- improve comments
- fill response with fail string for unknown storage types
Changes in v2:
- add this patch to series

 drivers/fastboot/fb_getvar.c | 58 ++++++++++++++++++++++++++----------
 1 file changed, 42 insertions(+), 16 deletions(-)
drivers/fastboot/fb_getvar.c +42 -16
index 4268628f5e..4aa2f88ece 100644
@@ -81,6 +81,47 @@ static const struct {
 	}
 };
 
+#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
+/**
+ * Get partition number and size for any storage type.
+ *
+ * Can be used to check if partition with specified name exists.
+ *
+ * If error occurs, this function guarantees to fill @p response with fail
+ * string. @p response can be rewritten in caller, if needed.
+ *
+ * @param[in] part_name Info for which partition name to look for
+ * @param[in,out] response Pointer to fastboot response buffer
+ * @param[out] size If not NULL, will contain partition size (in blocks)
+ * @return Partition number or negative value on error
+ */
+static int getvar_get_part_info(const char *part_name, char *response,
+				size_t *size)
+{
+	int r;
+# if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
+	struct blk_desc *dev_desc;
+	disk_partition_t part_info;
+
+	r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info,
+				       response);
+	if (r >= 0 && size)
+		*size = part_info.size;
+# elif CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
+	struct part_info *part_info;
+
+	r = fastboot_nand_get_part_info(part_name, &part_info, response);
+	if (r >= 0 && size)
+		*size = part_info->size;
+# else
+	fastboot_fail("this storage is not supported in bootloader", response);
+	r = -ENODEV;
+# endif
+
+	return r;
+}
+#endif
+
 static void getvar_version(char *var_parameter, char *response)
 {
 	fastboot_okay(FASTBOOT_VERSION, response);
@@ -176,22 +217,7 @@ static void getvar_partition_size(char *part_name, char *response)
 	int r;
 	size_t size;
 
-#if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
-	struct blk_desc *dev_desc;
-	disk_partition_t part_info;
-
-	r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info,
-				       response);
-	if (r >= 0)
-		size = part_info.size;
-#endif
-#if CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
-	struct part_info *part_info;
-
-	r = fastboot_nand_get_part_info(part_name, &part_info, response);
-	if (r >= 0)
-		size = part_info->size;
-#endif
+	r = getvar_get_part_info(part_name, response, &size);
 	if (r >= 0)
 		fastboot_response("OKAY", response, "0x%016zx", size);
 }
Signature · 1 line
2.20.1
#3 lore raw

PATCH v3 2/3 fastboot: getvar: Refactor fastboot_*_get_part_info() usage

On Thu, Jun 13, 2019 at 09:11:08PM +0300, Sam Protsenko wrote:
[..]

Quoted 1 line

+ * Get partition number and size for any storage type.

[..]

Quoted 1 line

+ * @return Partition number or negative value on error

[..]

I think the word 'number' should be blacklisted in the vocabulary of software development. It can be aliased with 'count' or 'index' depending on context.

[1] https://marc.info/?l=linux-kernel&m=132388598809954&w=2
[2] https://patchwork.ozlabs.org/patch/1044151/#2108815

Signature · 2 lines
Best Regards,
Eugeniu.
#4 lore raw

PATCH v3 2/3 fastboot: getvar: Refactor fastboot_*_get_part_info() usage

Hi Eugeniu,

Quoted 12 lines · Eugeniu Rosca

On Thu, Jun 13, 2019 at 09:11:08PM +0300, Sam Protsenko wrote:
[..]

Quoted 1 line

+ * Get partition number and size for any storage type.

[..]

Quoted 1 line

+ * @return Partition number or negative value on error

[..]

I think the word 'number' should be blacklisted in the vocabulary of software development. It can be aliased with 'count' or 'index' depending on context.

Although I understand the possible confusion between "index"/"count" meaning for "number" term, in this particular case I don't agree that this should be changed to "index" (in this patch at least). Yes, we shouldn't abuse this term (as any other term, for that matter), but I guess "partition number" is established expression, which means "partition index", and everybody understands that:

    $ grep -Ir 'partition number' | wc -l
    46
    $ grep -Ir 'partition index' | wc -l
    2

Similar result can be achieved when grepping in Linux kernel source tree. Also you can see this expression used in other places, like:

    $ man fdisk | grep 'partition number'
       The partition is a device name followed by a partition number.

All that said, I agree with comment on [1], where "number" can be really confusing.

So I hope you don't mind if we leave this as is in this patch?

Thanks!

Quoted 6 lines

[1] https://marc.info/?l=linux-kernel&m=132388598809954&w=2
[2] https://patchwork.ozlabs.org/patch/1044151/#2108815

--
Best Regards,
Eugeniu.

#5 lore raw

PATCH v3 2/3 fastboot: getvar: Refactor fastboot_*_get_part_info() usage

Hi Sam,

On Fri, Jun 14, 2019 at 03:41:17PM +0300, Sam Protsenko wrote:
[..]

Quoted 1 line

So I hope you don't mind if we leave this as is in this patch?

Fine. Thanks for the background.

Signature · 2 lines
Best Regards,
Eugeniu.
#6 lore raw

PATCH v3 3/3 fastboot: Check if partition really exist in getvar_has_slot()

In reply to Sam Protsenko

From: Igor Opaniuk <[email protected]>

Currently getvar_has_slot() invocation for "boot" and "system" partitions always returns affirmative response regardless the fact of existence of these partitions, which leads to impossibility to flash them on old non-A/B AOSP setups, where _a/_b suffixes aren't used:

$ fastboot flash boot boot.img
Sending 'boot__a' (11301 KB)    OKAY [  0.451s]
Writing 'boot__a'               FAILED (remote: 'cannot find partition')
fastboot: error: Command failed

Although partition layout is:
-> part list mmc 0
Partition Map for MMC device 0 -- Partition Type: EFI

Part Start LBA End LBA Name
Attributes
Type GUID
Partition GUID
1 0x00000800 0x000107ff "boot"

	attrs:	0x0000000000000000
	type:	ebd0a0a2-b9e5-4433-87c0-68b6b72699c7
	guid:	ea2e2470-db4a-d646-b828-10167f736d63
  2	0x00010800	0x000127ff	"environment"
	attrs:	0x0000000000000000
	type:	ebd0a0a2-b9e5-4433-87c0-68b6b72699c7
	guid:	10a819d2-6004-3d48-bd87-114e2a796db9
  3	0x00012800	0x0001a7ff	"recovery"
	attrs:	0x0000000000000000
	type:	ebd0a0a2-b9e5-4433-87c0-68b6b72699c7
	guid:	9ea116e4-8a34-0c48-8cf5-2fe9480f56cd
  4	0x0001a800	0x0031a7ff	"system"
	attrs:	0x0000000000000000

......

This patch adds checks of existence for requested partitions on eMMC/NAND.

Fixes f73a7df984a9 ("net: fastboot: Merge AOSP UDP fastboot")

---
Changes in v3:
- fix build on platforms where CONFIG_FASTBOOT is enabled but CONFIG_FASTBOOT_FLASH is not
- fix "no" response logic: "no" should be only reported when partition exists, but not slotted
Changes in v2:
- extract common code into getvar_get_part_info() (done in previous patch)
- use more secure strlcpy()

 drivers/fastboot/fb_getvar.c | 39 +++++++++++++++++++++++++++++++-----
 1 file changed, 34 insertions(+), 5 deletions(-)
drivers/fastboot/fb_getvar.c +34 -5
index 4aa2f88ece..584c7f0263 100644
@@ -20,7 +20,9 @@ static void getvar_product(char *var_parameter, char *response);
 static void getvar_platform(char *var_parameter, char *response);
 static void getvar_current_slot(char *var_parameter, char *response);
 static void getvar_slot_suffixes(char *var_parameter, char *response);
+#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
 static void getvar_has_slot(char *var_parameter, char *response);
+#endif
 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
 static void getvar_partition_type(char *part_name, char *response);
 #endif
@@ -65,9 +67,11 @@ static const struct {
 	}, {
 		.variable = "slot-suffixes",
 		.dispatch = getvar_slot_suffixes
+#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
 	}, {
 		.variable = "has-slot",
 		.dispatch = getvar_has_slot
+#endif
 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
 	}, {
 		.variable = "partition-type",
@@ -183,14 +187,39 @@ static void getvar_slot_suffixes(char *var_parameter, char *response)
 	fastboot_okay("_a,_b", response);
 }
 
+#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
 static void getvar_has_slot(char *part_name, char *response)
 {
-	if (part_name && (!strcmp(part_name, "boot") ||
-			  !strcmp(part_name, "system")))
-		fastboot_okay("yes", response);
-	else
-		fastboot_okay("no", response);
+	char part_name_wslot[PART_NAME_LEN];
+	size_t len;
+	int r;
+
+	if (!part_name || part_name[0] == '\0')
+		goto fail;
+
+	/* part_name_wslot = part_name + "_a" */
+	len = strlcpy(part_name_wslot, part_name, PART_NAME_LEN - 3);
+	if (len > PART_NAME_LEN - 3)
+		goto fail;
+	strcat(part_name_wslot, "_a");
+
+	r = getvar_get_part_info(part_name_wslot, response, NULL);
+	if (r >= 0) {
+		fastboot_okay("yes", response); /* part exists and slotted */
+		return;
+	}
+
+	r = getvar_get_part_info(part_name, response, NULL);
+	if (r >= 0)
+		fastboot_okay("no", response); /* part exists but not slotted */
+
+	/* At this point response is filled with okay or fail string */
+	return;
+
+fail:
+	fastboot_fail("invalid partition name", response);
 }
+#endif
 
 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
 static void getvar_partition_type(char *part_name, char *response)
Signature · 1 line
2.20.1