nvram.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
  7. * Copyright (C) 2008 Florian Fainelli <florian@openwrt.org>
  8. * Copyright (C) 2012 Jonas Gorski <jonas.gorski@gmail.com>
  9. */
  10. #define pr_fmt(fmt) "bcm63xx_nvram: " fmt
  11. #include <linux/bcm963xx_nvram.h>
  12. #include <linux/init.h>
  13. #include <linux/crc32.h>
  14. #include <linux/export.h>
  15. #include <linux/kernel.h>
  16. #include <linux/if_ether.h>
  17. #include <bcm63xx_nvram.h>
  18. #define BCM63XX_DEFAULT_PSI_SIZE 64
  19. static struct bcm963xx_nvram nvram;
  20. static int mac_addr_used;
  21. void __init bcm63xx_nvram_init(void *addr)
  22. {
  23. u32 crc, expected_crc;
  24. u8 hcs_mac_addr[ETH_ALEN] = { 0x00, 0x10, 0x18, 0xff, 0xff, 0xff };
  25. /* extract nvram data */
  26. memcpy(&nvram, addr, BCM963XX_NVRAM_V5_SIZE);
  27. /* check checksum before using data */
  28. if (bcm963xx_nvram_checksum(&nvram, &expected_crc, &crc))
  29. pr_warn("nvram checksum failed, contents may be invalid (expected %08x, got %08x)\n",
  30. expected_crc, crc);
  31. /* Cable modems have a different NVRAM which is embedded in the eCos
  32. * firmware and not easily extractible, give at least a MAC address
  33. * pool.
  34. */
  35. if (BCMCPU_IS_3368()) {
  36. memcpy(nvram.mac_addr_base, hcs_mac_addr, ETH_ALEN);
  37. nvram.mac_addr_count = 2;
  38. }
  39. }
  40. u8 *bcm63xx_nvram_get_name(void)
  41. {
  42. return nvram.name;
  43. }
  44. EXPORT_SYMBOL(bcm63xx_nvram_get_name);
  45. int bcm63xx_nvram_get_mac_address(u8 *mac)
  46. {
  47. u8 *oui;
  48. int count;
  49. if (mac_addr_used >= nvram.mac_addr_count) {
  50. pr_err("not enough mac addresses\n");
  51. return -ENODEV;
  52. }
  53. memcpy(mac, nvram.mac_addr_base, ETH_ALEN);
  54. oui = mac + ETH_ALEN/2 - 1;
  55. count = mac_addr_used;
  56. while (count--) {
  57. u8 *p = mac + ETH_ALEN - 1;
  58. do {
  59. (*p)++;
  60. if (*p != 0)
  61. break;
  62. p--;
  63. } while (p != oui);
  64. if (p == oui) {
  65. pr_err("unable to fetch mac address\n");
  66. return -ENODEV;
  67. }
  68. }
  69. mac_addr_used++;
  70. return 0;
  71. }
  72. EXPORT_SYMBOL(bcm63xx_nvram_get_mac_address);
  73. int bcm63xx_nvram_get_psi_size(void)
  74. {
  75. if (nvram.psi_size > 0)
  76. return nvram.psi_size;
  77. return BCM63XX_DEFAULT_PSI_SIZE;
  78. }
  79. EXPORT_SYMBOL(bcm63xx_nvram_get_psi_size);