From patchwork Mon Oct 11 12:10:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilias Apalodimas X-Patchwork-Id: 515606 Delivered-To: patch@linaro.org Received: by 2002:ac0:b5cc:0:0:0:0:0 with SMTP id x12csp4383420ime; Mon, 11 Oct 2021 05:10:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxbzStSDZmp62BlAyuIMNncirazm4RP5FhdOAHkZWqRyyEmDjwVLwBjBK8BVHHmj8Q2bH0z X-Received: by 2002:a17:906:7302:: with SMTP id di2mr25664976ejc.409.1633954238402; Mon, 11 Oct 2021 05:10:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633954238; cv=none; d=google.com; s=arc-20160816; b=IKF5510WDMxZgYAb4LhvEZonzp+Ui7kK+XfgzpatnR1bgOM5itQNAfnXEtm5DpsXV8 A+mdAu/tnDEJPaPDrZpi6eTtj5O4xE1NdZmMy94NTCpectk0X7pLXMUAuc3MMH2TRJUP HTyYbxCL7k6v31+CzH9gOStxNLJK/DLo/arwZYgfdzJ8ZBuhDVf0wiHJj9RaGjS/lDm3 6m3FVzLQwixwcM4E+bQdpL6QkxRpAndbaxVd3xmQqpquqGkEIdmrDxJ1Tv7gk8nxppI2 UDDadA7bco8rfU9mb8gzXo6Pto5BH6CxXFJ3qzpPtuw7a4Bzfv6yAO9xQHzV2mTBoU+p 1XyQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature; bh=4LagzVUfoM+3KD8vBNoJrIqMUeJLLsKIGp01WXyo0r4=; b=Ab2uWKyXMsYcfPBB6Gat5YDute4ncOk4Dbgg0J79DzJc+IzPw3tkFmfWjGC5VDzDKS BtiHlibxWH/kLc+V23OM1bwP8e6AeK5TcxAo3QVJhIY5oSuB0TYXtszT8Pz9tEecdZpy EYYV8S79lBfa3y1yKk0gfHSMA6YrXbKaDbGBOEyNs6Fm+ldxfNLNCWbPkCTDuuXx/j0J LDtWIAVtuMbCTAi2dyncErKew+vyW79DFN+ee7DRmZmWvjT78pXROmR1p4SjTvO4wp3H PNKPoFODfIHnLLbFPwJbp8Eyr8xTIiVB6EEo/iWZrUkCuYDlwMaLNwag56flQEC3oilp FV6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=GKOU7yNL; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id e5si10998656edm.9.2021.10.11.05.10.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Oct 2021 05:10:38 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=GKOU7yNL; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 869458316B; Mon, 11 Oct 2021 14:10:32 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="GKOU7yNL"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id F09798314C; Mon, 11 Oct 2021 14:10:30 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-ed1-x529.google.com (mail-ed1-x529.google.com [IPv6:2a00:1450:4864:20::529]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 5F40E83124 for ; Mon, 11 Oct 2021 14:10:27 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=ilias.apalodimas@linaro.org Received: by mail-ed1-x529.google.com with SMTP id ec8so17252591edb.6 for ; Mon, 11 Oct 2021 05:10:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=4LagzVUfoM+3KD8vBNoJrIqMUeJLLsKIGp01WXyo0r4=; b=GKOU7yNLqnUOIRet83cLRwvW1He5I+QNcOnNEp2XTrSj3FDzgjh/eUbW1oAuFtVVGz ajzfd2BB/ijZmbmZOvqUThJIYlr4sC9y57Rh8932PnN6sdN1J0CzSNTomoDvK4bV4bVf 9e6T8MpomI+rLgSVqBfvFZXWCTk5dIuH1/gK3l9Ymf7zueYztOxtMOdLXT3Utlq6TrxH 1+bdTyH1vv+F3IL3w12KgSFh3yHoDeIwYP8bJfCE9TBXRnXI4ztUwM2Pgw8o4UZGXCHs DbruB4dx72du/7Cx763klVZY4jR7eGAvdkfwbyq79sF/lBEpl95s883gvwdKxOpeQJh6 uk5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=4LagzVUfoM+3KD8vBNoJrIqMUeJLLsKIGp01WXyo0r4=; b=ztuEK/xlVxyoJ+7qEKDdzwfT7em5N5Kt9hvSixTEbvflJj+mi5Hia6nI9dKSG5vUmb /H6aAGf2e/MDjQ/ORcO+z41JGHAJ0v3RfwPEC0vQ2mtuuXdwJEZcGxBaYXLtrmWfoWm+ n6w5jh6efj88FKrF/2G68G4UYlngrsaA/0YW8C4tbc1OE59qUjFZMHgfeZ8/gbha6FBv iRM0QB6ap8MH6ewiZ/ZZH3XqXQooRtmO1d90VzC5DKqfp5QBFTNanHKlncIcurphfWWt uy0WWV4CB/cb+WuXiL1vaTWFz2gpBFn8Qmtn8urqJbTl4g+LTivrY1nWC+vwKzI/y1r5 VeBw== X-Gm-Message-State: AOAM530S0EwpyLsHtaMou2aVX18lgxcfW2A7bR2K5P9vaVUfphuAHtDL LGlVMlI8/hHaNvboDJx0BCknHQ== X-Received: by 2002:a17:906:8d7:: with SMTP id o23mr24981009eje.193.1633954226812; Mon, 11 Oct 2021 05:10:26 -0700 (PDT) Received: from apalos.home ([2a02:587:468e:477f:2e56:dcff:fe9a:8f06]) by smtp.gmail.com with ESMTPSA id v19sm3505648ejx.26.2021.10.11.05.10.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Oct 2021 05:10:26 -0700 (PDT) From: Ilias Apalodimas To: xypron.glpk@gmx.de Cc: ardb@kernel.org, Vincent.Stehle@arm.com, Ilias Apalodimas , Alexander Graf , u-boot@lists.denx.de Subject: [PATCH] efi_loader: Fix loaded image alignment Date: Mon, 11 Oct 2021 15:10:23 +0300 Message-Id: <20211011121023.110372-1-ilias.apalodimas@linaro.org> X-Mailer: git-send-email 2.33.0 MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 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" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean We are ignoring the alignment communicated via the PE/COFF header. Starting 5.10 the Linux kernel will loudly complain about it. For more details look at [1] (in linux kernel). So add a function that can allocate aligned EFI memory and use it for our relocated loaded image. [1] c32ac11da3f83 ("efi/libstub: arm64: Double check image alignment at entry") Signed-off-by: Ilias Apalodimas --- include/efi_loader.h | 2 ++ lib/efi_loader/efi_image_loader.c | 12 ++++---- lib/efi_loader/efi_memory.c | 50 +++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 6 deletions(-) -- 2.33.0 Reviewed-by: Heinrich Schuchardt Acked-by: Ard Biesheuvel diff --git a/include/efi_loader.h b/include/efi_loader.h index c440962fe522..5cdc72345e52 100644 --- a/include/efi_loader.h +++ b/include/efi_loader.h @@ -675,6 +675,8 @@ struct efi_device_path *efi_get_dp_from_boot(const efi_guid_t guid); #define efi_size_in_pages(size) (((size) + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT) /* Generic EFI memory allocator, call this to get memory */ void *efi_alloc(uint64_t len, int memory_type); +/* Allocate pages on the specified alignment */ +void *efi_alloc_aligned_pages(u64 len, int memory_type, size_t align); /* More specific EFI memory allocator, called by EFI payloads */ efi_status_t efi_allocate_pages(enum efi_allocate_type type, enum efi_memory_type memory_type, diff --git a/lib/efi_loader/efi_image_loader.c b/lib/efi_loader/efi_image_loader.c index e9572d4d5dbb..eb95580538cc 100644 --- a/lib/efi_loader/efi_image_loader.c +++ b/lib/efi_loader/efi_image_loader.c @@ -898,9 +898,9 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle, image_base = opt->ImageBase; efi_set_code_and_data_type(loaded_image_info, opt->Subsystem); handle->image_type = opt->Subsystem; - virt_size = ALIGN(virt_size, opt->SectionAlignment); - efi_reloc = efi_alloc(virt_size, - loaded_image_info->image_code_type); + efi_reloc = efi_alloc_aligned_pages(virt_size, + loaded_image_info->image_code_type, + opt->SectionAlignment); if (!efi_reloc) { log_err("Out of memory\n"); ret = EFI_OUT_OF_RESOURCES; @@ -914,9 +914,9 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle, image_base = opt->ImageBase; efi_set_code_and_data_type(loaded_image_info, opt->Subsystem); handle->image_type = opt->Subsystem; - virt_size = ALIGN(virt_size, opt->SectionAlignment); - efi_reloc = efi_alloc(virt_size, - loaded_image_info->image_code_type); + efi_reloc = efi_alloc_aligned_pages(virt_size, + loaded_image_info->image_code_type, + opt->SectionAlignment); if (!efi_reloc) { log_err("Out of memory\n"); ret = EFI_OUT_OF_RESOURCES; diff --git a/lib/efi_loader/efi_memory.c b/lib/efi_loader/efi_memory.c index f4acbee4f9b6..49d36d369aaa 100644 --- a/lib/efi_loader/efi_memory.c +++ b/lib/efi_loader/efi_memory.c @@ -549,6 +549,56 @@ efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages) return ret; } +/** + * efi_alloc_aligned_pages - allocate + * + * @len len in bytes + * @memory_type usage type of the allocated memory + * @align alignment in bytes + * Return: aligned memory or NULL + */ +void *efi_alloc_aligned_pages(u64 len, int memory_type, size_t align) +{ + u64 req_pages = efi_size_in_pages(len); + u64 true_pages = req_pages + efi_size_in_pages(align) - 1; + u64 free_pages = 0; + u64 aligned_mem; + efi_status_t r; + u64 mem; + + if (align & (align - 1)) + return NULL; + + if (true_pages < req_pages) + return NULL; + + if (align < EFI_PAGE_SIZE) { + r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, + req_pages, &mem); + return (r == EFI_SUCCESS) ? (void *)mem : NULL; + } + + r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, + true_pages, &mem); + if (r != EFI_SUCCESS) + return NULL; + + aligned_mem = ALIGN(mem, align); + /* Free pages before alignment */ + free_pages = efi_size_in_pages(aligned_mem - mem); + if (free_pages) + efi_free_pages(mem, free_pages); + + /* Free trailing pages */ + free_pages = true_pages - (req_pages + free_pages); + if (free_pages) { + mem = aligned_mem + req_pages * EFI_PAGE_SIZE; + efi_free_pages(mem, free_pages); + } + + return (void *)aligned_mem; +} + /** * efi_allocate_pool - allocate memory from pool *