032-09-MIPS-BCM47xx-Add-helper-variable-for-storing-NVRAM-l.patch 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. From f229d75f1472c4cd30f464e4a0f94f410046bd80 Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <zajec5@gmail.com>
  3. Date: Sat, 6 Jun 2015 23:16:23 +0200
  4. Subject: [PATCH] MIPS: BCM47xx: Add helper variable for storing NVRAM length
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. This simplifies code just a bit (also maybe makes it a bit more
  9. intuitive?) and will allow us to stop storing header. Right now we copy
  10. whole NVRAM including its header to the internal buffer. It is not
  11. needed to store a header as we don't access all these details like CRC,
  12. flags, etc. The next improvement that should follow is copying only the
  13. real contents.
  14. Signed-off-by: Rafał Miłecki <zajec5@gmail.com>
  15. Acked-by: Hauke Mehrtens <hauke@hauke-m.de>
  16. Cc: linux-mips@linux-mips.org
  17. Cc: Arend van Spriel <arend@broadcom.com>
  18. Cc: Hante Meuleman <meuleman@broadcom.com>
  19. Patchwork: https://patchwork.linux-mips.org/patch/10535/
  20. Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
  21. ---
  22. arch/mips/bcm47xx/nvram.c | 37 ++++++++++++++++---------------------
  23. 1 file changed, 16 insertions(+), 21 deletions(-)
  24. --- a/arch/mips/bcm47xx/nvram.c
  25. +++ b/arch/mips/bcm47xx/nvram.c
  26. @@ -35,6 +35,7 @@ struct nvram_header {
  27. };
  28. static char nvram_buf[NVRAM_SPACE];
  29. +static size_t nvram_len;
  30. static const u32 nvram_sizes[] = {0x8000, 0xF000, 0x10000};
  31. static u32 find_nvram_size(void __iomem *end)
  32. @@ -60,7 +61,7 @@ static int nvram_find_and_copy(void __io
  33. u32 *src, *dst;
  34. u32 size;
  35. - if (nvram_buf[0]) {
  36. + if (nvram_len) {
  37. pr_warn("nvram already initialized\n");
  38. return -EEXIST;
  39. }
  40. @@ -99,17 +100,18 @@ found:
  41. for (i = 0; i < sizeof(struct nvram_header); i += 4)
  42. *dst++ = __raw_readl(src++);
  43. header = (struct nvram_header *)nvram_buf;
  44. - if (header->len > size) {
  45. + nvram_len = header->len;
  46. + if (nvram_len > size) {
  47. pr_err("The nvram size according to the header seems to be bigger than the partition on flash\n");
  48. - header->len = size;
  49. + nvram_len = size;
  50. }
  51. - if (header->len >= NVRAM_SPACE) {
  52. + if (nvram_len >= NVRAM_SPACE) {
  53. pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
  54. header->len, NVRAM_SPACE - 1);
  55. - header->len = NVRAM_SPACE - 1;
  56. + nvram_len = NVRAM_SPACE - 1;
  57. }
  58. /* proceed reading data after header */
  59. - for (; i < header->len; i += 4)
  60. + for (; i < nvram_len; i += 4)
  61. *dst++ = readl(src++);
  62. nvram_buf[NVRAM_SPACE - 1] = '\0';
  63. @@ -144,7 +146,6 @@ static int nvram_init(void)
  64. #ifdef CONFIG_MTD
  65. struct mtd_info *mtd;
  66. struct nvram_header header;
  67. - struct nvram_header *pheader;
  68. size_t bytes_read;
  69. int err;
  70. @@ -155,20 +156,16 @@ static int nvram_init(void)
  71. err = mtd_read(mtd, 0, sizeof(header), &bytes_read, (uint8_t *)&header);
  72. if (!err && header.magic == NVRAM_MAGIC &&
  73. header.len > sizeof(header)) {
  74. - if (header.len >= NVRAM_SPACE) {
  75. + nvram_len = header.len;
  76. + if (nvram_len >= NVRAM_SPACE) {
  77. pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
  78. header.len, NVRAM_SPACE);
  79. - header.len = NVRAM_SPACE - 1;
  80. + nvram_len = NVRAM_SPACE - 1;
  81. }
  82. - err = mtd_read(mtd, 0, header.len, &bytes_read,
  83. + err = mtd_read(mtd, 0, nvram_len, &nvram_len,
  84. (u8 *)nvram_buf);
  85. - if (err)
  86. - return err;
  87. -
  88. - pheader = (struct nvram_header *)nvram_buf;
  89. - pheader->len = header.len;
  90. - return 0;
  91. + return err;
  92. }
  93. #endif
  94. @@ -183,7 +180,7 @@ int bcm47xx_nvram_getenv(const char *nam
  95. if (!name)
  96. return -EINVAL;
  97. - if (!nvram_buf[0]) {
  98. + if (!nvram_len) {
  99. err = nvram_init();
  100. if (err)
  101. return err;
  102. @@ -231,16 +228,14 @@ char *bcm47xx_nvram_get_contents(size_t
  103. {
  104. int err;
  105. char *nvram;
  106. - struct nvram_header *header;
  107. - if (!nvram_buf[0]) {
  108. + if (!nvram_len) {
  109. err = nvram_init();
  110. if (err)
  111. return NULL;
  112. }
  113. - header = (struct nvram_header *)nvram_buf;
  114. - *nvram_size = header->len - sizeof(struct nvram_header);
  115. + *nvram_size = nvram_len - sizeof(struct nvram_header);
  116. nvram = vmalloc(*nvram_size);
  117. if (!nvram)
  118. return NULL;