Project

General

Profile

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