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
|
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
|
2019-05-18 04:00:46 +00:00
|
|
|
self.data = tools.GetBytes(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
|
|
|
|
2019-11-07 00:22:44 +00:00
|
|
|
def LookupSymbol(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
|
|
|
|
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']
|
|
|
|
tools.Run('make', '-C', target_dir, '-f',
|
2019-08-24 13:22:59 +00:00
|
|
|
os.path.join(testdir, 'Makefile'), 'SRC=%s/' % testdir)
|
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.')
|
2018-10-02 03:12:41 +00:00
|
|
|
tools.SetInputDirs(['.'])
|
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, [])
|
|
|
|
self.assertIn('.ucode', syms)
|
|
|
|
|
|
|
|
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'])
|
|
|
|
self.assertIn('.ucode', syms)
|
|
|
|
syms = elf.GetSymbols(fname, ['missing'])
|
|
|
|
self.assertNotIn('.ucode', syms)
|
|
|
|
syms = elf.GetSymbols(fname, ['missing', 'ucode'])
|
|
|
|
self.assertIn('.ucode', 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
|
|
|
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)
|
2019-08-24 13:23:05 +00:00
|
|
|
self.assertEqual(tools.GetBytes(255, 20) + tools.GetBytes(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:
|
|
|
|
tout.Init(tout.DEBUG)
|
|
|
|
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:
|
|
|
|
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)
|
|
|
|
stdout = command.Output('objcopy', '-O', 'binary', elf_fname, bin_fname)
|
|
|
|
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)
|
|
|
|
data = tools.ReadFile(elf_fname)
|
|
|
|
|
|
|
|
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
|
|
|
|
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()
|