1#!/vendor/bin/sh
2#
3# The script belongs to the feature of UFS FFU via OTA: go/p23-ffu-ota
4# Its purpose is to copy the corresponding firmware into partition for UFS FFU.
5
6ufs_dev="/dev/sys/block/bootdevice"
7fw_dir="/vendor/firmware"
8blk_dev="/dev/block/by-name/fips"
9min_timestamp=$(date -d "2022-10-01" +%s)
10epoch_1y=$(date -u -d "1971-01-01" +%s)
11
12vendor=$(cat ${ufs_dev}/vendor | tr -d "[:space:]")
13model=$(cat ${ufs_dev}/model | tr -d "[:space:]")
14rev=$(cat ${ufs_dev}/rev | tr -d "[:space:]")
15
16file=$(find ${fw_dir} -name "*${vendor}${model}${rev}*" | head -n 1)
17lifec=$(cat ${ufs_dev}/health_descriptor/life_time_estimation_c | tr -d "[:space:]")
18[ ! "$lifec" ] && lifec=0xff
19
20atime=$(ls -lu -d /data 2>&1 | awk '{print $6}')
21timestamp=$(date -d "$atime" +%s)
22(( $timestamp < $min_timestamp )) && timestamp=$min_timestamp
23now=$(date +%s)
24
25if [ -n "$file" ] && (( $lifec < 0x0a )) && (( $timestamp + $epoch_1y >= $now )); then
26	# The first 4KB block at fips partition has been occupied, and unused space begins from 4 KB
27	# Refer to: go/pixel-mp-ffu-ota-1p
28	dd if="$file" of=$blk_dev bs=4k seek=1
29fi
30