Merge pull request #142 from widgetii/master

Fix U-Boot env autodiscovery patch for non-standard environments
pull/146/head
Igor Zalatov 2022-01-06 18:44:56 +03:00 committed by GitHub
commit c8a251eef6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 33 additions and 19 deletions

View File

@ -1,5 +1,5 @@
diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c
index 2a61a5d6f0..a008254a6a 100644
index 2a61a5d6f0..19f92e5cdd 100644
--- a/tools/env/fw_env.c
+++ b/tools/env/fw_env.c
@@ -37,7 +37,11 @@
@ -24,30 +24,44 @@ index 2a61a5d6f0..a008254a6a 100644
rc = -1;
continue;
}
@@ -1725,6 +1730,91 @@ static int check_device_config(int dev)
@@ -1725,6 +1730,105 @@ static int check_device_config(int dev)
return rc;
}
+#define CRC_SZ 4
+// Assume env only size of 64Kb
+#define ENV_LEN 0x10000
+
+// By default use 0x10000 but then can be changed after detection
+static size_t env_len = 0x10000;
+
+// Detect U-Boot environment area offset
+static int uboot_detect_env(void *buf, size_t len) {
+ // Jump over memory by step
+ int scan_step = ENV_LEN;
+ int res = -1;
+static int uboot_detect_env(void *buf, size_t size, size_t erasesize) {
+ size_t possible_lens[] = {0x10000, 0x40000, 0x20000};
+
+ for (int baddr = 0; baddr < len; baddr += scan_step) {
+ // Jump over memory by step
+ int scan_step = erasesize;
+
+ for (int baddr = 0; baddr < size; 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;
+
+ for (int i = 0; i < sizeof(possible_lens) / sizeof(possible_lens[0]);
+ i++) {
+ if (possible_lens[i] + baddr > size)
+ continue;
+
+ uint32_t crc = crc32(0, buf + baddr + CRC_SZ, possible_lens[i] - CRC_SZ);
+
+#if 0
+ printf("Detecting at %#x with len %#x CRC is %#x ~ %#x\n", baddr,
+ possible_lens[i], crc, expected_crc);
+#endif
+ if (crc == expected_crc) {
+ env_len = possible_lens[i];
+ return baddr;
+ }
+ }
+ }
+
+ return res;
+ return -1;
+}
+
+static char *open_mtdblock(int i, int *fd, uint32_t size, int flags) {
@ -70,7 +84,7 @@ index 2a61a5d6f0..a008254a6a 100644
+}
+
+static char dev[80];
+static int detect_env() {
+static int detect_env(void) {
+ FILE *fp;
+ char name[80];
+ int i, es, ee;
@ -93,14 +107,14 @@ index 2a61a5d6f0..a008254a6a 100644
+ i, &blockfd, mtd.size, MAP_POPULATE /* causes read-ahead on the file */);
+ if (!addr)
+ return res;
+ int off = uboot_detect_env(addr, mtd.size);
+ int off = uboot_detect_env(addr, mtd.size, mtd.erasesize);
+ close(blockfd);
+ if (off != -1) {
+ DEVNAME(0) = dev;
+ DEVOFFSET(0) = off;
+ ENVSIZE(0) = ENV_LEN;
+ ENVSIZE(0) = env_len;
+ DEVESIZE(0) = mtd.erasesize;
+ ENVSECTORS(0) = 1;
+ ENVSECTORS(0) = mtd.size / mtd.erasesize;
+ res = 1;
+ break;
+ }
@ -116,7 +130,7 @@ index 2a61a5d6f0..a008254a6a 100644
static int parse_config(struct env_opts *opts)
{
int rc;
@@ -1735,9 +1825,11 @@ static int parse_config(struct env_opts *opts)
@@ -1735,9 +1839,11 @@ 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)) {