64 Commits

Author SHA1 Message Date
Lauri Kenttä
5856d25b5b Update change log and tag v2.2.0 2023-11-17 23:24:58 +02:00
Lauri Kenttä
466ab69c48 Use shim 15.7 to support Secure Boot
Use shim-signed and shim-helpers-{arch}-signed from Debian:
https://packages.debian.org/bookworm/shim-signed
2023-11-17 23:24:58 +02:00
Lauri Kenttä
14aa79929a Easily override GIT_DESCRIBE 2023-11-17 23:13:12 +02:00
Lauri Kenttä
5e32a3f880 Add clean target to Makefile 2023-11-17 23:13:12 +02:00
Lauri Kenttä
db934099df Gather log during boot 2023-11-17 23:13:12 +02:00
Lauri Kenttä
e93ed54cb2 Make setup menu shorter 2023-11-17 22:47:29 +02:00
Lauri Kenttä
9b3b045a21 Add SBAT section to the EFI binaries 2023-11-17 22:26:43 +02:00
Lauri Kenttä
be8a5d35d2 Sign the EFI files 2023-11-17 22:26:43 +02:00
Lauri Kenttä
2366fc8b98 Warn about Fast Startup (Hiberboot) 2023-11-16 22:56:10 +02:00
Lauri Kenttä
e04ba3e451 Log boot entries during setup 2023-11-16 22:07:08 +02:00
Lauri Kenttä
f40f2bc9dc Clarify LogBGRT output for corner cases 2023-11-09 21:46:39 +02:00
Lauri Kenttä
2f572b24d4 Free BMP if it's invalid 2023-11-09 21:46:35 +02:00
Lauri Kenttä
da9909bbdd Update change log and tag v2.1.0 2023-10-04 15:25:03 +03:00
Lauri Kenttä
0ce904f133 Use 999999 as the 'big' value instead of 2**31-1 2023-10-04 15:25:03 +03:00
Lauri Kenttä
62c892009a Overwrite BGRT in a more readable manner 2023-10-04 15:25:03 +03:00
Lauri Kenttä
00bbfd6d82 Add ACPI table addresses to debug output 2023-10-04 15:25:03 +03:00
Lauri Kenttä
1e566a05a0 Clarify README for image conversion 2023-10-04 15:25:03 +03:00
Lauri Kenttä
1058f3e2b4 Log BGRT contents during setup 2023-10-04 15:25:03 +03:00
Lauri Kenttä
661758ba58 Crop BMP to screen size 2023-09-14 18:07:48 +03:00
Lauri Kenttä
93eec1a250 Improve BMP handling 2023-09-14 18:05:38 +03:00
Lauri Kenttä
33e4450d19 Check that BitLocker is disabled 2023-09-10 23:03:50 +03:00
Lauri Kenttä
99bdf5a310 Update change log and tag v2.0.0 2023-09-10 15:43:04 +03:00
Lauri Kenttä
d2bac39680 Add assembly information to setup.exe 2023-09-10 15:43:04 +03:00
Lauri Kenttä
a4c917294c Add option to boot to firmware 2023-09-10 15:43:04 +03:00
Lauri Kenttä
d6da4b7cee Support creating boot entry with bcdedit 2023-09-10 15:42:18 +03:00
Lauri Kenttä
f2185f624d Detect issues with boot= option 2023-09-10 15:02:58 +03:00
Lauri Kenttä
75e64a7ef3 Handle screen orientation 2023-09-10 15:02:58 +03:00
Lauri Kenttä
e5616c6cd2 Make coordinates relative to center
Relative coordinates make it easier to center the image.
The value "auto" is now replaced with simply 0.
The value "native" is replaced with "keep" for consistency.
The value is clamped, so x=-9999 will align to the left border etc.
2023-09-10 15:02:58 +03:00
Lauri Kenttä
ad0b71c49b Set EFI ReadKey timeout to 15 seconds 2023-09-10 15:02:58 +03:00
Lauri Kenttä
e44ce9f5ee Add logging 2023-09-10 15:02:58 +03:00
Lauri Kenttä
5f3c6afc23 Remove BootLoaderInfo class 2023-09-10 15:02:58 +03:00
Lauri Kenttä
1fa53f3f06 Remove SetupHelper class 2023-09-10 15:02:58 +03:00
Lauri Kenttä
8301a16ca8 Support arch=... parameter, improve auto-detection 2023-09-10 15:02:58 +03:00
Lauri Kenttä
de9d0d984a Add partial Linux support to installer 2023-09-10 15:02:58 +03:00
Lauri Kenttä
7dbdf33ea8 Create an own entry, don't replace MS boot loader 2023-09-10 15:02:58 +03:00
Lauri Kenttä
dfb5b916ed Elevate privileges only when needed 2023-09-10 15:02:58 +03:00
Lauri Kenttä
267af0bd9c Support dry run in the installer 2023-09-10 15:02:58 +03:00
Lauri Kenttä
be5894b387 Check for HackBGRT string when disabling old version 2023-09-10 15:02:58 +03:00
Lauri Kenttä
199650a567 Don't access ESP before showing the menu 2023-09-10 15:02:58 +03:00
Lauri Kenttä
31172f71ca Add support for quiet setup 2023-09-10 15:02:58 +03:00
Lauri Kenttä
990f245ac9 Convert images when installing 2023-09-09 17:55:44 +03:00
Lauri Kenttä
dfadf67a21 Configure before installing 2023-09-09 17:55:44 +03:00
Lauri Kenttä
691fbd164b Improve path handling for platform independency 2023-09-09 17:55:39 +03:00
Lauri Kenttä
7d5f4eac17 Makefile default target: efi, setup and zip 2023-08-27 13:05:19 +03:00
Lauri Kenttä
0b5899e801 Make GIT_DESCRIBE.data const 2023-08-27 13:05:08 +03:00
Lauri Kenttä
4cf12f26b2 Update Print function signature for gnu-efi 3.0.11 2020-03-01 06:03:17 +02:00
Lauri Kenttä
3396a4799d Update change log and tag v1.5.1 2018-08-11 14:01:48 +03:00
Lauri Kenttä
d35a9abb0a Add debug=0 to the default config.txt 2018-08-11 13:45:04 +03:00
Lauri Kenttä
85811d62a6 Add note about EFI System Partition to config.txt 2018-08-11 13:42:41 +03:00
Lauri Kenttä
449dc6acc6 Create OsIndications if it's missing 2018-07-19 19:10:01 +03:00
Lauri Kenttä
1980e5c05c Clarify default config.txt 2018-07-19 19:10:01 +03:00
Lauri Kenttä
9a59f69a28 Compile C# with csc (not mcs) 2018-07-11 16:59:27 +03:00
Lauri Kenttä
1fffbcff2c Fix member naming 2018-07-11 16:55:26 +03:00
Soheibooo
bd7a5f3eea Correct typo in README 2018-03-02 00:45:10 -05:00
Lauri Kenttä
32643fab96 Update change log and tag v1.5.0 2017-09-30 22:41:00 +03:00
Lauri Kenttä
ecbca09419 Update README: clearer installation instructions 2017-09-30 22:41:00 +03:00
Lauri Kenttä
fa0f846f79 Implement rebooting to UEFI setup 2017-09-30 22:41:00 +03:00
Lauri Kenttä
b469b600ba Reorder the setup process a bit 2017-09-30 22:41:00 +03:00
Lauri Kenttä
f7fa54cfee Move Secure Boot handling to another function 2017-09-30 21:23:12 +03:00
Lauri Kenttä
e092c4768c Add throwable for graceful exiting 2017-09-30 16:41:25 +03:00
Lauri Kenttä
7ad4762a3d Improve ESP detection and move to another file 2017-09-30 16:40:17 +03:00
Lauri Kenttä
cea656631a Reorder some BootLoaderInfo code 2017-09-30 15:40:28 +03:00
Lauri Kenttä
1b6b17ec9a Move some helpers to SetupHelper class 2017-09-30 15:39:52 +03:00
Lauri Kenttä
91aad3b971 Makefile: Add variable for C# file list 2017-09-25 21:18:46 +03:00
20 changed files with 2358 additions and 547 deletions

View File

@@ -2,6 +2,49 @@
All notable changes to this project will be documented in this file.
## 2.2.0 - 2023-11-17
### Added
- Support Secure Boot with *shim* boot loader.
- Gather debug log during boot and read it with setup.exe.
## 2.1.0 - 2023-10-04
### Added
- Check image size, crop if it's bigger than the screen.
- Check BitLocker status to avoid unbootable machine.
## 2.0.0 - 2023-09-10
### Added
- Log to `setup.log`.
- Image conversion (GIF, EXIF, JPG, PNG, TIFF) to BMP during setup.
- Quiet (batch) setup.
- Dry run in setup.
- EFI boot entry support in setup.
- Orientation parameter (o=0|90|180|270) for images.
### Changed
- Configure (edit config and images) before installing.
- Escalate privileges only when needed (after the menu).
- Try to detect and avoid some configuration errors.
- Wait at most 15 seconds for key presses during boot.
- Image coordinates are now relative to the center.
## 1.5.1 - 2018-08-11
### Fixed
- Clarify the default config.txt.
- Fix an exception in some cases when trying to boot to UEFI setup.
## 1.5.0 - 2017-09-30
### Added
- Support for rebooting to UEFI setup.
### Changed
- Minor enhancements to installer.
## 1.4.0 - 2017-08-29
### Added

View File

