nvram.c 2.4 KB

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