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 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
| 35 | #include <errno.h> |
| 36 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 37 | #include <algorithm> |
| 38 | |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 39 | #include <sparse/sparse.h> |
| 40 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 41 | #include "fastboot.h" |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 42 | #include "transport.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 | |
| 44 | static char ERROR[128]; |
| 45 | |
| 46 | char *fb_get_error(void) |
| 47 | { |
| 48 | return ERROR; |
| 49 | } |
| 50 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 51 | static int check_response(Transport* transport, uint32_t size, char* response) { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 52 | char status[65]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 53 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 54 | while (true) { |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 55 | int r = transport->Read(status, 64); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 56 | if (r < 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 57 | sprintf(ERROR, "status read failed (%s)", strerror(errno)); |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 58 | transport->Close(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 59 | return -1; |
| 60 | } |
| 61 | status[r] = 0; |
| 62 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 63 | if (r < 4) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 64 | sprintf(ERROR, "status malformed (%d bytes)", r); |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 65 | transport->Close(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 66 | return -1; |
| 67 | } |
| 68 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 69 | if (!memcmp(status, "INFO", 4)) { |
Brian Swetland | 63e5205 | 2010-06-28 11:14:26 -0700 | [diff] [blame] | 70 | fprintf(stderr,"(bootloader) %s\n", status + 4); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 71 | continue; |
| 72 | } |
| 73 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 74 | if (!memcmp(status, "OKAY", 4)) { |
| 75 | if (response) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 76 | strcpy(response, (char*) status + 4); |
| 77 | } |
| 78 | return 0; |
| 79 | } |
| 80 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 81 | if (!memcmp(status, "FAIL", 4)) { |
| 82 | if (r > 4) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 83 | sprintf(ERROR, "remote: %s", status + 4); |
| 84 | } else { |
| 85 | strcpy(ERROR, "remote failure"); |
| 86 | } |
| 87 | return -1; |
| 88 | } |
| 89 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 90 | if (!memcmp(status, "DATA", 4) && size > 0){ |
| 91 | uint32_t dsize = strtol(status + 4, 0, 16); |
| 92 | if (dsize > size) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 93 | strcpy(ERROR, "data size too large"); |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 94 | transport->Close(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 95 | return -1; |
| 96 | } |
| 97 | return dsize; |
| 98 | } |
| 99 | |
| 100 | strcpy(ERROR,"unknown status code"); |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 101 | transport->Close(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 102 | break; |
| 103 | } |
| 104 | |
| 105 | return -1; |
| 106 | } |
| 107 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 108 | static int _command_start(Transport* transport, const char* cmd, uint32_t size, char* response) { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 109 | size_t cmdsize = strlen(cmd); |
| 110 | if (cmdsize > 64) { |
| 111 | sprintf(ERROR, "command too large"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 112 | return -1; |
| 113 | } |
| 114 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 115 | if (response) { |
| 116 | response[0] = 0; |
| 117 | } |
| 118 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 119 | if (transport->Write(cmd, cmdsize) != static_cast<int>(cmdsize)) { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 120 | sprintf(ERROR, "command write failed (%s)", strerror(errno)); |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 121 | transport->Close(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 122 | return -1; |
| 123 | } |
| 124 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 125 | return check_response(transport, size, response); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 126 | } |
| 127 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 128 | static int _command_data(Transport* transport, const void* data, uint32_t size) { |
| 129 | int r = transport->Write(data, size); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 130 | if (r < 0) { |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 131 | sprintf(ERROR, "data transfer failure (%s)", strerror(errno)); |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 132 | transport->Close(); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 133 | return -1; |
| 134 | } |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 135 | if (r != ((int) size)) { |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 136 | sprintf(ERROR, "data transfer failure (short transfer)"); |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 137 | transport->Close(); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 138 | return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 139 | } |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 140 | return r; |
| 141 | } |
| 142 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 143 | static int _command_end(Transport* transport) { |
| 144 | return check_response(transport, 0, 0) < 0 ? -1 : 0; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 145 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 146 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 147 | static int _command_send(Transport* transport, const char* cmd, const void* data, uint32_t size, |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 148 | char* response) { |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 149 | if (size == 0) { |
| 150 | return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 151 | } |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 152 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 153 | int r = _command_start(transport, cmd, size, response); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 154 | if (r < 0) { |
| 155 | return -1; |
| 156 | } |
| 157 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 158 | r = _command_data(transport, data, size); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 159 | if (r < 0) { |
| 160 | return -1; |
| 161 | } |
| 162 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 163 | r = _command_end(transport); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 164 | if (r < 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 165 | return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 166 | } |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 167 | |
| 168 | return size; |
| 169 | } |
| 170 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 171 | static int _command_send_no_data(Transport* transport, const char* cmd, char* response) { |
| 172 | return _command_start(transport, cmd, 0, response); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 173 | } |
| 174 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 175 | int fb_command(Transport* transport, const char* cmd) { |
| 176 | return _command_send_no_data(transport, cmd, 0); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 177 | } |
| 178 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 179 | int fb_command_response(Transport* transport, const char* cmd, char* response) { |
| 180 | return _command_send_no_data(transport, cmd, response); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 181 | } |
| 182 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 183 | int fb_download_data(Transport* transport, const void* data, uint32_t size) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 184 | char cmd[64]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 185 | sprintf(cmd, "download:%08x", size); |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 186 | return _command_send(transport, cmd, data, size, 0) < 0 ? -1 : 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 187 | } |
| 188 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 189 | #define TRANSPORT_BUF_SIZE 1024 |
| 190 | static char transport_buf[TRANSPORT_BUF_SIZE]; |
| 191 | static int transport_buf_len; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 192 | |
| 193 | static int fb_download_data_sparse_write(void *priv, const void *data, int len) |
| 194 | { |
| 195 | int r; |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 196 | Transport* transport = reinterpret_cast<Transport*>(priv); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 197 | int to_write; |
Elliott Hughes | b3748de | 2015-06-23 20:27:58 -0700 | [diff] [blame] | 198 | const char* ptr = reinterpret_cast<const char*>(data); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 199 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 200 | if (transport_buf_len) { |
| 201 | to_write = std::min(TRANSPORT_BUF_SIZE - transport_buf_len, len); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 202 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 203 | memcpy(transport_buf + transport_buf_len, ptr, to_write); |
| 204 | transport_buf_len += to_write; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 205 | ptr += to_write; |
| 206 | len -= to_write; |
| 207 | } |
| 208 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 209 | if (transport_buf_len == TRANSPORT_BUF_SIZE) { |
| 210 | r = _command_data(transport, transport_buf, TRANSPORT_BUF_SIZE); |
| 211 | if (r != TRANSPORT_BUF_SIZE) { |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 212 | return -1; |
| 213 | } |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 214 | transport_buf_len = 0; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 215 | } |
| 216 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 217 | if (len > TRANSPORT_BUF_SIZE) { |
| 218 | if (transport_buf_len > 0) { |
| 219 | sprintf(ERROR, "internal error: transport_buf not empty\n"); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 220 | return -1; |
| 221 | } |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 222 | to_write = round_down(len, TRANSPORT_BUF_SIZE); |
| 223 | r = _command_data(transport, ptr, to_write); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 224 | if (r != to_write) { |
| 225 | return -1; |
| 226 | } |
| 227 | ptr += to_write; |
| 228 | len -= to_write; |
| 229 | } |
| 230 | |
| 231 | if (len > 0) { |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 232 | if (len > TRANSPORT_BUF_SIZE) { |
| 233 | sprintf(ERROR, "internal error: too much left for transport_buf\n"); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 234 | return -1; |
| 235 | } |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 236 | memcpy(transport_buf, ptr, len); |
| 237 | transport_buf_len = len; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 243 | static int fb_download_data_sparse_flush(Transport* transport) { |
| 244 | if (transport_buf_len > 0) { |
| 245 | if (_command_data(transport, transport_buf, transport_buf_len) != transport_buf_len) { |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 246 | return -1; |
| 247 | } |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 248 | transport_buf_len = 0; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 249 | } |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 250 | return 0; |
| 251 | } |
| 252 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 253 | int fb_download_data_sparse(Transport* transport, struct sparse_file* s) { |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 254 | int size = sparse_file_len(s, true, false); |
| 255 | if (size <= 0) { |
| 256 | return -1; |
| 257 | } |
| 258 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 259 | char cmd[64]; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 260 | sprintf(cmd, "download:%08x", size); |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 261 | int r = _command_start(transport, cmd, size, 0); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 262 | if (r < 0) { |
| 263 | return -1; |
| 264 | } |
| 265 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 266 | 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] | 267 | if (r < 0) { |
| 268 | return -1; |
| 269 | } |
| 270 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 271 | r = fb_download_data_sparse_flush(transport); |
Jeremy Compostella | 9f0d6bd | 2015-02-22 10:47:16 +0100 | [diff] [blame] | 272 | if (r < 0) { |
| 273 | return -1; |
| 274 | } |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 275 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 276 | return _command_end(transport); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 277 | } |