Project

General

Profile

Download (4.26 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php -f
2

    
3
<?php
4

    
5
$g['booting'] = true;
6
require("globals.inc");
7

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

    
10
require("functions.inc");
11
echo ".";
12
require("config.inc");
13
echo ".";
14
$g['booting'] = false;
15

    
16
$d_fwupenabled_path = $g['varrun_path'] . "/fwup.enabled";
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
			touch($d_fwupenabled_path);
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(stristr($url,"bdiff"))
92
				$type = "bdiff";			
93
			if(file_exists("/root/firmware.tgz"))
94
				do_upgrade("/root/firmware.tgz", $type);
95
		} else {
96
			echo "\nCould not download update.\n\n";
97
			fclose($fp);
98
			die -1;
99
		}
100
	case "2":
101
		echo "\nEnter the complete path to the .tgz update file: ";
102
		$path = chop(fgets($fp));
103
		if(!$path) {
104
			fclose($fp);
105
			die;
106
		}
107
		if(stristr($fp,"bdiff"))
108
			$type = "bdiff";
109
		if(file_exists($path)) {
110
			touch($d_fwupenabled_path);
111
			do_upgrade($path, $type);
112
		} else {
113
			echo "\nCould not find file.\n\n";
114
			fclose($fp);
115
			die -1;
116
		}
117
}
118

    
119
function check_for_kernel_file() {
120
	global $fp;
121
	$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
	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
		echo "\n4. Developers kernel (slower performing, more debugging)\n";
135
		echo "\nPlease enter a number [1-4]: ";
136
		$selection = strtoupper(chop(fgets($fp)));
137
		switch ($selection) {
138
			case "1":
139
				exec("echo UP > /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
function do_upgrade($path, $type) {
156
	global $fp;
157
	check_for_kernel_file();
158
	echo "\nOne moment please... Invoking firmware upgrade...\n";
159
	if($type == "bdiff") 
160
		exec("/etc/rc.firmware delta_update $path");
161
	else 
162
		exec("/etc/rc.firmware pfSenseupgrade $path");
163
	unlink_if_exists($path);
164
	die;
165
}
166

    
167
exec("rm -f /root/*.md5");
168
fclose($fp);
169

    
170
?>
(47-47/84)