032-08-mips-bcm47xx-allow-retrieval-of-complete-nvram-conte.patch 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. From 2536295c2aeafc769215a6b2883126fa94c90b9a Mon Sep 17 00:00:00 2001
  2. From: Hante Meuleman <meuleman@broadcom.com>
  3. Date: Thu, 21 May 2015 15:27:23 +0200
  4. Subject: [PATCH] mips: bcm47xx: allow retrieval of complete nvram contents
  5. Host platforms such as routers supported by OpenWrt can
  6. support NVRAM reading directly from internal NVRAM store.
  7. The brcmfmac for one requires the complete nvram contents
  8. to select what needs to be sent to wireless device.
  9. Signed-off-by: Arend van Spriel <arend@broadcom.com>
  10. Signed-off-by: Hante Meuleman <meuleman@broadcom.com>
  11. Reviewed-by: Arend Van Spriel <arend@broadcom.com>
  12. Reviewed-by: Franky (Zhenhui) Lin <frankyl@broadcom.com>
  13. Reviewed-by: Pieter-Paul Giesberts <pieterpg@broadcom.com>
  14. Reviewed-by: Daniel (Deognyoun) Kim <dekim@broadcom.com>
  15. Cc: linux-mips@linux-mips.org
  16. Patchwork: https://patchwork.linux-mips.org/patch/10093/
  17. Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
  18. ---
  19. arch/mips/bcm47xx/nvram.c | 60 ++++++++++++++++++++++++++++++++-----------
  20. include/linux/bcm47xx_nvram.h | 15 +++++++++++
  21. 2 files changed, 60 insertions(+), 15 deletions(-)
  22. --- a/arch/mips/bcm47xx/nvram.c
  23. +++ b/arch/mips/bcm47xx/nvram.c
  24. @@ -94,17 +94,22 @@ static int nvram_find_and_copy(void __io
  25. return -ENXIO;
  26. found:
  27. - if (header->len > size)
  28. - pr_err("The nvram size accoridng to the header seems to be bigger than the partition on flash\n");
  29. - if (header->len > NVRAM_SPACE)
  30. - pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
  31. - header->len, NVRAM_SPACE - 1);
  32. -
  33. src = (u32 *)header;
  34. dst = (u32 *)nvram_buf;
  35. for (i = 0; i < sizeof(struct nvram_header); i += 4)
  36. *dst++ = __raw_readl(src++);
  37. - for (; i < header->len && i < NVRAM_SPACE && i < size; i += 4)
  38. + header = (struct nvram_header *)nvram_buf;
  39. + if (header->len > size) {
  40. + pr_err("The nvram size according to the header seems to be bigger than the partition on flash\n");
  41. + header->len = size;
  42. + }
  43. + if (header->len >= NVRAM_SPACE) {
  44. + pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
  45. + header->len, NVRAM_SPACE - 1);
  46. + header->len = NVRAM_SPACE - 1;
  47. + }
  48. + /* proceed reading data after header */
  49. + for (; i < header->len; i += 4)
  50. *dst++ = readl(src++);
  51. nvram_buf[NVRAM_SPACE - 1] = '\0';
  52. @@ -139,6 +144,7 @@ static int nvram_init(void)
  53. #ifdef CONFIG_MTD
  54. struct mtd_info *mtd;
  55. struct nvram_header header;
  56. + struct nvram_header *pheader;
  57. size_t bytes_read;
  58. int err;
  59. @@ -147,20 +153,21 @@ static int nvram_init(void)
  60. return -ENODEV;
  61. err = mtd_read(mtd, 0, sizeof(header), &bytes_read, (uint8_t *)&header);
  62. - if (!err && header.magic == NVRAM_MAGIC) {
  63. - u8 *dst = (uint8_t *)nvram_buf;
  64. - size_t len = header.len;
  65. -
  66. - if (len >= NVRAM_SPACE) {
  67. - len = NVRAM_SPACE - 1;
  68. + if (!err && header.magic == NVRAM_MAGIC &&
  69. + header.len > sizeof(header)) {
  70. + if (header.len >= NVRAM_SPACE) {
  71. pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
  72. - header.len, len);
  73. + header.len, NVRAM_SPACE);
  74. + header.len = NVRAM_SPACE - 1;
  75. }
  76. - err = mtd_read(mtd, 0, len, &bytes_read, dst);
  77. + err = mtd_read(mtd, 0, header.len, &bytes_read,
  78. + (u8 *)nvram_buf);
  79. if (err)
  80. return err;
  81. + pheader = (struct nvram_header *)nvram_buf;
  82. + pheader->len = header.len;
  83. return 0;
  84. }
  85. #endif
  86. @@ -219,3 +226,26 @@ int bcm47xx_nvram_gpio_pin(const char *n
  87. return -ENOENT;
  88. }
  89. EXPORT_SYMBOL(bcm47xx_nvram_gpio_pin);
  90. +
  91. +char *bcm47xx_nvram_get_contents(size_t *nvram_size)
  92. +{
  93. + int err;
  94. + char *nvram;
  95. + struct nvram_header *header;
  96. +
  97. + if (!nvram_buf[0]) {
  98. + err = nvram_init();
  99. + if (err)
  100. + return NULL;
  101. + }
  102. +
  103. + header = (struct nvram_header *)nvram_buf;
  104. + *nvram_size = header->len - sizeof(struct nvram_header);
  105. + nvram = vmalloc(*nvram_size);
  106. + if (!nvram)
  107. + return NULL;
  108. + memcpy(nvram, &nvram_buf[sizeof(struct nvram_header)], *nvram_size);
  109. +
  110. + return nvram;
  111. +}
  112. +EXPORT_SYMBOL(bcm47xx_nvram_get_contents);