nvram.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2013 Broadcom Corporation
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  11. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  13. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/slab.h>
  18. #include <linux/firmware.h>
  19. #include "nvram.h"
  20. /* brcmf_nvram_strip :Takes a buffer of "<var>=<value>\n" lines read from a file
  21. * and ending in a NUL. Removes carriage returns, empty lines, comment lines,
  22. * and converts newlines to NULs. Shortens buffer as needed and pads with NULs.
  23. * End of buffer is completed with token identifying length of buffer.
  24. */
  25. void *brcmf_nvram_strip(const struct firmware *nv, u32 *new_length)
  26. {
  27. u8 *nvram;
  28. u32 i;
  29. u32 len;
  30. u32 column;
  31. u8 val;
  32. bool comment;
  33. u32 token;
  34. __le32 token_le;
  35. /* Alloc for extra 0 byte + roundup by 4 + length field */
  36. nvram = kmalloc(nv->size + 1 + 3 + sizeof(token_le), GFP_KERNEL);
  37. if (!nvram)
  38. return NULL;
  39. len = 0;
  40. column = 0;
  41. comment = false;
  42. for (i = 0; i < nv->size; i++) {
  43. val = nv->data[i];
  44. if (val == 0)
  45. break;
  46. if (val == '\r')
  47. continue;
  48. if (comment && (val != '\n'))
  49. continue;
  50. comment = false;
  51. if (val == '#') {
  52. comment = true;
  53. continue;
  54. }
  55. if (val == '\n') {
  56. if (column == 0)
  57. continue;
  58. nvram[len] = 0;
  59. len++;
  60. column = 0;
  61. continue;
  62. }
  63. nvram[len] = val;
  64. len++;
  65. column++;
  66. }
  67. column = len;
  68. *new_length = roundup(len + 1, 4);
  69. while (column != *new_length) {
  70. nvram[column] = 0;
  71. column++;
  72. }
  73. token = *new_length / 4;
  74. token = (~token << 16) | (token & 0x0000FFFF);
  75. token_le = cpu_to_le32(token);
  76. memcpy(&nvram[*new_length], &token_le, sizeof(token_le));
  77. *new_length += sizeof(token_le);
  78. return nvram;
  79. }
  80. void brcmf_nvram_free(void *nvram)
  81. {
  82. kfree(nvram);
  83. }