docs/gnulinux/encrypted_debian: don't tell the user to choose their own LV name
[libreboot.git] / flash
1 #!/bin/bash
2
3 #  flash script: uses flashrom to flash a libreboot ROM image
4 #
5 #       Copyright (C) 2014, 2015 Leah Rowe <info@minifree.org>
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU General Public License as published by
9 #    the Free Software Foundation, either version 3 of the License, or
10 #    (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU General Public License for more details.
16 #
17 #    You should have received a copy of the GNU General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20
21 # So that I one day find it again
22 # http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02
23
24 ## Don't add here. errors are expected.
25 [ "x${DEBUG+set}" = 'xset' ] && set -v
26 # set -u -e
27
28 if [ ${EUID} -ne 0 ]; then
29    printf "This script must be run as root\n"
30    exit 1
31 fi
32
33 arch="unknown"
34 if [ "$(uname -i)" = "i686" ] || [ "$(uname -m)" = "i686" ]; then
35         arch="i686"
36 elif [ "$(uname -i)" = "x86_64" ] || [ "$(uname -m)" = "x86_64" ]; then
37         arch="x86_64"
38 else
39         printf "This script must be run on an i686 or x86_64 host. x86_64 is recommended.\n"
40         exit 1
41 fi
42
43 usage="usage: ./flash mode path/to/yourrom.rom"
44 availablemodes="update, forceupdate, i945lenovo_firstflash, i945lenovo_secondflash, i945apple_firstflash"
45
46 mode="unknown"
47 rompath="unknown"
48
49 # User specified no or too few/many parameters
50 if [ $# -lt 2 ]; then
51         printf "%s\n" "${usage}"
52         printf "You need to specify exactly one mode, and one file\n"
53         printf "%s\n" "${availablemodes}"
54         exit 1
55 fi
56
57 mode="${1}"
58 rompath="${2}"
59
60 # User specified an invalid mode of operation
61 if [ "${mode}" != "update" ] && [ "${mode}" != "forceupdate" ] && [ "${mode}" != "i945lenovo_firstflash" ] && [ "${mode}" != "i945lenovo_secondflash" ] && [ "${mode}" != "i945apple_firstflash" ]; then
62         printf "%s\n" "${usage}"
63         printf "Invalid mode. Modes available: %s\n" "${availablemodes}"
64         exit 1
65 else
66         printf "Mode selected: %s\n" "${mode}"
67 fi
68
69 # The specified file does not exist
70 if [ ! -f "${rompath}" ]; then
71         printf "File not found!\n"
72         exit 1
73 fi
74
75 flashrom="unknown"
76 if [ -f "build" ]; then
77         # git or libreboot_src
78         flashrom="./flashrom/flashrom"
79 else
80         # libreboot_util
81         flashrom="./flashrom/${arch}/flashrom"
82 fi
83 if [ ! -f "${flashrom}" ]; then
84         printf "flashrom binary not present\n"
85         exit 1
86 fi
87
88 # i945 lenovobios
89 bucts="unknown"
90 flashrom_lenovobios_sst="unknown"
91 flashrom_lenovobios_macronix="unknown"
92 if [ "${mode}" = "i945lenovo_firstflash" ] || [ "${mode}" = "i945lenovo_secondflash" ]; then
93         if [ -f "build" ]; then
94                 # git or libreboot_src
95                 bucts="./bucts/bucts"
96                 flashrom_lenovobios_sst="./flashrom/flashrom_lenovobios_sst"
97                 flashrom_lenovobios_macronix="./flashrom/flashrom_lenovobios_macronix"
98         else
99                 # libreboot_util
100                 bucts="./bucts/${arch}/bucts"
101                 flashrom_lenovobios_sst="./flashrom/${arch}/flashrom_lenovobios_sst"
102                 flashrom_lenovobios_macronix="./flashrom/${arch}/flashrom_lenovobios_macronix"
103         fi
104         
105         # anti-bricking precaution
106         if [ ! -f "${bucts}" ]; then
107                 printf "bucts binary not present. ABORTING so as to protect against bricking the system.\n"
108                 exit 1
109         fi
110         
111         # fail if flashrom is not present
112         if [ ! -f "${flashrom_lenovobios_sst}" ] || [ ! -f "${flashrom_lenovobios_macronix}" ]; then
113                 printf "Flashrom binaries not present.\n"
114                 exit 1
115         fi
116 fi
117
118 if [ "${mode}" = "update" ]; then
119         ${flashrom} -p internal -w "${rompath}"
120 elif [ "${mode}" = "forceupdate" ]; then
121         ${flashrom} -p internal:boardmismatch=force,laptop=force_I_want_a_brick -w "${rompath}"
122 elif [ "${mode}" = "i945apple_firstflash" ]; then
123         ${flashrom} -p internal:laptop=force_I_want_a_brick -w "${rompath}"
124 elif [ "${mode}" = "i945lenovo_firstflash" ]; then
125         ${bucts} 1 # needed to prevent bricks.
126         # One will fail (this is harmless), and the other will succeed.
127         ${flashrom_lenovobios_sst} -p internal -w "${rompath}"
128         ${flashrom_lenovobios_macronix} -p internal -w "${rompath}"
129 elif [ "${mode}" = "i945lenovo_secondflash" ]; then
130         ${flashrom} -p internal:laptop=force_I_want_a_brick -w "${rompath}"
131         ${bucts} 0
132 fi
133
134