]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/boot/efi/linux.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / boot / efi / linux.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /*
3 * This program is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU Lesser General Public License as published by
5 * the Free Software Foundation; either version 2.1 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * Copyright (C) 2015 Kay Sievers <kay@vrfy.org>
14 */
15
16 #include <efi.h>
17 #include <efilib.h>
18
19 #include "linux.h"
20 #include "util.h"
21
22 #define SETUP_MAGIC 0x53726448 /* "HdrS" */
23 struct SetupHeader {
24 UINT8 boot_sector[0x01f1];
25 UINT8 setup_secs;
26 UINT16 root_flags;
27 UINT32 sys_size;
28 UINT16 ram_size;
29 UINT16 video_mode;
30 UINT16 root_dev;
31 UINT16 signature;
32 UINT16 jump;
33 UINT32 header;
34 UINT16 version;
35 UINT16 su_switch;
36 UINT16 setup_seg;
37 UINT16 start_sys;
38 UINT16 kernel_ver;
39 UINT8 loader_id;
40 UINT8 load_flags;
41 UINT16 movesize;
42 UINT32 code32_start;
43 UINT32 ramdisk_start;
44 UINT32 ramdisk_len;
45 UINT32 bootsect_kludge;
46 UINT16 heap_end;
47 UINT8 ext_loader_ver;
48 UINT8 ext_loader_type;
49 UINT32 cmd_line_ptr;
50 UINT32 ramdisk_max;
51 UINT32 kernel_alignment;
52 UINT8 relocatable_kernel;
53 UINT8 min_alignment;
54 UINT16 xloadflags;
55 UINT32 cmdline_size;
56 UINT32 hardware_subarch;
57 UINT64 hardware_subarch_data;
58 UINT32 payload_offset;
59 UINT32 payload_length;
60 UINT64 setup_data;
61 UINT64 pref_address;
62 UINT32 init_size;
63 UINT32 handover_offset;
64 } __attribute__((packed));
65
66 #ifdef __x86_64__
67 typedef VOID(*handover_f)(VOID *image, EFI_SYSTEM_TABLE *table, struct SetupHeader *setup);
68 static inline VOID linux_efi_handover(EFI_HANDLE image, struct SetupHeader *setup) {
69 handover_f handover;
70
71 asm volatile ("cli");
72 handover = (handover_f)((UINTN)setup->code32_start + 512 + setup->handover_offset);
73 handover(image, ST, setup);
74 }
75 #else
76 typedef VOID(*handover_f)(VOID *image, EFI_SYSTEM_TABLE *table, struct SetupHeader *setup) __attribute__((regparm(0)));
77 static inline VOID linux_efi_handover(EFI_HANDLE image, struct SetupHeader *setup) {
78 handover_f handover;
79
80 handover = (handover_f)((UINTN)setup->code32_start + setup->handover_offset);
81 handover(image, ST, setup);
82 }
83 #endif
84
85 EFI_STATUS linux_exec(EFI_HANDLE *image,
86 CHAR8 *cmdline, UINTN cmdline_len,
87 UINTN linux_addr,
88 UINTN initrd_addr, UINTN initrd_size) {
89 struct SetupHeader *image_setup;
90 struct SetupHeader *boot_setup;
91 EFI_PHYSICAL_ADDRESS addr;
92 EFI_STATUS err;
93
94 image_setup = (struct SetupHeader *)(linux_addr);
95 if (image_setup->signature != 0xAA55 || image_setup->header != SETUP_MAGIC)
96 return EFI_LOAD_ERROR;
97
98 if (image_setup->version < 0x20b || !image_setup->relocatable_kernel)
99 return EFI_LOAD_ERROR;
100
101 addr = 0x3fffffff;
102 err = uefi_call_wrapper(BS->AllocatePages, 4, AllocateMaxAddress, EfiLoaderData,
103 EFI_SIZE_TO_PAGES(0x4000), &addr);
104 if (EFI_ERROR(err))
105 return err;
106 boot_setup = (struct SetupHeader *)(UINTN)addr;
107 ZeroMem(boot_setup, 0x4000);
108 CopyMem(boot_setup, image_setup, sizeof(struct SetupHeader));
109 boot_setup->loader_id = 0xff;
110
111 boot_setup->code32_start = (UINT32)linux_addr + (image_setup->setup_secs+1) * 512;
112
113 if (cmdline) {
114 addr = 0xA0000;
115 err = uefi_call_wrapper(BS->AllocatePages, 4, AllocateMaxAddress, EfiLoaderData,
116 EFI_SIZE_TO_PAGES(cmdline_len + 1), &addr);
117 if (EFI_ERROR(err))
118 return err;
119 CopyMem((VOID *)(UINTN)addr, cmdline, cmdline_len);
120 ((CHAR8 *)addr)[cmdline_len] = 0;
121 boot_setup->cmd_line_ptr = (UINT32)addr;
122 }
123
124 boot_setup->ramdisk_start = (UINT32)initrd_addr;
125 boot_setup->ramdisk_len = (UINT32)initrd_size;
126
127 linux_efi_handover(image, boot_setup);
128 return EFI_LOAD_ERROR;
129 }