binman: Handle repeated bytes for Python 3

The method of multiplying a character by a number works well for creating
a repeated string in Python 2. But in Python 3 we need to use bytes()
instead, to avoid unicode problems, since 'bytes' is no-longer just an
alias of 'str'.

Create a function to handle this detail and call it from the relevant
places in binman.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass 2019-05-14 15:53:47 -06:00
parent 6d1d641864
commit e6d85ff9f2
6 changed files with 50 additions and 25 deletions

View file

@ -332,7 +332,7 @@ class Section(object):
def GetData(self): def GetData(self):
"""Get the contents of the section""" """Get the contents of the section"""
section_data = chr(self._pad_byte) * self._size section_data = tools.GetBytes(self._pad_byte, self._size)
for entry in self._entries.values(): for entry in self._entries.values():
data = entry.GetData() data = entry.GetData()

View file

@ -122,7 +122,7 @@ class TestElf(unittest.TestCase):
section = FakeSection(sym_value=None) section = FakeSection(sym_value=None)
elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms') elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
syms = elf.LookupAndWriteSymbols(elf_fname, entry, section) syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
self.assertEqual(chr(255) * 16 + 'a' * 4, entry.data) self.assertEqual(tools.GetBytes(255, 16) + 'a' * 4, entry.data)
def testDebug(self): def testDebug(self):
"""Check that enabling debug in the elf module produced debug output""" """Check that enabling debug in the elf module produced debug output"""

View file

@ -5,7 +5,7 @@
from entry import Entry from entry import Entry
import fdt_util import fdt_util
import tools
class Entry_fill(Entry): class Entry_fill(Entry):
"""An entry which is filled to a particular byte value """An entry which is filled to a particular byte value
@ -28,5 +28,5 @@ class Entry_fill(Entry):
self.fill_value = fdt_util.GetByte(self._node, 'fill-byte', 0) self.fill_value = fdt_util.GetByte(self._node, 'fill-byte', 0)
def ObtainContents(self): def ObtainContents(self):
self.SetContents(chr(self.fill_value) * self.size) self.SetContents(tools.GetBytes(self.fill_value, self.size))
return True return True

View file

@ -38,5 +38,5 @@ class Entry_u_boot_spl_bss_pad(Entry_blob):
bss_size = elf.GetSymbolAddress(fname, '__bss_size') bss_size = elf.GetSymbolAddress(fname, '__bss_size')
if not bss_size: if not bss_size:
self.Raise('Expected __bss_size symbol in spl/u-boot-spl') self.Raise('Expected __bss_size symbol in spl/u-boot-spl')
self.SetContents(chr(0) * bss_size) self.SetContents(tools.GetBytes(0, bss_size))
return True return True

View file

