summaryrefslogtreecommitdiffstats
path: root/modules-available/serversetup-bwlp-ipxe/inc/bootentry.inc.php
blob: dec7052880db456bfa744adbaed70cd80433e35e (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<?php

abstract class BootEntry
{

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

	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 $module module this entry belongs to, or special values .script/.exec
	 * @param string $jsonString serialized entry data
	 * @return BootEntry|null instance representing boot entry, null on error
	 */
	public static function fromJson($module, $data)
	{
		if ($module{0} !== '.') {
			// Hook from other module
			$hook = Hook::loadSingle($module, 'ipxe-bootentry');
			if ($hook === false) {
				error_log('Module ' . $module . ' doesnt have an ipxe-bootentry hook');
				return null;
			}
			$ret = $hook->run();
			if (!($ret instanceof BootEntryHook))
				return null;
			return $ret->getBootEntry($data);
		}
		if (is_string($data)) {
			$data = json_decode($data, true);
		}
		if ($module === '.script') {
			return new CustomBootEntry($data);
		}
		if ($module === '.exec') {
			return new StandardBootEntry($data);
		}
		return null;
	}

	public static function forMenu($menuId)
	{
		return new MenuBootEntry($menuId);
	}

	public static function newStandardBootEntry($initData, $efi = false, $arch = false)
	{
		$ret = new StandardBootEntry($initData, $efi, $arch);
		$list = [];
		if ($ret->arch() !== self::EFI) {
			$list[] = self::BIOS;
		}
		if ($ret->arch() === self::EFI || $ret->arch() === self::BOTH) {
			$list[] = self::EFI;
		}
		$data = $ret->toArray();
		foreach ($list as $mode) {
			if (empty($data[$mode]['executable'])) {
				error_log('Incomplete stdbot: ' . print_r($initData, true));
				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 module, data FROM serversetup_bootentry
			WHERE entryid = :id LIMIT 1", ['id' => $id]);
		if ($row === false)
			return false;
		return self::fromJson($row['module'], $row['data']);
	}

	/**
	 * Get all existing BootEntries from database, skipping those of
	 * unknown type. Returned array is assoc, key is entryid
	 *
	 * @return BootEntry[] all existing BootEntries
	 */
	public static function getAll()
	{
		$res = Database::simpleQuery("SELECT entryid, data FROM serversetup_bootentry");
		$ret = [];
		while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
			$tmp = self::fromJson($row['module'], $row['data']);
			if ($tmp === null)
				continue;
			$ret[$row['entryid']] = $tmp;
		}
		return $ret;
	}

}

class StandardBootEntry extends BootEntry
{
	/**
	 * @var ExecData PCBIOS boot data
	 */
	protected $pcbios;
	/**
	 * @var ExecData same for EFI
	 */
	protected $efi;

	protected $arch; // Constants below

	const KEYS = ['executable', 'initRd', 'commandLine', 'replace', 'imageFree', 'autoUnload', 'resetConsole', 'dhcpOptions'];

	public function __construct($data, $efi = false, $arch = false)
	{
		$this->pcbios = new ExecData();
		$this->efi = new ExecData();
		if ($data instanceof PxeSection) {
			// Import from PXELINUX menu
			$this->fromPxeMenu($data);
		} elseif ($data instanceof ExecData && is_string($arch)) {
			if (!($efi instanceof ExecData)) {
				$efi = new ExecData();
			}
			$this->pcbios = $data;
			$this->efi = $efi;
			$this->arch = $arch;
		} elseif (is_array($data)) {
			// Serialized data
			if (!isset($data['arch'])) {
				error_log('Serialized data to StandardBootEntry doesnt contain arch: ' . json_encode($data));
			} else {
				$this->arch = $data['arch'];
			}
			if (isset($data[BootEntry::BIOS]) || isset($data[BootEntry::EFI])) {
				// Current format
				$this->fromCurrentFormat($data);
			} else {
				// Convert legacy DB format
				$this->fromLegacyFormat($data);
			}
		} elseif ($arch == BootEntry::EFI && $efi instanceof ExecData) {
			$this->efi = $efi;
			$this->arch = $arch;
		} else {
			error_log('Invalid StandardBootEntry constructor call');
		}
		if (!in_array($this->arch, [BootEntry::BIOS, BootEntry::EFI, BootEntry::BOTH, BootEntry::AGNOSTIC])) {
			$this->arch = BootEntry::AGNOSTIC;
		}
	}

	private function fromLegacyFormat($data)
	{
		$ok = false;
		foreach (self::KEYS as $key) {
			if (isset($data[$key][BootEntry::BIOS])) {
				$this->pcbios->{$key} = $data[$key][BootEntry::BIOS];
				$ok = true;
			}
			if (isset($data[$key][BootEntry::EFI])) {
				$this->efi->{$key} = $data[$key][BootEntry::EFI];
				$ok = true;
			}
		}
		if (!$ok) {
			// Very old entry
			foreach (self::KEYS as $key) {
				if (isset($data[$key])) {
					$this->pcbios->{$key} = $data[$key];
				}
			}
		}
	}

	private function fromCurrentFormat($data)
	{
		foreach (self::KEYS as $key) {
			if (isset($data[BootEntry::BIOS][$key])) {
				$this->pcbios->{$key} = $data[BootEntry::BIOS][$key];
			}
			if (isset($data[BootEntry::EFI][$key])) {
				$this->efi->{$key} = $data[BootEntry::EFI][$key];
			}
		}
	}

	/**
	 * @param PxeSection $data
	 */
	private function fromPxeMenu($data)
	{
		$bios = $this->pcbios;
		$bios->executable = $data->kernel;
		$bios->initRd = $data->initrd;
		$bios->commandLine = ' ' . str_replace('vga=current', '', $data->append) . ' ';
		$bios->resetConsole = true;
		$bios->replace = true;
		$bios->autoUnload = true;
		if (strpos($bios->commandLine, ' quiet ') !== false) {
			$bios->commandLine .= ' loglevel=5 rd.systemd.show_status=auto';
		}
		if ($data->ipAppend & 1) {
			$bios->commandLine .= ' ${ipappend1}';
		}
		if ($data->ipAppend & 2) {
			$bios->commandLine .= ' ${ipappend2}';
		}
		if ($data->ipAppend & 4) {
			$bios->commandLine .= ' SYSUUID=${uuid}';
		}
		$bios->commandLine = trim(preg_replace('/\s+/', ' ', $bios->commandLine));
	}

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

	public function supportsMode($mode)
	{
		if ($mode === $this->arch || $this->arch === BootEntry::AGNOSTIC)
			return true;
		if ($mode === BootEntry::BIOS || $mode === BootEntry::EFI) {
			return $this->arch === BootEntry::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 === BootEntry::AGNOSTIC || $mode == BootEntry::BIOS) {
			$entry = $this->pcbios;
		} else {
			$entry = $this->efi;
		}
		$entry->sanitize();

		$script = '';
		if ($entry->resetConsole) {
			$script .= "console ||\n";
		}
		if ($entry->imageFree) {
			$script .= "imgfree ||\n";
		}
		foreach ($entry->dhcpOptions as $opt) {
			if (empty($opt['value'])) {
				$val = '${}';
			} else {
				if (empty($opt['hex'])) {
					$val = bin2hex($opt['value']);
				} else {
					$val = $opt['value'];
				}
				preg_match_all('/[0-9a-f]{2}/', $val, $out);
				$val = implode(':', $out[0]);
			}
			$script .= 'set net${idx}/' . $opt['opt'] . ':hex ' . $val
				. ' || prompt Cannot override DHCP server option ' . $opt['opt'] . ". Press any key to continue anyways.\n";
		}
		$initrds = [];
		if (!empty($entry->initRd)) {
			foreach (array_values($entry->initRd) as $i => $initrd) {
				if (empty($initrd))
					continue;
				$script .= "initrd --name initrd$i $initrd || goto $failLabel\n";
				$initrds[] = "initrd$i";
			}
		}
		$script .= "boot ";
		if ($entry->autoUnload) {
			$script .= "-a ";
		}
		if ($entry->replace) {
			$script .= "-r ";
		}
		$script .= $entry->executable;
		if (empty($initrds)) {
			$rdBase = '';
		} else {
			$rdBase = " initrd=" . implode(',', $initrds);
		}
		if (!empty($entry->commandLine)) {
			$script .= "$rdBase {$entry->commandLine}";
		}
		$script .= " || goto $failLabel\n";
		if ($entry->resetConsole) {
			$script .= "goto start ||\n";
		}
		return $script;
	}

	public function addFormFields(&$array)
	{
		$array[$this->arch . '_selected'] = 'selected';
		$array['entries'][] = $this->pcbios->toFormFields(BootEntry::BIOS);
		$array['entries'][] = $this->efi->toFormFields(BootEntry::EFI);
		$array['exec_checked'] = 'checked';
	}

	public function toArray()
	{
		return [
			BootEntry::BIOS => $this->pcbios->toArray(),
			BootEntry::EFI => $this->efi->toArray(),
			'arch' => $this->arch,
		];
	}
}

class CustomBootEntry extends BootEntry
{
	protected $script;

	public function __construct($data)
	{
		if (is_array($data) && isset($data['script'])) {
			$this->script = $data['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];
	}
}

class MenuBootEntry extends BootEntry
{
	protected $menuId;

	public function __construct($menuId)
	{
		$this->menuId = $menuId;
	}

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

	public function toScript($failLabel, $mode)
	{
		return 'chain -ar ${self}&menuid=' . $this->menuId . ' || goto ' . $failLabel . "\n";
	}

	public function toArray()
	{
		return [];
	}

	public function addFormFields(&$array)
	{
	}
}