f_fastboot.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. */
  13. #include <config.h>
  14. #include <common.h>
  15. #include <errno.h>
  16. #include <malloc.h>
  17. #include <linux/usb/ch9.h>
  18. #include <linux/usb/gadget.h>
  19. #include <linux/usb/composite.h>
  20. #include <linux/compiler.h>
  21. #include <version.h>
  22. #include <g_dnl.h>
  23. #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
  24. #include <fb_mmc.h>
  25. #endif
  26. #define FASTBOOT_VERSION "0.4"
  27. #define FASTBOOT_INTERFACE_CLASS 0xff
  28. #define FASTBOOT_INTERFACE_SUB_CLASS 0x42
  29. #define FASTBOOT_INTERFACE_PROTOCOL 0x03
  30. #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0 (0x0200)
  31. #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1 (0x0040)
  32. #define TX_ENDPOINT_MAXIMUM_PACKET_SIZE (0x0040)
  33. /* The 64 defined bytes plus \0 */
  34. #define RESPONSE_LEN (64 + 1)
  35. #define EP_BUFFER_SIZE 4096
  36. struct f_fastboot {
  37. struct usb_function usb_function;
  38. /* IN/OUT EP's and corresponding requests */
  39. struct usb_ep *in_ep, *out_ep;
  40. struct usb_request *in_req, *out_req;
  41. };
  42. static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
  43. {
  44. return container_of(f, struct f_fastboot, usb_function);
  45. }
  46. static struct f_fastboot *fastboot_func;
  47. static unsigned int download_size;
  48. static unsigned int download_bytes;
  49. static struct usb_endpoint_descriptor fs_ep_in = {
  50. .bLength = USB_DT_ENDPOINT_SIZE,
  51. .bDescriptorType = USB_DT_ENDPOINT,
  52. .bEndpointAddress = USB_DIR_IN,
  53. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  54. .wMaxPacketSize = TX_ENDPOINT_MAXIMUM_PACKET_SIZE,
  55. .bInterval = 0x00,
  56. };
  57. static struct usb_endpoint_descriptor fs_ep_out = {
  58. .bLength = USB_DT_ENDPOINT_SIZE,
  59. .bDescriptorType = USB_DT_ENDPOINT,
  60. .bEndpointAddress = USB_DIR_OUT,
  61. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  62. .wMaxPacketSize = RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1,
  63. .bInterval = 0x00,
  64. };
  65. static struct usb_endpoint_descriptor hs_ep_out = {
  66. .bLength = USB_DT_ENDPOINT_SIZE,
  67. .bDescriptorType = USB_DT_ENDPOINT,
  68. .bEndpointAddress = USB_DIR_OUT,
  69. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  70. .wMaxPacketSize = RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0,
  71. .bInterval = 0x00,
  72. };
  73. static struct usb_interface_descriptor interface_desc = {
  74. .bLength = USB_DT_INTERFACE_SIZE,
  75. .bDescriptorType = USB_DT_INTERFACE,
  76. .bInterfaceNumber = 0x00,
  77. .bAlternateSetting = 0x00,
  78. .bNumEndpoints = 0x02,
  79. .bInterfaceClass = FASTBOOT_INTERFACE_CLASS,
  80. .bInterfaceSubClass = FASTBOOT_INTERFACE_SUB_CLASS,
  81. .bInterfaceProtocol = FASTBOOT_INTERFACE_PROTOCOL,
  82. };
  83. static struct usb_descriptor_header *fb_runtime_descs[] = {
  84. (struct usb_descriptor_header *)&interface_desc,
  85. (struct usb_descriptor_header *)&fs_ep_in,
  86. (struct usb_descriptor_header *)&hs_ep_out,
  87. NULL,
  88. };
  89. /*
  90. * static strings, in UTF-8
  91. */
  92. static const char fastboot_name[] = "Android Fastboot";
  93. static struct usb_string fastboot_string_defs[] = {
  94. [0].s = fastboot_name,
  95. { } /* end of list */
  96. };
  97. static struct usb_gadget_strings stringtab_fastboot = {
  98. .language = 0x0409, /* en-us */
  99. .strings = fastboot_string_defs,
  100. };
  101. static struct usb_gadget_strings *fastboot_strings[] = {
  102. &stringtab_fastboot,
  103. NULL,
  104. };
  105. static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
  106. static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
  107. {
  108. int status = req->status;
  109. if (!status)
  110. return;
  111. printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
  112. }
  113. static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
  114. {
  115. int id;
  116. struct usb_gadget *gadget = c->cdev->gadget;
  117. struct f_fastboot *f_fb = func_to_fastboot(f);
  118. /* DYNAMIC interface numbers assignments */
  119. id = usb_interface_id(c, f);
  120. if (id < 0)
  121. return id;
  122. interface_desc.bInterfaceNumber = id;
  123. id = usb_string_id(c->cdev);
  124. if (id < 0)
  125. return id;
  126. fastboot_string_defs[0].id = id;
  127. interface_desc.iInterface = id;
  128. f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
  129. if (!f_fb->in_ep)
  130. return -ENODEV;
  131. f_fb->in_ep->driver_data = c->cdev;
  132. f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
  133. if (!f_fb->out_ep)
  134. return -ENODEV;
  135. f_fb->out_ep->driver_data = c->cdev;
  136. hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
  137. return 0;
  138. }
  139. static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
  140. {
  141. memset(fastboot_func, 0, sizeof(*fastboot_func));
  142. }
  143. static void fastboot_disable(struct usb_function *f)
  144. {
  145. struct f_fastboot *f_fb = func_to_fastboot(f);
  146. usb_ep_disable(f_fb->out_ep);
  147. usb_ep_disable(f_fb->in_ep);
  148. if (f_fb->out_req) {
  149. free(f_fb->out_req->buf);
  150. usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
  151. f_fb->out_req = NULL;
  152. }
  153. if (f_fb->in_req) {
  154. free(f_fb->in_req->buf);
  155. usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
  156. f_fb->in_req = NULL;
  157. }
  158. }
  159. static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
  160. {
  161. struct usb_request *req;
  162. req = usb_ep_alloc_request(ep, 0);
  163. if (!req)
  164. return NULL;
  165. req->length = EP_BUFFER_SIZE;
  166. req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
  167. if (!req->buf) {
  168. usb_ep_free_request(ep, req);
  169. return NULL;
  170. }
  171. memset(req->buf, 0, req->length);
  172. return req;
  173. }
  174. static int fastboot_set_alt(struct usb_function *f,
  175. unsigned interface, unsigned alt)
  176. {
  177. int ret;
  178. struct usb_composite_dev *cdev = f->config->cdev;
  179. struct usb_gadget *gadget = cdev->gadget;
  180. struct f_fastboot *f_fb = func_to_fastboot(f);
  181. debug("%s: func: %s intf: %d alt: %d\n",
  182. __func__, f->name, interface, alt);
  183. /* make sure we don't enable the ep twice */
  184. if (gadget->speed == USB_SPEED_HIGH)
  185. ret = usb_ep_enable(f_fb->out_ep, &hs_ep_out);
  186. else
  187. ret = usb_ep_enable(f_fb->out_ep, &fs_ep_out);
  188. if (ret) {
  189. puts("failed to enable out ep\n");
  190. return ret;
  191. }
  192. f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
  193. if (!f_fb->out_req) {
  194. puts("failed to alloc out req\n");
  195. ret = -EINVAL;
  196. goto err;
  197. }
  198. f_fb->out_req->complete = rx_handler_command;
  199. ret = usb_ep_enable(f_fb->in_ep, &fs_ep_in);
  200. if (ret) {
  201. puts("failed to enable in ep\n");
  202. goto err;
  203. }
  204. f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
  205. if (!f_fb->in_req) {
  206. puts("failed alloc req in\n");
  207. ret = -EINVAL;
  208. goto err;
  209. }
  210. f_fb->in_req->complete = fastboot_complete;
  211. ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
  212. if (ret)
  213. goto err;
  214. return 0;
  215. err:
  216. fastboot_disable(f);
  217. return ret;
  218. }
  219. static int fastboot_add(struct usb_configuration *c)
  220. {
  221. struct f_fastboot *f_fb = fastboot_func;
  222. int status;
  223. debug("%s: cdev: 0x%p\n", __func__, c->cdev);
  224. if (!f_fb) {
  225. f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
  226. if (!f_fb)
  227. return -ENOMEM;
  228. fastboot_func = f_fb;
  229. memset(f_fb, 0, sizeof(*f_fb));
  230. }
  231. f_fb->usb_function.name = "f_fastboot";
  232. f_fb->usb_function.hs_descriptors = fb_runtime_descs;
  233. f_fb->usb_function.bind = fastboot_bind;
  234. f_fb->usb_function.unbind = fastboot_unbind;
  235. f_fb->usb_function.set_alt = fastboot_set_alt;
  236. f_fb->usb_function.disable = fastboot_disable;
  237. f_fb->usb_function.strings = fastboot_strings;
  238. status = usb_add_function(c, &f_fb->usb_function);
  239. if (status) {
  240. free(f_fb);
  241. fastboot_func = f_fb;
  242. }
  243. return status;
  244. }
  245. DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
  246. static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
  247. {
  248. struct usb_request *in_req = fastboot_func->in_req;
  249. int ret;
  250. memcpy(in_req->buf, buffer, buffer_size);
  251. in_req->length = buffer_size;
  252. ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
  253. if (ret)
  254. printf("Error %d on queue\n", ret);
  255. return 0;
  256. }
  257. static int fastboot_tx_write_str(const char *buffer)
  258. {
  259. return fastboot_tx_write(buffer, strlen(buffer));
  260. }
  261. static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
  262. {
  263. do_reset(NULL, 0, 0, NULL);
  264. }
  265. static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
  266. {
  267. fastboot_func->in_req->complete = compl_do_reset;
  268. fastboot_tx_write_str("OKAY");
  269. }
  270. static int strcmp_l1(const char *s1, const char *s2)
  271. {
  272. if (!s1 || !s2)
  273. return -1;
  274. return strncmp(s1, s2, strlen(s1));
  275. }
  276. static void cb_getvar(struct usb_ep *ep, struct usb_request *req)
  277. {
  278. char *cmd = req->buf;
  279. char response[RESPONSE_LEN];
  280. const char *s;
  281. size_t chars_left;
  282. strcpy(response, "OKAY");
  283. chars_left = sizeof(response) - strlen(response) - 1;
  284. strsep(&cmd, ":");
  285. if (!cmd) {
  286. error("missing variable\n");
  287. fastboot_tx_write_str("FAILmissing var");
  288. return;
  289. }
  290. if (!strcmp_l1("version", cmd)) {
  291. strncat(response, FASTBOOT_VERSION, chars_left);
  292. } else if (!strcmp_l1("bootloader-version", cmd)) {
  293. strncat(response, U_BOOT_VERSION, chars_left);
  294. } else if (!strcmp_l1("downloadsize", cmd)) {
  295. char str_num[12];
  296. sprintf(str_num, "%08x", CONFIG_USB_FASTBOOT_BUF_SIZE);
  297. strncat(response, str_num, chars_left);
  298. } else if (!strcmp_l1("serialno", cmd)) {
  299. s = getenv("serial#");
  300. if (s)
  301. strncat(response, s, chars_left);
  302. else
  303. strcpy(response, "FAILValue not set");
  304. } else {
  305. error("unknown variable: %s\n", cmd);
  306. strcpy(response, "FAILVariable not implemented");
  307. }
  308. fastboot_tx_write_str(response);
  309. }
  310. static unsigned int rx_bytes_expected(void)
  311. {
  312. int rx_remain = download_size - download_bytes;
  313. if (rx_remain < 0)
  314. return 0;
  315. if (rx_remain > EP_BUFFER_SIZE)
  316. return EP_BUFFER_SIZE;
  317. return rx_remain;
  318. }
  319. #define BYTES_PER_DOT 0x20000
  320. static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
  321. {
  322. char response[RESPONSE_LEN];
  323. unsigned int transfer_size = download_size - download_bytes;
  324. const unsigned char *buffer = req->buf;
  325. unsigned int buffer_size = req->actual;
  326. if (req->status != 0) {
  327. printf("Bad status: %d\n", req->status);
  328. return;
  329. }
  330. if (buffer_size < transfer_size)
  331. transfer_size = buffer_size;
  332. memcpy((void *)CONFIG_USB_FASTBOOT_BUF_ADDR + download_bytes,
  333. buffer, transfer_size);
  334. download_bytes += transfer_size;
  335. /* Check if transfer is done */
  336. if (download_bytes >= download_size) {
  337. /*
  338. * Reset global transfer variable, keep download_bytes because
  339. * it will be used in the next possible flashing command
  340. */
  341. download_size = 0;
  342. req->complete = rx_handler_command;
  343. req->length = EP_BUFFER_SIZE;
  344. sprintf(response, "OKAY");
  345. fastboot_tx_write_str(response);
  346. printf("\ndownloading of %d bytes finished\n", download_bytes);
  347. } else {
  348. req->length = rx_bytes_expected();
  349. if (req->length < ep->maxpacket)
  350. req->length = ep->maxpacket;
  351. }
  352. if (download_bytes && !(download_bytes % BYTES_PER_DOT)) {
  353. putc('.');
  354. if (!(download_bytes % (74 * BYTES_PER_DOT)))
  355. putc('\n');
  356. }
  357. req->actual = 0;
  358. usb_ep_queue(ep, req, 0);
  359. }
  360. static void cb_download(struct usb_ep *ep, struct usb_request *req)
  361. {
  362. char *cmd = req->buf;
  363. char response[RESPONSE_LEN];
  364. strsep(&cmd, ":");
  365. download_size = simple_strtoul(cmd, NULL, 16);
  366. download_bytes = 0;
  367. printf("Starting download of %d bytes\n", download_size);
  368. if (0 == download_size) {
  369. sprintf(response, "FAILdata invalid size");
  370. } else if (download_size > CONFIG_USB_FASTBOOT_BUF_SIZE) {
  371. download_size = 0;
  372. sprintf(response, "FAILdata too large");
  373. } else {
  374. sprintf(response, "DATA%08x", download_size);
  375. req->complete = rx_handler_dl_image;
  376. req->length = rx_bytes_expected();
  377. if (req->length < ep->maxpacket)
  378. req->length = ep->maxpacket;
  379. }
  380. fastboot_tx_write_str(response);
  381. }
  382. static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
  383. {
  384. char boot_addr_start[12];
  385. char *bootm_args[] = { "bootm", boot_addr_start, NULL };
  386. puts("Booting kernel..\n");
  387. sprintf(boot_addr_start, "0x%lx", load_addr);
  388. do_bootm(NULL, 0, 2, bootm_args);
  389. /* This only happens if image is somehow faulty so we start over */
  390. do_reset(NULL, 0, 0, NULL);
  391. }
  392. static void cb_boot(struct usb_ep *ep, struct usb_request *req)
  393. {
  394. fastboot_func->in_req->complete = do_bootm_on_complete;
  395. fastboot_tx_write_str("OKAY");
  396. }
  397. #ifdef CONFIG_FASTBOOT_FLASH
  398. static void cb_flash(struct usb_ep *ep, struct usb_request *req)
  399. {
  400. char *cmd = req->buf;
  401. char response[RESPONSE_LEN];
  402. strsep(&cmd, ":");
  403. if (!cmd) {
  404. error("missing partition name\n");
  405. fastboot_tx_write_str("FAILmissing partition name");
  406. return;
  407. }
  408. strcpy(response, "FAILno flash device defined");
  409. #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
  410. fb_mmc_flash_write(cmd, (void *)CONFIG_USB_FASTBOOT_BUF_ADDR,
  411. download_bytes, response);
  412. #endif
  413. fastboot_tx_write_str(response);
  414. }
  415. #endif
  416. struct cmd_dispatch_info {
  417. char *cmd;
  418. void (*cb)(struct usb_ep *ep, struct usb_request *req);
  419. };
  420. static const struct cmd_dispatch_info cmd_dispatch_info[] = {
  421. {
  422. .cmd = "reboot",
  423. .cb = cb_reboot,
  424. }, {
  425. .cmd = "getvar:",
  426. .cb = cb_getvar,
  427. }, {
  428. .cmd = "download:",
  429. .cb = cb_download,
  430. }, {
  431. .cmd = "boot",
  432. .cb = cb_boot,
  433. },
  434. #ifdef CONFIG_FASTBOOT_FLASH
  435. {
  436. .cmd = "flash",
  437. .cb = cb_flash,
  438. },
  439. #endif
  440. };
  441. static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
  442. {
  443. char *cmdbuf = req->buf;
  444. void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
  445. int i;
  446. for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
  447. if (!strcmp_l1(cmd_dispatch_info[i].cmd, cmdbuf)) {
  448. func_cb = cmd_dispatch_info[i].cb;
  449. break;
  450. }
  451. }
  452. if (!func_cb) {
  453. error("unknown command: %s\n", cmdbuf);
  454. fastboot_tx_write_str("FAILunknown command");
  455. } else {
  456. func_cb(ep, req);
  457. }
  458. if (req->status == 0) {
  459. *cmdbuf = '\0';
  460. req->actual = 0;
  461. usb_ep_queue(ep, req, 0);
  462. }
  463. }