mirror of
https://xff.cz/git/u-boot/
synced 2025-09-04 02:02:08 +02:00
patman: Update fmap code for Python 3
This needs special care to ensure that the bytes type is used for binary data. Add conversion code to deal with strings and bytes correctly. Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
@@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
from entry import Entry
|
from entry import Entry
|
||||||
import fmap_util
|
import fmap_util
|
||||||
|
import tools
|
||||||
|
|
||||||
|
|
||||||
class Entry_fmap(Entry):
|
class Entry_fmap(Entry):
|
||||||
@@ -46,7 +47,7 @@ class Entry_fmap(Entry):
|
|||||||
if pos is not None:
|
if pos is not None:
|
||||||
pos -= entry.section.GetRootSkipAtStart()
|
pos -= entry.section.GetRootSkipAtStart()
|
||||||
areas.append(fmap_util.FmapArea(pos or 0, entry.size or 0,
|
areas.append(fmap_util.FmapArea(pos or 0, entry.size or 0,
|
||||||
entry.name, 0))
|
tools.FromUnicode(entry.name), 0))
|
||||||
|
|
||||||
entries = self.section._image.GetEntries()
|
entries = self.section._image.GetEntries()
|
||||||
areas = []
|
areas = []
|
||||||
|
@@ -8,9 +8,12 @@
|
|||||||
|
|
||||||
import collections
|
import collections
|
||||||
import struct
|
import struct
|
||||||
|
import sys
|
||||||
|
|
||||||
|
import tools
|
||||||
|
|
||||||
# constants imported from lib/fmap.h
|
# constants imported from lib/fmap.h
|
||||||
FMAP_SIGNATURE = '__FMAP__'
|
FMAP_SIGNATURE = b'__FMAP__'
|
||||||
FMAP_VER_MAJOR = 1
|
FMAP_VER_MAJOR = 1
|
||||||
FMAP_VER_MINOR = 0
|
FMAP_VER_MINOR = 0
|
||||||
FMAP_STRLEN = 32
|
FMAP_STRLEN = 32
|
||||||
@@ -50,6 +53,8 @@ FmapArea = collections.namedtuple('FmapArea', FMAP_AREA_NAMES)
|
|||||||
|
|
||||||
|
|
||||||
def NameToFmap(name):
|
def NameToFmap(name):
|
||||||
|
if type(name) == bytes and sys.version_info[0] >= 3:
|
||||||
|
name = name.decode('utf-8') # pragma: no cover (for Python 2)
|
||||||
return name.replace('\0', '').replace('-', '_').upper()
|
return name.replace('\0', '').replace('-', '_').upper()
|
||||||
|
|
||||||
def ConvertName(field_names, fields):
|
def ConvertName(field_names, fields):
|
||||||
@@ -65,7 +70,7 @@ def ConvertName(field_names, fields):
|
|||||||
value: value of that field (string for the ones we support)
|
value: value of that field (string for the ones we support)
|
||||||
"""
|
"""
|
||||||
name_index = field_names.index('name')
|
name_index = field_names.index('name')
|
||||||
fields[name_index] = NameToFmap(fields[name_index])
|
fields[name_index] = tools.ToBytes(NameToFmap(fields[name_index]))
|
||||||
|
|
||||||
def DecodeFmap(data):
|
def DecodeFmap(data):
|
||||||
"""Decode a flashmap into a header and list of areas
|
"""Decode a flashmap into a header and list of areas
|
||||||
@@ -106,7 +111,8 @@ def EncodeFmap(image_size, name, areas):
|
|||||||
ConvertName(names, params)
|
ConvertName(names, params)
|
||||||
return struct.pack(fmt, *params)
|
return struct.pack(fmt, *params)
|
||||||
|
|
||||||
values = FmapHeader(FMAP_SIGNATURE, 1, 0, 0, image_size, name, len(areas))
|
values = FmapHeader(FMAP_SIGNATURE, 1, 0, 0, image_size,
|
||||||
|
tools.FromUnicode(name), len(areas))
|
||||||
blob = _FormatBlob(FMAP_HEADER_FORMAT, FMAP_HEADER_NAMES, values)
|
blob = _FormatBlob(FMAP_HEADER_FORMAT, FMAP_HEADER_NAMES, values)
|
||||||
for area in areas:
|
for area in areas:
|
||||||
blob += _FormatBlob(FMAP_AREA_FORMAT, FMAP_AREA_NAMES, area)
|
blob += _FormatBlob(FMAP_AREA_FORMAT, FMAP_AREA_NAMES, area)
|
||||||
|
Reference in New Issue
Block a user