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