1 # SPDX-License-Identifier: LGPL-2.1+
3 # Copyright 2017 Zbigniew Jędrzejewski-Szmek
5 # systemd is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as published by
7 # the Free Software Foundation; either version 2.1 of the License, or
8 # (at your option) any later version.
10 # systemd is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 efi_headers = files('''
38 systemd_boot_sources = '''
50 if conf.get('ENABLE_EFI') == 1 and get_option('gnu-efi') != 'false'
51 efi_cc = get_option('efi-cc')
52 efi_ld = get_option('efi-ld')
54 efi_incdir = get_option('efi-includedir')
55 have_header = (gnu_efi_arch != '' and
56 cc.has_header('@0@/@1@/efibind.h'.format(efi_incdir, gnu_efi_arch)))
58 if have_header and EFI_MACHINE_TYPE_NAME == ''
59 error('gnu-efi is available, but EFI_MACHINE_TYPE_NAME is unknown')
62 efi_libdir = get_option('efi-libdir')
64 cmd = 'cd /usr/lib/$(@0@ -print-multi-os-directory) && pwd'.format(efi_cc)
65 ret = run_command('sh', '-c', cmd)
66 if ret.returncode() == 0
67 efi_libdir = ret.stdout().strip()
71 have_gnu_efi = have_header and efi_libdir != ''
76 if get_option('gnu-efi') == 'true' and not have_gnu_efi
77 error('gnu-efi support requested, but headers were not found')
81 efi_conf = configuration_data()
82 efi_conf.set_quoted('PACKAGE_VERSION', meson.project_version())
83 efi_conf.set_quoted('EFI_MACHINE_TYPE_NAME', EFI_MACHINE_TYPE_NAME)
84 efi_conf.set10('ENABLE_TPM', get_option('tpm'))
85 efi_conf.set('SD_TPM_PCR', get_option('tpm-pcrindex'))
87 efi_config_h = configure_file(
88 output : 'efi_config.h',
89 configuration : efi_conf)
91 objcopy = find_program('objcopy')
93 efi_ldsdir = get_option('efi-ldsdir')
94 arch_lds = 'elf_@0@_efi.lds'.format(gnu_efi_arch)
96 efi_ldsdir = join_paths(efi_libdir, 'gnuefi')
97 cmd = run_command('test', '-f', join_paths(efi_ldsdir, arch_lds))
98 if cmd.returncode() != 0
99 efi_ldsdir = efi_libdir
100 cmd = run_command('test', '-f', join_paths(efi_ldsdir, arch_lds))
101 if cmd.returncode() != 0
102 error('Cannot find @0@'.format(arch_lds))
107 message('efi-libdir: "@0@"'.format(efi_libdir))
108 message('efi-ldsdir: "@0@"'.format(efi_ldsdir))
109 message('efi-includedir: "@0@"'.format(efi_incdir))
111 compile_args = ['-Wall',
119 '-fno-strict-aliasing',
120 '-fno-stack-protector',
122 '-Wno-missing-field-initializers',
123 '-isystem', efi_incdir,
124 '-isystem', join_paths(efi_incdir, gnu_efi_arch),
125 '-include', efi_config_h]
126 if efi_arch == 'x86_64'
127 compile_args += ['-mno-red-zone',
130 '-DEFI_FUNCTION_WRAPPER',
131 '-DGNU_EFI_USE_MS_ABI']
132 elif efi_arch == 'ia32'
133 compile_args += ['-mno-sse',
138 join_paths(efi_ldsdir, arch_lds),
144 join_paths(efi_ldsdir, 'crt0-efi-@0@.o'.format(gnu_efi_arch))]
145 if efi_arch == 'aarch64' or efi_arch == 'arm'
146 # Aarch64 and ARM32 don't have an EFI capable objcopy. Use 'binary'
147 # instead, and add required symbols manually.
148 efi_ldflags += ['--defsym=EFI_SUBSYSTEM=0xa']
149 efi_format = ['-O', 'binary']
151 efi_format = ['--target=efi-app-@0@'.format(gnu_efi_arch)]
154 systemd_boot_objects = []
156 foreach file : common_sources + systemd_boot_sources + stub_sources
157 o_file = custom_target(file + '.o',
159 output : file + '.o',
160 command : [efi_cc, '-c', '@INPUT@', '-o', '@OUTPUT@']
162 depend_files : efi_headers)
163 if (common_sources + systemd_boot_sources).contains(file)
164 systemd_boot_objects += [o_file]
166 if (common_sources + stub_sources).contains(file)
167 stub_objects += [o_file]
171 libgcc_file_name = run_command(efi_cc, '-print-libgcc-file-name').stdout().strip()
172 systemd_boot_efi_name = 'systemd-boot@0@.efi'.format(EFI_MACHINE_TYPE_NAME)
173 stub_efi_name = 'linux@0@.efi.stub'.format(EFI_MACHINE_TYPE_NAME)
174 no_undefined_symbols = find_program('no-undefined-symbols.sh')
176 foreach tuple : [['systemd_boot.so', systemd_boot_efi_name, systemd_boot_objects],
177 ['stub.so', stub_efi_name, stub_objects]]
182 command : [efi_ld, '-o', '@OUTPUT@'] +
183 efi_ldflags + tuple[2] +
184 ['-lefi', '-lgnuefi', libgcc_file_name])
186 test('no-undefined-symbols-' + tuple[0],
187 no_undefined_symbols,
190 stub = custom_target(
204 ['@INPUT@', '@OUTPUT@'],
206 install_dir : bootlibdir)
208 set_variable(tuple[0].underscorify(), so)
209 set_variable(tuple[0].underscorify() + '_stub', stub)
213 ############################################################
216 test_efi_disk_img = custom_target(
218 input : [systemd_boot_so, stub_so_stub],
219 output : 'test-efi-disk.img',
220 command : [test_efi_create_disk_sh, '@OUTPUT@',
221 '@INPUT0@', '@INPUT1@', splash_bmp])