[U-Boot] [RFC PATCH v2 10/20] fastboot: Merge USB and UDP getvar implementation

Alex Kiernan alex.kiernan at gmail.com
Mon Apr 30 08:32:44 UTC 2018


Merge the USB and UDP getvar implementations into one. The USB side
gains new variables previously defined in the network side and the
network side now supports fetching of arbitrary variables prefixed with
'fastboot.'

Signed-off-by: Alex Kiernan <alex.kiernan at gmail.com>
---

Changes in v2: None

 drivers/fastboot/Makefile       |  1 +
 drivers/fastboot/fb_common.c    |  7 ++++
 drivers/fastboot/fb_getvar.c    | 93 +++++++++++++++++++++++++++++++++++++++++
 drivers/usb/gadget/f_fastboot.c | 56 +------------------------
 include/fastboot.h              | 18 ++++++++
 net/fastboot.c                  | 75 +--------------------------------
 6 files changed, 121 insertions(+), 129 deletions(-)
 create mode 100644 drivers/fastboot/fb_getvar.c

diff --git a/drivers/fastboot/Makefile b/drivers/fastboot/Makefile
index c12dfa8..9af4073 100644
--- a/drivers/fastboot/Makefile
+++ b/drivers/fastboot/Makefile
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier:      GPL-2.0+
 
 obj-y += fb_common.o
+obj-y += fb_getvar.o
 obj-$(CONFIG_FASTBOOT_FLASH) += image-sparse.o
 
 obj-$(CONFIG_FASTBOOT_FLASH_MMC) += fb_mmc.o
diff --git a/drivers/fastboot/fb_common.c b/drivers/fastboot/fb_common.c
index f0bf53d..3b68f25 100644
--- a/drivers/fastboot/fb_common.c
+++ b/drivers/fastboot/fb_common.c
@@ -65,3 +65,10 @@ void timed_send_info(ulong *start, const char *msg)
 	}
 #endif
 }
+
+int strcmp_l1(const char *s1, const char *s2)
+{
+	if (!s1 || !s2)
+		return -1;
+	return strncmp(s1, s2, strlen(s1));
+}
diff --git a/drivers/fastboot/fb_getvar.c b/drivers/fastboot/fb_getvar.c
new file mode 100644
index 0000000..aa68371
--- /dev/null
+++ b/drivers/fastboot/fb_getvar.c
@@ -0,0 +1,93 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ */
+
+#include <common.h>
+#include <fastboot.h>
+#include <version.h>
+
+/**
+ * Writes ascii string specified by cmd_parameter to response.
+ *
+ * @param cmd_parameter	Point to command parameter
+ * @param response	Pointer to fastboot response buffer
+ */
+void fb_getvar(char *cmd_parameter, char *response)
+{
+	if (!cmd_parameter) {
+		fastboot_fail("missing var", response);
+	} else if (!strcmp("version", cmd_parameter)) {
+		fastboot_okay(FASTBOOT_VERSION, response);
+	} else if (!strcmp("bootloader-version", cmd_parameter) ||
+		   !strcmp("version-bootloader", cmd_parameter)) {
+		fastboot_okay(U_BOOT_VERSION, response);
+	} else if (!strcmp("downloadsize", cmd_parameter) ||
+		   !strcmp("max-download-size", cmd_parameter)) {
+		fastboot_response("OKAY", response,
+				  "0x%08x", CONFIG_FASTBOOT_BUF_SIZE);
+	} else if (!strcmp("serialno", cmd_parameter)) {
+		const char *tmp = env_get("serial#");
+
+		if (tmp)
+			fastboot_okay(tmp, response);
+		else
+			fastboot_fail("Value not set", response);
+	} else if (!strcmp("version-baseband", cmd_parameter)) {
+		fastboot_okay("N/A", response);
+	} else if (!strcmp("product", cmd_parameter)) {
+		const char *board = env_get("board");
+
+		if (board)
+			fastboot_okay(board, response);
+		else
+			fastboot_fail("Board not set", response);
+	} else if (!strcmp("current-slot", cmd_parameter)) {
+		/* A/B not implemented, for now always return _a */
+		fastboot_okay("_a", response);
+	} else if (!strcmp("slot-suffixes", cmd_parameter)) {
+		fastboot_okay("_a,_b", response);
+	} else if (!strcmp_l1("has-slot", cmd_parameter)) {
+		char *part_name = cmd_parameter;
+
+		cmd_parameter = strsep(&part_name, ":");
+		if (!strcmp(part_name, "boot") || !strcmp(part_name, "system"))
+			fastboot_okay("yes", response);
+		else
+			fastboot_okay("no", response);
+	} else if (!strcmp_l1("partition-type", cmd_parameter) ||
+		   !strcmp_l1("partition-size", cmd_parameter)) {
+		disk_partition_t part_info;
+		struct blk_desc *dev_desc;
+		char *part_name = cmd_parameter;
+
+		cmd_parameter = strsep(&part_name, ":");
+		dev_desc = blk_get_dev("mmc", 0);
+		if (!dev_desc) {
+			fastboot_fail("block device not found", response);
+		} else if (part_get_info_by_name(dev_desc, part_name,
+						 &part_info) < 0) {
+			fastboot_fail("partition not found", response);
+		} else if (!strcmp_l1("partition-type", cmd_parameter)) {
+			fastboot_okay((char *)part_info.type, response);
+		} else if (!strcmp_l1("partition-size", cmd_parameter)) {
+			fastboot_response("OKAY", response,
+					  "0x%016x", (int)part_info.size);
+		}
+	} else {
+#define FASTBOOT_ENV_PREFIX	"fastboot."
+		char envstr[FASTBOOT_RESPONSE_LEN];
+		const char *s;
+
+		snprintf(envstr, sizeof(envstr) - 1,
+			 FASTBOOT_ENV_PREFIX "%s", cmd_parameter);
+		s = env_get(envstr);
+		if (s) {
+			fastboot_response("OKAY", response, "%s", s);
+		} else {
+			pr_warn("WARNING: unknown variable: %s\n",
+				cmd_parameter);
+			fastboot_fail("Variable not implemented", response);
+		}
+	}
+}
diff --git a/drivers/usb/gadget/f_fastboot.c b/drivers/usb/gadget/f_fastboot.c
index e757c3e..a493c75 100644
--- a/drivers/usb/gadget/f_fastboot.c
+++ b/drivers/usb/gadget/f_fastboot.c
@@ -148,7 +148,6 @@ static struct usb_gadget_strings *fastboot_strings[] = {
 };
 
 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
