#!/bin/bash
clear

C_RED_BLD='\033[1;31m' C_YLW_BLD='\033[1;93m' C_GRN_BLD='\033[1;32m' C_BLU_BLD='\033[1;94m' C_CYN_BLD='\033[1;36m' C_WHT_BLD='\033[1;37m'	C_GRY_BLD='\033[1;30m'
C_RED='\033[0;91m'	  C_YLW='\033[0;93m'		 C_GRN='\033[0;92m'		C_BLU='\033[0;94m'	  C_CYN='\033[0;36m'		 C_WHT='\033[0;37m'		C_GRY='\033[0;30m'
C_BLANK='\033[0m'      C_FLASH='\033[1;5m'

TOOLKIT_PATH="$(pwd)" # "${HOME}/ps4"

function function_continue() {
	echo
	read -p "Press enter key to continue. "
	echo
	}

function function_exit() {
	echo
	read -p "Press enter key to exit script. "
	exit
	}

function function_exit_2() {
	read -p "You didn't chose propper option... Press enter key to exit script. "
	exit
	}

function function_probe_loop() {
	if [[ "$(echo ${DEVICE} | grep -o /dev/mapper/loop)" = "/dev/mapper/loop" ]]; then
		DEVICE_CUT=$(echo ${DEVICE} | cut -c 17)
		DEVICE="/dev/loop${DEVICE_CUT}"
	fi
	echo
	}

function function_decryption_internal() {
 # checking if partitions are sorted (1-15) or aren't (1..31), by checking if partition no.31 exist
	if [[ -e ${DEVICE}31 ]]; then
		sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_SAMU}			-s 256 ${IV_01} ps4hdd_01 ${DEVICE}1	&> /dev/null	# preinst				/dev/da0x0
		sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_SAMU}			-s 256 ${IV_03} ps4hdd_03 ${DEVICE}3	&> /dev/null	# preinst2				/dev/da0x1
		sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_KERNEL}		-s 256			 ps4hdd_05 ${DEVICE}5	&> /dev/null	# eap_kern (no IV?)	/dev/da0x2
		sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_EAP}			-s 256 ${IV_07} ps4hdd_07 ${DEVICE}7						# eap_vsh				/dev/da0x3
		sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_SAMU}			-s 256 ${IV_09} ps4hdd_09 ${DEVICE}9	&> /dev/null	# system					/dev/da0x4
		sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_SAMU}			-s 256 ${IV_10} ps4hdd_10 ${DEVICE}10	&> /dev/null	# system_b				/dev/da0x4b
		sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_SAMU}			-s 256 ${IV_11} ps4hdd_11 ${DEVICE}11	&> /dev/null	# system_ex				/dev/da0x5
		sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_SAMU}			-s 256 ${IV_12} ps4hdd_12 ${DEVICE}12	&> /dev/null	# system_ex_b			/dev/da0x5b
		sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_EAP}			-s 256			 ps4hdd_13 ${DEVICE}13						# swap (no IV used)	/dev/da0x6,da0x6x0,da0x6x1,da0x6x2 
		sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_SAMU}			-s 256 ${IV_17} ps4hdd_17 ${DEVICE}17	&> /dev/null	# app_tmp				/dev/da0x8
		sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_SAMU}			-s 256 ${IV_19} ps4hdd_19 ${DEVICE}19	&> /dev/null	# system_data			/dev/da0x9
		sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_EAP}			-s 256 ${IV_25} ps4hdd_25 ${DEVICE}25						# update					/dev/da0x12
		sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_EAP}			-s 256 ${IV_27} ps4hdd_27 ${DEVICE}27						# user					/dev/da0x13
		sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_EAP}			-s 256 ${IV_29} ps4hdd_29 ${DEVICE}29						# eap_user				/dev/da0x14
		sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_SAMU}			-s 256 ${IV_31} ps4hdd_31 ${DEVICE}31	&> /dev/null	# app_reserved			/dev/da0x15
		else
			sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_SAMU}		-s 256 ${IV_01} ps4hdd_01 ${DEVICE}1	&> /dev/null
			sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_SAMU}		-s 256 ${IV_03} ps4hdd_03 ${DEVICE}2	&> /dev/null
			sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_KERNEL}	-s 256			 ps4hdd_05 ${DEVICE}3	&> /dev/null
			sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_EAP}		-s 256 ${IV_07} ps4hdd_07 ${DEVICE}4
			sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_SAMU}		-s 256 ${IV_09} ps4hdd_09 ${DEVICE}5	&> /dev/null
			sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_SAMU}		-s 256 ${IV_10} ps4hdd_10 ${DEVICE}6	&> /dev/null
			sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_SAMU}		-s 256 ${IV_11} ps4hdd_11 ${DEVICE}7	&> /dev/null
			sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_SAMU}		-s 256 ${IV_12} ps4hdd_12 ${DEVICE}8	&> /dev/null
			sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_EAP}		-s 256			 ps4hdd_13 ${DEVICE}9
			sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_SAMU}		-s 256 ${IV_17} ps4hdd_17 ${DEVICE}10	&> /dev/null
			sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_SAMU}		-s 256 ${IV_19} ps4hdd_19 ${DEVICE}11	&> /dev/null
			sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_EAP}		-s 256 ${IV_25} ps4hdd_25 ${DEVICE}12
			sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_EAP}		-s 256 ${IV_27} ps4hdd_27 ${DEVICE}13
			sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_EAP}		-s 256 ${IV_29} ps4hdd_29 ${DEVICE}14
			sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_SAMU}		-s 256 ${IV_31} ps4hdd_31 ${DEVICE}15	&> /dev/null
	fi
	}

# ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

echo -e "${C_GRN_BLD}PS4 HDD Mounter v2.2 by Berion"
echo -e "${C_YLW}Script allows to mount all HDD partitions."
echo
echo -e "${C_RED_BLD}Confirm root permissions every time you will be asked for!${C_BLANK}"
echo

# ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

echo -e "${C_CYN_BLD}STEP 1: Storage Type${C_BLANK}"
echo
echo -e " ${C_WHT_BLD}1.${C_BLANK} ${C_YLW_BLD}Internal${C_BLANK} disk"
echo -e " ${C_WHT_BLD}2.${C_BLANK} ${C_YLW_BLD}External${C_BLANK} disk"
echo

while :; do
read MENU
echo
case $MENU in

	1) # Internal Storage
		echo -e "${C_CYN_BLD}STEP 2: Checking Environment${C_BLANK}"
		echo

		KEY_EAP="eap_hdd_key.bin"
		KEY_KERNEL="eap_kernel_key.bin"
		KEY_SAMU="samu_hdd_key.bin"

		cd ${TOOLKIT_PATH}/keys &> /dev/null

		if [[ -f "${KEY_EAP}" ]]; then
			echo -e "${C_GRN}Found ${C_GRN_BLD}EAP HDD Key${C_GRN}."
			echo -e "You can decrypt user partitions.${C_BLANK}"
			else
				echo -e "${C_RED}Missing ${C_RED_BLD}EAP HDD Key${C_RED} (\"${TOOLKIT_PATH}/keys/${KEY_EAP}\")."
				echo -e "Key is needed for user partitions decryption.${C_BLANK}"
		fi
		echo
		if [[ -f "${KEY_KERNEL}" ]]; then
			echo -e "${C_GRN}Found ${C_GRN_BLD}EAP Kernel Key${C_GRN}."
			echo -e "You can decrypt kernel partition.${C_BLANK}"
			else
				echo -e "${C_RED}Missing ${C_RED_BLD}EAP Kernel Key${C_RED} (\"${TOOLKIT_PATH}/keys/${KEY_KERNEL}\")."
				echo -e "Key is needed for kernel partition decryption.${C_BLANK}"
		fi
		echo
		if [[ -f "${KEY_SAMU}" ]]; then
			echo -e "${C_GRN}Found ${C_GRN_BLD}SAMU HDD Key${C_GRN}."
			echo -e "You can decrypt system partitions.${C_BLANK}"
			else
				echo -e "${C_RED}Missing ${C_RED_BLD}SAMU HDD Key${C_RED} (\"${TOOLKIT_PATH}/keys/${KEY_SAMU}\")"
				echo -e "Key is needed for system partitions decryption.${C_BLANK}"
		fi
		function_continue

		# //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

		echo -e "${C_CYN_BLD}STEP 3: Devices${C_BLANK}"
		echo
		echo -e "Choose your ${C_YLW_BLD}disk from PS4${C_BLANK} (type e.g. /dev/sdx) from below device list:${C_BLANK}"
		echo
		lsblk -p -o MODEL,NAME,SIZE,LABEL,MOUNTPOINT
		echo
		read DEVICE
		echo -e "OK, so you've chosen: \"${C_YLW_BLD}${DEVICE}${C_BLANK}\"."
		if [[ "$(echo ${DEVICE} | grep -o /dev/loop)" = "/dev/loop" ]]; then
			echo
			sudo kpartx -a ${DEVICE}
			DEVICE_CUT=$(echo ${DEVICE} | cut -c 10-12)
			DEVICE="/dev/mapper/loop${DEVICE_CUT}p"
		fi
		function_continue

		# //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

		echo -e "${C_CYN_BLD}STEP 4: Mounting${C_BLANK}"
		echo
		echo -e "${C_WHT_BLD}Choose WISELY${C_BLANK}:"
		echo -e " ${C_WHT_BLD}A.${C_BLANK} ${C_RED}Do ${C_RED_BLD}NOT${C_RED} use ${C_RED_BLD}IV${C_BLANK} (CUH-10xx and some CUH-11xx)."
		echo -e " ${C_WHT_BLD}B.${C_BLANK} ${C_YLW}Use ${C_YLW_BLD}IV${C_BLANK}        (some CUH-11xx, CUH-2xxx, CUH-7xxx)."
		echo

		while :; do
		read MENU_SUB
		echo
		case $MENU_SUB in
			A | a)
				IV_01=""
				IV_03=""
				IV_05=""
				IV_07=""
				IV_09=""
				IV_10=""
				IV_11=""
				IV_12=""
				IV_17=""
				IV_19=""
				IV_25=""
				IV_27=""
				IV_29=""
				IV_31=""
				break;;

			B | b)
			 # IV=(n-1)<<32
			 # checking if partitions are sorted (1-15) or aren't (1..31), by checking if partition no.31 exist
				if [[ -e ${DEVICE}31 ]]; then
					IV_01="-p 4294967296"		# p1		? 1<<32 or 0
					IV_03="-p 8589934592"		# p3
					IV_05="-p 17179869184"		# p5		?
					IV_07="-p 25769803776"		# p7
					IV_09="-p 34359738368"		# p9
					IV_10="-p 38654705664"		# p10
					IV_11="-p 42949672960"		# p11
					IV_12="-p 47244640256"		# p12
					IV_17="-p 68719476736"		# p17
					IV_19="-p 77309411328"		# p19
					IV_25="-p 103079215104"		# p25
					IV_27="-p 111669149696"		# p27
					IV_29="-p 120259084288"		# p29
					IV_31="-p 128849018880"		# p31
					else
						IV_01="-p 4294967296"	# p1		? 1<<32 or 0
						IV_03="-p 4294967296"	# p2		? 2-1<<32
						IV_05="-p 8589934592"	# p3		?
						IV_07="-p 12884901888"	# p4
						IV_09="-p 17179869184"	# p5
						IV_10="-p 21474836480"	# p6
						IV_11="-p 25769803776"	# p7
						IV_12="-p 30064771072"	# p8
						IV_17="-p 38654705664"	# p10
						IV_19="-p 42949672960"	# p11
						IV_25="-p 47244640256"	# p12
						IV_27="-p 51539607552"	# p13
						IV_29="-p 55834574848"	# p14
						IV_31="-p 60129542144"	# p15
				fi
				break;;

			*)
				function_exit_2
				break;;
		esac
		done

		echo -e "${C_WHT_BLD}What to do after disk decryption?${C_BLANK}"
		echo -e " ${C_WHT_BLD}A.${C_BLANK} Do not mount       (for disks with ${C_GRN_BLD}healthy ${C_GRN}GPT${C_BLANK})."
		echo -e " ${C_WHT_BLD}B.${C_BLANK} Mount as Read Only (for disks with ${C_GRN_BLD}healthy ${C_GRN}GPT${C_BLANK})."
		echo -e " ${C_WHT_BLD}C.${C_BLANK} Mount as Read Only (for disks with ${C_RED_BLD}broken ${C_RED}GPT${C_BLANK})."
		echo -e " ${C_WHT_BLD}D.${C_BLANK} Mount as Writable  (for disks with ${C_GRN_BLD}healthy ${C_GRN}GPT${C_BLANK}). ${C_RED_BLD}${C_FLASH}[[EXPERIMENTAL]]${C_BLANK}"
		echo

		while :; do
		read MENU_SUB2
		echo
		case $MENU_SUB2 in
			A | a) # decryption only
				function_decryption_internal
				function_probe_loop
				lsblk -p -o MODEL,NAME,SIZE,LABEL,FSTYPE,MOUNTPOINT -b ${DEVICE}
				break;;

			B | b) # RO
				function_decryption_internal

				sudo mount -t msdos -o ro					/dev/mapper/ps4hdd_01 ${TOOLKIT_PATH}/storage/hdd/preinst			&> /dev/null # FAT32						?
				sudo mount -t msdos -o ro					/dev/mapper/ps4hdd_03 ${TOOLKIT_PATH}/storage/hdd/preinst2			&> /dev/null # FAT32						?
				sudo mount -t msdos -o ro					/dev/mapper/ps4hdd_07 ${TOOLKIT_PATH}/storage/hdd/eap_vsh							 # FAT16
				sudo mount          -o ro					/dev/mapper/ps4hdd_09 ${TOOLKIT_PATH}/storage/hdd/system				&> /dev/null # FAT32/exFAT/TexFAT	?
				sudo mount          -o ro					/dev/mapper/ps4hdd_10 ${TOOLKIT_PATH}/storage/hdd/system_b			&> /dev/null # FAT32/exFAT/TexFAT	?
				sudo mount          -o ro					/dev/mapper/ps4hdd_11 ${TOOLKIT_PATH}/storage/hdd/system_ex			&> /dev/null # FAT32/exFAT/TexFAT	?
				sudo mount          -o ro					/dev/mapper/ps4hdd_12 ${TOOLKIT_PATH}/storage/hdd/system_ex_b		&> /dev/null # FAT32/exFAT/TexFAT	?
				sudo mount -t ufs   -o ro,ufstype=ufs2	/dev/mapper/ps4hdd_17 ${TOOLKIT_PATH}/storage/hdd/app_tmp			&> /dev/null # UFS2/FFS					?
				sudo mount -t ufs   -o ro,ufstype=ufs2	/dev/mapper/ps4hdd_19 ${TOOLKIT_PATH}/storage/hdd/system_data		&> /dev/null # UFS2/FFS					?
				sudo mount -t msdos -o ro					/dev/mapper/ps4hdd_25 ${TOOLKIT_PATH}/storage/hdd/update								 # FAT32
				sudo mount -t ufs   -o ro,ufstype=ufs2 /dev/mapper/ps4hdd_27 ${TOOLKIT_PATH}/storage/hdd/user								 # UFS2/FFS
				sudo mount -t ufs   -o ro,ufstype=ufs2 /dev/mapper/ps4hdd_29 ${TOOLKIT_PATH}/storage/hdd/eap_user							 # UFS2/FFS
				sudo mount -t ufs   -o ro,ufstype=ufs2 /dev/mapper/ps4hdd_31 ${TOOLKIT_PATH}/storage/hdd/app_reserved		&> /dev/null # UFS2/FFS					?

				function_probe_loop
				lsblk -p -o MODEL,NAME,SIZE,FSTYPE,MOUNTPOINT -b ${DEVICE}
				break;;

			C | c) # RO, missing or broken GPT
				echo -e "${C_WHT_BLD}Choose sector of ${C_YLW_BLD}user${C_WHT_BLD} partition:${C_BLANK}"
				echo -e "${C_WHT_BLD} A.${C_BLANK} user defined LBA"
				echo -e "${C_WHT_BLD} B.${C_BLANK} LBA ${C_YLW_BLD}35653632${C_BLANK}"
				echo
				while :; do
				read MENU_SUB3
				echo
				case $MENU_SUB3 in
					A | a)
						echo -e "Type sector number:"
						read OFFSET_CUSTOM
						echo
						OFFSET_USER=$(echo "$((${OFFSET_CUSTOM}*512))")
						break;;

					B | b)
						OFFSET_USER=$(echo "$((35653632*512))")
					 # 0x440100000 (18254659584 bytes)
						break;;

					*)
					function_exit_2
					break;;
				esac
				done

				function_probe_loop
				sudo losetup /dev/loop27 --offset=${OFFSET_USER} -r ${DEVICE}
				sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_EAP} -s 256 ${IV_27} ps4hdd_27 /dev/loop27
				sudo mount -t ufs -o ro,ufstype=ufs2 /dev/mapper/ps4hdd_27 ${TOOLKIT_PATH}/storage/hdd/user

				echo
				echo -e "\"${C_YLW}/dev/loop27${C_BLANK}\":"
				sudo hexdump -C /dev/loop27 | head -8
				echo
				echo -e "\"${C_YLW}/dev/mapper/ps4hdd_27${C_BLANK}\":"
				sudo hexdump -C /dev/mapper/ps4hdd_27 | head -8
				echo
				lsblk -p -o MODEL,NAME,SIZE,LABEL,FSTYPE,MOUNTPOINT -b /dev/loop27
				break;;

			D | d) # RW
				cd ${TOOLKIT_PATH}/apps &> /dev/null
				if [[ -f "ufs.ko" ]]; then
					chmod +x ufs.ko
					sudo insmod ufs.ko
					else echo -e "${C_RED_BLD}ERROR:${C_BLANK} I couldn't find custom \"${C_WHT_BLD}ufs.ko${C_BLANK}\"."
				fi
				function_continue
				function_decryption_internal

				sudo mount -t msdos							/dev/mapper/ps4hdd_01 ${TOOLKIT_PATH}/storage/hdd/preinst				&> /dev/null # FAT32						?
				sudo mount -t msdos							/dev/mapper/ps4hdd_03 ${TOOLKIT_PATH}/storage/hdd/preinst2				&> /dev/null # FAT32						?
				sudo mount -t msdos							/dev/mapper/ps4hdd_07 ${TOOLKIT_PATH}/storage/hdd/eap_vsh								 # FAT16
				sudo mount 										/dev/mapper/ps4hdd_09 ${TOOLKIT_PATH}/storage/hdd/system					&> /dev/null # FAT32/exFAT/TexFAT	?
				sudo mount         							/dev/mapper/ps4hdd_10 ${TOOLKIT_PATH}/storage/hdd/system_b				&> /dev/null # FAT32/exFAT/TexFAT	?
				sudo mount       								/dev/mapper/ps4hdd_11 ${TOOLKIT_PATH}/storage/hdd/system_ex				&> /dev/null # FAT32/exFAT/TexFAT	?
				sudo mount    									/dev/mapper/ps4hdd_12 ${TOOLKIT_PATH}/storage/hdd/system_ex_b			&> /dev/null # FAT32/exFAT/TexFAT	?
				sudo mount -t ufs   -o ro,ufstype=ufs2	/dev/mapper/ps4hdd_17 ${TOOLKIT_PATH}/storage/hdd/app_tmp				&> /dev/null # UFS2/FFS					?
				sudo mount -t ufs   -o ro,ufstype=ufs2	/dev/mapper/ps4hdd_19 ${TOOLKIT_PATH}/storage/hdd/system_data			&> /dev/null # UFS2/FFS					?
				sudo mount -t msdos							/dev/mapper/ps4hdd_25 ${TOOLKIT_PATH}/storage/hdd/update									 # FAT32
				sudo mount -t ufs   -o ro,ufstype=ufs2 /dev/mapper/ps4hdd_27 ${TOOLKIT_PATH}/storage/hdd/user									 # UFS2/FFS
				sudo mount -t ufs   -o ro,ufstype=ufs2 /dev/mapper/ps4hdd_29 ${TOOLKIT_PATH}/storage/hdd/eap_user								 # UFS2/FFS
				sudo mount -t ufs   -o ro,ufstype=ufs2 /dev/mapper/ps4hdd_31 ${TOOLKIT_PATH}/storage/hdd/app_reserved			&> /dev/null # UFS2/FFS					?

				sudo mount -t ufs -o remount,rw,ufstype=ufs2 /dev/mapper/ps4hdd_17 ${TOOLKIT_PATH}/storage/hdd/app_tmp		&> /dev/null
				sudo mount -t ufs -o remount,rw,ufstype=ufs2 /dev/mapper/ps4hdd_19 ${TOOLKIT_PATH}/storage/hdd/system_data	&> /dev/null
				sudo mount -t ufs -o remount,rw,ufstype=ufs2 /dev/mapper/ps4hdd_27 ${TOOLKIT_PATH}/storage/hdd/user			&> /dev/null
				sudo mount -t ufs -o remount,rw,ufstype=ufs2 /dev/mapper/ps4hdd_29 ${TOOLKIT_PATH}/storage/hdd/eap_user		&> /dev/null
				sudo mount -t ufs -o remount,rw,ufstype=ufs2 /dev/mapper/ps4hdd_31 ${TOOLKIT_PATH}/storage/hdd/app_reserved	&> /dev/null

				function_probe_loop
				lsblk -p -o MODEL,NAME,SIZE,FSTYPE,MOUNTPOINT -b ${DEVICE}
				break;;

		*)
			function_exit_2
			break;;
		esac
		done
		break;;

	2) # External Storage
		echo -e "${C_CYN_BLD}STEP 2: Checking Environment${C_BLANK}"
		echo

		KEY_ES="es_hdd_key.bin"

		cd ${TOOLKIT_PATH}/keys &> /dev/null

		if [[ -f "${KEY_ES}" ]]; then
			echo -e "${C_GRN}Found ${C_GRN_BLD}ES HDD Key${C_GRN}."
			echo -e "You can decrypt External Storage disk.${C_BLANK}"
			else
				echo -e "${C_RED}Missing ${C_RED_BLD}ES HDD Key${C_RED} (\"${TOOLKIT_PATH}/keys/${KEY_ES}\")"
				echo -e "Key is needed for External Storage decryption."
				function_exit
		fi
		function_continue

		# //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

		echo -e "Choose your ${C_YLW_BLD}external disk from PS4${C_BLANK} (type eg: ${C_YLW}/dev/sdx${C_BLANK}) from below device list:"
		echo
		lsblk -p -o MODEL,NAME,SIZE,LABEL,MOUNTPOINT
		echo
		read DEVICE
		echo -e "Ok, so You've chose: \"${C_YLW_BLD}${DEVICE}${C_BLANK}\""
		function_continue

		# //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

		sudo cryptsetup -r create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_ES} -s 256 ps4hdd_es ${DEVICE}
		lsblk -p -o MODEL,NAME,SIZE,LABEL,FSTYPE,MOUNTPOINT ${DEVICE}
		echo
		sudo hexdump -s 20480 -C /dev/mapper/ps4hdd_es | head -3
		echo
		sudo hexdump -s 65536 -C /dev/mapper/ps4hdd_es | head -16

	 # currently mounting not possible due to encryption (decrypted only UFS2 fs table, fs data is encrypted differently)
	 # sudo mount -t ufs -o ro,ufstype=? /dev/mapper/ps4hdd_es ${TOOLKIT_PATH}/storage/hdd/ext0

		break;;

	*)
		function_exit_2
		break;;
esac
done

# ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

unset DEVICE
unset DEVICE_CUT
unset IV_01
unset IV_03
unset IV_05
unset IV_07
unset IV_09
unset IV_10
unset IV_11
unset IV_12
unset IV_17
unset IV_19
unset IV_25
unset IV_27
unset IV_29
unset IV_31
unset KEY_EAP
unset KEY_ES
unset KEY_KERNEL
unset KEY_SAMU
unset MENU
unset MENU_SUB
unset MENU_SUB2
unset MENU_SUB3
unset OFFSET_CUSTOM
unset OFFSET_USER
unset TOOLKIT_PATH

echo -e ${C_WHT_BLD}
read -p "All done. Press enter key to exit. "
