Project

General

Profile

Download (6.15 KB) Statistics
| Branch: | Tag: | Revision:
1 f4feb493 Scott Ullrich
#!/usr/local/bin/php -f
2
3
<?php
4
5 5b6eac01 Scott Ullrich
require("globals.inc");
6 45e63af1 Scott Ullrich
require("config.inc");
7
require("functions.inc");
8 5f2d078e Scott Ullrich
9 a368a026 Ermal Lu?i
$g['booting'] = true;
10 5b6eac01 Scott Ullrich
11
echo "Starting the {$g['product_name']} console firmware update system";
12
13 f4feb493 Scott Ullrich
require("functions.inc");
14
echo ".";
15
16 a368a026 Ermal Lu?i
$g['booting'] = false;
17 f502151d Scott Ullrich
18 b043503a jim-p
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 f4feb493 Scott Ullrich
$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
$command = strtoupper(chop(fgets($fp)));
50
51
switch ($command) {
52
	case "q":
53
	case "quit":
54
		echo "\n";
55 31210184 Scott Ullrich
		fclose($fp);
56 f4feb493 Scott Ullrich
		die;
57
	break;
58
	case "1":
59 b043503a jim-p
		echo "\nEnter the URL to the .tgz or .img.gz update file. \nType 'auto' to use {$autoupdateurl}\n> ";
60 f4feb493 Scott Ullrich
		$url = chop(fgets($fp));
61 31210184 Scott Ullrich
		if(!$url) { 
62
			fclose($fp);
63 9b5d2d24 Scott Ullrich
			die;
64 31210184 Scott Ullrich
		}
65 b043503a jim-p
		if($url == "auto") {
66
			$url = $autoupdateurl;
67
		}
68 f4feb493 Scott Ullrich
		$status = does_url_exist($url);
69
		if($status) {
70 8e57ecdf Scott Ullrich
			conf_mount_rw();
71 a368a026 Ermal Lu?i
			mark_subsystem_dirty('firmware');
72 60b7af16 Scott Ullrich
			if(file_exists("/root/firmware.tgz"))
73
				unlink("/root/firmware.tgz");
74 e1f1691f Scott Ullrich
			echo "\nFetching file size...\n";
75
			$file_size = exec("fetch -s \"$url\"");
76
			$file_size = trim($file_size, "\r");
77
			echo "\nFile size: $file_size\n";
78 1d2ac3a5 Scott Ullrich
			echo "\nFetching file...\n";
79 1d2cc906 Scott Ullrich
			exec("fetch -1 -w15 -a -v -o /root/firmware.tgz \"$url\"");
80
			if($file_size <> filesize("/root/firmware.tgz")) {
81 e1f1691f Scott Ullrich
				echo "\nFile size mismatch.  Upgrade cancelled.\n\n";
82 31210184 Scott Ullrich
				fclose($fp);
83 e1f1691f Scott Ullrich
				die;
84
			}			
85 1d2cc906 Scott Ullrich
			if(!file_exists("/root/firmware.tgz")) {
86 e1f1691f Scott Ullrich
				echo "Something went wrong during file transfer.  Exiting.\n\n";
87 31210184 Scott Ullrich
				fclose($fp);
88 e1f1691f Scott Ullrich
				die;
89
			}
90 2ab2d8fb PiBa-NL
			$status = does_url_exist("$url.sha256");
91 1d2ac3a5 Scott Ullrich
			if($status) { 
92 2ab2d8fb PiBa-NL
				echo "\nFetching sha256...\n";
93
				exec("fetch -1 -w15 -a -v -o /root/firmware.tgz.sha256 \"$url.sha256\"");
94 4cc6345e Scott Ullrich
			} else {
95
				echo "\n\nWARNING.\n";
96 2ab2d8fb PiBa-NL
				echo "\nCould not locate a sha256 file.  We cannot verify the download once completed.\n\n";
97 4cc6345e Scott Ullrich
				sleep(15);
98 1d2ac3a5 Scott Ullrich
			}
99 2ab2d8fb PiBa-NL
			if(file_exists("/root/firmware.tgz.sha256")) {
100
				$source_sha256 = trim(`cat /root/firmware.tgz.sha256 | awk '{ print \$4 }'`,"\r");
101
				$file_sha256 = trim(`sha256 /root/firmware.tgz | awk '{ print \$4 }'`,"\r");
102
				echo "URL sha256: $source_sha256\n";
103
				echo "Downloaded file sha256: $file_sha256\n";
104
				if($source_sha256 <> $file_sha256) {
105
					echo "\n\nsha256 checksum does not match.  Cancelling upgrade.\n\n";
106
					exec("rm -f /root/*.sha256");
107 31210184 Scott Ullrich
					fclose($fp);
108 1d2ac3a5 Scott Ullrich
					die -1;
109
				}
110 2ab2d8fb PiBa-NL
				echo "\nsha256 checksum matches.\n";
111
				exec("rm -f /root/*.sha256");
112 1d2ac3a5 Scott Ullrich
			}
113 285b3e3f Scott Ullrich
			if(strstr($url,"bdiff")) {
114
				echo "Binary DIFF upgrade file detected...\n";
115 0a453188 Scott Ullrich
				$type = "bdiff";
116 873b2f0c jim-p
			} elseif(strstr($url,"nanobsd")) {
117
				echo "NanoBSD upgrade file detected...\n";
118
				$type = "nanobsd";
119 d2fdbd6f Scott Ullrich
			} else {
120 873b2f0c jim-p
				$type = "normal";
121 9875b318 Scott Ullrich
			}
122 873b2f0c jim-p
			do_upgrade("/root/firmware.tgz", $type);
123
			exit;
124 11da95a6 Scott Ullrich
		}
125 f4feb493 Scott Ullrich
	case "2":
126 873b2f0c jim-p
		echo "\nEnter the complete path to the .tgz or .img.gz update file: ";
127 f4feb493 Scott Ullrich
		$path = chop(fgets($fp));
128 31210184 Scott Ullrich
		if(!$path) {
129
			fclose($fp);
130 9b5d2d24 Scott Ullrich
			die;
131 31210184 Scott Ullrich
		}
132 3c043ca0 Scott Ullrich
		if(stristr($path,"bdiff"))
133 e1aea2cd Scott Ullrich
			$type = "bdiff";
134 3c043ca0 Scott Ullrich
		if(stristr($path,"nanobsd"))
135 0a453188 Scott Ullrich
			$type = "nanobsd";			
136 f4feb493 Scott Ullrich
		if(file_exists($path)) {
137 a368a026 Ermal Lu?i
			mark_subsystem_dirty('firmware');
138 e1aea2cd Scott Ullrich
			do_upgrade($path, $type);
139 f4feb493 Scott Ullrich
		} else {
140
			echo "\nCould not find file.\n\n";
141 31210184 Scott Ullrich
			fclose($fp);
142 f4feb493 Scott Ullrich
			die -1;
143
		}
144
}
145
146 92d8801f Scott Ullrich
function check_for_kernel_file() {
147 00392a13 Scott Ullrich
	global $fp;
148 9daeb964 Scott Ullrich
	$platform = file_get_contents("/etc/platform");
149
	$platform = str_replace("\n", "", $platform);
150
	$platform = str_replace("\r", "", $platform);
151 227811a4 Scott Ullrich
	if($platform == "embedded" or $platform == "wrap" or $platform == "nanobsd") {
152 9daeb964 Scott Ullrich
		exec("echo wrap > /boot/kernel/pfsense_kernel.txt");
153
		echo "\n";
154
		return;
155
	}	
156 92d8801f Scott Ullrich
	if(!file_exists("/boot/kernel/pfsense_kernel.txt")) {
157
		echo "\nPlease select which kernel you would like to use:\n";
158 bff167fd jim-p
		echo "\n1. Standard Kernel";
159
		echo "\n2. Embedded kernel (no video or keyboard)";
160 9c61a345 jim-p
		echo "\nPlease enter a number [1-2]: ";
161 92d8801f Scott Ullrich
		$selection = strtoupper(chop(fgets($fp)));
162
		switch ($selection) {
163
			case "1":
164
				exec("echo SMP > /boot/kernel/pfsense_kernel.txt");
165
			break;
166 bff167fd jim-p
			case "2":
167 92d8801f Scott Ullrich
				exec("echo wrap > /boot/kernel/pfsense_kernel.txt");
168
			break;
169
		}
170
		echo "\n";
171
	}
172
}
173
174 e1aea2cd Scott Ullrich
function do_upgrade($path, $type) {
175 71ed7da5 Scott Ullrich
	global $g, $fp;
176
	
177
	$sigchk = verify_digital_signature($path);
178
	if ($sigchk == 1)
179
		$sig_warning = "The digital signature on this image is invalid.";
180
	else if ($sigchk == 2)
181
		$sig_warning = "This image is not digitally signed.";
182
	else if (($sigchk == 3) || ($sigchk == 4))
183
		$sig_warning = "There has been an error verifying the signature on this image.";
184
	if($sig_warning) {
185 dbbc0fc8 Scott Ullrich
		$sig_warning = "\nWARNING! ACHTUNG! DANGER!\n\n{$sig_warning}\n\n" .
186 71ed7da5 Scott Ullrich
			"This means that the image you uploaded is not an official/supported image and\n" .
187
			"may lead to unexpected behavior or security compromises.\n\n" .
188
			"Only install images that come from sources that you trust, and make sure\n".
189
			"that the image has not been tampered with.\n\n".
190
			"Do you want to install this image anyway at your own risk [n]?";
191
		echo $sig_warning;
192
		$command = strtoupper(chop(fgets($fp)));
193
		if(strtoupper($command) == "Y" or strtoupper($command) == "Y" or strtoupper($command) == "YES") {
194 7615c951 Scott Ullrich
			echo "\nContinuing upgrade...";
195 71ed7da5 Scott Ullrich
		} else {
196
			echo "\nUpgrade cancelled.\n\n";
197
			die;
198
		}
199
	}
200 a368a026 Ermal Lu?i
	mark_subsystem_dirty('firmwarelock');
201 92d8801f Scott Ullrich
	check_for_kernel_file();
202 dfd6af96 Scott Ullrich
	echo "\nOne moment please...\nInvoking firmware upgrade...";
203 873b2f0c jim-p
	if($type == "bdiff")
204 6e75ac00 Scott Ullrich
		mwexec_bg("/etc/rc.firmware delta_update $path");
205 873b2f0c jim-p
	elseif($type == "nanobsd")
206 9fab6391 Scott Ullrich
		mwexec_bg("/etc/rc.firmware pfSenseNanoBSDupgrade $path");
207
	else
208 6e75ac00 Scott Ullrich
		mwexec_bg("/etc/rc.firmware pfSenseupgrade $path");
209 b30f4ba1 Scott Ullrich
	sleep(10);
210 a368a026 Ermal Lu?i
	while(is_subsystem_dirty('firmwarelock')) {
211 6e75ac00 Scott Ullrich
		sleep(1);
212 dfd6af96 Scott Ullrich
		echo ".";
213
	}
214 1a4075a9 Scott Ullrich
	sleep(10);
215 6e75ac00 Scott Ullrich
	echo "Done.  Rebooting...\n\n";
216 a368a026 Ermal Lu?i
	clear_subsystem_dirty('firmwarelock');
217 f4feb493 Scott Ullrich
}
218
219 2ab2d8fb PiBa-NL
exec("rm -f /root/*.sha256");
220 1939b5ce Scott Ullrich
fclose($fp);
221
222 ce387866 Ermal
?>