f_fastboot.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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 <fastboot.h>
  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>
  24. #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
  25. #include <fb_mmc.h>
  26. #endif
  27. #ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
  28. #include <fb_nand.h>
  29. #endif
  30. #define FASTBOOT_VERSION "0.4"
  31. #define FASTBOOT_INTERFACE_CLASS 0xff
  32. #define FASTBOOT_INTERFACE_SUB_CLASS 0x42
  33. #define FASTBOOT_INTERFACE_PROTOCOL 0x03
  34. #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0 (0x0200)
  35. #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1 (0x0040)
  36. #define TX_ENDPOINT_MAXIMUM_PACKET_SIZE (0x0040)
  37. #define EP_BUFFER_SIZE 4096
  38. struct f_fastboot {
  39. struct usb_function usb_function;
  40. /* IN/OUT EP's and corresponding requests */
  41. struct usb_ep *in_ep, *out_ep;
  42. struct usb_request *in_req, *out_req;
  43. };
  44. static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
  45. {
  46. return container_of(f, struct f_fastboot, usb_function);
  47. }
  48. static struct f_fastboot *fastboot_func;
  49. static unsigned int fastboot_flash_session_id;
  50. static unsigned int download_size;
  51. static unsigned int download_bytes;
  52. static bool is_high_speed;
  53. static struct usb_endpoint_descriptor fs_ep_in = {
  54. .bLength = USB_DT_ENDPOINT_SIZE,
  55. .bDescriptorType = USB_DT_ENDPOINT,
  56. .bEndpointAddress = USB_DIR_IN,
  57. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  58. .wMaxPacketSize = TX_ENDPOINT_MAXIMUM_PACKET_SIZE,
  59. .bInterval = 0x00,
  60. };
  61. static struct usb_endpoint_descriptor fs_ep_out = {
  62. .bLength = USB_DT_ENDPOINT_SIZE,
  63. .bDescriptorType = USB_DT_ENDPOINT,
  64. .bEndpointAddress = USB_DIR_OUT,
  65. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  66. .wMaxPacketSize = RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1,
  67. .bInterval = 0x00,
  68. };
  69. static struct usb_endpoint_descriptor hs_ep_out = {
  70. .bLength = USB_DT_ENDPOINT_SIZE,
  71. .bDescriptorType = USB_DT_ENDPOINT,
  72. .bEndpointAddress = USB_DIR_OUT,
  73. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  74. .wMaxPacketSize = RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0,
  75. .bInterval = 0x00,
  76. };
  77. static struct usb_interface_descriptor interface_desc = {
  78. .bLength = USB_DT_INTERFACE_SIZE,
  79. .bDescriptorType = USB_DT_INTERFACE,
  80. .bInterfaceNumber = 0x00,
  81. .bAlternateSetting = 0x00,
  82. .bNumEndpoints = 0x02,
  83. .bInterfaceClass = FASTBOOT_INTERFACE_CLASS,
  84. .bInterfaceSubClass = FASTBOOT_INTERFACE_SUB_CLASS,
  85. .bInterfaceProtocol = FASTBOOT_INTERFACE_PROTOCOL,
  86. };
  87. static struct usb_descriptor_header *fb_runtime_descs[] = {
  88. (struct usb_descriptor_header *)&interface_desc,
  89. (struct usb_descriptor_header *)&fs_ep_in,
  90. (struct usb_descriptor_header *)&hs_ep_out,
  91. NULL,
  92. };
  93. /*
  94. * static strings, in UTF-8
  95. */
  96. static const char fastboot_name[] = "Android Fastboot";
  97. static struct usb_string fastboot_string_defs[] = {
  98. [0].s = fastboot_name,
  99. { } /* end of list */
  100. };
  101. static struct usb_gadget_strings stringtab_fastboot = {
  102. .language = 0x0409, /* en-us */
  103. .strings = fastboot_string_defs,
  104. };
  105. static struct usb_gadget_strings *fastboot_strings[] = {
  106. &stringtab_fastboot,
  107. NULL,
  108. };
  109. static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
  110. static int strcmp_l1(const char *s1, const char *s2);
  111. void fastboot_fail(char *response, const char *reason)
  112. {
  113. strncpy(response, "FAIL\0", 5);
  114. strncat(response, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
  115. }
  116. void fastboot_okay(char *response, const char *reason)
  117. {
  118. strncpy(response, "OKAY\0", 5);
  119. strncat(response, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
  120. }
  121. static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
  122. {
  123. int status = req->status;
  124. if (!status)
  125. return;
  126. printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
  127. }
  128. static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
  129. {
  130. int id;
  131. struct usb_gadget *gadget = c->cdev->gadget;
  132. struct f_fastboot *f_fb = func_to_fastboot(f);
  133. const char *s;
  134. /* DYNAMIC interface numbers assignments */
  135. id = usb_interface_id(c, f);
  136. if (id < 0)
  137. return id;
  138. interface_desc.bInterfaceNumber = id;
  139. id = usb_string_id(c->cdev);
  140. if (id < 0)
  141. return id;
  142. fastboot_string_defs[0].id = id;
  143. interface_desc.iInterface = id;
  144. f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
  145. if (!f_fb->in_ep)
  146. return -ENODEV;
  147. f_fb->in_ep->driver_data = c->cdev;
  148. f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
  149. if (!f_fb->out_ep)
  150. return -ENODEV;
  151. f_fb->out_ep->driver_data = c->cdev;
  152. hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
  153. s = getenv("serial#");
  154. if (s)
  155. g_dnl_set_serialnumber((char *)s);
  156. return 0;
  157. }
  158. static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
  159. {
  160. memset(fastboot_func, 0, sizeof(*fastboot_func));
  161. }
  162. static void fastboot_disable(struct usb_function *f)
  163. {
  164. struct f_fastboot *f_fb = func_to_fastboot(f);
  165. usb_ep_disable(f_fb->out_ep);
  166. usb_ep_disable(f_fb->in_ep);
  167. if (f_fb->out_req) {
  168. free(f_fb->out_req->buf);
  169. usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
  170. f_fb->out_req = NULL;
  171. }
  172. if (f_fb->in_req) {
  173. free(f_fb->in_req->buf);
  174. usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
  175. f_fb->in_req = NULL;
  176. }
  177. }
  178. static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
  179. {
  180. struct usb_request *req;
  181. req = usb_ep_alloc_request(ep, 0);
  182. if (!req)
  183. return NULL;
  184. req->length = EP_BUFFER_SIZE;
  185. req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
  186. if (!req->buf) {
  187. usb_ep_free_request(ep, req);
  188. return NULL;
  189. }
  190. memset(req->buf, 0, req->length);
  191. return req;
  192. }
  193. static int fastboot_set_alt(struct usb_function *f,
  194. unsigned interface, unsigned alt)
  195. {
  196. int ret;
  197. struct usb_composite_dev *cdev = f->config->cdev;
  198. struct usb_gadget *gadget = cdev->gadget;
  199. struct f_fastboot *f_fb = func_to_fastboot(f);
  200. debug("%s: func: %s intf: %d alt: %d\n",
  201. __func__, f->name, interface, alt);
  202. /* make sure we don't enable the ep twice */
  203. if (gadget->speed == USB_SPEED_HIGH) {
  204. ret = usb_ep_enable(f_fb->out_ep, &hs_ep_out);
  205. is_high_speed = true;
  206. } else {
  207. ret = usb_ep_enable(f_fb->out_ep, &fs_ep_out);
  208. is_high_speed = false;
  209. }
  210. if (ret) {
  211. puts("failed to enable out ep\n");
  212. return ret;
  213. }
  214. f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
  215. if (!f_fb->out_req) {
  216. puts("failed to alloc out req\n");
  217. ret = -EINVAL;
  218. goto err;
  219. }
  220. f_fb->out_req->complete = rx_handler_command;
  221. ret = usb_ep_enable(f_fb->in_ep, &fs_ep_in);
  222. if (ret) {
  223. puts("failed to enable in ep\n");
  224. goto err;
  225. }
  226. f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
  227. if (!f_fb->in_req) {
  228. puts("failed alloc req in\n");
  229. ret = -EINVAL;
  230. goto err;
  231. }
  232. f_fb->in_req->complete = fastboot_complete;
  233. ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
  234. if (ret)
  235. goto err;
  236. return 0;
  237. err:
  238. fastboot_disable(f);
  239. return ret;
  240. }
  241. static int fastboot_add(struct usb_configuration *c)
  242. {
  243. struct f_fastboot *f_fb = fastboot_func;
  244. int status;
  245. debug("%s: cdev: 0x%p\n", __func__, c->cdev);
  246. if (!f_fb) {
  247. f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
  248. if (!f_fb)
  249. return -ENOMEM;
  250. fastboot_func = f_fb;
  251. memset(f_fb, 0, sizeof(*f_fb));
  252. }
  253. f_fb->usb_function.name = "f_fastboot";
  254. f_fb->usb_function.hs_descriptors = fb_runtime_descs;
  255. f_fb->usb_function.bind = fastboot_bind;
  256. f_fb->usb_function.unbind = fastboot_unbind;
  257. f_fb->usb_function.set_alt = fastboot_set_alt;
  258. f_fb->usb_function.disable = fastboot_disable;
  259. f_fb->usb_function.strings = fastboot_strings;
  260. status = usb_add_function(c, &f_fb->usb_function);
  261. if (status) {
  262. free(f_fb);
  263. fastboot_func = f_fb;
  264. }
  265. return status;
  266. }
  267. DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
  268. static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
  269. {
  270. struct usb_request *in_req = fastboot_func->in_req;
  271. int ret;
  272. memcpy(in_req->buf, buffer, buffer_size);
  273. in_req->length = buffer_size;
  274. usb_ep_dequeue(fastboot_func->in_ep, in_req);
  275. ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
  276. if (ret)
  277. printf("Error %d on queue\n", ret);
  278. return 0;
  279. }
  280. static int fastboot_tx_write_str(const char *buffer)
  281. {
  282. return fastboot_tx_write(buffer, strlen(buffer));
  283. }
  284. static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
  285. {
  286. do_reset(NULL, 0, 0, NULL);
  287. }
  288. int __weak fb_set_reboot_flag(void)
  289. {
  290. return -ENOSYS;
  291. }
  292. static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
  293. {
  294. char *cmd = req->buf;
  295. if (!strcmp_l1("reboot-bootloader", cmd)) {
  296. if (fb_set_reboot_flag()) {
  297. fastboot_tx_write_str("FAILCannot set reboot flag");
  298. return;
  299. }
  300. }
  301. fastboot_func->in_req->complete = compl_do_reset;
  302. fastboot_tx_write_str("OKAY");
  303. }
  304. static int strcmp_l1(const char *s1, const char *s2)
  305. {
  306. if (!s1 || !s2)
  307. return -1;
  308. return strncmp(s1, s2, strlen(s1));
  309. }
  310. static void cb_getvar(struct usb_ep *ep, struct usb_request *req)
  311. {
  312. char *cmd = req->buf;
  313. char response[FASTBOOT_RESPONSE_LEN];
  314. const char *s;
  315. size_t chars_left;
  316. strcpy(response, "OKAY");
  317. chars_left = sizeof(response) - strlen(response) - 1;
  318. strsep(&cmd, ":");
  319. if (!cmd) {
  320. error("missing variable\n");
  321. fastboot_tx_write_str("FAILmissing var");
  322. return;
  323. }
  324. if (!strcmp_l1("version", cmd)) {
  325. strncat(response, FASTBOOT_VERSION, chars_left);
  326. } else if (!strcmp_l1("bootloader-version", cmd)) {
  327. strncat(response, U_BOOT_VERSION, chars_left);
  328. } else if (!strcmp_l1("downloadsize", cmd) ||
  329. !strcmp_l1("max-download-size", cmd)) {
  330. char str_num[12];
  331. sprintf(str_num, "0x%08x", CONFIG_FASTBOOT_BUF_SIZE);
  332. strncat(response, str_num, chars_left);
  333. /*
  334. * This also indicates the start of a new flashing
  335. * "session", in which we could have 1-N buffers to
  336. * write to a partition.
  337. *
  338. * Reset our session counter.
  339. */
  340. fastboot_flash_session_id = 0;
  341. } else if (!strcmp_l1("serialno", cmd)) {
  342. s = getenv("serial#");
  343. if (s)
  344. strncat(response, s, chars_left);
  345. else
  346. strcpy(response, "FAILValue not set");
  347. } else {
  348. error("unknown variable: %s\n", cmd);
  349. strcpy(response, "FAILVariable not implemented");
  350. }
  351. fastboot_tx_write_str(response);
  352. }
  353. static unsigned int rx_bytes_expected(unsigned int maxpacket)
  354. {
  355. int rx_remain = download_size - download_bytes;
  356. int rem = 0;
  357. if (rx_remain < 0)
  358. return 0;
  359. if (rx_remain > EP_BUFFER_SIZE)
  360. return EP_BUFFER_SIZE;
  361. if (rx_remain < maxpacket) {
  362. rx_remain = maxpacket;
  363. } else if (rx_remain % maxpacket != 0) {
  364. rem = rx_remain % maxpacket;
  365. rx_remain = rx_remain + (maxpacket - rem);
  366. }
  367. return rx_remain;
  368. }
  369. #define BYTES_PER_DOT 0x20000
  370. static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
  371. {
  372. char response[FASTBOOT_RESPONSE_LEN];
  373. unsigned int transfer_size = download_size - download_bytes;
  374. const unsigned char *buffer = req->buf;
  375. unsigned int buffer_size = req->actual;
  376. unsigned int pre_dot_num, now_dot_num;
  377. unsigned int max;
  378. if (req->status != 0) {
  379. printf("Bad status: %d\n", req->status);
  380. return;
  381. }
  382. if (buffer_size < transfer_size)
  383. transfer_size = buffer_size;
  384. memcpy((void *)CONFIG_FASTBOOT_BUF_ADDR + download_bytes,
  385. buffer, transfer_size);
  386. pre_dot_num = download_bytes / BYTES_PER_DOT;
  387. download_bytes += transfer_size;
  388. now_dot_num = download_bytes / BYTES_PER_DOT;
  389. if (pre_dot_num != now_dot_num) {
  390. putc('.');
  391. if (!(now_dot_num % 74))
  392. putc('\n');
  393. }
  394. /* Check if transfer is done */
  395. if (download_bytes >= download_size) {
  396. /*
  397. * Reset global transfer variable, keep download_bytes because
  398. * it will be used in the next possible flashing command
  399. */
  400. download_size = 0;
  401. req->complete = rx_handler_command;
  402. req->length = EP_BUFFER_SIZE;
  403. strcpy(response, "OKAY");
  404. fastboot_tx_write_str(response);
  405. printf("\ndownloading of %d bytes finished\n", download_bytes);
  406. } else {
  407. max = is_high_speed ? hs_ep_out.wMaxPacketSize :
  408. fs_ep_out.wMaxPacketSize;
  409. req->length = rx_bytes_expected(max);
  410. if (req->length < ep->maxpacket)
  411. req->length = ep->maxpacket;
  412. }
  413. req->actual = 0;
  414. usb_ep_queue(ep, req, 0);
  415. }
  416. static void cb_download(struct usb_ep *ep, struct usb_request *req)
  417. {
  418. char *cmd = req->buf;
  419. char response[FASTBOOT_RESPONSE_LEN];
  420. unsigned int max;
  421. strsep(&cmd, ":");
  422. download_size = simple_strtoul(cmd, NULL, 16);
  423. download_bytes = 0;
  424. printf("Starting download of %d bytes\n", download_size);
  425. if (0 == download_size) {
  426. strcpy(response, "FAILdata invalid size");
  427. } else if (download_size > CONFIG_FASTBOOT_BUF_SIZE) {
  428. download_size = 0;
  429. strcpy(response, "FAILdata too large");
  430. } else {
  431. sprintf(response, "DATA%08x", download_size);
  432. req->complete = rx_handler_dl_image;
  433. max = is_high_speed ? hs_ep_out.wMaxPacketSize :
  434. fs_ep_out.wMaxPacketSize;
  435. req->length = rx_bytes_expected(max);
  436. if (req->length < ep->maxpacket)
  437. req->length = ep->maxpacket;
  438. }
  439. fastboot_tx_write_str(response);
  440. }
  441. static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
  442. {
  443. char boot_addr_start[12];
  444. char *bootm_args[] = { "bootm", boot_addr_start, NULL };
  445. puts("Booting kernel..\n");
  446. sprintf(boot_addr_start, "0x%lx", load_addr);
  447. do_bootm(NULL, 0, 2, bootm_args);
  448. /* This only happens if image is somehow faulty so we start over */
  449. do_reset(NULL, 0, 0, NULL);
  450. }
  451. static void cb_boot(struct usb_ep *ep, struct usb_request *req)
  452. {
  453. fastboot_func->in_req->complete = do_bootm_on_complete;
  454. fastboot_tx_write_str("OKAY");
  455. }
  456. static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
  457. {
  458. g_dnl_trigger_detach();
  459. }
  460. static void cb_continue(struct usb_ep *ep, struct usb_request *req)
  461. {
  462. fastboot_func->in_req->complete = do_exit_on_complete;
  463. fastboot_tx_write_str("OKAY");
  464. }
  465. #ifdef CONFIG_FASTBOOT_FLASH
  466. static void cb_flash(struct usb_ep *ep, struct usb_request *req)
  467. {
  468. char *cmd = req->buf;
  469. char response[FASTBOOT_RESPONSE_LEN];
  470. strsep(&cmd, ":");
  471. if (!cmd) {
  472. error("missing partition name\n");
  473. fastboot_tx_write_str("FAILmissing partition name");
  474. return;
  475. }
  476. strcpy(response, "FAILno flash device defined");
  477. #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
  478. fb_mmc_flash_write(cmd, fastboot_flash_session_id,
  479. (void *)CONFIG_FASTBOOT_BUF_ADDR,
  480. download_bytes, response);
  481. #endif
  482. #ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
  483. fb_nand_flash_write(cmd, fastboot_flash_session_id,
  484. (void *)CONFIG_FASTBOOT_BUF_ADDR,
  485. download_bytes, response);
  486. #endif
  487. fastboot_flash_session_id++;
  488. fastboot_tx_write_str(response);
  489. }
  490. #endif
  491. static void cb_oem(struct usb_ep *ep, struct usb_request *req)
  492. {
  493. char *cmd = req->buf;
  494. #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
  495. if (strncmp("format", cmd + 4, 6) == 0) {
  496. char cmdbuf[32];
  497. sprintf(cmdbuf, "gpt write mmc %x $partitions",
  498. CONFIG_FASTBOOT_FLASH_MMC_DEV);
  499. if (run_command(cmdbuf, 0))
  500. fastboot_tx_write_str("FAIL");
  501. else
  502. fastboot_tx_write_str("OKAY");
  503. } else
  504. #endif
  505. if (strncmp("unlock", cmd + 4, 8) == 0) {
  506. fastboot_tx_write_str("FAILnot implemented");
  507. }
  508. else {
  509. fastboot_tx_write_str("FAILunknown oem command");
  510. }
  511. }
  512. #ifdef CONFIG_FASTBOOT_FLASH
  513. static void cb_erase(struct usb_ep *ep, struct usb_request *req)
  514. {
  515. char *cmd = req->buf;
  516. char response[FASTBOOT_RESPONSE_LEN];
  517. strsep(&cmd, ":");
  518. if (!cmd) {
  519. error("missing partition name");
  520. fastboot_tx_write_str("FAILmissing partition name");
  521. return;
  522. }
  523. strcpy(response, "FAILno flash device defined");
  524. #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
  525. fb_mmc_erase(cmd, response);
  526. #endif
  527. #ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
  528. fb_nand_erase(cmd, response);
  529. #endif
  530. fastboot_tx_write_str(response);
  531. }
  532. #endif
  533. struct cmd_dispatch_info {
  534. char *cmd;
  535. void (*cb)(struct usb_ep *ep, struct usb_request *req);
  536. };
  537. static const struct cmd_dispatch_info cmd_dispatch_info[] = {
  538. {
  539. .cmd = "reboot",
  540. .cb = cb_reboot,
  541. }, {
  542. .cmd = "getvar:",
  543. .cb = cb_getvar,
  544. }, {
  545. .cmd = "download:",
  546. .cb = cb_download,
  547. }, {
  548. .cmd = "boot",
  549. .cb = cb_boot,
  550. }, {
  551. .cmd = "continue",
  552. .cb = cb_continue,
  553. },
  554. #ifdef CONFIG_FASTBOOT_FLASH
  555. {
  556. .cmd = "flash",
  557. .cb = cb_flash,
  558. }, {
  559. .cmd = "erase",
  560. .cb = cb_erase,
  561. },
  562. #endif
  563. {
  564. .cmd = "oem",
  565. .cb = cb_oem,
  566. },
  567. };
  568. static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
  569. {
  570. char *cmdbuf = req->buf;
  571. void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
  572. int i;
  573. if (req->status != 0 || req->length == 0)
  574. return;
  575. for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
  576. if (!strcmp_l1(cmd_dispatch_info[i].cmd, cmdbuf)) {
  577. func_cb = cmd_dispatch_info[i].cb;
  578. break;
  579. }
  580. }
  581. if (!func_cb) {
  582. error("unknown command: %s\n", cmdbuf);
  583. fastboot_tx_write_str("FAILunknown command");
  584. } else {
  585. if (req->actual < req->length) {
  586. u8 *buf = (u8 *)req->buf;
  587. buf[req->actual] = 0;
  588. func_cb(ep, req);
  589. } else {
  590. error("buffer overflow\n");
  591. fastboot_tx_write_str("FAILbuffer overflow");
  592. }
  593. }
  594. *cmdbuf = '\0';
  595. req->actual = 0;
  596. usb_ep_queue(ep, req, 0);
  597. }