From patchwork Mon Mar 11 00:16:43 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heinrich Schuchardt X-Patchwork-Id: 1054074 X-Patchwork-Delegate: xypron.glpk@gmx.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gmx.de Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 44Hdvb5Ndnz9s4Y for ; Mon, 11 Mar 2019 11:16:59 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 6765EC21DF8; Mon, 11 Mar 2019 00:16:54 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 58410C21C57; Mon, 11 Mar 2019 00:16:52 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 2643BC21C57; Mon, 11 Mar 2019 00:16:51 +0000 (UTC) Received: from mout.gmx.net (mout.gmx.net [212.227.17.20]) by lists.denx.de (Postfix) with ESMTPS id C6503C21C2F for ; Mon, 11 Mar 2019 00:16:50 +0000 (UTC) Received: from LT02.fritz.box ([84.118.159.3]) by mail.gmx.com (mrgmx101 [212.227.17.174]) with ESMTPSA (Nemesis) id 0Mchyv-1hKX0z1eqH-00I0NF; Mon, 11 Mar 2019 01:16:48 +0100 From: Heinrich Schuchardt To: Alexander Graf Date: Mon, 11 Mar 2019 01:16:43 +0100 Message-Id: <20190311001643.29297-1-xypron.glpk@gmx.de> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 X-Provags-ID: V03:K1:hy+a0DenNZMD+a8ft/5GAjFbHUhmFlMDOXn/1p311mMjujYAIDD QAVUW0aRqe0QJwWnHqpUMg2XkLmNiqU34PWZhwqovG7EFJRmW0/YYYy+wrM/douJv9D20LS B+jifhlhL5UT8as+nfmzdwUZ04bcfa4hkD1XuHkteiuW8D2yKBtO1q5gb8NCxi4mQS6kSxS /lOnP66zMThgUszxJUOEA== X-UI-Out-Filterresults: notjunk:1; V03:K0:3hfVnuyniA4=:iJ5jMsX8HEZP8DWY2YZibK 4iSKfXSSWQT/zfswNWZKfHCnUvcGoaiFJP+cjXDvhUouRIdy1KYExFqS/X8dTA7aPkgaMm3iC hGW+VT5kuQXB/Ce63qqUl61bgOwymaGn73PFdmVCaI89j4eq/nJErIXZzYaRS/ZxGNRFrP2mN wpzOIiSsnydtoJN/hVs4rsQj80C9+c2nq7cXsNgIZnYg6sSE49lZmWm2i7bdHTCN8MPfr/rO+ 4QgM+2Ef/7sL3HM9T1mGLuOXurvYRE1HCiotoueIX8SL9ECq2oTZjS0gRe7IpRaZQ9Zn+Dmu/ URuXaqqinEKi6yDREFdNEILMIUxAqzzELOG48tBJmQGGTQiTRqXBCXkeXFEmckQrIDWhC6VfS aFzv7sWpOc3ytti87VHVn9VNlZ5+pzkxE8u3RcNE0cBik1PJGpSbXH5q8qXbuH+vN+z/jQa2r /08Gj9eDTWq2UhdRkj1/rDWrPO8bxRzwpXgrIviHaH1FaodB9mssObllRAbpUhjQE4n5YF9tC 0/ZGwJ4JvxqrE17+cr0N547HmA5ae3ZU+4S6089pOGsx5MZMG7mSNQTckowQz6dBuP/HcAgBc FcdqE76CYQp7KMt7Wq9eemLsh06QIE15vWBN9Lov5X47yRVwNHiP1qyFzsCcokGptTOTHjNX5 e5MXV6/1vVFiNwjCIBYfhigFSMl8wyLRFEUWraZpGYwu0EAQmEyaMFppkDaPb+EOrHVN6vEnt rwX71GwDRkL9qV8A1OGV0A/wqLsC992ifOd0Nu5EdaKG9Hz6Phl/DJDqnJp5cjmNzCRvWQyw3 wUWf6wAX48Cwu/RUiWNGp+pOIZ34p6oHKj2DuWgcsKO8NWvsKyUIixzYSMIHl8CcR0+nN4sHC r/8esB44ycgEYMKhzBoRYJEbMHgaV5j/R6jXNuJrl71swMUYGocXxqSFCBu4YA Cc: Heinrich Schuchardt , u-boot@lists.denx.de, Ard Biesheuvel , Alexander Graf Subject: [U-Boot] [PATCH v4 1/1] efi_loader: Patch non-runtime code out at ExitBootServices already X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" From: Alexander Graf While discussing something compeltely different, Ard pointed out that it might be legal to omit calling SetVirtualAddressMap altogether. There is even a patch on the Linux Kernel Mailing List that implements such behavior by now: https://patchwork.kernel.org/patch/10782393/ While that sounds great, we currently rely on the SetVirtualAddressMap call to remove all references to code that would not work outside of boot services. So let's patch out those bits already on the call to ExitBootServices, so that we can successfully run even when an OS chooses to omit any call to SetVirtualAddressMap. Reported-by: Ard Biesheuvel Signed-off-by: Alexander Graf OpenBSD is not calling SetVirtualAddressMap on ARM 32 bit. Adjust selftest: expect 'U-Boot' instead of 'resetting'. Reviewed-by: Heinrich Schuchardt --- v4 Adjust selftest: expect 'U-Boot' instead of 'resetting'. (by fault sent as v2) v3 Add link to upstream Linux patch v2 Add missing icache invalidation --- include/efi_loader.h | 2 ++ lib/efi_loader/efi_boottime.c | 1 + lib/efi_loader/efi_runtime.c | 29 ++++++++++++++++++++--------- test/py/tests/test_efi_selftest.py | 4 ++-- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/include/efi_loader.h b/include/efi_loader.h index 512880ab8fb..82db7775c72 100644 --- a/include/efi_loader.h +++ b/include/efi_loader.h @@ -310,6 +310,8 @@ void efi_save_gd(void); void efi_restore_gd(void); /* Call this to relocate the runtime section to an address space */ void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map); +/* Call this when we start to live in a runtime only world */ +void efi_runtime_detach(ulong offset); /* Call this to set the current device name */ void efi_set_bootdev(const char *dev, const char *devnr, const char *path); /* Add a new object to the object list. */ diff --git a/lib/efi_loader/efi_boottime.c b/lib/efi_loader/efi_boottime.c index bd8b8a17ae7..233bca78e0a 100644 --- a/lib/efi_loader/efi_boottime.c +++ b/lib/efi_loader/efi_boottime.c @@ -1967,6 +1967,7 @@ static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle, bootm_disable_interrupts(); /* Disable boot time services */ + efi_runtime_detach((ulong)gd->relocaddr); systab.con_in_handle = NULL; systab.con_in = NULL; systab.con_out_handle = NULL; diff --git a/lib/efi_loader/efi_runtime.c b/lib/efi_loader/efi_runtime.c index 636dfdab39d..17d22d429e2 100644 --- a/lib/efi_loader/efi_runtime.c +++ b/lib/efi_loader/efi_runtime.c @@ -276,15 +276,11 @@ struct efi_runtime_detach_list_struct { void *patchto; }; -static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = { +static struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = { { /* do_reset is gone */ .ptr = &efi_runtime_services.reset_system, .patchto = efi_reset_system, - }, { - /* invalidate_*cache_all are gone */ - .ptr = &efi_runtime_services.set_virtual_address_map, - .patchto = &efi_unimplemented, }, { /* RTC accessors are gone */ .ptr = &efi_runtime_services.get_time, @@ -328,7 +324,15 @@ static bool efi_runtime_tobedetached(void *p) return false; } -static void efi_runtime_detach(ulong offset) +/** + * efi_runtime_detach() - Remove any dependency on non-runtime sections + * + * This function patches all remaining code to be self-sufficient inside + * runtime sections. Any calls to non-runtime will be removed after this. + * + * @offset: relocaddr for pre-set_v_a_space, offset to VA after + */ +__efi_runtime void efi_runtime_detach(ulong offset) { int i; ulong patchoff = offset - (ulong)gd->relocaddr; @@ -344,6 +348,8 @@ static void efi_runtime_detach(ulong offset) /* Update CRC32 */ efi_update_table_header_crc32(&efi_runtime_services.hdr); + + invalidate_icache_all(); } /* Relocate EFI runtime to uboot_reloc_base = offset */ @@ -430,19 +436,25 @@ void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map) * @virtmap: virtual address mapping information * Return: status code */ -static efi_status_t EFIAPI efi_set_virtual_address_map( +static __efi_runtime efi_status_t EFIAPI efi_set_virtual_address_map( unsigned long memory_map_size, unsigned long descriptor_size, uint32_t descriptor_version, struct efi_mem_desc *virtmap) { + static __efi_runtime_data bool is_patched; int n = memory_map_size / descriptor_size; int i; int rt_code_sections = 0; + if (is_patched) + return EFI_INVALID_PARAMETER; + EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size, descriptor_version, virtmap); + is_patched = true; + /* * TODO: * Further down we are cheating. While really we should implement @@ -514,8 +526,7 @@ static efi_status_t EFIAPI efi_set_virtual_address_map( map->physical_start + gd->relocaddr; efi_runtime_relocate(new_offset, map); - /* Once we're virtual, we can no longer handle - complex callbacks */ + /* We need to repatch callbacks for their new VA */ efi_runtime_detach(new_offset); return EFI_EXIT(EFI_SUCCESS); } diff --git a/test/py/tests/test_efi_selftest.py b/test/py/tests/test_efi_selftest.py index 36b35ee536b..6f5b2b8f406 100644 --- a/test/py/tests/test_efi_selftest.py +++ b/test/py/tests/test_efi_selftest.py @@ -20,7 +20,7 @@ def test_efi_selftest(u_boot_console): if m != 0: raise Exception('Failures occurred during the EFI selftest') u_boot_console.run_command(cmd='', wait_for_echo=False, wait_for_prompt=False); - m = u_boot_console.p.expect(['resetting', 'U-Boot']) + m = u_boot_console.p.expect(['U-Boot']) if m != 0: raise Exception('Reset failed during the EFI selftest') u_boot_console.restart_uboot(); @@ -46,7 +46,7 @@ def test_efi_selftest_watchdog_reboot(u_boot_console): assert '\'watchdog reboot\'' in output u_boot_console.run_command(cmd='setenv efi_selftest watchdog reboot') u_boot_console.run_command(cmd='bootefi selftest', wait_for_prompt=False) - m = u_boot_console.p.expect(['resetting', 'U-Boot']) + m = u_boot_console.p.expect(['U-Boot']) if m != 0: raise Exception('Reset failed in \'watchdog reboot\' test') u_boot_console.restart_uboot();