From patchwork Fri Jun 7 18:52:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 802341 Delivered-To: patch@linaro.org Received: by 2002:adf:f147:0:b0:35b:5a80:51b4 with SMTP id y7csp950160wro; Fri, 7 Jun 2024 11:58:53 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXLDiJVNTjdFeGUTHeaxOSWU4hBH/skfx+LGqTK41U6eqq31iVL44DXJgd7wFCWZKWY1atjCbEPxFSxY5ioOnPt X-Google-Smtp-Source: AGHT+IHQcGLSCAcb0NfVgrIwFpXCN3V0Hm+/0sGTxRwGyCocdp07OPOz/SzRRMPWanEDtIyY2xvm X-Received: by 2002:a17:906:80c2:b0:a68:fa24:9e82 with SMTP id a640c23a62f3a-a6cd7891d6emr220017466b.38.1717786733072; Fri, 07 Jun 2024 11:58:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1717786733; cv=none; d=google.com; s=arc-20160816; b=Gmewrp/d6wFPaaKrkdFjhwH4w9r+Zm3jbH0yH+UwLz76swxUmynRq3JPOc9IIWFB4T LrfQECoAHyYlhsaQKm20HeoJon1s+dSOFTzlWI/vmuZYnVEvOqttSVpQjVRTW3YPq++0 sPF9a8Ah0vcp+rq+22nv/z0JmNYhw3y7usFITtkWTzvY/R8ovs1d+w65+kKrBezC5O0x BxdkkTq1xXeBPq0ugKyVqJuc6848ZPPu7siSDgPDNl3FNwx8oEAHjllEQNa6fkFS0TAs RucV8koGT+YO6P4JojHagXL3u5P8QkWnoGeAomUglq+EeIwXCfmNsVFMmHVSb5QTbaTz fZIA== 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:references:in-reply-to:message-id:date:subject:cc:to :from; bh=MG2PhwcoeBzAbLq14VFgSza7/oYE8MjuUCZqvw7nOwY=; fh=xLSOOz5Q0Mrmzd+lwBRTYS7D/8TpUWM8TEHgIjpMzow=; b=qrOF79TBfe4f2VlAm/uDh0K/zm41ky/tjpKHb9bM1suN+UOFFgmpt3OEiw3DCoyyoZ x+0OkPu12IINj1SWONHYGvAFCjoHDPPzQfuH23DQsxaQZD0MnKuN9If4pxfN7TFyFdWi Zt6bCbESewvkKzXXzc1bvhj1JLqt4k4HpFK1brYVuSy56Znq4r6vogHjZsPfeZM8S9Z0 h395iJk2tGj2k919Fj2eksApEd2JLYQWUC2Hiwn4uU+kSEcnB4i7ZOvC+S4uhPrfg6IR TL9mNlfY3kIFkId5JzU1itzkRGMw2gad5eLHTgARb6BetVwp19gcI8PrGyoaiPdQJKL/ cflQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id a640c23a62f3a-a6c807283dasi204706866b.734.2024.06.07.11.58.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 11:58:53 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (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 6ACE588578; Fri, 7 Jun 2024 20:55:14 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by phobos.denx.de (Postfix, from userid 109) id EA5F38855C; Fri, 7 Jun 2024 20:55:12 +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=-1.2 required=5.0 tests=BAYES_00, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, SPF_HELO_NONE,SPF_SOFTFAIL,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by phobos.denx.de (Postfix) with ESMTP id E39D388578 for ; Fri, 7 Jun 2024 20:55:09 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=fail smtp.mailfrom=sughosh.ganu@linaro.org Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id C8AE31480; Fri, 7 Jun 2024 11:55:33 -0700 (PDT) Received: from a079122.blr.arm.com (a079122.arm.com [10.162.17.48]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id AEC303F792; Fri, 7 Jun 2024 11:55:06 -0700 (PDT) From: Sughosh Ganu To: u-boot@lists.denx.de Cc: Tom Rini , Ilias Apalodimas , Heinrich Schuchardt , Simon Glass , Marek Vasut , Mark Kettenis , Fabio Estevam , Sughosh Ganu Subject: [RFC PATCH 30/31] temp: cmd: efi_mem: add a command to test efi alloc/free Date: Sat, 8 Jun 2024 00:22:39 +0530 Message-Id: <20240607185240.1892031-31-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240607185240.1892031-1-sughosh.ganu@linaro.org> References: <20240607185240.1892031-1-sughosh.ganu@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 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.8 at phobos.denx.de X-Virus-Status: Clean Not for committing. Signed-off-by: Sughosh Ganu --- cmd/Makefile | 1 + cmd/efi_memory.c | 155 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 cmd/efi_memory.c diff --git a/cmd/Makefile b/cmd/Makefile index 87133cc27a..35fcc4af5a 100644 --- a/cmd/Makefile +++ b/cmd/Makefile @@ -10,6 +10,7 @@ obj-$(CONFIG_CMD_BOOTM) += bootm.o obj-y += help.o obj-y += panic.o obj-y += version.o +obj-y += efi_memory.o # command obj-$(CONFIG_CMD_ARMFFA) += armffa.o diff --git a/cmd/efi_memory.c b/cmd/efi_memory.c new file mode 100644 index 0000000000..52ddcb7146 --- /dev/null +++ b/cmd/efi_memory.c @@ -0,0 +1,155 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Allocate and Free EFI memory + * + * Copyright (c) 2024 Linaro Limited + */ + +#include +#include +#include +#include + +#include + +static int do_efi_mem_free(struct cmd_tbl *cmdtp, int flag, int argc, + char * const argv[]) +{ + uint64_t addr = 0, size = 0; + efi_uintn_t pages; + efi_status_t status; + + if (argc != 3) + return CMD_RET_USAGE; + + argc--; argv++; + + size = simple_strtoul(argv[0], NULL, 16); + if (!size) { + printf("Enter valid size for free in Hex\n"); + return CMD_RET_USAGE; + } + + + addr = simple_strtoul(argv[1], NULL, 16); + if (!addr) { + printf("Enter a valid address in Hex\n"); + return CMD_RET_USAGE; + } + + pages = efi_size_in_pages(size + (addr & EFI_PAGE_MASK)); + + status = efi_free_pages(addr, pages); + if (status != EFI_SUCCESS) { + printf("Unable to free memory, error (%#lx)\n", status); + return CMD_RET_FAILURE; + } + + return CMD_RET_SUCCESS; +} + +static int do_efi_mem_alloc(struct cmd_tbl *cmdtp, int flag, int argc, + char * const argv[]) +{ + enum efi_allocate_type type; + uint64_t addr = 0, size = 0; + efi_uintn_t pages; + efi_status_t status; + bool max = false; + + if (argc < 2) + return CMD_RET_USAGE; + + argc--; argv++; + + if (!strcmp("max", argv[0])) { + if (argc != 3) + return CMD_RET_USAGE; + + max = true; + argv++; + argc--; + } + + size = simple_strtoul(argv[0], NULL, 16); + if (!size) { + printf("Enter valid size for allocation in Hex\n"); + return CMD_RET_USAGE; + } + + if (max || argc == 2) { + addr = simple_strtoul(argv[1], NULL, 16); + if (!addr) { + printf("Enter a valid address in Hex\n"); + return CMD_RET_USAGE; + } + } + + if (max) + type = EFI_ALLOCATE_MAX_ADDRESS; + else if (addr) + type = EFI_ALLOCATE_ADDRESS; + else + type = EFI_ALLOCATE_ANY_PAGES; + + pages = efi_size_in_pages(size + (addr & EFI_PAGE_MASK)); + status = efi_allocate_pages(type, EFI_BOOT_SERVICES_DATA, pages, + &addr); + if (status != EFI_SUCCESS) { + printf("efi_allocate_pages failed %lx\n", status); + return CMD_RET_FAILURE; + } else { + printf("Address returned %#llx\n", addr); + } + + return CMD_RET_SUCCESS; +} + +static struct cmd_tbl cmd_efi_mem_sub[] = { + U_BOOT_CMD_MKENT(alloc, 3, 0, do_efi_mem_alloc, + "", ""), + U_BOOT_CMD_MKENT(free, 2, 0, do_efi_mem_free, + "", ""), +}; + +static int do_efi_mem(struct cmd_tbl *cmdtp, int flag, int argc, + char *const argv[]) +{ + struct cmd_tbl *cp; + efi_status_t r; + + if (argc < 3) + return CMD_RET_USAGE; + + argc--; argv++; + + /* Initialize UEFI subsystem */ + r = efi_init_obj_list(); + if (r != EFI_SUCCESS) { + printf("Error: Cannot initialize UEFI sub-system, r = %lu\n", + r & ~EFI_ERROR_MASK); + return CMD_RET_FAILURE; + } + + cp = find_cmd_tbl(argv[0], cmd_efi_mem_sub, + ARRAY_SIZE(cmd_efi_mem_sub)); + if (!cp) + return CMD_RET_USAGE; + + return cp->cmd(cmdtp, flag, argc, argv); +} + +U_BOOT_LONGHELP(efi_mem, + "Functions to allocate and free memory\n" + "\n" + "efi_mem alloc [addr]\n" + "efi_mem alloc max \n" + "efi_mem free \n" + "\n" +); + +U_BOOT_CMD( + efi_mem, CONFIG_SYS_MAXARGS, 0, do_efi_mem, + "Allocate and free EFI memory", + efi_mem_help_text +);