012-fw_env-autodetect-NAND-erase-size-and-env-sectors.patch 1.8 KB

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