Project

General

Profile

Download (6.15 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.sha256");
91
			if($status) { 
92
				echo "\nFetching sha256...\n";
93
				exec("fetch -1 -w15 -a -v -o /root/firmware.tgz.sha256 \"$url.sha256\"");
94
			} else {
95
				echo "\n\nWARNING.\n";
96
				echo "\nCould not locate a sha256 file.  We cannot verify the download once completed.\n\n";
97
				sleep(15);
98
			}
99
			if(file_exists("/root/firmware.tgz.sha256")) {
100
				$source_sha256 = trim(`cat /root/firmware.tgz.sha256 | awk '{ print \$4 }'`,"\r");
101
				$file_sha256 = trim(`sha256 /root/firmware.tgz | awk '{ print \$4 }'`,"\r");
102
				echo "URL sha256: $source_sha256\n";
103
				echo "Downloaded file sha256: $file_sha256\n";
104
				if($source_sha256 <> $file_sha256) {
105
					echo "\n\nsha256 checksum does not match.  Cancelling upgrade.\n\n";
106
					exec("rm -f /root/*.sha256");
107
					fclose($fp);
108
					die -1;
109
				}
110
				echo "\nsha256 checksum matches.\n";
111
				exec("rm -f /root/*.sha256");
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. Standard Kernel";
159
		echo "\n2. Embedded kernel (no video or keyboard)";
160
		echo "\nPlease enter a number [1-2]: ";
161
		$selection = strtoupper(chop(fgets($fp)));
162
		switch ($selection) {
163
			case "1":
164
				exec("echo SMP > /boot/kernel/pfsense_kernel.txt");
165
			break;
166
			case "2":
167
				exec("echo wrap > /boot/kernel/pfsense_kernel.txt");
168
			break;
169
		}
170
		echo "\n";
171
	}
172
}
173

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

    
219
exec("rm -f /root/*.sha256");
220
fclose($fp);
221

    
222
?>
(62-62/110)