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 | */ |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 28 | #include "engine.h" |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 29 | |
Colin Cross | 81c632e | 2013-01-23 19:13:43 -0800 | [diff] [blame] | 30 | #include <errno.h> |
Elliott Hughes | 3ab8b85 | 2015-08-25 19:34:13 -0700 | [diff] [blame] | 31 | #include <stdarg.h> |
Mark Salyzyn | 5957c1f | 2014-04-30 14:05:28 -0700 | [diff] [blame] | 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 34 | #include <string.h> |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 35 | #include <sys/stat.h> |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 36 | #include <sys/types.h> |
| 37 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 38 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 39 | #include <memory> |
| 40 | #include <vector> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 41 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 42 | #include <android-base/stringprintf.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 | |
Jerry Zhang | 769a9c1 | 2018-05-15 17:02:50 -0700 | [diff] [blame] | 44 | #include "constants.h" |
Elliott Hughes | 11ff345 | 2018-04-09 13:58:41 -0700 | [diff] [blame] | 45 | #include "transport.h" |
| 46 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 47 | enum Op { |
| 48 | OP_DOWNLOAD, |
| 49 | OP_COMMAND, |
| 50 | OP_QUERY, |
| 51 | OP_NOTICE, |
| 52 | OP_DOWNLOAD_SPARSE, |
| 53 | OP_WAIT_FOR_DISCONNECT, |
| 54 | OP_DOWNLOAD_FD, |
| 55 | OP_UPLOAD, |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 56 | }; |
| 57 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 58 | struct Action { |
| 59 | Action(Op op, const std::string& cmd) : op(op), cmd(cmd) {} |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 60 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 61 | Op op; |
| 62 | std::string cmd; |
| 63 | std::string msg; |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 64 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 65 | std::string product; |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 66 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 67 | void* data = nullptr; |
| 68 | // The protocol only supports 32-bit sizes, so you'll have to break |
| 69 | // anything larger into multiple chunks. |
| 70 | uint32_t size = 0; |
| 71 | |
| 72 | int fd = -1; |
| 73 | |
| 74 | int (*func)(Action& a, int status, const char* resp) = nullptr; |
| 75 | |
| 76 | double start = -1; |
| 77 | }; |
| 78 | |
| 79 | static std::vector<std::unique_ptr<Action>> action_list; |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 80 | static fastboot::FastBootDriver* fb = nullptr; |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 81 | |
David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 82 | static constexpr char kStatusFormat[] = "%-50s "; |
| 83 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 84 | void fb_init(fastboot::FastBootDriver& fbi) { |
| 85 | fb = &fbi; |
| 86 | auto cb = [](std::string& info) { fprintf(stderr, "(bootloader) %s\n", info.c_str()); }; |
| 87 | fb->SetInfoCallback(cb); |
| 88 | } |
Colin Cross | 80f2d03 | 2012-05-24 18:24:53 -0700 | [diff] [blame] | 89 | |
David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 90 | void fb_reinit(Transport* transport) { |
David Anderson | 03de645 | 2018-09-04 14:32:54 -0700 | [diff] [blame^] | 91 | if (Transport* old_transport = fb->set_transport(transport)) { |
| 92 | delete old_transport; |
| 93 | } |
David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 96 | const std::string fb_get_error() { |
| 97 | return fb->Error(); |
| 98 | } |
| 99 | |
| 100 | bool fb_getvar(const std::string& key, std::string* value) { |
| 101 | return !fb->GetVar(key, value); |
Colin Cross | 80f2d03 | 2012-05-24 18:24:53 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 104 | 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] | 105 | if (status) { |
| 106 | fprintf(stderr,"FAILED (%s)\n", resp); |
| 107 | } else { |
Daniel Sandler | cb6e22b | 2010-02-25 14:05:33 -0500 | [diff] [blame] | 108 | double split = now(); |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 109 | fprintf(stderr, "OKAY [%7.3fs]\n", (split - a.start)); |
| 110 | a.start = split; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 111 | } |
| 112 | return status; |
| 113 | } |
| 114 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 115 | static Action& queue_action(Op op, const std::string& cmd) { |
| 116 | std::unique_ptr<Action> a{new Action(op, cmd)}; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 117 | a->func = cb_default; |
Daniel Sandler | cb6e22b | 2010-02-25 14:05:33 -0500 | [diff] [blame] | 118 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 119 | action_list.push_back(std::move(a)); |
| 120 | return *action_list.back(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 123 | void fb_set_active(const std::string& slot) { |
Jerry Zhang | 769a9c1 | 2018-05-15 17:02:50 -0700 | [diff] [blame] | 124 | Action& a = queue_action(OP_COMMAND, FB_CMD_SET_ACTIVE ":" + slot); |
Elliott Hughes | f238d87 | 2018-03-29 14:46:29 -0700 | [diff] [blame] | 125 | a.msg = "Setting current slot to '" + slot + "'"; |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 128 | void fb_queue_erase(const std::string& partition) { |
Jerry Zhang | 769a9c1 | 2018-05-15 17:02:50 -0700 | [diff] [blame] | 129 | Action& a = queue_action(OP_COMMAND, FB_CMD_ERASE ":" + partition); |
Elliott Hughes | f238d87 | 2018-03-29 14:46:29 -0700 | [diff] [blame] | 130 | a.msg = "Erasing '" + partition + "'"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 133 | void fb_queue_flash_fd(const std::string& partition, int fd, uint32_t sz) { |
| 134 | Action& a = queue_action(OP_DOWNLOAD_FD, ""); |
| 135 | a.fd = fd; |
| 136 | a.size = sz; |
Elliott Hughes | f238d87 | 2018-03-29 14:46:29 -0700 | [diff] [blame] | 137 | a.msg = android::base::StringPrintf("Sending '%s' (%u KB)", partition.c_str(), sz / 1024); |
Chris Fries | 0ea946c | 2017-04-12 10:25:57 -0500 | [diff] [blame] | 138 | |
Jerry Zhang | 769a9c1 | 2018-05-15 17:02:50 -0700 | [diff] [blame] | 139 | Action& b = queue_action(OP_COMMAND, FB_CMD_FLASH ":" + partition); |
Elliott Hughes | f238d87 | 2018-03-29 14:46:29 -0700 | [diff] [blame] | 140 | b.msg = "Writing '" + partition + "'"; |
Chris Fries | 0ea946c | 2017-04-12 10:25:57 -0500 | [diff] [blame] | 141 | } |
| 142 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 143 | void fb_queue_flash(const std::string& partition, void* data, uint32_t sz) { |
| 144 | Action& a = queue_action(OP_DOWNLOAD, ""); |
| 145 | a.data = data; |
| 146 | a.size = sz; |
Elliott Hughes | f238d87 | 2018-03-29 14:46:29 -0700 | [diff] [blame] | 147 | a.msg = android::base::StringPrintf("Sending '%s' (%u KB)", partition.c_str(), sz / 1024); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 148 | |
Jerry Zhang | 769a9c1 | 2018-05-15 17:02:50 -0700 | [diff] [blame] | 149 | Action& b = queue_action(OP_COMMAND, FB_CMD_FLASH ":" + partition); |
Elliott Hughes | f238d87 | 2018-03-29 14:46:29 -0700 | [diff] [blame] | 150 | b.msg = "Writing '" + partition + "'"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 153 | void fb_queue_flash_sparse(const std::string& partition, struct sparse_file* s, uint32_t sz, |
| 154 | size_t current, size_t total) { |
| 155 | Action& a = queue_action(OP_DOWNLOAD_SPARSE, ""); |
| 156 | a.data = s; |
| 157 | a.size = 0; |
Elliott Hughes | f238d87 | 2018-03-29 14:46:29 -0700 | [diff] [blame] | 158 | a.msg = android::base::StringPrintf("Sending sparse '%s' %zu/%zu (%u KB)", partition.c_str(), |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 159 | current, total, sz / 1024); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 160 | |
Jerry Zhang | 769a9c1 | 2018-05-15 17:02:50 -0700 | [diff] [blame] | 161 | Action& b = queue_action(OP_COMMAND, FB_CMD_FLASH ":" + partition); |
Elliott Hughes | f238d87 | 2018-03-29 14:46:29 -0700 | [diff] [blame] | 162 | b.msg = android::base::StringPrintf("Writing sparse '%s' %zu/%zu", partition.c_str(), current, |
| 163 | total); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 164 | } |
| 165 | |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 166 | void fb_queue_create_partition(const std::string& partition, const std::string& size) { |
| 167 | Action& a = queue_action(OP_COMMAND, FB_CMD_CREATE_PARTITION ":" + partition + ":" + size); |
| 168 | a.msg = "Creating '" + partition + "'"; |
| 169 | } |
| 170 | |
| 171 | void fb_queue_delete_partition(const std::string& partition) { |
| 172 | Action& a = queue_action(OP_COMMAND, FB_CMD_DELETE_PARTITION ":" + partition); |
| 173 | a.msg = "Deleting '" + partition + "'"; |
| 174 | } |
| 175 | |
| 176 | void fb_queue_resize_partition(const std::string& partition, const std::string& size) { |
| 177 | Action& a = queue_action(OP_COMMAND, FB_CMD_RESIZE_PARTITION ":" + partition + ":" + size); |
| 178 | a.msg = "Resizing '" + partition + "'"; |
| 179 | } |
| 180 | |
Elliott Hughes | b3748de | 2015-06-23 20:27:58 -0700 | [diff] [blame] | 181 | 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] | 182 | unsigned n; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 183 | |
| 184 | for (n = 0; n < count; n++) { |
| 185 | const char *val = value[n]; |
| 186 | int len = strlen(val); |
| 187 | int match; |
| 188 | |
| 189 | if ((len > 1) && (val[len-1] == '*')) { |
| 190 | len--; |
| 191 | match = !strncmp(val, str, len); |
| 192 | } else { |
| 193 | match = !strcmp(val, str); |
| 194 | } |
| 195 | |
| 196 | if (match) return 1; |
| 197 | } |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 202 | static int cb_check(Action& a, int status, const char* resp, int invert) { |
| 203 | const char** value = reinterpret_cast<const char**>(a.data); |
| 204 | unsigned count = a.size; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 205 | unsigned n; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 206 | |
| 207 | if (status) { |
| 208 | fprintf(stderr,"FAILED (%s)\n", resp); |
| 209 | return status; |
| 210 | } |
| 211 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 212 | if (!a.product.empty()) { |
| 213 | if (a.product != cur_product) { |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 214 | double split = now(); |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 215 | fprintf(stderr, "IGNORE, product is %s required only for %s [%7.3fs]\n", cur_product, |
| 216 | a.product.c_str(), (split - a.start)); |
| 217 | a.start = split; |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 218 | return 0; |
| 219 | } |
| 220 | } |
| 221 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 222 | int yes = match(resp, value, count); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 223 | if (invert) yes = !yes; |
| 224 | |
| 225 | if (yes) { |
Daniel Sandler | cb6e22b | 2010-02-25 14:05:33 -0500 | [diff] [blame] | 226 | double split = now(); |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 227 | fprintf(stderr, "OKAY [%7.3fs]\n", (split - a.start)); |
| 228 | a.start = split; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 229 | return 0; |
| 230 | } |
| 231 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 232 | fprintf(stderr, "FAILED\n\n"); |
| 233 | fprintf(stderr, "Device %s is '%s'.\n", a.cmd.c_str() + 7, resp); |
| 234 | fprintf(stderr, "Update %s '%s'", invert ? "rejects" : "requires", value[0]); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 235 | for (n = 1; n < count; n++) { |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 236 | fprintf(stderr, " or '%s'", value[n]); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 237 | } |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 238 | fprintf(stderr, ".\n\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 239 | return -1; |
| 240 | } |
| 241 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 242 | 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] | 243 | return cb_check(a, status, resp, 0); |
| 244 | } |
| 245 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 246 | 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] | 247 | return cb_check(a, status, resp, 1); |
| 248 | } |
| 249 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 250 | void fb_queue_require(const std::string& product, const std::string& var, bool invert, |
| 251 | size_t nvalues, const char** values) { |
Jerry Zhang | 769a9c1 | 2018-05-15 17:02:50 -0700 | [diff] [blame] | 252 | Action& a = queue_action(OP_QUERY, FB_CMD_GETVAR ":" + var); |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 253 | a.product = product; |
| 254 | a.data = values; |
| 255 | a.size = nvalues; |
| 256 | a.msg = "Checking " + var; |
| 257 | a.func = invert ? cb_reject : cb_require; |
| 258 | if (a.data == nullptr) die("out of memory"); |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 261 | 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] | 262 | if (status) { |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 263 | fprintf(stderr, "%s FAILED (%s)\n", a.cmd.c_str(), resp); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 264 | return status; |
| 265 | } |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 266 | fprintf(stderr, "%s: %s\n", static_cast<const char*>(a.data), resp); |
| 267 | free(static_cast<char*>(a.data)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 268 | return 0; |
| 269 | } |
| 270 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 271 | void fb_queue_display(const std::string& label, const std::string& var) { |
Jerry Zhang | 769a9c1 | 2018-05-15 17:02:50 -0700 | [diff] [blame] | 272 | Action& a = queue_action(OP_QUERY, FB_CMD_GETVAR ":" + var); |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 273 | a.data = xstrdup(label.c_str()); |
| 274 | a.func = cb_display; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 275 | } |
| 276 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 277 | static int cb_save(Action& a, int status, const char* resp) { |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 278 | if (status) { |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 279 | fprintf(stderr, "%s FAILED (%s)\n", a.cmd.c_str(), resp); |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 280 | return status; |
| 281 | } |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 282 | strncpy(reinterpret_cast<char*>(a.data), resp, a.size); |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 283 | return 0; |
| 284 | } |
| 285 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 286 | void fb_queue_query_save(const std::string& var, char* dest, uint32_t dest_size) { |
Jerry Zhang | 769a9c1 | 2018-05-15 17:02:50 -0700 | [diff] [blame] | 287 | Action& a = queue_action(OP_QUERY, FB_CMD_GETVAR ":" + var); |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 288 | a.data = dest; |
| 289 | a.size = dest_size; |
| 290 | a.func = cb_save; |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 293 | static int cb_do_nothing(Action&, int, const char*) { |
| 294 | fprintf(stderr, "\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 295 | return 0; |
| 296 | } |
| 297 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 298 | void fb_queue_reboot() { |
Jerry Zhang | 769a9c1 | 2018-05-15 17:02:50 -0700 | [diff] [blame] | 299 | Action& a = queue_action(OP_COMMAND, FB_CMD_REBOOT); |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 300 | a.func = cb_do_nothing; |
Elliott Hughes | f238d87 | 2018-03-29 14:46:29 -0700 | [diff] [blame] | 301 | a.msg = "Rebooting"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 302 | } |
| 303 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 304 | void fb_queue_command(const std::string& cmd, const std::string& msg) { |
| 305 | Action& a = queue_action(OP_COMMAND, cmd); |
| 306 | a.msg = msg; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 309 | void fb_queue_download(const std::string& name, void* data, uint32_t size) { |
| 310 | Action& a = queue_action(OP_DOWNLOAD, ""); |
| 311 | a.data = data; |
| 312 | a.size = size; |
| 313 | a.msg = "Downloading '" + name + "'"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 316 | void fb_queue_download_fd(const std::string& name, int fd, uint32_t sz) { |
| 317 | Action& a = queue_action(OP_DOWNLOAD_FD, ""); |
| 318 | a.fd = fd; |
| 319 | a.size = sz; |
Elliott Hughes | f238d87 | 2018-03-29 14:46:29 -0700 | [diff] [blame] | 320 | a.msg = android::base::StringPrintf("Sending '%s' (%u KB)", name.c_str(), sz / 1024); |
Jocelyn Bohr | 98cc283 | 2017-01-26 19:20:53 -0800 | [diff] [blame] | 321 | } |
| 322 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 323 | void fb_queue_upload(const std::string& outfile) { |
| 324 | Action& a = queue_action(OP_UPLOAD, ""); |
| 325 | a.data = xstrdup(outfile.c_str()); |
| 326 | a.msg = "Uploading '" + outfile + "'"; |
Jocelyn Bohr | 91fefad | 2017-01-27 14:17:56 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 329 | void fb_queue_notice(const std::string& notice) { |
| 330 | Action& a = queue_action(OP_NOTICE, ""); |
| 331 | a.msg = notice; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 332 | } |
| 333 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 334 | void fb_queue_wait_for_disconnect() { |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 335 | queue_action(OP_WAIT_FOR_DISCONNECT, ""); |
| 336 | } |
| 337 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 338 | int64_t fb_execute_queue() { |
Chris Fries | 6a99971 | 2017-04-04 09:52:47 -0500 | [diff] [blame] | 339 | int64_t status = 0; |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 340 | for (auto& a : action_list) { |
Daniel Sandler | cb6e22b | 2010-02-25 14:05:33 -0500 | [diff] [blame] | 341 | a->start = now(); |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 342 | if (!a->msg.empty()) { |
David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 343 | fprintf(stderr, kStatusFormat, a->msg.c_str()); |
Elliott Hughes | 855cdf8 | 2018-04-02 14:24:03 -0700 | [diff] [blame] | 344 | verbose("\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 345 | } |
| 346 | if (a->op == OP_DOWNLOAD) { |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 347 | char* cbuf = static_cast<char*>(a->data); |
| 348 | status = fb->Download(cbuf, a->size); |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 349 | status = a->func(*a, status, status ? fb_get_error().c_str() : ""); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 350 | if (status) break; |
Chris Fries | 0ea946c | 2017-04-12 10:25:57 -0500 | [diff] [blame] | 351 | } else if (a->op == OP_DOWNLOAD_FD) { |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 352 | status = fb->Download(a->fd, a->size); |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 353 | status = a->func(*a, status, status ? fb_get_error().c_str() : ""); |
Chris Fries | 0ea946c | 2017-04-12 10:25:57 -0500 | [diff] [blame] | 354 | if (status) break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 355 | } else if (a->op == OP_COMMAND) { |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 356 | status = fb->RawCommand(a->cmd); |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 357 | status = a->func(*a, status, status ? fb_get_error().c_str() : ""); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 358 | if (status) break; |
| 359 | } else if (a->op == OP_QUERY) { |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 360 | std::string resp; |
| 361 | status = fb->RawCommand(a->cmd, &resp); |
| 362 | status = a->func(*a, status, status ? fb_get_error().c_str() : resp.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 363 | if (status) break; |
| 364 | } else if (a->op == OP_NOTICE) { |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 365 | // We already showed the notice because it's in `Action::msg`. |
Elliott Hughes | f238d87 | 2018-03-29 14:46:29 -0700 | [diff] [blame] | 366 | fprintf(stderr, "\n"); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 367 | } else if (a->op == OP_DOWNLOAD_SPARSE) { |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 368 | status = fb->Download(reinterpret_cast<sparse_file*>(a->data)); |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 369 | status = a->func(*a, status, status ? fb_get_error().c_str() : ""); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 370 | if (status) break; |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 371 | } else if (a->op == OP_WAIT_FOR_DISCONNECT) { |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 372 | fb->WaitForDisconnect(); |
Jocelyn Bohr | 91fefad | 2017-01-27 14:17:56 -0800 | [diff] [blame] | 373 | } else if (a->op == OP_UPLOAD) { |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 374 | status = fb->Upload(reinterpret_cast<const char*>(a->data)); |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 375 | status = a->func(*a, status, status ? fb_get_error().c_str() : ""); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 376 | } else { |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 377 | die("unknown action: %d", a->op); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 378 | } |
| 379 | } |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 380 | action_list.clear(); |
Brian Carlstrom | eb31c0b | 2010-04-23 12:38:51 -0700 | [diff] [blame] | 381 | return status; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 382 | } |
David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 383 | |
| 384 | bool fb_reboot_to_userspace() { |
| 385 | // First ensure that the queue is flushed. |
| 386 | fb_execute_queue(); |
| 387 | |
| 388 | fprintf(stderr, kStatusFormat, "Rebooting to userspace fastboot"); |
| 389 | verbose("\n"); |
| 390 | |
| 391 | if (fb->RebootTo("fastboot") != fastboot::RetCode::SUCCESS) { |
| 392 | fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str()); |
| 393 | return false; |
| 394 | } |
| 395 | fprintf(stderr, "OKAY\n"); |
| 396 | |
David Anderson | 03de645 | 2018-09-04 14:32:54 -0700 | [diff] [blame^] | 397 | fb_reinit(nullptr); |
David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 398 | return true; |
| 399 | } |