aboutsummaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorTom Rini <trini@konsulko.com>2020-06-30 11:43:18 -0400
committerTom Rini <trini@konsulko.com>2020-06-30 11:43:18 -0400
commit5fdb3c0e7ee6bac6b8809ae69e52f16d22d45035 (patch)
treed04c64ec751e7adf24de995ce0fa7eabc88865bc /tools
parent6b3c74428a3faca92701843c954b717e8d186b17 (diff)
parente35c2a8fdd41a34c06c409ce700c5d5591429367 (diff)
downloadu-boot-5fdb3c0e7ee6bac6b8809ae69e52f16d22d45035.zip
u-boot-5fdb3c0e7ee6bac6b8809ae69e52f16d22d45035.tar.gz
u-boot-5fdb3c0e7ee6bac6b8809ae69e52f16d22d45035.tar.bz2
Merge tag 'mips-pull-2020-06-29' of https://gitlab.denx.de/u-boot/custodians/u-boot-mips into next
- net: pcnet: cleanup and add DM support - Makefile: add rule to build an endian-swapped U-Boot image used by MIPS Malta EL variants - CI: add Qemu tests for MIPS Malta
Diffstat (limited to 'tools')
-rwxr-xr-xtools/endian-swap.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/tools/endian-swap.py b/tools/endian-swap.py
new file mode 100755
index 0000000..5990efa
--- /dev/null
+++ b/tools/endian-swap.py
@@ -0,0 +1,55 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0+
+
+"""
+Simple tool to swap the byte endianness of a binary file.
+"""
+
+import argparse
+import io
+
+def parse_args():
+ """Parse command line arguments."""
+ description = "Swap endianness of given input binary and write to output binary."
+
+ parser = argparse.ArgumentParser(description=description)
+ parser.add_argument("input_bin", type=str, help="input binary")
+ parser.add_argument("output_bin", type=str, help="output binary")
+ parser.add_argument("-c", action="store", dest="chunk_size", type=int,
+ default=io.DEFAULT_BUFFER_SIZE, help="chunk size for reading")
+
+ return parser.parse_args()
+
+def swap_chunk(chunk_orig):
+ """Swap byte endianness of the given chunk.
+
+ Returns:
+ swapped chunk
+ """
+ chunk = bytearray(chunk_orig)
+
+ # align to 4 bytes and pad with 0x0
+ chunk_len = len(chunk)
+ pad_len = chunk_len % 4
+ if pad_len > 0:
+ chunk += b'\x00' * (4 - pad_len)
+
+ chunk[0::4], chunk[1::4], chunk[2::4], chunk[3::4] =\
+ chunk[3::4], chunk[2::4], chunk[1::4], chunk[0::4]
+
+ return chunk
+
+def main():
+ args = parse_args()
+
+ with open(args.input_bin, "rb") as input_bin:
+ with open(args.output_bin, "wb") as output_bin:
+ while True:
+ chunk = bytearray(input_bin.read(args.chunk_size))
+ if not chunk:
+ break
+
+ output_bin.write(swap_chunk(chunk))
+
+if __name__ == '__main__':
+ main()