Skip to content

printenv: accept variable names and -n like fw_printenv - #281

Open
officialasishkumar wants to merge 2 commits into
OpenIPC:masterfrom
officialasishkumar:printenv-names
Open

officialasishkumar wants to merge 2 commits into
OpenIPC:masterfrom
officialasishkumar:printenv-names

Conversation

@officialasishkumar

Copy link
Copy Markdown

Fixes #280

ipctool printenv [-n] [name...]. The command is documented as a drop-in replacement for fw_printenv but ignored its arguments.

Behaviour

  • No names: the whole environment, unchanged.
  • Names: name=value for each, in the order given. Names match whole (bootcmd does not match bootcmdnor).
  • -n: just the value; exactly one name, as in fw_printenv.
  • A name that is not set: ## Error: "name" not defined on stderr and exit status 1; the other names are still printed.
  • Names asked for but no U-Boot environment found: error and exit 1, not a silent success.

Testing

New uboot_test builds uboot.c against a stub flash holding a synthetic environment with a valid CRC, so cmd_printenv() runs end to end (environment detection included) without hardware. It covers the cases above plus an empty value and -n with zero or two names. Built with -Wall -Wextra and ASan/UBSan on aarch64 Linux; passes. main.c compiles against the new signature.

Usage text and README updated.

`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>
@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Summary by Qodo

Support named variables and -n in printenv

🐞 Bug fix ✨ Enhancement 🧪 Tests 📝 Documentation 🕐 20-40 Minutes

Grey Divider

AI Description

• Make printenv accept variable names and print only requested entries in argument order.
• Add -n for value-only output and report missing names without suppressing other results.
• Document the syntax and test the command against a synthetic U-Boot environment.
Diagram

graph TD
  CLI["printenv arguments"] --> Valid{"Valid -n usage?"} --> Scan["Scan MTD"] --> Names{"Names requested?"} --> Found{"Environment found?"} --> Vars["Print selected variables"]
  Valid -- "no" --> Failure["Report failure"]
  Names -- "no" --> All["Print full environment"]
  Found -- "no" --> Failure
  Vars -- "missing name" --> Failure
Loading
High-Level Assessment

Keep argument handling in cmd_printenv and selective formatting in a separate function while reusing the existing environment scan. Parsing the full dump externally or relying on the cached environment would not provide the same command behavior and detection path.

Files changed (6) +291 / -7

Enhancement (2) +8 / -3
main.cForward printenv arguments from the CLI +3/-2

Forward printenv arguments from the CLI

• Passes the subcommand argument vector to cmd_printenv and updates the displayed usage.

src/main.c

uboot.hExpose argument-aware printenv interfaces +5/-1

Expose argument-aware printenv interfaces

• Declares the selective-print helper and changes cmd_printenv to accept command arguments.

src/uboot.h

Bug fix (1) +64 / -3
uboot.cSelect U-Boot variables and propagate printenv failures +64/-3

Select U-Boot variables and propagate printenv failures

• Adds whole-name lookup, ordered output, and value-only formatting while retaining the full dump when no names are given. Validates -n and returns failures for missing names or an absent environment on named requests.

src/uboot.c

Tests (2) +215 / -0
CMakeLists.txtAdd the U-Boot command test target +4/-0

Add the U-Boot command test target

• Builds uboot_test with the production U-Boot implementation and test-provided flash stubs.

CMakeLists.txt

uboot_test.cTest printenv against a synthetic flash environment +211/-0

Test printenv against a synthetic flash environment

• Supplies a CRC-valid environment and MTD stubs to exercise detection and command output end to end. Covers full and selective output, ordering, exact matches, empty values, invalid -n usage, missing names, and an absent environment.

src/uboot_test.c

Documentation (1) +4 / -1
README.mdDocument named and value-only printenv usage +4/-1

Document named and value-only printenv usage

• Updates the command synopsis and adds examples for multiple names and -n.

README.md

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0) 🎨 UX issues (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)

Grey Divider


Remediation recommended

1. Test macro fails the repo format check ✓ Resolved

Resolving commit: e189e5f

Resolution reason
The commit realigns the CAPTURE macro's first line so its trailing backslash now sits at column 80
(line 37 changed to #define CAPTURE(...) followed by padding ending at column 80), matching the
other continuation lines. It also re-pads the two fread lines (48-49) to the same alignment. This is
a manual reflow of the macro's backslashes rather than a full clang-format run on the file, but it
addresses the reported formatting violation.
Description
The CAPTURE macro definition in src/uboot_test.c ends its first line with the continuation
backslash at column 94, while the rest of the macro uses column 80, and that line also runs past the
80-column limit. The repository's .clang-format would realign it, so any formatting pass will
rewrite this file and show it as changed.

2. Named lookups can read past flash ✓ Resolved

Resolving commit: e189e5f

Resolution reason
The entry scan is bounded by env_len via memchr over [ptr, end), and the loop stops if no terminator
is found within the environment. A regression test with a CRC-valid unterminated payload was added.
Description
uboot_print_vars() scans entries with strlen() without limiting reads to the detected
environment size. If a flash environment has a valid CRC but no NUL terminator in its payload,
ipctool printenv  accepts it and reads beyond the mapped flash region.

Grey Divider

Tip of the day
💡 Did you know, you can start a comment with 'qodo' or '@qodo' to chat about any finding

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@openipc-ai openipc-ai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_test is never built or run in CI. The unit-tests job in .github/workflows/pr-build-check.yml builds 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 for uboot_test.

The blocking ones are the CI entry and the unbounded walk in the no-names path (comment on line 273).

Comment thread src/uboot.c
c->nnames, c->names,
c->value_only);
else
uboot_printenv_cb(addr + u_off);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/uboot.c
bool found = false;
/* The CRC does not promise the list is terminated, so never scan
* past the environment. */
while (ptr < end && *ptr) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/uboot.c
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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/uboot.c
const char *nul = memchr(ptr, 0, end - ptr);
if (!nul)
break;
if ((size_t)(nul - ptr) > len && !memcmp(ptr, names[i], len) &&

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/uboot.c
int cmd_printenv(int argc, char **argv) {
bool value_only = false;
int first = 1;
if (first < argc && !strcmp(argv[first], "-n")) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-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.

Comment thread src/uboot.c
};
enum_mtd_info(&ctx, cb_uboot_env);
return EXIT_SUCCESS;
if (nnames && !ctx.found) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

printenv: accept variable names and -n like fw_printenv

2 participants