what_pi 8.44 KB
Newer Older
Dave Morriss's avatar
Dave Morriss committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash -
#===============================================================================
#
#         FILE: what_pi
#
#        USAGE: ./what_pi
#
#  DESCRIPTION: To be run on a RPi. Reports back what model it is. Uses info
#               from /proc/cpuinfo and a lookup table from
#               http://elinux.org/RPi_HardwareHistory
#
#      OPTIONS: ---
# REQUIREMENTS: ---
#         BUGS: ---
Dave Morriss's avatar
Dave Morriss committed
15
#        NOTES: ---
Dave Morriss's avatar
Dave Morriss committed
16
#       AUTHOR: Dave Morriss (djm), Dave.Morriss@gmail.com
17
#               Rob Evans (n1be) - added call to ifconfig
Dave Morriss's avatar
Dave Morriss committed
18
#      VERSION: 0.0.10
Dave Morriss's avatar
Dave Morriss committed
19
#      CREATED: 2016-06-17 18:17:47
Dave Morriss's avatar
Dave Morriss committed
20
#     REVISION: 2020-11-16 16:22:23
Dave Morriss's avatar
Dave Morriss committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#
#===============================================================================

set -o nounset                              # Treat unset variables as an error

SCRIPT=${0##*/}

#===  FUNCTION  ================================================================
#         NAME: network_info
#  DESCRIPTION: Reports some basic network information in a (hopefully)
#               generalised way.
#   PARAMETERS: None
#      RETURNS: Nothing
#===============================================================================
network_info () {
    local d dev mac

    echo "Network information:"
39
    printf '  %-11s: %s\n' "Hostname" "$(hostname -f)"
Dave Morriss's avatar
Dave Morriss committed
40
41
42
43
44
    for d in /sys/class/net/*/address; do
        dev="${d%/*}"
        dev="${dev##*/}"
        if [[ $dev != 'lo' ]]; then
            mac="$(cat "$d")"
45
            printf '  %-11s: %s (%s)\n' "MAC" "$mac" "$dev"
46
47
48
49
50
            /sbin/ifconfig "$dev" | \
                sed -n -e '/^[[:space:]]\+inet/{
                    s/^[[:space:]]\+/    /
                    s/^\([[:space:]]\+inet \)/\1 /
                    p}'
Dave Morriss's avatar
Dave Morriss committed
51
52
53
54
55
56
57
58
59
60
61
        fi
    done
}

#===  FUNCTION  ================================================================
#         NAME: settings_info
#  DESCRIPTION: Reports stuff about settings and config file elements
#   PARAMETERS: None
#      RETURNS: Nothing
#===============================================================================
settings_info () {
62
63
64
65
66
67
68
    local codec vcgencmd

    vcgencmd=$(which vcgencmd)
    if [[ -z $vcgencmd ]]; then
        echo "Unable to find 'vcgencmd'; can't continue"
        return
    fi
Dave Morriss's avatar
Dave Morriss committed
69
70
71
72

    #
    # Is the user in the 'video' group?
    #
73
    if id -Gn | grep -Eq 'video|root'; then
Dave Morriss's avatar
Dave Morriss committed
74
        echo "Various configuration and other settings:"
75
        echo "CPU $($vcgencmd measure_temp)"
Dave Morriss's avatar
Dave Morriss committed
76
        for codec in H264 MPG2 WVC1 MPG4 MJPG WMV9; do
77
            echo -e "$codec:\\t$($vcgencmd codec_enabled $codec)"
Dave Morriss's avatar
Dave Morriss committed
78
        done
79
80
        $vcgencmd get_config sdtv_mode
        $vcgencmd get_config sdtv_aspect
Dave Morriss's avatar
Dave Morriss committed
81
    else
82
        echo "Can't run 'vcgencmd'; you're not in the 'video' group"
Dave Morriss's avatar
Dave Morriss committed
83
84
85
    fi
}

Dave Morriss's avatar
Dave Morriss committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#===  FUNCTION  ================================================================
#         NAME: os_info
#  DESCRIPTION: Reports stuff about the OS version (and tries not to pollute
#               the shell environment in doing so)
#   PARAMETERS: None
#      RETURNS: Nothing
#===============================================================================
os_info () {
    local os_release='/etc/os-release'

    echo "Operating system details:"

    if [[ -e $os_release ]]; then
        eval "local $(grep 'PRETTY_NAME' $os_release)"
        printf '%-13s: %s\n' "Release" "$PRETTY_NAME"
    else
        printf '%-13s: %s\n' "Release" "unknown"
    fi

    printf '%-13s: %s\n' "Kernel" "$(uname -rv)"
    printf '%-13s: %s\n' "Machine" "$(uname -m)"
}

