import os import struct import sys """ row-by-row dump page = data + spare metadata + iokit return code + iokit return code 2 """ class NANDImageFlat(object): def __init__(self, filename, geometry): flags = os.O_RDONLY if sys.platform == "win32": flags |= os.O_BINARY self.fd = os.open(filename, flags) self.nCEs = geometry["#ce"] self.pageSize = geometry["#page-bytes"] self.metaSize = geometry.get("meta-per-logical-page", 12) self.dumpedPageSize = geometry.get("dumpedPageSize", self.pageSize + self.metaSize + 8) self.hasIOKitStatus = True if self.dumpedPageSize == self.pageSize + geometry["#spare-bytes"] or self.dumpedPageSize == self.pageSize + self.metaSize: self.hasIOKitStatus = False self.blankPage = "\xFF" * self.pageSize self.blankSpare = "\xFF" * self.metaSize self.imageSize = os.path.getsize(filename) expectedSize = geometry["#ce"] * geometry["#ce-blocks"] * geometry["#block-pages"] * self.dumpedPageSize if self.imageSize < expectedSize: raise Exception("Error: image appears to be truncated, expected size=%d" % expectedSize) print "Image size matches expected size, looks ok" def _readPage(self, ce, page): i = page * self.nCEs + ce off = i * self.dumpedPageSize os.lseek(self.fd, off, os.SEEK_SET) return os.read(self.fd, self.dumpedPageSize) def readPage(self, ce, page): d = self._readPage(ce, page) if not d or len(d) != self.dumpedPageSize: return None,None if self.hasIOKitStatus: r1,r2 = struct.unpack("