Repository navigation
printenv: accept variable names and -n like fw_printenv #281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -98,6 +98,40 @@ void uboot_printenv_cb(const char *env) { | |
| } | ||
| } | ||
|
|
||
| /* What `printenv <name>...` prints, with fw_printenv's output: "name=value" | ||
| * per variable, or the bare value when value_only. A name that is not set is | ||
| * reported on err and makes the result nonzero, but the rest are still | ||
| * printed. A name is matched whole: "bootcmd" is not "bootcmdnor". */ | ||
| int uboot_print_vars(FILE *out, FILE *err, const char *env, int nnames, | ||
| char **names, bool value_only) { | ||
| int missing = 0; | ||
| 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; | ||
| bool found = false; | ||
| /* The CRC does not promise the list is terminated, so never scan | ||
| * past the environment. */ | ||
| while (ptr < end && *ptr) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is now the third copy of the "walk the NUL-separated |
||
| const char *nul = memchr(ptr, 0, end - ptr); | ||
| if (!nul) | ||
| break; | ||
| if ((size_t)(nul - ptr) > len && !memcmp(ptr, names[i], len) && | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Names containing |
||
| ptr[len] == '=') { | ||
| fprintf(out, "%s\n", value_only ? ptr + len + 1 : ptr); | ||
| found = true; | ||
| break; | ||
| } | ||
| ptr = nul + 1; | ||
| } | ||
| if (!found) { | ||
| fprintf(err, "## Error: \"%s\" not defined\n", names[i]); | ||
| missing++; | ||
| } | ||
| } | ||
| return missing ? EXIT_FAILURE : EXIT_SUCCESS; | ||
| } | ||
|
|
||
| static void *uenv; | ||
| void uboot_copyenv_int(const void *buf) { | ||
| if (!uenv) { | ||
|
|
@@ -206,6 +240,14 @@ typedef struct { | |
| const char *key; | ||
| const char *value; | ||
| enum FLASH_OP fop; | ||
| // OP_PRINTENV: the variables to show (none: all of them) | ||
| int nnames; | ||
| char **names; | ||
| bool value_only; | ||
| // OP_PRINTENV: the status of the command, and whether the environment | ||
| // was found to be asked at all | ||
| int rc; | ||
| bool found; | ||
| } ctx_uboot_t; | ||
|
|
||
| static bool cb_uboot_env(int i, const char *name, struct mtd_info_user *mtd, | ||
|
|
@@ -222,7 +264,13 @@ static bool cb_uboot_env(int i, const char *name, struct mtd_info_user *mtd, | |
| if (u_off != -1) { | ||
| switch (c->op) { | ||
| case OP_PRINTENV: | ||
| uboot_printenv_cb(addr + u_off); | ||
| c->found = true; | ||
| if (c->nnames) | ||
| c->rc = uboot_print_vars(stdout, stderr, addr + u_off, | ||
| c->nnames, c->names, | ||
| c->value_only); | ||
| else | ||
| uboot_printenv_cb(addr + u_off); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This path is still unbounded. |
||
| break; | ||
| case OP_SETENV: | ||
| uboot_setenv_cb(i, u_off, addr + u_off, c->key, c->value, | ||
|
|
@@ -259,12 +307,32 @@ const char *uboot_getenv(const char *name) { | |
| return uboot_env_get_param(name); | ||
| } | ||
|
|
||
| int cmd_printenv() { | ||
| int cmd_printenv(int argc, char **argv) { | ||
| bool value_only = false; | ||
| int first = 1; | ||
| if (first < argc && !strcmp(argv[first], "-n")) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| value_only = true; | ||
| first++; | ||
| } | ||
| int nnames = argc - first; | ||
| if (value_only && nnames != 1) { | ||
| fprintf(stderr, "Usage: ipctool printenv [-n] [<name>...]\n" | ||
| " -n needs exactly one name\n"); | ||
| return EXIT_FAILURE; | ||
| } | ||
|
|
||
| ctx_uboot_t ctx = { | ||
| .op = OP_PRINTENV, | ||
| .nnames = nnames, | ||
| .names = argv + first, | ||
| .value_only = value_only, | ||
| }; | ||
| enum_mtd_info(&ctx, cb_uboot_env); | ||
| return EXIT_SUCCESS; | ||
| if (nnames && !ctx.found) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| fprintf(stderr, "U-Boot environment not found\n"); | ||
| return EXIT_FAILURE; | ||
| } | ||
| return ctx.rc; | ||
| } | ||
|
|
||
| void set_env_param_ram(const char *key, const char *value) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,223 @@ | ||
| /* | ||
| * uboot_test: what `ipctool printenv [-n] [name...]` prints and returns. | ||
| * The flash is a stub that hands back one synthetic U-Boot environment, so | ||
| * the command runs end to end -- environment detection included -- on a PC. | ||
| */ | ||
| #include <stdbool.h> | ||
| #include <stdint.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
| #include <unistd.h> | ||
|
|
||
| #include "mtd.h" | ||
| #include "uboot.h" | ||
|
|
||
| #define ENV_SIZE 0x10000 | ||
|
|
||
| static int failed; | ||
| #define CHECK(cond, ...) \ | ||
| do { \ | ||
| if (!(cond)) { \ | ||
| fprintf(stderr, "FAIL %s:%d: ", __FILE__, __LINE__); \ | ||
| fprintf(stderr, __VA_ARGS__); \ | ||
| fprintf(stderr, "\n"); \ | ||
| failed = 1; \ | ||
| } \ | ||
| } while (0) | ||
|
|
||
| static uint8_t flash[ENV_SIZE]; | ||
|
|
||
| static uint32_t crc32_ref(const uint8_t *p, size_t n) { | ||
| uint32_t crc = 0xFFFFFFFF; | ||
| while (n--) { | ||
| crc ^= *p++; | ||
| for (int k = 0; k < 8; k++) | ||
| crc = (crc >> 1) ^ (0xEDB88320 & -(crc & 1)); | ||
| } | ||
| return ~crc; | ||
| } | ||
|
|
||
| /* The environment as U-Boot stores it: CRC, then NUL-separated "k=v" strings | ||
| * ending in an empty one, padded with zeroes. */ | ||
| static void make_env(const char *const *vars) { | ||
| memset(flash, 0, sizeof(flash)); | ||
| uint8_t *p = flash + 4; | ||
| for (; *vars; vars++) { | ||
| strcpy((char *)p, *vars); | ||
| p += strlen(*vars) + 1; | ||
| } | ||
| uint32_t crc = crc32_ref(flash + 4, ENV_SIZE - 4); | ||
| memcpy(flash, &crc, 4); | ||
| } | ||
|
|
||
| /* --- the parts of mtd.c the command reaches --- */ | ||
| static bool stub_has_env = true; | ||
|
|
||
| char *open_mtdblock(int i, int *fd, uint32_t size, int flags) { | ||
| (void)i; | ||
| (void)size; | ||
| (void)flags; | ||
| *fd = -1; | ||
| return (char *)flash; | ||
| } | ||
|
|
||
| void enum_mtd_info(void *ctx, cb_mtd cb) { | ||
| if (!stub_has_env) | ||
| return; | ||
| struct mtd_info_user mtd = {.size = ENV_SIZE, .erasesize = ENV_SIZE}; | ||
| cb(0, "env", &mtd, ctx); | ||
| } | ||
|
|
||
| bool mtd_write(int mtd, uint32_t offset, uint32_t erasesize, const char *data, | ||
| size_t size) { | ||
| (void)mtd; | ||
| (void)offset; | ||
| (void)erasesize; | ||
| (void)data; | ||
| (void)size; | ||
| return true; | ||
| } | ||
|
|
||
| /* Evaluate expr with stdout and stderr captured into out and err. */ | ||
| #define CAPTURE(rc, out, outsz, err, errsz, expr) \ | ||
| do { \ | ||
| fflush(stdout); \ | ||
| fflush(stderr); \ | ||
| int so = dup(1), se = dup(2); \ | ||
| FILE *fo = tmpfile(), *fe = tmpfile(); \ | ||
| dup2(fileno(fo), 1); \ | ||
| dup2(fileno(fe), 2); \ | ||
| (rc) = (expr); \ | ||
| fflush(stdout); \ | ||
| fflush(stderr); \ | ||
| dup2(so, 1); \ | ||
| dup2(se, 2); \ | ||
| close(so); \ | ||
| close(se); \ | ||
| rewind(fo); \ | ||
| rewind(fe); \ | ||
| size_t no = fread(out, 1, (outsz) - 1, fo); \ | ||
| size_t ne = fread(err, 1, (errsz) - 1, fe); \ | ||
| (out)[no] = 0; \ | ||
| (err)[ne] = 0; \ | ||
| fclose(fo); \ | ||
| fclose(fe); \ | ||
| } while (0) | ||
|
|
||
| static int run(char *out, size_t outsz, char *err, size_t errsz, int argc, | ||
| char **argv) { | ||
| int rc; | ||
| CAPTURE(rc, out, outsz, err, errsz, cmd_printenv(argc, argv)); | ||
| return rc; | ||
| } | ||
|
|
||
| int main(void) { | ||
| const char *const vars[] = {"bootcmd=run nor", "bootcmdnor=sf probe", | ||
| "baudrate=115200", "empty=", NULL}; | ||
| make_env(vars); | ||
|
|
||
| char out[4096], err[4096]; | ||
| int rc; | ||
|
|
||
| /* No names: the whole environment, as it always was. */ | ||
| { | ||
| char *argv[] = {"printenv"}; | ||
| rc = run(out, sizeof(out), err, sizeof(err), 1, argv); | ||
| CHECK(rc == 0, "rc %d", rc); | ||
| CHECK(!strcmp(out, "bootcmd=run nor\nbootcmdnor=sf probe\n" | ||
| "baudrate=115200\nempty=\n"), | ||
| "out '%s'", out); | ||
| CHECK(!*err, "err '%s'", err); | ||
| } | ||
|
|
||
| /* One name: that variable, whole-name matched. */ | ||
| { | ||
| char *argv[] = {"printenv", "bootcmd"}; | ||
| rc = run(out, sizeof(out), err, sizeof(err), 2, argv); | ||
| CHECK(rc == 0, "rc %d", rc); | ||
| CHECK(!strcmp(out, "bootcmd=run nor\n"), "out '%s'", out); | ||
| } | ||
|
|
||
| /* Several names come out in the order asked, not the order stored. */ | ||
| { | ||
| char *argv[] = {"printenv", "baudrate", "bootcmdnor"}; | ||
| rc = run(out, sizeof(out), err, sizeof(err), 3, argv); | ||
| CHECK(rc == 0, "rc %d", rc); | ||
| CHECK(!strcmp(out, "baudrate=115200\nbootcmdnor=sf probe\n"), | ||
| "out '%s'", out); | ||
| } | ||
|
|
||
| /* A missing name is an error but does not hide the others. */ | ||
| { | ||
| char *argv[] = {"printenv", "nosuch", "baudrate"}; | ||
| rc = run(out, sizeof(out), err, sizeof(err), 3, argv); | ||
| CHECK(rc == 1, "rc %d", rc); | ||
| CHECK(!strcmp(out, "baudrate=115200\n"), "out '%s'", out); | ||
| CHECK(!strcmp(err, "## Error: \"nosuch\" not defined\n"), "err '%s'", | ||
| err); | ||
| } | ||
|
|
||
| /* A prefix of a name is not the name. */ | ||
| { | ||
| char *argv[] = {"printenv", "boot"}; | ||
| rc = run(out, sizeof(out), err, sizeof(err), 2, argv); | ||
| CHECK(rc == 1, "rc %d", rc); | ||
| CHECK(!*out, "out '%s'", out); | ||
| } | ||
|
|
||
| /* -n: the bare value, nothing else on stdout. */ | ||
| { | ||
| char *argv[] = {"printenv", "-n", "baudrate"}; | ||
| rc = run(out, sizeof(out), err, sizeof(err), 3, argv); | ||
| CHECK(rc == 0, "rc %d", rc); | ||
| CHECK(!strcmp(out, "115200\n"), "out '%s'", out); | ||
| } | ||
|
|
||
| /* -n on a variable that is set but empty prints an empty line. */ | ||
| { | ||
| char *argv[] = {"printenv", "-n", "empty"}; | ||
| rc = run(out, sizeof(out), err, sizeof(err), 3, argv); | ||
| CHECK(rc == 0, "rc %d", rc); | ||
| CHECK(!strcmp(out, "\n"), "out '%s'", out); | ||
| } | ||
|
|
||
| /* -n takes exactly one name. */ | ||
| { | ||
| char *argv[] = {"printenv", "-n"}; | ||
| rc = run(out, sizeof(out), err, sizeof(err), 2, argv); | ||
| CHECK(rc == 1, "rc %d", rc); | ||
| CHECK(!*out && *err, "out '%s' err '%s'", out, err); | ||
|
|
||
| char *argv2[] = {"printenv", "-n", "baudrate", "empty"}; | ||
| rc = run(out, sizeof(out), err, sizeof(err), 4, argv2); | ||
| CHECK(rc == 1, "rc %d", rc); | ||
| CHECK(!*out && *err, "out '%s' err '%s'", out, err); | ||
| } | ||
|
|
||
| /* A CRC-valid payload with no terminator is read only up to its end. */ | ||
| { | ||
| memset(flash + 4, 'x', ENV_SIZE - 4); | ||
| uint32_t crc = crc32_ref(flash + 4, ENV_SIZE - 4); | ||
| memcpy(flash, &crc, 4); | ||
| char *argv[] = {"printenv", "x"}; | ||
| rc = run(out, sizeof(out), err, sizeof(err), 2, argv); | ||
| CHECK(rc == 1, "rc %d", rc); | ||
| CHECK(!*out && *err, "out '%s' err '%s'", out, err); | ||
| make_env(vars); | ||
| } | ||
|
|
||
| /* Asked for a name and no environment exists: a failure, said so. */ | ||
| { | ||
| stub_has_env = false; | ||
| char *argv[] = {"printenv", "baudrate"}; | ||
| rc = run(out, sizeof(out), err, sizeof(err), 2, argv); | ||
| CHECK(rc == 1, "rc %d", rc); | ||
| CHECK(!*out && *err, "out '%s' err '%s'", out, err); | ||
| stub_has_env = true; | ||
| } | ||
|
|
||
| if (!failed) | ||
| printf("uboot_test: ok\n"); | ||
| return failed; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uboot_print_varsis exported and takesenv, but its bound comes from the file-staticenv_len, whichuboot_detect_envsets as a side effect. It's only correct when called straight after detection. With the0x10000default and any smaller buffer (e.g. a caller passinguenvor a test fixture), it reads past the end. Please pass the length as a parameter.