common: fastboot: factor out download_data from rx_handler
authorChanho Park <chanho61.park@samsung.com>
Mon, 27 Jul 2015 06:21:53 +0000 (15:21 +0900)
committerChanho Park <chanho61.park@samsung.com>
Tue, 28 Jul 2015 08:49:33 +0000 (17:49 +0900)
This patch is the first trial for refactoring cmd_fastboot because it's too
complex to modify the codes.

Signed-off-by: Chanho Park <chanho61.park@samsung.com>
common/cmd_fastboot.c

index 4b9a77e68d8ba313db447efe4081d371718ccba1..8ad4b7735f27db74d6c2988ed34d04bb008ae76b 100644 (file)
@@ -151,6 +151,7 @@ static struct cmd_fastboot_interface interface =
 #if defined(CONFIG_RAMDUMP_MODE)
 static unsigned int is_ramdump = 0;
 #endif
+
 static unsigned int download_size;
 static unsigned int download_bytes;
 //static unsigned int download_bytes_unpadded;
@@ -764,95 +765,93 @@ static void start_ramdump(void *buf)
 }
 #endif
 
-static int rx_handler (const unsigned char *buffer, unsigned int buffer_size)
+static int download_data(const unsigned char *buffer,
+                        unsigned int buffer_size, char *response)
 {
-       int ret = 1;
+       /* Something to download */
+       if (buffer_size) {
+               /* Handle possible overflow */
+               unsigned int transfer_size = download_size - download_bytes;
 
-       /* Use 65 instead of 64
-          null gets dropped
-          strcpy's need the extra byte */
-       /* A special hardware needs 8 bytes aligned address for dma transfer */
-       char *response = (char *)memalign(8, 65);
+               if (buffer_size < transfer_size)
+                       transfer_size = buffer_size;
 
-       if (download_size)
-       {
-               /* Something to download */
-
-               if (buffer_size)
-               {
-                       /* Handle possible overflow */
-                       unsigned int transfer_size = download_size - download_bytes;
-
-                       if (buffer_size < transfer_size)
-                               transfer_size = buffer_size;
-
-                       /* Save the data to the transfer buffer */
-                       memcpy (interface.transfer_buffer + download_bytes,
+               /* Save the data to the transfer buffer */
+               memcpy(interface.transfer_buffer + download_bytes,
                                buffer, transfer_size);
 
-                       download_bytes += transfer_size;
+               download_bytes += transfer_size;
 
-                       /* Check if transfer is done */
-                       if (download_bytes >= download_size)
-                       {
-                               /* Reset global transfer variable,
-                                  Keep download_bytes because it will be
-                                  used in the next possible flashing command */
-                               download_size = 0;
+               /* Check if transfer is done */
+               if (download_bytes >= download_size) {
+                       /* Reset global transfer variable,
+                          Keep download_bytes because it will be
+                          used in the next possible flashing command */
+                       download_size = 0;
 
-                               if (download_error)
-                               {
-                                       /* There was an earlier error */
-                                       sprintf(response, "ERROR");
-                               }
-                               else
-                               {
-                                       /* Everything has transferred,
-                                          send the OK response */
-                                       sprintf(response, "OKAY");
-                               }
-                               fastboot_tx_status(response, strlen(response), FASTBOOT_TX_ASYNC);
+                       if (download_error) {
+                               /* There was an earlier error */
+                               sprintf(response, "ERROR");
+                       } else {
+                               /* Everything has transferred,
+                                  send the OK response */
+                               sprintf(response, "OKAY");
+                       }
+                       fastboot_tx_status(response, strlen(response),
+                                          FASTBOOT_TX_ASYNC);
 
-                               printf("\ndownloading of %d bytes finished\n", download_bytes);
+                       printf("\ndownloading of %d bytes finished\n",
+                               download_bytes);
 #ifdef CONFIG_USE_LCD
-                               LCD_setprogress(0);
+                       LCD_setprogress(0);
 #endif
 #if defined(CONFIG_RAMDUMP_MODE)
-                               if (is_ramdump) {
-                                       is_ramdump = 0;
-                                       start_ramdump((void *)buffer);
-                               }
-#endif
+                       if (is_ramdump) {
+                               is_ramdump = 0;
+                               start_ramdump((void *)buffer);
                        }
-
-                       /* Provide some feedback */
-                       if (download_bytes && download_size &&
-                           0 == (download_bytes & (0x100000 - 1)))
-                       {
-                               /* Some feeback that the download is happening */
-                               if (download_error)
-                                       printf("X");
-                               else
-                                       printf(".");
-                               if (0 == (download_bytes %
-                                         (80 * 0x100000)))
-                                       printf("\n");
-#ifdef CONFIG_USE_LCD
-                               LCD_setfgcolor(0x2E8B57);
-                               LCD_setprogress(download_bytes / (download_size/100));
 #endif
-                       }
                }
-               else
+
+               /* Provide some feedback */
+               if (download_bytes && download_size &&
+                               0 == (download_bytes & (0x100000 - 1)))
                {
-                       /* Ignore empty buffers */
-                       printf("Warning empty download buffer\n");
-                       printf("Ignoring\n");
+                       /* Some feeback that the download is happening */
+                       if (download_error)
+                               printf("X");
+                       else
+                               printf(".");
+
+                       if (0 == (download_bytes % (80 * 0x100000)))
+                               printf("\n");
+#ifdef CONFIG_USE_LCD
+                       LCD_setfgcolor(0x2E8B57);
+                       LCD_setprogress(download_bytes / (download_size/100));
+#endif
                }
-               ret = 0;
+       } else {
+               /* Ignore empty buffers */
+               printf("Warning empty download buffer\n");
+               printf("Ignoring\n");
        }
-       else
-       {
+
+       return 0;
+}
+
+static int rx_handler (const unsigned char *buffer, unsigned int buffer_size)
+{
+       int ret = 1;
+
+       /* Use 65 instead of 64
+          null gets dropped
+          strcpy's need the extra byte */
+       /* A special hardware needs 8 bytes aligned address for dma transfer */
+       char *response = (char *)memalign(8, 65);
+
+       if (download_size)
+               ret = download_data(buffer, buffer_size, response);
+       else {
                /* A command */
 
                /* Cast to make compiler happy with string functions */