diff options
author | George Kraft <george.kraft@calxeda.com> | 2012-08-10 12:03:09 -0500 |
---|---|---|
committer | George Kraft <george.kraft@calxeda.com> | 2012-08-10 12:03:09 -0500 |
commit | 2202a937935565fd6f59570095a661af3084d6b8 (patch) | |
tree | 69ff3988a419545cf160e664bdc95d00034a42b5 | |
parent | d3bf633fb5c7c97b5f5d8aef8df2ad9594758aef (diff) | |
download | cxmanage-2202a937935565fd6f59570095a661af3084d6b8.tar.gz |
simg: Use 4KB alignment for CDB and boot log images
-rw-r--r-- | cxmanage/image.py | 20 | ||||
-rw-r--r-- | cxmanage/simg.py | 24 |
2 files changed, 30 insertions, 14 deletions
diff --git a/cxmanage/image.py b/cxmanage/image.py index a91fde0..51f6816 100644 --- a/cxmanage/image.py +++ b/cxmanage/image.py @@ -35,7 +35,7 @@ import os import tempfile from cxmanage import CxmanageError -from cxmanage.simg import create_simg, has_simg, valid_simg +from cxmanage.simg import create_simg, has_simg, valid_simg, get_simg_contents class Image: """ An image consists of an image type, a filename, and any info needed @@ -77,8 +77,9 @@ class Image: daddr = self.daddr # Create simg - simg = create_simg(contents, version=version, - daddr=daddr, skip_crc32=self.skip_crc32) + align = (self.type in ["CDB", "BOOT_LOG"]) + simg = create_simg(contents, version=version, daddr=daddr, + skip_crc32=self.skip_crc32, align=align) filename = tempfile.mkstemp(".simg", work_dir + "/")[1] open(filename, "w").write(simg) @@ -97,17 +98,20 @@ class Image: if self.simg: return os.path.getsize(self.filename) else: - return os.path.getsize(self.filename) + 28 + contents = open(self.filename).read() + align = (self.type in ["CDB", "BOOT_LOG"]) + simg = create_simg(contents, skip_crc32=True, align=align) + return len(simg) def verify(self): """ Return true if the image is valid, false otherwise """ - if self.type == "CDB": + if self.type in ["CDB", "BOOT_LOG"]: # Look for "CDBH" contents = open(self.filename).read() if self.simg: - return contents[28:32] == "CDBH" - else: - return contents[:4] == "CDBH" + contents = get_simg_contents(contents) + + return contents[:4] == "CDBH" return True diff --git a/cxmanage/simg.py b/cxmanage/simg.py index 175e3c5..a6bfd0e 100644 --- a/cxmanage/simg.py +++ b/cxmanage/simg.py @@ -71,7 +71,7 @@ class SIMGHeader: self.crc32) -def create_simg(contents, version=0, daddr=0, skip_crc32=False): +def create_simg(contents, version=0, daddr=0, skip_crc32=False, align=False): """Create an SIMG version of a file Assumes version and hdrfmt are 0. @@ -82,6 +82,9 @@ def create_simg(contents, version=0, daddr=0, skip_crc32=False): header.imglen = len(contents) header.daddr = daddr + if align: + header.imgoff = 4096 + # Calculate crc value if skip_crc32: crc32 = 0 @@ -92,7 +95,7 @@ def create_simg(contents, version=0, daddr=0, skip_crc32=False): header.flags = 0xFFFFFFFF header.crc32 = crc32 - return str(header) + contents + return str(header).ljust(header.imgoff, chr(0)) + contents def has_simg(simg): @@ -112,16 +115,17 @@ def valid_simg(simg): if not has_simg(simg): return False header = SIMGHeader(simg[:28]) - contents = simg[28:] # Check offset - if header.imgoff != 28: + if header.imgoff < 28: return False # Check length + start = header.imgoff + end = start + header.imglen + contents = simg[start:end] if len(contents) < header.imglen: return False - contents = contents[:header.imglen] # Check crc32 crc32 = header.crc32 @@ -136,7 +140,15 @@ def valid_simg(simg): def get_simg_header(simg): """ Return the header of this SIMG """ - if len(simg) < 28 or not has_simg(simg): + if not valid_simg(simg): raise ValueError("Failed to read invalid SIMG") return SIMGHeader(simg[:28]) + + +def get_simg_contents(simg): + """ Return the contents of this SIMG """ + header = get_simg_header(simg) + start = header.imgoff + end = start + header.imglen + return simg[start:end] |