summaryrefslogtreecommitdiff
path: root/scripts/cow_converter.py
diff options
context:
space:
mode:
authorAndroid Build Role Account android-build-prod <android-build-team-robot@google.com>2021-03-25 10:07:36 +0000
committerAndroid Build Role Account android-build-prod <android-build-team-robot@google.com>2021-03-25 10:07:36 +0000
commit75548accd9594d870fa712e3da2d9f618e38c60e (patch)
treede292a70ddc2edd6da37d3d9b370debd8b80b88f /scripts/cow_converter.py
parent9c7061227a48006b715abc21030bf7cd3038e656 (diff)
parentf1c33f832a626903c5fd7dc61a472685d7427c82 (diff)
Snap for 7234866 from f1c33f832a626903c5fd7dc61a472685d7427c82 to s-keystone-qcom-release
Change-Id: Ib1ef35430561943fb1741db48297001fe97c328e
Diffstat (limited to 'scripts/cow_converter.py')
-rw-r--r--scripts/cow_converter.py87
1 files changed, 87 insertions, 0 deletions
diff --git a/scripts/cow_converter.py b/scripts/cow_converter.py
new file mode 100644
index 00000000..14e016cb
--- /dev/null
+++ b/scripts/cow_converter.py
@@ -0,0 +1,87 @@
+#!/usr/bin/env python3
+#
+# Copyright (C) 2021 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+"""Command-line tool for converting OTA payloads to VABC style COW images."""
+
+import os
+import sys
+import tempfile
+import zipfile
+import subprocess
+
+
+def IsSparseImage(filepath):
+ """Determine if an image is a sparse image
+ Args:
+ filepath: str, a path to an .img file
+
+ Returns:
+ return true iff the filepath is a sparse image.
+
+ """
+ with open(filepath, 'rb') as fp:
+ # Magic for android sparse image format
+ # https://source.android.com/devices/bootloader/images
+ return fp.read(4) == b'\x3A\xFF\x26\xED'
+
+
+def ConvertCOW(ota_path, target_file_path, tmp_dir, output_dir):
+ """Convert ota payload to COW IMAGE
+ Args:
+ ota_path: str, path to ota.zip
+ target_file_path: str, path to target_file.zip,
+ must be the target build for OTA.
+ tmp_dir: A temp dir as scratch space
+ output_dir: A directory where all converted COW images will be written.
+ """
+ with zipfile.ZipFile(ota_path) as ota_zip:
+ payload_path = ota_zip.extract("payload.bin", output_dir)
+ with zipfile.ZipFile(target_file_path) as zfp:
+ for fileinfo in zfp.infolist():
+ img_name = os.path.basename(fileinfo.filename)
+ if not fileinfo.filename.endswith(".img"):
+ continue
+ if fileinfo.filename.startswith("IMAGES/") or \
+ fileinfo.filename.startswith("RADIO/"):
+ img_path = zfp.extract(fileinfo, tmp_dir)
+ target_img_path = os.path.join(output_dir, img_name)
+ if IsSparseImage(img_path):
+ subprocess.check_call(["simg2img", img_path, target_img_path])
+ else:
+ os.rename(img_path, target_img_path)
+ print("Extracted", fileinfo.filename, "size:", fileinfo.file_size)
+
+ subprocess.call(["cow_converter", payload_path,
+ output_dir])
+
+
+def main():
+ if len(sys.argv) != 4:
+ print(
+ "Usage:", sys.argv[0], "<your_ota.zip> <target_file.zip> <output dir>")
+ return 1
+ ota_path = sys.argv[1]
+ target_file_path = sys.argv[2]
+ output_dir = sys.argv[3]
+ os.makedirs(output_dir, exist_ok=True)
+ with tempfile.TemporaryDirectory() as tmp_dir:
+ ConvertCOW(ota_path, target_file_path, tmp_dir, output_dir)
+ return 0
+
+
+if __name__ == '__main__':
+ sys.exit(main())