Repository navigation
printenv: accept variable names and -n like fw_printenv - #281
officialasishkumar wants to merge 2 commits into
Conversation
`ipctool printenv` is advertised as a drop-in replacement for fw_printenv but ignored its arguments, so `ipctool printenv bootargs` dumped the entire environment and scripts had to grep for one value. Now `ipctool printenv [-n] [name...]`: - with no names, the whole environment is printed as before; - with names, each is printed as name=value in the order given. A name is matched whole, so "bootcmd" does not pick up "bootcmdnor"; - -n prints only the value and, as in fw_printenv, takes exactly one name; - a name that is not set is reported on stderr as `## Error: "name" not defined` and the exit status is 1, but the remaining names are still printed; - asking for names when no U-Boot environment can be found is an error rather than a silent success. The printing is a separate function, uboot_print_vars(). uboot_test builds uboot.c against a stub flash holding one synthetic environment with a valid CRC, so cmd_printenv() runs end to end, environment detection included, on a PC. Usage text and README are updated. Fixes OpenIPC#280 Signed-off-by: Asish Kumar <officialasishkumar@gmail.com>
PR Summary by QodoSupport named variables and -n in printenv
AI Description
Diagram
High-Level Assessment
Files changed (6)
|
Code Review by Qodo
1.
|
openipc-ai
left a comment
There was a problem hiding this comment.
Thanks for this, accepting names and -n makes printenv much more useful from scripts. A few things to sort out before merge, inline below. Two more are in files this PR doesn't touch:
uboot_testis never built or run in CI. The unit-tests job in.github/workflows/pr-build-check.ymlbuilds and runs every test target by name (cYAML_test...jffs2_test), so the new test needs an entry there. Otherwise a later regression in name matching or the bounded scan still goes green.CLAUDE.md's "Tests and CI" section lists each host unit test and what it covers. Please add a line foruboot_test.
The blocking ones are the CI entry and the unbounded walk in the no-names path (comment on line 273).
| c->nnames, c->names, | ||
| c->value_only); | ||
| else | ||
| uboot_printenv_cb(addr + u_off); |
There was a problem hiding this comment.
This path is still unbounded. uboot_printenv_cb walks while (*ptr) with no end, but the comment at L113 notes that the CRC doesn't promise a terminator, and uboot_test builds exactly that environment. So ipctool printenv (no names) on such flash reads past env_len and the MTD mapping, and can print garbage or SIGSEGV, while printenv x on the same flash is safe. Please give it the same ptr < end / memchr bound.
| bool found = false; | ||
| /* The CRC does not promise the list is terminated, so never scan | ||
| * past the environment. */ | ||
| while (ptr < end && *ptr) { |
There was a problem hiding this comment.
This is now the third copy of the "walk the NUL-separated k=v list and match a whole name" loop, next to uboot_env_get_param() and uboot_setenv_cb(). It's the only bounded one, so the overread this fixes is still there in ipctool -s and setenv. Suggestion: factor out one bounded find_var(env, len, name) helper and use it in all three places, so the fix is made once. Fine as a follow-up if you'd rather keep this PR small, but it's worth a note.
| for (int i = 0; i < nnames; i++) { | ||
| size_t len = strlen(names[i]); | ||
| const char *ptr = env + CRC_SZ; | ||
| const char *end = env + env_len; |
There was a problem hiding this comment.
uboot_print_vars is exported and takes env, but its bound comes from the file-static env_len, which uboot_detect_env sets as a side effect. It's only correct when called straight after detection. With the 0x10000 default and any smaller buffer (e.g. a caller passing uenv or a test fixture), it reads past the end. Please pass the length as a parameter.
| const char *nul = memchr(ptr, 0, end - ptr); | ||
| if (!nul) | ||
| break; | ||
| if ((size_t)(nul - ptr) > len && !memcmp(ptr, names[i], len) && |
There was a problem hiding this comment.
Names containing = aren't rejected. ipctool printenv 'bootargs=mem' against bootargs=mem=32M console=... passes the memcmp, ptr[len] is the second =, and the bootargs line is printed as though bootargs=mem existed. fw_printenv rejects such names. Suggest if (strchr(names[i], '=') || !*names[i]) → the ## Error path.
| int cmd_printenv(int argc, char **argv) { | ||
| bool value_only = false; | ||
| int first = 1; | ||
| if (first < argc && !strcmp(argv[first], "-n")) { |
There was a problem hiding this comment.
-n is only recognised as the very first argument, so ipctool printenv ipaddr -n looks up a variable called -n, prints ## Error: "-n" not defined and exits 1. --noheader likewise. If this is meant as a drop-in for fw_printenv, please either parse options properly (getopt with + / a small loop) or narrow the README claim.
| }; | ||
| enum_mtd_info(&ctx, cb_uboot_env); | ||
| return EXIT_SUCCESS; | ||
| if (nnames && !ctx.found) { |
There was a problem hiding this comment.
"U-Boot environment not found" and the nonzero exit only happen when names were given. Plain ipctool printenv with no env (not in mtd0/1, or a CRC mismatch) prints nothing and exits 0, so a script checking $? takes it as an empty environment. fw_printenv fails in that case. Dropping the nnames && would make both paths consistent.
Fixes #280
ipctool printenv [-n] [name...]. The command is documented as a drop-in replacement forfw_printenvbut ignored its arguments.Behaviour
name=valuefor each, in the order given. Names match whole (bootcmddoes not matchbootcmdnor).-n: just the value; exactly one name, as infw_printenv.## Error: "name" not definedon stderr and exit status 1; the other names are still printed.Testing
New
uboot_testbuildsuboot.cagainst a stub flash holding a synthetic environment with a valid CRC, socmd_printenv()runs end to end (environment detection included) without hardware. It covers the cases above plus an empty value and-nwith zero or two names. Built with-Wall -Wextraand ASan/UBSan on aarch64 Linux; passes.main.ccompiles against the new signature.Usage text and README updated.