def guess_oob_offset(image, headers, oob_size): oobs_bytes = YaffsParser.get_oob_bytes(image, headers, oob_size) best_parsed = [] # We use -16 because we are looking for 16 bytes in the tag # for parsing for offset in xrange(0, oob_size-16): parsed = [] for bytes in oobs_bytes: parsed_oob = YaffsOobTag(bytes, offset) if not parsed_oob.isHeaderTag: continue else: parsed.append(parsed_oob) if len(parsed) > len(best_parsed): best_offset = offset best_parsed = parsed object_ids = set([o.object_id for o in best_parsed]) if len(object_ids) > 0: print "OOB tag offset is %d" % best_offset print "with %d valid header tags" % len(best_parsed) print "Object id: %s" % str(object_ids) return best_offset print "Unable to determine OOB tag offset." return None
def guess_block_size(image, chunk_size, oob_size, oob_offset): chunk_pairs = YaffsParser.extract_chunks(image, chunk_size, oob_size) chunks = [c for c, o in chunk_pairs[:1024]] oobs_bytes = YaffsParser.get_oob_bytes(image, chunks, oob_size) oobs = [YaffsOobTag(b, oob_offset) for b in oobs_bytes] prev = -1 counts = [] count = 0 for oob in oobs: if oob.block_seq != prev: if count > 0: counts.append(count) count = 1 prev = oob.block_seq else: count += 1 import collections size, freq = collections.Counter(counts).most_common(1)[0] if freq == 1: print "Unable to determine block size." return None print "Most likely block size: %d" % size return size
def main(): """ Assume we pass this script the image file path as an argument on the command line. """ usage = 'usage: %prog [options] imagefile' parser = OptionParser(usage=usage) parser.add_option('--chunksize', action='store', type='int', dest='chunk_size', default=2048) parser.add_option('--oobsize', action='store', type='int', dest='oob_size', default=64) options, args = parser.parse_args() if len(args) != 1: print "Incorrect command line arguments. Missing (or too many) image files" return 1 image = args[0] headers = Scanner.get_anchor_headers(image, options.chunk_size, options.oob_size, 'contacts2.db') oobs = YaffsParser.get_oob_bytes(image, headers, options.oob_size) for oob in oobs: sys.stdout.write(oob) #Separate each oob with 16 'X' Bytes. sys.stdout.write('X'*16)
def count_constant_oobs(image, chunks, oobsize): oobs = YaffsParser.get_oob_bytes(image, chunks, oobsize) constants_count = 0 constant = '\xff' * oobsize for oob in oobs: if oob == constant: constants_count += 1 return constants_count