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