Skip to content

Commit 338a8b1

Browse files
pillo79jhedberg
authored andcommitted
west: bindesc: add extract subcommand
Add a new 'west bindesc extract' subcommand that dumps all binary descriptors into a separate binary file. It will also report the range (offset and length) of the dumped data within the original image. Signed-off-by: Luca Burelli <[email protected]>
1 parent 0a5427b commit 338a8b1

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

doc/develop/west/zephyr-cmds.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ You can dump all of the descriptors in an image using::
252252

253253
west bindesc dump build/zephyr/zephyr.bin
254254

255+
You can extract the descriptor data area of the image to a file using::
256+
257+
west bindesc extract
258+
255259
You can list all known standard descriptor names using::
256260

257261
west bindesc list

scripts/west_commands/bindesc.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ def do_add_parser(self, parser_adder):
120120
help='Target CPU is big endian')
121121
dump_parser.set_defaults(subcmd='dump', big_endian=False)
122122

123+
extract_parser = subparsers.add_parser('extract',
124+
help='Extract the binary descriptor blob to a file')
125+
extract_parser.add_argument('file', type=str, help='Executable file')
126+
extract_parser.add_argument('out_file', type=str, help='Bindesc binary dump file')
127+
extract_parser.add_argument('--file-type', type=str, choices=self.EXTENSIONS,
128+
help='Input file type')
129+
extract_parser.add_argument('-b', '--big-endian', action='store_true',
130+
help='Target CPU is big endian')
131+
extract_parser.set_defaults(subcmd='extract', big_endian=False)
132+
123133
search_parser = subparsers.add_parser('search', help='Search for a specific descriptor')
124134
search_parser.add_argument('descriptor', type=str, help='Descriptor name')
125135
search_parser.add_argument('file', type=str, help='Executable file')
@@ -204,6 +214,29 @@ def get_offset(self, args):
204214
self.die('Could not find binary descriptor magic')
205215
self.inf(f'{index} {hex(index)}')
206216

217+
def extract(self, args):
218+
image = self.get_image_data(args.file)
219+
220+
magic = struct.pack('>Q' if self.is_big_endian else 'Q', self.MAGIC)
221+
index = image.find(magic)
222+
if index == -1:
223+
self.die('Could not find binary descriptor magic')
224+
225+
index += len(magic) # index points to first descriptor
226+
block_start = index
227+
current_tag = self.bytes_to_short(image[index:index+2])
228+
while current_tag != self.DESCRIPTORS_END:
229+
index += 2 # index points to length
230+
length = self.bytes_to_short(image[index:index+2])
231+
# go to next tag
232+
index = self.align(index + 2 + length, 4)
233+
current_tag = self.bytes_to_short(image[index:index+2])
234+
block_len = index - block_start
235+
236+
with open(args.out_file, 'wb') as out_file:
237+
out_file.write(image[block_start:index])
238+
self.inf(f'{block_start}+{block_len} {hex(block_start)}+{hex(block_len)}')
239+
207240
def do_run(self, args, _):
208241
if MISSING_REQUIREMENTS:
209242
raise RuntimeError('one or more Python dependencies were missing; '

0 commit comments

Comments
 (0)