summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRafał Miłecki <rafal@milecki.pl>2021-03-08 10:03:17 +0100
committerThomas Bogendoerfer <tsbogend@alpha.franken.de>2021-03-12 15:53:07 +0100
commit0a24b51a3264a3f942a75025ea5ff6133c8989b0 (patch)
tree6ac1c0ba74e61a02d8fb00855cc825ef664b075d
parentfb009cbdd0693bd633f11e99526617b3d392cfad (diff)
firmware: bcm47xx_nvram: add helper checking for NVRAM
This avoids duplicating code doing casting and checking for NVRAM magic. Signed-off-by: Rafał Miłecki <rafal@milecki.pl> Signed-off-by: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
-rw-r--r--drivers/firmware/broadcom/bcm47xx_nvram.c30
1 files changed, 18 insertions, 12 deletions
diff --git a/drivers/firmware/broadcom/bcm47xx_nvram.c b/drivers/firmware/broadcom/bcm47xx_nvram.c
index b04007adc79f..99f3ec180be6 100644
--- a/drivers/firmware/broadcom/bcm47xx_nvram.c
+++ b/drivers/firmware/broadcom/bcm47xx_nvram.c
@@ -34,14 +34,20 @@ static char nvram_buf[NVRAM_SPACE];
static size_t nvram_len;
static const u32 nvram_sizes[] = {0x6000, 0x8000, 0xF000, 0x10000};
+/**
+ * bcm47xx_nvram_is_valid - check for a valid NVRAM at specified memory
+ */
+static bool bcm47xx_nvram_is_valid(void __iomem *nvram)
+{
+ return ((struct nvram_header *)nvram)->magic == NVRAM_MAGIC;
+}
+
static u32 find_nvram_size(void __iomem *end)
{
- struct nvram_header __iomem *header;
int i;
for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
- header = (struct nvram_header *)(end - nvram_sizes[i]);
- if (header->magic == NVRAM_MAGIC)
+ if (bcm47xx_nvram_is_valid(end - nvram_sizes[i]))
return nvram_sizes[i];
}
@@ -55,6 +61,7 @@ static int bcm47xx_nvram_find_and_copy(void __iomem *flash_start, size_t res_siz
{
struct nvram_header __iomem *header;
size_t flash_size;
+ size_t offset;
u32 size;
if (nvram_len) {
@@ -68,31 +75,30 @@ static int bcm47xx_nvram_find_and_copy(void __iomem *flash_start, size_t res_siz
/* Windowed flash access */
size = find_nvram_size(flash_start + flash_size);
if (size) {
- header = (struct nvram_header *)(flash_start + flash_size - size);
+ offset = flash_size - size;
goto found;
}
flash_size <<= 1;
}
/* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
- header = (struct nvram_header *)(flash_start + 4096);
- if (header->magic == NVRAM_MAGIC) {
- size = NVRAM_SPACE;
+
+ offset = 4096;
+ if (bcm47xx_nvram_is_valid(flash_start + offset))
goto found;
- }
- header = (struct nvram_header *)(flash_start + 1024);
- if (header->magic == NVRAM_MAGIC) {
- size = NVRAM_SPACE;
+ offset = 1024;
+ if (bcm47xx_nvram_is_valid(flash_start + offset))
goto found;
- }
pr_err("no nvram found\n");
return -ENXIO;
found:
+ header = (struct nvram_header *)(flash_start + offset);
__ioread32_copy(nvram_buf, header, sizeof(*header) / 4);
nvram_len = ((struct nvram_header *)(nvram_buf))->len;
+ size = res_size - offset;
if (nvram_len > size) {
pr_err("The nvram size according to the header seems to be bigger than the partition on flash\n");
nvram_len = size;