1 #include <linux/types.h>
2 #include <linux/init.h>
5 #include <linux/fs_struct.h>
6 #include <linux/path.h>
7 #include <linux/uaccess.h>
8 #include <linux/slab.h>
9 #include "wlan_common.h"
11 static int hex2num(char c)
13 if (c >= '0' && c <= '9')
16 if (c >= 'a' && c <= 'f')
19 if (c >= 'A' && c <= 'F')
25 static int hex2byte(const char *hex)
41 * hexstr2bin - Convert ASCII hex string into binary data
42 * @hex: ASCII hex string (e.g., "01ab")
43 * @buf: Buffer for the binary data
44 * @len: Length of the text to convert in bytes (of buf); hex will be double
46 * Returns: 0 on success, -1 on failure (invalid hex string)
48 static int hexstr2bin(const char *hex, u8 *buf, size_t len)
52 const char *ipos = hex;
55 for (i = 0; i < len; i++) {
66 static struct hostap_conf *hostap_conf_create(void)
68 struct hostap_conf *conf = NULL;
69 conf = kmalloc(sizeof(struct hostap_conf), GFP_KERNEL);
74 static int fp_size(struct file *f)
79 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0))
80 error = vfs_getattr(&f->f_path, &stat);
82 error = vfs_getattr(f->f_path.mnt, f->f_path.dentry, &stat);
88 pr_err("get hostapd conf file stat error\n");
93 static int hostap_conf_read(char *filename, char **buf)
100 fp = filp_open(filename, O_RDONLY, 0);
102 pr_err("open %s file error\n", filename);
110 pr_err("load file:%s error\n", filename);
114 *buf = kzalloc(size + 1, GFP_KERNEL);
115 vfs_read(fp, *buf, size, &pos);
118 filp_close(fp, NULL);
124 static char *get_line(char *buf, char *end)
126 if (buf == NULL || buf >= end)
128 while (buf != end && *buf != '\n')
134 static unsigned char hostap_conf_parse(char *buf, int size,
135 struct hostap_conf *conf)
137 unsigned char ret = 0;
138 char *spos = buf, *epos = NULL, *pos = NULL;
139 int line = 0, errors = 0;
144 for (; (epos = get_line(spos, buf + size)); (spos = epos + 1)) {
149 while (*pos != '\0' && pos <= epos) {
160 pos = strchr(spos, '=');
162 pr_err("Line %d: invalid line '%s'", line, spos);
170 if (strcmp(spos, "wpa_psk") == 0) {
171 strlcpy(conf->wpa_psk, pos, sizeof(conf->wpa_psk));
172 conf->len = strlen(pos);
181 int hostap_conf_load(char *filename, unsigned char *key_val)
183 struct hostap_conf *conf = NULL;
187 if (filename == NULL)
188 filename = HOSTAP_CONF_FILE_NAME;
190 size = hostap_conf_read(filename, &buf);
192 conf = hostap_conf_create();
195 pr_err("create hostap_conf struct error.\n");
199 hostap_conf_parse(buf, size, conf);
200 if (conf->len > 64) {
201 pr_err("wpa_psk len is error.(%d)\n", conf->len);
204 hexstr2bin(conf->wpa_psk, key_val, conf->len / 2);