Enable making block device's hash table 34/246034/5
authorKichan Kwon <k_c.kwon@samsung.com>
Thu, 22 Oct 2020 10:42:54 +0000 (19:42 +0900)
committerKichan Kwon <k_c.kwon@samsung.com>
Thu, 29 Oct 2020 09:11:46 +0000 (18:11 +0900)
Change-Id: I1a9aa283dd81a0f2922a256c5ab9c0b8c8d5ab6b
Signed-off-by: Kichan Kwon <k_c.kwon@samsung.com>
scripts/verityctl

index 6229846c8a2afd148668fd0faf27c6f59ba2a1d4..06c0f0bc4976e77dfe09e20ce90a9cf6022a148e 100755 (executable)
@@ -17,7 +17,7 @@ format()
 {
        IMG_FILE=$1
 
-       if [ -f $IMG_FILE ]
+       if [ -f $IMG_FILE ] || [ -b $IMG_FILE ]
        then
                echo "Run verityctl format $IMG_FILE"
        else
@@ -25,9 +25,34 @@ format()
                exit 1
        fi
 
-       IMG_PATH=`dirname $IMG_FILE`
+       # Block device such as /dev/mmcblk0p1
+       if [ -b $IMG_FILE ]
+       then
+               IMG_PATH=/tmp
+
+               block_count=`/sbin/tune2fs -l $IMG_FILE | grep "Block count" | gawk '{print $3}'`
+               block_size=`/sbin/tune2fs -l $IMG_FILE | grep "Block size" | gawk '{print $3}'`
+
+               ((meta_offset=$block_count * $block_size))
+               part_block_size=`lsblk -rbno SIZE $IMG_FILE`
+
+               echo "partition size: $part_block_size"
+               echo "meta data offset: $meta_offset"
+
+               if [ $part_block_size -le $meta_offset ]
+               then
+                       echo "$IMG_FILE does not have dm-verity meta data"
+                       exit 0
+               fi
+
+               OPTS="--data-blocks=$block_count"
+       else
+               IMG_PATH=`dirname $IMG_FILE`
+               OPTS=""
+       fi
+
+       /sbin/veritysetup format $OPTS $IMG_FILE $IMG_PATH/hash_data | tee $IMG_PATH/verity_format_output.txt
 
-       /sbin/veritysetup format $IMG_FILE $IMG_PATH/hash_data | tee $IMG_PATH/verity_format_output.txt
        root_hash=`grep "Root hash" $IMG_PATH/verity_format_output.txt | gawk '{print $3}'`
        salt=`grep "Salt" $IMG_PATH/verity_format_output.txt | gawk '{print $2}'`
 
@@ -37,7 +62,26 @@ format()
        echo $root_hash   | dd of=/$IMG_PATH/meta_data bs=1 seek=32 conv=notrunc 2> /dev/null
        echo $salt        | dd of=/$IMG_PATH/meta_data bs=1 seek=96 conv=notrunc 2> /dev/null
 
-       cat $IMG_PATH/meta_data $IMG_PATH/hash_data >> $IMG_FILE
+       if [ -b $IMG_FILE ]
+       then
+               ((hash_offset=$meta_offset + 32768))
+
+               hash_size=`ls -s --block-size=1 $IMG_PATH/hash_data | gawk '{print $1}'`
+               ((hash_end_offset=$hash_offset + $hash_size))
+
+               echo "hash end offset: $hash_end_offset"
+
+               if [ $part_block_size -lt $hash_end_offset ]
+               then
+                       echo "$IMG_FILE does not have enough space for hash data"
+                       exit 1
+               fi
+
+               dd if=$IMG_PATH/meta_data of=$IMG_FILE bs=1 seek=$meta_offset 2> /dev/null
+               dd if=$IMG_PATH/hash_data of=$IMG_FILE bs=1 seek=$hash_offset 2> /dev/null
+       else
+               cat $IMG_PATH/meta_data $IMG_PATH/hash_data >> $IMG_FILE
+       fi
 
        rm -f $IMG_PATH/hash_data
        rm -f $IMG_PATH/meta_data