Skip to content
This repository has been archived by the owner on Mar 7, 2021. It is now read-only.

Commit

Permalink
begonia: Rewrite dtbo makefile & append_certs script
Browse files Browse the repository at this point in the history
Reflect the upstream logic rewrite in dtbo generation as needed.
Also clean and reorganize script a bit

Reference:
[0]: LineageOS/android_vendor_lineage@fe00ea9

Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
  • Loading branch information
theimpulson committed Aug 26, 2020
1 parent 3c42ae5 commit cf684eb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 40 deletions.
30 changes: 13 additions & 17 deletions dtbo/append_certs.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
#!/usr/bin/env python

import sys
import argparse
import os
from shutil import copyfile
from os import stat


def padding_file(input_file, align_num):
"""
Fill 0 to make input_file's size a multiple of align_num.
"""
filesize = os.stat(input_file).st_size
"""Fill 0 to make input_file's size a multiple of align_num."""
filesize = stat(input_file).st_size
file1 = open(input_file, 'ab+')
padding = filesize % align_num
if padding != 0:
Expand All @@ -19,28 +15,28 @@ def padding_file(input_file, align_num):
file1.write("\x00")
file1.close()

def append_file(img_file, cert_file):
"""
Append provided cert_file to the end of target img_file.
"""
padding_file(img_file, 16)

def append_file(img_file, cert_file, alignment):
"""Append provided cert_file to the end of target img_file."""
padding_file(img_file, alignment)
file1 = open(img_file, 'ab+')
file2 = open(cert_file, 'rb')
file1.write(file2.read())
file1.close()
file2.close()


def main():
parser = argparse.ArgumentParser(description='Sign an image with provided der certs')
parser.add_argument('--image', type=str, help='Target image')
parser.add_argument('--alignment', type=int, help='Alignment for calculating padding')
parser.add_argument('--cert1', type=str, help='Certificate - cert1.der')
parser.add_argument('--cert2', type=str, help='Certificate - cert2.der')
parser.add_argument('--output', type=str, help='Output image')
parser.add_argument('--dtbo', type=str, help='Path to the DTBO img')
args = parser.parse_args()

copyfile(args.image, args.output)
append_file(args.output, args.cert1)
append_file(args.output, args.cert2)
append_file(args.dtbo, args.cert1, args.alignment)
append_file(args.dtbo, args.cert2, args.alignment)


if __name__ == '__main__':
main()
34 changes: 11 additions & 23 deletions dtbo/dtbo.mk
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
BOARD_PREBUILT_DTBOIMAGE := $(PRODUCT_OUT)/dtbo-pre.img
BOARD_KERNEL_DTBO_CFG := dtboimg.cfg
MKDTBOIMG := system/libufdt/utils/src/mkdtboimg.py
APPEND_CERTS := $(DEVICE_PATH)/dtbo/append_certs.py

# BUG: mkdtboimg.py doesn't support absolute paths yet. Fix this later.
define build-dtboimage-target-from-cfg
$(call pretty,"Target dtbo image from cfg: $(BOARD_PREBUILT_DTBOIMAGE)")
$(hide) $(MKDTBOIMG) cfg_create $@ $(KERNEL_OUT)/$(BOARD_KERNEL_DTBO_CFG) -d /
$(hide) chmod a+r $@
endef

define append-dtboimage-certs
$(call pretty,"Target signed dtbo image: $(BOARD_PREBUILT_DTBOIMAGE)")
$(hide) mv $(BOARD_PREBUILT_DTBOIMAGE) $(BOARD_PREBUILT_DTBOIMAGE).tmp
$(hide) $(APPEND_CERTS) --image $(BOARD_PREBUILT_DTBOIMAGE).tmp --cert1 \
$(DEVICE_PATH)/dtbo/cert1.der --cert2 $(DEVICE_PATH)/dtbo/cert2.der --output $(BOARD_PREBUILT_DTBOIMAGE)
$(hide) chmod a+r $@
endef


$(BOARD_PREBUILT_DTBOIMAGE): $(MKDTBOIMG) $(INSTALLED_KERNEL_TARGET)
$(build-dtboimage-target-from-cfg)
$(append-dtboimage-certs)
BOARD_DTBO_CFG := $(DTBO_OUT)/dtboimg.cfg
MKDTIMG := $(HOST_OUT_EXECUTABLES)/mkdtimg$(HOST_EXECUTABLE_SUFFIX)
MKDTBOIMG := $(HOST_OUT_EXECUTABLES)/mkdtboimg.py$(HOST_EXECUTABLE_SUFFIX)

$(BOARD_PREBUILT_DTBOIMAGE): $(DTC) $(MKDTIMG) $(MKDTBOIMG)
$(BOARD_PREBUILT_DTBOIMAGE):
@echo "Building dtbo.img"
$(call make-dtbo-target,$(KERNEL_DEFCONFIG))
$(call make-dtbo-target,dtbs)
$(MKDTBOIMG) cfg_create $@ $(BOARD_DTBO_CFG) -d $(DTBO_OUT)/arch/$(KERNEL_ARCH)/boot/dts
$(APPEND_CERTS) --alignment 16 --cert1 $(DEVICE_PATH)/dtbo/cert1.der --cert2 $(DEVICE_PATH)/dtbo/cert2.der --dtbo $(BOARD_PREBUILT_DTBOIMAGE)

0 comments on commit cf684eb

Please sign in to comment.