ab7ceaa0f36d9d65224c139650f50ac52b718c4b
[openwrt/staging/chunkeey.git] /
1 From f229d75f1472c4cd30f464e4a0f94f410046bd80 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <zajec5@gmail.com>
3 Date: Sat, 6 Jun 2015 23:16:23 +0200
4 Subject: [PATCH] MIPS: BCM47xx: Add helper variable for storing NVRAM length
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 This simplifies code just a bit (also maybe makes it a bit more
10 intuitive?) and will allow us to stop storing header. Right now we copy
11 whole NVRAM including its header to the internal buffer. It is not
12 needed to store a header as we don't access all these details like CRC,
13 flags, etc. The next improvement that should follow is copying only the
14 real contents.
15
16 Signed-off-by: Rafał Miłecki <zajec5@gmail.com>
17 Acked-by: Hauke Mehrtens <hauke@hauke-m.de>
18 Cc: linux-mips@linux-mips.org
19 Cc: Arend van Spriel <arend@broadcom.com>
20 Cc: Hante Meuleman <meuleman@broadcom.com>
21 Patchwork: https://patchwork.linux-mips.org/patch/10535/
22 Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
23 ---
24 arch/mips/bcm47xx/nvram.c | 37 ++++++++++++++++---------------------
25 1 file changed, 16 insertions(+), 21 deletions(-)
26
27 diff --git a/arch/mips/bcm47xx/nvram.c b/arch/mips/bcm47xx/nvram.c
28 index 2ed762e..9ccdce8 100644
29 --- a/arch/mips/bcm47xx/nvram.c
30 +++ b/arch/mips/bcm47xx/nvram.c
31 @@ -35,6 +35,7 @@ struct nvram_header {
32 };
33
34 static char nvram_buf[NVRAM_SPACE];
35 +static size_t nvram_len;
36 static const u32 nvram_sizes[] = {0x8000, 0xF000, 0x10000};
37
38 static u32 find_nvram_size(void __iomem *end)
39 @@ -60,7 +61,7 @@ static int nvram_find_and_copy(void __iomem *iobase, u32 lim)
40 u32 *src, *dst;
41 u32 size;
42
43 - if (nvram_buf[0]) {
44 + if (nvram_len) {
45 pr_warn("nvram already initialized\n");
46 return -EEXIST;
47 }
48 @@ -99,17 +100,18 @@ found:
49 for (i = 0; i < sizeof(struct nvram_header); i += 4)
50 *dst++ = __raw_readl(src++);
51 header = (struct nvram_header *)nvram_buf;
52 - if (header->len > size) {
53 + nvram_len = header->len;
54 + if (nvram_len > size) {
55 pr_err("The nvram size according to the header seems to be bigger than the partition on flash\n");
56 - header->len = size;
57 + nvram_len = size;
58 }
59 - if (header->len >= NVRAM_SPACE) {
60 + if (nvram_len >= NVRAM_SPACE) {
61 pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
62 header->len, NVRAM_SPACE - 1);
63 - header->len = NVRAM_SPACE - 1;
64 + nvram_len = NVRAM_SPACE - 1;
65 }
66 /* proceed reading data after header */
67 - for (; i < header->len; i += 4)
68 + for (; i < nvram_len; i += 4)
69 *dst++ = readl(src++);
70 nvram_buf[NVRAM_SPACE - 1] = '\0';
71
72 @@ -144,7 +146,6 @@ static int nvram_init(void)
73 #ifdef CONFIG_MTD
74 struct mtd_info *mtd;
75 struct nvram_header header;
76 - struct nvram_header *pheader;
77 size_t bytes_read;
78 int err;
79
80 @@ -155,20 +156,16 @@ static int nvram_init(void)
81 err = mtd_read(mtd, 0, sizeof(header), &bytes_read, (uint8_t *)&header);
82 if (!err && header.magic == NVRAM_MAGIC &&
83 header.len > sizeof(header)) {
84 - if (header.len >= NVRAM_SPACE) {
85 + nvram_len = header.len;
86 + if (nvram_len >= NVRAM_SPACE) {
87 pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
88 header.len, NVRAM_SPACE);
89 - header.len = NVRAM_SPACE - 1;
90 + nvram_len = NVRAM_SPACE - 1;
91 }
92
93 - err = mtd_read(mtd, 0, header.len, &bytes_read,
94 + err = mtd_read(mtd, 0, nvram_len, &nvram_len,
95 (u8 *)nvram_buf);
96 - if (err)
97 - return err;
98 -
99 - pheader = (struct nvram_header *)nvram_buf;
100 - pheader->len = header.len;
101 - return 0;
102 + return err;
103 }
104 #endif
105
106 @@ -183,7 +180,7 @@ int bcm47xx_nvram_getenv(const char *name, char *val, size_t val_len)
107 if (!name)
108 return -EINVAL;
109
110 - if (!nvram_buf[0]) {
111 + if (!nvram_len) {
112 err = nvram_init();
113 if (err)
114 return err;
115 @@ -231,16 +228,14 @@ char *bcm47xx_nvram_get_contents(size_t *nvram_size)
116 {
117 int err;
118 char *nvram;
119 - struct nvram_header *header;
120
121 - if (!nvram_buf[0]) {
122 + if (!nvram_len) {
123 err = nvram_init();
124 if (err)
125 return NULL;
126 }
127
128 - header = (struct nvram_header *)nvram_buf;
129 - *nvram_size = header->len - sizeof(struct nvram_header);
130 + *nvram_size = nvram_len - sizeof(struct nvram_header);
131 nvram = vmalloc(*nvram_size);
132 if (!nvram)
133 return NULL;
134 --
135 1.8.4.5
136