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