Project

General

Profile

Download (6.34 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
if(isset($config['system']['firmware']['alturl']['enable']))
19
	$updater_url = "{$config['system']['firmware']['alturl']['firmwareurl']}";
20
else
21
	$updater_url = $g['update_url'];
22

    
23
$nanosize = "";
24
if ($g['platform'] == "nanobsd") {
25
	if (file_exists("/etc/nano_use_vga.txt"))
26
		$nanosize = "-nanobsd-vga-";
27
	else
28
		$nanosize = "-nanobsd-";
29

    
30
	$nanosize .= strtolower(trim(file_get_contents("/etc/nanosize.txt")));
31
	$update_filename = "latest{$nanosize}.img.gz";
32
} else {
33
	$update_filename = "latest.tgz";
34
}
35
$autoupdateurl = "{$updater_url}/{$update_filename}";
36

    
37
$fp = fopen('php://stdin', 'r');
38

    
39
echo ".\n\n";
40

    
41
$shell_active = true;
42

    
43
echo "1) Update from a URL\n";
44
echo "2) Update from a local file\n";
45
echo "Q) Quit\n";
46

    
47
echo "\nPlease select an option to continue: ";
48

    
49
$command = strtoupper(chop(fgets($fp)));
50

    
51
switch ($command) {
52
	case "q":
53
	case "quit":
54
		echo "\n";
55
		fclose($fp);
56
		die;
57
	break;
58
	case "1":
59
		echo "\nEnter the URL to the .tgz or .img.gz update file. \nType 'auto' to use {$autoupdateurl}\n> ";
60
		$url = chop(fgets($fp));
61
		if(!$url) { 
62
			fclose($fp);
63
			die;
64
		}
65
		if($url == "auto") {
66
			$url = $autoupdateurl;
67
		}
68
		$status = does_url_exist($url);
69
		if($status) {
70
			conf_mount_rw();
71
			mark_subsystem_dirty('firmware');
72
			if(file_exists("/root/firmware.tgz"))
73
				unlink("/root/firmware.tgz");
74
			echo "\nFetching file size...\n";
75
			$file_size = exec("fetch -s \"$url\"");
76
			$file_size = trim($file_size, "\r");
77
			echo "\nFile size: $file_size\n";
78
			echo "\nFetching file...\n";
79
			exec("fetch -1 -w15 -a -v -o /root/firmware.tgz \"$url\"");
80
			if($file_size <> filesize("/root/firmware.tgz")) {
81
				echo "\nFile size mismatch.  Upgrade cancelled.\n\n";
82
				fclose($fp);
83
				die;
84
			}			
85
			if(!file_exists("/root/firmware.tgz")) {
86
				echo "Something went wrong during file transfer.  Exiting.\n\n";
87
				fclose($fp);
88
				die;
89
			}
90
			$status = does_url_exist("$url.md5");
91
			if($status) { 
92
				echo "\nFetching MD5...\n";
93
				exec("fetch -1 -w15 -a -v -o /root/firmware.tgz.md5 \"$url.md5\"");
94
			} else {
95
				echo "\n\nWARNING.\n";
96
				echo "\nCould not locate a MD5 file.  We cannot verify the download once completed.\n\n";
97
				sleep(15);
98
			}
99
			if(file_exists("/root/firmware.tgz.md5")) {
100
				$source_md5 = trim(`cat /root/firmware.tgz.md5 | awk '{ print \$4 }'`,"\r");
101
				$file_md5 = trim(`md5 /root/firmware.tgz | awk '{ print \$4 }'`,"\r");
102
				echo "URL MD5: $source_md5\n";
103
				echo "Downloaded file MD5: $file_md5\n";
104
				if($source_md5 <> $file_md5) {
105
					echo "\n\nMD5 checksum does not match.  Cancelling upgrade.\n\n";
106
					exec("rm -f /root/*.md5");
107
					fclose($fp);
108
					die -1;
109
				}
110
				echo "\nMD5 checksum matches.\n";
111
				exec("rm -f /root/*.md5");
112
			}
113
			if(strstr($url,"bdiff")) {
114
				echo "Binary DIFF upgrade file detected...\n";
115
				$type = "bdiff";
116
			} elseif(strstr($url,"nanobsd")) {
117
				echo "NanoBSD upgrade file detected...\n";
118
				$type = "nanobsd";
119
			} else {
120
				$type = "normal";
121
			}
122
			do_upgrade("/root/firmware.tgz", $type);
123
			exit;
124
		}
125
	case "2":
126
		echo "\nEnter the complete path to the .tgz or .img.gz update file: ";
127
		$path = chop(fgets($fp));
128
		if(!$path) {
129
			fclose($fp);
130
			die;
131
		}
132
		if(stristr($path,"bdiff"))
133
			$type = "bdiff";
134
		if(stristr($path,"nanobsd"))
135
			$type = "nanobsd";			
136
		if(file_exists($path)) {
137
			mark_subsystem_dirty('firmware');
138
			do_upgrade($path, $type);
139
		} else {
140
			echo "\nCould not find file.\n\n";
141
			fclose($fp);
142
			die -1;
143
		}
144
}
145

    
146
function check_for_kernel_file() {
147
	global $fp;
148
	$platform = file_get_contents("/etc/platform");
149
	$platform = str_replace("\n", "", $platform);
150
	$platform = str_replace("\r", "", $platform);
151
	if($platform == "embedded" or $platform == "wrap" or $platform == "nanobsd") {
152
		exec("echo wrap > /boot/kernel/pfsense_kernel.txt");
153
		echo "\n";
154
		return;
155
	}	
156
	if(!file_exists("/boot/kernel/pfsense_kernel.txt")) {
157
		echo "\nPlease select which kernel you would like to use:\n";
158
		echo "\n1. Non SMP kernel";
159
		echo "\n2. SMP kernel";
160
		echo "\n3. Embedded kernel (no video or keyboard)";
161
		echo "\n4. Developers kernel (slower performing, more debugging)\n";
162
		echo "\nPlease enter a number [1-4]: ";
163
		$selection = strtoupper(chop(fgets($fp)));
164
		switch ($selection) {
165
			case "1":
166
				exec("echo UP > /boot/kernel/pfsense_kernel.txt");
167
			break;
168
			case "2":
169
				exec("echo SMP > /boot/kernel/pfsense_kernel.txt");
170
			break;
171
			case "3":
172
				exec("echo wrap > /boot/kernel/pfsense_kernel.txt");
173
			break;
174
			case "4":
175
				exec("echo Developers > /boot/kernel/pfsense_kernel.txt");
176
			break;		
177
		}
178
		echo "\n";
179
	}
180
}
181

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

    
227
exec("rm -f /root/*.md5");
228
fclose($fp);
229

    
230
?>
(61-61/107)