ASR_BASE
Change-Id: Icf3719cc0afe3eeb3edc7fa80a2eb5199ca9dda1
diff --git a/package/base-files/files/lib/functions/caldata.sh b/package/base-files/files/lib/functions/caldata.sh
new file mode 100644
index 0000000..f0fc907
--- /dev/null
+++ b/package/base-files/files/lib/functions/caldata.sh
@@ -0,0 +1,218 @@
+# Copyright (C) 2019 OpenWrt.org
+
+. /lib/functions.sh
+. /lib/functions/system.sh
+
+caldata_dd() {
+ local source=$1
+ local target=$2
+ local count=$(($3))
+ local offset=$(($4))
+
+ dd if=$source of=$target iflag=skip_bytes,fullblock bs=$count skip=$offset count=1 2>/dev/null
+ return $?
+}
+
+caldata_die() {
+ echo "caldata: " "$*"
+ exit 1
+}
+
+caldata_extract() {
+ local part=$1
+ local offset=$(($2))
+ local count=$(($3))
+ local mtd
+
+ mtd=$(find_mtd_chardev $part)
+ [ -n "$mtd" ] || caldata_die "no mtd device found for partition $part"
+
+ caldata_dd $mtd /lib/firmware/$FIRMWARE $count $offset || \
+ caldata_die "failed to extract calibration data from $mtd"
+}
+
+caldata_extract_ubi() {
+ local part=$1
+ local offset=$(($2))
+ local count=$(($3))
+ local ubidev
+ local ubi
+
+ . /lib/upgrade/nand.sh
+
+ ubidev=$(nand_find_ubi $CI_UBIPART)
+ ubi=$(nand_find_volume $ubidev $part)
+ [ -n "$ubi" ] || caldata_die "no UBI volume found for $part"
+
+ caldata_dd /dev/$ubi /lib/firmware/$FIRMWARE $count $offset || \
+ caldata_die "failed to extract calibration data from $ubi"
+}
+
+caldata_extract_mmc() {
+ local part=$1
+ local offset=$(($2))
+ local count=$(($3))
+ local mmc_part
+
+ mmc_part=$(find_mmc_part $part)
+ [ -n "$mmc_part" ] || caldata_die "no mmc partition found for partition $part"
+
+ caldata_dd $mmc_part /lib/firmware/$FIRMWARE $count $offset || \
+ caldata_die "failed to extract calibration data from $mmc_part"
+}
+
+caldata_extract_reverse() {
+ local part=$1
+ local offset=$2
+ local count=$(($3))
+ local mtd
+ local reversed
+ local caldata
+
+ mtd=$(find_mtd_chardev "$part")
+ reversed=$(hexdump -v -s $offset -n $count -e '1/1 "%02x "' $mtd)
+
+ for byte in $reversed; do
+ caldata="\x${byte}${caldata}"
+ done
+
+ printf "%b" "$caldata" > /lib/firmware/$FIRMWARE
+}
+
+caldata_from_file() {
+ local source=$1
+ local offset=$(($2))
+ local count=$(($3))
+ local target=$4
+
+ [ -n "$target" ] || target=/lib/firmware/$FIRMWARE
+
+ caldata_dd $source $target $count $offset || \
+ caldata_die "failed to extract calibration data from $source"
+}
+
+caldata_sysfsload_from_file() {
+ local source=$1
+ local offset=$(($2))
+ local count=$(($3))
+ local target_dir="/sys/$DEVPATH"
+ local target="$target_dir/data"
+
+ [ -d "$target_dir" ] || \
+ caldata_die "no sysfs dir to write: $target"
+
+ echo 1 > "$target_dir/loading"
+ caldata_dd $source $target $count $offset
+ if [ $? != 0 ]; then
+ echo 1 > "$target_dir/loading"
+ caldata_die "failed to extract calibration data from $source"
+ else
+ echo 0 > "$target_dir/loading"
+ fi
+}
+
+caldata_valid() {
+ local expected="$1"
+ local target=$2
+
+ [ -n "$target" ] || target=/lib/firmware/$FIRMWARE
+
+ magic=$(hexdump -v -n 2 -e '1/1 "%02x"' $target)
+ [ "$magic" = "$expected" ]
+ return $?
+}
+
+caldata_patch_data() {
+ local data=$1
+ local data_count=$((${#1} / 2))
+ [ -n "$2" ] && local data_offset=$(($2))
+ [ -n "$3" ] && local chksum_offset=$(($3))
+ local target=$4
+ local fw_data
+ local fw_chksum
+
+ [ -z "$data" -o -z "$data_offset" ] && return
+
+ [ -n "$target" ] || target=/lib/firmware/$FIRMWARE
+
+ fw_data=$(hexdump -v -n $data_count -s $data_offset -e '1/1 "%02x"' $target)
+
+ if [ "$data" != "$fw_data" ]; then
+
+ if [ -n "$chksum_offset" ]; then
+ fw_chksum=$(hexdump -v -n 2 -s $chksum_offset -e '1/1 "%02x"' $target)
+ fw_chksum=$(xor $fw_chksum $(data_2xor_val $fw_data) $(data_2xor_val $data))
+
+ data_2bin $fw_chksum | \
+ dd of=$target conv=notrunc bs=1 seek=$chksum_offset count=2 || \
+ caldata_die "failed to write chksum to eeprom file"
+ fi
+
+ data_2bin $data | \
+ dd of=$target conv=notrunc bs=1 seek=$data_offset count=$data_count || \
+ caldata_die "failed to write data to eeprom file"
+ fi
+}
+
+ath9k_patch_mac() {
+ local mac=$1
+ local target=$2
+
+ caldata_patch_data "${mac//:/}" 0x2 "" "$target"
+}
+
+ath9k_patch_mac_crc() {
+ local mac=$1
+ local mac_offset=$2
+ local chksum_offset=$((mac_offset - 10))
+ local target=$4
+
+ caldata_patch_data "${mac//:/}" "$mac_offset" "$chksum_offset" "$target"
+}
+
+ath10k_patch_mac() {
+ local mac=$1
+ local target=$2
+
+ caldata_patch_data "${mac//:/}" 0x6 0x2 "$target"
+}
+
+ath11k_patch_mac() {
+ local mac=$1
+ # mac_id from 0 to 5
+ local mac_id=$2
+ local target=$3
+
+ [ -z "$mac_id" ] && return
+
+ caldata_patch_data "${mac//:/}" $(printf "0x%x" $(($mac_id * 0x6 + 0xe))) 0xa "$target"
+}
+
+ath10k_remove_regdomain() {
+ local target=$1
+
+ caldata_patch_data "0000" 0xc 0x2 "$target"
+}
+
+ath11k_remove_regdomain() {
+ local target=$1
+ local regdomain
+ local regdomain_data
+
+ regdomain=$(hexdump -v -n 2 -s 0x34 -e '1/1 "%02x"' $target)
+ caldata_patch_data "0000" 0x34 0xa "$target"
+
+ for offset in 0x450 0x458 0x500 0x5a8; do
+ regdomain_data=$(hexdump -v -n 2 -s $offset -e '1/1 "%02x"' $target)
+
+ if [ "$regdomain" == "$regdomain_data" ]; then
+ caldata_patch_data "0000" $offset 0xa "$target"
+ fi
+ done
+}
+
+ath11k_set_macflag() {
+ local target=$1
+
+ caldata_patch_data "0100" 0x3e 0xa "$target"
+}
diff --git a/package/base-files/files/lib/functions/ipv4.sh b/package/base-files/files/lib/functions/ipv4.sh
new file mode 100644
index 0000000..d0b93db
--- /dev/null
+++ b/package/base-files/files/lib/functions/ipv4.sh
@@ -0,0 +1,268 @@
+uint_max=4294967295
+
+d_10_0_0_0=167772160
+d_10_255_255_255=184549375
+
+d_172_16_0_0=2886729728
+d_172_31_255_255=2887778303
+
+d_192_168_0_0=3232235520
+d_192_168_255_255=3232301055
+
+d_169_254_0_0=2851995648
+d_169_254_255_255=2852061183
+
+d_127_0_0_0=2130706432
+d_127_255_255_255=2147483647
+
+d_224_0_0_0=3758096384
+d_239_255_255_255=4026531839
+
+# check that $1 is only base 10 digits, and that it doesn't
+# exceed 2^32-1
+assert_uint32() {
+ local __n="$1"
+
+ if [ -z "$__n" -o -n "${__n//[0-9]/}" ]; then
+ printf "Not a decimal integer (%s)\n" "$__n ">&2
+ return 1
+ fi
+
+ if [ "$__n" -gt $uint_max ]; then
+ printf "Out of range (%s)\n" "$__n" >&2
+ return 1
+ fi
+
+ if [ "$((__n + 0))" != "$__n" ]; then
+ printf "Not normalized notation (%s)\n" "$__n" >&2
+ return 1
+ fi
+
+ return 0
+}
+
+# return a count of the number of bits set in $1
+bitcount() {
+ local __var="$1" __c="$2"
+ assert_uint32 "$__c" || return 1
+
+ __c=$((((__c >> 1) & 0x55555555) + (__c & 0x55555555)))
+ __c=$((((__c >> 2) & 0x33333333) + (__c & 0x33333333)))
+ __c=$((((__c >> 4) & 0x0f0f0f0f) + (__c & 0x0f0f0f0f)))
+ __c=$((((__c >> 8) & 0x00ff00ff) + (__c & 0x00ff00ff)))
+ __c=$((((__c >> 16) & 0x0000ffff) + (__c & 0x0000ffff)))
+
+ export -- "$__var=$__c"
+}
+
+# tedious but portable with busybox's limited shell
+# we check each octet to be in the range of 0..255,
+# and also make sure there's no extaneous characters.
+str2ip() {
+ local __var="$1" __ip="$2" __n __val=0
+
+ case "$__ip" in
+ [0-9].*)
+ __n="${__ip:0:1}"
+ __ip="${__ip:2}"
+ ;;
+ [1-9][0-9].*)
+ __n="${__ip:0:2}"
+ __ip="${__ip:3}"
+ ;;
+ 1[0-9][0-9].*|2[0-4][0-9].*|25[0-5].*)
+ __n="${__ip:0:3}"
+ __ip="${__ip:4}"
+ ;;
+ *)
+ printf "Not a dotted quad (%s)\n" "$2" >&2
+ return 1
+ ;;
+ esac
+
+ __val=$((__n << 24))
+
+ case "$__ip" in
+ [0-9].*)
+ __n="${__ip:0:1}"
+ __ip="${__ip:2}"
+ ;;
+ [1-9][0-9].*)
+ __n="${__ip:0:2}"
+ __ip="${__ip:3}"
+ ;;
+ 1[0-9][0-9].*|2[0-4][0-9].*|25[0-5].*)
+ __n="${__ip:0:3}"
+ __ip="${__ip:4}"
+ ;;
+ *)
+ printf "Not a dotted quad (%s)\n" "$2" >&2
+ return 1
+ ;;
+ esac
+
+ __val=$((__val + (__n << 16)))
+
+ case "$__ip" in
+ [0-9].*)
+ __n="${__ip:0:1}"
+ __ip="${__ip:2}"
+ ;;
+ [1-9][0-9].*)
+ __n="${__ip:0:2}"
+ __ip="${__ip:3}"
+ ;;
+ 1[0-9][0-9].*|2[0-4][0-9].*|25[0-5].*)
+ __n="${__ip:0:3}"
+ __ip="${__ip:4}"
+ ;;
+ *)
+ printf "Not a dotted quad (%s)\n" "$2" >&2
+ return 1
+ ;;
+ esac
+
+ __val=$((__val + (__n << 8)))
+
+ case "$__ip" in
+ [0-9])
+ __n="${__ip:0:1}"
+ __ip="${__ip:1}"
+ ;;
+ [1-9][0-9])
+ __n="${__ip:0:2}"
+ __ip="${__ip:2}"
+ ;;
+ 1[0-9][0-9]|2[0-4][0-9]|25[0-5])
+ __n="${__ip:0:3}"
+ __ip="${__ip:3}"
+ ;;
+ *)
+ printf "Not a dotted quad (%s)\n" "$2" >&2
+ return 1
+ ;;
+ esac
+
+ __val=$((__val + __n))
+
+ if [ -n "$__ip" ]; then
+ printf "Not a dotted quad (%s)\n" "$2" >&2
+ return 1
+ fi
+
+ export -- "$__var=$__val"
+ return 0
+}
+
+# convert back from an integer to dotted-quad.
+ip2str() {
+ local __var="$1" __n="$2"
+ assert_uint32 "$__n" || return 1
+
+ export -- "$__var=$((__n >> 24)).$(((__n >> 16) & 255)).$(((__n >> 8) & 255)).$((__n & 255))"
+}
+
+# convert prefix into an integer bitmask
+prefix2netmask() {
+ local __var="$1" __n="$2"
+ assert_uint32 "$__n" || return 1
+
+ if [ "$__n" -gt 32 ]; then
+ printf "Prefix out-of-range (%s)" "$__n" >&2
+ return 1
+ fi
+
+ export -- "$__var=$(((~(uint_max >> __n)) & uint_max))"
+}
+
+_is_contiguous() {
+ local __x="$1" # no checking done
+ local __y=$((~__x & uint_max))
+ local __z=$(((__y + 1) & uint_max))
+
+ [ $((__z & __y)) -eq 0 ]
+}
+
+# check argument as being contiguous upper bits (and yes,
+# 0 doesn't have any discontiguous bits).
+is_contiguous() {
+ local __var="$1" __x="$2" __val=0
+ assert_uint32 "$__x" || return 1
+
+ local __y=$((~__x & uint_max))
+ local __z=$(((__y + 1) & uint_max))
+
+ [ $((__z & __y)) -eq 0 ] && __val=1
+
+ export -- "$__var=$__val"
+}
+
+# convert mask to prefix, validating that it's a conventional
+# (contiguous) netmask.
+netmask2prefix() {
+ local __var="$1" __n="$2" __cont __bits
+ assert_uint32 "$__n" || return 1
+
+ is_contiguous __cont "$__n" || return 1
+ if [ $__cont -eq 0 ]; then
+ printf "Not a contiguous netmask (%08x)\n" "$__n" >&2
+ return 1
+ fi
+
+ bitcount __bits "$__n" # already checked
+
+ export -- "$__var=$__bits"
+}
+
+# check the argument as being an rfc-1918 address
+is_rfc1918() {
+ local __var="$1" __x="$2" __val=0
+ assert_uint32 "$__x" || return 1
+
+ if [ $d_10_0_0_0 -le $__x ] && [ $__x -le $d_10_255_255_255 ]; then
+ __val=1
+ elif [ $d_172_16_0_0 -le $__x ] && [ $__x -le $d_172_31_255_255 ]; then
+ __val=1
+ elif [ $d_192_168_0_0 -le $__x ] && [ $__x -le $d_192_168_255_255 ]; then
+ __val=1
+ fi
+
+ export -- "$__var=$__val"
+}
+
+# check the argument as being an rfc-3927 address
+is_rfc3927() {
+ local __var="$1" __x="$2" __val=0
+ assert_uint32 "$__x" || return 1
+
+ if [ $d_169_254_0_0 -le $__x ] && [ $__x -le $d_169_254_255_255 ]; then
+ __val=1
+ fi
+
+ export -- "$__var=$__val"
+}
+
+# check the argument as being an rfc-1122 loopback address
+is_loopback() {
+ local __var="$1" __x="$2" __val=0
+ assert_uint32 "$__x" || return 1
+
+ if [ $d_127_0_0_0 -le $__x ] && [ $__x -le $d_127_255_255_255 ]; then
+ __val=1
+ fi
+
+ export -- "$__var=$__val"
+}
+
+# check the argument as being a multicast address
+is_multicast() {
+ local __var="$1" __x="$2" __val=0
+ assert_uint32 "$__x" || return 1
+
+ if [ $d_224_0_0_0 -le $__x ] && [ $__x -le $d_239_255_255_255 ]; then
+ __val=1
+ fi
+
+ export -- "$__var=$__val"
+}
+
diff --git a/package/base-files/files/lib/functions/leds.sh b/package/base-files/files/lib/functions/leds.sh
new file mode 100644
index 0000000..333d900
--- /dev/null
+++ b/package/base-files/files/lib/functions/leds.sh
@@ -0,0 +1,125 @@
+# Copyright (C) 2013 OpenWrt.org
+
+get_dt_led_path() {
+ local ledpath
+ local basepath="/proc/device-tree"
+ local nodepath="$basepath/aliases/led-$1"
+
+ [ -f "$nodepath" ] && ledpath=$(cat "$nodepath")
+ [ -n "$ledpath" ] && ledpath="$basepath$ledpath"
+
+ echo "$ledpath"
+}
+
+get_dt_led_color_func() {
+ local enum
+ local func
+ local idx
+ local label
+
+ [ -e "$1/function" ] && func=$(cat "$1/function")
+ [ -e "$1/color" ] && idx=$((0x$(hexdump -n 4 -e '4/1 "%02x"' "$1/color")))
+ [ -e "$1/function-enumerator" ] && \
+ enum=$((0x$(hexdump -n 4 -e '4/1 "%02x"' "$1/function-enumerator")))
+
+ [ -z "$idx" ] && [ -z "$func" ] && return 2
+
+ if [ -n "$idx" ]; then
+ for color in "white" "red" "green" "blue" "amber" \
+ "violet" "yellow" "ir" "multicolor" "rgb" \
+ "purple" "orange" "pink" "cyan" "lime"
+ do
+ [ $idx -eq 0 ] && label="$color" && break
+ idx=$((idx-1))
+ done
+ fi
+
+ label="$label:$func"
+ [ -n "$enum" ] && label="$label-$enum"
+ echo "$label"
+
+ return 0
+}
+
+get_dt_led() {
+ local label
+ local ledpath=$(get_dt_led_path $1)
+
+ [ -n "$ledpath" ] && \
+ label=$(cat "$ledpath/label" 2>/dev/null) || \
+ label=$(cat "$ledpath/chan-name" 2>/dev/null) || \
+ label=$(get_dt_led_color_func "$ledpath") || \
+ label=$(basename "$ledpath")
+
+ echo "$label"
+}
+
+led_set_attr() {
+ [ -f "/sys/class/leds/$1/$2" ] && echo "$3" > "/sys/class/leds/$1/$2"
+}
+
+led_timer() {
+ led_set_attr $1 "trigger" "timer"
+ led_set_attr $1 "delay_on" "$2"
+ led_set_attr $1 "delay_off" "$3"
+}
+
+led_on() {
+ led_set_attr $1 "trigger" "none"
+ led_set_attr $1 "brightness" 255
+}
+
+led_off() {
+ led_set_attr $1 "trigger" "none"
+ led_set_attr $1 "brightness" 0
+}
+
+status_led_restore_trigger() {
+ local trigger
+ local ledpath=$(get_dt_led_path $1)
+
+ [ -n "$ledpath" ] && \
+ trigger=$(cat "$ledpath/linux,default-trigger" 2>/dev/null)
+
+ [ -n "$trigger" ] && \
+ led_set_attr "$(get_dt_led $1)" "trigger" "$trigger"
+}
+
+status_led_set_timer() {
+ led_timer $status_led "$1" "$2"
+ [ -n "$status_led2" ] && led_timer $status_led2 "$1" "$2"
+}
+
+status_led_set_heartbeat() {
+ led_set_attr $status_led "trigger" "heartbeat"
+}
+
+status_led_on() {
+ led_on $status_led
+ [ -n "$status_led2" ] && led_on $status_led2
+}
+
+status_led_off() {
+ led_off $status_led
+ [ -n "$status_led2" ] && led_off $status_led2
+}
+
+status_led_blink_slow() {
+ led_timer $status_led 1000 1000
+}
+
+status_led_blink_fast() {
+ led_timer $status_led 100 100
+}
+
+status_led_blink_preinit() {
+ led_timer $status_led 100 100
+}
+
+status_led_blink_failsafe() {
+ led_timer $status_led 50 50
+}
+
+status_led_blink_preinit_regular() {
+ led_timer $status_led 200 200
+}
diff --git a/package/base-files/files/lib/functions/migrations.sh b/package/base-files/files/lib/functions/migrations.sh
new file mode 100644
index 0000000..d43ea35
--- /dev/null
+++ b/package/base-files/files/lib/functions/migrations.sh
@@ -0,0 +1,67 @@
+. /lib/functions.sh
+
+migrate_led_sysfs() {
+ local cfg="$1"; shift
+ local tuples="$@"
+ local sysfs
+ local name
+
+ config_get sysfs ${cfg} sysfs
+ config_get name ${cfg} name
+
+ [ -z "${sysfs}" ] && return
+
+ for tuple in ${tuples}; do
+ local old=${tuple%=*}
+ local new=${tuple#*=}
+ local new_sysfs
+
+ new_sysfs=$(echo ${sysfs} | sed "s/${old}/${new}/")
+
+ [ "${new_sysfs}" = "${sysfs}" ] && continue
+
+ uci set system.${cfg}.sysfs="${new_sysfs}"
+
+ logger -t led-migration "sysfs option of LED \"${name}\" updated to ${new_sysfs}"
+ done;
+}
+
+remove_devicename_led_sysfs() {
+ local cfg="$1"; shift
+ local exceptions="$@"
+ local sysfs
+ local name
+ local new_sysfs
+
+ config_get sysfs ${cfg} sysfs
+ config_get name ${cfg} name
+
+ # only continue if two or more colons are present
+ echo "${sysfs}" | grep -q ":.*:" || return
+
+ for exception in ${exceptions}; do
+ # no change if exceptions provided as argument are found for devicename
+ echo "${sysfs}" | grep -q "^${exception}:" && return
+ done
+
+ new_sysfs=$(echo ${sysfs} | sed "s/^[^:]*://")
+
+ uci set system.${cfg}.sysfs="${new_sysfs}"
+
+ logger -t led-migration "sysfs option of LED \"${name}\" updated to ${new_sysfs}"
+}
+
+migrate_leds() {
+ config_load system
+ config_foreach migrate_led_sysfs led "$@"
+}
+
+remove_devicename_leds() {
+ config_load system
+ config_foreach remove_devicename_led_sysfs led "$@"
+}
+
+migrations_apply() {
+ local realm="$1"
+ [ -n "$(uci changes ${realm})" ] && uci -q commit ${realm}
+}
diff --git a/package/base-files/files/lib/functions/network.sh b/package/base-files/files/lib/functions/network.sh
new file mode 100644
index 0000000..4851a58
--- /dev/null
+++ b/package/base-files/files/lib/functions/network.sh
@@ -0,0 +1,325 @@
+# 1: destination variable
+# 2: interface
+# 3: path
+# 4: separator
+# 5: limit
+__network_ifstatus() {
+ local __tmp
+
+ [ -z "$__NETWORK_CACHE" ] && {
+ __tmp="$(ubus call network.interface dump 2>&1)"
+ case "$?" in
+ 4) : ;;
+ 0) export __NETWORK_CACHE="$__tmp" ;;
+ *) echo "$__tmp" >&2 ;;
+ esac
+ }
+
+ __tmp="$(jsonfilter ${4:+-F "$4"} ${5:+-l "$5"} -s "${__NETWORK_CACHE:-{}}" -e "$1=@.interface${2:+[@.interface='$2']}$3")"
+
+ [ -z "$__tmp" ] && \
+ unset "$1" && \
+ return 1
+
+ eval "$__tmp"
+}
+
+# determine first IPv4 address of given logical interface
+# 1: destination variable
+# 2: interface
+network_get_ipaddr() {
+ __network_ifstatus "$1" "$2" "['ipv4-address'][0].address";
+}
+
+# determine first IPv6 address of given logical interface
+# 1: destination variable
+# 2: interface
+network_get_ipaddr6() {
+ __network_ifstatus "$1" "$2" "['ipv6-address'][0].address" || \
+ __network_ifstatus "$1" "$2" "['ipv6-prefix-assignment'][0]['local-address'].address" || \
+ return 1
+}
+
+# determine first IPv4 subnet of given logical interface
+# 1: destination variable
+# 2: interface
+network_get_subnet() {
+ __network_ifstatus "$1" "$2" "['ipv4-address'][0]['address','mask']" "/"
+}
+
+# determine first IPv6 subnet of given logical interface
+# 1: destination variable
+# 2: interface
+network_get_subnet6() {
+ local __nets __addr
+
+ if network_get_subnets6 __nets "$2"; then
+ # Attempt to return first non-fe80::/10, non-fc::/7 range
+ for __addr in $__nets; do
+ case "$__addr" in fe[8ab]?:*|f[cd]??:*)
+ continue
+ esac
+ export "$1=$__addr"
+ return 0
+ done
+
+ # Attempt to return first non-fe80::/10 range
+ for __addr in $__nets; do
+ case "$__addr" in fe[8ab]?:*)
+ continue
+ esac
+ export "$1=$__addr"
+ return 0
+ done
+
+ # Return first item
+ for __addr in $__nets; do
+ export "$1=$__addr"
+ return 0
+ done
+ fi
+
+ unset "$1"
+ return 1
+}
+
+# determine first IPv6 prefix of given logical interface
+# 1: destination variable
+# 2: interface
+network_get_prefix6() {
+ __network_ifstatus "$1" "$2" "['ipv6-prefix'][0]['address','mask']" "/"
+}
+
+# determine first IPv6 prefix assignment of given logical interface
+# 1: destination variable
+# 2: interface
+network_get_prefix_assignment6() {
+ __network_ifstatus "$1" "$2" "['ipv6-prefix-assignment'][0]['address','mask']" "/"
+}
+
+# determine all IPv4 addresses of given logical interface
+# 1: destination variable
+# 2: interface
+network_get_ipaddrs() {
+ __network_ifstatus "$1" "$2" "['ipv4-address'][*].address"
+}
+
+# determine all IPv6 addresses of given logical interface
+# 1: destination variable
+# 2: interface
+network_get_ipaddrs6() {
+ local __addr
+ local __list=""
+
+ if __network_ifstatus "__addr" "$2" "['ipv6-address'][*].address"; then
+ for __addr in $__addr; do
+ __list="${__list:+$__list }${__addr}"
+ done
+ fi
+
+ if __network_ifstatus "__addr" "$2" "['ipv6-prefix-assignment'][*]['local-address'].address"; then
+ for __addr in $__addr; do
+ __list="${__list:+$__list }${__addr}"
+ done
+ fi
+
+ if [ -n "$__list" ]; then
+ export "$1=$__list"
+ return 0
+ fi
+
+ unset "$1"
+ return 1
+}
+
+# determine all IP addresses of given logical interface
+# 1: destination variable
+# 2: interface
+network_get_ipaddrs_all() {
+ local __addr __addr6
+
+ network_get_ipaddrs __addr "$2"
+ network_get_ipaddrs6 __addr6 "$2"
+
+ if [ -n "$__addr" -o -n "$__addr6" ]; then
+ export "$1=${__addr:+$__addr }$__addr6"
+ return 0
+ fi
+
+ unset "$1"
+ return 1
+}
+
+# determine all IPv4 subnets of given logical interface
+# 1: destination variable
+# 2: interface
+network_get_subnets() {
+ __network_ifstatus "$1" "$2" "['ipv4-address'][*]['address','mask']" "/ "
+}
+
+# determine all IPv6 subnets of given logical interface
+# 1: destination variable
+# 2: interface
+network_get_subnets6() {
+ local __addr __mask
+ local __list=""
+
+ if __network_ifstatus "__addr" "$2" "['ipv6-address'][*]['address','mask']" "/ "; then
+ for __addr in $__addr; do
+ __list="${__list:+$__list }${__addr}"
+ done
+ fi
+
+ if __network_ifstatus "__addr" "$2" "['ipv6-prefix-assignment'][*]['local-address'].address" && \
+ __network_ifstatus "__mask" "$2" "['ipv6-prefix-assignment'][*].mask"; then
+ for __addr in $__addr; do
+ __list="${__list:+$__list }${__addr}/${__mask%% *}"
+ __mask="${__mask#* }"
+ done
+ fi
+
+ if [ -n "$__list" ]; then
+ export "$1=$__list"
+ return 0
+ fi
+
+ unset "$1"
+ return 1
+}
+
+# determine all IPv6 prefixes of given logical interface
+# 1: destination variable
+# 2: interface
+network_get_prefixes6() {
+ __network_ifstatus "$1" "$2" "['ipv6-prefix'][*]['address','mask']" "/ "
+}
+
+# determine all IPv6 prefix assignments of given logical interface
+# 1: destination variable
+# 2: interface
+network_get_prefix_assignments6() {
+ __network_ifstatus "$1" "$2" "['ipv6-prefix-assignment'][*]['address','mask']" "/ "
+}
+
+# determine IPv4 gateway of given logical interface
+# 1: destination variable
+# 2: interface
+# 3: consider inactive gateway if "true" (optional)
+network_get_gateway() {
+ __network_ifstatus "$1" "$2" ".route[@.target='0.0.0.0' && !@.table].nexthop" "" 1 && \
+ return 0
+
+ [ "$3" = 1 -o "$3" = "true" ] && \
+ __network_ifstatus "$1" "$2" ".inactive.route[@.target='0.0.0.0' && !@.table].nexthop" "" 1
+}
+
+# determine IPv6 gateway of given logical interface
+# 1: destination variable
+# 2: interface
+# 3: consider inactive gateway if "true" (optional)
+network_get_gateway6() {
+ __network_ifstatus "$1" "$2" ".route[@.target='::' && !@.table].nexthop" "" 1 && \
+ return 0
+
+ [ "$3" = 1 -o "$3" = "true" ] && \
+ __network_ifstatus "$1" "$2" ".inactive.route[@.target='::' && !@.table].nexthop" "" 1
+}
+
+# determine the DNS servers of the given logical interface
+# 1: destination variable
+# 2: interface
+# 3: consider inactive servers if "true" (optional)
+network_get_dnsserver() {
+ __network_ifstatus "$1" "$2" "['dns-server'][*]" && return 0
+
+ [ "$3" = 1 -o "$3" = "true" ] && \
+ __network_ifstatus "$1" "$2" ".inactive['dns-server'][*]"
+}
+
+# determine the domains of the given logical interface
+# 1: destination variable
+# 2: interface
+# 3: consider inactive domains if "true" (optional)
+network_get_dnssearch() {
+ __network_ifstatus "$1" "$2" "['dns-search'][*]" && return 0
+
+ [ "$3" = 1 -o "$3" = "true" ] && \
+ __network_ifstatus "$1" "$2" ".inactive['dns-search'][*]"
+}
+
+
+# 1: destination variable
+# 2: addr
+# 3: inactive
+__network_wan()
+{
+ __network_ifstatus "$1" "" \
+ "[@.route[@.target='$2' && !@.table]].interface" "" 1 && \
+ return 0
+
+ [ "$3" = 1 -o "$3" = "true" ] && \
+ __network_ifstatus "$1" "" \
+ "[@.inactive.route[@.target='$2' && !@.table]].interface" "" 1
+}
+
+# find the logical interface which holds the current IPv4 default route
+# 1: destination variable
+# 2: consider inactive default routes if "true" (optional)
+network_find_wan() { __network_wan "$1" "0.0.0.0" "$2"; }
+
+# find the logical interface which holds the current IPv6 default route
+# 1: destination variable
+# 2: consider inactive default routes if "true" (optional)
+network_find_wan6() { __network_wan "$1" "::" "$2"; }
+
+# test whether the given logical interface is running
+# 1: interface
+network_is_up()
+{
+ local __up
+ __network_ifstatus "__up" "$1" ".up" && [ "$__up" = 1 ]
+}
+
+# determine the protocol of the given logical interface
+# 1: destination variable
+# 2: interface
+network_get_protocol() { __network_ifstatus "$1" "$2" ".proto"; }
+
+# determine the uptime of the given logical interface
+# 1: destination variable
+# 2: interface
+network_get_uptime() { __network_ifstatus "$1" "$2" ".uptime"; }
+
+# determine the metric of the given logical interface
+# 1: destination variable
+# 2: interface
+network_get_metric() { __network_ifstatus "$1" "$2" ".metric"; }
+
+# determine the layer 3 linux network device of the given logical interface
+# 1: destination variable
+# 2: interface
+network_get_device() { __network_ifstatus "$1" "$2" ".l3_device"; }
+
+# determine the layer 2 linux network device of the given logical interface
+# 1: destination variable
+# 2: interface
+network_get_physdev() { __network_ifstatus "$1" "$2" ".device"; }
+
+# defer netifd actions on the given linux network device
+# 1: device name
+network_defer_device()
+{
+ ubus call network.device set_state \
+ "$(printf '{ "name": "%s", "defer": true }' "$1")" 2>/dev/null
+}
+
+# continue netifd actions on the given linux network device
+# 1: device name
+network_ready_device()
+{
+ ubus call network.device set_state \
+ "$(printf '{ "name": "%s", "defer": false }' "$1")" 2>/dev/null
+}
+
+# flush the internal value cache to force re-reading values from ubus
+network_flush_cache() { unset __NETWORK_CACHE; }
diff --git a/package/base-files/files/lib/functions/preinit.sh b/package/base-files/files/lib/functions/preinit.sh
new file mode 100644
index 0000000..591e810
--- /dev/null
+++ b/package/base-files/files/lib/functions/preinit.sh
@@ -0,0 +1,87 @@
+# Copyright (C) 2006-2013 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+boot_hook_splice_start() {
+ export -n PI_HOOK_SPLICE=1
+}
+
+boot_hook_splice_finish() {
+ local hook
+ for hook in $PI_STACK_LIST; do
+ local v; eval "v=\${${hook}_splice:+\$${hook}_splice }$hook"
+ export -n "${hook}=${v% }"
+ export -n "${hook}_splice="
+ done
+ export -n PI_HOOK_SPLICE=
+}
+
+boot_hook_init() {
+ local hook="${1}_hook"
+ export -n "PI_STACK_LIST=${PI_STACK_LIST:+$PI_STACK_LIST }$hook"
+ export -n "$hook="
+}
+
+boot_hook_add() {
+ local hook="${1}_hook${PI_HOOK_SPLICE:+_splice}"
+ local func="${2}"
+
+ [ -n "$func" ] && {
+ local v; eval "v=\$$hook"
+ export -n "$hook=${v:+$v }$func"
+ }
+}
+
+boot_hook_shift() {
+ local hook="${1}_hook"
+ local rvar="${2}"
+
+ local v; eval "v=\$$hook"
+ [ -n "$v" ] && {
+ local first="${v%% *}"
+
+ [ "$v" != "${v#* }" ] && \
+ export -n "$hook=${v#* }" || \
+ export -n "$hook="
+
+ export -n "$rvar=$first"
+ return 0
+ }
+
+ return 1
+}
+
+boot_run_hook() {
+ local hook="$1"
+ local func
+
+ while boot_hook_shift "$hook" func; do
+ local ran; eval "ran=\$PI_RAN_$func"
+ [ -n "$ran" ] || {
+ export -n "PI_RAN_$func=1"
+ $func "$1" "$2"
+ }
+ done
+}
+
+pivot() { # <new_root> <old_root>
+ /bin/mount -o noatime,move /proc $1/proc && \
+ pivot_root $1 $1$2 && {
+ /bin/mount -o noatime,move $2/dev /dev
+ /bin/mount -o noatime,move $2/tmp /tmp
+ /bin/mount -o noatime,move $2/sys /sys 2>&-
+ /bin/mount -o noatime,move $2/overlay /overlay 2>&-
+ return 0
+ }
+}
+
+fopivot() { # <rw_root> <work_dir> <ro_root> <dupe?>
+ /bin/mount -o noatime,lowerdir=/,upperdir=$1,workdir=$2 -t overlay "overlayfs:$1" /mnt
+ pivot /mnt $3
+}
+
+ramoverlay() {
+ mkdir -p /tmp/root
+ /bin/mount -t tmpfs -o noatime,mode=0755 root /tmp/root
+ mkdir -p /tmp/root/root /tmp/root/work
+ fopivot /tmp/root/root /tmp/root/work /rom 1
+}
diff --git a/package/base-files/files/lib/functions/service.sh b/package/base-files/files/lib/functions/service.sh
new file mode 100644
index 0000000..3d08e14
--- /dev/null
+++ b/package/base-files/files/lib/functions/service.sh
@@ -0,0 +1,103 @@
+#
+# service: simple wrapper around start-stop-daemon
+#
+# Usage: service ACTION EXEC ARGS...
+#
+# Action:
+# -C check if EXEC is alive
+# -S start EXEC, passing it ARGS as its arguments
+# -K kill EXEC, sending it a TERM signal if not specified otherwise
+#
+# Environment variables exposed:
+# SERVICE_DAEMONIZE run EXEC in background
+# SERVICE_WRITE_PID create a pid-file and use it for matching
+# SERVICE_MATCH_EXEC use EXEC command-line for matching (default)
+# SERVICE_MATCH_NAME use EXEC process name for matching
+# SERVICE_USE_PID assume EXEC create its own pid-file and use it for matching
+# SERVICE_NAME process name to use (default to EXEC file part)
+# SERVICE_PID_FILE pid file to use (default to /var/run/$SERVICE_NAME.pid)
+# SERVICE_SIG signal to send when using -K
+# SERVICE_SIG_RELOAD default signal used when reloading
+# SERVICE_SIG_STOP default signal used when stopping
+# SERVICE_STOP_TIME time to wait for a process to stop gracefully before killing it
+# SERVICE_UID user EXEC should be run as
+# SERVICE_GID group EXEC should be run as
+#
+# SERVICE_DEBUG don't do anything, but show what would be done
+# SERVICE_QUIET don't print anything
+#
+
+SERVICE_QUIET=1
+SERVICE_SIG_RELOAD="HUP"
+SERVICE_SIG_STOP="TERM"
+SERVICE_STOP_TIME=5
+SERVICE_MATCH_EXEC=1
+
+service() {
+ local ssd
+ local exec
+ local name
+ local start
+ ssd="${SERVICE_DEBUG:+echo }start-stop-daemon${SERVICE_QUIET:+ -q}"
+ case "$1" in
+ -C)
+ ssd="$ssd -K -t"
+ ;;
+ -S)
+ ssd="$ssd -S${SERVICE_DAEMONIZE:+ -b}${SERVICE_WRITE_PID:+ -m}"
+ start=1
+ ;;
+ -K)
+ ssd="$ssd -K${SERVICE_SIG:+ -s $SERVICE_SIG}"
+ ;;
+ *)
+ echo "service: unknown ACTION '$1'" 1>&2
+ return 1
+ esac
+ shift
+ exec="$1"
+ [ -n "$exec" ] || {
+ echo "service: missing argument" 1>&2
+ return 1
+ }
+ [ -x "$exec" ] || {
+ echo "service: file '$exec' is not executable" 1>&2
+ return 1
+ }
+ name="${SERVICE_NAME:-${exec##*/}}"
+ [ -z "$SERVICE_USE_PID$SERVICE_WRITE_PID$SERVICE_PID_FILE" ] \
+ || ssd="$ssd -p ${SERVICE_PID_FILE:-/var/run/$name.pid}"
+ [ -z "$SERVICE_MATCH_NAME" ] || ssd="$ssd -n $name"
+ ssd="$ssd${SERVICE_UID:+ -c $SERVICE_UID${SERVICE_GID:+:$SERVICE_GID}}"
+ [ -z "$SERVICE_MATCH_EXEC$start" ] || ssd="$ssd -x $exec"
+ shift
+ $ssd${1:+ -- "$@"}
+}
+
+service_check() {
+ service -C "$@"
+}
+
+service_signal() {
+ SERVICE_SIG="${SERVICE_SIG:-USR1}" service -K "$@"
+}
+
+service_start() {
+ service -S "$@"
+}
+
+service_stop() {
+ local try
+ SERVICE_SIG="${SERVICE_SIG:-$SERVICE_SIG_STOP}" service -K "$@" || return 1
+ while [ $((try++)) -lt $SERVICE_STOP_TIME ]; do
+ service -C "$@" || return 0
+ sleep 1
+ done
+ SERVICE_SIG="KILL" service -K "$@"
+ sleep 1
+ ! service -C "$@"
+}
+
+service_reload() {
+ SERVICE_SIG="${SERVICE_SIG:-$SERVICE_SIG_RELOAD}" service -K "$@"
+}
diff --git a/package/base-files/files/lib/functions/system.sh b/package/base-files/files/lib/functions/system.sh
new file mode 100644
index 0000000..f43281b
--- /dev/null
+++ b/package/base-files/files/lib/functions/system.sh
@@ -0,0 +1,317 @@
+# Copyright (C) 2006-2013 OpenWrt.org
+
+. /lib/functions.sh
+. /usr/share/libubox/jshn.sh
+
+get_mac_binary() {
+ local path="$1"
+ local offset="$2"
+
+ if ! [ -e "$path" ]; then
+ echo "get_mac_binary: file $path not found!" >&2
+ return
+ fi
+
+ hexdump -v -n 6 -s $offset -e '5/1 "%02x:" 1/1 "%02x"' $path 2>/dev/null
+}
+
+get_mac_label_dt() {
+ local basepath="/proc/device-tree"
+ local macdevice="$(cat "$basepath/aliases/label-mac-device" 2>/dev/null)"
+ local macaddr
+
+ [ -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)
+
+ 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
+}
+
+find_mtd_chardev() {
+ local INDEX=$(find_mtd_index "$1")
+ local PREFIX=/dev/mtd
+
+ [ -d /dev/mtd ] && PREFIX=/dev/mtd/
+ echo "${INDEX:+$PREFIX$INDEX}"
+}
+
+get_mac_ascii() {
+ local part="$1"
+ local key="$2"
+ local mac_dirty
+
+ mac_dirty=$(strings "$part" | tr -d ' \t' | sed -n 's/^'"$key"'=//p' | head -n 1)
+
+ # "canonicalize" mac
+ [ -n "$mac_dirty" ] && macaddr_canonicalize "$mac_dirty"
+}
+
+mtd_get_mac_ascii() {
+ local mtdname="$1"
+ local key="$2"
+ local part
+
+ part=$(find_mtd_part "$mtdname")
+ if [ -z "$part" ]; then
+ echo "mtd_get_mac_ascii: partition $mtdname not found!" >&2
+ return
+ fi
+
+ get_mac_ascii "$part" "$key"
+}
+
+mtd_get_mac_encrypted_arcadyan() {
+ local iv="00000000000000000000000000000000"
+ local key="2A4B303D7644395C3B2B7053553C5200"
+ local mac_dirty
+ local mtdname="$1"
+ local part
+ local size
+
+ part=$(find_mtd_part "$mtdname")
+ if [ -z "$part" ]; then
+ echo "mtd_get_mac_encrypted_arcadyan: partition $mtdname not found!" >&2
+ return
+ fi
+
+ # Config decryption and getting mac. Trying uencrypt and openssl utils.
+ size=$((0x$(dd if=$part skip=9 bs=1 count=4 2>/dev/null | hexdump -v -e '1/4 "%08x"')))
+ if [[ -f "/usr/bin/uencrypt" ]]; then
+ mac_dirty=$(dd if=$part bs=1 count=$size skip=$((0x100)) 2>/dev/null | \
+ uencrypt -d -n -k $key -i $iv | grep mac | cut -c 5-)
+ elif [[ -f "/usr/bin/openssl" ]]; then
+ mac_dirty=$(dd if=$part bs=1 count=$size skip=$((0x100)) 2>/dev/null | \
+ openssl aes-128-cbc -d -nopad -K $key -iv $iv | grep mac | cut -c 5-)
+ else
+ echo "mtd_get_mac_encrypted_arcadyan: Neither uencrypt nor openssl was found!" >&2
+ return
+ fi
+
+ # "canonicalize" mac
+ [ -n "$mac_dirty" ] && macaddr_canonicalize "$mac_dirty"
+}
+
+mtd_get_mac_encrypted_deco() {
+ local mtdname="$1"
+
+ if ! [ -e "$mtdname" ]; then
+ echo "mtd_get_mac_encrypted_deco: file $mtdname not found!" >&2
+ return
+ fi
+
+ tplink_key="3336303032384339"
+
+ key=$(dd if=$mtdname bs=1 skip=16 count=8 2>/dev/null | \
+ uencrypt -n -d -k $tplink_key -c des-ecb | hexdump -v -n 8 -e '1/1 "%02x"')
+
+ macaddr=$(dd if=$mtdname bs=1 skip=32 count=8 2>/dev/null | \
+ uencrypt -n -d -k $key -c des-ecb | hexdump -v -n 6 -e '5/1 "%02x:" 1/1 "%02x"')
+
+ echo $macaddr
+}
+
+mtd_get_mac_uci_config_ubi() {
+ local volumename="$1"
+
+ . /lib/upgrade/nand.sh
+
+ local ubidev=$(nand_attach_ubi $CI_UBIPART)
+ local part=$(nand_find_volume $ubidev $volumename)
+
+ cat "/dev/$part" | sed -n 's/^\s*option macaddr\s*'"'"'\?\([0-9A-F:]\+\)'"'"'\?/\1/Ip'
+}
+
+mtd_get_mac_text() {
+ local mtdname="$1"
+ local offset=$((${2:-0}))
+ local length="${3:-17}"
+ local part
+
+ part=$(find_mtd_part "$mtdname")
+ if [ -z "$part" ]; then
+ echo "mtd_get_mac_text: partition $mtdname not found!" >&2
+ return
+ fi
+
+ [ $((offset + length)) -le $(mtd_get_part_size "$mtdname") ] || return
+
+ macaddr_canonicalize $(dd bs=1 if="$part" skip="$offset" count="$length" 2>/dev/null)
+}
+
+mtd_get_mac_binary() {
+ local mtdname="$1"
+ local offset="$2"
+ local part
+
+ part=$(find_mtd_part "$mtdname")
+ get_mac_binary "$part" "$offset"
+}
+
+mtd_get_mac_binary_ubi() {
+ local mtdname="$1"
+ local offset="$2"
+
+ . /lib/upgrade/nand.sh
+
+ local ubidev=$(nand_find_ubi $CI_UBIPART)
+ local part=$(nand_find_volume $ubidev $1)
+
+ get_mac_binary "/dev/$part" "$offset"
+}
+
+mtd_get_part_size() {
+ local part_name=$1
+ local first dev size erasesize name
+ while read dev size erasesize name; do
+ name=${name#'"'}; name=${name%'"'}
+ if [ "$name" = "$part_name" ]; then
+ echo $((0x$size))
+ break
+ fi
+ done < /proc/mtd
+}
+
+mmc_get_mac_ascii() {
+ local part_name="$1"
+ local key="$2"
+ local part
+
+ part=$(find_mmc_part "$part_name")
+ if [ -z "$part" ]; then
+ echo "mmc_get_mac_ascii: partition $part_name not found!" >&2
+ return
+ fi
+
+ get_mac_ascii "$part" "$key"
+}
+
+mmc_get_mac_binary() {
+ local part_name="$1"
+ local offset="$2"
+ local part
+
+ part=$(find_mmc_part "$part_name")
+ get_mac_binary "$part" "$offset"
+}
+
+macaddr_add() {
+ local mac=$1
+ local val=$2
+ local oui=${mac%:*:*:*}
+ local nic=${mac#*:*:*:}
+
+ nic=$(printf "%06x" $((0x${nic//:/} + val & 0xffffff)) | sed 's/^\(.\{2\}\)\(.\{2\}\)\(.\{2\}\)/\1:\2:\3/')
+ echo $oui:$nic
+}
+
+macaddr_generate_from_mmc_cid() {
+ local mmc_dev=$1
+
+ local sd_hash=$(sha256sum /sys/class/block/$mmc_dev/device/cid)
+ local mac_base=$(macaddr_canonicalize "$(echo "${sd_hash}" | dd bs=1 count=12 2>/dev/null)")
+ echo "$(macaddr_unsetbit_mc "$(macaddr_setbit_la "${mac_base}")")"
+}
+
+macaddr_geteui() {
+ local mac=$1
+ local sep=$2
+
+ echo ${mac:9:2}$sep${mac:12:2}$sep${mac:15:2}
+}
+
+macaddr_setbit() {
+ local mac=$1
+ local bit=${2:-0}
+
+ [ $bit -gt 0 -a $bit -le 48 ] || return
+
+ printf "%012x" $(( 0x${mac//:/} | 2**(48-bit) )) | sed -e 's/\(.\{2\}\)/\1:/g' -e 's/:$//'
+}
+
+macaddr_unsetbit() {
+ local mac=$1
+ local bit=${2:-0}
+
+ [ $bit -gt 0 -a $bit -le 48 ] || return
+
+ printf "%012x" $(( 0x${mac//:/} & ~(2**(48-bit)) )) | sed -e 's/\(.\{2\}\)/\1:/g' -e 's/:$//'
+}
+
+macaddr_setbit_la() {
+ macaddr_setbit $1 7
+}
+
+macaddr_unsetbit_mc() {
+ local mac=$1
+
+ printf "%02x:%s" $((0x${mac%%:*} & ~0x01)) ${mac#*:}
+}
+
+macaddr_random() {
+ local randsrc=$(get_mac_binary /dev/urandom 0)
+
+ echo "$(macaddr_unsetbit_mc "$(macaddr_setbit_la "${randsrc}")")"
+}
+
+macaddr_canonicalize() {
+ local mac="$1"
+ local canon=""
+
+ mac=$(echo -n $mac | tr -d \")
+ [ ${#mac} -gt 17 ] && return
+ [ -n "${mac//[a-fA-F0-9\.: -]/}" ] && return
+
+ for octet in ${mac//[\.:-]/ }; do
+ case "${#octet}" in
+ 1)
+ octet="0${octet}"
+ ;;
+ 2)
+ ;;
+ 4)
+ octet="${octet:0:2} ${octet:2:2}"
+ ;;
+ 12)
+ octet="${octet:0:2} ${octet:2:2} ${octet:4:2} ${octet:6:2} ${octet:8:2} ${octet:10:2}"
+ ;;
+ *)
+ return
+ ;;
+ esac
+ canon=${canon}${canon:+ }${octet}
+ done
+
+ [ ${#canon} -ne 17 ] && return
+
+ printf "%02x:%02x:%02x:%02x:%02x:%02x" 0x${canon// / 0x} 2>/dev/null
+}
+
+dt_is_enabled() {
+ grep -q okay "/proc/device-tree/$1/status"
+}
diff --git a/package/base-files/files/lib/functions/uci-defaults.sh b/package/base-files/files/lib/functions/uci-defaults.sh
new file mode 100644
index 0000000..5293ce1
--- /dev/null
+++ b/package/base-files/files/lib/functions/uci-defaults.sh
@@ -0,0 +1,791 @@
+. /lib/functions.sh
+. /usr/share/libubox/jshn.sh
+
+json_select_array() {
+ local _json_no_warning=1
+
+ json_select "$1"
+ [ $? = 0 ] && return
+
+ json_add_array "$1"
+ json_close_array
+
+ json_select "$1"
+}
+
+json_select_object() {
+ local _json_no_warning=1
+
+ json_select "$1"
+ [ $? = 0 ] && return
+
+ json_add_object "$1"
+ json_close_object
+
+ json_select "$1"
+}
+
+ucidef_set_interface() {
+ local network=$1; shift
+
+ [ -z "$network" ] && return
+
+ json_select_object network
+ json_select_object "$network"
+
+ while [ -n "$1" ]; do
+ local opt=$1; shift
+ local val=$1; shift
+
+ [ -n "$opt" -a -n "$val" ] || break
+
+ [ "$opt" = "device" -a "$val" != "${val/ //}" ] && {
+ json_select_array "ports"
+ for e in $val; do json_add_string "" "$e"; done
+ json_close_array
+ } || {
+ json_add_string "$opt" "$val"
+ }
+ done
+
+ if ! json_is_a protocol string; then
+ case "$network" in
+ lan) json_add_string protocol static ;;
+ wan) json_add_string protocol dhcp ;;
+ *) json_add_string protocol none ;;
+ esac
+ fi
+
+ json_select ..
+ json_select ..
+}
+
+ucidef_set_board_id() {
+ json_select_object model
+ json_add_string id "$1"
+ json_select ..
+}
+
+ucidef_set_model_name() {
+ json_select_object model
+ json_add_string name "$1"
+ json_select ..
+}
+
+ucidef_set_compat_version() {
+ json_select_object system
+ json_add_string compat_version "${1:-1.0}"
+ json_select ..
+}
+
+ucidef_set_interface_lan() {
+ ucidef_set_interface "lan" device "$1" protocol "${2:-static}"
+}
+
+ucidef_set_interface_wan() {
+ ucidef_set_interface "wan" device "$1" protocol "${2:-dhcp}"
+}
+
+ucidef_set_interfaces_lan_wan() {
+ local lan_if="$1"
+ local wan_if="$2"
+
+ ucidef_set_interface_lan "$lan_if"
+ ucidef_set_interface_wan "$wan_if"
+}
+
+ucidef_set_bridge_device() {
+ json_select_object bridge
+ json_add_string name "${1:-switch0}"
+ json_select ..
+}
+
+ucidef_set_bridge_mac() {
+ json_select_object bridge
+ json_add_string macaddr "${1}"
+ json_select ..
+}
+
+_ucidef_set_network_device_common() {
+ json_select_object "network_device"
+ json_select_object "${1}"
+ json_add_string "${2}" "${3}"
+ json_select ..
+ json_select ..
+}
+
+ucidef_set_network_device_mac() {
+ _ucidef_set_network_device_common $1 macaddr $2
+}
+
+ucidef_set_network_device_path() {
+ _ucidef_set_network_device_common $1 path $2
+}
+
+ucidef_set_network_device_path_port() {
+ _ucidef_set_network_device_common $1 path $2
+ _ucidef_set_network_device_common $1 port $3
+}
+
+ucidef_set_network_device_gro() {
+ _ucidef_set_network_device_common $1 gro $2
+}
+
+ucidef_set_network_device_conduit() {
+ _ucidef_set_network_device_common $1 conduit $2
+}
+
+_ucidef_add_switch_port() {
+ # inherited: $num $device $need_tag $want_untag $role $index $prev_role
+ # inherited: $n_cpu $n_ports $n_vlan $cpu0 $cpu1 $cpu2 $cpu3 $cpu4 $cpu5
+
+ n_ports=$((n_ports + 1))
+
+ json_select_array ports
+ json_add_object
+ json_add_int num "$num"
+ [ -n "$device" ] && json_add_string device "$device"
+ [ -n "$need_tag" ] && json_add_boolean need_tag "$need_tag"
+ [ -n "$want_untag" ] && json_add_boolean want_untag "$want_untag"
+ [ -n "$role" ] && json_add_string role "$role"
+ [ -n "$index" ] && json_add_int index "$index"
+ json_close_object
+ json_select ..
+
+ # record pointer to cpu entry for lookup in _ucidef_finish_switch_roles()
+ [ -n "$device" ] && {
+ export "cpu$n_cpu=$n_ports"
+ n_cpu=$((n_cpu + 1))
+ }
+
+ # create/append object to role list
+ [ -n "$role" ] && {
+ json_select_array roles
+
+ if [ "$role" != "$prev_role" ]; then
+ json_add_object
+ json_add_string role "$role"
+ json_add_string ports "$num"
+ json_close_object
+
+ prev_role="$role"
+ n_vlan=$((n_vlan + 1))
+ else
+ json_select_object "$n_vlan"
+ json_get_var port ports
+ json_add_string ports "$port $num"
+ json_select ..
+ fi
+
+ json_select ..
+ }
+}
+
+_ucidef_finish_switch_roles() {
+ # inherited: $name $n_cpu $n_vlan $cpu0 $cpu1 $cpu2 $cpu3 $cpu4 $cpu5
+ local index role roles num device need_tag want_untag port ports
+
+ json_select switch
+ json_select "$name"
+ json_get_keys roles roles
+ json_select ..
+ json_select ..
+
+ for index in $roles; do
+ eval "port=\$cpu$(((index - 1) % n_cpu))"
+
+ json_select switch
+ json_select "$name"
+ json_select ports
+ json_select "$port"
+ json_get_vars num device need_tag want_untag
+ json_select ..
+ json_select ..
+
+ if [ ${need_tag:-0} -eq 1 -o ${want_untag:-0} -ne 1 ]; then
+ num="${num}t"
+ device="${device}.${index}"
+ fi
+
+ json_select roles
+ json_select "$index"
+ json_get_vars role ports
+ json_add_string ports "$ports $num"
+ json_add_string device "$device"
+ json_select ..
+ json_select ..
+ json_select ..
+ json_select ..
+
+ json_select_object network
+ local devices
+
+ json_select_object "$role"
+ # attach previous interfaces (for multi-switch devices)
+ json_get_var devices device
+ if ! list_contains devices "$device"; then
+ devices="${devices:+$devices }$device"
+ fi
+ json_select ..
+ json_select ..
+
+ ucidef_set_interface "$role" device "$devices"
+ done
+}
+
+ucidef_set_ar8xxx_switch_mib() {
+ local name="$1"
+ local type="$2"
+ local interval="$3"
+
+ json_select_object switch
+ json_select_object "$name"
+ json_add_int ar8xxx_mib_type $type
+ json_add_int ar8xxx_mib_poll_interval $interval
+ json_select ..
+ json_select ..
+}
+
+ucidef_add_switch() {
+ local name="$1"; shift
+ local port num role device index need_tag prev_role
+ local cpu0 cpu1 cpu2 cpu3 cpu4 cpu5
+ local n_cpu=0 n_vlan=0 n_ports=0
+
+ json_select_object switch
+ json_select_object "$name"
+ json_add_boolean enable 1
+ json_add_boolean reset 1
+
+ for port in "$@"; do
+ case "$port" in
+ [0-9]*@*)
+ num="${port%%@*}"
+ device="${port##*@}"
+ need_tag=0
+ want_untag=0
+ [ "${num%t}" != "$num" ] && {
+ num="${num%t}"
+ need_tag=1
+ }
+ [ "${num%u}" != "$num" ] && {
+ num="${num%u}"
+ want_untag=1
+ }
+ ;;
+ [0-9]*:*:[0-9]*)
+ num="${port%%:*}"
+ index="${port##*:}"
+ role="${port#[0-9]*:}"; role="${role%:*}"
+ ;;
+ [0-9]*:*)
+ num="${port%%:*}"
+ role="${port##*:}"
+ ;;
+ esac
+
+ if [ -n "$num" ] && [ -n "$device$role" ]; then
+ _ucidef_add_switch_port
+ fi
+
+ unset num device role index need_tag want_untag
+ done
+ json_select ..
+ json_select ..
+
+ _ucidef_finish_switch_roles
+}
+
+ucidef_add_switch_attr() {
+ local name="$1"
+ local key="$2"
+ local val="$3"
+
+ json_select_object switch
+ json_select_object "$name"
+
+ case "$val" in
+ true|false) [ "$val" != "true" ]; json_add_boolean "$key" $? ;;
+ [0-9]) json_add_int "$key" "$val" ;;
+ *) json_add_string "$key" "$val" ;;
+ esac
+
+ json_select ..
+ json_select ..
+}
+
+ucidef_add_switch_port_attr() {
+ local name="$1"
+ local port="$2"
+ local key="$3"
+ local val="$4"
+ local ports i num
+
+ json_select_object switch
+ json_select_object "$name"
+
+ json_get_keys ports ports
+ json_select_array ports
+
+ for i in $ports; do
+ json_select "$i"
+ json_get_var num num
+
+ if [ -n "$num" ] && [ $num -eq $port ]; then
+ json_select_object attr
+
+ case "$val" in
+ true|false) [ "$val" != "true" ]; json_add_boolean "$key" $? ;;
+ [0-9]) json_add_int "$key" "$val" ;;
+ *) json_add_string "$key" "$val" ;;
+ esac
+
+ json_select ..
+ fi
+
+ json_select ..
+ done
+
+ json_select ..
+ json_select ..
+ json_select ..
+}
+
+ucidef_set_interface_macaddr() {
+ local network="$1"
+ local macaddr="$2"
+
+ ucidef_set_interface "$network" macaddr "$macaddr"
+}
+
+ucidef_set_label_macaddr() {
+ local macaddr="$1"
+
+ json_select_object system
+ json_add_string label_macaddr "$macaddr"
+ json_select ..
+}
+
+ucidef_add_atm_bridge() {
+ local vpi="$1"
+ local vci="$2"
+ local encaps="$3"
+ local payload="$4"
+ local nameprefix="$5"
+
+ json_select_object dsl
+ json_select_object atmbridge
+ json_add_int vpi "$vpi"
+ json_add_int vci "$vci"
+ json_add_string encaps "$encaps"
+ json_add_string payload "$payload"
+ json_add_string nameprefix "$nameprefix"
+ json_select ..
+ json_select ..
+}
+
+ucidef_add_adsl_modem() {
+ local annex="$1"
+ local firmware="$2"
+
+ json_select_object dsl
+ json_select_object modem
+ json_add_string type "adsl"
+ json_add_string annex "$annex"
+ json_add_string firmware "$firmware"
+ json_select ..
+ json_select ..
+}
+
+ucidef_add_vdsl_modem() {
+ local annex="$1"
+ local tone="$2"
+ local xfer_mode="$3"
+
+ json_select_object dsl
+ json_select_object modem
+ json_add_string type "vdsl"
+ json_add_string annex "$annex"
+ json_add_string tone "$tone"
+ json_add_string xfer_mode "$xfer_mode"
+ json_select ..
+ json_select ..
+}
+
+ucidef_set_led_ataport() {
+ _ucidef_set_led_trigger "$1" "$2" "$3" ata"$4"
+}
+
+_ucidef_set_led_common() {
+ local cfg="led_$1"
+ local name="$2"
+ local sysfs="$3"
+
+ json_select_object led
+
+ json_select_object "$1"
+ json_add_string name "$name"
+ json_add_string sysfs "$sysfs"
+}
+
+ucidef_set_led_default() {
+ local default="$4"
+
+ _ucidef_set_led_common "$1" "$2" "$3"
+
+ json_add_string default "$default"
+ json_select ..
+
+ json_select ..
+}
+
+ucidef_set_led_heartbeat() {
+ _ucidef_set_led_common "$1" "$2" "$3"
+
+ json_add_string trigger heartbeat
+ json_select ..
+
+ json_select ..
+}
+
+ucidef_set_led_gpio() {
+ local gpio="$4"
+ local inverted="$5"
+
+ _ucidef_set_led_common "$1" "$2" "$3"
+
+ json_add_string trigger "$trigger"
+ json_add_string type gpio
+ json_add_int gpio "$gpio"
+ json_add_boolean inverted "$inverted"
+ json_select ..
+
+ json_select ..
+}
+
+ucidef_set_led_ide() {
+ _ucidef_set_led_trigger "$1" "$2" "$3" disk-activity
+}
+
+ucidef_set_led_netdev() {
+ local dev="$4"
+ local mode="${5:-link tx rx}"
+
+ _ucidef_set_led_common "$1" "$2" "$3"
+
+ json_add_string type netdev
+ json_add_string device "$dev"
+ json_add_string mode "$mode"
+ json_select ..
+
+ json_select ..
+}
+
+ucidef_set_led_oneshot() {
+ _ucidef_set_led_timer $1 $2 $3 "oneshot" $4 $5
+}
+
+ucidef_set_led_portstate() {
+ local port_state="$4"
+
+ _ucidef_set_led_common "$1" "$2" "$3"
+
+ json_add_string trigger port_state
+ json_add_string type portstate
+ json_add_string port_state "$port_state"
+ json_select ..
+
+ json_select ..
+}
+
+ucidef_set_led_rssi() {
+ local iface="$4"
+ local minq="$5"
+ local maxq="$6"
+ local offset="${7:-0}"
+ local factor="${8:-1}"
+
+ _ucidef_set_led_common "$1" "$2" "$3"
+
+ json_add_string type rssi
+ json_add_string name "$name"
+ json_add_string iface "$iface"
+ json_add_string minq "$minq"
+ json_add_string maxq "$maxq"
+ json_add_string offset "$offset"
+ json_add_string factor "$factor"
+ json_select ..
+
+ json_select ..
+}
+
+ucidef_set_led_switch() {
+ local trigger_name="$4"
+ local port_mask="$5"
+ local speed_mask="$6"
+ local mode="$7"
+
+ _ucidef_set_led_common "$1" "$2" "$3"
+
+ json_add_string trigger "$trigger_name"
+ json_add_string type switch
+ json_add_string mode "$mode"
+ json_add_string port_mask "$port_mask"
+ json_add_string speed_mask "$speed_mask"
+ json_select ..
+
+ json_select ..
+}
+
+_ucidef_set_led_timer() {
+ local trigger_name="$4"
+ local delayon="$5"
+ local delayoff="$6"
+
+ _ucidef_set_led_common "$1" "$2" "$3"
+
+ json_add_string type "$trigger_name"
+ json_add_string trigger "$trigger_name"
+ json_add_int delayon "$delayon"
+ json_add_int delayoff "$delayoff"
+ json_select ..
+
+ json_select ..
+}
+
+ucidef_set_led_timer() {
+ _ucidef_set_led_timer $1 $2 $3 "timer" $4 $5
+}
+
+_ucidef_set_led_trigger() {
+ local trigger_name="$4"
+
+ _ucidef_set_led_common "$1" "$2" "$3"
+
+ json_add_string trigger "$trigger_name"
+ json_select ..
+
+ json_select ..
+}
+
+ucidef_set_led_usbdev() {
+ local dev="$4"
+
+ _ucidef_set_led_common "$1" "$2" "$3"
+
+ json_add_string type usb
+ json_add_string device "$dev"
+ json_select ..
+
+ json_select ..
+}
+
+ucidef_set_led_usbhost() {
+ _ucidef_set_led_trigger "$1" "$2" "$3" usb-host
+}
+
+ucidef_set_led_usbport() {
+ local obj="$1"
+ local name="$2"
+ local sysfs="$3"
+ shift
+ shift
+ shift
+
+ _ucidef_set_led_common "$obj" "$name" "$sysfs"
+
+ json_add_string type usbport
+ json_select_array ports
+ for port in "$@"; do
+ json_add_string port "$port"
+ done
+ json_select ..
+ json_select ..
+
+ json_select ..
+}
+
+ucidef_set_led_wlan() {
+ _ucidef_set_led_trigger "$1" "$2" "$3" "$4"
+}
+
+ucidef_set_rssimon() {
+ local dev="$1"
+ local refresh="$2"
+ local threshold="$3"
+
+ json_select_object rssimon
+
+ json_select_object "$dev"
+ [ -n "$refresh" ] && json_add_int refresh "$refresh"
+ [ -n "$threshold" ] && json_add_int threshold "$threshold"
+ json_select ..
+
+ json_select ..
+}
+
+ucidef_add_gpio_switch() {
+ local cfg="$1"
+ local name="$2"
+ local pin="$3"
+ local default="${4:-0}"
+
+ json_select_object gpioswitch
+ json_select_object "$cfg"
+ json_add_string name "$name"
+ json_add_string pin "$pin"
+ json_add_int default "$default"
+ json_select ..
+ json_select ..
+}
+
+ucidef_set_hostname() {
+ local hostname="$1"
+
+ json_select_object system
+ json_add_string hostname "$hostname"
+ json_select ..
+}
+
+ucidef_set_timezone() {
+ local timezone="$1"
+ json_select_object system
+ json_add_string timezone "$timezone"
+ json_select ..
+}
+
+ucidef_set_wireless() {
+ local band="$1"
+ local ssid="$2"
+ local encryption="$3"
+ local key="$4"
+
+ case "$band" in
+ all|2g|5g|6g) ;;
+ *) return;;
+ esac
+ [ -z "$ssid" ] && return
+
+ json_select_object wlan
+ json_select_object defaults
+ json_select_object ssids
+ json_select_object "$band"
+ json_add_string ssid "$ssid"
+ [ -n "$encryption" ] && json_add_string encryption "$encryption"
+ [ -n "$key" ] && json_add_string key "$key"
+ json_select ..
+ json_select ..
+ json_select ..
+ json_select ..
+}
+
+ucidef_set_country() {
+ local country="$1"
+
+ json_select_object wlan
+ json_select_object defaults
+ json_add_string country "$country"
+ json_select ..
+ json_select ..
+}
+
+ucidef_set_wireless_mac_count() {
+ local band="$1"
+ local mac_count="$2"
+
+ case "$band" in
+ 2g|5g|6g) ;;
+ *) return;;
+ esac
+ [ -z "$mac_count" ] && return
+
+ json_select_object wlan
+ json_select_object defaults
+ json_select_object ssids
+ json_select_object "$band"
+ json_add_string mac_count "$mac_count"
+ json_select ..
+ json_select ..
+ json_select ..
+ json_select ..
+}
+
+ucidef_set_root_password_plain() {
+ local passwd="$1"
+ json_select_object credentials
+ json_add_string root_password_plain "$passwd"
+ json_select ..
+}
+
+ucidef_set_root_password_hash() {
+ local passwd="$1"
+ json_select_object credentials
+ json_add_string root_password_hash "$passwd"
+ json_select ..
+}
+
+ucidef_set_ssh_authorized_key() {
+ local ssh_key="$1"
+ json_select_object credentials
+ json_select_array ssh_authorized_keys
+ json_add_string "" "$ssh_key"
+ json_select ..
+ json_select ..
+}
+
+ucidef_set_ntpserver() {
+ local server
+
+ json_select_object system
+ json_select_array ntpserver
+ for server in "$@"; do
+ json_add_string "" "$server"
+ done
+ json_select ..
+ json_select ..
+}
+
+ucidef_set_poe() {
+ json_select_object poe
+ json_add_string "budget" "$1"
+ json_select_array ports
+ for port in $2; do
+ json_add_string "" "$port"
+ done
+ json_select ..
+ json_select ..
+}
+
+ucidef_add_wlan() {
+ local path="$1"; shift
+
+ ucidef_wlan_idx=${ucidef_wlan_idx:-0}
+
+ json_select_object wlan
+ json_select_object "wl$ucidef_wlan_idx"
+ json_add_string path "$path"
+ json_add_fields "$@"
+ json_select ..
+ json_select ..
+
+ ucidef_wlan_idx="$((ucidef_wlan_idx + 1))"
+}
+
+board_config_update() {
+ json_init
+ [ -f ${CFG} ] && json_load "$(cat ${CFG})"
+
+ # auto-initialize model id and name if applicable
+ if ! json_is_a model object; then
+ json_select_object model
+ [ -f "/tmp/sysinfo/board_name" ] && \
+ json_add_string id "$(cat /tmp/sysinfo/board_name)"
+ [ -f "/tmp/sysinfo/model" ] && \
+ json_add_string name "$(cat /tmp/sysinfo/model)"
+ json_select ..
+ fi
+}
+
+board_config_flush() {
+ json_dump -i -o ${CFG}
+}