ソースを参照

base-files: add macaddr_canonicalize helper function

In commit r38690, the MAC address canonicalization
has been converted to use 'tr' instead of 'printf'.
This only works if with MAC addresses which uses
the 'xx:xx:xx:xx:xx:xx' format.

However on some boards, the MAC addresses are stored
in different format in the mtd partition. Some vendors
are using hyphens or dots as separators instead of
colons. Also the leading zeroes may be missing from the
individual octets or those are replaced with spaces.

Add a new function which can be used to convert these
into the 'xx:xx:xx:xx:xx:xx' format. Also update the
'mtd_get_mac_ascii' function to use the new helper.

The helper function is based on this code:
  http://isquared.nl/blog/2010/08/11/Bash-function-to-canonicalize-MAC-addresses/

Signed-off-by: Gabor Juhos <[email protected]>

SVN-Revision: 38803
Gabor Juhos 12 年 前
コミット
b7914344ad
2 ファイル変更35 行追加2 行削除
  1. 1 1
      package/base-files/Makefile
  2. 34 1
      package/base-files/files/lib/functions.sh

+ 1 - 1
package/base-files/Makefile

@@ -11,7 +11,7 @@ include $(INCLUDE_DIR)/kernel.mk
 include $(INCLUDE_DIR)/version.mk
 include $(INCLUDE_DIR)/version.mk
 
 
 PKG_NAME:=base-files
 PKG_NAME:=base-files
-PKG_RELEASE:=148
+PKG_RELEASE:=149
 
 
 PKG_FILE_DEPENDS:=$(PLATFORM_DIR)/ $(GENERIC_PLATFORM_DIR)/base-files/
 PKG_FILE_DEPENDS:=$(PLATFORM_DIR)/ $(GENERIC_PLATFORM_DIR)/base-files/
 PKG_BUILD_DEPENDS:=opkg/host
 PKG_BUILD_DEPENDS:=opkg/host

+ 34 - 1
package/base-files/files/lib/functions.sh

@@ -257,7 +257,7 @@ mtd_get_mac_ascii()
 	mac_dirty=$(strings "$part" | sed -n 's/^'"$key"'=//p')
 	mac_dirty=$(strings "$part" | sed -n 's/^'"$key"'=//p')
 
 
 	# "canonicalize" mac
 	# "canonicalize" mac
-	[ -n "$mac_dirty" ] && echo ${mac_dirty} | tr [A-F] [a-f]
+	[ -n "$mac_dirty" ] && macaddr_canonicalize "$mac_dirty"
 }
 }
 
 
 mtd_get_mac_binary() {
 mtd_get_mac_binary() {
@@ -310,6 +310,39 @@ macaddr_2bin()
 	echo -ne \\x${mac//:/\\x}
 	echo -ne \\x${mac//:/\\x}
 }
 }
 
 
+macaddr_canonicalize()
+{
+	local mac="$1"
+	local canon=""
+
+	[ ${#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
+}
+
 strtok() { # <string> { <variable> [<separator>] ... }
 strtok() { # <string> { <variable> [<separator>] ... }
 	local tmp
 	local tmp
 	local val="$1"
 	local val="$1"