Project

General

Profile

Download (4.25 KB) Statistics
| Branch: | Tag: | Revision:
1 f4feb493 Scott Ullrich
#!/usr/local/bin/php -f
2
3
<?php
4
5
echo "Starting the pfSense console firmware update system";
6
7
echo ".";
8
require("globals.inc");
9
$g['booting'] = true;
10
require("functions.inc");
11
echo ".";
12
require("config.inc");
13
echo ".";
14
$g['booting'] = false;
15
16 f502151d Scott Ullrich
$d_fwupenabled_path = $g['varrun_path'] . "/fwup.enabled";
17
18 f4feb493 Scott Ullrich
$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 31210184 Scott Ullrich
		fclose($fp);
37 f4feb493 Scott Ullrich
		die;
38
	break;
39
	case "1":
40
		echo "\nEnter the URL to the .tgz update file:\n> ";
41
		$url = chop(fgets($fp));
42 31210184 Scott Ullrich
		if(!$url) { 
43
			fclose($fp);
44 9b5d2d24 Scott Ullrich
			die;
45 31210184 Scott Ullrich
		}
46 f4feb493 Scott Ullrich
		$status = does_url_exist($url);
47
		if($status) {
48 8e57ecdf Scott Ullrich
			conf_mount_rw();
49 60b7af16 Scott Ullrich
			touch($d_fwupenabled_path);
50
			if(file_exists("/root/firmware.tgz"))
51
				unlink("/root/firmware.tgz");
52 e1f1691f Scott Ullrich
			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 1d2ac3a5 Scott Ullrich
			echo "\nFetching file...\n";
57 1d2cc906 Scott Ullrich
			exec("fetch -1 -w15 -a -v -o /root/firmware.tgz \"$url\"");
58
			if($file_size <> filesize("/root/firmware.tgz")) {
59 e1f1691f Scott Ullrich
				echo "\nFile size mismatch.  Upgrade cancelled.\n\n";
60 31210184 Scott Ullrich
				fclose($fp);
61 e1f1691f Scott Ullrich
				die;
62
			}			
63 1d2cc906 Scott Ullrich
			if(!file_exists("/root/firmware.tgz")) {
64 e1f1691f Scott Ullrich
				echo "Something went wrong during file transfer.  Exiting.\n\n";
65 31210184 Scott Ullrich
				fclose($fp);
66 e1f1691f Scott Ullrich
				die;
67
			}
68 1d2ac3a5 Scott Ullrich
			$status = does_url_exist("$url.md5");
69
			if($status) { 
70
				echo "\nFetching MD5...\n";
71 1d2cc906 Scott Ullrich
				exec("fetch -1 -w15 -a -v -o /root/firmware.tgz.md5 \"$url.md5\"");
72 4cc6345e Scott Ullrich
			} 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 1d2ac3a5 Scott Ullrich
			}
77 1d2cc906 Scott Ullrich
			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 e1f1691f Scott Ullrich
				echo "URL MD5: $source_md5\n";
81
				echo "Downloaded file MD5: $file_md5\n";
82 1d2ac3a5 Scott Ullrich
				if($source_md5 <> $file_md5) {
83
					echo "\n\nMD5 checksum does not match.  Cancelling upgrade.\n\n";
84 7afeb359 Scott Ullrich
					exec("rm -f /root/*.md5");
85 31210184 Scott Ullrich
					fclose($fp);
86 1d2ac3a5 Scott Ullrich
					die -1;
87
				}
88
				echo "\nMD5 checksum matches.\n";
89 7afeb359 Scott Ullrich
				exec("rm -f /root/*.md5");
90 1d2ac3a5 Scott Ullrich
			}
91 e1aea2cd Scott Ullrich
			if(stristr($url,"bdiff"))
92
				$type = "bdiff";			
93 1d2cc906 Scott Ullrich
			if(file_exists("/root/firmware.tgz"))