-static int strcmp_l1(const char *s1, const char *s2);
 
 static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
 {
@@ -376,66 +375,13 @@ static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
 	fastboot_tx_write_str("OKAY");
 }
 
-static int strcmp_l1(const char *s1, const char *s2)
-{
-	if (!s1 || !s2)
-		return -1;
-	return strncmp(s1, s2, strlen(s1));
-}
-
 static void cb_getvar(struct usb_ep *ep, struct usb_request *req)
 {
 	char *cmd = req->buf;
 	char response[FASTBOOT_RESPONSE_LEN];
-	const char *s;
-	size_t chars_left;
-
-	strcpy(response, "OKAY");
-	chars_left = sizeof(response) - strlen(response) - 1;
 
 	strsep(&cmd, ":");
-	if (!cmd) {
-		pr_err("missing variable");
-		fastboot_tx_write_str("FAILmissing var");
-		return;
-	}
-
-	if (!strcmp_l1("version", cmd)) {
-		strncat(response, FASTBOOT_VERSION, chars_left);
-	} else if (!strcmp_l1("bootloader-version", cmd)) {
-		strncat(response, U_BOOT_VERSION, chars_left);
-	} else if (!strcmp_l1("downloadsize", cmd) ||
-		!strcmp_l1("max-download-size", cmd)) {
-		char str_num[12];
-
-		sprintf(str_num, "0x%08x", CONFIG_FASTBOOT_BUF_SIZE);
-		strncat(response, str_num, chars_left);
-	} else if (!strcmp_l1("serialno", cmd)) {
-		s = env_get("serial#");
-		if (s)
-			strncat(response, s, chars_left);
-		else
-			strcpy(response, "FAILValue not set");
-	} else {
-		char *envstr;
-
-		envstr = malloc(strlen("fastboot.") + strlen(cmd) + 1);
-		if (!envstr) {
-			fastboot_tx_write_str("FAILmalloc error");
-			return;
-		}
-
-		sprintf(envstr, "fastboot.%s", cmd);
-		s = env_get(envstr);
-		if (s) {
-			strncat(response, s, chars_left);
-		} else {
-			printf("WARNING: unknown variable: %s\n", cmd);
-			strcpy(response, "FAILVariable not implemented");
-		}
-
-		free(envstr);
-	}
+	fb_getvar(cmd, response);
 	fastboot_tx_write_str(response);
 }
 
diff --git a/include/fastboot.h b/include/fastboot.h
index 6f69423..fb58358 100644
--- a/include/fastboot.h
+++ b/include/fastboot.h
@@ -13,6 +13,8 @@
 #ifndef _FASTBOOT_H_
 #define _FASTBOOT_H_
 
+#define FASTBOOT_VERSION	"0.4"
+
 /* The 64 defined bytes plus \0 */
 #define FASTBOOT_RESPONSE_LEN	(64 + 1)
 
@@ -24,6 +26,14 @@ void fastboot_fail(const char *reason, char *response);
 void fastboot_okay(const char *reason, char *response);
 
 /**
+ * Writes ascii string specified by cmd_parameter to response.
+ *
+ * @param cmd_parameter	Pointer to variable name
+ * @param response	Pointer to fastboot response buffer
+ */
+void fb_getvar(char *cmd_parameter, char *response);
+
+/**
  * Send an INFO packet during long commands based on timer. If
  * CONFIG_UDP_FUNCTION_FASTBOOT is defined, an INFO packet is sent
  * if the time is 30 seconds after start. Else, noop.
@@ -36,4 +46,12 @@ void fastboot_okay(const char *reason, char *response);
  */
 void timed_send_info(ulong *start, const char *msg);
 
