Project

General

Profile

Download (6.34 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 1d2ac3a5 Scott Ullrich
			$status = does_url_exist("$url.md5");
91
			if($status) { 
92
				echo "\nFetching MD5...\n";
93 1d2cc906 Scott Ullrich
				exec("fetch -1 -w15 -a -v -o /root/firmware.tgz.md5 \"$url.md5\"");
94 4cc6345e Scott Ullrich
			} else {
95
				echo "\n\nWARNING.\n";
96 5eeb1d9d Chris Buechler
				echo "\nCould not locate a MD5 file.  We cannot verify the download once completed.\n\n";
97 4cc6345e Scott Ullrich
				sleep(15);
98 1d2ac3a5 Scott Ullrich
			}
99 1d2cc906 Scott Ullrich
			if(file_exists("/root/firmware.tgz.md5")) {
100
				$source_md5 = trim(`cat /root/firmware.tgz.md5 | awk '{ print \$4 }'`,"\r");
101
				$file_md5 = trim(`md5 /root/firmware.tgz | awk '{ print \$4 }'`,"\r");
102 e1f1691f Scott Ullrich
				echo "URL MD5: $source_md5\n";
103
				echo "Downloaded file MD5: $file_md5\n";
104 1d2ac3a5 Scott Ullrich
				if($source_md5 <> $file_md5) {
105
					echo "\n\nMD5 checksum does not match.  Cancelling upgrade.\n\n";
106 7afeb359 Scott Ullrich
					exec("rm -f /root/*.md5");
107 31210184 Scott Ullrich
					fclose($fp);
108 1d2ac3a5 Scott Ullrich
					die -1;
109
				}
110
				echo "\nMD5 checksum matches.\n";
111 7afeb359 Scott Ullrich
				exec("rm -f /root/*.md5");
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
		echo "\n1. Non SMP kernel";
159
		echo "\n2. SMP kernel";
160
		echo "\n3. Embedded kernel (no video or keyboard)";
161 c79e6edf Scott Ullrich
		echo "\n4. Developers kernel (slower performing, more debugging)\n";
162 92d8801f Scott Ullrich
		echo "\nPlease enter a number [1-4]: ";
163
		$selection = strtoupper(chop(fgets($fp)));
164
		switch ($selection) {
165
			case "1":
166 b3191bd2 Scott Ullrich
				exec("echo UP > /boot/kernel/pfsense_kernel.txt");
167 92d8801f Scott Ullrich
			break;
168
			case "2":
169
				exec("echo SMP > /boot/kernel/pfsense_kernel.txt");
170
			break;
171
			case "3":
172
				exec("echo wrap > /boot/kernel/pfsense_kernel.txt");
173
			break;
174
			case "4":
175
				exec("echo Developers > /boot/kernel/pfsense_kernel.txt");
176
			break;		
177
		}
178
		echo "\n";
179
	}
180
}
181
182 e1aea2cd Scott Ullrich
function do_upgrade($path, $type) {
183 71ed7da5 Scott Ullrich
	global $g, $fp;
184
	
185
	$sigchk = verify_digital_signature($path);
186
	if ($sigchk == 1)
187
		$sig_warning = "The digital signature on this image is invalid.";
188
	else if ($sigchk == 2)
189
		$sig_warning = "This image is not digitally signed.";
190
	else if (($sigchk == 3) || ($sigchk == 4))
191
		$sig_warning = "There has been an error verifying the signature on this image.";
192
	if($sig_warning) {
193 dbbc0fc8 Scott Ullrich
		$sig_warning = "\nWARNING! ACHTUNG! DANGER!\n\n{$sig_warning}\n\n" .
194 71ed7da5 Scott Ullrich
			"This means that the image you uploaded is not an official/supported image and\n" .
195
			"may lead to unexpected behavior or security compromises.\n\n" .
196
			"Only install images that come from sources that you trust, and make sure\n".
197
			"that the image has not been tampered with.\n\n".
198
			"Do you want to install this image anyway at your own risk [n]?";
199
		echo $sig_warning;
200
		$command = strtoupper(chop(fgets($fp)));
201
		if(strtoupper($command) == "Y" or strtoupper($command) == "Y" or strtoupper($command) == "YES") {
202 7615c951 Scott Ullrich
			echo "\nContinuing upgrade...";
203 71ed7da5 Scott Ullrich
		} else {
204
			echo "\nUpgrade cancelled.\n\n";
205
			die;
206
		}
207
	}
208 a368a026 Ermal Lu?i
	mark_subsystem_dirty('firmwarelock');
209 92d8801f Scott Ullrich
	check_for_kernel_file();
210 dfd6af96 Scott Ullrich
	echo "\nOne moment please...\nInvoking firmware upgrade...";
211 873b2f0c jim-p
	if($type == "bdiff")
212 6e75ac00 Scott Ullrich
		mwexec_bg("/etc/rc.firmware delta_update $path");
213 873b2f0c jim-p
	elseif($type == "nanobsd")
214 9fab6391 Scott Ullrich
		mwexec_bg("/etc/rc.firmware pfSenseNanoBSDupgrade $path");
215
	else
216 6e75ac00 Scott Ullrich
		mwexec_bg("/etc/rc.firmware pfSenseupgrade $path");
217 b30f4ba1 Scott Ullrich
	sleep(10);
218 a368a026 Ermal Lu?i
	while(is_subsystem_dirty('firmwarelock')) {
219 6e75ac00 Scott Ullrich
		sleep(1);
220 dfd6af96 Scott Ullrich
		echo ".";
221
	}
222 1a4075a9 Scott Ullrich
	sleep(10);
223 6e75ac00 Scott Ullrich
	echo "Done.  Rebooting...\n\n";
224 a368a026 Ermal Lu?i
	clear_subsystem_dirty('firmwarelock');
225 f4feb493 Scott Ullrich
}
226
227 7afeb359 Scott Ullrich
exec("rm -f /root/*.md5");
228 1939b5ce Scott Ullrich
fclose($fp);
229
230 ce387866 Ermal
?>