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