f_fastboot.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2008 - 2009
  4. * Windriver, <www.windriver.com>
  5. * Tom Rix <Tom.Rix@windriver.com>
  6. *
  7. * Copyright 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
  8. *
  9. * Copyright 2014 Linaro, Ltd.
  10. * Rob Herring <robh@kernel.org>
  11. */
  12. #include <command.h>
  13. #include <config.h>
  14. #include <common.h>
  15. #include <env.h>
  16. #include <errno.h>
  17. #include <fastboot.h>
  18. #include <log.h>
  19. #include <malloc.h>
  20. #include <linux/usb/ch9.h>
  21. #include <linux/usb/gadget.h>
  22. #include <linux/usb/composite.h>
  23. #include <linux/compiler.h>
  24. #include <g_dnl.h>
  25. #define FASTBOOT_INTERFACE_CLASS 0xff
  26. #define FASTBOOT_INTERFACE_SUB_CLASS 0x42
  27. #define FASTBOOT_INTERFACE_PROTOCOL 0x03
  28. #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0 (0x0200)
  29. #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1 (0x0040)
  30. #define TX_ENDPOINT_MAXIMUM_PACKET_SIZE (0x0040)
  31. #define EP_BUFFER_SIZE 4096
  32. /*
  33. * EP_BUFFER_SIZE must always be an integral multiple of maxpacket size
  34. * (64 or 512 or 1024), else we break on certain controllers like DWC3
  35. * that expect bulk OUT requests to be divisible by maxpacket size.
  36. */
  37. struct f_fastboot {
  38. struct usb_function usb_function;
  39. /* IN/OUT EP's and corresponding requests */
  40. struct usb_ep *in_ep, *out_ep;
  41. struct usb_request *in_req, *out_req;
  42. };
  43. static char fb_ext_prop_name[] = "DeviceInterfaceGUID";
  44. static char fb_ext_prop_data[] = "{4866319A-F4D6-4374-93B9-DC2DEB361BA9}";
  45. static struct usb_os_desc_ext_prop fb_ext_prop = {
  46. .type = 1, /* NUL-terminated Unicode String (REG_SZ) */
  47. .name = fb_ext_prop_name,
  48. .data = fb_ext_prop_data,
  49. };
  50. /* 16 bytes of "Compatible ID" and "Subcompatible ID" */
  51. static char fb_cid[16] = {'W', 'I', 'N', 'U', 'S', 'B'};
  52. static struct usb_os_desc fb_os_desc = {
  53. .ext_compat_id = fb_cid,
  54. };
  55. static struct usb_os_desc_table fb_os_desc_table = {
  56. .os_desc = &fb_os_desc,
  57. };
  58. static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
  59. {
  60. return container_of(f, struct f_fastboot, usb_function);
  61. }
  62. static struct f_fastboot *fastboot_func;
  63. static struct usb_endpoint_descriptor fs_ep_in = {
  64. .bLength = USB_DT_ENDPOINT_SIZE,
  65. .bDescriptorType = USB_DT_ENDPOINT,
  66. .bEndpointAddress = USB_DIR_IN,
  67. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  68. .wMaxPacketSize = cpu_to_le16(64),
  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,
  75. .wMaxPacketSize = cpu_to_le16(64),
  76. };
  77. static struct usb_endpoint_descriptor hs_ep_in = {
  78. .bLength = USB_DT_ENDPOINT_SIZE,
  79. .bDescriptorType = USB_DT_ENDPOINT,
  80. .bEndpointAddress = USB_DIR_IN,
  81. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  82. .wMaxPacketSize = cpu_to_le16(512),
  83. };
  84. static struct usb_endpoint_descriptor hs_ep_out = {
  85. .bLength = USB_DT_ENDPOINT_SIZE,
  86. .bDescriptorType = USB_DT_ENDPOINT,
  87. .bEndpointAddress = USB_DIR_OUT,
  88. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  89. .wMaxPacketSize = cpu_to_le16(512),
  90. };
  91. static struct usb_interface_descriptor interface_desc = {
  92. .bLength = USB_DT_INTERFACE_SIZE,
  93. .bDescriptorType = USB_DT_INTERFACE,
  94. .bInterfaceNumber = 0x00,
  95. .bAlternateSetting = 0x00,
  96. .bNumEndpoints = 0x02,
  97. .bInterfaceClass = FASTBOOT_INTERFACE_CLASS,
  98. .bInterfaceSubClass = FASTBOOT_INTERFACE_SUB_CLASS,
  99. .bInterfaceProtocol = FASTBOOT_INTERFACE_PROTOCOL,
  100. };
  101. static struct usb_descriptor_header *fb_fs_function[] = {
  102. (struct usb_descriptor_header *)&interface_desc,
  103. (struct usb_descriptor_header *)&fs_ep_in,
  104. (struct usb_descriptor_header *)&fs_ep_out,
  105. };
  106. static struct usb_descriptor_header *fb_hs_function[] = {
  107. (struct usb_descriptor_header *)&interface_desc,
  108. (struct usb_descriptor_header *)&hs_ep_in,
  109. (struct usb_descriptor_header *)&hs_ep_out,
  110. NULL,
  111. };
  112. /* Super speed */
  113. static struct usb_endpoint_descriptor ss_ep_in = {
  114. .bLength = USB_DT_ENDPOINT_SIZE,
  115. .bDescriptorType = USB_DT_ENDPOINT,
  116. .bEndpointAddress = USB_DIR_IN,
  117. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  118. .wMaxPacketSize = cpu_to_le16(1024),
  119. };
  120. static struct usb_endpoint_descriptor ss_ep_out = {
  121. .bLength = USB_DT_ENDPOINT_SIZE,
  122. .bDescriptorType = USB_DT_ENDPOINT,
  123. .bEndpointAddress = USB_DIR_OUT,
  124. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  125. .wMaxPacketSize = cpu_to_le16(1024),
  126. };
  127. static struct usb_ss_ep_comp_descriptor fb_ss_bulk_comp_desc = {
  128. .bLength = sizeof(fb_ss_bulk_comp_desc),
  129. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  130. };
  131. static struct usb_descriptor_header *fb_ss_function[] = {
  132. (struct usb_descriptor_header *)&interface_desc,
  133. (struct usb_descriptor_header *)&ss_ep_in,
  134. (struct usb_descriptor_header *)&fb_ss_bulk_comp_desc,
  135. (struct usb_descriptor_header *)&ss_ep_out,
  136. (struct usb_descriptor_header *)&fb_ss_bulk_comp_desc,
  137. NULL,
  138. };
  139. static struct usb_endpoint_descriptor *
  140. fb_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
  141. struct usb_endpoint_descriptor *hs,
  142. struct usb_endpoint_descriptor *ss)
  143. {
  144. if (gadget_is_superspeed(g) && g->speed >= USB_SPEED_SUPER)
  145. return ss;
  146. if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
  147. return hs;
  148. return fs;
  149. }
  150. /*
  151. * static strings, in UTF-8
  152. */
  153. static const char fastboot_name[] = "Android Fastboot";
  154. static struct usb_string fastboot_string_defs[] = {
  155. [0].s = fastboot_name,
  156. { } /* end of list */
  157. };
  158. static struct usb_gadget_strings stringtab_fastboot = {
  159. .language = 0x0409, /* en-us */
  160. .strings = fastboot_string_defs,
  161. };
  162. static struct usb_gadget_strings *fastboot_strings[] = {
  163. &stringtab_fastboot,
  164. NULL,
  165. };
  166. static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
  167. static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
  168. {
  169. int status = req->status;
  170. if (!status)
  171. return;
  172. printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
  173. }
  174. static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
  175. {
  176. int id;
  177. struct usb_gadget *gadget = c->cdev->gadget;
  178. struct f_fastboot *f_fb = func_to_fastboot(f);
  179. const char *s;
  180. /* DYNAMIC interface numbers assignments */
  181. id = usb_interface_id(c, f);
  182. if (id < 0)
  183. return id;
  184. interface_desc.bInterfaceNumber = id;
  185. /* Enable OS and Extended Properties Feature Descriptor */
  186. c->cdev->use_os_string = 1;
  187. f->os_desc_table = &fb_os_desc_table;
  188. f->os_desc_n = 1;
  189. f->os_desc_table->if_id = id;
  190. INIT_LIST_HEAD(&fb_os_desc.ext_prop);
  191. fb_ext_prop.name_len = strlen(fb_ext_prop.name) * 2 + 2;
  192. fb_os_desc.ext_prop_len = 10 + fb_ext_prop.name_len;
  193. fb_os_desc.ext_prop_count = 1;
  194. fb_ext_prop.data_len = strlen(fb_ext_prop.data) * 2 + 2;
  195. fb_os_desc.ext_prop_len += fb_ext_prop.data_len + 4;
  196. list_add_tail(&fb_ext_prop.entry, &fb_os_desc.ext_prop);
  197. id = usb_string_id(c->cdev);
  198. if (id < 0)
  199. return id;
  200. fastboot_string_defs[0].id = id;
  201. interface_desc.iInterface = id;
  202. f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
  203. if (!f_fb->in_ep)
  204. return -ENODEV;
  205. f_fb->in_ep->driver_data = c->cdev;
  206. f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
  207. if (!f_fb->out_ep)
  208. return -ENODEV;
  209. f_fb->out_ep->driver_data = c->cdev;
  210. f->descriptors = fb_fs_function;
  211. if (gadget_is_dualspeed(gadget)) {
  212. /* Assume endpoint addresses are the same for both speeds */
  213. hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
  214. hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
  215. /* copy HS descriptors */
  216. f->hs_descriptors = fb_hs_function;
  217. }
  218. if (gadget_is_superspeed(gadget)) {
  219. ss_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
  220. ss_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
  221. f->ss_descriptors = fb_ss_function;
  222. }
  223. s = env_get("serial#");
  224. if (s)
  225. g_dnl_set_serialnumber((char *)s);
  226. return 0;
  227. }
  228. static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
  229. {
  230. f->os_desc_table = NULL;
  231. list_del(&fb_os_desc.ext_prop);
  232. memset(fastboot_func, 0, sizeof(*fastboot_func));
  233. }
  234. static void fastboot_disable(struct usb_function *f)
  235. {
  236. struct f_fastboot *f_fb = func_to_fastboot(f);
  237. usb_ep_disable(f_fb->out_ep);
  238. usb_ep_disable(f_fb->in_ep);
  239. if (f_fb->out_req) {
  240. free(f_fb->out_req->buf);
  241. usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
  242. f_fb->out_req = NULL;
  243. }
  244. if (f_fb->in_req) {
  245. free(f_fb->in_req->buf);
  246. usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
  247. f_fb->in_req = NULL;
  248. }
  249. }
  250. static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
  251. {
  252. struct usb_request *req;
  253. req = usb_ep_alloc_request(ep, 0);
  254. if (!req)
  255. return NULL;
  256. req->length = EP_BUFFER_SIZE;
  257. req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
  258. if (!req->buf) {
  259. usb_ep_free_request(ep, req);
  260. return NULL;
  261. }
  262. memset(req->buf, 0, req->length);
  263. return req;
  264. }
  265. static int fastboot_set_alt(struct usb_function *f,
  266. unsigned interface, unsigned alt)
  267. {
  268. int ret;
  269. struct usb_composite_dev *cdev = f->config->cdev;
  270. struct usb_gadget *gadget = cdev->gadget;
  271. struct f_fastboot *f_fb = func_to_fastboot(f);
  272. const struct usb_endpoint_descriptor *d;
  273. debug("%s: func: %s intf: %d alt: %d\n",
  274. __func__, f->name, interface, alt);
  275. d = fb_ep_desc(gadget, &fs_ep_out, &hs_ep_out, &ss_ep_out);
  276. ret = usb_ep_enable(f_fb->out_ep, d);
  277. if (ret) {
  278. puts("failed to enable out ep\n");
  279. return ret;
  280. }
  281. f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
  282. if (!f_fb->out_req) {
  283. puts("failed to alloc out req\n");
  284. ret = -EINVAL;
  285. goto err;
  286. }
  287. f_fb->out_req->complete = rx_handler_command;
  288. d = fb_ep_desc(gadget, &fs_ep_in, &hs_ep_in, &ss_ep_in);
  289. ret = usb_ep_enable(f_fb->in_ep, d);
  290. if (ret) {
  291. puts("failed to enable in ep\n");
  292. goto err;
  293. }
  294. f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
  295. if (!f_fb->in_req) {
  296. puts("failed alloc req in\n");
  297. ret = -EINVAL;
  298. goto err;
  299. }
  300. f_fb->in_req->complete = fastboot_complete;
  301. ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
  302. if (ret)
  303. goto err;
  304. return 0;
  305. err:
  306. fastboot_disable(f);
  307. return ret;
  308. }
  309. static int fastboot_add(struct usb_configuration *c)
  310. {
  311. struct f_fastboot *f_fb = fastboot_func;
  312. int status;
  313. debug("%s: cdev: 0x%p\n", __func__, c->cdev);
  314. if (!f_fb) {
  315. f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
  316. if (!f_fb)
  317. return -ENOMEM;
  318. fastboot_func = f_fb;
  319. memset(f_fb, 0, sizeof(*f_fb));
  320. }
  321. f_fb->usb_function.name = "f_fastboot";
  322. f_fb->usb_function.bind = fastboot_bind;
  323. f_fb->usb_function.unbind = fastboot_unbind;
  324. f_fb->usb_function.set_alt = fastboot_set_alt;
  325. f_fb->usb_function.disable = fastboot_disable;
  326. f_fb->usb_function.strings = fastboot_strings;
  327. status = usb_add_function(c, &f_fb->usb_function);
  328. if (status) {
  329. free(f_fb);
  330. fastboot_func = NULL;
  331. }
  332. return status;
  333. }
  334. DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
  335. static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
  336. {
  337. struct usb_request *in_req = fastboot_func->in_req;
  338. int ret;
  339. memcpy(in_req->buf, buffer, buffer_size);
  340. in_req->length = buffer_size;
  341. usb_ep_dequeue(fastboot_func->in_ep, in_req);
  342. ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
  343. if (ret)
  344. printf("Error %d on queue\n", ret);
  345. return 0;
  346. }
  347. static int fastboot_tx_write_str(const char *buffer)
  348. {
  349. return fastboot_tx_write(buffer, strlen(buffer));
  350. }
  351. static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
  352. {
  353. do_reset(NULL, 0, 0, NULL);
  354. }
  355. static unsigned int rx_bytes_expected(struct usb_ep *ep)
  356. {
  357. int rx_remain = fastboot_data_remaining();
  358. unsigned int rem;
  359. unsigned int maxpacket = usb_endpoint_maxp(ep->desc);
  360. if (rx_remain <= 0)
  361. return 0;
  362. else if (rx_remain > EP_BUFFER_SIZE)
  363. return EP_BUFFER_SIZE;
  364. /*
  365. * Some controllers e.g. DWC3 don't like OUT transfers to be
  366. * not ending in maxpacket boundary. So just make them happy by
  367. * always requesting for integral multiple of maxpackets.
  368. * This shouldn't bother controllers that don't care about it.
  369. */
  370. rem = rx_remain % maxpacket;
  371. if (rem > 0)
  372. rx_remain = rx_remain + (maxpacket - rem);
  373. return rx_remain;
  374. }
  375. static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
  376. {
  377. char response[FASTBOOT_RESPONSE_LEN] = {0};
  378. unsigned int transfer_size = fastboot_data_remaining();
  379. const unsigned char *buffer = req->buf;
  380. unsigned int buffer_size = req->actual;
  381. if (req->status != 0) {
  382. printf("Bad status: %d\n", req->status);
  383. return;
  384. }
  385. if (buffer_size < transfer_size)
  386. transfer_size = buffer_size;
  387. fastboot_data_download(buffer, transfer_size, response);
  388. if (response[0]) {
  389. fastboot_tx_write_str(response);
  390. } else if (!fastboot_data_remaining()) {
  391. fastboot_data_complete(response);
  392. /*
  393. * Reset global transfer variable
  394. */
  395. req->complete = rx_handler_command;
  396. req->length = EP_BUFFER_SIZE;
  397. fastboot_tx_write_str(response);
  398. } else {
  399. req->length = rx_bytes_expected(ep);
  400. }
  401. req->actual = 0;
  402. usb_ep_queue(ep, req, 0);
  403. }
  404. static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
  405. {
  406. g_dnl_trigger_detach();
  407. }
  408. static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
  409. {
  410. fastboot_boot();
  411. do_exit_on_complete(ep, req);
  412. }
  413. #if CONFIG_IS_ENABLED(FASTBOOT_UUU_SUPPORT)
  414. static void do_acmd_complete(struct usb_ep *ep, struct usb_request *req)
  415. {
  416. /* When usb dequeue complete will be called
  417. * Need status value before call run_command.
  418. * otherwise, host can't get last message.
  419. */
  420. if (req->status == 0)
  421. fastboot_acmd_complete();
  422. }
  423. #endif
  424. static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
  425. {
  426. char *cmdbuf = req->buf;
  427. char response[FASTBOOT_RESPONSE_LEN] = {0};
  428. int cmd = -1;
  429. if (req->status != 0 || req->length == 0)
  430. return;
  431. if (req->actual < req->length) {
  432. cmdbuf[req->actual] = '\0';
  433. cmd = fastboot_handle_command(cmdbuf, response);
  434. } else {
  435. pr_err("buffer overflow");
  436. fastboot_fail("buffer overflow", response);
  437. }
  438. if (!strncmp("DATA", response, 4)) {
  439. req->complete = rx_handler_dl_image;
  440. req->length = rx_bytes_expected(ep);
  441. }
  442. if (!strncmp("OKAY", response, 4)) {
  443. switch (cmd) {
  444. case FASTBOOT_COMMAND_BOOT:
  445. fastboot_func->in_req->complete = do_bootm_on_complete;
  446. break;
  447. case FASTBOOT_COMMAND_CONTINUE:
  448. fastboot_func->in_req->complete = do_exit_on_complete;
  449. break;
  450. case FASTBOOT_COMMAND_REBOOT:
  451. case FASTBOOT_COMMAND_REBOOT_BOOTLOADER:
  452. case FASTBOOT_COMMAND_REBOOT_FASTBOOTD:
  453. case FASTBOOT_COMMAND_REBOOT_RECOVERY:
  454. fastboot_func->in_req->complete = compl_do_reset;
  455. break;
  456. #if CONFIG_IS_ENABLED(FASTBOOT_UUU_SUPPORT)
  457. case FASTBOOT_COMMAND_ACMD:
  458. fastboot_func->in_req->complete = do_acmd_complete;
  459. break;
  460. #endif
  461. }
  462. }
  463. fastboot_tx_write_str(response);
  464. *cmdbuf = '\0';
  465. req->actual = 0;
  466. usb_ep_queue(ep, req, 0);
  467. }