diff options
author | Marek BehĂșn <marek.behun@nic.cz> | 2021-09-24 23:07:07 +0200 |
---|---|---|
committer | Stefan Roese <sr@denx.de> | 2021-10-01 11:07:13 +0200 |
commit | 99a3d02370b6b526594ee73eb26d39e8d8ced8d3 (patch) | |
tree | 6e0a38737770af1451084464cf6b645fbe2c2f16 /tools | |
parent | 93b55636b09faaa0ab809d4110a07dfd982a48b6 (diff) | |
download | u-boot-99a3d02370b6b526594ee73eb26d39e8d8ced8d3.zip u-boot-99a3d02370b6b526594ee73eb26d39e8d8ced8d3.tar.gz u-boot-99a3d02370b6b526594ee73eb26d39e8d8ced8d3.tar.bz2 |
tools: kwboot: Check whether baudrate was set to requested value
The tcsetattr() function can return 0 even if baudrate was not changed.
Check whether baudrate was changed to requested value, and in case of
arbitrary baudrate, check whether the set value is within 3% tolerance.
Signed-off-by: Marek BehĂșn <marek.behun@nic.cz>
Reviewed-by: Stefan Roese <sr@denx.de>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/kwboot.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/tools/kwboot.c b/tools/kwboot.c index 7ccab29..d8b9507 100644 --- a/tools/kwboot.c +++ b/tools/kwboot.c @@ -568,6 +568,13 @@ kwboot_tty_baudrate_to_speed(int baudrate) } static int +_is_within_tolerance(int value, int reference, int tolerance) +{ + return 100 * value >= reference * (100 - tolerance) && + 100 * value <= reference * (100 + tolerance); +} + +static int kwboot_tty_change_baudrate(int fd, int baudrate) { struct termios tio; @@ -601,7 +608,32 @@ kwboot_tty_change_baudrate(int fd, int baudrate) if (rc) return rc; + rc = tcgetattr(fd, &tio); + if (rc) + return rc; + + if (cfgetospeed(&tio) != speed || cfgetispeed(&tio) != speed) + goto baud_fail; + +#ifdef BOTHER + /* + * Check whether set baudrate is within 3% tolerance. + * If BOTHER is defined, Linux always fills out c_ospeed / c_ispeed + * with real values. + */ + if (!_is_within_tolerance(tio.c_ospeed, baudrate, 3)) + goto baud_fail; + + if (!_is_within_tolerance(tio.c_ispeed, baudrate, 3)) + goto baud_fail; +#endif + return 0; + +baud_fail: + fprintf(stderr, "Could not set baudrate to requested value\n"); + errno = EINVAL; + return -1; } static int |