blob: 262a08ee61c6786dcadba80d8b2b15cc7333323e [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Deepa Dinamani26bc2d32013-03-15 13:17:16 -07005 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
6 *
Brian Swetland9c4c0752009-01-25 16:23:50 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
Deepa Dinamani26bc2d32013-03-15 13:17:16 -070010 * * 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 Swetland9c4c0752009-01-25 16:23:50 -080013 * notice, this list of conditions and the following disclaimer in
Deepa Dinamani26bc2d32013-03-15 13:17:16 -070014 * the documentation and/or other materials provided with the
Brian Swetland9c4c0752009-01-25 16:23:50 -080015 * 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 Dinamani26bc2d32013-03-15 13:17:16 -070024 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
Brian Swetland9c4c0752009-01-25 16:23:50 -080025 * 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 Dinamani0bf2f442012-10-19 11:41:06 -070034#include <platform.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080035#include <kernel/thread.h>
36#include <kernel/event.h>
37#include <dev/udc.h>
Channagoud Kadabia0930b92013-04-16 15:14:37 -070038#include "fastboot.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080039
Hanumant Singh108cdc62012-12-11 16:48:49 -080040#define MAX_USBFS_BULK_SIZE (32 * 1024)
Shashank Mittal6a5609f2011-08-04 15:51:59 -070041
Brian Swetland9c4c0752009-01-25 16:23:50 -080042void boot_linux(void *bootimg, unsigned sz);
43
44/* todo: give lk strtoul and nuke this */
45static 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
72struct 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
79struct fastboot_var {
80 struct fastboot_var *next;
81 const char *name;
82 const char *value;
83};
84
85static struct fastboot_cmd *cmdlist;
86
87void 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
101static struct fastboot_var *varlist;
102
103void 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
116static event_t usb_online;
117static event_t txn_done;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800118static struct udc_endpoint *in, *out;
119static struct udc_request *req;
120int txn_status;
121
122static void *download_base;
123static unsigned download_max;
124static 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
131static unsigned fastboot_state = STATE_OFFLINE;
132
133static void req_complete(struct udc_request *req, unsigned actual, int status)
134{
135 txn_status = status;
136 req->length = actual;
Hanumant Singh108cdc62012-12-11 16:48:49 -0800137
Brian Swetland9c4c0752009-01-25 16:23:50 -0800138 event_signal(&txn_done, 0);
139}
140
141static 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 Mittal1cc65b02011-12-20 15:30:17 -0800152 xfer = (len > MAX_USBFS_BULK_SIZE) ? MAX_USBFS_BULK_SIZE : len;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700153 req->buf = PA((addr_t)buf);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800154 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 Singh108cdc62012-12-11 16:48:49 -0800175 /*
176 * Force reload of buffer from memory
177 * since transaction is complete now.
178 */
179 arch_invalidate_cache_range(_buf, count);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800180 return count;
181
182oops:
183 fastboot_state = STATE_ERROR;
184 return -1;
185}
186
187static int usb_write(void *buf, unsigned len)
188{
189 int r;
190
191 if (fastboot_state == STATE_ERROR)
192 goto oops;
193
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700194 req->buf = PA((addr_t)buf);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800195 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
209oops:
210 fastboot_state = STATE_ERROR;
211 return -1;
212}
213
214void fastboot_ack(const char *code, const char *reason)
215{
Deepa Dinamani26bc2d32013-03-15 13:17:16 -0700216 STACKBUF_DMA_ALIGN(response, MAX_RSP_SIZE);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800217
218 if (fastboot_state != STATE_COMMAND)
219 return;
220
221 if (reason == 0)
222 reason = "";
223
Shashank Mittal6a5609f2011-08-04 15:51:59 -0700224 snprintf(response, MAX_RSP_SIZE, "%s%s", code, reason);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800225 fastboot_state = STATE_COMPLETE;
226
227 usb_write(response, strlen(response));
228
229}
230
Shashank Mittal6a5609f2011-08-04 15:51:59 -0700231void fastboot_info(const char *reason)
232{
Deepa Dinamani26bc2d32013-03-15 13:17:16 -0700233 STACKBUF_DMA_ALIGN(response, MAX_RSP_SIZE);
Shashank Mittal6a5609f2011-08-04 15:51:59 -0700234
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 Swetland9c4c0752009-01-25 16:23:50 -0800246void fastboot_fail(const char *reason)
247{
248 fastboot_ack("FAIL", reason);
249}
250
251void fastboot_okay(const char *info)
252{
253 fastboot_ack("OKAY", info);
254}
255
256static 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
269static void cmd_download(const char *arg, void *data, unsigned sz)
270{
Deepa Dinamani26bc2d32013-03-15 13:17:16 -0700271 STACKBUF_DMA_ALIGN(response, MAX_RSP_SIZE);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800272 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 Dudanif63d02f2011-10-01 08:29:53 -0700281 snprintf(response, MAX_RSP_SIZE, "DATA%08x", len);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800282 if (usb_write(response, strlen(response)) < 0)
283 return;
284
285 r = usb_read(download_base, len);
Greg Griscod6250552011-06-29 14:40:23 -0700286 if ((r < 0) || ((unsigned) r != len)) {
Brian Swetland9c4c0752009-01-25 16:23:50 -0800287 fastboot_state = STATE_ERROR;
288 return;
289 }
290 download_size = len;
291 fastboot_okay("");
292}
293
294static void fastboot_command_loop(void)
295{
296 struct fastboot_cmd *cmd;
297 int r;
298 dprintf(INFO,"fastboot: processing commands\n");
299
Deepa Dinamani26bc2d32013-03-15 13:17:16 -0700300 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 Swetland9c4c0752009-01-25 16:23:50 -0800306again:
307 while (fastboot_state != STATE_ERROR) {
Shashank Mittal6a5609f2011-08-04 15:51:59 -0700308 r = usb_read(buffer, MAX_RSP_SIZE);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800309 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 Dinamani26bc2d32013-03-15 13:17:16 -0700329 free(buffer);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800330}
331
332static int fastboot_handler(void *arg)
333{
334 for (;;) {
335 event_wait(&usb_online);
336 fastboot_command_loop();
337 }
338 return 0;
339}
340
341static 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
348static struct udc_endpoint *fastboot_endpoints[2];
349
350static 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
360int 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);
neetid32ba8472011-12-07 16:34:06 -0800393 if (!thr)
394 {
395 goto fail_alloc_in;
396 }
Brian Swetland9c4c0752009-01-25 16:23:50 -0800397 thread_resume(thr);
398 return 0;
399
400fail_udc_register:
401 udc_request_free(req);
402fail_alloc_req:
403 udc_endpoint_free(out);
404fail_alloc_out:
405 udc_endpoint_free(in);
406fail_alloc_in:
407 return -1;
408}