1#!/bin/bash
2#
3# Copyright (c) 2021 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15set -e
16
17OUTPUT_DIR=$1
18TYPE=$2
19
20if [ "${TYPE}" = "veritysetup" ]; then
21    veritysetup format ${OUTPUT_DIR}/systemfs_ext4.img ${OUTPUT_DIR}/systemhashfs/system_hashtree\
22    --data-block-size=1024 --hash-block-size=1024 > ${OUTPUT_DIR}/system_verityheader
23    sed -n "9p" ${OUTPUT_DIR}/system_verityheader | awk '{print $3}' > ${OUTPUT_DIR}/systemhashfs/system_roothash
24elif [ "${TYPE}" = "addS82ohos" ]; then
25    mkdir ${OUTPUT_DIR}/rootfs/systemfs
26    mkdir ${OUTPUT_DIR}/rootfs/systemhashfs
27    CMD='
28if [ ! -e /dev/mmcblk0p6 ]; then
29exit 0
30fi
31mount -t ext4 -o ro /dev/mmcblk0p6 /systemhashfs
32/bin/veritysetup create dm-systemfs /dev/mmcblk0p5 /systemhashfs/system_hashtree $(cat /systemhashfs/system_roothash)
33mount -t ext4 /dev/dm-0 /systemfs
34echo 3840 > /sys/block/dm-0/queue/read_ahead_kb
35echo 0 > /sys/module/dm_verity/parameters/prefetch_cluster'
36    echo "$CMD" >> ${OUTPUT_DIR}/rootfs/etc/init.d/S82ohos
37else
38    echo "Unsupported type!" >&2
39fi
40