c74bd064958de6fd7ae073c92dab15b65d67aa21
[openwrt/staging/nbd.git] /
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
9 First of all it shouldn't modify copied NVRAM just to make sure it can
10 loop over all entries. It's enough to just compare current position
11 pointer with the end of buffer address.
12 Secondly buffer is guaranteed to be \0 ended, so we don't need strnchr.
13
14 Signed-off-by: Rafał Miłecki <zajec5@gmail.com>
15 Cc: linux-mips@linux-mips.org
16 Cc: Hauke Mehrtens <hauke@hauke-m.de>
17 Cc: Hante Meuleman <meuleman@broadcom.com>
18 Cc: Ian Kent <raven@themaw.net>
19 Patchwork: https://patchwork.linux-mips.org/patch/10032/
20 Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
21 ---
22 arch/mips/bcm47xx/nvram.c | 13 +++++--------
23 1 file changed, 5 insertions(+), 8 deletions(-)
24
25 diff --git a/arch/mips/bcm47xx/nvram.c b/arch/mips/bcm47xx/nvram.c
26 index dee1c32..95d028c 100644
27 --- a/arch/mips/bcm47xx/nvram.c
28 +++ b/arch/mips/bcm47xx/nvram.c
29 @@ -171,7 +171,7 @@ static int nvram_init(void)
30 int bcm47xx_nvram_getenv(const char *name, char *val, size_t val_len)
31 {
32 char *var, *value, *end, *eq;
33 - int data_left, err;
34 + int err;
35
36 if (!name)
37 return -EINVAL;
38 @@ -184,19 +184,16 @@ int bcm47xx_nvram_getenv(const char *name, char *val, size_t val_len)
39
40 /* Look for name=value and return value */
41 var = &nvram_buf[sizeof(struct nvram_header)];
42 - end = nvram_buf + sizeof(nvram_buf) - 2;
43 - end[0] = '\0';
44 - end[1] = '\0';
45 - for (; *var; var = value + strlen(value) + 1) {
46 - data_left = end - var;
47 -
48 - eq = strnchr(var, data_left, '=');
49 + end = nvram_buf + sizeof(nvram_buf);
50 + while (var < end && *var) {
51 + eq = strchr(var, '=');
52 if (!eq)
53 break;
54 value = eq + 1;
55 if (eq - var == strlen(name) &&
56 strncmp(var, name, eq - var) == 0)
57 return snprintf(val, val_len, "%s", value);
58 + var = value + strlen(value) + 1;
59 }
60 return -ENOENT;
61 }
62 --
63 1.8.4.5
64