The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 12 | * the documentation and/or other materials provided with the |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 | |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 29 | #include "fastboot.h" |
| 30 | #include "make_ext4fs.h" |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 31 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <stdarg.h> |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 35 | #include <stdbool.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 36 | #include <string.h> |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 37 | #include <sys/stat.h> |
Daniel Sandler | cb6e22b | 2010-02-25 14:05:33 -0500 | [diff] [blame] | 38 | #include <sys/time.h> |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 39 | #include <sys/types.h> |
| 40 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 41 | |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 42 | #ifdef USE_MINGW |
| 43 | #include <fcntl.h> |
| 44 | #else |
| 45 | #include <sys/mman.h> |
| 46 | #endif |
| 47 | |
| 48 | extern struct fs_info info; |
| 49 | |
| 50 | #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 51 | |
Daniel Sandler | cb6e22b | 2010-02-25 14:05:33 -0500 | [diff] [blame] | 52 | double now() |
| 53 | { |
| 54 | struct timeval tv; |
| 55 | gettimeofday(&tv, NULL); |
| 56 | return (double)tv.tv_sec + (double)tv.tv_usec / 1000000; |
| 57 | } |
| 58 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 59 | char *mkmsg(const char *fmt, ...) |
| 60 | { |
| 61 | char buf[256]; |
| 62 | char *s; |
| 63 | va_list ap; |
| 64 | |
| 65 | va_start(ap, fmt); |
| 66 | vsprintf(buf, fmt, ap); |
| 67 | va_end(ap); |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 68 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 69 | s = strdup(buf); |
| 70 | if (s == 0) die("out of memory"); |
| 71 | return s; |
| 72 | } |
| 73 | |
| 74 | #define OP_DOWNLOAD 1 |
| 75 | #define OP_COMMAND 2 |
| 76 | #define OP_QUERY 3 |
| 77 | #define OP_NOTICE 4 |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 78 | #define OP_FORMAT 5 |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 79 | #define OP_DOWNLOAD_SPARSE 6 |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 80 | |
| 81 | typedef struct Action Action; |
| 82 | |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 83 | #define CMD_SIZE 64 |
| 84 | |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 85 | struct Action |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 86 | { |
| 87 | unsigned op; |
| 88 | Action *next; |
| 89 | |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 90 | char cmd[CMD_SIZE]; |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 91 | const char *prod; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 92 | void *data; |
| 93 | unsigned size; |
| 94 | |
| 95 | const char *msg; |
| 96 | int (*func)(Action *a, int status, char *resp); |
Daniel Sandler | cb6e22b | 2010-02-25 14:05:33 -0500 | [diff] [blame] | 97 | |
| 98 | double start; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | static Action *action_list = 0; |
| 102 | static Action *action_last = 0; |
| 103 | |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 104 | |
| 105 | struct image_data { |
| 106 | long long partition_size; |
| 107 | long long image_size; // real size of image file |
| 108 | void *buffer; |
| 109 | }; |
| 110 | |
| 111 | void generate_ext4_image(struct image_data *image); |
| 112 | void cleanup_image(struct image_data *image); |
| 113 | |
Colin Cross | 80f2d03 | 2012-05-24 18:24:53 -0700 | [diff] [blame] | 114 | int fb_getvar(struct usb_handle *usb, char *response, const char *fmt, ...) |
| 115 | { |
| 116 | char cmd[CMD_SIZE] = "getvar:"; |
| 117 | int getvar_len = strlen(cmd); |
| 118 | va_list args; |
| 119 | |
| 120 | response[FB_RESPONSE_SZ] = '\0'; |
| 121 | va_start(args, fmt); |
| 122 | vsnprintf(cmd + getvar_len, sizeof(cmd) - getvar_len, fmt, args); |
| 123 | va_end(args); |
| 124 | cmd[CMD_SIZE - 1] = '\0'; |
| 125 | return fb_command_response(usb, cmd, response); |
| 126 | } |
| 127 | |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 128 | struct generator { |
| 129 | char *fs_type; |
| 130 | |
| 131 | /* generate image and return it as image->buffer. |
| 132 | * size of the buffer returned as image->image_size. |
| 133 | * |
| 134 | * image->partition_size specifies what is the size of the |
| 135 | * file partition we generate image for. |
| 136 | */ |
| 137 | void (*generate)(struct image_data *image); |
| 138 | |
| 139 | /* it cleans the buffer allocated during image creation. |
| 140 | * this function probably does free() or munmap(). |
| 141 | */ |
| 142 | void (*cleanup)(struct image_data *image); |
| 143 | } generators[] = { |
| 144 | { "ext4", generate_ext4_image, cleanup_image } |
| 145 | }; |
| 146 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 147 | static int cb_default(Action *a, int status, char *resp) |
| 148 | { |
| 149 | if (status) { |
| 150 | fprintf(stderr,"FAILED (%s)\n", resp); |
| 151 | } else { |
Daniel Sandler | cb6e22b | 2010-02-25 14:05:33 -0500 | [diff] [blame] | 152 | double split = now(); |
| 153 | fprintf(stderr,"OKAY [%7.3fs]\n", (split - a->start)); |
| 154 | a->start = split; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 155 | } |
| 156 | return status; |
| 157 | } |
| 158 | |
| 159 | static Action *queue_action(unsigned op, const char *fmt, ...) |
| 160 | { |
| 161 | Action *a; |
| 162 | va_list ap; |
Bruce Beare | 50b3995 | 2010-07-15 08:52:01 -0700 | [diff] [blame] | 163 | size_t cmdsize; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 164 | |
| 165 | a = calloc(1, sizeof(Action)); |
| 166 | if (a == 0) die("out of memory"); |
| 167 | |
| 168 | va_start(ap, fmt); |
Bruce Beare | 50b3995 | 2010-07-15 08:52:01 -0700 | [diff] [blame] | 169 | cmdsize = vsnprintf(a->cmd, sizeof(a->cmd), fmt, ap); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 170 | va_end(ap); |
| 171 | |
Bruce Beare | 50b3995 | 2010-07-15 08:52:01 -0700 | [diff] [blame] | 172 | if (cmdsize >= sizeof(a->cmd)) { |
| 173 | free(a); |
| 174 | die("Command length (%d) exceeds maximum size (%d)", cmdsize, sizeof(a->cmd)); |
| 175 | } |
| 176 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 177 | if (action_last) { |
| 178 | action_last->next = a; |
| 179 | } else { |
| 180 | action_list = a; |
| 181 | } |
| 182 | action_last = a; |
| 183 | a->op = op; |
| 184 | a->func = cb_default; |
Daniel Sandler | cb6e22b | 2010-02-25 14:05:33 -0500 | [diff] [blame] | 185 | |
| 186 | a->start = -1; |
| 187 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 188 | return a; |
| 189 | } |
| 190 | |
| 191 | void fb_queue_erase(const char *ptn) |
| 192 | { |
| 193 | Action *a; |
| 194 | a = queue_action(OP_COMMAND, "erase:%s", ptn); |
| 195 | a->msg = mkmsg("erasing '%s'", ptn); |
| 196 | } |
| 197 | |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 198 | /* Loads file content into buffer. Returns NULL on error. */ |
| 199 | static void *load_buffer(int fd, off_t size) |
| 200 | { |
| 201 | void *buffer; |
| 202 | |
| 203 | #ifdef USE_MINGW |
| 204 | ssize_t count = 0; |
| 205 | |
| 206 | // mmap is more efficient but mingw does not support it. |
| 207 | // In this case we read whole image into memory buffer. |
| 208 | buffer = malloc(size); |
| 209 | if (!buffer) { |
| 210 | perror("malloc"); |
| 211 | return NULL; |
| 212 | } |
| 213 | |
| 214 | lseek(fd, 0, SEEK_SET); |
| 215 | while(count < size) { |
| 216 | ssize_t actually_read = read(fd, (char*)buffer+count, size-count); |
| 217 | |
| 218 | if (actually_read == 0) { |
| 219 | break; |
| 220 | } |
| 221 | if (actually_read < 0) { |
| 222 | if (errno == EINTR) { |
| 223 | continue; |
| 224 | } |
| 225 | perror("read"); |
| 226 | free(buffer); |
| 227 | return NULL; |
| 228 | } |
| 229 | |
| 230 | count += actually_read; |
| 231 | } |
| 232 | #else |
| 233 | buffer = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 234 | if (buffer == MAP_FAILED) { |
| 235 | perror("mmap"); |
| 236 | return NULL; |
| 237 | } |
| 238 | #endif |
| 239 | |
| 240 | return buffer; |
| 241 | } |
| 242 | |
| 243 | void cleanup_image(struct image_data *image) |
| 244 | { |
| 245 | #ifdef USE_MINGW |
| 246 | free(image->buffer); |
| 247 | #else |
| 248 | munmap(image->buffer, image->image_size); |
| 249 | #endif |
| 250 | } |
| 251 | |
| 252 | void generate_ext4_image(struct image_data *image) |
| 253 | { |
| 254 | int fd; |
| 255 | struct stat st; |
| 256 | |
| 257 | #ifdef USE_MINGW |
| 258 | /* Ideally we should use tmpfile() here, the same as with unix version. |
| 259 | * But unfortunately it is not portable as it is not clear whether this |
| 260 | * function opens file in TEXT or BINARY mode. |
| 261 | * |
| 262 | * There are also some reports it is buggy: |
| 263 | * http://pdplab.it.uom.gr/teaching/gcc_manuals/gnulib.html#tmpfile |
| 264 | * http://www.mega-nerd.com/erikd/Blog/Windiots/tmpfile.html |
| 265 | */ |
| 266 | char *filename = tempnam(getenv("TEMP"), "fastboot-format.img"); |
| 267 | fd = open(filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0644); |
| 268 | unlink(filename); |
| 269 | #else |
| 270 | fd = fileno(tmpfile()); |
| 271 | #endif |
JP Abgrall | 30ae580 | 2012-05-07 20:25:24 -0700 | [diff] [blame] | 272 | /* reset ext4fs info so we can be called multiple times */ |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 273 | reset_ext4fs_info(); |
| 274 | info.len = image->partition_size; |
Kenny Root | 1ecb868 | 2012-03-30 20:44:45 -0700 | [diff] [blame] | 275 | make_ext4fs_internal(fd, NULL, NULL, NULL, 0, 1, 0, 0, 0, NULL); |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 276 | |
| 277 | fstat(fd, &st); |
| 278 | image->image_size = st.st_size; |
| 279 | image->buffer = load_buffer(fd, st.st_size); |
| 280 | |
| 281 | close(fd); |
| 282 | } |
| 283 | |
JP Abgrall | 30ae580 | 2012-05-07 20:25:24 -0700 | [diff] [blame] | 284 | int fb_format(Action *a, usb_handle *usb, int skip_if_not_supported) |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 285 | { |
| 286 | const char *partition = a->cmd; |
| 287 | char response[FB_RESPONSE_SZ+1]; |
| 288 | int status = 0; |
| 289 | struct image_data image; |
| 290 | struct generator *generator = NULL; |
| 291 | int fd; |
| 292 | unsigned i; |
| 293 | char cmd[CMD_SIZE]; |
| 294 | |
Colin Cross | 80f2d03 | 2012-05-24 18:24:53 -0700 | [diff] [blame] | 295 | status = fb_getvar(usb, response, "partition-type:%s", partition); |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 296 | if (status) { |
JP Abgrall | 30ae580 | 2012-05-07 20:25:24 -0700 | [diff] [blame] | 297 | if (skip_if_not_supported) { |
| 298 | fprintf(stderr, |
| 299 | "Erase successful, but not automatically formatting.\n"); |
| 300 | fprintf(stderr, |
| 301 | "Can't determine partition type.\n"); |
| 302 | return 0; |
| 303 | } |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 304 | fprintf(stderr,"FAILED (%s)\n", fb_get_error()); |
| 305 | return status; |
| 306 | } |
| 307 | |
| 308 | for (i = 0; i < ARRAY_SIZE(generators); i++) { |
| 309 | if (!strncmp(generators[i].fs_type, response, FB_RESPONSE_SZ)) { |
| 310 | generator = &generators[i]; |
| 311 | break; |
| 312 | } |
| 313 | } |
| 314 | if (!generator) { |
JP Abgrall | 30ae580 | 2012-05-07 20:25:24 -0700 | [diff] [blame] | 315 | if (skip_if_not_supported) { |
| 316 | fprintf(stderr, |
| 317 | "Erase successful, but not automatically formatting.\n"); |
| 318 | fprintf(stderr, |
| 319 | "File system type %s not supported.\n", response); |
| 320 | return 0; |
| 321 | } |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 322 | fprintf(stderr,"Formatting is not supported for filesystem with type '%s'.\n", |
| 323 | response); |
| 324 | return -1; |
| 325 | } |
| 326 | |
Colin Cross | 80f2d03 | 2012-05-24 18:24:53 -0700 | [diff] [blame] | 327 | status = fb_getvar(usb, response, "partition-size:%s", partition); |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 328 | if (status) { |
JP Abgrall | 30ae580 | 2012-05-07 20:25:24 -0700 | [diff] [blame] | 329 | if (skip_if_not_supported) { |
| 330 | fprintf(stderr, |
| 331 | "Erase successful, but not automatically formatting.\n"); |
| 332 | fprintf(stderr, "Unable to get partition size\n."); |
| 333 | return 0; |
| 334 | } |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 335 | fprintf(stderr,"FAILED (%s)\n", fb_get_error()); |
| 336 | return status; |
| 337 | } |
| 338 | image.partition_size = strtoll(response, (char **)NULL, 16); |
| 339 | |
| 340 | generator->generate(&image); |
| 341 | if (!image.buffer) { |
| 342 | fprintf(stderr,"Cannot generate image.\n"); |
| 343 | return -1; |
| 344 | } |
| 345 | |
| 346 | // Following piece of code is similar to fb_queue_flash() but executes |
| 347 | // actions directly without queuing |
| 348 | fprintf(stderr, "sending '%s' (%lli KB)...\n", partition, image.image_size/1024); |
| 349 | status = fb_download_data(usb, image.buffer, image.image_size); |
| 350 | if (status) goto cleanup; |
| 351 | |
| 352 | fprintf(stderr, "writing '%s'...\n", partition); |
| 353 | snprintf(cmd, sizeof(cmd), "flash:%s", partition); |
| 354 | status = fb_command(usb, cmd); |
| 355 | if (status) goto cleanup; |
| 356 | |
| 357 | cleanup: |
| 358 | generator->cleanup(&image); |
| 359 | |
| 360 | return status; |
| 361 | } |
| 362 | |
JP Abgrall | 30ae580 | 2012-05-07 20:25:24 -0700 | [diff] [blame] | 363 | void fb_queue_format(const char *partition, int skip_if_not_supported) |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 364 | { |
| 365 | Action *a; |
| 366 | |
| 367 | a = queue_action(OP_FORMAT, partition); |
JP Abgrall | 30ae580 | 2012-05-07 20:25:24 -0700 | [diff] [blame] | 368 | a->data = (void*)skip_if_not_supported; |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 369 | a->msg = mkmsg("formatting '%s' partition", partition); |
| 370 | } |
| 371 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 372 | void fb_queue_flash(const char *ptn, void *data, unsigned sz) |
| 373 | { |
| 374 | Action *a; |
| 375 | |
| 376 | a = queue_action(OP_DOWNLOAD, ""); |
| 377 | a->data = data; |
| 378 | a->size = sz; |
| 379 | a->msg = mkmsg("sending '%s' (%d KB)", ptn, sz / 1024); |
| 380 | |
| 381 | a = queue_action(OP_COMMAND, "flash:%s", ptn); |
| 382 | a->msg = mkmsg("writing '%s'", ptn); |
| 383 | } |
| 384 | |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 385 | void fb_queue_flash_sparse(const char *ptn, struct sparse_file *s, unsigned sz) |
| 386 | { |
| 387 | Action *a; |
| 388 | |
| 389 | a = queue_action(OP_DOWNLOAD_SPARSE, ""); |
| 390 | a->data = s; |
| 391 | a->size = 0; |
| 392 | a->msg = mkmsg("sending sparse '%s' (%d KB)", ptn, sz / 1024); |
| 393 | |
| 394 | a = queue_action(OP_COMMAND, "flash:%s", ptn); |
| 395 | a->msg = mkmsg("writing '%s'", ptn); |
| 396 | } |
| 397 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 398 | static int match(char *str, const char **value, unsigned count) |
| 399 | { |
| 400 | const char *val; |
| 401 | unsigned n; |
| 402 | int len; |
| 403 | |
| 404 | for (n = 0; n < count; n++) { |
| 405 | const char *val = value[n]; |
| 406 | int len = strlen(val); |
| 407 | int match; |
| 408 | |
| 409 | if ((len > 1) && (val[len-1] == '*')) { |
| 410 | len--; |
| 411 | match = !strncmp(val, str, len); |
| 412 | } else { |
| 413 | match = !strcmp(val, str); |
| 414 | } |
| 415 | |
| 416 | if (match) return 1; |
| 417 | } |
| 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | |
| 423 | |
| 424 | static int cb_check(Action *a, int status, char *resp, int invert) |
| 425 | { |
| 426 | const char **value = a->data; |
| 427 | unsigned count = a->size; |
| 428 | unsigned n; |
| 429 | int yes; |
| 430 | |
| 431 | if (status) { |
| 432 | fprintf(stderr,"FAILED (%s)\n", resp); |
| 433 | return status; |
| 434 | } |
| 435 | |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 436 | if (a->prod) { |
| 437 | if (strcmp(a->prod, cur_product) != 0) { |
| 438 | double split = now(); |
| 439 | fprintf(stderr,"IGNORE, product is %s required only for %s [%7.3fs]\n", |
| 440 | cur_product, a->prod, (split - a->start)); |
| 441 | a->start = split; |
| 442 | return 0; |
| 443 | } |
| 444 | } |
| 445 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 446 | yes = match(resp, value, count); |
| 447 | if (invert) yes = !yes; |
| 448 | |
| 449 | if (yes) { |
Daniel Sandler | cb6e22b | 2010-02-25 14:05:33 -0500 | [diff] [blame] | 450 | double split = now(); |
| 451 | fprintf(stderr,"OKAY [%7.3fs]\n", (split - a->start)); |
| 452 | a->start = split; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | fprintf(stderr,"FAILED\n\n"); |
| 457 | fprintf(stderr,"Device %s is '%s'.\n", a->cmd + 7, resp); |
| 458 | fprintf(stderr,"Update %s '%s'", |
| 459 | invert ? "rejects" : "requires", value[0]); |
| 460 | for (n = 1; n < count; n++) { |
| 461 | fprintf(stderr," or '%s'", value[n]); |
| 462 | } |
| 463 | fprintf(stderr,".\n\n"); |
| 464 | return -1; |
| 465 | } |
| 466 | |
| 467 | static int cb_require(Action *a, int status, char *resp) |
| 468 | { |
| 469 | return cb_check(a, status, resp, 0); |
| 470 | } |
| 471 | |
| 472 | static int cb_reject(Action *a, int status, char *resp) |
| 473 | { |
| 474 | return cb_check(a, status, resp, 1); |
| 475 | } |
| 476 | |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 477 | void fb_queue_require(const char *prod, const char *var, |
| 478 | int invert, unsigned nvalues, const char **value) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 479 | { |
| 480 | Action *a; |
| 481 | a = queue_action(OP_QUERY, "getvar:%s", var); |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 482 | a->prod = prod; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 483 | a->data = value; |
| 484 | a->size = nvalues; |
| 485 | a->msg = mkmsg("checking %s", var); |
| 486 | a->func = invert ? cb_reject : cb_require; |
| 487 | if (a->data == 0) die("out of memory"); |
| 488 | } |
| 489 | |
| 490 | static int cb_display(Action *a, int status, char *resp) |
| 491 | { |
| 492 | if (status) { |
| 493 | fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp); |
| 494 | return status; |
| 495 | } |
| 496 | fprintf(stderr, "%s: %s\n", (char*) a->data, resp); |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | void fb_queue_display(const char *var, const char *prettyname) |
| 501 | { |
| 502 | Action *a; |
| 503 | a = queue_action(OP_QUERY, "getvar:%s", var); |
| 504 | a->data = strdup(prettyname); |
| 505 | if (a->data == 0) die("out of memory"); |
| 506 | a->func = cb_display; |
| 507 | } |
| 508 | |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 509 | static int cb_save(Action *a, int status, char *resp) |
| 510 | { |
| 511 | if (status) { |
| 512 | fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp); |
| 513 | return status; |
| 514 | } |
| 515 | strncpy(a->data, resp, a->size); |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | void fb_queue_query_save(const char *var, char *dest, unsigned dest_size) |
| 520 | { |
| 521 | Action *a; |
| 522 | a = queue_action(OP_QUERY, "getvar:%s", var); |
| 523 | a->data = (void *)dest; |
| 524 | a->size = dest_size; |
| 525 | a->func = cb_save; |
| 526 | } |
| 527 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 528 | static int cb_do_nothing(Action *a, int status, char *resp) |
| 529 | { |
| 530 | fprintf(stderr,"\n"); |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | void fb_queue_reboot(void) |
| 535 | { |
| 536 | Action *a = queue_action(OP_COMMAND, "reboot"); |
| 537 | a->func = cb_do_nothing; |
| 538 | a->msg = "rebooting"; |
| 539 | } |
| 540 | |
| 541 | void fb_queue_command(const char *cmd, const char *msg) |
| 542 | { |
| 543 | Action *a = queue_action(OP_COMMAND, cmd); |
| 544 | a->msg = msg; |
| 545 | } |
| 546 | |
| 547 | void fb_queue_download(const char *name, void *data, unsigned size) |
| 548 | { |
| 549 | Action *a = queue_action(OP_DOWNLOAD, ""); |
| 550 | a->data = data; |
| 551 | a->size = size; |
| 552 | a->msg = mkmsg("downloading '%s'", name); |
| 553 | } |
| 554 | |
| 555 | void fb_queue_notice(const char *notice) |
| 556 | { |
| 557 | Action *a = queue_action(OP_NOTICE, ""); |
| 558 | a->data = (void*) notice; |
| 559 | } |
| 560 | |
Brian Carlstrom | eb31c0b | 2010-04-23 12:38:51 -0700 | [diff] [blame] | 561 | int fb_execute_queue(usb_handle *usb) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 562 | { |
| 563 | Action *a; |
| 564 | char resp[FB_RESPONSE_SZ+1]; |
Brian Carlstrom | eb31c0b | 2010-04-23 12:38:51 -0700 | [diff] [blame] | 565 | int status = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 566 | |
| 567 | a = action_list; |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 568 | if (!a) |
| 569 | return status; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 570 | resp[FB_RESPONSE_SZ] = 0; |
| 571 | |
Daniel Sandler | cb6e22b | 2010-02-25 14:05:33 -0500 | [diff] [blame] | 572 | double start = -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 573 | for (a = action_list; a; a = a->next) { |
Daniel Sandler | cb6e22b | 2010-02-25 14:05:33 -0500 | [diff] [blame] | 574 | a->start = now(); |
| 575 | if (start < 0) start = a->start; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 576 | if (a->msg) { |
Brian Swetland | 63e5205 | 2010-06-28 11:14:26 -0700 | [diff] [blame] | 577 | // fprintf(stderr,"%30s... ",a->msg); |
| 578 | fprintf(stderr,"%s...\n",a->msg); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 579 | } |
| 580 | if (a->op == OP_DOWNLOAD) { |
| 581 | status = fb_download_data(usb, a->data, a->size); |
| 582 | status = a->func(a, status, status ? fb_get_error() : ""); |
| 583 | if (status) break; |
| 584 | } else if (a->op == OP_COMMAND) { |
| 585 | status = fb_command(usb, a->cmd); |
| 586 | status = a->func(a, status, status ? fb_get_error() : ""); |
| 587 | if (status) break; |
| 588 | } else if (a->op == OP_QUERY) { |
| 589 | status = fb_command_response(usb, a->cmd, resp); |
| 590 | status = a->func(a, status, status ? fb_get_error() : resp); |
| 591 | if (status) break; |
| 592 | } else if (a->op == OP_NOTICE) { |
| 593 | fprintf(stderr,"%s\n",(char*)a->data); |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 594 | } else if (a->op == OP_FORMAT) { |
JP Abgrall | 30ae580 | 2012-05-07 20:25:24 -0700 | [diff] [blame] | 595 | status = fb_format(a, usb, (int)a->data); |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 596 | status = a->func(a, status, status ? fb_get_error() : ""); |
| 597 | if (status) break; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 598 | } else if (a->op == OP_DOWNLOAD_SPARSE) { |
| 599 | status = fb_download_data_sparse(usb, a->data); |
| 600 | status = a->func(a, status, status ? fb_get_error() : ""); |
| 601 | if (status) break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 602 | } else { |
| 603 | die("bogus action"); |
| 604 | } |
| 605 | } |
Daniel Sandler | cb6e22b | 2010-02-25 14:05:33 -0500 | [diff] [blame] | 606 | |
| 607 | fprintf(stderr,"finished. total time: %.3fs\n", (now() - start)); |
Brian Carlstrom | eb31c0b | 2010-04-23 12:38:51 -0700 | [diff] [blame] | 608 | return status; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 609 | } |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 610 | |
| 611 | int fb_queue_is_empty(void) |
| 612 | { |
| 613 | return (action_list == NULL); |
| 614 | } |