summaryrefslogtreecommitdiff
path: root/tools/patman/tools.py
diff options
context:
space:
mode:
authorTom Rini <trini@konsulko.com>2019-07-29 17:59:51 -0400
committerTom Rini <trini@konsulko.com>2019-07-29 17:59:51 -0400
commitde17e1fc03d97d420e9cb59f3a4d0f17c8bdcce5 (patch)
tree3b2d1e382ee75a3d2141d902395c9f3d5f49904e /tools/patman/tools.py
parent333755ef7b6f824366eed37ae068c20a4f25a123 (diff)
parent4f4fb85ec0bfe45da11aa23ada565387ee676e80 (diff)
Merge tag 'dm-pull-29jul19' of https://gitlab.denx.de/u-boot/custodians/u-boot-dm
binman support for replacing files
Diffstat (limited to 'tools/patman/tools.py')
-rw-r--r--tools/patman/tools.py29
1 files changed, 27 insertions, 2 deletions
diff --git a/tools/patman/tools.py b/tools/patman/tools.py
index e945b54fa28..0d4705db760 100644
--- a/tools/patman/tools.py
+++ b/tools/patman/tools.py
@@ -9,6 +9,7 @@ import command
import glob
import os
import shutil
+import struct
import sys
import tempfile
@@ -82,6 +83,7 @@ def FinaliseOutputDir():
"""Tidy up: delete output directory if temporary and not preserved."""
if outdir and not preserve_outdir:
_RemoveOutputDir()
+ outdir = None
def GetOutputFilename(fname):
"""Return a filename within the output directory.
@@ -100,6 +102,7 @@ def _FinaliseForTest():
if outdir:
_RemoveOutputDir()
+ outdir = None
def SetInputDirs(dirname):
"""Add a list of input directories, where input files are kept.
@@ -377,7 +380,7 @@ def ToBytes(string):
return string.encode('utf-8')
return string
-def Compress(indata, algo):
+def Compress(indata, algo, with_header=True):
"""Compress some data using a given algorithm
Note that for lzma this uses an old version of the algorithm, not that
@@ -408,9 +411,12 @@ def Compress(indata, algo):
data = Run('gzip', '-c', fname, binary=True)
else:
raise ValueError("Unknown algorithm '%s'" % algo)
+ if with_header:
+ hdr = struct.pack('<I', len(data))
+ data = hdr + data
return data
-def Decompress(indata, algo):
+def Decompress(indata, algo, with_header=True):
"""Decompress some data using a given algorithm
Note that for lzma this uses an old version of the algorithm, not that
@@ -428,6 +434,9 @@ def Decompress(indata, algo):
"""
if algo == 'none':
return indata
+ if with_header:
+ data_len = struct.unpack('<I', indata[:4])[0]
+ indata = indata[4:4 + data_len]
fname = GetOutputFilename('%s.decomp.tmp' % algo)
with open(fname, 'wb') as fd:
fd.write(indata)
@@ -473,3 +482,19 @@ def RunIfwiTool(ifwi_file, cmd, fname=None, subpart=None, entry_name=None):
if entry_name:
args += ['-d', '-e', entry_name]
Run(*args)
+
+def ToHex(val):
+ """Convert an integer value (or None) to a string
+
+ Returns:
+ hex value, or 'None' if the value is None
+ """
+ return 'None' if val is None else '%#x' % val
+
+def ToHexSize(val):
+ """Return the size of an object in hex
+
+ Returns:
+ hex value of size, or 'None' if the value is None
+ """
+ return 'None' if val is None else '%#x' % len(val)