Skip to content

Commit 5da7ba5

Browse files
yonschnashif
authored andcommitted
west: bindesc: Added get_offset command
Add `west bindesc get_offset` command to print the offset of the descriptors inside the given image. Signed-off-by: Yonatan Schachter <[email protected]>
1 parent c8c25b5 commit 5da7ba5

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

doc/develop/west/zephyr-cmds.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,7 @@ You can dump all of the descriptors in an image using::
215215
You can list all known standard descriptor names using::
216216

217217
west bindesc list
218+
219+
You can print the offset of the descriptors inside the image using::
220+
221+
west bindesc get_offset

scripts/west_commands/bindesc.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ def do_add_parser(self, parser_adder):
145145
list_parser = subparsers.add_parser('list', help='List all known descriptors')
146146
list_parser.set_defaults(subcmd='list', big_endian=False)
147147

148+
get_offset_parser = subparsers.add_parser('get_offset', help='Get the offset of the descriptors')
149+
get_offset_parser.add_argument('file', type=str, help='Executable file')
150+
get_offset_parser.add_argument('--file-type', type=str, choices=self.EXTENSIONS,
151+
help='File type')
152+
get_offset_parser.add_argument('-b', '--big-endian', action='store_true',
153+
help='Target CPU is big endian')
154+
get_offset_parser.set_defaults(subcmd='get_offset', big_endian=False)
148155
return parser
149156

150157
def dump(self, args):
@@ -188,6 +195,15 @@ def custom_search(self, args):
188195
custom_tag = self.bindesc_gen_tag(custom_type, int(args.id, 16))
189196
self.common_search(args, custom_tag)
190197

198+
def get_offset(self, args):
199+
image = self.get_image_data(args.file)
200+
201+
magic = struct.pack('>Q' if self.is_big_endian else 'Q', self.MAGIC)
202+
index = image.find(magic)
203+
if index == -1:
204+
log.die('Could not find binary descriptor magic')
205+
log.inf(f'{index} {hex(index)}')
206+
191207
def do_run(self, args, _):
192208
if MISSING_REQUIREMENTS:
193209
raise RuntimeError('one or more Python dependencies were missing; '

0 commit comments

Comments
 (0)