blob: d2479188c0baea5ca30dd6b63840c914bbfaec6a [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
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
12 * the documentation and/or other materials provided with the
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
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
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
29#include <debug.h>
30#include <string.h>
31#include <stdlib.h>
32#include <kernel/thread.h>
33#include <kernel/event.h>
34#include <dev/udc.h>
35
36void boot_linux(void *bootimg, unsigned sz);
37
38/* todo: give lk strtoul and nuke this */
39static unsigned hex2unsigned(const char *x)
40{
41 unsigned n = 0;
42
43 while(*x) {
44 switch(*x) {
45 case '0': case '1': case '2': case '3': case '4':
46 case '5': case '6': case '7': case '8': case '9':
47 n = (n << 4) | (*x - '0');
48 break;
49 case 'a': case 'b': case 'c':
50 case 'd': case 'e': case 'f':
51 n = (n << 4) | (*x - 'a' + 10);
52 break;
53 case 'A': case 'B': case 'C':
54 case 'D': case 'E': case 'F':
55 n = (n << 4) | (*x - 'A' + 10);
56 break;
57 default:
58 return n;
59 }
60 x++;
61 }
62
63 return n;
64}
65
66struct fastboot_cmd {
67 struct fastboot_cmd *next;
68 const char *prefix;
69 unsigned prefix_len;
70 void (*handle)(const char *arg, void *data, unsigned sz);
71};
72
73struct fastboot_var {
74 struct fastboot_var *next;
75 const char *name;
76 const char *value;
77};
78
79static struct fastboot_cmd *cmdlist;
80
81void fastboot_register(const char *prefix,
82 void (*handle)(const char *arg, void *data, unsigned sz))
83{
84 struct fastboot_cmd *cmd;
85 cmd = malloc(sizeof(*cmd));
86 if (cmd) {
87 cmd->prefix = prefix;
88 cmd->prefix_len = strlen(prefix);
89 cmd->handle = handle;
90 cmd->next = cmdlist;
91 cmdlist = cmd;
92 }
93}
94
95static struct fastboot_var *varlist;
96
97void fastboot_publish(const char *name, const char *value)
98{
99 struct fastboot_var *var;
100 var = malloc(sizeof(*var));
101 if (var) {
102 var->name = name;
103 var->value = value;
104 var->next = varlist;
105 varlist = var;
106 }
107}
108
109
110static event_t usb_online;
111static event_t txn_done;
112static unsigned char buffer[4096];
113static struct udc_endpoint *in, *out;
114static struct udc_request *req;
115int txn_status;
116
117static void *download_base;
118static unsigned download_max;
119static unsigned download_size;
120
121#define STATE_OFFLINE 0
122#define STATE_COMMAND 1
123#define STATE_COMPLETE 2
124#define STATE_ERROR 3
125
126static unsigned fastboot_state = STATE_OFFLINE;
127
128static void req_complete(struct udc_request *req, unsigned actual, int status)
129{
130 txn_status = status;
131 req->length = actual;
132 event_signal(&txn_done, 0);
133}
134
135static int usb_read(void *_buf, unsigned len)
136{
137 int r;
138 unsigned xfer;
139 unsigned char *buf = _buf;
140 int count = 0;
141
142 if (fastboot_state == STATE_ERROR)
143 goto oops;
144
145 while (len > 0) {
146 xfer = (len > 4096) ? 4096 : len;
147 req->buf = buf;
148 req->length = xfer;
149 req->complete = req_complete;
150 r = udc_request_queue(out, req);
151 if (r < 0) {
152 dprintf(INFO, "usb_read() queue failed\n");
153 goto oops;
154 }
155 event_wait(&txn_done);
156
157 if (txn_status < 0) {
158 dprintf(INFO, "usb_read() transaction failed\n");
159 goto oops;
160 }
161
162 count += req->length;
163 buf += req->length;
164 len -= req->length;
165
166 /* short transfer? */
167 if (req->length != xfer) break;
168 }
169
170 return count;
171
172oops:
173 fastboot_state = STATE_ERROR;
174 return -1;
175}
176
177static int usb_write(void *buf, unsigned len)
178{
179 int r;
180
181 if (fastboot_state == STATE_ERROR)
182 goto oops;
183
184 req->buf = buf;
185 req->length = len;
186 req->complete = req_complete;
187 r = udc_request_queue(in, req);
188 if (r < 0) {
189 dprintf(INFO, "usb_write() queue failed\n");
190 goto oops;
191 }
192 event_wait(&txn_done);
193 if (txn_status < 0) {
194 dprintf(INFO, "usb_write() transaction failed\n");
195 goto oops;
196 }
197 return req->length;
198
199oops:
200 fastboot_state = STATE_ERROR;
201 return -1;
202}
203
204void fastboot_ack(const char *code, const char *reason)
205{
206 char response[64];
207
208 if (fastboot_state != STATE_COMMAND)
209 return;
210
211 if (reason == 0)
212 reason = "";
213
214 snprintf(response, 64, "%s%s", code, reason);
215 fastboot_state = STATE_COMPLETE;
216
217 usb_write(response, strlen(response));
218
219}
220
221void fastboot_fail(const char *reason)
222{
223 fastboot_ack("FAIL", reason);
224}
225
226void fastboot_okay(const char *info)
227{
228 fastboot_ack("OKAY", info);
229}
230
231static void cmd_getvar(const char *arg, void *data, unsigned sz)
232{
233 struct fastboot_var *var;
234
235 for (var = varlist; var; var = var->next) {
236 if (!strcmp(var->name, arg)) {
237 fastboot_okay(var->value);
238 return;
239 }
240 }
241 fastboot_okay("");
242}
243
244static void cmd_download(const char *arg, void *data, unsigned sz)
245{
246 char response[64];
247 unsigned len = hex2unsigned(arg);
248 int r;
249
250 download_size = 0;
251 if (len > download_max) {
252 fastboot_fail("data too large");
253 return;
254 }
255
256 sprintf(response,"DATA%08x", len);
257 if (usb_write(response, strlen(response)) < 0)
258 return;
259
260 r = usb_read(download_base, len);
Greg Griscod6250552011-06-29 14:40:23 -0700261 if ((r < 0) || ((unsigned) r != len)) {
Brian Swetland9c4c0752009-01-25 16:23:50 -0800262 fastboot_state = STATE_ERROR;
263 return;
264 }
265 download_size = len;
266 fastboot_okay("");
267}
268
269static void fastboot_command_loop(void)
270{
271 struct fastboot_cmd *cmd;
272 int r;
273 dprintf(INFO,"fastboot: processing commands\n");
274
275again:
276 while (fastboot_state != STATE_ERROR) {
277 r = usb_read(buffer, 64);
278 if (r < 0) break;
279 buffer[r] = 0;
280 dprintf(INFO,"fastboot: %s\n", buffer);
281
282 for (cmd = cmdlist; cmd; cmd = cmd->next) {
283 if (memcmp(buffer, cmd->prefix, cmd->prefix_len))
284 continue;
285 fastboot_state = STATE_COMMAND;
286 cmd->handle((const char*) buffer + cmd->prefix_len,
287 (void*) download_base, download_size);
288 if (fastboot_state == STATE_COMMAND)
289 fastboot_fail("unknown reason");
290 goto again;
291 }
292
293 fastboot_fail("unknown command");
294
295 }
296 fastboot_state = STATE_OFFLINE;
297 dprintf(INFO,"fastboot: oops!\n");
298}
299
300static int fastboot_handler(void *arg)
301{
302 for (;;) {
303 event_wait(&usb_online);
304 fastboot_command_loop();
305 }
306 return 0;
307}
308
309static void fastboot_notify(struct udc_gadget *gadget, unsigned event)
310{
311 if (event == UDC_EVENT_ONLINE) {
312 event_signal(&usb_online, 0);
313 }
314}
315
316static struct udc_endpoint *fastboot_endpoints[2];
317
318static struct udc_gadget fastboot_gadget = {
319 .notify = fastboot_notify,
320 .ifc_class = 0xff,
321 .ifc_subclass = 0x42,
322 .ifc_protocol = 0x03,
323 .ifc_endpoints = 2,
324 .ifc_string = "fastboot",
325 .ept = fastboot_endpoints,
326};
327
328int fastboot_init(void *base, unsigned size)
329{
330 thread_t *thr;
331 dprintf(INFO, "fastboot_init()\n");
332
333 download_base = base;
334 download_max = size;
335
336 event_init(&usb_online, 0, EVENT_FLAG_AUTOUNSIGNAL);
337 event_init(&txn_done, 0, EVENT_FLAG_AUTOUNSIGNAL);
338
339 in = udc_endpoint_alloc(UDC_TYPE_BULK_IN, 512);
340 if (!in)
341 goto fail_alloc_in;
342 out = udc_endpoint_alloc(UDC_TYPE_BULK_OUT, 512);
343 if (!out)
344 goto fail_alloc_out;
345
346 fastboot_endpoints[0] = in;
347 fastboot_endpoints[1] = out;
348
349 req = udc_request_alloc();
350 if (!req)
351 goto fail_alloc_req;
352
353 if (udc_register_gadget(&fastboot_gadget))
354 goto fail_udc_register;
355
356 fastboot_register("getvar:", cmd_getvar);
357 fastboot_register("download:", cmd_download);
358 fastboot_publish("version", "0.5");
359
360 thr = thread_create("fastboot", fastboot_handler, 0, DEFAULT_PRIORITY, 4096);
361 thread_resume(thr);
362 return 0;
363
364fail_udc_register:
365 udc_request_free(req);
366fail_alloc_req:
367 udc_endpoint_free(out);
368fail_alloc_out:
369 udc_endpoint_free(in);
370fail_alloc_in:
371 return -1;
372}