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