Root/target/linux/ar71xx/files/arch/mips/ar71xx/nvram.c

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
11#include <linux/kernel.h>
12#include <linux/vmalloc.h>
13#include <linux/errno.h>
14#include <linux/init.h>
15#include <linux/string.h>
16
17#include "nvram.h"
18
19char *nvram_find_var(const char *name, const char *buf, unsigned buf_len)
20{
21    unsigned len = strlen(name);
22    char *cur, *last;
23
24    if (buf_len == 0 || len == 0)
25        return NULL;
26
27    if (buf_len < len)
28        return NULL;
29
30    if (len == 1)
31        return memchr(buf, (int) *name, buf_len);
32
33    last = (char *) buf + buf_len - len;
34    for (cur = (char *) buf; cur <= last; cur++)
35        if (cur[0] == name[0] && memcmp(cur, name, len) == 0)
36            return cur + len;
37
38    return NULL;
39}
40
41int nvram_parse_mac_addr(const char *nvram, unsigned nvram_len,
42             const char *name, char *mac)
43{
44    char *buf;
45    char *mac_str;
46    int ret;
47    int t;
48
49    buf = vmalloc(nvram_len);
50    if (!buf)
51        return -ENOMEM;
52
53    memcpy(buf, nvram, nvram_len);
54    buf[nvram_len - 1] = '\0';
55
56    mac_str = nvram_find_var(name, buf, nvram_len);
57    if (!mac_str) {
58        ret = -EINVAL;
59        goto free;
60    }
61
62    t = sscanf(mac_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
63           &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]);
64
65    if (t != 6) {
66        ret = -EINVAL;
67        goto free;
68    }
69
70    ret = 0;
71
72free:
73    vfree(buf);
74    return ret;
75}
76

Archive Download this file



interactive