aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/brcm47xx/patches-4.1/031-09-MIPS-BCM47xx-Add-helper-variable-for-storing-NVRAM-l.patch
blob: 053144a07d5f5d952195879da2aa7060a9728109 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
From f229d75f1472c4cd30f464e4a0f94f410046bd80 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <zajec5@gmail.com>
Date: Sat, 6 Jun 2015 23:16:23 +0200
Subject: [PATCH] MIPS: BCM47xx: Add helper variable for storing NVRAM length
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This simplifies code just a bit (also maybe makes it a bit more
intuitive?) and will allow us to stop storing header. Right now we copy
whole NVRAM including its header to the internal buffer. It is not
needed to store a header as we don't access all these details like CRC,
flags, etc. The next improvement that should follow is copying only the
real contents.

Signed-off-by: Rafał Miłecki <zajec5@gmail.com>
Acked-by: Hauke Mehrtens <hauke@hauke-m.de>
Cc: linux-mips@linux-mips.org
Cc: Arend van Spriel <arend@broadcom.com>
Cc: Hante Meuleman <meuleman@broadcom.com>
Patchwork: https://patchwork.linux-mips.org/patch/10535/
Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
---
 arch/mips/bcm47xx/nvram.c | 37 ++++++++++++++++---------------------
 1 file changed, 16 insertions(+), 21 deletions(-)

--- a/arch/mips/bcm47xx/nvram.c
+++ b/arch/mips/bcm47xx/nvram.c
@@ -35,6 +35,7 @@ struct nvram_header {
 };
 
 static char nvram_buf[NVRAM_SPACE];
+static size_t nvram_len;
 static const u32 nvram_sizes[] = {0x8000, 0xF000, 0x10000};
 
 static u32 find_nvram_size(void __iomem *end)
@@ -60,7 +61,7 @@ static int nvram_find_and_copy(void __io
 	u32 *src, *dst;
 	u32 size;
 
-	if (nvram_buf[0]) {
+	if (nvram_len) {
 		pr_warn("nvram already initialized\n");
 		return -EEXIST;
 	}
@@ -99,17 +100,18 @@ found:
 	for (i = 0; i < sizeof(struct nvram_header); i += 4)
 		*dst++ = __raw_readl(src++);
 	header = (struct nvram_header *)nvram_buf;
-	if (header->len > size) {
+	nvram_len = header->len;
+	if (nvram_len > size) {
 		pr_err("The nvram size according to the header seems to be bigger than the partition on flash\n");
-		header->len = size;
+		nvram_len = size;
 	}
-	if (header->len >= NVRAM_SPACE) {
+	if (nvram_len >= NVRAM_SPACE) {
 		pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
 		       header->len, NVRAM_SPACE - 1);
-		header->len = NVRAM_SPACE - 1;
+		nvram_len = NVRAM_SPACE - 1;
 	}
 	/* proceed reading data after header */
-	for (; i < header->len; i += 4)
+	for (; i < nvram_len; i += 4)
 		*dst++ = readl(src++);
 	nvram_buf[NVRAM_SPACE - 1] = '\0';
 
@@ -144,7 +146,6 @@ static int nvram_init(void)
 #ifdef CONFIG_MTD
 	struct mtd_info *mtd;
 	struct nvram_header header;
-	struct nvram_header *pheader;
 	size_t bytes_read;
 	int err;
 
@@ -155,20 +156,16 @@ static int nvram_init(void)
 	err = mtd_read(mtd, 0, sizeof(header), &bytes_read, (uint8_t *)&header);
 	if (!err && header.magic == NVRAM_MAGIC &&
 	    header.len > sizeof(header)) {
-		if (header.len >= NVRAM_SPACE) {
+		nvram_len = header.len;
+		if (nvram_len >= NVRAM_SPACE) {
 			pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
 				header.len, NVRAM_SPACE);
-			header.len = NVRAM_SPACE - 1;
+			nvram_len = NVRAM_SPACE - 1;
 		}
 
-		err = mtd_read(mtd, 0, header.len, &bytes_read,
+		err = mtd_read(mtd, 0, nvram_len, &nvram_len,
 			       (u8 *)nvram_buf);
-		if (err)
-			return err;
-
-		pheader = (struct nvram_header *)nvram_buf;
-		pheader->len = header.len;
-		return 0;
+		return err;
 	}
 #endif
 
@@ -183,7 +180,7 @@ int bcm47xx_nvram_getenv(const char *nam
 	if (!name)
 		return -EINVAL;
 
-	if (!nvram_buf[0]) {
+	if (!nvram_len) {
 		err = nvram_init();
 		if (err)
 			return err;
@@ -231,16 +228,14 @@ char *bcm47xx_nvram_get_contents(size_t
 {
 	int err;
 	char *nvram;
-	struct nvram_header *header;
 
-	if (!nvram_buf[0]) {
+	if (!nvram_len) {
 		err = nvram_init();
 		if (err)
 			return NULL;
 	}
 
-	header = (struct nvram_header *)nvram_buf;
-	*nvram_size = header->len - sizeof(struct nvram_header);
+	*nvram_size = nvram_len - sizeof(struct nvram_header);
 	nvram = vmalloc(*nvram_size);
 	if (!nvram)
 		return NULL;