summaryrefslogtreecommitdiff
path: root/util/unpack_ftb.py
diff options
context:
space:
mode:
Diffstat (limited to 'util/unpack_ftb.py')
-rwxr-xr-xutil/unpack_ftb.py130
1 files changed, 65 insertions, 65 deletions
diff --git a/util/unpack_ftb.py b/util/unpack_ftb.py
index 03127a7089..4873190fb3 100755
--- a/util/unpack_ftb.py
+++ b/util/unpack_ftb.py
@@ -1,35 +1,33 @@
#!/usr/bin/env python
-# Copyright 2018 The Chromium OS Authors. All rights reserved.
+# Copyright 2018 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
-#
-# Ignore indention messages, since legacy scripts use 2 spaces instead of 4.
-# pylint: disable=bad-indentation,docstring-section-indent
-# pylint: disable=docstring-trailing-quotes
# Note: This is a py2/3 compatible file.
from __future__ import print_function
+
import argparse
import ctypes
import os
class Header(ctypes.Structure):
- _pack_ = 1
- _fields_ = [
- ('signature', ctypes.c_uint32),
- ('ftb_ver', ctypes.c_uint32),
- ('chip_id', ctypes.c_uint32),
- ('svn_ver', ctypes.c_uint32),
- ('fw_ver', ctypes.c_uint32),
- ('config_id', ctypes.c_uint32),
- ('config_ver', ctypes.c_uint32),
- ('reserved', ctypes.c_uint8 * 8),
- ('release_info', ctypes.c_ulonglong),
- ('sec_size', ctypes.c_uint32 * 4),
- ('crc', ctypes.c_uint32),
- ]
+ _pack_ = 1
+ _fields_ = [
+ ("signature", ctypes.c_uint32),
+ ("ftb_ver", ctypes.c_uint32),
+ ("chip_id", ctypes.c_uint32),
+ ("svn_ver", ctypes.c_uint32),
+ ("fw_ver", ctypes.c_uint32),
+ ("config_id", ctypes.c_uint32),
+ ("config_ver", ctypes.c_uint32),
+ ("reserved", ctypes.c_uint8 * 8),
+ ("release_info", ctypes.c_ulonglong),
+ ("sec_size", ctypes.c_uint32 * 4),
+ ("crc", ctypes.c_uint32),
+ ]
+
FW_HEADER_SIZE = 64
FW_HEADER_SIGNATURE = 0xAA55AA55
@@ -44,7 +42,7 @@ FLASH_SEC_ADDR = [
0x0000 * 4, # CODE
0x7C00 * 4, # CONFIG
0x7000 * 4, # CX
- None # This section shouldn't exist
+ None, # This section shouldn't exist
]
UPDATE_PDU_SIZE = 4096
@@ -59,64 +57,66 @@ OUTPUT_FILE_SIZE = UPDATE_PDU_SIZE + 128 * 1024
def main():
- parser = argparse.ArgumentParser()
- parser.add_argument('--input', '-i', required=True)
- parser.add_argument('--output', '-o', required=True)
- args = parser.parse_args()
+ parser = argparse.ArgumentParser()
+ parser.add_argument("--input", "-i", required=True)
+ parser.add_argument("--output", "-o", required=True)
+ args = parser.parse_args()
- with open(args.input, 'rb') as f:
- bs = f.read()
+ with open(args.input, "rb") as f:
+ bs = f.read()
- size = len(bs)
- if size < FW_HEADER_SIZE + FW_BYTES_ALIGN:
- raise Exception('FW size too small')
+ size = len(bs)
+ if size < FW_HEADER_SIZE + FW_BYTES_ALIGN:
+ raise Exception("FW size too small")
- print('FTB file size:', size)
+ print("FTB file size:", size)
- header = Header()
- assert ctypes.sizeof(header) == FW_HEADER_SIZE
+ header = Header()
+ assert ctypes.sizeof(header) == FW_HEADER_SIZE
- ctypes.memmove(ctypes.addressof(header), bs, ctypes.sizeof(header))
- if (header.signature != FW_HEADER_SIGNATURE or
- header.ftb_ver != FW_FTB_VER or
- header.chip_id != FW_CHIP_ID):
- raise Exception('Invalid header')
+ ctypes.memmove(ctypes.addressof(header), bs, ctypes.sizeof(header))
+ if (
+ header.signature != FW_HEADER_SIGNATURE
+ or header.ftb_ver != FW_FTB_VER
+ or header.chip_id != FW_CHIP_ID
+ ):
+ raise Exception("Invalid header")
- for key, _ in header._fields_:
- v = getattr(header, key)
- if isinstance(v, ctypes.Array):
- print(key, list(map(hex, v)))
- else:
- print(key, hex(v))
+ for key, _ in header._fields_:
+ v = getattr(header, key)
+ if isinstance(v, ctypes.Array):
+ print(key, list(map(hex, v)))
+ else:
+ print(key, hex(v))
- dimension = sum(header.sec_size)
+ dimension = sum(header.sec_size)
- assert dimension + FW_HEADER_SIZE + FW_BYTES_ALIGN == size
- data = bs[FW_HEADER_SIZE:FW_HEADER_SIZE + dimension]
+ assert dimension + FW_HEADER_SIZE + FW_BYTES_ALIGN == size
+ data = bs[FW_HEADER_SIZE : FW_HEADER_SIZE + dimension]
- with open(args.output, 'wb') as f:
- # ensure the file size
- f.seek(OUTPUT_FILE_SIZE - 1, os.SEEK_SET)
- f.write(b'\x00')
+ with open(args.output, "wb") as f:
+ # ensure the file size
+ f.seek(OUTPUT_FILE_SIZE - 1, os.SEEK_SET)
+ f.write(b"\x00")
- f.seek(0, os.SEEK_SET)
- f.write(bs[0 : ctypes.sizeof(header)])
+ f.seek(0, os.SEEK_SET)
+ f.write(bs[0 : ctypes.sizeof(header)])
- offset = 0
- # write each sections
- for i, addr in enumerate(FLASH_SEC_ADDR):
- size = header.sec_size[i]
- assert addr is not None or size == 0
+ offset = 0
+ # write each sections
+ for i, addr in enumerate(FLASH_SEC_ADDR):
+ size = header.sec_size[i]
+ assert addr is not None or size == 0
- if size == 0:
- continue
+ if size == 0:
+ continue
- f.seek(UPDATE_PDU_SIZE + addr, os.SEEK_SET)
- f.write(data[offset : offset + size])
- offset += size
+ f.seek(UPDATE_PDU_SIZE + addr, os.SEEK_SET)
+ f.write(data[offset : offset + size])
+ offset += size
- f.flush()
+ f.flush()
-if __name__ == '__main__':
- main()
+if __name__ == "__main__":
+ main()