summaryrefslogtreecommitdiffstats
path: root/etc/rc.initial.firmware_update
blob: c666ff7b38d2339ad0196adb6dbd822f29814f12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/local/bin/php -f

<?php

echo "Starting the pfSense console firmware update system";

echo ".";
require("globals.inc");
$g['booting'] = true;
require("functions.inc");
echo ".";
require("config.inc");
echo ".";
$g['booting'] = false;

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

echo ".\n\n";

$shell_active = true;

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

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

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

switch ($command) {
	case "q":
	case "quit":
		echo "\n";
		die;
	break;
	case "1":
		echo "\nEnter the URL to the .tgz update file:\n> ";
		$url = chop(fgets($fp));
		if(!$url) 
			die;
		$status = does_url_exist($url);
		if($status) {
			echo "\nFetching file size...\n";
			$file_size = exec("fetch -s \"$url\"");
			$file_size = trim($file_size, "\r");
			echo "\nFile size: $file_size\n";
			echo "\nFetching file...\n";
			exec("fetch -1 -w15 -a -v -o /tmp/firmware.tgz \"$url\"");
			if($file_size <> filesize("/tmp/firmware.tgz")) {
				echo "\nFile size mismatch.  Upgrade cancelled.\n\n";
				die;
			}			
			if(!file_exists("/tmp/firmware.tgz")) {
				echo "Something went wrong during file transfer.  Exiting.\n\n";
				die;
			}
			$status = does_url_exist("$url.md5");
			if($status) { 
				echo "\nFetching MD5...\n";
				exec("fetch -1 -w15 -a -v -o /tmp/firmware.tgz.md5 \"$url.md5\"");
			} else {
				echo "\n\nWARNING.\n";
				echo "\nCould not locate a MD5 file.  We cannot verify the download once its done.\n\n";
				sleep(15);
			}
			if(file_exists("/tmp/firmware.tgz.md5")) {
				$source_md5 = trim(`cat /tmp/firmware.tgz.md5 | awk '{ print \$4 }'`,"\r");
				$file_md5 = trim(`md5 /tmp/firmware.tgz | awk '{ print \$4 }'`,"\r");
				echo "URL MD5: $source_md5\n";
				echo "Downloaded file MD5: $file_md5\n";
				if($source_md5 <> $file_md5) {
					echo "\n\nMD5 checksum does not match.  Cancelling upgrade.\n\n";
					die -1;
				}
				echo "\nMD5 checksum matches.\n";
			}
			if(file_exists("/tmp/firmware.tgz"))
				do_upgrade("/tmp/firmware.tgz");
		} else {
			echo "\nCould not download update.\n\n";
			die -1;
		}
	case "2":
		echo "\nEnter the complete path to the .tgz update file: ";
		$path = chop(fgets($fp));
		if(!$path) 
			die;
		if(file_exists($path)) {
			do_upgrade($path);
		} else {
			echo "\nCould not find file.\n\n";
			die -1;
		}
}

function do_upgrade($path) {
	echo "\nOne moment please... Invoking firmware upgrade...\n";
	exec("/etc/rc.firmware pfSenseupgrade $path");
}

?>
OpenPOWER on IntegriCloud