cmd_dfu.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * cmd_dfu.c -- dfu command
  3. *
  4. * Copyright (C) 2012 Samsung Electronics
  5. * authors: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
  6. * Lukasz Majewski <l.majewski@samsung.com>
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <watchdog.h>
  12. #include <dfu.h>
  13. #include <g_dnl.h>
  14. #include <usb.h>
  15. static int do_dfu(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  16. {
  17. bool dfu_reset = false;
  18. if (argc < 4)
  19. return CMD_RET_USAGE;
  20. char *usb_controller = argv[1];
  21. char *interface = argv[2];
  22. char *devstring = argv[3];
  23. int ret, i = 0;
  24. ret = dfu_init_env_entities(interface, devstring);
  25. if (ret)
  26. goto done;
  27. ret = CMD_RET_SUCCESS;
  28. if (argc > 4 && strcmp(argv[4], "list") == 0) {
  29. dfu_show_entities();
  30. goto done;
  31. }
  32. int controller_index = simple_strtoul(usb_controller, NULL, 0);
  33. board_usb_init(controller_index, USB_INIT_DEVICE);
  34. g_dnl_clear_detach();
  35. g_dnl_register("usb_dnl_dfu");
  36. while (1) {
  37. if (g_dnl_detach()) {
  38. /*
  39. * Check if USB bus reset is performed after detach,
  40. * which indicates that -R switch has been passed to
  41. * dfu-util. In this case reboot the device
  42. */
  43. if (dfu_usb_get_reset()) {
  44. dfu_reset = true;
  45. goto exit;
  46. }
  47. /*
  48. * This extra number of usb_gadget_handle_interrupts()
  49. * calls is necessary to assure correct transmission
  50. * completion with dfu-util
  51. */
  52. if (++i == 10000)
  53. goto exit;
  54. }
  55. if (ctrlc())
  56. goto exit;
  57. WATCHDOG_RESET();
  58. usb_gadget_handle_interrupts(controller_index);
  59. }
  60. exit:
  61. g_dnl_unregister();
  62. board_usb_cleanup(controller_index, USB_INIT_DEVICE);
  63. done:
  64. dfu_free_entities();
  65. if (dfu_reset)
  66. run_command("reset", 0);
  67. g_dnl_clear_detach();
  68. return ret;
  69. }
  70. U_BOOT_CMD(dfu, CONFIG_SYS_MAXARGS, 1, do_dfu,
  71. "Device Firmware Upgrade",
  72. "<USB_controller> <interface> <dev> [list]\n"
  73. " - device firmware upgrade via <USB_controller>\n"
  74. " on device <dev>, attached to interface\n"
  75. " <interface>\n"
  76. " [list] - list available alt settings\n"
  77. );