Home | History | Annotate | Download | only in fastboot
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  *  * Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  *  * Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in
     12  *    the documentation and/or other materials provided with the
     13  *    distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
     22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include "fastboot.h"
     30 #include "make_ext4fs.h"
     31 
     32 #include <errno.h>
     33 #include <stdio.h>
     34 #include <stdlib.h>
     35 #include <stdarg.h>
     36 #include <stdbool.h>
     37 #include <string.h>
     38 #include <sys/stat.h>
     39 #include <sys/time.h>
     40 #include <sys/types.h>
     41 #include <unistd.h>
     42 
     43 #ifdef USE_MINGW
     44 #include <fcntl.h>
     45 #else
     46 #include <sys/mman.h>
     47 #endif
     48 
     49 #define ARRAY_SIZE(x)           (sizeof(x)/sizeof(x[0]))
     50 
     51 double now()
     52 {
     53     struct timeval tv;
     54     gettimeofday(&tv, NULL);
     55     return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
     56 }
     57 
     58 char *mkmsg(const char *fmt, ...)
     59 {
     60     char buf[256];
     61     char *s;
     62     va_list ap;
     63 
     64     va_start(ap, fmt);
     65     vsprintf(buf, fmt, ap);
     66     va_end(ap);
     67 
     68     s = strdup(buf);
     69     if (s == 0) die("out of memory");
     70     return s;
     71 }
     72 
     73 #define OP_DOWNLOAD   1
     74 #define OP_COMMAND    2
     75 #define OP_QUERY      3
     76 #define OP_NOTICE     4
     77 #define OP_FORMAT     5
     78 #define OP_DOWNLOAD_SPARSE 6
     79 
     80 typedef struct Action Action;
     81 
     82 #define CMD_SIZE 64
     83 
     84 struct Action
     85 {
     86     unsigned op;
     87     Action *next;
     88 
     89     char cmd[CMD_SIZE];
     90     const char *prod;
     91     void *data;
     92     unsigned size;
     93 
     94     const char *msg;
     95     int (*func)(Action *a, int status, char *resp);
     96 
     97     double start;
     98 };
     99 
    100 static Action *action_list = 0;
    101 static Action *action_last = 0;
    102 
    103 
    104 struct image_data {
    105     long long partition_size;
    106     long long image_size; // real size of image file
    107     void *buffer;
    108 };
    109 
    110 void generate_ext4_image(struct image_data *image);
    111 void cleanup_image(struct image_data *image);
    112 
    113 int fb_getvar(struct usb_handle *usb, char *response, const char *fmt, ...)
    114 {
    115     char cmd[CMD_SIZE] = "getvar:";
    116     int getvar_len = strlen(cmd);
    117     va_list args;
    118 
    119     response[FB_RESPONSE_SZ] = '\0';
    120     va_start(args, fmt);
    121     vsnprintf(cmd + getvar_len, sizeof(cmd) - getvar_len, fmt, args);
    122     va_end(args);
    123     cmd[CMD_SIZE - 1] = '\0';
    124     return fb_command_response(usb, cmd, response);
    125 }
    126 
    127 struct generator {
    128     char *fs_type;
    129 
    130     /* generate image and return it as image->buffer.
    131      * size of the buffer returned as image->image_size.
    132      *
    133      * image->partition_size specifies what is the size of the
    134      * file partition we generate image for.
    135      */
    136     void (*generate)(struct image_data *image);
    137 
    138     /* it cleans the buffer allocated during image creation.
    139      * this function probably does free() or munmap().
    140      */
    141     void (*cleanup)(struct image_data *image);
    142 } generators[] = {
    143     { "ext4", generate_ext4_image, cleanup_image }
    144 };
    145 
    146 /* Return true if this partition is supported by the fastboot format command.
    147  * It is also used to determine if we should first erase a partition before
    148  * flashing it with an ext4 filesystem.  See needs_erase()
    149  *
    150  * Not all devices report the filesystem type, so don't report any errors,
    151  * just return false.
    152  */
    153 int fb_format_supported(usb_handle *usb, const char *partition)
    154 {
    155     char response[FB_RESPONSE_SZ+1];
    156     struct generator *generator = NULL;
    157     int status;
    158     unsigned int i;
    159 
    160     status = fb_getvar(usb, response, "partition-type:%s", partition);
    161     if (status) {
    162         return 0;
    163     }
    164 
    165     for (i = 0; i < ARRAY_SIZE(generators); i++) {
    166         if (!strncmp(generators[i].fs_type, response, FB_RESPONSE_SZ)) {
    167             generator = &generators[i];
    168             break;
    169         }
    170     }
    171 
    172     if (generator) {
    173         return 1;
    174     }
    175 
    176     return 0;
    177 }
    178 
    179 static int cb_default(Action *a, int status, char *resp)
    180 {
    181     if (status) {
    182         fprintf(stderr,"FAILED (%s)\n", resp);
    183     } else {
    184         double split = now();
    185         fprintf(stderr,"OKAY [%7.3fs]\n", (split - a->start));
    186         a->start = split;
    187     }
    188     return status;
    189 }
    190 
    191 static Action *queue_action(unsigned op, const char *fmt, ...)
    192 {
    193     Action *a;
    194     va_list ap;
    195     size_t cmdsize;
    196 
    197     a = calloc(1, sizeof(Action));
    198     if (a == 0) die("out of memory");
    199 
    200     va_start(ap, fmt);
    201     cmdsize = vsnprintf(a->cmd, sizeof(a->cmd), fmt, ap);
    202     va_end(ap);
    203 
    204     if (cmdsize >= sizeof(a->cmd)) {
    205         free(a);
    206         die("Command length (%d) exceeds maximum size (%d)", cmdsize, sizeof(a->cmd));
    207     }
    208 
    209     if (action_last) {
    210         action_last->next = a;
    211     } else {
    212         action_list = a;
    213     }
    214     action_last = a;
    215     a->op = op;
    216     a->func = cb_default;
    217 
    218     a->start = -1;
    219 
    220     return a;
    221 }
    222 
    223 void fb_queue_erase(const char *ptn)
    224 {
    225     Action *a;
    226     a = queue_action(OP_COMMAND, "erase:%s", ptn);
    227     a->msg = mkmsg("erasing '%s'", ptn);
    228 }
    229 
    230 /* Loads file content into buffer. Returns NULL on error. */
    231 static void *load_buffer(int fd, off_t size)
    232 {
    233     void *buffer;
    234 
    235 #ifdef USE_MINGW
    236     ssize_t count = 0;
    237 
    238     // mmap is more efficient but mingw does not support it.
    239     // In this case we read whole image into memory buffer.
    240     buffer = malloc(size);
    241     if (!buffer) {
    242         perror("malloc");
    243         return NULL;
    244     }
    245 
    246     lseek(fd, 0, SEEK_SET);
    247     while(count < size) {
    248         ssize_t actually_read = read(fd, (char*)buffer+count, size-count);
    249 
    250         if (actually_read == 0) {
    251             break;
    252         }
    253         if (actually_read < 0) {
    254             if (errno == EINTR) {
    255                 continue;
    256             }
    257             perror("read");
    258             free(buffer);
    259             return NULL;
    260         }
    261 
    262         count += actually_read;
    263     }
    264 #else
    265     buffer = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
    266     if (buffer == MAP_FAILED) {
    267         perror("mmap");
    268         return NULL;
    269     }
    270 #endif
    271 
    272     return buffer;
    273 }
    274 
    275 void cleanup_image(struct image_data *image)
    276 {
    277 #ifdef USE_MINGW
    278     free(image->buffer);
    279 #else
    280     munmap(image->buffer, image->image_size);
    281 #endif
    282 }
    283 
    284 void generate_ext4_image(struct image_data *image)
    285 {
    286     int fd;
    287     struct stat st;
    288 
    289     fd = fileno(tmpfile());
    290     make_ext4fs_sparse_fd(fd, image->partition_size, NULL, NULL);
    291 
    292     fstat(fd, &st);
    293     image->image_size = st.st_size;
    294     image->buffer = load_buffer(fd, st.st_size);
    295 
    296     close(fd);
    297 }
    298 
    299 int fb_format(Action *a, usb_handle *usb, int skip_if_not_supported)
    300 {
    301     const char *partition = a->cmd;
    302     char response[FB_RESPONSE_SZ+1];
    303     int status = 0;
    304     struct image_data image;
    305     struct generator *generator = NULL;
    306     int fd;
    307     unsigned i;
    308     char cmd[CMD_SIZE];
    309 
    310     status = fb_getvar(usb, response, "partition-type:%s", partition);
    311     if (status) {
    312         if (skip_if_not_supported) {
    313             fprintf(stderr,
    314                     "Erase successful, but not automatically formatting.\n");
    315             fprintf(stderr,
    316                     "Can't determine partition type.\n");
    317             return 0;
    318         }
    319         fprintf(stderr,"FAILED (%s)\n", fb_get_error());
    320         return status;
    321     }
    322 
    323     for (i = 0; i < ARRAY_SIZE(generators); i++) {
    324         if (!strncmp(generators[i].fs_type, response, FB_RESPONSE_SZ)) {
    325             generator = &generators[i];
    326             break;
    327         }
    328     }
    329     if (!generator) {
    330         if (skip_if_not_supported) {
    331             fprintf(stderr,
    332                     "Erase successful, but not automatically formatting.\n");
    333             fprintf(stderr,
    334                     "File system type %s not supported.\n", response);
    335             return 0;
    336         }
    337         fprintf(stderr,"Formatting is not supported for filesystem with type '%s'.\n",
    338                 response);
    339         return -1;
    340     }
    341 
    342     status = fb_getvar(usb, response, "partition-size:%s", partition);
    343     if (status) {
    344         if (skip_if_not_supported) {
    345             fprintf(stderr,
    346                     "Erase successful, but not automatically formatting.\n");
    347             fprintf(stderr, "Unable to get partition size\n.");
    348             return 0;
    349         }
    350         fprintf(stderr,"FAILED (%s)\n", fb_get_error());
    351         return status;
    352     }
    353     image.partition_size = strtoll(response, (char **)NULL, 16);
    354 
    355     generator->generate(&image);
    356     if (!image.buffer) {
    357         fprintf(stderr,"Cannot generate image.\n");
    358         return -1;
    359     }
    360 
    361     // Following piece of code is similar to fb_queue_flash() but executes
    362     // actions directly without queuing
    363     fprintf(stderr, "sending '%s' (%lli KB)...\n", partition, image.image_size/1024);
    364     status = fb_download_data(usb, image.buffer, image.image_size);
    365     if (status) goto cleanup;
    366 
    367     fprintf(stderr, "writing '%s'...\n", partition);
    368     snprintf(cmd, sizeof(cmd), "flash:%s", partition);
    369     status = fb_command(usb, cmd);
    370     if (status) goto cleanup;
    371 
    372 cleanup:
    373     generator->cleanup(&image);
    374 
    375     return status;
    376 }
    377 
    378 void fb_queue_format(const char *partition, int skip_if_not_supported)
    379 {
    380     Action *a;
    381 
    382     a = queue_action(OP_FORMAT, partition);
    383     a->data = (void*)skip_if_not_supported;
    384     a->msg = mkmsg("formatting '%s' partition", partition);
    385 }
    386 
    387 void fb_queue_flash(const char *ptn, void *data, unsigned sz)
    388 {
    389     Action *a;
    390 
    391     a = queue_action(OP_DOWNLOAD, "");
    392     a->data = data;
    393     a->size = sz;
    394     a->msg = mkmsg("sending '%s' (%d KB)", ptn, sz / 1024);
    395 
    396     a = queue_action(OP_COMMAND, "flash:%s", ptn);
    397     a->msg = mkmsg("writing '%s'", ptn);
    398 }
    399 
    400 void fb_queue_flash_sparse(const char *ptn, struct sparse_file *s, unsigned sz)
    401 {
    402     Action *a;
    403 
    404     a = queue_action(OP_DOWNLOAD_SPARSE, "");
    405     a->data = s;
    406     a->size = 0;
    407     a->msg = mkmsg("sending sparse '%s' (%d KB)", ptn, sz / 1024);
    408 
    409     a = queue_action(OP_COMMAND, "flash:%s", ptn);
    410     a->msg = mkmsg("writing '%s'", ptn);
    411 }
    412 
    413 static int match(char *str, const char **value, unsigned count)
    414 {
    415     const char *val;
    416     unsigned n;
    417     int len;
    418 
    419     for (n = 0; n < count; n++) {
    420         const char *val = value[n];
    421         int len = strlen(val);
    422         int match;
    423 
    424         if ((len > 1) && (val[len-1] == '*')) {
    425             len--;
    426             match = !strncmp(val, str, len);
    427         } else {
    428             match = !strcmp(val, str);
    429         }
    430 
    431         if (match) return 1;
    432     }
    433 
    434     return 0;
    435 }
    436 
    437 
    438 
    439 static int cb_check(Action *a, int status, char *resp, int invert)
    440 {
    441     const char **value = a->data;
    442     unsigned count = a->size;
    443     unsigned n;
    444     int yes;
    445 
    446     if (status) {
    447         fprintf(stderr,"FAILED (%s)\n", resp);
    448         return status;
    449     }
    450 
    451     if (a->prod) {
    452         if (strcmp(a->prod, cur_product) != 0) {
    453             double split = now();
    454             fprintf(stderr,"IGNORE, product is %s required only for %s [%7.3fs]\n",
    455                     cur_product, a->prod, (split - a->start));
    456             a->start = split;
    457             return 0;
    458         }
    459     }
    460 
    461     yes = match(resp, value, count);
    462     if (invert) yes = !yes;
    463 
    464     if (yes) {
    465         double split = now();
    466         fprintf(stderr,"OKAY [%7.3fs]\n", (split - a->start));
    467         a->start = split;
    468         return 0;
    469     }
    470 
    471     fprintf(stderr,"FAILED\n\n");
    472     fprintf(stderr,"Device %s is '%s'.\n", a->cmd + 7, resp);
    473     fprintf(stderr,"Update %s '%s'",
    474             invert ? "rejects" : "requires", value[0]);
    475     for (n = 1; n < count; n++) {
    476         fprintf(stderr," or '%s'", value[n]);
    477     }
    478     fprintf(stderr,".\n\n");
    479     return -1;
    480 }
    481 
    482 static int cb_require(Action *a, int status, char *resp)
    483 {
    484     return cb_check(a, status, resp, 0);
    485 }
    486 
    487 static int cb_reject(Action *a, int status, char *resp)
    488 {
    489     return cb_check(a, status, resp, 1);
    490 }
    491 
    492 void fb_queue_require(const char *prod, const char *var,
    493 		int invert, unsigned nvalues, const char **value)
    494 {
    495     Action *a;
    496     a = queue_action(OP_QUERY, "getvar:%s", var);
    497     a->prod = prod;
    498     a->data = value;
    499     a->size = nvalues;
    500     a->msg = mkmsg("checking %s", var);
    501     a->func = invert ? cb_reject : cb_require;
    502     if (a->data == 0) die("out of memory");
    503 }
    504 
    505 static int cb_display(Action *a, int status, char *resp)
    506 {
    507     if (status) {
    508         fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp);
    509         return status;
    510     }
    511     fprintf(stderr, "%s: %s\n", (char*) a->data, resp);
    512     return 0;
    513 }
    514 
    515 void fb_queue_display(const char *var, const char *prettyname)
    516 {
    517     Action *a;
    518     a = queue_action(OP_QUERY, "getvar:%s", var);
    519     a->data = strdup(prettyname);
    520     if (a->data == 0) die("out of memory");
    521     a->func = cb_display;
    522 }
    523 
    524 static int cb_save(Action *a, int status, char *resp)
    525 {
    526     if (status) {
    527         fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp);
    528         return status;
    529     }
    530     strncpy(a->data, resp, a->size);
    531     return 0;
    532 }
    533 
    534 void fb_queue_query_save(const char *var, char *dest, unsigned dest_size)
    535 {
    536     Action *a;
    537     a = queue_action(OP_QUERY, "getvar:%s", var);
    538     a->data = (void *)dest;
    539     a->size = dest_size;
    540     a->func = cb_save;
    541 }
    542 
    543 static int cb_do_nothing(Action *a, int status, char *resp)
    544 {
    545     fprintf(stderr,"\n");
    546     return 0;
    547 }
    548 
    549 void fb_queue_reboot(void)
    550 {
    551     Action *a = queue_action(OP_COMMAND, "reboot");
    552     a->func = cb_do_nothing;
    553     a->msg = "rebooting";
    554 }
    555 
    556 void fb_queue_command(const char *cmd, const char *msg)
    557 {
    558     Action *a = queue_action(OP_COMMAND, cmd);
    559     a->msg = msg;
    560 }
    561 
    562 void fb_queue_download(const char *name, void *data, unsigned size)
    563 {
    564     Action *a = queue_action(OP_DOWNLOAD, "");
    565     a->data = data;
    566     a->size = size;
    567     a->msg = mkmsg("downloading '%s'", name);
    568 }
    569 
    570 void fb_queue_notice(const char *notice)
    571 {
    572     Action *a = queue_action(OP_NOTICE, "");
    573     a->data = (void*) notice;
    574 }
    575 
    576 int fb_execute_queue(usb_handle *usb)
    577 {
    578     Action *a;
    579     char resp[FB_RESPONSE_SZ+1];
    580     int status = 0;
    581 
    582     a = action_list;
    583     if (!a)
    584         return status;
    585     resp[FB_RESPONSE_SZ] = 0;
    586 
    587     double start = -1;
    588     for (a = action_list; a; a = a->next) {
    589         a->start = now();
    590         if (start < 0) start = a->start;
    591         if (a->msg) {
    592             // fprintf(stderr,"%30s... ",a->msg);
    593             fprintf(stderr,"%s...\n",a->msg);
    594         }
    595         if (a->op == OP_DOWNLOAD) {
    596             status = fb_download_data(usb, a->data, a->size);
    597             status = a->func(a, status, status ? fb_get_error() : "");
    598             if (status) break;
    599         } else if (a->op == OP_COMMAND) {
    600             status = fb_command(usb, a->cmd);
    601             status = a->func(a, status, status ? fb_get_error() : "");
    602             if (status) break;
    603         } else if (a->op == OP_QUERY) {
    604             status = fb_command_response(usb, a->cmd, resp);
    605             status = a->func(a, status, status ? fb_get_error() : resp);
    606             if (status) break;
    607         } else if (a->op == OP_NOTICE) {
    608             fprintf(stderr,"%s\n",(char*)a->data);
    609         } else if (a->op == OP_FORMAT) {
    610             status = fb_format(a, usb, (int)a->data);
    611             status = a->func(a, status, status ? fb_get_error() : "");
    612             if (status) break;
    613         } else if (a->op == OP_DOWNLOAD_SPARSE) {
    614             status = fb_download_data_sparse(usb, a->data);
    615             status = a->func(a, status, status ? fb_get_error() : "");
    616             if (status) break;
    617         } else {
    618             die("bogus action");
    619         }
    620     }
    621 
    622     fprintf(stderr,"finished. total time: %.3fs\n", (now() - start));
    623     return status;
    624 }
    625 
    626 int fb_queue_is_empty(void)
    627 {
    628     return (action_list == NULL);
    629 }
    630