102-fw_env-add-fallback-to-Linux-s-NVMEM-based-access.patch 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. From 8142c4554ffaa927529f24427a35f7ee2861793a Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <[email protected]>
  3. Date: Thu, 16 Jun 2022 20:59:03 +0200
  4. Subject: [PATCH] fw_env: add fallback to Linux's NVMEM based access
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. A new DT binding for describing environment data block has been added in
  9. Linux's commit 5db1c2dbc04c ("dt-bindings: nvmem: add U-Boot environment
  10. variables binding"). Once we get a proper Linux NVMEM driver it'll be
  11. possible to use Linux's binary interface for user-space as documented
  12. in the:
  13. https://www.kernel.org/doc/html/latest/driver-api/nvmem.html
  14. This commits makes fw_env fallback to looking for a compatible NVMEM
  15. device in case config file isn't present. In a long term this may make
  16. config files redundant and avoid code (info) duplication.
  17. Signed-off-by: Rafał Miłecki <[email protected]>
  18. ---
  19. tools/env/fw_env.c | 70 ++++++++++++++++++++++++++++++++++++++++++++--
  20. 1 file changed, 67 insertions(+), 3 deletions(-)
  21. --- a/tools/env/fw_env.c
  22. +++ b/tools/env/fw_env.c
  23. @@ -1713,6 +1713,67 @@ static int check_device_config(int dev)
  24. return rc;
  25. }
  26. +static int find_nvmem_device(void)
  27. +{
  28. + const char *path = "/sys/bus/nvmem/devices";
  29. + struct dirent *dent;
  30. + char *nvmem = NULL;
  31. + char comp[256];
  32. + char buf[32];
  33. + int bytes;
  34. + DIR *dir;
  35. +
  36. + dir = opendir(path);
  37. + if (!dir) {
  38. + return -EIO;
  39. + }
  40. +
  41. + while (!nvmem && (dent = readdir(dir))) {
  42. + FILE *fp;
  43. +
  44. + if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) {
  45. + continue;
  46. + }
  47. +
  48. + bytes = snprintf(comp, sizeof(comp), "%s/%s/of_node/compatible", path, dent->d_name);
  49. + if (bytes < 0 || bytes == sizeof(comp)) {
  50. + continue;
  51. + }
  52. +
  53. + fp = fopen(comp, "r");
  54. + if (!fp) {
  55. + continue;
  56. + }
  57. +
  58. + fread(buf, sizeof(buf), 1, fp);
  59. +
  60. + if (!strcmp(buf, "u-boot,env")) {
  61. + bytes = asprintf(&nvmem, "%s/%s/nvmem", path, dent->d_name);
  62. + if (bytes < 0) {
  63. + nvmem = NULL;
  64. + }
  65. + }
  66. +
  67. + fclose(fp);
  68. + }
  69. +
  70. + closedir(dir);
  71. +
  72. + if (nvmem) {
  73. + struct stat s;
  74. +
  75. + stat(nvmem, &s);
  76. +
  77. + DEVNAME(0) = nvmem;
  78. + DEVOFFSET(0) = 0;
  79. + ENVSIZE(0) = s.st_size;
  80. +
  81. + return 0;
  82. + }
  83. +
  84. + return -ENOENT;
  85. +}
  86. +
  87. static int parse_config(struct env_opts *opts)
  88. {
  89. int rc;
  90. @@ -1723,9 +1784,12 @@ static int parse_config(struct env_opts
  91. #if defined(CONFIG_FILE)
  92. /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
  93. if (get_config(opts->config_file)) {
  94. - fprintf(stderr, "Cannot parse config file '%s': %m\n",
  95. - opts->config_file);
  96. - return -1;
  97. + if (find_nvmem_device()) {
  98. + fprintf(stderr, "Cannot parse config file '%s': %m\n",
  99. + opts->config_file);
  100. + fprintf(stderr, "Failed to find NVMEM device\n");
  101. + return -1;
  102. + }
  103. }
  104. #else
  105. DEVNAME(0) = DEVICE1_NAME;