diff --git a/configure.py b/configure.py index 0c190911..96e2980a 100644 --- a/configure.py +++ b/configure.py @@ -1854,7 +1854,7 @@ config.libs = [ Object(Matching, "fa/kernel/pfk_api.c"), Object(Matching, "fa/driver/nand_drv.c"), - Object(NonMatching, "fa/driver/sd_drv.c"), + Object(Matching, "fa/driver/sd_drv.c"), Object(Matching, "fa/driver/pfd_cmn.c"), Object(Matching, "fa/driver/msc_drv.c"), diff --git a/libs/RVL_SDK/src/fa/driver/sd_drv.c b/libs/RVL_SDK/src/fa/driver/sd_drv.c index 53fe28ed..ef95040b 100644 --- a/libs/RVL_SDK/src/fa/driver/sd_drv.c +++ b/libs/RVL_SDK/src/fa/driver/sd_drv.c @@ -29,13 +29,13 @@ typedef struct PFD_SDDRV_FORMAT_DATA { u32 total_sectors; } PFD_SDDRV_FORMAT_DATA; -PFD_SDDRV_INFO g_pfd_sddrv_info; -PFD_SDDEV_STORAGE g_pfd_sddev; +volatile PFD_SDDRV_INFO g_pfd_sddrv_info; +PFD_SDDEV_STORAGE g_pfd_sddev ATTRIBUTE_ALIGN(32); u8 g_pfd_sddrv_buf[0x200]; static FAInsertCallback g_attach_func = 0; static FAEjectCallback g_detach_func = 0; -u32 g_event; +u32 g_event ATTRIBUTE_ALIGN(32); extern u8 pfd_get_media_drv_char(FADisk*, s8*, u32); extern void pdm_disk_notify_media_insert(FADisk*); @@ -243,39 +243,23 @@ static inline void clear_mount_flag(void) { g_pfd_sddrv_info.flags &= ~2; } -static inline u32 pfd_sddrv_flags(const PFD_SDDRV_INFO* info) { - return info->flags; -} - -static inline SDDev* pfd_sddrv_device(const PFD_SDDRV_INFO* info) { - return info->device; -} - -static inline FADisk* pfd_sddrv_disk(const PFD_SDDRV_INFO* info) { - return info->disk; -} - -static inline s8 pfd_sddrv_drive(const PFD_SDDRV_INFO* info) { - return info->drive; -} - s32 pfd_st_inter_callback(s32 status, void* data) { s32 result; if ((status & 1) != 1) { return 0; } - if (pfd_sddrv_device(&g_pfd_sddrv_info) != 0) { + if (g_pfd_sddrv_info.device != 0) { g_event = 2; result = ISD_RegisterDeviceIntrHandler(g_pfd_sddrv_info.device, (SDDevIntrCallback)pfd_st_removal_callback, &g_event); if (result != 0) { OSReport("ERR:Failed to regist intr handler. pfd_st_inter_callback()\n"); } } - if (pfd_sddrv_disk(&g_pfd_sddrv_info) != 0) { + if (g_pfd_sddrv_info.disk != 0) { update_media_drive(); pdm_disk_notify_media_insert(g_pfd_sddrv_info.disk); - if (pfd_sddrv_drive(&g_pfd_sddrv_info) != 0 && g_attach_func != 0) { + if (g_pfd_sddrv_info.drive != 0 && g_attach_func != 0) { g_attach_func(g_pfd_sddrv_info.drive); } } @@ -291,36 +275,39 @@ s32 pfd_st_removal_callback(s32 status, void* data) { return 0; } g_pfd_sddrv_info.media_inserted = 0; - if (g_pfd_sddrv_info.media_inserted == 0 && pfd_sddrv_device(&g_pfd_sddrv_info) != 0) { + if (g_pfd_sddrv_info.device != 0) { g_event = 1; result = ISD_RegisterDeviceIntrHandler(g_pfd_sddrv_info.device, (SDDevIntrCallback)pfd_st_inter_callback, &g_event); if (result != 0) { OSReport("ERR:Failed to regist intr handler. pfd_st_removal_callback()\n"); } } - if (pfd_sddrv_disk(&g_pfd_sddrv_info) != 0) { + if (g_pfd_sddrv_info.disk != 0) { update_media_drive(); pdm_disk_notify_media_eject(g_pfd_sddrv_info.disk); - if (pfd_sddrv_drive(&g_pfd_sddrv_info) != 0 && g_detach_func != 0) { + if (g_pfd_sddrv_info.drive != 0 && g_detach_func != 0) { g_detach_func(g_pfd_sddrv_info.drive); } } return 0; } +#pragma push +#pragma ppc_iro_level 0 s32 pfd_sddrv_init(FADisk* disk) { s32 sd_result; SDDev* device; u32 status; - if (disk == 0) { + if (disk == NULL) { return -30; } - if ((pfd_sddrv_flags(&g_pfd_sddrv_info) & 1) != 0) { + if ((g_pfd_sddrv_info.flags & 1) != 0) { OSReport("INFO SD Card driver is already initialize. pfd_sddrv_init()\n"); if (disk != g_pfd_sddrv_info.disk) { return -44; } + return 0; } else { if ((g_pfd_sddrv_info.flags & 4) == 0) { g_pfd_sddrv_info.bytes_per_sector = 0x200; @@ -372,6 +359,7 @@ s32 pfd_sddrv_init(FADisk* disk) { } return 0; } +#pragma pop s32 pfd_sddrv_mount(FADisk* disk) { s32 result; @@ -473,7 +461,7 @@ s32 pfd_sddrv_unmount(FADisk* disk) { if (disk == 0) { return -30; } - if ((pfd_sddrv_flags(&g_pfd_sddrv_info) & 2) != 0) { + if ((g_pfd_sddrv_info.flags & 2) != 0) { clear_mount_flag(); } return 0; @@ -485,7 +473,7 @@ s32 pfd_sddrv_finalize(FADisk* disk) { if (disk == 0) { return -30; } - if ((pfd_sddrv_flags(&g_pfd_sddrv_info) & 2) != 0) { + if ((g_pfd_sddrv_info.flags & 2) != 0) { clear_mount_flag(); } if ((g_pfd_sddrv_info.flags & 1) != 0) { @@ -1371,22 +1359,24 @@ s32 pfd_sddrv_store_fat32_bpb_buf(PFD_SDDRV_FORMAT_DATA* format_data, u8* sector static s32 pfd_sddrv_store_fat32_reserved_buf(u8* sector_buffer) { PFD_SDDRV_RESERVED_BOOT_SECTOR* reserved_boot_sector; - s32 result = -30; - if (sector_buffer != 0) { - pf_memset(sector_buffer, 0, 0x200); - reserved_boot_sector = (PFD_SDDRV_RESERVED_BOOT_SECTOR*)sector_buffer; - if (((u32)reserved_boot_sector->signature.bytes & 1) != 0) { - reserved_boot_sector->signature.bytes[0] = 0x55; - reserved_boot_sector->signature.bytes[1] = 0xaa; - } else { - reserved_boot_sector->signature.value = 0x55aa; - } - result = 0; + pf_memset(sector_buffer, 0, 0x200); + reserved_boot_sector = (PFD_SDDRV_RESERVED_BOOT_SECTOR*)sector_buffer; + if (((u32)reserved_boot_sector->signature.bytes & 1) != 0) { + reserved_boot_sector->signature.bytes[0] = 0x55; + reserved_boot_sector->signature.bytes[1] = 0xaa; + } else { + reserved_boot_sector->signature.value = 0x55aa; } - return result; + return 0; } +static inline s32 pfd_sddrv_write_sector(u32 sector) { + return ISD_WriteBlock(g_pfd_sddrv_info.device, sector, g_pfd_sddrv_buf, 1); +} + +#pragma push +#pragma ppc_iro_level 0 s32 pfd_sddrv_build_fat32_mbr_bpb(u32 total_sectors) { PFD_SDDRV_FORMAT_DATA format_data; s32 result; @@ -1408,12 +1398,12 @@ s32 pfd_sddrv_build_fat32_mbr_bpb(u32 total_sectors) { if (g_pfd_sddrv_info.media_ejected != 0) { return -33; } - result = ISD_WriteBlock(g_pfd_sddrv_info.device, format_data.partition_start_sector, g_pfd_sddrv_buf, 1); + result = pfd_sddrv_write_sector(format_data.partition_start_sector); if (result != 0) { OSReport("ERR Failed to write BPB fields. ISD_WriteBlock()\n"); return -36; } - result = ISD_WriteBlock(g_pfd_sddrv_info.device, format_data.partition_start_sector + 6, g_pfd_sddrv_buf, 1); + result = pfd_sddrv_write_sector(format_data.partition_start_sector + 6); if (result != 0) { OSReport("ERR Failed to write BPB fields. ISD_WriteBlock()\n"); return -36; @@ -1429,12 +1419,12 @@ s32 pfd_sddrv_build_fat32_mbr_bpb(u32 total_sectors) { if (g_pfd_sddrv_info.media_ejected != 0) { return -33; } - result = ISD_WriteBlock(g_pfd_sddrv_info.device, format_data.partition_start_sector + 1, g_pfd_sddrv_buf, 1); + result = pfd_sddrv_write_sector(format_data.partition_start_sector + 1); if (result != 0) { OSReport("ERR Failed to write BPB fields. ISD_WriteBlock()\n"); return -36; } - result = ISD_WriteBlock(g_pfd_sddrv_info.device, format_data.partition_start_sector + 7, g_pfd_sddrv_buf, 1); + result = pfd_sddrv_write_sector(format_data.partition_start_sector + 7); if (result != 0) { OSReport("ERR Failed to write BPB fields. ISD_WriteBlock()\n"); return -36; @@ -1451,12 +1441,12 @@ s32 pfd_sddrv_build_fat32_mbr_bpb(u32 total_sectors) { if (g_pfd_sddrv_info.media_ejected != 0) { return -33; } - result = ISD_WriteBlock(g_pfd_sddrv_info.device, format_data.partition_start_sector + 2, g_pfd_sddrv_buf, 1); + result = pfd_sddrv_write_sector(format_data.partition_start_sector + 2); if (result != 0) { OSReport("ERR Failed to write BPB fields. ISD_WriteBlock()\n"); return -36; } - result = ISD_WriteBlock(g_pfd_sddrv_info.device, format_data.partition_start_sector + 8, g_pfd_sddrv_buf, 1); + result = pfd_sddrv_write_sector(format_data.partition_start_sector + 8); if (result != 0) { OSReport("ERR Failed to write BPB fields. ISD_WriteBlock()\n"); return -36; @@ -1472,13 +1462,14 @@ s32 pfd_sddrv_build_fat32_mbr_bpb(u32 total_sectors) { if (g_pfd_sddrv_info.media_ejected != 0) { return -33; } - result = ISD_WriteBlock(g_pfd_sddrv_info.device, 0, g_pfd_sddrv_buf, 1); + result = pfd_sddrv_write_sector(0); if (result != 0) { OSReport("ERR Failed to write MBR fields. ISD_WriteBlock()\n"); return -36; } return 0; } +#pragma pop s32 pfd_sddrv_full_format(void) { FADiskInfo geometry; diff --git a/tools/decomp-assist/opus-fa.attempts.md b/tools/decomp-assist/opus-fa.attempts.md new file mode 100644 index 00000000..3b860cb0 --- /dev/null +++ b/tools/decomp-assist/opus-fa.attempts.md @@ -0,0 +1,33 @@ +# opus-fa attempts + +## libs/RVL_SDK/src/fa/driver/sd_drv (23/26 -> 26/26, linked) + +### pfd_sddrv_finalize (4 -> 0) +- Evidence for volatile: target reloads `flags` straight after testing it (`lwz; rlwinm.; beq; lwz` with no store in between) and keeps the final state stores in source order. The previous rounds faked the reloads with const-pointer accessors. +- Fix: `volatile PFD_SDDRV_INFO g_pfd_sddrv_info;` (definition-level, lever 19: the struct is written from the SD insert/remove interrupt callbacks). The fake accessors `pfd_sddrv_flags/device/disk/drive` are removed and the fields read directly. The removal callback loses its `media_inserted == 0 &&` re-check, which only existed to force a reload. Every other function stays exact. + +### pfd_sddrv_init (142 -> 0) +- Explicit `return 0;` in the already-initialized branch: 142 -> 26. MWCC then turned `disk != info.disk ? -44 : 0` into a branchless select. The target branches instead. +- if/else/eq/goto spellings of the same return: still branchless (26). +- `#pragma ppc_iro_level 0` around the function (lever 26): 26 -> 1. Only the null test was left: target `li r0,0; cmplw r3,r0`. +- `disk == NULL` (NULL is `((void*)0)`) under IRO 0: 1 -> 0. Without IRO 0, NULL gives cmpwi. +- Diagnostic only: optimization_level 1/2/3 and iro 1/2 did not help. + +### pfd_sddrv_build_fat32_mbr_bpb (122 -> 0) +- Target leaves an unreachable `bl OSReport` without its argument after the inlined reserved-sector writer, and has no null check of the buffer. A mini test shows this exact remnant comes from an inline helper with no null check that returns 0, compiled under IRO 0. +- The reserved-sector helper is now written like its sibling `pfd_sddrv_store_fat32_fsi_buf` (no null check, `return 0`). The only caller passes `g_pfd_sddrv_buf`, so behaviour is unchanged. With IRO 0 around build_fat32: 122 -> 2. +- Last 2: the first write loads the sector before the device. A local `partition_start_sector` like the FAT16 builder: 14. `+ 0`, casts: 2. Fix: `static inline s32 pfd_sddrv_write_sector(u32 sector)` wrapping `ISD_WriteBlock(device, sector, g_pfd_sddrv_buf, 1)`, so the argument is evaluated before the device load: 0. + +### Link +- Flipped to Matching. The first DOL had `addi r27/r28` swapped in physical_read/write. The cause was `ATTRIBUTE_ALIGN(32)` on `g_pfd_sddrv_buf`, which changes codegen, so it was dropped. +- Without a 32-aligned .bss object, every `g_pfd_sddrv_info` access moved by 0x10. The fix is `ATTRIBUTE_ALIGN(32)` on `g_pfd_sddev` (the IPC device storage) and on `g_event` (the IPC event word, which the target places at .sbss+0x20). +- DOL SHA1 26116613f624061ba99c8d1a299aaa6efa85670d OK. + +## libs/RVL_SDK/src/fa/pdm_partition: pdm_part_is_master_boot_sector (start 84 in repo / 21 in opus-vf best -> best 19, not exact) +Remaining difference: the byte-load schedule of the inlined LE32 read. The target builds the tree (b2<<16 + b0) + (b3<<24 + b1<<8) and loads b3, b2, b0, b1. Ours loads b3, b2, b1, b0. +- mwdbg: our pre-RA scheduler already emits b1 before b0, so b1 has a higher priority. In the target, b1 shares b3's register, which proves its load comes after the b3 shift in the pre-RA order. So the target DAG or the initial order differs, and it is not a register tie. +- Mini-file study (/tmp/opus-fa/perm*.py): MWCC reassociates a 4-term add chain into pairs (L0+L2),(L1+L3) in source operand order. No permutation or parenthesization of the 4 terms gives the target pairing together with the target load order. All 120 tried in the real function: best 19 (e.g. `b1<<8 + b0 + b2<<16 + b3<<24`). Saved as opus-fa.best.pdm_partition.c. +- Unrolled byte loops (+=, |=, Horner): 43-69. Loads before the zero store: 73 (the zero store is DCE'd). Reusing a local for start/count: 21. Removing the zero store: 73. Count read first: 88. +- buf indexing instead of `buf += 16`: 90-91. Partition-entry pointer: 69. +- Helper boundaries (local for b0, u8/u32 parameter helper, two u16 halves, pointer to the field): 20-21. +- Pragmas (diagnostic): iro 0/1: 37 (25 with pointer loop 2, best 23 over all 120 sum shapes). scheduling off: 77. IRO is not the lever here.