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
17set -e
18
19KERNEL_INFO_FILES=(
20  "System.map"
21  "vmlinux"
22  "vmlinux.symvers"
23  "modules.builtin"
24  "modules.builtin.modinfo"
25  "system_dlkm.img"
26  "system_dlkm_staging_archive.tar.gz"
27)
28
29function download_kernel_info_files {
30  local bid=$1
31  local kernel_target=$2
32  local output_folder=$3
33
34  local url_base="https://ci.android.com/builds/submitted/${bid}/${kernel_target}/latest"
35  local url_list="${url_base}/list.json"
36  local list_json="$(curl -sfL "$url_list")"
37  # do nothing and return if cannot get the file list
38  [[ -z "$list_json" ]] && return
39
40  # Pick manifest.xml if the file is not ready
41  local artifact_manifest="manifest_${bid}.xml"
42  local output_manifest="${output_folder}/manifest.xml"
43  if [[ ! -f "$output_manifest" ]]; then
44    echo "Pick ${artifact_manifest} to ${output_manifest}..."
45    curl -sfL "${url_base}/raw/${artifact_manifest}" -o "$output_manifest"
46  fi
47
48  # Pick kernel binaries
49  for f in "${KERNEL_INFO_FILES[@]}"; do
50    # The URL request always return 200 even the file does not exist,
51    # so we check it in the file list. Skip the file if it does not exist.
52    [[ "$list_json" =~ "\"name\":\"${f}\"" ]] || continue
53
54    local output="${output_folder}/$(basename "$f")"
55
56    echo "Pick ${output}..."
57    curl -sfL "${url_base}/raw/${f}" -o "$output"
58  done
59}
60
61function download_all_kernel_info_files {
62  local kernel_target=$1
63  local folder_pattern=$2
64
65  for folder in $(find "${DIST_DIR}/kernel" -type d -regex ".*/${folder_pattern}"); do
66    local prebuilt_info="${folder}/prebuilt-info.txt"
67    local bid=$(cat "$prebuilt_info" | sed -rn 's/.*"kernel-build-id"\s*:\s*([0-9]+).*/\1/p')
68    download_kernel_info_files "$bid" "$kernel_target" "$folder"
69  done
70}
71
72
73if [[ -z "${DIST_DIR}" ]]; then
74  echo "DIST_DIR must be defined." 1>&2
75  exit 1
76fi
77
78ARCH=$1
79
80download_all_kernel_info_files "kernel_${ARCH}" '[0-9]+.[0-9]+'
81download_all_kernel_info_files "kernel_debug_${ARCH}" '[0-9]+.[0-9]+-debug'
82