treewide: Move device_tree to commonlib
[coreboot2.git] / util / scripts / dts-to-fmd.sh
blob5c3772697b2a5d3a19270b508a0f5a61450904a5
1 #!/usr/bin/env bash
2 # SPDX-License-Identifier: GPL-2.0-only
4 # converts a depthcharge fmap.dts into an fmaptool compatible .fmd format
5 # requires fdt utilities (dtc, fdtget)
7 # $1 dts file name
8 # result on stdout
9 set -e
11 if [ $# -lt 1 ]; then
12 echo "usage: $0 dts-file"
13 exit 1
16 DTS=$1
17 DTB=`mktemp`
19 # $1 node
20 # $2 start variable name
21 # $3 size variable name
22 get_reg() {
23 local t=`fdtget -t x $DTB $1 reg 2>/dev/null`
24 if [ -n "$t" ]; then
25 export $2=0x`echo $t|cut -d' ' -f1`
26 export $3=0x`echo $t|cut -d' ' -f2`
27 else
28 export $3=0x`fdtget -t x $DTB $1 size`
32 dtc -O dtb -o $DTB $DTS
33 get_reg /flash ROM_START ROM_SIZE
34 printf "FLASH@${ROM_START} ${ROM_SIZE} {"
36 PREFIX="\t"
37 REGION_START=-1
38 REGION_SIZE=0
39 CONTAINER_END=$(( ${ROM_SIZE} ))
40 CONTAINER_END_STACK=${CONTAINER_END}
41 CONTAINER_OFFSET=0
42 CONTAINER_OFFSET_STACK=0
44 FMAP_REGIONS=`fdtget -l $DTB /flash`
45 for region in $FMAP_REGIONS; do
46 OLD_REGION_START=$REGION_START
47 OLD_REGION_SIZE=$REGION_SIZE
48 get_reg /flash/$region REGION_START REGION_SIZE
50 # determine if we're past an existing container
51 while [ $(( ${REGION_START} )) -ge ${CONTAINER_END} ]; do
52 PREFIX=`printf "${PREFIX}" | cut -c2-`
53 printf "\n${PREFIX}}"
54 CONTAINER_END_STACK=`printf "${CONTAINER_END_STACK}" | cut -d' ' -f2-`
55 CONTAINER_OFFSET_STACK=`printf "${CONTAINER_OFFSET_STACK}" | cut -d' ' -f2-`
56 CONTAINER_END=`printf ${CONTAINER_END_STACK} | cut -d' ' -f1`
57 CONTAINER_OFFSET=`printf ${CONTAINER_OFFSET_STACK} | cut -d' ' -f1`
58 done
60 # determine if we're inside a new container region now
61 if [ $(( ${OLD_REGION_START} + ${OLD_REGION_SIZE} )) -gt $(( ${REGION_START} )) ]; then
62 PREFIX="\t${PREFIX}"
63 CONTAINER_END=$(( ${OLD_REGION_START} + ${OLD_REGION_SIZE} ))
64 CONTAINER_OFFSET=$(( ${OLD_REGION_START} ))
65 CONTAINER_END_STACK="${CONTAINER_END} ${CONTAINER_END_STACK}"
66 CONTAINER_OFFSET_STACK="${CONTAINER_OFFSET} ${CONTAINER_OFFSET_STACK}"
67 printf " {"
70 LOCAL_REGION_START=$(( ${REGION_START} - ${CONTAINER_OFFSET} ))
71 LOCAL_REGION_START=`printf "0x%x" ${LOCAL_REGION_START}`
73 REGION_NAME=`fdtget $DTB /flash/$region label | tr '[a-z]-' '[A-Z]_'`
74 REGION_TYPE=`fdtget $DTB /flash/$region type 2>/dev/null | cut -d'/' -f1`
76 # a CBFS region? if so, mark as such
77 if [ "${REGION_TYPE}" = "blob cbfs" ]; then
78 IS_CBFS="(CBFS)"
79 else
80 IS_CBFS=""
83 # special handling: rename BOOT_STUB to COREBOOT, mark them as CBFS
84 if [ "${REGION_NAME}" = "BOOT_STUB" ]; then
85 REGION_NAME="COREBOOT"
87 if [ "${REGION_NAME}" = "COREBOOT" ]; then
88 IS_CBFS="(CBFS)"
91 # also mark RW_LEGACY (seabios et al) as CBFS
92 if [ "${REGION_NAME}" = "RW_LEGACY" ]; then
93 IS_CBFS="(CBFS)"
96 # special handling: COREBOOT region at 0, inject a 128K bootblock
97 # The size may need changes to accommodate the chipsets,
98 # but should work for now.
99 if [ "${REGION_NAME}" = "COREBOOT" -a \
100 $(( ${REGION_START} )) -eq 0 ]; then
101 printf "\n${PREFIX}BOOTBLOCK@0 128K"
102 LOCAL_REGION_START=$(( ${LOCAL_REGION_START} + 128*1024 ))
103 LOCAL_REGION_START=`printf 0x%x ${LOCAL_REGION_START}`
104 REGION_SIZE=$(( ${REGION_SIZE} - 128*1024 ))
105 REGION_SIZE=`printf 0x%x ${REGION_SIZE}`
108 printf "\n${PREFIX}${REGION_NAME}${IS_CBFS}@${LOCAL_REGION_START} ${REGION_SIZE}"
109 done
111 while [ -n "${PREFIX}" ]; do
112 PREFIX=`printf "${PREFIX}" | cut -c2-`
113 printf "\n${PREFIX}}"
114 done
115 printf "\n"
117 rm -f $DTB