8/05/2024

EFI variable library - I

Introduction

This talks about how EfiPy2 use runtime service (EfiPy2.gRT) to read/write EFI variable. It is native interface in UEFI system. In EDKII libraries. For user, it could have more convenience method to enumerate/read/write variable.

EfiPy2 creates more two python style modules, EfiPyVariable and EfiPyVariables. Of course, sample codes are needed, they are VariableSamples.py and VariablesSamples.py.

This post introduces EfiPyVariable first and EfiPyVariables will be explained in next post.

EfiPyVariable

This module includes class Variable which uses gRT.GetVariable and gRT.SetVariable for EFI variable operation. This class required at least one parameter, Variable Name, to created an variable object, and Guid parameter and Attribute parameter are optional.
    Variable.__init__ (self, Name, Guid = EfiPyVarGuid, Attribute = EfiPyVarAttr)
In this module, two default variables EfiPyVarGuid and EfiPyVarAttr are used by Variable's object if variable GUID and variable attribute are not provided in object initialization function Variable.__init__().
EfiPyVarGuid is equal to gEfiGlobalVariableGuid while EfiPyVarAttr is variable full range access attribute.

Variable object has SetVariable()/GetVariable() member function. these two functions invoke EDKII gRT functions SetVariable()/GetVariable().

Before SetVariable() is called, its attribute and value can be changed by @attribute.setter and @value.setter. But variable's GUID and name cannot be changed.

After GetVariable() is invoke, user can get its value and attribute by their @property. Variable's name and GUID can only by given when object created.

Both SetVariable()/GetVariable() raise SystemError exception when function failed.

Examples - GetVariable()

VariableSamples.py is this module's example. It retrieves 'Boot0001' variable with global GUID.
  var = Variable ('Boot0001')
  var.GetVariable ()

Then print this variable's value, size and attribute
  print (f'{var.Value}\n  size: {len(var)}, Attribute: {var.Attribute}')

Examples - GetVariable()

This sample code creates a new variable 'EfiPy2VariableTest' with specified GUID. In the variable object creating stage, it has GUID parameter in it.
  var = Variable ('EfiPy2VariableTest', VariableGuid)

Until now, the variable's buffer only accept binary array object. In this stage, variable buffer's size is decided, too.
  var.Value = bytearray (b'\x01abcde\x001234')

After all parameter are set done, SetVariable() can be called.
  var.SetVariable ()

No comments:

Post a Comment