94 e1aea2cd Scott Ullrich
				do_upgrade("/root/firmware.tgz", $type);
95 f4feb493 Scott Ullrich
		} else {
96
			echo "\nCould not download update.\n\n";
97 31210184 Scott Ullrich
			fclose($fp);
98 f4feb493 Scott Ullrich
			die -1;
99
		}
100
	case "2":
101
		echo "\nEnter the complete path to the .tgz update file: ";
102
		$path = chop(fgets($fp));
103 31210184 Scott Ullrich
		if(!$path) {
104
			fclose($fp);
105 9b5d2d24 Scott Ullrich
			die;
106 31210184 Scott Ullrich
		}
107 e1aea2cd Scott Ullrich
		if(stristr($fp,"bdiff"))
108
			$type = "bdiff";
109 f4feb493 Scott Ullrich
		if(file_exists($path)) {
110 60b7af16 Scott Ullrich
			touch($d_fwupenabled_path);
111 e1aea2cd Scott Ullrich
			do_upgrade($path, $type);
112 f4feb493 Scott Ullrich
		} else {
113
			echo "\nCould not find file.\n\n";
114 31210184 Scott Ullrich
			fclose($fp);
115 f4feb493 Scott Ullrich
			die -1;
116
		}
117
}
118
119 92d8801f Scott Ullrich
function check_for_kernel_file() {
120 00392a13 Scott Ullrich
	global $fp;
121 9daeb964 Scott Ullrich
	$platform = file_get_contents("/etc/platform");
122
	$platform = str_replace("\n", "", $platform);
123
	$platform = str_replace("\r", "", $platform);
124
	if($platform == "embedded" or $platform == "wrap") {
125
		exec("echo wrap > /boot/kernel/pfsense_kernel.txt");
126
		echo "\n";
127
		return;
128
	}	
129 92d8801f Scott Ullrich
	if(!file_exists("/boot/kernel/pfsense_kernel.txt")) {
130
		echo "\nPlease select which kernel you would like to use:\n";
131
		echo "\n1. Non SMP kernel";
132
		echo "\n2. SMP kernel";
133
		echo "\n3. Embedded kernel (no video or keyboard)";
134 c79e6edf Scott Ullrich
		echo "\n4. Developers kernel (slower performing, more debugging)\n";
135 92d8801f Scott Ullrich
		echo "\nPlease enter a number [1-4]: ";
136
		$selection = strtoupper(chop(fgets($fp)));
137
		switch ($selection) {
138
			case "1":
139
				touch("/boot/kernel/pfsense_kernel.txt");
140
			break;
141
			case "2":
142
				exec("echo SMP > /boot/kernel/pfsense_kernel.txt");
143
			break;
144
			case "3":
145
				exec("echo wrap > /boot/kernel/pfsense_kernel.txt");
146
			break;
147
			case "4":
148
				exec("echo Developers > /boot/kernel/pfsense_kernel.txt");
149
			break;		
150
		}
151
		echo "\n";
152
	}
153
}
154
155 e1aea2cd Scott Ullrich
function do_upgrade($path, $type) {
156 00392a13 Scott Ullrich
	global $fp;
157 92d8801f Scott Ullrich
	check_for_kernel_file();
158 f4feb493 Scott Ullrich
	echo "\nOne moment please... Invoking firmware upgrade...\n";
159 e1aea2cd Scott Ullrich
	if($type == "bdiff") 
160
		exec("/etc/rc.firmware delta_update $path");
161
	else 
162
		exec("/etc/rc.firmware pfSenseupgrade $path");
163 23a44298 Scott Ullrich
	unlink_if_exists($path);
164 6ec96809 Scott Ullrich
	die;
165 f4feb493 Scott Ullrich
}
166
167 7afeb359 Scott Ullrich
exec("rm -f /root/*.md5");
168 1939b5ce Scott Ullrich
fclose($fp);
169
170 f4feb493 Scott Ullrich
?>