blob: 3124088b63ea19e861602a1d13ecc93ef5a34991 [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*)
developer67d45e72022-08-02 11:42:56 +080050 #1024k - 18 byte
51 lan2_mac_offset=0xFFFEE
52 lan_mac_offset=0xFFFF4
53 wan_mac_offset=0xFFFFA
developer6c4462f2022-08-01 14:40:07 +080054 ;;
developerfd40db22021-04-29 10:08:25 +080055 *)
56 lan_mac_offset=0x2A
57 wan_mac_offset=0x24
58 ;;
59esac
60
61#1.Read the offset's data from the Factory
62#usage: Get_offset_data length offset
63Get_offset_data()
64{
65 local length=$1
66 local offset=$2
67
68 hexdump -v -n ${length} -s ${offset} -e ''`expr ${length} - 1`'/1 "%02x-" "%02x "' ${factory_mtd}
69}
70
71overwrite_data=
72
73Get_offset_overwrite_data()
74{
75 local length=$1
76 local offset=$2
77
78 overwrite_data=`hexdump -v -n ${length} -s ${offset} -e ''\`expr ${length} - 1\`'/1 "%02x " " %02x"' ${factory_mtd}`
79}
80
81#2.Write the offset's data from the Factory
82#usage: Set_offset_data length offset data
83Set_offset_data()
84{
85 local length=$1
86 local offset=$2
87 local index=`expr $# - ${length} + 1`
88 local data=""
89
90 for j in $(seq ${index} `expr ${length} + ${index} - 1`)
91 do
92 temp=`eval echo '$'{"$j"}`
93 data=${data}"\x${temp}"
94 done
95
96 dd if=${factory_mtd} of=/tmp/Factory.backup
97 printf "${data}" | dd conv=notrunc of=/tmp/Factory.backup bs=1 seek=$((${offset}))
98 mtd write /tmp/Factory.backup ${factory_name}
99 rm -rf /tmp/Factory.backup
100}
101
102#3.Read Factory lan/wan mac address
103GetMac()
104{
105 if [ "$1" == "lan" ]; then
106 #read lan mac
107 Get_offset_data 6 ${lan_mac_offset}
developer67d45e72022-08-02 11:42:56 +0800108 elif [ "$1" == "lan2" ]; then
109 #read lan2 mac
110 Get_offset_data 6 ${lan2_mac_offset}
developerfd40db22021-04-29 10:08:25 +0800111 elif [ "$1" == "wan" ]; then
112 #read wan mac
113 Get_offset_data 6 ${wan_mac_offset}
114 else
115 usage
116 exit 1
117 fi
118}
119
120
121#4.write Factory lan/wan mac address
122SetMac()
123{
124 if [ "$#" != "9" ]; then
125 echo "Mac address must be 6 bytes!"
126 exit 1
127 fi
128
129 if [ "$1" == "lan" ]; then
130 #write lan mac
131 Set_offset_data 6 ${lan_mac_offset} $@
132
developer67d45e72022-08-02 11:42:56 +0800133 elif [ "$1" == "lan2" ]; then
134 #write lan2 mac
135 Set_offset_data 6 ${lan2_mac_offset} $@
136
developerfd40db22021-04-29 10:08:25 +0800137 elif [ "$1" == "wan" ]; then
138 #write wan mac
139 Set_offset_data 6 ${wan_mac_offset} $@
140 else
141 usage
142 exit 1
143 fi
144}
145
146#usage:
147# 1. Set/Get the mac_address: mtk_factory -r/-w lan/wan /data
148# 2. Set/Get the offset data: mtk_factory -r/-w length offset /data
149# 3. Overwrite from offset1 to offset2 by length byte : mtk_factory -o length from to
150if [ "$1" == "-r" ]; then
developer67d45e72022-08-02 11:42:56 +0800151 if [ "$2" == "lan" -o "$2" == "lan2" -o "$2" == "wan" ]; then
developerfd40db22021-04-29 10:08:25 +0800152 GetMac $2
153 elif [ "$2" -eq "$2" ]; then
154 Get_offset_data $2 $3
155 else
156 echo "Unknown command!"
157 usage
158 exit 1
159 fi
160elif [ "$1" == "-w" ]; then
developer67d45e72022-08-02 11:42:56 +0800161 if [ "$2" == "lan" -o "$2" == "lan2" -o "$2" == "wan" ]; then
developerfd40db22021-04-29 10:08:25 +0800162 SetMac $2 $@
163 else
164 Set_offset_data $2 $3 $@
165 fi
166elif [ "$1" == "-o" ]; then
167 Get_offset_overwrite_data $2 $3
168 Set_offset_data $2 $4 ${overwrite_data}
169else
170 echo "Unknown command!"
171 usage
172 exit 1
173fi