Project

General

Profile

Download (3.59 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/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
$fp = fopen('php://stdin', 'r');
17

    
18
echo ".\n\n";
19

    
20
$shell_active = true;
21

    
22
echo "1) Update from a URL\n";
23
echo "2) Update from a local file\n";
24
echo "Q) Quit\n";
25

    
26
echo "\nPlease select an option to continue: ";
27

    
28
$command = strtoupper(chop(fgets($fp)));
29

    
30
switch ($command) {
31
	case "q":
32
	case "quit":
33
		echo "\n";
34
		fclose($fp);
35
		die;
36
	break;
37
	case "1":
38
		echo "\nEnter the URL to the .tgz update file:\n> ";
39
		$url = chop(fgets($fp));
40
		if(!$url) { 
41
			fclose($fp);
42
			die;
43
		}
44
		$status = does_url_exist($url);
45
		if($status) {
46
			conf_mount_rw();
47
			echo "\nFetching file size...\n";
48
			$file_size = exec("fetch -s \"$url\"");
49
			$file_size = trim($file_size, "\r");
50
			echo "\nFile size: $file_size\n";
51
			echo "\nFetching file...\n";
52
			exec("fetch -1 -w15 -a -v -o /root/firmware.tgz \"$url\"");
53
			if($file_size <> filesize("/root/firmware.tgz")) {
54
				echo "\nFile size mismatch.  Upgrade cancelled.\n\n";
55
				fclose($fp);
56
				die;
57
			}			
58
			if(!file_exists("/root/firmware.tgz")) {
59
				echo "Something went wrong during file transfer.  Exiting.\n\n";
60
				fclose($fp);
61
				die;
62
			}
63
			$status = does_url_exist("$url.md5");
64
			if($status) { 
65
				echo "\nFetching MD5...\n";
66
				exec("fetch -1 -w15 -a -v -o /root/firmware.tgz.md5 \"$url.md5\"");
67
			} else {
68
				echo "\n\nWARNING.\n";
69
				echo "\nCould not locate a MD5 file.  We cannot verify the download once its done.\n\n";
70
				sleep(15);
71
			}
72
			if(file_exists("/root/firmware.tgz.md5")) {
73
				$source_md5 = trim(`cat /root/firmware.tgz.md5 | awk '{ print \$4 }'`,"\r");
74
				$file_md5 = trim(`md5 /root/firmware.tgz | awk '{ print \$4 }'`,"\r");
75
				echo "URL MD5: $source_md5\n";
76
				echo "Downloaded file MD5: $file_md5\n";
77
				if($source_md5 <> $file_md5) {
78
					echo "\n\nMD5 checksum does not match.  Cancelling upgrade.\n\n";
79
					fclose($fp);
80
					exec("rm -f /root/*.md5");
81
					die -1;
82
				}
83
				echo "\nMD5 checksum matches.\n";
84
				exec("rm -f /root/*.md5");
85
			}
86
			if(file_exists("/root/firmware.tgz"))
87
				do_upgrade("/root/firmware.tgz");
88
		} else {
89
			echo "\nCould not download update.\n\n";
90
			fclose($fp);
91
			die -1;
92
		}
93
	case "2":
94
		echo "\nEnter the complete path to the .tgz update file: ";
95
		$path = chop(fgets($fp));
96
		if(!$path) {
97
			fclose($fp);
98
			die;
99
		}
100
		if(file_exists($path)) {
101
			do_upgrade($path);
102
		} else {
103
			echo "\nCould not find file.\n\n";
104
			fclose($fp);
105
			die -1;
106
		}
107
}
108

    
109
function check_for_kernel_file() {
110
	global $fp;
111
	if(!file_exists("/boot/kernel/pfsense_kernel.txt")) {
112
		echo "\nPlease select which kernel you would like to use:\n";
113
		echo "\n1. Non SMP kernel";
114
		echo "\n2. SMP kernel";
115
		echo "\n3. Embedded kernel (no video or keyboard)";
116
		echo "\n4. Developers kernel (slower performing, more debugging)\n";
117
		echo "\nPlease enter a number [1-4]: ";
118
		$selection = strtoupper(chop(fgets($fp)));
119
		switch ($selection) {
120
			case "1":
121
				touch("/boot/kernel/pfsense_kernel.txt");
122
			break;
123
			case "2":
124
				exec("echo SMP > /boot/kernel/pfsense_kernel.txt");
125
			break;
126
			case "3":
127
				exec("echo wrap > /boot/kernel/pfsense_kernel.txt");
128
			break;
129
			case "4":
130
				exec("echo Developers > /boot/kernel/pfsense_kernel.txt");
131
			break;		
132
		}
133
		echo "\n";
134
	}
135
}
136

    
137
function do_upgrade($path) {
138
	global $fp;
139
	check_for_kernel_file();
140
	echo "\nOne moment please... Invoking firmware upgrade...\n";
141
	exec("/etc/rc.firmware pfSenseupgrade $path");
142
	unlink_if_exists($path);
143
	die;
144
}
145

    
146
exec("rm -f /root/*.md5");
147
fclose($fp);
148

    
149
?>
(49-49/77)