#!/bin/bash
############################################################
echo "wait more in case of troubles"
echo "sending gen-blob in usb mode"
sleep 0.7
TOOLS/boot_usb_2421 setflag usb
sleep 0.6
TOOLS/boot_usb_2421 setflag usb
#i know... it's weird, but try without! and don't expect this working 100% of the time!! :)
#i like to be able to plug the phone and launch the script the same time.
TOOLS/boot_usb_2421 TOOLS/gen-blob 0
echo "waiting for re-enumeration"
sleep 7

#################### ATTEMPT TO DETECT PHONE'S GENERATION
######## All Gen1 have the kernel in flash offseted at 0x20000
######## All Gen2 have the kernel at 0xA0000
rm -rf header_0x0

TOOLS/boot_usb_2421 read 0 8 header_0x0 
if [ $? != 0 ]; then
	echo "boot_usb error"
	exit 0
fi
dd if=header_0x0 of=4bytes-head bs=1 count=4
echo -ne "\x87\xc0\x00\xea" > head_gen2
echo -ne "\x00\x00\x00\xea" > head_a910i

if [ "`hexdump head_gen2`" = "`hexdump 4bytes-head`" ]; then
	PHONE_GEN=2
elif [ "`hexdump head_a910i`" = "`hexdump 4bytes-head`" ]; then
	PHONE_GEN=2
else
	PHONE_GEN=1
fi
rm -rf diffa910i diffgen2 head_gen2 head_gen1 
echo "Detected GEN_$PHONE_GEN phone"
#############END GENERATION DETECTION (works here! :) )