@ -551,8 +551,8 @@ class TestFunctional(unittest.TestCase):
with open(fname, 'rb') as fd: with open(fname, 'rb') as fd:
data = fd.read() data = fd.read()
self.assertEqual(U_BOOT_DATA, data[3:7]) self.assertEqual(U_BOOT_DATA, data[3:7])
self.assertEqual(chr(0) * 3, data[:3]) self.assertEqual(tools.GetBytes(0, 3), data[:3])
self.assertEqual(chr(0) * 5, data[7:]) self.assertEqual(tools.GetBytes(0, 5), data[7:])
def testBadAlign(self): def testBadAlign(self):
"""Test that an invalid alignment value is detected""" """Test that an invalid alignment value is detected"""
@ -731,7 +731,8 @@ class TestFunctional(unittest.TestCase):
"""Test that the image pad byte can be specified""" """Test that the image pad byte can be specified"""
self._SetupSplElf() self._SetupSplElf()
data = self._DoReadFile('021_image_pad.dts') data = self._DoReadFile('021_image_pad.dts')
self.assertEqual(U_BOOT_SPL_DATA + (chr(0xff) * 1) + U_BOOT_DATA, data) self.assertEqual(U_BOOT_SPL_DATA + tools.GetBytes(0xff, 1) +
U_BOOT_DATA, data)
def testImageName(self): def testImageName(self):
"""Test that image files can be named""" """Test that image files can be named"""
@ -754,8 +755,8 @@ class TestFunctional(unittest.TestCase):
"""Test that entries can be sorted""" """Test that entries can be sorted"""
self._SetupSplElf() self._SetupSplElf()
data = self._DoReadFile('024_sorted.dts') data = self._DoReadFile('024_sorted.dts')
self.assertEqual(chr(0) * 1 + U_BOOT_SPL_DATA + chr(0) * 2 + self.assertEqual(tools.GetBytes(0, 1) + U_BOOT_SPL_DATA +
U_BOOT_DATA, data) tools.GetBytes(0, 2) + U_BOOT_DATA, data)
def testPackZeroOffset(self): def testPackZeroOffset(self):
"""Test that an entry at offset 0 is not given a new offset""" """Test that an entry at offset 0 is not given a new offset"""
@ -797,8 +798,8 @@ class TestFunctional(unittest.TestCase):
"""Test that a basic x86 ROM can be created""" """Test that a basic x86 ROM can be created"""
self._SetupSplElf() self._SetupSplElf()
data = self._DoReadFile('029_x86-rom.dts') data = self._DoReadFile('029_x86-rom.dts')
self.assertEqual(U_BOOT_DATA + chr(0) * 7 + U_BOOT_SPL_DATA + self.assertEqual(U_BOOT_DATA + tools.GetBytes(0, 7) + U_BOOT_SPL_DATA +
chr(0) * 2, data) tools.GetBytes(0, 2), data)
def testPackX86RomMeNoDesc(self): def testPackX86RomMeNoDesc(self):
"""Test that an invalid Intel descriptor entry is detected""" """Test that an invalid Intel descriptor entry is detected"""
@ -1005,7 +1006,7 @@ class TestFunctional(unittest.TestCase):
used_len = len(U_BOOT_NODTB_DATA) + fdt_len used_len = len(U_BOOT_NODTB_DATA) + fdt_len
third = data[used_len:] third = data[used_len:]
self.assertEqual(chr(0) * (0x200 - used_len), third) self.assertEqual(tools.GetBytes(0, 0x200 - used_len), third)
def testUnknownPosSize(self): def testUnknownPosSize(self):
"""Test that microcode must be placed within the image""" """Test that microcode must be placed within the image"""
@ -1034,7 +1035,8 @@ class TestFunctional(unittest.TestCase):
# ELF file with a '__bss_size' symbol # ELF file with a '__bss_size' symbol
self._SetupSplElf() self._SetupSplElf()
data = self._DoReadFile('047_spl_bss_pad.dts') data = self._DoReadFile('047_spl_bss_pad.dts')
self.assertEqual(U_BOOT_SPL_DATA + (chr(0) * 10) + U_BOOT_DATA, data) self.assertEqual(U_BOOT_SPL_DATA + tools.GetBytes(0, 10) + U_BOOT_DATA,
data)
def testSplBssPadMissing(self): def testSplBssPadMissing(self):
"""Test that a missing symbol is detected""" """Test that a missing symbol is detected"""
@ -1108,9 +1110,9 @@ class TestFunctional(unittest.TestCase):
self._SetupSplElf('u_boot_binman_syms') self._SetupSplElf('u_boot_binman_syms')
data = self._DoReadFile('053_symbols.dts') data = self._DoReadFile('053_symbols.dts')
sym_values = struct.pack('<LQL', 0x24 + 0, 0x24 + 24, 0x24 + 20) sym_values = struct.pack('<LQL', 0x24 + 0, 0x24 + 24, 0x24 + 20)
expected = (sym_values + U_BOOT_SPL_DATA[16:] + chr(0xff) + expected = (sym_values + U_BOOT_SPL_DATA[16:] +
U_BOOT_DATA + tools.GetBytes(0xff, 1) + U_BOOT_DATA + sym_values +
sym_values + U_BOOT_SPL_DATA[16:]) U_BOOT_SPL_DATA[16:])
self.assertEqual(expected, data) self.assertEqual(expected, data)
def testPackUnitAddress(self): def testPackUnitAddress(self):
@ -1280,8 +1282,8 @@ class TestFunctional(unittest.TestCase):
} }
data, _, _, _ = self._DoReadFileDtb('066_text.dts', data, _, _, _ = self._DoReadFileDtb('066_text.dts',
entry_args=entry_args) entry_args=entry_args)
expected = (TEXT_DATA + chr(0) * (8 - len(TEXT_DATA)) + TEXT_DATA2 + expected = (TEXT_DATA + tools.GetBytes(0, 8 - len(TEXT_DATA)) +
TEXT_DATA3 + 'some text') TEXT_DATA2 + TEXT_DATA3 + 'some text')
self.assertEqual(expected, data) self.assertEqual(expected, data)
def testEntryDocs(self): def testEntryDocs(self):
@ -1340,7 +1342,7 @@ class TestFunctional(unittest.TestCase):
def testFill(self): def testFill(self):
"""Test for an fill entry type""" """Test for an fill entry type"""
data = self._DoReadFile('069_fill.dts') data = self._DoReadFile('069_fill.dts')
expected = 8 * chr(0xff) + 8 * chr(0) expected = tools.GetBytes(0xff, 8) + tools.GetBytes(0, 8)
self.assertEqual(expected, data) self.assertEqual(expected, data)
def testFillNoSize(self): def testFillNoSize(self):
@ -1370,7 +1372,8 @@ class TestFunctional(unittest.TestCase):
data, _, _, _ = self._DoReadFileDtb('071_gbb.dts', entry_args=entry_args) data, _, _, _ = self._DoReadFileDtb('071_gbb.dts', entry_args=entry_args)
# Since futility # Since futility
expected = GBB_DATA + GBB_DATA + 8 * chr(0) + (0x2180 - 16) * chr(0) expected = (GBB_DATA + GBB_DATA + tools.GetBytes(0, 8) +
tools.GetBytes(0, 0x2180 - 16))
self.assertEqual(expected, data) self.assertEqual(expected, data)
def testGbbTooSmall(self): def testGbbTooSmall(self):
@ -1445,7 +1448,7 @@ class TestFunctional(unittest.TestCase):
def testFillZero(self): def testFillZero(self):
"""Test for an fill entry type with a size of 0""" """Test for an fill entry type with a size of 0"""
data = self._DoReadFile('080_fill_empty.dts') data = self._DoReadFile('080_fill_empty.dts')
self.assertEqual(chr(0) * 16, data) self.assertEqual(tools.GetBytes(0, 16), data)
def testTextMissing(self): def testTextMissing(self):
"""Test for a text entry type where there is no text""" """Test for a text entry type where there is no text"""
@ -1796,9 +1799,12 @@ class TestFunctional(unittest.TestCase):
0000002c 00000000 00000004 u-boot 0000002c 00000000 00000004 u-boot
''', map_data) ''', map_data)
self.assertEqual(data, self.assertEqual(data,
4 * chr(0x26) + U_BOOT_DATA + 12 * chr(0x21) + tools.GetBytes(0x26, 4) + U_BOOT_DATA +
4 * chr(0x26) + U_BOOT_DATA + 12 * chr(0x61) + tools.GetBytes(0x21, 12) +
4 * chr(0x26) + U_BOOT_DATA + 8 * chr(0x26)) tools.GetBytes(0x26, 4) + U_BOOT_DATA +
tools.GetBytes(0x61, 12) +
tools.GetBytes(0x26, 4) + U_BOOT_DATA +
tools.GetBytes(0x26, 8))
if __name__ == "__main__": if __name__ == "__main__":

View file

@ -7,6 +7,7 @@ import command
import glob import glob
import os import os
import shutil import shutil
import sys
import tempfile import tempfile
import tout import tout
@ -239,3 +240,21 @@ def WriteFile(fname, data):
#(fname, len(data), len(data))) #(fname, len(data), len(data)))
with open(Filename(fname), 'wb') as fd: with open(Filename(fname), 'wb') as fd:
fd.write(data) fd.write(data)
def GetBytes(byte, size):
"""Get a string of bytes of a given size
This handles the unfortunate different between Python 2 and Python 2.
Args:
byte: Numeric byte value to use
size: Size of bytes/string to return
Returns:
A bytes type with 'byte' repeated 'size' times
"""
if sys.version_info[0] >= 3:
data = bytes([byte]) * size
else:
data = chr(byte) * size
return data