2008-07-29 11:08:54 +00:00
|
|
|
# A script to convert an entire directory to a C array, in the "romfs" format
|
|
|
|
import os, sys
|
|
|
|
import re
|
|
|
|
import struct
|
|
|
|
|
|
|
|
_crtline = ' '
|
|
|
|
_numdata = 0
|
|
|
|
_bytecnt = 0
|
2009-01-08 15:20:05 +00:00
|
|
|
|
2008-07-29 11:08:54 +00:00
|
|
|
# Line output function
|
|
|
|
def _add_data( data, outfile, moredata = True ):
|
|
|
|
global _crtline, _numdata, _bytecnt
|
|
|
|
_bytecnt = _bytecnt + 1
|
|
|
|
if moredata:
|
|
|
|
_crtline = _crtline + "0x%02X, " % data
|
|
|
|
else:
|
|
|
|
_crtline = _crtline + "0x%02X" % data
|
|
|
|
_numdata = _numdata + 1
|
|
|
|
if _numdata == 16 or not moredata:
|
|
|
|
outfile.write( _crtline + '\n' )
|
|
|
|
_crtline = ' '
|
|
|
|
_numdata = 0
|
|
|
|
|
2009-01-08 15:20:05 +00:00
|
|
|
# dirname - the directory where the files are located.
|
2008-07-29 11:08:54 +00:00
|
|
|
# outname - the name of the C output
|
2009-01-08 15:20:05 +00:00
|
|
|
# flist - list of files
|
2008-07-29 11:08:54 +00:00
|
|
|
# Returns True for OK, False for error
|
2009-01-08 15:20:05 +00:00
|
|
|
def mkfs( dirname, outname, flist ):
|
|
|
|
# Try to create the output files
|
2008-07-29 11:08:54 +00:00
|
|
|
outfname = outname + ".h"
|
|
|
|
try:
|
|
|
|
outfile = file( outfname, "wb" )
|
|
|
|
except:
|
|
|
|
print "Unable to create output file"
|
|
|
|
return False
|
|
|
|
|
|
|
|
global _crtline, _numdata, _bytecnt
|
|
|
|
_crtline = ' '
|
|
|
|
_numdata = 0
|
|
|
|
_bytecnt = 0
|
|
|
|
# Generate headers
|
|
|
|
outfile.write( "// Generated by mkfs.py\n// DO NOT MODIFY\n\n" )
|
|
|
|
outfile.write( "#ifndef __%s_H__\n#define __%s_H__\n\n" % ( outname.upper(), outname.upper() ) )
|
|
|
|
|
|
|
|
outfile.write( "const unsigned char %s_fs[] = \n{\n" % ( outname.lower() ) )
|
|
|
|
|
|
|
|
# Process all files
|
|
|
|
for fname in flist:
|
2009-01-08 15:20:05 +00:00
|
|
|
if len( fname ) > 14:
|
|
|
|
print "Skipping %s (name longer than 14 chars)" % realname
|
|
|
|
continue
|
|
|
|
|
2008-07-29 11:08:54 +00:00
|
|
|
# Get actual file name
|
|
|
|
realname = os.path.join( dirname, fname )
|
|
|
|
|
|
|
|
# Ensure it actually is a file
|
|
|
|
if not os.path.isfile( realname ):
|
2009-01-08 15:20:05 +00:00
|
|
|
print "Skipping %s ... (not found or not a regular file)" % fname
|
2008-07-29 11:08:54 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
# Try to open and read the file
|
|
|
|
try:
|
|
|
|
crtfile = file( realname, "rb" )
|
|
|
|
filedata = crtfile.read()
|
|
|
|
except:
|
|
|
|
outfile.close()
|
|
|
|
os.remove( outfname )
|
|
|
|
print "Unable to read %s" % fname
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Write name, size, id, numpars
|
|
|
|
for c in fname:
|
|
|
|
_add_data( ord( c ), outfile )
|
|
|
|
_add_data( 0, outfile ) # ASCIIZ
|
|
|
|
size_l = len( filedata ) & 0xFF
|
|
|
|
size_h = ( len( filedata ) >> 8 ) & 0xFF
|
|
|
|
_add_data( size_l, outfile )
|
|
|
|
_add_data( size_h, outfile )
|
|
|
|
# Then write the rest of the file
|
|
|
|
for c in filedata:
|
|
|
|
_add_data( ord( c ), outfile )
|
|
|
|
|
|
|
|
# Report
|
|
|
|
print "Encoded file %s (%d bytes)" % ( fname, len( filedata ) )
|
|
|
|
|
|
|
|
# All done, write the final "0" (terminator)
|
|
|
|
_add_data( 0, outfile, False )
|
|
|
|
outfile.write( "};\n\n#endif\n" );
|
|
|
|
outfile.close()
|
2008-09-10 18:41:31 +00:00
|
|
|
print "Done, total size is %d bytes" % _bytecnt
|
2008-07-29 11:08:54 +00:00
|
|
|
return True
|