Project

General

Profile

Download (5.79 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
$fp = fopen('php://stdin', 'r');
17

    
18
echo ".\n\n";
19

    
20
$shell_active = true;
21

    
22
echo "1) Update from a URL\n";
23
echo "2) Update from a local file\n";
24
echo "Q) Quit\n";
25

    
26
echo "\nPlease select an option to continue: ";
27

    
28
$command = strtoupper(chop(fgets($fp)));
29

    
30
switch ($command) {
31
	case "q":
32
	case "quit":
33
		echo "\n";
34
		fclose($fp);
35
		die;
36
	break;
37
	case "1":
38
		echo "\nEnter the URL to the .tgz update file:\n> ";
39
		$url = chop(fgets($fp));
40
		if(!$url) { 
41
			fclose($fp);
42
			die;
43
		}
44
		$status = does_url_exist($url);
45
		if($status) {
46
			conf_mount_rw();
47
			mark_subsystem_dirty('firmware');
48
			if(file_exists("/root/firmware.tgz"))
49
				unlink("/root/firmware.tgz");
50
			echo "\nFetching file size...\n";
51
			$file_size = exec("fetch -s \"$url\"");
52
			$file_size = trim($file_size, "\r");
53
			echo "\nFile size: $file_size\n";
54
			echo "\nFetching file...\n";
55
			exec("fetch -1 -w15 -a -v -o /root/firmware.tgz \"$url\"");
56
			if($file_size <> filesize("/root/firmware.tgz")) {
57
				echo "\nFile size mismatch.  Upgrade cancelled.\n\n";
58
				fclose($fp);
59
				die;
60
			}			
61
			if(!file_exists("/root/firmware.tgz")) {
62
				echo "Something went wrong during file transfer.  Exiting.\n\n";
63
				fclose($fp);
64
				die;
65
			}
66
			$status = does_url_exist("$url.md5");
67
			if($status) { 
68
				echo "\nFetching MD5...\n";
69
				exec("fetch -1 -w15 -a -v -o /root/firmware.tgz.md5 \"$url.md5\"");
70
			} else {
71
				echo "\n\nWARNING.\n";
72
				echo "\nCould not locate a MD5 file.  We cannot verify the download once its done.\n\n";
73
				sleep(15);
74
			}
75
			if(file_exists("/root/firmware.tgz.md5")) {
76
				$source_md5 = trim(`cat /root/firmware.tgz.md5 | awk '{ print \$4 }'`,"\r");
77
				$file_md5 = trim(`md5 /root/firmware.tgz | awk '{ print \$4 }'`,"\r");
78
				echo "URL MD5: $source_md5\n";
79
				echo "Downloaded file MD5: $file_md5\n";
80
				if($source_md5 <> $file_md5) {
81
					echo "\n\nMD5 checksum does not match.  Cancelling upgrade.\n\n";
82
					exec("rm -f /root/*.md5");
83
					fclose($fp);
84
					die -1;
85
				}
86
				echo "\nMD5 checksum matches.\n";
87
				exec("rm -f /root/*.md5");
88
			}
89
			if(strstr($url,"bdiff")) {
90
				echo "Binary DIFF upgrade file detected...\n";
91
				$type = "bdiff";
92
			}
93
			if(strstr($url,"nanobsd")) {
94
				echo "NanoBSD upgrade file detected...\n";			
95
				$type = "nanobsd";	
96
			}			
97
			if(file_exists("/root/firmware.tgz")) {
98
				$type = "normal";	
99
				do_upgrade("/root/firmware.tgz", $type);
100
				exit;
101
			} else {
102
				echo "\nCould not download update.\n\n";
103
				fclose($fp);
104
				die -1;
105
			}
106
		}
107
	case "2":
108
		echo "\nEnter the complete path to the .tgz update file: ";
109
		$path = chop(fgets($fp));
110
		if(!$path) {
111
			fclose($fp);
112
			die;
113
		}
114
		if(stristr($path,"bdiff"))
115
			$type = "bdiff";
116
		if(stristr($path,"nanobsd"))
117
			$type = "nanobsd";			
118
		if(file_exists($path)) {
119
			mark_subsystem_dirty('firmware');
120
			do_upgrade($path, $type);
121
		} else {
122
			echo "\nCould not find file.\n\n";
123
			fclose($fp);
124
			die -1;
125
		}
126
}
127

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

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

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

    
212
?>
(51-51/89)