base-files: Add new functions for ath11k caldata
[openwrt/staging/pepe2k.git] / package / base-files / files / lib / functions / system.sh
index f3c901ff7982f32c1033ecca7fb3cc38a13895ef..048e32f0e164147b3b73211c015323cf241f9167 100644 (file)
@@ -61,11 +61,21 @@ find_mtd_chardev() {
        echo "${INDEX:+$PREFIX$INDEX}"
 }
 
+get_mac_ascii() {
+       local part="$1"
+       local key="$2"
+       local mac_dirty
+
+       mac_dirty=$(strings "$part" | sed -n 's/^'"$key"'=//p')
+
+       # "canonicalize" mac
+       [ -n "$mac_dirty" ] && macaddr_canonicalize "$mac_dirty"
+}
+
 mtd_get_mac_ascii() {
        local mtdname="$1"
        local key="$2"
        local part
-       local mac_dirty
 
        part=$(find_mtd_part "$mtdname")
        if [ -z "$part" ]; then
@@ -73,17 +83,75 @@ mtd_get_mac_ascii() {
                return
        fi
 
-       mac_dirty=$(strings "$part" | sed -n 's/^'"$key"'=//p')
+       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))
+       local mtdname="$1"
+       local offset=$((${2:-0}))
+       local length="${3:-17}"
        local part
-       local mac_dirty
 
        part=$(find_mtd_part "$mtdname")
        if [ -z "$part" ]; then
@@ -91,15 +159,9 @@ mtd_get_mac_text() {
                return
        fi
 
-       if [ -z "$offset" ]; then
-               echo "mtd_get_mac_text: offset missing!" >&2
-               return
-       fi
-
-       mac_dirty=$(dd if="$part" bs=1 skip="$offset" count=17 2>/dev/null)
+       [ $((offset + length)) -le $(mtd_get_part_size "$mtdname") ] || return
 
-       # "canonicalize" mac
-       [ -n "$mac_dirty" ] && macaddr_canonicalize "$mac_dirty"
+       macaddr_canonicalize $(dd bs=1 if="$part" skip="$offset" count="$length" 2>/dev/null)
 }
 
 mtd_get_mac_binary() {
@@ -135,6 +197,29 @@ mtd_get_part_size() {
        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
@@ -145,6 +230,14 @@ macaddr_add() {
        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
@@ -174,10 +267,16 @@ macaddr_setbit_la() {
        macaddr_setbit $1 7
 }
 
-macaddr_2bin() {
+macaddr_unsetbit_mc() {
        local mac=$1
 
-       echo -ne \\x${mac//:/\\x}
+       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() {
@@ -212,3 +311,7 @@ macaddr_canonicalize() {
 
        printf "%02x:%02x:%02x:%02x:%02x:%02x" 0x${canon// / 0x} 2>/dev/null
 }
+
+dt_is_enabled() {
+       grep -q okay "/proc/device-tree/$1/status"
+}