From: Yuya Hamamachi <[email protected]> The net_loop() currently conflates return value with file size at the end of successful transfer, in NETLOOP_SUCCESS state.
The return type of net_loop() is int, which makes this practice workable for file sizes below 2 GiB, but anything above that will lead to overflow and bogus negative return value from net_loop(). The return file size is only used by a few sites in the code base, which can be easily fixed. Change the net_loop() return value to always be only a return code, in case of error the returned value is the error code, in case of successful transfer the value is 0 or 1 instead of 0 or net_boot_file_size . This surely always fits into a signed integer. By keeping the return code 0 or 1 in case of successful transfer, no conditionals which depended on the old behavior are broken, but all the sites had to be inspected and updated accordingly. Fix the few sites which depend on the file size by making them directly use the net_boot_file_size variable value. This variable is accessible to all of those sites already, because they all include net-common.h . Signed-off-by: Yuya Hamamachi <[email protected]> Signed-off-by: Marek Vasut <[email protected]> --- Cc: "Alvin Šipraga" <[email protected]> Cc: Alexander Sverdlin <[email protected]> Cc: Andre Przywara <[email protected]> Cc: Andrew Goodbody <[email protected]> Cc: Ilias Apalodimas <[email protected]> Cc: Jerome Forissier <[email protected]> Cc: Joe Hershberger <[email protected]> Cc: Leonard Anderweit <[email protected]> Cc: Marek Vasut <[email protected]> Cc: Mattijs Korpershoek <[email protected]> Cc: Quentin Schulz <[email protected]> Cc: Ramon Fried <[email protected]> Cc: Simon Glass <[email protected]> Cc: Tom Rini <[email protected]> Cc: Yuya Hamamachi <[email protected]> Cc: [email protected] --- cmd/mvebu/bubt.c | 2 +- cmd/net.c | 9 +++++---- common/update.c | 10 +++++----- net/net.c | 2 +- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/cmd/mvebu/bubt.c b/cmd/mvebu/bubt.c index 2755c26cdf7..f8e97d03920 100644 --- a/cmd/mvebu/bubt.c +++ b/cmd/mvebu/bubt.c @@ -661,7 +661,7 @@ static size_t tftp_read_file(const char *file_name) */ image_load_addr = get_load_addr(); ret = net_loop(TFTPGET); - return ret > 0 ? ret : 0; + return ret > 0 ? net_boot_file_size : 0; } static int is_tftp_active(void) diff --git a/cmd/net.c b/cmd/net.c index 24099764493..f6f556f36ae 100644 --- a/cmd/net.c +++ b/cmd/net.c @@ -354,8 +354,8 @@ static int netboot_common(enum proto_t proto, struct cmd_tbl *cmdtp, int argc, char *const argv[]) { char *s; - int rcode = 0; - int size; + int rcode; + u32 size; net_boot_file_name_explicit = false; *net_boot_file_name = '\0'; @@ -396,8 +396,9 @@ static int netboot_common(enum proto_t proto, struct cmd_tbl *cmdtp, int argc, } } - size = net_loop(proto); - if (size < 0) { + rcode = net_loop(proto); + size = net_boot_file_size; + if (rcode < 0) { bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK); return CMD_RET_FAILURE; } diff --git a/common/update.c b/common/update.c index 6801b49479d..0bafffede9e 100644 --- a/common/update.c +++ b/common/update.c @@ -32,7 +32,7 @@ static uchar *saved_prot_info; #endif static int update_load(char *filename, ulong msec_max, int cnt_max, ulong addr) { - int size, rv; + int rv; ulong saved_timeout_msecs; int saved_timeout_count; char *saved_netretry, *saved_bootfile; @@ -54,12 +54,12 @@ static int update_load(char *filename, ulong msec_max, int cnt_max, ulong addr) /* download the update file */ image_load_addr = addr; copy_filename(net_boot_file_name, filename, sizeof(net_boot_file_name)); - size = net_loop(TFTPGET); + rv = net_loop(TFTPGET); - if (size < 0) + if (rv < 0) rv = 1; - else if (size > 0) - flush_cache(addr, size); + else + flush_cache(addr, net_boot_file_size); /* restore changed globals and env variable */ tftp_timeout_ms = saved_timeout_msecs; diff --git a/net/net.c b/net/net.c index 8a8160e633f..ae3b977781f 100644 --- a/net/net.c +++ b/net/net.c @@ -692,7 +692,7 @@ restart: eth_set_last_protocol(protocol); - ret = net_boot_file_size; + ret = !!net_boot_file_size; debug_cond(DEBUG_INT_STATE, "--- net_loop Success!\n"); goto done; -- 2.51.0

