Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ Where:
upgrade <bundle> 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 <key> <value> drop-in replacement for fw_setenv
dmesg drop-in replacement for dmesg
i2cget <device address> <register>
Expand Down Expand Up @@ -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)"
```

Expand Down
5 changes: 3 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ void print_usage() {
" nand-migrate [--dry-run] [--wipe-settings] <rootfs.ubifs>\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 <key> <value> drop-in replacement for fw_setenv\n"
" dmesg drop-in replacement for dmesg\n"
" i2cget <device address> <register>\n"
Expand Down Expand Up @@ -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"))
Expand Down
74 changes: 71 additions & 3 deletions src/uboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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.

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.

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.

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) {
Expand Down Expand Up @@ -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,
Expand All @@ -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);

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.

break;
case OP_SETENV:
uboot_setenv_cb(i, u_off, addr + u_off, c->key, c->value,
Expand Down Expand Up @@ -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")) {

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.

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) {

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.

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) {
Expand Down
6 changes: 5 additions & 1 deletion src/uboot.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#define ENV_MTD_NUM 2

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

int uboot_detect_env(void *buf, size_t size, size_t erasesize);
Expand All @@ -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 */
223 changes: 223 additions & 0 deletions src/uboot_test.c
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;
}