From nobody Wed Nov 27 08:29:01 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1700061400; cv=none; d=zohomail.com; s=zohoarc; b=fUgR6KNWBmgpkKl7pImhBQQChvS2N0NEHV5tY8GDIRuEos0yZNPknzuo0u6zh4q12h7eyqqMNDZZV8sL8nTntpIKMG2rxK8/Rr2azHwDS2jlRNGSC9oFdUOyPptyy9lu1XmsXM4bskKPS5UDTkHfKGbNsLiRMcFMUCNuFx2IzV8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1700061400; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Jd2IXpciXzQV/Ndug0FShFZVrniKjWvZCrz9CPP4QT4=; b=mM4mDOrUyp9Q6W63c2w4v8H/viXz9K5k/8MqPTsHLfDLJ/nhx0BypYbqA92Wu1ESP1SLVZLxmQzEMZzNGLfV5bUMMBCaBjBfjOLKCPkWOCrhQUjBBRgEx/6lqg8Mfau919uPXUXqjCp7qSlzIs82bji6C3uHSavGLQIvC3VPvOE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1700061400496411.98602599568176; Wed, 15 Nov 2023 07:16:40 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r3HaV-0004Eh-5n; Wed, 15 Nov 2023 10:13:55 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1r3HZe-0003tz-OF for qemu-devel@nongnu.org; Wed, 15 Nov 2023 10:13:05 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1r3HZW-0003F5-QY for qemu-devel@nongnu.org; Wed, 15 Nov 2023 10:12:59 -0500 Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-231-XjJnpZOWPG6JiEAxwf3Qzw-1; Wed, 15 Nov 2023 10:12:47 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 459853810D23; Wed, 15 Nov 2023 15:12:47 +0000 (UTC) Received: from sirius.home.kraxel.org (unknown [10.39.192.56]) by smtp.corp.redhat.com (Postfix) with ESMTPS id CB6CDC1596F; Wed, 15 Nov 2023 15:12:46 +0000 (UTC) Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id A7228180AC0F; Wed, 15 Nov 2023 16:12:42 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700061169; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Jd2IXpciXzQV/Ndug0FShFZVrniKjWvZCrz9CPP4QT4=; b=XlNRI4kxmA2wrbWwGAp+76mLSEa6NexO1PyHmy6LLkIFu51Shi/sosq0eb8y9edE/TAoyF P0cnGi4BBdIesqZ3HELtqTGHRKym/mn8wpoc6nY6DntY99QlmdD7/KPTvkbT/vXm24UbwM IVb3/PIILAXYB5obiUwKZKINGLSdjwI= X-MC-Unique: XjJnpZOWPG6JiEAxwf3Qzw-1 From: Gerd Hoffmann To: qemu-devel@nongnu.org Cc: qemu-arm@nongnu.org, Eric Blake , Thomas Huth , Michael Roth , Paolo Bonzini , Peter Maydell , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?L=C3=A1szl=C3=B3=20=C3=89rsek?= , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , graf@amazon.com, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Markus Armbruster , Gerd Hoffmann Subject: [PATCH 05/16] hw/uefi: add var-service-core.c Date: Wed, 15 Nov 2023 16:12:27 +0100 Message-ID: <20231115151242.184645-6-kraxel@redhat.com> In-Reply-To: <20231115151242.184645-1-kraxel@redhat.com> References: <20231115151242.184645-1-kraxel@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=kraxel@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.099, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1700061400841100013 Content-Type: text/plain; charset="utf-8" This is the core code for guest <-> host communication. This accepts request messages from the guest, dispatches them to the service called, and sends back the response message. Signed-off-by: Gerd Hoffmann --- hw/uefi/var-service-core.c | 350 +++++++++++++++++++++++++++++++++++++ 1 file changed, 350 insertions(+) create mode 100644 hw/uefi/var-service-core.c diff --git a/hw/uefi/var-service-core.c b/hw/uefi/var-service-core.c new file mode 100644 index 000000000000..b37f5c403d2f --- /dev/null +++ b/hw/uefi/var-service-core.c @@ -0,0 +1,350 @@ +/* + * SPDX-License-Identifier: GPL-2.0-or-later + * + * uefi vars device + */ +#include "qemu/osdep.h" +#include "sysemu/dma.h" +#include "migration/vmstate.h" + +#include "hw/uefi/var-service.h" +#include "hw/uefi/var-service-api.h" +#include "hw/uefi/var-service-edk2.h" + +#include "trace/trace-hw_uefi.h" + +static int uefi_vars_pre_load(void *opaque) +{ + uefi_vars_state *uv =3D opaque; + + uefi_vars_clear_all(uv); + uefi_vars_policies_clear(uv); + g_free(uv->buffer); + return 0; +} + +static int uefi_vars_post_load(void *opaque, int version_id) +{ + uefi_vars_state *uv =3D opaque; + + uefi_vars_update_storage(uv); + uv->buffer =3D g_malloc(uv->buf_size); + return 0; +} + +const VMStateDescription vmstate_uefi_vars =3D { + .name =3D "uefi-vars", + .pre_load =3D uefi_vars_pre_load, + .post_load =3D uefi_vars_post_load, + .fields =3D (VMStateField[]) { + VMSTATE_UINT16(sts, uefi_vars_state), + VMSTATE_UINT32(buf_size, uefi_vars_state), + VMSTATE_UINT32(buf_addr_lo, uefi_vars_state), + VMSTATE_UINT32(buf_addr_hi, uefi_vars_state), + VMSTATE_BOOL(end_of_dxe, uefi_vars_state), + VMSTATE_BOOL(ready_to_boot, uefi_vars_state), + VMSTATE_BOOL(exit_boot_service, uefi_vars_state), + VMSTATE_BOOL(policy_locked, uefi_vars_state), + VMSTATE_UINT64(used_storage, uefi_vars_state), + VMSTATE_QTAILQ_V(variables, uefi_vars_state, 0, + vmstate_uefi_variable, uefi_variable, next), + VMSTATE_QTAILQ_V(var_policies, uefi_vars_state, 0, + vmstate_uefi_var_policy, uefi_var_policy, next), + VMSTATE_END_OF_LIST() + }, +}; + +size_t uefi_strlen(const uint16_t *str, size_t len) +{ + size_t pos =3D 0; + + for (;;) { + if (pos =3D=3D len) { + return pos; + } + if (str[pos] =3D=3D 0) { + return pos; + } + pos++; + } +} + +gboolean uefi_str_equal(const uint16_t *a, size_t alen, + const uint16_t *b, size_t blen) +{ + size_t pos =3D 0; + + alen =3D alen / 2; + blen =3D blen / 2; + for (;;) { + if (pos =3D=3D alen && pos =3D=3D blen) { + return true; + } + if (pos =3D=3D alen && b[pos] =3D=3D 0) { + return true; + } + if (pos =3D=3D blen && a[pos] =3D=3D 0) { + return true; + } + if (pos =3D=3D alen || pos =3D=3D blen) { + return false; + } + if (a[pos] =3D=3D 0 && b[pos] =3D=3D 0) { + return true; + } + if (a[pos] !=3D b[pos]) { + return false; + } + pos++; + } +} + +char *uefi_ucs2_to_ascii(const uint16_t *ucs2, uint64_t ucs2_size) +{ + char *str =3D g_malloc0(ucs2_size / 2 + 1); + int i; + + for (i =3D 0; i * 2 < ucs2_size; i++) { + if (ucs2[i] =3D=3D 0) { + break; + } + if (ucs2[i] < 128) { + str[i] =3D ucs2[i]; + } else { + str[i] =3D '?'; + } + } + str[i] =3D 0; + return str; +} + +void uefi_trace_variable(const char *action, QemuUUID guid, + const uint16_t *name, uint64_t name_size) +{ + QemuUUID be =3D qemu_uuid_bswap(guid); + char *str_uuid =3D qemu_uuid_unparse_strdup(&be); + char *str_name =3D uefi_ucs2_to_ascii(name, name_size); + + trace_uefi_variable(action, str_name, name_size, str_uuid); + + g_free(str_name); + g_free(str_uuid); +} + +void uefi_trace_status(const char *action, efi_status status) +{ + switch (status) { + case EFI_SUCCESS: + trace_uefi_status(action, "success"); + break; + case EFI_INVALID_PARAMETER: + trace_uefi_status(action, "invalid parameter"); + break; + case EFI_UNSUPPORTED: + trace_uefi_status(action, "unsupported"); + break; + case EFI_BAD_BUFFER_SIZE: + trace_uefi_status(action, "bad buffer size"); + break; + case EFI_BUFFER_TOO_SMALL: + trace_uefi_status(action, "buffer too small"); + break; + case EFI_WRITE_PROTECTED: + trace_uefi_status(action, "write protected"); + break; + case EFI_OUT_OF_RESOURCES: + trace_uefi_status(action, "out of resources"); + break; + case EFI_NOT_FOUND: + trace_uefi_status(action, "not found"); + break; + case EFI_ACCESS_DENIED: + trace_uefi_status(action, "access denied"); + break; + case EFI_ALREADY_STARTED: + trace_uefi_status(action, "already started"); + break; + default: + trace_uefi_status(action, "unknown error"); + break; + } +} + +static uint32_t uefi_vars_cmd_mm(uefi_vars_state *uv) +{ + hwaddr dma; + mm_header *mhdr; + uint32_t size, retval; + + dma =3D uv->buf_addr_lo | ((hwaddr)uv->buf_addr_hi << 32); + mhdr =3D (mm_header *) uv->buffer; + + if (!uv->buffer || uv->buf_size < sizeof(*mhdr)) { + return UEFI_VARS_STS_ERR_BAD_BUFFER_SIZE; + } + + /* read header */ + dma_memory_read(&address_space_memory, dma, + uv->buffer, sizeof(*mhdr), + MEMTXATTRS_UNSPECIFIED); + + size =3D sizeof(*mhdr) + mhdr->length; + if (uv->buf_size < size) { + return UEFI_VARS_STS_ERR_BAD_BUFFER_SIZE; + } + + /* read buffer (excl header) */ + dma_memory_read(&address_space_memory, dma + sizeof(*mhdr), + uv->buffer + sizeof(*mhdr), mhdr->length, + MEMTXATTRS_UNSPECIFIED); + memset(uv->buffer + size, 0, uv->buf_size - size); + + /* dispatch */ + if (qemu_uuid_is_equal(&mhdr->guid, &EfiSmmVariableProtocolGuid)) { + retval =3D uefi_vars_mm_vars_proto(uv); + + } else if (qemu_uuid_is_equal(&mhdr->guid, &VarCheckPolicyLibMmiHandle= rGuid)) { + retval =3D uefi_vars_mm_check_policy_proto(uv); + + } else if (qemu_uuid_is_equal(&mhdr->guid, &EfiEndOfDxeEventGroupGuid)= ) { + trace_uefi_event("end-of-dxe"); + uv->end_of_dxe =3D true; + retval =3D UEFI_VARS_STS_SUCCESS; + + } else if (qemu_uuid_is_equal(&mhdr->guid, &EfiEventReadyToBootGuid)) { + trace_uefi_event("ready-to-boot"); + uv->ready_to_boot =3D true; + retval =3D UEFI_VARS_STS_SUCCESS; + + } else if (qemu_uuid_is_equal(&mhdr->guid, &EfiEventExitBootServicesGu= id)) { + trace_uefi_event("exit-boot-service"); + uv->exit_boot_service =3D true; + retval =3D UEFI_VARS_STS_SUCCESS; + + } else { + retval =3D UEFI_VARS_STS_ERR_NOT_SUPPORTED; + } + + /* write buffer */ + dma_memory_write(&address_space_memory, dma, + uv->buffer, sizeof(*mhdr) + mhdr->length, + MEMTXATTRS_UNSPECIFIED); + + return retval; +} + +static void uefi_vars_soft_reset(uefi_vars_state *uv) +{ + g_free(uv->buffer); + uv->buffer =3D NULL; + uv->buf_size =3D 0; + uv->buf_addr_lo =3D 0; + uv->buf_addr_hi =3D 0; +} + +void uefi_vars_hard_reset(uefi_vars_state *uv) +{ + trace_uefi_hard_reset(); + uefi_vars_soft_reset(uv); + + uv->end_of_dxe =3D false; + uv->ready_to_boot =3D false; + uv->exit_boot_service =3D false; + uv->policy_locked =3D false; + + uefi_vars_clear_volatile(uv); + uefi_vars_policies_clear(uv); + uefi_vars_auth_init(uv); +} + +static uint32_t uefi_vars_cmd(uefi_vars_state *uv, uint32_t cmd) +{ + switch (cmd) { + case UEFI_VARS_CMD_RESET: + uefi_vars_soft_reset(uv); + return UEFI_VARS_STS_SUCCESS; + case UEFI_VARS_CMD_MM: + return uefi_vars_cmd_mm(uv); + default: + return UEFI_VARS_STS_ERR_NOT_SUPPORTED; + } +} + +static uint64_t uefi_vars_read(void *opaque, hwaddr addr, unsigned size) +{ + uefi_vars_state *uv =3D opaque; + uint64_t retval =3D -1; + + trace_uefi_reg_read(addr, size); + + switch (addr) { + case UEFI_VARS_REG_MAGIC: + retval =3D UEFI_VARS_MAGIC_VALUE; + break; + case UEFI_VARS_REG_CMD_STS: + retval =3D uv->sts; + break; + case UEFI_VARS_REG_BUFFER_SIZE: + retval =3D uv->buf_size; + break; + case UEFI_VARS_REG_BUFFER_ADDR_LO: + retval =3D uv->buf_addr_lo; + break; + case UEFI_VARS_REG_BUFFER_ADDR_HI: + retval =3D uv->buf_addr_hi; + break; + } + return retval; +} + +static void uefi_vars_write(void *opaque, hwaddr addr, uint64_t val, unsig= ned size) +{ + uefi_vars_state *uv =3D opaque; + + trace_uefi_reg_write(addr, val, size); + + switch (addr) { + case UEFI_VARS_REG_CMD_STS: + uv->sts =3D uefi_vars_cmd(uv, val); + break; + case UEFI_VARS_REG_BUFFER_SIZE: + if (val > MAX_BUFFER_SIZE) { + val =3D MAX_BUFFER_SIZE; + } + uv->buf_size =3D val; + g_free(uv->buffer); + uv->buffer =3D g_malloc(uv->buf_size); + break; + case UEFI_VARS_REG_BUFFER_ADDR_LO: + uv->buf_addr_lo =3D val; + break; + case UEFI_VARS_REG_BUFFER_ADDR_HI: + uv->buf_addr_hi =3D val; + break; + } +} + +static const MemoryRegionOps uefi_vars_ops =3D { + .read =3D uefi_vars_read, + .write =3D uefi_vars_write, + .endianness =3D DEVICE_LITTLE_ENDIAN, + .impl =3D { + .min_access_size =3D 2, + .max_access_size =3D 4, + }, +}; + +void uefi_vars_init(Object *obj, uefi_vars_state *uv) +{ + QTAILQ_INIT(&uv->variables); + QTAILQ_INIT(&uv->var_policies); + uv->jsonfd =3D -1; + memory_region_init_io(&uv->mr, obj, &uefi_vars_ops, uv, + "uefi-vars", UEFI_VARS_REGS_SIZE); +} + +void uefi_vars_realize(uefi_vars_state *uv, Error **errp) +{ + uefi_vars_json_init(uv, errp); + uefi_vars_json_load(uv, errp); +} --=20 2.41.0