[U-Boot] [PATCH v3 04/13] fastboot: Extract fastboot_okay/fail to fb_common.c
Alex Kiernan
alex.kiernan at gmail.com
Mon May 14 09:08:59 UTC 2018
Add drivers/fastboot/fb_common.c, where fastboot_okay/fail are implemented
so we can call them from a non-USB implementation.
Introduce fastboot_response which takes varargs parameters so we can
use it to generate formatted response strings. Refactor fastboot_okay/fail
to use it.
Signed-off-by: Alex Kiernan <alex.kiernan at gmail.com>
Reviewed-by: Joe Hershberger <joe.hershberger at ni.com>
---
Changes in v3:
- Merge subsequent patch for formatting response strings into this one
- allow NULL to fastboot_okay() when there's no message to send
Changes in v2: None
drivers/fastboot/Makefile | 2 ++
drivers/fastboot/fb_common.c | 49 +++++++++++++++++++++++++++++++++++++++++
drivers/usb/gadget/f_fastboot.c | 13 -----------
include/fastboot.h | 4 ++++
4 files changed, 55 insertions(+), 13 deletions(-)
create mode 100644 drivers/fastboot/fb_common.c
diff --git a/drivers/fastboot/Makefile b/drivers/fastboot/Makefile
index 651fbf0..b38dcff 100644
--- a/drivers/fastboot/Makefile
+++ b/drivers/fastboot/Makefile
@@ -1,5 +1,7 @@
# SPDX-License-Identifier: GPL-2.0+
+obj-y += fb_common.o
+
ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
obj-y += fb_mmc.o
endif
diff --git a/drivers/fastboot/fb_common.c b/drivers/fastboot/fb_common.c
new file mode 100644
index 0000000..c9638f1
--- /dev/null
+++ b/drivers/fastboot/fb_common.c
@@ -0,0 +1,49 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * (C) Copyright 2008 - 2009
+ * Windriver, <www.windriver.com>
+ * Tom Rix <Tom.Rix at windriver.com>
+ *
+ * Copyright 2011 Sebastian Andrzej Siewior <bigeasy at linutronix.de>
+ *
+ * Copyright 2014 Linaro, Ltd.
+ * Rob Herring <robh at kernel.org>
+ */
+
+#include <common.h>
+#include <fastboot.h>
+
+/**
+ * Writes a response to response buffer of the form "$tag$reason".
+ *
+ * @param tag The first part of the response
+ * @param response Pointer to fastboot response buffer
+ * @param format printf style format string
+ */
+void fastboot_response(const char *tag, char *response,
+ const char *format, ...)
+{
+ va_list args;
+
+ strlcpy(response, tag, FASTBOOT_RESPONSE_LEN);
+ if (format) {
+ va_start(args, format);
+ vsnprintf(response + strlen(response),
+ FASTBOOT_RESPONSE_LEN - strlen(response) - 1,
+ format, args);
+ va_end(args);
+ }
+}
+
+void fastboot_fail(const char *reason, char *response)
+{
+ fastboot_response("FAIL", response, "%s", reason);
+}
+
+void fastboot_okay(const char *reason, char *response)
+{
+ if (reason)
+ fastboot_response("OKAY", response, "%s", reason);
+ else
+ fastboot_response("OKAY", response, NULL);
+}
diff --git a/drivers/usb/gadget/f_fastboot.c b/drivers/usb/gadget/f_fastboot.c
index bb60612..2e6e161 100644
--- a/drivers/usb/gadget/f_fastboot.c
+++ b/drivers/usb/gadget/f_fastboot.c
@@ -149,19 +149,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);
-
-void fastboot_fail(const char *reason, char *response)
-{
- strncpy(response, "FAIL\0", 5);
- strncat(response, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
-}
-
-void fastboot_okay(const char *reason, char *response)
-{
- strncpy(response, "OKAY\0", 5);
- strncat(response, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
-}
-
static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
{
int status = req->status;
diff --git a/include/fastboot.h b/include/fastboot.h
index ed52dae..434c46b 100644
--- a/include/fastboot.h
+++ b/include/fastboot.h
@@ -15,6 +15,10 @@
/* The 64 defined bytes plus \0 */
#define FASTBOOT_RESPONSE_LEN (64 + 1)
+void fastboot_response(const char *tag, char *response,
+ const char *format, ...)
+ __attribute__ ((format (__printf__, 3, 4)));
+
void fastboot_fail(const char *reason, char *response);
void fastboot_okay(const char *reason, char *response);
--
2.7.4
More information about the U-Boot
mailing list