Project

General

Profile

Download (5.71 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 or .img.gz 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
			} elseif(strstr($url,"nanobsd")) {
95
				echo "NanoBSD upgrade file detected...\n";
96
				$type = "nanobsd";
97
			} else {
98
				$type = "normal";
99
			}
100
			do_upgrade("/root/firmware.tgz", $type);
101
			exit;
102
		}
103
	case "2":
104
		echo "\nEnter the complete path to the .tgz or .img.gz update file: ";
105
		$path = chop(fgets($fp));
106
		if(!$path) {
107
			fclose($fp);
108
			die;
109
		}
110
		if(stristr($path,"bdiff"))
111
			$type = "bdiff";
112
		if(stristr($path,"nanobsd"))
113
			$type = "nanobsd";			
114
		if(file_exists($path)) {
115
			mark_subsystem_dirty('firmware');
116
			do_upgrade($path, $type);
117
		} else {
118
			echo "\nCould not find file.\n\n";
119
			fclose($fp);
120
			die -1;
121
		}
122
}
123

    
124
function check_for_kernel_file() {
125
	global $fp;
126
	$platform = file_get_contents("/etc/platform");
127
	$platform = str_replace("\n", "", $platform);
128
	$platform = str_replace("\r", "", $platform);
129
	if($platform == "embedded" or $platform == "wrap" or $platform == "nanobsd") {
130
		exec("echo wrap > /boot/kernel/pfsense_kernel.txt");
131
		echo "\n";
132
		return;
133
	}	
134
	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
		echo "\n4. Developers kernel (slower performing, more debugging)\n";
140
		echo "\nPlease enter a number [1-4]: ";
141
		$selection = strtoupper(chop(fgets($fp)));
142
		switch ($selection) {
143
			case "1":
144
				exec("echo UP > /boot/kernel/pfsense_kernel.txt");
145
			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
function do_upgrade($path, $type) {
161
	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
		$sig_warning = "\nWARNING! ACHTUNG! DANGER!\n\n{$sig_warning}\n\n" .
172
			"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
			echo "\nContinuing upgrade...";
181
		} else {
182
			echo "\nUpgrade cancelled.\n\n";
183
			die;
184
		}
185
	}
186
	mark_subsystem_dirty('firmwarelock');
187
	check_for_kernel_file();
188
	echo "\nOne moment please...\nInvoking firmware upgrade...";
189
	if($type == "bdiff")
190
		mwexec_bg("/etc/rc.firmware delta_update $path");
191
	elseif($type == "nanobsd")
192
		mwexec_bg("/etc/rc.firmware pfSenseNanoBSDupgrade $path");
193
	else
194
		mwexec_bg("/etc/rc.firmware pfSenseupgrade $path");
195
	sleep(10);
196
	while(is_subsystem_dirty('firmwarelock')) {
197
		sleep(1);
198
		echo ".";
199
	}
200
	sleep(10);
201
	echo "Done.  Rebooting...\n\n";
202
	clear_subsystem_dirty('firmwarelock');
203
}
204

    
205
exec("rm -f /root/*.md5");
206
fclose($fp);
207

    
208
?>
(53-53/94)