diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c index 2a61a5d6f0..6398e29c38 100644 --- a/tools/env/fw_env.c +++ b/tools/env/fw_env.c @@ -484,7 +484,8 @@ int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts) val = fw_getenv(name); if (!val) { - fprintf(stderr, "## Error: \"%s\" not defined\n", name); + if (!value_only) + fprintf(stderr, "## Error: \"%s\" not defined\n", name); rc = -1; continue; } @@ -1725,6 +1726,88 @@ static int check_device_config(int dev) return rc; } +#define CRC_SZ 4 +// Assume env only size of 64Kb +#define ENV_LEN 0x10000 + +// Detect U-Boot environment area offset +int uboot_detect_env(void *buf, size_t len) { + // Jump over memory by step + int scan_step = ENV_LEN; + int res = -1; + + for (int baddr = 0; baddr < len; baddr += scan_step) { + uint32_t expected_crc = *(int *)(buf + baddr); + uint32_t crc = crc32(0, buf + baddr + CRC_SZ, ENV_LEN - CRC_SZ); + if (crc == expected_crc) { + res = baddr; + break; + } + } + + return res; +} + +char *open_mtdblock(int i, int *fd, uint32_t size, int flags) { + char filename[PATH_MAX]; + + snprintf(filename, sizeof filename, "/dev/mtdblock%d", i); + *fd = open(filename, O_RDONLY); + if (*fd == -1) { + return NULL; + } + + char *addr = + (char *)mmap(NULL, size, PROT_READ, MAP_PRIVATE | flags, *fd, 0); + if ((void *)addr == MAP_FAILED) { + close(*fd); + return NULL; + } + + return addr; +} + +static char dev[80]; +static void detect_env() { + FILE *fp; + char name[80]; + int i, es, ee; + struct mtd_info_user mtd; + + if ((fp = fopen("/proc/mtd", "r"))) { + bool running = true; + while (fgets(dev, sizeof dev, fp) && running) { + name[0] = 0; + if (sscanf(dev, "mtd%d: %x %x \"%64[^\"]\"", &i, &es, &ee, name)) { + snprintf(dev, sizeof dev, "/dev/mtd%d", i); + int devfd = open(dev, O_RDWR); + if (devfd < 0) + continue; + + if (ioctl(devfd, MEMGETINFO, &mtd) >= 0) { + int blockfd; + char *addr = open_mtdblock( + i, &blockfd, mtd.size, MAP_POPULATE /* causes read-ahead on the file */); + if (!addr) + return; + int off = uboot_detect_env(addr, mtd.size); + close(blockfd); + if (off != -1) { + DEVNAME(0) = dev; + DEVOFFSET(0) = off; + ENVSIZE(0) = ENV_LEN; + DEVESIZE(0) = mtd.erasesize; + ENVSECTORS(0) = 1; + break; + } + } + close(devfd); + } + } + fclose(fp); + } +} + static int parse_config(struct env_opts *opts) { int rc; @@ -1735,9 +1818,7 @@ static int parse_config(struct env_opts *opts) #if defined(CONFIG_FILE) /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */ if (get_config(opts->config_file)) { - fprintf(stderr, "Cannot parse config file '%s': %m\n", - opts->config_file); - return -1; + detect_env(); } #else DEVNAME(0) = DEVICE1_NAME;