aboutsummaryrefslogtreecommitdiffstats
path: root/build.sh
blob: 53c645aef78c2d81c1cf6cb71911d53822d6dc25 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/bin/bash
# Copyright (C) 2019 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

set -e

supported_devices=" \
  i9300 \
  i9305 \
  n710x \
"

supported_os=" \
  Android \
  GNU/Linux \
"

supported_bootloaders="\
  s-boot \
  u-boot \
"

sentinel=""
dtb=""
cmdline=""

print_supported()
{
  printf "<"
  for elm in $@ ; do
    printf "${elm}|"
  done
  printf "\b> "
}

# Example:
# check_supported i9300 device ${supported_devices}
check_supported()
{
  elm="$1"
  type="$2"
  shift 2

  for supported in $@ ; do
    if [ "${elm}" = "${supported}" ] ; then
      return 0
    fi
  done

  echo "Invalid ${type} '${elm}'. Usage:"
  usage
  exit 1
}

usage()
{
  printf "%s " "$0"
  print_supported ${supported_devices}
  print_supported ${supported_bootloaders}
  print_supported ${supported_os}
  printf "\b\n"

  exit 1
}

add_secure_firmware_node()
{
  dtsi="arch/arm/boot/dts/exynos4412-midas.dtsi"
  if ! grep "firmware@204f000" ${dtsi} 2>&1 > /dev/null ; then
      # Add the node
      cat ${dtsi} | \
      awk 'BEGIN {chosen_found=0} \
        { \
          {print $0}
          if ($0 == "\tchosen {") {chosen_found=1}; fi; \
          if (chosen_found == 1 && $0 == "\t};") { \
            chosen_found=0; \
            print(""); \
            print("\tfirmware@204f000 {"); \
            print("\t\tcompatible = \"samsung,secure-firmware\";"); \
            print("\t\treg = <0x0204F000 0x1000>;"); \
            print("\t};"); \
          }; fi; \
        }' \
      > ${dtsi}.1
      mv -f ${dtsi}.1 ${dtsi}
    fi
}

remove_secure_firmware_node()
{
    dtsi="arch/arm/boot/dts/exynos4412-midas.dtsi"
    cat ${dtsi}  | \
    awk 'BEGIN {do_print=1} \
	{ \
	  if ($0 == "\tfirmware@204f000 {") {do_print=0}; fi; \
	  if (do_print == 1) {print $0} ; fi; \
	  if ($0 == "\t};") {do_print=1}; fi; \
	}' \
	> ${dtsi}.1
   mv -f ${dtsi}.1 ${dtsi}
}

add_arm_decompressor_tlb_flush()
{
  head_s_file="arch/arm/boot/compressed/head.S"

  if ! grep -A 1 -P '^\t\tmcrne\tp15, 0, r0, c8, c7, 0\t@ flush I,D TLBs$' \
    arch/arm/boot/compressed/head.S | \
    tail -n1 | \
    grep -P '^\t\tmcr\tp15, 0, r0, c7, c5, 4\t@ ISB$' \
    2>&1 > /dev/null ; then
    cat ${head_s_file} | \
    awk 'BEGIN {armv7_mmu_cache_on_found=0;} \
         { \
           if ($0 == "__armv7_mmu_cache_on:") { \
             armv7_mmu_cache_on_found=1; \
           }; fi; \
           if (armv7_mmu_cache_on_found == 1 && \
              ($0 == "\t\tmcrne\tp15, 0, r3, c2, c0, 0\t@ load page table pointer")) { \
               armv7_mmu_cache_on_found=0; \
               print("\t\tmcrne\tp15, 0, r0, c8, c7, 0\t@ flush I,D TLBs"); \
               print("\t\tmcr\tp15, 0, r0, c7, c5, 4\t@ ISB"); \
            }; fi; \
           {print $0} \
         }' \
    > ${head_s_file}.1
    mv -f ${head_s_file}.1 ${head_s_file}
fi
}

remove_arm_decompressor_tlb_flush()
{
    # I'm unlikely to make other changes to that file
    git checkout -- arch/arm/boot/compressed/head.S
}

check_kconfig_empty_config()
{
    config="$1"

    if [ "${config}" = "" ] ; then
	echo "Exiting: empty kconfig configuration"
	exit 1
    fi
}

