developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | usage() |
| 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 | |
| 23 | factory_name="Factory" |
| 24 | factory_mtd=/dev/$(grep -i ''${factory_name}'' /proc/mtd | cut -c 1-4) |
| 25 | |
| 26 | #default:7622 |
| 27 | lan_mac_offset=0x2A |
| 28 | wan_mac_offset=0x24 |
| 29 | |
| 30 | case `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 | ;; |
| 49 | *) |
| 50 | lan_mac_offset=0x2A |
| 51 | wan_mac_offset=0x24 |
| 52 | ;; |
| 53 | esac |
| 54 | |
| 55 | #1.Read the offset's data from the Factory |
| 56 | #usage: Get_offset_data length offset |
| 57 | Get_offset_data() |
| 58 | { |
| 59 | local length=$1 |
| 60 | local offset=$2 |
| 61 | |
| 62 | hexdump -v -n ${length} -s ${offset} -e ''`expr ${length} - 1`'/1 "%02x-" "%02x "' ${factory_mtd} |
| 63 | } |
| 64 | |
| 65 | overwrite_data= |
| 66 | |
| 67 | Get_offset_overwrite_data() |
| 68 | { |
| 69 | local length=$1 |
| 70 | local offset=$2 |
| 71 | |
| 72 | overwrite_data=`hexdump -v -n ${length} -s ${offset} -e ''\`expr ${length} - 1\`'/1 "%02x " " %02x"' ${factory_mtd}` |
| 73 | } |
| 74 | |
| 75 | #2.Write the offset's data from the Factory |
| 76 | #usage: Set_offset_data length offset data |
| 77 | Set_offset_data() |
| 78 | { |
| 79 | local length=$1 |
| 80 | local offset=$2 |
| 81 | local index=`expr $# - ${length} + 1` |
| 82 | local data="" |
| 83 | |
| 84 | for j in $(seq ${index} `expr ${length} + ${index} - 1`) |
| 85 | do |
| 86 | temp=`eval echo '$'{"$j"}` |
| 87 | data=${data}"\x${temp}" |
| 88 | done |
| 89 | |
| 90 | dd if=${factory_mtd} of=/tmp/Factory.backup |
| 91 | printf "${data}" | dd conv=notrunc of=/tmp/Factory.backup bs=1 seek=$((${offset})) |
| 92 | mtd write /tmp/Factory.backup ${factory_name} |
| 93 | rm -rf /tmp/Factory.backup |
| 94 | } |
| 95 | |
| 96 | #3.Read Factory lan/wan mac address |
| 97 | GetMac() |
| 98 | { |
| 99 | if [ "$1" == "lan" ]; then |
| 100 | #read lan mac |
| 101 | Get_offset_data 6 ${lan_mac_offset} |
| 102 | elif [ "$1" == "wan" ]; then |
| 103 | #read wan mac |
| 104 | Get_offset_data 6 ${wan_mac_offset} |
| 105 | else |
| 106 | usage |
| 107 | exit 1 |
| 108 | fi |
| 109 | } |
| 110 | |
| 111 | |
| 112 | #4.write Factory lan/wan mac address |
| 113 | SetMac() |
| 114 | { |
| 115 | if [ "$#" != "9" ]; then |
| 116 | echo "Mac address must be 6 bytes!" |
| 117 | exit 1 |
| 118 | fi |
| 119 | |
| 120 | if [ "$1" == "lan" ]; then |
| 121 | #write lan mac |
| 122 | Set_offset_data 6 ${lan_mac_offset} $@ |
| 123 | |
| 124 | elif [ "$1" == "wan" ]; then |
| 125 | #write wan mac |
| 126 | Set_offset_data 6 ${wan_mac_offset} $@ |
| 127 | else |
| 128 | usage |
| 129 | exit 1 |
| 130 | fi |
| 131 | } |
| 132 | |
| 133 | #usage: |
| 134 | # 1. Set/Get the mac_address: mtk_factory -r/-w lan/wan /data |
| 135 | # 2. Set/Get the offset data: mtk_factory -r/-w length offset /data |
| 136 | # 3. Overwrite from offset1 to offset2 by length byte : mtk_factory -o length from to |
| 137 | if [ "$1" == "-r" ]; then |
| 138 | if [ "$2" == "lan" -o "$2" == "wan" ]; then |
| 139 | GetMac $2 |
| 140 | elif [ "$2" -eq "$2" ]; then |
| 141 | Get_offset_data $2 $3 |
| 142 | else |
| 143 | echo "Unknown command!" |
| 144 | usage |
| 145 | exit 1 |
| 146 | fi |
| 147 | elif [ "$1" == "-w" ]; then |
| 148 | if [ "$2" == "lan" -o "$2" == "wan" ]; then |
| 149 | SetMac $2 $@ |
| 150 | else |
| 151 | Set_offset_data $2 $3 $@ |
| 152 | fi |
| 153 | elif [ "$1" == "-o" ]; then |
| 154 | Get_offset_overwrite_data $2 $3 |
| 155 | Set_offset_data $2 $4 ${overwrite_data} |
| 156 | else |
| 157 | echo "Unknown command!" |
| 158 | usage |
| 159 | exit 1 |
| 160 | fi |