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