From patchwork Fri Dec 9 18:24:03 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 87544 Delivered-To: patch@linaro.org Received: by 10.140.20.101 with SMTP id 92csp449987qgi; Fri, 9 Dec 2016 10:25:41 -0800 (PST) X-Received: by 10.84.168.129 with SMTP id f1mr161533889plb.42.1481307941495; Fri, 09 Dec 2016 10:25:41 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n6si34947739pla.24.2016.12.09.10.25.41; Fri, 09 Dec 2016 10:25:41 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-efi-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org; spf=pass (google.com: best guess record for domain of linux-efi-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-efi-owner@vger.kernel.org; dmarc=fail (p=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752807AbcLISZk (ORCPT + 2 others); Fri, 9 Dec 2016 13:25:40 -0500 Received: from mail-wj0-f177.google.com ([209.85.210.177]:35053 "EHLO mail-wj0-f177.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752704AbcLISYL (ORCPT ); Fri, 9 Dec 2016 13:24:11 -0500 Received: by mail-wj0-f177.google.com with SMTP id v7so21371149wjy.2 for ; Fri, 09 Dec 2016 10:24:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=890VkhAIUtE1CTUAANjyJ0R1vrgT4by0DPjZL7EhX+A=; b=bVoEDG19Y2vrj84XvRmdwKBpes2vbCsZxIyLEUhBeQDMuK5KtTfomXubtKOqTvR3ei o8VsftqBolSbLvl4RNCUDI9XrQ05iB4qa4vu2bH0OYsyXcDiFtymKjGIq8RKsyikcHL0 Jwk9QfsS3nl5xG3x62Q+g+9fsqP19Vkk4ArbY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=890VkhAIUtE1CTUAANjyJ0R1vrgT4by0DPjZL7EhX+A=; b=TjRtEgWMIi+23qXooITyL6BLvDsIAhvUWiGQwr9FQS05khAZMRz/8hIu+ypFHlrJL/ PhVIzMmje3Am6onJ3JqFRyTxxfMFB8geG8NmCj/77oj7sLirJbeLHbi+lqnbxlEUt/ub 33FJxRZWNDRc6wWnuKIdo7TdGZlIQIlH0xhNTFn5rJt0whHfDoJ3DFbz8T2C7/V+/Iig iumdWW+eyhLE02WIbgg3deIi/w5I2OyznHD953Evx5DKVDCAne8cI4CWqFnBxSdUxdLJ +KT6jAKjSu5LRlDLTQUAqaHFQjNHQVk/JXZOcGmJo6SX0nBXkqm6XsyQIbDYNXcy9RDG S+dg== X-Gm-Message-State: AKaTC03SOudLW/2jYqCY490nVPpbZWDVb/phDT9ZfHdXCMMv1J0mbFAjDg9IqeT9CJARCTxj X-Received: by 10.194.136.166 with SMTP id qb6mr67505486wjb.42.1481307850057; Fri, 09 Dec 2016 10:24:10 -0800 (PST) Received: from localhost.localdomain ([105.144.52.243]) by smtp.gmail.com with ESMTPSA id c81sm21747663wmf.22.2016.12.09.10.24.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 09 Dec 2016 10:24:09 -0800 (PST) From: Ard Biesheuvel To: linux-efi@vger.kernel.org, linux-arm-kernel@lists.infradead.org, james.morse@arm.com Cc: matt@codeblueprint.co.uk, jhugo@codeaurora.org, Ard Biesheuvel Subject: [PATCH] efi/libstub: arm*: Pass latest memory map to the kernel Date: Fri, 9 Dec 2016 18:24:03 +0000 Message-Id: <1481307843-18521-1-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.7.4 Sender: linux-efi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org As reported by James, the current libstub code involving the annotated memory map only works somewhat correctly by accident, due to the fact that a pool allocation happens to be reused immediately, retaining its former contents. Instead of juggling memory maps, which makes the code more complex than it needs to be, simply put a placholder value into the FDT, and only write the actual value after ExitBootServices() has been called. Reported-by: James Morse Signed-off-by: Ard Biesheuvel --- drivers/firmware/efi/libstub/fdt.c | 51 ++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 19 deletions(-) -- 2.7.4 -- To unsubscribe from this list: send the line "unsubscribe linux-efi" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/firmware/efi/libstub/fdt.c b/drivers/firmware/efi/libstub/fdt.c index a6a93116a8f0..5d39dff77f17 100644 --- a/drivers/firmware/efi/libstub/fdt.c +++ b/drivers/firmware/efi/libstub/fdt.c @@ -101,7 +101,7 @@ efi_status_t update_fdt(efi_system_table_t *sys_table, void *orig_fdt, if (status) goto fdt_set_fail; - fdt_val64 = cpu_to_fdt64((u64)(unsigned long)memory_map); + fdt_val64 = U64_MAX; /* placeholder */ status = fdt_setprop(fdt, node, "linux,uefi-mmap-start", &fdt_val64, sizeof(fdt_val64)); if (status) @@ -148,6 +148,24 @@ efi_status_t update_fdt(efi_system_table_t *sys_table, void *orig_fdt, return EFI_LOAD_ERROR; } +static efi_status_t update_fdt_memmap(void *fdt, u64 memmap) +{ + int node = fdt_path_offset(fdt, "/chosen"); + efi_status_t status; + + if (node < 0) + return EFI_LOAD_ERROR; + + memmap = cpu_to_fdt64(memmap); + status = fdt_setprop_inplace(fdt, node, "linux,uefi-mmap-start", + &memmap, sizeof(memmap)); + + if (status) + return EFI_LOAD_ERROR; + + return EFI_SUCCESS; +} + #ifndef EFI_FDT_ALIGN #define EFI_FDT_ALIGN EFI_PAGE_SIZE #endif @@ -243,15 +261,6 @@ efi_status_t allocate_new_fdt_and_exit_boot(efi_system_table_t *sys_table, goto fail; } - /* - * Now that we have done our final memory allocation (and free) - * we can get the memory map key needed for - * exit_boot_services(). - */ - status = efi_get_memory_map(sys_table, &map); - if (status != EFI_SUCCESS) - goto fail_free_new_fdt; - status = update_fdt(sys_table, (void *)fdt_addr, fdt_size, (void *)*new_fdt_addr, new_fdt_size, @@ -266,20 +275,16 @@ efi_status_t allocate_new_fdt_and_exit_boot(efi_system_table_t *sys_table, /* * We need to allocate more space for the new * device tree, so free existing buffer that is - * too small. Also free memory map, as we will need - * to get new one that reflects the free/alloc we do - * on the device tree buffer. + * too small. */ efi_free(sys_table, new_fdt_size, *new_fdt_addr); - sys_table->boottime->free_pool(memory_map); new_fdt_size += EFI_PAGE_SIZE; } else { pr_efi_err(sys_table, "Unable to construct new device tree.\n"); - goto fail_free_mmap; + goto fail_free_new_fdt; } } - sys_table->boottime->free_pool(memory_map); priv.runtime_map = runtime_map; priv.runtime_entry_count = &runtime_entry_count; status = efi_exit_boot_services(sys_table, handle, &map, &priv, @@ -288,6 +293,17 @@ efi_status_t allocate_new_fdt_and_exit_boot(efi_system_table_t *sys_table, if (status == EFI_SUCCESS) { efi_set_virtual_address_map_t *svam; + status = update_fdt_memmap((void *)*new_fdt_addr, + (u64)memory_map); + if (status != EFI_SUCCESS) { + /* + * The kernel won't get far without the memory map, but + * may still be able to print something meaningful so + * return success here. + */ + return EFI_SUCCESS; + } + /* Install the new virtual address map */ svam = sys_table->runtime->set_virtual_address_map; status = svam(runtime_entry_count * desc_size, desc_size, @@ -319,9 +335,6 @@ efi_status_t allocate_new_fdt_and_exit_boot(efi_system_table_t *sys_table, pr_efi_err(sys_table, "Exit boot services failed.\n"); -fail_free_mmap: - sys_table->boottime->free_pool(memory_map); - fail_free_new_fdt: efi_free(sys_table, new_fdt_size, *new_fdt_addr);