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 | |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 29 | #define round_down(a, b) \ |
| 30 | ({ typeof(a) _a = (a); typeof(b) _b = (b); _a - (_a % _b); }) |
| 31 | |
Jocelyn Bohr | 91fefad | 2017-01-27 14:17:56 -0800 | [diff] [blame] | 32 | #include <fcntl.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 <string.h> |
| 36 | #include <errno.h> |
| 37 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 38 | #include <algorithm> |
Jocelyn Bohr | 91fefad | 2017-01-27 14:17:56 -0800 | [diff] [blame] | 39 | #include <vector> |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 40 | |
Jocelyn Bohr | 91fefad | 2017-01-27 14:17:56 -0800 | [diff] [blame] | 41 | #include <android-base/file.h> |
Elliott Hughes | 2810d00 | 2016-04-25 14:31:18 -0700 | [diff] [blame] | 42 | #include <android-base/stringprintf.h> |
Jocelyn Bohr | 91fefad | 2017-01-27 14:17:56 -0800 | [diff] [blame] | 43 | #include <android-base/unique_fd.h> |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 44 | #include <sparse/sparse.h> |
Chris Fries | 0ea946c | 2017-04-12 10:25:57 -0500 | [diff] [blame] | 45 | #include <utils/FileMap.h> |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 46 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 47 | #include "fastboot.h" |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 48 | #include "transport.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 49 | |
Elliott Hughes | 2810d00 | 2016-04-25 14:31:18 -0700 | [diff] [blame] | 50 | static std::string g_error; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 51 | |
Jocelyn Bohr | 91fefad | 2017-01-27 14:17:56 -0800 | [diff] [blame] | 52 | using android::base::unique_fd; |
| 53 | using android::base::WriteStringToFile; |
| 54 | |
Elliott Hughes | 2810d00 | 2016-04-25 14:31:18 -0700 | [diff] [blame] | 55 | const std::string fb_get_error() { |
| 56 | return g_error; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Chris Fries | 6a99971 | 2017-04-04 09:52:47 -0500 | [diff] [blame] | 59 | static int64_t check_response(Transport* transport, uint32_t size, char* response) { |
Elliott Hughes | 6ebec93 | 2018-04-10 14:22:13 -0700 | [diff] [blame] | 60 | char status[FB_RESPONSE_SZ + 1]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 61 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 62 | while (true) { |
Elliott Hughes | 6ebec93 | 2018-04-10 14:22:13 -0700 | [diff] [blame] | 63 | int r = transport->Read(status, FB_RESPONSE_SZ); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 64 | if (r < 0) { |
Elliott Hughes | 2810d00 | 2016-04-25 14:31:18 -0700 | [diff] [blame] | 65 | g_error = android::base::StringPrintf("status read failed (%s)", strerror(errno)); |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 66 | transport->Close(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 67 | return -1; |
| 68 | } |
| 69 | status[r] = 0; |
| 70 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 71 | if (r < 4) { |
Elliott Hughes | 2810d00 | 2016-04-25 14:31:18 -0700 | [diff] [blame] | 72 | g_error = android::base::StringPrintf("status malformed (%d bytes)", r); |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 73 | transport->Close(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 74 | return -1; |
| 75 | } |
| 76 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 77 | if (!memcmp(status, "INFO", 4)) { |
Elliott Hughes | 234910b | 2018-04-10 15:34:19 -0700 | [diff] [blame] | 78 | verbose("received INFO \"%s\"", status + 4); |
Elliott Hughes | 855cdf8 | 2018-04-02 14:24:03 -0700 | [diff] [blame] | 79 | fprintf(stderr, "(bootloader) %s\n", status + 4); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 80 | continue; |
| 81 | } |
| 82 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 83 | if (!memcmp(status, "OKAY", 4)) { |
Elliott Hughes | 234910b | 2018-04-10 15:34:19 -0700 | [diff] [blame] | 84 | verbose("received OKAY \"%s\"", status + 4); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 85 | if (response) { |
Elliott Hughes | 855cdf8 | 2018-04-02 14:24:03 -0700 | [diff] [blame] | 86 | strcpy(response, status + 4); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 87 | } |
| 88 | return 0; |
| 89 | } |
| 90 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 91 | if (!memcmp(status, "FAIL", 4)) { |
Elliott Hughes | 234910b | 2018-04-10 15:34:19 -0700 | [diff] [blame] | 92 | verbose("received FAIL \"%s\"", status + 4); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 93 | if (r > 4) { |
Elliott Hughes | 2810d00 | 2016-04-25 14:31:18 -0700 | [diff] [blame] | 94 | g_error = android::base::StringPrintf("remote: %s", status + 4); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 95 | } else { |
Elliott Hughes | 2810d00 | 2016-04-25 14:31:18 -0700 | [diff] [blame] | 96 | g_error = "remote failure"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 97 | } |
| 98 | return -1; |
| 99 | } |
| 100 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 101 | if (!memcmp(status, "DATA", 4) && size > 0){ |
Elliott Hughes | 855cdf8 | 2018-04-02 14:24:03 -0700 | [diff] [blame] | 102 | verbose("received DATA %s", status + 4); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 103 | uint32_t dsize = strtol(status + 4, 0, 16); |
| 104 | if (dsize > size) { |
Elliott Hughes | 2810d00 | 2016-04-25 14:31:18 -0700 | [diff] [blame] | 105 | g_error = android::base::StringPrintf("data size too large (%d)", dsize); |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 106 | transport->Close(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 107 | return -1; |
| 108 | } |
| 109 | return dsize; |
| 110 | } |
| 111 | |
Elliott Hughes | 855cdf8 | 2018-04-02 14:24:03 -0700 | [diff] [blame] | 112 | verbose("received unknown status code \"%4.4s\"", status); |
Elliott Hughes | 2810d00 | 2016-04-25 14:31:18 -0700 | [diff] [blame] | 113 | g_error = "unknown status code"; |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 114 | transport->Close(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 115 | break; |
| 116 | } |
| 117 | |
| 118 | return -1; |
| 119 | } |
| 120 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 121 | static int64_t _command_start(Transport* transport, const std::string& cmd, uint32_t size, |
| 122 | char* response) { |
Elliott Hughes | 6ebec93 | 2018-04-10 14:22:13 -0700 | [diff] [blame] | 123 | if (cmd.size() > FB_COMMAND_SZ) { |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 124 | g_error = android::base::StringPrintf("command too large (%zu)", cmd.size()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 125 | return -1; |
| 126 | } |
| 127 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 128 | if (response) { |
| 129 | response[0] = 0; |
| 130 | } |
| 131 | |
Elliott Hughes | 855cdf8 | 2018-04-02 14:24:03 -0700 | [diff] [blame] | 132 | verbose("sending command \"%s\"", cmd.c_str()); |
| 133 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 134 | if (transport->Write(cmd.c_str(), cmd.size()) != static_cast<int>(cmd.size())) { |
Elliott Hughes | 2810d00 | 2016-04-25 14:31:18 -0700 | [diff] [blame] | 135 | g_error = android::base::StringPrintf("command write failed (%s)", strerror(errno)); |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 136 | transport->Close(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 137 | return -1; |
| 138 | } |
| 139 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 140 | return check_response(transport, size, response); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Jocelyn Bohr | 91fefad | 2017-01-27 14:17:56 -0800 | [diff] [blame] | 143 | static int64_t _command_write_data(Transport* transport, const void* data, uint32_t size) { |
Elliott Hughes | 855cdf8 | 2018-04-02 14:24:03 -0700 | [diff] [blame] | 144 | verbose("sending data (%" PRIu32 " bytes)", size); |
| 145 | |
Chris Fries | 6a99971 | 2017-04-04 09:52:47 -0500 | [diff] [blame] | 146 | int64_t r = transport->Write(data, size); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 147 | if (r < 0) { |
Jocelyn Bohr | 91fefad | 2017-01-27 14:17:56 -0800 | [diff] [blame] | 148 | g_error = android::base::StringPrintf("data write failure (%s)", strerror(errno)); |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 149 | transport->Close(); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 150 | return -1; |
| 151 | } |
Chris Fries | 6a99971 | 2017-04-04 09:52:47 -0500 | [diff] [blame] | 152 | if (r != static_cast<int64_t>(size)) { |
Jocelyn Bohr | 91fefad | 2017-01-27 14:17:56 -0800 | [diff] [blame] | 153 | g_error = "data write failure (short transfer)"; |
| 154 | transport->Close(); |
| 155 | return -1; |
| 156 | } |
| 157 | return r; |
| 158 | } |
| 159 | |
| 160 | static int64_t _command_read_data(Transport* transport, void* data, uint32_t size) { |
Elliott Hughes | 855cdf8 | 2018-04-02 14:24:03 -0700 | [diff] [blame] | 161 | verbose("reading data (%" PRIu32 " bytes)", size); |
| 162 | |
Jocelyn Bohr | 91fefad | 2017-01-27 14:17:56 -0800 | [diff] [blame] | 163 | int64_t r = transport->Read(data, size); |
| 164 | if (r < 0) { |
| 165 | g_error = android::base::StringPrintf("data read failure (%s)", strerror(errno)); |
| 166 | transport->Close(); |
| 167 | return -1; |
| 168 | } |
| 169 | if (r != (static_cast<int64_t>(size))) { |
| 170 | g_error = "data read failure (short transfer)"; |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 171 | transport->Close(); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 172 | return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 173 | } |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 174 | return r; |
| 175 | } |
| 176 | |
Chris Fries | 6a99971 | 2017-04-04 09:52:47 -0500 | [diff] [blame] | 177 | static int64_t _command_end(Transport* transport) { |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 178 | return check_response(transport, 0, 0) < 0 ? -1 : 0; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 179 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 180 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 181 | static int64_t _command_send(Transport* transport, const std::string& cmd, const void* data, |
| 182 | uint32_t size, char* response) { |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 183 | if (size == 0) { |
| 184 | return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 185 | } |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 186 | |
Chris Fries | 6a99971 | 2017-04-04 09:52:47 -0500 | [diff] [blame] | 187 | int64_t r = _command_start(transport, cmd, size, response); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 188 | if (r < 0) { |
| 189 | return -1; |
| 190 | } |
Jocelyn Bohr | 91fefad | 2017-01-27 14:17:56 -0800 | [diff] [blame] | 191 | r = _command_write_data(transport, data, size); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 192 | if (r < 0) { |
| 193 | return -1; |
| 194 | } |
| 195 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 196 | r = _command_end(transport); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 197 | if (r < 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 198 | return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 199 | } |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 200 | |
| 201 | return size; |
| 202 | } |
| 203 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 204 | static int64_t _command_send_fd(Transport* transport, const std::string& cmd, int fd, uint32_t size, |
Chris Fries | 0ea946c | 2017-04-12 10:25:57 -0500 | [diff] [blame] | 205 | char* response) { |
| 206 | static constexpr uint32_t MAX_MAP_SIZE = 512 * 1024 * 1024; |
| 207 | off64_t offset = 0; |
| 208 | uint32_t remaining = size; |
| 209 | |
| 210 | if (_command_start(transport, cmd, size, response) < 0) { |
| 211 | return -1; |
| 212 | } |
| 213 | |
| 214 | while (remaining) { |
| 215 | android::FileMap filemap; |
| 216 | size_t len = std::min(remaining, MAX_MAP_SIZE); |
| 217 | |
| 218 | if (!filemap.create(NULL, fd, offset, len, true)) { |
| 219 | return -1; |
| 220 | } |
| 221 | |
Jocelyn Bohr | 91fefad | 2017-01-27 14:17:56 -0800 | [diff] [blame] | 222 | if (_command_write_data(transport, filemap.getDataPtr(), len) < 0) { |
Chris Fries | 0ea946c | 2017-04-12 10:25:57 -0500 | [diff] [blame] | 223 | return -1; |
| 224 | } |
| 225 | |
| 226 | remaining -= len; |
| 227 | offset += len; |
| 228 | } |
| 229 | |
| 230 | if (_command_end(transport) < 0) { |
| 231 | return -1; |
| 232 | } |
| 233 | |
| 234 | return size; |
| 235 | } |
| 236 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 237 | static int _command_send_no_data(Transport* transport, const std::string& cmd, char* response) { |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 238 | return _command_start(transport, cmd, 0, response); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 241 | int fb_command(Transport* transport, const std::string& cmd) { |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 242 | return _command_send_no_data(transport, cmd, 0); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 243 | } |
| 244 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 245 | int fb_command_response(Transport* transport, const std::string& cmd, char* response) { |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 246 | return _command_send_no_data(transport, cmd, response); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Chris Fries | 6a99971 | 2017-04-04 09:52:47 -0500 | [diff] [blame] | 249 | int64_t fb_download_data(Transport* transport, const void* data, uint32_t size) { |
Chris Fries | 0ea946c | 2017-04-12 10:25:57 -0500 | [diff] [blame] | 250 | std::string cmd(android::base::StringPrintf("download:%08x", size)); |
| 251 | return _command_send(transport, cmd.c_str(), data, size, 0) < 0 ? -1 : 0; |
| 252 | } |
| 253 | |
| 254 | int64_t fb_download_data_fd(Transport* transport, int fd, uint32_t size) { |
| 255 | std::string cmd(android::base::StringPrintf("download:%08x", size)); |
| 256 | return _command_send_fd(transport, cmd.c_str(), fd, size, 0) < 0 ? -1 : 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 257 | } |
| 258 | |
Jocelyn Bohr | 91fefad | 2017-01-27 14:17:56 -0800 | [diff] [blame] | 259 | int64_t fb_upload_data(Transport* transport, const char* outfile) { |
| 260 | // positive return value is the upload size sent by the device |
| 261 | int64_t r = _command_start(transport, "upload", std::numeric_limits<int32_t>::max(), nullptr); |
| 262 | if (r <= 0) { |
| 263 | g_error = android::base::StringPrintf("command start failed (%s)", strerror(errno)); |
| 264 | return r; |
| 265 | } |
| 266 | |
| 267 | std::string data; |
| 268 | data.resize(r); |
| 269 | if ((r = _command_read_data(transport, &data[0], data.size())) == -1) { |
| 270 | return r; |
| 271 | } |
| 272 | |
| 273 | if (!WriteStringToFile(data, outfile, true)) { |
| 274 | g_error = android::base::StringPrintf("write to '%s' failed", outfile); |
| 275 | return -1; |
| 276 | } |
| 277 | |
| 278 | return _command_end(transport); |
| 279 | } |
| 280 | |
Tao Bao | 7d27ffd | 2018-04-23 17:24:17 -0700 | [diff] [blame] | 281 | static constexpr size_t TRANSPORT_BUF_SIZE = 1024; |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 282 | static char transport_buf[TRANSPORT_BUF_SIZE]; |
Tao Bao | 7d27ffd | 2018-04-23 17:24:17 -0700 | [diff] [blame] | 283 | static size_t transport_buf_len; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 284 | |
Tao Bao | 7d27ffd | 2018-04-23 17:24:17 -0700 | [diff] [blame] | 285 | static int fb_download_data_sparse_write(void* priv, const void* data, size_t len) { |
| 286 | const char* ptr = static_cast<const char*>(data); |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 287 | if (transport_buf_len) { |
Tao Bao | 7d27ffd | 2018-04-23 17:24:17 -0700 | [diff] [blame] | 288 | size_t to_write = std::min(TRANSPORT_BUF_SIZE - transport_buf_len, len); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 289 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 290 | memcpy(transport_buf + transport_buf_len, ptr, to_write); |
| 291 | transport_buf_len += to_write; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 292 | ptr += to_write; |
| 293 | len -= to_write; |
| 294 | } |
| 295 | |
Tao Bao | 7d27ffd | 2018-04-23 17:24:17 -0700 | [diff] [blame] | 296 | Transport* transport = static_cast<Transport*>(priv); |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 297 | if (transport_buf_len == TRANSPORT_BUF_SIZE) { |
Tao Bao | 7d27ffd | 2018-04-23 17:24:17 -0700 | [diff] [blame] | 298 | int64_t r = _command_write_data(transport, transport_buf, TRANSPORT_BUF_SIZE); |
| 299 | if (r != static_cast<int64_t>(TRANSPORT_BUF_SIZE)) { |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 300 | return -1; |
| 301 | } |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 302 | transport_buf_len = 0; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 303 | } |
| 304 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 305 | if (len > TRANSPORT_BUF_SIZE) { |
| 306 | if (transport_buf_len > 0) { |
Elliott Hughes | 2810d00 | 2016-04-25 14:31:18 -0700 | [diff] [blame] | 307 | g_error = "internal error: transport_buf not empty"; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 308 | return -1; |
| 309 | } |
Tao Bao | 7d27ffd | 2018-04-23 17:24:17 -0700 | [diff] [blame] | 310 | size_t to_write = round_down(len, TRANSPORT_BUF_SIZE); |
| 311 | int64_t r = _command_write_data(transport, ptr, to_write); |
| 312 | if (r != static_cast<int64_t>(to_write)) { |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 313 | return -1; |
| 314 | } |
| 315 | ptr += to_write; |
| 316 | len -= to_write; |
| 317 | } |
| 318 | |
| 319 | if (len > 0) { |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 320 | if (len > TRANSPORT_BUF_SIZE) { |
Elliott Hughes | 2810d00 | 2016-04-25 14:31:18 -0700 | [diff] [blame] | 321 | g_error = "internal error: too much left for transport_buf"; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 322 | return -1; |
| 323 | } |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 324 | memcpy(transport_buf, ptr, len); |
| 325 | transport_buf_len = len; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | return 0; |
| 329 | } |
| 330 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 331 | static int fb_download_data_sparse_flush(Transport* transport) { |
| 332 | if (transport_buf_len > 0) { |
Jocelyn Bohr | 91fefad | 2017-01-27 14:17:56 -0800 | [diff] [blame] | 333 | int64_t r = _command_write_data(transport, transport_buf, transport_buf_len); |
Chris Fries | 6a99971 | 2017-04-04 09:52:47 -0500 | [diff] [blame] | 334 | if (r != static_cast<int64_t>(transport_buf_len)) { |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 335 | return -1; |
| 336 | } |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 337 | transport_buf_len = 0; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 338 | } |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 339 | return 0; |
| 340 | } |
| 341 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 342 | int fb_download_data_sparse(Transport* transport, struct sparse_file* s) { |
Tao Bao | 41cf35f | 2018-04-24 10:54:21 -0700 | [diff] [blame] | 343 | int64_t size = sparse_file_len(s, true, false); |
| 344 | if (size <= 0 || size > std::numeric_limits<uint32_t>::max()) { |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 345 | return -1; |
| 346 | } |
| 347 | |
Tao Bao | 41cf35f | 2018-04-24 10:54:21 -0700 | [diff] [blame] | 348 | std::string cmd(android::base::StringPrintf("download:%08" PRIx64, size)); |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 349 | int r = _command_start(transport, cmd, size, 0); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 350 | if (r < 0) { |
| 351 | return -1; |
| 352 | } |
| 353 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 354 | r = sparse_file_callback(s, true, false, fb_download_data_sparse_write, transport); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 355 | if (r < 0) { |
| 356 | return -1; |
| 357 | } |
| 358 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 359 | r = fb_download_data_sparse_flush(transport); |
Jeremy Compostella | 9f0d6bd | 2015-02-22 10:47:16 +0100 | [diff] [blame] | 360 | if (r < 0) { |
| 361 | return -1; |
| 362 | } |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 363 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 364 | return _command_end(transport); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 365 | } |