Skip to content
Snippets Groups Projects
Unverified Commit 2e265e03 authored by Matthias Schiffer's avatar Matthias Schiffer
Browse files

gluon-core: refactor primary MAC logic

Remove a lot of redundant code by switching to a match table listing
the targets and boards for each candidate for the primary MAC interface.

In addition, we add some flexiblity by allow to switch out the sysfs file
data source for the MAC address.
parent 59d182fe
Branches
Tags
No related merge requests found
...@@ -8,71 +8,132 @@ if sysconfig.primary_mac then ...@@ -8,71 +8,132 @@ if sysconfig.primary_mac then
end end
local util = require 'gluon.util'
local platform = require 'gluon.platform' local platform = require 'gluon.platform'
local util = require 'gluon.util'
local try_files = { local function sysfs(...)
'/sys/class/net/eth0/address' local path = string.format(...)
} return function()
local addr = util.readfile(path)
if addr then
return util.trim(addr)
end
end
end
if not ( local function eth(index)
util.contains({'x86', 'brcm2708'}, platform.get_target()) or return sysfs('/sys/class/net/eth%d/address', index)
platform.match('ar71xx', 'mikrotik')
) then
table.insert(try_files, 1, '/sys/class/ieee80211/phy0/macaddress')
end end
if platform.match('ar71xx', 'generic', {'tl-wdr3600', 'tl-wdr4300', local function phy(index)
'tl-wr902ac-v1'}) then return sysfs('/sys/class/ieee80211/phy%d/macaddress', index)
table.insert(try_files, 1, '/sys/class/ieee80211/phy1/macaddress') end
elseif platform.match('ar71xx', 'generic', {'a40', 'a60',
-- Entries are matched in the order they are listed
local primary_addrs = {
{eth(0), {
{'x86'},
{'brcm2708'},
{'ar71xx', 'generic', {
'a40',
'a60',
'archer-c25-v1', 'archer-c25-v1',
'archer-c60-v2', 'archer-c60-v2',
'archer-c7-v4', 'archer-c7-v5', 'archer-c7-v4',
'archer-c7-v5',
'carambola2', 'carambola2',
'koala', 'koala',
'mr600', 'mr600v2', 'mr600',
'mr900', 'mr900v2', 'mr600v2',
'mr1750', 'mr1750v2', 'mr900',
'om2p', 'om2pv2', 'om2pv4', 'mr900v2',
'om2p-hs', 'om2p-hsv2', 'om2p-hsv3', 'mr1750',
'mr1750v2',
'om2p',
'om2pv2',
'om2pv4',
'om2p-hs',
'om2p-hsv2',
'om2p-hsv3',
'om2p-hsv4', 'om2p-hsv4',
'om2p-lc', 'om2p-lc',
'om5p', 'om5p-an', 'om5p',
'om5p-ac', 'om5p-acv2', 'om5p-an',
'om5p-ac',
'om5p-acv2',
'unifi-outdoor-plus', 'unifi-outdoor-plus',
'unifiac-lite', 'unifiac-pro'}) then 'unifiac-lite',
table.insert(try_files, 1, '/sys/class/net/eth0/address') 'unifiac-pro',
elseif platform.match('ar71xx', 'generic', {'archer-c5', 'archer-c58-v1', }},
'archer-c59-v1', 'archer-c60-v1', {'ar71xx', 'mikrotik'},
'archer-c7'}) then {'ar71xx', 'nand', {
table.insert(try_files, 1, '/sys/class/net/eth1/address') 'hiveap-121',
elseif platform.match('ar71xx', 'nand', {'hiveap-121'}) then }},
table.insert(try_files, 1, '/sys/class/net/eth0/address') {'ath79', 'generic', {
elseif platform.match('ath79', 'generic', {'ocedo,raccoon'}) then 'ocedo,raccoon',
table.insert(try_files, 1, '/sys/class/net/eth0/address') }},
elseif platform.match('ipq40xx', 'generic', {'avm,fritzbox-4040', {'ipq40xx', 'generic', {
'openmesh,a42', 'openmesh,a62'}) then 'avm,fritzbox-4040',
table.insert(try_files, 1, '/sys/class/net/eth0/address') 'openmesh,a42',
elseif platform.match('ipq806x', 'generic', {'netgear,r7800'}) then 'openmesh,a62',
table.insert(try_files, 1, '/sys/class/net/eth1/address') }},
elseif platform.match('mpc85xx', 'p1020', {'aerohive,hiveap-330'}) then {'mpc85xx', 'p1020', {
table.insert(try_files, 1, '/sys/class/net/eth0/address') 'aerohive,hiveap-330',
elseif platform.match('mpc85xx', 'p1020', {'ocedo,panda'}) then }},
table.insert(try_files, 1, '/sys/class/net/eth1/address') {'ramips', 'mt7620', {
elseif platform.match('ramips', 'mt7620', {'miwifi-mini', 'tplink,c2-v1', 'c20-v1', 'c20i', 'c50'}) then 'miwifi-mini', 'tplink,c2-v1', 'c20-v1', 'c20i', 'c50',
table.insert(try_files, 1, '/sys/class/net/eth0/address') }},
elseif platform.match('ramips', 'mt7621', {'dir-860l-b1'}) then }},
table.insert(try_files, 1, '/sys/class/ieee80211/phy1/macaddress') {eth(1), {
end {'ar71xx', 'generic', {
'archer-c5',
'archer-c58-v1',
'archer-c59-v1',
'archer-c60-v1',
'archer-c7',
}},
{'ipq806x', 'generic', {
'netgear,r7800',
}},
{'mpc85xx', 'p1020', {
'ocedo,panda',
}},
}},
{phy(1), {
{'ar71xx', 'generic', {
'tl-wdr3600',
'tl-wdr4300',
'tl-wr902ac-v1',
}},
{'ramips', 'mt7621', {
'dir-860l-b1',
}},
}},
-- phy0 default
{phy(0), {
{}, -- matches everything
}},
-- eth0 fallback when phy0 does not exist
{eth(0), {
{}, -- matches everything
}},
}
for _, file in ipairs(try_files) do for _, matcher in ipairs(primary_addrs) do
local addr = util.readfile(file) local f, matches = unpack(matcher)
for _, match in ipairs(matches) do
if platform.match(unpack(match)) then
local addr = f()
if addr then if addr then
sysconfig.primary_mac = util.trim(addr) sysconfig.primary_mac = addr
break return
end end
end end
end
end
error('no primary MAC address found')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment