blob: e5bd47e6ddcee677e434079b9853e8e768ba9368 [file] [log] [blame]
developerfd40db22021-04-29 10:08:25 +08001#!/bin/sh
2
3usage()
4{
5 echo "This is a script to get or set mtk factory's data"
6 echo "-Typically, get or set the eth lan/wan mac_address-"
7 echo "Usage1: $0 <op> <side> [mac_address] "
8 echo " <op>: -r or -w (Read or Write action)"
9 echo " [mac_address]: MAC[1] MAC[2] MAC[3] MAC[4] MAC[5] MAC[6] (only for write action)"
10 echo "Usage2: $0 <op> <length> <offset> [data] "
11 echo " <length>: length bytes of input"
12 echo " <offset>: Skip offset bytes from the beginning of the input"
13 echo "Usage3: $0 -o <length> <get_from> <overwrite_to>"
14 echo "Example:"
15 echo "$0 -w lan 00 0c 43 68 55 56"
16 echo "$0 -r lan"
17 echo "$0 -w 8 0x22 11 22 33 44 55 66 77 88"
18 echo "$0 -r 8 0x22"
19 echo "$0 -o 12 0x24 0x7fff4"
20 exit 1
21}
22
23factory_name="Factory"
24factory_mtd=/dev/$(grep -i ''${factory_name}'' /proc/mtd | cut -c 1-4)
25
26#default:7622
27lan_mac_offset=0x2A
28wan_mac_offset=0x24
29
30case `cat /tmp/sysinfo/board_name` in
31 *7621*ax*)
32 # 256k - 12 byte
33 lan_mac_offset=0x3FFF4
34 wan_mac_offset=0x3FFFA
35 ;;
36 *7621*)
37 lan_mac_offset=0xe000
38 wan_mac_offset=0xe006
39 ;;
40 *7622*)
41 #512k -12 byte
42 lan_mac_offset=0x7FFF4
43 wan_mac_offset=0x7FFFA
44 ;;
45 *7623*)
46 lan_mac_offset=0x1F800
47 wan_mac_offset=0x1F806
48 ;;
developer6c4462f2022-08-01 14:40:07 +080049 *7988*)
50 #2560k - 12 byte
51 lan_mac_offset=0x27FFF4
52 wan_mac_offset=0x27FFFA
53 ;;
developerfd40db22021-04-29 10:08:25 +080054 *)
55 lan_mac_offset=0x2A
56 wan_mac_offset=0x24
57 ;;
58esac
59
60#1.Read the offset's data from the Factory
61#usage: Get_offset_data length offset
62Get_offset_data()
63{
64 local length=$1
65 local offset=$2
66
67 hexdump -v -n ${length} -s ${offset} -e ''`expr ${length} - 1`'/1 "%02x-" "%02x "' ${factory_mtd}
68}
69
70overwrite_data=
71
72Get_offset_overwrite_data()
73{
74 local length=$1
75 local offset=$2
76
77 overwrite_data=`hexdump -v -n ${length} -s ${offset} -e ''\`expr ${length} - 1\`'/1 "%02x " " %02x"' ${factory_mtd}`
78}
79
80#2.Write the offset's data from the Factory
81#usage: Set_offset_data length offset data
82Set_offset_data()
83{
84 local length=$1
85 local offset=$2
86 local index=`expr $# - ${length} + 1`
87 local data=""
88
89 for j in $(seq ${index} `expr ${length} + ${index} - 1`)
90 do
91 temp=`eval echo '$'{"$j"}`
92 data=${data}"\x${temp}"
93 done
94
95 dd if=${factory_mtd} of=/tmp/Factory.backup
96 printf "${data}" | dd conv=notrunc of=/tmp/Factory.backup bs=1 seek=$((${offset}))
97 mtd write /tmp/Factory.backup ${factory_name}
98 rm -rf /tmp/Factory.backup
99}
100
101#3.Read Factory lan/wan mac address
102GetMac()
103{
104 if [ "$1" == "lan" ]; then
105 #read lan mac
106 Get_offset_data 6 ${lan_mac_offset}
107 elif [ "$1" == "wan" ]; then
108 #read wan mac
109 Get_offset_data 6 ${wan_mac_offset}
110 else
111 usage
112 exit 1
113 fi
114}
115
116
117#4.write Factory lan/wan mac address
118SetMac()
119{
120 if [ "$#" != "9" ]; then
121 echo "Mac address must be 6 bytes!"
122 exit 1
123 fi
124
125 if [ "$1" == "lan" ]; then
126 #write lan mac
127 Set_offset_data 6 ${lan_mac_offset} $@
128
129 elif [ "$1" == "wan" ]; then
130 #write wan mac
131 Set_offset_data 6 ${wan_mac_offset} $@
132 else
133 usage
134 exit 1
135 fi
136}
137
138#usage:
139# 1. Set/Get the mac_address: mtk_factory -r/-w lan/wan /data
140# 2. Set/Get the offset data: mtk_factory -r/-w length offset /data
141# 3. Overwrite from offset1 to offset2 by length byte : mtk_factory -o length from to
142if [ "$1" == "-r" ]; then
143 if [ "$2" == "lan" -o "$2" == "wan" ]; then
144 GetMac $2
145 elif [ "$2" -eq "$2" ]; then
146 Get_offset_data $2 $3
147 else
148 echo "Unknown command!"
149 usage
150 exit 1
151 fi
152elif [ "$1" == "-w" ]; then
153 if [ "$2" == "lan" -o "$2" == "wan" ]; then
154 SetMac $2 $@
155 else
156 Set_offset_data $2 $3 $@
157 fi
158elif [ "$1" == "-o" ]; then
159 Get_offset_overwrite_data $2 $3
160 Set_offset_data $2 $4 ${overwrite_data}
161else
162 echo "Unknown command!"
163 usage
164 exit 1
165fi