
How to fix bad petitboot on PC
-------------------------------

# connect your PS3 HDD to your PC, e.g. with SATA-USB adapter

HDD_DEV=/dev/sdh

modprobe dm_mod
insmod dm-bswap16

hdd_size=`blockdev --getsize $HDD_DEV`

# we have to setup device mapper bswap16 target
# else HDD encryption/decryption won't work properly

echo "0 $hdd_size bswap16 $HDD_DEV" | dmsetup create hdd

# create key file

echo <your data key as hex string> <your tweak key as hex string> | xxd -r -p > hdd_key.bin

# create DM crypto target
# key size is 256bit because PS3 uses XTS-AES-128 and
# the key is just the concatenation of the data and tweak keys.

cryptsetup create -c aes-xts-plain64 -d ./hdd_key.bin -s 256 hdd_crypt /dev/mapper/hdd

# create device mapper partitions with kpartx

kpartx-ps3 -a /dev/mapper/hdd_crypt

# cell_ext_os_area starts at offset 0xe740000 on VFLASH

# first dump os area parameters
# it begins at offset 0xe740200

dd if=/dev/mapper/hdd_crypt1 of=params.bin bs=1 count=512 skip=$((0xe740200))

# now clear the boot flag
# just make the first 4 bytes in params.bin all 0s

dd if=/dev/zero of=params.bin bs=1 count=4 conv=notrunc

# now we write it back

dd of=/dev/mapper/hdd_crypt1 if=params.bin bs=1 count=512 seek=$((0xe740200))

sync

# clean up everything before disconnecting PS3 HDD

kpartx-ps3 -d /dev/mapper/hdd_crypt
dmsetup remove hdd_crypt
dmsetup remove hdd

# now GameOS should boot and you can flash a new petitboot :)
