Advertisement
Sweetening

ps3_hdd_mod.sh

Jul 20th, 2024 (edited)
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.89 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Constants
  4. PS3RAM_DEV="/dev/ps3ram"
  5. PS3HVC_DEV="/dev/ps3hvc"
  6. PS3HVC_HVCALL="ps3hvc_hvcall"
  7. LPAR_ID=1
  8. LAID=0x1070000002000001
  9. HDD_OBJ_FIRST_REG_OBJ_OFFSET=0xb0
  10. REG_OBJ_ACL_TABLE_OFFSET=0x58
  11. NUM_ACL_ENTRIES=8
  12. ACL_ENTRY_SIZE=24
  13. REG_OBJ_SIZE=$((REG_OBJ_ACL_TABLE_OFFSET + NUM_ACL_ENTRIES * ACL_ENTRY_SIZE))
  14.  
  15. # Functions
  16. ram_read_val_8() {
  17.     local _off=$1
  18.     local _val
  19.     _val=$(dd if="$PS3RAM_DEV" bs=1 count=1 skip="$_off" 2>/dev/null | hexdump -v -e '1/1 "%02x"')
  20.     printf "0x%02x" "$_val"
  21. }
  22.  
  23. ram_read_val_64() {
  24.     local _off=$1
  25.     local _val
  26.     _val=$(dd if="$PS3RAM_DEV" bs=1 count=8 skip="$_off" 2>/dev/null | hexdump -v -e '1/1 "%02x"')
  27.     printf "0x%016x" "$_val"
  28. }
  29.  
  30. ram_write_val_64() {
  31.     local _off=$1
  32.     local _val=$2
  33.     printf "$_val" | dd of="$PS3RAM_DEV" bs=1 count=8 seek="$_off" 2>/dev/null
  34. }
  35.  
  36. hdd_reg_is_valid() {
  37.     local _hdd_obj_off=$1
  38.     local _reg_idx=$2
  39.     local _val
  40.     _val=$(ram_read_val_8 $((_hdd_obj_off + HDD_OBJ_FIRST_REG_OBJ_OFFSET + _reg_idx * REG_OBJ_SIZE + 0x50)))
  41.     echo "$_val"
  42. }
  43.  
  44. hdd_reg_acl_get_laid() {
  45.     local _hdd_obj_off=$1
  46.     local _reg_idx=$2
  47.     local _acl_idx=$3
  48.     local _val
  49.     _val=$(ram_read_val_64 $((_hdd_obj_off + HDD_OBJ_FIRST_REG_OBJ_OFFSET + _reg_idx * REG_OBJ_SIZE + REG_OBJ_ACL_TABLE_OFFSET + _acl_idx * ACL_ENTRY_SIZE)))
  50.     echo "$_val"
  51. }
  52.  
  53. hdd_reg_acl_get_access_rights() {
  54.     local _hdd_obj_off=$1
  55.     local _reg_idx=$2
  56.     local _acl_idx=$3
  57.     local _val
  58.     _val=$(ram_read_val_64 $((_hdd_obj_off + HDD_OBJ_FIRST_REG_OBJ_OFFSET + _reg_idx * REG_OBJ_SIZE + REG_OBJ_ACL_TABLE_OFFSET + _acl_idx * ACL_ENTRY_SIZE + 8)))
  59.     echo "$_val"
  60. }
  61.  
  62. hdd_reg_acl_set_access_rights() {
  63.     local _hdd_obj_off=$1
  64.     local _reg_idx=$2
  65.     local _acl_idx=$3
  66.     local _val=$4
  67.     ram_write_val_64 $((_hdd_obj_off + HDD_OBJ_FIRST_REG_OBJ_OFFSET + _reg_idx * REG_OBJ_SIZE + REG_OBJ_ACL_TABLE_OFFSET + _acl_idx * ACL_ENTRY_SIZE + 8)) "$_val"
  68. }
  69.  
  70. # Usage menu
  71. usage() {
  72.     echo "Usage: $0 <print|patch|restore>"
  73.     echo
  74.     echo "Commands:"
  75.     echo "  print     Print the ACL entries of the disk storage device"
  76.     echo "  patch     Patch the ACL entries of the disk storage device"
  77.     echo "  restore   Restore the ACL entries of the disk storage device"
  78.     exit 1
  79. }
  80.  
  81. # Main script
  82. if [ $# -ne 1 ]; then
  83.     usage
  84. fi
  85.  
  86. case $1 in
  87.     print|patch|restore) ;;
  88.     *) usage ;;
  89. esac
  90.  
  91. CMD=$1
  92.  
  93. fw_ver=$($PS3HVC_HVCALL "$PS3HVC_DEV" get_version_info)
  94.  
  95. # Set STORAGE_SYS_OFFSET based on firmware version
  96. case $fw_ver in
  97.     0x0000000300040001) STORAGE_SYS_OFFSET=0x348300 ;;
  98.     0x0000000300050000) STORAGE_SYS_OFFSET=0x348350 ;;
  99.     0x0000000300050005) STORAGE_SYS_OFFSET=0x34b3b8 ;;
  100.     *) echo "Unsupported firmware version $fw_ver" >&2; exit 1 ;;
  101. esac
  102.  
  103. STORAGE_SYS_DEV_TABLE_OFFSET=0xee8
  104.  
  105. # Check for VFLASH
  106. flag=$($PS3HVC_HVCALL "$PS3HVC_DEV" get_repo_node_val "$LPAR_ID" "0x0000000073797300" "0x666c617368000000" "0x6578740000000000" 0 | awk '{ printf $1 }')
  107.  
  108. if [ "$flag" = "0x00000000000000fe" ]; then
  109.     # VFLASH on
  110.     HDD_REG_LIST="2 3"
  111. else
  112.     # VFLASH off
  113.     HDD_REG_LIST="1 2"
  114. fi
  115.  
  116. # Get number of storage devices
  117. num_dev=$($PS3HVC_HVCALL "$PS3HVC_DEV" get_repo_node_val "$LPAR_ID" "0x0000000062757304" "0x6e756d5f64657600" 0 0 | awk '{ printf $1 }')
  118. num_dev=$(printf "%d" "$num_dev")
  119.  
  120. echo "Number of storage devices: $num_dev"
  121.  
  122. # Get index of disk storage device
  123. echo "Searching for disk storage device ..."
  124.  
  125. dev_idx=0
  126. found=0
  127. while [ $dev_idx -lt $num_dev -a $found -eq 0 ]; do
  128.     rnv_dev="$(expr substr 0x6465760000000000 1 17)${dev_idx}"
  129.  
  130.     type=$($PS3HVC_HVCALL "$PS3HVC_DEV" get_repo_node_val "$LPAR_ID" "0x0000000062757304" "$rnv_dev" "0x7479706500000000" 0 | awk '{ printf $1 }')
  131.     if [ "$type" = "0x0000000000000000" ]; then
  132.         found=1
  133.     else
  134.         dev_idx=$(expr $dev_idx + 1)
  135.     fi
  136. done
  137.  
  138. if [ $found -eq 0 ]; then
  139.     echo "Disk storage device was not found"
  140.     exit 1
  141. fi
  142.  
  143. echo "Found disk storage device"
  144. echo "Device index: $dev_idx"
  145.  
  146. # Get ID of disk storage device
  147. dev_id=$($PS3HVC_HVCALL "$PS3HVC_DEV" get_repo_node_val "$LPAR_ID" "0x0000000062757304" "$rnv_dev" "0x6964000000000000" 0 | awk '{ printf $1 }')
  148.  
  149. echo "Device ID: $dev_id"
  150.  
  151. # Get number of regions on disk storage device
  152. n_regs=$($PS3HVC_HVCALL "$PS3HVC_DEV" get_repo_node_val "$LPAR_ID" "0x0000000062757304" "$rnv_dev" "0x6e5f726567730000" 0 | awk '{ printf $1 }')
  153. n_regs=$(printf "%d" "$n_regs")
  154.  
  155. echo "Number of regions: $n_regs"
  156.  
  157. # Get RAM address of disk storage device object
  158. val=$(ram_read_val_64 "$STORAGE_SYS_OFFSET")
  159. val=$(ram_read_val "$val")
  160. hdd_obj_offset=$(ram_read_val_64 $val)
  161. hdd_obj_offset=$(ram_read_val_64 $((val + $STORAGE_SYS_DEV_TABLE_OFFSET + 8 * dev_id)))
  162.  
  163. printf "Disk storage device object is at address 0x%x\n" "$hdd_obj_offset"
  164.  
  165. # Print/patch/restore region ACL entries of disk storage device
  166. for reg_idx in $HDD_REG_LIST; do
  167.     valid=$(hdd_reg_is_valid "$hdd_obj_offset" "$reg_idx")
  168.     if [ "$valid" = "0x01" ]; then
  169.         echo "Region $reg_idx"
  170.  
  171.         for acl_idx in {0..7}; do
  172.             laid=$(hdd_reg_acl_get_laid "$hdd_obj_offset" "$reg_idx" "$acl_idx")
  173.             access_rights=$(hdd_reg_acl_get_access_rights "$hdd_obj_offset" "$reg_idx" "$acl_idx")
  174.  
  175.             if [ "$laid" = "$LAID" ]; then
  176.                 echo "Found GameOS ACL entry index $acl_idx"
  177.  
  178.                 if [ "$CMD" = "print" ]; then
  179.                     echo "$laid $access_rights"
  180.                 elif [ "$CMD" = "patch" ]; then
  181.                     echo "Patching ..."
  182.                     hdd_reg_acl_set_access_rights "$hdd_obj_offset" "$reg_idx" "$acl_idx" '\x00\x00\x00\x00\x00\x00\x00\x02'
  183.                 else
  184.                     echo "Restoring ..."
  185.                     hdd_reg_acl_set_access_rights "$hdd_obj_offset" "$reg_idx" "$acl_idx" '\x00\x00\x00\x00\x00\x00\x00\x03'
  186.                 fi
  187.             fi
  188.         done
  189.     fi
  190. done
  191.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement