frno7 / linux

Linux 2.2, 2.6, 3.x, 4.x and 5.x kernels for the PlayStation 2.
Other
92 stars 6 forks source link

Support aligned partition allocation (APA) #39

Open frno7 opened 3 years ago

frno7 commented 3 years ago

Aligned partition allocation (APA) is a proprietary disk partition scheme used on the PlayStation 2. There are two kinds of partitions: main and sub partitions. A main partition can have up to 64 sub partitions. Partition sizes are restricted to powers of 2, from 128 MiB to 32 GiB. There is tooling for Linux 2.x to have game data coexist with Linux on the same disk. It needs to be reviewed for Linux 5.x.

See also https://github.com/frno7/linux/issues/18#issuecomment-898936927 and ideas for a hybrid APA and GPT partition scheme.

uyjulian commented 3 years ago

PFS and APA partitions can be managed from userspace using PFS Shell. https://github.com/ps2homebrew/pfsshell

The linux partition types EXT2, ReiserFS, and swap are all the partition types implemented in the HDD driver.
I think for future expandability with other filesystems e.g. ext4 and >2TB access, targeting a APA protective header+GPT (for HDD boot only) or APA+GPT hybrid scheme (for HDD boot+APA/PFS backwards compatibility) would be the best idea.

This would require support in partitioning tools e.g. parted to detect the APA header and recalculate the checksum and protective area.

uyjulian commented 1 year ago

For reference, here is the APA partition driver as implemented by SCE: https://github.com/rickgaiser/linux-2.4.17-ps2/blob/69d1691010e92b14133758864c1433042de028cf/fs/partitions/ps2.c

frno7 commented 1 year ago

Thanks, @uyjulian! I found fs/partitions/ps2.c on the ps2-v2.6.35.14 branch, with updates made by @jur and the commit message

Partitions which are fragmented/splitted over the disc are not fully supported.
Either apascan or device mapper should be used to merge it.
Both tools use different formats. apascan is compatible with the old Linux.
bignaux commented 1 year ago

To remember i started to port https://gitlab.com/ps2max/linux/linux/-/blob/ps2-v3.8/block/partitions/ps2.c to latest kernel, i should be able to back to that soon.

mirh commented 1 year ago

See also https://github.com/ps2homebrew/Open-PS2-Loader/issues/519 https://www.psx-place.com/threads/the-future-of-the-playstation-2-internal-hard-drive.40830/

uyjulian commented 1 year ago

A problem with implementing APA in the kernel is that it may be non trivial to implement. So it may be better to have a userspace program like apascan do the mapping instead.

On PS2 Linux beta kernel 2.2.1, the offset before the main partition filesystem data start is 4096 (drivers/block/genhd.c, PAGE_SIZE=4096) bytes. On PSBBN kernel 2.4.17, the offset before the main partition filesystem data start is 4194304 (fs/partitions/ps2.h, PS2_PART_RESV_MAIN=4 1024 1024) bytes.

On both systems, the offset before the sub partition filesystem data start is 4096 (2.2.1: drivers/block/genhd.c, PAGE_SIZE=4096; 2.4.17: fs/partitions/ps2.h, PS2_PART_RESV_SUB=4 * 1024)

On both systems, the data between 0 and 4096 is the APA partition header. For patched kernel 2.4.17, the data between 4096 and 4194304 is the PS2ICON3D data.

Partitions may be fragmented. Even if the sub-partitions are together, there is still the APA header between them.

frno7 commented 1 year ago

Thanks, @mirh!

@uyjulian, this repo has both ps2-v2.2.1:drivers/block/genhd.c, and ps2-v2.6.35.14:fs/partitions/ps2.h which I believe has improvements over 2.4.17, see https://github.com/frno7/linux/issues/39#issuecomment-1351761471.

A problem with implementing APA in the kernel is that it may be non trivial to implement. So it may be better to have a userspace program like apascan do the mapping instead.

The entire block device would be available, so user space programs would work nicely regardless of APA in the kernel. The only issue is that as per https://github.com/frno7/linux/issues/18#issuecomment-1368373735 the ATA driver isn’t quite stable yet. :-)

Partitions may be fragmented. Even if the sub-partitions are together, there is still the APA header between them.

Yeah, @jur mentioned in the comment mentioned above that fragments aren’t entirely supported, but that they apparently can be merged using apascan or a device mapper.