xf.li | 8611891 | 2025-03-19 20:07:27 -0700 | [diff] [blame^] | 1 | #!/bin/sh |
| 2 | # |
| 3 | #Author: zhouguopo |
| 4 | # |
| 5 | |
| 6 | #外部传参 |
| 7 | # 参数1: mount点 |
| 8 | # 参数2: 分区名 |
| 9 | # 参数3: vol_name |
| 10 | # 参数4: type 默认ubifs,可以是squashfs |
| 11 | mount_point=$1 |
| 12 | mtd_name=$2 |
| 13 | vol_name=$3 |
| 14 | if [ $# -lt 4 ]; then |
| 15 | fs_type=ubifs |
| 16 | else |
| 17 | fs_type=$4 |
| 18 | fi |
| 19 | |
| 20 | secboot=$(cat /proc/cmdline | grep "pubkeyhash=") |
| 21 | sestatus=$(sestatus | grep "SELinux status" | awk '{print $NF}') |
| 22 | |
| 23 | if [ x"$UBI_MNT_OPT" = x"" ]; then |
| 24 | UBI_MNT_OPT=rw,noatime |
| 25 | fi |
| 26 | if [ x"$fs_type" = x"squashfs" ]; then |
| 27 | if [ x"$sestatus" = x"enabled" ]; then |
| 28 | UBI_MNT_OPT=ro,defcontext=system_u:object_r:default_t:s0 |
| 29 | else |
| 30 | UBI_MNT_OPT=ro |
| 31 | fi |
| 32 | fi |
| 33 | echo "mount_point:$mount_point" |
| 34 | echo "mtd_name:$mtd_name" |
| 35 | echo "vol_name:$vol_name" |
| 36 | echo "fs_type:$fs_type" |
| 37 | echo "UBI_MNT_OPT:$UBI_MNT_OPT" |
| 38 | #exit -1 |
| 39 | |
| 40 | g_ubi_dev="ubi0_0" |
| 41 | g_ubiblock_dev="ubiblock0_0" |
| 42 | # check_vol_is_attached vol_name |
| 43 | # 1 is attached, 0 not attached |
| 44 | function check_vol_is_attached() |
| 45 | { |
| 46 | ret=0 |
| 47 | vol_name=$1 |
| 48 | found=0 |
| 49 | #declare -g g_ubi_dev |
| 50 | while read -r f; do |
| 51 | vol_name_tmp=$(cat "$f") |
| 52 | if [ x"$vol_name_tmp" = x"$vol_name" ]; then |
| 53 | g_ubi_dev=$(echo "$f" | awk -F'/' '{print $7}') |
| 54 | echo "$vol_name already attached $g_ubi_dev" |
| 55 | found=1 |
| 56 | break |
| 57 | fi |
| 58 | done <<EOF |
| 59 | $(find /sys/devices/virtual/ubi -name "name") |
| 60 | EOF |
| 61 | if [ "$found" -eq 1 ]; then |
| 62 | ret=1 |
| 63 | fi |
| 64 | return "$ret" |
| 65 | } |
| 66 | |
| 67 | function mtd_do_attach() |
| 68 | { |
| 69 | mtd_name_temp=$1 |
| 70 | #not attached and do attach |
| 71 | MTD_NUM=`cat /proc/mtd | grep "$mtd_name\"" | awk '{print $1}'| cut -b 4- |sed 's/://g'` |
| 72 | echo "attach $mtd_name mtd$MTD_NUM" |
| 73 | ubiattach /dev/ubi_ctrl -m ${MTD_NUM} |
| 74 | if [ $? != 0 ];then |
| 75 | echo "fail to attach $2" |
| 76 | return 1 |
| 77 | fi |
| 78 | return 0 |
| 79 | } |
| 80 | |
| 81 | check_vol_is_attached $vol_name |
| 82 | if [ $? = 0 ];then |
| 83 | mtd_do_attach $mtd_name #not attached and do attach |
| 84 | if [ $? = 1 ]; then |
| 85 | exit -1 #attach fail and exit error |
| 86 | else |
| 87 | #check again,fill g_ubi_dev |
| 88 | check_vol_is_attached $vol_name |
| 89 | if [ $? = 0 ];then |
| 90 | echo "check ubi vol attached again and fail" |
| 91 | exit -2 |
| 92 | fi |
| 93 | fi |
| 94 | fi |
| 95 | |
| 96 | if [ $fs_type = "squashfs" ]; then |
| 97 | g_ubiblock_dev=`echo $g_ubi_dev | sed 's/ubi/ubiblock/'` |
| 98 | echo "g_ubiblock_dev:$g_ubiblock_dev" |
| 99 | if [ ! -b "/dev/$g_ubiblock_dev" ]; then |
| 100 | echo "g_ubiblock_dev:$g_ubiblock_dev not exist and create" |
| 101 | ubiblock -c /dev/$g_ubi_dev |
| 102 | fi |
| 103 | fi |
| 104 | |
| 105 | if [ $fs_type = "squashfs" ]; then |
| 106 | if [[ "$secboot" != "" ]]; then |
| 107 | zxic_parse_squashfs_verity /dev/$g_ubiblock_dev /tmp/sign /tmp/raw_table /tmp/hash_tree_offset |
| 108 | #openssl dgst -sha256 -verify /etc_ro/dm-verity-pub.pem -signature /tmp/sign /tmp/raw_table |
| 109 | if [ -f /usr/lib/libcrypto.so.3 ]; then |
| 110 | oem_zxic_verify_3 -s /tmp/sign -f /tmp/raw_table |
| 111 | else |
| 112 | oem_zxic_verify -s /tmp/sign -f /tmp/raw_table |
| 113 | fi |
| 114 | if [ $? -ne 0 ]; then |
| 115 | echo "dm-verity sign verify fail" |
| 116 | exit 1 |
| 117 | fi |
| 118 | |
| 119 | root_hash=` sed -n '/Root hash/p' /tmp/raw_table | awk '{print $3}' ` |
| 120 | salt=` sed -n '/Salt/p' /tmp/raw_table | awk '{print $2}' ` |
| 121 | hash_offset=` cat /tmp/hash_tree_offset ` |
| 122 | veritysetup --restart-on-corruption open /dev/$g_ubiblock_dev $mtd_name /dev/$g_ubiblock_dev --hash-offset=$hash_offset $root_hash -s $salt |
| 123 | if [ $? -ne 0 ]; then |
| 124 | echo "dm-verity veritysetup open fail" |
| 125 | exit 1 |
| 126 | fi |
| 127 | rm /tmp/sign /tmp/raw_table /tmp/hash_tree_offset |
| 128 | mount -t $fs_type -o $UBI_MNT_OPT /dev/mapper/$mtd_name $mount_point |
| 129 | else |
| 130 | mount -t $fs_type -o $UBI_MNT_OPT /dev/$g_ubiblock_dev $mount_point |
| 131 | fi |
| 132 | else |
| 133 | mount -t $fs_type -o $UBI_MNT_OPT $g_ubi_dev $mount_point |
| 134 | fi |
| 135 | |