nvram.c 2.3 KB

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