From 39f4a85bb2dd5915ebc86921c34da26faa278fec Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sat, 7 Jan 2023 14:07:13 -0700 Subject: binman: Add a way to check for a valid ELF file Add a function which checks whether data is in ELF format or not. This will be used by binman to check this for entries. Signed-off-by: Simon Glass --- tools/binman/elf.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'tools/binman/elf.py') diff --git a/tools/binman/elf.py b/tools/binman/elf.py index fe50bf542c3..73f318b81d2 100644 --- a/tools/binman/elf.py +++ b/tools/binman/elf.py @@ -518,3 +518,18 @@ def read_loadable_segments(data): rend = start + segment['p_filesz'] segments.append((i, segment['p_paddr'], data[start:rend])) return segments, entry + +def is_valid(data): + """Check if some binary data is a valid ELF file + + Args: + data (bytes): Bytes to check + + Returns: + bool: True if a valid Elf file, False if not + """ + try: + DecodeElf(data, 0) + return True + except ELFError: + return False -- cgit v1.2.3 From c1157860c5e9ca45e41859e013ed83919e7397f0 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Wed, 11 Jan 2023 16:10:17 -0700 Subject: binman: Provide general support for updating ELF symbols The current support for updating variables in a binary is hard-coded to work with U-Boot: - It assumes the image starts at __image_copy_start - It uses the existing U-Boot-specific entry types It is useful for other projects to use these feature. Add properties to enable writing symbols for any blob, a way of specifying the base symbol and a way of providing the ELF filename to allow symbol lookup to take place. With this it is possible to update a Zephyr image, such as zephyr.bin after it has been built. Signed-off-by: Simon Glass --- tools/binman/elf.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'tools/binman/elf.py') diff --git a/tools/binman/elf.py b/tools/binman/elf.py index 73f318b81d2..9ac00ed9ccf 100644 --- a/tools/binman/elf.py +++ b/tools/binman/elf.py @@ -210,7 +210,8 @@ def GetPackString(sym, msg): raise ValueError('%s has size %d: only 4 and 8 are supported' % (msg, sym.size)) -def LookupAndWriteSymbols(elf_fname, entry, section, is_elf=False): +def LookupAndWriteSymbols(elf_fname, entry, section, is_elf=False, + base_sym=None): """Replace all symbols in an entry with their correct values The entry contents is updated so that values for referenced symbols will be @@ -223,7 +224,10 @@ def LookupAndWriteSymbols(elf_fname, entry, section, is_elf=False): entry entry: Entry to process section: Section which can be used to lookup symbol values + base_sym: Base symbol marking the start of the image """ + if not base_sym: + base_sym = '__image_copy_start' fname = tools.get_input_filename(elf_fname) syms = GetSymbols(fname, ['image', 'binman']) if is_elf: @@ -243,7 +247,7 @@ def LookupAndWriteSymbols(elf_fname, entry, section, is_elf=False): if not syms: tout.debug('LookupAndWriteSymbols: no syms') return - base = syms.get('__image_copy_start') + base = syms.get(base_sym) if not base and not is_elf: tout.debug('LookupAndWriteSymbols: no base') return -- cgit v1.2.3 From 571bc4e67d39e4c376f8bab0d6518ab5ee832d9e Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Wed, 11 Jan 2023 16:10:19 -0700 Subject: binman: Support positioning an entry by and ELF symbol MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In some cases it is useful to position an entry over the top of a symbol in an ELF file. For example, if the symbol holds a version string then it allows the string to be accessed from the fdtmap. Add support for this. Suggested-by: Pali Rohár Suggested-by: Keith Short Signed-off-by: Simon Glass --- tools/binman/elf.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'tools/binman/elf.py') diff --git a/tools/binman/elf.py b/tools/binman/elf.py index 9ac00ed9ccf..3cc8a384495 100644 --- a/tools/binman/elf.py +++ b/tools/binman/elf.py @@ -210,6 +210,29 @@ def GetPackString(sym, msg): raise ValueError('%s has size %d: only 4 and 8 are supported' % (msg, sym.size)) +def GetSymbolOffset(elf_fname, sym_name, base_sym=None): + """Read the offset of a symbol compared to base symbol + + This is useful for obtaining the value of a single symbol relative to the + base of a binary blob. + + Args: + elf_fname: Filename of the ELF file to read + sym_name (str): Name of symbol to read + base_sym (str): Base symbol to sue to calculate the offset (or None to + use '__image_copy_start' + + Returns: + int: Offset of the symbol relative to the base symbol + """ + if not base_sym: + base_sym = '__image_copy_start' + fname = tools.get_input_filename(elf_fname) + syms = GetSymbols(fname, [base_sym, sym_name]) + base = syms[base_sym].address + val = syms[sym_name].address + return val - base + def LookupAndWriteSymbols(elf_fname, entry, section, is_elf=False, base_sym=None): """Replace all symbols in an entry with their correct values -- cgit v1.2.3