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" |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 30 | #include "fs.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> |
Elliott Hughes | 3ab8b85 | 2015-08-25 19:34:13 -0700 | [diff] [blame] | 33 | #include <stdarg.h> |
Mark Salyzyn | 5957c1f | 2014-04-30 14:05:28 -0700 | [diff] [blame] | 34 | #include <stdio.h> |
| 35 | #include <stdlib.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> |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 38 | #include <sys/types.h> |
| 39 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 40 | |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 41 | #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 42 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 | #define OP_DOWNLOAD 1 |
| 44 | #define OP_COMMAND 2 |
| 45 | #define OP_QUERY 3 |
| 46 | #define OP_NOTICE 4 |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 47 | #define OP_DOWNLOAD_SPARSE 5 |
| 48 | #define OP_WAIT_FOR_DISCONNECT 6 |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 49 | |
| 50 | typedef struct Action Action; |
| 51 | |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 52 | #define CMD_SIZE 64 |
| 53 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 54 | struct Action { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 55 | unsigned op; |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 56 | Action* next; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 57 | |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 58 | char cmd[CMD_SIZE]; |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 59 | const char* prod; |
| 60 | void* data; |
| 61 | |
| 62 | // The protocol only supports 32-bit sizes, so you'll have to break |
| 63 | // anything larger into chunks. |
| 64 | uint32_t size; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 65 | |
| 66 | const char *msg; |
Elliott Hughes | b3748de | 2015-06-23 20:27:58 -0700 | [diff] [blame] | 67 | int (*func)(Action* a, int status, const char* resp); |
Daniel Sandler | cb6e22b | 2010-02-25 14:05:33 -0500 | [diff] [blame] | 68 | |
| 69 | double start; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | static Action *action_list = 0; |
| 73 | static Action *action_last = 0; |
| 74 | |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 75 | |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 76 | |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 77 | |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame^] | 78 | bool fb_getvar(usb_handle* usb, const std::string& key, std::string* value) { |
| 79 | std::string cmd = "getvar:"; |
| 80 | cmd += key; |
Colin Cross | 80f2d03 | 2012-05-24 18:24:53 -0700 | [diff] [blame] | 81 | |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame^] | 82 | char buf[FB_RESPONSE_SZ + 1]; |
| 83 | memset(buf, 0, sizeof(buf)); |
| 84 | if (fb_command_response(usb, cmd.c_str(), buf)) { |
| 85 | return false; |
| 86 | } |
| 87 | *value = buf; |
| 88 | return true; |
Colin Cross | 80f2d03 | 2012-05-24 18:24:53 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 91 | |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame^] | 92 | // Return true if this partition is supported by the fastboot format command. |
| 93 | // It is also used to determine if we should first erase a partition before |
| 94 | // flashing it with an ext4 filesystem. See needs_erase() |
| 95 | // |
| 96 | // Not all devices report the filesystem type, so don't report any errors, |
| 97 | // just return false. |
| 98 | bool fb_format_supported(usb_handle *usb, const char *partition, const char *type_override) { |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 99 | if (type_override) { |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame^] | 100 | return fs_get_generator(type_override) != nullptr; |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 101 | } |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame^] | 102 | std::string partition_type; |
| 103 | if (!fb_getvar(usb, std::string("partition-type:") + partition, &partition_type)) { |
| 104 | return false; |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 105 | } |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame^] | 106 | return fs_get_generator(partition_type.c_str()) != nullptr; |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Elliott Hughes | b3748de | 2015-06-23 20:27:58 -0700 | [diff] [blame] | 109 | static int cb_default(Action* a, int status, const char* resp) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 110 | if (status) { |
| 111 | fprintf(stderr,"FAILED (%s)\n", resp); |
| 112 | } else { |
Daniel Sandler | cb6e22b | 2010-02-25 14:05:33 -0500 | [diff] [blame] | 113 | double split = now(); |
| 114 | fprintf(stderr,"OKAY [%7.3fs]\n", (split - a->start)); |
| 115 | a->start = split; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 116 | } |
| 117 | return status; |
| 118 | } |
| 119 | |
| 120 | static Action *queue_action(unsigned op, const char *fmt, ...) |
| 121 | { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 122 | va_list ap; |
Bruce Beare | 50b3995 | 2010-07-15 08:52:01 -0700 | [diff] [blame] | 123 | size_t cmdsize; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 124 | |
Elliott Hughes | b3748de | 2015-06-23 20:27:58 -0700 | [diff] [blame] | 125 | Action* a = reinterpret_cast<Action*>(calloc(1, sizeof(Action))); |
| 126 | if (a == nullptr) die("out of memory"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 127 | |
| 128 | va_start(ap, fmt); |
Bruce Beare | 50b3995 | 2010-07-15 08:52:01 -0700 | [diff] [blame] | 129 | cmdsize = vsnprintf(a->cmd, sizeof(a->cmd), fmt, ap); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 130 | va_end(ap); |
| 131 | |
Bruce Beare | 50b3995 | 2010-07-15 08:52:01 -0700 | [diff] [blame] | 132 | if (cmdsize >= sizeof(a->cmd)) { |
| 133 | free(a); |
| 134 | die("Command length (%d) exceeds maximum size (%d)", cmdsize, sizeof(a->cmd)); |
| 135 | } |
| 136 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 137 | if (action_last) { |
| 138 | action_last->next = a; |
| 139 | } else { |
| 140 | action_list = a; |
| 141 | } |
| 142 | action_last = a; |
| 143 | a->op = op; |
| 144 | a->func = cb_default; |
Daniel Sandler | cb6e22b | 2010-02-25 14:05:33 -0500 | [diff] [blame] | 145 | |
| 146 | a->start = -1; |
| 147 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 148 | return a; |
| 149 | } |
| 150 | |
| 151 | void fb_queue_erase(const char *ptn) |
| 152 | { |
| 153 | Action *a; |
| 154 | a = queue_action(OP_COMMAND, "erase:%s", ptn); |
| 155 | a->msg = mkmsg("erasing '%s'", ptn); |
| 156 | } |
| 157 | |
Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 158 | void fb_queue_flash(const char *ptn, void *data, unsigned sz) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 159 | { |
| 160 | Action *a; |
| 161 | |
| 162 | a = queue_action(OP_DOWNLOAD, ""); |
| 163 | a->data = data; |
| 164 | a->size = sz; |
| 165 | a->msg = mkmsg("sending '%s' (%d KB)", ptn, sz / 1024); |
| 166 | |
| 167 | a = queue_action(OP_COMMAND, "flash:%s", ptn); |
| 168 | a->msg = mkmsg("writing '%s'", ptn); |
| 169 | } |
| 170 | |
Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 171 | void fb_queue_flash_sparse(const char *ptn, struct sparse_file *s, unsigned sz) |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 172 | { |
| 173 | Action *a; |
| 174 | |
| 175 | a = queue_action(OP_DOWNLOAD_SPARSE, ""); |
| 176 | a->data = s; |
| 177 | a->size = 0; |
| 178 | a->msg = mkmsg("sending sparse '%s' (%d KB)", ptn, sz / 1024); |
| 179 | |
| 180 | a = queue_action(OP_COMMAND, "flash:%s", ptn); |
| 181 | a->msg = mkmsg("writing '%s'", ptn); |
| 182 | } |
| 183 | |
Elliott Hughes | b3748de | 2015-06-23 20:27:58 -0700 | [diff] [blame] | 184 | static int match(const char* str, const char** value, unsigned count) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 185 | unsigned n; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 186 | |
| 187 | for (n = 0; n < count; n++) { |
| 188 | const char *val = value[n]; |
| 189 | int len = strlen(val); |
| 190 | int match; |
| 191 | |
| 192 | if ((len > 1) && (val[len-1] == '*')) { |
| 193 | len--; |
| 194 | match = !strncmp(val, str, len); |
| 195 | } else { |
| 196 | match = !strcmp(val, str); |
| 197 | } |
| 198 | |
| 199 | if (match) return 1; |
| 200 | } |
| 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | |
| 206 | |
Elliott Hughes | b3748de | 2015-06-23 20:27:58 -0700 | [diff] [blame] | 207 | static int cb_check(Action* a, int status, const char* resp, int invert) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 208 | { |
Elliott Hughes | b3748de | 2015-06-23 20:27:58 -0700 | [diff] [blame] | 209 | const char** value = reinterpret_cast<const char**>(a->data); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 210 | unsigned count = a->size; |
| 211 | unsigned n; |
| 212 | int yes; |
| 213 | |
| 214 | if (status) { |
| 215 | fprintf(stderr,"FAILED (%s)\n", resp); |
| 216 | return status; |
| 217 | } |
| 218 | |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 219 | if (a->prod) { |
| 220 | if (strcmp(a->prod, cur_product) != 0) { |
| 221 | double split = now(); |
| 222 | fprintf(stderr,"IGNORE, product is %s required only for %s [%7.3fs]\n", |
| 223 | cur_product, a->prod, (split - a->start)); |
| 224 | a->start = split; |
| 225 | return 0; |
| 226 | } |
| 227 | } |
| 228 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 229 | yes = match(resp, value, count); |
| 230 | if (invert) yes = !yes; |
| 231 | |
| 232 | if (yes) { |
Daniel Sandler | cb6e22b | 2010-02-25 14:05:33 -0500 | [diff] [blame] | 233 | double split = now(); |
| 234 | fprintf(stderr,"OKAY [%7.3fs]\n", (split - a->start)); |
| 235 | a->start = split; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | fprintf(stderr,"FAILED\n\n"); |
| 240 | fprintf(stderr,"Device %s is '%s'.\n", a->cmd + 7, resp); |
| 241 | fprintf(stderr,"Update %s '%s'", |
| 242 | invert ? "rejects" : "requires", value[0]); |
| 243 | for (n = 1; n < count; n++) { |
| 244 | fprintf(stderr," or '%s'", value[n]); |
| 245 | } |
| 246 | fprintf(stderr,".\n\n"); |
| 247 | return -1; |
| 248 | } |
| 249 | |
Elliott Hughes | b3748de | 2015-06-23 20:27:58 -0700 | [diff] [blame] | 250 | static int cb_require(Action*a, int status, const char* resp) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 251 | return cb_check(a, status, resp, 0); |
| 252 | } |
| 253 | |
Elliott Hughes | b3748de | 2015-06-23 20:27:58 -0700 | [diff] [blame] | 254 | static int cb_reject(Action* a, int status, const char* resp) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 255 | return cb_check(a, status, resp, 1); |
| 256 | } |
| 257 | |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 258 | void fb_queue_require(const char *prod, const char *var, |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 259 | bool invert, size_t nvalues, const char **value) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 260 | { |
| 261 | Action *a; |
| 262 | a = queue_action(OP_QUERY, "getvar:%s", var); |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 263 | a->prod = prod; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 264 | a->data = value; |
| 265 | a->size = nvalues; |
| 266 | a->msg = mkmsg("checking %s", var); |
| 267 | a->func = invert ? cb_reject : cb_require; |
Elliott Hughes | b3748de | 2015-06-23 20:27:58 -0700 | [diff] [blame] | 268 | if (a->data == nullptr) die("out of memory"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 269 | } |
| 270 | |
Elliott Hughes | b3748de | 2015-06-23 20:27:58 -0700 | [diff] [blame] | 271 | static int cb_display(Action* a, int status, const char* resp) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 272 | if (status) { |
| 273 | fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp); |
| 274 | return status; |
| 275 | } |
| 276 | fprintf(stderr, "%s: %s\n", (char*) a->data, resp); |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | void fb_queue_display(const char *var, const char *prettyname) |
| 281 | { |
| 282 | Action *a; |
| 283 | a = queue_action(OP_QUERY, "getvar:%s", var); |
| 284 | a->data = strdup(prettyname); |
Elliott Hughes | b3748de | 2015-06-23 20:27:58 -0700 | [diff] [blame] | 285 | if (a->data == nullptr) die("out of memory"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 286 | a->func = cb_display; |
| 287 | } |
| 288 | |
Elliott Hughes | b3748de | 2015-06-23 20:27:58 -0700 | [diff] [blame] | 289 | static int cb_save(Action* a, int status, const char* resp) { |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 290 | if (status) { |
| 291 | fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp); |
| 292 | return status; |
| 293 | } |
Elliott Hughes | b3748de | 2015-06-23 20:27:58 -0700 | [diff] [blame] | 294 | strncpy(reinterpret_cast<char*>(a->data), resp, a->size); |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | void fb_queue_query_save(const char *var, char *dest, unsigned dest_size) |
| 299 | { |
| 300 | Action *a; |
| 301 | a = queue_action(OP_QUERY, "getvar:%s", var); |
| 302 | a->data = (void *)dest; |
| 303 | a->size = dest_size; |
| 304 | a->func = cb_save; |
| 305 | } |
| 306 | |
Elliott Hughes | b3748de | 2015-06-23 20:27:58 -0700 | [diff] [blame] | 307 | static int cb_do_nothing(Action*, int , const char*) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 308 | fprintf(stderr,"\n"); |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | void fb_queue_reboot(void) |
| 313 | { |
| 314 | Action *a = queue_action(OP_COMMAND, "reboot"); |
| 315 | a->func = cb_do_nothing; |
| 316 | a->msg = "rebooting"; |
| 317 | } |
| 318 | |
| 319 | void fb_queue_command(const char *cmd, const char *msg) |
| 320 | { |
| 321 | Action *a = queue_action(OP_COMMAND, cmd); |
| 322 | a->msg = msg; |
| 323 | } |
| 324 | |
| 325 | void fb_queue_download(const char *name, void *data, unsigned size) |
| 326 | { |
| 327 | Action *a = queue_action(OP_DOWNLOAD, ""); |
| 328 | a->data = data; |
| 329 | a->size = size; |
| 330 | a->msg = mkmsg("downloading '%s'", name); |
| 331 | } |
| 332 | |
| 333 | void fb_queue_notice(const char *notice) |
| 334 | { |
| 335 | Action *a = queue_action(OP_NOTICE, ""); |
| 336 | a->data = (void*) notice; |
| 337 | } |
| 338 | |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 339 | void fb_queue_wait_for_disconnect(void) |
| 340 | { |
| 341 | queue_action(OP_WAIT_FOR_DISCONNECT, ""); |
| 342 | } |
| 343 | |
Brian Carlstrom | eb31c0b | 2010-04-23 12:38:51 -0700 | [diff] [blame] | 344 | int fb_execute_queue(usb_handle *usb) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 345 | { |
| 346 | Action *a; |
| 347 | char resp[FB_RESPONSE_SZ+1]; |
Brian Carlstrom | eb31c0b | 2010-04-23 12:38:51 -0700 | [diff] [blame] | 348 | int status = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 349 | |
| 350 | a = action_list; |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 351 | if (!a) |
| 352 | return status; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 353 | resp[FB_RESPONSE_SZ] = 0; |
| 354 | |
Daniel Sandler | cb6e22b | 2010-02-25 14:05:33 -0500 | [diff] [blame] | 355 | double start = -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 356 | for (a = action_list; a; a = a->next) { |
Daniel Sandler | cb6e22b | 2010-02-25 14:05:33 -0500 | [diff] [blame] | 357 | a->start = now(); |
| 358 | if (start < 0) start = a->start; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 359 | if (a->msg) { |
Brian Swetland | 63e5205 | 2010-06-28 11:14:26 -0700 | [diff] [blame] | 360 | // fprintf(stderr,"%30s... ",a->msg); |
| 361 | fprintf(stderr,"%s...\n",a->msg); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 362 | } |
| 363 | if (a->op == OP_DOWNLOAD) { |
| 364 | status = fb_download_data(usb, a->data, a->size); |
| 365 | status = a->func(a, status, status ? fb_get_error() : ""); |
| 366 | if (status) break; |
| 367 | } else if (a->op == OP_COMMAND) { |
| 368 | status = fb_command(usb, a->cmd); |
| 369 | status = a->func(a, status, status ? fb_get_error() : ""); |
| 370 | if (status) break; |
| 371 | } else if (a->op == OP_QUERY) { |
| 372 | status = fb_command_response(usb, a->cmd, resp); |
| 373 | status = a->func(a, status, status ? fb_get_error() : resp); |
| 374 | if (status) break; |
| 375 | } else if (a->op == OP_NOTICE) { |
| 376 | fprintf(stderr,"%s\n",(char*)a->data); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 377 | } else if (a->op == OP_DOWNLOAD_SPARSE) { |
Elliott Hughes | b3748de | 2015-06-23 20:27:58 -0700 | [diff] [blame] | 378 | status = fb_download_data_sparse(usb, reinterpret_cast<sparse_file*>(a->data)); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 379 | status = a->func(a, status, status ? fb_get_error() : ""); |
| 380 | if (status) break; |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 381 | } else if (a->op == OP_WAIT_FOR_DISCONNECT) { |
| 382 | usb_wait_for_disconnect(usb); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 383 | } else { |
| 384 | die("bogus action"); |
| 385 | } |
| 386 | } |
Daniel Sandler | cb6e22b | 2010-02-25 14:05:33 -0500 | [diff] [blame] | 387 | |
| 388 | fprintf(stderr,"finished. total time: %.3fs\n", (now() - start)); |
Brian Carlstrom | eb31c0b | 2010-04-23 12:38:51 -0700 | [diff] [blame] | 389 | return status; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 390 | } |