f_fastboot.c 11 KB

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