[PATCH 03/10] setexpr: Add explicit support for 32- and 64-bit ints

Simon Glass
2020-11-01T21:15:37Z

At present this function assumes that a size of 4 refers to a ulong. This
is true on 32-bit machines but not commonly on 64-bit machines.

This means that the 'l' specify does not work correctly with setexpr.

Add an explicit case for 32-bit values so that 64-bit machines can still
use the 'l' specifier. On 32-bit machines, 64-bit is still not supported.

This corrects the operation of the default size (which is 4 for setexpr),
so update the tests accordingly.

The original code for reading from memory was included in 47ab5ad1457
("cmd_setexpr: allow memory addresses in expressions") but I am not adding
a Fixes: tag since that code was not written with 64-bit machines in mind.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

cmd/setexpr.c | 4 ++++
test/cmd/setexpr.c | 18 +++++++++---------
2 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/cmd/setexpr.c b/cmd/setexpr.c
index 770dc24d2b7..dd9c2574fdc 100644
--- a/cmd/setexpr.c
+++ b/cmd/setexpr.c
@@ -39,6 +39,10 @@ static ulong get_arg(char *s, int w)
unmap_sysmem(p);
return val;
case 4:
+ p = map_sysmem(addr, sizeof(u32));
+ val = *(u32 *)p;
+ unmap_sysmem(p);
+ return val;
default:
p = map_sysmem(addr, sizeof(ulong));
val = *p;
diff --git a/test/cmd/setexpr.c b/test/cmd/setexpr.c
index cab6fdf4b83..e950c380ce0 100644
--- a/test/cmd/setexpr.c
+++ b/test/cmd/setexpr.c
@@ -45,7 +45,7 @@ static int setexpr_test_int(struct unit_test_state *uts)
ut_assertok(run_command("setexpr.l fred 0", 0));
ut_asserteq_str("0", env_get("fred"));
ut_assertok(run_command("setexpr.l fred *0", 0));
- ut_asserteq_str("ffffffff3456789a", env_get("fred"));
+ ut_asserteq_str("3456789a", env_get("fred"));

/* 64-bit */
*(u64 *)buf = 0x456789abcdef0123;
@@ -58,7 +58,7 @@ static int setexpr_test_int(struct unit_test_state *uts)
ut_assertok(run_command("setexpr fred 0", 0));
ut_asserteq_str("0", env_get("fred"));
ut_assertok(run_command("setexpr fred *0", 0));
- ut_asserteq_str("456789abcdef0123", env_get("fred"));
+ ut_asserteq_str("cdef0123", env_get("fred"));

unmap_sysmem(buf);

@@ -90,7 +90,7 @@ static int setexpr_test_plus(struct unit_test_state *uts)
*(u32 *)buf = 0x3456789a;
*(u32 *)(buf + 0x10) = 0xc3384235;
ut_assertok(run_command("setexpr.l fred *0 + *10", 0));
- ut_asserteq_str("fffffffef78ebacf", env_get("fred"));
+ ut_asserteq_str("f78ebacf", env_get("fred"));

/* 64-bit */
*(u64 *)buf = 0x456789abcdef0123;
@@ -100,7 +100,7 @@ static int setexpr_test_plus(struct unit_test_state *uts)

/* default */
ut_assertok(run_command("setexpr fred *0 + *10", 0));
- ut_asserteq_str("8eeebc2f407393a6", env_get("fred"));
+ ut_asserteq_str("1407393a6", env_get("fred"));

unmap_sysmem(buf);

@@ -121,14 +121,14 @@ static int setexpr_test_oper(struct unit_test_state *uts)

/* Quote | to avoid confusing hush */
ut_assertok(run_command("setexpr fred *0 \"|\" *10", 0));
- ut_asserteq_str("ffffffff00561234", env_get("fred"));
+ ut_asserteq_str("561234", env_get("fred"));

*(u32 *)buf = 0x561200;
*(u32 *)(buf + 0x10) = 0x1234;

/* Quote & to avoid confusing hush */
ut_assertok(run_command("setexpr.l fred *0 \"&\" *10", 0));
- ut_asserteq_str("ffffffff00001200", env_get("fred"));
+ ut_asserteq_str("1200", env_get("fred"));

ut_assertok(run_command("setexpr.l fred *0 ^ *10", 0));
ut_asserteq_str("560034", env_get("fred"));
@@ -137,13 +137,13 @@ static int setexpr_test_oper(struct unit_test_state *uts)
ut_asserteq_str("55ffcc", env_get("fred"));

ut_assertok(run_command("setexpr.l fred *0 * *10", 0));
- ut_asserteq_str("ffa9dbd21ebfa800", env_get("fred"));
+ ut_asserteq_str("61ebfa800", env_get("fred"));

ut_assertok(run_command("setexpr.l fred *0 / *10", 0));
- ut_asserteq_str("1", env_get("fred"));
+ ut_asserteq_str("4ba", env_get("fred"));

ut_assertok(run_command("setexpr.l fred *0 % *10", 0));
- ut_asserteq_str("55ffcc", env_get("fred"));
+ ut_asserteq_str("838", env_get("fred"));

unmap_sysmem(buf);

--
2.29.1.341.ge80a0c044ae-goog