Adding eeprom info bash script
[jme.git] / scripts / jme_eeprom.sh
1 #!/bin/bash
2
3 ########################################
4 # Getting eeprom data                  #
5 ########################################
6
7 # Check bash version
8 msg="Please use bash version 3 or later."
9 if [[ ${BASH_VERSION} =~ ^([0-9])\.[0-9]\.(.*) ]]; then
10         if (( ${BASH_REMATCH[1]} < 3 )); then
11                 echo ${msg}
12                 exit
13         fi
14 else
15         echo ${msg}
16         exit
17 fi
18
19 # Parse arguments
20 if [[ ! $1 =~ ^eth[0-9]$ ]]; then
21         echo "Usage: $0 ethX"
22         echo
23         echo "Print eeprom info of JME Ethernet."
24         echo
25         exit
26 fi
27
28 # Get eeprom data via ethtool
29 PATH="${PATH}:/sbin:/usr/sbin:/bin:/usr/bin"
30 romdata=$(ethtool -e $1 raw on |\
31           od --width=1 --format=x1 --address-radix=x --output-duplicates |\
32           awk '{print $2;}')
33 if [[ -z "${romdata}" ]]; then
34         exit
35 fi
36 romdata=$(echo ${romdata} | tr '[:lower:]' '[:upper:]' | sed 's/\( FF\)\+$//')
37
38 ########################################
39 # Check for all non-optional data      #
40 ########################################
41
42 # Check JME eeprom prefix
43 echo -n "EEPROM prefix check: "
44 if [[ ${romdata} =~ ^55\ AA\ (.*)$ ]]; then
45         echo OK.
46 else
47         echo "Failed. (Not JMicron NIC?)"
48         exit
49 fi
50 romdata=${BASH_REMATCH[1]}
51
52 # Check JME eeprom suffix
53 echo -n "EEPROM suffix check: "
54 if [[ ${romdata} =~ ^(.*)\ 80\ 00\ 00$ ]]; then
55         echo OK.
56 else
57         echo "Failed (Not JMicron NIC?)."
58         exit
59 fi
60 romdata=${BASH_REMATCH[1]}
61
62 # Check JME eeprom MAC Address
63 echo -n "EEPROM MAC Address check: "
64 if [[ ${romdata} =~ ^(.*)[\ ]?01\ 38\ ([0-9A-F]{2})\ 01\ 39\ ([0-9A-F]{2})\ 01\ 3A\ ([0-9A-F]{2})\ 01\ 3B\ ([0-9A-F]{2})\ 01\ 3C\ ([0-9A-F]{2})\ 01\ 3D\ ([0-9A-F]{2})[\ ]?(.*)$ ]]; then
65         echo OK.
66         MACADDR=${BASH_REMATCH[2]}:${BASH_REMATCH[3]}:${BASH_REMATCH[4]}:${BASH_REMATCH[5]}:${BASH_REMATCH[6]}:${BASH_REMATCH[7]}
67         echo "       MAC Address: ${MACADDR}"
68 else
69         echo Failed.
70         exit
71 fi
72 romdata="${BASH_REMATCH[1]} ${BASH_REMATCH[8]}"
73
74 ########################################
75 # Print out all optional data         #
76 ########################################
77
78 if [[ -z "${romdata}" ]]; then
79         exit
80 fi
81
82 # Print other eeprom data
83 (( smicmd = 0 ))
84 echo -e "\nOther eeprom data:"
85 while [[ ! -z "${romdata}" ]]; do
86         if [[ ! ${romdata} =~ ^[\ ]?(0[012])\ ([0-9A-F]{2})\ ([0-9A-F]{2})[\ ]?(.*) ]]; then
87                 echo "    EEPROM contains error data"
88                 exit
89         fi
90         cmd=${BASH_REMATCH[1]}
91         address=${BASH_REMATCH[2]}
92         value=${BASH_REMATCH[3]}
93         romdata=${BASH_REMATCH[4]}
94
95         if [[ (( ${smicmd} == 0 )) && ${cmd} == "01" && ${address} == "53" ]]; then
96                 (( ++smicmd )); v1=${value}
97         elif [[ (( ${smicmd} == 1 )) && ${cmd} == "01" && ${address} == "52" ]]; then
98                 (( ++smicmd )); v2=${value}
99         elif [[ (( ${smicmd} == 2 )) && ${cmd} == "01" && ${address} == "51" ]]; then
100                 (( ++smicmd )); v3=${value}
101         elif [[ (( ${smicmd} == 3 )) && ${cmd} == "01" && ${address} == "50" ]]; then
102                 (( smicmd = 0 ))
103                 v4=${value}
104                 data=${v1}${v2}
105                 bcenv="scale = 0; obase = 10; ibase = 16;"
106                 reg=$(echo "${bcenv} ${v3} / 8;" | bc)
107                 phy=$(echo "${bcenv} a = ${v3}; b = ${v4};\
108                             a %= 4; a *= 8; b /= 40; a + b;" | bc)
109                 rw=$(echo "${bcenv} a = ${v4}; a /= 20; a % 2;" | bc)
110                 if [[ ${rw} == "1" ]]; then
111                         rw="Write"
112                         value=" value=${data}"
113                 else
114                         rw="Read"
115                         value=""
116                 fi
117                 valid=$(echo "${bcenv} a = ${v4}; a % 20;" | bc)
118                 if [[ ${valid} == "16" ]]; then
119                         valid="Valid"
120                 else
121                         valid="Invalid"
122                 fi
123                 echo "${valid} SMI Command: ${rw} phyaddr=${phy} reg=${reg}${value}"
124         elif [[ (( ${smicmd} != 0 )) ]]; then
125                 echo "    EEPROM contains error data"
126                 exit
127         else
128                 if [[ ${cmd} == "00" ]]; then
129                         echo -n "Configuration Space: "
130                 elif [[ ${cmd} == "01" ]]; then
131                         echo -n "First IO Space(BAR2): "
132                 elif [[ ${cmd} == "02" ]]; then
133                         echo -n "Second IO Space(BAR3): "
134                 fi
135                 echo "Address=${address} Value=${value}"
136         fi
137 done
138