Thursday, December 28, 2017

New package LoLeOp for EfiPy new release - EfiPy_r0.1.5(24622)

Release Note:
Add LoLeOp for x86 IO port, Memory access, CPUID and MSR operation.
Download site: https://sourceforge.net/projects/efipy/
Sample code at EFI\StdLib\lib\python.27\site-packages\LoLeOp\Samples of download tarball.
Its architecture can be improved.
Following are simple samples. For more detail,  it will published in the next of this blog.

Sample of X86 IO port read/write:

from LoLeOp.Io import Io8

# write value 0x10 to IO port 0x70
Io8[0x70] = 0x10

# read value from IO port 0x71
Io71 = Io8[0x71]

# Write bit 0~3 value of IO port 0x70 as 0x02
Io8[0x70][0:3] = 0x02
# read bit 0~3 value of IO port 0x71
IO71_3 = Io8[0x71][0:3]

Sample of Memory read/write:

from LoLeOp.Mem import Mem8, Mem16, Mem32, Mem64

# Read 1 byte from Address 0x0C000000
memX = Mem8[0x0C000000]

# Read 2 byte from Address 0x0C000000
memX = Mem16[0x0C000000]

# Read bit 5 of Address 0x0C000000 
memX = Mem8[0x0C000000][5]

# Set 1 in bit 2 of Address 0x0C000000
Mem8[0x0C000000][2] |= 0x01

Sample of read CPUID:

from LoLeOp.CpuId import CPUID

# Read CPUID index 0x00 (Input parameter EAX as 0x00)
# Assembly code (Intel syntax)
# mov eax, 0x0
# cpuid

CpuId00 = CPUID (0)

# print cpuid instructoin return value of EAX
print "CpuId00.EAX: %X" % CpuId00.EAX

# print cpuid instructoin return value of bit 0, 1 of EAX
print "CpuId00.EAX[0:1]: %X" % CpuId00.EAX[0:1]



# print cpuid instructoin return value of MaximumLeaf of EAX
print "CpuId00.EAX.MaximumLeaf: %X" % CpuId00.EAX.MaximumLeaf


Sample of read SDBG bit from MSR operaton:

# EFI\StdLib\lib\python.27\site-packages\LoLeOp\Samples\SdbgSample.py

from LoLeOp.CpuId import CPUID

from LoLeOp.Msr import rMsr, EFIPY_MSR_COMMON_Reg
from LoLeOp.BitOp import _MemArray

CellExt = {
  # "CellUnion": EFIPY_MSR_COMMON_Reg,
  "CellDefault": EFIPY_MSR_COMMON_Reg,
  "CellArray": {},
  "CellType":  'Uint64',
  "CellBits":  'Bits'
}

print "Looking for platform IA32_DEBUG_INTERFACE status..."

CpuId01 = CPUID (0x01)
if CpuId01.ECX.SDBG != 1:
  print "This platform does not support IA32_DEBUG_INTERFACE...exit."
  exit (0)

print "This platform supports IA32_DEBUG_INTERFACE"
print "SDBG value:            %X" % CpuId01.ECX[11]

msr = _MemArray("msr",  64, CellClass = rMsr, CellExt = CellExt)

# Read bit 0 of MSR 0xC80
print "Enable (R/W):          %x" % msr[0xC80][0]

# Read bit 30 of MSR 0xC80print "Lock (R/W):            %x" % msr[0xC80][30]# Read bit 31 of MSR 0xC80print "Debug Occurred (R/O):  %x" % msr[0xC80][31]