dfu.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * cmd_dfu.c -- dfu command
  3. *
  4. * Copyright (C) 2015
  5. * Lukasz Majewski <l.majewski@majess.pl>
  6. *
  7. * Copyright (C) 2012 Samsung Electronics
  8. * authors: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
  9. * Lukasz Majewski <l.majewski@samsung.com>
  10. *
  11. * SPDX-License-Identifier: GPL-2.0+
  12. */
  13. #include <common.h>
  14. #include <watchdog.h>
  15. #include <dfu.h>
  16. #include <console.h>
  17. #include <g_dnl.h>
  18. #include <usb.h>
  19. #include <net.h>
  20. static int do_dfu(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  21. {
  22. bool dfu_reset = false;
  23. if (argc < 4)
  24. return CMD_RET_USAGE;
  25. char *usb_controller = argv[1];
  26. char *interface = argv[2];
  27. char *devstring = argv[3];
  28. int ret, i = 0;
  29. #ifdef CONFIG_DFU_TFTP
  30. unsigned long addr = 0;
  31. if (!strcmp(argv[1], "tftp")) {
  32. if (argc == 5)
  33. addr = simple_strtoul(argv[4], NULL, 0);
  34. return update_tftp(addr, interface, devstring);
  35. }
  36. #endif
  37. ret = dfu_init_env_entities(interface, devstring);
  38. if (ret)
  39. goto done;
  40. ret = CMD_RET_SUCCESS;
  41. if (argc > 4 && strcmp(argv[4], "list") == 0) {
  42. dfu_show_entities();
  43. goto done;
  44. }
  45. int controller_index = simple_strtoul(usb_controller, NULL, 0);
  46. board_usb_init(controller_index, USB_INIT_DEVICE);
  47. g_dnl_clear_detach();
  48. g_dnl_register("usb_dnl_dfu");
  49. while (1) {
  50. if (g_dnl_detach()) {
  51. /*
  52. * Check if USB bus reset is performed after detach,
  53. * which indicates that -R switch has been passed to
  54. * dfu-util. In this case reboot the device
  55. */
  56. if (dfu_usb_get_reset()) {
  57. dfu_reset = true;
  58. goto exit;
  59. }
  60. /*
  61. * This extra number of usb_gadget_handle_interrupts()
  62. * calls is necessary to assure correct transmission
  63. * completion with dfu-util
  64. */
  65. if (++i == 10000)
  66. goto exit;
  67. }
  68. if (ctrlc())
  69. goto exit;
  70. if (dfu_get_defer_flush()) {
  71. /*
  72. * Call to usb_gadget_handle_interrupts() is necessary
  73. * to act on ZLP OUT transaction from HOST PC after
  74. * transmitting the whole file.
  75. *
  76. * If this ZLP OUT packet is NAK'ed, the HOST libusb
  77. * function fails after timeout (by default it is set to
  78. * 5 seconds). In such situation the dfu-util program
  79. * exits with error message.
  80. */
  81. usb_gadget_handle_interrupts(controller_index);
  82. ret = dfu_flush(dfu_get_defer_flush(), NULL, 0, 0);
  83. dfu_set_defer_flush(NULL);
  84. if (ret) {
  85. error("Deferred dfu_flush() failed!");
  86. goto exit;
  87. }
  88. }
  89. WATCHDOG_RESET();
  90. usb_gadget_handle_interrupts(controller_index);
  91. }
  92. exit:
  93. g_dnl_unregister();
  94. board_usb_cleanup(controller_index, USB_INIT_DEVICE);
  95. done:
  96. dfu_free_entities();
  97. if (dfu_reset)
  98. run_command("reset", 0);
  99. g_dnl_clear_detach();
  100. return ret;
  101. }
  102. U_BOOT_CMD(dfu, CONFIG_SYS_MAXARGS, 1, do_dfu,
  103. "Device Firmware Upgrade",
  104. "<USB_controller> <interface> <dev> [list]\n"
  105. " - device firmware upgrade via <USB_controller>\n"
  106. " on device <dev>, attached to interface\n"
  107. " <interface>\n"
  108. " [list] - list available alt settings\n"
  109. #ifdef CONFIG_DFU_TFTP
  110. "dfu tftp <interface> <dev> [<addr>]\n"
  111. " - device firmware upgrade via TFTP\n"
  112. " on device <dev>, attached to interface\n"
  113. " <interface>\n"
  114. " [<addr>] - address where FIT image has been stored\n"
  115. #endif
  116. );