nvram.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * BCM947xx nvram variable access
  3. *
  4. * Copyright (C) 2005 Broadcom Corporation
  5. * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
  6. * Copyright (C) 2010-2011 Hauke Mehrtens <hauke@hauke-m.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/types.h>
  15. #include <linux/module.h>
  16. #include <linux/ssb/ssb.h>
  17. #include <linux/kernel.h>
  18. #include <linux/string.h>
  19. #include <asm/addrspace.h>
  20. #include <asm/mach-bcm47xx/nvram.h>
  21. #include <asm/mach-bcm47xx/bcm47xx.h>
  22. static char nvram_buf[NVRAM_SPACE];
  23. /* Probe for NVRAM header */
  24. static void early_nvram_init(void)
  25. {
  26. #ifdef CONFIG_BCM47XX_SSB
  27. struct ssb_mipscore *mcore_ssb;
  28. #endif
  29. #ifdef CONFIG_BCM47XX_BCMA
  30. struct bcma_drv_cc *bcma_cc;
  31. #endif
  32. struct nvram_header *header;
  33. int i;
  34. u32 base = 0;
  35. u32 lim = 0;
  36. u32 off;
  37. u32 *src, *dst;
  38. switch (bcm47xx_bus_type) {
  39. #ifdef CONFIG_BCM47XX_SSB
  40. case BCM47XX_BUS_TYPE_SSB:
  41. mcore_ssb = &bcm47xx_bus.ssb.mipscore;
  42. base = mcore_ssb->flash_window;
  43. lim = mcore_ssb->flash_window_size;
  44. break;
  45. #endif
  46. #ifdef CONFIG_BCM47XX_BCMA
  47. case BCM47XX_BUS_TYPE_BCMA:
  48. bcma_cc = &bcm47xx_bus.bcma.bus.drv_cc;
  49. base = bcma_cc->pflash.window;
  50. lim = bcma_cc->pflash.window_size;
  51. break;
  52. #endif
  53. }
  54. off = FLASH_MIN;
  55. while (off <= lim) {
  56. /* Windowed flash access */
  57. header = (struct nvram_header *)
  58. KSEG1ADDR(base + off - NVRAM_SPACE);
  59. if (header->magic == NVRAM_HEADER)
  60. goto found;
  61. off <<= 1;
  62. }
  63. /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
  64. header = (struct nvram_header *) KSEG1ADDR(base + 4096);
  65. if (header->magic == NVRAM_HEADER)
  66. goto found;
  67. header = (struct nvram_header *) KSEG1ADDR(base + 1024);
  68. if (header->magic == NVRAM_HEADER)
  69. goto found;
  70. return;
  71. found:
  72. src = (u32 *) header;
  73. dst = (u32 *) nvram_buf;
  74. for (i = 0; i < sizeof(struct nvram_header); i += 4)
  75. *dst++ = *src++;
  76. for (; i < header->len && i < NVRAM_SPACE; i += 4)
  77. *dst++ = le32_to_cpu(*src++);
  78. }
  79. int nvram_getenv(char *name, char *val, size_t val_len)
  80. {
  81. char *var, *value, *end, *eq;
  82. if (!name)
  83. return NVRAM_ERR_INV_PARAM;
  84. if (!nvram_buf[0])
  85. early_nvram_init();
  86. /* Look for name=value and return value */
  87. var = &nvram_buf[sizeof(struct nvram_header)];
  88. end = nvram_buf + sizeof(nvram_buf) - 2;
  89. end[0] = end[1] = '\0';
  90. for (; *var; var = value + strlen(value) + 1) {
  91. eq = strchr(var, '=');
  92. if (!eq)
  93. break;
  94. value = eq + 1;
  95. if ((eq - var) == strlen(name) &&
  96. strncmp(var, name, (eq - var)) == 0) {
  97. return snprintf(val, val_len, "%s", value);
  98. }
  99. }
  100. return NVRAM_ERR_ENVNOTFOUND;
  101. }
  102. EXPORT_SYMBOL(nvram_getenv);