## /***************************************************************************
##  *     Copyright (c) 2010, Broadcom Corporation
##  *     All Rights Reserved
##  *     Confidential Property of Broadcom Corporation
##  *
##  *  THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE
##  *  AGREEMENT  BETWEEN THE USER AND BROADCOM.  YOU HAVE NO RIGHT TO USE OR
##  *  EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT.
##  *
##  * $brcm_Workfile: $
##  * $brcm_Revision: $
##  * $brcm_Date: $
##  *
##  * Module Description: generate headers for securing application image
##  *
##  * Revision History:
##  *
##  * $brcm_Log: $
##  * 
##  *
##  ***************************************************************************/

import os
import sys
import array
import struct

def usage() :
    print "usage : makeheaders.py <boot order> <file name>"
    print "usage : makeheaders.py --scm <scm loader file>"
    print "boot order - value of boot order field to place in the header"
    print "file name - name of the flash image for which to make header"
    return


bhfname = "bootheader.bin"
ahfname = "appheader.bin"
shfname = "scmheader.bin"
version = "TEST_IMAGE.BROADCOM.02.02.05.002"
scm = 0

if (3 > len(sys.argv)) :
    usage()
    sys.exit(1)

if ( "--scm" == sys.argv[1]) :
    scm = 1
else :
    bootorder = int(sys.argv[1])
    
fname = sys.argv[2]
fsize = os.path.getsize(fname)

print "%s size = %d" % (fname, fsize)
if ((0 != (fsize % 64)) and (0 == scm)) :
    print "Size of compressed application is not aligned on 64 bytes"
    sys.exit(1)

if(0 == scm) :
    bootheader = array.array('I', [bootorder, 0x7574, 0xFFFFFFFFL, 0xFFFFFFFFL])
    bhfile = open(bhfname,"wb")
    bootheader.tofile(bhfile)
    bhfile.close()

    appheader = struct.pack("<4L64s12L", fsize, 1, 0x7574, 0x41544448, version,
                            0xFFFFFFFFL, 0xFFFFFFFFL, 0xFFFFFFFFL, 0xFFFFFFFFL,
                            0xFFFFFFFFL, 0xFFFFFFFFL, 0xFFFFFFFFL, 0xFFFFFFFFL,
                            0xFFFFFFFFL, 0xFFFFFFFFL, 0xFFFFFFFFL, 0xFFFFFFFFL)
    ahfile = open(ahfname, "wb")
    ahfile.write(appheader)
    ahfile.close()
else :
    print "SCM header is not supported for this chip."
    sys.exit(1)
