freemyipod r162 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r161‎ | r162 | r163 >
Date:02:53, 19 August 2010
Author:theseven
Status:new
Tags:
Comment:
Add emBIOS boot option configuration tool
Modified paths:
  • /embios/trunk/tools/embiosbootcfg.py (added) (history)
  • /embios/trunk/tools/libembiosbootcfg.py (added) (history)

Diff [purge]

Index: embios/trunk/tools/libembiosbootcfg.py
@@ -0,0 +1,79 @@
 2+#!/usr/bin/env python
 3+#
 4+#
 5+# Copyright 2010 TheSeven
 6+#
 7+#
 8+# This file is part of emBIOS.
 9+#
 10+# emBIOS is free software: you can redistribute it and/or
 11+# modify it under the terms of the GNU General Public License as
 12+# published by the Free Software Foundation, either version 2 of the
 13+# License, or (at your option) any later version.
 14+#
 15+# emBIOS is distributed in the hope that it will be useful,
 16+# but WITHOUT ANY WARRANTY; without even the implied warranty of
 17+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 18+# See the GNU General Public License for more details.
 19+#
 20+# You should have received a copy of the GNU General Public License
 21+# along with emBIOS. If not, see <http://www.gnu.org/licenses/>.
 22+#
 23+#
 24+
 25+import struct
 26+
 27+def configure(binary, **args):
 28+ start = binary.index("emBIboot", 0, 512)
 29+ version = struct.unpack("<I", binary[start + 8 : start + 12])[0]
 30+ if version != 0: raise ValueError("Unknown boot configuration data version")
 31+ (tryfile, filename, fileflags, filedest,
 32+ tryflash, flashname, flashflags, flashdest,
 33+ trymmap, mmapaddr, mmapsize, mmapflags, mmapdest) \
 34+ = struct.unpack("<I256sIII8sIIIIIII", binary[start + 12 : start + 320])
 35+ if "reset" in args and args["reset"]:
 36+ tryfile = 0
 37+ filename = "\0" * 256
 38+ fileflags = 0
 39+ filedest = 0
 40+ tryflash = 0
 41+ flashname = "\0" * 8
 42+ flashflags = 0
 43+ flashdest = 0
 44+ trymmap = 0
 45+ mmapaddr = 0
 46+ mmapsize = 0
 47+ mmapflags = 0
 48+ mmapdest = 0
 49+ if "tryfile" in args: tryfile = 1 if args["tryfile"] else 0
 50+ if "filename" in args: filename = args["filename"].ljust(256, "\0")
 51+ if "filecomp" in args:
 52+ if args["filecomp"]: fileflags = fileflags | 2
 53+ else: fileflags = fileflags & ~2
 54+ if "filecopy" in args:
 55+ if args["filecopy"]: fileflags = fileflags | 1
 56+ else: fileflags = fileflags & ~1
 57+ if "filedest" in args: filedest = args["filedest"]
 58+ if "tryflash" in args: tryflash = 1 if args["tryflash"] else 0
 59+ if "flashname" in args: flashname = args["flashname"].ljust(8)
 60+ if "flashcomp" in args:
 61+ if args["flashcomp"]: flashflags = flashflags | 2
 62+ else: flashflags = flashflags & ~2
 63+ if "flashcopy" in args:
 64+ if args["flashcopy"]: flashflags = flashflags | 1
 65+ else: flashflags = flashflags & ~1
 66+ if "flashdest" in args: flashdest = args["flashdest"]
 67+ if "trymmap" in args: trymmap = 1 if args["trymmap"] else 0
 68+ if "mmapaddr" in args: mmapaddr = args["mmapaddr"]
 69+ if "mmapsize" in args: mmapsize = args["mmapsize"]
 70+ if "mmapcomp" in args:
 71+ if args["mmapcomp"]: mmapflags = mmapflags | 2
 72+ else: mmapflags = mmapflags & ~2
 73+ if "mmapcopy" in args:
 74+ if args["mmapcopy"]: mmapflags = mmapflags | 1
 75+ else: mmapflags = mmapflags & ~1
 76+ if "mmapdest" in args: mmapdest = args["mmapdest"]
 77+ data = struct.pack("<I256sIII8sIIIIIII", tryfile, filename, fileflags, filedest,
 78+ tryflash, flashname, flashflags, flashdest,
 79+ trymmap, mmapaddr, mmapsize, mmapflags, mmapdest)
 80+ return binary[:start + 12] + data + binary[start + 320:]
