#!/usr/bin/python
#
# $Id: crack_flash.py,v 1.1 2006/02/21 19:57:22 jlivesey Exp $
#
import os, sys, pickle, string, struct

file_name =""
bytes=0
pages=0
spares=0
blocks=0
page=0
page_verbose=0
page_verbose_1=0
spare_verbose=0
spare_verbose_1=0
good_block=0
block_page=0
hex_input=1
bin_input=0

def usage():
	print "usage: crack_flash.py flash_data_file"
	print "       -pv:  dump the page data"
	print "       -pv1: dump the first word of page data"
	print "       -sv:  dump the spare data"
	print "       -sv1: dump the first word of spare data"
	print "       -h:   The input file is one-byte wide hex"
	print "       -b:   The input file is raw binary"
	print "             (default is one-byte wide hex)"

def usage1():
	print ""
	print "Don't use the '-b' option to crack_flash to decode a binary flash file."
	print "Instead, convert the input binary flash to hex by running it through"
	print "bin2hex to produce a one byte wide hex file, which is the format"
	print "that crack_flash.py understands"
	print ""
	print " Example:"
	print "  % bin2hex joint.flashified.bin joint.flashified.hex -b"
	print "  % crack_flash.py joint.flashified.hex [options]"
	print ""
	sys.exit ()

if len(sys.argv) <= 1:
	usage ()
	sys.exit ()
else:
	file_name = sys.argv[1]
	if len(sys.argv) > 2:
		for i in range (2, len(sys.argv)):
			if sys.argv[i] == "-pv":
				page_verbose=1
			if sys.argv[i] == "-pv1":
				page_verbose_1=1
			if sys.argv[i] == "-sv":
				spare_verbose=1
			if sys.argv[i] == "-sv1":
				spare_verbose_1=1
			if sys.argv[i] == "-bp":
				block_page=1
			if sys.argv[i] == "-b":
				hex_input=0
				bin_input=1
			if sys.argv[i] == "-h":
				hex_input=1
				bin_input=0

print "filename", file_name

if hex_input == 1:
	f = open(file_name, "r");
else:
	usage1 ()

while 1:
	for i in range (0, 2048, 4):
		if hex_input == 1:
			byte0=f.readline()
			byte1=f.readline()
			byte2=f.readline()
			byte3=f.readline()
			line=byte0[0:2]+byte1[0:2]+byte2[0:2]+byte3[0:2]
		if page_verbose:
			print repr(line)
		if (i == 0) and (page_verbose_1 == 1):
			print repr(line), "(word 0 of page", repr(pages+1), ")"
		bytes=bytes+4
	pages=pages + 1
	page = page + 1
	if (pages % 64) == 0:
		blocks = blocks + 1
		page = 0
		good_block = 0
	for i in range (0, 64, 4):
		if hex_input == 1:
			byte0=f.readline()
			byte1=f.readline()
			byte2=f.readline()
			byte3=f.readline()
			line=byte0[0:2]+byte1[0:2]+byte2[0:2]+byte3[0:2]
		if blocks != 0 and (page == 1 or page == 2) and i == 0:
			if line == 'ffffffff':
				good_block = 1
			else:
				print repr(line)
				good_block = -1
		if spare_verbose:
			if i == 0:
				print repr(line), "(word 0 of spare", repr(spares+1), ")"
			else:
				print repr(line)
		if i == 0 and spare_verbose_1 == 1:
			print repr(line), "(word 0 of spare", repr(spares+1), ")"
		if i == 4 and block_page == 1:
			print "block/page", repr(line), "(word 1 of spare", repr(spares+1), ")"
		bytes=bytes+4
	spares=spares+1
	print "block", repr(blocks), "page", repr(page), repr(hex(pages)), "spares", repr(spares), "bytes", repr(bytes),
	if good_block != 0:
		if good_block == 1:
			print "good block"
		else:
			print "bad_block", repr(good_block)
		good_block = 0
	else:
		print ""
	if hex_input == 1:
		if len(byte0) == 0:
			sys.exit()

# eof
