1#
2# Copyright (c) 2022, Google, Inc. All rights reserved
3#
4# Permission is hereby granted, free of charge, to any person obtaining
5# a copy of this software and associated documentation files
6# (the "Software"), to deal in the Software without restriction,
7# including without limitation the rights to use, copy, modify, merge,
8# publish, distribute, sublicense, and/or sell copies of the Software,
9# and to permit persons to whom the Software is furnished to do so,
10# subject to the following conditions:
11#
12# The above copyright notice and this permission notice shall be
13# included in all copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22#
23
24# Build a devicetree blob module for Trusty
25#
26# This makefile generates a .dtb from a .dts specified by MODULE_DTS, then
27# generates a .c that includes the .dtb with an .incbin directive and builds the
28# generated source file with library.mk.
29#
30# args:
31# MODULE : module name (required)
32# MODULE_DTS : a .dts file (required)
33# MODULE_DTC_FLAGS : flags passed to dtc invocation
34# MODULE_DTS_INCLUDES : list of include directories used to preprocess the .dts
35
36MODULE_DTS_INCLUDES := $(foreach inc,$(MODULE_DTS_INCLUDES),$(addprefix -I,$(inc)))
37
38MODULE_CPP_DTS := $(patsubst %.dts,$(BUILDDIR)/%.cpp.dts,$(MODULE_DTS))
39MODULE_DTB := $(patsubst %.cpp.dts,%.dtb,$(MODULE_CPP_DTS))
40MODULE_DTB_C := $(patsubst %.dtb,%.dtb.c,$(MODULE_DTB))
41
42DTC_PREBUILT := prebuilts/misc/linux-x86/dtc/dtc
43
44# Preprocess the .dts files
45$(MODULE_CPP_DTS): MODULE_DTS_INCLUDES := $(MODULE_DTS_INCLUDES)
46$(MODULE_CPP_DTS): $(BUILDDIR)/%.cpp.dts: %.dts
47	@$(MKDIR)
48	$(NOECHO)$(CC) -E -nostdinc $(MODULE_DTS_INCLUDES) -undef -D__DTS__ \
49		-x assembler-with-cpp -o $@ $<
50
51# Compile each .dts into a .dtb
52$(MODULE_DTB): MODULE_DTC_FLAGS := $(MODULE_DTC_FLAGS)
53$(MODULE_DTB): DTC_PREBUILT := $(DTC_PREBUILT)
54$(MODULE_DTB): %.dtb: %.cpp.dts
55	@$(MKDIR)
56	$(NOECHO)$(DTC_PREBUILT) -O dtb -o $@ $(MODULE_DTC_FLAGS) --symbols $<
57
58# We generate a symbol name by taking the module name and replacing
59# all non-alphanumeric characters in it with underscores.
60# We need to do this separately for every input file since the symbol
61# names need to all be different. The command needs to be executed
62# as a shell command inside the recipe.
63DT_SYM_MANGLE_CMD = `printf "dtb_sym_$(basename $<)" | tr -c '[:alnum:]' '_'`
64
65# Generate the .c that embeds the .dtb in the module.
66$(MODULE_DTB_C): %.dtb.c: %.dtb
67	@$(MKDIR)
68	$(NOECHO) printf "#include <lk/compiler.h>\n" > $@
69	$(NOECHO) printf "INCBIN_ALIGNED($(DT_SYM_MANGLE_CMD), \
70		$(DT_SYM_MANGLE_CMD)_size, \"$<\", \".dtb\", 8);\n" >> $@
71
72# Add the generated .c to the module sources
73MODULE_SRCS += \
74	$(MODULE_DTB_C) \
75
76include make/library.mk
77
78MODULE_DTS :=
79MODULE_DTC_FLAGS :=
80MODULE_DTS_INCLUDES :=
81
82MODULE_CPP_DTS :=
83MODULE_DTB :=
84MODULE_DTB_C :=
85
86DTC_PREBUILT :=
87