1#!/bin/bash
2# Copyright (C) 2016 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16PROG_NAME=`basename $0`
17
18function usage() {
19  echo "Usage:"
20  echo "  $PROG_NAME (--fdt|--ufdt) (--remote) <Base DTS> <Overlay DTS> <Output DTS>"
21}
22
23function on_exit() {
24  rm -rf "$TEMP_DIR"
25}
26
27#
28# Start
29#
30
31# Setup OVERLAY
32if [ "$1" == "--fdt" ]; then
33  shift
34  OVERLAY="fdt_apply_overlay"
35elif [ "$1" == "--ufdt" ]; then
36  shift
37  OVERLAY="ufdt_apply_overlay"
38else
39  usage
40  exit 1
41fi
42
43# --remote: run overlay on the device with adb
44if [ "$1" == "--remote" ]; then
45  shift
46  EXE_PATH="${ANDROID_PRODUCT_OUT}/obj/EXECUTABLES"
47  REMOTE_PATH="/data/local/tmp"
48  adb push "${EXE_PATH}/${OVERLAY}_intermediates/${OVERLAY}" \
49    "$REMOTE_PATH" > /dev/null
50fi
51
52if [[ $# -lt 3 ]]; then
53  usage
54  exit 1
55fi
56
57BASE_DTS=$1
58OVERLAY_DTS=$2
59OUT_DTS=$3
60
61TEMP_DIR=`mktemp -d`
62# The script will exit directly if any command fails.
63set -e
64trap on_exit EXIT
65
66# Compile the *-base.dts to make *-base.dtb
67BASE_DTS_NAME=`basename "$BASE_DTS"`
68BASE_DTB_NAME="${BASE_DTS_NAME}-base.dtb"
69BASE_DTB="${TEMP_DIR}/${BASE_DTB_NAME}"
70dtc -@ -qq -O dtb -o "$BASE_DTB" "$BASE_DTS"
71
72# Compile the *-overlay.dts to make *-overlay.dtb
73OVERLAY_DTS_NAME=`basename "$OVERLAY_DTS"`
74OVERLAY_DTB_NAME="${OVERLAY_DTS_NAME}-overlay.dtb"
75OVERLAY_DTB="${TEMP_DIR}/${OVERLAY_DTB_NAME}"
76dtc -@ -qq -O dtb -o "$OVERLAY_DTB" "$OVERLAY_DTS"
77
78# Run ufdt_apply_overlay to combine *-base.dtb and *-overlay.dtb
79# into *-merged.dtb
80MERGED_DTB_NAME="${BASE_DTS_NAME}-merged.dtb"
81MERGED_DTB="${TEMP_DIR}/${MERGED_DTB_NAME}"
82if [ -z "$REMOTE_PATH" ]; then
83  "$OVERLAY" "$BASE_DTB" "$OVERLAY_DTB" "$MERGED_DTB"
84else
85  adb push "$BASE_DTB" "$REMOTE_PATH" > /dev/null
86  adb push "$OVERLAY_DTB" "$REMOTE_PATH" > /dev/null
87  adb shell "
88    cd "$REMOTE_PATH" &&
89    "./${OVERLAY}" "$BASE_DTB_NAME" "$OVERLAY_DTB_NAME" "$MERGED_DTB_NAME"
90  "
91  adb pull "${REMOTE_PATH}/${MERGED_DTB_NAME}" "$MERGED_DTB" > /dev/null
92fi
93
94if [ ! -z "$REMOTE_PATH" ]; then
95  # clean up
96  adb shell "
97    cd "$REMOTE_PATH" &&
98    rm -f "$OVERLAY" &&
99    rm -f "$BASE_DTB_NAME" &&
100    rm -f "$OVERLAY_DTB_NAME" &&
101    rm -f "$MERGED_DTB_NAME"
102  " > /dev/null
103fi
104
105# Dump
106dtc -s -O dts -o "$OUT_DTS" "$MERGED_DTB"
107