nvram.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Atheros AR71xx minimal nvram support
  3. *
  4. * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/vmalloc.h>
  12. #include <linux/errno.h>
  13. #include <linux/init.h>
  14. #include <linux/string.h>
  15. #include "nvram.h"
  16. char *ath79_nvram_find_var(const char *name, const char *buf, unsigned buf_len)
  17. {
  18. unsigned len = strlen(name);
  19. char *cur, *last;
  20. if (buf_len == 0 || len == 0)
  21. return NULL;
  22. if (buf_len < len)
  23. return NULL;
  24. if (len == 1)
  25. return memchr(buf, (int) *name, buf_len);
  26. last = (char *) buf + buf_len - len;
  27. for (cur = (char *) buf; cur <= last; cur++)
  28. if (cur[0] == name[0] && memcmp(cur, name, len) == 0)
  29. return cur + len;
  30. return NULL;
  31. }
  32. int ath79_nvram_parse_mac_addr(const char *nvram, unsigned nvram_len,
  33. const char *name, char *mac)
  34. {
  35. char *buf;
  36. char *mac_str;
  37. int ret;
  38. int t;
  39. buf = vmalloc(nvram_len);
  40. if (!buf)
  41. return -ENOMEM;
  42. memcpy(buf, nvram, nvram_len);
  43. buf[nvram_len - 1] = '\0';
  44. mac_str = ath79_nvram_find_var(name, buf, nvram_len);
  45. if (!mac_str) {
  46. ret = -EINVAL;
  47. goto free;
  48. }
  49. if (strlen(mac_str) == 19 && mac_str[0] == '"' && mac_str[18] == '"') {
  50. mac_str[18] = 0;
  51. mac_str++;
  52. }
  53. t = sscanf(mac_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
  54. &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]);
  55. if (t != 6) {
  56. ret = -EINVAL;
  57. goto free;
  58. }
  59. ret = 0;
  60. free:
  61. vfree(buf);
  62. return ret;
  63. }