cmd_dfu.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 <dfu.h>
  12. #include <g_dnl.h>
  13. #include <usb.h>
  14. static int do_dfu(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  15. {
  16. bool dfu_reset = false;
  17. if (argc < 4)
  18. return CMD_RET_USAGE;
  19. char *usb_controller = argv[1];
  20. char *interface = argv[2];
  21. char *devstring = argv[3];
  22. int ret, i = 0;
  23. ret = dfu_init_env_entities(interface, devstring);
  24. if (ret)
  25. goto done;
  26. ret = CMD_RET_SUCCESS;
  27. if (argc > 4 && strcmp(argv[4], "list") == 0) {
  28. dfu_show_entities();
  29. goto done;
  30. }
  31. int controller_index = simple_strtoul(usb_controller, NULL, 0);
  32. board_usb_init(controller_index, USB_INIT_DEVICE);
  33. dfu_clear_detach();
  34. g_dnl_register("usb_dnl_dfu");
  35. while (1) {
  36. if (dfu_detach()) {
  37. /*
  38. * Check if USB bus reset is performed after detach,
  39. * which indicates that -R switch has been passed to
  40. * dfu-util. In this case reboot the device
  41. */
  42. if (dfu_usb_get_reset()) {
  43. dfu_reset = true;
  44. goto exit;
  45. }
  46. /*
  47. * This extra number of usb_gadget_handle_interrupts()
  48. * calls is necessary to assure correct transmission
  49. * completion with dfu-util
  50. */
  51. if (++i == 10000)
  52. goto exit;
  53. }
  54. if (ctrlc())
  55. goto exit;
  56. usb_gadget_handle_interrupts();
  57. }
  58. exit:
  59. g_dnl_unregister();
  60. done:
  61. dfu_free_entities();
  62. if (dfu_reset)
  63. run_command("reset", 0);
  64. dfu_clear_detach();
  65. return ret;
  66. }
  67. U_BOOT_CMD(dfu, CONFIG_SYS_MAXARGS, 1, do_dfu,
  68. "Device Firmware Upgrade",
  69. "<USB_controller> <interface> <dev> [list]\n"
  70. " - device firmware upgrade via <USB_controller>\n"
  71. " on device <dev>, attached to interface\n"
  72. " <interface>\n"
  73. " [list] - list available alt settings\n"
  74. );