base-files: don't store label MAC address in uci system config
authorAdrian Schmutzler <freifunk@adrianschmutzler.de>
Mon, 4 Nov 2019 19:44:41 +0000 (20:44 +0100)
committerAdrian Schmutzler <freifunk@adrianschmutzler.de>
Thu, 7 Nov 2019 16:13:18 +0000 (17:13 +0100)
If set, label MAC address is available from one of two sources,
device tree or board.json. So far, the function get_mac_label
was meant for retrieving the address, while an option in uci
system config was specified only for case 2 (board.json).

The uci config option has several drawbacks:
- it is only used for a fraction of devices (those not in DT)
- label MAC address is a device property, while config implies
  user interaction
- label_macaddr option will only be set if /etc/config/system
  does not exist (i.e. only for new installations)

Thus, this patch changes the behavior of get_mac_label:
Instead of writing the value in board.json to uci system config
and reading from this location afterwards, get_mac_label now
extracts data from board.json directly. The uci config option
won't be used anymore.
In addition, two utility functions for extraction only from DT
or from board.json are introduced.

Since this is only changing the access to the label MAC address, it
won't interfere with the addresses stored in the code base so far.

Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>
package/base-files/files/bin/config_generate
package/base-files/files/lib/functions/system.sh

index 0b26afe57f445cd2fee145166826b0a27ad0fcb0..3ca035ca8b20d460644698636a025b6b5e3e2e5d 100755 (executable)
@@ -260,11 +260,6 @@ generate_static_system() {
                                uci -q set "system.@system[-1].hostname=$hostname"
                        fi
 
-                       local label_macaddr
-                       if json_get_var label_macaddr label_macaddr; then
-                               uci -q set "system.@system[-1].label_macaddr=$label_macaddr"
-                       fi
-
                        if json_is_a ntpserver array; then
                                local keys key
                                json_get_keys keys ntpserver
index cb0508fe9cd84ba49dfd777c7ba7fc6820f7ad2f..be7efe4e2eed88ae5bc2b9db0ba8258e28acd8d9 100644 (file)
@@ -1,5 +1,7 @@
 # Copyright (C) 2006-2013 OpenWrt.org
 
+. /usr/share/libubox/jshn.sh
+
 get_mac_binary() {
        local path="$1"
        local offset="$2"
@@ -12,14 +14,41 @@ get_mac_binary() {
        hexdump -v -n 6 -s $offset -e '5/1 "%02x:" 1/1 "%02x"' $path 2>/dev/null
 }
 
-get_mac_label() {
+get_mac_label_dt() {
        local basepath="/proc/device-tree"
        local macdevice="$(cat "$basepath/aliases/label-mac-device" 2>/dev/null)"
        local macaddr
 
-       [ -n "$macdevice" ] && macaddr=$(get_mac_binary "$basepath/$macdevice/mac-address" 0 2>/dev/null)
+       [ -n "$macdevice" ] || return
+
+       macaddr=$(get_mac_binary "$basepath/$macdevice/mac-address" 0 2>/dev/null)
        [ -n "$macaddr" ] || macaddr=$(get_mac_binary "$basepath/$macdevice/local-mac-address" 0 2>/dev/null)
-       [ -n "$macaddr" ] || macaddr=$(uci -q get system.@system[0].label_macaddr)
+
+       echo $macaddr
+}
+
+get_mac_label_json() {
+       local cfg="/etc/board.json"
+       local macaddr
+
+       [ -s "$cfg" ] || return
+
+       json_init
+       json_load "$(cat $cfg)"
+       if json_is_a system object; then
+               json_select system
+                       json_get_var macaddr label_macaddr
+               json_select ..
+       fi
+
+       echo $macaddr
+}
+
+get_mac_label() {
+       local macaddr=$(get_mac_label_dt)
+
+       [ -n "$macaddr" ] || macaddr=$(get_mac_label_json)
+
        echo $macaddr
 }