From nobody Mon Apr 29 23:48:16 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+100776+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+100776+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1678165940; cv=none; d=zohomail.com; s=zohoarc; b=iuhmdrzYgYLKhMFERFaR8lQAh0fdlDt99iBQ1fdmqZKPece8Am7qP/HBeWgZnXuBWg1SSh7/IDR03K19jjPd/iAwFhmhN0BfPjgUM0GzBV2W+KOJQF6cpssf1sknzUhZ++nShErbEDvWi2ihAfP9t0zmNs9AnCT/EGVjzfvm4pk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678165940; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=Pelo4lYij3AkC90q2S8IqQ10fzeOX4srhReEecOz6aU=; b=FawMfHakb+l+GN580yhwTuvO4lC0ebh2AkM6jb4qRKZSZzbIibx+RLZnHaQVRFDYo+R7Wt6KiOED8DL4BBuMIQm2RR6G37zWN18XxknW17w8frV2s4bosYdjUmOX+Bno9SDLMc55mHxvJIzvvTK1YB/Z5cK6dtAxe37USTPJ8ng= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+100776+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1678165940140375.19287818303167; Mon, 6 Mar 2023 21:12:20 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id uLNBYY1788612xzPBb88LJV1; Mon, 06 Mar 2023 21:12:19 -0800 X-Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by mx.groups.io with SMTP id smtpd.web11.7830.1678165938772555047 for ; Mon, 06 Mar 2023 21:12:19 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10641"; a="334476357" X-IronPort-AV: E=Sophos;i="5.98,238,1673942400"; d="scan'208";a="334476357" X-Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Mar 2023 21:12:18 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10641"; a="740598877" X-IronPort-AV: E=Sophos;i="5.98,238,1673942400"; d="scan'208";a="740598877" X-Received: from akadapat-mobl1.gar.corp.intel.com ([10.215.201.155]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Mar 2023 21:12:16 -0800 From: "Ajay Kadapathri" To: devel@edk2.groups.io Cc: Rebecca Cran , Michael D Kinney , Jayaprakash N Subject: [edk2-devel] [edk2-libc Patch 1/2] Python/Python3.6.8: migration of edk2module from chipsec repo Date: Tue, 7 Mar 2023 10:41:49 +0530 Message-Id: <20230307051150.772-2-ajay.kadapathri@intel.com> In-Reply-To: <20230307051150.772-1-ajay.kadapathri@intel.com> References: <20230307051150.772-1-ajay.kadapathri@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,ajay.kadapathri@intel.com X-Gm-Message-State: Y9H8K8rg6FhLPThV20yOOmWax1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1678165939; bh=zj01kB5KllOxn/lP6YoE3LzsaOvmWXST7pgj9emzqGE=; h=Cc:Date:From:Reply-To:Subject:To; b=hCRmByXRhFVSLWMimZwleXih2UspJIdqrosCtSYVGrfvd3lbXlbxu43XqZLszfUVoxt fYAyLc0Lpeb7r5ABcDRrvr+7Qt/3SeZ+TVn9+8Hs0Dv7c418cSDxF3KYf4xzIt269Ciay Ds4s7FfBbt24PqqpexZH20Svr42vYnMD2bM= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1678165941276100001 Content-Type: text/plain; charset="utf-8" REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D4161 This patch merges the enhancements done by chipsec tool to edk2 module into edk2-libc repo. The chipsec enhancements can be used for various other Python based tools to access platform registers. These enhancements providing a set of APIs to access the platform registers directly from the python scripts running on UEFI shell. This will benefit the Python users on UEFI shell in general and enhances it's usability. Python can be used effectively to implement tools, scripts required for automation, debug from UEFI shell. Cc: Rebecca Cran Cc: Michael D Kinney Cc: Jayaprakash N Signed-off-by: Ajay Kadapathri --- .../Python-3.6.8/PyMod-3.6.8/Modules/cpu.nasm | 48 ++ .../PyMod-3.6.8/Modules/cpu_gcc.s | 49 ++ .../PyMod-3.6.8/Modules/cpu_ia32.nasm | 20 + .../PyMod-3.6.8/Modules/cpu_ia32_gcc.s | 22 + .../PyMod-3.6.8/Modules/edk2module.c | 521 +++++++++++++++++- .../Python/Python-3.6.8/Python368.inf | 5 + 6 files changed, 663 insertions(+), 2 deletions(-) create mode 100644 AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Mod= ules/cpu.nasm create mode 100644 AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Mod= ules/cpu_gcc.s create mode 100644 AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Mod= ules/cpu_ia32.nasm create mode 100644 AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Mod= ules/cpu_ia32_gcc.s diff --git a/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Modules/cp= u.nasm b/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Modules/cpu.na= sm new file mode 100644 index 0000000..2d4f561 --- /dev/null +++ b/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Modules/cpu.nasm @@ -0,0 +1,48 @@ +;TITLE cpu.asm: Assembly code for the x64 resources + +DEFAULT REL +SECTION .text + +;-------------------------------------------------------------------------= ----- +; void +; _swsmi ( +; unsigned int smi_code_data // rcx +; IN UINT64 rax_value // rdx +; IN UINT64 rbx_value // r8 +; IN UINT64 rcx_value // r9 +; IN UINT64 rdx_value // rsp + 0x28 +; IN UINT64 rsi_value // rsp + 0x30 +; IN UINT64 rdi_value // rsp + 0x38 +; ) +;-------------------------------------------------------------------------= ----- +global ASM_PFX(_swsmi) +ASM_PFX(_swsmi): + push rbx + push rsi + push rdi + + ; rsp - 0x18 + + ; setting up GPR (arguments) to SMI handler call + ; notes: + ; RAX will get partially overwritten (AX) by _smi_code_data (which i= s passed in RCX) + ; RDX will get partially overwritten (DX) by the value of APMC port = (=3D 0x00B2) + mov rax, rdx ; rax_value + mov ax, cx ; smi_code_data + mov rdx, r10 ; rdx_value + mov rdx, [rsp + 040h] ; rsp + 0x28 + 0x18 + + mov rbx, r8 ; rbx_value + mov rcx, r9 ; rcx_value + mov rsi, [rsp + 048h] ; rsi_value + mov rdi, [rsp + 050h] ; rdi_value + + ; this OUT instruction will write WORD value (smi_code_data) to ports = 0xB2 and 0xB3 (SW SMI control and data ports) + out 0B2h, ax + + ; @TODO: some SM handlers return data/errorcode in GPRs, need to retur= n this to the caller + + pop rdi + pop rsi + pop rbx + ret \ No newline at end of file diff --git a/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Modules/cp= u_gcc.s b/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Modules/cpu_g= cc.s new file mode 100644 index 0000000..5fc536e --- /dev/null +++ b/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Modules/cpu_gcc.s @@ -0,0 +1,49 @@ +# cpu_gcc.s: Assembly code for the x64 resources (GCC) + +.global _swsmi + +.intel_syntax noprefix +.text + +#-------------------------------------------------------------------------= ----- +# void +# _swsmi ( +# unsigned int smi_code_data // rcx +# IN UINT64 rax_value // rdx +# IN UINT64 rbx_value // r8 +# IN UINT64 rcx_value // r9 +# IN UINT64 rdx_value // rsp + 0x28 +# IN UINT64 rsi_value // rsp + 0x30 +# IN UINT64 rdi_value // rsp + 0x38 +# ) +#-------------------------------------------------------------------------= ----- +_swsmi: + push rbx + push rsi + push rdi + + # rsp - 0x18 + + # setting up GPR (arguments) to SMI handler call + # notes: + # RAX will get partially overwritten (AX) by _smi_code_data (which i= s passed in RCX) + # RDX will get partially overwritten (DX) by the value of APMC port = (=3D 0x00B2) + mov rax, rdx # rax_value + mov ax, cx # smi_code_data + mov rdx, r10 # rdx_value + mov rdx, [rsp + 0x040] # rsp + 0x28 + 0x18 + + mov rbx, r8 # rbx_value + mov rcx, r9 # rcx_value + mov rsi, [rsp + 0x048] # rsi_value + mov rdi, [rsp + 0x050] # rdi_value + + # this OUT instruction will write WORD value (smi_code_data) to ports = 0xB2 and 0xB3 (SW SMI control and data ports) + out 0x0B2, ax + + # @TODO: some SM handlers return data/errorcode in GPRs, need to retur= n this to the caller + + pop rdi + pop rsi + pop rbx + ret \ No newline at end of file diff --git a/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Modules/cp= u_ia32.nasm b/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Modules/c= pu_ia32.nasm new file mode 100644 index 0000000..71e2e2c --- /dev/null +++ b/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Modules/cpu_ia32.= nasm @@ -0,0 +1,20 @@ +;TITLE cpu.asm: Assembly code for the i386 resources + +SECTION .text + +;-------------------------------------------------------------------------= ----- +; void +; _swsmi ( +; unsigned int smi_code_data // rcx +; IN UINT32 rax_value // rdx +; IN UINT32 rbx_value // r8 +; IN UINT32 rcx_value // r9 +; IN UINT32 rdx_value // r10 +; IN UINT32 rsi_value // r11 +; IN UINT32 rdi_value // r12 +; ) +;-------------------------------------------------------------------------= ----- +global ASM_PFX(_swsmi) +ASM_PFX(_swsmi): + xor eax, eax + ret \ No newline at end of file diff --git a/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Modules/cp= u_ia32_gcc.s b/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Modules/= cpu_ia32_gcc.s new file mode 100644 index 0000000..77e307d --- /dev/null +++ b/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Modules/cpu_ia32_= gcc.s @@ -0,0 +1,22 @@ + +.global _swsmi + + +.intel_syntax noprefix +.text + +#-------------------------------------------------------------------------= ----- +# void +# _swsmi ( +# unsigned int smi_code_data // rcx +# IN UINT32 rax_value // rdx +# IN UINT32 rbx_value // r8 +# IN UINT32 rcx_value // r9 +# IN UINT32 rdx_value // r10 +# IN UINT32 rsi_value // r11 +# IN UINT32 rdi_value // r12 +# ) +#-------------------------------------------------------------------------= ----- +_swsmi: + xor eax, eax + ret diff --git a/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Modules/ed= k2module.c b/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Modules/ed= k2module.c index 0501a2b..a0e6273 100644 --- a/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Modules/edk2modul= e.c +++ b/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Modules/edk2modul= e.c @@ -3,7 +3,7 @@ Derived from posixmodule.c in Python 2.7.2. =20 Copyright (c) 2015, Daryl McDaniel. All rights reserved.
- Copyright (c) 2011 - 2021, Intel Corporation. All rights reserved.
+ Copyright (c) 2011 - 2023, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made avai= lable under the terms and conditions of the BSD License that accompanies this dist= ribution. The full text of the license may be found at @@ -23,12 +23,20 @@ #include #include #include +#include +#include #include =20 #ifdef __cplusplus extern "C" { #endif =20 +PyTypeObject EfiGuidType; + +extern void _swsmi( unsigned int smi_code_data, unsigned int rax_value, un= signed int rbx_value, unsigned int rcx_value, unsigned int rdx_value, unsig= ned int rsi_value, unsigned int rdi_value ); +// -- Support routines +EFI_STATUS GuidToStr( IN EFI_GUID *guid, IN OUT UINT8 *str_buffer ); + PyDoc_STRVAR(edk2__doc__, "This module provides access to UEFI firmware functionality t= hat is\n\ standardized by the C Standard and the POSIX standard (a thin= ly\n\ @@ -3800,6 +3808,499 @@ edk2_abort(PyObject *self, PyObject *noargs) return NULL; } =20 +unsigned int ReadPCICfg( + unsigned char bus, + unsigned char dev, + unsigned char fun, + unsigned char off, + unsigned char len // 1, 2, 4 bytes + ) +{ + unsigned int result =3D 0; + + if (1 =3D=3D len) result =3D PciRead8(PCI_LIB_ADDRESS(bus, dev, fun, off= )); + else if (2 =3D=3D len) result =3D PciRead16(PCI_LIB_ADDRESS(bus, dev, fu= n, off)); + else if (4 =3D=3D len) result =3D PciRead32(PCI_LIB_ADDRESS(bus, dev, fu= n, off)); + return result; +} + +void WritePCICfg( + unsigned char bus, + unsigned char dev, + unsigned char fun, + unsigned char off, + unsigned char len, // 1, 2, 4 bytes + unsigned int val + ) +{ + if (1 =3D=3D len) PciWrite8(PCI_LIB_ADDRESS(bus, dev, fun, off), (val & = 0xFF)); + else if (2 =3D=3D len) PciWrite16(PCI_LIB_ADDRESS(bus, dev, fun, off), (= val & 0xFFFF)); + else if (4 =3D=3D len) PciWrite32(PCI_LIB_ADDRESS(bus, dev, fun, off), v= al); +} + +PyDoc_STRVAR(efi_rdmsr__doc__, +"rdmsr(ecx) -> (eax,edx)\n\ +Read the given MSR."); + +static PyObject * +edk2_rdmsr(PyObject *self, PyObject *args) +{ + unsigned int vecx, veax, vedx; + UINT64 data =3D 0; + if (!PyArg_ParseTuple(args, "I", &vecx)) + return NULL; + Py_BEGIN_ALLOW_THREADS + data =3D AsmReadMsr64(vecx); + Py_END_ALLOW_THREADS + veax =3D (UINT32)data; + vedx =3D (UINT64)data >> 32; + return Py_BuildValue("(II)", (unsigned long)veax, (unsigned long)vedx); +} + +PyDoc_STRVAR(efi_wrmsr__doc__, +"wrmsr(ecx, eax, edx) -> None\n\ +Write edx:eax to the given MSR."); + +static PyObject * +edk2_wrmsr(PyObject *self, PyObject *args) +{ + unsigned int vecx, veax, vedx; + UINT64 data =3D 0; + if (!PyArg_ParseTuple(args, "III", &vecx, &veax, &vedx)) + return NULL; + data =3D vedx << 32 | veax; + Py_BEGIN_ALLOW_THREADS + AsmWriteMsr64(vecx, data); + Py_END_ALLOW_THREADS + Py_INCREF(Py_None); + return Py_None; +} + +PyDoc_STRVAR(efi_swsmi__doc__, +"swsmi(smi_code_data, rax_value, rbx_value, rcx_value, rdx_value, rsi_valu= e, rdi_value) -> None\n\ +Triggering Software SMI"); + +static PyObject * +posix_swsmi(PyObject *self, PyObject *args) +{ + unsigned int smi_code_data, rax_value, rbx_value, rcx_value, rdx_value, = rsi_value, rdi_value; + if (!PyArg_Parse(args, "(IIIIIII)", &smi_code_data, &rax_value, &rbx_val= ue, &rcx_value, &rdx_value, &rsi_value, &rdi_value)) + return NULL; + Py_BEGIN_ALLOW_THREADS + _swsmi( smi_code_data, rax_value, rbx_value, rcx_value, rdx_value, rsi_v= alue, rdi_value ); + Py_END_ALLOW_THREADS + Py_INCREF(Py_None); + return Py_None; +} + +PyDoc_STRVAR(efi_cpuid__doc__, +"cpuid(eax, ecx) -> (eax:ebx:ecx:edx)\n\ +Read the CPUID.";); + +static PyObject * +edk2_cpuid(PyObject *self, PyObject *args) +{ + UINT32 eax, ecx, rax_value, rbx_value, rcx_value, rdx_value; + if (!PyArg_ParseTuple(args, "II", &eax, &ecx)) + return NULL; + Py_BEGIN_ALLOW_THREADS + AsmCpuidEx( eax, ecx, &rax_value, &rbx_value, &rcx_value, &rdx_value); + Py_END_ALLOW_THREADS + return Py_BuildValue("(IIII))", (unsigned long)rax_value, (unsigned = long)rbx_value, (unsigned long)rcx_value, (unsigned long)rdx_value); +} + +PyDoc_STRVAR(efi_allocphysmem__doc__, +"allocphysmem(length, max_pa) -> (va)\n\ +Use malloc to allocate space in memory.";); + +static PyObject * +posix_allocphysmem(PyObject *self, PyObject *args) +{ + unsigned int length, max_pa; + void *va; + if (!PyArg_ParseTuple(args, "II", &length, &max_pa)) + return NULL; + =20 + Py_BEGIN_ALLOW_THREADS + va =3D malloc(length); + Py_END_ALLOW_THREADS + + // return Py_BuildValue("(K)", (unsigned long)va); + return Py_BuildValue("(I)", (unsigned long)va); +} + +PyDoc_STRVAR(efi_readio__doc__, +"readio(addr, size) -> (int)\n\ +Read the value (size =3D=3D 1, 2, or 4 bytes) of the specified IO port."); + +static PyObject * +edk2_readio(PyObject *self, PyObject *args) +{ + unsigned int addr, sz, result; + short addrs; + + if (!PyArg_ParseTuple(args, "II", &addr, &sz)) + return NULL; + + Py_BEGIN_ALLOW_THREADS + result =3D 0; + addrs =3D (short)(addr & 0xffff); + if (1 =3D=3D sz) result =3D (IoRead8(addrs) & 0xFF); + else if (2 =3D=3D sz) result =3D (IoRead16(addrs) & 0xFFFF); + else if (4 =3D=3D sz) result =3D IoRead32(addrs); + Py_END_ALLOW_THREADS + return PyLong_FromUnsignedLong((unsigned long)result); +} + +PyDoc_STRVAR(efi_writeio__doc__, +"writeio(addr, size, value) -> None\n\ +Write the value (size =3D=3D 1, 2, or 4 bytes) of the specified IO port."); + +static PyObject * +edk2_writeio(PyObject *self, PyObject *args) +{ + unsigned int addr, sz, value; + short addrs; + + if (!PyArg_ParseTuple(args, "III", &addr, &sz, &value)) + return NULL; + + Py_BEGIN_ALLOW_THREADS + addrs =3D (short)(addr & 0xffff); + if (1 =3D=3D sz) IoWrite8((unsigned char)(value & 0xFF), addrs); + else if (2 =3D=3D sz) IoWrite16((unsigned short)(value & 0xFFFF), addrs); + else if (4 =3D=3D sz) IoWrite32(value, addrs); + Py_END_ALLOW_THREADS + + Py_INCREF(Py_None); + return Py_None; +} + +PyDoc_STRVAR(efi_readpci__doc__, +"readpci(bus,dev,func,addr,size) -> (int)\n\ +Read the value (size =3D=3D 1, 2, or 4 bytes) of the specified PCI b/d/f."= ); + +static PyObject * +edk2_readpci(PyObject *self, PyObject *args) +{ + unsigned int bus, dev, func, off, sz, result; + + if (!PyArg_ParseTuple(args, "IIIII", &bus, &dev, &func, &off, &sz)) + return NULL; + + Py_BEGIN_ALLOW_THREADS + result =3D ReadPCICfg( bus, dev, func, off, sz ); + Py_END_ALLOW_THREADS + + return PyLong_FromUnsignedLong((unsigned long)result); +} + +PyDoc_STRVAR(efi_writepci__doc__, +"writepci(bus,dev,func,addr,value,len) -> None\n\ +Write the value to the specified PCI b/d/f. Len is value size (either 1, = 2, or 4 bytes)."); + +static PyObject * +edk2_writepci(PyObject *self, PyObject *args) +{ + unsigned int bus, dev, func, off, val, len; + + if (!PyArg_ParseTuple(args, "IIIIII", &bus, &dev, &func, &off, &val, &le= n)) + return NULL; + + Py_BEGIN_ALLOW_THREADS + WritePCICfg( bus, dev, func, off, len, val ); + Py_END_ALLOW_THREADS + + Py_INCREF(Py_None); + return Py_None; +} + +PyDoc_STRVAR(efi_readmem__doc__, +"readmem(addr_lo, addr_hi, len) -> ByteString\n\ +Read the given memory address."); + +static PyObject * +posix_readmem(PyObject *self, PyObject *args) +{ + PyObject *data; + UINT32 addr_lo, addr_hi; + char *buffer, *cbuffer, *addr; + int len, index; + + if (!PyArg_ParseTuple(args, "III", &addr_lo, &addr_hi, &len)) + return NULL; + +#ifdef MDE_CPU_X64 + addr =3D (unsigned char*)((UINT64)addr_lo | ((UINT64)addr_hi << 32)); +#else + addr =3D (unsigned char*)addr_lo; +#endif + + buffer =3D malloc(len); + if (buffer =3D=3D NULL) + return NULL; + + cbuffer =3D buffer; + index =3D len; + + Py_BEGIN_ALLOW_THREADS + + while(index--){ + *cbuffer =3D *addr; + cbuffer++; + addr++; + } + + Py_END_ALLOW_THREADS + =20 + data =3D Py_BuildValue("y#", buffer, len); + free(buffer); + + return data; +} + +PyDoc_STRVAR(efi_readmem_dword__doc__, +"readmem_dword(addr_lo, addr_hi) -> (int32)\n\ +Read the given memory address and return 32-bit value."); + +static PyObject * +posix_readmem_dword(PyObject *self, PyObject *args) +{ + unsigned int result, *addr; + UINT32 addr_lo, addr_hi; + + if (!PyArg_ParseTuple(args, "II", &addr_lo, &addr_hi)) + return NULL; + +#ifdef MDE_CPU_X64 + addr =3D (unsigned int*)((UINT64)addr_lo | ((UINT64)addr_hi << 32)); +#else + addr =3D (unsigned int*)addr_lo; +#endif + + Py_BEGIN_ALLOW_THREADS + result =3D *addr; + Py_END_ALLOW_THREADS + + return PyLong_FromUnsignedLong((unsigned long)result); +} + +PyDoc_STRVAR(efi_writemem__doc__, +"writemem(addr_lo, addr_hi, buf) -> None\n\ +Write the buf (PyString) to the given memory address."); + +static PyObject * +posix_writemem(PyObject *self, PyObject *args) +{ + char *buf, *addr; + int len; + UINT32 addr_lo, addr_hi; + + if (!PyArg_ParseTuple(args, "IIs#", &addr_lo, &addr_hi, &buf, &len)) + return NULL; + +#ifdef MDE_CPU_X64 + addr =3D (unsigned char*)((UINT64)addr_lo | ((UINT64)addr_hi << 32)); +#else + addr =3D (unsigned char*)addr_lo; +#endif + + Py_BEGIN_ALLOW_THREADS + while(len--){ + *addr =3D *buf; + buf++; + addr++; + } + Py_END_ALLOW_THREADS + + Py_INCREF(Py_None); + return Py_None; +} + +PyDoc_STRVAR(efi_writemem_dword__doc__, +"writemem_dword(addr_lo, addr_hi, val) -> None\n\ +Write the 32-bit value to the given memory address."); + +static PyObject * +posix_writemem_dword(PyObject *self, PyObject *args) +{ + unsigned int *addr, val; + UINT32 addr_lo, addr_hi; + + if (!PyArg_ParseTuple(args, "III", &addr_lo, &addr_hi, &val)) + return NULL; + +#ifdef MDE_CPU_X64 + addr =3D (unsigned int*)((UINT64)addr_lo | ((UINT64)addr_hi << 32)); +#else + addr =3D (unsigned int*)addr_lo; +#endif + + Py_BEGIN_ALLOW_THREADS + *addr =3D val; + Py_END_ALLOW_THREADS + + Py_INCREF(Py_None); + return Py_None; +} + +PyDoc_STRVAR(MiscRT_GetVariable__doc__, +"(Status, Attributes, Data, DataSize) =3D GetVariable(VariableName, GUID, = DataSize)\n\n\ +Returns the value of a variable."); + +static +PyObject * +MiscRT_GetVariable(PyObject *self, PyObject *args) +{ + PyObject *data_out; + CHAR16 *VariableName; + EFI_GUID VendorGuid; + UINT32 GuidSize; + UINT32 Attributes; + UINT64 DataSize; + char *Data; + const CHAR16 *GuidIn; + EFI_STATUS Status; + + if(!PyArg_ParseTuple(args, "uu#K", &VariableName, &GuidIn, &GuidSize, &D= ataSize)) + { + return NULL; + } + + StrToGuid(GuidIn, &VendorGuid); + + Data =3D malloc(DataSize); + if (!Data) + return NULL; + + Py_BEGIN_ALLOW_THREADS + Status =3D gRT->GetVariable(VariableName, &VendorGuid, &Attributes, (UIN= TN *)&DataSize, (void*)Data); + Py_END_ALLOW_THREADS + + data_out =3D Py_BuildValue("(IIy#K)", (UINT32)Status, Attributes, Data, = DataSize, DataSize); + free(Data); + + return data_out; +} + +PyDoc_STRVAR(MiscRT_GetNextVariableName__doc__, +"(Status, VariableNameSize, VariableName, VendorGuid) =3D GetNextVariableN= ame(NameSize, VariableName, GUID)\n\n\ +Enumerates the current variable names."); + +static +PyObject * +MiscRT_GetNextVariableName(PyObject *self, PyObject *args) +{ + UINT64 NameSize; + CHAR16 *VariableName, *NameIn; + UINT32 GuidSize, VariableNameSize, i; + EFI_GUID VendorGuid; + EFI_STATUS Status; + const char *GuidIn;=20 + char *VendorGuidPtr, *GuidOut[37]; + + if(!PyArg_ParseTuple(args, "Ky#s#", &NameSize, &NameIn, &VariableNameSiz= e, &GuidIn, &GuidSize)) + { + return NULL; + } + + VendorGuidPtr =3D (char *)&VendorGuid; + for (i=3D0; iGetNextVariableName((UINTN *)&NameSize, (CHAR16 *)Variab= leName, &VendorGuid); + Py_END_ALLOW_THREADS + + GuidToStr((EFI_GUID *)&VendorGuid, (UINT8 *)GuidOut); + + return Py_BuildValue("(IuKs)", (UINT32) Status, VariableName, NameSize, = &GuidOut); +} + +PyDoc_STRVAR(MiscRT_SetVariable__doc__, +"(Status, DataSize, GUID) =3D SetVariable(VariableName, GUID, Attributes, = Data, DataSize)\n\n\ +Sets the value of a variable."); + +static +PyObject * +MiscRT_SetVariable(PyObject *self, PyObject *args) +{ + CHAR16 *VariableName; + UINT64 DataSize; + char *Data, *guidptr, *VendorGuidPtr; + char *GuidOut[37]; + EFI_STATUS Status; + EFI_GUID VendorGuid; + UINT32 Attributes; + UINT32 GuidSize, strDataSize; + const CHAR16 *GuidIn; + + if(!PyArg_ParseTuple(args, "uu#Is#I", &VariableName, &GuidIn, &GuidSize,= &Attributes, &Data, &strDataSize, &DataSize)) + { + return NULL; + } + + StrToGuid(GuidIn, &VendorGuid); + + Py_BEGIN_ALLOW_THREADS + Status =3D gRT->SetVariable(VariableName, &VendorGuid, Attributes, (UINT= N)DataSize, (void*)Data); + Py_END_ALLOW_THREADS + + GuidToStr((EFI_GUID *)&VendorGuid, (UINT8 *)GuidOut); + +// return Py_BuildValue("(IKu#)", (UINT32) Status, DataSize, &VendorGuid= , sizeof(VendorGuid)); + return Py_BuildValue("(IKs)", (UINT32) Status, DataSize, &GuidOut); +} + + + +EFI_STATUS +GuidToStr ( + IN EFI_GUID *guid, + IN OUT UINT8 *str_buffer + ) +/*++ + +Routine Description: + This function prints a GUID to a buffer + +Arguments: + guid - Pointer to a GUID + str_buffer - Pointer to a str buffer + +Returns: + EFI_SUCCESS GUID was printed + EFI_INVALID_PARAMETER GUID was NULL + =20 +--*/ +{ + if (guid =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + sprintf ( + (CHAR8 *)str_buffer, + "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X", + (unsigned) guid->Data1, + guid->Data2, + guid->Data3, + guid->Data4[0], + guid->Data4[1], + guid->Data4[2], + guid->Data4[3], + guid->Data4[4], + guid->Data4[5], + guid->Data4[6], + guid->Data4[7] + ); + + return EFI_SUCCESS; +} + static PyMethodDef edk2_methods[] =3D { {"access", edk2_access, METH_VARARGS, edk2_access__doc__}, #ifdef HAVE_TTYNAME @@ -4057,7 +4558,23 @@ static PyMethodDef edk2_methods[] =3D { #ifdef HAVE_PATHCONF {"pathconf", edk2_pathconf, METH_VARARGS, edk2_pathconf__doc__}, #endif - {"abort", edk2_abort, METH_NOARGS, edk2_abort__doc__}, + {"abort", edk2_abort, METH_NOARGS, edk2_abort__doc= __}, + {"rdmsr", edk2_rdmsr, METH_VARARGS, efi_= rdmsr__doc__}, + {"wrmsr", edk2_wrmsr, METH_VARARGS, efi_= wrmsr__doc__}, + {"readpci", edk2_readpci, METH_VARARGS, efi_= readpci__doc__}, + {"writepci", edk2_writepci, METH_VARARGS, efi_= writepci__doc__}, + {"readmem", posix_readmem, METH_VARARGS, efi= _readmem__doc__}, + {"readmem_dword", posix_readmem_dword, METH_VARARGS, efi= _readmem_dword__doc__}, + {"writemem", posix_writemem, METH_VARARGS, efi= _writemem__doc__}, + {"writemem_dword", posix_writemem_dword, METH_VARARGS, efi= _writemem_dword__doc__}, + {"writeio", edk2_writeio, METH_VARARGS, efi_= writeio__doc__}, + {"readio", edk2_readio, METH_VARARGS, efi_= readio__doc__}, + {"swsmi", posix_swsmi, METH_VARARGS, efi= _swsmi__doc__}, + {"allocphysmem", posix_allocphysmem, METH_VARARGS, efi= _allocphysmem__doc__}, + {"cpuid", edk2_cpuid, METH_VARARGS, efi_= cpuid__doc__}, + {"GetVariable", MiscRT_GetVariable, METH_VARARGS, Mis= cRT_GetVariable__doc__}, + {"GetNextVariableName", MiscRT_GetNextVariableName, METH_VARARGS, Mis= cRT_GetNextVariableName__doc__}, + {"SetVariable", MiscRT_SetVariable, METH_VARARGS, Mis= cRT_SetVariable__doc__}, {NULL, NULL} /* Sentinel */ }; =20 diff --git a/AppPkg/Applications/Python/Python-3.6.8/Python368.inf b/AppPkg= /Applications/Python/Python-3.6.8/Python368.inf index 121d4a9..8e70999 100644 --- a/AppPkg/Applications/Python/Python-3.6.8/Python368.inf +++ b/AppPkg/Applications/Python/Python-3.6.8/Python368.inf @@ -42,6 +42,7 @@ LibGen LibNetUtil DevMedia + PciLib #BsdSocketLib #EfiSocketLib =20 @@ -262,9 +263,13 @@ Modules/zlib =20 [Sources.IA32] Modules/_ctypes/libffi_msvc/win32.c | MSFT # + PyMod-$(PYTHON_VERSION)/Modules/cpu_ia32.nasm | MSFT # + PyMod-$(PYTHON_VERSION)/Modules/cpu_ia32_gcc.s | GCC # =20 [Sources.X64] Modules/_ctypes/libffi_msvc/win64.asm | MSFT # + PyMod-$(PYTHON_VERSION)/Modules/cpu.nasm | MSFT # + PyMod-$(PYTHON_VERSION)/Modules/cpu_gcc.s | GCC # =20 [BuildOptions] MSFT:*_*_*_CC_FLAGS =3D /GL- /Oi- /wd4018 /wd4054 /wd4055 /wd4101 /wd413= 1 /wd4152 /wd4204 /wd4210 /wd4244 /wd4267 /wd4305 /wd4310 /wd4389 /wd4701 /= wd4702 /wd4706 /wd4456 /wd4312 /wd4457 /wd4459 /wd4474 /wd4476 /I$(WORKSPAC= E)\AppPkg\Applications\Python\Python-3.6.8\Include /DHAVE_MEMMOVE /DUSE_PYE= XPAT_CAPI /DXML_STATIC -D UEFI /WX- /DXML_POOR_ENTROPY /DUEFI_C_SOURCE --=20 2.39.1.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#100776): https://edk2.groups.io/g/devel/message/100776 Mute This Topic: https://groups.io/mt/97443895/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Mon Apr 29 23:48:16 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+100777+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+100777+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1678165950; cv=none; d=zohomail.com; s=zohoarc; b=lqe/kq9VqeRREjz7VHvaQ+8CR5TMy6Yg5UbvnWQTkfH270p5RaYiGzpNfwaOSwppng5Um1/Z+nx9JP0xlSOrkoKzXDrShh7nqXc+YiH5XJLid6FkQHZW0V7Rl3hJxgt20Ik4jTdJA/V1Zcjk+iPcwdlXT4PrdkT/Z1UDdoMpDls= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678165950; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=d7HvdLamtSQTQXbBJTKiUZQXQKlDBJ2J29ufXLnZHT8=; b=b7BnM1mov3WRm0T9KBccNj8MxIhkRUpjdisWrlaM9yx15iMiiG3zvz03U3/E7UyWRHhw1mJuYnm6nhTdpvLsv2y87OT03UEJ1pgBtTKyP7UdY2PWw0QahrPp16nBv9xDkSkZMsFjjn9licLApsl2MfVzdiDSnOHZ87hd2ut+ZZk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+100777+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1678165950651193.5889808092611; Mon, 6 Mar 2023 21:12:30 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id 8hgsYY1788612xJqhgSq0Cwz; Mon, 06 Mar 2023 21:12:30 -0800 X-Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by mx.groups.io with SMTP id smtpd.web10.7946.1678165949770901635 for ; Mon, 06 Mar 2023 21:12:29 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10641"; a="334476403" X-IronPort-AV: E=Sophos;i="5.98,238,1673942400"; d="scan'208";a="334476403" X-Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Mar 2023 21:12:29 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10641"; a="740598886" X-IronPort-AV: E=Sophos;i="5.98,238,1673942400"; d="scan'208";a="740598886" X-Received: from akadapat-mobl1.gar.corp.intel.com ([10.215.201.155]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Mar 2023 21:12:28 -0800 From: "Ajay Kadapathri" To: devel@edk2.groups.io Cc: Rebecca Cran , Michael D Kinney , Jayaprakash N Subject: [edk2-devel] [edk2-libc Patch 2/2] Incorporated review comments Date: Tue, 7 Mar 2023 10:41:50 +0530 Message-Id: <20230307051150.772-3-ajay.kadapathri@intel.com> In-Reply-To: <20230307051150.772-1-ajay.kadapathri@intel.com> References: <20230307051150.772-1-ajay.kadapathri@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,ajay.kadapathri@intel.com X-Gm-Message-State: 4Sq5ckF2EocN1hkHQA9IYg90x1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1678165950; bh=MU8Ihi/hYX2n059XnMIP0IAQYgOpEhXXOCHyCyQ1o8M=; h=Cc:Date:From:Reply-To:Subject:To; b=ejdbq6pRA6Nnpj1hlEuj94B/h0uxmE+dgmfRYJAydKun3bdwNumUtYkOIh0+gmeQEyH +s7WWYtArvI1Ru1tqNYT4eGCZN6cH7D0NBPPUNaAa3O6hszSX0N6oDerl548rsrRg2kHY z+VbjO2gthbSTkIhysGxJxf4LI/CQcCvlUg= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1678165951256100001 Content-Type: text/plain; charset="utf-8" Cc: Rebecca Cran Cc: Michael D Kinney Cc: Jayaprakash N Signed-off-by: Ajay Kadapathri --- .../PyMod-3.6.8/Modules/edk2module.c | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Modules/ed= k2module.c b/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Modules/ed= k2module.c index a0e6273..4c400b9 100644 --- a/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Modules/edk2modul= e.c +++ b/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Modules/edk2modul= e.c @@ -4257,26 +4257,24 @@ MiscRT_SetVariable(PyObject *self, PyObject *args) } =20 =20 +/** + This function prints a GUID to a buffer + =20 + @param guid Pointer to a GUID + =20 + @param str_buffer Pointer to a str buffer + =20 + + @retval EFI_SUCCESS GUID was printed + =20 + @retval EFI_INVALID_PARAMETER GUID was NULL =20 +**/ EFI_STATUS GuidToStr ( IN EFI_GUID *guid, IN OUT UINT8 *str_buffer ) -/*++ - -Routine Description: - This function prints a GUID to a buffer - -Arguments: - guid - Pointer to a GUID - str_buffer - Pointer to a str buffer - -Returns: - EFI_SUCCESS GUID was printed - EFI_INVALID_PARAMETER GUID was NULL - =20 ---*/ { if (guid =3D=3D NULL) { return EFI_INVALID_PARAMETER; --=20 2.39.1.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#100777): https://edk2.groups.io/g/devel/message/100777 Mute This Topic: https://groups.io/mt/97443897/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-