1 /* 2 * This file is subject to the terms and conditions of the GNU General Public 3 * License. See the file "COPYING" in the main directory of this archive 4 * for more details. 5 * 6 * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr> 7 * Copyright (C) 2008 Florian Fainelli <florian@openwrt.org> 8 * Copyright (C) 2012 Jonas Gorski <jonas.gorski@gmail.com> 9 */ 10 11 #define pr_fmt(fmt) "bcm63xx_nvram: " fmt 12 13 #include <linux/init.h> 14 #include <linux/export.h> 15 #include <linux/kernel.h> 16 #include <linux/if_ether.h> 17 18 #include <bcm63xx_nvram.h> 19 20 /* 21 * nvram structure 22 */ 23 struct bcm963xx_nvram { 24 u32 version; 25 u8 reserved1[256]; 26 u8 name[16]; 27 u32 main_tp_number; 28 u32 psi_size; 29 u32 mac_addr_count; 30 u8 mac_addr_base[ETH_ALEN]; 31 u8 reserved2[2]; 32 u32 checksum_old; 33 u8 reserved3[720]; 34 u32 checksum_high; 35 }; 36 37 static struct bcm963xx_nvram nvram; 38 static int mac_addr_used; 39 40 int __init bcm63xx_nvram_init(void *addr) 41 { 42 unsigned int check_len; 43 u8 *p; 44 u32 val; 45 46 /* extract nvram data */ 47 memcpy(&nvram, addr, sizeof(nvram)); 48 49 /* check checksum before using data */ 50 if (nvram.version <= 4) 51 check_len = offsetof(struct bcm963xx_nvram, checksum_old); 52 else 53 check_len = sizeof(nvram); 54 val = 0; 55 p = (u8 *)&nvram; 56 57 while (check_len--) 58 val += *p; 59 if (val) 60 return -EINVAL; 61 62 return 0; 63 } 64 65 u8 *bcm63xx_nvram_get_name(void) 66 { 67 return nvram.name; 68 } 69 EXPORT_SYMBOL(bcm63xx_nvram_get_name); 70 71 int bcm63xx_nvram_get_mac_address(u8 *mac) 72 { 73 u8 *oui; 74 int count; 75 76 if (mac_addr_used >= nvram.mac_addr_count) { 77 pr_err("not enough mac addresses\n"); 78 return -ENODEV; 79 } 80 81 memcpy(mac, nvram.mac_addr_base, ETH_ALEN); 82 oui = mac + ETH_ALEN/2 - 1; 83 count = mac_addr_used; 84 85 while (count--) { 86 u8 *p = mac + ETH_ALEN - 1; 87 88 do { 89 (*p)++; 90 if (*p != 0) 91 break; 92 p--; 93 } while (p != oui); 94 95 if (p == oui) { 96 pr_err("unable to fetch mac address\n"); 97 return -ENODEV; 98 } 99 } 100 101 mac_addr_used++; 102 return 0; 103 } 104 EXPORT_SYMBOL(bcm63xx_nvram_get_mac_address); 105