bcm963xx_nvram.h 3.0 KB

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