Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2008 - 2009 |
| 3 | * Windriver, <www.windriver.com> |
| 4 | * Tom Rix <Tom.Rix@windriver.com> |
| 5 | * |
| 6 | * Copyright 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de> |
| 7 | * |
| 8 | * Copyright 2014 Linaro, Ltd. |
| 9 | * Rob Herring <robh@kernel.org> |
| 10 | * |
| 11 | * SPDX-License-Identifier: GPL-2.0+ |
| 12 | */ |
Steve Rae | 593cbd9 | 2014-08-26 11:47:29 -0700 | [diff] [blame] | 13 | #include <config.h> |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 14 | #include <common.h> |
| 15 | #include <errno.h> |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 16 | #include <fastboot.h> |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 17 | #include <malloc.h> |
| 18 | #include <linux/usb/ch9.h> |
| 19 | #include <linux/usb/gadget.h> |
| 20 | #include <linux/usb/composite.h> |
| 21 | #include <linux/compiler.h> |
| 22 | #include <version.h> |
| 23 | #include <g_dnl.h> |
Steve Rae | d1b5ed0 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 24 | #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV |
| 25 | #include <fb_mmc.h> |
| 26 | #endif |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 27 | #ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV |
| 28 | #include <fb_nand.h> |
| 29 | #endif |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 30 | |
| 31 | #define FASTBOOT_VERSION "0.4" |
| 32 | |
| 33 | #define FASTBOOT_INTERFACE_CLASS 0xff |
| 34 | #define FASTBOOT_INTERFACE_SUB_CLASS 0x42 |
| 35 | #define FASTBOOT_INTERFACE_PROTOCOL 0x03 |
| 36 | |
| 37 | #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0 (0x0200) |
| 38 | #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1 (0x0040) |
| 39 | #define TX_ENDPOINT_MAXIMUM_PACKET_SIZE (0x0040) |
| 40 | |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 41 | #define EP_BUFFER_SIZE 4096 |
| 42 | |
| 43 | struct f_fastboot { |
| 44 | struct usb_function usb_function; |
| 45 | |
Steve Rae | 593cbd9 | 2014-08-26 11:47:29 -0700 | [diff] [blame] | 46 | /* IN/OUT EP's and corresponding requests */ |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 47 | struct usb_ep *in_ep, *out_ep; |
| 48 | struct usb_request *in_req, *out_req; |
| 49 | }; |
| 50 | |
| 51 | static inline struct f_fastboot *func_to_fastboot(struct usb_function *f) |
| 52 | { |
| 53 | return container_of(f, struct f_fastboot, usb_function); |
| 54 | } |
| 55 | |
| 56 | static struct f_fastboot *fastboot_func; |
Maxime Ripard | 6c9e00e | 2015-10-15 14:34:15 +0200 | [diff] [blame] | 57 | static unsigned int fastboot_flash_session_id; |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 58 | static unsigned int download_size; |
| 59 | static unsigned int download_bytes; |
Dileep Katta | 9e4b510 | 2015-02-17 02:02:36 +0530 | [diff] [blame] | 60 | static bool is_high_speed; |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 61 | |
| 62 | static struct usb_endpoint_descriptor fs_ep_in = { |
| 63 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 64 | .bDescriptorType = USB_DT_ENDPOINT, |
| 65 | .bEndpointAddress = USB_DIR_IN, |
| 66 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Roger Quadros | 718156a | 2016-04-12 15:51:48 +0300 | [diff] [blame] | 67 | .wMaxPacketSize = cpu_to_le16(64), |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | static struct usb_endpoint_descriptor fs_ep_out = { |
| 71 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 72 | .bDescriptorType = USB_DT_ENDPOINT, |
| 73 | .bEndpointAddress = USB_DIR_OUT, |
| 74 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Roger Quadros | 718156a | 2016-04-12 15:51:48 +0300 | [diff] [blame] | 75 | .wMaxPacketSize = cpu_to_le16(64), |
| 76 | }; |
| 77 | |
| 78 | static struct usb_endpoint_descriptor hs_ep_in = { |
| 79 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 80 | .bDescriptorType = USB_DT_ENDPOINT, |
| 81 | .bEndpointAddress = USB_DIR_IN, |
| 82 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 83 | .wMaxPacketSize = cpu_to_le16(512), |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | static struct usb_endpoint_descriptor hs_ep_out = { |
| 87 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 88 | .bDescriptorType = USB_DT_ENDPOINT, |
| 89 | .bEndpointAddress = USB_DIR_OUT, |
| 90 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Roger Quadros | 718156a | 2016-04-12 15:51:48 +0300 | [diff] [blame] | 91 | .wMaxPacketSize = cpu_to_le16(512), |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | static struct usb_interface_descriptor interface_desc = { |
| 95 | .bLength = USB_DT_INTERFACE_SIZE, |
| 96 | .bDescriptorType = USB_DT_INTERFACE, |
| 97 | .bInterfaceNumber = 0x00, |
| 98 | .bAlternateSetting = 0x00, |
| 99 | .bNumEndpoints = 0x02, |
| 100 | .bInterfaceClass = FASTBOOT_INTERFACE_CLASS, |
| 101 | .bInterfaceSubClass = FASTBOOT_INTERFACE_SUB_CLASS, |
| 102 | .bInterfaceProtocol = FASTBOOT_INTERFACE_PROTOCOL, |
| 103 | }; |
| 104 | |
Roger Quadros | 718156a | 2016-04-12 15:51:48 +0300 | [diff] [blame] | 105 | static struct usb_descriptor_header *fb_fs_function[] = { |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 106 | (struct usb_descriptor_header *)&interface_desc, |
| 107 | (struct usb_descriptor_header *)&fs_ep_in, |
Roger Quadros | 718156a | 2016-04-12 15:51:48 +0300 | [diff] [blame] | 108 | (struct usb_descriptor_header *)&fs_ep_out, |
| 109 | }; |
| 110 | |
| 111 | static struct usb_descriptor_header *fb_hs_function[] = { |
| 112 | (struct usb_descriptor_header *)&interface_desc, |
| 113 | (struct usb_descriptor_header *)&hs_ep_in, |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 114 | (struct usb_descriptor_header *)&hs_ep_out, |
| 115 | NULL, |
| 116 | }; |
| 117 | |
Roger Quadros | 8b704a0 | 2016-04-13 11:30:00 +0300 | [diff] [blame^] | 118 | static struct usb_endpoint_descriptor * |
| 119 | fb_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs, |
| 120 | struct usb_endpoint_descriptor *hs) |
| 121 | { |
| 122 | if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) |
| 123 | return hs; |
| 124 | return fs; |
| 125 | } |
| 126 | |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 127 | /* |
| 128 | * static strings, in UTF-8 |
| 129 | */ |
| 130 | static const char fastboot_name[] = "Android Fastboot"; |
| 131 | |
| 132 | static struct usb_string fastboot_string_defs[] = { |
| 133 | [0].s = fastboot_name, |
| 134 | { } /* end of list */ |
| 135 | }; |
| 136 | |
| 137 | static struct usb_gadget_strings stringtab_fastboot = { |
| 138 | .language = 0x0409, /* en-us */ |
| 139 | .strings = fastboot_string_defs, |
| 140 | }; |
| 141 | |
| 142 | static struct usb_gadget_strings *fastboot_strings[] = { |
| 143 | &stringtab_fastboot, |
| 144 | NULL, |
| 145 | }; |
| 146 | |
| 147 | static void rx_handler_command(struct usb_ep *ep, struct usb_request *req); |
Alexey Firago | e2ec3e4 | 2015-02-25 17:10:47 +0300 | [diff] [blame] | 148 | static int strcmp_l1(const char *s1, const char *s2); |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 149 | |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 150 | |
| 151 | void fastboot_fail(char *response, const char *reason) |
| 152 | { |
| 153 | strncpy(response, "FAIL\0", 5); |
| 154 | strncat(response, reason, FASTBOOT_RESPONSE_LEN - 4 - 1); |
| 155 | } |
| 156 | |
| 157 | void fastboot_okay(char *response, const char *reason) |
| 158 | { |
| 159 | strncpy(response, "OKAY\0", 5); |
| 160 | strncat(response, reason, FASTBOOT_RESPONSE_LEN - 4 - 1); |
| 161 | } |
| 162 | |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 163 | static void fastboot_complete(struct usb_ep *ep, struct usb_request *req) |
| 164 | { |
| 165 | int status = req->status; |
| 166 | if (!status) |
| 167 | return; |
| 168 | printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual); |
| 169 | } |
| 170 | |
| 171 | static int fastboot_bind(struct usb_configuration *c, struct usb_function *f) |
| 172 | { |
| 173 | int id; |
| 174 | struct usb_gadget *gadget = c->cdev->gadget; |
| 175 | struct f_fastboot *f_fb = func_to_fastboot(f); |
Dileep Katta | 537cd07 | 2015-02-13 14:33:43 +0800 | [diff] [blame] | 176 | const char *s; |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 177 | |
| 178 | /* DYNAMIC interface numbers assignments */ |
| 179 | id = usb_interface_id(c, f); |
| 180 | if (id < 0) |
| 181 | return id; |
| 182 | interface_desc.bInterfaceNumber = id; |
| 183 | |
| 184 | id = usb_string_id(c->cdev); |
| 185 | if (id < 0) |
| 186 | return id; |
| 187 | fastboot_string_defs[0].id = id; |
| 188 | interface_desc.iInterface = id; |
| 189 | |
| 190 | f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in); |
| 191 | if (!f_fb->in_ep) |
| 192 | return -ENODEV; |
| 193 | f_fb->in_ep->driver_data = c->cdev; |
| 194 | |
| 195 | f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out); |
| 196 | if (!f_fb->out_ep) |
| 197 | return -ENODEV; |
| 198 | f_fb->out_ep->driver_data = c->cdev; |
| 199 | |
Roger Quadros | 718156a | 2016-04-12 15:51:48 +0300 | [diff] [blame] | 200 | f->descriptors = fb_fs_function; |
| 201 | |
| 202 | if (gadget_is_dualspeed(gadget)) { |
| 203 | /* Assume endpoint addresses are the same for both speeds */ |
| 204 | hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress; |
| 205 | hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress; |
| 206 | /* copy HS descriptors */ |
| 207 | f->hs_descriptors = fb_hs_function; |
| 208 | } |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 209 | |
Dileep Katta | 537cd07 | 2015-02-13 14:33:43 +0800 | [diff] [blame] | 210 | s = getenv("serial#"); |
| 211 | if (s) |
| 212 | g_dnl_set_serialnumber((char *)s); |
| 213 | |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f) |
| 218 | { |
| 219 | memset(fastboot_func, 0, sizeof(*fastboot_func)); |
| 220 | } |
| 221 | |
| 222 | static void fastboot_disable(struct usb_function *f) |
| 223 | { |
| 224 | struct f_fastboot *f_fb = func_to_fastboot(f); |
| 225 | |
| 226 | usb_ep_disable(f_fb->out_ep); |
| 227 | usb_ep_disable(f_fb->in_ep); |
| 228 | |
| 229 | if (f_fb->out_req) { |
| 230 | free(f_fb->out_req->buf); |
| 231 | usb_ep_free_request(f_fb->out_ep, f_fb->out_req); |
| 232 | f_fb->out_req = NULL; |
| 233 | } |
| 234 | if (f_fb->in_req) { |
| 235 | free(f_fb->in_req->buf); |
| 236 | usb_ep_free_request(f_fb->in_ep, f_fb->in_req); |
| 237 | f_fb->in_req = NULL; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | static struct usb_request *fastboot_start_ep(struct usb_ep *ep) |
| 242 | { |
| 243 | struct usb_request *req; |
| 244 | |
| 245 | req = usb_ep_alloc_request(ep, 0); |
| 246 | if (!req) |
| 247 | return NULL; |
| 248 | |
| 249 | req->length = EP_BUFFER_SIZE; |
| 250 | req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE); |
| 251 | if (!req->buf) { |
| 252 | usb_ep_free_request(ep, req); |
| 253 | return NULL; |
| 254 | } |
| 255 | |
| 256 | memset(req->buf, 0, req->length); |
| 257 | return req; |
| 258 | } |
| 259 | |
| 260 | static int fastboot_set_alt(struct usb_function *f, |
| 261 | unsigned interface, unsigned alt) |
| 262 | { |
| 263 | int ret; |
| 264 | struct usb_composite_dev *cdev = f->config->cdev; |
| 265 | struct usb_gadget *gadget = cdev->gadget; |
| 266 | struct f_fastboot *f_fb = func_to_fastboot(f); |
Roger Quadros | 8b704a0 | 2016-04-13 11:30:00 +0300 | [diff] [blame^] | 267 | const struct usb_endpoint_descriptor *d; |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 268 | |
| 269 | debug("%s: func: %s intf: %d alt: %d\n", |
| 270 | __func__, f->name, interface, alt); |
| 271 | |
Roger Quadros | 8b704a0 | 2016-04-13 11:30:00 +0300 | [diff] [blame^] | 272 | if (gadget->speed == USB_SPEED_HIGH) |
Dileep Katta | 9e4b510 | 2015-02-17 02:02:36 +0530 | [diff] [blame] | 273 | is_high_speed = true; |
Roger Quadros | 8b704a0 | 2016-04-13 11:30:00 +0300 | [diff] [blame^] | 274 | else |
Dileep Katta | 9e4b510 | 2015-02-17 02:02:36 +0530 | [diff] [blame] | 275 | is_high_speed = false; |
Roger Quadros | 8b704a0 | 2016-04-13 11:30:00 +0300 | [diff] [blame^] | 276 | |
| 277 | d = fb_ep_desc(gadget, &fs_ep_out, &hs_ep_out); |
| 278 | ret = usb_ep_enable(f_fb->out_ep, d); |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 279 | if (ret) { |
| 280 | puts("failed to enable out ep\n"); |
| 281 | return ret; |
| 282 | } |
| 283 | |
| 284 | f_fb->out_req = fastboot_start_ep(f_fb->out_ep); |
| 285 | if (!f_fb->out_req) { |
| 286 | puts("failed to alloc out req\n"); |
| 287 | ret = -EINVAL; |
| 288 | goto err; |
| 289 | } |
| 290 | f_fb->out_req->complete = rx_handler_command; |
| 291 | |
Roger Quadros | 8b704a0 | 2016-04-13 11:30:00 +0300 | [diff] [blame^] | 292 | d = fb_ep_desc(gadget, &fs_ep_in, &hs_ep_in); |
| 293 | ret = usb_ep_enable(f_fb->in_ep, d); |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 294 | if (ret) { |
| 295 | puts("failed to enable in ep\n"); |
| 296 | goto err; |
| 297 | } |
| 298 | |
| 299 | f_fb->in_req = fastboot_start_ep(f_fb->in_ep); |
| 300 | if (!f_fb->in_req) { |
| 301 | puts("failed alloc req in\n"); |
| 302 | ret = -EINVAL; |
| 303 | goto err; |
| 304 | } |
| 305 | f_fb->in_req->complete = fastboot_complete; |
| 306 | |
| 307 | ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0); |
| 308 | if (ret) |
| 309 | goto err; |
| 310 | |
| 311 | return 0; |
| 312 | err: |
| 313 | fastboot_disable(f); |
| 314 | return ret; |
| 315 | } |
| 316 | |
| 317 | static int fastboot_add(struct usb_configuration *c) |
| 318 | { |
| 319 | struct f_fastboot *f_fb = fastboot_func; |
| 320 | int status; |
| 321 | |
| 322 | debug("%s: cdev: 0x%p\n", __func__, c->cdev); |
| 323 | |
| 324 | if (!f_fb) { |
| 325 | f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb)); |
| 326 | if (!f_fb) |
| 327 | return -ENOMEM; |
| 328 | |
| 329 | fastboot_func = f_fb; |
| 330 | memset(f_fb, 0, sizeof(*f_fb)); |
| 331 | } |
| 332 | |
| 333 | f_fb->usb_function.name = "f_fastboot"; |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 334 | f_fb->usb_function.bind = fastboot_bind; |
| 335 | f_fb->usb_function.unbind = fastboot_unbind; |
| 336 | f_fb->usb_function.set_alt = fastboot_set_alt; |
| 337 | f_fb->usb_function.disable = fastboot_disable; |
| 338 | f_fb->usb_function.strings = fastboot_strings; |
| 339 | |
| 340 | status = usb_add_function(c, &f_fb->usb_function); |
| 341 | if (status) { |
| 342 | free(f_fb); |
| 343 | fastboot_func = f_fb; |
| 344 | } |
| 345 | |
| 346 | return status; |
| 347 | } |
| 348 | DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add); |
| 349 | |
Steve Rae | 593cbd9 | 2014-08-26 11:47:29 -0700 | [diff] [blame] | 350 | static int fastboot_tx_write(const char *buffer, unsigned int buffer_size) |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 351 | { |
| 352 | struct usb_request *in_req = fastboot_func->in_req; |
| 353 | int ret; |
| 354 | |
| 355 | memcpy(in_req->buf, buffer, buffer_size); |
| 356 | in_req->length = buffer_size; |
Paul Kocialkowski | bc9071c | 2015-07-04 16:46:16 +0200 | [diff] [blame] | 357 | |
| 358 | usb_ep_dequeue(fastboot_func->in_ep, in_req); |
| 359 | |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 360 | ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0); |
| 361 | if (ret) |
| 362 | printf("Error %d on queue\n", ret); |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | static int fastboot_tx_write_str(const char *buffer) |
| 367 | { |
| 368 | return fastboot_tx_write(buffer, strlen(buffer)); |
| 369 | } |
| 370 | |
| 371 | static void compl_do_reset(struct usb_ep *ep, struct usb_request *req) |
| 372 | { |
| 373 | do_reset(NULL, 0, 0, NULL); |
| 374 | } |
| 375 | |
Alexey Firago | e2ec3e4 | 2015-02-25 17:10:47 +0300 | [diff] [blame] | 376 | int __weak fb_set_reboot_flag(void) |
| 377 | { |
| 378 | return -ENOSYS; |
| 379 | } |
| 380 | |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 381 | static void cb_reboot(struct usb_ep *ep, struct usb_request *req) |
| 382 | { |
Alexey Firago | e2ec3e4 | 2015-02-25 17:10:47 +0300 | [diff] [blame] | 383 | char *cmd = req->buf; |
| 384 | if (!strcmp_l1("reboot-bootloader", cmd)) { |
| 385 | if (fb_set_reboot_flag()) { |
| 386 | fastboot_tx_write_str("FAILCannot set reboot flag"); |
| 387 | return; |
| 388 | } |
| 389 | } |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 390 | fastboot_func->in_req->complete = compl_do_reset; |
| 391 | fastboot_tx_write_str("OKAY"); |
| 392 | } |
| 393 | |
| 394 | static int strcmp_l1(const char *s1, const char *s2) |
| 395 | { |
| 396 | if (!s1 || !s2) |
| 397 | return -1; |
| 398 | return strncmp(s1, s2, strlen(s1)); |
| 399 | } |
| 400 | |
| 401 | static void cb_getvar(struct usb_ep *ep, struct usb_request *req) |
| 402 | { |
| 403 | char *cmd = req->buf; |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 404 | char response[FASTBOOT_RESPONSE_LEN]; |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 405 | const char *s; |
Jeroen Hofstee | 29425be | 2014-06-14 00:57:14 +0200 | [diff] [blame] | 406 | size_t chars_left; |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 407 | |
| 408 | strcpy(response, "OKAY"); |
Jeroen Hofstee | 29425be | 2014-06-14 00:57:14 +0200 | [diff] [blame] | 409 | chars_left = sizeof(response) - strlen(response) - 1; |
| 410 | |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 411 | strsep(&cmd, ":"); |
| 412 | if (!cmd) { |
Steve Rae | a18c270 | 2016-01-27 15:02:41 -0800 | [diff] [blame] | 413 | error("missing variable"); |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 414 | fastboot_tx_write_str("FAILmissing var"); |
| 415 | return; |
| 416 | } |
| 417 | |
| 418 | if (!strcmp_l1("version", cmd)) { |
Jeroen Hofstee | 29425be | 2014-06-14 00:57:14 +0200 | [diff] [blame] | 419 | strncat(response, FASTBOOT_VERSION, chars_left); |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 420 | } else if (!strcmp_l1("bootloader-version", cmd)) { |
Jeroen Hofstee | 29425be | 2014-06-14 00:57:14 +0200 | [diff] [blame] | 421 | strncat(response, U_BOOT_VERSION, chars_left); |
Eric Nelson | c674a66 | 2014-09-30 12:05:40 -0700 | [diff] [blame] | 422 | } else if (!strcmp_l1("downloadsize", cmd) || |
| 423 | !strcmp_l1("max-download-size", cmd)) { |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 424 | char str_num[12]; |
| 425 | |
Paul Kocialkowski | a588d99 | 2015-07-20 12:38:22 +0200 | [diff] [blame] | 426 | sprintf(str_num, "0x%08x", CONFIG_FASTBOOT_BUF_SIZE); |
Jeroen Hofstee | 29425be | 2014-06-14 00:57:14 +0200 | [diff] [blame] | 427 | strncat(response, str_num, chars_left); |
Maxime Ripard | 6c9e00e | 2015-10-15 14:34:15 +0200 | [diff] [blame] | 428 | |
| 429 | /* |
| 430 | * This also indicates the start of a new flashing |
| 431 | * "session", in which we could have 1-N buffers to |
| 432 | * write to a partition. |
| 433 | * |
| 434 | * Reset our session counter. |
| 435 | */ |
| 436 | fastboot_flash_session_id = 0; |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 437 | } else if (!strcmp_l1("serialno", cmd)) { |
| 438 | s = getenv("serial#"); |
| 439 | if (s) |
Jeroen Hofstee | 29425be | 2014-06-14 00:57:14 +0200 | [diff] [blame] | 440 | strncat(response, s, chars_left); |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 441 | else |
| 442 | strcpy(response, "FAILValue not set"); |
| 443 | } else { |
Rob Herring | 7432220 | 2016-03-17 17:21:23 +0100 | [diff] [blame] | 444 | char envstr[32]; |
| 445 | |
| 446 | snprintf(envstr, sizeof(envstr) - 1, "fastboot.%s", cmd); |
| 447 | s = getenv(envstr); |
| 448 | if (s) { |
| 449 | strncat(response, s, chars_left); |
| 450 | } else { |
| 451 | printf("WARNING: unknown variable: %s\n", cmd); |
| 452 | strcpy(response, "FAILVariable not implemented"); |
| 453 | } |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 454 | } |
| 455 | fastboot_tx_write_str(response); |
| 456 | } |
| 457 | |
Dileep Katta | 9e4b510 | 2015-02-17 02:02:36 +0530 | [diff] [blame] | 458 | static unsigned int rx_bytes_expected(unsigned int maxpacket) |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 459 | { |
| 460 | int rx_remain = download_size - download_bytes; |
Dileep Katta | 9e4b510 | 2015-02-17 02:02:36 +0530 | [diff] [blame] | 461 | int rem = 0; |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 462 | if (rx_remain < 0) |
| 463 | return 0; |
| 464 | if (rx_remain > EP_BUFFER_SIZE) |
| 465 | return EP_BUFFER_SIZE; |
Dileep Katta | 9e4b510 | 2015-02-17 02:02:36 +0530 | [diff] [blame] | 466 | if (rx_remain < maxpacket) { |
| 467 | rx_remain = maxpacket; |
| 468 | } else if (rx_remain % maxpacket != 0) { |
| 469 | rem = rx_remain % maxpacket; |
| 470 | rx_remain = rx_remain + (maxpacket - rem); |
| 471 | } |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 472 | return rx_remain; |
| 473 | } |
| 474 | |
| 475 | #define BYTES_PER_DOT 0x20000 |
| 476 | static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req) |
| 477 | { |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 478 | char response[FASTBOOT_RESPONSE_LEN]; |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 479 | unsigned int transfer_size = download_size - download_bytes; |
| 480 | const unsigned char *buffer = req->buf; |
| 481 | unsigned int buffer_size = req->actual; |
Bo Shen | 23d1d10 | 2014-09-19 14:15:12 +0800 | [diff] [blame] | 482 | unsigned int pre_dot_num, now_dot_num; |
Dileep Katta | 9e4b510 | 2015-02-17 02:02:36 +0530 | [diff] [blame] | 483 | unsigned int max; |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 484 | |
| 485 | if (req->status != 0) { |
| 486 | printf("Bad status: %d\n", req->status); |
| 487 | return; |
| 488 | } |
| 489 | |
| 490 | if (buffer_size < transfer_size) |
| 491 | transfer_size = buffer_size; |
| 492 | |
Paul Kocialkowski | a588d99 | 2015-07-20 12:38:22 +0200 | [diff] [blame] | 493 | memcpy((void *)CONFIG_FASTBOOT_BUF_ADDR + download_bytes, |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 494 | buffer, transfer_size); |
| 495 | |
Bo Shen | 23d1d10 | 2014-09-19 14:15:12 +0800 | [diff] [blame] | 496 | pre_dot_num = download_bytes / BYTES_PER_DOT; |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 497 | download_bytes += transfer_size; |
Bo Shen | 23d1d10 | 2014-09-19 14:15:12 +0800 | [diff] [blame] | 498 | now_dot_num = download_bytes / BYTES_PER_DOT; |
| 499 | |
| 500 | if (pre_dot_num != now_dot_num) { |
| 501 | putc('.'); |
| 502 | if (!(now_dot_num % 74)) |
| 503 | putc('\n'); |
| 504 | } |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 505 | |
| 506 | /* Check if transfer is done */ |
| 507 | if (download_bytes >= download_size) { |
| 508 | /* |
| 509 | * Reset global transfer variable, keep download_bytes because |
| 510 | * it will be used in the next possible flashing command |
| 511 | */ |
| 512 | download_size = 0; |
| 513 | req->complete = rx_handler_command; |
| 514 | req->length = EP_BUFFER_SIZE; |
| 515 | |
Ben Whitten | 192bc69 | 2015-12-30 13:05:58 +0000 | [diff] [blame] | 516 | strcpy(response, "OKAY"); |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 517 | fastboot_tx_write_str(response); |
| 518 | |
| 519 | printf("\ndownloading of %d bytes finished\n", download_bytes); |
| 520 | } else { |
Dileep Katta | 9e4b510 | 2015-02-17 02:02:36 +0530 | [diff] [blame] | 521 | max = is_high_speed ? hs_ep_out.wMaxPacketSize : |
| 522 | fs_ep_out.wMaxPacketSize; |
| 523 | req->length = rx_bytes_expected(max); |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 524 | if (req->length < ep->maxpacket) |
| 525 | req->length = ep->maxpacket; |
| 526 | } |
| 527 | |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 528 | req->actual = 0; |
| 529 | usb_ep_queue(ep, req, 0); |
| 530 | } |
| 531 | |
| 532 | static void cb_download(struct usb_ep *ep, struct usb_request *req) |
| 533 | { |
| 534 | char *cmd = req->buf; |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 535 | char response[FASTBOOT_RESPONSE_LEN]; |
Dileep Katta | 9e4b510 | 2015-02-17 02:02:36 +0530 | [diff] [blame] | 536 | unsigned int max; |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 537 | |
| 538 | strsep(&cmd, ":"); |
| 539 | download_size = simple_strtoul(cmd, NULL, 16); |
| 540 | download_bytes = 0; |
| 541 | |
| 542 | printf("Starting download of %d bytes\n", download_size); |
| 543 | |
| 544 | if (0 == download_size) { |
Ben Whitten | 192bc69 | 2015-12-30 13:05:58 +0000 | [diff] [blame] | 545 | strcpy(response, "FAILdata invalid size"); |
Paul Kocialkowski | a588d99 | 2015-07-20 12:38:22 +0200 | [diff] [blame] | 546 | } else if (download_size > CONFIG_FASTBOOT_BUF_SIZE) { |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 547 | download_size = 0; |
Ben Whitten | 192bc69 | 2015-12-30 13:05:58 +0000 | [diff] [blame] | 548 | strcpy(response, "FAILdata too large"); |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 549 | } else { |
| 550 | sprintf(response, "DATA%08x", download_size); |
| 551 | req->complete = rx_handler_dl_image; |
Dileep Katta | 9e4b510 | 2015-02-17 02:02:36 +0530 | [diff] [blame] | 552 | max = is_high_speed ? hs_ep_out.wMaxPacketSize : |
| 553 | fs_ep_out.wMaxPacketSize; |
| 554 | req->length = rx_bytes_expected(max); |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 555 | if (req->length < ep->maxpacket) |
| 556 | req->length = ep->maxpacket; |
| 557 | } |
| 558 | fastboot_tx_write_str(response); |
| 559 | } |
| 560 | |
| 561 | static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req) |
| 562 | { |
| 563 | char boot_addr_start[12]; |
| 564 | char *bootm_args[] = { "bootm", boot_addr_start, NULL }; |
| 565 | |
| 566 | puts("Booting kernel..\n"); |
| 567 | |
| 568 | sprintf(boot_addr_start, "0x%lx", load_addr); |
| 569 | do_bootm(NULL, 0, 2, bootm_args); |
| 570 | |
| 571 | /* This only happens if image is somehow faulty so we start over */ |
| 572 | do_reset(NULL, 0, 0, NULL); |
| 573 | } |
| 574 | |
| 575 | static void cb_boot(struct usb_ep *ep, struct usb_request *req) |
| 576 | { |
| 577 | fastboot_func->in_req->complete = do_bootm_on_complete; |
| 578 | fastboot_tx_write_str("OKAY"); |
| 579 | } |
| 580 | |
Rob Herring | 267abc6 | 2014-12-10 14:43:04 -0600 | [diff] [blame] | 581 | static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req) |
| 582 | { |
| 583 | g_dnl_trigger_detach(); |
| 584 | } |
| 585 | |
| 586 | static void cb_continue(struct usb_ep *ep, struct usb_request *req) |
| 587 | { |
| 588 | fastboot_func->in_req->complete = do_exit_on_complete; |
| 589 | fastboot_tx_write_str("OKAY"); |
| 590 | } |
| 591 | |
Steve Rae | d1b5ed0 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 592 | #ifdef CONFIG_FASTBOOT_FLASH |
| 593 | static void cb_flash(struct usb_ep *ep, struct usb_request *req) |
| 594 | { |
| 595 | char *cmd = req->buf; |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 596 | char response[FASTBOOT_RESPONSE_LEN]; |
Steve Rae | d1b5ed0 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 597 | |
| 598 | strsep(&cmd, ":"); |
| 599 | if (!cmd) { |
Steve Rae | a18c270 | 2016-01-27 15:02:41 -0800 | [diff] [blame] | 600 | error("missing partition name"); |
Steve Rae | d1b5ed0 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 601 | fastboot_tx_write_str("FAILmissing partition name"); |
| 602 | return; |
| 603 | } |
| 604 | |
| 605 | strcpy(response, "FAILno flash device defined"); |
| 606 | #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV |
Maxime Ripard | 6c9e00e | 2015-10-15 14:34:15 +0200 | [diff] [blame] | 607 | fb_mmc_flash_write(cmd, fastboot_flash_session_id, |
| 608 | (void *)CONFIG_FASTBOOT_BUF_ADDR, |
Steve Rae | d1b5ed0 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 609 | download_bytes, response); |
| 610 | #endif |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 611 | #ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV |
| 612 | fb_nand_flash_write(cmd, fastboot_flash_session_id, |
| 613 | (void *)CONFIG_FASTBOOT_BUF_ADDR, |
| 614 | download_bytes, response); |
| 615 | #endif |
Maxime Ripard | 6c9e00e | 2015-10-15 14:34:15 +0200 | [diff] [blame] | 616 | fastboot_flash_session_id++; |
Steve Rae | d1b5ed0 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 617 | fastboot_tx_write_str(response); |
| 618 | } |
| 619 | #endif |
| 620 | |
Michael Scott | de19562 | 2015-01-26 15:49:00 -0600 | [diff] [blame] | 621 | static void cb_oem(struct usb_ep *ep, struct usb_request *req) |
| 622 | { |
| 623 | char *cmd = req->buf; |
Maxime Ripard | 4adef27 | 2015-10-15 22:04:04 +0200 | [diff] [blame] | 624 | #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV |
Rob Herring | 372d7de | 2015-01-26 15:49:01 -0600 | [diff] [blame] | 625 | if (strncmp("format", cmd + 4, 6) == 0) { |
| 626 | char cmdbuf[32]; |
| 627 | sprintf(cmdbuf, "gpt write mmc %x $partitions", |
| 628 | CONFIG_FASTBOOT_FLASH_MMC_DEV); |
| 629 | if (run_command(cmdbuf, 0)) |
| 630 | fastboot_tx_write_str("FAIL"); |
| 631 | else |
| 632 | fastboot_tx_write_str("OKAY"); |
| 633 | } else |
| 634 | #endif |
Michael Scott | de19562 | 2015-01-26 15:49:00 -0600 | [diff] [blame] | 635 | if (strncmp("unlock", cmd + 4, 8) == 0) { |
| 636 | fastboot_tx_write_str("FAILnot implemented"); |
| 637 | } |
| 638 | else { |
| 639 | fastboot_tx_write_str("FAILunknown oem command"); |
| 640 | } |
| 641 | } |
| 642 | |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 643 | #ifdef CONFIG_FASTBOOT_FLASH |
| 644 | static void cb_erase(struct usb_ep *ep, struct usb_request *req) |
| 645 | { |
| 646 | char *cmd = req->buf; |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 647 | char response[FASTBOOT_RESPONSE_LEN]; |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 648 | |
| 649 | strsep(&cmd, ":"); |
| 650 | if (!cmd) { |
| 651 | error("missing partition name"); |
| 652 | fastboot_tx_write_str("FAILmissing partition name"); |
| 653 | return; |
| 654 | } |
| 655 | |
| 656 | strcpy(response, "FAILno flash device defined"); |
| 657 | |
| 658 | #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV |
| 659 | fb_mmc_erase(cmd, response); |
| 660 | #endif |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 661 | #ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV |
| 662 | fb_nand_erase(cmd, response); |
| 663 | #endif |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 664 | fastboot_tx_write_str(response); |
| 665 | } |
| 666 | #endif |
| 667 | |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 668 | struct cmd_dispatch_info { |
| 669 | char *cmd; |
| 670 | void (*cb)(struct usb_ep *ep, struct usb_request *req); |
| 671 | }; |
| 672 | |
| 673 | static const struct cmd_dispatch_info cmd_dispatch_info[] = { |
| 674 | { |
| 675 | .cmd = "reboot", |
| 676 | .cb = cb_reboot, |
| 677 | }, { |
| 678 | .cmd = "getvar:", |
| 679 | .cb = cb_getvar, |
| 680 | }, { |
| 681 | .cmd = "download:", |
| 682 | .cb = cb_download, |
| 683 | }, { |
| 684 | .cmd = "boot", |
| 685 | .cb = cb_boot, |
Rob Herring | 267abc6 | 2014-12-10 14:43:04 -0600 | [diff] [blame] | 686 | }, { |
| 687 | .cmd = "continue", |
| 688 | .cb = cb_continue, |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 689 | }, |
Steve Rae | d1b5ed0 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 690 | #ifdef CONFIG_FASTBOOT_FLASH |
| 691 | { |
| 692 | .cmd = "flash", |
| 693 | .cb = cb_flash, |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 694 | }, { |
| 695 | .cmd = "erase", |
| 696 | .cb = cb_erase, |
Steve Rae | d1b5ed0 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 697 | }, |
| 698 | #endif |
Michael Scott | de19562 | 2015-01-26 15:49:00 -0600 | [diff] [blame] | 699 | { |
| 700 | .cmd = "oem", |
| 701 | .cb = cb_oem, |
| 702 | }, |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 703 | }; |
| 704 | |
| 705 | static void rx_handler_command(struct usb_ep *ep, struct usb_request *req) |
| 706 | { |
| 707 | char *cmdbuf = req->buf; |
| 708 | void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL; |
| 709 | int i; |
| 710 | |
Paul Kocialkowski | 94b385f | 2015-07-04 16:46:15 +0200 | [diff] [blame] | 711 | if (req->status != 0 || req->length == 0) |
| 712 | return; |
| 713 | |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 714 | for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) { |
| 715 | if (!strcmp_l1(cmd_dispatch_info[i].cmd, cmdbuf)) { |
| 716 | func_cb = cmd_dispatch_info[i].cb; |
| 717 | break; |
| 718 | } |
| 719 | } |
| 720 | |
Steve Rae | 593cbd9 | 2014-08-26 11:47:29 -0700 | [diff] [blame] | 721 | if (!func_cb) { |
Steve Rae | a18c270 | 2016-01-27 15:02:41 -0800 | [diff] [blame] | 722 | error("unknown command: %s", cmdbuf); |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 723 | fastboot_tx_write_str("FAILunknown command"); |
Steve Rae | 593cbd9 | 2014-08-26 11:47:29 -0700 | [diff] [blame] | 724 | } else { |
Eric Nelson | e214058 | 2014-10-01 14:30:56 -0700 | [diff] [blame] | 725 | if (req->actual < req->length) { |
| 726 | u8 *buf = (u8 *)req->buf; |
| 727 | buf[req->actual] = 0; |
| 728 | func_cb(ep, req); |
| 729 | } else { |
Steve Rae | a18c270 | 2016-01-27 15:02:41 -0800 | [diff] [blame] | 730 | error("buffer overflow"); |
Eric Nelson | e214058 | 2014-10-01 14:30:56 -0700 | [diff] [blame] | 731 | fastboot_tx_write_str("FAILbuffer overflow"); |
| 732 | } |
Steve Rae | 593cbd9 | 2014-08-26 11:47:29 -0700 | [diff] [blame] | 733 | } |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 734 | |
Paul Kocialkowski | 94b385f | 2015-07-04 16:46:15 +0200 | [diff] [blame] | 735 | *cmdbuf = '\0'; |
| 736 | req->actual = 0; |
| 737 | usb_ep_queue(ep, req, 0); |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 738 | } |