1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_BCM963XX_NVRAM_H__
3 #define __LINUX_BCM963XX_NVRAM_H__
4
5 #include <linux/crc32.h>
6 #include <linux/if_ether.h>
7 #include <linux/sizes.h>
8 #include <linux/types.h>
9
10 /*
11 * Broadcom BCM963xx SoC board nvram data structure.
12 *
13 * The nvram structure varies in size depending on the SoC board version. Use
14 * the appropriate minimum BCM963XX_NVRAM_*_SIZE define for the information
15 * you need instead of sizeof(struct bcm963xx_nvram) as this may change.
16 */
17
18 #define BCM963XX_NVRAM_V4_SIZE 300
19 #define BCM963XX_NVRAM_V5_SIZE (1 * SZ_1K)
20
21 #define BCM963XX_DEFAULT_PSI_SIZE 64
22
23 enum bcm963xx_nvram_nand_part {
24 BCM963XX_NVRAM_NAND_PART_BOOT = 0,
25 BCM963XX_NVRAM_NAND_PART_ROOTFS_1,
26 BCM963XX_NVRAM_NAND_PART_ROOTFS_2,
27 BCM963XX_NVRAM_NAND_PART_DATA,
28 BCM963XX_NVRAM_NAND_PART_BBT,
29
30 __BCM963XX_NVRAM_NAND_NR_PARTS
31 };
32
33 struct bcm963xx_nvram {
34 u32 version;
35 char bootline[256];
36 char name[16];
37 u32 main_tp_number;
38 u32 psi_size;
39 u32 mac_addr_count;
40 u8 mac_addr_base[ETH_ALEN];
41 u8 __reserved1[2];
42 u32 checksum_v4;
43
44 u8 __reserved2[292];
45 u32 nand_part_offset[__BCM963XX_NVRAM_NAND_NR_PARTS];
46 u32 nand_part_size[__BCM963XX_NVRAM_NAND_NR_PARTS];
47 u8 __reserved3[388];
48 u32 checksum_v5;
49 };
50
51 #define BCM963XX_NVRAM_NAND_PART_OFFSET(nvram, part) \
52 bcm963xx_nvram_nand_part_offset(nvram, BCM963XX_NVRAM_NAND_PART_ ##part)
53
bcm963xx_nvram_nand_part_offset(const struct bcm963xx_nvram * nvram,enum bcm963xx_nvram_nand_part part)54 static inline u64 __pure bcm963xx_nvram_nand_part_offset(
55 const struct bcm963xx_nvram *nvram,
56 enum bcm963xx_nvram_nand_part part)
57 {
58 return nvram->nand_part_offset[part] * SZ_1K;
59 }
60
61 #define BCM963XX_NVRAM_NAND_PART_SIZE(nvram, part) \
62 bcm963xx_nvram_nand_part_size(nvram, BCM963XX_NVRAM_NAND_PART_ ##part)
63
bcm963xx_nvram_nand_part_size(const struct bcm963xx_nvram * nvram,enum bcm963xx_nvram_nand_part part)64 static inline u64 __pure bcm963xx_nvram_nand_part_size(
65 const struct bcm963xx_nvram *nvram,
66 enum bcm963xx_nvram_nand_part part)
67 {
68 return nvram->nand_part_size[part] * SZ_1K;
69 }
70
71 /*
72 * bcm963xx_nvram_checksum - Verify nvram checksum
73 *
74 * @nvram: pointer to full size nvram data structure
75 * @expected_out: optional pointer to store expected checksum value
76 * @actual_out: optional pointer to store actual checksum value
77 *
78 * Return: 0 if the checksum is valid, otherwise -EINVAL
79 */
bcm963xx_nvram_checksum(const struct bcm963xx_nvram * nvram,u32 * expected_out,u32 * actual_out)80 static int __maybe_unused bcm963xx_nvram_checksum(
81 const struct bcm963xx_nvram *nvram,
82 u32 *expected_out, u32 *actual_out)
83 {
84 u32 expected, actual;
85 size_t len;
86
87 if (nvram->version <= 4) {
88 expected = nvram->checksum_v4;
89 len = BCM963XX_NVRAM_V4_SIZE - sizeof(u32);
90 } else {
91 expected = nvram->checksum_v5;
92 len = BCM963XX_NVRAM_V5_SIZE - sizeof(u32);
93 }
94
95 /*
96 * Calculate the CRC32 value for the nvram with a checksum value
97 * of 0 without modifying or copying the nvram by combining:
98 * - The CRC32 of the nvram without the checksum value
99 * - The CRC32 of a zero checksum value (which is also 0)
100 */
101 actual = crc32_le_combine(
102 crc32_le(~0, (u8 *)nvram, len), 0, sizeof(u32));
103
104 if (expected_out)
105 *expected_out = expected;
106
107 if (actual_out)
108 *actual_out = actual;
109
110 return expected == actual ? 0 : -EINVAL;
111 };
112
113 #endif /* __LINUX_BCM963XX_NVRAM_H__ */
114