2018-05-06 21:58:06 +00:00
|
|
|
# SPDX-License-Identifier: GPL-2.0+
|
2017-11-14 01:54:54 +00:00
|
|
|
# Copyright (c) 2017 Google, Inc
|
|
|
|
# Written by Simon Glass <sjg@chromium.org>
|
|
|
|
#
|
|
|
|
# Test for the elf module
|
|
|
|
|
|
|
|
import os
|
2019-07-08 19:18:34 +00:00
|
|
|
import shutil
|
2021-11-04 03:09:16 +00:00
|
|
|
import struct
|
2017-11-14 01:54:54 +00:00
|
|
|
import sys
|
2019-07-08 19:18:34 +00:00
|
|
|
import tempfile
|
2017-11-14 01:54:54 +00:00
|
|
|
import unittest
|
|
|
|
|
2020-04-18 00:09:03 +00:00
|
|
|
from binman import elf
|
2020-04-18 00:09:04 +00:00
|
|
|
from patman import command
|
|
|
|
from patman import test_util
|
|
|
|
from patman import tools
|
|
|
|
from patman import tout
|
2017-11-14 01:54:54 +00:00
|
|
|
|
|
|
|
binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FakeEntry:
|
2018-07-17 19:25:26 +00:00
|
|
|
"""A fake Entry object, usedfor testing
|
|
|
|
|
|
|
|
This supports an entry with a given size.
|
|
|
|
"""
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
def __init__(self, contents_size):
|
|
|
|
self.contents_size = contents_size
|
2022-01-29 21:14:04 +00:00
|
|
|
self.data = tools.get_bytes(ord('a'), contents_size)
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
|
|
|
|
def GetPath(self):
|
|
|
|
return 'entry_path'
|
|
|
|
|
2018-07-17 19:25:26 +00:00
|
|
|
|
2018-06-01 15:38:13 +00:00
|
|
|
class FakeSection:
|
2018-07-17 19:25:26 +00:00
|
|
|
"""A fake Section object, used for testing
|
|
|
|
|
|
|
|
This has the minimum feature set needed to support testing elf functions.
|
|
|
|
A LookupSymbol() function is provided which returns a fake value for amu
|
|
|
|
symbol requested.
|
|
|
|
"""
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
def __init__(self, sym_value=1):
|
|
|
|
self.sym_value = sym_value
|
|
|
|
|
|
|
|
def GetPath(self):
|
2018-06-01 15:38:13 +00:00
|
|
|
return 'section_path'
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
|
2021-01-07 04:35:15 +00:00
|
|
|
def LookupImageSymbol(self, name, weak, msg, base_addr):
|
2018-07-17 19:25:26 +00:00
|
|
|
"""Fake implementation which returns the same value for all symbols"""
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
return self.sym_value
|
2017-11-14 01:54:54 +00:00
|
|
|
|
2021-01-07 04:35:15 +00:00
|
|
|
def GetImage(self):
|
|
|
|
return self
|
2018-07-17 19:25:26 +00:00
|
|
|
|
2019-08-24 13:22:53 +00:00
|
|
|
def BuildElfTestFiles(target_dir):
|
|
|
|
"""Build ELF files used for testing in binman
|
|
|
|
|
|
|
|
This compiles and links the test files into the specified directory. It the
|
|
|
|
Makefile and source files in the binman test/ directory.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
target_dir: Directory to put the files into
|
|
|
|
"""
|
|
|
|
if not os.path.exists(target_dir):
|
|
|
|
os.mkdir(target_dir)
|
|
|
|
testdir = os.path.join(binman_dir, 'test')
|
|
|
|
|
|
|
|
# If binman is involved from the main U-Boot Makefile the -r and -R
|
|
|
|
# flags are set in MAKEFLAGS. This prevents this Makefile from working
|
|
|
|
# correctly. So drop any make flags here.
|
|
|
|
if 'MAKEFLAGS' in os.environ:
|
|
|
|
del os.environ['MAKEFLAGS']
|
2021-11-04 03:09:15 +00:00
|
|
|
try:
|
2022-01-29 21:14:04 +00:00
|
|
|
tools.run('make', '-C', target_dir, '-f',
|
2021-11-04 03:09:15 +00:00
|
|
|
os.path.join(testdir, 'Makefile'), 'SRC=%s/' % testdir)
|
|
|
|
except ValueError as e:
|
|
|
|
# The test system seems to suppress this in a strange way
|
|
|
|
print(e)
|
2019-08-24 13:22:53 +00:00
|
|
|
|
|
|
|
|
2017-11-14 01:54:54 +00:00
|
|
|
class TestElf(unittest.TestCase):
|
2018-10-02 03:12:41 +00:00
|
|
|
@classmethod
|
2019-08-24 13:22:54 +00:00
|
|
|
def setUpClass(cls):
|
|
|
|
cls._indir = tempfile.mkdtemp(prefix='elf.')
|
2022-01-29 21:14:04 +00:00
|
|
|
tools.set_input_dirs(['.'])
|
2019-08-24 13:22:54 +00:00
|
|
|
BuildElfTestFiles(cls._indir)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
if cls._indir:
|
|
|
|
shutil.rmtree(cls._indir)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def ElfTestFile(cls, fname):
|
|
|
|
return os.path.join(cls._indir, fname)
|
2018-10-02 03:12:41 +00:00
|
|
|
|
2017-11-14 01:54:54 +00:00
|
|
|
def testAllSymbols(self):
|
2018-07-17 19:25:26 +00:00
|
|
|
"""Test that we can obtain a symbol from the ELF file"""
|
2019-08-24 13:22:54 +00:00
|
|
|
fname = self.ElfTestFile('u_boot_ucode_ptr')
|
2017-11-14 01:54:54 +00:00
|
|
|
syms = elf.GetSymbols(fname, [])
|
2022-01-10 03:13:37 +00:00
|
|
|
self.assertIn('_dt_ucode_base_size', syms)
|
2017-11-14 01:54:54 +00:00
|
|
|
|
|
|
|
def testRegexSymbols(self):
|
2018-07-17 19:25:26 +00:00
|
|
|
"""Test that we can obtain from the ELF file by regular expression"""
|
2019-08-24 13:22:54 +00:00
|
|
|
fname = self.ElfTestFile('u_boot_ucode_ptr')
|
2017-11-14 01:54:54 +00:00
|
|
|
syms = elf.GetSymbols(fname, ['ucode'])
|
2022-01-10 03:13:37 +00:00
|
|
|
self.assertIn('_dt_ucode_base_size', syms)
|
2017-11-14 01:54:54 +00:00
|
|
|
syms = elf.GetSymbols(fname, ['missing'])
|
2022-01-10 03:13:37 +00:00
|
|
|
self.assertNotIn('_dt_ucode_base_size', syms)
|
2017-11-14 01:54:54 +00:00
|
|
|
syms = elf.GetSymbols(fname, ['missing', 'ucode'])
|
2022-01-10 03:13:37 +00:00
|
|
|
self.assertIn('_dt_ucode_base_size', syms)
|
2017-11-14 01:54:54 +00:00
|
|
|
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
def testMissingFile(self):
|
2018-07-17 19:25:26 +00:00
|
|
|
"""Test that a missing file is detected"""
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
entry = FakeEntry(10)
|
2018-06-01 15:38:13 +00:00
|
|
|
section = FakeSection()
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
with self.assertRaises(ValueError) as e:
|
2018-06-01 15:38:13 +00:00
|
|
|
syms = elf.LookupAndWriteSymbols('missing-file', entry, section)
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
self.assertIn("Filename 'missing-file' not found in input path",
|
|
|
|
str(e.exception))
|
|
|
|
|
|
|
|
def testOutsideFile(self):
|
2018-07-17 19:25:26 +00:00
|
|
|
"""Test a symbol which extends outside the entry area is detected"""
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
entry = FakeEntry(10)
|
2018-06-01 15:38:13 +00:00
|
|
|
section = FakeSection()
|
2019-08-24 13:22:56 +00:00
|
|
|
elf_fname = self.ElfTestFile('u_boot_binman_syms')
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
with self.assertRaises(ValueError) as e:
|
2018-06-01 15:38:13 +00:00
|
|
|
syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
self.assertIn('entry_path has offset 4 (size 8) but the contents size '
|
|
|
|
'is a', str(e.exception))
|
|
|
|
|
|
|
|
def testMissingImageStart(self):
|
2018-07-17 19:25:26 +00:00
|
|
|
"""Test that we detect a missing __image_copy_start symbol
|
|
|
|
|
|
|
|
This is needed to mark the start of the image. Without it we cannot
|
|
|
|
locate the offset of a binman symbol within the image.
|
|
|
|
"""
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
entry = FakeEntry(10)
|
2018-06-01 15:38:13 +00:00
|
|
|
section = FakeSection()
|
2019-08-24 13:22:58 +00:00
|
|
|
elf_fname = self.ElfTestFile('u_boot_binman_syms_bad')
|
2018-06-01 15:38:13 +00:00
|
|
|
self.assertEqual(elf.LookupAndWriteSymbols(elf_fname, entry, section),
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
None)
|
|
|
|
|
|
|
|
def testBadSymbolSize(self):
|
2018-07-17 19:25:26 +00:00
|
|
|
"""Test that an attempt to use an 8-bit symbol are detected
|
|
|
|
|
|
|
|
Only 32 and 64 bits are supported, since we need to store an offset
|
|
|
|
into the image.
|
|
|
|
"""
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
entry = FakeEntry(10)
|
2018-06-01 15:38:13 +00:00
|
|
|
section = FakeSection()
|
2019-08-24 13:22:57 +00:00
|
|
|
elf_fname =self.ElfTestFile('u_boot_binman_syms_size')
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
with self.assertRaises(ValueError) as e:
|
2018-06-01 15:38:13 +00:00
|
|
|
syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
self.assertIn('has size 1: only 4 and 8 are supported',
|
|
|
|
str(e.exception))
|
|
|
|
|
|
|
|
def testNoValue(self):
|
2018-07-17 19:25:26 +00:00
|
|
|
"""Test the case where we have no value for the symbol
|
|
|
|
|
|
|
|
This should produce -1 values for all thress symbols, taking up the
|
|
|
|
first 16 bytes of the image.
|
|
|
|
"""
|
2019-08-24 13:23:05 +00:00
|
|
|
entry = FakeEntry(24)
|
2018-06-01 15:38:13 +00:00
|
|
|
section = FakeSection(sym_value=None)
|
2019-08-24 13:22:56 +00:00
|
|
|
elf_fname = self.ElfTestFile('u_boot_binman_syms')
|
2018-06-01 15:38:13 +00:00
|
|
|
syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
|
2022-01-29 21:14:04 +00:00
|
|
|
self.assertEqual(tools.get_bytes(255, 20) + tools.get_bytes(ord('a'), 4),
|
2019-05-18 04:00:46 +00:00
|
|
|
entry.data)
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
|
|
|
|
def testDebug(self):
|
2018-07-17 19:25:26 +00:00
|
|
|
"""Check that enabling debug in the elf module produced debug output"""
|
2019-07-20 18:23:36 +00:00
|
|
|
try:
|
2022-01-29 21:14:15 +00:00
|
|
|
tout.init(tout.DEBUG)
|
2019-07-20 18:23:36 +00:00
|
|
|
entry = FakeEntry(20)
|
|
|
|
section = FakeSection()
|
2019-08-24 13:22:56 +00:00
|
|
|
elf_fname = self.ElfTestFile('u_boot_binman_syms')
|
2019-07-20 18:23:36 +00:00
|
|
|
with test_util.capture_sys_output() as (stdout, stderr):
|
|
|
|
syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
|
|
|
|
self.assertTrue(len(stdout.getvalue()) > 0)
|
|
|
|
finally:
|
2022-01-29 21:14:15 +00:00
|
|
|
tout.init(tout.WARNING)
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
|
2019-07-08 19:18:34 +00:00
|
|
|
def testMakeElf(self):
|
|
|
|
"""Test for the MakeElf function"""
|
|
|
|
outdir = tempfile.mkdtemp(prefix='elf.')
|
|
|
|
expected_text = b'1234'
|
|
|
|
expected_data = b'wxyz'
|
|
|
|
elf_fname = os.path.join(outdir, 'elf')
|
2019-08-24 13:22:45 +00:00
|
|
|
bin_fname = os.path.join(outdir, 'bin')
|
2019-07-08 19:18:34 +00:00
|
|
|
|
|
|
|
# Make an Elf file and then convert it to a fkat binary file. This
|
|
|
|
# should produce the original data.
|
|
|
|
elf.MakeElf(elf_fname, expected_text, expected_data)
|
2022-01-29 21:14:04 +00:00
|
|
|
objcopy, args = tools.get_target_compile_tool('objcopy')
|
binman: Use target-specific tools when cross-compiling
Currently, binman always runs the compile tools like cc, objcopy, strip,
etc. using their literal name. Instead, this patch makes it use the
target-specific versions by default, derived from the tool-specific
environment variables (CC, OBJCOPY, STRIP, etc.) or from the
CROSS_COMPILE environment variable.
For example, the u-boot-elf etype directly uses 'strip'. Trying to run
the tests with 'CROSS_COMPILE=i686-linux-gnu- binman test' on an arm64
host results in the '097_elf_strip.dts' test to fail as the arm64
version of 'strip' can't understand the format of the x86 ELF file.
This also adjusts some command.Output() calls that caused test errors or
failures to use the target versions of the tools they call. After this,
patch, an arm64 host can run all tests with no errors or failures using
a correct CROSS_COMPILE value.
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
2020-09-06 11:46:05 +00:00
|
|
|
args += ['-O', 'binary', elf_fname, bin_fname]
|
2022-01-29 21:14:05 +00:00
|
|
|
stdout = command.output(objcopy, *args)
|
2019-07-08 19:18:34 +00:00
|
|
|
with open(bin_fname, 'rb') as fd:
|
|
|
|
data = fd.read()
|
|
|
|
self.assertEqual(expected_text + expected_data, data)
|
|
|
|
shutil.rmtree(outdir)
|
|
|
|
|
2019-07-08 19:18:35 +00:00
|
|
|
def testDecodeElf(self):
|
|
|
|
"""Test for the MakeElf function"""
|
|
|
|
if not elf.ELF_TOOLS:
|
|
|
|
self.skipTest('Python elftools not available')
|
|
|
|
outdir = tempfile.mkdtemp(prefix='elf.')
|
|
|
|
expected_text = b'1234'
|
|
|
|
expected_data = b'wxyz'
|
|
|
|
elf_fname = os.path.join(outdir, 'elf')
|
|
|
|
elf.MakeElf(elf_fname, expected_text, expected_data)
|
2022-01-29 21:14:04 +00:00
|
|
|
data = tools.read_file(elf_fname)
|
2019-07-08 19:18:35 +00:00
|
|
|
|
|
|
|
load = 0xfef20000
|
|
|
|
entry = load + 2
|
|
|
|
expected = expected_text + expected_data
|
|
|
|
self.assertEqual(elf.ElfInfo(expected, load, entry, len(expected)),
|
|
|
|
elf.DecodeElf(data, 0))
|
|
|
|
self.assertEqual(elf.ElfInfo(b'\0\0' + expected[2:],
|
|
|
|
load, entry, len(expected)),
|
|
|
|
elf.DecodeElf(data, load + 2))
|
2019-08-24 13:22:54 +00:00
|
|
|
shutil.rmtree(outdir)
|
2019-07-08 19:18:35 +00:00
|
|
|
|
2021-11-04 03:09:16 +00:00
|
|
|
def testEmbedData(self):
|
|
|
|
"""Test for the GetSymbolFileOffset() function"""
|
|
|
|
if not elf.ELF_TOOLS:
|
|
|
|
self.skipTest('Python elftools not available')
|
|
|
|
|
|
|
|
fname = self.ElfTestFile('embed_data')
|
|
|
|
offset = elf.GetSymbolFileOffset(fname, ['embed_start', 'embed_end'])
|
|
|
|
start = offset['embed_start'].offset
|
|
|
|
end = offset['embed_end'].offset
|
2022-01-29 21:14:04 +00:00
|
|
|
data = tools.read_file(fname)
|
2021-11-04 03:09:16 +00:00
|
|
|
embed_data = data[start:end]
|
|
|
|
expect = struct.pack('<III', 0x1234, 0x5678, 0)
|
|
|
|
self.assertEqual(expect, embed_data)
|
|
|
|
|
|
|
|
def testEmbedFail(self):
|
|
|
|
"""Test calling GetSymbolFileOffset() without elftools"""
|
|
|
|
try:
|
|
|
|
old_val = elf.ELF_TOOLS
|
|
|
|
elf.ELF_TOOLS = False
|
|
|
|
fname = self.ElfTestFile('embed_data')
|
|
|
|
with self.assertRaises(ValueError) as e:
|
|
|
|
elf.GetSymbolFileOffset(fname, ['embed_start', 'embed_end'])
|
|
|
|
self.assertIn('Python elftools package is not available',
|
|
|
|
str(e.exception))
|
|
|
|
finally:
|
|
|
|
elf.ELF_TOOLS = old_val
|
|
|
|
|
|
|
|
def testEmbedDataNoSym(self):
|
|
|
|
"""Test for GetSymbolFileOffset() getting no symbols"""
|
|
|
|
if not elf.ELF_TOOLS:
|
|
|
|
self.skipTest('Python elftools not available')
|
|
|
|
|
|
|
|
fname = self.ElfTestFile('embed_data')
|
|
|
|
offset = elf.GetSymbolFileOffset(fname, ['missing_sym'])
|
|
|
|
self.assertEqual({}, offset)
|
|
|
|
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
|
2017-11-14 01:54:54 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|