summarylogtreecommitdiffstats
path: root/bpir-writefip
blob: fdef883184e82cbd3e475ddc4083391e9396678d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/bin/bash

#############
# Functions #
#############
function getconf {
  conf=$(cat /boot/bootcfg/${1} 2>/dev/null)
  if [ -z "$conf" ]; then
    conf="${2}"
    echo -n "${conf}" >"/boot/bootcfg/${1}"
  fi
  echo "${conf}"
}

#################
# Set variables #
#################
rootdev=$(lsblk -pilno pkname,type,mountpoint | grep -G 'part /$' |  head -n1 | cut -d " " -f1)
partlabelroot=$(lsblk -plno partlabel $rootdev | grep -G '\-root$' )
[ -z "$partlabelroot" ] && exit
target=$(echo $partlabelroot | cut -d'-' -f1)

default_dtb="${target/m/-mini}"
default_dtb="${default_dtb/bpir/-bananapi-bpi-r}"
default_dtb=$(shopt -s nullglob; cd /boot/dtbs; echo "mt"*"${default_dtb}.dtb" | head -n 1)
default_dtb="${default_dtb/.dtb/}"

tmp="/tmp/bpir64-atf-git"
mkdir -p $tmp
mkdir -p /boot/dtbos
mkdir -p /boot/bootcfg
dtb=$(       getconf dtb     "$default_dtb")
atfdevice=$( getconf device  "$(echo $partlabelroot | cut -d'-' -f2)")
linux=$(     getconf linux   "/boot/Image")
cmdline=$(   getconf cmdline "console=ttyS0,115200 debug=7 rw rootwait audit=0")
initrd=$(    getconf initrd  "/boot/initramfs-linux-bpir64-git.img")
atfdtb=$(    getconf atfdtb  "/boot/dtbs/${dtb}-atf.dtb")

########## cleanup *-atf.dtb

fipdevicel="/dev/disk/by-partlabel/${target}-${atfdevice}-fip"
bootdevicel="/dev/disk/by-partlabel/${target}-${atfdevice}-boot"
atfdevicel="/dev/disk/by-partlabel/${target}-${atfdevice}-atf"
fstabstr="PARTLABEL=${target}-${atfdevice}-boot"

#######################
# Convert FIP to BOOT #
#######################
if [[ $@ =~ "--fip2boot" ]] && [ -L ${fipdevicel} ]; then
  set -e
  pkfip=$(lsblk -rno pkname ${fipdevicel})
  nrfip=$(cat "/sys/block/${pkfip}/$(basename $(realpath ${fipdevicel}))/partition")
  if [ -z "$pkfip" ] || [ -z "$nrfip" ]; then exit; fi
  [ -e $tmp/boot ] && rm -rf $tmp/boot
  echo "Moving files from /boot to $tmp/boot"
  mv -fT /boot $tmp/boot
  parted -s -- "/dev/$pkfip" name $nrfip ${target}-${atfdevice}-boot \
                             set  $nrfip boot on
  mkfs.vfat -v -F 32 -S 512 -s 16 -n "${target^^}-BOOT" ${bootdevicel}
  mkdir /boot
  mount -t vfat ${bootdevicel} /boot
  echo "Copying files from $tmp/boot to /boot"
  cp -rfT $tmp/boot /boot
  if [ -z "$(cat /etc/fstab | grep ${fstabstr})" ]; then
    sed -i -e '$a\' /etc/fstab # add newline
    echo "${fstabstr} /boot vfat defaults 0 2" >>/etc/fstab
  fi
  set +e
fi

#######################
# Convert BOOT to FIP #
#######################
if [[ $@ =~ "--boot2fip" ]] && [ -L ${bootdevicel} ]; then
  set -e
  pkfip=$(lsblk -rno pkname ${bootdevicel})
  nrfip=$(cat "/sys/block/${pkfip}/$(basename $(realpath ${bootdevicel}))/partition")
  if [ -z "$pkfip" ] || [ -z "$nrfip" ]; then exit; fi
  [ -e $tmp/boot ] && rm -rf $tmp/boot
  mkdir $tmp/boot
  echo "Copying files from /boot to $tmp/boot"
  cp -rfT /boot/ $tmp/boot
  umount /boot
  parted -s -- "/dev/$pkfip" name $nrfip ${target}-${atfdevice}-fip
  rm -rf /boot
  echo "Moving files from $tmp/boot to /boot"
  mv -fT $tmp/boot /boot
  sed -i '/\b'"${fstabstr}"'\b/d' /etc/fstab
  set +e
