Skip to content

Python UEFI

Jayaprakash Nevara edited this page Sep 17, 2024 · 3 revisions

Python UEFI stands for Python interpreter for UEFI shell. This is based on the standard C-Python version 3.6.8. This project aims at bringing the power of Python scripting to UEFI shell so that Python programming can be leveraged for Tools, and solutions development for debug, validation, and automation of x86/x64 Silicon, Firmware, and Platforms from UEFI shell.

The goal of this project is to help the validation, debug, and automation engineers to provide modern python programming language support from UEFI shell. Thereby reducing the dependency on the UEFI application development for various tasks performed from UEFI shell. Python UEFI is a game-changing capability bringing modernization to the way we work from UEFI. This project leverages the standard C library port of UEFI available in this repo.

Open Source Projects Dependent on Python UEFI

Building Python UEFI Interpreter using Visual Studio Tool Chain(s)

1. Introduction

This is a Best-Known Methods document capturing the details of build environment setup for compiling the Python UEFI interpreter version 3.6.8 using Microsoft Visual Studio Tool chains on Windows 11 systems. These BKMs have been tested on Windows 11 with VS2019 and VS2022 tool chains.

2. Pre-requisites

a. Install the Microsoft Visual Studio tool chain VS2022 community version by downloading it from here. You could also use VS2019 for building Python UEFI interpreter.

b. Install the latest version of NASM assembler for X64 bit platforms by downloading it from here.

c. Install the ASL tools by downloading it from Intel's website. Unzip the iasl-win-*.zip folder, create a folder with name ASL in C:\ drive as C:\ASL\, then copy all the contents from the unzipped folder to C:\ASL\.

d. Install the latest version of Python by downloading it from here. While installing Python, click on customize installation, click next, check the Add Python installation path to Path environment variable, modify the installation path to c:\python312\, then click install.

e. Install git bash or any other git source code management tools you are comfortable with. You may download the git bash for windows from this website.

3. Setup Edk2 Build Environment

a. Open a windows command prompt (not PowerShell) and create a folder with name src and change your working folder to the newly created folder using the commands provided below:

   mkdir src
   cd src

b. Clone the edk2 repo and initialize all the submodule within it using the commands provided below:

   git clone https://github.com/tianocore/edk2.git
   cd edk2
   git submodule update --init

c. Run the below command to build the BaseTools:

   python -m pip install -r pip-requirements.txt --upgrade
   python BaseTools\Edk2ToolsBuild.py -t VS2022

4. Setup Build Environment to Build Python UEFI Interpreter

a. Change your working directory to src folder using the following command:

   cd ..

b. Clone edk2-libc repo using the following command:

   git clone https://github.com/tianocore/edk2-libc.git

c. Set the PACKAGES_PATH and EDK2_LIBC_PATH using the commands below:

   set PACKAGES_PATH=%cd%\edk2;%cd%\edk2-libc
   set EDK2_LIBC_PATH=%cd%\edk2-libc

5. Build Python UEFI Interpreter with VS2022 compiler

You may use the commands provided below to build the Python UEFI interpreter using Visual Studio 2022 compiler tool chain.

   cd edk2
   edksetup.bat
   cd ..
   cd edk2-libc\AppPkg\Applications\Python\Python-3.6.8\
   python srcprep.py
   cd ..\..\..\..\..\edk2

Use one of the below command to build Python UEFI interpreter

   build -t VS2022 -a X64 -p ../edk2-libc/AppPkg/AppPkg.dsc -m ../edk2-libc/AppPkg/Applications/Python/Python-3.6.8/Python368.inf  -b RELEASE -D BUILD_PYTHON368
                                                     or
   build -t VS2022 -a X64 -b RELEASE -p ../edk2-libc/AppPkg/AppPkg.dsc -D BUILD_PYTHON368

6. Create a Deployable Python UEFI package

To create a usable Python UEFI package with all the dependencies from the build environment, you can use the batch (.bat) create_python_pkg.bat script available under edk2-libc/AppPkg/Applications/Python/Python-3.6.8 folder.

Ensure that EDK2_LIBC_PATH environment variable set to edk2-libc folder path using the below command:

   echo %EDK2_LIBC_PATH%

Then use the commands provided below to create a Python UEFI package:

   cd ..\edk2-libc\AppPkg\Applications\Python\Python-3.6.8
   create_python_pkg.bat VS2022 RELEASE X64 myPyUEFI

This creates a Python UEFI package at src\edk2\myPyUEFI.

In the above command:

  • VS2022 refers to the tool chain.
  • RELEASE refers to the build type.
  • X64 refers to the architecture.
  • myPyUEFI refers to the name of the folder for the Python UEFI package.

When you run the create_python_pkg.bat without any parameters, it lists the help information as shown below:

Batch Script to create Python EFI Package.

Usage: ./create_python_pkg.sh

Where

  • ToolChain : name of the tool chain such as VS2019 / VS2022
  • Target : build type such as RELEASE, DEBUG
  • Architecture : Architecture such as X64
  • OutFolder : Output directory for creating the package

7. Usage of Python UEFI package from UEFI shell

  1. Connect a USB thumb drive to your development system and format it with FAT32 file system.
  2. Copy the EFI <OutFolder> (Ex: myPyUEFI) folder to a USB thumb drive, ensure that EFI folder is available at the root of the thumb drive.
  3. Eject the thumb drive from your development system and connect/insert the thumb drive to a system under test (SUT) and boot the SUT to UEFI shell.
  4. Switch to the right file system such as FS0: or FS1: and so on depending on how your USB thumb drive has been enumerated by the shell.
  5. Start using the Python interpreter from UEFI shell by executing the Python command from shell to get into interactive interpreter mode of Python, or execute Python <NameOfPythonScript> to execute the Python script from UEFI shell.

Alternatively, you could create a FAT32 partition on your system and copy the contents of the EFI folder from the above package to this partition. This partition will be visible to the UEFI shell as a file system such as FS1/FS2 depending on how the UEFI environment enumerates.

8. Pre-build Python UEFI interpreter package from edk2-libc repo

The pre-built Python UEFI interpreter packages are available at: