From 56c86faa380502939229da87321110d3e3ca881c Mon Sep 17 00:00:00 2001 From: WessellUrdata <86524864+WessellUrdata@users.noreply.github.com> Date: Thu, 5 Jan 2023 22:14:50 +0800 Subject: [PATCH] Created patch-vbmeta.py Completed script --- patch-vbmeta.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 patch-vbmeta.py diff --git a/patch-vbmeta.py b/patch-vbmeta.py new file mode 100644 index 0000000..0921e45 --- /dev/null +++ b/patch-vbmeta.py @@ -0,0 +1,44 @@ +import os +import sys + +# Magic for the vbmeta image header +AVB_MAGIC = b"AVB0" +AVB_MAGIC_LEN = 4 + +# Information about the verification flags +FLAGS_OFFSET = 123 +FLAGS_TO_SET = b'\x03' + + +### main program + +# if an argument is not provided +if len(sys.argv) != 2: + sys.exit("Usage: python ./" + os.path.basename(__file__) + " ") + +# try reading the file with read/write to make sure it exists +FILE = sys.argv[1] + +try: + fd = os.open(FILE, os.O_RDWR) +except OSError: + sys.exit("Error reading file: " + FILE + ". File not modified. Exiting...") + +# making sure it's a vbmeta image by reading the magic bytes at the start of the file +magic = os.read(fd, AVB_MAGIC_LEN) + +if (magic != AVB_MAGIC): + fd.close() + sys.exit("Error: The provided image is not a valid vbmeta image. File not modified. Exiting...") + +# set the disable-verity and disable-verification flags at offset 123 +try: + os.lseek(fd, FLAGS_OFFSET, os.SEEK_SET) + os.write(fd, FLAGS_TO_SET) +except OSError: + fd.close() + sys.exit("Error: Failed when patching the vbmeta image. Exiting...") + +# end of program +os.close(fd) +print("Patching successful.") \ No newline at end of file