uboot-envtools: backport some usefull patches from v2024.04-rc1
[openwrt/staging/jow.git] / package / boot / uboot-envtools / patches / 012-fw_env-autodetect-NAND-erase-size-and-env-sectors.patch
1 From d73a6641868029b5cae53ed00c5766921c9d8b1f Mon Sep 17 00:00:00 2001
2 From: Anthony Loiseau <anthony.loiseau@allcircuits.com>
3 Date: Thu, 21 Dec 2023 23:44:38 +0100
4 Subject: [PATCH] fw_env: autodetect NAND erase size and env sectors
5
6 As already done for NOR chips, if device ESIZE and ENVSECTORS static
7 configurations are both zero, then autodetect them at runtime.
8
9 Cc: Joe Hershberger <joe.hershberger@ni.com>
10 cc: Stefan Agner <stefan@agner.ch>
11 cc: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
12 Signed-off-by: Anthony Loiseau <anthony.loiseau@allcircuits.com>
13 ---
14 tools/env/README | 3 +++
15 tools/env/fw_env.c | 11 +++++++++--
16 2 files changed, 12 insertions(+), 2 deletions(-)
17
18 --- a/tools/env/README
19 +++ b/tools/env/README
20 @@ -58,6 +58,9 @@ DEVICEx_ENVSECTORS defines the number of
21 this environment instance. On NAND this is used to limit the range
22 within which bad blocks are skipped, on NOR it is not used.
23
24 +If DEVICEx_ESIZE and DEVICEx_ENVSECTORS are both zero, then a runtime
25 +detection is attempted for NOR and NAND mtd types.
26 +
27 To prevent losing changes to the environment and to prevent confusing the MTD
28 drivers, a lock file at /run/fw_printenv.lock is used to serialize access
29 to the environment.
30 --- a/tools/env/fw_env.c
31 +++ b/tools/env/fw_env.c
32 @@ -1655,8 +1655,15 @@ static int check_device_config(int dev)
33 }
34 DEVTYPE(dev) = mtdinfo.type;
35 if (DEVESIZE(dev) == 0 && ENVSECTORS(dev) == 0 &&
36 - mtdinfo.type == MTD_NORFLASH)
37 - DEVESIZE(dev) = mtdinfo.erasesize;
38 + mtdinfo.erasesize > 0) {
39 + if (mtdinfo.type == MTD_NORFLASH)
40 + DEVESIZE(dev) = mtdinfo.erasesize;
41 + else if (mtdinfo.type == MTD_NANDFLASH) {
42 + DEVESIZE(dev) = mtdinfo.erasesize;
43 + ENVSECTORS(dev) =
44 + mtdinfo.size / mtdinfo.erasesize;
45 + }
46 + }
47 if (DEVESIZE(dev) == 0)
48 /* Assume the erase size is the same as the env-size */
49 DEVESIZE(dev) = ENVSIZE(dev);