fi

########################
# Write ATF bootloader #
########################
if [[ $@ =~ "--atf" ]]; then
  if [ -L ${atfdevicel} ]; then
    if [ -f "/boot/${target}-atf-${atfdevice}-header.bin" ]; then
      headerdev="/dev/"$(lsblk -no pkname ${atfdevicel})
      echo Writing ${target}-atf-${atfdevice}-header.bin to ${headerdev}
      dd if=/boot/${target}-atf-${atfdevice}-header.bin of="${headerdev}"
    fi
    echo -e "Target = ${target}, ATF device = ${atfdevice}\nZeroing: ${atfdevicel}"
    dd bs=64k if=/dev/zero of="${atfdevicel}" 2>/dev/null
    echo "Writing ${target}-atf-${atfdevice}-atf.bin to ${atfdevicel}"
    dd bs=64k if=/boot/${target}-atf-${atfdevice}-atf.bin of="${atfdevicel}"
  fi
fi

#################
# Create DTBO's #
#################
cp -vf "/boot/dtbs/${dtb}.dtb" "/boot/dtbs/${dtb}-fixed.dtb"
rm -rf /boot/dtbos/*.dtbo
for bp in /boot/dtbos/*.dts; do
  echo "Creating .dtbo from $(basename $bp)"
  dtc -@ -q -I dts -O dtb -o ${bp/".dts"/".dtbo"} $bp
  cat $bp | grep "//fdtput" | while read -r line ; do
    echo fdtput "/boot/dtbs/${dtb}-fixed.dtb" ${line/"//fdtput"/""}
         fdtput "/boot/dtbs/${dtb}-fixed.dtb" ${line/"//fdtput"/""}
  done
done

########################
# Create a single .dtb #
########################
fdtoverlay -vi "/boot/dtbs/${dtb}-fixed.dtb" -o "$atfdtb" \
                /boot/dtbos/*.dtbo
origargs=$(fdtget -ts "$atfdtb" "/chosen" "bootargs")
bootargs="root=PARTLABEL=${target}-${atfdevice}-root $origargs $cmdline"
echo BOOTARGS = "$bootargs"
fdtput -ts "$atfdtb" "/chosen" "bootargs" "$bootargs"
fdtput -ts "$atfdtb" "/memory" "device_type" "memory"
if [ -f "$initrd" ];then
  ins="0x48000000"
  ine="0x$(printf '%x\n' $(( $ins + $(du -b $initrd | cut -f1) )))"
  fdtput -tx "$atfdtb" "/chosen" "linux,initrd-end" "$ine"
  fdtput -tx "$atfdtb" "/chosen" "linux,initrd-start" "$ins"
else
  echo -n "EMPTYBYT" > "$tmp/initrd"
  initrd="$tmp/initrd"
fi
if [ ! -f "$atfdtb" ];then
  echo -n "EMPTYBYT" > "$tmp/atfdtb"
  atfdtb="$tmp/atfdtb"
fi

##############################
# Create and write fip image #
##############################
if [ -L ${fipdevicel} ]; then
  bl31="/boot/${target}-atf-${atfdevice}-bl31.bin"
  imageid=$(od --skip-bytes=$((4*14)) --read-bytes=4 --address-radix=n -x \
              $linux | sed 's/ //g')
  fipfiles=""
  [ -f "$bl31" ]   && fipfiles+=" --soc-fw $bl31"
  [ -f "$linux" ]  && fipfiles+=" --nt-fw $linux"
  if [[ "$imageid" == "5241644d" ]]; then # We have a linux kernel image
    [ -f "$atfdtb" ] && fipfiles+=" --nt-fw-config $atfdtb"
    [ -f "$initrd" ] && fipfiles+=" --tos-fw-extra2 $initrd"
  fi
  fiptool --verbose create $tmp/fip.bin $fipfiles
  fiptool info $tmp/fip.bin
  echo Writing FIP to: ${fipdevicel}
  dd bs=1M of=${fipdevicel} if=/dev/zero 2>/dev/null
  dd bs=1M of=${fipdevicel} if=$tmp/fip.bin
  sync
fi

exit 0