f_fastboot.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
  44. {
  45. return container_of(f, struct f_fastboot, usb_function);
  46. }
  47. static struct f_fastboot *fastboot_func;
  48. static struct usb_endpoint_descriptor fs_ep_in = {
  49. .bLength = USB_DT_ENDPOINT_SIZE,
  50. .bDescriptorType = USB_DT_ENDPOINT,
  51. .bEndpointAddress = USB_DIR_IN,
  52. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  53. .wMaxPacketSize = cpu_to_le16(64),
  54. };
  55. static struct usb_endpoint_descriptor fs_ep_out = {
  56. .bLength = USB_DT_ENDPOINT_SIZE,
  57. .bDescriptorType = USB_DT_ENDPOINT,
  58. .bEndpointAddress = USB_DIR_OUT,
  59. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  60. .wMaxPacketSize = cpu_to_le16(64),
  61. };
  62. static struct usb_endpoint_descriptor hs_ep_in = {
  63. .bLength = USB_DT_ENDPOINT_SIZE,
  64. .bDescriptorType = USB_DT_ENDPOINT,
  65. .bEndpointAddress = USB_DIR_IN,
  66. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  67. .wMaxPacketSize = cpu_to_le16(512),
  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 = cpu_to_le16(512),
  75. };
  76. static struct usb_interface_descriptor interface_desc = {
  77. .bLength = USB_DT_INTERFACE_SIZE,
  78. .bDescriptorType = USB_DT_INTERFACE,
  79. .bInterfaceNumber = 0x00,
  80. .bAlternateSetting = 0x00,
  81. .bNumEndpoints = 0x02,
  82. .bInterfaceClass = FASTBOOT_INTERFACE_CLASS,
  83. .bInterfaceSubClass = FASTBOOT_INTERFACE_SUB_CLASS,
  84. .bInterfaceProtocol = FASTBOOT_INTERFACE_PROTOCOL,
  85. };
  86. static struct usb_descriptor_header *fb_fs_function[] = {
  87. (struct usb_descriptor_header *)&interface_desc,
  88. (struct usb_descriptor_header *)&fs_ep_in,
  89. (struct usb_descriptor_header *)&fs_ep_out,
  90. };
  91. static struct usb_descriptor_header *fb_hs_function[] = {
  92. (struct usb_descriptor_header *)&interface_desc,
  93. (struct usb_descriptor_header *)&hs_ep_in,
  94. (struct usb_descriptor_header *)&hs_ep_out,
  95. NULL,
  96. };
  97. static struct usb_endpoint_descriptor *
  98. fb_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
  99. struct usb_endpoint_descriptor *hs)
  100. {
  101. if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
  102. return hs;
  103. return fs;
  104. }
  105. /*
  106. * static strings, in UTF-8
  107. */
  108. static const char fastboot_name[] = "Android Fastboot";
  109. static struct usb_string fastboot_string_defs[] = {
  110. [0].s = fastboot_name,
  111. { } /* end of list */
  112. };
  113. static struct usb_gadget_strings stringtab_fastboot = {
  114. .language = 0x0409, /* en-us */
  115. .strings = fastboot_string_defs,
  116. };
  117. static struct usb_gadget_strings *fastboot_strings[] = {
  118. &stringtab_fastboot,
  119. NULL,
  120. };
  121. static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
  122. static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
  123. {
  124. int status = req->status;
  125. if (!status)
  126. return;
  127. printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
  128. }
  129. static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
  130. {
  131. int id;
  132. struct usb_gadget *gadget = c->cdev->gadget;
  133. struct f_fastboot *f_fb = func_to_fastboot(f);
  134. const char *s;
  135. /* DYNAMIC interface numbers assignments */
  136. id = usb_interface_id(c, f);
  137. if (id < 0)
  138. return id;
  139. interface_desc.bInterfaceNumber = id;
  140. id = usb_string_id(c->cdev);
  141. if (id < 0)
  142. return id;
  143. fastboot_string_defs[0].id = id;
  144. interface_desc.iInterface = id;
  145. f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
  146. if (!f_fb->in_ep)
  147. return -ENODEV;
  148. f_fb->in_ep->driver_data = c->cdev;
  149. f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
  150. if (!f_fb->out_ep)
  151. return -ENODEV;
  152. f_fb->out_ep->driver_data = c->cdev;
  153. f->descriptors = fb_fs_function;
  154. if (gadget_is_dualspeed(gadget)) {
  155. /* Assume endpoint addresses are the same for both speeds */
  156. hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
  157. hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
  158. /* copy HS descriptors */
  159. f->hs_descriptors = fb_hs_function;
  160. }
  161. s = env_get("serial#");
  162. if (s)
  163. g_dnl_set_serialnumber((char *)s);
  164. return 0;
  165. }
  166. static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
  167. {
  168. memset(fastboot_func, 0, sizeof(*fastboot_func));
  169. }
  170. static void fastboot_disable(struct usb_function *f)
  171. {
  172. struct f_fastboot *f_fb = func_to_fastboot(f);
  173. usb_ep_disable(f_fb->out_ep);
  174. usb_ep_disable(f_fb->in_ep);
  175. if (f_fb->out_req) {
  176. free(f_fb->out_req->buf);
  177. usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
  178. f_fb->out_req = NULL;
  179. }
  180. if (f_fb->in_req) {
  181. free(f_fb->in_req->buf);
  182. usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
  183. f_fb->in_req = NULL;
  184. }
  185. }
  186. static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
  187. {
  188. struct usb_request *req;
  189. req = usb_ep_alloc_request(ep, 0);
  190. if (!req)
  191. return NULL;
  192. req->length = EP_BUFFER_SIZE;
  193. req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
  194. if (!req->buf) {
  195. usb_ep_free_request(ep, req);
  196. return NULL;
  197. }
  198. memset(req->buf, 0, req->length);
  199. return req;
  200. }
  201. static int fastboot_set_alt(struct usb_function *f,
  202. unsigned interface, unsigned alt)
  203. {
  204. int ret;
  205. struct usb_composite_dev *cdev = f->config->cdev;
  206. struct usb_gadget *gadget = cdev->gadget;
  207. struct f_fastboot *f_fb = func_to_fastboot(f);
  208. const struct usb_endpoint_descriptor *d;
  209. debug("%s: func: %s intf: %d alt: %d\n",
  210. __func__, f->name, interface, alt);
  211. d = fb_ep_desc(gadget, &fs_ep_out, &hs_ep_out);
  212. ret = usb_ep_enable(f_fb->out_ep, d);
  213. if (ret) {
  214. puts("failed to enable out ep\n");
  215. return ret;
  216. }
  217. f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
  218. if (!f_fb->out_req) {
  219. puts("failed to alloc out req\n");
  220. ret = -EINVAL;
  221. goto err;
  222. }
  223. f_fb->out_req->complete = rx_handler_command;
  224. d = fb_ep_desc(gadget, &fs_ep_in, &hs_ep_in);
  225. ret = usb_ep_enable(f_fb->in_ep, d);
  226. if (ret) {
  227. puts("failed to enable in ep\n");
  228. goto err;
  229. }
  230. f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
  231. if (!f_fb->in_req) {
  232. puts("failed alloc req in\n");
  233. ret = -EINVAL;
  234. goto err;
  235. }
  236. f_fb->in_req->complete = fastboot_complete;
  237. ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
  238. if (ret)
  239. goto err;
  240. return 0;
  241. err:
  242. fastboot_disable(f);
  243. return ret;
  244. }
  245. static int fastboot_add(struct usb_configuration *c)
  246. {
  247. struct f_fastboot *f_fb = fastboot_func;
  248. int status;
  249. debug("%s: cdev: 0x%p\n", __func__, c->cdev);
  250. if (!f_fb) {
  251. f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
  252. if (!f_fb)
  253. return -ENOMEM;
  254. fastboot_func = f_fb;
  255. memset(f_fb, 0, sizeof(*f_fb));
  256. }
  257. f_fb->usb_function.name = "f_fastboot";
  258. f_fb->usb_function.bind = fastboot_bind;
  259. f_fb->usb_function.unbind = fastboot_unbind;
  260. f_fb->usb_function.set_alt = fastboot_set_alt;
  261. f_fb->usb_function.disable = fastboot_disable;
  262. f_fb->usb_function.strings = fastboot_strings;
  263. status = usb_add_function(c, &f_fb->usb_function);
  264. if (status) {
  265. free(f_fb);
  266. fastboot_func = NULL;
  267. }
  268. return status;
  269. }
  270. DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
  271. static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
  272. {
  273. struct usb_request *in_req = fastboot_func->in_req;
  274. int ret;
  275. memcpy(in_req->buf, buffer, buffer_size);
  276. in_req->length = buffer_size;
  277. usb_ep_dequeue(fastboot_func->in_ep, in_req);
  278. ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
  279. if (ret)
  280. printf("Error %d on queue\n", ret);
  281. return 0;
  282. }
  283. static int fastboot_tx_write_str(const char *buffer)
  284. {
  285. return fastboot_tx_write(buffer, strlen(buffer));
  286. }
  287. static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
  288. {
  289. do_reset(NULL, 0, 0, NULL);
  290. }
  291. static unsigned int rx_bytes_expected(struct usb_ep *ep)
  292. {
  293. int rx_remain = fastboot_data_remaining();
  294. unsigned int rem;
  295. unsigned int maxpacket = ep->maxpacket;
  296. if (rx_remain <= 0)
  297. return 0;
  298. else if (rx_remain > EP_BUFFER_SIZE)
  299. return EP_BUFFER_SIZE;
  300. /*
  301. * Some controllers e.g. DWC3 don't like OUT transfers to be
  302. * not ending in maxpacket boundary. So just make them happy by
  303. * always requesting for integral multiple of maxpackets.
  304. * This shouldn't bother controllers that don't care about it.
  305. */
  306. rem = rx_remain % maxpacket;
  307. if (rem > 0)
  308. rx_remain = rx_remain + (maxpacket - rem);
  309. return rx_remain;
  310. }
  311. static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
  312. {
  313. char response[FASTBOOT_RESPONSE_LEN] = {0};
  314. unsigned int transfer_size = fastboot_data_remaining();
  315. const unsigned char *buffer = req->buf;
  316. unsigned int buffer_size = req->actual;
  317. if (req->status != 0) {
  318. printf("Bad status: %d\n", req->status);
  319. return;
  320. }
  321. if (buffer_size < transfer_size)
  322. transfer_size = buffer_size;
  323. fastboot_data_download(buffer, transfer_size, response);
  324. if (response[0]) {
  325. fastboot_tx_write_str(response);
  326. } else if (!fastboot_data_remaining()) {
  327. fastboot_data_complete(response);
  328. /*
  329. * Reset global transfer variable
  330. */
  331. req->complete = rx_handler_command;
  332. req->length = EP_BUFFER_SIZE;
  333. fastboot_tx_write_str(response);
  334. } else {
  335. req->length = rx_bytes_expected(ep);
  336. }
  337. req->actual = 0;
  338. usb_ep_queue(ep, req, 0);
  339. }
  340. static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
  341. {
  342. g_dnl_trigger_detach();
  343. }
  344. static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
  345. {
  346. fastboot_boot();
  347. do_exit_on_complete(ep, req);
  348. }
  349. static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
  350. {
  351. char *cmdbuf = req->buf;
  352. char response[FASTBOOT_RESPONSE_LEN] = {0};
  353. int cmd = -1;
  354. if (req->status != 0 || req->length == 0)
  355. return;
  356. if (req->actual < req->length) {
  357. cmdbuf[req->actual] = '\0';
  358. cmd = fastboot_handle_command(cmdbuf, response);
  359. } else {
  360. pr_err("buffer overflow");
  361. fastboot_fail("buffer overflow", response);
  362. }
  363. if (!strncmp("DATA", response, 4)) {
  364. req->complete = rx_handler_dl_image;
  365. req->length = rx_bytes_expected(ep);
  366. }
  367. if (!strncmp("OKAY", response, 4)) {
  368. switch (cmd) {
  369. case FASTBOOT_COMMAND_BOOT:
  370. fastboot_func->in_req->complete = do_bootm_on_complete;
  371. break;
  372. case FASTBOOT_COMMAND_CONTINUE:
  373. fastboot_func->in_req->complete = do_exit_on_complete;
  374. break;
  375. case FASTBOOT_COMMAND_REBOOT:
  376. case FASTBOOT_COMMAND_REBOOT_BOOTLOADER:
  377. case FASTBOOT_COMMAND_REBOOT_FASTBOOTD:
  378. case FASTBOOT_COMMAND_REBOOT_RECOVERY:
  379. fastboot_func->in_req->complete = compl_do_reset;
  380. break;
  381. }
  382. }
  383. fastboot_tx_write_str(response);
  384. *cmdbuf = '\0';
  385. req->actual = 0;
  386. usb_ep_queue(ep, req, 0);
  387. }