autoboot.c 7.0 KB

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