From patchwork Sun Feb 20 22:42:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zixuan Wang X-Patchwork-Id: 12752909 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3411CC433EF for ; Sun, 20 Feb 2022 22:42:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245174AbiBTWnC (ORCPT ); Sun, 20 Feb 2022 17:43:02 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:53372 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243751AbiBTWnA (ORCPT ); Sun, 20 Feb 2022 17:43:00 -0500 Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DDC9810FE1 for ; Sun, 20 Feb 2022 14:42:38 -0800 (PST) Received: by mail-pg1-x52b.google.com with SMTP id 75so12707481pgb.4 for ; Sun, 20 Feb 2022 14:42:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=oPA5R1WSU1kKD6bVBGCw5++hf0Sn3ln+ch1niI7ZUvM=; b=YnUQVMFbuzAc9BqsUk9FxnbfS8UvmX/lDPHJ/XW+RZMRncv8gnJbrbSvhfZRyPCxWN FVdESDLLamaZZyd4tN5TvZGH128f5DlOTW9zISVmiopwbJa8NLmca5+fdlS47U3IYjz/ /rMeP/Uyfom/qaeO9o5aYvnjNMp4OKAXtVx/1/o8H+TrxPtPWQmyrn3ezvTB1hV60LqZ OM0kO58XSH+Nl9Cg9AVahTTIZxibF/gj8yDco+9AxAHHHVRjWPP39AcsoI5fQrr/f2Eq 4hzl0WyiIcc2VISqx/igqFLX41UTdEt+TXz/9N1nIQpgW6yiB6hlyXHwspNoZpgb9X3X iT5g== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=oPA5R1WSU1kKD6bVBGCw5++hf0Sn3ln+ch1niI7ZUvM=; b=en4BeBVN1huimWLP/RAVQ7DlWWxfmRrKb7LaI8H//LVvbhOPWIllz5q1wi4SNcWbLJ WIQDl/F1W2LbQvCYDuEn9zngm9a2G2qLsTC7ROpgladHsnGh9/5v6oh1xlHEV/s2BG4g ozM/H6WOwHaAhHQIunWk0ir39Qyx0/E1LDFJVGYYN5J5eXNAi5TLuloRN9/Ix3NYw5p8 d/4XSomz/7XVY7GbYgRaplQUy4gEPAsbvyac2QlqHYniVA8YR3VNRJU0+wAkMl833EgO R8opMMD1v8AnF2N7TSvLAgpZm4X4byW+rdPR88twjg54tsJE1ojw3mFVb4j+PviusbJi urrA== X-Gm-Message-State: AOAM531MSvVD4Zbkbn97YGi4BixMwcdEI1e48HipjDCCNplDWOZ5UnHt KfDcxwMUjFBZ/DEoDoC53/2yBKFcm3Q= X-Google-Smtp-Source: ABdhPJxSV28JK7J7cuPXysHCJM1puMXC77hYskvfqX6VDacNfziD32928xM1LT1fEp9EMQDY5zigUw== X-Received: by 2002:a05:6a00:198e:b0:4e1:52be:877d with SMTP id d14-20020a056a00198e00b004e152be877dmr17607577pfl.78.1645396958064; Sun, 20 Feb 2022 14:42:38 -0800 (PST) Received: from localhost.localdomain (netadmin.ucsd.edu. [137.110.160.224]) by smtp.gmail.com with ESMTPSA id gk17sm605629pjb.15.2022.02.20.14.42.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Feb 2022 14:42:37 -0800 (PST) From: Zixuan Wang To: kvm@vger.kernel.org, pbonzini@redhat.com, drjones@redhat.com Cc: marcorr@google.com, erdemaktas@google.com, rientjes@google.com, seanjc@google.com, brijesh.singh@amd.com, Thomas.Lendacky@amd.com, varad.gautam@suse.com, jroedel@suse.de, bp@suse.de Subject: [kvm-unit-tests PATCH v1 1/3] x86 UEFI: pull UEFI definitions for file operations Date: Sun, 20 Feb 2022 14:42:32 -0800 Message-Id: <20220220224234.422499-2-zxwang42@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220220224234.422499-1-zxwang42@gmail.com> References: <20220220224234.422499-1-zxwang42@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Zixuan Wang Pull kernel's UEFI definitions for file operations. These definitions will enable kvm-unit-tests to read files from the disk image, through UEFI services. Signed-off-by: Zixuan Wang --- lib/linux/efi.h | 82 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 2 deletions(-) diff --git a/lib/linux/efi.h b/lib/linux/efi.h index 455625a..a811bbd 100644 --- a/lib/linux/efi.h +++ b/lib/linux/efi.h @@ -1,5 +1,9 @@ /* SPDX-License-Identifier: GPL-2.0 */ -/* Relevant definitions from linux/efi.h. */ +/* + * Relevant definitions from + * - linux/efi.h + * - drivers/firmware/efi/libstub/efistub.h + */ #ifndef __LINUX_UEFI_H #define __LINUX_UEFI_H @@ -58,7 +62,10 @@ typedef guid_t efi_guid_t; (b) & 0xff, ((b) >> 8) & 0xff, \ (c) & 0xff, ((c) >> 8) & 0xff, d } } -#define ACPI_TABLE_GUID EFI_GUID(0xeb9d2d30, 0x2d88, 0x11d3, 0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d) +#define ACPI_TABLE_GUID EFI_GUID(0xeb9d2d30, 0x2d88, 0x11d3, 0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d) +#define LOADED_IMAGE_PROTOCOL_GUID EFI_GUID(0x5b1b31a1, 0x9562, 0x11d2, 0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b) +#define EFI_FILE_SYSTEM_GUID EFI_GUID(0x964e5b22, 0x6459, 0x11d2, 0x8e, 0x39, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b) +#define EFI_FILE_INFO_ID EFI_GUID(0x09576e92, 0x6d3f, 0x11d2, 0x8e, 0x39, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b) typedef struct { efi_guid_t guid; @@ -416,6 +423,77 @@ struct efi_boot_memmap { unsigned long *buff_size; }; +#define __aligned_u64 u64 __attribute__((aligned(8))) +typedef struct { + u32 revision; + efi_handle_t parent_handle; + efi_system_table_t *system_table; + efi_handle_t device_handle; + void *file_path; + void *reserved; + u32 load_options_size; + void *load_options; + void *image_base; + __aligned_u64 image_size; + unsigned int image_code_type; + unsigned int image_data_type; + efi_status_t (__efiapi *unload)(efi_handle_t image_handle); +} efi_loaded_image_t; + +typedef struct { + u64 size; + u64 file_size; + u64 phys_size; + efi_time_t create_time; + efi_time_t last_access_time; + efi_time_t modification_time; + __aligned_u64 attribute; + efi_char16_t filename[]; +} efi_file_info_t; + +typedef struct efi_file_protocol efi_file_protocol_t; + +struct efi_file_protocol { + u64 revision; + efi_status_t (__efiapi *open) (efi_file_protocol_t *, + efi_file_protocol_t **, + efi_char16_t *, u64, u64); + efi_status_t (__efiapi *close) (efi_file_protocol_t *); + efi_status_t (__efiapi *delete) (efi_file_protocol_t *); + efi_status_t (__efiapi *read) (efi_file_protocol_t *, + unsigned long *, void *); + efi_status_t (__efiapi *write) (efi_file_protocol_t *, + unsigned long, void *); + efi_status_t (__efiapi *get_position)(efi_file_protocol_t *, u64 *); + efi_status_t (__efiapi *set_position)(efi_file_protocol_t *, u64); + efi_status_t (__efiapi *get_info) (efi_file_protocol_t *, + efi_guid_t *, unsigned long *, + void *); + efi_status_t (__efiapi *set_info) (efi_file_protocol_t *, + efi_guid_t *, unsigned long, + void *); + efi_status_t (__efiapi *flush) (efi_file_protocol_t *); +}; + +typedef struct efi_simple_file_system_protocol efi_simple_file_system_protocol_t; + +struct efi_simple_file_system_protocol { + u64 revision; + int (__efiapi *open_volume)(efi_simple_file_system_protocol_t *, + efi_file_protocol_t **); +}; + +#define EFI_FILE_MODE_READ 0x0000000000000001 +#define EFI_FILE_MODE_WRITE 0x0000000000000002 +#define EFI_FILE_MODE_CREATE 0x8000000000000000 + +#define MAX_FILENAME_SIZE 256 + +struct finfo { + efi_file_info_t info; + efi_char16_t filename[MAX_FILENAME_SIZE]; +}; + #define efi_bs_call(func, ...) efi_system_table->boottime->func(__VA_ARGS__) #define efi_rs_call(func, ...) efi_system_table->runtime->func(__VA_ARGS__) From patchwork Sun Feb 20 22:42:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zixuan Wang X-Patchwork-Id: 12752910 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 323F4C433FE for ; Sun, 20 Feb 2022 22:42:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245183AbiBTWnD (ORCPT ); Sun, 20 Feb 2022 17:43:03 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:53478 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243751AbiBTWnC (ORCPT ); Sun, 20 Feb 2022 17:43:02 -0500 Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 644BD11C03 for ; Sun, 20 Feb 2022 14:42:40 -0800 (PST) Received: by mail-pg1-x52b.google.com with SMTP id d16so12698380pgd.9 for ; Sun, 20 Feb 2022 14:42:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=mcKHBB1zPuBm0PoJry2FdYYyxTjDZhYA3Qp7gMq+zSM=; b=T317m3U6f/FfkrgT6zn4pK8Clfw0Dm5kW577Ez7jY+Fdtt5yG4OpRFvSkdkZ8PUc6W MTFv0qkoGL/pLxf/SrO5XllYPcPMtWT1SjRyHDL7eLfayRF0slEjHBZVVoQQdRdQo+0z SvbR8akTDaw6gFIks9KPO+rOrItD6F2HRLe/gJnnkLjrIV+aP+3cosJjo/Xay3KA0dwQ 5e9KKdILQyCVU7RwO2LSnR2KrmmMUuZuaCDf7Rw3ddWqlX1NPH0jKRoZ+xsW8LD0u97p 03rlwprbrmEZHBmrb3kNw3wuGoGdvAEm9FE6xOkIYBqJPdas5+K2uXxTZLzn6YSnBlap 7YPw== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=mcKHBB1zPuBm0PoJry2FdYYyxTjDZhYA3Qp7gMq+zSM=; b=IWu/+wEfWe+6IJ9Yq58EaUk1kUF1/YS0Uu7ufa8rFgqkmvX2TzHo+guhtuR+Y/GB/t XwqOugAJf8uGvWkVuFuTQcW0nHH8B3e2fJceTHQKRlHIuRCdRtd6qcS3AAey7OodtRSL H9tXniW2C4vCSJrwkChwc/Bp0L1kx8EqEeY97Vw9MJDKvnGl1i2znT/9E3OcrBEHZQHw EQouel0G0vxW6V5/RZWLEO3oHUp+JkyZt+a5c9SWARSJOitrQt/xXm5xBIBtfb2rIb5K akhblYU/f/K9gxvlvokxtF8a0gkrcbKkjspSv4ehZL5okFZfMeJ6CKvd2j6t128fQ1Q0 /1cA== X-Gm-Message-State: AOAM531V7/b3CculgZOAog09i3hKTU0iIzby8JopW8KOhMgKbSFuFcr/ bNC124I7MbFN1ZyHuuZ40+Z4fehepJc= X-Google-Smtp-Source: ABdhPJz9LMFf9el9bmYKIzbUiu+vHobXdAD9cadVC6TGx5uX/noKEWvP7NN3Bab4zUO7Co2QLfT1Gg== X-Received: by 2002:a05:6a00:244d:b0:4e0:1f65:d5da with SMTP id d13-20020a056a00244d00b004e01f65d5damr17258965pfj.6.1645396959480; Sun, 20 Feb 2022 14:42:39 -0800 (PST) Received: from localhost.localdomain (netadmin.ucsd.edu. [137.110.160.224]) by smtp.gmail.com with ESMTPSA id gk17sm605629pjb.15.2022.02.20.14.42.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Feb 2022 14:42:38 -0800 (PST) From: Zixuan Wang To: kvm@vger.kernel.org, pbonzini@redhat.com, drjones@redhat.com Cc: marcorr@google.com, erdemaktas@google.com, rientjes@google.com, seanjc@google.com, brijesh.singh@amd.com, Thomas.Lendacky@amd.com, varad.gautam@suse.com, jroedel@suse.de, bp@suse.de Subject: [kvm-unit-tests PATCH v1 2/3] x86 UEFI: read envs from file Date: Sun, 20 Feb 2022 14:42:33 -0800 Message-Id: <20220220224234.422499-3-zxwang42@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220220224234.422499-1-zxwang42@gmail.com> References: <20220220224234.422499-1-zxwang42@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Zixuan Wang This commit enables kvm-unit-tests to get envs from the host through UEFI. Previously, when kvm-unit-tests runs with Multiboot, it gets a kernel binary through QEMU's '-kernel' argument and envs through the '-initrd' argument. But under UEFI, kvm-unit-tests loads an EFI binary from a disk image instead of QEMU's '-kernel' argument. In this case, QEMU rejects the '-initrd' argument without the '-kernel' argument being used, which makes kvm-unit-tests unable to get an initrd through this argument. This commit enables the host to pass the initrd through a file: 1. The host runner script 'x86/efi/run' captures the '-initrd' argument and suppress it from QEMU arguments, so QEMU will not report the error 2. The runner script reads the default initrd and the user-specified initrd to generate an 'ENVS.TXT' file for each EFI test case 3. The kvm-unit-tests EFI set up process reads this 'ENVS.TXT' from the disk image and pass it to 'setup_env()' Signed-off-by: Zixuan Wang --- lib/efi.c | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++ x86/efi/run | 26 ++++++++++- 2 files changed, 151 insertions(+), 1 deletion(-) diff --git a/lib/efi.c b/lib/efi.c index 64cc978..4ddc3a4 100644 --- a/lib/efi.c +++ b/lib/efi.c @@ -8,6 +8,7 @@ */ #include "efi.h" +#include "argv.h" #include #include @@ -96,6 +97,121 @@ static void efi_exit(efi_status_t code) efi_rs_call(reset_system, EFI_RESET_SHUTDOWN, code, 0, NULL); } +static efi_status_t efi_get_volume(efi_handle_t handle, + efi_file_protocol_t **volume) +{ + efi_guid_t loaded_image_protocol = LOADED_IMAGE_PROTOCOL_GUID; + efi_guid_t file_system_protocol = EFI_FILE_SYSTEM_GUID; + efi_loaded_image_t *loaded_image = NULL; + efi_simple_file_system_protocol_t *io; + efi_status_t status; + + status = efi_bs_call(handle_protocol, handle, &loaded_image_protocol, + (void **)&loaded_image); + if (status != EFI_SUCCESS) { + printf("ERROR: failed to handle loaded image"); + goto efi_get_volume_error; + } + + status = efi_bs_call(handle_protocol, loaded_image->device_handle, + &file_system_protocol, (void **)&io); + if (status != EFI_SUCCESS) { + printf("ERROR: failed to handle file system protocol"); + goto efi_get_volume_error; + } + + status = io->open_volume(io, volume); + if (status != EFI_SUCCESS) { + printf("ERROR: failed to open volume"); + goto efi_get_volume_error; + } + + return EFI_SUCCESS; + +efi_get_volume_error: + printf(" error: 0x%lx\n", status); + return EFI_ABORTED; +} + +static efi_status_t efi_read_file(efi_file_protocol_t *volume, + efi_char16_t *file_name, + unsigned long *file_size, char **file_data) +{ + efi_guid_t file_info_guid = EFI_FILE_INFO_ID; + efi_file_protocol_t *file_handle = NULL; + struct finfo file_info; + unsigned long file_info_size; + efi_status_t status; + + status = volume->open(volume, &file_handle, file_name, + EFI_FILE_MODE_READ, 0); + if (status != EFI_SUCCESS) { + printf("ERROR: failed to open file"); + goto efi_read_file_error; + } + + file_info_size = sizeof(file_info); + status = file_handle->get_info(file_handle, &file_info_guid, + &file_info_size, &file_info); + if (status != EFI_SUCCESS) { + printf("ERROR: failed to get file info"); + goto efi_read_file_error; + } + + *file_size = file_info.info.file_size; + status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, *file_size + 1, + (void **)file_data); + if (status != EFI_SUCCESS) { + printf("ERROR: failed allocate buffer"); + goto efi_read_file_error; + } + + status = file_handle->read(file_handle, file_size, *file_data); + if (status != EFI_SUCCESS) { + printf("ERROR: failed to read file data"); + goto efi_read_file_error; + } + + status = file_handle->close(file_handle); + if (status != EFI_SUCCESS) { + printf("ERROR: failed to close file"); + goto efi_read_file_error; + } + + (*file_data)[*file_size] = '\0'; + + return EFI_SUCCESS; + +efi_read_file_error: + /* + * TODO: Current printf does not support wide char (2nd byte of the each + * wide char is always a '\0'), thus only the 1st character is printed. + */ + printf(" file: %ls, error: 0x%lx\n", file_name, status); + return EFI_ABORTED; +} + +static efi_status_t efi_set_up_envs(efi_file_protocol_t *volume) +{ + efi_char16_t file_name[] = L"ENVS.TXT"; + unsigned long file_size; + char *file_data = NULL; + efi_status_t status; + + status = efi_read_file(volume, file_name, &file_size, &file_data); + if (status != EFI_SUCCESS) { + printf("Failed to read file\n"); + goto efi_set_up_envs_error; + } + + setup_env(file_data, (int)file_size); + + return EFI_SUCCESS; + +efi_set_up_envs_error: + return EFI_ABORTED; +} + efi_status_t efi_main(efi_handle_t handle, efi_system_table_t *sys_tab) { int ret; @@ -103,12 +219,22 @@ efi_status_t efi_main(efi_handle_t handle, efi_system_table_t *sys_tab) efi_bootinfo_t efi_bootinfo; efi_system_table = sys_tab; + efi_file_protocol_t *volume = NULL; /* Memory map struct values */ efi_memory_desc_t *map = NULL; unsigned long map_size = 0, desc_size = 0, key = 0, buff_size = 0; u32 desc_ver; + /* Open env and args files */ + status = efi_get_volume(handle, &volume); + if (status != EFI_SUCCESS) { + printf("Failed to get volume\n"); + goto efi_main_error; + } + + efi_set_up_envs(volume); + /* Set up efi_bootinfo */ efi_bootinfo.mem_map.map = ↦ efi_bootinfo.mem_map.map_size = &map_size; diff --git a/x86/efi/run b/x86/efi/run index ac368a5..f4a5930 100755 --- a/x86/efi/run +++ b/x86/efi/run @@ -12,6 +12,7 @@ if [ ! -f config.mak ]; then exit 2 fi source config.mak +source scripts/arch-run.bash : "${EFI_SRC:=$(realpath "$(dirname "$0")/../")}" : "${EFI_UEFI:=/usr/share/ovmf/OVMF.fd}" @@ -43,6 +44,29 @@ fi mkdir -p "$EFI_CASE_DIR" cp "$EFI_SRC/$EFI_CASE.efi" "$EFI_CASE_BINARY" +# Capture -initrd +efi_qemu_args="" +efi_qemu_initrd="" +while [[ -n "$1" ]]; do + case "$1" in + --initrd|-initrd) + shift 1 + efi_qemu_initrd="$1" + shift 1 + ;; + *) + efi_qemu_args+=" $1" + shift 1 + ;; + esac +done + +# Create ENVS file +initrd_create +KVM_UNIT_TESTS_EFI_ENV="$EFI_TEST/$EFI_CASE/ENVS.TXT" +mv "$KVM_UNIT_TESTS_ENV" "$KVM_UNIT_TESTS_EFI_ENV" +[[ -f "$efi_qemu_initrd" ]] && cat "$efi_qemu_initrd" >> "$KVM_UNIT_TESTS_EFI_ENV" + # Run test case with 256MiB QEMU memory. QEMU default memory size is 128MiB. # After UEFI boot up and we call `LibMemoryMap()`, the largest consecutive # memory region is ~42MiB. Although this is sufficient for many test cases to @@ -60,5 +84,5 @@ cp "$EFI_SRC/$EFI_CASE.efi" "$EFI_CASE_BINARY" -net none \ -nographic \ -m 256 \ - "$@" \ + ${efi_qemu_args} \ -smp "$EFI_SMP" From patchwork Sun Feb 20 22:42:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zixuan Wang X-Patchwork-Id: 12752911 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 91D8EC433F5 for ; Sun, 20 Feb 2022 22:42:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245187AbiBTWnE (ORCPT ); Sun, 20 Feb 2022 17:43:04 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:53572 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245184AbiBTWnD (ORCPT ); Sun, 20 Feb 2022 17:43:03 -0500 Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BDC72E089 for ; Sun, 20 Feb 2022 14:42:41 -0800 (PST) Received: by mail-pj1-x1029.google.com with SMTP id ev16-20020a17090aead000b001bc3835fea8so1212956pjb.0 for ; Sun, 20 Feb 2022 14:42:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=HjAl366zu24rAX+PE059gK9rclNP/mCI9CQZBpcrWe4=; b=pTaPhob+fU7TT4b1LBBZixlKSca++H2XCdjaHuEte5tvw8SVM5nR1bX6GURMthwp1y CjIIfjiDbhXT0Qb9Ntai0PFBaKmNYOuzOI1gtDLCnclerl3hwtoXYH8rGcQoqdlgks9T hAVIn3sIozOLa25EOJDVe3oByOzbnPMMaB8QnN1OO1b+eIjVcIBtvhNr4jbDPK8w1Yh/ VAFVumSKqAsTGDIiGOXMIsbcoN0cBnZgz3lm0ebwndlHclLkAPsOJz+wT1iEO/lB+Rk6 jxW8JrHSfK6dfZvtFHSHzkrUM+EL5zWJMOonrgGo100dYx9GgWDJU1zC1OpBJWTIzsZF CiKQ== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=HjAl366zu24rAX+PE059gK9rclNP/mCI9CQZBpcrWe4=; b=fihbr9OfEIb8dQpTrWSutr4fUDJJDxLiN/PvNXs5S8bp7/lpFerG3V8lE8lyfseg2x obc7v5sQIzt6FXXVpct6NhpQAANFW+aevMGM2D903Aww40tYIyeFc26aau4cTUASRr1u yz1Q48Ddw9YwykZq8J+Tnui59IFC/oAyaMuhhrEfpuOuwU6gG2WY0d3pQ/RxjKUg3d0F JkCO/gXI3dPXdF2b4cfWTxsWMdONpuniMQeDwlNTMFNzAha3vcRHzlEs149xAewg16JF bSbV578vg+/K80dp1jIK/PyYhRb/YLgREf7NnBXJDJe36dRGWgY8+J6lDxAv7emGy6cc PPvw== X-Gm-Message-State: AOAM5321U3nYhODIAndTeYL3OaXVPJydcdy8Zh/t3CA3pyy8xefsM9Xa PhV3JrDJx/X6pGm2NvZxV4cB5njHFZk= X-Google-Smtp-Source: ABdhPJzfBJk2AxN+WwuShI3A45bMJbjp5/QiEWAhC4/toJsy3cD1SbmD545iK5FPFsZWCPMBmqsq7g== X-Received: by 2002:a17:902:f54a:b0:14f:47:a452 with SMTP id h10-20020a170902f54a00b0014f0047a452mr16502512plf.87.1645396960928; Sun, 20 Feb 2022 14:42:40 -0800 (PST) Received: from localhost.localdomain (netadmin.ucsd.edu. [137.110.160.224]) by smtp.gmail.com with ESMTPSA id gk17sm605629pjb.15.2022.02.20.14.42.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Feb 2022 14:42:40 -0800 (PST) From: Zixuan Wang To: kvm@vger.kernel.org, pbonzini@redhat.com, drjones@redhat.com Cc: marcorr@google.com, erdemaktas@google.com, rientjes@google.com, seanjc@google.com, brijesh.singh@amd.com, Thomas.Lendacky@amd.com, varad.gautam@suse.com, jroedel@suse.de, bp@suse.de Subject: [kvm-unit-tests PATCH v1 3/3] x86 UEFI: read args from file Date: Sun, 20 Feb 2022 14:42:34 -0800 Message-Id: <20220220224234.422499-4-zxwang42@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220220224234.422499-1-zxwang42@gmail.com> References: <20220220224234.422499-1-zxwang42@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Zixuan Wang This commit enables kvm-unit-tests to get args from the host through UEFI. Previously, when compiled to run under UEFI, kvm-unit-tests could not get args from the host through QEMU's '-append' argument. This is because kvm-unit-tests (under UEFI) does not rely on QEMU's '-kernel' argument, while without which, QEMU rejects the '-append' argument. This commit enables the host to pass the args through a file. This process is similar to the '-initrd' support introduced in the previous commit. Signed-off-by: Zixuan Wang --- lib/efi.c | 24 ++++++++++++++++++++++++ x86/efi/run | 12 +++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/efi.c b/lib/efi.c index 4ddc3a4..dfde788 100644 --- a/lib/efi.c +++ b/lib/efi.c @@ -14,6 +14,7 @@ /* From lib/argv.c */ extern int __argc, __envc; +extern const char *__args; extern char *__argv[100]; extern char *__environ[200]; @@ -212,6 +213,28 @@ efi_set_up_envs_error: return EFI_ABORTED; } +static efi_status_t efi_set_up_args(efi_file_protocol_t *volume) +{ + efi_char16_t file_name[] = L"ARGS.TXT"; + unsigned long file_size; + char *file_data = NULL; + efi_status_t status; + + status = efi_read_file(volume, file_name, &file_size, &file_data); + if (status != EFI_SUCCESS) { + printf("Failed to read file\n"); + goto efi_set_up_envs_error; + } + + __args = file_data; + __setup_args(); + + return EFI_SUCCESS; + +efi_set_up_envs_error: + return EFI_ABORTED; +} + efi_status_t efi_main(efi_handle_t handle, efi_system_table_t *sys_tab) { int ret; @@ -234,6 +257,7 @@ efi_status_t efi_main(efi_handle_t handle, efi_system_table_t *sys_tab) } efi_set_up_envs(volume); + efi_set_up_args(volume); /* Set up efi_bootinfo */ efi_bootinfo.mem_map.map = ↦ diff --git a/x86/efi/run b/x86/efi/run index f4a5930..e02a6e1 100755 --- a/x86/efi/run +++ b/x86/efi/run @@ -44,9 +44,10 @@ fi mkdir -p "$EFI_CASE_DIR" cp "$EFI_SRC/$EFI_CASE.efi" "$EFI_CASE_BINARY" -# Capture -initrd +# Capture -initrd and -append efi_qemu_args="" efi_qemu_initrd="" +efi_qemu_append="" while [[ -n "$1" ]]; do case "$1" in --initrd|-initrd) @@ -54,6 +55,11 @@ while [[ -n "$1" ]]; do efi_qemu_initrd="$1" shift 1 ;; + --append|-append) + shift 1 + efi_qemu_append="$1" + shift 1 + ;; *) efi_qemu_args+=" $1" shift 1 @@ -67,6 +73,10 @@ KVM_UNIT_TESTS_EFI_ENV="$EFI_TEST/$EFI_CASE/ENVS.TXT" mv "$KVM_UNIT_TESTS_ENV" "$KVM_UNIT_TESTS_EFI_ENV" [[ -f "$efi_qemu_initrd" ]] && cat "$efi_qemu_initrd" >> "$KVM_UNIT_TESTS_EFI_ENV" +# Create ARGS file +KVM_UNIT_TESTS_EFI_ARG="$EFI_TEST/$EFI_CASE/ARGS.TXT" +echo -n "$efi_qemu_append" > "$KVM_UNIT_TESTS_EFI_ARG" + # Run test case with 256MiB QEMU memory. QEMU default memory size is 128MiB. # After UEFI boot up and we call `LibMemoryMap()`, the largest consecutive # memory region is ~42MiB. Although this is sufficient for many test cases to