[U-Boot] [RFC PATCH v2 05/20] fastboot: Introduce fastboot_response and refactor fastboot_okay/fail
Alex Kiernan
alex.kiernan at gmail.com
Mon Apr 30 08:32:39 UTC 2018
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>
---
Changes in v2: None
drivers/fastboot/fb_common.c | 28 ++++++++++++++++++++++++----
include/fastboot.h | 4 ++++
2 files changed, 28 insertions(+), 4 deletions(-)
diff --git a/drivers/fastboot/fb_common.c b/drivers/fastboot/fb_common.c
index 53cffe3..fe58803 100644
--- a/drivers/fastboot/fb_common.c
+++ b/drivers/fastboot/fb_common.c
@@ -13,14 +13,34 @@
#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)
{
- strncpy(response, "FAIL\0", 5);
- strncat(response, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
+ fastboot_response("FAIL", response, "%s", reason);
}
void fastboot_okay(const char *reason, char *response)
{
- strncpy(response, "OKAY\0", 5);
- strncat(response, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
+ fastboot_response("OKAY", response, "%s", reason);
}
diff --git a/include/fastboot.h b/include/fastboot.h
index f22080a..2140c94 100644
--- a/include/fastboot.h
+++ b/include/fastboot.h
@@ -16,6 +16,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