/*
* just test for TY TBW7808
* get mac address from filesystem when wlan0 link up.
* if (read OK & valid data) [not default mac address and not 00:00:00:00:00:00]
* return this mac addr
*else
* gen GUID to support random mac addr then return it
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MY_MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
#define MY_MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
#define MAC_ADDR_FILE "/vendor/mac_addr.info"
/*
* gen random seq and serialize to mac format. save it to [custom_mac]
*/
static void random_soft_mac_address(char *custom_mac, int len)
{
uuid_be uid;
memset(custom_mac, 0, len);
uuid_be_gen(&uid);
sprintf(custom_mac, MY_MACSTR, MY_MAC2STR(uid.b));
memcpy(custom_mac, "88:88:00", 8);
printk(KERN_ERR "\n\n\n\n*****************************************************\n");
printk(KERN_ERR "UUID-MAC :["MY_MACSTR"]\n", MY_MAC2STR(uid.b));
printk(KERN_ERR "custom_mac:[%s]\n", custom_mac);
printk(KERN_ERR "\n\n\n\n*****************************************************\n");
}
static int is_valid_mac(const char *mac, int len)
{
int idx = 0;
if (len < 17) return 0;
if (mac[17] != '\0') return 0;
if ((mac[2] != ':') || (mac[5] != ':') || (mac[8] != ':') || (mac[11] != ':') || (mac[14] != ':'))
return 0;
for (idx=0; idx<6; idx++) {
if (!isxdigit(mac[idx*3+0]) || !isxdigit(mac[idx*3+1]))
return 0;
}
return 1;
}
/*
* get mac addr from rootfilesystem into custom_mac.
* return 0: get OK
* >0: the mac is random
* <0: some error
*/
int ty_read_macaddr_from_fs(char *custom_mac, int len)
{
struct file *fp = NULL;
char buf[18] = {0};
int iret;
if (len < 18) return -1;
memset(custom_mac, 0, len);
fp = filp_open(MAC_ADDR_FILE, O_RDONLY, 0);
if (IS_ERR(fp)) { //File not exists. so Create it and write mac addr
printk(KERN_ERR "[%s-%d] mac address file not exist.\n", __func__, __LINE__);
goto gen_random_mac;
}
else { // mac file found.
iret = kernel_read(fp, 0, buf, 18);
filp_close(fp, NULL);
buf[17] = '\0';
if (!is_valid_mac(buf, sizeof(buf)) || (strncmp(buf , "00:00:00:00:00:00" , 17) == 0)) { //invalid mac addr
printk(KERN_ERR "[%s-%d] mac address file .\n", __func__, __LINE__);
goto gen_random_mac;
}
else { //mac addr OK
memcpy(custom_mac, buf, 17);
}
}
return 0;
gen_random_mac:
random_soft_mac_address(custom_mac, len);
return 1;
}
/*
* 0: write OK.
* else : some error.
* maybe this file should be created by android framework ?
*/
/*
int ty_write_macaddr_to_fs(const char *custom_mac, int len)
{
struct file *fp = NULL;
mm_segment_t oldfs = {0};
if (len < 18) return -1;
fp = filp_open(MAC_ADDR_FILE, O_RDWR | O_CREAT, 0666);
if (IS_ERR(fp)) { //can't create file ?
printk(KERN_ERR "[%s-%d] can't create mac address file.\n", __func__, __LINE__);
return -1;
}
else { //create file OK. now write mac addr to file.
oldfs = get_fs();
set_fs(get_ds());
if (fp->f_mode & FMODE_WRITE) {
ret = fp->f_op->write(fp,
(const char *)custom_mac,
len, &fp->f_pos);
if (ret < 0)
DHD_ERROR(("[WIFI] Mac address [%s] Failed to write into File.\n", custom_mac));
else
DHD_INFO(("[WIFI] Mac address [%s] written into File.\n", custom_mac));
}
set_fs(oldfs);
filp_close(fp, NULL);
}
return 0;
}
*/