diff --git a/CMakeLists.txt b/CMakeLists.txt index 8605408..c31763a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -571,4 +571,8 @@ if(NOT ONLY_LIBRARY) # What restore --raw decides before it touches the flash. add_executable(raw_image_test src/raw_image_test.c src/raw_image.c) target_include_directories(raw_image_test PRIVATE include src) + + # What printenv prints, against a stub flash holding one environment. + add_executable(uboot_test src/uboot_test.c src/uboot.c) + target_include_directories(uboot_test PRIVATE include src) endif() diff --git a/README.md b/README.md index 2fae991..a5a0d18 100644 --- a/README.md +++ b/README.md @@ -125,7 +125,8 @@ Where: upgrade upgrade to OpenIPC firmware (experimental! use only on cameras with UART) [-f, --force] enforce - printenv drop-in replacement for fw_printenv + printenv [-n] [name...] drop-in replacement for fw_printenv; + -n prints just the value of one name setenv drop-in replacement for fw_setenv dmesg drop-in replacement for dmesg i2cget @@ -397,6 +398,8 @@ sent. Scripts and agents choose up front: ```console # ipctool printenv | grep bootargs + # ipctool printenv bootargs baudrate + # ipctool printenv -n ipaddr # ipctool setenv bootargs "mem=\${osmem} mtdparts=hi_sfc:256k(boot),64k(env),2048k(kernel),5120k(rootfs),-(rootfs_data)" ``` diff --git a/src/main.c b/src/main.c index 41b14f2..79cee81 100644 --- a/src/main.c +++ b/src/main.c @@ -101,7 +101,8 @@ void print_usage() { " nand-migrate [--dry-run] [--wipe-settings] \n" " move a NAND camera off the retired\n" " layout with a `kernel` UBI volume\n" - " printenv drop-in replacement for fw_printenv\n" + " printenv [-n] [name...] drop-in replacement for fw_printenv;\n" + " -n prints just the value of one name\n" " setenv drop-in replacement for fw_setenv\n" " dmesg drop-in replacement for dmesg\n" " i2cget \n" @@ -252,7 +253,7 @@ int main(int argc, char *argv[]) { else if (!strcmp(argv[1], "nand-migrate")) return nand_migrate_cmd(argc - 1, argv + 1); else if (!strcmp(argv[1], "printenv")) - return cmd_printenv(); + return cmd_printenv(argc - 1, argv + 1); else if (!strcmp(argv[1], "setenv")) return cmd_set_env(argc - 1, argv + 1); else if (!strcmp(argv[optind], "dmesg")) diff --git a/src/uboot.c b/src/uboot.c index 2fdcee3..b20cb35 100644 --- a/src/uboot.c +++ b/src/uboot.c @@ -98,6 +98,40 @@ void uboot_printenv_cb(const char *env) { } } +/* What `printenv ...` 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) { + const char *nul = memchr(ptr, 0, end - ptr); + if (!nul) + break; + if ((size_t)(nul - ptr) > len && !memcmp(ptr, names[i], len) && + 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); 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")) { + value_only = true; + first++; + } + int nnames = argc - first; + if (value_only && nnames != 1) { + fprintf(stderr, "Usage: ipctool printenv [-n] [...]\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) { + 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) { diff --git a/src/uboot.h b/src/uboot.h index 99a797b..ea4e4bf 100644 --- a/src/uboot.h +++ b/src/uboot.h @@ -3,6 +3,8 @@ #define ENV_MTD_NUM 2 +#include +#include #include int uboot_detect_env(void *buf, size_t size, size_t erasesize); @@ -15,7 +17,9 @@ void set_env_param_ram(const char *key, const char *value); void set_env_param_rom(const char *key, const char *value, int i, size_t u_off, size_t erasesize); -int cmd_printenv(); +int uboot_print_vars(FILE *out, FILE *err, const char *env, int nnames, + char **names, bool value_only); +int cmd_printenv(int argc, char **argv); int cmd_set_env(int argc, char **argv); #endif /* UBOOT_H */ diff --git a/src/uboot_test.c b/src/uboot_test.c new file mode 100644 index 0000000..4e782ac --- /dev/null +++ b/src/uboot_test.c @@ -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 +#include +#include +#include +#include +#include + +#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; +}