Skip to content

Commit

Permalink
Additional lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Teddy Reed committed Dec 28, 2015
1 parent 09bd61f commit 2144c08
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 48 deletions.
26 changes: 19 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env python

from setuptools import setup, find_packages, Extension, Command
import os
import re
from setuptools import setup, find_packages, Extension, Command


class LintCommand(Command):
Expand Down Expand Up @@ -39,19 +40,30 @@ def run(self):
))

with open('README.rst') as f:
readme = f.read()
README = f.read()

with open('LICENSE') as f:
license = f.read()
LICENSE = f.read()

with open("uefi_firmware/__init__.py", "r") as f:
__INIT__ = f.read()

TITLE = re.search(r'^__title__\s*=\s*[\'"]([^\'"]*)[\'"]',
__INIT__, re.MULTILINE).group(1)
VERSION = re.search(r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]',
__INIT__, re.MULTILINE).group(1)
AUTHOR = re.search(r'^__author__\s*=\s*[\'"]([^\'"]*)[\'"]',
__INIT__, re.MULTILINE).group(1)

setup(
title=TITLE,
name='UEFI Firmware Parser',
version='0.2',
version=VERSION,
description='Various data structures and parsing tools for UEFI firmware.',
long_description=readme,
author='Teddy Reed',
long_description=README,
author=AUTHOR,
author_email='[email protected]',
license=license,
license=LICENSE,
packages=find_packages(exclude=('tests', 'docs')),
test_suite="tests",
cmdclass={
Expand Down
2 changes: 2 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

import test_compression
7 changes: 7 additions & 0 deletions uefi_firmware/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
'''UEFI Firmware parser utils.
'''

import uefi
import pfs
import me

__title__ = "uefi_firmware"
__version__ = "0.3"
__author__ = "Teddy Reed"
__license__ = "BSD"
30 changes: 21 additions & 9 deletions uefi_firmware/base.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
'''Base provides basic firmware object structures.
'''

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import ctypes

from .utils import dump_data, sguid, blue


class BaseObject(object):

'''A base object can be used to access direct content.'''


class FirmwareObject(object):
data = None
def __init__(self):
self.data = None
self.name = None
self.attrs = None
self.guid = None

@property
def content(self):
if hasattr(self, "data"):
if hasattr(self, "data") and self.data is not None:
return self.data
return ""

Expand All @@ -24,15 +34,15 @@ def objects(self):

@property
def label(self):
if hasattr(self, "name"):
if hasattr(self, "name") and self.name is not None:
if self.name is None:
return ""
return self.name
return ""

@property
def guid_label(self):
if not hasattr(self, "guid"):
if not hasattr(self, "guid") or self.guid is None:
return ""
return sguid(self.guid)

Expand All @@ -42,7 +52,7 @@ def type_label(self):

@property
def attrs_label(self):
if hasattr(self, "attrs"):
if hasattr(self, "attrs") and self.attrs is not None:
return self.attrs
return {}

Expand Down Expand Up @@ -70,6 +80,8 @@ def iterate_objects(self, include_content=False):


class StructuredObject(object):
def __init__(self):
self.fields = []

def parse_structure(self, data, structure):
'''Construct an instance object of the provided structure.'''
Expand All @@ -87,7 +99,7 @@ def parse_structure(self, data, structure):

def show_structure(self):
for field in self.fields:
print "%s: %s" % (field, getattr(self.structure, field, None))
print ("%s: %s" % (field, getattr(self.structure, field, None)))


class RawObject(FirmwareObject, BaseObject):
Expand All @@ -99,9 +111,9 @@ def build(self, generate_checksum, debug=False):
return self.data

def showinfo(self, ts='', index=None):
print "%s%s size= %d " % (
print ("%s%s size= %d " % (
ts, blue("RawObject:"), len(self.data)
)
))

def dump(self, parent='', index=None):
path = os.path.join(parent, "object.raw")
Expand Down
73 changes: 41 additions & 32 deletions uefi_firmware/uefi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
# -*- coding: utf-8 -*-
'''EFI and UEFI related structures.
This package defines firmware structures for unpacking, decompressing,
extracting, and rebuilding UEFI data.
'''

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import struct
Expand All @@ -8,7 +15,8 @@
from .guids import get_guid_name
from .structs.uefi_structs import *
from .structs.flash_structs import *
import efi_compressor

from uefi_firmware import efi_compressor


def _get_file_type(file_type):
Expand Down Expand Up @@ -41,7 +49,7 @@ def compare(data1, data2):
md5_1 = md5(data1).hexdigest()
md5_2 = md5(data2).hexdigest()
if (md5_1 != md5_2):
print "%s != %s" % (red(md5_1), red(md5_2))
print ("%s != %s" % (red(md5_1), red(md5_2)))
return False
return True

Expand Down Expand Up @@ -180,12 +188,12 @@ def dump(self, parent, index=0):
def showinfo(self, ts="", index=0):
'''Potential for A LOT of variables.'''
if self.guid is not None and self.name is not None:
print "%s %s %s %s" % (
print ("%s %s %s %s" % (
blue("%sVariable:" % ts),
green(sguid(self.guid)),
purple(self.name),
"attrs= %s" % self.attrs["attrs"]
)
))


class NVARVariableStore(FirmwareVariableStore):
Expand Down Expand Up @@ -236,10 +244,10 @@ def dump(self, parent, index=0):
def showinfo(self, ts="", index=0):
if not self.valid_header:
return
print "%s %s" % (
print ("%s %s" % (
blue("%sNVAR Variable Store:" % ts),
"variables: %d" % self.attrs["variables"]
)
))
for i, variable in enumerate(self.variables):
variable.showinfo("%s " % ts, i)

Expand All @@ -254,7 +262,7 @@ def objects(self):
def process_subsections(self):
self.subsections = []

if not self.data:
if self.data is None:
return False

subsection_offset = 0
Expand Down Expand Up @@ -402,14 +410,15 @@ def build(self, generate_checksum=False, debug=False):

def showinfo(self, ts):
if self.name is not None:
print "%s %s" % (blue("%sCompressed Name:" % ts), purple(self.name))
print ("%s %s" % (blue("%sCompressed Name:" % ts), purple(self.name)))
for i, _object in enumerate(self.subsections):
_object.showinfo(ts, i)


class VersionSection(EfiSection):

def __init__(self, data):
self.data = data
self.build_number = struct.unpack("<16s", self.data[:16])


Expand Down Expand Up @@ -442,7 +451,7 @@ def build(self, generate_checksum=False, debug=False):
def showinfo(self, ts='', index=-1):
# print "%sGUID: %s" % (ts, green(sguid(self.guid)))
if self.name is not None:
print "%sGUID Description: %s" % (ts, purple(self.name))
print ("%sGUID Description: %s" % (ts, purple(self.name)))


class GuidDefinedSection(EfiSection):
Expand Down Expand Up @@ -533,10 +542,10 @@ def showinfo(self, ts='', index=0):
auth_status = "AUTH_VALID"
if self.attrs["attrs"] == self.ATTR_PROCESSING_REQUIRED:
auth_status = "PROCESSING_REQUIRED"
print "%s%s %s offset= 0x%x attrs= 0x%x (%s)" % (
print ("%s%s %s offset= 0x%x attrs= 0x%x (%s)" % (
ts, blue("Guid-Defined:"), green(sguid(self.guid)),
self.offset, self.attrs["attrs"], purple(auth_status)
)
))
if len(self.subsections) > 0:
for i, section in enumerate(self.subsections):
section.showinfo("%s " % ts, index=i)
Expand Down Expand Up @@ -658,13 +667,13 @@ def build(self, generate_checksum=False, debug=False):
pass

def showinfo(self, ts='', index=-1):
print "%s type 0x%02x, size 0x%x (%d bytes) (%s section)" % (
print ("%s type 0x%02x, size 0x%x (%d bytes) (%s section)" % (
blue("%sSection %d:" % (ts, index)),
self.type, self.size, self.size,
_get_section_type(self.type)[0]
)
))
if self.type == 0x15 and self.name is not None:
print "%sName: %s" % (ts, purple(self.name))
print ("%sName: %s" % (ts, purple(self.name)))

if self.parsed_object is not None:
'''If this is a specific object, show that object's info.'''
Expand Down Expand Up @@ -830,11 +839,11 @@ def build(self, generate_checksum=False, debug=False):
size = self.size
trailling_bytes = size - (len(data) + 24)
if trailling_bytes < 0:
print "%s adding %s-bytes to GUID: %s" % (
print ("%s adding %s-bytes to GUID: %s" % (
red("Warning"),
red(trailling_bytes * -1),
red(sguid(self.guid))
)
))
size += (trailling_bytes * -1)

string_size = struct.pack("<I", size)
Expand All @@ -852,7 +861,7 @@ def showinfo(self, ts='', index="N/A"):
else:
guid_display = "%s (%s)" % (
green(sguid(self.guid)), purple(guid_name))
print "%s %s type 0x%02x, attr 0x%02x, state 0x%02x, size 0x%x (%d bytes), (%s)" % (
print ("%s %s type 0x%02x, attr 0x%02x, state 0x%02x, size 0x%x (%d bytes), (%s)" % (
blue("%sFile %s:" % (ts, index)),
guid_display,
self.type,
Expand All @@ -861,7 +870,7 @@ def showinfo(self, ts='', index="N/A"):
self.size,
self.size,
_get_file_type(self.type)[0]
)
))

for i, blob in enumerate(self.raw_blobs):
if type(blob) not in [str, bytes]:
Expand All @@ -879,7 +888,7 @@ def showinfo(self, ts='', index="N/A"):
def _guessinfo(self, ts, data, index="N/A"):
if data[:4] == "\x01\x00\x00\x00" and data[
20:24] == "\x01\x00\x00\x00":
print "%s Might contain CPU microcodes" % (blue("%sBlob %d:" % (ts, index)))
print ("%s Might contain CPU microcodes" % (blue("%sBlob %d:" % (ts, index))))

def dump(self, parent=""):
parent = os.path.join(parent, "file-%s" % sguid(self.guid))
Expand Down Expand Up @@ -945,11 +954,11 @@ def build(self, generate_checksum=False, debug=False):
data += self.overflow_data

if len(data) != len(self._data):
print "ffs size mismatch old=%d new=%d %d" % (
print ("ffs size mismatch old=%d new=%d %d" % (
len(self._data),
len(data),
len(self._data) - len(data)
)
))

return data
pass
Expand Down Expand Up @@ -1111,24 +1120,24 @@ def showinfo(self, ts='', index=None):
if not self.valid_header or len(self.data) == 0:
return

print "%s %s attr 0x%08x, rev %d, cksum 0x%x, size 0x%x (%d bytes)" % (
print ("%s %s attr 0x%08x, rev %d, cksum 0x%x, size 0x%x (%d bytes)" % (
blue("%sFirmware Volume:" % (ts)),
green(sguid(self.guid)),
self.attributes,
self.revision,
self.checksum,
self.size,
self.size
)
print blue("%s Firmware Volume Blocks: " % (ts)),
))
print (blue("%s Firmware Volume Blocks: " % (ts)), end=False)
for block_size, block_length in self.blocks:
print "(%d, 0x%x)" % (block_size, block_length),
print ""
print ("(%d, 0x%x)" % (block_size, block_length), end=False)
print ("")

for _ffs in self.firmware_filesystems:
_ffs.showinfo(ts + " ")
for raw in self.raw_objects:
print "%s%s NVRAM" % ("%s " % ts, blue("Raw section:"))
print ("%s%s NVRAM" % ("%s " % ts, blue("Raw section:")))

def dump(self, parent="", index=None):
if len(self.data) == 0:
Expand Down Expand Up @@ -1282,17 +1291,17 @@ def showinfo(self, ts='', index=None):
if not self.valid_header or len(self.data) == 0:
return

print "%s %s flags 0x%08x, size 0x%x (%d bytes)" % (
print ("%s %s flags 0x%08x, size 0x%x (%d bytes)" % (
blue("%sFirmware Capsule:" % (ts)),
"%s/%s" % (green(sguid(self.capsule_guid)),
green(sguid(self.guid))),
self.flags, self.size, self.size
)
print "%s Details: size= 0x%x (%d bytes) body= 0x0%x, oem= 0x0%x, author= 0x0%x" % (
))
print ("%s Details: size= 0x%x (%d bytes) body= 0x0%x, oem= 0x0%x, author= 0x0%x" % (
ts, self.image_size, self.image_size,
self.offsets["capsule_body"], self.offsets[
"oem_header"], self.offsets["author_info"]
)
))
# print self.offsets

if self.capsule_body is not None:
Expand Down

0 comments on commit 2144c08

Please sign in to comment.