11#! /usr/bin/env python3
22#
33# Copyright 2017 Linaro Limited
4+ # Copyright 2019 Arm Limited
45#
56# Licensed under the Apache License, Version 2.0 (the "License");
67# you may not use this file except in compliance with the License.
1415# See the License for the specific language governing permissions and
1516# limitations under the License.
1617
18+ import re
1719import click
1820import getpass
1921import imgtool .keys as keys
@@ -106,6 +108,29 @@ def validate_header_size(ctx, param, value):
106108 return value
107109
108110
111+ def get_dependencies (ctx , param , value ):
112+ if value is not None :
113+ versions = []
114+ images = re .findall (r"\((\d+)" , value )
115+ if len (images ) == 0 :
116+ raise click .BadParameter (
117+ "Image dependency format is invalid: {}" .format (value ))
118+ raw_versions = re .findall (r",\s*([0-9.+]+)\)" , value )
119+ if len (images ) != len (raw_versions ):
120+ raise click .BadParameter (
121+ '''There's a mismatch between the number of dependency images
122+ and versions in: {}''' .format (value ))
123+ for raw_version in raw_versions :
124+ try :
125+ versions .append (decode_version (raw_version ))
126+ except ValueError as e :
127+ raise click .BadParameter ("{}" .format (e ))
128+ dependencies = dict ()
129+ dependencies [image .DEP_IMAGES_KEY ] = images
130+ dependencies [image .DEP_VERSIONS_KEY ] = versions
131+ return dependencies
132+
133+
109134class BasedIntParamType (click .ParamType ):
110135 name = 'integer'
111136
@@ -138,6 +163,9 @@ def convert(self, value, param, ctx):
138163 help = 'Add --header-size zeroed bytes at the beginning of the image' )
139164@click .option ('-H' , '--header-size' , callback = validate_header_size ,
140165 type = BasedIntParamType (), required = True )
166+ @click .option ('-d' , '--dependencies' , callback = get_dependencies ,
167+ required = False , help = '''Add dependence on another image, format:
168+ "(<image_ID>,<image_version>), ... "''' )
141169@click .option ('-v' , '--version' , callback = validate_version , required = True )
142170@click .option ('--align' , type = click .Choice (['1' , '2' , '4' , '8' ]),
143171 required = True )
@@ -146,7 +174,8 @@ def convert(self, value, param, ctx):
146174 INFILE and OUTFILE are parsed as Intel HEX if the params have
147175 .hex extension, othewise binary format is used''' )
148176def sign (key , align , version , header_size , pad_header , slot_size , pad ,
149- max_sectors , overwrite_only , endian , encrypt , infile , outfile ):
177+ max_sectors , overwrite_only , endian , encrypt , infile , outfile ,
178+ dependencies ):
150179 img = image .Image (version = decode_version (version ), header_size = header_size ,
151180 pad_header = pad_header , pad = pad , align = int (align ),
152181 slot_size = slot_size , max_sectors = max_sectors ,
@@ -159,7 +188,7 @@ def sign(key, align, version, header_size, pad_header, slot_size, pad,
159188 raise Exception ("Encryption only available with RSA key" )
160189 if key and not isinstance (key , keys .RSA2048 ):
161190 raise Exception ("Signing only available with private RSA key" )
162- img .create (key , enckey )
191+ img .create (key , enckey , dependencies )
163192 img .save (outfile )
164193
165194
0 commit comments