From 67b080b158c3fb45e66bf44df35e15377365bcb6 Mon Sep 17 00:00:00 2001 From: lambdadroid Date: Fri, 25 Oct 2019 17:07:08 +0200 Subject: [PATCH] Detect if DTB is placed in second area of Android boot image (!1828) postmarketOS/pmaports!700 adds a new "deviceinfo_bootimg_dtb_second" option that places the DTB in the "second" area of the Android boot image. Attempt to detect this automatically by checking the extracted second binary for the FDT magic (0xd00dfeed). --- pmb/aportgen/device.py | 2 ++ pmb/config/__init__.py | 1 + pmb/parse/bootimg.py | 9 +++++++++ test/test_bootimg.py | 20 ++++++++++++++++++-- test/test_questions.py | 3 ++- test/testdata/bootimg/dtb-second-boot.img | Bin 0 -> 4096 bytes 6 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 test/testdata/bootimg/dtb-second-boot.img diff --git a/pmb/aportgen/device.py b/pmb/aportgen/device.py index 658e139b..9fb99d54 100644 --- a/pmb/aportgen/device.py +++ b/pmb/aportgen/device.py @@ -106,6 +106,7 @@ def generate_deviceinfo_fastboot_content(args, bootimg=None): if bootimg is None: bootimg = {"cmdline": "", "qcdt": "false", + "dtb_second": "false", "base": "", "kernel_offset": "", "ramdisk_offset": "", @@ -116,6 +117,7 @@ def generate_deviceinfo_fastboot_content(args, bootimg=None): deviceinfo_kernel_cmdline=\"""" + bootimg["cmdline"] + """\" deviceinfo_generate_bootimg="true" deviceinfo_bootimg_qcdt=\"""" + bootimg["qcdt"] + """\" + deviceinfo_bootimg_dtb_second=\"""" + bootimg["dtb_second"] + """\" deviceinfo_flash_offset_base=\"""" + bootimg["base"] + """\" deviceinfo_flash_offset_kernel=\"""" + bootimg["kernel_offset"] + """\" deviceinfo_flash_offset_ramdisk=\"""" + bootimg["ramdisk_offset"] + """\" diff --git a/pmb/config/__init__.py b/pmb/config/__init__.py index 7847885b..64003998 100644 --- a/pmb/config/__init__.py +++ b/pmb/config/__init__.py @@ -267,6 +267,7 @@ deviceinfo_attributes = [ "kernel_cmdline", "generate_bootimg", "bootimg_qcdt", + "bootimg_dtb_second", "flash_offset_base", "flash_offset_kernel", "flash_offset_ramdisk", diff --git a/pmb/parse/bootimg.py b/pmb/parse/bootimg.py index 2eee1de0..067a2189 100644 --- a/pmb/parse/bootimg.py +++ b/pmb/parse/bootimg.py @@ -21,6 +21,14 @@ import logging import pmb +def is_dtb(path): + if not os.path.isfile(path): + return False + with open(path, 'rb') as f: + # Check FDT magic identifier (0xd00dfeed) + return f.read(4) == b'\xd0\x0d\xfe\xed' + + def bootimg(args, path): if not os.path.exists(path): raise RuntimeError("Could not find file '" + path + "'") @@ -77,6 +85,7 @@ def bootimg(args, path): output["cmdline"] = f.read().replace('\n', '') output["qcdt"] = ("true" if os.path.isfile(bootimg_path + "-dt") and os.path.getsize(bootimg_path + "-dt") > 0 else "false") + output["dtb_second"] = ("true" if is_dtb(bootimg_path + "-second") else "false") # Cleanup pmb.chroot.root(args, ["rm", "-r", temp_path]) diff --git a/test/test_bootimg.py b/test/test_bootimg.py index 0ad38936..7c51587f 100644 --- a/test/test_bootimg.py +++ b/test/test_bootimg.py @@ -68,7 +68,8 @@ def test_bootimg_normal(args): "tags_offset": "0x0e000000", "pagesize": "2048", "cmdline": "bootopt=64S3,32S1,32S1", - "qcdt": "false"} + "qcdt": "false", + "dtb_second": "false"} assert pmb.parse.bootimg(args, path) == output @@ -81,5 +82,20 @@ def test_bootimg_qcdt(args): "tags_offset": "0x0e000000", "pagesize": "2048", "cmdline": "bootopt=64S3,32S1,32S1", - "qcdt": "true"} + "qcdt": "true", + "dtb_second": "false"} + assert pmb.parse.bootimg(args, path) == output + + +def test_bootimg_dtb_second(args): + path = pmb_src + "/test/testdata/bootimg/dtb-second-boot.img" + output = {"base": "0x00000000", + "kernel_offset": "0x00008000", + "ramdisk_offset": "0x02000000", + "second_offset": "0x00f00000", + "tags_offset": "0x00000100", + "pagesize": "2048", + "cmdline": "bootopt=64S3,32S1,32S1", + "qcdt": "false", + "dtb_second": "true"} assert pmb.parse.bootimg(args, path) == output diff --git a/test/test_questions.py b/test/test_questions.py index a8bb068f..e5660425 100644 --- a/test/test_questions.py +++ b/test/test_questions.py @@ -113,7 +113,8 @@ def test_questions_bootimg(args, monkeypatch): "tags_offset": "0x0e000000", "pagesize": "2048", "cmdline": "bootopt=64S3,32S1,32S1", - "qcdt": "false"} + "qcdt": "false", + "dtb_second": "false"} assert func(args) == output diff --git a/test/testdata/bootimg/dtb-second-boot.img b/test/testdata/bootimg/dtb-second-boot.img new file mode 100644 index 0000000000000000000000000000000000000000..fcc76849a59d4b373342a6a27641bffddf4e1e86 GIT binary patch literal 4096 zcmZ?ta|!bIbWvmgfd()MBA7hD%nu9*@*05J?8vteSOmH`Mb0Wl|-HA;+zz-S1JhQMeDjE2By2n>l30091O7W)7I literal 0 HcmV?d00001