Index: embios/trunk/tools/embiosbootcfg.py
@@ -0,0 +1,98 @@
 2+#!/usr/bin/env python
 3+#
 4+#
 5+# Copyright 2010 TheSeven
 6+#
 7+#
 8+# This file is part of emBIOS.
 9+#
 10+# emBIOS is free software: you can redistribute it and/or
 11+# modify it under the terms of the GNU General Public License as
 12+# published by the Free Software Foundation, either version 2 of the
 13+# License, or (at your option) any later version.
 14+#
 15+# emBIOS is distributed in the hope that it will be useful,
 16+# but WITHOUT ANY WARRANTY; without even the implied warranty of
 17+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 18+# See the GNU General Public License for more details.
 19+#
 20+# You should have received a copy of the GNU General Public License
 21+# along with emBIOS. If not, see <http://www.gnu.org/licenses/>.
 22+#
 23+#
 24+
 25+import sys
 26+import libembiosbootcfg
 27+from optparse import *
 28+
 29+parser = OptionParser("usage: %prog [options] <infile> <outfile>")
 30+filegroup = OptionGroup(parser, "Booting from a file",
 31+ "Use these options to configure emBIOS to try "
 32+ "booting from a file on a FAT32 partition")
 33+filegroup.add_option("--file", help = "Boot from FILE")
 34+filegroup.add_option("--file-compressed", action = "store_true", default = False,
 35+ help = "Specify this if FILE is compressed")
 36+filegroup.add_option("--file-run-from", type = "int", metavar = "ADDR",
 37+ help = "Make sure FILE is executed from memory address ADDR")
 38+parser.add_option_group(filegroup)
 39+flashgroup = OptionGroup(parser, "Booting from a boot flash image",
 40+ "Use these options to configure emBIOS to try "
 41+ "booting from an image located in the boot flash")
 42+flashgroup.add_option("--flash", metavar = "NAME", help = "Boot from flash image NAME")
 43+flashgroup.add_option("--flash-compressed", action = "store_true", default = False,
 44+ help = "Specify this if the image is compressed")
 45+flashgroup.add_option("--flash-run-from", type = "int", metavar = "ADDR",
 46+ help = "Make sure the image is executed from memory address ADDR")
 47+parser.add_option_group(flashgroup)
 48+mmapgroup = OptionGroup(parser, "Booting from a memory region",
 49+ "Use these options to configure emBIOS to try "
 50+ "booting from a memory location, such as an embedded "
 51+ "app or an app located on a memory-mapped flash")
 52+mmapgroup.add_option("--mmap-addr", metavar = "ADDR", help = "Boot from memory location ADDR")
 53+mmapgroup.add_option("--mmap-size", metavar = "SIZE",
 54+ help = "Specifies the size of the executable at ADDR in bytes")
 55+mmapgroup.add_option("--mmap-compressed", action = "store_true", default = False,
 56+ help = "Specify this if the executable is compressed")
 57+mmapgroup.add_option("--mmap-run-from", type = "int", metavar = "COPYADDR",
 58+ help = "Copy the executable to COPYADDR before executing it")
 59+parser.add_option_group(mmapgroup)
 60+(options, args) = parser.parse_args()
 61+if len(args) != 2: parser.error("incorrect number of arguments")
 62+
 63+if (options.mmap_addr and not options.mmap_size) or \
 64+ (not options.mmap_addr and options.mmap_size):
 65+ parser.error("either none or both of --mmap-addr and --map-size must be specified")
 66+
 67+file = open(args[0], "rb")
 68+data = file.read()
 69+file.close()
 70+
 71+config = {"reset": True}
 72+if options.file:
 73+ config["tryfile"] = True
 74+ config["filename"] = options.file
 75+ if options.file_compressed: config["filecomp"] = True
 76+ if options.file_run_from:
 77+ config["filecopy"] = True
 78+ config["filedest"] = options.file_run_from
 79+if options.flash:
 80+ config["tryflash"] = True
 81+ config["flashname"] = options.flash
 82+ if options.flash_compressed: config["flashcomp"] = True
 83+ if options.flash_run_from:
 84+ config["flashcopy"] = True
 85+ config["flashdest"] = options.flash_run_from
 86+if options.mmap_addr:
 87+ config["trymmap"] = True
 88+ config["mmapaddr"] = options.mmap_addr
 89+ config["mmapsize"] = options.mmap_size
 90+ if options.mmap_compressed: config["mmapcomp"] = True
 91+ if options.mmap_run_from:
 92+ config["mmapcopy"] = True
 93+ config["mmapdest"] = options.mmap_run_from
 94+
 95+data = libembiosbootcfg.configure(data, **config)
 96+
 97+file = open(args[1], "wb")
 98+file.write(data)
 99+file.close()