1#!/bin/bash
2
3# Copyright (C) 2021 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17if [ -z "${ANDROID_BUILD_TOP}" ]; then
18    WD="$(pwd)"
19else
20    WD="${ANDROID_BUILD_TOP}"
21fi
22if [ -z "${OUT_DIR}" ]; then
23    OUT_DIR="${WD}/out"
24fi
25
26if [[ -z "${DIST_DIR}" ]]; then
27  echo "DIST_DIR must be defined." 1>&2
28  exit 1
29fi
30
31GZIP="gzip"
32LZ4="${OUT_DIR}/host/linux-x86/bin/lz4"
33
34function prepare_lz4()
35{
36  if ! [ -f ${LZ4} ]; then
37    echo "make $LZ4"
38    cd ${WD}
39    build/soong/soong_ui.bash --make-mode lz4
40    cd -
41  fi
42}
43
44#
45#  This function copies kernel prebuilts from build chaining path to a staging
46#  output directory.
47#
48function prepare_kernel_image()
49{
50  local prebuilt_path=$1
51  local kernel_version=$2
52  local arch=$3
53  local build_variant=$4
54
55  local prebuilts_root="out/prebuilt_cached/${prebuilt_path}"
56  local out_root="${OUT_DIR}/target/kernel/${kernel_version}/${arch}"
57  local dist_root="${DIST_DIR}/kernel/${kernel_version}"
58  local postfix=""
59  if [[ "$build_variant" == "debug" ]]; then
60     dist_root="${dist_root}-debug"
61     postfix="-allsyms"
62  fi
63
64  mkdir -p "${out_root}"
65
66  printf "%-38s %20s\n" "copy kernel ${kernel_version} ${arch} ${build_variant} to" "${out_root}"
67  if [[ "$arch" == "x86_64" ]]; then
68    cp "${prebuilts_root}/bzImage" "${out_root}/kernel-${kernel_version}${postfix}"
69  else
70    # Pack all compress format for kernel images
71    cp "${prebuilts_root}/Image" "${out_root}/kernel-${kernel_version}${postfix}"
72    cp "${prebuilts_root}/Image.lz4" "${out_root}/kernel-${kernel_version}-lz4${postfix}"
73    "$GZIP" -nc \
74       "${prebuilts_root}/Image">"${out_root}/kernel-${kernel_version}-gz${postfix}"
75  fi
76
77  # Prepare the dist folder
78  mkdir -p "${dist_root}"
79
80  # Prepare prebuilt-info.txt
81#  BID=$(cat "${prebuilts_root}/BUILD_INFO" | sed -n 's/^.*"bid":\s*"\(.*\)".*$/\1/p')
82#  cat > "${dist_root}/prebuilt-info.txt" <<EOF
83#{
84#    "kernel-build-id": ${BID}
85#}
86#EOF
87  printf "generate ${dist_root}/prebuilt-info.txt with kernel\n"
88  cksum "${out_root}/kernel-${kernel_version}${postfix}"
89  local t="${out_root}/prebuilt-info.txt"
90  echo "{" > $t
91  echo -n "    \"kernel-build-id\": " >> $t
92  strings "${out_root}/kernel-${kernel_version}${postfix}" |grep -E "Linux version [0-9]\." | sed -e 's/Linux version.*-ab//'| cut -f1 -d ' ' >> $t
93  echo "}" >> $t
94  cp $t ${dist_root}/prebuilt-info.txt
95
96}
97
98#
99#  This function copies kernel module prebuilts from the build chaining path to a
100#  staging output directory.
101#
102function prepare_kernel_modules()
103{
104  local prebuilt_path=$1
105  local kernel_version=$2
106  local arch=$3
107
108  local prebuilts_root="out/prebuilt_cached/${prebuilt_path}"
109  local out_root="${OUT_DIR}/target/kernel/${kernel_version}/${arch}"
110  local dist_root="${DIST_DIR}/kernel/${kernel_version}"
111
112  local initramfs_root="${out_root}/initramfs"
113  rm -rf ${initramfs_root}
114  mkdir -p "${initramfs_root}"
115
116  printf "%-38s %20s\n" "copy kernel modules ${kernel_version} ${arch} to" "${initramfs_root}"
117  "${LZ4}" -dcfm "${prebuilts_root}/initramfs.img" | (cd "${initramfs_root}"; cpio -imd)
118
119  for x in $(find "${initramfs_root}" -type f -name "*.ko"); do
120    cp "$x" "${out_root}"
121  done
122}
123
124#
125#  This function updates kernel prebuilts with the local staging directory.
126#
127function update_kernel_prebuilts_with_artifact
128{
129  local kernel_version=$1
130  local arch=$2
131  local out_root="${OUT_DIR}/target/kernel/${kernel_version}/${arch}"
132  local prebuilts_dir="kernel/prebuilts/${kernel_version}/${arch}/"
133  local list="\
134    kernel-${kernel_version}-allsyms \
135    kernel-${kernel_version}-gz-allsyms \
136    kernel-${kernel_version}-lz4-allsyms \
137    kernel-${kernel_version} \
138    kernel-${kernel_version}-gz \
139    kernel-${kernel_version}-lz4 \
140    prebuilt-info.txt"
141  printf "%20s\n --> %20s\n" "${out_root}" "${prebuilts_dir}"
142  for f in ${list}; do
143    echo \
144    cp -f ${out_root}/$f ${prebuilts_dir}
145    cp -f ${out_root}/$f ${prebuilts_dir}
146  done
147}
148
149#
150#  This function updates kernel module prebuilts with the local staging directory.
151#
152function update_kernel_module_prebuilts_with_artifact
153{
154  local kernel_version=$1
155  local arch=$2
156  local out_root="${OUT_DIR}/target/kernel/${kernel_version}/${arch}"
157  local initramfs_root="${out_root}/initramfs"
158  local prebuilts_dir="kernel/prebuilts/common-modules/virtual-device/${kernel_version}/${arch}/"
159
160  printf "%20s\n --> %20s\n" "${initramfs_root}" "${prebuilts_dir}"
161  rm -f ${prebuilts_dir}/*.ko
162  for x in $(find "${initramfs_root}" -type f -name "*.ko"); do
163    cp -f ${x} ${prebuilts_dir}
164  done
165}
166
167function pack_boot_for_certification
168{
169  local file=boot.zip
170  local dist=$1
171  pushd ${dist}
172  rm -f ${file}
173  zip ${file} boot*.img
174  popd
175}
176