From patchwork Mon Feb 17 14:48:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 206566 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.1 required=3.0 tests=DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, MAILING_LIST_MULTI, MENTIONS_GIT_HOSTING, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 97528C34021 for ; Mon, 17 Feb 2020 14:48:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 69251207FD for ; Mon, 17 Feb 2020 14:48:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1581950917; bh=3iEQo+clV6uRKXkbTV7tOyjrl8iCUKQhTBO16ZCLhyo=; h=From:To:Cc:Subject:Date:List-ID:From; b=yFkYzbu52qpsh/7YDveRnPHwfL/pbJ3IdBtymI8f+j30hXkUqN+LSWc31hDu/RTsy Gsz+y+MjtJ8MAa4F4DsT6/65zcRTyyln4ShY7xf/kH/6UvsSd26dPFT0+2AxrPQhjf l55P1WFEW9m4HeZm6vFntvsLRIfne8kgd9PpCBYI= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728819AbgBQOsh (ORCPT ); Mon, 17 Feb 2020 09:48:37 -0500 Received: from mail.kernel.org ([198.145.29.99]:58216 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728723AbgBQOsh (ORCPT ); Mon, 17 Feb 2020 09:48:37 -0500 Received: from cam-smtp0.cambridge.arm.com (fw-tnat.cambridge.arm.com [217.140.96.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 9526D20718; Mon, 17 Feb 2020 14:48:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1581950916; bh=3iEQo+clV6uRKXkbTV7tOyjrl8iCUKQhTBO16ZCLhyo=; h=From:To:Cc:Subject:Date:From; b=gGUIn6lG+NSAkf8MOrtuPRJz5TnZPH0irrflBE6iSOCNuSXMChP/jA8r1A3Mq5gt4 k0ZubixXHOe2xZa6l8nm7lKEcCSauJTWyGeiIk9qFGAR1cCXvTgA0odmSxcXjamKMZ RqEQ2Q59ED0k3O/r49FTjPveP65SLLMeBImYoG5k= From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: Ard Biesheuvel , lersek@redhat.com, leif@nuviainc.com, pjones@redhat.com, mjg59@google.com, agraf@csgraf.de, daniel.kiper@oracle.com, hdegoede@redhat.com, nivedita@alum.mit.edu, mingo@kernel.org Subject: [PATCH v2 0/5] efi/x86: add support for generic EFI mixed mode boot Date: Mon, 17 Feb 2020 15:48:17 +0100 Message-Id: <20200217144822.24616-1-ardb@kernel.org> X-Mailer: git-send-email 2.17.1 Sender: linux-efi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org This series is another part of my effort to reduce the level of knowledge on the part of the bootloader or firmware of internal per-architecture details regarding where/how the kernel is loaded and where its initrd and other context data are passed. Changes since v1: - add mixed mode wrappers for the EFI loaded_image protocol (#2) - expose Exit() boot service and invoke it on errors rather than doing and ordinary function return or entering a deadloop (#3) - move code into .text and r/o data into .rodata where appropriate, and disentangle the mixed mode boot path and the ordinary one (#4) - disable initrd load from file in mixed mode - reorder patches #4 and #5 ---- v1 blurb (cont'd) --- The x86 architecture has a so-called 'EFI handover protocol', which defines how the bootparams struct should be populated, and how it should be interpreted to figure out where to load the kernel, and at which offset in the binary the entrypoint is located. This scheme allows the initrd to be loaded beforehand, and allows 32-bit firmware to invoke a 64-bit kernel via a special entrypoint that manages the state transitions between the two execution modes. Due to this, x86 loaders currently do not rely on LoadImage and StartImage, and therefore, are forced to re-implement things like image authentication for secure boot and taking the measurements for measured boot in their open coded clones of these routines. My previous series on this topic [0][1] implements a generic way to load the initrd from any source supported by the loader without relying on something like a device tree or bootparams structure, and so native boot should not need the EFI handover protocol anymore after those change are merged. What remains is mixed mode boot, which also needs the EFI handover protocol regardless of whether an initrd is loaded or not. So let's get rid of that requirement, and take advantage of the fact that EDK2 based firmware does support LoadImage() for X64 binaries on IA32 firmware, which means we can rely on the secure boot and measured boot checks being performed by the firmware. The only thing we need to put on top is a way to discover the non-native entrypoint into the binary in a way that does not rely on x86 specific headers and data structures. So let's introduce a new .compat header in the PE/COFF metadata of the bzImage, and populate it with a tuple, allowing a generic EFI loader to decide whether the entrypoint supports its native machine type, and invoke it as an ordinary EFI application entrypoint. Since we will not be passing a bootparams structure, we need to discover the base of the image (which contains the setup header) via the loaded image protocol before we can enter the kernel in 32-bit mode at startup_32() A loader implementation for OVMF can be found at [2]. Note that this loader code is fully generic, and permits mixed mode images to be launched from the UEFI shell or other generic components based on LoadImage/Startimage. It could be used without modifications if other architectures ever emerge that support kernels that can be invoked from a non-native (but cross-type supported) loader. [0] https://lore.kernel.org/linux-arm-kernel/20200206140352.6300-1-ardb@kernel.org/ [1] https://lore.kernel.org/linux-efi/20200216141104.21477-1-ardb@kernel.org/ [2] https://github.com/ardbiesheuvel/edk2/commits/linux-efi-generic Cc: lersek@redhat.com Cc: leif@nuviainc.com Cc: pjones@redhat.com Cc: mjg59@google.com Cc: agraf@csgraf.de Cc: daniel.kiper@oracle.com Cc: hdegoede@redhat.com Cc: nivedita@alum.mit.edu Cc: mingo@kernel.org Ard Biesheuvel (5): efi/x86: Drop redundant .bss section efi/libstub/x86: Make loaded_image protocol handling mixed mode safe efi/libstub/x86: Use Exit() boot service to exit the stub on errors efi/x86: Implement mixed mode boot without the handover protocol efi/x86: Add true mixed mode entry point into .compat section arch/x86/boot/Makefile | 2 +- arch/x86/boot/compressed/head_64.S | 60 +++++++++++++++- arch/x86/boot/header.S | 23 +++---- arch/x86/boot/tools/build.c | 69 ++++++++++++------- arch/x86/include/asm/efi.h | 8 +++ .../firmware/efi/libstub/efi-stub-helper.c | 4 +- drivers/firmware/efi/libstub/efistub.h | 50 ++++++++++---- drivers/firmware/efi/libstub/x86-stub.c | 53 ++++++++------ 8 files changed, 192 insertions(+), 77 deletions(-)