nvram.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. struct nvram_header *header;
  30. int i;
  31. u32 base = 0;
  32. u32 lim = 0;
  33. u32 off;
  34. u32 *src, *dst;
  35. switch (bcm47xx_bus_type) {
  36. #ifdef CONFIG_BCM47XX_SSB
  37. case BCM47XX_BUS_TYPE_SSB:
  38. mcore_ssb = &bcm47xx_bus.ssb.mipscore;
  39. base = mcore_ssb->flash_window;
  40. lim = mcore_ssb->flash_window_size;
  41. break;
  42. #endif
  43. }
  44. off = FLASH_MIN;
  45. while (off <= lim) {
  46. /* Windowed flash access */
  47. header = (struct nvram_header *)
  48. KSEG1ADDR(base + off - NVRAM_SPACE);
  49. if (header->magic == NVRAM_HEADER)
  50. goto found;
  51. off <<= 1;
  52. }
  53. /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
  54. header = (struct nvram_header *) KSEG1ADDR(base + 4096);
  55. if (header->magic == NVRAM_HEADER)
  56. goto found;
  57. header = (struct nvram_header *) KSEG1ADDR(base + 1024);
  58. if (header->magic == NVRAM_HEADER)
  59. goto found;
  60. return;
  61. found:
  62. src = (u32 *) header;
  63. dst = (u32 *) nvram_buf;
  64. for (i = 0; i < sizeof(struct nvram_header); i += 4)
  65. *dst++ = *src++;
  66. for (; i < header->len && i < NVRAM_SPACE; i += 4)
  67. *dst++ = le32_to_cpu(*src++);
  68. }
  69. int nvram_getenv(char *name, char *val, size_t val_len)
  70. {
  71. char *var, *value, *end, *eq;
  72. if (!name)
  73. return NVRAM_ERR_INV_PARAM;
  74. if (!nvram_buf[0])
  75. early_nvram_init();
  76. /* Look for name=value and return value */
  77. var = &nvram_buf[sizeof(struct nvram_header)];
  78. end = nvram_buf + sizeof(nvram_buf) - 2;
  79. end[0] = end[1] = '\0';
  80. for (; *var; var = value + strlen(value) + 1) {
  81. eq = strchr(var, '=');
  82. if (!eq)
  83. break;
  84. value = eq + 1;
  85. if ((eq - var) == strlen(name) &&
  86. strncmp(var, name, (eq - var)) == 0) {
  87. snprintf(val, val_len, "%s", value);
  88. return 0;
  89. }
  90. }
  91. return NVRAM_ERR_ENVNOTFOUND;
  92. }
  93. EXPORT_SYMBOL(nvram_getenv);