fastboot: add fb_getvar

Add an fb_getvar helper that can be used to get values from the
target.

Change-Id: I0da088fcbc8d40076c7bf5ef6e5bbd97fae61471
diff --git a/fastboot/engine.c b/fastboot/engine.c
index 46b0828..7dc29e4 100644
--- a/fastboot/engine.c
+++ b/fastboot/engine.c
@@ -111,6 +111,20 @@
 void generate_ext4_image(struct image_data *image);
 void cleanup_image(struct image_data *image);
 
+int fb_getvar(struct usb_handle *usb, char *response, const char *fmt, ...)
+{
+    char cmd[CMD_SIZE] = "getvar:";
+    int getvar_len = strlen(cmd);
+    va_list args;
+
+    response[FB_RESPONSE_SZ] = '\0';
+    va_start(args, fmt);
+    vsnprintf(cmd + getvar_len, sizeof(cmd) - getvar_len, fmt, args);
+    va_end(args);
+    cmd[CMD_SIZE - 1] = '\0';
+    return fb_command_response(usb, cmd, response);
+}
+
 struct generator {
     char *fs_type;
 
@@ -278,9 +292,7 @@
     unsigned i;
     char cmd[CMD_SIZE];
 
-    response[FB_RESPONSE_SZ] = '\0';
-    snprintf(cmd, sizeof(cmd), "getvar:partition-type:%s", partition);
-    status = fb_command_response(usb, cmd, response);
+    status = fb_getvar(usb, response, "partition-type:%s", partition);
     if (status) {
         if (skip_if_not_supported) {
             fprintf(stderr,
@@ -312,9 +324,7 @@
         return -1;
     }
 
-    response[FB_RESPONSE_SZ] = '\0';
-    snprintf(cmd, sizeof(cmd), "getvar:partition-size:%s", partition);
-    status = fb_command_response(usb, cmd, response);
+    status = fb_getvar(usb, response, "partition-size:%s", partition);
     if (status) {
         if (skip_if_not_supported) {
             fprintf(stderr,
diff --git a/fastboot/fastboot.h b/fastboot/fastboot.h
index 1d3e2b8..a84b0be 100644
--- a/fastboot/fastboot.h
+++ b/fastboot/fastboot.h
@@ -41,6 +41,7 @@
 #define FB_RESPONSE_SZ 64
 
 /* engine.c - high level command queue engine */
+int fb_getvar(struct usb_handle *usb, char *response, const char *fmt, ...);
 void fb_queue_flash(const char *ptn, void *data, unsigned sz);;
 void fb_queue_erase(const char *ptn);
 void fb_queue_format(const char *ptn, int skip_if_not_supported);