[openwrt/openwrt] base-files: rework mtd_get_mac_text()

LEDE Commits lede-commits at lists.infradead.org
Fri Jan 6 06:35:13 PST 2023


hauke pushed a commit to openwrt/openwrt.git, branch master:
https://git.openwrt.org/1e3a8f454e46e6c235b972571761a0c6b23a3bfd

commit 1e3a8f454e46e6c235b972571761a0c6b23a3bfd
Author: Michael Pratt <mcpratt at pm.me>
AuthorDate: Sat Dec 10 06:34:54 2022 -0500

    base-files: rework mtd_get_mac_text()
    
    It's necessary to be able to specify the length
    for MAC addresses that are stored in flash, for example,
    in a case where it is stored without any delimiter.
    
    Let both offset and length have default values.
    
    Add a sanity check related to partition size.
    
    Also, clean up syntax and unnecessary lines.
    
    Signed-off-by: Michael Pratt <mcpratt at pm.me>
---
 package/base-files/files/lib/functions/system.sh | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)

diff --git a/package/base-files/files/lib/functions/system.sh b/package/base-files/files/lib/functions/system.sh
index 94ccc02bb8..d06354b01f 100644
--- a/package/base-files/files/lib/functions/system.sh
+++ b/package/base-files/files/lib/functions/system.sh
@@ -141,10 +141,10 @@ mtd_get_mac_uci_config_ubi() {
 }
 
 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
@@ -152,15 +152,9 @@ mtd_get_mac_text() {
 		return
 	fi
 
-	if [ -z "$offset" ]; then
-		echo "mtd_get_mac_text: offset missing!" >&2
-		return
-	fi
+	[ $((offset + length)) -le $(mtd_get_part_size "$mtdname") ] || return
 
-	mac_dirty=$(dd if="$part" bs=1 skip="$offset" count=17 2>/dev/null)
-
-	# "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() {




More information about the lede-commits mailing list