031-02-MIPS-BCM47XX-Simplify-function-looking-for-NVRAM-ent.patch 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. From f6f895644230b13618f14f7108f9b23a21a87bfa Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <zajec5@gmail.com>
  3. Date: Tue, 12 May 2015 18:46:12 +0200
  4. Subject: [PATCH] MIPS: BCM47XX: Simplify function looking for NVRAM entry
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. First of all it shouldn't modify copied NVRAM just to make sure it can
  9. loop over all entries. It's enough to just compare current position
  10. pointer with the end of buffer address.
  11. Secondly buffer is guaranteed to be \0 ended, so we don't need strnchr.
  12. Signed-off-by: Rafał Miłecki <zajec5@gmail.com>
  13. Cc: linux-mips@linux-mips.org
  14. Cc: Hauke Mehrtens <hauke@hauke-m.de>
  15. Cc: Hante Meuleman <meuleman@broadcom.com>
  16. Cc: Ian Kent <raven@themaw.net>
  17. Patchwork: https://patchwork.linux-mips.org/patch/10032/
  18. Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
  19. ---
  20. arch/mips/bcm47xx/nvram.c | 13 +++++--------
  21. 1 file changed, 5 insertions(+), 8 deletions(-)
  22. --- a/arch/mips/bcm47xx/nvram.c
  23. +++ b/arch/mips/bcm47xx/nvram.c
  24. @@ -171,7 +171,7 @@ static int nvram_init(void)
  25. int bcm47xx_nvram_getenv(const char *name, char *val, size_t val_len)
  26. {
  27. char *var, *value, *end, *eq;
  28. - int data_left, err;
  29. + int err;
  30. if (!name)
  31. return -EINVAL;
  32. @@ -184,19 +184,16 @@ int bcm47xx_nvram_getenv(const char *nam
  33. /* Look for name=value and return value */
  34. var = &nvram_buf[sizeof(struct nvram_header)];
  35. - end = nvram_buf + sizeof(nvram_buf) - 2;
  36. - end[0] = '\0';
  37. - end[1] = '\0';
  38. - for (; *var; var = value + strlen(value) + 1) {
  39. - data_left = end - var;
  40. -
  41. - eq = strnchr(var, data_left, '=');
  42. + end = nvram_buf + sizeof(nvram_buf);
  43. + while (var < end && *var) {
  44. + eq = strchr(var, '=');
  45. if (!eq)
  46. break;
  47. value = eq + 1;
  48. if (eq - var == strlen(name) &&
  49. strncmp(var, name, eq - var) == 0)
  50. return snprintf(val, val_len, "%s", value);
  51. + var = value + strlen(value) + 1;
  52. }
  53. return -ENOENT;
  54. }