Skip to content

Python UEFI Samples

Jayaprakash Nevara edited this page Sep 11, 2024 · 10 revisions

This wiki page provides sample code to use Python UEFI provided capabilities to interact with the underlying UEFI platform. Use Python UEFI to read certain platform registers such as PCIe configuration space, Module Specific Registers (MSRs), IO Port registers, MMIO registers, CPUIDinstructions execution and etc.

Executing CPUID Instructions:

Python UEFI has a built in module known as edk2 module which provides APIs to perform the basic register read/write operations.

Signature of cpuid function: cpuid(eax, ecx) -> (eax, ebx, ecx, edx) Depending on the input values EAX and ECX the cpuid function returns different information about the processor.

  1. The following code snippet demonstrates how to read the CPUID of the given platform from UEFI shell using Python script. import edk2

eax=0x1 and ecx=0 => result in getting the CPUID of the platform

eax=0x01 ecx=0x00 x=edk2.cpuid(eax, ecx) print(hex(x[0]))

  1. The following code snippet demonstrates how to read vendor id string of the given platform from UEFI shell using Python script.

import edk2

utility function to convert 32 bit hexadecimal data into bytes data type

def hex2bytes(data): b=bytearray() for count in range(4): d = data & 0xff b.append(d) data = data >> 8 return b

eax=0 and ecx=0 => result in getting the CPU vendor id string

eax=0x00 ecx=0x00 x=edk2.cpuid(eax, ecx)

x[1] is EBX value

data = hex2bytes(x[1])

x[3] is EDX value

data += hex2bytes(x[3])

x[2] is ECX value

data += hex2bytes(x[2])

print(data) vendor_string = [chr(item) for item in data]

print("Vendor String = {}".format(vendor_string))

This page will be updated with more sample code

Clone this wiki locally