@@ -7,37 +7,65 @@ LIBS = -L$(GNUEFI_LIB) -lefi -lgcc
GNUEFI_INC = /usr/$(CC_PREFIX)/include/efi
GNUEFI_LIB = /usr/$(CC_PREFIX)/lib
FILES_C = src/main.c src/util.c src/types.c src/config.c
FILES_C = src/main.c src/util.c src/types.c src/config.c src/sbat.c
FILES_H = $(wildcard src/*.h)
GIT_DESCRIBE = $(firstword $(shell git describe --tags) unknown)
CFLAGS += '-DGIT_DESCRIBE=L"$(GIT_DESCRIBE)"'
FILES_CS = src/Setup.cs src/Esp.cs src/Efi.cs
GIT_DESCRIBE := $(firstword $(GIT_DESCRIBE) $(shell git describe --tags) unknown)
CFLAGS += '-DGIT_DESCRIBE_W=L"$(GIT_DESCRIBE)"' '-DGIT_DESCRIBE="$(GIT_DESCRIBE)"'
ZIPDIR = HackBGRT-$(GIT_DESCRIBE:v%=%)
ZIP = $(ZIPDIR).zip
efi: bootx64.efi bootia32.efi
setup: setup.exe
.PHONY: all efi efi-signed setup zip clean
all: efi setup
efi: efi/bootx64.efi efi/bootia32.efi
efi-signed: efi-signed/bootx64.efi efi-signed/bootia32.efi
setup: setup.exe
zip: $(ZIP)
$(ZIP): bootx64.efi bootia32.efi config.txt splash.bmp setup.exe README.md CHANGELOG.md README.efilib LICENSE
$(ZIP): efi-signed certificate.cer config.txt splash.bmp setup.exe README.md CHANGELOG.md README.efilib LICENSE shim-signed shim.md
test ! -d "$(ZIPDIR)"
mkdir "$(ZIPDIR)"
cp -a $^ "$(ZIPDIR)" || (rm -rf "$(ZIPDIR)"; exit 1)
7z a -mx=9 "$(ZIP)" "$(ZIPDIR)" || (rm -rf "$(ZIPDIR)"; exit 1)
rm -rf "$(ZIPDIR)"
src/GIT_DESCRIBE.cs: src/Setup.cs $(FILES_C) $(FILES_H)
echo 'public class GIT_DESCRIBE { public static string data = "$(GIT_DESCRIBE)"; }' > $@
src/GIT_DESCRIBE.cs: $(FILES_CS) $(FILES_C) $(FILES_H)
echo 'public class GIT_DESCRIBE { public const string data = "$(GIT_DESCRIBE)"; }' > $@
setup.exe: src/Setup.cs src/GIT_DESCRIBE.cs
mcs -define:GIT_DESCRIBE -out:$@ $^
setup.exe: $(FILES_CS) src/GIT_DESCRIBE.cs
csc /define:GIT_DESCRIBE /out:$@ $^
bootx64.efi: CC_PREFIX = x86_64-w64-mingw32
bootx64.efi: GNUEFI_ARCH = x86_64
bootx64.efi: $(FILES_C)
certificate.cer pki:
@echo
@echo "You need proper keys to sign the EFI executables."
@echo "Example:"
@echo "mkdir -p pki"
@echo "certutil --empty-password -N -d pki"
@echo "efikeygen -d pki -n HackBGRT-signer -S -k -c 'CN=HackBGRT Secure Boot Signer,OU=HackBGRT,O=Unknown,MAIL=unknown@example.com' -u 'URL'"
@echo "certutil -d pki -n HackBGRT-signer -Lr > certificate.cer"
@echo "Modify and run the commands yourself."
@echo
@false
efi-signed/%.efi: efi/%.efi
mkdir -p efi-signed
pesign --force -n pki -i $< -o $@ -c HackBGRT-signer -s
efi-signed/bootx64.efi: pki
efi-signed/bootia32.efi: pki
efi/bootx64.efi: CC_PREFIX = x86_64-w64-mingw32
efi/bootx64.efi: GNUEFI_ARCH = x86_64
efi/bootx64.efi: $(FILES_C)
@mkdir -p efi
$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@ $(LIBS) -s
bootia32.efi: CC_PREFIX = i686-w64-mingw32
bootia32.efi: GNUEFI_ARCH = ia32
bootia32.efi: $(FILES_C)
efi/bootia32.efi: CC_PREFIX = i686-w64-mingw32
efi/bootia32.efi: GNUEFI_ARCH = ia32
efi/bootia32.efi: $(FILES_C)
@mkdir -p efi
$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@ $(LIBS) -s
clean:
rm -rf setup.exe efi efi-signed

View File

@@ -4,52 +4,83 @@ HackBGRT is intended as a boot logo changer for UEFI-based Windows systems.
## Summary
When booting on a UEFI-based computer, Windows may show a vendor-defined logo which is stored on the UEFI firmware in a section called Boot Graphics Resource Table (BGRT). It's usually very difficult to change the image permamently, but a custom UEFI application may be used to overwrite it during the boot. HackBGRT does exactly that.
When booting on a UEFI-based computer, Windows may show a vendor-defined logo which is stored on the UEFI firmware in a section called Boot Graphics Resource Table (BGRT). It's usually very difficult to change the image permanently, but a custom UEFI application may be used to overwrite it during the boot. HackBGRT does exactly that.
## Usage
**Important:** If you mess up the installation, your system may become unbootable! Create a rescue disk before use. This software comes with no warranty. Use at your own risk.
* Make sure that your computer is booting with UEFI.
* Make sure that Secure Boot is disabled, or learn to sign EFI applications.
* Simple Windows installation:
* Get the latest release.
* Start `setup.exe` and select `I` (install).
* The installer will launch Notepad for modifying the configuration.
* If you need only one custom image, the defaults are fine.
* Otherwise, check out the examples in the configuration file.
* The installer will launch Paint for creating the image(s).
* You can create multiple images by using Save As.
* Be sure to always use the 24-bit BMP/DIB format.
* If Windows later restores the original boot loader, simply reinstall.
* If you wish to change the image or other configuration, simply reinstall.
* Installation for Windows with another boot loader (e.g. GRUB):
* Extract the latest release to `[EFI System Partition]\EFI\HackBGRT\`.
* Set `boot=\EFI\Microsoft\Boot\bootmgfw.efi` in `config.txt`.
* Point your boot loader to `\EFI\HackBGRT\bootx64.efi`.
* Installation for all operating systems:
* Extract the latest release to `[EFI System Partition]\EFI\HackBGRT\`.
* Set `boot=` to your preferred boot loader in `config.txt`.
* Set `\EFI\HackBGRT\bootx64.efi` as your default boot loader with `efibootmgr` or some other EFI boot manager tool.
* Make sure that you have read the Secure Boot instructions.
* Make sure that BitLocker is disabled, or find your recovery key.
### Secure Boot instructions
HackBGRT is not approved by Microsoft. Instead, HackBGRT comes with the *shim* boot loader, which allows to manually select HackBGRT as a trusted program. After installing HackBGRT and rebooting your computer, you have to **follow the instructions in [shim.md](shim.md)** to achieve this. These steps cannot be automated, that's the whole point of Secure Boot. Although HackBGRT is self-signed with a certificate, it's not advisable to enroll foreign certificates directly into your firmware.
The *shim* boot loader is maintained by Red Hat, Inc, and the included signed copy of *shim* is extracted from Debian GNU/Linux many thanks to the maintainers! For copyright information, see [shim-signed/COPYRIGHT](shim-signed/COPYRIGHT).
### Windows installation
* Get the latest release from the Releases page.
* Start `setup.exe` and follow the instructions.
* The installer will launch Paint for editing the image.
* If Windows later restores the original boot loader, just reinstall.
* If you wish to change the image or other configuration, just reinstall.
* For advanced settings, edit `config.txt` before installing. No extra support provided!
* After installing, read the instructions in [shim.md](shim.md) and reboot your computer.
### Quiet (batch) installation
* Edit the `config.txt` and `splash.bmp` (or any other images) to your needs.
* Run `setup.exe batch COMMANDS` as administrator, with some of the following commands:
* `install` copy the files but don't enable.
* `enable-entry` create a new EFI boot entry.
* `disable-entry` disable the EFI boot entry.
* `enable-bcdedit` use `bcdedit` to create a new EFI boot entry.
* `disable-bootmgr` use `bcdedit` to disable the EFI boot entry.
* `enable-overwrite` overwrite the MS boot loader.
* `disable-overwrite` restore the MS boot loader.
* `skip-shim` skip *shim* when installing.
* `allow-secure-boot` ignore Secure Boot in subsequent commands.
* `allow-bitlocker` ignore BitLocker in subsequent commands.
* `allow-bad-loader` ignore bad boot loader configuration in subsequent commands.
* `disable` run all relevant `disable-*` commands.
* `uninstall` disable and remove completely.
* `show-boot-log` show the debug log collected during boot (if `log=1` is set in `config.txt`).
* For example, run `setup.exe batch install allow-secure-boot enable-overwrite` to copy files and overwrite the MS boot loader regardless of Secure Boot status.
### Multi-boot configurations
If you only need HackBGRT for Windows:
* Run `setup.exe`, install files without enabling.
* Configure your boot loader to start `\EFI\HackBGRT\loader.efi`.
If you need it for other systems as well:
* Configure HackBGRT to start your boot loader (such as systemd-boot): `boot=\EFI\systemd\systemd-bootx64.efi`.
* Run `setup.exe`, install as a new EFI boot entry.
To install purely on Linux, you can install with `setup.exe dry-run` and then manually copy files from `dry-run/EFI` to your `[EFI System Partition]/EFI`. For further instructions, consult the documentation of your own Linux system.
## Configuration
The configuration options are described in `config.txt`, which should be stored in `[EFI System Partition]\EFI\HackBGRT\config.txt`.
The configuration options are described in `config.txt`, which the installer copies into `[EFI System Partition]\EFI\HackBGRT\config.txt`.
## Images
The image path can be changed in the configuration file. The default path is `[EFI System Partition]\EFI\HackBGRT\splash.bmp`.
The image must be a 24-bit BMP file with a 54-byte header. That's a TrueColor BMP3 in Imagemagick, or 24-bit BMP/DIB in Microsoft Paint.
The installer copies and converts files whose `path` starts with `\EFI\HackBGRT\`. For example, to use a file named `my.jpg`, copy it in the installer folder (same folder as `setup.exe`) and set the image path in `config.txt` to `path=\EFI\HackBGFT\my.jpg`.
Multiple images may be specified, in which case one is picked at random.
If you copy an image file to ESP manually, note that the image must be a 24-bit BMP file with a 54-byte header. That's a TrueColor BMP3 in Imagemagick, or 24-bit BMP/DIB in Microsoft Paint.
Advanced users may edit the `config.txt` to define multiple images, in which case one is picked at random.
## Recovery
If something breaks and you can't boot to Windows, you have the following options:
* Windows installation (or recovery) media can fix boot issues.
* You can copy `[EFI System Partition]\EFI\HackBGRT\bootmgfw-original.efi` into `[EFI System Partition]\EFI\Microsoft\Boot\bootmgfw.efi` by some other means such as Linux or Windows command prompt.
If something breaks and you can't boot to Windows, you need to use the Windows installation disk (or recovery disk) to fix boot issues.
## Building

41
config.txt Executable file → Normal file
View File

@@ -1,35 +1,48 @@
# vim: set fileencoding=utf-8
# The same options may be given also as command line parameters in the EFI Shell, which is useful for debugging.
# Boot loader path. Point to the real Windows boot loader.
boot=\EFI\HackBGRT\bootmgfw-original.efi
# Boot loader path. MS = either backup or original Windows boot loader.
boot=MS
# The image is specified with an image line.
# Multiple image lines may be present, in which case one will be picked by random.
# The image line may contain the following parts:
# Any of the following:
# - "n=[0-9]+", a weight for this image in the randomization process. Default: n=1.
# - "x={auto|native|[0-9]+}", the x coordinate. Default: x=auto.
# - "y={auto|native|[0-9]+}", the y coordinate. Default: y=auto.
# - "n=(number)", a weight for this image in the randomization process. Default: n=1.
# - "x=(number)" or "x=keep", the x offset from the center. Default: x=0.
# - "y=(number)" or "y=keep", the y offset from the center. Default: y=0.
# - "o=(0|90|180|270|keep)", the screen orientation, degrees anticlockwise. Default: o=keep.
# One of the following:
# - "keep" to keep the firmware logo. Sets also x=native,y=native by default.
# - "keep" to keep the firmware logo. Also keeps coordinates by default.
# - "remove" to remove the BGRT. Makes x and y meaningless.
# - "black" to use only a black image. Makes x and y meaningless.
# - "path=..." to read a BMP file. The file must be a 24-bit BMP file with a 54-byte header.
# - "path=..." to read a BMP file.
# * NOTE: For path=\EFI\HackBGRT\*, the installer will copy and convert the file if necessary.
# * NOTE: For other paths, make sure that the file is a 24-bit BMP file with a 54-byte header.
# * NOTE: The file must be on the EFI System Partition. Do not add a drive letter!
# Examples:
# - image=remove
# - image=black
# - image=x=auto,y=0,path=\EFI\HackBGRT\topimage.bmp
# - image=n=1,path=\EFI\HackBGRT\splash.bmp
# - image=n=50,path=\EFI\HackBGRT\probable.bmp
# - image= x=0 y=-200 path=\EFI\HackBGRT\topimage.bmp
# - image= n=1 o=90 path=\EFI\HackBGRT\sideways.bmp
# - image= n=50 y=999999 o=keep path=\EFI\HackBGRT\probable.bmp
# The above examples together would produce
# - 1/54 chance for the default OS logo
# - 1/54 chance for black screen
# - 1/54 chance for topimage.bmp, centered at the top of the screen
# - 1/54 chance for splash.bmp, automatically positioned
# - 50/54 chance for probable.bmp, automatically positioned
# - 1/54 chance for topimage.bmp, 200 px above the center of the screen
# - 1/54 chance for splash.bmp, centered, orientation set to 90 degrees
# - 50/54 chance for probable.bmp, at the bottom edge, explicitly default orientation
# Default: just one image.
image=path=\EFI\HackBGRT\splash.bmp
image= y=-200 path=\EFI\HackBGRT\splash.bmp
# Preferred resolution. Use 0x0 for maximum and -1x-1 for original.
resolution=0x0
# Logging (0 for disabled, 1 for enabled).
# When logging is enabled, setup.exe can show debug information about the current boot.
# The log might occupy a few kilobytes of RAM.
log=1
# Debug mode (0 for disabled, 1 for enabled).
# Shows debug information and prompts for keypress before booting.
debug=0

55
shim-signed/COPYRIGHT Normal file
View File

@@ -0,0 +1,55 @@
Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: shim
Upstream-Contact: Peter Jones <pjones@redhat.com>
Source: https://github.com/rhboot/shim
Files: *
Copyright: 2012 Red Hat, Inc
2009-2012 Intel Corporation
License: BSD-2-Clause
Files: debian/po/cs.po
Copyright: 2018 Michal Simunek <michal.simunek@gmail.com>
License: BSD-2-Clause
Files: debian/po/de.po
Copyright: 2017, 2018 Markus Hiereth <markus.hiereth@freenet.de>
License: BSD-2-Clause
Files: debian/po/fr.po
Copyright: 2017, 2018 Alban Vidal <alban.vidal@zordhak.fr>
License: BSD-2-Clause
Files: debian/po/nl.po
Copyright: 2017, 2018 Frans Spiesschaert <Frans.Spiesschaert@yucom.be>
License: BSD-2-Clause
Files: debian/po/pt.po
Copyright: 2017, 2018 Rui Branco <ruipb@debianpt.org>
License: BSD-2-Clause
License: BSD-2-Clause
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
.
Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the
distribution.
.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.

BIN
shim-signed/mmia32.efi Normal file

Binary file not shown.

BIN
shim-signed/mmx64.efi Normal file

Binary file not shown.

BIN
shim-signed/shimia32.efi Normal file

Binary file not shown.

BIN
shim-signed/shimx64.efi Normal file

Binary file not shown.

286
shim.md Normal file
View File

@@ -0,0 +1,286 @@
# Secure Boot and *shim*
Secure Boot accepts only trusted files during boot. The *shim* boot loader is a tool which allows you to select which files to trust. HackBGRT installs *shim* for you, but you need to configure it with these instructions.
On the first boot after installing HackBGRT, you will see an error message saying "Verification failed". You need to press a key to enter the setup tool (MOKManager) where you can choose to trust HackBGRT. After that, use arrow keys to navigate and *Enter* to continue as described below.
## 1. Verification failed
This is the start of *shim* configuration.
```
ERROR
Verification failed: (0x1A) Security Violation
+----+
| OK |
+----+
```
Select `OK`, *Enter*.
```
Shim UEFI key management
Press any key to perform MOK management
Booting in 5 seconds
```
Press a key quickly to enter *MOK management* or the *MOKManager* program.
## 2. MOK management
```
Perform MOK management
Continue to boot
Enroll key from disk
Enroll hash from disk
```
Select `Enroll hash from disk`, *Enter*. This is the safest option where you choose to trust only a specific version of HackBGRT.
You can also choose to `Enroll key from disk`, which means that you choose to trust anything signed with the same certificate. How do you know if it's safe? You don't that's why you should rather use the other option or build your own version of HackBGRT with your own certificate.
## 3a. Enroll hash from disk
```
Select Binary
The Selected Binary will have its hash Enrolled
This means it will subsequently Boot with no prompting
Remember to make sure it is a genuine binary before enrolling its hash
+----------------+
| YOUR DISK NAME |
+----------------+
```
Select the disk, *Enter*.
```
+---------------+
| EFI/ |
| loader/ |
| vmlinuz-linux |
+---------------+
```
Select `EFI/`, *Enter*.
```
+------------+
| ../ |
| Boot/ |
| HackBGRT/ |
| Microsoft/ |
+------------+
```
Select `HackBGRT/`, *Enter*.
```
+-----------------+
| ../ |
| grubx64.efi |
| loader.efi |
| mmx64.efi |
| certificate.cer |
| splash.bmp |
| config.txt |
+-----------------+
```
Select `grubx64.efi`, *Enter*.
```
[Enroll MOK]
+------------+
| View key 0 |
| Continue |
+------------+
```
To verify the key contents, select `View key 0`, *Enter*.
```
SHA256 hash
(some hexadecimal values)
```
Press *Enter* to continue.
```
[Enroll MOK]
+------------+
| View key 0 |
| Continue |
+------------+
```
Select `Continue`, *Enter*.
```
Enroll the key(s)?
+-----+
| No |
| Yes |
+-----+
```
Select `Yes`, *Enter*.
```
Perform MOK management
+-----------------------+
| Reboot |
| Enroll key from disk |
| Enroll hash from disk |
+-----------------------+
```
Select `Reboot`, *Enter*.
You are now ready to boot using HackBGRT.
## 3b. Enroll key from disk
```
Select Key
The selected key will be enrolled into the MOK database
This means any binaries signed with it will be run without prompting
Remember to make sure it is a genuine key before Enrolling it
+----------------+
| YOUR DISK NAME |
+----------------+
```
Select the disk, *Enter*.
```
+---------------+
| EFI/ |
| loader/ |
| vmlinuz-linux |
+---------------+
```
Select `EFI/`, *Enter*.
```
+------------+
| ../ |
| Boot/ |
| HackBGRT/ |
| Microsoft/ |
+------------+
```
Select `HackBGRT/`, *Enter*.
```
+-----------------+
| ../ |
| grubx64.efi |
| loader.efi |
| mmx64.efi |
| certificate.cer |
| splash.bmp |
| config.txt |
+-----------------+
```
Select `certificate.cer`, *Enter*.
```
[Enroll MOK]
+------------+
| View key 0 |
| Continue |
+------------+
```
To verify the key contents, select `View key 0`, *Enter*.
```
[Extended Key Usage]
OID: Code Signing
[Serial Number]
6B:24:52:E9:3B:84:41:73:B0:22:92:E8:BE:8E:38:85:
[Issuer]
CN=HackBGRT Secure Boot Signer, O=Metabolix
[Subject]
CN=HackBGRT Secure Boot Signer, O=Metabolix
[Valid Not Before]
Nov 9 13:43:56 2023 GMT
[Valid Not After]
Jan 19 03:14:07 2037 GMT
[Fingerprint]
79 8E 64 40 D1 D1 F4 53 30 8D
A0 83 A4 77 FE 57 45 30 36 60
```
Press *Enter* to continue.
```
[Enroll MOK]
+------------+
| View key 0 |
| Continue |
+------------+
```
Select `Continue`, *Enter*.
```
Enroll the key(s)?
+-----+
| No |
| Yes |
+-----+
```
Select `Yes`, *Enter*.
```
Perform MOK management
+-----------------------+
| Reboot |
| Enroll key from disk |
| Enroll hash from disk |
+-----------------------+
```
Select `Reboot`, *Enter*.
You are now ready to boot using HackBGRT.
## Tutorial: *shim* for dummies
To install *shim* manually, follow these steps (assuming x64 architecture):
1. Get *shim*, preferably *shim-signed*.
2. Rename your boot loader to `grubx64.efi`.
3. Copy `shimx64.efi` where your loader used to be.
4. Copy `mmx64.efi` to the same folder.
The *shim* boot process is as follows:
1. Your computer starts `your-loader-name.efi`, which is now really *shim*.
2. Next, *shim* tries to load `grubx64.efi`.
3. If `grubx64.efi` is trusted, the boot process continues.
4. Otherwise, *shim* offers to launch *MOKManager* `mmx64.efi`, and you can try again after that.

518
src/Efi.cs Normal file
View File

@@ -0,0 +1,518 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.Win32;
/**
* Methods for handling EFI variables.
*/
public class Efi {
[DllImport("kernel32.dll", SetLastError = true)]
private static extern UInt32 GetFirmwareEnvironmentVariableEx(string lpName, string lpGuid, [MarshalAs(UnmanagedType.LPArray)] byte[] pBuffer, UInt32 nSize, out UInt32 pdwAttributes);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern UInt32 SetFirmwareEnvironmentVariableEx(string lpName, string lpGuid, [MarshalAs(UnmanagedType.LPArray)] byte[] pBuffer, UInt32 nSize, UInt32 pdwAttributes);
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
private static extern bool AdjustTokenPrivileges(IntPtr htoken, bool disable, ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
[DllImport("kernel32.dll", ExactSpelling = true)]
private static extern IntPtr GetCurrentProcess();
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
private static extern bool OpenProcessToken(IntPtr h, int acc, out IntPtr phtok);
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern UInt32 GetSystemFirmwareTable(UInt32 provider, UInt32 table, [MarshalAs(UnmanagedType.LPArray)] byte[] buffer, UInt32 len);
[StructLayout(LayoutKind.Sequential, Pack = 1)]
private struct TokPriv1Luid {
public int Count;
public long Luid;
public int Attr;
}
/**
* Information about an EFI variable.
*/
public class Variable {
public string Name, Guid;
public UInt32 Attributes;
public byte[] Data;
/**
* Convert to string.
*
* @return String representation of this object.
*/
public override string ToString() {
if (Data == null) {
return $"{Name} Guid={Guid} Attributes={Attributes} Data=null";
}
var hex = BitConverter.ToString(Data).Replace("-", " ");
var text = new string(Data.Select(c => 0x20 <= c && c <= 0x7f ? (char) c : ' ').ToArray());
return $"{Name} Guid={Guid} Attributes={Attributes} Text='{text}' Bytes='{hex}'";
}
}
/**
* Information about an EFI boot entry.
*/
public class BootEntryData {
public UInt32 Attributes;
public string Label;
public class DevicePathNode {
public byte Type, SubType;
public byte[] Data;
public DevicePathNode(byte[] data) {
Type = data[0];
SubType = data[1];
Data = data.Skip(4).ToArray();
}
public byte[] ToBytes() {
var len = Data.Length + 4;
return new byte[] { Type, SubType, (byte)(len & 0xff), (byte)(len >> 8) }.Concat(Data).ToArray();
}
}
public List<DevicePathNode> DevicePathNodes;
public byte[] Arguments;
public BootEntryData(byte[] data) {
Attributes = BitConverter.ToUInt32(data, 0);
var pathNodesLength = BitConverter.ToUInt16(data, 4);
Label = new string(BytesToUInt16s(data).Skip(3).TakeWhile(i => i != 0).Select(i => (char)i).ToArray());
var pos = 6 + 2 * (Label.Length + 1);
var pathNodesEnd = pos + pathNodesLength;
DevicePathNodes = new List<DevicePathNode>();
while (pos + 4 <= pathNodesEnd) {
var len = BitConverter.ToUInt16(data, pos + 2);
if (len < 4 || pos + len > pathNodesEnd) {
return; // throw new Exception("Bad entry.");
}
DevicePathNodes.Add(new DevicePathNode(data.Skip(pos).Take(len).ToArray()));
pos += len;
}
Arguments = data.Skip(pathNodesEnd).ToArray();
}
public byte[] ToBytes() {
return new byte[0]
.Concat(BitConverter.GetBytes((UInt32) Attributes))
.Concat(BitConverter.GetBytes((UInt16) DevicePathNodes.Sum(n => n.Data.Length + 4)))
.Concat(Encoding.Unicode.GetBytes(Label + "\0"))
.Concat(DevicePathNodes.SelectMany(n => n.ToBytes()))
.Concat(Arguments)
.ToArray();
}
public DevicePathNode FileNameNode {
get {
var d = DevicePathNodes;
return d.Count > 1 && d[d.Count - 1].Type == 0x7F && d[d.Count - 2].Type == 0x04 ? d[d.Count - 2] : null;
}
}
public bool HasFileName {
get {
return FileNameNode != null;
}
}
public string FileName {
get {
if (!HasFileName) {
return "";
}
return new string(Encoding.Unicode.GetChars(FileNameNode.Data).TakeWhile(c => c != '\0').ToArray());
}
set {
if (!HasFileName) {
throw new Exception("Logic error: Setting FileName on a bad boot entry.");
}
FileNameNode.Data = Encoding.Unicode.GetBytes(value + "\0");
}
}
}
/**
* GUID of the global EFI variables.
*/
public const string EFI_GLOBAL_GUID = "{8be4df61-93ca-11d2-aa0d-00e098032b8c}";
/**
* GUID for HackBGRT EFI variables.
*/
public const string EFI_HACKBGRT_GUID = "{03c64761-075f-4dba-abfb-2ed89e18b236}";
/**
* Directory containing EFI variables in Linux.
*/
public const string LinuxEfiDir = "/sys/firmware/efi/efivars";
/**
* Enable the privilege to access EFI variables.
*/
public static void EnablePrivilege() {
if (Directory.Exists(LinuxEfiDir)) {
var linuxEfiFile = $"{LinuxEfiDir}/BootOrder-8be4df61-93ca-11d2-aa0d-00e098032b8c";
if (File.Exists(linuxEfiFile)) {
using (FileStream fs = File.OpenWrite(linuxEfiFile)) {
// OpenWrite throws an exception on error.
}
}
return;
}
const int SE_PRIVILEGE_ENABLED = 0x00000002;
const int TOKEN_QUERY = 0x00000008;
const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
const string SE_SYSTEM_ENVIRONMENT_NAME = "SeSystemEnvironmentPrivilege";
IntPtr htoken = IntPtr.Zero;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, out htoken)) {
throw new Exception("EnablePrivilege: OpenProcessToken failed: " + Marshal.GetLastWin32Error());
}
try {
TokPriv1Luid tp;
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
if (!LookupPrivilegeValue(null, SE_SYSTEM_ENVIRONMENT_NAME, ref tp.Luid)) {
throw new Exception("EnablePrivilege: LookupPrivilegeValue failed: " + Marshal.GetLastWin32Error());
}
if (!AdjustTokenPrivileges(htoken, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero)) {
throw new Exception("EnablePrivilege: AdjustTokenPrivileges failed: " + Marshal.GetLastWin32Error());
}
} finally {
CloseHandle(htoken);
}
}
/**
* Get an EFI variable.
*
* @param name Name of the EFI variable.
* @param guid GUID of the EFI variable.
* @return Information about the EFI variable.
*/
private static Variable GetVariable(string name, string guid = EFI_GLOBAL_GUID) {
Variable result = new Variable();
result.Name = name;
result.Guid = guid;
result.Data = null;
result.Attributes = 0;
if (Directory.Exists(LinuxEfiDir)) {
var linuxEfiFile = $"{LinuxEfiDir}/{name}-{guid.Substring(1, guid.Length - 2)}";
if (File.Exists(linuxEfiFile)) {
var d = File.ReadAllBytes(linuxEfiFile);
result.Attributes = (UInt32)(d[0] + 0x100 * d[1] + 0x10000 * d[2] + 0x1000000 * d[3]);
result.Data = d.Skip(4).ToArray();
}
return result;
}
for (UInt32 i = 4096; i <= 1024*1024; i *= 2) {
byte[] buf = new byte[i];
UInt32 len = GetFirmwareEnvironmentVariableEx(name, guid, buf, (UInt32) buf.Length, out result.Attributes);
if (len == buf.Length) {
continue;
}
if (len > 0 || Marshal.GetLastWin32Error() == 0) {
result.Data = new byte[len];
Array.Copy(buf, 0, result.Data, 0, len);
return result;
}
switch (len != 0 ? 0 : Marshal.GetLastWin32Error()) {
case 203:
// Not found.
return result;
case 87:
throw new Exception("GetVariable: Invalid parameter");
case 1314:
throw new Exception("GetVariable: Privilege not held");
default:
throw new Exception("GetVariable: error " + Marshal.GetLastWin32Error());
}
}
throw new Exception("GetFirmwareEnvironmentVariable: too big data");
}
/**
* Set an EFI variable.
*
* @param v Information of the variable.
* @param dryRun Don't actually set the variable.
*/
private static void SetVariable(Variable v, bool dryRun = false) {
Setup.WriteLine($"Writing EFI variable {v.Name} (see log for details)");
Setup.Log($"Writing EFI variable: {v}");
if (dryRun) {
return;
}
if (Directory.Exists(LinuxEfiDir)) {
var linuxEfiFile = $"{LinuxEfiDir}/{v.Name}-{v.Guid.Substring(1, v.Guid.Length - 2)}";
var a = v.Attributes;
var b = new byte[] { (byte) a, (byte) (a >> 8), (byte) (a >> 16), (byte) (a >> 24) };
// FIXME: Just writing won't work: File.WriteAllBytes(linuxEfiFile, b.Concat(v.Data).ToArray());
Setup.WriteLine("FIXME: Can't yet write EFI variables in Linux.");
return;
}
UInt32 r = SetFirmwareEnvironmentVariableEx(v.Name, v.Guid, v.Data, (UInt32) v.Data.Length, v.Attributes);
if (r == 0) {
switch (Marshal.GetLastWin32Error()) {
case 87:
throw new Exception("SetVariable: Invalid parameter");
case 1314:
throw new Exception("SetVariable: Privilege not held");
default:
throw new Exception("SetVariable: error " + Marshal.GetLastWin32Error());
}
}
}
/**
* Check if Secure Boot is enabled.
*
* @return 0 for disabled, 1 for enabled, other for unknown.
*/
public static int GetSecureBootStatus() {
// GetVariable("SecureBoot") reports always 1 (on Lenovo E335).
// Windows registry seems to work better, though.
try {
return (int) Registry.GetValue(
"HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\SecureBoot\\State",
"UEFISecureBootEnabled",
-1
);
} catch {
return -1;
}
}
/**
* Check if it's possible to reboot to EFI setup.
*
* @return True, if it's possible to reboot to EFI setup.
*/
public static bool CanBootToFW() {
try {
Variable tmp = GetVariable("OsIndicationsSupported");
return tmp.Data != null && (tmp.Data[0] & 1) != 0;
} catch {
return false;
}
}
/**
* Mark that the next reboot should go to EFI setup.
*/
public static void SetBootToFW() {
Variable tmp = GetVariable("OsIndications");
if (tmp.Data == null) {
tmp.Data = new byte[8];
tmp.Attributes = 7;
}
tmp.Data[0] |= 1;
SetVariable(tmp);
}
/**
* Convert bytes into UInt16 values.
*
* @param bytes The byte array.
* @return An enumeration of UInt16 values.
*/
public static IEnumerable<UInt16> BytesToUInt16s(byte[] bytes) {
// TODO: return bytes.Chunk(2).Select(b => (UInt16) (b[0] + 0x100 * b[1])).ToArray();
return Enumerable.Range(0, bytes.Length / 2).Select(i => (UInt16) (bytes[2 * i] + 0x100 * bytes[2 * i + 1]));
}
/**
* Disable the said boot entry from BootOrder.
*
* @param label Label of the boot entry.
* @param fileName File name of the boot entry.
* @param dryRun Don't actually disable the entry.
* @return True, if the entry was found in BootOrder.
*/
public static bool DisableBootEntry(string label, string fileName, bool dryRun = false) {
Variable bootOrder;
try {
bootOrder = GetVariable("BootOrder");
} catch {
return false;
}
if (bootOrder.Data == null) {
return false;
}
Setup.Log($"Read EFI variable: {bootOrder}");
var found = false;
var bootOrderInts = new List<UInt16>();
foreach (var num in BytesToUInt16s(bootOrder.Data)) {
var entry = GetVariable(String.Format("Boot{0:X04}", num));
if (entry.Data != null) {
var entryData = new BootEntryData(entry.Data);
if (entryData.Label == label && entryData.FileName == fileName) {
found = true;
continue;
}
}
bootOrderInts.Add(num);
}
if (found) {
bootOrder.Data = bootOrderInts.SelectMany(num => new byte[] { (byte)(num & 0xff), (byte)(num >> 8) }).ToArray();
SetVariable(bootOrder, dryRun);
}
return found;
}
/**
* Create and enable the said boot entry from BootOrder.
*
* @param label Label of the boot entry.
* @param fileName File name of the boot entry.
* @param dryRun Don't actually create the entry.
*/
public static void MakeAndEnableBootEntry(string label, string fileName, bool dryRun = false) {
Variable msEntry = null, ownEntry = null;
UInt16 msNum = 0, ownNum = 0;
// Find a free entry and the MS bootloader entry.
Variable bootOrder = null;
try {
bootOrder = GetVariable("BootOrder");
} catch {
if (dryRun) {
return;
}
}
if (bootOrder == null || bootOrder.Data == null) {
throw new Exception("MakeBootEntry: Could not read BootOrder. Maybe your computer is defective.");
}
var bootCurrent = GetVariable("BootCurrent");
if (bootCurrent.Data == null) {
throw new Exception("MakeBootEntry: Could not read BootCurrent. Maybe your computer is defective.");
}
Setup.Log($"Read EFI variable: {bootOrder}");
Setup.Log($"Read EFI variable: {bootCurrent}");
var bootOrderInts = new List<UInt16>(BytesToUInt16s(bootOrder.Data));
foreach (var num in BytesToUInt16s(bootCurrent.Data).Concat(bootOrderInts).Concat(Enumerable.Range(0, 0xffff).Select(i => (UInt16) i))) {
var entry = GetVariable(String.Format("Boot{0:X04}", num));
if (entry.Data == null) {
if (ownEntry == null) {
ownNum = num;
ownEntry = entry;
}
} else {
var entryData = new BootEntryData(entry.Data);
if (!entryData.HasFileName) {
continue;
}
if (entryData.Label == label && entryData.FileName == fileName) {
ownNum = num;
ownEntry = entry;
}
if (msEntry == null && entryData.FileName.StartsWith("\\EFI\\Microsoft\\Boot\\bootmgfw.efi", StringComparison.OrdinalIgnoreCase)) {
msNum = num;
msEntry = entry;
}
}
if (ownEntry != null && msEntry != null) {
break;
}
}
if (ownEntry == null) {
throw new Exception("MakeBootEntry: Boot entry list is full.");
} else if (msEntry == null) {
throw new Exception("MakeBootEntry: Windows Boot Manager not found.");
} else {
Setup.Log($"Read EFI variable: {msEntry}");
// Make a new boot entry using the MS entry as a starting point.
var entryData = new BootEntryData(msEntry.Data);
entryData.Arguments = Encoding.UTF8.GetBytes(label + "\0");
entryData.Attributes = 1; // LOAD_OPTION_ACTIVE
entryData.Label = label;
entryData.FileName = fileName;
ownEntry.Attributes = 7; // EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS
ownEntry.Data = entryData.ToBytes();
SetVariable(ownEntry, dryRun);
}
var msPos = bootOrderInts.IndexOf(msNum);
var ownPos = bootOrderInts.IndexOf(ownNum);
var mustAdd = ownPos == -1;
var mustMove = 0 <= msPos && msPos <= ownPos;
if (mustAdd || mustMove) {
if (mustMove) {
bootOrderInts.RemoveAt(ownPos);
}
bootOrderInts.Insert(msPos < 0 ? 0 : msPos, ownNum);
bootOrder.Data = bootOrderInts.SelectMany(num => new byte[] { (byte)(num & 0xff), (byte)(num >> 8) }).ToArray();
SetVariable(bootOrder, dryRun);
}
}
/**
* Log the boot entries.
*/
public static void LogBootEntries() {
try {
var bootOrder = GetVariable("BootOrder");
var bootCurrent = GetVariable("BootCurrent");
Setup.Log($"LogBootOrder: {bootOrder}");
Setup.Log($"LogBootOrder: {bootCurrent}");
var bootOrderInts = new List<UInt16>(BytesToUInt16s(bootOrder.Data));
// Windows can't enumerate EFI variables, and trying them all is too slow.
// BootOrder + BootCurrent + the first 0xff entries should be enough.
foreach (var num in BytesToUInt16s(bootCurrent.Data).Concat(bootOrderInts).Concat(Enumerable.Range(0, 0xff).Select(i => (UInt16) i))) {
var entry = GetVariable(String.Format("Boot{0:X04}", num));
if (entry.Data != null) {
Setup.Log($"LogBootOrder: {entry}");
}
}
} catch (Exception e) {
Setup.Log($"LogBootOrder failed: {e.ToString()}");
}
}
/**
* Retrieve HackBGRT log collected during boot.
*/
public static string GetHackBGRTLog() {
try {
var log = GetVariable("HackBGRTLog", EFI_HACKBGRT_GUID);
return new string(BytesToUInt16s(log.Data).Select(i => (char)i).ToArray());
} catch (Exception e) {
return $"Log not found: {e.ToString()}";
}
}
/**
* Log the BGRT table (for debugging).
*/
public static void LogBGRT() {
try {
const UInt32 acpiBE = 0x41435049, bgrtLE = 0x54524742;
UInt32 size = GetSystemFirmwareTable(acpiBE, bgrtLE, null, 0);
byte[] buf = new byte[size];
var ret = GetSystemFirmwareTable(acpiBE, bgrtLE, buf, size);
if (ret == size) {
var hex = BitConverter.ToString(buf).Replace("-", " ");
Setup.Log($"LogBGRT: {size} bytes: {hex}");
} else if (ret == 0) {
Setup.Log($"LogBGRT: Win32Error {Marshal.GetLastWin32Error()}");
} else {
Setup.Log($"LogBGRT: Size problems: spec {0x38}, buf {size}, ret {ret}");
}
} catch (Exception e) {
Setup.Log($"LogBGRT failed: {e}");
}
}
}

