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
18# Set to true to get more debug information
19DEBUG=false
20# Remove the comment to enable valgrind
21# Require build with $mmma external/valgrind
22#VALGRIND="valgrind --leak-check=yes --show-reachable=yes"
23
24function usage() {
25  echo "Usage:"
26  echo "  $PROG_NAME (--fdt|--ufdt) (--remote) <Base DTS> <Overlay DTS> <Output DTS>"
27}
28
29function on_exit() {
30  rm -rf "$TEMP_DIR"
31}
32
33#
34# Start
35#
36
37# Setup OVERLAY
38if [ "$1" == "--fdt" ]; then
39  shift
40  OVERLAY="fdt_apply_overlay"
41elif [ "$1" == "--ufdt" ]; then
42  shift
43  OVERLAY="ufdt_apply_overlay"
44else
45  usage
46  exit 1
47fi
48
49# --remote: run overlay on the device with adb
50if [ "$1" == "--remote" ]; then
51  shift
52  EXE_PATH="${ANDROID_PRODUCT_OUT}/obj/EXECUTABLES"
53  REMOTE_PATH="/data/local/tmp"
54  adb push "${EXE_PATH}/${OVERLAY}_intermediates/${OVERLAY}" \
55    "$REMOTE_PATH" > /dev/null
56fi
57
58if [[ $# -lt 3 ]]; then
59  usage
60  exit 1
61fi
62
63BASE_DTS=$1
64OVERLAY_DTS=$2
65OUT_DTS=$3
66
67TEMP_DIR=`mktemp -d`
68# The script will exit directly if any command fails.
69set -e
70trap on_exit EXIT
71
72# Compile the *-base.dts to make *-base.dtb
73BASE_DTS_NAME=`basename "$BASE_DTS"`
74BASE_DTB_NAME="${BASE_DTS_NAME}-base.dtb"
75BASE_DTB="${TEMP_DIR}/${BASE_DTB_NAME}"
76dtc -@ -qq -O dtb -o "$BASE_DTB" "$BASE_DTS"
77if $DEBUG; then
78  echo "[base.dts]"
79  dtc -O dts "$BASE_DTB"
80fi
81
82# Compile the *-overlay.dts to make *-overlay.dtb
83OVERLAY_DTS_NAME=`basename "$OVERLAY_DTS"`
84OVERLAY_DTB_NAME="${OVERLAY_DTS_NAME}-overlay.dtb"
85OVERLAY_DTB="${TEMP_DIR}/${OVERLAY_DTB_NAME}"
86dtc -@ -qq -O dtb -o "$OVERLAY_DTB" "$OVERLAY_DTS"
87if $DEBUG; then
88  echo "[overlay.dts]"
89  dtc -O dts "$OVERLAY_DTB"
90fi
91
92# Run ufdt_apply_overlay to combine *-base.dtb and *-overlay.dtb
93# into *-merged.dtb
94MERGED_DTB_NAME="${BASE_DTS_NAME}-merged.dtb"
95MERGED_DTB="${TEMP_DIR}/${MERGED_DTB_NAME}"
96if [ -z "$REMOTE_PATH" ]; then
97  $VALGRIND "$OVERLAY" "$BASE_DTB" "$OVERLAY_DTB" "$MERGED_DTB"
98else
99  adb push "$BASE_DTB" "$REMOTE_PATH" > /dev/null
100  adb push "$OVERLAY_DTB" "$REMOTE_PATH" > /dev/null
101  adb shell "
102    cd "$REMOTE_PATH" &&
103    "./${OVERLAY}" "$BASE_DTB_NAME" "$OVERLAY_DTB_NAME" "$MERGED_DTB_NAME"
104  "
105  adb pull "${REMOTE_PATH}/${MERGED_DTB_NAME}" "$MERGED_DTB" > /dev/null
106fi
107
108if [ ! -z "$REMOTE_PATH" ]; then
109  # clean up
110  adb shell "
111    cd "$REMOTE_PATH" &&
112    rm -f "$OVERLAY" &&
113    rm -f "$BASE_DTB_NAME" &&
114    rm -f "$OVERLAY_DTB_NAME" &&
115    rm -f "$MERGED_DTB_NAME"
116  " > /dev/null
117fi
118
119# Dump
120dtc -s -O dts -o "$OUT_DTS" "$MERGED_DTB"
121if $DEBUG; then
122  echo "[merged.dts]"
123  cat $OUT_DTS
124fi
125