#!/bin/sh
#
# dm250 restore script, using backups from EKESETE.net script
#

EMMC=$1

if [ "$EMMC" = "" ]; then
	echo "usage: ${0} <device>"
	exit 1
fi

if [ ! -f mmcblk0p1.img ]; then
	echo "mmcblk0p1.img not found"
	exit 1
fi

echo -n "restoring to ${EMMC}, ok? "
read

# the backup script does not back up the rockchip ID block starting at sector
# 64 (header, rk3128_ddr_300MHz_v2.04.bin, rk312x_miniloader, u-boot
# parameters) but it's the same on every dm250 extracted with:
#   dd if=mmcblk0.img of=idb.img bs=512 count=8192
# or fetched from:
#	https://jcs.org/dm250/dm250-idb.img

if [ -f dm250-idb.img ]; then
	echo "restoring IDB @0..."
	dd if=dm250-idb.img of=${EMMC} conv=notrunc
else
	echo "no dm250-idb.img, not restoring IDB"
fi

# rkpart offsets are relative to the end of the 4MB (8192 sector) reserved
# area, so raw eMMC sector = rkpart_sector + 8192.

echo "restoring uboot @8388608..."
dd if=mmcblk0p1.img of=${EMMC} bs=1m seek=8 conv=notrunc

echo "restoring kernel @16777216..."
dd if=mmcblk0p2.img of=${EMMC} bs=1m seek=16 conv=notrunc

echo "restoring resource @29360128..."
dd if=mmcblk0p3.img of=${EMMC} bs=1m seek=28 conv=notrunc

echo "restoring boot @35651584..."
dd if=mmcblk0p4.img of=${EMMC} bs=1m seek=34 conv=notrunc

echo "restoring warp @102760448..."
dd if=mmcblk0p5.img of=${EMMC} bs=1m seek=98 conv=notrunc

echo "restoring ro_data @639631360..."
dd if=mmcblk0p6.img of=${EMMC} bs=1m seek=610 conv=notrunc

echo "restoring public @907935744..."
dd if=mmcblk0p7.img of=${EMMC} bs=1m seek=866 conv=notrunc

echo "restoring user_dic @2250244096..."
dd if=mmcblk0p8.img of=${EMMC} bs=1m seek=2146 conv=notrunc

echo "restoring sys @3323068416..."
dd if=mmcblk0p9.img of=${EMMC} bs=1m seek=3170 conv=notrunc

echo "restoring sys2 @3327262720..."
dd if=mmcblk0p10.img of=${EMMC} bs=1m seek=3174 conv=notrunc

echo "restoring sys_info @3329359872..."
dd if=mmcblk0p11.img of=${EMMC} bs=1m seek=3176 conv=notrunc

echo "restoring user_lib @3330408448..."
dd if=mmcblk0p12.img of=${EMMC} bs=1m seek=3177 conv=notrunc

echo "restoring test_mode @3598688256..."
dd if=mmcblk0p13.img of=${EMMC} bs=1m seek=3433 conv=notrunc

echo "restoring recover_kernel @3632242688..."
dd if=mmcblk0p14.img of=${EMMC} bs=1m seek=3465 conv=notrunc

echo "restoring recover_boot @3644825600..."
dd if=mmcblk0p15.img of=${EMMC} bs=1m seek=3477 conv=notrunc

echo "restoring org_kernel @3711959040..."
dd if=mmcblk0p16.img of=${EMMC} bs=1m seek=3541 conv=notrunc

echo "restoring org_resource @3724541952..."
dd if=mmcblk0p17.img of=${EMMC} bs=1m seek=3553 conv=notrunc

echo "restoring org_boot @3730833408..."
dd if=mmcblk0p18.img of=${EMMC} bs=1m seek=3559 conv=notrunc

echo "restoring org_warp @3797966848..."
dd if=mmcblk0p19.img of=${EMMC} bs=1m seek=3623 conv=notrunc

echo "restoring org_ro_data @4335616000..."
dd if=mmcblk0p20.img of=${EMMC} bs=1m seek=4135 conv=notrunc

echo "restoring org_user_dic @4603895808..."
dd if=mmcblk0p21.img of=${EMMC} bs=1m seek=4391 conv=notrunc

echo "restoring org_user_lib @4608090112..."
dd if=mmcblk0p22.img of=${EMMC} bs=1m seek=4395 conv=notrunc

echo "restoring sync_dir @4876369920..."
dd if=mmcblk0p23.img of=${EMMC} bs=1m seek=4651 conv=notrunc

echo "restoring backup @5144649728..."
dd if=mmcblk0p24.img of=${EMMC} bs=1m seek=4907 conv=notrunc

echo "restoring auto_backup @5146746880..."
dd if=mmcblk0p25.img of=${EMMC} bs=1m seek=4909 conv=notrunc

echo "restoring trash @6489636864..."
dd if=mmcblk0p26.img of=${EMMC} bs=1m seek=6189 conv=notrunc

echo "restoring reserve @7160725504..."
dd if=mmcblk0p27.img of=${EMMC} bs=1m seek=6829 conv=notrunc
