bcm963xx_nvram.h 2.9 KB

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