├── .gitignore ├── samples └── uefi_root │ └── loader │ ├── loader.conf │ └── entries │ ├── mll-x86.conf │ └── mll-x86_64.conf ├── project ├── test │ ├── splash.bmp │ └── test-efi-create-disk.sh ├── src │ ├── kernel-install │ │ ├── 50-depmod.install │ │ ├── 90-loaderentry.install │ │ └── kernel-install │ └── sd-boot │ │ ├── disk.h │ │ ├── measure.h │ │ ├── splash.h │ │ ├── graphics.h │ │ ├── linux.h │ │ ├── pe.h │ │ ├── shim.h │ │ ├── console.h │ │ ├── disk.c │ │ ├── util.h │ │ ├── graphics.c │ │ ├── linux.c │ │ ├── stub.c │ │ ├── pe.c │ │ ├── console.c │ │ ├── shim.c │ │ ├── util.c │ │ ├── splash.c │ │ └── measure.c ├── .gitignore ├── shell-completion │ ├── zsh │ │ └── _kernel-install │ └── bash │ │ └── kernel-install ├── autogen.sh ├── README.md ├── m4 │ └── ax_normalize_path.m4 ├── Makefile.am ├── configure.ac ├── man │ └── kernel-install.xml └── LICENSE ├── qemu_uefi_32.sh ├── qemu_uefi_64.sh ├── LICENSE ├── generate_release.sh ├── prepare_third_party_software.sh ├── README.md ├── generate_mll_iso_32.sh └── generate_mll_iso_64.sh /.gitignore: -------------------------------------------------------------------------------- 1 | systemd-boot_installed/ 2 | work/ 3 | -------------------------------------------------------------------------------- /samples/uefi_root/loader/loader.conf: -------------------------------------------------------------------------------- 1 | default mll-x86_64 2 | timeout 5 3 | editor 1 4 | -------------------------------------------------------------------------------- /project/test/splash.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ivandavidov/systemd-boot/HEAD/project/test/splash.bmp -------------------------------------------------------------------------------- /qemu_uefi_32.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | qemu-system-i386 -pflash work/ovmf/ovmf-32.fd -m 128M -cdrom work/mll_32.iso -boot d -vga std > /dev/null 2>&1 & 4 | -------------------------------------------------------------------------------- /qemu_uefi_64.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | qemu-system-x86_64 -pflash work/ovmf/ovmf-64.fd -m 128M -cdrom work/mll_64.iso -boot d -vga std > /dev/null 2>&1 & 4 | -------------------------------------------------------------------------------- /samples/uefi_root/loader/entries/mll-x86.conf: -------------------------------------------------------------------------------- 1 | title Minimal Linux Live 2 | version x86 3 | efi /minimal/x86/kernel.xz 4 | options initrd=/minimal/x86/rootfs.xz 5 | -------------------------------------------------------------------------------- /samples/uefi_root/loader/entries/mll-x86_64.conf: -------------------------------------------------------------------------------- 1 | title Minimal Linux Live 2 | version x86_64 3 | efi /minimal/x86_64/kernel.xz 4 | options initrd=/minimal/x86_64/rootfs.xz 5 | -------------------------------------------------------------------------------- /project/src/kernel-install/50-depmod.install: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*- 3 | # ex: ts=8 sw=4 sts=4 et filetype=sh 4 | 5 | [[ $1 == "add" ]] || exit 0 6 | [[ $2 ]] || exit 1 7 | 8 | exec depmod -a "$2" 9 | -------------------------------------------------------------------------------- /project/.gitignore: -------------------------------------------------------------------------------- 1 | *.efi 2 | *.efi.stub 3 | *.8 4 | *.a 5 | *.cache 6 | *.gch 7 | *.la 8 | *.lo 9 | *.log 10 | *.o 11 | *.so 12 | *.plist 13 | *.stamp 14 | *.swp 15 | *.trs 16 | *~ 17 | .config.args 18 | .deps/ 19 | .dirstamp 20 | *.tar.xz 21 | Makefile 22 | Makefile.in 23 | aclocal.m4 24 | config.h 25 | config.h.in 26 | config.log 27 | config.status 28 | configure 29 | stamp-* 30 | build-aux -------------------------------------------------------------------------------- /project/shell-completion/zsh/_kernel-install: -------------------------------------------------------------------------------- 1 | #compdef kernel-install 2 | 3 | _images(){ 4 | if [[ "$words[2]" == "remove" ]]; then 5 | _message 'No more options' 6 | else 7 | _path_files -W /boot/ -P /boot/ -g "vmlinuz-*" 8 | fi 9 | } 10 | 11 | _kernels(){ 12 | read _MACHINE_ID < /etc/machine-id 13 | _kernel=( /lib/modules/[0-9]* ) 14 | if [[ "$cmd" == "remove" && -n "$_MACHINE_ID" ]]; then 15 | _kernel=( "/boot/$_MACHINE_ID"/[0-9]* ) 16 | fi 17 | _kernel=( ${_kernel##*/} ) 18 | _describe "installed kernels" _kernel 19 | } 20 | 21 | _arguments \ 22 | '1::add or remove:(add remove)' \ 23 | '2::kernel versions:_kernels' \ 24 | '3::kernel images:_images' 25 | 26 | #vim: set ft=zsh sw=4 ts=4 et 27 | -------------------------------------------------------------------------------- /project/src/sd-boot/disk.h: -------------------------------------------------------------------------------- 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 14 | */ 15 | 16 | #ifndef __SDBOOT_DISK_H 17 | #define __SDBOOT_DISK_H 18 | 19 | EFI_STATUS disk_get_part_uuid(EFI_HANDLE *handle, CHAR16 uuid[37]); 20 | #endif 21 | -------------------------------------------------------------------------------- /project/src/sd-boot/measure.h: -------------------------------------------------------------------------------- 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 | */ 14 | #ifndef __SDBOOT_MEASURE_H 15 | #define __SDBOOT_MEASURE_H 16 | 17 | EFI_STATUS tpm_log_event(UINT32 pcrindex, const EFI_PHYSICAL_ADDRESS buffer, UINTN buffer_size, const CHAR16 *description); 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /project/src/sd-boot/splash.h: -------------------------------------------------------------------------------- 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) 2012-2013 Kay Sievers 14 | * Copyright (C) 2012 Harald Hoyer 15 | */ 16 | 17 | #ifndef __SDBOOT_SPLASH_H 18 | #define __SDBOOT_SPLASH_H 19 | 20 | EFI_STATUS graphics_splash(UINT8 *content, UINTN len, const EFI_GRAPHICS_OUTPUT_BLT_PIXEL *background); 21 | #endif 22 | -------------------------------------------------------------------------------- /project/src/sd-boot/graphics.h: -------------------------------------------------------------------------------- 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) 2012-2013 Kay Sievers 14 | * Copyright (C) 2012 Harald Hoyer 15 | * Copyright (C) 2013 Intel Corporation 16 | * Authored by Joonas Lahtinen 17 | */ 18 | 19 | #ifndef __SDBOOT_GRAPHICS_H 20 | #define __SDBOOT_GRAPHICS_H 21 | 22 | EFI_STATUS graphics_mode(BOOLEAN on); 23 | #endif 24 | -------------------------------------------------------------------------------- /project/src/sd-boot/linux.h: -------------------------------------------------------------------------------- 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 14 | */ 15 | 16 | #ifndef __SDBOOT_kernel_H 17 | #define __SDBOOT_kernel_H 18 | 19 | EFI_STATUS linux_exec(EFI_HANDLE *image, 20 | CHAR8 *cmdline, UINTN cmdline_size, 21 | UINTN linux_addr, 22 | UINTN initrd_addr, UINTN initrd_size, BOOLEAN secure); 23 | #endif 24 | -------------------------------------------------------------------------------- /project/src/sd-boot/pe.h: -------------------------------------------------------------------------------- 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 14 | */ 15 | 16 | #ifndef __SDBOOT_PEFILE_H 17 | #define __SDBOOT_PEFILE_H 18 | 19 | EFI_STATUS pe_memory_locate_sections(CHAR8 *base, 20 | CHAR8 **sections, UINTN *addrs, UINTN *offsets, UINTN *sizes); 21 | EFI_STATUS pe_file_locate_sections(EFI_FILE *dir, CHAR16 *path, 22 | CHAR8 **sections, UINTN *addrs, UINTN *offsets, UINTN *sizes); 23 | #endif 24 | -------------------------------------------------------------------------------- /project/src/sd-boot/shim.h: -------------------------------------------------------------------------------- 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 | * Port to systemd-boot 14 | * Copyright 2017 Max Resch 15 | * 16 | * Security Policy Handling 17 | * Copyright 2012 18 | * https://github.com/mjg59/efitools 19 | */ 20 | 21 | #ifndef __SDBOOT_SHIM_H 22 | #define __SDBOOT_SHIM_H 23 | 24 | BOOLEAN shim_loaded(void); 25 | 26 | BOOLEAN secure_boot_enabled(void); 27 | 28 | EFI_STATUS security_policy_install(void); 29 | 30 | EFI_STATUS security_policy_uninstall(void); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Ivan Davidov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /generate_release.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | set -e 4 | 5 | SRC_DIR=$PWD 6 | 7 | mkdir -p $SRC_DIR/work/release 8 | cd $SRC_DIR/work/release 9 | 10 | DATE_PARSED=`LANG=en_US ; date +"%d-%b-%Y"` 11 | RELEASE_NAME=systemd-boot_$DATE_PARSED 12 | RELEASE_DIR=$SRC_DIR/work/release/$RELEASE_NAME 13 | 14 | rm -rf $RELEASE_DIR* 15 | mkdir -p $RELEASE_DIR 16 | 17 | cp -r $SRC_DIR/work/uefi_root \ 18 | $RELEASE_DIR 19 | 20 | rm -rf $RELEASE_DIR/uefi_root/loader 21 | 22 | cp -r $SRC_DIR/samples/uefi_root/loader \ 23 | $RELEASE_DIR/uefi_root 24 | 25 | cd $SRC_DIR/work/release 26 | 27 | tar -cvf $RELEASE_NAME.tar $RELEASE_NAME 28 | 29 | xz -z -k -9 -e systemd-boot_${DATE_PARSED}.tar 30 | 31 | cat << CEOF 32 | 33 | ############################################################ 34 | # # 35 | # systemd-boot release '${DATE_PARSED}' has been generated. # 36 | # # 37 | # work/release/systemd-boot_${DATE_PARSED}.tar.xz # 38 | # # 39 | ############################################################ 40 | 41 | CEOF 42 | -------------------------------------------------------------------------------- /project/autogen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # This file is part of systemd-boot 4 | # 5 | # Copyright (C) 2013-2016 Karel Zak 6 | # Copyright (C) 2016 Michal Sekletar 7 | # 8 | # systemd-boot is free software; you can redistribute it and/or modify it 9 | # under the terms of the GNU Lesser General Public License as published by 10 | # the Free Software Foundation; either version 2.1 of the License, or 11 | # (at your option) any later version. 12 | # 13 | # systemd-boot is distributed in the hope that it will be useful, but 14 | # WITHOUT ANY WARRANTY; without even the implied warranty of 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 | # Lesser General Public License for more details. 17 | # 18 | # You should have received a copy of the GNU Lesser General Public License 19 | # along with systemd; If not, see . 20 | 21 | set -e 22 | 23 | autoreconf --force --install --symlink 24 | 25 | args="--prefix=/usr" 26 | 27 | if [ "x$1" = "xc" ]; then 28 | ./configure $args 29 | make clean 30 | else 31 | echo 32 | echo "----------------------------------------------------------------" 33 | echo "Initialized build system. For a common configuration please run:" 34 | echo "----------------------------------------------------------------" 35 | echo 36 | echo "./configure $args" 37 | echo 38 | fi 39 | -------------------------------------------------------------------------------- /project/src/sd-boot/console.h: -------------------------------------------------------------------------------- 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) 2012-2013 Kay Sievers 14 | * Copyright (C) 2012 Harald Hoyer 15 | */ 16 | 17 | #ifndef __SDBOOT_CONSOLE_H 18 | #define __SDBOOT_CONSOLE_H 19 | 20 | #define EFI_SHIFT_STATE_VALID 0x80000000 21 | #define EFI_RIGHT_CONTROL_PRESSED 0x00000004 22 | #define EFI_LEFT_CONTROL_PRESSED 0x00000008 23 | #define EFI_RIGHT_ALT_PRESSED 0x00000010 24 | #define EFI_LEFT_ALT_PRESSED 0x00000020 25 | 26 | #define EFI_CONTROL_PRESSED (EFI_RIGHT_CONTROL_PRESSED|EFI_LEFT_CONTROL_PRESSED) 27 | #define EFI_ALT_PRESSED (EFI_RIGHT_ALT_PRESSED|EFI_LEFT_ALT_PRESSED) 28 | #define KEYPRESS(keys, scan, uni) ((((UINT64)keys) << 32) | ((scan) << 16) | (uni)) 29 | #define KEYCHAR(k) ((k) & 0xffff) 30 | #define CHAR_CTRL(c) ((c) - 'a' + 1) 31 | 32 | enum console_mode_change_type { 33 | CONSOLE_MODE_KEEP = 0, 34 | CONSOLE_MODE_SET, 35 | CONSOLE_MODE_AUTO, 36 | CONSOLE_MODE_MAX, 37 | }; 38 | 39 | EFI_STATUS console_key_read(UINT64 *key, BOOLEAN wait); 40 | EFI_STATUS console_set_mode(UINTN *mode, enum console_mode_change_type how); 41 | #endif 42 | -------------------------------------------------------------------------------- /project/test/test-efi-create-disk.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | 3 | # create GPT table with EFI System Partition 4 | rm -f test-efi-disk.img 5 | dd if=/dev/null of=test-efi-disk.img bs=1M seek=512 count=1 6 | parted --script test-efi-disk.img "mklabel gpt" "mkpart ESP fat32 1MiB 511MiB" "set 1 boot on" 7 | 8 | # create FAT32 file system 9 | LOOP=$(losetup --show -f -P test-efi-disk.img) 10 | mkfs.vfat -F32 ${LOOP}p1 11 | mkdir -p mnt 12 | mount ${LOOP}p1 mnt 13 | 14 | mkdir -p mnt/EFI/{BOOT,systemd} 15 | cp systemd-bootx64.efi mnt/EFI/BOOT/BOOTX64.efi 16 | 17 | [ -e /boot/shellx64.efi ] && cp /boot/shellx64.efi mnt/ 18 | 19 | mkdir mnt/EFI/Linux 20 | echo -n "foo=yes bar=no root=/dev/fakeroot debug rd.break=initqueue" > mnt/cmdline.txt 21 | objcopy \ 22 | --add-section .osrel=/etc/os-release --change-section-vma .osrel=0x20000 \ 23 | --add-section .cmdline=mnt/cmdline.txt --change-section-vma .cmdline=0x30000 \ 24 | --add-section .splash=test/splash.bmp --change-section-vma .splash=0x40000 \ 25 | --add-section .linux=/boot/$(cat /etc/machine-id)/$(uname -r)/linux --change-section-vma .linux=0x2000000 \ 26 | --add-section .initrd=/boot/$(cat /etc/machine-id)/$(uname -r)/initrd --change-section-vma .initrd=0x3000000 \ 27 | linuxx64.efi.stub mnt/EFI/Linux/linux-test.efi 28 | 29 | # install entries 30 | mkdir -p mnt/loader/entries 31 | echo -e "timeout 3\n" > mnt/loader/loader.conf 32 | echo -e "title Test\nefi /test\n" > mnt/loader/entries/test.conf 33 | echo -e "title Test2\nlinux /test2\noptions option=yes word number=1000 more\n" > mnt/loader/entries/test2.conf 34 | echo -e "title Test3\nlinux /test3\n" > mnt/loader/entries/test3.conf 35 | echo -e "title Test4\nlinux /test4\n" > mnt/loader/entries/test4.conf 36 | echo -e "title Test5\nefi /test5\n" > mnt/loader/entries/test5.conf 37 | echo -e "title Test6\nlinux /test6\n" > mnt/loader/entries/test6.conf 38 | 39 | sync 40 | umount mnt 41 | rmdir mnt 42 | losetup -d $LOOP 43 | -------------------------------------------------------------------------------- /project/shell-completion/bash/kernel-install: -------------------------------------------------------------------------------- 1 | # kernel-install(8) completion -*- shell-script -*- 2 | # 3 | # This file is part of systemd. 4 | # 5 | # Copyright 2013 Kay Sievers 6 | # Copyright 2013 Harald Hoyer 7 | # 8 | # systemd is free software; you can redistribute it and/or modify it 9 | # under the terms of the GNU Lesser General Public License as published by 10 | # the Free Software Foundation; either version 2.1 of the License, or 11 | # (at your option) any later version. 12 | # 13 | # systemd is distributed in the hope that it will be useful, but 14 | # WITHOUT ANY WARRANTY; without even the implied warranty of 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 | # General Public License for more details. 17 | # 18 | # You should have received a copy of the GNU Lesser General Public License 19 | # along with systemd; If not, see . 20 | 21 | _kernel_install() { 22 | local comps 23 | local MACHINE_ID 24 | local cur=${COMP_WORDS[COMP_CWORD]} 25 | 26 | case $COMP_CWORD in 27 | 1) 28 | comps="add remove" 29 | ;; 30 | 2) 31 | comps=$(cd /lib/modules; echo [0-9]*) 32 | if [[ ${COMP_WORDS[1]} == "remove" ]] && [[ -f /etc/machine-id ]]; then 33 | read MACHINE_ID < /etc/machine-id 34 | if [[ $MACHINE_ID ]] && ( [[ -d /boot/$MACHINE_ID ]] || [[ -L /boot/$MACHINE_ID ]] ); then 35 | comps=$(cd "/boot/$MACHINE_ID"; echo [0-9]*) 36 | fi 37 | fi 38 | ;; 39 | 3) 40 | [[ "$cur" ]] || cur=/boot/vmlinuz-${COMP_WORDS[2]} 41 | comps=$(compgen -f -- "$cur") 42 | compopt -o filenames 43 | ;; 44 | esac 45 | 46 | COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) 47 | return 0 48 | } 49 | 50 | complete -F _kernel_install kernel-install 51 | -------------------------------------------------------------------------------- /project/src/sd-boot/disk.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 14 | */ 15 | 16 | #include 17 | #include 18 | 19 | #include "util.h" 20 | 21 | EFI_STATUS disk_get_part_uuid(EFI_HANDLE *handle, CHAR16 uuid[37]) { 22 | EFI_DEVICE_PATH *device_path; 23 | 24 | /* export the device path this image is started from */ 25 | device_path = DevicePathFromHandle(handle); 26 | if (device_path) { 27 | _cleanup_freepool_ EFI_DEVICE_PATH *paths = NULL; 28 | EFI_DEVICE_PATH *path; 29 | 30 | paths = UnpackDevicePath(device_path); 31 | for (path = paths; !IsDevicePathEnd(path); path = NextDevicePathNode(path)) { 32 | HARDDRIVE_DEVICE_PATH *drive; 33 | 34 | if (DevicePathType(path) != MEDIA_DEVICE_PATH) 35 | continue; 36 | if (DevicePathSubType(path) != MEDIA_HARDDRIVE_DP) 37 | continue; 38 | drive = (HARDDRIVE_DEVICE_PATH *)path; 39 | if (drive->SignatureType != SIGNATURE_TYPE_GUID) 40 | continue; 41 | 42 | GuidToString(uuid, (EFI_GUID *)&drive->Signature); 43 | return EFI_SUCCESS; 44 | } 45 | } 46 | 47 | return EFI_NOT_FOUND; 48 | } 49 | -------------------------------------------------------------------------------- /prepare_third_party_software.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | set -ex 4 | 5 | SRC_DIR=$PWD 6 | 7 | ovmf_x86() { 8 | mkdir -p work/ovmf 9 | cd work/ovmf 10 | wget --max-redirect 999 -O ovmf-32.zip -c https://sourceforge.net/projects/edk2/files/OVMF/OVMF-IA32-r15214.zip 11 | unzip -p ovmf-32.zip OVMF.fd > ovmf-32.fd 12 | cd $SRC_DIR 13 | } 14 | 15 | ovmf_x86_64() { 16 | mkdir -p work/ovmf 17 | cd work/ovmf 18 | wget --max-redirect 999 -O ovmf-64.zip -c https://sourceforge.net/projects/edk2/files/OVMF/OVMF-X64-r15214.zip 19 | unzip -p ovmf-64.zip OVMF.fd > ovmf-64.fd 20 | cd $SRC_DIR 21 | } 22 | 23 | syslinux() { 24 | mkdir -p work/syslinux 25 | cd work/syslinux 26 | wget -O syslinux.tar.xz -c http://kernel.org/pub/linux/utils/boot/syslinux/syslinux-6.03.tar.xz 27 | tar -xvf syslinux.tar.xz 28 | cp -r syslinux-*/* . 29 | cd $SRC_DIR 30 | } 31 | 32 | mll_32() { 33 | mkdir -p work/mll 34 | cd work/mll 35 | wget -O mll-32.iso -c http://minimal.idzona.com/download/2017/minimal_linux_live_20-Jan-2017_32-bit.iso 36 | rm -rf mll-32 37 | xorriso -osirrox on -indev mll-32.iso -extract / mll-32 38 | chmod -R ugo+rw mll-32 mll-32/* 39 | cp mll-32/kernel.xz kernel-32.xz 40 | cp mll-32/rootfs.xz rootfs-32.xz 41 | cd $SRC_DIR 42 | } 43 | 44 | mll_64() { 45 | mkdir -p work/mll 46 | cd work/mll 47 | wget -O mll-64.iso -c http://minimal.idzona.com/download/2017/minimal_linux_live_20-Jan-2017_64-bit.iso 48 | rm -rf mll-64 49 | xorriso -osirrox on -indev mll-64.iso -extract / mll-64 50 | chmod -R ugo+rw mll-64 mll-64/* 51 | cp mll-64/kernel.xz kernel-64.xz 52 | cp mll-64/rootfs.xz rootfs-64.xz 53 | cd $SRC_DIR 54 | } 55 | 56 | systemd_boot_precompiled() { 57 | rm -rf work/uefi_root 58 | wget -O work/systemd-boot.tar.xz -c \ 59 | https://github.com/ivandavidov/systemd-boot/releases/download/systemd-boot_26-May-2018/systemd-boot_26-May-2018.tar.xz 60 | cd work 61 | tar -xvf systemd-boot.tar.xz 62 | cd `ls -d systemd-boot_*` 63 | cp -r * .. 64 | cd .. 65 | rm -rf systemd-boot* 66 | cd $SRC_DIR 67 | } 68 | 69 | ovmf_x86 70 | ovmf_x86_64 71 | syslinux 72 | mll_32 73 | mll_64 74 | systemd_boot_precompiled 75 | 76 | cd $SRC_DIR 77 | -------------------------------------------------------------------------------- /project/src/sd-boot/util.h: -------------------------------------------------------------------------------- 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) 2012-2013 Kay Sievers 14 | * Copyright (C) 2012 Harald Hoyer 15 | */ 16 | 17 | #ifndef __SDBOOT_UTIL_H 18 | #define __SDBOOT_UTIL_H 19 | 20 | #include 21 | #include 22 | 23 | #define ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0])) 24 | 25 | static inline const CHAR16 *yes_no(BOOLEAN b) { 26 | return b ? L"yes" : L"no"; 27 | } 28 | 29 | EFI_STATUS parse_boolean(CHAR8 *v, BOOLEAN *b); 30 | 31 | UINT64 ticks_read(void); 32 | UINT64 ticks_freq(void); 33 | UINT64 time_usec(void); 34 | 35 | EFI_STATUS efivar_set(CHAR16 *name, CHAR16 *value, BOOLEAN persistent); 36 | EFI_STATUS efivar_set_raw(const EFI_GUID *vendor, CHAR16 *name, CHAR8 *buf, UINTN size, BOOLEAN persistent); 37 | EFI_STATUS efivar_set_int(CHAR16 *name, UINTN i, BOOLEAN persistent); 38 | VOID efivar_set_time_usec(CHAR16 *name, UINT64 usec); 39 | 40 | EFI_STATUS efivar_get(CHAR16 *name, CHAR16 **value); 41 | EFI_STATUS efivar_get_raw(const EFI_GUID *vendor, CHAR16 *name, CHAR8 **buffer, UINTN *size); 42 | EFI_STATUS efivar_get_int(CHAR16 *name, UINTN *i); 43 | 44 | CHAR8 *strchra(CHAR8 *s, CHAR8 c); 45 | CHAR16 *stra_to_path(CHAR8 *stra); 46 | CHAR16 *stra_to_str(CHAR8 *stra); 47 | 48 | EFI_STATUS file_read(EFI_FILE_HANDLE dir, CHAR16 *name, UINTN off, UINTN size, CHAR8 **content, UINTN *content_size); 49 | #endif 50 | 51 | static inline void FreePoolp(void *p) { 52 | FreePool(*(void**) p); 53 | } 54 | 55 | #define _cleanup_(x) __attribute__((cleanup(x))) 56 | #define _cleanup_freepool_ _cleanup_(FreePoolp) 57 | -------------------------------------------------------------------------------- /project/src/kernel-install/90-loaderentry.install: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*- 3 | # ex: ts=8 sw=4 sts=4 et filetype=sh 4 | 5 | COMMAND="$1" 6 | KERNEL_VERSION="$2" 7 | BOOT_DIR_ABS="$3" 8 | KERNEL_IMAGE="$4" 9 | 10 | if [[ -f /etc/machine-id ]]; then 11 | read MACHINE_ID < /etc/machine-id 12 | fi 13 | 14 | if ! [[ $MACHINE_ID ]]; then 15 | exit 1 16 | fi 17 | 18 | BOOT_DIR="/$MACHINE_ID/$KERNEL_VERSION" 19 | BOOT_ROOT=${BOOT_DIR_ABS%$BOOT_DIR} 20 | LOADER_ENTRY="$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION.conf" 21 | 22 | if [[ $COMMAND == remove ]]; then 23 | exec rm -f "$LOADER_ENTRY" 24 | fi 25 | 26 | if ! [[ $COMMAND == add ]]; then 27 | exit 1 28 | fi 29 | 30 | if ! [[ $KERNEL_IMAGE ]]; then 31 | exit 1 32 | fi 33 | 34 | if [[ -f /etc/os-release ]]; then 35 | . /etc/os-release 36 | elif [[ -f /usr/lib/os-release ]]; then 37 | . /usr/lib/os-release 38 | fi 39 | 40 | if ! [[ $PRETTY_NAME ]]; then 41 | PRETTY_NAME="Linux $KERNEL_VERSION" 42 | fi 43 | 44 | declare -a BOOT_OPTIONS 45 | 46 | if [[ -f /etc/kernel/cmdline ]]; then 47 | read -r -d '' -a BOOT_OPTIONS < /etc/kernel/cmdline 48 | fi 49 | 50 | if ! [[ ${BOOT_OPTIONS[*]} ]]; then 51 | read -r -d '' -a line < /proc/cmdline 52 | for i in "${line[@]}"; do 53 | [[ "${i#initrd=*}" != "$i" ]] && continue 54 | BOOT_OPTIONS+=("$i") 55 | done 56 | fi 57 | 58 | if ! [[ ${BOOT_OPTIONS[*]} ]]; then 59 | echo "Could not determine the kernel command line parameters." >&2 60 | echo "Please specify the kernel command line in /etc/kernel/cmdline!" >&2 61 | exit 1 62 | fi 63 | 64 | cp "$KERNEL_IMAGE" "$BOOT_DIR_ABS/linux" && 65 | chown root:root "$BOOT_DIR_ABS/linux" && 66 | chmod 0644 "$BOOT_DIR_ABS/linux" || { 67 | echo "Could not copy '$KERNEL_IMAGE to '$BOOT_DIR_ABS/linux'." >&2 68 | exit 1 69 | } 70 | 71 | mkdir -p "${LOADER_ENTRY%/*}" || { 72 | echo "Could not create loader entry directory '${LOADER_ENTRY%/*}'." >&2 73 | exit 1 74 | } 75 | 76 | { 77 | echo "title $PRETTY_NAME" 78 | echo "version $KERNEL_VERSION" 79 | echo "machine-id $MACHINE_ID" 80 | echo "options ${BOOT_OPTIONS[*]}" 81 | echo "linux $BOOT_DIR/linux" 82 | [[ -f $BOOT_DIR_ABS/initrd ]] && \ 83 | echo "initrd $BOOT_DIR/initrd" 84 | : 85 | } > "$LOADER_ENTRY" || { 86 | echo "Could not create loader entry '$LOADER_ENTRY'." >&2 87 | exit 1 88 | } 89 | exit 0 90 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## systemd-boot 2 | 3 | ``systemd-boot`` (previously known as [gummyboot](https://en.wikipedia.org/wiki/Gummiboot_(software))) is a simple UEFI boot manager. Its main job is to launch the selected boot menu entry. 'systemd-boot' leverages APIs provided by the UEFI and offloads all the heavy lifting to the firmware (e.g. loading files from disk, executing EFI images). This allows for very minimal implementation, but feature complete to support common usecases (desktop, laptop). 4 | 5 | Precompiled 'systemd-boot' UEFI images and sample configuration files are available in the [release](https://github.com/ivandavidov/systemd-boot/releases) section. The latest stable release is [systemd-boot_26-May-2018](https://github.com/ivandavidov/systemd-boot/releases/tag/systemd-boot_26-May-2018). Each release contains the following artifacts: 6 | 7 | * UEFI compliant directory structure with general purpose 'systemd-boot' UEFI boot loader images for 'x86' and 'x86_64' architectures. 8 | * Sample configuration files for 'x86' and 'x86_64' machines which describe the boot entries for [Minimal Linux Live](http://github.com/ivandavidov/minimal "Minimal Linux Live"). 9 | 10 | The primary use case of this project is to provide precompiled general purpose 'systemd-boot' UEFI boot loader images which in turn support the UEFI boot process of Minimal Linux Live. 11 | 12 | The raw source code, along with build documentation can be found in the [project](https://github.com/ivandavidov/systemd-boot/tree/master/project) folder. Most probably you don't need it, unless you really want to build 'systemd-boot' from scratch. 13 | 14 | The shell scripts in the main folder rely on the precompiled binaries and on other third party software. You don't need these scripts, nor you need the third party software, unless you want to build custom Minimal Linux Live ISO image which boots on UEFI systems. This serves as proof of concept that the precompiled binaries work fine. 15 | 16 | ## How to use 17 | 18 | Good documentation regarding 'systemd-boot' can be found here: 19 | 20 | * [freedesktop.org - systemd-boot UEFI Boot Manager](http://www.freedesktop.org/wiki/Software/systemd/systemd-boot) 21 | * [wiki.archlinux.org - systemd-boot](http://wiki.archlinux.org/index.php/Systemd-boot) 22 | 23 | The helper scripts in the main folder do the following: 24 | 25 | * Download and prepare third party software dependencies, e.g. the precompiled UEFI boot loader images, OVMF images, Syslinux, sample kernel/initramfs files, etc. 26 | * Genrate sample 'El Torito' boot image as described in UEFI specification 2.7, sections 13.3.1.x and 13.3.2.x. 27 | * Generate sample ISO image with UEFI boot support. 28 | * Run [QEMU](https://www.qemu.org) with UEFI enabled configuration and attached sample ISO image. 29 | 30 | Note that you need QEMU on your system in order to test the sample ISO images. You can install it like this (Ubuntu): 31 | 32 | * ``sudo apt update && sudo apt install qemu`` 33 | 34 | Please consider all helper scritps just as helper scripts. You can use them to gain some more knowledge about the 'systemd-boot' UEFI boot loader insfrastructure and how to create UEFI compatible ISO images. 35 | -------------------------------------------------------------------------------- /generate_mll_iso_32.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | set -ex 4 | 5 | SRC_DIR=$PWD 6 | 7 | cleanup() { 8 | ORIG_USER=`who | awk '{print \$1}'` 9 | chown -R $ORIG_USER:$ORIG_USER $SRC_DIR/work/* 10 | } 11 | 12 | # Genrate 'El Torito' boot image as per UEFI sepcification 2.7, 13 | # sections 13.3.1.x and 13.3.2.x. 14 | generate_uefi_boot_image() ( 15 | kernel_size=`du -b $SRC_DIR/work/mll/kernel-32.xz | awk '{print \$1}'` 16 | rootfs_size=`du -b $SRC_DIR/work/mll/rootfs-32.xz | awk '{print \$1}'` 17 | loader_size=`du -b $SRC_DIR/work/uefi_root/EFI/BOOT/BOOTIA32.EFI | awk '{print \$1}'` 18 | 19 | image_size=$((kernel_size + rootfs_size + loader_size + 65536)) 20 | 21 | rm -f $SRC_DIR/work/uefi_32.img 22 | truncate -s $image_size $SRC_DIR/work/uefi_32.img 23 | 24 | LOOP_DEVICE_HDD=$(losetup -f) 25 | losetup $LOOP_DEVICE_HDD $SRC_DIR/work/uefi_32.img 26 | 27 | mkfs.vfat $LOOP_DEVICE_HDD 28 | 29 | rm -rf $SRC_DIR/work/uefi_32_image 30 | mkdir -p $SRC_DIR/work/uefi_32_image 31 | mount $SRC_DIR/work/uefi_32.img $SRC_DIR/work/uefi_32_image 32 | 33 | # The default image file names are described in UEFI 34 | # specification 2.7, section 3.5.1.1. Note that the 35 | # x86_64 UEFI image file name indeed contains small 36 | # letter 'x'. 37 | mkdir -p $SRC_DIR/work/uefi_32_image/EFI/BOOT 38 | cp $SRC_DIR/work/uefi_root/EFI/BOOT/BOOTIA32.EFI \ 39 | $SRC_DIR/work/uefi_32_image/EFI/BOOT 40 | 41 | mkdir -p $SRC_DIR/work/uefi_32_image/loader/entries 42 | cp $SRC_DIR/samples/uefi_root/loader/loader.conf \ 43 | $SRC_DIR/work/uefi_32_image/loader 44 | cp $SRC_DIR/samples/uefi_root/loader/entries/mll-x86.conf \ 45 | $SRC_DIR/work/uefi_32_image/loader/entries 46 | 47 | mkdir -p $SRC_DIR/work/uefi_32_image/minimal/x86 48 | cp $SRC_DIR/work/mll/kernel-32.xz \ 49 | $SRC_DIR/work/uefi_32_image/minimal/x86/kernel.xz 50 | cp $SRC_DIR/work/mll/rootfs-32.xz \ 51 | $SRC_DIR/work/uefi_32_image/minimal/x86/rootfs.xz 52 | 53 | sync 54 | umount $SRC_DIR/work/uefi_32_image 55 | sync 56 | sleep 1 57 | 58 | rm -rf $SRC_DIR/work/uefi_32_image 59 | 60 | chmod ugo+r $SRC_DIR/work/uefi_32.img 61 | ) 62 | 63 | check_root() { 64 | if [ ! "$(id -u)" = "0" ] ; then 65 | cat << CEOF 66 | 67 | ISO image preparation process for UEFI systems requires root permissions 68 | but you don't have such permissions. Restart this script with root 69 | permissions in order to generate UEFI compatible ISO image. 70 | 71 | CEOF 72 | 73 | exit 1 74 | fi 75 | } 76 | 77 | prepare_iso_structure() { 78 | rm -rf $SRC_DIR/work/isoimage_32_root 79 | mkdir -p $SRC_DIR/work/isoimage_32_root 80 | 81 | cp $SRC_DIR/work/uefi_32.img \ 82 | $SRC_DIR/work/isoimage_32_root 83 | } 84 | 85 | generate_iso_image() { 86 | xorriso -as mkisofs \ 87 | -isohybrid-mbr $SRC_DIR/work/syslinux/bios/mbr/isohdpfx.bin \ 88 | -c boot.cat \ 89 | -e uefi_32.img \ 90 | -no-emul-boot \ 91 | -isohybrid-gpt-basdat \ 92 | -o $SRC_DIR/work/mll_32.iso \ 93 | $SRC_DIR/work/isoimage_32_root 94 | } 95 | 96 | check_root 97 | generate_uefi_boot_image 98 | prepare_iso_structure 99 | generate_iso_image 100 | cleanup 101 | -------------------------------------------------------------------------------- /generate_mll_iso_64.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | set -ex 4 | 5 | SRC_DIR=$PWD 6 | 7 | cleanup() { 8 | ORIG_USER=`who | awk '{print \$1}'` 9 | chown -R $ORIG_USER:$ORIG_USER $SRC_DIR/work/* 10 | } 11 | 12 | # Genrate 'El Torito' boot image as per UEFI sepcification 2.7, 13 | # sections 13.3.1.x and 13.3.2.x. 14 | generate_uefi_boot_image() ( 15 | kernel_size=`du -b $SRC_DIR/work/mll/kernel-64.xz | awk '{print \$1}'` 16 | rootfs_size=`du -b $SRC_DIR/work/mll/rootfs-64.xz | awk '{print \$1}'` 17 | loader_size=`du -b $SRC_DIR/work/uefi_root/EFI/BOOT/BOOTx64.EFI | awk '{print \$1}'` 18 | 19 | image_size=$((kernel_size + rootfs_size + loader_size + 65536)) 20 | 21 | rm -f $SRC_DIR/work/uefi_64.img 22 | truncate -s $image_size $SRC_DIR/work/uefi_64.img 23 | 24 | LOOP_DEVICE_HDD=$(losetup -f) 25 | losetup $LOOP_DEVICE_HDD $SRC_DIR/work/uefi_64.img 26 | 27 | mkfs.vfat $LOOP_DEVICE_HDD 28 | 29 | rm -rf $SRC_DIR/work/uefi_64_image 30 | mkdir -p $SRC_DIR/work/uefi_64_image 31 | mount $SRC_DIR/work/uefi_64.img $SRC_DIR/work/uefi_64_image 32 | 33 | # The default image file names are described in UEFI 34 | # specification 2.7, section 3.5.1.1. Note that the 35 | # x86_64 UEFI image file name indeed contains small 36 | # letter 'x'. 37 | mkdir -p $SRC_DIR/work/uefi_64_image/EFI/BOOT 38 | cp $SRC_DIR/work/uefi_root/EFI/BOOT/BOOTx64.EFI \ 39 | $SRC_DIR/work/uefi_64_image/EFI/BOOT 40 | 41 | mkdir -p $SRC_DIR/work/uefi_64_image/loader/entries 42 | cp $SRC_DIR/samples/uefi_root/loader/loader.conf \ 43 | $SRC_DIR/work/uefi_64_image/loader 44 | cp $SRC_DIR/samples/uefi_root/loader/entries/mll-x86_64.conf \ 45 | $SRC_DIR/work/uefi_64_image/loader/entries 46 | 47 | mkdir -p $SRC_DIR/work/uefi_64_image/minimal/x86_64 48 | cp $SRC_DIR/work/mll/kernel-64.xz \ 49 | $SRC_DIR/work/uefi_64_image/minimal/x86_64/kernel.xz 50 | cp $SRC_DIR/work/mll/rootfs-64.xz \ 51 | $SRC_DIR/work/uefi_64_image/minimal/x86_64/rootfs.xz 52 | 53 | sync 54 | umount $SRC_DIR/work/uefi_64_image 55 | sync 56 | sleep 1 57 | 58 | rm -rf $SRC_DIR/work/uefi_64_image 59 | 60 | chmod ugo+r $SRC_DIR/work/uefi_64.img 61 | ) 62 | 63 | check_root() { 64 | if [ ! "$(id -u)" = "0" ] ; then 65 | cat << CEOF 66 | 67 | ISO image preparation process for UEFI systems requires root permissions 68 | but you don't have such permissions. Restart this script with root 69 | permissions in order to generate UEFI compatible ISO image. 70 | 71 | CEOF 72 | 73 | exit 1 74 | fi 75 | } 76 | 77 | prepare_iso_structure() { 78 | rm -rf $SRC_DIR/work/isoimage_64_root 79 | mkdir -p $SRC_DIR/work/isoimage_64_root 80 | 81 | cp $SRC_DIR/work/uefi_64.img \ 82 | $SRC_DIR/work/isoimage_64_root 83 | } 84 | 85 | generate_iso_image() { 86 | xorriso -as mkisofs \ 87 | -isohybrid-mbr $SRC_DIR/work/syslinux/bios/mbr/isohdpfx.bin \ 88 | -c boot.cat \ 89 | -e uefi_64.img \ 90 | -no-emul-boot \ 91 | -isohybrid-gpt-basdat \ 92 | -o $SRC_DIR/work/mll_64.iso \ 93 | $SRC_DIR/work/isoimage_64_root 94 | } 95 | 96 | check_root 97 | generate_uefi_boot_image 98 | prepare_iso_structure 99 | generate_iso_image 100 | cleanup 101 | -------------------------------------------------------------------------------- /project/README.md: -------------------------------------------------------------------------------- 1 | ## systemd-boot 2 | 3 | ``systemd-boot`` is a simple UEFI boot manager. Its main job is to launch the selected boot menu entry. 'systemd-boot' leverages APIs provided by the UEFI and offloads all the heavy lifting to the firmware (e.g. loading files from disk, executing EFI images). This allows for very minimal implementation, but feature complete to support common usecases (desktop, laptop). 4 | 5 | 'systemd-boot' fully supports the [Freedesktop Boot Loader Specification](https://www.freedesktop.org/wiki/Specifications/BootLoaderSpec). 6 | 7 | ## Installation 8 | 9 | If you want to build 'systemd-boot' from scratch, then you'll need to add all [Minimal Linux Live](http://github.com/ivandavidov/minimal "Minimal Linux Live") dependencies and the 'systemd-boot' build dependencies (Ubuntu): 10 | 11 | * ``sudo apt update && sudo apt install wget make gawk gcc bc xorriso gnu-efi dh-autoreconf`` 12 | 13 | If you have already built Minimal Linux Live, then you need to add just 2 more build dependencies (Ubuntu): 14 | 15 | * ``sudo apt update && sudo apt install gnu-efi dh-autoreconf`` 16 | 17 | These are the steps you need to follow in order to build and install 'systemd-boot': 18 | 19 | ``` 20 | # Assuming you are in the main GitHub project folder 21 | 22 | # Remove the old artifacts. 23 | rm -rf systemd-boot_installed 24 | 25 | # Navigate to the main project directory. 26 | cd project 27 | 28 | # Configure, build and install 'systemd-boot' in local folder. 29 | ./autogen.sh 30 | ./configure --prefix=$PWD/../systemd-boot_installed/usr 31 | make 32 | make install 33 | ``` 34 | 35 | The above set of commands will preapre and install all 'systemd-boot' artifacts in the following local folder: 36 | 37 | * ``systemd-boot_installed/usr`` 38 | 39 | You can find the generated EFI boot loader image here: 40 | 41 | * ``systemd-boot_installed/usr/lib/systemd-boot/systemd-boot{ARCH}.efi`` 42 | 43 | For your convenience, the above described build process has been automated in the [build_project.sh](https://github.com/ivandavidov/systemd-boot/blob/master/build_project.sh) shell script. 44 | 45 | On 32-bit x86 machines you may need to manually tweak the 'configure.ac' script and/or some of the 'gnu-efi' header locations, because the 'gnu-efi' headers are installed for 'ia32' architectures but the actual architecture is most likely 'i686' or similar. The same applies for the 'gnu-efi' linker which contains the architecture in its file name (e.g. 'ia32') and it may not be resolved if the actual architecture is 'i686' or similar. 46 | 47 | I had no such build issues on 'x86_64' machine. 48 | 49 | Note that installation of 'sytemd-boot' to the EFI System Partition (if you want to use 'systemd-boot' as main boot loader on your machine) must be handled separately. It is usually done by ``bootctl`` command line utility from the 'systemd' package. 50 | 51 | ## How to use 52 | 53 | Good documentation regarding 'systemd-boot' can be found here: 54 | 55 | * [freedesktop.org - systemd-boot UEFI Boot Manager](http://www.freedesktop.org/wiki/Software/systemd/systemd-boot) 56 | * [wiki.archlinux.org - systemd-boot](http://wiki.archlinux.org/index.php/Systemd-boot) 57 | 58 | ## Dependencies 59 | 60 | * gnu-efi - mandatory build dependency 61 | * dh-autoreconf - mandatory build dependency 62 | * qemu - optional 63 | * OVMF - optional 64 | * Syslinux - optional 65 | 66 | ## Authors 67 | 68 | * Kay Sievers 69 | * Harald Hoyer 70 | * ... and many others (for complete list see git history [here](https://www.github.com/systemd/systemd)). 71 | -------------------------------------------------------------------------------- /project/src/sd-boot/graphics.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) 2012-2013 Kay Sievers 14 | * Copyright (C) 2012 Harald Hoyer 15 | * Copyright (C) 2013 Intel Corporation 16 | * Authored by Joonas Lahtinen 17 | */ 18 | 19 | #include 20 | #include 21 | 22 | #include "graphics.h" 23 | #include "util.h" 24 | 25 | EFI_STATUS graphics_mode(BOOLEAN on) { 26 | #define EFI_CONSOLE_CONTROL_PROTOCOL_GUID \ 27 | { 0xf42f7782, 0x12e, 0x4c12, { 0x99, 0x56, 0x49, 0xf9, 0x43, 0x4, 0xf7, 0x21 } }; 28 | 29 | struct _EFI_CONSOLE_CONTROL_PROTOCOL; 30 | 31 | typedef enum { 32 | EfiConsoleControlScreenText, 33 | EfiConsoleControlScreenGraphics, 34 | EfiConsoleControlScreenMaxValue, 35 | } EFI_CONSOLE_CONTROL_SCREEN_MODE; 36 | 37 | typedef EFI_STATUS (EFIAPI *EFI_CONSOLE_CONTROL_PROTOCOL_GET_MODE)( 38 | struct _EFI_CONSOLE_CONTROL_PROTOCOL *This, 39 | EFI_CONSOLE_CONTROL_SCREEN_MODE *Mode, 40 | BOOLEAN *UgaExists, 41 | BOOLEAN *StdInLocked 42 | ); 43 | 44 | typedef EFI_STATUS (EFIAPI *EFI_CONSOLE_CONTROL_PROTOCOL_SET_MODE)( 45 | struct _EFI_CONSOLE_CONTROL_PROTOCOL *This, 46 | EFI_CONSOLE_CONTROL_SCREEN_MODE Mode 47 | ); 48 | 49 | typedef EFI_STATUS (EFIAPI *EFI_CONSOLE_CONTROL_PROTOCOL_LOCK_STD_IN)( 50 | struct _EFI_CONSOLE_CONTROL_PROTOCOL *This, 51 | CHAR16 *Password 52 | ); 53 | 54 | typedef struct _EFI_CONSOLE_CONTROL_PROTOCOL { 55 | EFI_CONSOLE_CONTROL_PROTOCOL_GET_MODE GetMode; 56 | EFI_CONSOLE_CONTROL_PROTOCOL_SET_MODE SetMode; 57 | EFI_CONSOLE_CONTROL_PROTOCOL_LOCK_STD_IN LockStdIn; 58 | } EFI_CONSOLE_CONTROL_PROTOCOL; 59 | 60 | EFI_GUID ConsoleControlProtocolGuid = EFI_CONSOLE_CONTROL_PROTOCOL_GUID; 61 | EFI_CONSOLE_CONTROL_PROTOCOL *ConsoleControl = NULL; 62 | EFI_CONSOLE_CONTROL_SCREEN_MODE new; 63 | EFI_CONSOLE_CONTROL_SCREEN_MODE current; 64 | BOOLEAN uga_exists; 65 | BOOLEAN stdin_locked; 66 | EFI_STATUS err; 67 | 68 | err = LibLocateProtocol(&ConsoleControlProtocolGuid, (VOID **)&ConsoleControl); 69 | if (EFI_ERROR(err)) 70 | /* console control protocol is nonstandard and might not exist. */ 71 | return err == EFI_NOT_FOUND ? EFI_SUCCESS : err; 72 | 73 | /* check current mode */ 74 | err = uefi_call_wrapper(ConsoleControl->GetMode, 4, ConsoleControl, ¤t, &uga_exists, &stdin_locked); 75 | if (EFI_ERROR(err)) 76 | return err; 77 | 78 | /* do not touch the mode */ 79 | new = on ? EfiConsoleControlScreenGraphics : EfiConsoleControlScreenText; 80 | if (new == current) 81 | return EFI_SUCCESS; 82 | 83 | err = uefi_call_wrapper(ConsoleControl->SetMode, 2, ConsoleControl, new); 84 | 85 | /* some firmware enables the cursor when switching modes */ 86 | uefi_call_wrapper(ST->ConOut->EnableCursor, 2, ST->ConOut, FALSE); 87 | 88 | return err; 89 | } 90 | -------------------------------------------------------------------------------- /project/src/kernel-install/kernel-install: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*- 3 | # ex: ts=8 sw=4 sts=4 et filetype=sh 4 | # 5 | # This file is part of systemd. 6 | # 7 | # Copyright 2013 Harald Hoyer 8 | # 9 | # systemd is free software; you can redistribute it and/or modify it 10 | # under the terms of the GNU Lesser General Public License as published by 11 | # the Free Software Foundation; either version 2.1 of the License, or 12 | # (at your option) any later version. 13 | # 14 | # systemd is distributed in the hope that it will be useful, but 15 | # WITHOUT ANY WARRANTY; without even the implied warranty of 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | # General Public License for more details. 18 | # 19 | # You should have received a copy of the GNU Lesser General Public License 20 | # along with systemd; If not, see . 21 | 22 | SKIP_REMAINING=77 23 | 24 | usage() 25 | { 26 | echo "Usage:" 27 | echo " $0 add KERNEL-VERSION KERNEL-IMAGE" 28 | echo " $0 remove KERNEL-VERSION" 29 | } 30 | 31 | dropindirs_sort() 32 | { 33 | local suffix=$1; shift 34 | local -a files 35 | local f d i 36 | 37 | readarray -t files < <( 38 | for d in "$@"; do 39 | for i in "$d/"*"$suffix"; do 40 | if [[ -e "$i" ]]; then 41 | echo "${i##*/}" 42 | fi 43 | done 44 | done | sort -Vu 45 | ) 46 | 47 | for f in "${files[@]}"; do 48 | for d in "$@"; do 49 | if [[ -e "$d/$f" ]]; then 50 | echo "$d/$f" 51 | continue 2 52 | fi 53 | done 54 | done 55 | } 56 | 57 | export LC_COLLATE=C 58 | 59 | for i in "$@"; do 60 | if [ "$i" == "--help" -o "$i" == "-h" ]; then 61 | usage 62 | exit 0 63 | fi 64 | done 65 | 66 | if [[ "${0##*/}" == 'installkernel' ]]; then 67 | COMMAND='add' 68 | else 69 | COMMAND="$1" 70 | shift 71 | fi 72 | 73 | KERNEL_VERSION="$1" 74 | KERNEL_IMAGE="$2" 75 | 76 | if [[ -f /etc/machine-id ]]; then 77 | read MACHINE_ID < /etc/machine-id 78 | fi 79 | 80 | if ! [[ $MACHINE_ID ]]; then 81 | echo "Could not determine your machine ID from /etc/machine-id." >&2 82 | echo "Please run 'systemd-machine-id-setup' as root. See man:machine-id(5)" >&2 83 | exit 1 84 | fi 85 | 86 | if [[ ! $COMMAND ]] || [[ ! $KERNEL_VERSION ]]; then 87 | echo "Not enough arguments" >&2 88 | exit 1 89 | fi 90 | 91 | if [[ -d /efi/loader/entries ]] || [[ -d /efi/$MACHINE_ID ]]; then 92 | BOOT_DIR_ABS="/efi/$MACHINE_ID/$KERNEL_VERSION" 93 | elif [[ -d /boot/loader/entries ]] || [[ -d /boot/$MACHINE_ID ]]; then 94 | BOOT_DIR_ABS="/boot/$MACHINE_ID/$KERNEL_VERSION" 95 | elif [[ -d /boot/efi/loader/entries ]] || [[ -d /boot/efi/$MACHINE_ID ]]; then 96 | BOOT_DIR_ABS="/boot/efi/$MACHINE_ID/$KERNEL_VERSION" 97 | elif mountpoint -q /efi; then 98 | BOOT_DIR_ABS="/efi/$MACHINE_ID/$KERNEL_VERSION" 99 | elif mountpoint -q /boot/efi; then 100 | BOOT_DIR_ABS="/boot/efi/$MACHINE_ID/$KERNEL_VERSION" 101 | else 102 | BOOT_DIR_ABS="/boot/$MACHINE_ID/$KERNEL_VERSION" 103 | fi 104 | 105 | ret=0 106 | 107 | readarray -t PLUGINS < <( 108 | dropindirs_sort ".install" \ 109 | "/etc/kernel/install.d" \ 110 | "/usr/lib/kernel/install.d" 111 | ) 112 | 113 | case $COMMAND in 114 | add) 115 | if [[ ! "$KERNEL_IMAGE" ]]; then 116 | echo "Command 'add' requires an argument" >&2 117 | exit 1 118 | fi 119 | 120 | mkdir -p "$BOOT_DIR_ABS" || { 121 | echo "Could not create boot directory '$BOOT_DIR_ABS'." >&2 122 | exit 1 123 | } 124 | 125 | for f in "${PLUGINS[@]}"; do 126 | if [[ -x $f ]]; then 127 | "$f" add "$KERNEL_VERSION" "$BOOT_DIR_ABS" "$KERNEL_IMAGE" 128 | x=$? 129 | if [[ $x == $SKIP_REMAINING ]]; then 130 | exit 0 131 | fi 132 | ((ret+=$x)) 133 | fi 134 | done 135 | ;; 136 | 137 | remove) 138 | for f in "${PLUGINS[@]}"; do 139 | if [[ -x $f ]]; then 140 | "$f" remove "$KERNEL_VERSION" "$BOOT_DIR_ABS" 141 | x=$? 142 | if [[ $x == $SKIP_REMAINING ]]; then 143 | exit 0 144 | fi 145 | ((ret+=$x)) 146 | fi 147 | done 148 | 149 | rm -rf "$BOOT_DIR_ABS" 150 | ((ret+=$?)) 151 | ;; 152 | 153 | *) 154 | echo "Unknown command '$COMMAND'" >&2 155 | exit 1 156 | ;; 157 | esac 158 | 159 | exit $ret 160 | -------------------------------------------------------------------------------- /project/m4/ax_normalize_path.m4: -------------------------------------------------------------------------------- 1 | # =========================================================================== 2 | # http://www.gnu.org/software/autoconf-archive/ax_normalize_path.html 3 | # =========================================================================== 4 | # 5 | # SYNOPSIS 6 | # 7 | # AX_NORMALIZE_PATH(VARNAME, [REFERENCE_STRING]) 8 | # 9 | # DESCRIPTION 10 | # 11 | # Perform some cleanups on the value of $VARNAME (interpreted as a path): 12 | # 13 | # - empty paths are changed to '.' 14 | # - trailing slashes are removed 15 | # - repeated slashes are squeezed except a leading doubled slash '//' 16 | # (which might indicate a networked disk on some OS). 17 | # 18 | # REFERENCE_STRING is used to turn '/' into '\' and vice-versa: if 19 | # REFERENCE_STRING contains some backslashes, all slashes and backslashes 20 | # are turned into backslashes, otherwise they are all turned into slashes. 21 | # 22 | # This makes processing of DOS filenames quite easier, because you can 23 | # turn a filename to the Unix notation, make your processing, and turn it 24 | # back to original notation. 25 | # 26 | # filename='A:\FOO\\BAR\' 27 | # old_filename="$filename" 28 | # # Switch to the unix notation 29 | # AX_NORMALIZE_PATH([filename], ["/"]) 30 | # # now we have $filename = 'A:/FOO/BAR' and we can process it as if 31 | # # it was a Unix path. For instance let's say that you want 32 | # # to append '/subpath': 33 | # filename="$filename/subpath" 34 | # # finally switch back to the original notation 35 | # AX_NORMALIZE_PATH([filename], ["$old_filename"]) 36 | # # now $filename equals to 'A:\FOO\BAR\subpath' 37 | # 38 | # One good reason to make all path processing with the unix convention is 39 | # that backslashes have a special meaning in many cases. For instance 40 | # 41 | # expr 'A:\FOO' : 'A:\Foo' 42 | # 43 | # will return 0 because the second argument is a regex in which 44 | # backslashes have to be backslashed. In other words, to have the two 45 | # strings to match you should write this instead: 46 | # 47 | # expr 'A:\Foo' : 'A:\\Foo' 48 | # 49 | # Such behavior makes DOS filenames extremely unpleasant to work with. So 50 | # temporary turn your paths to the Unix notation, and revert them to the 51 | # original notation after the processing. See the macro 52 | # AX_COMPUTE_RELATIVE_PATHS for a concrete example of this. 53 | # 54 | # REFERENCE_STRING defaults to $VARIABLE, this means that slashes will be 55 | # converted to backslashes if $VARIABLE already contains some backslashes 56 | # (see $thirddir below). 57 | # 58 | # firstdir='/usr/local//share' 59 | # seconddir='C:\Program Files\\' 60 | # thirddir='C:\home/usr/' 61 | # AX_NORMALIZE_PATH([firstdir]) 62 | # AX_NORMALIZE_PATH([seconddir]) 63 | # AX_NORMALIZE_PATH([thirddir]) 64 | # # $firstdir = '/usr/local/share' 65 | # # $seconddir = 'C:\Program Files' 66 | # # $thirddir = 'C:\home\usr' 67 | # 68 | # LICENSE 69 | # 70 | # Copyright (c) 2008 Alexandre Duret-Lutz 71 | # 72 | # This program is free software; you can redistribute it and/or modify it 73 | # under the terms of the GNU General Public License as published by the 74 | # Free Software Foundation; either version 2 of the License, or (at your 75 | # option) any later version. 76 | # 77 | # This program is distributed in the hope that it will be useful, but 78 | # WITHOUT ANY WARRANTY; without even the implied warranty of 79 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 80 | # Public License for more details. 81 | # 82 | # You should have received a copy of the GNU General Public License along 83 | # with this program. If not, see . 84 | # 85 | # As a special exception, the respective Autoconf Macro's copyright owner 86 | # gives unlimited permission to copy, distribute and modify the configure 87 | # scripts that are the output of Autoconf when processing the Macro. You 88 | # need not follow the terms of the GNU General Public License when using 89 | # or distributing such scripts, even though portions of the text of the 90 | # Macro appear in them. The GNU General Public License (GPL) does govern 91 | # all other use of the material that constitutes the Autoconf Macro. 92 | # 93 | # This special exception to the GPL applies to versions of the Autoconf 94 | # Macro released by the Autoconf Archive. When you make and distribute a 95 | # modified version of the Autoconf Macro, you may extend this special 96 | # exception to the GPL to apply to your modified version as well. 97 | 98 | #serial 5 99 | 100 | AU_ALIAS([ADL_NORMALIZE_PATH], [AX_NORMALIZE_PATH]) 101 | AC_DEFUN([AX_NORMALIZE_PATH], 102 | [case ":[$]$1:" in 103 | # change empty paths to '.' 104 | ::) $1='.' ;; 105 | # strip trailing slashes 106 | :*[[\\/]]:) $1=`echo "[$]$1" | sed 's,[[\\/]]*[$],,'` ;; 107 | :*:) ;; 108 | esac 109 | # squeze repeated slashes 110 | case ifelse($2,,"[$]$1",$2) in 111 | # if the path contains any backslashes, turn slashes into backslashes 112 | *\\*) $1=`echo "[$]$1" | sed 's,\(.\)[[\\/]][[\\/]]*,\1\\\\,g'` ;; 113 | # if the path contains slashes, also turn backslashes into slashes 114 | *) $1=`echo "[$]$1" | sed 's,\(.\)[[\\/]][[\\/]]*,\1/,g'` ;; 115 | esac]) 116 | -------------------------------------------------------------------------------- /project/src/sd-boot/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 14 | */ 15 | 16 | #include 17 | #include 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, BOOLEAN secure) { 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 | if (secure) { 112 | /* set secure boot flag in linux kernel zero page, see 113 | - Documentation/x86/zero-page.txt 114 | - arch/x86/include/uapi/asm/bootparam.h 115 | - drivers/firmware/efi/libstub/secureboot.c 116 | in the linux kernel source tree 117 | Possible values: 0 (unassigned), 1 (undetected), 2 (disabled), 3 (enabled) 118 | */ 119 | boot_setup->boot_sector[0x1ec] = 3; 120 | } 121 | 122 | boot_setup->code32_start = (UINT32)linux_addr + (image_setup->setup_secs+1) * 512; 123 | 124 | if (cmdline) { 125 | addr = 0xA0000; 126 | err = uefi_call_wrapper(BS->AllocatePages, 4, AllocateMaxAddress, EfiLoaderData, 127 | EFI_SIZE_TO_PAGES(cmdline_len + 1), &addr); 128 | if (EFI_ERROR(err)) 129 | return err; 130 | CopyMem((VOID *)(UINTN)addr, cmdline, cmdline_len); 131 | ((CHAR8 *)addr)[cmdline_len] = 0; 132 | boot_setup->cmd_line_ptr = (UINT32)addr; 133 | } 134 | 135 | boot_setup->ramdisk_start = (UINT32)initrd_addr; 136 | boot_setup->ramdisk_len = (UINT32)initrd_size; 137 | 138 | linux_efi_handover(image, boot_setup); 139 | return EFI_LOAD_ERROR; 140 | } 141 | -------------------------------------------------------------------------------- /project/src/sd-boot/stub.c: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: LGPL-2.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 13 | */ 14 | 15 | #include 16 | #include 17 | 18 | #include "disk.h" 19 | #include "graphics.h" 20 | #include "linux.h" 21 | #include "measure.h" 22 | #include "pe.h" 23 | #include "splash.h" 24 | #include "util.h" 25 | 26 | /* magic string to find in the binary image */ 27 | static const char __attribute__((used)) magic[] = "#### LoaderInfo: systemd-stub " PACKAGE_VERSION " ####"; 28 | 29 | static const EFI_GUID global_guid = EFI_GLOBAL_VARIABLE; 30 | 31 | EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) { 32 | EFI_LOADED_IMAGE *loaded_image; 33 | _cleanup_freepool_ CHAR8 *b = NULL; 34 | UINTN size; 35 | BOOLEAN secure = FALSE; 36 | CHAR8 *sections[] = { 37 | (UINT8 *)".cmdline", 38 | (UINT8 *)".linux", 39 | (UINT8 *)".initrd", 40 | (UINT8 *)".splash", 41 | NULL 42 | }; 43 | UINTN addrs[ELEMENTSOF(sections)-1] = {}; 44 | UINTN offs[ELEMENTSOF(sections)-1] = {}; 45 | UINTN szs[ELEMENTSOF(sections)-1] = {}; 46 | CHAR8 *cmdline = NULL; 47 | UINTN cmdline_len; 48 | CHAR16 uuid[37]; 49 | EFI_STATUS err; 50 | 51 | InitializeLib(image, sys_table); 52 | 53 | err = uefi_call_wrapper(BS->OpenProtocol, 6, image, &LoadedImageProtocol, (VOID **)&loaded_image, 54 | image, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); 55 | if (EFI_ERROR(err)) { 56 | Print(L"Error getting a LoadedImageProtocol handle: %r ", err); 57 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); 58 | return err; 59 | } 60 | 61 | if (efivar_get_raw(&global_guid, L"SecureBoot", &b, &size) == EFI_SUCCESS) 62 | if (*b > 0) 63 | secure = TRUE; 64 | 65 | err = pe_memory_locate_sections(loaded_image->ImageBase, sections, addrs, offs, szs); 66 | if (EFI_ERROR(err)) { 67 | Print(L"Unable to locate embedded .linux section: %r ", err); 68 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); 69 | return err; 70 | } 71 | 72 | if (szs[0] > 0) 73 | cmdline = (CHAR8 *)(loaded_image->ImageBase + addrs[0]); 74 | 75 | cmdline_len = szs[0]; 76 | 77 | /* if we are not in secure boot mode, accept a custom command line and replace the built-in one */ 78 | if (!secure && loaded_image->LoadOptionsSize > 0 && *(CHAR16 *)loaded_image->LoadOptions != 0) { 79 | CHAR16 *options; 80 | CHAR8 *line; 81 | UINTN i; 82 | 83 | options = (CHAR16 *)loaded_image->LoadOptions; 84 | cmdline_len = (loaded_image->LoadOptionsSize / sizeof(CHAR16)) * sizeof(CHAR8); 85 | line = AllocatePool(cmdline_len); 86 | for (i = 0; i < cmdline_len; i++) 87 | line[i] = options[i]; 88 | cmdline = line; 89 | 90 | #if ENABLE_TPM 91 | /* Try to log any options to the TPM, especially manually edited options */ 92 | err = tpm_log_event(SD_TPM_PCR, 93 | (EFI_PHYSICAL_ADDRESS) loaded_image->LoadOptions, 94 | loaded_image->LoadOptionsSize, loaded_image->LoadOptions); 95 | if (EFI_ERROR(err)) { 96 | Print(L"Unable to add image options measurement: %r", err); 97 | uefi_call_wrapper(BS->Stall, 1, 200 * 1000); 98 | } 99 | #endif 100 | } 101 | 102 | /* export the device path this image is started from */ 103 | if (disk_get_part_uuid(loaded_image->DeviceHandle, uuid) == EFI_SUCCESS) 104 | efivar_set(L"LoaderDevicePartUUID", uuid, FALSE); 105 | 106 | /* if LoaderImageIdentifier is not set, assume the image with this stub was loaded directly from UEFI */ 107 | if (efivar_get_raw(&global_guid, L"LoaderImageIdentifier", &b, &size) != EFI_SUCCESS) { 108 | _cleanup_freepool_ CHAR16 *s; 109 | 110 | s = DevicePathToStr(loaded_image->FilePath); 111 | efivar_set(L"LoaderImageIdentifier", s, FALSE); 112 | } 113 | 114 | /* if LoaderFirmwareInfo is not set, let's set it */ 115 | if (efivar_get_raw(&global_guid, L"LoaderFirmwareInfo", &b, &size) != EFI_SUCCESS) { 116 | _cleanup_freepool_ CHAR16 *s; 117 | 118 | s = PoolPrint(L"%s %d.%02d", ST->FirmwareVendor, ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff); 119 | efivar_set(L"LoaderFirmwareInfo", s, FALSE); 120 | } 121 | 122 | /* ditto for LoaderFirmwareType */ 123 | if (efivar_get_raw(&global_guid, L"LoaderFirmwareType", &b, &size) != EFI_SUCCESS) { 124 | _cleanup_freepool_ CHAR16 *s; 125 | 126 | s = PoolPrint(L"UEFI %d.%02d", ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff); 127 | efivar_set(L"LoaderFirmwareType", s, FALSE); 128 | } 129 | 130 | /* add StubInfo */ 131 | if (efivar_get_raw(&global_guid, L"StubInfo", &b, &size) != EFI_SUCCESS) 132 | efivar_set(L"StubInfo", L"systemd-stub " PACKAGE_VERSION, FALSE); 133 | 134 | if (szs[3] > 0) 135 | graphics_splash((UINT8 *)((UINTN)loaded_image->ImageBase + addrs[3]), szs[3], NULL); 136 | 137 | err = linux_exec(image, cmdline, cmdline_len, 138 | (UINTN)loaded_image->ImageBase + addrs[1], 139 | (UINTN)loaded_image->ImageBase + addrs[2], szs[2], secure); 140 | 141 | graphics_mode(FALSE); 142 | Print(L"Execution of embedded linux image failed: %r\n", err); 143 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); 144 | return err; 145 | } 146 | -------------------------------------------------------------------------------- /project/src/sd-boot/pe.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 14 | */ 15 | 16 | #include 17 | #include 18 | 19 | #include "pe.h" 20 | #include "util.h" 21 | 22 | struct DosFileHeader { 23 | UINT8 Magic[2]; 24 | UINT16 LastSize; 25 | UINT16 nBlocks; 26 | UINT16 nReloc; 27 | UINT16 HdrSize; 28 | UINT16 MinAlloc; 29 | UINT16 MaxAlloc; 30 | UINT16 ss; 31 | UINT16 sp; 32 | UINT16 Checksum; 33 | UINT16 ip; 34 | UINT16 cs; 35 | UINT16 RelocPos; 36 | UINT16 nOverlay; 37 | UINT16 reserved[4]; 38 | UINT16 OEMId; 39 | UINT16 OEMInfo; 40 | UINT16 reserved2[10]; 41 | UINT32 ExeHeader; 42 | } __attribute__((packed)); 43 | 44 | #define PE_HEADER_MACHINE_I386 0x014c 45 | #define PE_HEADER_MACHINE_X64 0x8664 46 | struct PeFileHeader { 47 | UINT16 Machine; 48 | UINT16 NumberOfSections; 49 | UINT32 TimeDateStamp; 50 | UINT32 PointerToSymbolTable; 51 | UINT32 NumberOfSymbols; 52 | UINT16 SizeOfOptionalHeader; 53 | UINT16 Characteristics; 54 | } __attribute__((packed)); 55 | 56 | struct PeHeader { 57 | UINT8 Magic[4]; 58 | struct PeFileHeader FileHeader; 59 | } __attribute__((packed)); 60 | 61 | struct PeSectionHeader { 62 | UINT8 Name[8]; 63 | UINT32 VirtualSize; 64 | UINT32 VirtualAddress; 65 | UINT32 SizeOfRawData; 66 | UINT32 PointerToRawData; 67 | UINT32 PointerToRelocations; 68 | UINT32 PointerToLinenumbers; 69 | UINT16 NumberOfRelocations; 70 | UINT16 NumberOfLinenumbers; 71 | UINT32 Characteristics; 72 | } __attribute__((packed)); 73 | 74 | EFI_STATUS pe_memory_locate_sections(CHAR8 *base, CHAR8 **sections, UINTN *addrs, UINTN *offsets, UINTN *sizes) { 75 | struct DosFileHeader *dos; 76 | struct PeHeader *pe; 77 | UINTN i; 78 | UINTN offset; 79 | 80 | dos = (struct DosFileHeader *)base; 81 | 82 | if (CompareMem(dos->Magic, "MZ", 2) != 0) 83 | return EFI_LOAD_ERROR; 84 | 85 | pe = (struct PeHeader *)&base[dos->ExeHeader]; 86 | if (CompareMem(pe->Magic, "PE\0\0", 4) != 0) 87 | return EFI_LOAD_ERROR; 88 | 89 | /* PE32+ Subsystem type */ 90 | if (pe->FileHeader.Machine != PE_HEADER_MACHINE_X64 && 91 | pe->FileHeader.Machine != PE_HEADER_MACHINE_I386) 92 | return EFI_LOAD_ERROR; 93 | 94 | if (pe->FileHeader.NumberOfSections > 96) 95 | return EFI_LOAD_ERROR; 96 | 97 | offset = dos->ExeHeader + sizeof(*pe) + pe->FileHeader.SizeOfOptionalHeader; 98 | 99 | for (i = 0; i < pe->FileHeader.NumberOfSections; i++) { 100 | struct PeSectionHeader *sect; 101 | UINTN j; 102 | 103 | sect = (struct PeSectionHeader *)&base[offset]; 104 | for (j = 0; sections[j]; j++) { 105 | if (CompareMem(sect->Name, sections[j], strlena(sections[j])) != 0) 106 | continue; 107 | 108 | if (addrs) 109 | addrs[j] = (UINTN)sect->VirtualAddress; 110 | if (offsets) 111 | offsets[j] = (UINTN)sect->PointerToRawData; 112 | if (sizes) 113 | sizes[j] = (UINTN)sect->VirtualSize; 114 | } 115 | offset += sizeof(*sect); 116 | } 117 | 118 | return EFI_SUCCESS; 119 | } 120 | 121 | EFI_STATUS pe_file_locate_sections(EFI_FILE *dir, CHAR16 *path, CHAR8 **sections, UINTN *addrs, UINTN *offsets, UINTN *sizes) { 122 | EFI_FILE_HANDLE handle; 123 | struct DosFileHeader dos; 124 | struct PeHeader pe; 125 | UINTN len; 126 | UINTN headerlen; 127 | EFI_STATUS err; 128 | _cleanup_freepool_ CHAR8 *header = NULL; 129 | 130 | err = uefi_call_wrapper(dir->Open, 5, dir, &handle, path, EFI_FILE_MODE_READ, 0ULL); 131 | if (EFI_ERROR(err)) 132 | return err; 133 | 134 | /* MS-DOS stub */ 135 | len = sizeof(dos); 136 | err = uefi_call_wrapper(handle->Read, 3, handle, &len, &dos); 137 | if (EFI_ERROR(err)) 138 | goto out; 139 | if (len != sizeof(dos)) { 140 | err = EFI_LOAD_ERROR; 141 | goto out; 142 | } 143 | 144 | err = uefi_call_wrapper(handle->SetPosition, 2, handle, dos.ExeHeader); 145 | if (EFI_ERROR(err)) 146 | goto out; 147 | 148 | len = sizeof(pe); 149 | err = uefi_call_wrapper(handle->Read, 3, handle, &len, &pe); 150 | if (EFI_ERROR(err)) 151 | goto out; 152 | if (len != sizeof(pe)) { 153 | err = EFI_LOAD_ERROR; 154 | goto out; 155 | } 156 | 157 | headerlen = sizeof(dos) + sizeof(pe) + pe.FileHeader.SizeOfOptionalHeader + pe.FileHeader.NumberOfSections * sizeof(struct PeSectionHeader); 158 | header = AllocatePool(headerlen); 159 | if (!header) { 160 | err = EFI_OUT_OF_RESOURCES; 161 | goto out; 162 | } 163 | len = headerlen; 164 | err = uefi_call_wrapper(handle->SetPosition, 2, handle, 0); 165 | if (EFI_ERROR(err)) 166 | goto out; 167 | 168 | err = uefi_call_wrapper(handle->Read, 3, handle, &len, header); 169 | if (EFI_ERROR(err)) 170 | goto out; 171 | 172 | if (len != headerlen) { 173 | err = EFI_LOAD_ERROR; 174 | goto out; 175 | } 176 | 177 | err = pe_memory_locate_sections(header, sections, addrs, offsets, sizes); 178 | out: 179 | uefi_call_wrapper(handle->Close, 1, handle); 180 | return err; 181 | } 182 | -------------------------------------------------------------------------------- /project/Makefile.am: -------------------------------------------------------------------------------- 1 | # This file is part of systemd-boot 2 | # 3 | # Copyright (C) 2013-2016 Karel Zak 4 | # Copyright (C) 2016 Michal Sekletar 5 | # 6 | # systemd-boot is free software; you can redistribute it and/or modify it 7 | # under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation; either version 2.1 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # systemd-boot is distributed in the hope that it will be useful, but 12 | # WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | # Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with systemd-boot; If not, see . 18 | 19 | systemd_bootlibdir = $(prefix)/lib/systemd-boot 20 | kernelinstalldir = $(prefix)/lib/kernel/install.d 21 | bashcompletiondir=@bashcompletiondir@ 22 | zshcompletiondir=@zshcompletiondir@ 23 | 24 | dist_bin_SCRIPTS = \ 25 | src/kernel-install/kernel-install 26 | 27 | dist_kernelinstall_SCRIPTS = \ 28 | src/kernel-install/50-depmod.install \ 29 | src/kernel-install/90-loaderentry.install 30 | 31 | dist_bashcompletion_data = \ 32 | shell-completion/bash/kernel-install 33 | 34 | dist_zshcompletion_data = \ 35 | shell-completion/zsh/_kernel-install 36 | 37 | if ENABLE_BASH_COMPLETION 38 | dist_bashcompletion_DATA = $(dist_bashcompletion_data) 39 | endif 40 | if ENABLE_ZSH_COMPLETION 41 | dist_zshcompletion_DATA = $(dist_zshcompletion_data) 42 | endif 43 | 44 | EXTRA_DIST = autogen.sh README.md LICENSE 45 | CLEANFILES = 46 | 47 | DISTCHECK_CONFIGURE_FLAGS = \ 48 | --with-bashcompletiondir=$$dc_install_base/$(bashcompletiondir) \ 49 | --with-zshcompletiondir=$$dc_install_base/$(zshcompletiondir) 50 | 51 | if ENABLE_MANPAGES 52 | %.8: %.xml 53 | $(AM_V_GEN)$(XSLTPROC) -o $@ --nonet \ 54 | --stringparam man.output.quietly 1 \ 55 | --stringparam man.th.extra1.suppress 1 \ 56 | --stringparam man.authors.section.enabled 0 \ 57 | --stringparam man.copyright.section.enabled 0 \ 58 | http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl $< 59 | 60 | dist_man_MANS = man/kernel-install.8 61 | endif 62 | 63 | EXTRA_DIST += man/kernel-install.xml 64 | CLEANFILES += man/kernel-install.8 65 | 66 | # ------------------------------------------------------------------------------ 67 | # EFI compilation -- this part of the build system uses custom make rules and 68 | # bypasses regular automake to provide absolute control on compiler and linker 69 | # flags. 70 | EFI_MACHINE_TYPE_NAME = @MACHINE_TYPE_NAME@ 71 | 72 | efi_cppflags = \ 73 | $(EFI_CPPFLAGS) \ 74 | -I$(top_builddir) -include config.h \ 75 | -I$(EFI_INC_DIR)/efi \ 76 | -I$(EFI_INC_DIR)/efi/$(ARCH) \ 77 | -DEFI_MACHINE_TYPE_NAME=\"$(EFI_MACHINE_TYPE_NAME)\" 78 | 79 | efi_cflags = \ 80 | $(EFI_CFLAGS) \ 81 | -Wall \ 82 | -Wextra \ 83 | -std=gnu90 \ 84 | -nostdinc \ 85 | -ggdb -O0 \ 86 | -fpic \ 87 | -fshort-wchar \ 88 | -nostdinc \ 89 | -ffreestanding \ 90 | -fno-strict-aliasing \ 91 | -fno-stack-protector \ 92 | -Wsign-compare \ 93 | -mno-sse \ 94 | -mno-mmx 95 | 96 | if ARCH_X86_64 97 | efi_cflags += \ 98 | -mno-red-zone \ 99 | -DEFI_FUNCTION_WRAPPER \ 100 | -DGNU_EFI_USE_MS_ABI 101 | endif 102 | 103 | efi_ldflags = \ 104 | $(EFI_LDFLAGS) \ 105 | -T $(EFI_LDS_DIR)/elf_$(ARCH)_efi.lds \ 106 | -shared \ 107 | -Bsymbolic \ 108 | -nostdlib \ 109 | -znocombreloc \ 110 | -L $(EFI_LIB_DIR) \ 111 | $(EFI_LDS_DIR)/crt0-efi-$(ARCH).o 112 | 113 | # ------------------------------------------------------------------------------ 114 | systemd_boot_headers = \ 115 | src/sd-boot/util.h \ 116 | src/sd-boot/console.h \ 117 | src/sd-boot/graphics.h \ 118 | src/sd-boot/pe.h \ 119 | src/sd-boot/measure.h \ 120 | src/sd-boot/disk.h \ 121 | src/sd-boot/shim.h 122 | 123 | systemd_boot_sources = \ 124 | src/sd-boot/util.c \ 125 | src/sd-boot/console.c \ 126 | src/sd-boot/graphics.c \ 127 | src/sd-boot/pe.c \ 128 | src/sd-boot/disk.c \ 129 | src/sd-boot/measure.c \ 130 | src/sd-boot/boot.c \ 131 | src/sd-boot/shim.c 132 | 133 | systemd_boot_objects = $(addprefix $(top_builddir)/,$(systemd_boot_sources:.c=.o)) 134 | systemd_boot_solib = $(top_builddir)/src/sd-boot/systemd_boot.so 135 | systemd_boot = systemd-boot$(EFI_MACHINE_TYPE_NAME).efi 136 | 137 | systemd_bootlib_DATA = $(systemd_boot) 138 | CLEANFILES += \ 139 | $(systemd_boot_objects) \ 140 | $(systemd_boot_solib) \ 141 | $(systemd_boot) 142 | 143 | EXTRA_DIST += \ 144 | $(systemd_boot_sources) \ 145 | $(systemd_boot_headers) 146 | 147 | $(top_builddir)/src/sd-boot/%.o: $(top_srcdir)/src/sd-boot/%.c $(addprefix $(top_srcdir)/,$(systemd_boot_headers)) 148 | @$(MKDIR_P) $(top_builddir)/src/sd-boot 149 | $(AM_V_CC)$(CC) $(efi_cppflags) $(efi_cflags) -c $< -o $@ 150 | 151 | $(systemd_boot_solib): $(systemd_boot_objects) 152 | $(AM_V_CCLD)$(LD) $(efi_ldflags) $(systemd_boot_objects) \ 153 | -o $@ -lefi -lgnuefi $(shell $(CC) -print-libgcc-file-name); \ 154 | nm -D -u $@ | grep ' U ' && exit 1 || : 155 | .DELETE_ON_ERROR: $(sustemd_boot_solib) 156 | 157 | $(systemd_boot): $(systemd_boot_solib) 158 | $(AM_V_GEN) objcopy -j .text -j .sdata -j .data -j .dynamic \ 159 | -j .dynsym -j .rel -j .rela -j .reloc \ 160 | --target=efi-app-$(ARCH) $< $@ 161 | 162 | # ------------------------------------------------------------------------------ 163 | stub_headers = \ 164 | src/sd-boot/util.h \ 165 | src/sd-boot/pe.h \ 166 | src/sd-boot/disk.h \ 167 | src/sd-boot/graphics.h \ 168 | src/sd-boot/splash.h \ 169 | src/sd-boot/measure.h \ 170 | src/sd-boot/linux.h 171 | 172 | stub_sources = \ 173 | src/sd-boot/util.c \ 174 | src/sd-boot/pe.c \ 175 | src/sd-boot/disk.c \ 176 | src/sd-boot/graphics.c \ 177 | src/sd-boot/splash.c \ 178 | src/sd-boot/linux.c \ 179 | src/sd-boot/measure.c \ 180 | src/sd-boot/stub.c 181 | 182 | stub_objects = $(addprefix $(top_builddir)/,$(stub_sources:.c=.o)) 183 | stub_solib = $(top_builddir)/src/sd-boot/stub.so 184 | stub = linux$(EFI_MACHINE_TYPE_NAME).efi.stub 185 | 186 | systemd_bootlib_DATA += $(stub) 187 | CLEANFILES += \ 188 | $(stub_objects) \ 189 | $(stub_solib) \ 190 | $(stub) 191 | 192 | EXTRA_DIST += \ 193 | $(stub_sources) \ 194 | $(stub_headers) \ 195 | test/splash.bmp 196 | 197 | $(top_builddir)/src/sd-boot/%.o: $(top_srcdir)/src/sd-boot/%.c $(addprefix $(top_srcdir)/,$(stub_headers)) 198 | @$(MKDIR_P) $(top_builddir)/src/sd-boot 199 | $(AM_V_CC)$(CC) $(efi_cppflags) $(efi_cflags) -c $< -o $@ 200 | 201 | $(stub_solib): $(stub_objects) 202 | $(AM_V_CCLD)$(LD) $(efi_ldflags) $(stub_objects) \ 203 | -o $@ -lefi -lgnuefi $(shell $(CC) -print-libgcc-file-name); \ 204 | nm -D -u $@ | grep ' U ' && exit 1 || : 205 | .DELETE_ON_ERROR: $(systemd_boot_solib) 206 | 207 | $(stub): $(stub_solib) 208 | $(AM_V_GEN) objcopy -j .text -j .sdata -j .data -j .dynamic \ 209 | -j .dynsym -j .rel -j .rela -j .reloc \ 210 | --target=efi-app-$(ARCH) $< $@ 211 | 212 | # ------------------------------------------------------------------------------ 213 | CLEANFILES += test-disk.img 214 | EXTRA_DIST += test/test-efi-create-disk.sh 215 | 216 | test-efi-disk.img: systemd_boot$(EFI_MACHINE_TYPE_NAME).efi test/test-efi-create-disk.sh 217 | $(AM_V_GEN)test/test-efi-create-disk.sh 218 | 219 | qemu: test-efi-disk.img 220 | $(QEMU) -machine accel=kvm -m 1024 -bios $(QEMU_BIOS) -snapshot test-efi-disk.img 221 | 222 | install-tree: all 223 | rm -rf $(abs_srcdir)/install-tree 224 | $(MAKE) install DESTDIR=$(abs_srcdir)/install-tree 225 | tree $(abs_srcdir)/install-tree 226 | -------------------------------------------------------------------------------- /project/configure.ac: -------------------------------------------------------------------------------- 1 | # This file is part of systemd-boot 2 | # 3 | # Copyright (C) 2013-2016 Karel Zak 4 | # Copyright (C) 2016 Michal Sekeltar 5 | # 6 | # systemd-boot is free software; you can redistribute it and/or modify it 7 | # under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation; either version 2.1 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # systemd-boot is distributed in the hope that it will be useful, but 12 | # WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | # Lesser General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU Lesser General Public License 17 | # along with systemd-boot; If not, see . 18 | 19 | AC_PREREQ([2.64]) 20 | AC_INIT([systemd-boot], 21 | [26-May-2018], 22 | [http://github.com/ivandavidov/systemd-boot/issues], 23 | [systemd-boot], 24 | [http://github.com/ivandavidov/systemd-boot]) 25 | 26 | AC_CONFIG_AUX_DIR([build-aux]) 27 | AC_CONFIG_MACRO_DIR([m4]) 28 | AC_CONFIG_SRCDIR([src/sd-boot/boot.c]) 29 | AC_CONFIG_HEADERS([config.h]) 30 | 31 | AM_INIT_AUTOMAKE([foreign 1.11 -Wall -Wno-portability silent-rules tar-pax no-dist-gzip dist-xz subdir-objects]) 32 | AC_CANONICAL_HOST 33 | 34 | # Figure out target architecture and EFI machine type 35 | ARCH="$host_cpu" 36 | 37 | AS_IF([test x"$host_cpu" = x"x86_64"], [MACHINE_TYPE_NAME=x64]) 38 | AS_CASE([$host_cpu], [i*86], [MACHINE_TYPE_NAME=ia32]) 39 | 40 | AM_CONDITIONAL(ARCH_X86_64, [test x"$MACHINE_TYPE_NAME" = "x64"]) 41 | AM_CONDITIONAL(ARCH_IA32, [test x"$MACHINE_TYPE_NAME" = "ia32"]) 42 | 43 | AC_SUBST([ARCH]) 44 | AC_SUBST([MACHINE_TYPE_NAME]) 45 | AC_DEFINE_UNQUOTED([MACHINE_TYPE_NAME], [$MACHINE_TYPE_NAME], [EFI machine type]) 46 | 47 | AC_SYS_LARGEFILE 48 | AC_SYS_LONG_FILE_NAMES 49 | 50 | AC_PROG_CC 51 | AC_PROG_MKDIR_P 52 | AC_PATH_PROG([QEMU], [qemu-system-$ARCH]) 53 | 54 | AC_PATH_PROG([XSLTPROC], [xsltproc]) 55 | 56 | PKG_PROG_PKG_CONFIG 57 | 58 | # ------------------------------------------------------------------------------ 59 | AC_ARG_WITH(ovmf, 60 | AS_HELP_STRING([--with-ovmf=PATH], [Path to OVMF firmware binary]), 61 | [OVMF_PATH="$withval"], 62 | [AC_MSG_WARN([*** No OVMF path specified, using defaults])]) 63 | 64 | # Search for OVMF firmware in default locations in case 65 | AS_IF([ test -z "$OVMF_PATH" ], [ 66 | AC_PATH_PROG([QEMU], [qemu-system-x86_64]) 67 | # Fedora 68 | AC_CHECK_FILE([/usr/share/edk2/ovmf/OVMF_CODE.fd], [QEMU_BIOS=/usr/share/edk2/ovmf/OVMF_CODE.fd]) 69 | # Debian 70 | AC_CHECK_FILE([/usr/share/qemu/OVMF.fd], [QEMU_BIOS=/usr/share/qemu/OVMF.fd]) 71 | # Arch Linux 72 | AC_CHECK_FILE([/usr/share/ovmf/${MACHINE_TYPE_NAME}/ovmf_${MACHINE_TYPE_NAME}.bin], [QEMU_BIOS=/usr/share/ovmf/${MACHINE_TYPE_NAME}/ovmf_${MACHINE_TYPE_NAME}.bin]) 73 | # SUSE 74 | AC_CHECK_FILE([/usr/share/qemu/ovmf-${ARCH}.bin], [QEMU_BIOS=/usr/share/qemu/ovmf-${ARCH}.bin]) 75 | AC_SUBST([QEMU_BIOS]) 76 | ]) 77 | 78 | # ------------------------------------------------------------------------------ 79 | CPPFLAGS="$CPPFLAGS -I/usr/include/efi/$ARCH" 80 | AC_CHECK_HEADER([efi/efi.h], [], [AC_MSG_ERROR([*** gnu-efi headers not found])]) 81 | 82 | efiroot=$(echo $(cd /usr/lib/$(gcc -print-multi-os-directory); pwd)) 83 | EFI_LIB_DIR="$efiroot" 84 | AC_ARG_WITH(efi-libdir, 85 | AS_HELP_STRING([--with-efi-libdir=PATH], [Path to efi lib directory]), 86 | [EFI_LIB_DIR="$withval"], [EFI_LIB_DIR="$efiroot"] 87 | ) 88 | AC_SUBST([EFI_LIB_DIR]) 89 | 90 | # ------------------------------------------------------------------------------ 91 | AC_ARG_WITH(efi-ldsdir, 92 | AS_HELP_STRING([--with-efi-ldsdir=PATH], [Path to efi lds directory]), 93 | [EFI_LDS_DIR="$withval"], 94 | [ 95 | for EFI_LDS_DIR in "${efiroot}/gnuefi" "${efiroot}"; do 96 | for lds in ${EFI_LDS_DIR}/elf_${ARCH}_efi.lds; do 97 | test -f ${lds} && break 2 98 | done 99 | done 100 | ] 101 | ) 102 | AC_SUBST([EFI_LDS_DIR]) 103 | 104 | # ------------------------------------------------------------------------------ 105 | AC_ARG_WITH(efi-includedir, 106 | AS_HELP_STRING([--with-efi-includedir=PATH], [Path to efi include directory]), 107 | [EFI_INC_DIR="$withval"], [EFI_INC_DIR="/usr/include"] 108 | ) 109 | AC_SUBST([EFI_INC_DIR]) 110 | 111 | # ------------------------------------------------------------------------------ 112 | have_tpm=no 113 | AC_ARG_ENABLE([tpm], AS_HELP_STRING([--enable-tpm], [Enable optional TPM support]), 114 | [case "${enableval}" in 115 | yes) have_tpm=yes ;; 116 | no) have_tpm=no ;; 117 | *) AC_MSG_ERROR(bad value ${enableval} for --enable-tpm) ;; 118 | esac], 119 | [have_tpm=no]) 120 | 121 | AS_IF([test "x${have_tpm}" != xno], AC_DEFINE(SD_BOOT_LOG_TPM, 1, [Define if TPM should be used to log events and extend the registers])) 122 | 123 | AC_ARG_WITH(tpm-pcrindex, 124 | AS_HELP_STRING([--with-tpm-pcrindex=], 125 | [TPM PCR register number to use]), 126 | [SD_TPM_PCR="$withval"], 127 | [SD_TPM_PCR="8"]) 128 | 129 | AC_DEFINE_UNQUOTED(SD_TPM_PCR, [$SD_TPM_PCR], [TPM PCR register number to use]) 130 | 131 | # ------------------------------------------------------------------------------ 132 | AC_ARG_WITH([bashcompletiondir], 133 | AS_HELP_STRING([--with-bashcompletiondir=DIR], [bash completions directory]), 134 | [], [with_bashcompletiondir=${prefix}/usr/share/bash-completion/completions]) 135 | AM_CONDITIONAL(ENABLE_BASH_COMPLETION, [test "$with_bashcompletiondir" != "no"]) 136 | AX_NORMALIZE_PATH([with_bashcompletiondir]) 137 | AC_SUBST([bashcompletiondir], [$with_bashcompletiondir]) 138 | 139 | # ------------------------------------------------------------------------------ 140 | AC_ARG_WITH([zshcompletiondir], 141 | AS_HELP_STRING([--with-zshcompletiondir=DIR], [zsh completions directory]), 142 | [], [with_zshcompletiondir=${prefix}/usr/share/zsh/site-functions]) 143 | AM_CONDITIONAL(ENABLE_ZSH_COMPLETION, [test "$with_zshcompletiondir" != "no"]) 144 | AX_NORMALIZE_PATH([with_zshcompletiondir]) 145 | AC_SUBST([zshcompletiondir], [$with_zshcompletiondir]) 146 | 147 | # ------------------------------------------------------------------------------ 148 | have_manpages=no 149 | AC_ARG_ENABLE(manpages, AS_HELP_STRING([--disable-manpages], [disable manpages])) 150 | AS_IF([test "x$enable_manpages" != xno], [ 151 | AS_IF([test "x$enable_manpages" = xyes -a "x$XSLTPROC" = x], [ 152 | AC_MSG_ERROR([*** Manpages requested but xsltproc not found]) 153 | ]) 154 | AS_IF([test "x$XSLTPROC" != x], [have_manpages=yes]) 155 | ]) 156 | AM_CONDITIONAL(ENABLE_MANPAGES, [test "x$have_manpages" = "xyes"]) 157 | 158 | # ------------------------------------------------------------------------------ 159 | AC_CONFIG_FILES([ 160 | Makefile 161 | ]) 162 | 163 | AC_OUTPUT 164 | AC_MSG_RESULT([ 165 | $PACKAGE_NAME $VERSION 166 | 167 | prefix: ${prefix} 168 | datarootdir: ${datarootdir} 169 | Bash completions dir: ${with_bashcompletiondir} 170 | Zsh completions dir: ${with_zshcompletiondir} 171 | 172 | arch: $ARCH 173 | EFI machine type: $MACHINE_TYPE_NAME 174 | EFI libdir: ${EFI_LIB_DIR} 175 | EFI ldsdir: ${EFI_LDS_DIR} 176 | EFI includedir: ${EFI_INC_DIR} 177 | TPM: ${have_tpm} 178 | 179 | QEMU: ${QEMU} 180 | QEMU OVMF: ${QEMU_BIOS} 181 | ]) 182 | -------------------------------------------------------------------------------- /project/man/kernel-install.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 23 | 24 | 25 | 26 | 27 | kernel-install 28 | systemd 29 | 30 | 31 | 32 | Developer 33 | Harald 34 | Hoyer 35 | harald@redhat.com 36 | 37 | 38 | 39 | 40 | 41 | kernel-install 42 | 8 43 | 44 | 45 | 46 | kernel-install 47 | Add and remove kernel and initramfs images to and from /boot 48 | 49 | 50 | 51 | 52 | kernel-install 53 | COMMAND 54 | KERNEL-VERSION 55 | KERNEL-IMAGE 56 | 57 | 58 | 59 | 60 | Description 61 | 62 | kernel-install is used to install and remove kernel and 63 | initramfs images to and from /boot. 64 | 65 | 66 | kernel-install will execute the files 67 | located in the directory /usr/lib/kernel/install.d/ 68 | and the local administration directory /etc/kernel/install.d/. 69 | All files are collectively sorted and executed in lexical order, regardless of the directory in 70 | which they live. However, files with identical filenames replace each other. 71 | Files in /etc/kernel/install.d/ take precedence over files with the same name 72 | in /usr/lib/kernel/install.d/. This can be used to override a system-supplied 73 | executables with a local file if needed; a symbolic link in /etc/kernel/install.d/ 74 | with the same name as an executable in /usr/lib/kernel/install.d/, 75 | pointing to /dev/null, disables the executable entirely. Executables must have the 76 | extension .install; other extensions are ignored. 77 | 78 | An executable should return 0 on success. It may also 79 | return 77 to cause the whole operation to terminate 80 | (executables later in lexical order will be skipped). 81 | 82 | 83 | 84 | Commands 85 | The following commands are understood: 86 | 87 | 88 | add KERNEL-VERSION KERNEL-IMAGE 89 | 90 | kernel-install creates the directory 91 | /boot/MACHINE-ID/KERNEL-VERSION/ 92 | and calls executables from 93 | /usr/lib/kernel/install.d/*.install and 94 | /etc/kernel/install.d/*.install with 95 | the arguments 96 | add KERNEL-VERSION \ 97 | /boot/MACHINE-ID/KERNEL-VERSION/ KERNEL-IMAGE 98 | 99 | 100 | The kernel-install plugin 50-depmod.install runs depmod for the KERNEL-VERSION. 101 | 102 | The kernel-install plugin 103 | 90-loaderentry.install copies 104 | KERNEL-IMAGE to 105 | /boot/MACHINE-ID/KERNEL-VERSION/linux. 106 | It also creates a boot loader entry according to the boot 107 | loader specification in 108 | /boot/loader/entries/MACHINE-ID-KERNEL-VERSION.conf. 109 | The title of the entry is the 110 | PRETTY_NAME parameter specified 111 | in /etc/os-release or 112 | /usr/lib/os-release (if the former is 113 | missing), or "Linux 114 | KERNEL-VERSION", if unset. If 115 | the file initrd is found next to the 116 | linux file, the initrd will be added to 117 | the configuration. 118 | 119 | 120 | 121 | remove KERNEL-VERSION 122 | 123 | Calls executables from /usr/lib/kernel/install.d/*.install 124 | and /etc/kernel/install.d/*.install with the arguments 125 | remove KERNEL-VERSION /boot/MACHINE-ID/KERNEL-VERSION/ 126 | 127 | 128 | kernel-install removes the entire directory 129 | /boot/MACHINE-ID/KERNEL-VERSION/ afterwards. 130 | 131 | The kernel-install plugin 90-loaderentry.install removes the file 132 | /boot/loader/entries/MACHINE-ID-KERNEL-VERSION.conf. 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | Exit status 142 | If every executable returns 0 or 77, 0 is returned, and a non-zero failure code otherwise. 143 | 144 | 145 | 146 | Files 147 | 148 | 149 | 150 | /usr/lib/kernel/install.d/*.install 151 | /etc/kernel/install.d/*.install 152 | 153 | 154 | Drop-in files which are executed by kernel-install. 155 | 156 | 157 | 158 | 159 | /etc/kernel/cmdline 160 | /proc/cmdline 161 | 162 | 163 | The content of the file /etc/kernel/cmdline specifies the kernel command line to use. 164 | If that file does not exist, /proc/cmdline is used. 165 | 166 | 167 | 168 | 169 | /etc/machine-id 170 | 171 | 172 | The content of the file specifies the machine identification MACHINE-ID. 173 | 174 | 175 | 176 | 177 | /etc/os-release 178 | /usr/lib/os-release 179 | 180 | 181 | The content of the file specifies the operating system title PRETTY_NAME. 182 | 183 | 184 | 185 | 186 | 187 | 188 | See Also 189 | 190 | machine-id5, 191 | os-release5, 192 | Boot loader specification 193 | 194 | 195 | 196 | 197 | -------------------------------------------------------------------------------- /project/src/sd-boot/console.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) 2012-2013 Kay Sievers 14 | * Copyright (C) 2012 Harald Hoyer 15 | */ 16 | 17 | #include 18 | #include 19 | 20 | #include "console.h" 21 | #include "util.h" 22 | 23 | #define SYSTEM_FONT_WIDTH 8 24 | #define SYSTEM_FONT_HEIGHT 19 25 | 26 | #define EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID \ 27 | { 0xdd9e7534, 0x7762, 0x4698, { 0x8c, 0x14, 0xf5, 0x85, 0x17, 0xa6, 0x25, 0xaa } } 28 | 29 | struct _EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL; 30 | 31 | typedef EFI_STATUS (EFIAPI *EFI_INPUT_RESET_EX)( 32 | struct _EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, 33 | BOOLEAN ExtendedVerification 34 | ); 35 | 36 | typedef UINT8 EFI_KEY_TOGGLE_STATE; 37 | 38 | typedef struct { 39 | UINT32 KeyShiftState; 40 | EFI_KEY_TOGGLE_STATE KeyToggleState; 41 | } EFI_KEY_STATE; 42 | 43 | typedef struct { 44 | EFI_INPUT_KEY Key; 45 | EFI_KEY_STATE KeyState; 46 | } EFI_KEY_DATA; 47 | 48 | typedef EFI_STATUS (EFIAPI *EFI_INPUT_READ_KEY_EX)( 49 | struct _EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, 50 | EFI_KEY_DATA *KeyData 51 | ); 52 | 53 | typedef EFI_STATUS (EFIAPI *EFI_SET_STATE)( 54 | struct _EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, 55 | EFI_KEY_TOGGLE_STATE *KeyToggleState 56 | ); 57 | 58 | typedef EFI_STATUS (EFIAPI *EFI_KEY_NOTIFY_FUNCTION)( 59 | EFI_KEY_DATA *KeyData 60 | ); 61 | 62 | typedef EFI_STATUS (EFIAPI *EFI_REGISTER_KEYSTROKE_NOTIFY)( 63 | struct _EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, 64 | EFI_KEY_DATA KeyData, 65 | EFI_KEY_NOTIFY_FUNCTION KeyNotificationFunction, 66 | VOID **NotifyHandle 67 | ); 68 | 69 | typedef EFI_STATUS (EFIAPI *EFI_UNREGISTER_KEYSTROKE_NOTIFY)( 70 | struct _EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, 71 | VOID *NotificationHandle 72 | ); 73 | 74 | typedef struct _EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL { 75 | EFI_INPUT_RESET_EX Reset; 76 | EFI_INPUT_READ_KEY_EX ReadKeyStrokeEx; 77 | EFI_EVENT WaitForKeyEx; 78 | EFI_SET_STATE SetState; 79 | EFI_REGISTER_KEYSTROKE_NOTIFY RegisterKeyNotify; 80 | EFI_UNREGISTER_KEYSTROKE_NOTIFY UnregisterKeyNotify; 81 | } EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL; 82 | 83 | EFI_STATUS console_key_read(UINT64 *key, BOOLEAN wait) { 84 | EFI_GUID EfiSimpleTextInputExProtocolGuid = EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID; 85 | static EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *TextInputEx; 86 | static BOOLEAN checked; 87 | UINTN index; 88 | EFI_INPUT_KEY k; 89 | EFI_STATUS err; 90 | 91 | if (!checked) { 92 | err = LibLocateProtocol(&EfiSimpleTextInputExProtocolGuid, (VOID **)&TextInputEx); 93 | if (EFI_ERROR(err)) 94 | TextInputEx = NULL; 95 | 96 | checked = TRUE; 97 | } 98 | 99 | /* wait until key is pressed */ 100 | if (wait) 101 | uefi_call_wrapper(BS->WaitForEvent, 3, 1, &ST->ConIn->WaitForKey, &index); 102 | 103 | if (TextInputEx) { 104 | EFI_KEY_DATA keydata; 105 | UINT64 keypress; 106 | 107 | err = uefi_call_wrapper(TextInputEx->ReadKeyStrokeEx, 2, TextInputEx, &keydata); 108 | if (!EFI_ERROR(err)) { 109 | UINT32 shift = 0; 110 | 111 | /* do not distinguish between left and right keys */ 112 | if (keydata.KeyState.KeyShiftState & EFI_SHIFT_STATE_VALID) { 113 | if (keydata.KeyState.KeyShiftState & (EFI_RIGHT_CONTROL_PRESSED|EFI_LEFT_CONTROL_PRESSED)) 114 | shift |= EFI_CONTROL_PRESSED; 115 | if (keydata.KeyState.KeyShiftState & (EFI_RIGHT_ALT_PRESSED|EFI_LEFT_ALT_PRESSED)) 116 | shift |= EFI_ALT_PRESSED; 117 | }; 118 | 119 | /* 32 bit modifier keys + 16 bit scan code + 16 bit unicode */ 120 | keypress = KEYPRESS(shift, keydata.Key.ScanCode, keydata.Key.UnicodeChar); 121 | if (keypress > 0) { 122 | *key = keypress; 123 | return 0; 124 | } 125 | } 126 | } 127 | 128 | /* fallback for firmware which does not support SimpleTextInputExProtocol 129 | * 130 | * This is also called in case ReadKeyStrokeEx did not return a key, because 131 | * some broken firmwares offer SimpleTextInputExProtocol, but never acually 132 | * handle any key. */ 133 | err = uefi_call_wrapper(ST->ConIn->ReadKeyStroke, 2, ST->ConIn, &k); 134 | if (EFI_ERROR(err)) 135 | return err; 136 | 137 | *key = KEYPRESS(0, k.ScanCode, k.UnicodeChar); 138 | return 0; 139 | } 140 | 141 | static EFI_STATUS change_mode(UINTN mode) { 142 | EFI_STATUS err; 143 | 144 | err = uefi_call_wrapper(ST->ConOut->SetMode, 2, ST->ConOut, mode); 145 | 146 | /* Special case mode 1: when using OVMF and qemu, setting it returns error 147 | * and breaks console output. */ 148 | if (EFI_ERROR(err) && mode == 1) 149 | uefi_call_wrapper(ST->ConOut->SetMode, 2, ST->ConOut, (UINTN)0); 150 | 151 | return err; 152 | } 153 | 154 | static UINT64 text_area_from_font_size(void) { 155 | EFI_STATUS err; 156 | UINT64 text_area; 157 | UINTN rows, columns; 158 | 159 | err = uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut, ST->ConOut->Mode->Mode, &columns, &rows); 160 | if (EFI_ERROR(err)) { 161 | columns = 80; 162 | rows = 25; 163 | } 164 | 165 | text_area = SYSTEM_FONT_WIDTH * SYSTEM_FONT_HEIGHT * (UINT64)rows * (UINT64)columns; 166 | 167 | return text_area; 168 | } 169 | 170 | static EFI_STATUS mode_auto(UINTN *mode) { 171 | const UINT32 HORIZONTAL_MAX_OK = 1920; 172 | const UINT32 VERTICAL_MAX_OK = 1080; 173 | const UINT64 VIEWPORT_RATIO = 10; 174 | UINT64 screen_area, text_area; 175 | EFI_GUID GraphicsOutputProtocolGuid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; 176 | EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput; 177 | EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Info; 178 | EFI_STATUS err; 179 | BOOLEAN keep = FALSE; 180 | 181 | err = LibLocateProtocol(&GraphicsOutputProtocolGuid, (VOID **)&GraphicsOutput); 182 | if (!EFI_ERROR(err) && GraphicsOutput->Mode && GraphicsOutput->Mode->Info) { 183 | Info = GraphicsOutput->Mode->Info; 184 | 185 | /* Start verifying if we are in a resolution larger than Full HD 186 | * (1920x1080). If we're not, assume we're in a good mode and do not 187 | * try to change it. */ 188 | if (Info->HorizontalResolution <= HORIZONTAL_MAX_OK && Info->VerticalResolution <= VERTICAL_MAX_OK) 189 | keep = TRUE; 190 | /* For larger resolutions, calculate the ratio of the total screen 191 | * area to the text viewport area. If it's less than 10 times bigger, 192 | * then assume the text is readable and keep the text mode. */ 193 | else { 194 | screen_area = (UINT64)Info->HorizontalResolution * (UINT64)Info->VerticalResolution; 195 | text_area = text_area_from_font_size(); 196 | 197 | if (text_area != 0 && screen_area/text_area < VIEWPORT_RATIO) 198 | keep = TRUE; 199 | } 200 | } 201 | 202 | if (keep) { 203 | /* Just clear the screen instead of changing the mode and return. */ 204 | *mode = ST->ConOut->Mode->Mode; 205 | uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut); 206 | return EFI_SUCCESS; 207 | } 208 | 209 | /* If we reached here, then we have a high resolution screen and the text 210 | * viewport is less than 10% the screen area, so the firmware developer 211 | * screwed up. Try to switch to a better mode. Mode number 2 is first non 212 | * standard mode, which is provided by the device manufacturer, so it should 213 | * be a good mode. 214 | * Note: MaxMode is the number of modes, not the last mode. */ 215 | if (ST->ConOut->Mode->MaxMode > 2) 216 | *mode = 2; 217 | /* Try again with mode different than zero (assume user requests 218 | * auto mode due to some problem with mode zero). */ 219 | else if (ST->ConOut->Mode->MaxMode == 2) 220 | *mode = 1; 221 | /* Else force mode change to zero. */ 222 | else 223 | *mode = 0; 224 | 225 | return change_mode(*mode); 226 | } 227 | 228 | EFI_STATUS console_set_mode(UINTN *mode, enum console_mode_change_type how) { 229 | if (how == CONSOLE_MODE_AUTO) 230 | return mode_auto(mode); 231 | 232 | if (how == CONSOLE_MODE_MAX) { 233 | /* Note: MaxMode is the number of modes, not the last mode. */ 234 | if (ST->ConOut->Mode->MaxMode > 0) 235 | *mode = ST->ConOut->Mode->MaxMode-1; 236 | else 237 | *mode = 0; 238 | } 239 | 240 | return change_mode(*mode); 241 | } 242 | -------------------------------------------------------------------------------- /project/src/sd-boot/shim.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 | * Port to systemd-boot 14 | * Copyright 2017 Max Resch 15 | * 16 | * Security Policy Handling 17 | * Copyright 2012 18 | * https://github.com/mjg59/efitools 19 | */ 20 | 21 | #include 22 | #include 23 | 24 | #include "util.h" 25 | #include "shim.h" 26 | 27 | /* well known shim lock guid */ 28 | #define SHIM_LOCK_GUID 29 | 30 | struct ShimLock { 31 | EFI_STATUS __attribute__((sysv_abi)) (*shim_verify) (VOID *buffer, UINT32 size); 32 | 33 | /* context is actually a struct for the PE header, but it isn't needed so void is sufficient just do define the interface 34 | * see shim.c/shim.h and PeHeader.h in the github shim repo */ 35 | EFI_STATUS __attribute__((sysv_abi)) (*generate_hash) (VOID *data, UINT32 datasize, VOID *context, UINT8 *sha256hash, UINT8 *sha1hash); 36 | 37 | EFI_STATUS __attribute__((sysv_abi)) (*read_header) (VOID *data, UINT32 datasize, VOID *context); 38 | }; 39 | 40 | static const EFI_GUID simple_fs_guid = SIMPLE_FILE_SYSTEM_PROTOCOL; 41 | static const EFI_GUID global_guid = EFI_GLOBAL_VARIABLE; 42 | 43 | static const EFI_GUID security_protocol_guid = { 0xa46423e3, 0x4617, 0x49f1, {0xb9, 0xff, 0xd1, 0xbf, 0xa9, 0x11, 0x58, 0x39 } }; 44 | static const EFI_GUID security2_protocol_guid = { 0x94ab2f58, 0x1438, 0x4ef1, {0x91, 0x52, 0x18, 0x94, 0x1a, 0x3a, 0x0e, 0x68 } }; 45 | static const EFI_GUID shim_lock_guid = { 0x605dab50, 0xe046, 0x4300, {0xab, 0xb6, 0x3d, 0xd8, 0x10, 0xdd, 0x8b, 0x23} }; 46 | 47 | BOOLEAN shim_loaded(void) { 48 | struct ShimLock *shim_lock; 49 | 50 | return uefi_call_wrapper(BS->LocateProtocol, 3, (EFI_GUID*) &shim_lock_guid, NULL, (VOID**) &shim_lock) == EFI_SUCCESS; 51 | } 52 | 53 | static BOOLEAN shim_validate(VOID *data, UINT32 size) { 54 | struct ShimLock *shim_lock; 55 | 56 | if (!data) 57 | return FALSE; 58 | 59 | if (uefi_call_wrapper(BS->LocateProtocol, 3, (EFI_GUID*) &shim_lock_guid, NULL, (VOID**) &shim_lock) != EFI_SUCCESS) 60 | return FALSE; 61 | 62 | if (!shim_lock) 63 | return FALSE; 64 | 65 | return shim_lock->shim_verify(data, size) == EFI_SUCCESS; 66 | } 67 | 68 | BOOLEAN secure_boot_enabled(void) { 69 | _cleanup_freepool_ CHAR8 *b = NULL; 70 | UINTN size; 71 | 72 | if (efivar_get_raw(&global_guid, L"SecureBoot", &b, &size) == EFI_SUCCESS) 73 | return *b > 0; 74 | 75 | return FALSE; 76 | } 77 | 78 | /* 79 | * See the UEFI Platform Initialization manual (Vol2: DXE) for this 80 | */ 81 | struct _EFI_SECURITY2_PROTOCOL; 82 | struct _EFI_SECURITY_PROTOCOL; 83 | struct _EFI_DEVICE_PATH_PROTOCOL; 84 | 85 | typedef struct _EFI_SECURITY2_PROTOCOL EFI_SECURITY2_PROTOCOL; 86 | typedef struct _EFI_SECURITY_PROTOCOL EFI_SECURITY_PROTOCOL; 87 | typedef struct _EFI_DEVICE_PATH_PROTOCOL EFI_DEVICE_PATH_PROTOCOL; 88 | 89 | typedef EFI_STATUS (EFIAPI *EFI_SECURITY_FILE_AUTHENTICATION_STATE) ( 90 | const EFI_SECURITY_PROTOCOL *This, 91 | UINT32 AuthenticationStatus, 92 | const EFI_DEVICE_PATH_PROTOCOL *File 93 | ); 94 | 95 | typedef EFI_STATUS (EFIAPI *EFI_SECURITY2_FILE_AUTHENTICATION) ( 96 | const EFI_SECURITY2_PROTOCOL *This, 97 | const EFI_DEVICE_PATH_PROTOCOL *DevicePath, 98 | VOID *FileBuffer, 99 | UINTN FileSize, 100 | BOOLEAN BootPolicy 101 | ); 102 | 103 | struct _EFI_SECURITY2_PROTOCOL { 104 | EFI_SECURITY2_FILE_AUTHENTICATION FileAuthentication; 105 | }; 106 | 107 | struct _EFI_SECURITY_PROTOCOL { 108 | EFI_SECURITY_FILE_AUTHENTICATION_STATE FileAuthenticationState; 109 | }; 110 | 111 | /* Handle to the original authenticator for security1 protocol */ 112 | static EFI_SECURITY_FILE_AUTHENTICATION_STATE esfas = NULL; 113 | 114 | /* Handle to the original authenticator for security2 protocol */ 115 | static EFI_SECURITY2_FILE_AUTHENTICATION es2fa = NULL; 116 | 117 | /* 118 | * Perform shim/MOK and Secure Boot authentication on a binary that's already been 119 | * loaded into memory. This function does the platform SB authentication first 120 | * but preserves its return value in case of its failure, so that it can be 121 | * returned in case of a shim/MOK authentication failure. This is done because 122 | * the SB failure code seems to vary from one implementation to another, and I 123 | * don't want to interfere with that at this time. 124 | */ 125 | static EFIAPI EFI_STATUS security2_policy_authentication (const EFI_SECURITY2_PROTOCOL *this, 126 | const EFI_DEVICE_PATH_PROTOCOL *device_path, 127 | VOID *file_buffer, UINTN file_size, BOOLEAN boot_policy) { 128 | EFI_STATUS status; 129 | 130 | /* Chain original security policy */ 131 | status = uefi_call_wrapper(es2fa, 5, this, device_path, file_buffer, file_size, boot_policy); 132 | 133 | /* if OK, don't bother with MOK check */ 134 | if (status == EFI_SUCCESS) 135 | return status; 136 | 137 | if (shim_validate(file_buffer, file_size)) 138 | return EFI_SUCCESS; 139 | 140 | return status; 141 | } 142 | 143 | /* 144 | * Perform both shim/MOK and platform Secure Boot authentication. This function loads 145 | * the file and performs shim/MOK authentication first simply to avoid double loads 146 | * of Linux kernels, which are much more likely to be shim/MOK-signed than platform-signed, 147 | * since kernels are big and can take several seconds to load on some computers and 148 | * filesystems. This also has the effect of returning whatever the platform code is for 149 | * authentication failure, be it EFI_ACCESS_DENIED, EFI_SECURITY_VIOLATION, or something 150 | * else. (This seems to vary between implementations.) 151 | */ 152 | static EFIAPI EFI_STATUS security_policy_authentication (const EFI_SECURITY_PROTOCOL *this, UINT32 authentication_status, 153 | const EFI_DEVICE_PATH_PROTOCOL *device_path_const) { 154 | EFI_STATUS status; 155 | _cleanup_freepool_ EFI_DEVICE_PATH *dev_path = NULL; 156 | _cleanup_freepool_ CHAR16 *dev_path_str = NULL; 157 | EFI_HANDLE h; 158 | EFI_FILE *root; 159 | _cleanup_freepool_ CHAR8 *file_buffer = NULL; 160 | UINTN file_size; 161 | 162 | if (!device_path_const) 163 | return EFI_INVALID_PARAMETER; 164 | 165 | dev_path = DuplicateDevicePath((EFI_DEVICE_PATH*) device_path_const); 166 | 167 | status = uefi_call_wrapper(BS->LocateDevicePath, 3, (EFI_GUID*) &simple_fs_guid, &dev_path, &h); 168 | if (status != EFI_SUCCESS) 169 | return status; 170 | 171 | /* No need to check return value, this already happend in efi_main() */ 172 | root = LibOpenRoot(h); 173 | dev_path_str = DevicePathToStr(dev_path); 174 | 175 | status = file_read(root, dev_path_str, 0, 0, &file_buffer, &file_size); 176 | if (EFI_ERROR(status)) 177 | return status; 178 | uefi_call_wrapper(root->Close, 1, root); 179 | 180 | if (shim_validate(file_buffer, file_size)) 181 | return EFI_SUCCESS; 182 | 183 | /* Try using the platform's native policy.... */ 184 | return uefi_call_wrapper(esfas, 3, this, authentication_status, device_path_const); 185 | } 186 | 187 | EFI_STATUS security_policy_install(void) { 188 | EFI_SECURITY_PROTOCOL *security_protocol; 189 | EFI_SECURITY2_PROTOCOL *security2_protocol = NULL; 190 | EFI_STATUS status; 191 | 192 | /* Already Installed */ 193 | if (esfas) 194 | return EFI_ALREADY_STARTED; 195 | 196 | /* 197 | * Don't bother with status here. The call is allowed 198 | * to fail, since SECURITY2 was introduced in PI 1.2.1. 199 | * Use security2_protocol == NULL as indicator. 200 | */ 201 | uefi_call_wrapper(BS->LocateProtocol, 3, (EFI_GUID*) &security2_protocol_guid, NULL, (VOID**) &security2_protocol); 202 | 203 | status = uefi_call_wrapper(BS->LocateProtocol, 3, (EFI_GUID*) &security_protocol_guid, NULL, (VOID**) &security_protocol); 204 | /* This one is mandatory, so there's a serious problem */ 205 | if (status != EFI_SUCCESS) 206 | return status; 207 | 208 | esfas = security_protocol->FileAuthenticationState; 209 | security_protocol->FileAuthenticationState = security_policy_authentication; 210 | 211 | if (security2_protocol) { 212 | es2fa = security2_protocol->FileAuthentication; 213 | security2_protocol->FileAuthentication = security2_policy_authentication; 214 | } 215 | 216 | return EFI_SUCCESS; 217 | } 218 | 219 | EFI_STATUS security_policy_uninstall(void) { 220 | EFI_STATUS status; 221 | 222 | if (esfas) { 223 | EFI_SECURITY_PROTOCOL *security_protocol; 224 | 225 | status = uefi_call_wrapper(BS->LocateProtocol, 3, (EFI_GUID*) &security_protocol_guid, NULL, (VOID**) &security_protocol); 226 | 227 | if (status != EFI_SUCCESS) 228 | return status; 229 | 230 | security_protocol->FileAuthenticationState = esfas; 231 | esfas = NULL; 232 | } else 233 | /* nothing installed */ 234 | return EFI_NOT_STARTED; 235 | 236 | if (es2fa) { 237 | EFI_SECURITY2_PROTOCOL *security2_protocol; 238 | 239 | status = uefi_call_wrapper(BS->LocateProtocol, 3, (EFI_GUID*) &security2_protocol_guid, NULL, (VOID**) &security2_protocol); 240 | 241 | if (status != EFI_SUCCESS) 242 | return status; 243 | 244 | security2_protocol->FileAuthentication = es2fa; 245 | es2fa = NULL; 246 | } 247 | 248 | return EFI_SUCCESS; 249 | } 250 | -------------------------------------------------------------------------------- /project/src/sd-boot/util.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) 2012-2013 Kay Sievers 14 | * Copyright (C) 2012 Harald Hoyer 15 | */ 16 | 17 | #include 18 | #include 19 | 20 | #include "util.h" 21 | 22 | /* 23 | * Allocated random UUID, intended to be shared across tools that implement 24 | * the (ESP)\loader\entries\-.conf convention and the 25 | * associated EFI variables. 26 | */ 27 | static const EFI_GUID loader_guid = { 0x4a67b082, 0x0a4c, 0x41cf, {0xb6, 0xc7, 0x44, 0x0b, 0x29, 0xbb, 0x8c, 0x4f} }; 28 | 29 | #ifdef __x86_64__ 30 | UINT64 ticks_read(VOID) { 31 | UINT64 a, d; 32 | __asm__ volatile ("rdtsc" : "=a" (a), "=d" (d)); 33 | return (d << 32) | a; 34 | } 35 | #elif defined(__i386__) 36 | UINT64 ticks_read(VOID) { 37 | UINT64 val; 38 | __asm__ volatile ("rdtsc" : "=A" (val)); 39 | return val; 40 | } 41 | #else 42 | UINT64 ticks_read(VOID) { 43 | UINT64 val = 1; 44 | return val; 45 | } 46 | #endif 47 | 48 | /* count TSC ticks during a millisecond delay */ 49 | UINT64 ticks_freq(VOID) { 50 | UINT64 ticks_start, ticks_end; 51 | 52 | ticks_start = ticks_read(); 53 | uefi_call_wrapper(BS->Stall, 1, 1000); 54 | ticks_end = ticks_read(); 55 | 56 | return (ticks_end - ticks_start) * 1000; 57 | } 58 | 59 | UINT64 time_usec(VOID) { 60 | UINT64 ticks; 61 | static UINT64 freq; 62 | 63 | ticks = ticks_read(); 64 | if (ticks == 0) 65 | return 0; 66 | 67 | if (freq == 0) { 68 | freq = ticks_freq(); 69 | if (freq == 0) 70 | return 0; 71 | } 72 | 73 | return 1000 * 1000 * ticks / freq; 74 | } 75 | 76 | EFI_STATUS parse_boolean(CHAR8 *v, BOOLEAN *b) { 77 | if (strcmpa(v, (CHAR8 *)"1") == 0 || 78 | strcmpa(v, (CHAR8 *)"yes") == 0 || 79 | strcmpa(v, (CHAR8 *)"y") == 0 || 80 | strcmpa(v, (CHAR8 *)"true") == 0) { 81 | *b = TRUE; 82 | return EFI_SUCCESS; 83 | } 84 | 85 | if (strcmpa(v, (CHAR8 *)"0") == 0 || 86 | strcmpa(v, (CHAR8 *)"no") == 0 || 87 | strcmpa(v, (CHAR8 *)"n") == 0 || 88 | strcmpa(v, (CHAR8 *)"false") == 0) { 89 | *b = FALSE; 90 | return EFI_SUCCESS; 91 | } 92 | 93 | return EFI_INVALID_PARAMETER; 94 | } 95 | 96 | EFI_STATUS efivar_set_raw(const EFI_GUID *vendor, CHAR16 *name, CHAR8 *buf, UINTN size, BOOLEAN persistent) { 97 | UINT32 flags; 98 | 99 | flags = EFI_VARIABLE_BOOTSERVICE_ACCESS|EFI_VARIABLE_RUNTIME_ACCESS; 100 | if (persistent) 101 | flags |= EFI_VARIABLE_NON_VOLATILE; 102 | 103 | return uefi_call_wrapper(RT->SetVariable, 5, name, (EFI_GUID *)vendor, flags, size, buf); 104 | } 105 | 106 | EFI_STATUS efivar_set(CHAR16 *name, CHAR16 *value, BOOLEAN persistent) { 107 | return efivar_set_raw(&loader_guid, name, (CHAR8 *)value, value ? (StrLen(value)+1) * sizeof(CHAR16) : 0, persistent); 108 | } 109 | 110 | EFI_STATUS efivar_set_int(CHAR16 *name, UINTN i, BOOLEAN persistent) { 111 | CHAR16 str[32]; 112 | 113 | SPrint(str, 32, L"%d", i); 114 | return efivar_set(name, str, persistent); 115 | } 116 | 117 | EFI_STATUS efivar_get(CHAR16 *name, CHAR16 **value) { 118 | _cleanup_freepool_ CHAR8 *buf = NULL; 119 | CHAR16 *val; 120 | UINTN size; 121 | EFI_STATUS err; 122 | 123 | err = efivar_get_raw(&loader_guid, name, &buf, &size); 124 | if (EFI_ERROR(err)) 125 | return err; 126 | 127 | val = StrDuplicate((CHAR16 *)buf); 128 | if (!val) 129 | return EFI_OUT_OF_RESOURCES; 130 | 131 | *value = val; 132 | return EFI_SUCCESS; 133 | } 134 | 135 | EFI_STATUS efivar_get_int(CHAR16 *name, UINTN *i) { 136 | _cleanup_freepool_ CHAR16 *val = NULL; 137 | EFI_STATUS err; 138 | 139 | err = efivar_get(name, &val); 140 | if (!EFI_ERROR(err)) 141 | *i = Atoi(val); 142 | 143 | return err; 144 | } 145 | 146 | EFI_STATUS efivar_get_raw(const EFI_GUID *vendor, CHAR16 *name, CHAR8 **buffer, UINTN *size) { 147 | _cleanup_freepool_ CHAR8 *buf = NULL; 148 | UINTN l; 149 | EFI_STATUS err; 150 | 151 | l = sizeof(CHAR16 *) * EFI_MAXIMUM_VARIABLE_SIZE; 152 | buf = AllocatePool(l); 153 | if (!buf) 154 | return EFI_OUT_OF_RESOURCES; 155 | 156 | err = uefi_call_wrapper(RT->GetVariable, 5, name, (EFI_GUID *)vendor, NULL, &l, buf); 157 | if (!EFI_ERROR(err)) { 158 | *buffer = buf; 159 | buf = NULL; 160 | if (size) 161 | *size = l; 162 | } 163 | 164 | return err; 165 | } 166 | 167 | VOID efivar_set_time_usec(CHAR16 *name, UINT64 usec) { 168 | CHAR16 str[32]; 169 | 170 | if (usec == 0) 171 | usec = time_usec(); 172 | if (usec == 0) 173 | return; 174 | 175 | SPrint(str, 32, L"%ld", usec); 176 | efivar_set(name, str, FALSE); 177 | } 178 | 179 | static INTN utf8_to_16(CHAR8 *stra, CHAR16 *c) { 180 | CHAR16 unichar; 181 | UINTN len; 182 | UINTN i; 183 | 184 | if (stra[0] < 0x80) 185 | len = 1; 186 | else if ((stra[0] & 0xe0) == 0xc0) 187 | len = 2; 188 | else if ((stra[0] & 0xf0) == 0xe0) 189 | len = 3; 190 | else if ((stra[0] & 0xf8) == 0xf0) 191 | len = 4; 192 | else if ((stra[0] & 0xfc) == 0xf8) 193 | len = 5; 194 | else if ((stra[0] & 0xfe) == 0xfc) 195 | len = 6; 196 | else 197 | return -1; 198 | 199 | switch (len) { 200 | case 1: 201 | unichar = stra[0]; 202 | break; 203 | case 2: 204 | unichar = stra[0] & 0x1f; 205 | break; 206 | case 3: 207 | unichar = stra[0] & 0x0f; 208 | break; 209 | case 4: 210 | unichar = stra[0] & 0x07; 211 | break; 212 | case 5: 213 | unichar = stra[0] & 0x03; 214 | break; 215 | case 6: 216 | unichar = stra[0] & 0x01; 217 | break; 218 | } 219 | 220 | for (i = 1; i < len; i++) { 221 | if ((stra[i] & 0xc0) != 0x80) 222 | return -1; 223 | unichar <<= 6; 224 | unichar |= stra[i] & 0x3f; 225 | } 226 | 227 | *c = unichar; 228 | return len; 229 | } 230 | 231 | CHAR16 *stra_to_str(CHAR8 *stra) { 232 | UINTN strlen; 233 | UINTN len; 234 | UINTN i; 235 | CHAR16 *str; 236 | 237 | len = strlena(stra); 238 | str = AllocatePool((len + 1) * sizeof(CHAR16)); 239 | 240 | strlen = 0; 241 | i = 0; 242 | while (i < len) { 243 | INTN utf8len; 244 | 245 | utf8len = utf8_to_16(stra + i, str + strlen); 246 | if (utf8len <= 0) { 247 | /* invalid utf8 sequence, skip the garbage */ 248 | i++; 249 | continue; 250 | } 251 | 252 | strlen++; 253 | i += utf8len; 254 | } 255 | str[strlen] = '\0'; 256 | return str; 257 | } 258 | 259 | CHAR16 *stra_to_path(CHAR8 *stra) { 260 | CHAR16 *str; 261 | UINTN strlen; 262 | UINTN len; 263 | UINTN i; 264 | 265 | len = strlena(stra); 266 | str = AllocatePool((len + 2) * sizeof(CHAR16)); 267 | 268 | str[0] = '\\'; 269 | strlen = 1; 270 | i = 0; 271 | while (i < len) { 272 | INTN utf8len; 273 | 274 | utf8len = utf8_to_16(stra + i, str + strlen); 275 | if (utf8len <= 0) { 276 | /* invalid utf8 sequence, skip the garbage */ 277 | i++; 278 | continue; 279 | } 280 | 281 | if (str[strlen] == '/') 282 | str[strlen] = '\\'; 283 | if (str[strlen] == '\\' && str[strlen-1] == '\\') { 284 | /* skip double slashes */ 285 | i += utf8len; 286 | continue; 287 | } 288 | 289 | strlen++; 290 | i += utf8len; 291 | } 292 | str[strlen] = '\0'; 293 | return str; 294 | } 295 | 296 | CHAR8 *strchra(CHAR8 *s, CHAR8 c) { 297 | do { 298 | if (*s == c) 299 | return s; 300 | } while (*s++); 301 | return NULL; 302 | } 303 | 304 | EFI_STATUS file_read(EFI_FILE_HANDLE dir, CHAR16 *name, UINTN off, UINTN size, CHAR8 **content, UINTN *content_size) { 305 | EFI_FILE_HANDLE handle; 306 | _cleanup_freepool_ CHAR8 *buf = NULL; 307 | EFI_STATUS err; 308 | 309 | err = uefi_call_wrapper(dir->Open, 5, dir, &handle, name, EFI_FILE_MODE_READ, 0ULL); 310 | if (EFI_ERROR(err)) 311 | return err; 312 | 313 | if (size == 0) { 314 | _cleanup_freepool_ EFI_FILE_INFO *info; 315 | 316 | info = LibFileInfo(handle); 317 | size = info->FileSize+1; 318 | } 319 | 320 | if (off > 0) { 321 | err = uefi_call_wrapper(handle->SetPosition, 2, handle, off); 322 | if (EFI_ERROR(err)) 323 | return err; 324 | } 325 | 326 | buf = AllocatePool(size + 1); 327 | err = uefi_call_wrapper(handle->Read, 3, handle, &size, buf); 328 | if (!EFI_ERROR(err)) { 329 | buf[size] = '\0'; 330 | *content = buf; 331 | buf = NULL; 332 | if (content_size) 333 | *content_size = size; 334 | } 335 | uefi_call_wrapper(handle->Close, 1, handle); 336 | 337 | return err; 338 | } 339 | -------------------------------------------------------------------------------- /project/src/sd-boot/splash.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) 2012-2013 Kay Sievers 14 | * Copyright (C) 2012 Harald Hoyer 15 | */ 16 | 17 | #include 18 | #include 19 | 20 | #include "graphics.h" 21 | #include "splash.h" 22 | #include "util.h" 23 | 24 | struct bmp_file { 25 | CHAR8 signature[2]; 26 | UINT32 size; 27 | UINT16 reserved[2]; 28 | UINT32 offset; 29 | } __attribute__((packed)); 30 | 31 | /* we require at least BITMAPINFOHEADER, later versions are 32 | accepted, but their features ignored */ 33 | struct bmp_dib { 34 | UINT32 size; 35 | UINT32 x; 36 | UINT32 y; 37 | UINT16 planes; 38 | UINT16 depth; 39 | UINT32 compression; 40 | UINT32 image_size; 41 | INT32 x_pixel_meter; 42 | INT32 y_pixel_meter; 43 | UINT32 colors_used; 44 | UINT32 colors_important; 45 | } __attribute__((packed)); 46 | 47 | struct bmp_map { 48 | UINT8 blue; 49 | UINT8 green; 50 | UINT8 red; 51 | UINT8 reserved; 52 | } __attribute__((packed)); 53 | 54 | EFI_STATUS bmp_parse_header(UINT8 *bmp, UINTN size, struct bmp_dib **ret_dib, 55 | struct bmp_map **ret_map, UINT8 **pixmap) { 56 | struct bmp_file *file; 57 | struct bmp_dib *dib; 58 | struct bmp_map *map; 59 | UINTN row_size; 60 | 61 | if (size < sizeof(struct bmp_file) + sizeof(struct bmp_dib)) 62 | return EFI_INVALID_PARAMETER; 63 | 64 | /* check file header */ 65 | file = (struct bmp_file *)bmp; 66 | if (file->signature[0] != 'B' || file->signature[1] != 'M') 67 | return EFI_INVALID_PARAMETER; 68 | if (file->size != size) 69 | return EFI_INVALID_PARAMETER; 70 | if (file->size < file->offset) 71 | return EFI_INVALID_PARAMETER; 72 | 73 | /* check device-independent bitmap */ 74 | dib = (struct bmp_dib *)(bmp + sizeof(struct bmp_file)); 75 | if (dib->size < sizeof(struct bmp_dib)) 76 | return EFI_UNSUPPORTED; 77 | 78 | switch (dib->depth) { 79 | case 1: 80 | case 4: 81 | case 8: 82 | case 24: 83 | if (dib->compression != 0) 84 | return EFI_UNSUPPORTED; 85 | 86 | break; 87 | 88 | case 16: 89 | case 32: 90 | if (dib->compression != 0 && dib->compression != 3) 91 | return EFI_UNSUPPORTED; 92 | 93 | break; 94 | 95 | default: 96 | return EFI_UNSUPPORTED; 97 | } 98 | 99 | row_size = ((UINTN) dib->depth * dib->x + 31) / 32 * 4; 100 | if (file->size - file->offset < dib->y * row_size) 101 | return EFI_INVALID_PARAMETER; 102 | if (row_size * dib->y > 64 * 1024 * 1024) 103 | return EFI_INVALID_PARAMETER; 104 | 105 | /* check color table */ 106 | map = (struct bmp_map *)(bmp + sizeof(struct bmp_file) + dib->size); 107 | if (file->offset < sizeof(struct bmp_file) + dib->size) 108 | return EFI_INVALID_PARAMETER; 109 | 110 | if (file->offset > sizeof(struct bmp_file) + dib->size) { 111 | UINT32 map_count; 112 | UINTN map_size; 113 | 114 | if (dib->colors_used) 115 | map_count = dib->colors_used; 116 | else { 117 | switch (dib->depth) { 118 | case 1: 119 | case 4: 120 | case 8: 121 | map_count = 1 << dib->depth; 122 | break; 123 | 124 | default: 125 | map_count = 0; 126 | break; 127 | } 128 | } 129 | 130 | map_size = file->offset - (sizeof(struct bmp_file) + dib->size); 131 | if (map_size != sizeof(struct bmp_map) * map_count) 132 | return EFI_INVALID_PARAMETER; 133 | } 134 | 135 | *ret_map = map; 136 | *ret_dib = dib; 137 | *pixmap = bmp + file->offset; 138 | 139 | return EFI_SUCCESS; 140 | } 141 | 142 | static VOID pixel_blend(UINT32 *dst, const UINT32 source) { 143 | UINT32 alpha, src, src_rb, src_g, dst_rb, dst_g, rb, g; 144 | 145 | alpha = (source & 0xff); 146 | 147 | /* convert src from RGBA to XRGB */ 148 | src = source >> 8; 149 | 150 | /* decompose into RB and G components */ 151 | src_rb = (src & 0xff00ff); 152 | src_g = (src & 0x00ff00); 153 | 154 | dst_rb = (*dst & 0xff00ff); 155 | dst_g = (*dst & 0x00ff00); 156 | 157 | /* blend */ 158 | rb = ((((src_rb - dst_rb) * alpha + 0x800080) >> 8) + dst_rb) & 0xff00ff; 159 | g = ((((src_g - dst_g) * alpha + 0x008000) >> 8) + dst_g) & 0x00ff00; 160 | 161 | *dst = (rb | g); 162 | } 163 | 164 | EFI_STATUS bmp_to_blt(EFI_GRAPHICS_OUTPUT_BLT_PIXEL *buf, 165 | struct bmp_dib *dib, struct bmp_map *map, 166 | UINT8 *pixmap) { 167 | UINT8 *in; 168 | UINTN y; 169 | 170 | /* transform and copy pixels */ 171 | in = pixmap; 172 | for (y = 0; y < dib->y; y++) { 173 | EFI_GRAPHICS_OUTPUT_BLT_PIXEL *out; 174 | UINTN row_size; 175 | UINTN x; 176 | 177 | out = &buf[(dib->y - y - 1) * dib->x]; 178 | for (x = 0; x < dib->x; x++, in++, out++) { 179 | switch (dib->depth) { 180 | case 1: { 181 | UINTN i; 182 | 183 | for (i = 0; i < 8 && x < dib->x; i++) { 184 | out->Red = map[((*in) >> (7 - i)) & 1].red; 185 | out->Green = map[((*in) >> (7 - i)) & 1].green; 186 | out->Blue = map[((*in) >> (7 - i)) & 1].blue; 187 | out++; 188 | x++; 189 | } 190 | out--; 191 | x--; 192 | break; 193 | } 194 | 195 | case 4: { 196 | UINTN i; 197 | 198 | i = (*in) >> 4; 199 | out->Red = map[i].red; 200 | out->Green = map[i].green; 201 | out->Blue = map[i].blue; 202 | if (x < (dib->x - 1)) { 203 | out++; 204 | x++; 205 | i = (*in) & 0x0f; 206 | out->Red = map[i].red; 207 | out->Green = map[i].green; 208 | out->Blue = map[i].blue; 209 | } 210 | break; 211 | } 212 | 213 | case 8: 214 | out->Red = map[*in].red; 215 | out->Green = map[*in].green; 216 | out->Blue = map[*in].blue; 217 | break; 218 | 219 | case 16: { 220 | UINT16 i = *(UINT16 *) in; 221 | 222 | out->Red = (i & 0x7c00) >> 7; 223 | out->Green = (i & 0x3e0) >> 2; 224 | out->Blue = (i & 0x1f) << 3; 225 | in += 1; 226 | break; 227 | } 228 | 229 | case 24: 230 | out->Red = in[2]; 231 | out->Green = in[1]; 232 | out->Blue = in[0]; 233 | in += 2; 234 | break; 235 | 236 | case 32: { 237 | UINT32 i = *(UINT32 *) in; 238 | 239 | pixel_blend((UINT32 *)out, i); 240 | 241 | in += 3; 242 | break; 243 | } 244 | } 245 | } 246 | 247 | /* add row padding; new lines always start at 32 bit boundary */ 248 | row_size = in - pixmap; 249 | in += ((row_size + 3) & ~3) - row_size; 250 | } 251 | 252 | return EFI_SUCCESS; 253 | } 254 | 255 | EFI_STATUS graphics_splash(UINT8 *content, UINTN len, const EFI_GRAPHICS_OUTPUT_BLT_PIXEL *background) { 256 | EFI_GRAPHICS_OUTPUT_BLT_PIXEL pixel = {}; 257 | EFI_GUID GraphicsOutputProtocolGuid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; 258 | EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput = NULL; 259 | struct bmp_dib *dib; 260 | struct bmp_map *map; 261 | UINT8 *pixmap; 262 | UINT64 blt_size; 263 | _cleanup_freepool_ VOID *blt = NULL; 264 | UINTN x_pos = 0; 265 | UINTN y_pos = 0; 266 | EFI_STATUS err; 267 | 268 | if (!background) { 269 | if (StriCmp(L"Apple", ST->FirmwareVendor) == 0) { 270 | pixel.Red = 0xc0; 271 | pixel.Green = 0xc0; 272 | pixel.Blue = 0xc0; 273 | } 274 | background = &pixel; 275 | } 276 | 277 | err = LibLocateProtocol(&GraphicsOutputProtocolGuid, (VOID **)&GraphicsOutput); 278 | if (EFI_ERROR(err)) 279 | return err; 280 | 281 | err = bmp_parse_header(content, len, &dib, &map, &pixmap); 282 | if (EFI_ERROR(err)) 283 | return err; 284 | 285 | if (dib->x < GraphicsOutput->Mode->Info->HorizontalResolution) 286 | x_pos = (GraphicsOutput->Mode->Info->HorizontalResolution - dib->x) / 2; 287 | if (dib->y < GraphicsOutput->Mode->Info->VerticalResolution) 288 | y_pos = (GraphicsOutput->Mode->Info->VerticalResolution - dib->y) / 2; 289 | 290 | uefi_call_wrapper(GraphicsOutput->Blt, 10, GraphicsOutput, 291 | (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)background, 292 | EfiBltVideoFill, 0, 0, 0, 0, 293 | GraphicsOutput->Mode->Info->HorizontalResolution, 294 | GraphicsOutput->Mode->Info->VerticalResolution, 0); 295 | 296 | /* EFI buffer */ 297 | blt_size = dib->x * dib->y * sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL); 298 | blt = AllocatePool(blt_size); 299 | if (!blt) 300 | return EFI_OUT_OF_RESOURCES; 301 | 302 | err = uefi_call_wrapper(GraphicsOutput->Blt, 10, GraphicsOutput, 303 | blt, EfiBltVideoToBltBuffer, x_pos, y_pos, 0, 0, 304 | dib->x, dib->y, 0); 305 | if (EFI_ERROR(err)) 306 | return err; 307 | 308 | err = bmp_to_blt(blt, dib, map, pixmap); 309 | if (EFI_ERROR(err)) 310 | return err; 311 | 312 | err = graphics_mode(TRUE); 313 | if (EFI_ERROR(err)) 314 | return err; 315 | 316 | return uefi_call_wrapper(GraphicsOutput->Blt, 10, GraphicsOutput, 317 | blt, EfiBltBufferToVideo, 0, 0, x_pos, y_pos, 318 | dib->x, dib->y, 0); 319 | } 320 | -------------------------------------------------------------------------------- /project/src/sd-boot/measure.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 | */ 14 | 15 | #if ENABLE_TPM 16 | 17 | #include 18 | #include 19 | #include "measure.h" 20 | 21 | #define EFI_TCG_PROTOCOL_GUID { 0xf541796d, 0xa62e, 0x4954, {0xa7, 0x75, 0x95, 0x84, 0xf6, 0x1b, 0x9c, 0xdd} } 22 | 23 | typedef struct _TCG_VERSION { 24 | UINT8 Major; 25 | UINT8 Minor; 26 | UINT8 RevMajor; 27 | UINT8 RevMinor; 28 | } TCG_VERSION; 29 | 30 | typedef struct tdEFI_TCG2_VERSION { 31 | UINT8 Major; 32 | UINT8 Minor; 33 | } EFI_TCG2_VERSION; 34 | 35 | typedef struct _TCG_BOOT_SERVICE_CAPABILITY { 36 | UINT8 Size; 37 | struct _TCG_VERSION StructureVersion; 38 | struct _TCG_VERSION ProtocolSpecVersion; 39 | UINT8 HashAlgorithmBitmap; 40 | BOOLEAN TPMPresentFlag; 41 | BOOLEAN TPMDeactivatedFlag; 42 | } TCG_BOOT_SERVICE_CAPABILITY; 43 | 44 | typedef struct tdTREE_BOOT_SERVICE_CAPABILITY { 45 | UINT8 Size; 46 | EFI_TCG2_VERSION StructureVersion; 47 | EFI_TCG2_VERSION ProtocolVersion; 48 | UINT32 HashAlgorithmBitmap; 49 | UINT32 SupportedEventLogs; 50 | BOOLEAN TrEEPresentFlag; 51 | UINT16 MaxCommandSize; 52 | UINT16 MaxResponseSize; 53 | UINT32 ManufacturerID; 54 | } TREE_BOOT_SERVICE_CAPABILITY; 55 | 56 | typedef UINT32 TCG_ALGORITHM_ID; 57 | #define TCG_ALG_SHA 0x00000004 // The SHA1 algorithm 58 | 59 | #define SHA1_DIGEST_SIZE 20 60 | 61 | typedef struct _TCG_DIGEST { 62 | UINT8 Digest[SHA1_DIGEST_SIZE]; 63 | } TCG_DIGEST; 64 | 65 | #define EV_IPL 13 66 | 67 | typedef struct _TCG_PCR_EVENT { 68 | UINT32 PCRIndex; 69 | UINT32 EventType; 70 | struct _TCG_DIGEST digest; 71 | UINT32 EventSize; 72 | UINT8 Event[1]; 73 | } TCG_PCR_EVENT; 74 | 75 | INTERFACE_DECL(_EFI_TCG); 76 | 77 | typedef EFI_STATUS(EFIAPI * EFI_TCG_STATUS_CHECK) (IN struct _EFI_TCG * This, 78 | OUT struct _TCG_BOOT_SERVICE_CAPABILITY * ProtocolCapability, 79 | OUT UINT32 * TCGFeatureFlags, 80 | OUT EFI_PHYSICAL_ADDRESS * EventLogLocation, 81 | OUT EFI_PHYSICAL_ADDRESS * EventLogLastEntry); 82 | 83 | typedef EFI_STATUS(EFIAPI * EFI_TCG_HASH_ALL) (IN struct _EFI_TCG * This, 84 | IN UINT8 * HashData, 85 | IN UINT64 HashDataLen, 86 | IN TCG_ALGORITHM_ID AlgorithmId, 87 | IN OUT UINT64 * HashedDataLen, IN OUT UINT8 ** HashedDataResult); 88 | 89 | typedef EFI_STATUS(EFIAPI * EFI_TCG_LOG_EVENT) (IN struct _EFI_TCG * This, 90 | IN struct _TCG_PCR_EVENT * TCGLogData, 91 | IN OUT UINT32 * EventNumber, IN UINT32 Flags); 92 | 93 | typedef EFI_STATUS(EFIAPI * EFI_TCG_PASS_THROUGH_TO_TPM) (IN struct _EFI_TCG * This, 94 | IN UINT32 TpmInputParameterBlockSize, 95 | IN UINT8 * TpmInputParameterBlock, 96 | IN UINT32 TpmOutputParameterBlockSize, 97 | IN UINT8 * TpmOutputParameterBlock); 98 | 99 | typedef EFI_STATUS(EFIAPI * EFI_TCG_HASH_LOG_EXTEND_EVENT) (IN struct _EFI_TCG * This, 100 | IN EFI_PHYSICAL_ADDRESS HashData, 101 | IN UINT64 HashDataLen, 102 | IN TCG_ALGORITHM_ID AlgorithmId, 103 | IN struct _TCG_PCR_EVENT * TCGLogData, 104 | IN OUT UINT32 * EventNumber, 105 | OUT EFI_PHYSICAL_ADDRESS * EventLogLastEntry); 106 | 107 | typedef struct _EFI_TCG { 108 | EFI_TCG_STATUS_CHECK StatusCheck; 109 | EFI_TCG_HASH_ALL HashAll; 110 | EFI_TCG_LOG_EVENT LogEvent; 111 | EFI_TCG_PASS_THROUGH_TO_TPM PassThroughToTPM; 112 | EFI_TCG_HASH_LOG_EXTEND_EVENT HashLogExtendEvent; 113 | } EFI_TCG; 114 | 115 | #define EFI_TCG2_PROTOCOL_GUID {0x607f766c, 0x7455, 0x42be, { 0x93, 0x0b, 0xe4, 0xd7, 0x6d, 0xb2, 0x72, 0x0f }} 116 | 117 | typedef struct tdEFI_TCG2_PROTOCOL EFI_TCG2_PROTOCOL; 118 | 119 | typedef UINT32 EFI_TCG2_EVENT_LOG_BITMAP; 120 | typedef UINT32 EFI_TCG2_EVENT_LOG_FORMAT; 121 | typedef UINT32 EFI_TCG2_EVENT_ALGORITHM_BITMAP; 122 | 123 | #define EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2 0x00000001 124 | #define EFI_TCG2_EVENT_LOG_FORMAT_TCG_2 0x00000002 125 | 126 | typedef struct tdEFI_TCG2_BOOT_SERVICE_CAPABILITY { 127 | UINT8 Size; 128 | EFI_TCG2_VERSION StructureVersion; 129 | EFI_TCG2_VERSION ProtocolVersion; 130 | EFI_TCG2_EVENT_ALGORITHM_BITMAP HashAlgorithmBitmap; 131 | EFI_TCG2_EVENT_LOG_BITMAP SupportedEventLogs; 132 | BOOLEAN TPMPresentFlag; 133 | UINT16 MaxCommandSize; 134 | UINT16 MaxResponseSize; 135 | UINT32 ManufacturerID; 136 | UINT32 NumberOfPCRBanks; 137 | EFI_TCG2_EVENT_ALGORITHM_BITMAP ActivePcrBanks; 138 | } EFI_TCG2_BOOT_SERVICE_CAPABILITY; 139 | 140 | #define EFI_TCG2_EVENT_HEADER_VERSION 1 141 | 142 | typedef struct { 143 | UINT32 HeaderSize; 144 | UINT16 HeaderVersion; 145 | UINT32 PCRIndex; 146 | UINT32 EventType; 147 | } __attribute__ ((packed)) EFI_TCG2_EVENT_HEADER; 148 | 149 | typedef struct tdEFI_TCG2_EVENT { 150 | UINT32 Size; 151 | EFI_TCG2_EVENT_HEADER Header; 152 | UINT8 Event[1]; 153 | } __attribute__ ((packed)) EFI_TCG2_EVENT; 154 | 155 | typedef EFI_STATUS(EFIAPI * EFI_TCG2_GET_CAPABILITY) (IN EFI_TCG2_PROTOCOL * This, 156 | IN OUT EFI_TCG2_BOOT_SERVICE_CAPABILITY * ProtocolCapability); 157 | 158 | typedef EFI_STATUS(EFIAPI * EFI_TCG2_GET_EVENT_LOG) (IN EFI_TCG2_PROTOCOL * This, 159 | IN EFI_TCG2_EVENT_LOG_FORMAT EventLogFormat, 160 | OUT EFI_PHYSICAL_ADDRESS * EventLogLocation, 161 | OUT EFI_PHYSICAL_ADDRESS * EventLogLastEntry, 162 | OUT BOOLEAN * EventLogTruncated); 163 | 164 | typedef EFI_STATUS(EFIAPI * EFI_TCG2_HASH_LOG_EXTEND_EVENT) (IN EFI_TCG2_PROTOCOL * This, 165 | IN UINT64 Flags, 166 | IN EFI_PHYSICAL_ADDRESS DataToHash, 167 | IN UINT64 DataToHashLen, IN EFI_TCG2_EVENT * EfiTcgEvent); 168 | 169 | typedef EFI_STATUS(EFIAPI * EFI_TCG2_SUBMIT_COMMAND) (IN EFI_TCG2_PROTOCOL * This, 170 | IN UINT32 InputParameterBlockSize, 171 | IN UINT8 * InputParameterBlock, 172 | IN UINT32 OutputParameterBlockSize, IN UINT8 * OutputParameterBlock); 173 | 174 | typedef EFI_STATUS(EFIAPI * EFI_TCG2_GET_ACTIVE_PCR_BANKS) (IN EFI_TCG2_PROTOCOL * This, OUT UINT32 * ActivePcrBanks); 175 | 176 | typedef EFI_STATUS(EFIAPI * EFI_TCG2_SET_ACTIVE_PCR_BANKS) (IN EFI_TCG2_PROTOCOL * This, IN UINT32 ActivePcrBanks); 177 | 178 | typedef EFI_STATUS(EFIAPI * EFI_TCG2_GET_RESULT_OF_SET_ACTIVE_PCR_BANKS) (IN EFI_TCG2_PROTOCOL * This, 179 | OUT UINT32 * OperationPresent, OUT UINT32 * Response); 180 | 181 | typedef struct tdEFI_TCG2_PROTOCOL { 182 | EFI_TCG2_GET_CAPABILITY GetCapability; 183 | EFI_TCG2_GET_EVENT_LOG GetEventLog; 184 | EFI_TCG2_HASH_LOG_EXTEND_EVENT HashLogExtendEvent; 185 | EFI_TCG2_SUBMIT_COMMAND SubmitCommand; 186 | EFI_TCG2_GET_ACTIVE_PCR_BANKS GetActivePcrBanks; 187 | EFI_TCG2_SET_ACTIVE_PCR_BANKS SetActivePcrBanks; 188 | EFI_TCG2_GET_RESULT_OF_SET_ACTIVE_PCR_BANKS GetResultOfSetActivePcrBanks; 189 | } EFI_TCG2; 190 | 191 | static EFI_STATUS tpm1_measure_to_pcr_and_event_log(const EFI_TCG *tcg, UINT32 pcrindex, const EFI_PHYSICAL_ADDRESS buffer, 192 | UINTN buffer_size, const CHAR16 *description) { 193 | EFI_STATUS status; 194 | TCG_PCR_EVENT *tcg_event; 195 | UINT32 event_number; 196 | EFI_PHYSICAL_ADDRESS event_log_last; 197 | UINTN desc_len; 198 | 199 | desc_len = (StrLen(description) + 1) * sizeof(CHAR16); 200 | 201 | tcg_event = AllocateZeroPool(desc_len + sizeof(TCG_PCR_EVENT)); 202 | 203 | if (!tcg_event) 204 | return EFI_OUT_OF_RESOURCES; 205 | 206 | tcg_event->EventSize = desc_len; 207 | CopyMem((VOID *) & tcg_event->Event[0], (VOID *) description, desc_len); 208 | 209 | tcg_event->PCRIndex = pcrindex; 210 | tcg_event->EventType = EV_IPL; 211 | 212 | event_number = 1; 213 | status = uefi_call_wrapper(tcg->HashLogExtendEvent, 7, 214 | (EFI_TCG *) tcg, buffer, buffer_size, TCG_ALG_SHA, tcg_event, &event_number, &event_log_last); 215 | 216 | if (EFI_ERROR(status)) 217 | return status; 218 | 219 | uefi_call_wrapper(BS->FreePool, 1, tcg_event); 220 | 221 | return EFI_SUCCESS; 222 | } 223 | 224 | /* 225 | * According to TCG EFI Protocol Specification for TPM 2.0 family, 226 | * all events generated after the invocation of EFI_TCG2_GET_EVENT_LOG 227 | * shall be stored in an instance of an EFI_CONFIGURATION_TABLE aka 228 | * EFI TCG 2.0 final events table. Hence, it is necessary to trigger the 229 | * internal switch through calling get_event_log() in order to allow 230 | * to retrieve the logs from OS runtime. 231 | */ 232 | static EFI_STATUS trigger_tcg2_final_events_table(const EFI_TCG2 *tcg, EFI_TCG2_EVENT_LOG_FORMAT log_fmt) 233 | { 234 | EFI_PHYSICAL_ADDRESS loc; 235 | EFI_PHYSICAL_ADDRESS last_loc; 236 | BOOLEAN truncated; 237 | return uefi_call_wrapper(tcg->GetEventLog, 5, (EFI_TCG2 *) tcg, 238 | log_fmt, &loc, &last_loc, &truncated); 239 | } 240 | 241 | static EFI_STATUS tpm2_measure_to_pcr_and_event_log(const EFI_TCG2 *tcg, UINT32 pcrindex, const EFI_PHYSICAL_ADDRESS buffer, 242 | UINT64 buffer_size, const CHAR16 *description, EFI_TCG2_EVENT_LOG_FORMAT log_fmt) { 243 | EFI_STATUS status; 244 | EFI_TCG2_EVENT *tcg_event; 245 | UINTN desc_len; 246 | static BOOLEAN triggered = FALSE; 247 | 248 | if (triggered == FALSE) { 249 | status = trigger_tcg2_final_events_table(tcg, log_fmt); 250 | if (EFI_ERROR(status)) 251 | return status; 252 | 253 | triggered = TRUE; 254 | } 255 | 256 | desc_len = StrLen(description) * sizeof(CHAR16); 257 | 258 | tcg_event = AllocateZeroPool(sizeof(*tcg_event) - sizeof(tcg_event->Event) + desc_len + 1); 259 | 260 | if (!tcg_event) 261 | return EFI_OUT_OF_RESOURCES; 262 | 263 | tcg_event->Size = sizeof(*tcg_event) - sizeof(tcg_event->Event) + desc_len + 1; 264 | tcg_event->Header.HeaderSize = sizeof(EFI_TCG2_EVENT_HEADER); 265 | tcg_event->Header.HeaderVersion = EFI_TCG2_EVENT_HEADER_VERSION; 266 | tcg_event->Header.PCRIndex = pcrindex; 267 | tcg_event->Header.EventType = EV_IPL; 268 | 269 | CopyMem((VOID *) tcg_event->Event, (VOID *) description, desc_len); 270 | 271 | status = uefi_call_wrapper(tcg->HashLogExtendEvent, 5, (EFI_TCG2 *) tcg, 0, buffer, (UINT64) buffer_size, tcg_event); 272 | 273 | uefi_call_wrapper(BS->FreePool, 1, tcg_event); 274 | 275 | if (EFI_ERROR(status)) 276 | return status; 277 | 278 | return EFI_SUCCESS; 279 | } 280 | 281 | static EFI_TCG * tcg1_interface_check(void) { 282 | EFI_GUID tpm_guid = EFI_TCG_PROTOCOL_GUID; 283 | EFI_STATUS status; 284 | EFI_TCG *tcg; 285 | TCG_BOOT_SERVICE_CAPABILITY capability; 286 | UINT32 features; 287 | EFI_PHYSICAL_ADDRESS event_log_location; 288 | EFI_PHYSICAL_ADDRESS event_log_last_entry; 289 | 290 | status = LibLocateProtocol(&tpm_guid, (void **) &tcg); 291 | 292 | if (EFI_ERROR(status)) 293 | return NULL; 294 | 295 | capability.Size = (UINT8) sizeof(capability); 296 | status = uefi_call_wrapper(tcg->StatusCheck, 5, tcg, &capability, &features, &event_log_location, &event_log_last_entry); 297 | 298 | if (EFI_ERROR(status)) 299 | return NULL; 300 | 301 | if (capability.TPMDeactivatedFlag) 302 | return NULL; 303 | 304 | if (!capability.TPMPresentFlag) 305 | return NULL; 306 | 307 | return tcg; 308 | } 309 | 310 | static EFI_TCG2 * tcg2_interface_check(EFI_TCG2_BOOT_SERVICE_CAPABILITY *caps) { 311 | EFI_GUID tpm2_guid = EFI_TCG2_PROTOCOL_GUID; 312 | EFI_STATUS status; 313 | EFI_TCG2 *tcg; 314 | 315 | status = LibLocateProtocol(&tpm2_guid, (void **) &tcg); 316 | 317 | if (EFI_ERROR(status)) 318 | return NULL; 319 | 320 | caps->Size = (UINT8) sizeof(EFI_TCG2_BOOT_SERVICE_CAPABILITY); 321 | status = uefi_call_wrapper(tcg->GetCapability, 2, tcg, caps); 322 | 323 | if (EFI_ERROR(status)) 324 | return NULL; 325 | 326 | if (caps->StructureVersion.Major == 1 && 327 | caps->StructureVersion.Minor == 0) { 328 | TCG_BOOT_SERVICE_CAPABILITY *caps_1_0; 329 | caps_1_0 = (TCG_BOOT_SERVICE_CAPABILITY *)caps; 330 | if (caps_1_0->TPMPresentFlag) 331 | return tcg; 332 | } 333 | 334 | if (!caps->TPMPresentFlag) 335 | return NULL; 336 | 337 | return tcg; 338 | } 339 | 340 | EFI_STATUS tpm_log_event(UINT32 pcrindex, const EFI_PHYSICAL_ADDRESS buffer, UINTN buffer_size, const CHAR16 *description) { 341 | EFI_TCG *tpm1; 342 | EFI_TCG2 *tpm2; 343 | EFI_TCG2_BOOT_SERVICE_CAPABILITY caps; 344 | 345 | tpm2 = tcg2_interface_check(&caps); 346 | if (tpm2) { 347 | EFI_TCG2_EVENT_LOG_BITMAP supported_logs; 348 | EFI_TCG2_EVENT_LOG_FORMAT log_fmt; 349 | 350 | if (caps.StructureVersion.Major == 1 && 351 | caps.StructureVersion.Minor == 0) 352 | supported_logs = ((TREE_BOOT_SERVICE_CAPABILITY *)&caps)->SupportedEventLogs; 353 | else 354 | supported_logs = caps.SupportedEventLogs; 355 | 356 | if (supported_logs & EFI_TCG2_EVENT_LOG_FORMAT_TCG_2) 357 | log_fmt = EFI_TCG2_EVENT_LOG_FORMAT_TCG_2; 358 | else 359 | log_fmt = EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2; 360 | 361 | uefi_call_wrapper(BS->Stall, 1, 2000 * 1000); 362 | return tpm2_measure_to_pcr_and_event_log(tpm2, pcrindex, buffer, buffer_size, description, log_fmt); 363 | } 364 | 365 | tpm1 = tcg1_interface_check(); 366 | if (tpm1) 367 | return tpm1_measure_to_pcr_and_event_log(tpm1, pcrindex, buffer, buffer_size, description); 368 | 369 | /* No active TPM found, so don't return an error */ 370 | return EFI_SUCCESS; 371 | } 372 | 373 | #endif 374 | -------------------------------------------------------------------------------- /project/LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 2.1, February 1999 3 | 4 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | [This is the first released version of the Lesser GPL. It also counts 10 | as the successor of the GNU Library Public License, version 2, hence 11 | the version number 2.1.] 12 | 13 | Preamble 14 | 15 | The licenses for most software are designed to take away your 16 | freedom to share and change it. By contrast, the GNU General Public 17 | Licenses are intended to guarantee your freedom to share and change 18 | free software--to make sure the software is free for all its users. 19 | 20 | This license, the Lesser General Public License, applies to some 21 | specially designated software packages--typically libraries--of the 22 | Free Software Foundation and other authors who decide to use it. You 23 | can use it too, but we suggest you first think carefully about whether 24 | this license or the ordinary General Public License is the better 25 | strategy to use in any particular case, based on the explanations below. 26 | 27 | When we speak of free software, we are referring to freedom of use, 28 | not price. Our General Public Licenses are designed to make sure that 29 | you have the freedom to distribute copies of free software (and charge 30 | for this service if you wish); that you receive source code or can get 31 | it if you want it; that you can change the software and use pieces of 32 | it in new free programs; and that you are informed that you can do 33 | these things. 34 | 35 | To protect your rights, we need to make restrictions that forbid 36 | distributors to deny you these rights or to ask you to surrender these 37 | rights. These restrictions translate to certain responsibilities for 38 | you if you distribute copies of the library or if you modify it. 39 | 40 | For example, if you distribute copies of the library, whether gratis 41 | or for a fee, you must give the recipients all the rights that we gave 42 | you. You must make sure that they, too, receive or can get the source 43 | code. If you link other code with the library, you must provide 44 | complete object files to the recipients, so that they can relink them 45 | with the library after making changes to the library and recompiling 46 | it. And you must show them these terms so they know their rights. 47 | 48 | We protect your rights with a two-step method: (1) we copyright the 49 | library, and (2) we offer you this license, which gives you legal 50 | permission to copy, distribute and/or modify the library. 51 | 52 | To protect each distributor, we want to make it very clear that 53 | there is no warranty for the free library. Also, if the library is 54 | modified by someone else and passed on, the recipients should know 55 | that what they have is not the original version, so that the original 56 | author's reputation will not be affected by problems that might be 57 | introduced by others. 58 | 59 | Finally, software patents pose a constant threat to the existence of 60 | any free program. We wish to make sure that a company cannot 61 | effectively restrict the users of a free program by obtaining a 62 | restrictive license from a patent holder. Therefore, we insist that 63 | any patent license obtained for a version of the library must be 64 | consistent with the full freedom of use specified in this license. 65 | 66 | Most GNU software, including some libraries, is covered by the 67 | ordinary GNU General Public License. This license, the GNU Lesser 68 | General Public License, applies to certain designated libraries, and 69 | is quite different from the ordinary General Public License. We use 70 | this license for certain libraries in order to permit linking those 71 | libraries into non-free programs. 72 | 73 | When a program is linked with a library, whether statically or using 74 | a shared library, the combination of the two is legally speaking a 75 | combined work, a derivative of the original library. The ordinary 76 | General Public License therefore permits such linking only if the 77 | entire combination fits its criteria of freedom. The Lesser General 78 | Public License permits more lax criteria for linking other code with 79 | the library. 80 | 81 | We call this license the "Lesser" General Public License because it 82 | does Less to protect the user's freedom than the ordinary General 83 | Public License. It also provides other free software developers Less 84 | of an advantage over competing non-free programs. These disadvantages 85 | are the reason we use the ordinary General Public License for many 86 | libraries. However, the Lesser license provides advantages in certain 87 | special circumstances. 88 | 89 | For example, on rare occasions, there may be a special need to 90 | encourage the widest possible use of a certain library, so that it becomes 91 | a de-facto standard. To achieve this, non-free programs must be 92 | allowed to use the library. A more frequent case is that a free 93 | library does the same job as widely used non-free libraries. In this 94 | case, there is little to gain by limiting the free library to free 95 | software only, so we use the Lesser General Public License. 96 | 97 | In other cases, permission to use a particular library in non-free 98 | programs enables a greater number of people to use a large body of 99 | free software. For example, permission to use the GNU C Library in 100 | non-free programs enables many more people to use the whole GNU 101 | operating system, as well as its variant, the GNU/Linux operating 102 | system. 103 | 104 | Although the Lesser General Public License is Less protective of the 105 | users' freedom, it does ensure that the user of a program that is 106 | linked with the Library has the freedom and the wherewithal to run 107 | that program using a modified version of the Library. 108 | 109 | The precise terms and conditions for copying, distribution and 110 | modification follow. Pay close attention to the difference between a 111 | "work based on the library" and a "work that uses the library". The 112 | former contains code derived from the library, whereas the latter must 113 | be combined with the library in order to run. 114 | 115 | GNU LESSER GENERAL PUBLIC LICENSE 116 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 117 | 118 | 0. This License Agreement applies to any software library or other 119 | program which contains a notice placed by the copyright holder or 120 | other authorized party saying it may be distributed under the terms of 121 | this Lesser General Public License (also called "this License"). 122 | Each licensee is addressed as "you". 123 | 124 | A "library" means a collection of software functions and/or data 125 | prepared so as to be conveniently linked with application programs 126 | (which use some of those functions and data) to form executables. 127 | 128 | The "Library", below, refers to any such software library or work 129 | which has been distributed under these terms. A "work based on the 130 | Library" means either the Library or any derivative work under 131 | copyright law: that is to say, a work containing the Library or a 132 | portion of it, either verbatim or with modifications and/or translated 133 | straightforwardly into another language. (Hereinafter, translation is 134 | included without limitation in the term "modification".) 135 | 136 | "Source code" for a work means the preferred form of the work for 137 | making modifications to it. For a library, complete source code means 138 | all the source code for all modules it contains, plus any associated 139 | interface definition files, plus the scripts used to control compilation 140 | and installation of the library. 141 | 142 | Activities other than copying, distribution and modification are not 143 | covered by this License; they are outside its scope. The act of 144 | running a program using the Library is not restricted, and output from 145 | such a program is covered only if its contents constitute a work based 146 | on the Library (independent of the use of the Library in a tool for 147 | writing it). Whether that is true depends on what the Library does 148 | and what the program that uses the Library does. 149 | 150 | 1. You may copy and distribute verbatim copies of the Library's 151 | complete source code as you receive it, in any medium, provided that 152 | you conspicuously and appropriately publish on each copy an 153 | appropriate copyright notice and disclaimer of warranty; keep intact 154 | all the notices that refer to this License and to the absence of any 155 | warranty; and distribute a copy of this License along with the 156 | Library. 157 | 158 | You may charge a fee for the physical act of transferring a copy, 159 | and you may at your option offer warranty protection in exchange for a 160 | fee. 161 | 162 | 2. You may modify your copy or copies of the Library or any portion 163 | of it, thus forming a work based on the Library, and copy and 164 | distribute such modifications or work under the terms of Section 1 165 | above, provided that you also meet all of these conditions: 166 | 167 | a) The modified work must itself be a software library. 168 | 169 | b) You must cause the files modified to carry prominent notices 170 | stating that you changed the files and the date of any change. 171 | 172 | c) You must cause the whole of the work to be licensed at no 173 | charge to all third parties under the terms of this License. 174 | 175 | d) If a facility in the modified Library refers to a function or a 176 | table of data to be supplied by an application program that uses 177 | the facility, other than as an argument passed when the facility 178 | is invoked, then you must make a good faith effort to ensure that, 179 | in the event an application does not supply such function or 180 | table, the facility still operates, and performs whatever part of 181 | its purpose remains meaningful. 182 | 183 | (For example, a function in a library to compute square roots has 184 | a purpose that is entirely well-defined independent of the 185 | application. Therefore, Subsection 2d requires that any 186 | application-supplied function or table used by this function must 187 | be optional: if the application does not supply it, the square 188 | root function must still compute square roots.) 189 | 190 | These requirements apply to the modified work as a whole. If 191 | identifiable sections of that work are not derived from the Library, 192 | and can be reasonably considered independent and separate works in 193 | themselves, then this License, and its terms, do not apply to those 194 | sections when you distribute them as separate works. But when you 195 | distribute the same sections as part of a whole which is a work based 196 | on the Library, the distribution of the whole must be on the terms of 197 | this License, whose permissions for other licensees extend to the 198 | entire whole, and thus to each and every part regardless of who wrote 199 | it. 200 | 201 | Thus, it is not the intent of this section to claim rights or contest 202 | your rights to work written entirely by you; rather, the intent is to 203 | exercise the right to control the distribution of derivative or 204 | collective works based on the Library. 205 | 206 | In addition, mere aggregation of another work not based on the Library 207 | with the Library (or with a work based on the Library) on a volume of 208 | a storage or distribution medium does not bring the other work under 209 | the scope of this License. 210 | 211 | 3. You may opt to apply the terms of the ordinary GNU General Public 212 | License instead of this License to a given copy of the Library. To do 213 | this, you must alter all the notices that refer to this License, so 214 | that they refer to the ordinary GNU General Public License, version 2, 215 | instead of to this License. (If a newer version than version 2 of the 216 | ordinary GNU General Public License has appeared, then you can specify 217 | that version instead if you wish.) Do not make any other change in 218 | these notices. 219 | 220 | Once this change is made in a given copy, it is irreversible for 221 | that copy, so the ordinary GNU General Public License applies to all 222 | subsequent copies and derivative works made from that copy. 223 | 224 | This option is useful when you wish to copy part of the code of 225 | the Library into a program that is not a library. 226 | 227 | 4. You may copy and distribute the Library (or a portion or 228 | derivative of it, under Section 2) in object code or executable form 229 | under the terms of Sections 1 and 2 above provided that you accompany 230 | it with the complete corresponding machine-readable source code, which 231 | must be distributed under the terms of Sections 1 and 2 above on a 232 | medium customarily used for software interchange. 233 | 234 | If distribution of object code is made by offering access to copy 235 | from a designated place, then offering equivalent access to copy the 236 | source code from the same place satisfies the requirement to 237 | distribute the source code, even though third parties are not 238 | compelled to copy the source along with the object code. 239 | 240 | 5. A program that contains no derivative of any portion of the 241 | Library, but is designed to work with the Library by being compiled or 242 | linked with it, is called a "work that uses the Library". Such a 243 | work, in isolation, is not a derivative work of the Library, and 244 | therefore falls outside the scope of this License. 245 | 246 | However, linking a "work that uses the Library" with the Library 247 | creates an executable that is a derivative of the Library (because it 248 | contains portions of the Library), rather than a "work that uses the 249 | library". The executable is therefore covered by this License. 250 | Section 6 states terms for distribution of such executables. 251 | 252 | When a "work that uses the Library" uses material from a header file 253 | that is part of the Library, the object code for the work may be a 254 | derivative work of the Library even though the source code is not. 255 | Whether this is true is especially significant if the work can be 256 | linked without the Library, or if the work is itself a library. The 257 | threshold for this to be true is not precisely defined by law. 258 | 259 | If such an object file uses only numerical parameters, data 260 | structure layouts and accessors, and small macros and small inline 261 | functions (ten lines or less in length), then the use of the object 262 | file is unrestricted, regardless of whether it is legally a derivative 263 | work. (Executables containing this object code plus portions of the 264 | Library will still fall under Section 6.) 265 | 266 | Otherwise, if the work is a derivative of the Library, you may 267 | distribute the object code for the work under the terms of Section 6. 268 | Any executables containing that work also fall under Section 6, 269 | whether or not they are linked directly with the Library itself. 270 | 271 | 6. As an exception to the Sections above, you may also combine or 272 | link a "work that uses the Library" with the Library to produce a 273 | work containing portions of the Library, and distribute that work 274 | under terms of your choice, provided that the terms permit 275 | modification of the work for the customer's own use and reverse 276 | engineering for debugging such modifications. 277 | 278 | You must give prominent notice with each copy of the work that the 279 | Library is used in it and that the Library and its use are covered by 280 | this License. You must supply a copy of this License. If the work 281 | during execution displays copyright notices, you must include the 282 | copyright notice for the Library among them, as well as a reference 283 | directing the user to the copy of this License. Also, you must do one 284 | of these things: 285 | 286 | a) Accompany the work with the complete corresponding 287 | machine-readable source code for the Library including whatever 288 | changes were used in the work (which must be distributed under 289 | Sections 1 and 2 above); and, if the work is an executable linked 290 | with the Library, with the complete machine-readable "work that 291 | uses the Library", as object code and/or source code, so that the 292 | user can modify the Library and then relink to produce a modified 293 | executable containing the modified Library. (It is understood 294 | that the user who changes the contents of definitions files in the 295 | Library will not necessarily be able to recompile the application 296 | to use the modified definitions.) 297 | 298 | b) Use a suitable shared library mechanism for linking with the 299 | Library. A suitable mechanism is one that (1) uses at run time a 300 | copy of the library already present on the user's computer system, 301 | rather than copying library functions into the executable, and (2) 302 | will operate properly with a modified version of the library, if 303 | the user installs one, as long as the modified version is 304 | interface-compatible with the version that the work was made with. 305 | 306 | c) Accompany the work with a written offer, valid for at 307 | least three years, to give the same user the materials 308 | specified in Subsection 6a, above, for a charge no more 309 | than the cost of performing this distribution. 310 | 311 | d) If distribution of the work is made by offering access to copy 312 | from a designated place, offer equivalent access to copy the above 313 | specified materials from the same place. 314 | 315 | e) Verify that the user has already received a copy of these 316 | materials or that you have already sent this user a copy. 317 | 318 | For an executable, the required form of the "work that uses the 319 | Library" must include any data and utility programs needed for 320 | reproducing the executable from it. However, as a special exception, 321 | the materials to be distributed need not include anything that is 322 | normally distributed (in either source or binary form) with the major 323 | components (compiler, kernel, and so on) of the operating system on 324 | which the executable runs, unless that component itself accompanies 325 | the executable. 326 | 327 | It may happen that this requirement contradicts the license 328 | restrictions of other proprietary libraries that do not normally 329 | accompany the operating system. Such a contradiction means you cannot 330 | use both them and the Library together in an executable that you 331 | distribute. 332 | 333 | 7. You may place library facilities that are a work based on the 334 | Library side-by-side in a single library together with other library 335 | facilities not covered by this License, and distribute such a combined 336 | library, provided that the separate distribution of the work based on 337 | the Library and of the other library facilities is otherwise 338 | permitted, and provided that you do these two things: 339 | 340 | a) Accompany the combined library with a copy of the same work 341 | based on the Library, uncombined with any other library 342 | facilities. This must be distributed under the terms of the 343 | Sections above. 344 | 345 | b) Give prominent notice with the combined library of the fact 346 | that part of it is a work based on the Library, and explaining 347 | where to find the accompanying uncombined form of the same work. 348 | 349 | 8. You may not copy, modify, sublicense, link with, or distribute 350 | the Library except as expressly provided under this License. Any 351 | attempt otherwise to copy, modify, sublicense, link with, or 352 | distribute the Library is void, and will automatically terminate your 353 | rights under this License. However, parties who have received copies, 354 | or rights, from you under this License will not have their licenses 355 | terminated so long as such parties remain in full compliance. 356 | 357 | 9. You are not required to accept this License, since you have not 358 | signed it. However, nothing else grants you permission to modify or 359 | distribute the Library or its derivative works. These actions are 360 | prohibited by law if you do not accept this License. Therefore, by 361 | modifying or distributing the Library (or any work based on the 362 | Library), you indicate your acceptance of this License to do so, and 363 | all its terms and conditions for copying, distributing or modifying 364 | the Library or works based on it. 365 | 366 | 10. Each time you redistribute the Library (or any work based on the 367 | Library), the recipient automatically receives a license from the 368 | original licensor to copy, distribute, link with or modify the Library 369 | subject to these terms and conditions. You may not impose any further 370 | restrictions on the recipients' exercise of the rights granted herein. 371 | You are not responsible for enforcing compliance by third parties with 372 | this License. 373 | 374 | 11. If, as a consequence of a court judgment or allegation of patent 375 | infringement or for any other reason (not limited to patent issues), 376 | conditions are imposed on you (whether by court order, agreement or 377 | otherwise) that contradict the conditions of this License, they do not 378 | excuse you from the conditions of this License. If you cannot 379 | distribute so as to satisfy simultaneously your obligations under this 380 | License and any other pertinent obligations, then as a consequence you 381 | may not distribute the Library at all. For example, if a patent 382 | license would not permit royalty-free redistribution of the Library by 383 | all those who receive copies directly or indirectly through you, then 384 | the only way you could satisfy both it and this License would be to 385 | refrain entirely from distribution of the Library. 386 | 387 | If any portion of this section is held invalid or unenforceable under any 388 | particular circumstance, the balance of the section is intended to apply, 389 | and the section as a whole is intended to apply in other circumstances. 390 | 391 | It is not the purpose of this section to induce you to infringe any 392 | patents or other property right claims or to contest validity of any 393 | such claims; this section has the sole purpose of protecting the 394 | integrity of the free software distribution system which is 395 | implemented by public license practices. Many people have made 396 | generous contributions to the wide range of software distributed 397 | through that system in reliance on consistent application of that 398 | system; it is up to the author/donor to decide if he or she is willing 399 | to distribute software through any other system and a licensee cannot 400 | impose that choice. 401 | 402 | This section is intended to make thoroughly clear what is believed to 403 | be a consequence of the rest of this License. 404 | 405 | 12. If the distribution and/or use of the Library is restricted in 406 | certain countries either by patents or by copyrighted interfaces, the 407 | original copyright holder who places the Library under this License may add 408 | an explicit geographical distribution limitation excluding those countries, 409 | so that distribution is permitted only in or among countries not thus 410 | excluded. In such case, this License incorporates the limitation as if 411 | written in the body of this License. 412 | 413 | 13. The Free Software Foundation may publish revised and/or new 414 | versions of the Lesser General Public License from time to time. 415 | Such new versions will be similar in spirit to the present version, 416 | but may differ in detail to address new problems or concerns. 417 | 418 | Each version is given a distinguishing version number. If the Library 419 | specifies a version number of this License which applies to it and 420 | "any later version", you have the option of following the terms and 421 | conditions either of that version or of any later version published by 422 | the Free Software Foundation. If the Library does not specify a 423 | license version number, you may choose any version ever published by 424 | the Free Software Foundation. 425 | 426 | 14. If you wish to incorporate parts of the Library into other free 427 | programs whose distribution conditions are incompatible with these, 428 | write to the author to ask for permission. For software which is 429 | copyrighted by the Free Software Foundation, write to the Free 430 | Software Foundation; we sometimes make exceptions for this. Our 431 | decision will be guided by the two goals of preserving the free status 432 | of all derivatives of our free software and of promoting the sharing 433 | and reuse of software generally. 434 | 435 | NO WARRANTY 436 | 437 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 438 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 439 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 440 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 441 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 442 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 443 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 444 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 445 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 446 | 447 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 448 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 449 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 450 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 451 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 452 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 453 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 454 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 455 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 456 | DAMAGES. 457 | 458 | END OF TERMS AND CONDITIONS 459 | 460 | How to Apply These Terms to Your New Libraries 461 | 462 | If you develop a new library, and you want it to be of the greatest 463 | possible use to the public, we recommend making it free software that 464 | everyone can redistribute and change. You can do so by permitting 465 | redistribution under these terms (or, alternatively, under the terms of the 466 | ordinary General Public License). 467 | 468 | To apply these terms, attach the following notices to the library. It is 469 | safest to attach them to the start of each source file to most effectively 470 | convey the exclusion of warranty; and each file should have at least the 471 | "copyright" line and a pointer to where the full notice is found. 472 | 473 | 474 | Copyright (C) 475 | 476 | This library is free software; you can redistribute it and/or 477 | modify it under the terms of the GNU Lesser General Public 478 | License as published by the Free Software Foundation; either 479 | version 2.1 of the License, or (at your option) any later version. 480 | 481 | This library is distributed in the hope that it will be useful, 482 | but WITHOUT ANY WARRANTY; without even the implied warranty of 483 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 484 | Lesser General Public License for more details. 485 | 486 | You should have received a copy of the GNU Lesser General Public 487 | License along with this library; if not, write to the Free Software 488 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 489 | 490 | Also add information on how to contact you by electronic and paper mail. 491 | 492 | You should also get your employer (if you work as a programmer) or your 493 | school, if any, to sign a "copyright disclaimer" for the library, if 494 | necessary. Here is a sample; alter the names: 495 | 496 | Yoyodyne, Inc., hereby disclaims all copyright interest in the 497 | library `Frob' (a library for tweaking knobs) written by James Random Hacker. 498 | 499 | , 1 April 1990 500 | Ty Coon, President of Vice 501 | 502 | That's all there is to it! 503 | --------------------------------------------------------------------------------