Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2009, Google Inc. |
| 3 | * All rights reserved. |
| 4 | * |
Deepa Dinamani | 26bc2d3 | 2013-03-15 13:17:16 -0700 | [diff] [blame] | 5 | * Copyright (c) 2013, The Linux Foundation. All rights reserved. |
| 6 | * |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
Deepa Dinamani | 26bc2d3 | 2013-03-15 13:17:16 -0700 | [diff] [blame] | 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above copyright |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 13 | * notice, this list of conditions and the following disclaimer in |
Deepa Dinamani | 26bc2d3 | 2013-03-15 13:17:16 -0700 | [diff] [blame] | 14 | * the documentation and/or other materials provided with the |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 15 | * distribution. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 20 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 21 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 23 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
Deepa Dinamani | 26bc2d3 | 2013-03-15 13:17:16 -0700 | [diff] [blame] | 24 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 25 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 27 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 28 | * SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #include <debug.h> |
| 32 | #include <string.h> |
| 33 | #include <stdlib.h> |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 34 | #include <platform.h> |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 35 | #include <kernel/thread.h> |
| 36 | #include <kernel/event.h> |
| 37 | #include <dev/udc.h> |
Channagoud Kadabi | a0930b9 | 2013-04-16 15:14:37 -0700 | [diff] [blame] | 38 | #include "fastboot.h" |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 39 | |
Hanumant Singh | 108cdc6 | 2012-12-11 16:48:49 -0800 | [diff] [blame] | 40 | #define MAX_USBFS_BULK_SIZE (32 * 1024) |
Shashank Mittal | 6a5609f | 2011-08-04 15:51:59 -0700 | [diff] [blame] | 41 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 42 | void boot_linux(void *bootimg, unsigned sz); |
| 43 | |
| 44 | /* todo: give lk strtoul and nuke this */ |
| 45 | static unsigned hex2unsigned(const char *x) |
| 46 | { |
| 47 | unsigned n = 0; |
| 48 | |
| 49 | while(*x) { |
| 50 | switch(*x) { |
| 51 | case '0': case '1': case '2': case '3': case '4': |
| 52 | case '5': case '6': case '7': case '8': case '9': |
| 53 | n = (n << 4) | (*x - '0'); |
| 54 | break; |
| 55 | case 'a': case 'b': case 'c': |
| 56 | case 'd': case 'e': case 'f': |
| 57 | n = (n << 4) | (*x - 'a' + 10); |
| 58 | break; |
| 59 | case 'A': case 'B': case 'C': |
| 60 | case 'D': case 'E': case 'F': |
| 61 | n = (n << 4) | (*x - 'A' + 10); |
| 62 | break; |
| 63 | default: |
| 64 | return n; |
| 65 | } |
| 66 | x++; |
| 67 | } |
| 68 | |
| 69 | return n; |
| 70 | } |
| 71 | |
| 72 | struct fastboot_cmd { |
| 73 | struct fastboot_cmd *next; |
| 74 | const char *prefix; |
| 75 | unsigned prefix_len; |
| 76 | void (*handle)(const char *arg, void *data, unsigned sz); |
| 77 | }; |
| 78 | |
| 79 | struct fastboot_var { |
| 80 | struct fastboot_var *next; |
| 81 | const char *name; |
| 82 | const char *value; |
| 83 | }; |
| 84 | |
| 85 | static struct fastboot_cmd *cmdlist; |
| 86 | |
| 87 | void fastboot_register(const char *prefix, |
| 88 | void (*handle)(const char *arg, void *data, unsigned sz)) |
| 89 | { |
| 90 | struct fastboot_cmd *cmd; |
| 91 | cmd = malloc(sizeof(*cmd)); |
| 92 | if (cmd) { |
| 93 | cmd->prefix = prefix; |
| 94 | cmd->prefix_len = strlen(prefix); |
| 95 | cmd->handle = handle; |
| 96 | cmd->next = cmdlist; |
| 97 | cmdlist = cmd; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | static struct fastboot_var *varlist; |
| 102 | |
| 103 | void fastboot_publish(const char *name, const char *value) |
| 104 | { |
| 105 | struct fastboot_var *var; |
| 106 | var = malloc(sizeof(*var)); |
| 107 | if (var) { |
| 108 | var->name = name; |
| 109 | var->value = value; |
| 110 | var->next = varlist; |
| 111 | varlist = var; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | |
| 116 | static event_t usb_online; |
| 117 | static event_t txn_done; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 118 | static struct udc_endpoint *in, *out; |
| 119 | static struct udc_request *req; |
| 120 | int txn_status; |
| 121 | |
| 122 | static void *download_base; |
| 123 | static unsigned download_max; |
| 124 | static unsigned download_size; |
| 125 | |
| 126 | #define STATE_OFFLINE 0 |
| 127 | #define STATE_COMMAND 1 |
| 128 | #define STATE_COMPLETE 2 |
| 129 | #define STATE_ERROR 3 |
| 130 | |
| 131 | static unsigned fastboot_state = STATE_OFFLINE; |
| 132 | |
| 133 | static void req_complete(struct udc_request *req, unsigned actual, int status) |
| 134 | { |
| 135 | txn_status = status; |
| 136 | req->length = actual; |
Hanumant Singh | 108cdc6 | 2012-12-11 16:48:49 -0800 | [diff] [blame] | 137 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 138 | event_signal(&txn_done, 0); |
| 139 | } |
| 140 | |
| 141 | static int usb_read(void *_buf, unsigned len) |
| 142 | { |
| 143 | int r; |
| 144 | unsigned xfer; |
| 145 | unsigned char *buf = _buf; |
| 146 | int count = 0; |
| 147 | |
| 148 | if (fastboot_state == STATE_ERROR) |
| 149 | goto oops; |
| 150 | |
| 151 | while (len > 0) { |
Shashank Mittal | 1cc65b0 | 2011-12-20 15:30:17 -0800 | [diff] [blame] | 152 | xfer = (len > MAX_USBFS_BULK_SIZE) ? MAX_USBFS_BULK_SIZE : len; |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 153 | req->buf = PA((addr_t)buf); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 154 | req->length = xfer; |
| 155 | req->complete = req_complete; |
| 156 | r = udc_request_queue(out, req); |
| 157 | if (r < 0) { |
| 158 | dprintf(INFO, "usb_read() queue failed\n"); |
| 159 | goto oops; |
| 160 | } |
| 161 | event_wait(&txn_done); |
| 162 | |
| 163 | if (txn_status < 0) { |
| 164 | dprintf(INFO, "usb_read() transaction failed\n"); |
| 165 | goto oops; |
| 166 | } |
| 167 | |
| 168 | count += req->length; |
| 169 | buf += req->length; |
| 170 | len -= req->length; |
| 171 | |
| 172 | /* short transfer? */ |
| 173 | if (req->length != xfer) break; |
| 174 | } |
Hanumant Singh | 108cdc6 | 2012-12-11 16:48:49 -0800 | [diff] [blame] | 175 | /* |
| 176 | * Force reload of buffer from memory |
| 177 | * since transaction is complete now. |
| 178 | */ |
| 179 | arch_invalidate_cache_range(_buf, count); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 180 | return count; |
| 181 | |
| 182 | oops: |
| 183 | fastboot_state = STATE_ERROR; |
| 184 | return -1; |
| 185 | } |
| 186 | |
| 187 | static int usb_write(void *buf, unsigned len) |
| 188 | { |
| 189 | int r; |
| 190 | |
| 191 | if (fastboot_state == STATE_ERROR) |
| 192 | goto oops; |
| 193 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 194 | req->buf = PA((addr_t)buf); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 195 | req->length = len; |
| 196 | req->complete = req_complete; |
| 197 | r = udc_request_queue(in, req); |
| 198 | if (r < 0) { |
| 199 | dprintf(INFO, "usb_write() queue failed\n"); |
| 200 | goto oops; |
| 201 | } |
| 202 | event_wait(&txn_done); |
| 203 | if (txn_status < 0) { |
| 204 | dprintf(INFO, "usb_write() transaction failed\n"); |
| 205 | goto oops; |
| 206 | } |
| 207 | return req->length; |
| 208 | |
| 209 | oops: |
| 210 | fastboot_state = STATE_ERROR; |
| 211 | return -1; |
| 212 | } |
| 213 | |
| 214 | void fastboot_ack(const char *code, const char *reason) |
| 215 | { |
Deepa Dinamani | 26bc2d3 | 2013-03-15 13:17:16 -0700 | [diff] [blame] | 216 | STACKBUF_DMA_ALIGN(response, MAX_RSP_SIZE); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 217 | |
| 218 | if (fastboot_state != STATE_COMMAND) |
| 219 | return; |
| 220 | |
| 221 | if (reason == 0) |
| 222 | reason = ""; |
| 223 | |
Shashank Mittal | 6a5609f | 2011-08-04 15:51:59 -0700 | [diff] [blame] | 224 | snprintf(response, MAX_RSP_SIZE, "%s%s", code, reason); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 225 | fastboot_state = STATE_COMPLETE; |
| 226 | |
| 227 | usb_write(response, strlen(response)); |
| 228 | |
| 229 | } |
| 230 | |
Shashank Mittal | 6a5609f | 2011-08-04 15:51:59 -0700 | [diff] [blame] | 231 | void fastboot_info(const char *reason) |
| 232 | { |
Deepa Dinamani | 26bc2d3 | 2013-03-15 13:17:16 -0700 | [diff] [blame] | 233 | STACKBUF_DMA_ALIGN(response, MAX_RSP_SIZE); |
Shashank Mittal | 6a5609f | 2011-08-04 15:51:59 -0700 | [diff] [blame] | 234 | |
| 235 | if (fastboot_state != STATE_COMMAND) |
| 236 | return; |
| 237 | |
| 238 | if (reason == 0) |
| 239 | return; |
| 240 | |
| 241 | snprintf(response, MAX_RSP_SIZE, "INFO%s", reason); |
| 242 | |
| 243 | usb_write(response, strlen(response)); |
| 244 | } |
| 245 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 246 | void fastboot_fail(const char *reason) |
| 247 | { |
| 248 | fastboot_ack("FAIL", reason); |
| 249 | } |
| 250 | |
| 251 | void fastboot_okay(const char *info) |
| 252 | { |
| 253 | fastboot_ack("OKAY", info); |
| 254 | } |
| 255 | |
| 256 | static void cmd_getvar(const char *arg, void *data, unsigned sz) |
| 257 | { |
| 258 | struct fastboot_var *var; |
| 259 | |
| 260 | for (var = varlist; var; var = var->next) { |
| 261 | if (!strcmp(var->name, arg)) { |
| 262 | fastboot_okay(var->value); |
| 263 | return; |
| 264 | } |
| 265 | } |
| 266 | fastboot_okay(""); |
| 267 | } |
| 268 | |
| 269 | static void cmd_download(const char *arg, void *data, unsigned sz) |
| 270 | { |
Deepa Dinamani | 26bc2d3 | 2013-03-15 13:17:16 -0700 | [diff] [blame] | 271 | STACKBUF_DMA_ALIGN(response, MAX_RSP_SIZE); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 272 | unsigned len = hex2unsigned(arg); |
| 273 | int r; |
| 274 | |
| 275 | download_size = 0; |
| 276 | if (len > download_max) { |
| 277 | fastboot_fail("data too large"); |
| 278 | return; |
| 279 | } |
| 280 | |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 281 | snprintf(response, MAX_RSP_SIZE, "DATA%08x", len); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 282 | if (usb_write(response, strlen(response)) < 0) |
| 283 | return; |
| 284 | |
| 285 | r = usb_read(download_base, len); |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 286 | if ((r < 0) || ((unsigned) r != len)) { |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 287 | fastboot_state = STATE_ERROR; |
| 288 | return; |
| 289 | } |
| 290 | download_size = len; |
| 291 | fastboot_okay(""); |
| 292 | } |
| 293 | |
| 294 | static void fastboot_command_loop(void) |
| 295 | { |
| 296 | struct fastboot_cmd *cmd; |
| 297 | int r; |
| 298 | dprintf(INFO,"fastboot: processing commands\n"); |
| 299 | |
Deepa Dinamani | 26bc2d3 | 2013-03-15 13:17:16 -0700 | [diff] [blame] | 300 | uint8_t *buffer = (uint8_t *)memalign(CACHE_LINE, ROUNDUP(4096, CACHE_LINE)); |
| 301 | if (!buffer) |
| 302 | { |
| 303 | dprintf(CRITICAL, "Could not allocate memory for fastboot buffer\n."); |
| 304 | ASSERT(0); |
| 305 | } |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 306 | again: |
| 307 | while (fastboot_state != STATE_ERROR) { |
Shashank Mittal | 6a5609f | 2011-08-04 15:51:59 -0700 | [diff] [blame] | 308 | r = usb_read(buffer, MAX_RSP_SIZE); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 309 | if (r < 0) break; |
| 310 | buffer[r] = 0; |
| 311 | dprintf(INFO,"fastboot: %s\n", buffer); |
| 312 | |
| 313 | for (cmd = cmdlist; cmd; cmd = cmd->next) { |
| 314 | if (memcmp(buffer, cmd->prefix, cmd->prefix_len)) |
| 315 | continue; |
| 316 | fastboot_state = STATE_COMMAND; |
| 317 | cmd->handle((const char*) buffer + cmd->prefix_len, |
| 318 | (void*) download_base, download_size); |
| 319 | if (fastboot_state == STATE_COMMAND) |
| 320 | fastboot_fail("unknown reason"); |
| 321 | goto again; |
| 322 | } |
| 323 | |
| 324 | fastboot_fail("unknown command"); |
| 325 | |
| 326 | } |
| 327 | fastboot_state = STATE_OFFLINE; |
| 328 | dprintf(INFO,"fastboot: oops!\n"); |
Deepa Dinamani | 26bc2d3 | 2013-03-15 13:17:16 -0700 | [diff] [blame] | 329 | free(buffer); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | static int fastboot_handler(void *arg) |
| 333 | { |
| 334 | for (;;) { |
| 335 | event_wait(&usb_online); |
| 336 | fastboot_command_loop(); |
| 337 | } |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | static void fastboot_notify(struct udc_gadget *gadget, unsigned event) |
| 342 | { |
| 343 | if (event == UDC_EVENT_ONLINE) { |
| 344 | event_signal(&usb_online, 0); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | static struct udc_endpoint *fastboot_endpoints[2]; |
| 349 | |
| 350 | static struct udc_gadget fastboot_gadget = { |
| 351 | .notify = fastboot_notify, |
| 352 | .ifc_class = 0xff, |
| 353 | .ifc_subclass = 0x42, |
| 354 | .ifc_protocol = 0x03, |
| 355 | .ifc_endpoints = 2, |
| 356 | .ifc_string = "fastboot", |
| 357 | .ept = fastboot_endpoints, |
| 358 | }; |
| 359 | |
| 360 | int fastboot_init(void *base, unsigned size) |
| 361 | { |
| 362 | thread_t *thr; |
| 363 | dprintf(INFO, "fastboot_init()\n"); |
| 364 | |
| 365 | download_base = base; |
| 366 | download_max = size; |
| 367 | |
| 368 | event_init(&usb_online, 0, EVENT_FLAG_AUTOUNSIGNAL); |
| 369 | event_init(&txn_done, 0, EVENT_FLAG_AUTOUNSIGNAL); |
| 370 | |
| 371 | in = udc_endpoint_alloc(UDC_TYPE_BULK_IN, 512); |
| 372 | if (!in) |
| 373 | goto fail_alloc_in; |
| 374 | out = udc_endpoint_alloc(UDC_TYPE_BULK_OUT, 512); |
| 375 | if (!out) |
| 376 | goto fail_alloc_out; |
| 377 | |
| 378 | fastboot_endpoints[0] = in; |
| 379 | fastboot_endpoints[1] = out; |
| 380 | |
| 381 | req = udc_request_alloc(); |
| 382 | if (!req) |
| 383 | goto fail_alloc_req; |
| 384 | |
| 385 | if (udc_register_gadget(&fastboot_gadget)) |
| 386 | goto fail_udc_register; |
| 387 | |
| 388 | fastboot_register("getvar:", cmd_getvar); |
| 389 | fastboot_register("download:", cmd_download); |
| 390 | fastboot_publish("version", "0.5"); |
| 391 | |
| 392 | thr = thread_create("fastboot", fastboot_handler, 0, DEFAULT_PRIORITY, 4096); |
neetid | 32ba847 | 2011-12-07 16:34:06 -0800 | [diff] [blame] | 393 | if (!thr) |
| 394 | { |
| 395 | goto fail_alloc_in; |
| 396 | } |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 397 | thread_resume(thr); |
| 398 | return 0; |
| 399 | |
| 400 | fail_udc_register: |
| 401 | udc_request_free(req); |
| 402 | fail_alloc_req: |
| 403 | udc_endpoint_free(out); |
| 404 | fail_alloc_out: |
| 405 | udc_endpoint_free(in); |
| 406 | fail_alloc_in: |
| 407 | return -1; |
| 408 | } |