Project

General

Profile

Download (5.82 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php -f
2

    
3
<?php
4

    
5
require("globals.inc");
6
require("util.inc");
7
$g['booting'] = true;
8

    
9
echo "Starting the {$g['product_name']} console firmware update system";
10

    
11
require("functions.inc");
12
echo ".";
13

    
14
$g['booting'] = false;
15

    
16
$d_fwupenabled_path = $g['varrun_path'] . "/fwup.enabled";
17

    
18
$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
		fclose($fp);
37
		die;
38
	break;
39
	case "1":
40
		echo "\nEnter the URL to the .tgz update file:\n> ";
41
		$url = chop(fgets($fp));
42
		if(!$url) { 
43
			fclose($fp);
44
			die;
45
		}
46
		$status = does_url_exist($url);
47
		if($status) {
48
			conf_mount_rw();
49
			touch($d_fwupenabled_path);
50
			if(file_exists("/root/firmware.tgz"))
51
				unlink("/root/firmware.tgz");
52
			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
			echo "\nFetching file...\n";
57
			exec("fetch -1 -w15 -a -v -o /root/firmware.tgz \"$url\"");
58
			if($file_size <> filesize("/root/firmware.tgz")) {
59
				echo "\nFile size mismatch.  Upgrade cancelled.\n\n";
60
				fclose($fp);
61
				die;
62
			}			
63
			if(!file_exists("/root/firmware.tgz")) {
64
				echo "Something went wrong during file transfer.  Exiting.\n\n";
65
				fclose($fp);
66
				die;
67
			}
68
			$status = does_url_exist("$url.md5");
69
			if($status) { 
70
				echo "\nFetching MD5...\n";
71
				exec("fetch -1 -w15 -a -v -o /root/firmware.tgz.md5 \"$url.md5\"");
72
			} 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
			}
77
			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
				echo "URL MD5: $source_md5\n";
81
				echo "Downloaded file MD5: $file_md5\n";
82
				if($source_md5 <> $file_md5) {
83
					echo "\n\nMD5 checksum does not match.  Cancelling upgrade.\n\n";
84
					exec("rm -f /root/*.md5");
85
					fclose($fp);
86
					die -1;
87
				}
88
				echo "\nMD5 checksum matches.\n";
89
				exec("rm -f /root/*.md5");
90
			}
91
			if(strstr($url,"bdiff")) {
92
				echo "Binary DIFF upgrade file detected...\n";
93
				$type = "bdiff";
94
			}
95
			if(strstr($url,"nanobsd")) {
96
				echo "NanoBSD upgrade file detected...\n";			
97
				$type = "nanobsd";	
98
			}			
99
			if(file_exists("/root/firmware.tgz")) {
100
				do_upgrade("/root/firmware.tgz", $type);
101
				exit;
102
			} else {
103
				echo "\nCould not download update.\n\n";
104
				fclose($fp);
105
				die -1;
106
			}
107
		}
108
	case "2":
109
		echo "\nEnter the complete path to the .tgz update file: ";
110
		$path = chop(fgets($fp));
111
		if(!$path) {
112
			fclose($fp);
113
			die;
114
		}
115
		if(stristr($path,"bdiff"))
116
			$type = "bdiff";
117
		if(stristr($path,"nanobsd"))
118
			$type = "nanobsd";			
119
		if(file_exists($path)) {
120
			touch($d_fwupenabled_path);
121
			do_upgrade($path, $type);
122
		} else {
123
			echo "\nCould not find file.\n\n";
124
			fclose($fp);
125
			die -1;
126
		}
127
}
128

    
129
function check_for_kernel_file() {
130
	global $fp;
131
	$platform = file_get_contents("/etc/platform");
132
	$platform = str_replace("\n", "", $platform);
133
	$platform = str_replace("\r", "", $platform);
134
	if($platform == "embedded" or $platform == "wrap" or $platform == "nanobsd") {
135
		exec("echo wrap > /boot/kernel/pfsense_kernel.txt");
136
		echo "\n";
137
		return;
138
	}	
139
	if(!file_exists("/boot/kernel/pfsense_kernel.txt")) {
140
		echo "\nPlease select which kernel you would like to use:\n";
141
		echo "\n1. Non SMP kernel";
142
		echo "\n2. SMP kernel";
143
		echo "\n3. Embedded kernel (no video or keyboard)";
144
		echo "\n4. Developers kernel (slower performing, more debugging)\n";
145
		echo "\nPlease enter a number [1-4]: ";
146
		$selection = strtoupper(chop(fgets($fp)));
147
		switch ($selection) {
148
			case "1":
149
				exec("echo UP > /boot/kernel/pfsense_kernel.txt");
150
			break;
151
			case "2":
152
				exec("echo SMP > /boot/kernel/pfsense_kernel.txt");
153
			break;
154
			case "3":
155
				exec("echo wrap > /boot/kernel/pfsense_kernel.txt");
156
			break;
157
			case "4":
158
				exec("echo Developers > /boot/kernel/pfsense_kernel.txt");
159
			break;		
160
		}
161
		echo "\n";
162
	}
163
}
164

    
165
function do_upgrade($path, $type) {
166
	global $g, $fp, $d_fwupenabled_path;
167
	
168
	$sigchk = verify_digital_signature($path);
169
	if ($sigchk == 1)
170
		$sig_warning = "The digital signature on this image is invalid.";
171
	else if ($sigchk == 2)
172
		$sig_warning = "This image is not digitally signed.";
173
	else if (($sigchk == 3) || ($sigchk == 4))
174
		$sig_warning = "There has been an error verifying the signature on this image.";
175
	if($sig_warning) {
176
		$sig_warning = "\nWARNING! ACHTUNG! DANGER!\n\n{$sig_warning}\n\n" .
177
			"This means that the image you uploaded is not an official/supported image and\n" .
178
			"may lead to unexpected behavior or security compromises.\n\n" .
179
			"Only install images that come from sources that you trust, and make sure\n".
180
			"that the image has not been tampered with.\n\n".
181
			"Do you want to install this image anyway at your own risk [n]?";
182
		echo $sig_warning;
183
		$command = strtoupper(chop(fgets($fp)));
184
		if(strtoupper($command) == "Y" or strtoupper($command) == "Y" or strtoupper($command) == "YES") {
185
			echo "\nContinuing upgrade...";
186
		} else {
187
			echo "\nUpgrade cancelled.\n\n";
188
			die;
189
		}
190
	}
191
	touch($d_fwupenabled_path);
192
	check_for_kernel_file();
193
	echo "\nOne moment please...\nInvoking firmware upgrade...";
194
	if($type == "bdiff") 
195
		mwexec_bg("/etc/rc.firmware delta_update $path");
196
	elseif($type == "nanobsd") 
197
		mwexec_bg("/etc/rc.firmware pfSenseNanoBSDupgrade $path");
198
	else
199
		mwexec_bg("/etc/rc.firmware pfSenseupgrade $path");
200
	sleep(10);
201
	while(file_exists($d_fwupenabled_path)) {
202
		sleep(1);
203
		echo ".";
204
	}
205
	sleep(10);
206
	echo "Done.  Rebooting...\n\n";
207
	unlink_if_exists($d_fwupenabled_path);
208
}
209

    
210
exec("rm -f /root/*.md5");
211
fclose($fp);
212

    
213
?>
(44-44/81)