f_fastboot.c 11 KB

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