summaryrefslogtreecommitdiffstats
path: root/modules-available/serversetup-bwlp-ipxe/inc/bootentry.inc.php
blob: 69adffd39cf56917d680923ac781dee718e75ca2 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php

abstract class BootEntry
{

	public function __construct($data = false)
	{
		if (is_array($data)) {
			foreach ($data as $key => $value) {
				if (property_exists($this, $key)) {
					$this->{$key} = $value;
				}
			}
		}
	}

	public abstract function supportsMode($mode);

	public abstract function toScript($failLabel, $mode);

	public abstract function toArray();

	public abstract function addFormFields(&$array);

	/*
	 *
	 */

	/**
	 * Return a BootEntry instance from the serialized data.
	 *
	 * @param string $jsonString serialized entry data
	 * @return BootEntry|null instance representing boot entry, null on error
	 */
	public static function fromJson($data)
	{
		if (is_string($data)) {
			$data = json_decode($data, true);
		}
		if (isset($data['script'])) {
			return new CustomBootEntry($data);
		}
		if (isset($data['executable'])) {
			return new StandardBootEntry($data);
		}
		return null;
	}

	public static function newStandardBootEntry($initData)
	{
		$ret = new StandardBootEntry($initData);
		$list = [];
		if ($ret->arch() !== StandardBootEntry::EFI) {
			$list[] = StandardBootEntry::BIOS;
		}
		if ($ret->arch() === StandardBootEntry::EFI || $ret->arch() === StandardBootEntry::BOTH) {
			$list[] = StandardBootEntry::EFI;
		}
		foreach ($list as $mode) {
			if (empty($initData['executable'][$mode]))
				return null;
		}
		return $ret;
	}

	public static function newCustomBootEntry($initData)
	{
		if (empty($initData['script']))
			return null;
		return new CustomBootEntry($initData);
	}

	/**
	 * Return a BootEntry instance from database with the given id.
	 *
	 * @param string $id
	 * @return BootEntry|null|false false == unknown id, null = unknown entry type, BootEntry instance on success
	 */
	public static function fromDatabaseId($id)
	{
		$row = Database::queryFirst("SELECT data FROM serversetup_bootentry
			WHERE entryid = :id LIMIT 1", ['id' => $id]);
		if ($row === false)
			return false;
		return self::fromJson($row['data']);
	}

}

class StandardBootEntry extends BootEntry
{
	protected $executable;
	protected $initRd;
	protected $commandLine;
	protected $replace;
	protected $autoUnload;
	protected $resetConsole;
	protected $arch; // Constants below

	const BIOS = 'PCBIOS'; // Only valid for legacy BIOS boot
	const EFI = 'EFI'; // Only valid for EFI boot
	const BOTH = 'PCBIOS-EFI'; // Supports both via distinct entry
	const AGNOSTIC = 'agnostic'; // Supports both via same entry (PCBIOS entry)

	public function __construct($data = false)
	{
		if ($data instanceof PxeSection) {
			// Gets arrayfied below
			$this->executable = $data->kernel;
			$this->initRd = $data->initrd;
			$this->commandLine = ' ' . str_replace('vga=current', '', $data->append) . ' ';
			$this->resetConsole = true;
			$this->replace = true;
			$this->autoUnload = true;
			if (strpos($this->commandLine, ' quiet ') !== false) {
				$this->commandLine .= ' loglevel=5 rd.systemd.show_status=auto';
			}
			if ($data->ipAppend & 1) {
				$this->commandLine .= ' ${ipappend1}';
			}
			if ($data->ipAppend & 2) {
				$this->commandLine .= ' ${ipappend2}';
			}
			if ($data->ipAppend & 4) {
				$this->commandLine .= ' SYSUUID=${uuid}';
			}
			$this->commandLine = trim(preg_replace('/\s+/', ' ', $this->commandLine));
		} else {
			parent::__construct($data);
		}
		// Convert legacy DB format
		foreach (['executable', 'initRd', 'commandLine', 'replace', 'autoUnload', 'resetConsole'] as $key) {
			if (!is_array($this->{$key})) {
				$this->{$key} = [ 'PCBIOS' => $this->{$key}, 'EFI' => '' ];
			}
		}
		if ($this->arch === null) {
			$this->arch = self::AGNOSTIC;
		}
	}

	public function arch()
	{
		return $this->arch;
	}

	public function supportsMode($mode)
	{
		if ($mode === $this->arch || $this->arch === self::AGNOSTIC)
			return true;
		if ($mode === self::BIOS || $mode === self::EFI) {
			return $this->arch === self::BOTH;
		}
		error_log('Unknown iPXE platform: ' . $mode);
		return false;
	}

	public function toScript($failLabel, $mode)
	{
		if (!$this->supportsMode($mode)) {
			return "prompt Entry doesn't have an executable for mode $mode\n";
		}
		if ($this->arch === self::AGNOSTIC) {
			$mode = self::BIOS;
		}

		$script = '';
		if ($this->resetConsole[$mode]) {
			$script .= "console ||\n";
		}
		if (!empty($this->initRd[$mode])) {
			$script .= "imgfree ||\n";
			if (!is_array($this->initRd[$mode])) {
				$script .= "initrd {$this->initRd[$mode]} || goto $failLabel\n";
			} else {
				foreach ($this->initRd[$mode] as $initrd) {
					$script .= "initrd $initrd || goto $failLabel\n";
				}
			}
		}
		$script .= "boot ";
		if ($this->autoUnload[$mode]) {
			$script .= "-a ";
		}
		if ($this->replace[$mode]) {
			$script .= "-r ";
		}
		$script .= $this->executable[$mode];
		$rdBase = basename($this->initRd[$mode]);
		if (!empty($this->commandLine[$mode])) {
			$script .= " initrd=$rdBase {$this->commandLine[$mode]}";
		}
		$script .= " || goto $failLabel\n";
		if ($this->resetConsole[$mode]) {
			$script .= "goto start ||\n";
		}
		return $script;
	}

	public function addFormFields(&$array)
	{
		$array[$this->arch . '_selected'] = 'selected';
		foreach ([self::BIOS, self::EFI] as $mode) {
			$array['entries'][] = [
				'is' . $mode => true,
				'mode' => $mode,
				'executable' => $this->executable[$mode],
				'initRd' => $this->initRd[$mode],
				'commandLine' => $this->commandLine[$mode],
				'replace_checked' => $this->replace[$mode] ? 'checked' : '',
				'autoUnload_checked' => $this->autoUnload[$mode] ? 'checked' : '',
				'resetConsole_checked' => $this->resetConsole[$mode] ? 'checked' : '',
			];
		}
		$array['exec_checked'] = 'checked';
	}

	public function toArray()
	{
		return [
			'executable' => $this->executable,
			'initRd' => $this->initRd,
			'commandLine' => $this->commandLine,
			'replace' => $this->replace,
			'autoUnload' => $this->autoUnload,
			'resetConsole' => $this->resetConsole,
			'arch' => $this->arch,
		];
	}
}

class CustomBootEntry extends BootEntry
{
	protected $script;

	public function supportsMode($mode)
	{
		return true;
	}

	public function toScript($failLabel, $mode)
	{
		return str_replace('%fail%', $failLabel, $this->script) . "\n";
	}

	public function addFormFields(&$array)
	{
		$array['entry'] = [
			'script' => $this->script,
		];
		$array['script_checked'] = 'checked';
	}

	public function toArray()
	{
		return ['script' => $this->script];
	}
}