129
src/Esp.cs Normal file
View File

@@ -0,0 +1,129 @@
using System;
using System.IO;
using System.Text.RegularExpressions;
/**
* EFI System Partition mounter.
*/
public sealed class Esp {
/** The singleton instance of this class, if ESP is mounted. */
private static Esp MountInstance;
/** EFI System Partition location. */
public static string Location { get; private set; }
/** MS boot loader path on ESP. */
public static string MsLoaderPath {
get {
return Path.Combine(new string[] { Location, "EFI", "Microsoft", "Boot", "bootmgfw.efi"});
}
}
/**
* Constructor: do nothing.
*/
private Esp() {
}
/**
* Destructor: unmount.
*/
~Esp() {
if (this == MountInstance) {
Unmount();
}
}
/**
* Try to find ESP at a path.
*
* @param tryPath The new path to try.
* @param requireMsLoader Look for MS boot loader specifically?
* @return true if the path was given and seems valid, false otherwise.
*/
public static bool TryPath(string tryPath, bool requireMsLoader = true) {
if (MountInstance != null && Location != null) {
Unmount();
}
Location = tryPath;
if (Location != null && Location != "") {
if (File.Exists(MsLoaderPath)) {
Setup.Log($"Esp.TryPath: {Location} has MS boot loader");
return true;
}
if (Directory.Exists(Path.Combine(Location, "EFI"))) {
Setup.Log($"Esp.TryPath: {Location} has EFI directory but no loader");
if (!requireMsLoader) {
return true;
}
}
}
Location = null;
return false;
}
/**
* Find the EFI System Partition, if it's already mounted.
*
* @return true if the drive was found.
*/
public static bool Find() {
if (MountInstance != null) {
return true;
}
Setup.Log("Esp.Find()");
try {
// Match "The EFI System Partition is mounted at E:\" with some language support.
var re = new Regex(" EFI[^\n]*(?:\n[ \t]*)?([A-Z]:\\\\)");
var m = re.Match(Setup.Execute("mountvol", "", false));
if (m.Success && TryPath(m.Groups[1].Captures[0].Value)) {
return true;
}
Setup.Log("Esp.Find: no match");
} catch (Exception e) {
Setup.Log($"Esp.Find: {e.ToString()}");
}
for (char c = 'A'; c <= 'Z'; ++c) {
if (TryPath(c + ":\\")) {
Setup.Log($"Esp.Find: found {c}");
return true;
}
}
Setup.Log("Esp.Find: not found");
return false;
}
/**
* Mount the EFI System Partition.
*
* @return true if the drive was mounted, false otherwise.
*/
public static bool Mount() {
if (MountInstance != null) {
return true;
}
for (char c = 'A'; c <= 'Z'; ++c) {
Setup.Log($"Esp.Mount: {c}");
if (Setup.Execute("mountvol", c + ": /S", true) != null) {
MountInstance = new Esp();
if (TryPath(c + ":\\", false)) {
return true;
} else {
throw new Exception("Mounted ESP at " + c + ":\\ but it seems to be invalid!");
}
}
}
return false;
}
/**
* Unmount the EFI System Partition, if necessary.
*/
private static void Unmount() {
if (MountInstance != null) {
Setup.Execute("mountvol", Location + " /D", true);
Location = null;
MountInstance = null;
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -8,7 +8,7 @@ BOOLEAN ReadConfigFile(struct HackBGRT_config* config, EFI_FILE_HANDLE root_dir,
UINTN data_bytes = 0;
data = LoadFileWithPadding(root_dir, path, &data_bytes, 4);
if (!data) {
Print(L"HackBGRT: Failed to load configuration (%s)!\n", path);
Log(1, L"HackBGRT: Failed to load configuration (%s)!\n", path);
return FALSE;
}
CHAR16* str;
@@ -70,35 +70,35 @@ BOOLEAN ReadConfigFile(struct HackBGRT_config* config, EFI_FILE_HANDLE root_dir,
return TRUE;
}
static void SetBMPWithRandom(struct HackBGRT_config* config, int weight, enum HackBGRT_action action, int x, int y, const CHAR16* path) {
static void SetBMPWithRandom(struct HackBGRT_config* config, int weight, enum HackBGRT_action action, int x, int y, int o, const CHAR16* path) {
config->image_weight_sum += weight;
UINT32 random = Random();
UINT32 limit = 0xfffffffful / config->image_weight_sum * weight;
if (config->debug) {
Print(L"HackBGRT: weight %d, action %d, x %d, y %d, path %s, random = %08x, limit = %08x\n", weight, action, x, y, path, random, limit);
}
Log(config->debug, L"HackBGRT: n=%d, action=%d, x=%d, y=%d, o=%d, path=%s, random = %08x, limit = %08x\n", weight, action, x, y, o, path, random, limit);
if (!config->image_weight_sum || random <= limit) {
config->action = action;
config->image_path = path;
config->orientation = o;
config->image_x = x;
config->image_y = y;
}
}
static int ParseCoordinate(const CHAR16* str, enum HackBGRT_action action) {
if (str && L'0' <= str[0] && str[0] <= L'9') {
return Atoi(str);
if (str && ((L'0' <= str[0] && str[0] <= L'9') || str[0] == L'-')) {
return str[0] == L'-' ? -(int)Atoi(str+1) : (int)Atoi(str);
}
if ((str && StrnCmp(str, L"native", 6) == 0) || action == HackBGRT_KEEP) {
return HackBGRT_coord_native;
if ((str && StrnCmp(str, L"keep", 4) == 0) || action == HackBGRT_KEEP) {
return HackBGRT_coord_keep;
}
return HackBGRT_coord_auto;
return 0;
}
static void ReadConfigImage(struct HackBGRT_config* config, const CHAR16* line) {
const CHAR16* n = StrStrAfter(line, L"n=");
const CHAR16* x = StrStrAfter(line, L"x=");
const CHAR16* y = StrStrAfter(line, L"y=");
const CHAR16* o = StrStrAfter(line, L"o=");
const CHAR16* f = StrStrAfter(line, L"path=");
enum HackBGRT_action action = HackBGRT_KEEP;
if (f) {
@@ -110,11 +110,14 @@ static void ReadConfigImage(struct HackBGRT_config* config, const CHAR16* line)
} else if (StrStr(line, L"keep")) {
action = HackBGRT_KEEP;
} else {
Print(L"HackBGRT: Invalid image line: %s\n", line);
Log(1, L"HackBGRT: Invalid image line: %s\n", line);
return;
}
int weight = n && (!f || n < f) ? Atoi(n) : 1;
SetBMPWithRandom(config, weight, action, ParseCoordinate(x, action), ParseCoordinate(y, action), f);
int x_val = ParseCoordinate(x, action);
int y_val = ParseCoordinate(y, action);
int o_val = o ? ParseCoordinate(o, action) : HackBGRT_coord_keep;
SetBMPWithRandom(config, weight, action, x_val, y_val, o_val, f);
}
static void ReadConfigResolution(struct HackBGRT_config* config, const CHAR16* line) {
@@ -124,7 +127,7 @@ static void ReadConfigResolution(struct HackBGRT_config* config, const CHAR16* l
config->resolution_x = *x == '-' ? -(int)Atoi(x+1) : (int)Atoi(x);
config->resolution_y = *y == '-' ? -(int)Atoi(y+1) : (int)Atoi(y);
} else {
Print(L"HackBGRT: Invalid resolution line: %s\n", line);
Log(1, L"HackBGRT: Invalid resolution line: %s\n", line);
}
}
@@ -138,6 +141,10 @@ void ReadConfigLine(struct HackBGRT_config* config, EFI_FILE_HANDLE root_dir, co
config->debug = (StrCmp(line, L"debug=1") == 0);
return;
}
if (StrnCmp(line, L"log=", 4) == 0) {
config->log = (StrCmp(line, L"log=1") == 0);
return;
}
if (StrnCmp(line, L"image=", 6) == 0) {
ReadConfigImage(config, line + 6);
return;
@@ -154,5 +161,5 @@ void ReadConfigLine(struct HackBGRT_config* config, EFI_FILE_HANDLE root_dir, co
ReadConfigResolution(config, line + 11);
return;
}
Print(L"Unknown configuration directive: %s\n", line);
Log(1, L"Unknown configuration directive: %s\n", line);
}

View File

@@ -14,22 +14,24 @@ enum HackBGRT_action {
* @see struct HackBGRT_config
*/
enum HackBGRT_coordinate {
HackBGRT_coord_auto = 0x10000001,
HackBGRT_coord_native = 0x10000002
HackBGRT_coord_keep = -1000000001
};
/**
* The configuration.
*/
struct HackBGRT_config {
int debug;
int debug, log;
enum HackBGRT_action action;
const CHAR16* image_path;
int image_x;
int image_y;
int image_weight_sum;
int orientation;
int resolution_x;
int resolution_y;
int old_resolution_x;
int old_resolution_y;
const CHAR16* boot_path;
};

View File

@@ -6,20 +6,20 @@
#include "util.h"
/**
* The Print function signature.
* The version.
*/
typedef UINTN print_t(IN CHAR16 *fmt, ...);
/**
* The function for debug printing; either Print or NullPrint.
*/
print_t* Debug = NullPrint;
#ifdef GIT_DESCRIBE_W
const CHAR16 version[] = GIT_DESCRIBE_W;
#else
const CHAR16 version[] = L"unknown; not an official release?";
#endif
/**
* The configuration.
*/
static struct HackBGRT_config config = {
.action = HackBGRT_KEEP
.log = 1,
.action = HackBGRT_KEEP,
};
/**
@@ -43,16 +43,18 @@ static EFI_GRAPHICS_OUTPUT_PROTOCOL* GOP(void) {
static void SetResolution(int w, int h) {
EFI_GRAPHICS_OUTPUT_PROTOCOL* gop = GOP();
if (!gop) {
Debug(L"GOP not found!\n");
config.old_resolution_x = config.resolution_x = 0;
config.old_resolution_y = config.resolution_y = 0;
Log(config.debug, L"GOP not found!\n");
return;
}
UINTN best_i = gop->Mode->Mode;
int best_w = gop->Mode->Info->HorizontalResolution;
int best_h = gop->Mode->Info->VerticalResolution;
w = (w <= 0 ? w < 0 ? best_w : 0x7fffffff : w);
h = (h <= 0 ? h < 0 ? best_h : 0x7fffffff : h);
int best_w = config.old_resolution_x = gop->Mode->Info->HorizontalResolution;
int best_h = config.old_resolution_y = gop->Mode->Info->VerticalResolution;
w = (w <= 0 ? w < 0 ? best_w : 999999 : w);
h = (h <= 0 ? h < 0 ? best_h : 999999 : h);
Debug(L"Looking for resolution %dx%d...\n", w, h);
Log(config.debug, L"Looking for resolution %dx%d...\n", w, h);
for (UINT32 i = gop->Mode->MaxMode; i--;) {
int new_w = 0, new_h = 0;
@@ -85,30 +87,14 @@ static void SetResolution(int w, int h) {
best_h = new_h;
best_i = i;
}
Debug(L"Found resolution %dx%d.\n", best_w, best_h);
Log(config.debug, L"Found resolution %dx%d.\n", best_w, best_h);
config.resolution_x = best_w;
config.resolution_y = best_h;
if (best_i != gop->Mode->Mode) {
gop->SetMode(gop, best_i);
}
}
/**
* Select the correct coordinate (manual, automatic, native)
*
* @param value The configured coordinate value; has special values for automatic and native.
* @param automatic The automatically calculated alternative.
* @param native The original coordinate.
* @see enum HackBGRT_coordinate
*/
static int SelectCoordinate(int value, int automatic, int native) {
if (value == HackBGRT_coord_auto) {
return automatic;
}
if (value == HackBGRT_coord_native) {
return native;
}
return value;
}
/**
* Create a new XSDT with the given number of entries.
*
@@ -121,7 +107,7 @@ ACPI_SDT_HEADER* CreateXsdt(ACPI_SDT_HEADER* xsdt0, UINTN entries) {
UINT32 xsdt_len = sizeof(ACPI_SDT_HEADER) + entries * sizeof(UINT64);
BS->AllocatePool(EfiACPIReclaimMemory, xsdt_len, (void**)&xsdt);
if (!xsdt) {
Print(L"HackBGRT: Failed to allocate memory for XSDT.\n");
Log(1, L"HackBGRT: Failed to allocate memory for XSDT.\n");
return 0;
}
ZeroMem(xsdt, xsdt_len);
@@ -153,17 +139,17 @@ static ACPI_BGRT* HandleAcpiTables(enum HackBGRT_action action, ACPI_BGRT* bgrt)
if (CompareMem(rsdp->signature, "RSD PTR ", 8) != 0 || rsdp->revision < 2 || !VerifyAcpiRsdp2Checksums(rsdp)) {
continue;
}
Debug(L"RSDP: revision = %d, OEM ID = %s\n", rsdp->revision, TmpStr(rsdp->oem_id, 6));
Log(config.debug, L"RSDP @%x: revision = %d, OEM ID = %s\n", (UINTN)rsdp, rsdp->revision, TmpStr(rsdp->oem_id, 6));
ACPI_SDT_HEADER* xsdt = (ACPI_SDT_HEADER *) (UINTN) rsdp->xsdt_address;
if (!xsdt || CompareMem(xsdt->signature, "XSDT", 4) != 0 || !VerifyAcpiSdtChecksum(xsdt)) {
Debug(L"* XSDT: missing or invalid\n");
Log(config.debug, L"* XSDT: missing or invalid\n");
continue;
}
UINT64* entry_arr = (UINT64*)&xsdt[1];
UINT32 entry_arr_length = (xsdt->length - sizeof(*xsdt)) / sizeof(UINT64);
Debug(L"* XSDT: OEM ID = %s, entry count = %d\n", TmpStr(xsdt->oem_id, 6), entry_arr_length);
Log(config.debug, L"* XSDT @%x: OEM ID = %s, entry count = %d\n", (UINTN)xsdt, TmpStr(xsdt->oem_id, 6), entry_arr_length);
int bgrt_count = 0;
for (int j = 0; j < entry_arr_length; j++) {
@@ -171,16 +157,16 @@ static ACPI_BGRT* HandleAcpiTables(enum HackBGRT_action action, ACPI_BGRT* bgrt)
if (CompareMem(entry->signature, "BGRT", 4) != 0) {
continue;
}
Debug(L" - ACPI table: %s, revision = %d, OEM ID = %s\n", TmpStr(entry->signature, 4), entry->revision, TmpStr(entry->oem_id, 6));
Log(config.debug, L" - ACPI table @%x: %s, revision = %d, OEM ID = %s\n", (UINTN)entry, TmpStr(entry->signature, 4), entry->revision, TmpStr(entry->oem_id, 6));
switch (action) {
case HackBGRT_KEEP:
if (!bgrt) {
Debug(L" -> Returning this one for later use.\n");
Log(config.debug, L" -> Returning this one for later use.\n");
bgrt = (ACPI_BGRT*) entry;
}
break;
case HackBGRT_REMOVE:
Debug(L" -> Deleting.\n");
Log(config.debug, L" -> Deleting.\n");
for (int k = j+1; k < entry_arr_length; ++k) {
entry_arr[k-1] = entry_arr[k];
}
@@ -190,13 +176,13 @@ static ACPI_BGRT* HandleAcpiTables(enum HackBGRT_action action, ACPI_BGRT* bgrt)
--j;
break;
case HackBGRT_REPLACE:
Debug(L" -> Replacing.\n");
Log(config.debug, L" -> Replacing.\n");
entry_arr[j] = (UINTN) bgrt;
}
bgrt_count += 1;
}
if (!bgrt_count && action == HackBGRT_REPLACE && bgrt) {
Debug(L" - Adding missing BGRT.\n");
Log(config.debug, L" - Adding missing BGRT.\n");
xsdt = CreateXsdt(xsdt, entry_arr_length + 1);
entry_arr = (UINT64*)&xsdt[1];
entry_arr[entry_arr_length++] = (UINTN) bgrt;
@@ -208,6 +194,44 @@ static ACPI_BGRT* HandleAcpiTables(enum HackBGRT_action action, ACPI_BGRT* bgrt)
return bgrt;
}
/**
* Generate a BMP with the given size and color.
*
* @param w The width.
* @param h The height.
* @param r The red component.
* @param g The green component.
* @param b The blue component.
* @return The generated BMP, or 0 on failure.
*/
static BMP* MakeBMP(int w, int h, UINT8 r, UINT8 g, UINT8 b) {
BMP* bmp = 0;
BS->AllocatePool(EfiBootServicesData, 54 + w * h * 4, (void**) &bmp);
if (!bmp) {
Log(1, L"HackBGRT: Failed to allocate a blank BMP!\n");
BS->Stall(1000000);
return 0;
}
*bmp = (BMP) {
.magic_BM = { 'B', 'M' },
.file_size = 54 + w * h * 4,
.pixel_data_offset = 54,
.dib_header_size = 40,
.width = w,
.height = h,
.planes = 1,
.bpp = 32,
};
UINT8* data = (UINT8*) bmp + bmp->pixel_data_offset;
for (int y = 0; y < h; ++y) for (int x = 0; x < w; ++x) {
*data++ = b;
*data++ = g;
*data++ = r;
*data++ = 0;
}
return bmp;
}
/**
* Load a bitmap or generate a black one.
*
@@ -216,32 +240,51 @@ static ACPI_BGRT* HandleAcpiTables(enum HackBGRT_action action, ACPI_BGRT* bgrt)
* @return The loaded BMP, or 0 if not available.
*/
static BMP* LoadBMP(EFI_FILE_HANDLE root_dir, const CHAR16* path) {
BMP* bmp = 0;
if (!path) {
BS->AllocatePool(EfiBootServicesData, 58, (void**) &bmp);
if (!bmp) {
Print(L"HackBGRT: Failed to allocate a blank BMP!\n");
BS->Stall(1000000);
return 0;
return MakeBMP(1, 1, 0, 0, 0); // empty path = black image
}
Log(config.debug, L"HackBGRT: Loading %s.\n", path);
UINTN size = 0;
BMP* bmp = LoadFile(root_dir, path, &size);
if (bmp) {
if (size >= bmp->file_size && CompareMem(bmp, "BM", 2) == 0 && bmp->file_size - bmp->pixel_data_offset > 4 && bmp->width && bmp->height && (bmp->bpp == 32 || bmp->bpp == 24) && bmp->compression == 0) {
return bmp;
}
FreePool(bmp);
Log(1, L"HackBGRT: Invalid BMP (%s)!\n", path);
} else {
Log(1, L"HackBGRT: Failed to load BMP (%s)!\n", path);
}
BS->Stall(1000000);
return MakeBMP(16, 16, 255, 0, 0); // error = red image
}
/**
* Crop a BMP to the given size.
*
* @param bmp The BMP to crop.
* @param w The maximum width.
* @param h The maximum height.
*/
static void CropBMP(BMP* bmp, int w, int h) {
const int old_pitch = -(-(bmp->width * (bmp->bpp / 8)) & ~3);
bmp->image_size = 0;
bmp->width = min(bmp->width, w);
bmp->height = min(bmp->height, h);
const int h_max = (bmp->file_size - bmp->pixel_data_offset) / old_pitch;
bmp->height = min(bmp->height, h_max);
const int new_pitch = -(-(bmp->width * (bmp->bpp / 8)) & ~3);
if (new_pitch < old_pitch) {
for (int i = 1; i < bmp->height; ++i) {
CopyMem(
bmp,
"\x42\x4d\x3a\x00\x00\x00\x00\x00\x00\x00\x36\x00\x00\x00\x28\x00"
"\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x18\x00\x00\x00"
"\x00\x00\x04\x00\x00\x00\x13\x0b\x00\x00\x13\x0b\x00\x00\x00\x00"
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
58
(UINT8*) bmp + bmp->pixel_data_offset + i * new_pitch,
(UINT8*) bmp + bmp->pixel_data_offset + i * old_pitch,
new_pitch
);
return bmp;
}
Debug(L"HackBGRT: Loading %s.\n", path);
bmp = LoadFile(root_dir, path, 0);
if (!bmp) {
Print(L"HackBGRT: Failed to load BMP (%s)!\n", path);
BS->Stall(1000000);
return 0;
}
return bmp;
bmp->file_size = bmp->pixel_data_offset + bmp->height * new_pitch;
}
/**
@@ -259,14 +302,15 @@ void HackBgrt(EFI_FILE_HANDLE root_dir) {
// KEEP/REPLACE: first get the old BGRT entry.
ACPI_BGRT* bgrt = HandleAcpiTables(HackBGRT_KEEP, 0);
// Get the old BMP and position, if possible.
BMP* old_bmp = 0;
int old_x = 0, old_y = 0;
if (bgrt && VerifyAcpiSdtChecksum(bgrt)) {
old_bmp = (BMP*) (UINTN) bgrt->image_address;
old_x = bgrt->image_offset_x;
old_y = bgrt->image_offset_y;
}
// Get the old BMP and position (relative to screen center), if possible.
const int old_valid = bgrt && VerifyAcpiSdtChecksum(bgrt);
BMP* old_bmp = old_valid ? (BMP*) (UINTN) bgrt->image_address : 0;
const int old_orientation = old_valid ? ((bgrt->status >> 1) & 3) : 0;
const int old_swap = old_orientation & 1;
const int old_reso_x = old_swap ? config.old_resolution_y : config.old_resolution_x;
const int old_reso_y = old_swap ? config.old_resolution_x : config.old_resolution_y;
const int old_x = old_bmp ? bgrt->image_offset_x + (old_bmp->width - old_reso_x) / 2 : 0;
const int old_y = old_bmp ? bgrt->image_offset_y + (old_bmp->height - old_reso_y) / 2 : 0;
// Missing BGRT?
if (!bgrt) {
@@ -277,17 +321,24 @@ void HackBgrt(EFI_FILE_HANDLE root_dir) {
// Replace missing = allocate new.
BS->AllocatePool(EfiACPIReclaimMemory, sizeof(*bgrt), (void**)&bgrt);
if (!bgrt) {
Print(L"HackBGRT: Failed to allocate memory for BGRT.\n");
Log(1, L"HackBGRT: Failed to allocate memory for BGRT.\n");
return;
}
}
// Clear the BGRT.
const char data[0x38] =
"BGRT" "\x38\x00\x00\x00" "\x00" "\xd6" "Mtblx*" "HackBGRT"
"\x20\x17\x00\x00" "PTL " "\x02\x00\x00\x00"
"\x01\x00" "\x00" "\x00";
CopyMem(bgrt, data, sizeof(data));
*bgrt = (ACPI_BGRT) {
.header = {
.signature = "BGRT",
.length = sizeof(*bgrt),
.revision = 1,
.oem_id = "Mtblx*",
.oem_table_id = "HackBGRT",
.oem_revision = 1,
.asl_compiler_id = *(const UINT32*) "None",
.asl_compiler_revision = 1,
},
.version = 1,
};
// Get the image (either old or new).
BMP* new_bmp = old_bmp;
@@ -301,37 +352,62 @@ void HackBgrt(EFI_FILE_HANDLE root_dir) {
return;
}
// Crop the image to screen.
CropBMP(new_bmp, config.resolution_x, config.resolution_y);
// Set the image address and orientation.
bgrt->image_address = (UINTN) new_bmp;
const int new_orientation = config.orientation == HackBGRT_coord_keep ? old_orientation : ((config.orientation / 90) & 3);
bgrt->status = new_orientation << 1;
// Calculate the automatically centered position for the image.
int auto_x = 0, auto_y = 0;
if (GOP()) {
auto_x = max(0, ((int)GOP()->Mode->Info->HorizontalResolution - (int)new_bmp->width) / 2);
auto_y = max(0, ((int)GOP()->Mode->Info->VerticalResolution * 2/3 - (int)new_bmp->height) / 2);
} else if (old_bmp) {
auto_x = max(0, old_x + ((int)old_bmp->width - (int)new_bmp->width) / 2);
auto_y = max(0, old_y + ((int)old_bmp->height - (int)new_bmp->height) / 2);
}
// New center coordinates.
const int new_x = config.image_x == HackBGRT_coord_keep ? old_x : config.image_x;
const int new_y = config.image_y == HackBGRT_coord_keep ? old_y : config.image_y;
const int new_swap = new_orientation & 1;
const int new_reso_x = new_swap ? config.resolution_y : config.resolution_x;
const int new_reso_y = new_swap ? config.resolution_x : config.resolution_y;
// Set the position (manual, automatic, original).
bgrt->image_offset_x = SelectCoordinate(config.image_x, auto_x, old_x);
bgrt->image_offset_y = SelectCoordinate(config.image_y, auto_y, old_y);
Debug(L"HackBGRT: BMP at (%d, %d).\n", (int) bgrt->image_offset_x, (int) bgrt->image_offset_y);
// Calculate absolute position.
const int max_x = new_reso_x - new_bmp->width;
const int max_y = new_reso_y - new_bmp->height;
bgrt->image_offset_x = max(0, min(max_x, new_x + (new_reso_x - new_bmp->width) / 2));
bgrt->image_offset_y = max(0, min(max_y, new_y + (new_reso_y - new_bmp->height) / 2));
Log(config.debug,
L"HackBGRT: BMP at (%d, %d), center (%d, %d), resolution (%d, %d), orientation %d.\n",
(int) bgrt->image_offset_x, (int) bgrt->image_offset_y,
new_x, new_y, new_reso_x, new_reso_y,
new_orientation * 90
);
// Store this BGRT in the ACPI tables.
SetAcpiSdtChecksum(bgrt);
HandleAcpiTables(HackBGRT_REPLACE, bgrt);
}
/**
* Load an application.
*/
static EFI_HANDLE LoadApp(int print_failure, EFI_HANDLE image_handle, EFI_LOADED_IMAGE* image, const CHAR16* path) {
EFI_DEVICE_PATH* boot_dp = FileDevicePath(image->DeviceHandle, (CHAR16*) path);
EFI_HANDLE result = 0;
Log(config.debug, L"HackBGRT: Loading application %s.\n", path);
if (EFI_ERROR(BS->LoadImage(0, image_handle, boot_dp, 0, 0, &result))) {
Log(config.debug || print_failure, L"HackBGRT: Failed to load application %s.\n", path);
}
return result;
}
/**
* The main program.
*/
EFI_STATUS EFIAPI EfiMain(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *ST_) {
InitializeLib(image_handle, ST_);
Log(0, L"HackBGRT version: %s\n", version);
EFI_LOADED_IMAGE* image;
if (EFI_ERROR(BS->HandleProtocol(image_handle, &LoadedImageProtocol, (void**) &image))) {
Debug(L"HackBGRT: LOADED_IMAGE_PROTOCOL failed.\n");
Log(config.debug, L"HackBGRT: LOADED_IMAGE_PROTOCOL failed.\n");
goto fail;
}
@@ -343,66 +419,73 @@ EFI_STATUS EFIAPI EfiMain(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *ST_) {
if (argc <= 1) {
const CHAR16* config_path = L"\\EFI\\HackBGRT\\config.txt";
if (!ReadConfigFile(&config, root_dir, config_path)) {
Print(L"HackBGRT: No config, no command line!\n", config_path);
Log(1, L"HackBGRT: No config, no command line!\n", config_path);
goto fail;
}
}
for (int i = 1; i < argc; ++i) {
ReadConfigLine(&config, root_dir, argv[i]);
}
Debug = config.debug ? Print : NullPrint;
if (config.debug) {
Print(L"HackBGRT version: %s\n", version);
}
SetResolution(config.resolution_x, config.resolution_y);
HackBgrt(root_dir);
EFI_HANDLE next_image_handle = 0;
if (!config.boot_path) {
Print(L"HackBGRT: Boot path not specified.\n");
} else {
Debug(L"HackBGRT: Loading application %s.\n", config.boot_path);
EFI_DEVICE_PATH* boot_dp = FileDevicePath(image->DeviceHandle, (CHAR16*) config.boot_path);
if (EFI_ERROR(BS->LoadImage(0, image_handle, boot_dp, 0, 0, &next_image_handle))) {
Print(L"HackBGRT: Failed to load application %s.\n", config.boot_path);
}
static CHAR16 backup_boot_path[] = L"\\EFI\\HackBGRT\\bootmgfw-original.efi";
static CHAR16 ms_boot_path[] = L"\\EFI\\Microsoft\\Boot\\bootmgfw.efi";
int try_ms_quietly = 1;
if (config.boot_path && StriCmp(config.boot_path, L"MS") != 0) {
next_image_handle = LoadApp(1, image_handle, image, config.boot_path);
try_ms_quietly = 0;
}
if (!next_image_handle) {
static CHAR16 default_boot_path[] = L"\\EFI\\HackBGRT\\bootmgfw-original.efi";
Debug(L"HackBGRT: Loading application %s.\n", default_boot_path);
EFI_DEVICE_PATH* boot_dp = FileDevicePath(image->DeviceHandle, default_boot_path);
if (EFI_ERROR(BS->LoadImage(0, image_handle, boot_dp, 0, 0, &next_image_handle))) {
Print(L"HackBGRT: Also failed to load application %s.\n", default_boot_path);
config.boot_path = backup_boot_path;
next_image_handle = LoadApp(!try_ms_quietly, image_handle, image, config.boot_path);
if (!next_image_handle) {
config.boot_path = ms_boot_path;
next_image_handle = LoadApp(!try_ms_quietly, image_handle, image, config.boot_path);
if (!next_image_handle) {
goto fail;
}
Print(L"HackBGRT: Reverting to %s.\n", default_boot_path);
Print(L"Press escape to cancel, any other key to boot.\n");
if (ReadKey().ScanCode == SCAN_ESC) {
}
if (try_ms_quietly) {
goto ready_to_boot;
}
Log(1, L"HackBGRT: Reverting to %s.\n", config.boot_path);
Print(L"Press escape to cancel or any other key (or wait 15 seconds) to boot.\n");
if (ReadKey(15000).ScanCode == SCAN_ESC) {
goto fail;
}
config.boot_path = default_boot_path;
}
if (config.debug) {
Print(L"HackBGRT: Ready to boot.\nPress escape to cancel, any other key to boot.\n");
if (ReadKey().ScanCode == SCAN_ESC) {
} else ready_to_boot: if (config.debug) {
Print(L"HackBGRT: Ready to boot.\n");
Print(L"If all goes well, you can set debug=0 and log=0 in config.txt.\n");
Print(L"Press escape to cancel or any other key (or wait 15 seconds) to boot.\n");
if (ReadKey(15000).ScanCode == SCAN_ESC) {
return 0;
}
}
if (!config.log) {
ClearLogVariable();
}
if (EFI_ERROR(BS->StartImage(next_image_handle, 0, 0))) {
Print(L"HackBGRT: Failed to start %s.\n", config.boot_path);
Log(1, L"HackBGRT: Failed to start %s.\n", config.boot_path);
goto fail;
}
Print(L"HackBGRT: Started %s. Why are we still here?!\n", config.boot_path);
Log(1, L"HackBGRT: Started %s. Why are we still here?!\n", config.boot_path);
Print(L"Please check that %s is not actually HackBGRT!\n", config.boot_path);
goto fail;
fail: {
Print(L"HackBGRT has failed. Use parameter debug=1 for details.\n");
Print(L"Get a Windows install disk or a recovery disk to fix your boot.\n");
#ifdef GIT_DESCRIBE
Print(L"HackBGRT version: " GIT_DESCRIBE L"\n");
#else
Print(L"HackBGRT version: unknown; not an official release?\n");
#endif
Print(L"Press any key to exit.\n");
ReadKey();
Log(1, L"HackBGRT has failed.\n");
Print(L"Dumping log:\n\n");
DumpLog();
Print(L"If you can't boot into Windows, get install/recovery disk to fix your boot.\n");
Print(L"Press any key (or wait 15 seconds) to exit.\n");
ReadKey(15000);
return 1;
}
}

10
src/sbat.c Normal file
View File

@@ -0,0 +1,10 @@
#ifdef GIT_DESCRIBE
#define SBAT_READABLE_VERSION GIT_DESCRIBE
#else
#define SBAT_READABLE_VERSION "unknown"
#endif
const char sbat[] __attribute__ ((section (".sbat"))) =
"sbat,1,SBAT Version,sbat,1,https://github.com/rhboot/shim/blob/main/SBAT.md\n"
"hackbgrt,1,Metabolix,HackBGRT," SBAT_READABLE_VERSION ",https://github.com/Metabolix/HackBGRT\n"
;

View File

@@ -52,6 +52,12 @@ typedef struct {
UINT32 height;
UINT16 planes;
UINT16 bpp;
UINT32 compression;
UINT32 image_size;
UINT32 x_pixels_per_meter;
UINT32 y_pixels_per_meter;
UINT32 colors_used;
UINT32 important_colors;
} BMP;
/**

View File

@@ -14,8 +14,31 @@ const CHAR16* TmpStr(CHAR8 *src, int length) {
return dest;
}
UINTN NullPrint(IN CHAR16 *fmt, ...) {
return 0;
#define log_buffer_size (65536)
CHAR16 log_buffer[log_buffer_size] = {0};
CHAR16 LogVarName[] = L"HackBGRTLog";
EFI_GUID LogVarGuid = {0x03c64761, 0x075f, 0x4dba, {0xab, 0xfb, 0x2e, 0xd8, 0x9e, 0x18, 0xb2, 0x36}}; // self-made: 03c64761-075f-4dba-abfb-2ed89e18b236
void Log(int print, IN CONST CHAR16 *fmt, ...) {
va_list args;
CHAR16 buf[256];
va_start(args, fmt);
VSPrint(buf, sizeof(buf), fmt, args); // size is in bytes, not CHAR16s
va_end(args);
if (print) {
Print(L"%s", buf);
}
StrnCat(log_buffer, buf, log_buffer_size - StrLen(log_buffer) - 1);
LibSetVariable(LogVarName, &LogVarGuid, StrLen(log_buffer) * 2, log_buffer);
}
void DumpLog(void) {
Print(L"%s", log_buffer);
}
void ClearLogVariable(void) {
LibDeleteVariable(LogVarName, &LogVarGuid);
}
const CHAR16* TrimLeft(const CHAR16* s) {
@@ -68,14 +91,15 @@ void RandomSeedAuto(void) {
RandomSeed(a, b), Random(), Random();
}
void WaitKey(void) {
EFI_STATUS WaitKey(UINT64 timeout_ms) {
ST->ConIn->Reset(ST->ConIn, FALSE);
WaitForSingleEvent(ST->ConIn->WaitForKey, 0);
const int ms_to_100ns = 10000;
return WaitForSingleEvent(ST->ConIn->WaitForKey, timeout_ms * ms_to_100ns);
}
EFI_INPUT_KEY ReadKey(void) {
WaitKey();
EFI_INPUT_KEY ReadKey(UINT64 timeout_ms) {
EFI_INPUT_KEY key = {0};
WaitKey(timeout_ms);
ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
return key;
}

View File

@@ -14,7 +14,17 @@ extern const CHAR16* TmpStr(CHAR8 *src, int length);
/**
* Empty function that has the same signature as Print.
*/
extern UINTN NullPrint(IN CHAR16 *fmt, ...);
extern void Log(int print, IN CONST CHAR16 *fmt, ...);
/**
* Dump the log buffer to the screen.
*/
extern void DumpLog(void);
/**
* Clear the log EFI variable, for minor RAM savings.
*/
extern void ClearLogVariable(void);
/**
* Return the greater of two numbers.
@@ -82,15 +92,18 @@ extern void RandomSeedAuto(void);
/**
* Wait for a key press. It will still remain in the buffer.
*
* @param timeout_ms The timeout in milliseconds, or 0 for no timeout.
*/
extern void WaitKey(void);
extern EFI_STATUS WaitKey(UINT64 timeout_ms);
/**
* Wait for a key press and read it.
*
* @param timeout_ms The timeout in milliseconds, or 0 for no timeout.
* @return The pressed key.
*/
extern EFI_INPUT_KEY ReadKey(void);
extern EFI_INPUT_KEY ReadKey(UINT64 timeout_ms);
/**
* Load a file, allocate some extra bytes as well.