2222from pathlib import Path
2323import zlib
2424import json
25- from utils import toc_common
26- from utils .config import *
27- from utils .toc_common import *
28- from json .decoder import JSONDecodeError
25+ import utils
26+ from utils .config import load_global_config , read_global_config
2927from utils .device_config import gen_device_config
3028from utils .sign_image_util import sign_image
31- from printInfo import *
32- from utils .gen_fw_cfg import *
29+ from utils .printInfo import printInfo , verboseModeSet
30+ from utils import paths
31+ from utils .toc_common import (
32+ addContentCertificate ,
33+ cleanBuild ,
34+ compressImage ,
35+ createContentCerts ,
36+ getBlob ,
37+ getCPUID ,
38+ getImageSize ,
39+ getObjectFlags ,
40+ getObjectType ,
41+ getObjectVersion ,
42+ read_json_file ,
43+ validateOptions ,
44+ )
3345
3446# Define Version constant for each separate tool
3547# 0.30.000 includes image signing
8092cpuid_switcher = {"A32_0" : 0 , "A32_1" : 1 , "M55_HP" : 2 , "M55_HE" : 3 }
8193
8294
83- certPath = Path ("cert/" )
84- imagePath = Path ("build/images" )
85-
8695# sizes of Certificates
8796CERT_CHAIN_SIZE = utils .toc_common .CERT_CHAIN_SIZE
8897CONT_CERT_SIZE = utils .toc_common .CONT_CERT_SIZE
@@ -102,6 +111,8 @@ def createOemTocPackage(fwsections, outputFile):
102111 global otocStartAddress
103112 global unmanaged
104113
114+ certPath = paths .CERT_INPUT_DIR
115+
105116 print ("Creating APP TOC Package..." )
106117 # Start by loading the Key1/Key2 Certificates for later on
107118 printInfo ("Loading Key1 Cert..." )
@@ -113,7 +124,7 @@ def createOemTocPackage(fwsections, outputFile):
113124 outf = open (outputFile , "wb" )
114125
115126 # create a memory layout map for debugging
116- mapf = open ("build/ app-package-map.txt" , "w" )
127+ mapf = open (paths . OUTPUT_DIR / " app-package-map.txt" , "w" )
117128 mapf .write ("* * * User managed MRAM locations* * * \n " )
118129
119130 debugScript = ""
@@ -128,8 +139,8 @@ def createOemTocPackage(fwsections, outputFile):
128139
129140 printInfo (img )
130141 mapf .write (f"{ img [1 ]} \t { hex (img [2 ])} \t { img [2 ]} \t { img [3 ]} \t { imgFile } \n " )
131- debugScript += "../build/images/" + imgFile + " " + img [1 ] + " "
132- sign_image ("build/images/" + imgFile , int (img [1 ], 16 ), 0xFFFFFFFF , "OEM" )
142+ debugScript += imgFile + " " + img [1 ] + " "
143+ sign_image (imgFile , int (img [1 ], 16 ), 0xFFFFFFFF , "OEM" )
133144
134145 # create a pointer to keep track addresses of components (object_address in OTOC)
135146 mPointer = oemManagedAreaStartAddress
@@ -161,7 +172,9 @@ def createOemTocPackage(fwsections, outputFile):
161172 mPointer += CERT_CHAIN_SIZE - CONT_CERT_SIZE
162173 # print(f'mPointer KC un: {mPointer - oemManagedAreaStartAddress}')
163174 # copy the Content Certificate (for signed or unsigned images)
164- addContentCertificate (outf , sec ["binary" ] + "_" + str (sec ["mramAddress" ]))
175+ addContentCertificate (
176+ outf , os .path .basename (sec ["binary" ]) + "_" + str (sec ["mramAddress" ])
177+ )
165178 # mapf.write(f"{hostAddress(mPointer)}\t{hex(CONT_CERT_SIZE)}\t{(CONT_CERT_SIZE)}\tSB" + sec['binary'] + ".crt\n")
166179 mapf .write (
167180 f"{ hex (mPointer )} \t { hex (CONT_CERT_SIZE )} \t { (CONT_CERT_SIZE )} \t SB"
@@ -198,7 +211,9 @@ def createOemTocPackage(fwsections, outputFile):
198211 mPointer += CERT_CHAIN_SIZE - CONT_CERT_SIZE
199212 # print(f'mPointer - KCs: {mPointer - oemManagedAreaStartAddress}')
200213 # copy the Content Certificate (for signed or unsigned images)
201- addContentCertificate (outf , sec ["binary" ] + "_" + str (sec ["mramAddress" ]))
214+ addContentCertificate (
215+ outf , os .path .basename (sec ["binary" ]) + "_" + str (sec ["mramAddress" ])
216+ )
202217 # mapf.write(f"{hostAddress(mPointer)}\t{hex(CONT_CERT_SIZE)}\t{(CONT_CERT_SIZE)}\tSB" + sec['binary'] + ".crt\n")
203218 mapf .write (
204219 f"{ hex (mPointer )} \t { hex (CONT_CERT_SIZE )} \t { (CONT_CERT_SIZE )} \t SB"
@@ -216,7 +231,7 @@ def createOemTocPackage(fwsections, outputFile):
216231 binFile += ".lzf"
217232
218233 # print("Copying: " + binFile)
219- outf .write (getBlob (imagePath / binFile ))
234+ outf .write (getBlob (binFile ))
220235 # mapf.write(f"{hostAddress(mPointer)}\t{hex(sec['size'])}\t{sec['size']}\t" + binFile + "\n")
221236 mapf .write (
222237 f"{ hex (mPointer )} \t { hex (sec ['size' ])} \t { sec ['size' ]} \t " + binFile + "\n "
@@ -358,15 +373,15 @@ def createOemTocPackage(fwsections, outputFile):
358373
359374 mapf .write ("\n * * * END of APP Package * * * \n " )
360375
361- debugScript += "../" + outputFile + " " + hex (oemManagedAreaStartAddress )
376+ debugScript += outputFile + " " + hex (oemManagedAreaStartAddress )
362377
363378 # close the file
364379 outf .close ()
365380
366381 mapf .close ()
367382
368383 # generate debug script file
369- dsFile = "bin/ application_package.ds" # Was bin/oempackage.ds
384+ dsFile = paths . OUTPUT_DIR / " application_package.ds" # Was bin/oempackage.ds
370385 try :
371386 ds = open (dsFile , "w" )
372387 # f.write('reset system\n')
@@ -410,7 +425,7 @@ def calcOemManagedArea(fwsections):
410425 numImages += 1
411426
412427 # determine the size of file and if padding is needed
413- fsize , padlen = getImageSize ("build/images/" + sec ["binary" ])
428+ fsize , padlen = getImageSize (sec ["binary" ])
414429 # update the size and padding information
415430 sec ["size" ] = fsize
416431 sec ["padlen" ] = padlen
@@ -420,11 +435,11 @@ def calcOemManagedArea(fwsections):
420435 compressLabel = "uncompressed"
421436 if "COMPRESS" in sec ["flags" ]:
422437 compressLabel = "compressed"
423- compressImage ("build/images/" + sec ["binary" ])
438+ compressImage (sec ["binary" ])
424439 # we should have the (new) compressed file, so we will check if this is the case...
425440
426441 # determine the size of (compressed) file and if padding is needed
427- fsize , padlen = getImageSize ("build/images/" + sec ["binary" ] + ".lzf" )
442+ fsize , padlen = getImageSize (sec ["binary" ] + ".lzf" )
428443 # add compressed file size and update padding information
429444 # (the compressed image will be included in the package so pad should be calculated for
430445 # compressed file size)
@@ -601,16 +616,27 @@ def validateVersAttr(version):
601616
602617
603618def updateDeviceConfig (file ):
619+ file = paths .CONFIG_INPUT_DIR / file
604620 # print('*** updateDeviceConfig: ', file)
605621 # Update the firewall configuration in the OEM DEVICE config file
606- update_fw_cfg_oem (file )
622+ # This doesn't do anything except format the json file.
623+ # update_fw_cfg_oem(file)
607624
608- cfg = read_global_config ("build/config/" + file )
625+ cfg = read_global_config (file )
609626 if "miscellaneous" in cfg :
627+ unwanted_item = False
610628 for item in cfg ["miscellaneous" ]:
611- item .pop ("sdesc" , None )
612- item .pop ("ldesc" , None )
613- item .pop ("options" , None )
629+ if "sdesc" in item or "ldesc" in item or "options" in item :
630+ unwanted_item = True
631+ if unwanted_item :
632+ print (
633+ "[ERROR] File '"
634+ + file .as_posix ()
635+ + "' should not contain 'miscellaneous' entry"
636+ )
637+ sys .exit (EXIT_WITH_ERROR )
638+ # Don't write out the json file, because it didn't change.
639+ return
614640 with open ("build/config/" + file , "w" ) as json_file :
615641 json .dump (cfg , json_file , indent = 2 )
616642
@@ -658,9 +684,34 @@ def main():
658684 action = "store_true" ,
659685 )
660686 parser .add_argument ("-v" , "--verbose" , help = "verbosity mode" , action = "store_true" )
687+ parser .add_argument (
688+ "--config-dir" ,
689+ type = str ,
690+ default = Path (os .path .dirname (__file__ )) / "build/config" ,
691+ help = "directory with configuration files" ,
692+ )
693+ parser .add_argument (
694+ "--output-dir" ,
695+ type = str ,
696+ default = "" ,
697+ help = "directory to place output files" ,
698+ )
699+ parser .add_argument (
700+ "--firmware-dir" ,
701+ type = str ,
702+ default = "" ,
703+ help = "directory contianing firmware files" ,
704+ )
661705
662706 args = parser .parse_args ()
663707
708+ # Set paths given on command line.
709+ paths .TOOLKIT_DIR = Path (os .path .dirname (__file__ ))
710+ paths .CERT_INPUT_DIR = paths .TOOLKIT_DIR / "cert"
711+ paths .CONFIG_INPUT_DIR = Path (args .config_dir )
712+ paths .FIRMWARE_INPUT_DIR = Path (args .firmware_dir )
713+ paths .OUTPUT_DIR = Path (args .output_dir )
714+
664715 if args .version :
665716 print (TOOL_VERSION )
666717 sys .exit ()
@@ -715,12 +766,15 @@ def main():
715766
716767 # update the size and padding information
717768 if sec ["identifier" ].strip ("\0 " ) != "DEVICE" :
769+ sec ["binary" ] = (paths .FIRMWARE_INPUT_DIR / sec ["binary" ]).as_posix ()
718770 continue
719771
720772 print ("Generating Device Configuration for: " + sec ["binary" ])
721773 updateDeviceConfig (sec ["binary" ])
722774 gen_device_config (sec ["binary" ], False )
723- sec ["binary" ] = sec ["binary" ][:- 5 ] + ".bin"
775+ sec ["binary" ] = (
776+ (paths .OUTPUT_DIR / sec ["binary" ]).with_suffix (".bin" ).as_posix ()
777+ )
724778
725779 # also check unmanaged images (mramAddress != 0) are between boundaries (OEM_BASE_ADDRESS - only in Rev_A as in Rev_B will be 0)
726780 # and images don't overlap... Also, advice is GAPs (big ones) exist - especially if tool can't create the layout...
@@ -746,7 +800,6 @@ def main():
746800
747801 sign_image (args .output , oemManagedAreaStartAddress , 0xFFFFFFFF , "OEM" )
748802
749- print ("Done!" )
750803 return 0
751804
752805
0 commit comments