set_kconfig_value()
{
    config="$1"
    check_kconfig_empty_config "${config}"

    shift 1
    value="$@"

    sed "/^${config}=\"*\"/d" -i .config
    echo "${config}=\"${value}\"" >> .config
}

set_kconfig_y()
{
    config="$1"

    check_kconfig_empty_config "${config}"
    sed "/^${config}=*/d" -i .config
    echo "${config}=y" >> .config
}

unset_kconfig()
{
    config="$1"

    check_kconfig_empty_config "${config}"
    sed "/^${config}=*/d" -i .config
    echo "# ${config} is not set" >> .config
}

jobs="-j$(grep processor /proc/cpuinfo  | wc -l) -k"

source arm_build.sh

if [ $# -ne 3 ] ; then
    usage
fi

device="$1"
bootloader="$2"
os="$3"

check_supported "${device}" "device" ${supported_devices}
check_supported "${bootloader}" "bootloader" ${supported_bootloaders}
check_supported "${os}" "OS" ${supported_os}

dtb="exynos4412-${device}.dtb"

make replicant_defconfig

if [ "${os}" = "Android" ] ; then
    set_kconfig_y CONFIG_USB_FUNCTIONFS
    cmdline="${cmdline} root=PARTLABEL=SYSTEM init=/init rootwait"
    cmdline="${cmdline} androidboot.hardware=smdk4x12"
    cmdline="${cmdline} androidboot.selinux=permissive enforcing=0"
    cmdline="${cmdline} exynosdrm.pixel_order=1"
elif [ "${os}" = "GNU/Linux" ] ; then
  unset_kconfig CONFIG_USB_FUNCTIONFS
  cmdline="${cmdline} root=/dev/mmcblk0 rw"
fi

cmdline="${cmdline} buildvariant=eng device=${device} rootwait console=ttySAC2,115200 loglevel=8"
cmdline="${cmdline} no_console_suspend"

if [ "${bootloader}" = "s-boot" ] ; then
    # boot.img cmdline is ignored and Android userspace needs access
    # to the IMEI which is passed as boot arguments
    unset_kconfig CONFIG_CMDLINE_FORCE
    unset_kconfig CONFIG_CMDLINE_FROM_BOOTLOADER
    set_kconfig_y CONFIG_CMDLINE_EXTEND
    set_kconfig_value CONFIG_CMDLINE "${cmdline}"

    # Workaround fatally flawed bootloader which has MMU on
    add_arm_decompressor_tlb_flush
    unset_kconfig CONFIG_STACKPROTECTOR_PER_TASK

    # Make sure that samsung,secure-firmware is present
    add_secure_firmware_node
elif [ "${bootloader}" = "u-boot" ] ; then
    # Make sure that the bootargs comes from the boot.img
    unset_kconfig CONFIG_CMDLINE
    unset_kconfig CONFIG_CMDLINE_FORCE
    unset_kconfig CONFIG_CMDLINE_EXTEND
    set_kconfig_y CONFIG_CMDLINE_FROM_BOOTLOADER

    # Remove samsung,secure-firmware as we have no TrustZone OS
    remove_secure_firmware_node

    # Remove workaround for s-boot if present
    remove_arm_decompressor_tlb_flush
fi

# Save the config
yes '' | make oldconfig
make savedefconfig
cp -f defconfig arch/arm/configs/replicant_defconfig

make ${jobs} \
	zImage \
	"${dtb}" \
	${sentinel}

cat \
	arch/arm/boot/zImage \
	"arch/arm/boot/dts/${dtb}" \
	> arch/arm/boot/zImage.dtb

mkbootimg \
	--base 0x10000000 \
	--kernel arch/arm/boot/zImage.dtb \
	--cmdline "${cmdline}" \
	--output arch/arm/boot/boot.img \
	${sentinel}

du -hs arch/arm/boot/boot.img
unbootimg -i arch/arm/boot/boot.img

if [ "${bootloader}" = "s-boot" ] ; then
  echo "The kenrel is ready at arch/arm/boot/boot.img. You can install it with:"
  echo "heimdall flash --BOOT arch/arm/boot/boot.img --RECOVERY arch/arm/boot/boot.img"
else
  echo OK
fi