autoboot.c 7.0 KB

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