autoboot.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <autoboot.h>
  9. #include <bootretry.h>
  10. #include <cli.h>
  11. #include <fdtdec.h>
  12. #include <menu.h>
  13. #include <post.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. #define MAX_DELAY_STOP_STR 32
  16. #ifndef DEBUG_BOOTKEYS
  17. #define DEBUG_BOOTKEYS 0
  18. #endif
  19. #define debug_bootkeys(fmt, args...) \
  20. debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
  21. /* Stored value of bootdelay, used by autoboot_command() */
  22. static int stored_bootdelay;
  23. /***************************************************************************
  24. * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
  25. * returns: 0 - no key string, allow autoboot 1 - got key string, abort
  26. */
  27. # if defined(CONFIG_AUTOBOOT_KEYED)
  28. static int abortboot_keyed(int bootdelay)
  29. {
  30. int abort = 0;
  31. uint64_t etime = endtick(bootdelay);
  32. struct {
  33. char *str;
  34. u_int len;
  35. int retry;
  36. }
  37. delaykey[] = {
  38. { .str = getenv("bootdelaykey"), .retry = 1 },
  39. { .str = getenv("bootstopkey"), .retry = 0 },
  40. };
  41. char presskey[MAX_DELAY_STOP_STR];
  42. u_int presskey_len = 0;
  43. u_int presskey_max = 0;
  44. u_int i;
  45. #ifndef CONFIG_ZERO_BOOTDELAY_CHECK
  46. if (bootdelay == 0)
  47. return 0;
  48. #endif
  49. # ifdef CONFIG_AUTOBOOT_PROMPT
  50. /*
  51. * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
  52. * To print the bootdelay value upon bootup.
  53. */
  54. printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
  55. # endif
  56. # ifdef CONFIG_AUTOBOOT_DELAY_STR
  57. if (delaykey[0].str == NULL)
  58. delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
  59. # endif
  60. # ifdef CONFIG_AUTOBOOT_STOP_STR
  61. if (delaykey[1].str == NULL)
  62. delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
  63. # endif
  64. for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
  65. delaykey[i].len = delaykey[i].str == NULL ?
  66. 0 : strlen(delaykey[i].str);
  67. delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
  68. MAX_DELAY_STOP_STR : delaykey[i].len;
  69. presskey_max = presskey_max > delaykey[i].len ?
  70. presskey_max : delaykey[i].len;
  71. debug_bootkeys("%s key:<%s>\n",
  72. delaykey[i].retry ? "delay" : "stop",
  73. delaykey[i].str ? delaykey[i].str : "NULL");
  74. }
  75. /* In order to keep up with incoming data, check timeout only
  76. * when catch up.
  77. */
  78. do {
  79. if (tstc()) {
  80. if (presskey_len < presskey_max) {
  81. presskey[presskey_len++] = getc();
  82. } else {
  83. for (i = 0; i < presskey_max - 1; i++)
  84. presskey[i] = presskey[i + 1];
  85. presskey[i] = getc();
  86. }
  87. }
  88. for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
  89. if (delaykey[i].len > 0 &&
  90. presskey_len >= delaykey[i].len &&
  91. memcmp(presskey + presskey_len -
  92. delaykey[i].len, delaykey[i].str,
  93. delaykey[i].len) == 0) {
  94. debug_bootkeys("got %skey\n",
  95. delaykey[i].retry ? "delay" :
  96. "stop");
  97. /* don't retry auto boot */
  98. if (!delaykey[i].retry)
  99. bootretry_dont_retry();
  100. abort = 1;
  101. }
  102. }
  103. } while (!abort && get_ticks() <= etime);
  104. if (!abort)
  105. debug_bootkeys("key timeout\n");
  106. #ifdef CONFIG_SILENT_CONSOLE
  107. if (abort)
  108. gd->flags &= ~GD_FLG_SILENT;
  109. #endif
  110. return abort;
  111. }
  112. # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
  113. #ifdef CONFIG_MENUKEY
  114. static int menukey;
  115. #endif
  116. static int abortboot_normal(int bootdelay)
  117. {
  118. int abort = 0;
  119. unsigned long ts;
  120. #ifdef CONFIG_MENUPROMPT
  121. printf(CONFIG_MENUPROMPT);
  122. #else
  123. if (bootdelay >= 0)
  124. printf("Hit any key to stop autoboot: %2d ", bootdelay);
  125. #endif
  126. #if defined CONFIG_ZERO_BOOTDELAY_CHECK
  127. /*
  128. * Check if key already pressed
  129. * Don't check if bootdelay < 0
  130. */
  131. if (bootdelay >= 0) {
  132. if (tstc()) { /* we got a key press */
  133. (void) getc(); /* consume input */
  134. puts("\b\b\b 0");
  135. abort = 1; /* don't auto boot */
  136. }
  137. }
  138. #endif
  139. while ((bootdelay > 0) && (!abort)) {
  140. --bootdelay;
  141. /* delay 1000 ms */
  142. ts = get_timer(0);
  143. do {
  144. if (tstc()) { /* we got a key press */
  145. abort = 1; /* don't auto boot */
  146. bootdelay = 0; /* no more delay */
  147. # ifdef CONFIG_MENUKEY
  148. menukey = getc();
  149. # else
  150. (void) getc(); /* consume input */
  151. # endif
  152. break;
  153. }
  154. udelay(10000);
  155. } while (!abort && get_timer(ts) < 1000);
  156. printf("\b\b\b%2d ", bootdelay);
  157. }
  158. putc('\n');
  159. #ifdef CONFIG_SILENT_CONSOLE
  160. if (abort)
  161. gd->flags &= ~GD_FLG_SILENT;
  162. #endif
  163. return abort;
  164. }
  165. # endif /* CONFIG_AUTOBOOT_KEYED */
  166. static int abortboot(int bootdelay)
  167. {
  168. #ifdef CONFIG_AUTOBOOT_KEYED
  169. return abortboot_keyed(bootdelay);
  170. #else
  171. return abortboot_normal(bootdelay);
  172. #endif
  173. }
  174. static void process_fdt_options(const void *blob)
  175. {
  176. #if defined(CONFIG_OF_CONTROL)
  177. ulong addr;
  178. /* Add an env variable to point to a kernel payload, if available */
  179. addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
  180. if (addr)
  181. setenv_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
  182. /* Add an env variable to point to a root disk, if available */
  183. addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
  184. if (addr)
  185. setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
  186. #endif /* CONFIG_OF_CONTROL */
  187. }
  188. const char *bootdelay_process(void)
  189. {
  190. char *s;
  191. int bootdelay;
  192. #ifdef CONFIG_BOOTCOUNT_LIMIT
  193. unsigned long bootcount = 0;
  194. unsigned long bootlimit = 0;
  195. #endif /* CONFIG_BOOTCOUNT_LIMIT */
  196. #ifdef CONFIG_BOOTCOUNT_LIMIT
  197. bootcount = bootcount_load();
  198. bootcount++;
  199. bootcount_store(bootcount);
  200. setenv_ulong("bootcount", bootcount);
  201. bootlimit = getenv_ulong("bootlimit", 10, 0);
  202. #endif /* CONFIG_BOOTCOUNT_LIMIT */
  203. s = getenv("bootdelay");
  204. bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
  205. #ifdef CONFIG_OF_CONTROL
  206. bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
  207. bootdelay);
  208. #endif
  209. debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
  210. #if defined(CONFIG_MENU_SHOW)
  211. bootdelay = menu_show(bootdelay);
  212. #endif
  213. bootretry_init_cmd_timeout();
  214. #ifdef CONFIG_POST
  215. if (gd->flags & GD_FLG_POSTFAIL) {
  216. s = getenv("failbootcmd");
  217. } else
  218. #endif /* CONFIG_POST */
  219. #ifdef CONFIG_BOOTCOUNT_LIMIT
  220. if (bootlimit && (bootcount > bootlimit)) {
  221. printf("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
  222. (unsigned)bootlimit);
  223. s = getenv("altbootcmd");
  224. } else
  225. #endif /* CONFIG_BOOTCOUNT_LIMIT */
  226. s = getenv("bootcmd");
  227. process_fdt_options(gd->fdt_blob);
  228. stored_bootdelay = bootdelay;
  229. return s;
  230. }
  231. void autoboot_command(const char *s)
  232. {
  233. debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
  234. if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
  235. #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
  236. int prev = disable_ctrlc(1); /* disable Control C checking */
  237. #endif
  238. run_command_list(s, -1, 0);
  239. #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
  240. disable_ctrlc(prev); /* restore Control C checking */
  241. #endif
  242. }
  243. #ifdef CONFIG_MENUKEY
  244. if (menukey == CONFIG_MENUKEY) {
  245. s = getenv("menucmd");
  246. if (s)
  247. run_command_list(s, -1, 0);
  248. }
  249. #endif /* CONFIG_MENUKEY */
  250. }