]> git.sur5r.net Git - u-boot/blobdiff - drivers/usb/gadget/f_fastboot.c
fastboot: Implement NAND backend
[u-boot] / drivers / usb / gadget / f_fastboot.c
index 751ec9e010795079a0d8c601026b0c7071f7a2fa..20b6c18b9cfd20c6a659fd14b99e0759c300ea5b 100644 (file)
@@ -13,6 +13,7 @@
 #include <config.h>
 #include <common.h>
 #include <errno.h>
+#include <fastboot.h>
 #include <malloc.h>
 #include <linux/usb/ch9.h>
 #include <linux/usb/gadget.h>
@@ -23,6 +24,9 @@
 #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
 #include <fb_mmc.h>
 #endif
+#ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
+#include <fb_nand.h>
+#endif
 
 #define FASTBOOT_VERSION               "0.4"
 
@@ -34,9 +38,6 @@
 #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1  (0x0040)
 #define TX_ENDPOINT_MAXIMUM_PACKET_SIZE      (0x0040)
 
-/* The 64 defined bytes plus \0 */
-#define RESPONSE_LEN   (64 + 1)
-
 #define EP_BUFFER_SIZE                 4096
 
 struct f_fastboot {
@@ -53,6 +54,7 @@ static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
 }
 
 static struct f_fastboot *fastboot_func;
+static unsigned int fastboot_flash_session_id;
 static unsigned int download_size;
 static unsigned int download_bytes;
 static bool is_high_speed;
