1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6 
7 #include <stdio.h>
8 #include <bootretry.h>
9 #include <cli.h>
10 #include <env.h>
11 #include <errno.h>
12 #include <time.h>
13 #include <vsprintf.h>
14 #include <watchdog.h>
15 
16 static uint64_t endtime;  /* must be set, default is instant timeout */
17 static int      retry_time = -1; /* -1 so can call readline before main_loop */
18 
19 /***************************************************************************
20  * initialize command line timeout
21  */
bootretry_init_cmd_timeout(void)22 void bootretry_init_cmd_timeout(void)
23 {
24 	char *s = env_get("bootretry");
25 
26 	if (s != NULL)
27 		retry_time = (int)simple_strtol(s, NULL, 10);
28 	else
29 		retry_time = CONFIG_BOOT_RETRY_TIME;
30 
31 	if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
32 		retry_time = CONFIG_BOOT_RETRY_MIN;
33 }
34 
35 /***************************************************************************
36  * reset command line timeout to retry_time seconds
37  */
bootretry_reset_cmd_timeout(void)38 void bootretry_reset_cmd_timeout(void)
39 {
40 	/* Parse changes to bootretry */
41 	bootretry_init_cmd_timeout();
42 	endtime = endtick(retry_time);
43 }
44 
bootretry_tstc_timeout(void)45 int bootretry_tstc_timeout(void)
46 {
47 	while (!tstc()) {	/* while no incoming data */
48 		if (retry_time >= 0 && get_ticks() > endtime)
49 			return -ETIMEDOUT;
50 		schedule();
51 	}
52 
53 	return 0;
54 }
55 
bootretry_dont_retry(void)56 void bootretry_dont_retry(void)
57 {
58 	retry_time = -1;
59 }
60