Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide better error message when MokManager is not found #663

Merged
merged 3 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ CFLAGS += -DENABLE_SHIM_CERT
else
TARGETS += $(MMNAME) $(FBNAME)
endif
OBJS = shim.o globals.o mok.o netboot.o cert.o replacements.o tpm.o version.o errlog.o sbat.o sbat_data.o sbat_var.o pe.o pe-relocate.o httpboot.o csv.o load-options.o
OBJS = shim.o globals.o mok.o netboot.o cert.o dp.o replacements.o tpm.o version.o errlog.o sbat.o sbat_data.o sbat_var.o pe.o pe-relocate.o httpboot.o csv.o load-options.o
KEYS = shim_cert.h ocsp.* ca.* shim.crt shim.csr shim.p12 shim.pem shim.key shim.cer
ORIG_SOURCES = shim.c globals.c mok.c netboot.c replacements.c tpm.c errlog.c sbat.c pe.c pe-relocate.c httpboot.c shim.h version.h $(wildcard include/*.h) cert.S sbat_var.S
MOK_OBJS = MokManager.o PasswordCrypt.o crypt_blowfish.o errlog.o sbat_data.o globals.o
ORIG_SOURCES = shim.c globals.c mok.c netboot.c dp.c replacements.c tpm.c errlog.c sbat.c pe.c pe-relocate.c httpboot.c shim.h version.h $(wildcard include/*.h) cert.S sbat_var.S
MOK_OBJS = MokManager.o PasswordCrypt.o crypt_blowfish.o errlog.o sbat_data.o globals.o dp.o
ORIG_MOK_SOURCES = MokManager.c PasswordCrypt.c crypt_blowfish.c shim.h $(wildcard include/*.h)
FALLBACK_OBJS = fallback.o tpm.o errlog.o sbat_data.o globals.o
ORIG_FALLBACK_SRCS = fallback.c
Expand Down
43 changes: 43 additions & 0 deletions dp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SPDX-License-Identifier: BSD-2-Clause-Patent
/*
* dp.c - device path helpers
* Copyright Peter Jones <[email protected]>
*/

#include "shim.h"

int
is_removable_media_path(EFI_LOADED_IMAGE *li)
{
unsigned int pathlen = 0;
CHAR16 *bootpath = NULL;
int ret = 0;

bootpath = DevicePathToStr(li->FilePath);

/* Check the beginning of the string and the end, to avoid
* caring about which arch this is. */
/* I really don't know why, but sometimes bootpath gives us
* L"\\EFI\\BOOT\\/BOOTX64.EFI". So just handle that here...
*/
if (StrnCaseCmp(bootpath, L"\\EFI\\BOOT\\BOOT", 14) &&
StrnCaseCmp(bootpath, L"\\EFI\\BOOT\\/BOOT", 15) &&
StrnCaseCmp(bootpath, L"EFI\\BOOT\\BOOT", 13) &&
StrnCaseCmp(bootpath, L"EFI\\BOOT\\/BOOT", 14))
goto error;

pathlen = StrLen(bootpath);
if (pathlen < 5 || StrCaseCmp(bootpath + pathlen - 4, L".EFI"))
goto error;

ret = 1;

error:
if (bootpath)
FreePool(bootpath);

return ret;
}


// vim:fenc=utf-8:tw=75:noet
14 changes: 14 additions & 0 deletions include/dp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: BSD-2-Clause-Patent
/*
* dp.h - device path helper functions
* Copyright Peter Jones <[email protected]>
*/

#ifndef DP_H_
#define DP_H_

int
is_removable_media_path(EFI_LOADED_IMAGE *li);

#endif /* !DP_H_ */
// vim:fenc=utf-8:tw=75:noet
26 changes: 26 additions & 0 deletions mok.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,32 @@ static EFI_STATUS check_mok_request(EFI_HANDLE image_handle)
efi_status = start_image(image_handle, MOK_MANAGER);

if (EFI_ERROR(efi_status)) {
/*
* We don't do this in the unit tests because we
* don't have simulation for console_countdown()
* and similar.
*/
#ifndef SHIM_UNIT_TEST
EFI_STATUS efi_status_2;
EFI_LOADED_IMAGE *li;
efi_status_2 = BS->HandleProtocol(image_handle, &EFI_LOADED_IMAGE_GUID,
(void **)&li);
if (EFI_ERROR(efi_status_2))
perror (L"Failed to get image: %r\n", efi_status_2);
else if (is_removable_media_path(li) &&
efi_status == EFI_NOT_FOUND) {
CHAR16 *title = L"Could not find MokManager";
CHAR16 *message = L"MokManager is missing on removable media.";
/*
* This occurs when system is booting on
* hard disk's EFI/BOOT/BOOTxxx.EFI entry
* while it should have booted on
* EFI/<os>/shimxxx.efi entry
*/
console_countdown(title, message, 10);
RT->ResetSystem(EfiResetWarm, EFI_SUCCESS, 0, NULL);
}
#endif
perror(L"Failed to start MokManager: %r\n", efi_status);
return efi_status;
}
Expand Down
35 changes: 1 addition & 34 deletions shim.c
Original file line number Diff line number Diff line change
Expand Up @@ -778,39 +778,6 @@ verify_buffer (char *data, int datasize,
return verify_buffer_sbat(data, datasize, context);
}

static int
is_removable_media_path(EFI_LOADED_IMAGE *li)
{
unsigned int pathlen = 0;
CHAR16 *bootpath = NULL;
int ret = 0;

bootpath = DevicePathToStr(li->FilePath);

/* Check the beginning of the string and the end, to avoid
* caring about which arch this is. */
/* I really don't know why, but sometimes bootpath gives us
* L"\\EFI\\BOOT\\/BOOTX64.EFI". So just handle that here...
*/
if (StrnCaseCmp(bootpath, L"\\EFI\\BOOT\\BOOT", 14) &&
StrnCaseCmp(bootpath, L"\\EFI\\BOOT\\/BOOT", 15) &&
StrnCaseCmp(bootpath, L"EFI\\BOOT\\BOOT", 13) &&
StrnCaseCmp(bootpath, L"EFI\\BOOT\\/BOOT", 14))
goto error;

pathlen = StrLen(bootpath);
if (pathlen < 5 || StrCaseCmp(bootpath + pathlen - 4, L".EFI"))
goto error;

ret = 1;

error:
if (bootpath)
FreePool(bootpath);

return ret;
}

static int
should_use_fallback(EFI_HANDLE image_handle)
{
Expand Down Expand Up @@ -1154,7 +1121,7 @@ EFI_STATUS read_image(EFI_HANDLE image_handle, CHAR16 *ImagePath,
efi_status = load_image(shim_li, data, datasize, *PathName);
if (EFI_ERROR(efi_status)) {
perror(L"Failed to load image %s: %r\n",
PathName, efi_status);
*PathName, efi_status);
PrintErrors();
ClearErrors();
return efi_status;
Expand Down
1 change: 1 addition & 0 deletions shim.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
#include "include/configtable.h"
#include "include/console.h"
#include "include/crypt_blowfish.h"
#include "include/dp.h"
#include "include/efiauthenticated.h"
#include "include/errors.h"
#include "include/execute.h"
Expand Down