+/**
+ * Compare strings over the length of s1
+ *
+ * @param s1		Pointer to string which limits the comparisson
+ * @param s2		Second string
+ */
+int strcmp_l1(const char *s1, const char *s2);
+
 #endif /* _FASTBOOT_H_ */
diff --git a/net/fastboot.c b/net/fastboot.c
index 960e7f1..cd09ada 100644
--- a/net/fastboot.c
+++ b/net/fastboot.c
@@ -57,7 +57,6 @@ static int fastboot_remote_port;
 /* The UDP port at our end */
 static int fastboot_our_port;
 
-static void fb_getvar(char *);
 static void fb_download(char *, unsigned int, char *);
 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
 static void fb_flash(char *);
@@ -167,7 +166,7 @@ static void fastboot_send(struct fastboot_header fb_header, char *fastboot_data,
 			if (cmd_parameter)
 				cmd_parameter = strdup(cmd_parameter);
 		} else if (!strcmp("getvar", cmd_string)) {
-			fb_getvar(response);
+			fb_getvar(cmd_parameter, response);
 		} else if (!strcmp("download", cmd_string)) {
 			fb_download(fastboot_data, fastboot_data_len, response);
 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
@@ -233,78 +232,6 @@ static void fastboot_send(struct fastboot_header fb_header, char *fastboot_data,
 }
 
 /**
- * Writes ascii string specified by cmd_parameter to response.
- *
- * @param repsonse    Pointer to fastboot response buffer
- */
-static void fb_getvar(char *response)
-{
-	if (!cmd_parameter) {
-		fastboot_fail("missing var", response);
-	} else if (!strcmp("version", cmd_parameter)) {
-		fastboot_okay(FASTBOOT_VERSION, response);
-	} else if (!strcmp("bootloader-version", cmd_parameter) ||
-		   !strcmp("version-bootloader", cmd_parameter)) {
-		fastboot_okay(U_BOOT_VERSION, response);
-	} else if (!strcmp("downloadsize", cmd_parameter) ||
-		   !strcmp("max-download-size", cmd_parameter)) {
-		fastboot_response("OKAY", response,
-				  "0x%08x", CONFIG_FASTBOOT_BUF_SIZE);
-	} else if (!strcmp("serialno", cmd_parameter)) {
-		const char *tmp = env_get("serial#");
-
-		if (tmp)
-			fastboot_okay(tmp, response);
-		else
-			fastboot_fail("Value not set", response);
-	} else if (!strcmp("version-baseband", cmd_parameter)) {
-		fastboot_okay("N/A", response);
-	} else if (!strcmp("product", cmd_parameter)) {
-		const char *board = env_get("board");
-
-		if (board)
-			fastboot_okay(board, response);
-		else
-			fastboot_fail("Board not set", response);
-	} else if (!strcmp("current-slot", cmd_parameter)) {
-		/* A/B not implemented, for now always return _a */
-		fastboot_okay("_a", response);
-	} else if (!strcmp("slot-suffixes", cmd_parameter)) {
-		fastboot_okay("_a,_b", response);
-	} else if (!strncmp("has-slot", cmd_parameter, 8)) {
-		char *part_name = cmd_parameter;
-
-		cmd_parameter = strsep(&part_name, ":");
-		if (!strcmp(part_name, "boot") || !strcmp(part_name, "system"))
-			fastboot_okay("yes", response);
-		else
-			fastboot_okay("no", response);
-	} else if (!strncmp("partition-type", cmd_parameter, 14) ||
-		   !strncmp("partition-size", cmd_parameter, 14)) {
-		disk_partition_t part_info;
-		struct blk_desc *dev_desc;
-		char *part_name = cmd_parameter;
-
-		cmd_parameter = strsep(&part_name, ":");
-		dev_desc = blk_get_dev("mmc", 0);
-		if (!dev_desc) {
-			fastboot_fail("block device not found", response);
-		} else if (part_get_info_by_name(dev_desc, part_name,
-						 &part_info) < 0) {
-			fastboot_fail("partition not found", response);
-		} else if (!strncmp("partition-type", cmd_parameter, 14)) {
-			fastboot_okay((char *)part_info.type, response);
-		} else if (!strncmp("partition-size", cmd_parameter, 14)) {
-			fastboot_response("OKAY", response,
-					  "0x%016x", (int)part_info.size);
-		}
-	} else {
-		printf("WARNING: unknown variable: %s\n", cmd_parameter);
-		fastboot_fail("Variable not implemented", response);
-	}
-}
-
-/**
  * Copies image data from fastboot_data to CONFIG_FASTBOOT_BUF_ADDR.
  * Writes to response.
  *
-- 
2.7.4



More information about the U-Boot mailing list