Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 399ed9a | 2014-04-10 20:01:30 -0600 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2000 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Simon Glass | 399ed9a | 2014-04-10 20:01:30 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <bootretry.h> |
| 9 | #include <cli.h> |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 10 | #include <env.h> |
Simon Glass | 399ed9a | 2014-04-10 20:01:30 -0600 | [diff] [blame] | 11 | #include <errno.h> |
Simon Glass | 495a5dc | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 12 | #include <time.h> |
Simon Glass | 399ed9a | 2014-04-10 20:01:30 -0600 | [diff] [blame] | 13 | #include <watchdog.h> |
| 14 | |
| 15 | #ifndef CONFIG_BOOT_RETRY_MIN |
| 16 | #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME |
| 17 | #endif |
| 18 | |
| 19 | static uint64_t endtime; /* must be set, default is instant timeout */ |
| 20 | static int retry_time = -1; /* -1 so can call readline before main_loop */ |
| 21 | |
| 22 | /*************************************************************************** |
| 23 | * initialize command line timeout |
| 24 | */ |
Simon Glass | 09007c4 | 2014-04-10 20:01:31 -0600 | [diff] [blame] | 25 | void bootretry_init_cmd_timeout(void) |
Simon Glass | 399ed9a | 2014-04-10 20:01:30 -0600 | [diff] [blame] | 26 | { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 27 | char *s = env_get("bootretry"); |
Simon Glass | 399ed9a | 2014-04-10 20:01:30 -0600 | [diff] [blame] | 28 | |
| 29 | if (s != NULL) |
| 30 | retry_time = (int)simple_strtol(s, NULL, 10); |
| 31 | else |
| 32 | retry_time = CONFIG_BOOT_RETRY_TIME; |
| 33 | |
| 34 | if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN) |
| 35 | retry_time = CONFIG_BOOT_RETRY_MIN; |
| 36 | } |
| 37 | |
| 38 | /*************************************************************************** |
| 39 | * reset command line timeout to retry_time seconds |
| 40 | */ |
Simon Glass | 09007c4 | 2014-04-10 20:01:31 -0600 | [diff] [blame] | 41 | void bootretry_reset_cmd_timeout(void) |
Simon Glass | 399ed9a | 2014-04-10 20:01:30 -0600 | [diff] [blame] | 42 | { |
| 43 | endtime = endtick(retry_time); |
| 44 | } |
| 45 | |
| 46 | int bootretry_tstc_timeout(void) |
| 47 | { |
| 48 | while (!tstc()) { /* while no incoming data */ |
| 49 | if (retry_time >= 0 && get_ticks() > endtime) |
| 50 | return -ETIMEDOUT; |
| 51 | WATCHDOG_RESET(); |
| 52 | } |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | void bootretry_dont_retry(void) |
| 58 | { |
| 59 | retry_time = -1; |
| 60 | } |