nvram.c 2.3 KB

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