Dave Morriss's avatar
Dave Morriss committed
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#===  FUNCTION  ================================================================
#         NAME: cleanup_temp
#  DESCRIPTION: Cleanup temporary files in case of a keyboard interrupt
#               (SIGINT) or a termination signal (SIGTERM) and at script
#               exit
#   PARAMETERS: * - names of temporary files to delete
#      RETURNS: Nothing
#===============================================================================
function cleanup_temp {
    for tmp in "$@"; do
        [ -e "$tmp" ] && rm --force "$tmp"
    done
    exit 0
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#
# Are we on a Pi at all?
# TODO: Check this. It works on all my machines, but may not work everywhere
#
model=$(grep -m 1 '^model name' /proc/cpuinfo | cut -f2 -d:)
re="ARMv[0-9]"
if [[ ! $model =~ $re ]]; then
    echo "This doesn't seem to be a Raspberry Pi"
    exit 1
fi

#
# Make temporary files and set traps to delete them
#
TMP1=$(mktemp) || { echo "$SCRIPT: creation of temporary file failed!"; exit 1; }
TMP2=$(mktemp) || { echo "$SCRIPT: creation of temporary file failed!"; exit 1; }
trap 'cleanup_temp $TMP1 $TMP2' SIGHUP SIGINT SIGPIPE SIGTERM EXIT

#
# Create a table of Pi stuff. Copied from http://elinux.org/RPi_HardwareHistory
# using simple cut and paste. The result is a table separated by tabs, and
# this script relies on this fact.
# You will have to refresh this every time a new Pi model is released. This
149
150
# version is dated Q4 2019 and includes the Pi 4 Model B (1GB, 2GB, and
# 4GB models).
151
152
153
154
# The master table has not been updated by Q4 2020, but an alternative source
# of information may be found at
# https://www.raspberrypi.org/documentation/hardware/raspberrypi/revision-codes/README.md.
# This has been used to update the list below.
Dave Morriss's avatar
Dave Morriss committed
155
#
156
#{{{
Dave Morriss's avatar
Dave Morriss committed
157
cat > "$TMP1" <<'ENDTABLE'
Dave Morriss's avatar
Dave Morriss committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
Revision	Release Date	Model	PCB Revision	Memory	Notes
Beta	Q1 2012	B (Beta)	?	256 MB	Beta Board
0002	Q1 2012	B	1.0	256 MB	
0003	Q3 2012	B (ECN0001)	1.0	256 MB	Fuses mod and D14 removed
0004	Q3 2012	B	2.0	256 MB	(Mfg by Sony)
0005	Q4 2012	B	2.0	256 MB	(Mfg by Qisda)
0006	Q4 2012	B	2.0	256 MB	(Mfg by Egoman)
0007	Q1 2013	A	2.0	256 MB	(Mfg by Egoman)
0008	Q1 2013	A	2.0	256 MB	(Mfg by Sony)
0009	Q1 2013	A	2.0	256 MB	(Mfg by Qisda)
000d	Q4 2012	B	2.0	512 MB	(Mfg by Egoman)
000e	Q4 2012	B	2.0	512 MB	(Mfg by Sony)
000f	Q4 2012	B	2.0	512 MB	(Mfg by Qisda)
0010	Q3 2014	B+	1.0	512 MB	(Mfg by Sony)
0011	Q2 2014	Compute Module 1	1.0	512 MB	(Mfg by Sony)
0012	Q4 2014	A+	1.1	256 MB	(Mfg by Sony)
0013	Q1 2015	B+	1.2	512 MB	(Mfg by Embest)
0014	Q2 2014	Compute Module 1	1.0	512 MB	(Mfg by Embest)
0015	?	A+	1.1	256 MB / 512 MB	(Mfg by Embest)
a01040	Unknown	2 Model B	1.0	1 GB	(Mfg by Sony)
a01041	Q1 2015	2 Model B	1.1	1 GB	(Mfg by Sony)
a21041	Q1 2015	2 Model B	1.1	1 GB	(Mfg by Embest)
a22042	Q3 2016	2 Model B (with BCM2837)	1.2	1 GB	(Mfg by Embest)
900021	Q3 2016	A+	1.1	512 MB	(Mfg by Sony)
900032	Q2 2016?	B+	1.2	512 MB	(Mfg by Sony)
900092	Q4 2015	Zero	1.2	512 MB	(Mfg by Sony)
900093	Q2 2016	Zero	1.3	512 MB	(Mfg by Sony)
920093	Q4 2016?	Zero	1.3	512 MB	(Mfg by Embest)
9000c1	Q1 2017	Zero W	1.1	512 MB	(Mfg by Sony)
a02082	Q1 2016	3 Model B	1.2	1 GB	(Mfg by Sony)
a020a0	Q1 2017	Compute Module 3 (and CM3 Lite)	1.0	1 GB	(Mfg by Sony)
a22082	Q1 2016	3 Model B	1.2	1 GB	(Mfg by Embest)
a32082	Q4 2016	3 Model B	1.2	1 GB	(Mfg by Sony Japan)
a020d3	Q1 2018	3 Model B+	1.3	1 GB	(Mfg by Sony)
9020e0	Q4 2018	3 Model A+	1.0	512 MB	(Mfg by Sony)
Dave Morriss's avatar
Dave Morriss committed
193
194
a03111	Q2 2019	4 Model B	1.1	1 GB	(Mfg by Sony)
b03111	Q2 2019	4 Model B	1.1	2 GB	(Mfg by Sony)
195
b03112	Q2 2019	4 Model B	1.2	2 GB	(Mfg by Sony)
Dave Morriss's avatar
Dave Morriss committed
196
c03111	Q2 2019	4 Model B	1.1	4 GB	(Mfg by Sony)
197
198
c03112	Q2 2019	4 Model B	1.2	4 GB	(Mfg by Sony)
d03114	Q2 2019	4 Model B	1.4	8 GB	(Mfg by Sony)
Dave Morriss's avatar
Dave Morriss committed
199
ENDTABLE
200
#}}}
Dave Morriss's avatar
Dave Morriss committed
201
202
203
204
205
206
207
208
209
210
211
212

#
# Grab two values from the /proc/cpuinfo file
#
REV="$(grep '^Revision' /proc/cpuinfo | awk '{print $3}' | sed 's/^1000//')"
SER="$(grep '^Serial' /proc/cpuinfo | awk '{print $3}')"

#
# Make an Awk script which finds the details in the above table and displays
# them
#
cat > "$TMP2" <<'ENDPROG'
213
214
215
216
BEGIN {
    fmt = "%-13s: %s\n%-13s: %s\n%-13s: %s\n%-13s: %s\n%-13s: %s\n%-13s: %s\n%-13s: %s\n"
    matched = 0
}
217
tolower($0) ~ "^" rev {
218
    printf fmt,
Dave Morriss's avatar
Dave Morriss committed
219
220
221
222
223
224
225
        "Revision",$1,
        "Release date",$2,
        "Model",$3,
        "PCB Revision",$4,
        "Memory",$5,
        "Notes",$6,
        "Serial no",serial
226
227
228
229
230
231
232
233
234
235
236
237
238
    matched = 1
}
END {
    if (matched == 0) {
        printf fmt,
            "Revision",rev " (unknown revision)",
            "Release date","--",
            "Model","--",
            "PCB Revision","--",
            "Memory","--",
            "Notes","--",
            "Serial no",serial
    }
Dave Morriss's avatar
Dave Morriss committed
239
240
241
242
243
244
245
246
}
ENDPROG

#
# Run Awk on the table with the above script, passing the revision number as
# a regular expression for searching, and the serial number as a simple
# string.
#
247
awk -v "rev=$REV" -v "serial=$SER" -F" *\\t *" -f "$TMP2" "$TMP1"
Dave Morriss's avatar
Dave Morriss committed
248

Dave Morriss's avatar
Dave Morriss committed
249
250
251
252
253
254
#
# Report OS information
#
echo
os_info

Dave Morriss's avatar
Dave Morriss committed
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#
# Report various settings and parameters
#
echo
settings_info

#
# Report network information
#
echo
network_info

exit

269
# vim: syntax=sh:ts=8:sw=4:ai:et:tw=78:fo=tcrqn21:fdm=marker