6ab072883d83f2106b6603c0305326cfc78ecf1a
[openwrt/openwrt.git] /
1 From 0a24b51a3264a3f942a75025ea5ff6133c8989b0 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
3 Date: Mon, 8 Mar 2021 10:03:17 +0100
4 Subject: [PATCH] firmware: bcm47xx_nvram: add helper checking for NVRAM
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 This avoids duplicating code doing casting and checking for NVRAM magic.
10
11 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
12 Signed-off-by: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
13 ---
14 drivers/firmware/broadcom/bcm47xx_nvram.c | 30 ++++++++++++++---------
15 1 file changed, 18 insertions(+), 12 deletions(-)
16
17 --- a/drivers/firmware/broadcom/bcm47xx_nvram.c
18 +++ b/drivers/firmware/broadcom/bcm47xx_nvram.c
19 @@ -34,14 +34,20 @@ static char nvram_buf[NVRAM_SPACE];
20 static size_t nvram_len;
21 static const u32 nvram_sizes[] = {0x6000, 0x8000, 0xF000, 0x10000};
22
23 +/**
24 + * bcm47xx_nvram_is_valid - check for a valid NVRAM at specified memory
25 + */
26 +static bool bcm47xx_nvram_is_valid(void __iomem *nvram)
27 +{
28 + return ((struct nvram_header *)nvram)->magic == NVRAM_MAGIC;
29 +}
30 +
31 static u32 find_nvram_size(void __iomem *end)
32 {
33 - struct nvram_header __iomem *header;
34 int i;
35
36 for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
37 - header = (struct nvram_header *)(end - nvram_sizes[i]);
38 - if (header->magic == NVRAM_MAGIC)
39 + if (bcm47xx_nvram_is_valid(end - nvram_sizes[i]))
40 return nvram_sizes[i];
41 }
42
43 @@ -55,6 +61,7 @@ static int bcm47xx_nvram_find_and_copy(v
44 {
45 struct nvram_header __iomem *header;
46 size_t flash_size;
47 + size_t offset;
48 u32 size;
49
50 if (nvram_len) {
51 @@ -68,31 +75,30 @@ static int bcm47xx_nvram_find_and_copy(v
52 /* Windowed flash access */
53 size = find_nvram_size(flash_start + flash_size);
54 if (size) {
55 - header = (struct nvram_header *)(flash_start + flash_size - size);
56 + offset = flash_size - size;
57 goto found;
58 }
59 flash_size <<= 1;
60 }
61
62 /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
63 - header = (struct nvram_header *)(flash_start + 4096);
64 - if (header->magic == NVRAM_MAGIC) {
65 - size = NVRAM_SPACE;
66 +
67 + offset = 4096;
68 + if (bcm47xx_nvram_is_valid(flash_start + offset))
69 goto found;
70 - }
71
72 - header = (struct nvram_header *)(flash_start + 1024);
73 - if (header->magic == NVRAM_MAGIC) {
74 - size = NVRAM_SPACE;
75 + offset = 1024;
76 + if (bcm47xx_nvram_is_valid(flash_start + offset))
77 goto found;
78 - }
79
80 pr_err("no nvram found\n");
81 return -ENXIO;
82
83 found:
84 + header = (struct nvram_header *)(flash_start + offset);
85 __ioread32_copy(nvram_buf, header, sizeof(*header) / 4);
86 nvram_len = ((struct nvram_header *)(nvram_buf))->len;
87 + size = res_size - offset;
88 if (nvram_len > size) {
89 pr_err("The nvram size according to the header seems to be bigger than the partition on flash\n");
90 nvram_len = size;