Extend U-Boot initrd support to include legacy images

This commit is contained in:
Carl D. Roth
2016-11-10 14:50:26 -08:00
parent 707279da6f
commit 52c96b0bea
2 changed files with 51 additions and 11 deletions

View File

@@ -10,7 +10,7 @@ import tempfile
import string
import shutil
import Fit
import Fit, Legacy
class SubprocessMixin:
@@ -879,16 +879,16 @@ class InitrdContext(SubprocessMixin):
# (it's inside this chroot anyway)
return initrdDir
class FitInitrdContext(SubprocessMixin):
class UbootInitrdContext(SubprocessMixin):
def __init__(self, path, log=None):
self.fitPath = path
self.path = path
self.log = log or logging.getLogger(self.__class__.__name__)
self.initrd = self.__initrd = None
def __enter__(self):
self.log.debug("parsing FIT image in %s", self.fitPath)
p = Fit.Parser(path=self.fitPath, log=self.log)
def _extractFit(self):
self.log.debug("parsing FIT image in %s", self.path)
p = Fit.Parser(path=self.path, log=self.log)
node = p.getInitrdNode()
if node is None:
raise ValueError("cannot find initrd node in FDT")
@@ -896,7 +896,7 @@ class FitInitrdContext(SubprocessMixin):
if prop is None:
raise ValueError("cannot find initrd data property in FDT")
with open(self.fitPath) as fd:
with open(self.path) as fd:
self.log.debug("reading initrd at [%x:%x]",
prop.offset, prop.offset+prop.sz)
fd.seek(prop.offset, 0)
@@ -907,7 +907,47 @@ class FitInitrdContext(SubprocessMixin):
self.log.debug("+ cat > %s", self.initrd)
with os.fdopen(fno, "w") as fd:
fd.write(buf)
return self
def _extractLegacy(self):
self.log.debug("parsing legacy U-Boot image in %s", self.path)
p = Legacy.Parser(path=self.path, log=self.log)
if p.ih_type != Legacy.Parser.IH_TYPE_MULTI:
raise ValueError("not a multi-file image")
if p.ih_os != Legacy.Parser.IH_OS_LINUX:
raise ValueError("invalid OS code")
sz, off = p.images[1]
# assume the initrd is the second of three images
with open(self.path) as fd:
self.log.debug("reading initrd at [%x:%x]",
off, off+sz)
fd.seek(off, 0)
buf = fd.read(sz)
fno, self.initrd = tempfile.mkstemp(prefix="initrd-",
suffix=".img")
self.log.debug("+ cat > %s", self.initrd)
with os.fdopen(fno, "w") as fd:
fd.write(buf)
def __enter__(self):
with open(path=self.path) as fd:
isFit = Fit.Parser.isFit(fd)
isLegacy = Legacy.Parser.isLegacy(fd)
if isFit:
self._extractFit()
return self
if isLegacy:
self._extractLegacy()
return self
raise ValueError("invalid U-Boot image %s" % self.path)
def shutdown(self):
initrd, self.initrd = self.initrd, None

View File

@@ -13,7 +13,7 @@ from InstallUtils import InitrdContext, MountContext
from InstallUtils import SubprocessMixin
from InstallUtils import ProcMountsParser, ProcMtdParser
from InstallUtils import BlkidParser
from InstallUtils import FitInitrdContext
from InstallUtils import UbootInitrdContext
import onl.platform.current
from onl.sysconfig import sysconfig
@@ -47,7 +47,7 @@ class AppBase(SubprocessMixin, object):
return 0
def _runFitShell(self, device):
with FitInitrdContext(path=device, log=self.log) as ctx:
with UbootInitrdContext(path=device, log=self.log) as ctx:
return self._runInitrdShell(ctx.initrd)
def shutdown(self):
@@ -174,7 +174,7 @@ class OnieBootContext:
part = parts[0]
self.log.debug("found ONIE MTD device %s",
part.charDevice or part.blockDevice)
with FitInitrdContext(part.blockDevice, log=self.log) as self.fctx:
with UbootInitrdContext(part.blockDevice, log=self.log) as self.fctx:
with InitrdContext(initrd=self.fctx.initrd, log=self.log) as self.ictx:
self.initrd = self.fctx.initrd
self.fctx.detach()