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 <Base DTS> <Overlay DTS> <Output DTS>"
21}
22
23function on_exit() {
24  rm -rf "$TEMP_DIR"
25}
26
27#
28# Start
29#
30
31if [[ $# -lt 3 ]]; then
32  usage
33  exit 1
34fi
35
36BASE_DTS=$1
37OVERLAY_DTS=$2
38OUT_DTS=$3
39
40TEMP_DIR=`mktemp -d`
41# The script will exit directly if any command fails.
42set -e
43trap on_exit EXIT
44
45# Finds '/dts-v1/; and /plugin/;' then replace them with '/* REMOVED */'
46OVERLAY_DTS_DIR=`dirname "$OVERLAY_DTS"`
47OVERLAY_DTS_NAME=`basename "$OVERLAY_DTS"`
48OVERLAY_DT_WO_HEADER_DTS="$TEMP_DIR/$OVERLAY_DTS_NAME"
49sed "s/\\(\\/dts-v1\\/\\s*;\\|\\/plugin\\/\\s*;\\)/\\/\\* REMOVED \\*\\//g" \
50  "$OVERLAY_DTS" > "$OVERLAY_DT_WO_HEADER_DTS"
51
52# Appends /include/ ...;
53BASE_DTS_DIR=`dirname "$BASE_DTS"`
54BASE_DTS_NAME=`basename "$BASE_DTS"`
55BASE_DT_WITH_INC_DTS="$TEMP_DIR/$BASE_DTS_NAME"
56cp "$BASE_DTS" "$BASE_DT_WITH_INC_DTS"
57echo "/include/ \"$OVERLAY_DT_WO_HEADER_DTS\"" >> "$BASE_DT_WITH_INC_DTS"
58
59# Simulate device tree overlay
60MERGED_DTB="$BASE_DT_WITH_INC_DTS.dtb"
61dtc -@ -i "$BASE_DTS_DIR" -i "$OVERLAY_DTS_DIR" -O dtb -o "$MERGED_DTB" "$BASE_DT_WITH_INC_DTS"
62
63# Dump
64dtc -s -O dts -o "$OUT_DTS" "$MERGED_DTB"
65