[U-Boot] [PATCH v3 1/5] common: introduce maximum load size

Rob Herring
2014-04-10T19:18:03Z

From: Rob Herring <robh@kernel.org>

Various commands that load images have no checks that a loaded image
does not exceed the available RAM space and will happily continue
overwriting u-boot or other RAM that should not be touched. Also,
some commands such as USB DFU or fastboot need to know the maximum
buffer size, but there is no common way to define this.

Introduce a global load_size and environment variable loadsize to
specify the size. The default is ~0UL which is effectively unlimited.

Signed-off-by: Rob Herring <robh@kernel.org>
---
README | 3 +++
common/board_r.c | 1 +
common/image.c | 17 +++++++++++++++++
include/common.h | 1 +
include/config_fallbacks.h | 4 ++++
5 files changed, 26 insertions(+)

diff --git a/README b/README
index 39e05d3..45c0438 100644
--- a/README
+++ b/README
@@ -4871,6 +4871,9 @@ List of environment variables (most likely not complete):
loadaddr - Default load address for commands like "bootp",
"rarpboot", "tftpboot", "loadb" or "diskboot"

+ loadsize - Maximum load size for commands like "bootp",
+ "rarpboot", "tftpboot", "loadb" or "diskboot"
+
loads_echo - see CONFIG_LOADS_ECHO

serverip - TFTP server IP address; needed for tftpboot command
diff --git a/common/board_r.c b/common/board_r.c
index 8629a65..b420f43 100644
--- a/common/board_r.c
+++ b/common/board_r.c
@@ -451,6 +451,7 @@ static int initr_env(void)

/* Initialize from environment */
load_addr = getenv_ulong("loadaddr", 16, load_addr);
+ load_size = getenv_ulong("loadsize", 16, load_size);
#if defined(CONFIG_SYS_EXTBDINFO)
#if defined(CONFIG_405GP) || defined(CONFIG_405EP)
#if defined(CONFIG_I2CFAST)
diff --git a/common/image.c b/common/image.c
index 9c6bec5..afbf806 100644
--- a/common/image.c
+++ b/common/image.c
@@ -396,6 +396,7 @@ static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
/*****************************************************************************/
#ifndef USE_HOSTCC
ulong load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
+ulong load_size = CONFIG_SYS_LOAD_SIZE; /* Default Load Size */
ulong save_addr; /* Default Save Address */
ulong save_size; /* Default Save Size (in bytes) */

@@ -415,6 +416,22 @@ static int on_loadaddr(const char *name, const char *value, enum env_op op,
}
U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);

+static int on_loadsize(const char *name, const char *value, enum env_op op,
+ int flags)
+{
+ switch (op) {
+ case env_op_create:
+ case env_op_overwrite:
+ load_size = simple_strtoul(value, NULL, 16);
+ break;
+ default:
+ break;
+ }
+
+ return 0;
+}
+U_BOOT_ENV_CALLBACK(loadsize, on_loadsize);
+
ulong getenv_bootm_low(void)
{
char *s = getenv("bootm_low");
diff --git a/include/common.h b/include/common.h
index cbd3c9e..80f366e 100644
--- a/include/common.h
+++ b/include/common.h
@@ -342,6 +342,7 @@ void flash_perror (int);
int source (ulong addr, const char *fit_uname);

extern ulong load_addr; /* Default Load Address */
+extern ulong load_size; /* Default Load Size (maximum) */
extern ulong save_addr; /* Default Save Address */
extern ulong save_size; /* Default Save Size */

diff --git a/include/config_fallbacks.h b/include/config_fallbacks.h
index e6fb47b..92a36f5 100644
--- a/include/config_fallbacks.h
+++ b/include/config_fallbacks.h
@@ -79,4 +79,8 @@
#define CONFIG_SYS_HZ 1000
#endif

+#ifndef CONFIG_SYS_LOAD_SIZE
+#define CONFIG_SYS_LOAD_SIZE (~0UL)
+#endif
+
#endif /* __CONFIG_FALLBACKS_H */
--
1.9.1