1#!/bin/bash
2
3# Copyright 2019 Google Inc. All rights reserved.
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
17source "${ANDROID_BUILD_TOP}/external/shflags/src/shflags"
18
19DEFINE_string loader1 \
20  "" "Path to loader1 image (partition 1)" "l"
21DEFINE_string env \
22  "" "Path to env image (partition 2)" "e"
23DEFINE_string loader2 \
24  "" "Path to loader2 image (partition 3)" "u"
25DEFINE_string trust \
26  "" "Path to trust image (partition 4)" "t"
27DEFINE_string rootfs \
28  "" "Path to rootfs image (partition 5)" "r"
29DEFINE_string tftp \
30  "192.168.0.1" "TFTP server address" "f"
31DEFINE_string tftpdir \
32  "/tftpboot" "TFTP server directory" "d"
33DEFINE_string version \
34  "2" "Specify which manifest version to use (default: latest)" "v"
35DEFINE_string ethaddr \
36  "" "MAC address of device to DFU (default: all)" "m"
37DEFINE_string kernel \
38  "" "Path to kernel build dir" "k"
39
40FLAGS_HELP="USAGE: $0 --kernel <dir> [flags]"
41
42FLAGS "$@" || exit $?
43eval set -- "${FLAGS_ARGV}"
44
45for arg in "$@" ; do
46	flags_help
47	exit 1
48done
49
50if [ -z ${FLAGS_kernel} ]; then
51	flags_help
52	exit 1
53fi
54
55confirm() {
56    read -r -p "${1:-Are you sure you want to continue? [y/N]} " response
57    case "$response" in
58        [yY][eE][sS]|[yY])
59            true
60            ;;
61        *)
62            false
63            ;;
64    esac
65}
66
67createManifest() {
68	>>manifest.txt
69}
70
71addKVToManifest() {
72	key=$1
73	value=$2
74	grep -q "^${key}=" manifest.txt && \
75		sed -i "s/^${key}=.*/${key}=${value}/" manifest.txt || \
76		echo "${key}=${value}" >> manifest.txt
77}
78
79addShaToManifest() {
80	DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
81	addKVToManifest "Sha" `${DIR}/gen_sha.sh --kernel ${FLAGS_kernel}`
82}
83
84addPathToManifest() {
85	key=$1
86	path=$2
87
88	if [ "${path}" != "" ]; then
89		filename=$(basename $path)
90		filetype=`file -b --mime-type "${path}"`
91		if [ "$key" == "UbootEnv" ] && [ "${filetype}" == "application/gzip" ]; then
92			echo "error: gzip not supported for env images"
93		fi
94		if [ "$key" != "UbootEnv" ] && [ "${filetype}" != "application/gzip" ]; then
95			echo "warning: gzip recommended for all non-env images"
96			confirm || exit 1
97		fi
98		if [ ! "${path}" -ef "${FLAGS_tftpdir}/${filename}" ]; then
99			cp "${path}" "${FLAGS_tftpdir}/"
100		fi
101	else
102		unset filename
103	fi
104
105	addKVToManifest "${key}" "${filename}"
106}
107
108createManifest
109addKVToManifest ManifestVersion ${FLAGS_version}
110addKVToManifest TftpServer ${FLAGS_tftp}
111addKVToManifest DFUethaddr ${FLAGS_ethaddr}
112addPathToManifest RootfsImg ${FLAGS_rootfs}
113addPathToManifest UbootEnv ${FLAGS_env}
114addPathToManifest TplSplImg ${FLAGS_loader1}
115addPathToManifest UbootItb ${FLAGS_loader2}
116addShaToManifest
117