Project

General

Profile

Download (5.71 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 873b2f0c jim-p
		echo "\nEnter the URL to the .tgz or .img.gz update file:\n> ";
41 f4feb493 Scott Ullrich
		$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 873b2f0c jim-p
			} elseif(strstr($url,"nanobsd")) {
95
				echo "NanoBSD upgrade file detected...\n";
96
				$type = "nanobsd";
97 d2fdbd6f Scott Ullrich
			} else {
98 873b2f0c jim-p
				$type = "normal";
99 9875b318 Scott Ullrich
			}
100 873b2f0c jim-p
			do_upgrade("/root/firmware.tgz", $type);
101
			exit;
102 11da95a6 Scott Ullrich
		}
103 f4feb493 Scott Ullrich
	case "2":
104 873b2f0c jim-p
		echo "\nEnter the complete path to the .tgz or .img.gz update file: ";
105 f4feb493 Scott Ullrich
		$path = chop(fgets($fp));
106 31210184 Scott Ullrich
		if(!$path) {
107
			fclose($fp);
108 9b5d2d24 Scott Ullrich
			die;
109 31210184 Scott Ullrich
		}
110 3c043ca0 Scott Ullrich
		if(stristr($path,"bdiff"))
111 e1aea2cd Scott Ullrich
			$type = "bdiff";
112 3c043ca0 Scott Ullrich
		if(stristr($path,"nanobsd"))
113 0a453188 Scott Ullrich
			$type = "nanobsd";			
114 f4feb493 Scott Ullrich
		if(file_exists($path)) {
115 a368a026 Ermal Lu?i
			mark_subsystem_dirty('firmware');
116 e1aea2cd Scott Ullrich
			do_upgrade($path, $type);
117 f4feb493 Scott Ullrich
		} else {
118
			echo "\nCould not find file.\n\n";
119 31210184 Scott Ullrich
			fclose($fp);
120 f4feb493 Scott Ullrich
			die -1;
121
		}
122
}
123
124 92d8801f Scott Ullrich
function check_for_kernel_file() {
125 00392a13 Scott Ullrich
	global $fp;
126 9daeb964 Scott Ullrich
	$platform = file_get_contents("/etc/platform");
127
	$platform = str_replace("\n", "", $platform);
128
	$platform = str_replace("\r", "", $platform);
129 227811a4 Scott Ullrich
	if($platform == "embedded" or $platform == "wrap" or $platform == "nanobsd") {
130 9daeb964 Scott Ullrich
		exec("echo wrap > /boot/kernel/pfsense_kernel.txt");
131
		echo "\n";
132
		return;
133
	}	
134 92d8801f Scott Ullrich
	if(!file_exists("/boot/kernel/pfsense_kernel.txt")) {
135
		echo "\nPlease select which kernel you would like to use:\n";
136
		echo "\n1. Non SMP kernel";
137
		echo "\n2. SMP kernel";
138
		echo "\n3. Embedded kernel (no video or keyboard)";
139 c79e6edf Scott Ullrich
		echo "\n4. Developers kernel (slower performing, more debugging)\n";
140 92d8801f Scott Ullrich
		echo "\nPlease enter a number [1-4]: ";
141
		$selection = strtoupper(chop(fgets($fp)));
142
		switch ($selection) {
143
			case "1":
144 b3191bd2 Scott Ullrich
				exec("echo UP > /boot/kernel/pfsense_kernel.txt");
145 92d8801f Scott Ullrich
			break;
146
			case "2":
147
				exec("echo SMP > /boot/kernel/pfsense_kernel.txt");
148
			break;
149
			case "3":
150
				exec("echo wrap > /boot/kernel/pfsense_kernel.txt");
151
			break;
152
			case "4":
153
				exec("echo Developers > /boot/kernel/pfsense_kernel.txt");
154
			break;		
155
		}
156
		echo "\n";
157
	}
158
}
159
160 e1aea2cd Scott Ullrich
function do_upgrade($path, $type) {
161 71ed7da5 Scott Ullrich
	global $g, $fp;
162
	
163
	$sigchk = verify_digital_signature($path);
164
	if ($sigchk == 1)
165
		$sig_warning = "The digital signature on this image is invalid.";
166
	else if ($sigchk == 2)
167
		$sig_warning = "This image is not digitally signed.";
168
	else if (($sigchk == 3) || ($sigchk == 4))
169
		$sig_warning = "There has been an error verifying the signature on this image.";
170
	if($sig_warning) {
171 dbbc0fc8 Scott Ullrich
		$sig_warning = "\nWARNING! ACHTUNG! DANGER!\n\n{$sig_warning}\n\n" .
172 71ed7da5 Scott Ullrich
			"This means that the image you uploaded is not an official/supported image and\n" .
173
			"may lead to unexpected behavior or security compromises.\n\n" .
174
			"Only install images that come from sources that you trust, and make sure\n".
175
			"that the image has not been tampered with.\n\n".
176
			"Do you want to install this image anyway at your own risk [n]?";
177
		echo $sig_warning;
178
		$command = strtoupper(chop(fgets($fp)));
179
		if(strtoupper($command) == "Y" or strtoupper($command) == "Y" or strtoupper($command) == "YES") {
180 7615c951 Scott Ullrich
			echo "\nContinuing upgrade...";
181 71ed7da5 Scott Ullrich
		} else {
182
			echo "\nUpgrade cancelled.\n\n";
183
			die;
184
		}
185
	}
186 a368a026 Ermal Lu?i
	mark_subsystem_dirty('firmwarelock');
187 92d8801f Scott Ullrich
	check_for_kernel_file();
188 dfd6af96 Scott Ullrich
	echo "\nOne moment please...\nInvoking firmware upgrade...";
189 873b2f0c jim-p
	if($type == "bdiff")
190 6e75ac00 Scott Ullrich
		mwexec_bg("/etc/rc.firmware delta_update $path");
191 873b2f0c jim-p
	elseif($type == "nanobsd")
192 9fab6391 Scott Ullrich
		mwexec_bg("/etc/rc.firmware pfSenseNanoBSDupgrade $path");
193
	else
194 6e75ac00 Scott Ullrich
		mwexec_bg("/etc/rc.firmware pfSenseupgrade $path");
195 b30f4ba1 Scott Ullrich
	sleep(10);
196 a368a026 Ermal Lu?i
	while(is_subsystem_dirty('firmwarelock')) {
197 6e75ac00 Scott Ullrich
		sleep(1);
198 dfd6af96 Scott Ullrich
		echo ".";
199
	}
200 1a4075a9 Scott Ullrich
	sleep(10);
201 6e75ac00 Scott Ullrich
	echo "Done.  Rebooting...\n\n";
202 a368a026 Ermal Lu?i
	clear_subsystem_dirty('firmwarelock');
203 f4feb493 Scott Ullrich
}
204
205 7afeb359 Scott Ullrich
exec("rm -f /root/*.md5");
206 1939b5ce Scott Ullrich
fclose($fp);
207
208 ce387866 Ermal
?>