Commit 0a24b51a authored by Rafał Miłecki's avatar Rafał Miłecki Committed by Thomas Bogendoerfer

firmware: bcm47xx_nvram: add helper checking for NVRAM

This avoids duplicating code doing casting and checking for NVRAM magic.
Signed-off-by: default avatarRafał Miłecki <rafal@milecki.pl>
Signed-off-by: default avatarThomas Bogendoerfer <tsbogend@alpha.franken.de>
parent fb009cbd
...@@ -34,14 +34,20 @@ static char nvram_buf[NVRAM_SPACE]; ...@@ -34,14 +34,20 @@ static char nvram_buf[NVRAM_SPACE];
static size_t nvram_len; static size_t nvram_len;
static const u32 nvram_sizes[] = {0x6000, 0x8000, 0xF000, 0x10000}; 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) static u32 find_nvram_size(void __iomem *end)
{ {
struct nvram_header __iomem *header;
int i; int i;
for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) { for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
header = (struct nvram_header *)(end - nvram_sizes[i]); if (bcm47xx_nvram_is_valid(end - nvram_sizes[i]))
if (header->magic == NVRAM_MAGIC)
return 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 ...@@ -55,6 +61,7 @@ static int bcm47xx_nvram_find_and_copy(void __iomem *flash_start, size_t res_siz
{ {
struct nvram_header __iomem *header; struct nvram_header __iomem *header;
size_t flash_size; size_t flash_size;
size_t offset;
u32 size; u32 size;
if (nvram_len) { if (nvram_len) {
...@@ -68,31 +75,30 @@ static int bcm47xx_nvram_find_and_copy(void __iomem *flash_start, size_t res_siz ...@@ -68,31 +75,30 @@ static int bcm47xx_nvram_find_and_copy(void __iomem *flash_start, size_t res_siz
/* Windowed flash access */ /* Windowed flash access */
size = find_nvram_size(flash_start + flash_size); size = find_nvram_size(flash_start + flash_size);
if (size) { if (size) {
header = (struct nvram_header *)(flash_start + flash_size - size); offset = flash_size - size;
goto found; goto found;
} }
flash_size <<= 1; flash_size <<= 1;
} }
/* Try embedded NVRAM at 4 KB and 1 KB as last resorts */ /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
header = (struct nvram_header *)(flash_start + 4096);
if (header->magic == NVRAM_MAGIC) { offset = 4096;
size = NVRAM_SPACE; if (bcm47xx_nvram_is_valid(flash_start + offset))
goto found; goto found;
}
header = (struct nvram_header *)(flash_start + 1024); offset = 1024;
if (header->magic == NVRAM_MAGIC) { if (bcm47xx_nvram_is_valid(flash_start + offset))
size = NVRAM_SPACE;
goto found; goto found;
}
pr_err("no nvram found\n"); pr_err("no nvram found\n");
return -ENXIO; return -ENXIO;
found: found:
header = (struct nvram_header *)(flash_start + offset);
__ioread32_copy(nvram_buf, header, sizeof(*header) / 4); __ioread32_copy(nvram_buf, header, sizeof(*header) / 4);
nvram_len = ((struct nvram_header *)(nvram_buf))->len; nvram_len = ((struct nvram_header *)(nvram_buf))->len;
size = res_size - offset;
if (nvram_len > size) { if (nvram_len > size) {
pr_err("The nvram size according to the header seems to be bigger than the partition on flash\n"); pr_err("The nvram size according to the header seems to be bigger than the partition on flash\n");
nvram_len = size; nvram_len = size;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment