gen_test.sh revision f6c209b3f409f879305ac9837524b9d34c850de6
1#!/bin/bash
2
3# We want to generate two device tree blob (.dtb) files by combining
4# the "base" and "add" device tree source (.dts) files in two
5# different ways.
6#
7# 1) /include/ the "add" at the end of the "base" file and
8#   compile with dtc to make the "gold standard" .dtb
9#
10# 2) Compile them separately (modifying the "add" file to
11#   be an overlay file) with dtc, then join them with the
12#   ov_test program
13#
14# To do this, we need to generate a lot of files from the .base_dts
15#    and .add_dts files:
16# .base_inc_dts - Has the /include/ "${FILENAME}.add_dts" at the end.
17# .base_inc_dtb - The dtc-generated "gold standard"
18# .add_ov_dts - Has the /plugin/ at the start
19# .base_dtb - Compiled version of just the base
20# .add_ov_dtbo - Compiled version of the overlay
21# .base_ov_dtb - overlay-test-joined version of the dtb
22#
23# Then, compare .base_inc_dtb and .base_ov_dtb with dtdiff
24# (or maybe diff?) and return 0 iff they are identical.
25
26# Include some functions from common.sh.
27SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
28source ${SCRIPT_DIR}/common.sh
29
30# Constants
31IN_DATA_DIR="testdata"
32OUT_DATA_DIR="test-out"
33
34# Global variables
35FILENAME=$1
36
37on_exit() {
38  rm -rf "${OUT_DATA_DIR}"
39}
40
41# Generate test cases to OUT_DATA_DIR
42prepare_tests() {
43  cp "${IN_DATA_DIR}/${FILENAME}.base_dts" "${OUT_DATA_DIR}/${FILENAME}.base_dts"
44  cp "${IN_DATA_DIR}/${FILENAME}.add_dts" "${OUT_DATA_DIR}/${FILENAME}.add_dts"
45
46  # Add the "include" to make .base_inc_dts
47  cp "${IN_DATA_DIR}/${FILENAME}.base_dts" "${OUT_DATA_DIR}/${FILENAME}.base_inc_dts"
48  echo "/include/ \"${FILENAME}.add_dts\"" >> "${OUT_DATA_DIR}/${FILENAME}.base_inc_dts"
49
50  # Generate .base_inc_dtb
51  dtc -@ -O dtb -s -b 0 -o "${OUT_DATA_DIR}/${FILENAME}.base_inc.dtb" \
52    "${OUT_DATA_DIR}/${FILENAME}.base_inc_dts"
53
54  # Add /dts-v1/ /plugin/; in front of .add_dts file. In order to trigger dtc's
55  # fragment generation mechanism.
56  if [ -a "${IN_DATA_DIR}/${FILENAME}.add_ov_dts" ]; then
57    cp "${IN_DATA_DIR}/${FILENAME}.add_ov_dts" "${OUT_DATA_DIR}/${FILENAME}.add_ov_dts"
58  else
59    sed "1i/dts-v1/ /plugin/;" "${IN_DATA_DIR}/${FILENAME}.add_dts" > \
60      "${OUT_DATA_DIR}/${FILENAME}.add_ov_dts"
61  fi
62}
63
64# Compile test cases into dtb, and do the diff things.
65compile_and_diff() {
66  # Compile the base to make .base_dtb
67  dtc -O dtb -b 0 -@ -o "${OUT_DATA_DIR}/${FILENAME}.base_dtb" \
68    "${OUT_DATA_DIR}/${FILENAME}.base_dts"
69
70  # Compile the overlay to make .add_ov_dtbo
71  dtc -O dtb -b 0 -@ -o "${OUT_DATA_DIR}/${FILENAME}.add_ov_dtbo" \
72    "${OUT_DATA_DIR}/${FILENAME}.add_ov_dts"
73
74  # Run ov_test to combine .base_dtb and .add_ov_dtbo
75  # into .base_ov_dtb
76  ufdt_apply_overlay "${OUT_DATA_DIR}/${FILENAME}.base_dtb" \
77    "${OUT_DATA_DIR}/${FILENAME}.add_ov_dtbo" \
78    "${OUT_DATA_DIR}/${FILENAME}.base_ov.dtb"
79
80  # Run the diff
81  dt_diff ${OUT_DATA_DIR}/${FILENAME}.base_inc.dtb ${OUT_DATA_DIR}/${FILENAME}.base_ov.dtb
82}
83
84# The script will exit directly if any command fails.
85set -e
86trap on_exit EXIT
87
88mkdir -p "${OUT_DATA_DIR}" >& /dev/null
89
90prepare_tests
91compile_and_diff
92