aboutsummaryrefslogtreecommitdiffstats
path: root/build.sh
blob: 11913dcc4e5e8fb6dd093fe3a02bafcf600fb182 (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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/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=" \
  i9100 \
  i9300 \
  i9305 \
  n710x \
  odroidu3 \
"

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 <path/to/wireless-regdb-repository/>\n"

  exit 1
}

add_secure_firmware_node()
{
  dtsi="$1"
  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="$1"
    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
}

cmdline_append()
{
    argument="$1"

    if [ -z "${cmdline}" ] ; then
	cmdline="${argument}"
    else
	cmdline="${cmdline} ${argument}"
    fi
}

cmdline_remove()
{
    argument="$1"

    cmdline="$(echo ${cmdline} | sed 's/ *'${argument}' */ /')"
}

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

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

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

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

if [ "${device}" = "odroidu3" -a "${bootloader}" = "s-boot" ] ; then
  echo "Invalid bootloader '${bootloader}' for the ${device}."
  echo "We are not aware of any ${bootloader} port on the ${device}."
  echo "You can try to use 'u-boot' instead of '${bootloader}'"
  echo "Usage:"
  usage
fi


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

if [ ${device} = "i9100" ] ; then
    dtb="exynos4210-${device}.dtb"
fi

make replicant_defconfig

# We are not in the Android tree so we need to tell where is the regdb
# repository
unset_kconfig CONFIG_EXTRA_FIRMWARE_DIR
set_kconfig_value CONFIG_EXTRA_FIRMWARE_DIR "${regdb_repository_path}"

# This script is meant to build a kernel for development purposes (so
# not for releases), so it's a good idea to be able to track which
# kernel is running, especially to record the git revision when some
# driver starts working
set_kconfig_y CONFIG_LOCALVERSION_AUTO

if [ "${os}" = "Android" ] ; then
    set_kconfig_y CONFIG_USB_FUNCTIONFS
    cmdline_append "root=PARTLABEL=SYSTEM init=/init rootwait"
    cmdline_append "firmware_class.path=/vendor/lib/firmware"
    cmdline_append "vt.global_cursor_default=0"
    if [ ${device} == "i9100" ] ; then
	cmdline_remove "androidboot.hardware=smdk4210"
    else
	cmdline_append "androidboot.hardware=smdk4x12"
    fi
    cmdline_append "androidboot.selinux=permissive enforcing=0"
    cmdline_append "exynosdrm.pixel_order=1"
elif [ "${os}" = "GNU/Linux" ] ; then
  unset_kconfig CONFIG_USB_FUNCTIONFS
  # Boot by default on the microSD
  cmdline_append "root=/dev/mmcblk0p1 rw"
fi

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

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
    if [ ${device} != "i9100" -a ${device} != "odroidu3" ] ; then
	add_secure_firmware_node "arch/arm/boot/dts/exynos4412-midas.dtsi"
    fi
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

    if [ ${device} != "i9100" -a ${device} != "odroidu3" ] ; then
	# Remove samsung,secure-firmware as we have no TrustZone OS
	remove_secure_firmware_node "arch/arm/boot/dts/exynos4412-midas.dtsi"
    fi

    # 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

if [ ${device} = "i9100" ] ; then
    cp arch/arm/boot/zImage.dtb arch/arm/boot/boot.img
    du -hs arch/arm/boot/boot.img
else
    base_address=0x10000000

    mkbootimg \
	--base "${base_address}" \
	--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
fi

if [ ${device} = "i9100" -a "${bootloader}" = "s-boot" ] ; then
  echo "The kenrel is ready at arch/arm/boot/boot.img. You can install it with:"
  echo "heimdall flash --KERNEL arch/arm/boot/boot.img --RECOVERY arch/arm/boot/boot.img"
elif [ "${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