#!/bin/bash

# Unattended PS4 HDD Mounter v1.4 by Berion
# Only for advanced and/or lazy people! ;)

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

DEVICE="/dev/sdx" # replace /dev/sdx* by your device (/dev/sda*, /dev/loop0p*, /dev/mmcblk0p*, /dev/nvme0p* etc.)
TYPE="NoIV"       # IV/NoIV
DISK="RO"         # RO/RW

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

if [[ $TYPE == "NoIV" ]]; then
	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=""
	else
		IV_01="-p 4294967296"
		IV_03="-p 8589934592"
		IV_05="-p 17179869184"
		IV_07="-p 25769803776"
		IV_09="-p 34359738368"
		IV_10="-p 38654705664"
		IV_11="-p 42949672960"
		IV_12="-p 47244640256"
		IV_17="-p 68719476736"
		IV_19="-p 77309411328"
		IV_25="-p 103079215104"
		IV_27="-p 111669149696"
		IV_29="-p 120259084288"
		IV_31="-p 128849018880"
fi

sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_SAMU}   -s 256 ${IV_01} ps4hdd_01 ${DEVICE}1  # preinst
sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_SAMU}   -s 256 ${IV_03} ps4hdd_03 ${DEVICE}3  # preinst2
sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_KERNEL} -s 256          ps4hdd_05 ${DEVICE}5  # eap_kern
sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_EAP}    -s 256 ${IV_07} ps4hdd_07 ${DEVICE}7  # eap_vsh
sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_SAMU}   -s 256 ${IV_09} ps4hdd_09 ${DEVICE}9  # system
sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_SAMU}   -s 256 ${IV_10} ps4hdd_10 ${DEVICE}10 # system_b
sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_SAMU}   -s 256 ${IV_11} ps4hdd_11 ${DEVICE}11 # system_ex
sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_SAMU}   -s 256 ${IV_12} ps4hdd_12 ${DEVICE}12 # system_ex_b
sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_EAP}    -s 256          ps4hdd_13 ${DEVICE}13 # swap
sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_SAMU}   -s 256 ${IV_17} ps4hdd_17 ${DEVICE}17 # app_tmp
sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_SAMU}   -s 256 ${IV_19} ps4hdd_19 ${DEVICE}19 # system_data
sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_EAP}    -s 256 ${IV_25} ps4hdd_25 ${DEVICE}25 # update
sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_EAP}    -s 256 ${IV_27} ps4hdd_27 ${DEVICE}27 # user
sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_EAP}    -s 256 ${IV_29} ps4hdd_29 ${DEVICE}29 # eap_user
sudo cryptsetup -q create -c aes-xts-plain64 -d ${TOOLKIT_PATH}/keys/${KEY_SAMU}   -s 256 ${IV_31} ps4hdd_31 ${DEVICE}31 # app_reserved

if [[ $DISK == "RO" ]]; then
	FLAG_FAT="-o ro"
	FLAG_UFS="-o ro,ufstype=ufs2"
	else
		FLAG_FAT=""
		FLAG_UFS="-o ro,ufstype=ufs2"
		FLAG_UFS_R="-o remount,rw,ufstype=ufs2"
		sudo insmod ${TOOLKIT_PATH}/apps/ufs.ko
fi

sudo mount -t msdos ${FLAG_FAT} /dev/mapper/ps4hdd_01 ${TOOLKIT_PATH}/storage/hdd/preinst
sudo mount -t msdos ${FLAG_FAT} /dev/mapper/ps4hdd_03 ${TOOLKIT_PATH}/storage/hdd/preinst2
sudo mount -t msdos ${FLAG_FAT} /dev/mapper/ps4hdd_07 ${TOOLKIT_PATH}/storage/hdd/eap_vsh
sudo mount          ${FLAG_FAT} /dev/mapper/ps4hdd_09 ${TOOLKIT_PATH}/storage/hdd/system
sudo mount          ${FLAG_FAT} /dev/mapper/ps4hdd_10 ${TOOLKIT_PATH}/storage/hdd/system_b
sudo mount          ${FLAG_FAT} /dev/mapper/ps4hdd_11 ${TOOLKIT_PATH}/storage/hdd/system_ex
sudo mount          ${FLAG_FAT} /dev/mapper/ps4hdd_12 ${TOOLKIT_PATH}/storage/hdd/system_ex_b
sudo mount -t ufs   ${FLAG_UFS} /dev/mapper/ps4hdd_17 ${TOOLKIT_PATH}/storage/hdd/app_tmp
sudo mount -t ufs   ${FLAG_UFS} /dev/mapper/ps4hdd_19 ${TOOLKIT_PATH}/storage/hdd/system_data
sudo mount -t msdos ${FLAG_FAT} /dev/mapper/ps4hdd_25 ${TOOLKIT_PATH}/storage/hdd/update
sudo mount -t ufs   ${FLAG_UFS} /dev/mapper/ps4hdd_27 ${TOOLKIT_PATH}/storage/hdd/user
sudo mount -t ufs   ${FLAG_UFS} /dev/mapper/ps4hdd_29 ${TOOLKIT_PATH}/storage/hdd/eap_user
sudo mount -t ufs   ${FLAG_UFS} /dev/mapper/ps4hdd_31 ${TOOLKIT_PATH}/storage/hdd/app_reserved

if [[ $DISK == "RW" ]]; then
	sudo mount -t ufs ${FLAG_UFS_R} /dev/mapper/ps4hdd_17 ${TOOLKIT_PATH}/storage/hdd/app_tmp
	sudo mount -t ufs ${FLAG_UFS_R} /dev/mapper/ps4hdd_19 ${TOOLKIT_PATH}/storage/hdd/system_data
	sudo mount -t ufs ${FLAG_UFS_R} /dev/mapper/ps4hdd_27 ${TOOLKIT_PATH}/storage/hdd/user
	sudo mount -t ufs ${FLAG_UFS_R} /dev/mapper/ps4hdd_29 ${TOOLKIT_PATH}/storage/hdd/eap_user
	sudo mount -t ufs ${FLAG_UFS_R} /dev/mapper/ps4hdd_31 ${TOOLKIT_PATH}/storage/hdd/app_reserved
fi

# echo && read -p "Press enter key to exit. "
