-
Notifications
You must be signed in to change notification settings - Fork 0
/
raspberry_pi_revision.sh
514 lines (485 loc) · 15.9 KB
/
raspberry_pi_revision.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
149
150
151
152
153
154
155
156
157
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#!/bin/bash
function run_rpifinder()
{
# Learn about our user's RPi hardware configuration by reading the revision number stored in '/proc/cpuinfo'
# Get revision number
#local HEXREVISION="$1" # uncomment this (and comment-out the line below this) if you want to pass revision numbers to this script instead of auto-detecting
local HEXREVISION=$(cat /proc/cpuinfo | grep Revision | cut -d ' ' -f 2) # Get revision number from cpuinfo (revision number is in hex)
# Convert revision number into a 32 bit binary string with leading zero's (name it "REVCODE")
local BINREVISION=$(echo "obase=2; ibase=16; ${HEXREVISION^^}" | bc) # Convert revision number from hex to binary (bc needs upper-case)
local COUNTBITS=${#BINREVISION}
if (( "$COUNTBITS" < "32" )); then # If the revision number is not 32 bits long, add leading zero's to it - Note: $(printf "%032d\n" $BINREVISION) doesn't work with large numbers
local ZEROSNEEDED=$((32-COUNTBITS))
local LEADINGZEROS=$(printf "%0${ZEROSNEEDED}d\n" 0)
local REVCODE=${LEADINGZEROS}${BINREVISION}
elif (( "$COUNTBITS" == "32" )); then
REVCODE=${BINREVISION}
else
echo "Something went wrong with calculating the Pi's revision number."
run_giveup
fi
# Parse $REVCODE (find substrings, determine new-format vs old-format, decipher/store info in variables, print info for the user).
# Now that REVCODE is readable in binary, create hexadecimal substrings from it.
# New-style revision codes: NOQuuuWwFMMMCCCCPPPPTTTTTTTTRRRR
# - https://www.raspberrypi.com/documentation/computers/raspberry-pi.html
# - https://github.com/raspberrypi/documentation/blob/develop/documentation/asciidoc/computers/raspberry-pi/revision-codes.adoc
# If a is constant, b=${a:12:5} does substring extraction where 12 is the offset (zero-based) and 5 is the length.
# - https://stackoverflow.com/questions/428109/extract-substring-in-bash
local N=${REVCODE:0:1} # Overvoltage (0: Overvoltage allowed, 1: Overvoltage disallowed)
local O=${REVCODE:1:1} # OTP Program (0: OTP programming allowed, 1: OTP programming disallowed)
local Q=${REVCODE:2:1} # OTP Read (0: OTP reading allowed, 1: OTP reading disallowed)
#local uuu=$(echo "obase=16; ibase=2; ${REVCODE:3:3}" | bc) # Unused bits
local W=${REVCODE:6:1} # Warranty bit [starting with RPi2/Zero] (0: Warranty is intact, 1: Warranty has been voided by overclocking)
local w=${REVCODE:7:1} # Unused bit [starting with RPi2/Zero] / warranty bit [prior to RPi2/Zero]
local F=${REVCODE:8:1} # New flag (1: new-style revision, 0: old-style revision)
local MMM=$(echo "obase=16; ibase=2; ${REVCODE:9:3}" | bc) # Memory size (0: 256MB, 1: 512MB, 2: 1GB, 3: 2GB, 4: 4GB, 5: 8GB)
local CCCC=$(echo "obase=16; ibase=2; ${REVCODE:12:4}" | bc) # Manufacturer (0: Sony UK, 1: Egoman, 2: Embest, 3: Sony Japan, 4: Embest, 5: Stadium)
local PPPP=$(echo "obase=16; ibase=2; ${REVCODE:16:4}" | bc) # Processor (0: BCM2835, 1: BCM2836, 2: BCM2837, 3: BCM2711, 4: BCM2712)
local TTTTTTTT=$(echo "obase=16; ibase=2; ${REVCODE:20:8}" | bc) # Type (0: A, 1: B, 2: A+, 3: B+, 4: 2B, 5: Alpha (early prototype), 6: CM1, 8: 3B,
# 9: Zero, a: CM3, c: Zero W, d: 3B+, e: 3A+, f: Internal use only, 10: CM3+,
# 11: 4B, 12: Zero 2 W, 13: 400, 14: CM4, 15: CM4S, 16: Internal use only, 17: 5)
local RRRR=$(echo "obase=16; ibase=2; ${REVCODE:28:4}" | bc) # Revision (0, 1, 2, etc.)
# Zero-out our variables in case this function runs twice (this step might be redundant)
PI_OVERVOLTAGE=""
PI_OTPPROGRAM=""
PI_OTPREAD=""
PI_WARRANTY=""
PI_RAM=""
PI_MANUFACTURER=""
PI_PROCESSOR=""
PI_TYPE=""
PI_REVISION=""
if [ "$F" = "0" ]; then
# Old-style revision codes [Leading 0x100 = warranty is void from overclocking (the "w" binary bit is set)]:
case $HEXREVISION in
"0002" | "1000002")
PI_TYPE="1B"
PI_REVISION="1.0"
PI_RAM="256MB"
PI_MANUFACTURER="Egoman"
;;
"0003" | "1000003")
PI_TYPE="1B"
PI_REVISION="1.0"
PI_RAM="256MB"
PI_MANUFACTURER="Egoman"
;;
"0004" | "1000004")
PI_TYPE="1B"
PI_REVISION="2.0"
PI_RAM="256MB"
PI_MANUFACTURER="Sony UK"
;;
"0005" | "1000005")
PI_TYPE="1B"
PI_REVISION="2.0"
PI_RAM="256MB"
PI_MANUFACTURER="Qisda"
;;
"0006" | "1000006")
PI_TYPE="1B"
PI_REVISION="2.0"
PI_RAM="256MB"
PI_MANUFACTURER="Egoman"
;;
"0007" | "1000007")
PI_TYPE="1A"
PI_REVISION="2.0"
PI_RAM="256MB"
PI_MANUFACTURER="Egoman"
;;
"0008" | "1000008")
PI_TYPE="1A"
PI_REVISION="2.0"
PI_RAM="256MB"
PI_MANUFACTURER="Sony UK"
;;
"0009" | "1000009")
PI_TYPE="1A"
PI_REVISION="2.0"
PI_RAM="256MB"
PI_MANUFACTURER="Qisda"
;;
"000d" | "100000d")
PI_TYPE="1B"
PI_REVISION="2.0"
PI_RAM="512MB"
PI_MANUFACTURER="Egoman"
;;
"000e" | "100000e")
PI_TYPE="1B"
PI_REVISION="2.0"
PI_RAM="512MB"
PI_MANUFACTURER="Sony UK"
;;
"000f" | "100000f")
PI_TYPE="1B"
PI_REVISION="2.0"
PI_RAM="512MB"
PI_MANUFACTURER="Egoman"
;;
"0010" | "1000010")
PI_TYPE="1B+"
PI_REVISION="1.2"
PI_RAM="512MB"
PI_MANUFACTURER="Sony UK"
;;
"0011" | "1000011")
PI_TYPE="CM1"
PI_REVISION="1.0"
PI_RAM="512MB"
PI_MANUFACTURER="Sony UK"
;;
"0012" | "1000012")
PI_TYPE="1A+"
PI_REVISION="1.1"
PI_RAM="256MB"
PI_MANUFACTURER="Sony UK"
;;
"0013" | "1000013")
PI_TYPE="1B+"
PI_REVISION="1.2"
PI_RAM="512MB"
PI_MANUFACTURER="Embest"
;;
"0014" | "1000014")
PI_TYPE="CM1"
PI_REVISION="1.0"
PI_RAM="512MB"
PI_MANUFACTURER="Embest"
;;
"0015" | "1000015")
PI_TYPE="1A+"
PI_REVISION="1.1"
PI_RAM="256MB/512MB"
PI_MANUFACTURER="Embest"
;;
*)
PI_TYPE="UNKNOWN"
PI_REVISION="UNKNOWN"
PI_RAM="UNKNOWN"
PI_MANUFACTURER="UNKNOWN"
echo "ERROR: Unable to parse Raspberry Pi old-style revision code."
run_giveup
;;
esac
case $w in
"0")
PI_WARRANTY="intact"
;;
"1")
PI_WARRANTY="voided by overclocking"
;;
*)
PI_WARRANTY="UNKNOWN"
echo "ERROR: Unable to parse Raspberry Pi old-style overclock/warranty code."
run_giveup
;;
esac
echo -e "\nRaspberry Pi Model ${PI_TYPE} Rev ${PI_REVISION} with ${PI_RAM} of RAM. Manufactured by ${PI_MANUFACTURER}."
echo "(Warranty ${PI_WARRANTY})"
elif [ "$F" = "1" ]; then
# New-style revision codes:
case $N in
"0")
PI_OVERVOLTAGE="allowed"
;;
"1")
PI_OVERVOLTAGE="disallowed"
;;
*)
PI_OVERVOLTAGE="UNKNOWN"
echo "ERROR: Unable to parse Raspberry Pi overvoltage allowance bit."
run_giveup
;;
esac
case $O in
"0")
PI_OTPPROGRAM="allowed"
;;
"1")
PI_OTPPROGRAM="disallowed"
;;
*)
PI_OTPPROGRAM="UNKNOWN"
echo "ERROR: Unable to parse Raspberry Pi OTP programming allowance bit."
run_giveup
;;
esac
case $Q in
"0")
PI_OTPREAD="allowed"
;;
"1")
PI_OTPREAD="disallowed"
;;
*)
PI_OTPREAD="UNKNOWN"
echo "ERROR: Unable to parse Raspberry Pi OTP reading allowance bit."
run_giveup
;;
esac
case $W in
"0")
PI_WARRANTY="intact"
;;
"1")
PI_WARRANTY="voided by overclocking"
;;
*)
PI_WARRANTY="UNKNOWN"
echo "ERROR: Unable to parse Raspberry Pi new-style overclock/warranty code."
run_giveup
;;
esac
case $MMM in
"0")
PI_RAM="256MB"
;;
"1")
PI_RAM="512MB"
;;
"2")
PI_RAM="1GB"
;;
"3")
PI_RAM="2GB"
;;
"4")
PI_RAM="4GB"
;;
"5")
PI_RAM="8GB"
;;
*)
PI_RAM="UNKNOWN"
echo "ERROR: Unable to parse Raspberry Pi RAM type code."
run_giveup
;;
esac
case $CCCC in
"0")
PI_MANUFACTURER="Sony UK"
;;
"1")
PI_MANUFACTURER="Egoman"
;;
"2")
PI_MANUFACTURER="Embest"
;;
"3")
PI_MANUFACTURER="Sony Japan"
;;
"4")
PI_MANUFACTURER="Embest"
;;
"5")
PI_MANUFACTURER="Stadium"
;;
*)
PI_MANUFACTURER="UNKNOWN"
echo "ERROR: Unable to parse Raspberry Pi manufacturer code."
run_giveup
;;
esac
case $PPPP in
"0")
PI_PROCESSOR="BCM2835"
;;
"1")
PI_PROCESSOR="BCM2836"
;;
"2")
PI_PROCESSOR="BCM2837"
;;
"3")
PI_PROCESSOR="BCM2711"
;;
"4")
PI_PROCESSOR="BCM2712"
;;
*)
PI_PROCESSOR="UNKNOWN"
echo "ERROR: Unable to parse Raspberry Pi processor type code."
run_giveup
;;
esac
case $TTTTTTTT in
"0")
PI_TYPE="1A"
;;
"1")
PI_TYPE="1B"
;;
"2")
PI_TYPE="1A+"
;;
"3")
PI_TYPE="1B+"
;;
"4")
PI_TYPE="2B"
;;
"5")
PI_TYPE="Alpha (early prototype)"
;;
"6")
PI_TYPE="CM1"
;;
"8")
PI_TYPE="3B"
;;
"9")
PI_TYPE="Zero"
;;
"a")
PI_TYPE="CM3"
;;
"c")
PI_TYPE="Zero W"
;;
"d")
PI_TYPE="3B+"
;;
"e")
PI_TYPE="3A+"
;;
"f")
PI_TYPE="Internal use only"
;;
"10")
PI_TYPE="CM3+"
;;
"11")
PI_TYPE="4B"
;;
"12")
PI_TYPE="Zero 2 W"
;;
"13")
PI_TYPE="400"
;;
"14")
PI_TYPE="CM4"
;;
"15")
PI_TYPE="CM4S"
;;
"16")
PI_TYPE="Internal use only"
;;
"17")
PI_TYPE="5"
;;
*)
PI_TYPE="UNKNOWN"
echo "ERROR: Unable to parse Raspberry Pi model code."
run_giveup
;;
esac
PI_REVISION="1.${RRRR}"
echo -e "\nRaspberry Pi Model ${PI_TYPE} Rev ${PI_REVISION} ${PI_PROCESSOR} with ${PI_RAM} of RAM. Manufactured by ${PI_MANUFACTURER}."
echo "(Overvoltage ${PI_OVERVOLTAGE}. OTP programming ${PI_OTPPROGRAM}. OTP reading ${PI_OTPREAD}. Warranty ${PI_WARRANTY})"
else
echo "ERROR: Could not read the Raspberry Pi's revision code version bit."
run_giveup
fi
# Categorize the Pi into a series (based on the $PI_TYPE variable)
if [ "$PI_TYPE" = "5" ]; then
SBC_SERIES=Pi5
elif [ "$PI_TYPE" = "4B" ] || [ "$PI_TYPE" = "400" ] || [ "$PI_TYPE" = "CM4" ]; then
SBC_SERIES=Pi4
elif [ "$PI_TYPE" = "3A+" ] || [ "$PI_TYPE" = "3B+" ] || [ "$PI_TYPE" = "CM3+" ]; then
SBC_SERIES=Pi3+
elif [ "$PI_TYPE" = "Zero 2 W" ]; then
SBC_SERIES=PiZ2
elif [ "$PI_TYPE" = "3B" ] || [ "$PI_TYPE" = "CM3" ]; then
SBC_SERIES=Pi3
elif [ "$PI_TYPE" = "Zero" ] || [ "$PI_TYPE" = "Zero W" ]; then
SBC_SERIES=PiZ1
elif [ "$PI_TYPE" = "2B" ]; then
SBC_SERIES=Pi2
elif [ "$PI_TYPE" = "1A+" ] || [ "$PI_TYPE" = "1B+" ]; then
SBC_SERIES=Pi1+
elif [ "$PI_TYPE" = "1A" ] || [ "$PI_TYPE" = "1B" ] || [ "$PI_TYPE" = "CM1" ]; then
SBC_SERIES=Pi1
elif [ "$PI_TYPE" = "Internal use only" ] || [ "$PI_TYPE" = "Alpha (early prototype)" ]; then
SBC_SERIES=X
else
echo "Error: Could not identify Pi series.">&2
run_giveup
fi
echo -e "\nThis Pi is part of the ${SBC_SERIES} series."
}
function run_detect_arch() # Finds what kind of processor we're running (aarch64, armv8l, armv7l, x86_64, x86, etc)
{
KARCH=$(uname -m) # don't use 'arch' since it is not supported by Termux
if [ "$KARCH" = "aarch64" ] || [ "$KARCH" = "aarch64-linux-gnu" ] || [ "$KARCH" = "arm64" ] || [ "$KARCH" = "aarch64_be" ]; then
ARCH=ARM64
echo -e "\nDetected an ARM processor running in 64-bit mode (detected ARM64)."
elif [ "$KARCH" = "armv8r" ] || [ "$KARCH" = "armv8l" ] || [ "$KARCH" = "armv7l" ] || [ "$KARCH" = "armhf" ] || [ "$KARCH" = "armel" ] || [ "$KARCH" = "armv8l-linux-gnu" ] || [ "$KARCH" = "armv7l-linux-gnueabi" ] || [ "$KARCH" = "armv7l-linux-gnueabihf" ] || [ "$KARCH" = "armv7a-linux-gnueabi" ] || [ "$KARCH" = "armv7a-linux-gnueabihf" ] || [ "$KARCH" = "armv7-linux-androideabi" ] || [ "$KARCH" = "arm-linux-gnueabi" ] || [ "$KARCH" = "arm-linux-gnueabihf" ] || [ "$KARCH" = "arm-none-eabi" ] || [ "$KARCH" = "arm-none-eabihf" ]; then
ARCH=ARM32
echo -e "\nDetected an ARM processor running in 32-bit mode (detected ARM32)."
elif [ "$KARCH" = "x86_64" ]; then
ARCH=x64
echo -e "\nDetected an x86_64 processor running in 64-bit mode (detected x64)."
elif [ "$KARCH" = "x86" ] || [ "$KARCH" = "i386" ] || [ "$KARCH" = "i686" ]; then
ARCH=x86
echo -e "\nDetected an x86 (or x86_64) processor running in 32-bit mode (detected x86)."
else
echo "Error: Could not identify processor architecture.">&2
run_giveup
fi
# References:
# https://unix.stackexchange.com/questions/136407/is-my-linux-arm-32-or-64-bit
# https://bgamari.github.io/posts/2019-06-12-arm-terminology.html
# https://superuser.com/questions/208301/linux-command-to-return-number-of-bits-32-or-64/208306#208306
# https://stackoverflow.com/questions/45125516/possible-values-for-uname-m
# Testing:
# RPi4B 64-bit OS: aarch64 (if I remember correctly)
# RPi4B & RPi3B+ 32-bit: armv7l
# Termux 64-bit with 64-bit proot: aarch64 (if I remember correctly)
# Termux 64-bit with 32-bit proot: armv8l
# Exagear RPi3/4 (32bit modified qemu chroot): i686 (if I remember correctly)
}
function run_gather_os_info()
{
# To my knowledge . . .
# Most post-2012 distros should have a standard '/etc/os-release' file for finding OS
# Pre-2012 distros (& small distros) may not have a canonical way of finding OS.
#
# Each release file has its own 'standard' vars, but five highly-conserved vars in all(?) os-release files are ...
# NAME="Alpine Linux"
# ID=alpine
# VERSION_ID=3.8.1
# PRETTY_NAME="Alpine Linux v3.8"
# HOME_URL="http://alpinelinux.org"
#
# Other known os-release file vars are listed here: https://docs.google.com/spreadsheets/d/1ixz0PfeWJ-n8eshMQN0BVoFAFnUmfI5HIMyBA0uK43o/edit#gid=0
#
# In general, we need to know: $ID (distro) & $VERSION_ID (distro version) into order to add Wine repo's for certain distro's/versions.
# If $VERSION_CODENAME is available then we should probably use this for figuring out which repo to use
#
# We will also have to determine package manager later, which we might try to do multiple ways (whitelist based on distro/version vs runtime detection)
# Try to find the os-release file on Linux systems
if [ -e /etc/os-release ]; then OS_INFOFILE='/etc/os-release' #&& echo "Found an OS info file located at ${OS_INFOFILE}"
elif [ -e /usr/lib/os-release ]; then OS_INFOFILE='/usr/lib/os-release' #&& echo "Found an OS info file located at ${OS_INFOFILE}"
elif [ -e /etc/*elease ]; then OS_INFOFILE='/etc/*elease' #&& echo "Found an OS info file located at ${OS_INFOFILE}"
# Add mac OS https://apple.stackexchange.com/questions/255546/how-to-find-file-release-in-os-x-el-capitan-10-11-6
# Add chrome OS
# Add chroot Android? (uname -o can be used to find "Android")
else OS_INFOFILE='' && echo "No Linux OS info files could be found!">&2 && run_giveup;
fi
# Load OS-Release File vars into memory (reads vars like "NAME", "ID", "VERSION_ID", "PRETTY_NAME", and "HOME_URL")
source "${OS_INFOFILE}"
# Print out some variables from the $OS_INFOFILE we found
echo -e "\nRunning ${ID} ${VERSION_ID} ${VERSION_CODENAME} - ${HOME_URL}"
}
function run_giveup()
{
echo "Something went wrong. Please report errors to the github."
exit
}
run_gather_os_info
run_detect_arch
run_rpifinder "$@"