summaryrefslogtreecommitdiffstats
path: root/modules-available/serversetup-bwlp-ipxe/inc/scriptbuilderbash.inc.php
blob: d6b542ece3e4adf14737972e12f3b99f66f9541e (plain) (blame)
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
<?php

class ScriptBuilderBash extends ScriptBuilderBase
{

	public function output(string $string): void
	{
		echo $string;
	}

	public function bootstrapLive(): bool { return false; }

	public function getMenu(IPxeMenu $menu, bool $bootstrap): string
	{
		return $this->menuToScript($menu);
	}

	public function getBootEntry(?BootEntry $entry): string
	{
		if ($entry === null) {
			return "echo 'Invalid boot entry id'\nread -n1 -r _\n";
		}
		return $entry->toScript($this);
	}

	public function getMenuEntry(?MenuEntry $entry, bool $honorPassword = true): string
	{
		if ($entry === null)
			return "echo 'Invalid menu entry id - press any key to continue'\nread -n1 -r _\n";
		return $entry->getBootEntryScript($this);
	}

	public function getSpecial(string $special): string
	{
		return ''; // We can't really do localboot here I guess
	}

	public function menuToScript(IPxeMenu $menu): string
	{
		$output = "declare -A items_name items_gap hotkey_item\ndeclare menu_default menu_timeout menu_title\n";
		foreach ($menu->items as $entry) {
			$id = $entry->menuentryid;
			if ($entry->bootEntry === null || (!empty($this->platform) && !$entry->bootEntry->supportsMode($this->platform)))
				continue;
			if (!$entry->hidden) {
				$output .= 'items_name[' . $id . ']=' . $this->bashString($entry->title) . "\n";
				if ($entry->gap) {
					$output .= 'items_gap[' . $id . "]=1\n";
				}
			}
			if ($entry->hotkey !== false) {
				$output .= 'hotkey_item[' . $entry->hotkey . ']=' . $id . "\n";
			}
			if ($id == $menu->defaultEntryId) {
				$output .= "menu_default={$id}\n";
			}
		}
		return $output . "menu_timeout=" . $menu->timeoutMs
			. "\nmenu_title=" . $this->bashString($menu->title) . "\n";
	}

	public function execDataToScript(?ExecData $agnostic, ?ExecData $bios, ?ExecData $efi): string
	{
		if ($agnostic !== null)
			return $this->execDataToScriptInternal($agnostic);
		if ($bios !== null && $this->platform === BootEntry::BIOS)
			return $this->execDataToScriptInternal($bios);
		if ($efi !== null && $this->platform === BootEntry::EFI)
			return $this->execDataToScriptInternal($efi);
		return $this->execDataToScriptInternal($bios ?? $efi ?? new ExecData());
	}

	private function execDataToScriptInternal(ExecData $entry) : string
	{
		$entry->sanitize();
		$script = "declare -a initrd\ndeclare kernel kcl\n";
		if (!empty($entry->initRd)) {
			foreach ($entry->initRd as $initrd) {
				if (empty($initrd))
					continue;
				$script .= 'initrd+=( ' . $this->bashString($initrd) . " )\n";
			}
		}
		$script .= 'kernel=' . $this->bashString($entry->executable) . "\n";
		$script .= 'kcl="' . str_replace('"', '"\\""', $entry->commandLine) . "\"\n"; // Allow expansion
		return $script;
	}

	private function bashString(string $string): string
	{
		if (strpos($string, "'") === false) {
			return "'$string'";
		}
		return "'" . str_replace("'", "'\\''", $string) . "'";
	}

}