@@ -123,6 +125,20 @@ 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(char *response, const char *reason)
+{
+       strncpy(response, "FAIL\0", 5);
+       strncat(response, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
+}
+
+void fastboot_okay(char *response, const char *reason)
+{
+       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)
 {
@@ -310,6 +326,9 @@ static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
 
        memcpy(in_req->buf, buffer, buffer_size);
        in_req->length = buffer_size;
+
+       usb_ep_dequeue(fastboot_func->in_ep, in_req);
+
        ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
        if (ret)
                printf("Error %d on queue\n", ret);
@@ -326,8 +345,20 @@ static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
        do_reset(NULL, 0, 0, NULL);
 }
 
+int __weak fb_set_reboot_flag(void)
+{
+       return -ENOSYS;
+}
+
 static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
 {
+       char *cmd = req->buf;
+       if (!strcmp_l1("reboot-bootloader", cmd)) {
+               if (fb_set_reboot_flag()) {
+                       fastboot_tx_write_str("FAILCannot set reboot flag");
+                       return;
+               }
+       }
        fastboot_func->in_req->complete = compl_do_reset;
        fastboot_tx_write_str("OKAY");
 }
@@ -342,7 +373,7 @@ static int strcmp_l1(const char *s1, const char *s2)
 static void cb_getvar(struct usb_ep *ep, struct usb_request *req)
 {
        char *cmd = req->buf;
-       char response[RESPONSE_LEN];
+       char response[FASTBOOT_RESPONSE_LEN];
        const char *s;
        size_t chars_left;
 
@@ -364,8 +395,17 @@ static void cb_getvar(struct usb_ep *ep, struct usb_request *req)
                !strcmp_l1("max-download-size", cmd)) {
                char str_num[12];
 
-               sprintf(str_num, "0x%08x", CONFIG_USB_FASTBOOT_BUF_SIZE);
+               sprintf(str_num, "0x%08x", CONFIG_FASTBOOT_BUF_SIZE);
                strncat(response, str_num, chars_left);
+
+               /*
+                * This also indicates the start of a new flashing
+                * "session", in which we could have 1-N buffers to
+                * write to a partition.
+                *
+                * Reset our session counter.
+                */
+               fastboot_flash_session_id = 0;
        } else if (!strcmp_l1("serialno", cmd)) {
                s = getenv("serial#");
                if (s)
@@ -399,7 +439,7 @@ static unsigned int rx_bytes_expected(unsigned int maxpacket)
 #define BYTES_PER_DOT  0x20000
 static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
 {
-       char response[RESPONSE_LEN];
+       char response[FASTBOOT_RESPONSE_LEN];
        unsigned int transfer_size = download_size - download_bytes;
        const unsigned char *buffer = req->buf;
        unsigned int buffer_size = req->actual;
@@ -414,7 +454,7 @@ static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
        if (buffer_size < transfer_size)
                transfer_size = buffer_size;
 
-       memcpy((void *)CONFIG_USB_FASTBOOT_BUF_ADDR + download_bytes,
+       memcpy((void *)CONFIG_FASTBOOT_BUF_ADDR + download_bytes,
               buffer, transfer_size);
 
        pre_dot_num = download_bytes / BYTES_PER_DOT;
@@ -456,7 +496,7 @@ static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
 static void cb_download(struct usb_ep *ep, struct usb_request *req)
 {
        char *cmd = req->buf;
-       char response[RESPONSE_LEN];
+       char response[FASTBOOT_RESPONSE_LEN];
        unsigned int max;
 
        strsep(&cmd, ":");
@@ -467,7 +507,7 @@ static void cb_download(struct usb_ep *ep, struct usb_request *req)
 
        if (0 == download_size) {
                sprintf(response, "FAILdata invalid size");
-       } else if (download_size > CONFIG_USB_FASTBOOT_BUF_SIZE) {
+       } else if (download_size > CONFIG_FASTBOOT_BUF_SIZE) {
                download_size = 0;
                sprintf(response, "FAILdata too large");
        } else {
@@ -517,7 +557,7 @@ static void cb_continue(struct usb_ep *ep, struct usb_request *req)
 static void cb_flash(struct usb_ep *ep, struct usb_request *req)
 {
        char *cmd = req->buf;
-       char response[RESPONSE_LEN];
+       char response[FASTBOOT_RESPONSE_LEN];
 
        strsep(&cmd, ":");
        if (!cmd) {
@@ -528,9 +568,16 @@ static void cb_flash(struct usb_ep *ep, struct usb_request *req)
 
        strcpy(response, "FAILno flash device defined");
 #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
-       fb_mmc_flash_write(cmd, (void *)CONFIG_USB_FASTBOOT_BUF_ADDR,
+       fb_mmc_flash_write(cmd, fastboot_flash_session_id,
+                          (void *)CONFIG_FASTBOOT_BUF_ADDR,
                           download_bytes, response);
 #endif
+#ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
+       fb_nand_flash_write(cmd, fastboot_flash_session_id,
+                           (void *)CONFIG_FASTBOOT_BUF_ADDR,
+                           download_bytes, response);
+#endif
+       fastboot_flash_session_id++;
        fastboot_tx_write_str(response);
 }
 #endif
@@ -538,7 +585,7 @@ static void cb_flash(struct usb_ep *ep, struct usb_request *req)
 static void cb_oem(struct usb_ep *ep, struct usb_request *req)
 {
        char *cmd = req->buf;
-#ifdef CONFIG_FASTBOOT_FLASH
+#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
        if (strncmp("format", cmd + 4, 6) == 0) {
                char cmdbuf[32];
                 sprintf(cmdbuf, "gpt write mmc %x $partitions",
@@ -561,7 +608,7 @@ static void cb_oem(struct usb_ep *ep, struct usb_request *req)
 static void cb_erase(struct usb_ep *ep, struct usb_request *req)
 {
        char *cmd = req->buf;
-       char response[RESPONSE_LEN];
+       char response[FASTBOOT_RESPONSE_LEN];
 
        strsep(&cmd, ":");
        if (!cmd) {
@@ -574,6 +621,9 @@ static void cb_erase(struct usb_ep *ep, struct usb_request *req)
 
 #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
        fb_mmc_erase(cmd, response);
+#endif
+#ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
+       fb_nand_erase(cmd, response);
 #endif
        fastboot_tx_write_str(response);
 }
@@ -622,6 +672,9 @@ static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
        void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
        int i;
 
+       if (req->status != 0 || req->length == 0)
+               return;
+
        for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
                if (!strcmp_l1(cmd_dispatch_info[i].cmd, cmdbuf)) {
                        func_cb = cmd_dispatch_info[i].cb;
@@ -643,9 +696,7 @@ static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
                }
        }
 
-       if (req->status == 0) {
-               *cmdbuf = '\0';
-               req->actual = 0;
-               usb_ep_queue(ep, req, 0);
-       }
+       *cmdbuf = '\0';
+       req->actual = 0;
+       usb_ep_queue(ep, req, 0);
 }