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("config.inc");
7
require("functions.inc");
8

    
9
$g['booting'] = true;
10

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

    
13
require("functions.inc");
14
echo ".";
15

    
16
$g['booting'] = false;
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
			mark_subsystem_dirty('firmware');
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
				$type = "normal";	
101
				do_upgrade("/root/firmware.tgz", $type);
102
				exit;
103
			} else {
104
				echo "\nCould not download update.\n\n";
105
				fclose($fp);
106
				die -1;
107
			}
108
		}
109
	case "2":
110
		echo "\nEnter the complete path to the .tgz update file: ";
111
		$path = chop(fgets($fp));
112
		if(!$path) {
113
			fclose($fp);
114
			die;
115
		}
116
		if(stristr($path,"bdiff"))
117
			$type = "bdiff";
118
		if(stristr($path,"nanobsd"))
119
			$type = "nanobsd";			
120
		if(file_exists($path)) {
121
			mark_subsystem_dirty('firmware');
122
			do_upgrade($path, $type);
123
		} else {
124
			echo "\nCould not find file.\n\n";
125
			fclose($fp);
126
			die -1;
127
		}
128
}
129

    
130
function check_for_kernel_file() {
131
	global $fp;
132
	$platform = file_get_contents("/etc/platform");
133
	$platform = str_replace("\n", "", $platform);
134
	$platform = str_replace("\r", "", $platform);
135
	if($platform == "embedded" or $platform == "wrap" or $platform == "nanobsd") {
136
		exec("echo wrap > /boot/kernel/pfsense_kernel.txt");
137
		echo "\n";
138
		return;
139
	}	
140
	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
		echo "\n4. Developers kernel (slower performing, more debugging)\n";
146
		echo "\nPlease enter a number [1-4]: ";
147
		$selection = strtoupper(chop(fgets($fp)));
148
		switch ($selection) {
149
			case "1":
150
				exec("echo UP > /boot/kernel/pfsense_kernel.txt");
151
			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
function do_upgrade($path, $type) {
167
	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
		$sig_warning = "\nWARNING! ACHTUNG! DANGER!\n\n{$sig_warning}\n\n" .
178
			"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
			echo "\nContinuing upgrade...";
187
		} else {
188
			echo "\nUpgrade cancelled.\n\n";
189
			die;
190
		}
191
	}
192
	mark_subsystem_dirty('firmwarelock');
193
	check_for_kernel_file();
194
	echo "\nOne moment please...\nInvoking firmware upgrade...";
195
	if($type == "bdiff") 
196
		mwexec_bg("/etc/rc.firmware delta_update $path");
197
	elseif($type == "nanobsd") 
198
		mwexec_bg("/etc/rc.firmware pfSenseNanoBSDupgrade $path");
199
	else
200
		mwexec_bg("/etc/rc.firmware pfSenseupgrade $path");
201
	sleep(10);
202
	while(is_subsystem_dirty('firmwarelock')) {
203
		sleep(1);
204
		echo ".";
205
	}
206
	sleep(10);
207
	echo "Done.  Rebooting...\n\n";
208
	clear_subsystem_dirty('firmwarelock');
209
}
210

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

    
214
?>
(52-52/93)