010-fw_env-fix-reading-NVMEM-device-s-compatible-value.patch 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <[email protected]>
  2. Date: Tue, 12 Dec 2023 18:23:45 +0100
  3. Subject: [PATCH] fw_env: fix reading NVMEM device's "compatible" value
  4. MIME-Version: 1.0
  5. Content-Type: text/plain; charset=UTF-8
  6. Content-Transfer-Encoding: 8bit
  7. Call to fread() was changed to check for return value. The problem is it
  8. can't be checked for returning 1 (as it is) to determine success.
  9. We call fread() with buffer size as "size" argument. Reading any
  10. "compatible" value shorter than buffer size will result in returning 0
  11. even on success.
  12. Modify code to use fstat() to determine expected read length.
  13. This fixes regression that broke using fw_env with NVMEM devices.
  14. Fixes: c059a22b7776 ("tools: env: fw_env: Fix unused-result warning")
  15. Cc: Jaehoon Chung <[email protected]>
  16. Signed-off-by: Rafał Miłecki <[email protected]>
  17. ---
  18. tools/env/fw_env.c | 18 ++++++++++++++----
  19. 1 file changed, 14 insertions(+), 4 deletions(-)
  20. --- a/tools/env/fw_env.c
  21. +++ b/tools/env/fw_env.c
  22. @@ -1732,6 +1732,7 @@ static int find_nvmem_device(void)
  23. }
  24. while (!nvmem && (dent = readdir(dir))) {
  25. + struct stat s;
  26. FILE *fp;
  27. size_t size;
  28. @@ -1749,14 +1750,22 @@ static int find_nvmem_device(void)
  29. continue;
  30. }
  31. - size = fread(buf, sizeof(buf), 1, fp);
  32. + if (fstat(fileno(fp), &s)) {
  33. + fprintf(stderr, "Failed to fstat %s\n", comp);
  34. + goto next;
  35. + }
  36. +
  37. + if (s.st_size >= sizeof(buf)) {
  38. + goto next;
  39. + }
  40. +
  41. + size = fread(buf, s.st_size, 1, fp);
  42. if (size != 1) {
  43. fprintf(stderr,
  44. "read failed about %s\n", comp);
  45. - fclose(fp);
  46. - return -EIO;
  47. + goto next;
  48. }
  49. -
  50. + buf[s.st_size] = '\0';
  51. if (!strcmp(buf, "u-boot,env")) {
  52. bytes = asprintf(&nvmem, "%s/%s/nvmem", path, dent->d_name);
  53. @@ -1765,6 +1774,7 @@ static int find_nvmem_device(void)
  54. }
  55. }
  56. +next:
  57. fclose(fp);
  58. }