fb_common.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 <common.h>
  13. #include <command.h>
  14. #include <env.h>
  15. #include <fastboot.h>
  16. #include <net/fastboot.h>
  17. /**
  18. * fastboot_buf_addr - base address of the fastboot download buffer
  19. */
  20. void *fastboot_buf_addr;
  21. /**
  22. * fastboot_buf_size - size of the fastboot download buffer
  23. */
  24. u32 fastboot_buf_size;
  25. /**
  26. * fastboot_progress_callback - callback executed during long operations
  27. */
  28. void (*fastboot_progress_callback)(const char *msg);
  29. /**
  30. * fastboot_response() - Writes a response of the form "$tag$reason".
  31. *
  32. * @tag: The first part of the response
  33. * @response: Pointer to fastboot response buffer
  34. * @format: printf style format string
  35. */
  36. void fastboot_response(const char *tag, char *response,
  37. const char *format, ...)
  38. {
  39. va_list args;
  40. strlcpy(response, tag, FASTBOOT_RESPONSE_LEN);
  41. if (format) {
  42. va_start(args, format);
  43. vsnprintf(response + strlen(response),
  44. FASTBOOT_RESPONSE_LEN - strlen(response) - 1,
  45. format, args);
  46. va_end(args);
  47. }
  48. }
  49. /**
  50. * fastboot_fail() - Write a FAIL response of the form "FAIL$reason".
  51. *
  52. * @reason: Pointer to returned reason string
  53. * @response: Pointer to fastboot response buffer
  54. */
  55. void fastboot_fail(const char *reason, char *response)
  56. {
  57. fastboot_response("FAIL", response, "%s", reason);
  58. }
  59. /**
  60. * fastboot_okay() - Write an OKAY response of the form "OKAY$reason".
  61. *
  62. * @reason: Pointer to returned reason string, or NULL to send a bare "OKAY"
  63. * @response: Pointer to fastboot response buffer
  64. */
  65. void fastboot_okay(const char *reason, char *response)
  66. {
  67. if (reason)
  68. fastboot_response("OKAY", response, "%s", reason);
  69. else
  70. fastboot_response("OKAY", response, NULL);
  71. }
  72. /**
  73. * fastboot_set_reboot_flag() - Set flag to indicate reboot-bootloader
  74. *
  75. * Set flag which indicates that we should reboot into the bootloader
  76. * following the reboot that fastboot executes after this function.
  77. *
  78. * This function should be overridden in your board file with one
  79. * which sets whatever flag your board specific Android bootloader flow
  80. * requires in order to re-enter the bootloader.
  81. */
  82. int __weak fastboot_set_reboot_flag(enum fastboot_reboot_reason reason)
  83. {
  84. return -ENOSYS;
  85. }
  86. /**
  87. * fastboot_get_progress_callback() - Return progress callback
  88. *
  89. * Return: Pointer to function called during long operations
  90. */
  91. void (*fastboot_get_progress_callback(void))(const char *)
  92. {
  93. return fastboot_progress_callback;
  94. }
  95. /**
  96. * fastboot_boot() - Execute fastboot boot command
  97. *
  98. * If ${fastboot_bootcmd} is set, run that command to execute the boot
  99. * process, if that returns, then exit the fastboot server and return
  100. * control to the caller.
  101. *
  102. * Otherwise execute "bootm <fastboot_buf_addr>", if that fails, reset
  103. * the board.
  104. */
  105. void fastboot_boot(void)
  106. {
  107. char *s;
  108. s = env_get("fastboot_bootcmd");
  109. if (s) {
  110. run_command(s, CMD_FLAG_ENV);
  111. } else {
  112. static char boot_addr_start[20];
  113. static char *const bootm_args[] = {
  114. "bootm", boot_addr_start, NULL
  115. };
  116. snprintf(boot_addr_start, sizeof(boot_addr_start) - 1,
  117. "0x%p", fastboot_buf_addr);
  118. printf("Booting kernel at %s...\n\n\n", boot_addr_start);
  119. do_bootm(NULL, 0, 2, bootm_args);
  120. /*
  121. * This only happens if image is somehow faulty so we start
  122. * over. We deliberately leave this policy to the invocation
  123. * of fastbootcmd if that's what's being run
  124. */
  125. do_reset(NULL, 0, 0, NULL);
  126. }
  127. }
  128. /**
  129. * fastboot_set_progress_callback() - set progress callback
  130. *
  131. * @progress: Pointer to progress callback
  132. *
  133. * Set a callback which is invoked periodically during long running operations
  134. * (flash and erase). This can be used (for example) by the UDP transport to
  135. * send INFO responses to keep the client alive whilst those commands are
  136. * executing.
  137. */
  138. void fastboot_set_progress_callback(void (*progress)(const char *msg))
  139. {
  140. fastboot_progress_callback = progress;
  141. }
  142. /*
  143. * fastboot_init() - initialise new fastboot protocol session
  144. *
  145. * @buf_addr: Pointer to download buffer, or NULL for default
  146. * @buf_size: Size of download buffer, or zero for default
  147. */
  148. void fastboot_init(void *buf_addr, u32 buf_size)
  149. {
  150. fastboot_buf_addr = buf_addr ? buf_addr :
  151. (void *)CONFIG_FASTBOOT_BUF_ADDR;
  152. fastboot_buf_size = buf_size ? buf_size : CONFIG_FASTBOOT_BUF_SIZE;
  153. fastboot_set_progress_callback(NULL);
  154. }