1
|
#!/usr/local/bin/php -f
|
2
|
|
3
|
<?php
|
4
|
|
5
|
require("globals.inc");
|
6
|
require("config.inc");
|
7
|
require("functions.inc");
|
8
|
require("util.inc");
|
9
|
|
10
|
$g['booting'] = true;
|
11
|
|
12
|
echo "Starting the {$g['product_name']} console firmware update system";
|
13
|
|
14
|
require("functions.inc");
|
15
|
echo ".";
|
16
|
|
17
|
$g['booting'] = false;
|
18
|
|
19
|
$fp = fopen('php://stdin', 'r');
|
20
|
|
21
|
echo ".\n\n";
|
22
|
|
23
|
$shell_active = true;
|
24
|
|
25
|
echo "1) Update from a URL\n";
|
26
|
echo "2) Update from a local file\n";
|
27
|
echo "Q) Quit\n";
|
28
|
|
29
|
echo "\nPlease select an option to continue: ";
|
30
|
|
31
|
$command = strtoupper(chop(fgets($fp)));
|
32
|
|
33
|
switch ($command) {
|
34
|
case "q":
|
35
|
case "quit":
|
36
|
echo "\n";
|
37
|
fclose($fp);
|
38
|
die;
|
39
|
break;
|
40
|
case "1":
|
41
|
echo "\nEnter the URL to the .tgz update file:\n> ";
|
42
|
$url = chop(fgets($fp));
|
43
|
if(!$url) {
|
44
|
fclose($fp);
|
45
|
die;
|
46
|
}
|
47
|
$status = does_url_exist($url);
|
48
|
if($status) {
|
49
|
conf_mount_rw();
|
50
|
mark_subsystem_dirty('firmware');
|
51
|
if(file_exists("/root/firmware.tgz"))
|
52
|
unlink("/root/firmware.tgz");
|
53
|
echo "\nFetching file size...\n";
|
54
|
$file_size = exec("fetch -s \"$url\"");
|
55
|
$file_size = trim($file_size, "\r");
|
56
|
echo "\nFile size: $file_size\n";
|
57
|
echo "\nFetching file...\n";
|
58
|
exec("fetch -1 -w15 -a -v -o /root/firmware.tgz \"$url\"");
|
59
|
if($file_size <> filesize("/root/firmware.tgz")) {
|
60
|
echo "\nFile size mismatch. Upgrade cancelled.\n\n";
|
61
|
fclose($fp);
|
62
|
die;
|
63
|
}
|
64
|
if(!file_exists("/root/firmware.tgz")) {
|
65
|
echo "Something went wrong during file transfer. Exiting.\n\n";
|
66
|
fclose($fp);
|
67
|
die;
|
68
|
}
|
69
|
$status = does_url_exist("$url.md5");
|
70
|
if($status) {
|
71
|
echo "\nFetching MD5...\n";
|
72
|
exec("fetch -1 -w15 -a -v -o /root/firmware.tgz.md5 \"$url.md5\"");
|
73
|
} else {
|
74
|
echo "\n\nWARNING.\n";
|
75
|
echo "\nCould not locate a MD5 file. We cannot verify the download once its done.\n\n";
|
76
|
sleep(15);
|
77
|
}
|
78
|
if(file_exists("/root/firmware.tgz.md5")) {
|
79
|
$source_md5 = trim(`cat /root/firmware.tgz.md5 | awk '{ print \$4 }'`,"\r");
|
80
|
$file_md5 = trim(`md5 /root/firmware.tgz | awk '{ print \$4 }'`,"\r");
|
81
|
echo "URL MD5: $source_md5\n";
|
82
|
echo "Downloaded file MD5: $file_md5\n";
|
83
|
if($source_md5 <> $file_md5) {
|
84
|
echo "\n\nMD5 checksum does not match. Cancelling upgrade.\n\n";
|
85
|
exec("rm -f /root/*.md5");
|
86
|
fclose($fp);
|
87
|
die -1;
|
88
|
}
|
89
|
echo "\nMD5 checksum matches.\n";
|
90
|
exec("rm -f /root/*.md5");
|
91
|
}
|
92
|
if(strstr($url,"bdiff")) {
|
93
|
echo "Binary DIFF upgrade file detected...\n";
|
94
|
$type = "bdiff";
|
95
|
}
|
96
|
if(strstr($url,"nanobsd")) {
|
97
|
echo "NanoBSD upgrade file detected...\n";
|
98
|
$type = "nanobsd";
|
99
|
}
|
100
|
if(file_exists("/root/firmware.tgz")) {
|
101
|
$type = "normal";
|
102
|
do_upgrade("/root/firmware.tgz", $type);
|
103
|
exit;
|
104
|
} else {
|
105
|
echo "\nCould not download update.\n\n";
|
106
|
fclose($fp);
|
107
|
die -1;
|
108
|
}
|
109
|
}
|
110
|
case "2":
|
111
|
echo "\nEnter the complete path to the .tgz update file: ";
|
112
|
$path = chop(fgets($fp));
|
113
|
if(!$path) {
|
114
|
fclose($fp);
|
115
|
die;
|
116
|
}
|
117
|
if(stristr($path,"bdiff"))
|
118
|
$type = "bdiff";
|
119
|
if(stristr($path,"nanobsd"))
|
120
|
$type = "nanobsd";
|
121
|
if(file_exists($path)) {
|
122
|
mark_subsystem_dirty('firmware');
|
123
|
do_upgrade($path, $type);
|
124
|
} else {
|
125
|
echo "\nCould not find file.\n\n";
|
126
|
fclose($fp);
|
127
|
die -1;
|
128
|
}
|
129
|
}
|
130
|
|
131
|
function check_for_kernel_file() {
|
132
|
global $fp;
|
133
|
$platform = file_get_contents("/etc/platform");
|
134
|
$platform = str_replace("\n", "", $platform);
|
135
|
$platform = str_replace("\r", "", $platform);
|
136
|
if($platform == "embedded" or $platform == "wrap" or $platform == "nanobsd") {
|
137
|
exec("echo wrap > /boot/kernel/pfsense_kernel.txt");
|
138
|
echo "\n";
|
139
|
return;
|
140
|
}
|
141
|
if(!file_exists("/boot/kernel/pfsense_kernel.txt")) {
|
142
|
echo "\nPlease select which kernel you would like to use:\n";
|
143
|
echo "\n1. Non SMP kernel";
|
144
|
echo "\n2. SMP kernel";
|
145
|
echo "\n3. Embedded kernel (no video or keyboard)";
|
146
|
echo "\n4. Developers kernel (slower performing, more debugging)\n";
|
147
|
echo "\nPlease enter a number [1-4]: ";
|
148
|
$selection = strtoupper(chop(fgets($fp)));
|
149
|
switch ($selection) {
|
150
|
case "1":
|
151
|
exec("echo UP > /boot/kernel/pfsense_kernel.txt");
|
152
|
break;
|
153
|
case "2":
|
154
|
exec("echo SMP > /boot/kernel/pfsense_kernel.txt");
|
155
|
break;
|
156
|
case "3":
|
157
|
exec("echo wrap > /boot/kernel/pfsense_kernel.txt");
|
158
|
break;
|
159
|
case "4":
|
160
|
exec("echo Developers > /boot/kernel/pfsense_kernel.txt");
|
161
|
break;
|
162
|
}
|
163
|
echo "\n";
|
164
|
}
|
165
|
}
|
166
|
|
167
|
function do_upgrade($path, $type) {
|
168
|
global $g, $fp;
|
169
|
|
170
|
$sigchk = verify_digital_signature($path);
|
171
|
if ($sigchk == 1)
|
172
|
$sig_warning = "The digital signature on this image is invalid.";
|
173
|
else if ($sigchk == 2)
|
174
|
$sig_warning = "This image is not digitally signed.";
|
175
|
else if (($sigchk == 3) || ($sigchk == 4))
|
176
|
$sig_warning = "There has been an error verifying the signature on this image.";
|
177
|
if($sig_warning) {
|
178
|
$sig_warning = "\nWARNING! ACHTUNG! DANGER!\n\n{$sig_warning}\n\n" .
|
179
|
"This means that the image you uploaded is not an official/supported image and\n" .
|
180
|
"may lead to unexpected behavior or security compromises.\n\n" .
|
181
|
"Only install images that come from sources that you trust, and make sure\n".
|
182
|
"that the image has not been tampered with.\n\n".
|
183
|
"Do you want to install this image anyway at your own risk [n]?";
|
184
|
echo $sig_warning;
|
185
|
$command = strtoupper(chop(fgets($fp)));
|
186
|
if(strtoupper($command) == "Y" or strtoupper($command) == "Y" or strtoupper($command) == "YES") {
|
187
|
echo "\nContinuing upgrade...";
|
188
|
} else {
|
189
|
echo "\nUpgrade cancelled.\n\n";
|
190
|
die;
|
191
|
}
|
192
|
}
|
193
|
mark_subsystem_dirty('firmwarelock');
|
194
|
check_for_kernel_file();
|
195
|
echo "\nOne moment please...\nInvoking firmware upgrade...";
|
196
|
if($type == "bdiff")
|
197
|
mwexec_bg("/etc/rc.firmware delta_update $path");
|
198
|
elseif($type == "nanobsd")
|
199
|
mwexec_bg("/etc/rc.firmware pfSenseNanoBSDupgrade $path");
|
200
|
else
|
201
|
mwexec_bg("/etc/rc.firmware pfSenseupgrade $path");
|
202
|
sleep(10);
|
203
|
while(is_subsystem_dirty('firmwarelock')) {
|
204
|
sleep(1);
|
205
|
echo ".";
|
206
|
}
|
207
|
sleep(10);
|
208
|
echo "Done. Rebooting...\n\n";
|
209
|
clear_subsystem_dirty('firmwarelock');
|
210
|
}
|
211
|
|
212
|
exec("rm -f /root/*.md5");
|
213
|
fclose($fp);
|
214
|
|
215
|
?>
|