diff --git a/README.md b/README.md index ef68be15..df19e99a 100644 --- a/README.md +++ b/README.md @@ -53,9 +53,9 @@ Progress | Decompiled | Matched | Linked | Data | |:---:|:---:|:---:|:---:| -| 94.06% | 82.26% | 58.19% | 89.35% | +| 94.28% | 82.29% | 58.19% | 89.44% | -units 777/1027 complete, functions 11927/12563 matched. +units 777/1027 complete, functions 11931/12563 matched. decompiled = code with a C/C++ implementation (objdiff fuzzy), matched = byte-exact code, linked = code actually linked into the DOL, data = byte-exact data diff --git a/libs/RVL_SDK/src/fa/driver/sd_drv.c b/libs/RVL_SDK/src/fa/driver/sd_drv.c index 9292606a..2e7cb84a 100644 --- a/libs/RVL_SDK/src/fa/driver/sd_drv.c +++ b/libs/RVL_SDK/src/fa/driver/sd_drv.c @@ -135,23 +135,35 @@ typedef union PFD_SDDRV_U16_BYTES { u8 bytes[2]; } PFD_SDDRV_U16_BYTES; -static inline void pfd_sddrv_store_le16(u8* field, u16 value) { - if (((u32)field & 1) != 0) { - field[0] = (u8)value; - field[1] = (u8)(value >> 8); - } else { - *(u16*)field = (u16)((value >> 8) | (value << 8)); - } -} +#define pfd_sddrv_store_le16(field, value) \ + do { \ + if (((u32)(field) & 1) != 0) { \ + (field)[0] = (u8)(value); \ + (field)[1] = (u8)((value) >> 8); \ + } else { \ + *(u16*)(field) = (((value) & 0xff) << 8) | (((value) & 0xff00) >> 8); \ + } \ + } while (0) -static inline void pfd_sddrv_store_le32(u8* field, u32 value) { - if (((u32)field & 3) != 0) { - field[0] = (u8)value; - field[1] = (u8)(value >> 8); - field[2] = (u8)(value >> 16); - field[3] = (u8)(value >> 24); - } else { - *(u32*)field = (value << 24) | ((value & 0xff00) << 8) | ((value >> 8) & 0xff00) | (value >> 24); +#define pfd_sddrv_store_le32(field, value) \ + do { \ + if (((u32)(field) & 3) != 0) { \ + (field)[0] = (u8)(value); \ + (field)[1] = (u8)((value) >> 8); \ + (field)[2] = (u8)((value) >> 16); \ + (field)[3] = (u8)((value) >> 24); \ + } else { \ + *(u32*)(field) = (((value) & 0xff) << 24 | ((value) & 0xff00) << 8) | \ + (((value) & 0xff000000) >> 24 | ((value) & 0xff0000) >> 8); \ + } \ + } while (0) + +static inline void pfd_sddrv_copy_bytes(u8* destination, const u8* source, u32 size) { + u32 index; + if (destination != 0 && source != 0) { + for (index = 0; index < size; index++) { + destination[index] = source[index]; + } } } @@ -456,7 +468,7 @@ s32 pfd_sddrv_finalize(FADisk* disk) { if (disk == 0) { return -30; } - if ((g_pfd_sddrv_info.flags & 2) != 0) { + if ((g_pfd_sddrv_info.flags & 2) == 2) { clear_mount_flag(); } if ((g_pfd_sddrv_info.flags & 1) != 0) { @@ -742,109 +754,108 @@ s32 pfd_sddrv_calc_mbr_bpb(PFD_SDDRV_FORMAT_DATA* format_data) { u32 fat_entry_bits; u32 fat_sectors; u32 next_fat_sectors; - u64 total_sectors; - u64 reserved_sectors; - u64 fixed_sectors; - u64 start_sector; - u64 current_start; - u64 clusters; - u64 cluster_count_plus_one; - u64 candidate_fat_sectors; + u32 total_sectors; + u8 sectors_per_cluster; + u32 reserved_sectors; + u32 fixed_sectors; + u32 start_sector; + u32 current_start; + u32 clusters; + u32 candidate_fat_sectors; u32 reserve_count; - int moved_start; + int moved_start = 0; + s32 result; OSCalendarTime current_time; if (format_data == 0) { return -30; } - pf_memset(&settings, 0, 0x10); + pf_memset(&settings, 0, sizeof(settings)); size_entry = sddrv_size_depend_tbl; - for (entry_index = 0; entry_index < 14; entry_index++, size_entry++) { - if (size_entry->min_sectors < format_data->total_sectors && format_data->total_sectors <= size_entry->max_sectors) { - settings.reserved_sectors = size_entry->reserved_sectors; - settings.fat_copies = size_entry->fat_copies; - settings.root_entries = size_entry->root_entries; - settings.sectors_per_cluster = size_entry->sectors_per_cluster; + for (entry_index = 0; entry_index < 14; size_entry++, entry_index++) { + if (size_entry->min_sectors < format_data->total_sectors && size_entry->max_sectors >= format_data->total_sectors) { + settings.reserved_sectors = sddrv_size_depend_tbl[entry_index].reserved_sectors; + settings.fat_copies = sddrv_size_depend_tbl[entry_index].fat_copies; + settings.root_entries = sddrv_size_depend_tbl[entry_index].root_entries; + settings.sectors_per_cluster = sddrv_size_depend_tbl[entry_index].sectors_per_cluster; break; } } - if (entry_index == 14) { + result = entry_index == 14 ? -30 : 0; + if (result != 0) { OSReport("ERR Failed to get values with total sectors. pfd_sddrv_get_value_with_total_sectors()\n"); - return -30; + return result; } - format_data->sectors_per_cluster = settings.sectors_per_cluster; + sectors_per_cluster = settings.sectors_per_cluster; + format_data->sectors_per_cluster = sectors_per_cluster; total_sectors = format_data->total_sectors; - clusters = total_sectors / settings.sectors_per_cluster; - if (total_sectors % settings.sectors_per_cluster != 0) { - clusters++; - } + clusters = total_sectors / sectors_per_cluster; if (clusters < 0x1005) { fat_entry_bits = 12; - } else { - if (clusters > 0xfff4) { - return -31; - } + } else if (clusters < 0xfff5) { fat_entry_bits = 16; + } else { + return -31; + } + if (total_sectors % sectors_per_cluster != 0) { + clusters++; } candidate_fat_sectors = clusters * fat_entry_bits; - fat_sectors = (u32)(candidate_fat_sectors >> 12); + fat_sectors = candidate_fat_sectors >> 12; if ((candidate_fat_sectors & 0xfff) != 0) { fat_sectors++; } reserved_sectors = settings.reserved_sectors; for (;;) { - u64 previous_fat_sectors = fat_sectors; - fixed_sectors = previous_fat_sectors * 2 + 0x21; + fixed_sectors = fat_sectors * 2 + 0x21; reserve_count = 1; current_start = reserved_sectors; - do { + for (;;) { start_sector = current_start - fixed_sectors; - if ((s64)start_sector >= 1) { + if ((s32)start_sector > 0) { break; } - reserve_count++; current_start += reserved_sectors; - } while (1); - current_start = (u64)reserve_count * reserved_sectors; - start_sector = current_start - fixed_sectors; - moved_start = 0; + reserve_count++; + } + current_start = reserve_count * reserved_sectors; for (;;) { - for (;;) { - if (!moved_start && reserved_sectors != (u32)start_sector) { - start_sector += reserved_sectors; - } - moved_start = 0; - clusters = ((total_sectors - start_sector) - fixed_sectors) / settings.sectors_per_cluster; - cluster_count_plus_one = clusters + 1; - if (cluster_count_plus_one >= 0xfe5 && cluster_count_plus_one <= 0x1004) { - break; - } + if (moved_start == 0 && reserved_sectors != start_sector) { + start_sector += reserved_sectors; + } + moved_start = 0; + clusters = (total_sectors - start_sector - fixed_sectors) / sectors_per_cluster + 1; + if (clusters >= 0xfe5 && clusters < 0x1005) { current_start += reserved_sectors; start_sector = current_start - fixed_sectors; + continue; } - candidate_fat_sectors = clusters * fat_entry_bits + 2; - next_fat_sectors = (u32)(candidate_fat_sectors >> 12); + candidate_fat_sectors = (clusters - 1) * fat_entry_bits + 2; + next_fat_sectors = candidate_fat_sectors >> 12; if ((candidate_fat_sectors & 0xfff) != 0) { next_fat_sectors++; } - if (next_fat_sectors <= previous_fat_sectors) { + if (next_fat_sectors <= fat_sectors) { break; } start_sector += reserved_sectors; moved_start = 1; } - fat_sectors = next_fat_sectors; - if (fat_sectors == previous_fat_sectors) { + if (next_fat_sectors == fat_sectors) { break; } + fat_sectors = next_fat_sectors; } format_data->sectors_per_fat = fat_sectors; - format_data->partition_start_sector = (u32)start_sector; - format_data->partition_sector_count = format_data->total_sectors - (u32)start_sector; - if (((format_data->partition_sector_count - (fat_sectors * 2 + 0x21)) / format_data->sectors_per_cluster + 1) < 0xff5) { + format_data->partition_start_sector = start_sector; + format_data->partition_sector_count = format_data->total_sectors - start_sector; + clusters = (format_data->partition_sector_count - (fat_sectors * 2 + 0x21)) / format_data->sectors_per_cluster + 1; + if (clusters < 0xff5) { format_data->fat_type = 0; - } else { + } else if (clusters >= 0xff5) { format_data->fat_type = 1; + } else { + return -31; } OSTicksToCalendarTime(OSGetTime(), ¤t_time); format_data->volume_serial_number = ((((current_time.year - 1900) & 0x7f) << 9) | @@ -856,48 +867,56 @@ s32 pfd_sddrv_calc_mbr_bpb(PFD_SDDRV_FORMAT_DATA* format_data) { return 0; } + s32 pfd_sddrv_store_bpb_buf(PFD_SDDRV_FORMAT_DATA* format_data, u8* sector_buffer) { const PFD_SDDRV_SIZE_DEPEND* size_entry; PFD_SDDRV_SIZE_SETTINGS settings; PFD_SDDRV_BPB* boot_sector; u32 entry_index; - u32 sectors_16; + u16 sectors_16; + s32 result; u32 sectors_32; - if (format_data == 0 || sector_buffer == 0) { + if (format_data == 0) { return -30; } - size_entry = sddrv_size_depend_tbl; pf_memset(&settings, 0, 0x10); - for (entry_index = 0; entry_index < 14; entry_index++, size_entry++) { - if (size_entry->min_sectors < format_data->total_sectors && format_data->total_sectors <= size_entry->max_sectors) { - settings.reserved_sectors = size_entry->reserved_sectors; - settings.fat_copies = size_entry->fat_copies; - settings.root_entries = size_entry->root_entries; - settings.sectors_per_cluster = size_entry->sectors_per_cluster; + size_entry = sddrv_size_depend_tbl; + for (entry_index = 0; entry_index < 14; size_entry++, entry_index++) { + if (size_entry->min_sectors < format_data->total_sectors && size_entry->max_sectors >= format_data->total_sectors) { + settings.reserved_sectors = sddrv_size_depend_tbl[entry_index].reserved_sectors; + settings.fat_copies = sddrv_size_depend_tbl[entry_index].fat_copies; + settings.root_entries = sddrv_size_depend_tbl[entry_index].root_entries; + settings.sectors_per_cluster = sddrv_size_depend_tbl[entry_index].sectors_per_cluster; break; } } - if (entry_index == 14) { + result = entry_index == 14 ? -30 : 0; + if (result != 0) { OSReport("ERR Failed to get values with total sectors. pfd_sddrv_get_value_with_total_sectors()\n"); - return -30; + return result; } - if (format_data->partition_sector_count == 0) { - return -30; - } - if (format_data->partition_sector_count <= 0xffff) { - sectors_16 = format_data->partition_sector_count; + sectors_32 = format_data->partition_sector_count; + if (sectors_32 != 0 && sectors_32 < 0x10000) { + sectors_16 = sectors_32; sectors_32 = 0; - } else { + } else if (sectors_32 >= 0x10000) { sectors_16 = 0; - sectors_32 = format_data->partition_sector_count; + } else { + return -30; } pf_memset(sector_buffer, 0, 0x200); boot_sector = (PFD_SDDRV_BPB*)sector_buffer; + pfd_sddrv_copy_bytes(boot_sector->oem_name, (const u8*)" ", 8); + pfd_sddrv_copy_bytes(boot_sector->volume_label, (const u8*)"NO NAME ", 11); + if (format_data->fat_type == 0) { + pfd_sddrv_copy_bytes(boot_sector->file_system_type, (const u8*)"FAT12 ", 8); + } else { + pfd_sddrv_copy_bytes(boot_sector->file_system_type, (const u8*)"FAT16 ", 8); + } boot_sector->jump[0] = 0xeb; boot_sector->jump[1] = 0; boot_sector->jump[2] = 0x90; - pf_memcpy(boot_sector->oem_name, " ", 8); pfd_sddrv_store_le16(boot_sector->bytes_per_sector, 0x200); boot_sector->sectors_per_cluster = format_data->sectors_per_cluster; pfd_sddrv_store_le16(boot_sector->reserved_sector_count, 1); @@ -913,13 +932,8 @@ s32 pfd_sddrv_store_bpb_buf(PFD_SDDRV_FORMAT_DATA* format_data, u8* sector_buffe boot_sector->drive_number = 0x80; boot_sector->extended_signature = 0x29; pfd_sddrv_store_le32(boot_sector->volume_serial_number, format_data->volume_serial_number); - pf_memcpy(boot_sector->volume_label, "NO NAME ", 11); - if (format_data->fat_type == 0) { - pf_memcpy(boot_sector->file_system_type, "FAT12 ", 8); - } else { - pf_memcpy(boot_sector->file_system_type, "FAT16 ", 8); - } - pfd_sddrv_store_le16(boot_sector->signature, 0xaa55); + boot_sector->signature[0] = 0x55; + boot_sector->signature[1] = 0xaa; return 0; } @@ -929,51 +943,63 @@ s32 pfd_sddrv_store_mbr_buf(PFD_SDDRV_FORMAT_DATA* format_data, u8* sector_buffe PFD_SDDRV_MBR* master_boot_record; PFD_SDDRV_PARTITION_ENTRY* partition; u32 entry_index; + s32 result; + u8 start_head; + u8 end_head; + u8 partition_type; u32 sectors_per_cylinder; - u32 start_cylinder; - u32 end_cylinder; - u32 start_sector; - u32 end_sector; + u16 start_cylinder; + u16 end_cylinder; + u16 start_sector; + u16 end_sector; - if (format_data == 0 || sector_buffer == 0) { + if (format_data == 0) { return -30; } - size_entry = sddrv_size_depend_tbl; pf_memset(&settings, 0, 0x10); - for (entry_index = 0; entry_index < 14; entry_index++, size_entry++) { - if (size_entry->min_sectors < format_data->total_sectors && format_data->total_sectors <= size_entry->max_sectors) { - settings.reserved_sectors = size_entry->reserved_sectors; - settings.fat_copies = size_entry->fat_copies; - settings.root_entries = size_entry->root_entries; - settings.sectors_per_cluster = size_entry->sectors_per_cluster; + size_entry = sddrv_size_depend_tbl; + for (entry_index = 0; entry_index < 14; size_entry++, entry_index++) { + if (size_entry->min_sectors < format_data->total_sectors && size_entry->max_sectors >= format_data->total_sectors) { + settings.reserved_sectors = sddrv_size_depend_tbl[entry_index].reserved_sectors; + settings.fat_copies = sddrv_size_depend_tbl[entry_index].fat_copies; + settings.root_entries = sddrv_size_depend_tbl[entry_index].root_entries; + settings.sectors_per_cluster = sddrv_size_depend_tbl[entry_index].sectors_per_cluster; break; } } - if (entry_index == 14 || format_data->partition_sector_count == 0) { - return -30; + result = entry_index == 14 ? -30 : 0; + if (result != 0) { + OSReport("ERR Failed to get values with total sectors. pfd_sddrv_get_value_with_total_sectors()\n"); + return result; } sectors_per_cylinder = settings.fat_copies * settings.root_entries; start_cylinder = format_data->partition_start_sector / sectors_per_cylinder; end_cylinder = (format_data->total_sectors - 1) / sectors_per_cylinder; start_sector = format_data->partition_start_sector % settings.root_entries + 1; end_sector = (format_data->total_sectors - 1) % settings.root_entries + 1; + start_head = (format_data->partition_start_sector % sectors_per_cylinder) / settings.root_entries; + end_head = ((format_data->total_sectors - 1) % sectors_per_cylinder) / settings.root_entries; + if (format_data->partition_sector_count == 0) { + return -30; + } + if (format_data->partition_sector_count != 0 && format_data->partition_sector_count < 0x7fa8) { + partition_type = 1; + } else if (format_data->partition_sector_count >= 0x7fa8 && format_data->partition_sector_count < 0x10000) { + partition_type = 4; + } else { + partition_type = 6; + } pf_memset(sector_buffer, 0, 0x200); master_boot_record = (PFD_SDDRV_MBR*)sector_buffer; partition = &master_boot_record->partitions[0]; partition->boot_indicator = 0; - partition->start_head = (format_data->partition_start_sector / settings.root_entries) % settings.fat_copies; - partition->start_sector_cylinder[0] = (u8)((start_sector & 0x3f) | ((start_cylinder >> 2) & 0xc0)); - partition->start_sector_cylinder[1] = (u8)start_cylinder; - if (format_data->partition_sector_count < 0x7fa8) { - partition->partition_type = 1; - } else if (format_data->partition_sector_count > 0xffff) { - partition->partition_type = 6; - } else { - partition->partition_type = 4; - } - partition->end_head = ((format_data->total_sectors - 1) / settings.root_entries) % settings.fat_copies; - partition->end_sector_cylinder[0] = (u8)((end_sector & 0x3f) | ((end_cylinder >> 2) & 0xc0)); - partition->end_sector_cylinder[1] = (u8)end_cylinder; + partition->start_head = start_head; + pfd_sddrv_store_le16(partition->start_sector_cylinder, + ((start_cylinder & 0xff) << 8) + ((start_sector & 0x3f) | ((start_cylinder & 0x300) >> 2))); + partition->partition_type = partition_type; + partition->end_head = end_head; + pfd_sddrv_store_le16(partition->end_sector_cylinder, + ((end_cylinder & 0xff) << 8) + ((end_sector & 0x3f) | ((end_cylinder & 0x300) >> 2))); pfd_sddrv_store_le32(partition->first_sector, format_data->partition_start_sector); pfd_sddrv_store_le32(partition->sector_count, format_data->partition_sector_count); master_boot_record->signature[0] = 0x55; @@ -1055,7 +1081,7 @@ s32 pfd_sddrv_calc_fat32_mbr_bpb(PFD_SDDRV_FORMAT_DATA* format_data) { pf_memset(&settings, 0, 0x10); total_sectors = format_data->total_sectors; size_entry = sddrv_size_depend_tbl; - for (entry_index = 0; entry_index < 14; entry_index++, size_entry++) { + for (entry_index = 0; entry_index < 14; size_entry++, entry_index++) { if (size_entry->min_sectors >= total_sectors) { continue; } @@ -1075,9 +1101,10 @@ s32 pfd_sddrv_calc_fat32_mbr_bpb(PFD_SDDRV_FORMAT_DATA* format_data) { } sectors_per_cluster = settings.sectors_per_cluster; format_data->sectors_per_cluster = sectors_per_cluster; + total_sectors = format_data->total_sectors; cluster_count = total_sectors / sectors_per_cluster; fat_sectors = (cluster_count >> 7) & 0xfffff; - if ((cluster_count & 0x7f) != 0) { + if ((cluster_count * 32 & 0xfe0) != 0) { fat_sectors++; } fat_sector_count = fat_sectors * 2; @@ -1099,7 +1126,7 @@ s32 pfd_sddrv_calc_fat32_mbr_bpb(PFD_SDDRV_FORMAT_DATA* format_data) { for (;;) { cluster_count = (available_sectors - data_start_sector) / sectors_per_cluster + 2; calculated_fat_sectors = (cluster_count >> 7) & 0xfffff; - if ((cluster_count & 0x7f) != 0) { + if ((cluster_count * 32 & 0xfe0) != 0) { calculated_fat_sectors++; } if (calculated_fat_sectors <= fat_sectors) { @@ -1138,23 +1165,24 @@ s32 pfd_sddrv_store_fat32_mbr_buf(PFD_SDDRV_FORMAT_DATA* format_data, u8* sector PFD_SDDRV_MBR* master_boot_record; PFD_SDDRV_PARTITION_ENTRY* partition; u32 entry_index; + s32 result; u32 cylinder_size; - u32 start_cylinder; - u32 start_head; - u32 start_sector; - u32 end_cylinder; - u32 end_head; - u32 end_sector; + u16 start_cylinder; + u8 start_head; + u16 start_sector; + u16 end_cylinder; + u8 end_head; + u16 end_sector; u32 partition_end; u8 partition_type; - if (format_data == 0 || sector_buffer == 0) { + if (format_data == 0) { return -30; } pf_memset(&settings, 0, 0x10); size_entry = sddrv_size_depend_tbl; for (entry_index = 0; entry_index < 14; size_entry++, entry_index++) { - if (size_entry->min_sectors < format_data->total_sectors && format_data->total_sectors <= size_entry->max_sectors) { + if (size_entry->min_sectors < format_data->total_sectors && size_entry->max_sectors >= format_data->total_sectors) { settings.reserved_sectors = sddrv_size_depend_tbl[entry_index].reserved_sectors; settings.fat_copies = sddrv_size_depend_tbl[entry_index].fat_copies; settings.root_entries = sddrv_size_depend_tbl[entry_index].root_entries; @@ -1162,9 +1190,10 @@ s32 pfd_sddrv_store_fat32_mbr_buf(PFD_SDDRV_FORMAT_DATA* format_data, u8* sector break; } } - if (entry_index == 14) { + result = entry_index == 14 ? -30 : 0; + if (result != 0) { OSReport("ERR Failed to get values with total sectors. pfd_sddrv_get_value_with_total_sectors()\n"); - return -30; + return result; } cylinder_size = settings.fat_copies * settings.root_entries; partition_end = format_data->total_sectors - 1; @@ -1194,11 +1223,11 @@ s32 pfd_sddrv_store_fat32_mbr_buf(PFD_SDDRV_FORMAT_DATA* format_data, u8* sector partition->boot_indicator = 0; partition->start_head = (u8)start_head; pfd_sddrv_store_le16(partition->start_sector_cylinder, - (u16)((start_cylinder << 8) | ((start_sector & 0x3f) | ((start_cylinder >> 2) & 0xc0)))); + ((start_cylinder & 0xff) << 8) + ((start_sector & 0x3f) | ((start_cylinder & 0x300) >> 2))); partition->partition_type = partition_type; partition->end_head = (u8)end_head; pfd_sddrv_store_le16(partition->end_sector_cylinder, - (u16)((end_cylinder << 8) | ((end_sector & 0x3f) | ((end_cylinder >> 2) & 0xc0)))); + ((end_cylinder & 0xff) << 8) + ((end_sector & 0x3f) | ((end_cylinder & 0x300) >> 2))); pfd_sddrv_store_le32(partition->first_sector, format_data->partition_start_sector); pfd_sddrv_store_le32(partition->sector_count, format_data->partition_sector_count); pfd_sddrv_store_le16(master_boot_record->signature, 0xaa55); @@ -1258,36 +1287,38 @@ s32 pfd_sddrv_store_fat32_bpb_buf(PFD_SDDRV_FORMAT_DATA* format_data, u8* sector const PFD_SDDRV_SIZE_DEPEND* size_entry; PFD_SDDRV_FAT32_BPB* boot_sector; u32 entry_index; - u32 sector_value; + s32 result; - if (format_data == 0 || sector_buffer == 0) { + if (format_data == 0) { return -30; } pf_memset(&settings, 0, 0x10); size_entry = sddrv_size_depend_tbl; - for (entry_index = 0; entry_index < 14; entry_index++, size_entry++) { - if (size_entry->min_sectors < format_data->total_sectors && format_data->total_sectors <= size_entry->max_sectors) { - settings.reserved_sectors = size_entry->reserved_sectors; - settings.fat_copies = size_entry->fat_copies; - settings.root_entries = size_entry->root_entries; - settings.sectors_per_cluster = size_entry->sectors_per_cluster; + for (entry_index = 0; entry_index < 14; size_entry++, entry_index++) { + if (size_entry->min_sectors < format_data->total_sectors && size_entry->max_sectors >= format_data->total_sectors) { + settings.reserved_sectors = sddrv_size_depend_tbl[entry_index].reserved_sectors; + settings.fat_copies = sddrv_size_depend_tbl[entry_index].fat_copies; + settings.root_entries = sddrv_size_depend_tbl[entry_index].root_entries; + settings.sectors_per_cluster = sddrv_size_depend_tbl[entry_index].sectors_per_cluster; break; } } - if (entry_index == 14) { + result = entry_index == 14 ? -30 : 0; + if (result != 0) { OSReport("ERR Failed to get values with total sectors. pfd_sddrv_get_value_with_total_sectors()\n"); - return -30; + return result; } pf_memset(sector_buffer, 0, 0x200); boot_sector = (PFD_SDDRV_FAT32_BPB*)sector_buffer; + pfd_sddrv_copy_bytes(boot_sector->oem_name, (const u8*)" ", 8); + pfd_sddrv_copy_bytes(boot_sector->volume_label, (const u8*)"NO NAME ", 11); + pfd_sddrv_copy_bytes(boot_sector->filesystem_type, (const u8*)"FAT32 ", 8); boot_sector->jump[0] = 0xeb; boot_sector->jump[1] = 0; boot_sector->jump[2] = 0x90; - pf_memcpy(boot_sector->oem_name, " ", 8); pfd_sddrv_store_le16(boot_sector->bytes_per_sector, 0x200); boot_sector->sectors_per_cluster = format_data->sectors_per_cluster; - sector_value = format_data->reserved_sectors; - pfd_sddrv_store_le16(boot_sector->reserved_sector_count, (u16)sector_value); + pfd_sddrv_store_le16(boot_sector->reserved_sector_count, format_data->reserved_sectors); boot_sector->fat_count = 2; pfd_sddrv_store_le16(boot_sector->root_entry_count, 0); pfd_sddrv_store_le16(boot_sector->total_sectors_16, 0); @@ -1306,8 +1337,6 @@ s32 pfd_sddrv_store_fat32_bpb_buf(PFD_SDDRV_FORMAT_DATA* format_data, u8* sector boot_sector->drive_number = 0x80; boot_sector->extended_signature = 0x29; pfd_sddrv_store_le32(boot_sector->volume_serial_number, format_data->volume_serial_number); - pf_memcpy(boot_sector->volume_label, "NO NAME ", 11); - pf_memcpy(boot_sector->filesystem_type, "FAT32 ", 8); if (((u32)boot_sector->signature & 1) != 0) { boot_sector->signature[0] = 0x55; boot_sector->signature[1] = 0xaa; @@ -1325,11 +1354,11 @@ static s32 pfd_sddrv_store_fat32_reserved_buf(u8* sector_buffer) { } 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.value = 0x55aa; - } else { + 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 0; } @@ -1337,7 +1366,6 @@ static s32 pfd_sddrv_store_fat32_reserved_buf(u8* sector_buffer) { s32 pfd_sddrv_build_fat32_mbr_bpb(u32 total_sectors) { PFD_SDDRV_FORMAT_DATA format_data; s32 result; - u32 partition_start_sector; pf_memset(&format_data, 0, 0x20); format_data.total_sectors = total_sectors; result = pfd_sddrv_calc_fat32_mbr_bpb(&format_data); @@ -1350,19 +1378,18 @@ s32 pfd_sddrv_build_fat32_mbr_bpb(u32 total_sectors) { OSReport("ERR Failed to store BPB values to buf. pfd_sddrv_store_fat32_bpb_buf()\n"); return result; } - partition_start_sector = format_data.partition_start_sector; if (g_pfd_sddrv_info.media_inserted == 0) { return -33; } if (g_pfd_sddrv_info.media_ejected != 0) { return -33; } - result = ISD_WriteBlock(g_pfd_sddrv_info.device, partition_start_sector, g_pfd_sddrv_buf, 1); + result = ISD_WriteBlock(g_pfd_sddrv_info.device, format_data.partition_start_sector, g_pfd_sddrv_buf, 1); if (result != 0) { OSReport("ERR Failed to write BPB fields. ISD_WriteBlock()\n"); return -36; } - result = ISD_WriteBlock(g_pfd_sddrv_info.device, partition_start_sector + 6, g_pfd_sddrv_buf, 1); + result = ISD_WriteBlock(g_pfd_sddrv_info.device, format_data.partition_start_sector + 6, g_pfd_sddrv_buf, 1); if (result != 0) { OSReport("ERR Failed to write BPB fields. ISD_WriteBlock()\n"); return -36; @@ -1378,12 +1405,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, partition_start_sector + 1, g_pfd_sddrv_buf, 1); + result = ISD_WriteBlock(g_pfd_sddrv_info.device, format_data.partition_start_sector + 1, g_pfd_sddrv_buf, 1); if (result != 0) { OSReport("ERR Failed to write BPB fields. ISD_WriteBlock()\n"); return -36; } - result = ISD_WriteBlock(g_pfd_sddrv_info.device, partition_start_sector + 7, g_pfd_sddrv_buf, 1); + result = ISD_WriteBlock(g_pfd_sddrv_info.device, format_data.partition_start_sector + 7, g_pfd_sddrv_buf, 1); if (result != 0) { OSReport("ERR Failed to write BPB fields. ISD_WriteBlock()\n"); return -36; @@ -1400,12 +1427,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, partition_start_sector + 2, g_pfd_sddrv_buf, 1); + result = ISD_WriteBlock(g_pfd_sddrv_info.device, format_data.partition_start_sector + 2, g_pfd_sddrv_buf, 1); if (result != 0) { OSReport("ERR Failed to write BPB fields. ISD_WriteBlock()\n"); return -36; } - result = ISD_WriteBlock(g_pfd_sddrv_info.device, partition_start_sector + 8, g_pfd_sddrv_buf, 1); + result = ISD_WriteBlock(g_pfd_sddrv_info.device, format_data.partition_start_sector + 8, g_pfd_sddrv_buf, 1); if (result != 0) { OSReport("ERR Failed to write BPB fields. ISD_WriteBlock()\n"); return -36; diff --git a/libs/RVL_SDK/src/nup/nup.cpp b/libs/RVL_SDK/src/nup/nup.cpp index dbebf451..176055dc 100644 --- a/libs/RVL_SDK/src/nup/nup.cpp +++ b/libs/RVL_SDK/src/nup/nup.cpp @@ -40,7 +40,7 @@ struct NUPAuditRecord { }; struct NUPSignedAuditData { - u32 titleId; + u32 format; NUPAuditRecord auditRecord; ESSignature signature; u8 signatureCertificate[0x180]; @@ -165,40 +165,37 @@ static ESError __nupHttpBufferFlushES(u8* buffer, unsigned long size, unsigned l return result; } +static inline char* __nupFindTag(char* response, const char* endTag, const char* startTag, + char** value, size_t* length) { + char* start; + char* end; + if (response != 0 && (start = strstr(response, startTag)) != 0 && + (end = strstr(start, endTag)) != 0) { + *value = start + strlen(startTag); + *length = end - *value; + return end + strlen(endTag); + } + return 0; +} + static s32 __nupParseServerInfo(NUPContextInfo* context, char* response, char* messageId, unsigned long long deviceId) { s32 result = 0; char* start; - char* end; char* afterEnd; char* cursor; size_t valueLength; size_t titleCount; - if (response == 0) { - result = -5004; - goto done; - } { const char* versionEndTag = ""; const char* versionStartTag = ""; - start = strstr(response, versionStartTag); - if (start == 0) { - result = -5004; - goto done; - } - end = strstr(start, versionEndTag); - if (end == 0) { - afterEnd = 0; - } else { - afterEnd = end + strlen(versionEndTag); - } + afterEnd = __nupFindTag(response, versionEndTag, versionStartTag, + &start, &valueLength); if (afterEnd == 0) { result = -5004; goto done; } - start += strlen(versionStartTag); - valueLength = end - start; if (valueLength != strlen("1.0") || strncmp(start, "1.0", valueLength) != 0) { result = -5004; goto done; @@ -208,22 +205,12 @@ static s32 __nupParseServerInfo(NUPContextInfo* context, char* response, char* m { const char* deviceIdEndTag = ""; const char* deviceIdStartTag = ""; - start = strstr(response, deviceIdStartTag); - if (start == 0) { - result = -5004; - goto done; - } - end = strstr(start, deviceIdEndTag); - if (end == 0) { - afterEnd = 0; - } else { - afterEnd = end + strlen(deviceIdEndTag); - } + afterEnd = __nupFindTag(response, deviceIdEndTag, deviceIdStartTag, + &start, &valueLength); if (afterEnd == 0) { result = -5004; goto done; } - start += strlen(deviceIdStartTag); if (strtoull(start, 0, 10) != deviceId) { result = -5004; goto done; @@ -233,23 +220,12 @@ static s32 __nupParseServerInfo(NUPContextInfo* context, char* response, char* m { const char* messageIdEndTag = ""; const char* messageIdStartTag = ""; - start = strstr(response, messageIdStartTag); - if (start == 0) { - result = -5004; - goto done; - } - end = strstr(start, messageIdEndTag); - if (end == 0) { - afterEnd = 0; - } else { - afterEnd = end + strlen(messageIdEndTag); - } + afterEnd = __nupFindTag(response, messageIdEndTag, messageIdStartTag, + &start, &valueLength); if (afterEnd == 0) { result = -5004; goto done; } - start += strlen(messageIdStartTag); - valueLength = end - start; if (valueLength != strlen(messageId) || strncmp(start, messageId, valueLength) != 0) { result = -5004; goto done; @@ -259,23 +235,12 @@ static s32 __nupParseServerInfo(NUPContextInfo* context, char* response, char* m { const char* errorCodeEndTag = ""; const char* errorCodeStartTag = ""; - start = strstr(response, errorCodeStartTag); - if (start == 0) { - result = -5004; - goto done; - } - end = strstr(start, errorCodeEndTag); - if (end == 0) { - afterEnd = 0; - } else { - afterEnd = end + strlen(errorCodeEndTag); - } + afterEnd = __nupFindTag(response, errorCodeEndTag, errorCodeStartTag, + &start, &valueLength); if (afterEnd == 0) { result = -5004; goto done; } - start += strlen(errorCodeStartTag); - valueLength = end - start; if (valueLength != strlen("0") || strncmp(start, "0", valueLength) != 0) { result = -5004; goto done; @@ -285,23 +250,12 @@ static s32 __nupParseServerInfo(NUPContextInfo* context, char* response, char* m { const char* uploadAuditEndTag = ""; const char* uploadAuditStartTag = ""; - start = strstr(response, uploadAuditStartTag); - if (start == 0) { - result = -5004; - goto done; - } - end = strstr(start, uploadAuditEndTag); - if (end == 0) { - afterEnd = 0; - } else { - afterEnd = end + strlen(uploadAuditEndTag); - } + afterEnd = __nupFindTag(response, uploadAuditEndTag, uploadAuditStartTag, + &start, &valueLength); if (afterEnd == 0) { result = -5004; goto done; } - start += strlen(uploadAuditStartTag); - valueLength = end - start; if (valueLength != strlen("0") || (*start != '0' && *start != '1')) { result = -5004; goto done; @@ -312,23 +266,12 @@ static s32 __nupParseServerInfo(NUPContextInfo* context, char* response, char* m { const char* contentPrefixEndTag = ""; const char* contentPrefixStartTag = ""; - start = strstr(response, contentPrefixStartTag); - if (start == 0) { - result = -5004; - goto done; - } - end = strstr(start, contentPrefixEndTag); - if (end == 0) { - afterEnd = 0; - } else { - afterEnd = end + strlen(contentPrefixEndTag); - } + afterEnd = __nupFindTag(response, contentPrefixEndTag, contentPrefixStartTag, + &start, &valueLength); if (afterEnd == 0) { result = -5004; goto done; } - start += strlen(contentPrefixStartTag); - valueLength = end - start; if (valueLength == 0) { result = -5004; goto done; @@ -345,23 +288,12 @@ static s32 __nupParseServerInfo(NUPContextInfo* context, char* response, char* m { const char* uncachedPrefixEndTag = ""; const char* uncachedPrefixStartTag = ""; - start = strstr(response, uncachedPrefixStartTag); - if (start == 0) { - result = -5004; - goto done; - } - end = strstr(start, uncachedPrefixEndTag); - if (end == 0) { - afterEnd = 0; - } else { - afterEnd = end + strlen(uncachedPrefixEndTag); - } + afterEnd = __nupFindTag(response, uncachedPrefixEndTag, uncachedPrefixStartTag, + &start, &valueLength); if (afterEnd == 0) { result = -5004; goto done; } - start += strlen(uncachedPrefixStartTag); - valueLength = end - start; if (valueLength == 0) { result = -5004; goto done; @@ -383,25 +315,14 @@ static s32 __nupParseServerInfo(NUPContextInfo* context, char* response, char* m const char* versionEndTag = ""; const char* versionStartTag = ""; for (;;) { - char* titleStart = strstr(cursor, titleIdStartTag); - if (titleStart == 0) { + cursor = __nupFindTag(cursor, titleIdEndTag, titleIdStartTag, &start, &valueLength); + if (cursor == 0) { break; } - char* titleEnd = strstr(titleStart, titleIdEndTag); - if (titleEnd == 0) { + cursor = __nupFindTag(cursor, versionEndTag, versionStartTag, &start, &valueLength); + if (cursor == 0) { break; } - afterEnd = titleEnd + strlen(titleIdEndTag); - char* versionStart = strstr(afterEnd, versionStartTag); - if (versionStart == 0) { - break; - } - char* versionEnd = strstr(versionStart, versionEndTag); - if (versionEnd == 0) { - break; - } - afterEnd = versionEnd + strlen(versionEndTag); - cursor = afterEnd; titleCount++; } } @@ -419,45 +340,34 @@ static s32 __nupParseServerInfo(NUPContextInfo* context, char* response, char* m const char* versionEndTag = ""; const char* versionStartTag = ""; for (;;) { - char* titleStart = strstr(cursor, titleIdStartTag); - if (titleStart == 0) { + char* titleStart; + char* versionStart; + size_t titleLength; + size_t versionLength; + char* parsedEnd; + cursor = __nupFindTag(cursor, titleIdEndTag, titleIdStartTag, + &titleStart, &titleLength); + if (cursor == 0) { break; } - char* titleEnd = strstr(titleStart, titleIdEndTag); - if (titleEnd == 0) { - result = -5004; - goto done; + cursor = __nupFindTag(cursor, versionEndTag, versionStartTag, + &versionStart, &versionLength); + if (cursor == 0) { + break; } - afterEnd = titleEnd + strlen(titleIdEndTag); - titleStart += strlen(titleIdStartTag); - char* versionStart = strstr(afterEnd, versionStartTag); - if (versionStart == 0) { - result = -5004; - goto done; - } - char* versionEnd = strstr(versionStart, versionEndTag); - if (versionEnd == 0) { - result = -5004; - goto done; - } - versionStart += strlen(versionStartTag); - afterEnd = versionEnd + strlen(versionEndTag); - - char* parsedEnd; unsigned long long titleId = strtoull(titleStart, &parsedEnd, 16); - if (parsedEnd != titleEnd) { + if (parsedEnd != titleStart + titleLength) { result = -5004; goto done; } unsigned long version = strtoul(versionStart, &parsedEnd, 10); - if (parsedEnd != versionEnd || version > 0xffff) { + if (parsedEnd != versionStart + versionLength || (u16)version != version) { result = -5004; goto done; } - NUPTitleInfo* title = &context->titles[context->titleCount++]; - title->titleId = titleId; - title->serverVersion = version; - cursor = afterEnd; + context->titles[context->titleCount].titleId = titleId; + context->titles[context->titleCount].serverVersion = version; + context->titleCount++; } } @@ -531,13 +441,12 @@ static s32 __nupGetServerInfo(char* serverAddress, char* messageId, unsigned lon systemVersion, auditData != 0 ? auditData : ""); result = __nupHttpPostFull(endpoint, headers, request, &responseData, &responseSize, 0, 0, 0); if (result == 0) { - char* responseCopy = (char*)nup::__nupMalloc(responseSize + 1); - *response = responseCopy; - if (responseCopy == 0) { + *response = (char*)nup::__nupMalloc(responseSize + 1); + if (*response == 0) { result = -5000; } else { - strncpy(responseCopy, (char*)responseData, responseSize); - responseCopy[responseSize] = '\0'; + strncpy(*response, (char*)responseData, responseSize); + (*response)[responseSize] = '\0'; } } } @@ -576,25 +485,28 @@ static s32 __nupGetTicketViews(ESTitleId titleId, ESTicketView** ticketViews, u3 return result; } +#pragma dont_inline on static u32 __nupSetAuditState(u8 state) { + const char* auditPath = "NUPAUDIT"; s32 result; if (state != 0) { - result = NANDDelete("NUPAUDIT"); - if (result != 0 && result != -12) { - result -= 6000; - } else { - result = 0; + result = NANDDelete(auditPath); + if (result != 0) { + if (result == -12) { + result = 0; + } else { + result -= 6000; + } } } else { - result = NANDCreateDir("NUPAUDIT", 0x30, 3); - if (result != 0 && result != -6) { - result -= 6000; - } else { - result = 0; + result = NANDCreateDir(auditPath, 0x30, 3); + if (result != 0) { + result = result == -6 ? 0 : result - 6000; } } return result; } +#pragma dont_inline reset static void __nupBase64Encode(u8* output, u8* input, unsigned long length) { const char* alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; @@ -637,7 +549,7 @@ static void __nupBase64Encode(u8* output, u8* input, unsigned long length) { static s32 __nupGetAuditData(NUPContextInfo* context, char** auditData) { s32 result = 0; - ESTitleId selectedTitle = 0; + u32 auditFormat = 0; ESTicketView* ticketViews = 0; u32 viewCount; u32 selected = 0; @@ -651,7 +563,7 @@ static s32 __nupGetAuditData(NUPContextInfo* context, char** auditData) { char deviceInfo[0x21]; u32 i; - for (i = 0; i < context->ownedTitleCount && selected == 0; i++) { + for (i = 0; selected == 0 && i < context->ownedTitleCount; i++) { ESTitleId ownedTitleId = context->ownedTitleIds[i]; if (ownedTitleId != 0x0000000100000002ULL && ownedTitleId != 0x0000000100000100ULL && ownedTitleId != 0x0000000100000101ULL) { @@ -667,7 +579,6 @@ static s32 __nupGetAuditData(NUPContextInfo* context, char** auditData) { for (selectedIndex = 0; selectedIndex < viewCount; selectedIndex++) { if (ticketViews[selectedIndex].reserved.empty_0x00[46] != 0) { selected = 1; - selectedTitle = ownedTitleId; break; } } @@ -680,12 +591,12 @@ static s32 __nupGetAuditData(NUPContextInfo* context, char** auditData) { unsigned long productCodeSize = 0; if (productCode != 0) { memcpy(deviceInfo, productCode, 5); - deviceInfo[5] = '\0'; + deviceInfo[4] = '\0'; productCodeSize = strlen(deviceInfo); } if (SCGetProductSNString(deviceInfo + productCodeSize, sizeof(deviceInfo) - productCodeSize) == 0) { memset(deviceInfo + productCodeSize, 0, sizeof(deviceInfo) - productCodeSize); - strncpy(deviceInfo + productCodeSize, __nupStatusMessage[4], sizeof(deviceInfo) - productCodeSize); + strncpy(deviceInfo + productCodeSize, "", sizeof(deviceInfo) - productCodeSize); } deviceInfo[0x20] = '\0'; @@ -711,7 +622,7 @@ static s32 __nupGetAuditData(NUPContextInfo* context, char** auditData) { if (signedData == 0) { result = -5000; } else { - memcpy(&signedData->titleId, &selectedTitle, sizeof(selectedTitle)); + memcpy(&signedData->format, &auditFormat, sizeof(auditFormat)); memcpy(&signedData->auditRecord, auditRecord, sizeof(NUPAuditRecord)); memcpy(signedData->signature, signature, sizeof(signature)); memcpy(signedData->signatureCertificate, signatureCertificate, 0x180); @@ -755,8 +666,9 @@ done: return result; } +#pragma dont_inline on static s32 __nupGetTmdView(ESTitleId titleId, ESTmdView** tmdView) { - u32 tmdViewSize = 0; + u32 tmdViewSize; s32 result; ESTmdView* view = 0; result = ES_GetTmdView(titleId, 0, &tmdViewSize); @@ -775,9 +687,10 @@ static s32 __nupGetTmdView(ESTitleId titleId, ESTmdView** tmdView) { *tmdView = view; return result; } +#pragma dont_inline reset static s32 __nupGetBootVersion(NUPContextInfo* context, ESTitleVersion* title) { - s32 result = 0; + s32 result; ESTmdView* tmdView = 0; if (title->titleId == 0x0000000100000001ULL) { @@ -812,43 +725,50 @@ static s32 __nupGetBootVersion(NUPContextInfo* context, ESTitleVersion* title) { result = 0; } - if (result >= 0) { + if (result < 0) { + goto done; + } + if (tmdView != 0) { result = ES_ListTitleContentsOnCard(title->titleId, 0, &title->contentCount); if (result == -0x6a) { result = 0; - } else if (result == 0 && tmdView != 0 && title->contentCount != 0) { - title->contentIds = (ESContentId*)nup::__nupMalloc(title->contentCount * sizeof(ESContentId)); - if (title->contentIds == 0) { - result = -5000; - title->contentCount = 0; - } else { - result = ES_ListTitleContentsOnCard(title->titleId, title->contentIds, &title->contentCount); - if (result != 0) { - nup::__nupFree(title->contentIds); - title->contentIds = 0; + } else if (result == 0) { + if (title->contentCount != 0) { + title->contentIds = (ESContentId*)nup::__nupMalloc(title->contentCount * sizeof(ESContentId)); + if (title->contentIds == 0) { + result = -5000; title->contentCount = 0; } else { - u32 contentIndex; - for (contentIndex = 0; contentIndex < tmdView->head.numContents; contentIndex++) { - u32 installedIndex; - for (installedIndex = 0; installedIndex < title->contentCount; installedIndex++) { - if (title->contentIds[installedIndex] == tmdView->contents[contentIndex].cid) { - break; - } - } - if (installedIndex == title->contentCount) { + result = ES_ListTitleContentsOnCard(title->titleId, title->contentIds, &title->contentCount); + if (result != 0) { + nup::__nupFree(title->contentIds); + title->contentIds = 0; + title->contentCount = 0; + } + } + } + if (result == 0 && title->contentCount >= tmdView->head.numContents) { + s32 contentIndex; + for (contentIndex = 0; contentIndex < tmdView->head.numContents; contentIndex++) { + u32 installedIndex; + for (installedIndex = 0; installedIndex < title->contentCount; installedIndex++) { + if (title->contentIds[installedIndex] == tmdView->contents[contentIndex].cid) { break; } } - if (contentIndex == tmdView->head.numContents) { - title->hasContent = 1; + if (installedIndex == title->contentCount) { + break; } } + if (contentIndex >= tmdView->head.numContents) { + title->hasContent = 1; + } } } } } +done: if (tmdView != 0) { nup::__nupFree(tmdView); } @@ -959,8 +879,7 @@ static s32 __nupGetTmd(NUPTitleInfo* title, char* contentPrefixUrl) { } static s32 __nupGetTitleSize(NUPTitleInfo* title) { - ESTmdView* tmdView = (ESTmdView*)title->tmdView; - u32 contentIndex; + s32 contentIndex; s32 result = 0; title->progressStep += 2; if (title->hasTicket == 0) { @@ -970,9 +889,13 @@ static s32 __nupGetTitleSize(NUPTitleInfo* title) { title->progressStep += 4; } title->progressStep += 5; - title->installedContentSize += 0x18000 + ((title->tmdSize + 0x3fff) & 0xffffc000); - for (contentIndex = 0; contentIndex < tmdView->head.numContents; contentIndex++) { - ESContentMeta* content = (ESContentMeta*)&tmdView->contents[contentIndex]; + title->installedContentSize += 0x4000; + title->installedContentSize += (title->tmdSize + 0x3fff) & 0xffffc000; + title->installedContentSize += 0x8000; + title->installedContentSize += 0x4000; + title->installedContentSize += 0x4000; + for (contentIndex = 0; contentIndex < ((ESTmdView*)title->tmdView)->head.numContents; contentIndex++) { + ESCmdView* content = &((ESTmdView*)title->tmdView)->contents[contentIndex]; u32 ownedIndex; ownedIndex = 0; if (title->contentCount != 0 && title->contentIds != 0) { @@ -984,7 +907,7 @@ static s32 __nupGetTitleSize(NUPTitleInfo* title) { } if (title->contentIds == 0 || ownedIndex == title->contentCount || title->titleId == 0x0000000100000001ULL) { - if (content->size > 0xfffffff0ULL) { + if (content->size >= 0xfffffff0ULL) { result = -0x1394; break; } @@ -999,11 +922,11 @@ static s32 __nupGetTitleSize(NUPTitleInfo* title) { static s32 __nupGetContentFull(NUPContextInfo* context, NUPTitleInfo* title, char* contentPrefixUrl, unsigned long contentId) { ESTmdView* tmdView = (ESTmdView*)title->tmdView; - u32 contentIndex; + s32 contentIndex; s32 result; - char* url = 0; u8* response = 0; unsigned long responseSize = 0; + char* url = 0; for (contentIndex = 0; contentIndex < tmdView->head.numContents; contentIndex++) { if (tmdView->contents[contentIndex].cid == contentId) { @@ -1013,8 +936,8 @@ static s32 __nupGetContentFull(NUPContextInfo* context, NUPTitleInfo* title, cha if (contentIndex >= tmdView->head.numContents) { result = -0x1389; } else { - unsigned long contentSize = tmdView->contents[contentIndex].size; - unsigned long alignedSize = (contentSize + 0xf) & 0xfffffff0; + u64 contentSize = tmdView->contents[contentIndex].size; + unsigned long alignedSize = (u32)((contentSize + 0xf) & ~0xfULL); unsigned long urlSize = strlen(contentPrefixUrl) + 0x28; url = (char*)nup::__nupMalloc(urlSize); if (url == 0) { @@ -1024,8 +947,8 @@ static s32 __nupGetContentFull(NUPContextInfo* context, NUPTitleInfo* title, cha result = __nupHttpGetFull(url, &response, &responseSize, alignedSize, __nupUpdateStatusIncr, context); if (result == 0) { - title->content = response; title->contentSize = responseSize; + title->content = response; } } } @@ -1038,6 +961,21 @@ static s32 __nupGetContentFull(NUPContextInfo* context, NUPTitleInfo* title, cha return result; } +static inline BOOL __nupHasContent(const NUPTitleInfo* title, ESContentId contentId) { + if (title->contentCount == 0 || title->contentIds == 0) { + return FALSE; + } + ESContentId* installedContent = title->contentIds; + u32 index; + for (index = 0; index < title->contentCount; index++) { + if (contentId == *installedContent) { + break; + } + installedContent++; + } + return index < title->contentCount; +} + static s32 __nupGetContentIncr(NUPContextInfo* context, NUPTitleInfo* title, char* contentPrefixUrl) { u32 contentIndex; s32 result = 0; @@ -1049,23 +987,13 @@ static s32 __nupGetContentIncr(NUPContextInfo* context, NUPTitleInfo* title, cha } else { for (contentIndex = 0; contentIndex < ((ESTmdView*)title->tmdView)->head.numContents; contentIndex++) { ESContentId contentId = ((ESTmdView*)title->tmdView)->contents[contentIndex].cid; - u32 ownedIndex = 0; - int alreadyInstalled = 0; - if (title->contentCount != 0 && title->contentIds != 0) { - for (; ownedIndex < title->contentCount; ownedIndex++) { - if (title->contentIds[ownedIndex] == contentId) { - alreadyInstalled = 1; - break; - } - } - } - if (alreadyInstalled == 0) { + if (!__nupHasContent(title, contentId)) { snprintf(url, urlSize, "%s/%016llx/%08x", contentPrefixUrl, title->titleId, contentId); unsigned long contentSize = ((ESTmdView*)title->tmdView)->contents[contentIndex].size; ESFd fd = ES_ImportContentBegin(title->titleId, contentId); - if (fd < 0) { + result = fd; + if (result < 0) { ES_ImportContentEnd(fd); - result = fd; break; } result = __nupHttpGetIncr(url, (contentSize + 0xf) & 0xfffffff0, @@ -1159,224 +1087,294 @@ static void __nupCleanup(NUPContextInfo* context) { static s32 __nupUpdateTitle(NUPContextInfo* context, NUPTitleInfo* title, char* contentPrefixUrl, char* uncachedContentPrefixUrl) { - s32 result = 0; + s32 result; if (title->titleId == 0x0000000100000001ULL) { result = __nupGetTicket(title, contentPrefixUrl); - if (result == 0) { - ESTmdView* tmdView = (ESTmdView*)title->tmdView; - if (tmdView->head.numContents != 1) { - result = -0x1389; - } else { - result = __nupGetContentFull(context, title, contentPrefixUrl, tmdView->contents[0].cid); - if (result == 0) { - result = ES_ImportBoot((ESTicket*)title->ticket, title->ticketCertificate, - title->ticketCertificateSize, (ESTitleMeta*)title->tmd, - title->tmdSize, title->tmdCertificate, title->tmdCertificateSize, - 0, 0, title->content, title->contentSize); - } - } + if (result != 0) { + goto done; } + ESTmdView* tmdView = (ESTmdView*)title->tmdView; + if (tmdView->head.numContents != 1) { + result = -0x1389; + goto done; + } + result = __nupGetContentFull(context, title, uncachedContentPrefixUrl, tmdView->contents[0].cid); + if (result != 0) { + goto done; + } + result = ES_ImportBoot((ESTicket*)title->ticket, title->ticketCertificate, + title->ticketCertificateSize, (ESTitleMeta*)title->tmd, + title->tmdSize, title->tmdCertificate, title->tmdCertificateSize, + 0, 0, title->content, title->contentSize); } else { ISFSStats stats; result = ISFS_GetStats(&stats); - if (result == 0) { - if (stats.freeBlocks >= (title->installedContentSize >> 14) && - stats.freeInodes >= title->progressStep) { - result = 1; - } + if (result == 0 && stats.freeBlocks >= (title->installedContentSize >> 14) && + title->progressStep <= stats.freeInodes) { + result = 1; } if (result == 0) { result = -0x1392; - } else if (result > 0) { - result = __nupGetTicket(title, contentPrefixUrl); + goto done; } - if (result == 0) { - result = ES_ImportTicket((ESTicket*)title->ticket, title->ticketCertificate, - title->ticketCertificateSize, 0, 0, 0); + if (result < 0) { + goto done; } - if (result == 0) { - result = ES_ImportTitleInit((ESTitleMeta*)title->tmd, title->tmdSize, title->tmdCertificate, - title->tmdCertificateSize, 0, 0, 0, 1); - if (result != 0) { - ES_ImportTitleCancel(); - } + result = __nupGetTicket(title, contentPrefixUrl); + if (result != 0) { + goto done; } - if (result == 0) { - result = __nupGetContentIncr(context, title, uncachedContentPrefixUrl); - if (result != 0) { - ES_ImportTitleCancel(); - } + result = ES_ImportTicket((ESTicket*)title->ticket, title->ticketCertificate, + title->ticketCertificateSize, 0, 0, 0); + if (result != 0) { + goto done; } - if (result == 0) { - result = ES_ImportTitleDone(); + result = ES_ImportTitleInit((ESTitleMeta*)title->tmd, title->tmdSize, title->tmdCertificate, + title->tmdCertificateSize, 0, 0, 0, 1); + if (result != 0) { + ES_ImportTitleCancel(); + goto done; } + result = __nupGetContentIncr(context, title, uncachedContentPrefixUrl); + if (result != 0) { + ES_ImportTitleCancel(); + goto done; + } + result = ES_ImportTitleDone(); } if (result == 0) { __nupCleanupTitleInfo(title); title->updateRequired = 0; } +done: return result; } extern "C" void* __nupOp(void* argument) { NUPContextInfo* context = (NUPContextInfo*)argument; - u8* response; - ESTmdView* tmdView; - char* auditData; + u8* response = 0; + ESTmdView* tmdView = 0; + char* auditData = 0; + NUPTitleInfo* bootTitle = 0; + NUPTitleInfo* menuTitle = 0; + NUPTitleInfo* systemTitle = 0; u32 deviceId; u32 bootVersion; + u16 serverBootVersion = 0; u16 bootTitleVersion; u16 systemMenuVersion; ESTitleId systemMenuTitleId; - char messageId[0x50]; + char messageId[0x15]; NANDStatus auditStatus; - s32 result = 0; + s32 result; u32 i; + BOOL needsAudit; - response = 0; - tmdView = 0; - auditData = 0; - snprintf(messageId, 0x15, "%llu", OSGetTime()); + snprintf(messageId, sizeof(messageId), "%llu", OSGetTime()); result = ES_GetDeviceId(&deviceId); + if (result != 0) { + goto done; + } + result = ES_GetBoot2Version(&bootVersion); if (result == 0) { - result = ES_GetBoot2Version(&bootVersion); - if (result == 0 && (bootVersion & 0xffff) != bootVersion) { + serverBootVersion = bootVersion; + if (serverBootVersion != bootVersion) { result = -0x1389; } } - if (result >= 0) { - result = __nupGetTmdView(0x0000000100000002ULL, &tmdView); - if (result >= 0) { - bootTitleVersion = tmdView->head.titleVersion; - systemMenuTitleId = tmdView->head.sysVersion; - nup::__nupFree(tmdView); - tmdView = 0; - result = __nupGetTmdView(systemMenuTitleId, &tmdView); - if (result >= 0) { - systemMenuVersion = tmdView->head.titleVersion; - } + if (result < 0) { + goto done; + } + result = __nupGetTmdView(0x0000000100000002ULL, &tmdView); + if (result < 0) { + goto done; + } + bootTitleVersion = tmdView->head.titleVersion; + systemMenuTitleId = tmdView->head.sysVersion; + nup::__nupFree(tmdView); + tmdView = 0; + result = __nupGetTmdView(systemMenuTitleId, &tmdView); + if (result < 0) { + goto done; + } + systemMenuVersion = tmdView->head.titleVersion; + nup::__nupFree(tmdView); + tmdView = 0; + result = ES_ListOwnedTitles(0, &context->ownedTitleCount); + if (result == 0) { + context->ownedTitleIds = (ESTitleId*)nup::__nupMalloc(context->ownedTitleCount * sizeof(ESTitleId)); + if (context->ownedTitleIds != 0) { + result = ES_ListOwnedTitles(context->ownedTitleIds, &context->ownedTitleCount); + } else { + result = -5000; } } - if (result >= 0) { - nup::__nupFree(tmdView); - tmdView = 0; + if (result != 0) { + goto done; } - if (result >= 0) { - result = ES_ListOwnedTitles(0, &context->ownedTitleCount); - if (result == 0) { - context->ownedTitleIds = (ESTitleId*)nup::__nupMalloc(context->ownedTitleCount * sizeof(ESTitleId)); - if (context->ownedTitleIds == 0) { - result = -5000; - } else { - result = ES_ListOwnedTitles(context->ownedTitleIds, &context->ownedTitleCount); - } + needsAudit = TRUE; + if (NANDGetStatus("NUPAUDIT", &auditStatus) == 0) { + needsAudit = FALSE; + } + if (needsAudit) { + result = __nupGetAuditData(context, &auditData); + if (result != 0) { + goto done; } } - s32 auditResult = 0; - if (result >= 0) { - auditResult = NANDGetStatus("NUPAUDIT", &auditStatus); - if (auditResult != 0) { - result = __nupGetAuditData(context, &auditData); - } - } - if (result >= 0) { - OSLockMutex(&context->mutex); - context->progress.result = 2; - context->progress.message = __nupStatusMessage[2]; - OSUnlockMutex(&context->mutex); - result = __nupGetServerInfo((char*)context->serverAddress, messageId, deviceId, - (char*)context->productArea, (char*)context->countryCode, - context->systemVersion, auditData, bootVersion, systemMenuVersion, - systemMenuTitleId, 0, (char**)&response); + OSLockMutex(&context->mutex); + context->progress.result = 2; + context->progress.message = __nupStatusMessage[2]; + OSUnlockMutex(&context->mutex); + result = __nupGetServerInfo((char*)context->serverAddress, messageId, + 0x0000000100000000ULL | deviceId, + (char*)context->productArea, (char*)context->countryCode, + context->systemVersion, auditData, serverBootVersion, bootTitleVersion, + systemMenuTitleId, systemMenuVersion, (char**)&response); + if (result != 0) { + goto done; } if (auditData != 0) { nup::__nupFree(auditData); - auditData = 0; } - if (result >= 0) { - result = __nupParseServerInfo(context, (char*)response, messageId, deviceId); + auditData = 0; + result = __nupParseServerInfo(context, (char*)response, messageId, + 0x0000000100000000ULL | deviceId); + if (result != 0) { + goto done; } - if (result >= 0 && ((auditResult != 0) != (context->uploadAuditData != 0))) { + if (needsAudit != context->uploadAuditData) { result = __nupSetAuditState(context->uploadAuditData); if (result != 0) { result = 0; } } - if (response != 0) { - nup::__nupFree(response); - response = 0; - } - if (result >= 0) { - for (i = 0; i < context->titleCount; i++) { - NUPTitleInfo* title = &context->titles[i]; - result = __nupGetBootVersion(context, (ESTitleVersion*)title); - if (result >= 0) { - title->updateRequired = - !title->hasTicket || !title->hasTmd || !title->hasContent || - title->serverVersion < title->installedVersion; - if (title->updateRequired != 0) { - context->updateCount++; - } - result = title->updateRequired; + for (i = 0; i < context->titleCount; i++) { + NUPTitleInfo* title = &context->titles[i]; + result = __nupGetBootVersion(context, (ESTitleVersion*)title); + if (result == 0) { + if (title->hasTicket && title->hasTmd && title->hasContent && + title->installedVersion >= title->serverVersion) { + title->updateRequired = 0; + } else { + title->updateRequired = 1; } + } + if (result >= 0) { + result = title->updateRequired; + } + if (result < 0) { + goto done; + } + if (result != 0) { + context->updateCount++; + } + } + for (i = 0; i < context->titleCount; i++) { + NUPTitleInfo* title = &context->titles[i]; + if (title->updateRequired != 0) { + result = __nupGetTmd(title, context->uncachedContentPrefixUrl); if (result < 0) { + goto done; + } + } + } + for (i = 0; i < context->titleCount; i++) { + NUPTitleInfo* title = &context->titles[i]; + if (title->updateRequired != 0) { + result = __nupGetTitleSize(title); + if (result < 0) { + goto done; + } + context->downloadedBytes += context->titles[i].updateContentSize; + } + } + { + u64 totalBytes = context->downloadedBytes; + OSLockMutex(&context->mutex); + context->progress.totalBytes = totalBytes; + context->progress.completedBytes = 0; + OSUnlockMutex(&context->mutex); + } + OSLockMutex(&context->mutex); + context->progress.result = 3; + context->progress.message = __nupStatusMessage[3]; + OSUnlockMutex(&context->mutex); + for (i = 0; i < context->titleCount; i++) { + NUPTitleInfo* title = &context->titles[i]; + if (title->titleId == 0x0000000100000001ULL) { + bootTitle = title; + } else if (title->titleId == 0x0000000100000002ULL) { + menuTitle = title; + } else if (title->titleId == systemMenuTitleId) { + systemTitle = title; + } + if (bootTitle != 0 && menuTitle != 0 && systemTitle != 0) { + break; + } + } + if (menuTitle != 0 && menuTitle->updateRequired != 0) { + ESTitleId requiredSystemTitle = ((ESTmdView*)menuTitle->tmdView)->head.sysVersion; + for (i = 0; i < context->titleCount; i++) { + if (context->titles[i].titleId == requiredSystemTitle) { + systemTitle = &context->titles[i]; break; } } } - if (result >= 0) { - for (i = 0; i < context->titleCount; i++) { - NUPTitleInfo* title = &context->titles[i]; - if (title->updateRequired != 0) { - result = __nupGetTmd(title, context->uncachedContentPrefixUrl); - if (result < 0) { - break; - } + if (bootTitle != 0 && bootTitle->updateRequired != 0) { + result = __nupUpdateTitle(context, bootTitle, context->uncachedContentPrefixUrl, + context->contentPrefixUrl); + if (result < 0) { + goto done; + } + } + if (systemTitle != 0 && systemTitle->updateRequired != 0) { + result = __nupUpdateTitle(context, systemTitle, context->uncachedContentPrefixUrl, + context->contentPrefixUrl); + if (result < 0) { + goto done; + } + } + if (menuTitle != 0 && menuTitle->updateRequired != 0) { + result = __nupUpdateTitle(context, menuTitle, context->uncachedContentPrefixUrl, + context->contentPrefixUrl); + if (result < 0) { + goto done; + } + } + for (i = 0; i < context->titleCount; i++) { + NUPTitleInfo* title = &context->titles[i]; + if (title->updateRequired != 0) { + result = __nupUpdateTitle(context, title, context->uncachedContentPrefixUrl, + context->contentPrefixUrl); + if (result < 0) { + goto done; } } } - if (result >= 0) { - for (i = 0; i < context->titleCount; i++) { - NUPTitleInfo* title = &context->titles[i]; - if (title->updateRequired != 0) { - result = __nupGetTitleSize(title); - if (result < 0) { - break; - } - context->progress.totalBytes += title->updateContentSize; - context->downloadedBytes += title->installedContentSize; - } - } +done: + if (tmdView != 0) { + nup::__nupFree(tmdView); } - if (result >= 0) { - OSLockMutex(&context->mutex); - context->progress.totalBytes = context->downloadedBytes; - context->progress.completedBytes = 0; - OSUnlockMutex(&context->mutex); - OSLockMutex(&context->mutex); - context->progress.result = 3; - context->progress.message = __nupStatusMessage[3]; - OSUnlockMutex(&context->mutex); - for (i = 0; i < context->titleCount; i++) { - NUPTitleInfo* title = &context->titles[i]; - if (title->updateRequired != 0) { - result = __nupUpdateTitle(context, title, context->contentPrefixUrl, - context->uncachedContentPrefixUrl); - if (result < 0) { - break; - } - } - } + if (response != 0) { + nup::__nupFree(response); + } + if (auditData != 0) { + nup::__nupFree(auditData); } __nupCleanup(context); - OSLockMutex(&context->mutex); - context->progress.result = result < 0 ? result : 0; - if (result < 0) { - context->progress.message = __nupStatusMessage[5]; - } else { + if (result >= 0) { + OSLockMutex(&context->mutex); + context->progress.result = 0; context->progress.message = __nupStatusMessage[0]; + OSUnlockMutex(&context->mutex); + } else { + OSLockMutex(&context->mutex); + context->progress.result = result; + context->progress.message = __nupStatusMessage[5]; + OSUnlockMutex(&context->mutex); } - OSUnlockMutex(&context->mutex); return 0; } diff --git a/libs/RVL_SDK/src/vi/vi.c b/libs/RVL_SDK/src/vi/vi.c index f3db6541..6d7bb194 100644 --- a/libs/RVL_SDK/src/vi/vi.c +++ b/libs/RVL_SDK/src/vi/vi.c @@ -116,11 +116,12 @@ static BOOL OnShutdown(BOOL final, u32 event) if ((s32)event >= 0) { goto beginShutdown; } - goto returnTrue; + return final; } if ((s32)event >= 7) { - goto returnTrue; + return final; } + goto returnTrue; beginShutdown: if (first) { VISetRGBModeImm(); @@ -137,8 +138,8 @@ beginShutdown: flushFlag3in1 = 1; NextBufAddr = HorVer.bufAddr; OSRestoreInterrupts(enabled); - first = FALSE; previousRetraceCount = retraceCount; + first = FALSE; return FALSE; } if (previousRetraceCount == retraceCount) { @@ -228,8 +229,9 @@ position_interrupt: OSClearContext(&exceptionContext); OSSetCurrentContext(&exceptionContext); if (PositionCallback != 0) { - previousVertical = __VIRegs[22] & 0x7FF; + vertical = __VIRegs[22] & 0x7FF; do { + previousVertical = vertical; horizontal = __VIRegs[23] & 0x7FF; vertical = __VIRegs[22] & 0x7FF; } while (previousVertical != vertical); @@ -253,14 +255,14 @@ position_interrupt: u32 vertical; u32 currentLine; - previousVertical = __VIRegs[22] & 0x7FF; + vertical = __VIRegs[22] & 0x7FF; do { + previousVertical = vertical; horizontal = __VIRegs[23] & 0x7FF; vertical = __VIRegs[22] & 0x7FF; } while (previousVertical != vertical); - currentLine = ((vertical - 1) << 1) + - (horizontal - 1) / CurrTiming->hlw; - if (CurrTiming->nhlines > currentLine) { + currentLine = (CurrTiming->nhlines / 2) + 1; + if (vertical != 1 && vertical != currentLine) { vsync_timing_err_cnt++; } } @@ -560,10 +562,16 @@ void __VIInit(VITVMode mode) vct = (currentTiming->nhlines / 2) + 1; __VIRegs[25] = hct; __VIRegs[24] = vct | 0x1000; - if (tv >= 4) { + switch (tv) { + case VI_PAL: + case VI_MPAL: + case VI_DEBUG: + break; + default: tv = 0; + break; } - if ((nonInter & 2) == 0) { + if (nonInter <= 1) { __VIRegs[1] = (nonInter << 2) | 1 | (tv << 8); __VIRegs[54] = 0; } else { @@ -582,6 +590,8 @@ void VIInit(void) u32 value; u32 tv; u32 bootromTv; + u32 scanMode; + u32 field; if (IsInitialized != 0) { return; @@ -618,12 +628,14 @@ void VIInit(void) bootromTv = *(u32*)0x800000CC; displayConfig = __VIRegs[1]; enabled = OSDisableInterrupts(); - if ((__VIRegs[54] & 1) != 0) { - HorVer.nonInter = 2; + if ((u32)(__VIRegs[54] & 1) == 1U) { + scanMode = 2; } else { - HorVer.nonInter = (__VIRegs[1] & 4) != 0; + field = (__VIRegs[1] >> 2) & 1; + scanMode = ((0U - field) | field) >> 31; } OSRestoreInterrupts(enabled); + HorVer.nonInter = scanMode; tv = (displayConfig >> 8) & 3; HorVer.tv = tv; if (bootromTv == 5 || (bootromTv == 1 && tv == 0)) { @@ -815,7 +827,7 @@ static void setHorizontalRegs(VITiming* currentTiming, u16 displayPosX, #pragma dont_inline reset #pragma dont_inline on -static void setVerticalRegs(u16 displayPosY, u16 displaySizeY, u8 equ, +static void setVerticalRegs(s32 displayPosY, s32 displaySizeY, u8 equ, u16 acv, u16 prbOdd, u16 prbEven, u16 psbOdd, u16 psbEven, int black) { @@ -824,10 +836,10 @@ static void setVerticalRegs(u16 displayPosY, u16 displaySizeY, u8 equ, u16 actualPsbOdd; u16 actualPsbEven; u16 actualAcv; - u16 d; - u16 c; + s32 d; + s32 c; - if (equ >= 10) { + if (HorVer.nonInter == 2 || HorVer.nonInter == 3) { c = 1; d = 2; } else { @@ -853,10 +865,10 @@ static void setVerticalRegs(u16 displayPosY, u16 displaySizeY, u8 equ, actualPsbEven += 2; actualAcv = 0; } - regs[0] = equ | (actualAcv << 4); MARK_CHANGED(0); - regs[7] = (u16)(u32)actualPrbOdd; + regs[7] = actualPrbOdd; MARK_CHANGED(7); + regs[0] = equ | (actualAcv << 4); regs[6] = (u16)(u32)actualPsbOdd; MARK_CHANGED(6); regs[9] = (u16)(u32)actualPrbEven; @@ -1012,10 +1024,10 @@ void VIConfigure(const GXRenderModeObj* renderMode) OSReport("mode in real games!!! \n"); OSReport("***************************************\n"); } - if (((tvInBootrom == 1 || tvInBootrom == 5) && - tvInGame != 1 && tvInGame != 5) || - ((tvInBootrom != 1 && tvInBootrom != 5) && - (tvInGame == 1 || tvInGame == 5))) { + if (((tvInBootrom != 1 && tvInBootrom != 5) && + (tvInGame == 1 || tvInGame == 5)) || + ((tvInBootrom == 1 || tvInBootrom == 5) && + tvInGame != 1 && tvInGame != 5)) { OSPanic("vi.c", 0xA57, "VIConfigure(): Tried to change mode from (%d) to (%d), which is forbidden\n", tvInBootrom, tvInGame); @@ -1058,7 +1070,7 @@ void VIConfigure(const GXRenderModeObj* renderMode) register1 = (register1 & ~(1 << 2)) | ((HorVer.nonInter & 1) << 2); } register1 = (register1 & ~(1 << 3)) | (HorVer.threeD << 3); - if (HorVer.tv == VI_TVMODE_PAL_INT || HorVer.tv == VI_TVMODE_PAL_DS) { + if (HorVer.tv == VI_DEBUG_PAL || HorVer.tv == VI_EURGB60) { register1 &= ~(3 << 8); } else { register1 = (register1 & ~(3 << 8)) | (HorVer.tv << 8); @@ -1066,8 +1078,7 @@ void VIConfigure(const GXRenderModeObj* renderMode) regs[1] = register1; MARK_CHANGED(1); register54 = regs[54]; - if ((s32)renderMode->viTVmode == VI_TVMODE_NTSC_PROG || - (s32)renderMode->viTVmode == 3) { + if (HorVer.nonInter == 2 || HorVer.nonInter == 3) { register54 |= 1; } else { register54 &= ~1; @@ -1319,51 +1330,61 @@ void __VIDisplayPositionToXY(u32 horizontalCount, u32 verticalCount, ((horizontalCount - 1) / currentTiming->hlw); if (HorVer.nonInter == 0) { if (halfLine < currentTiming->nhlines) { - fieldStart = currentTiming->prbOdd + currentTiming->equ * 3; + fieldStart = ((u32)currentTiming->equ << 2) - currentTiming->equ; fieldEnd = currentTiming->nhlines - currentTiming->psbOdd; - if (halfLine < fieldStart || halfLine >= fieldEnd) { + if (halfLine < fieldStart + currentTiming->prbOdd) { + *pixelY = -1; + } else if (halfLine >= fieldEnd) { *pixelY = -1; } else { - *pixelY = (halfLine - fieldStart) / 2; + *pixelY = (halfLine - fieldStart - currentTiming->prbOdd) & ~1U; } } else { fieldLine = halfLine - currentTiming->nhlines; - fieldStart = currentTiming->prbEven + currentTiming->equ * 3; + fieldStart = ((u32)currentTiming->equ << 2) - currentTiming->equ; fieldEnd = currentTiming->nhlines - currentTiming->psbEven; - if (fieldLine < fieldStart || fieldLine >= fieldEnd) { + if (fieldLine < fieldStart + currentTiming->prbEven) { + *pixelY = -1; + } else if (fieldLine >= fieldEnd) { *pixelY = -1; } else { - *pixelY = ((fieldLine - fieldStart) / 2) + 1; + *pixelY = ((fieldLine - fieldStart - currentTiming->prbEven) & ~1U) + 1; } } } else if (HorVer.nonInter == 1) { if (halfLine >= currentTiming->nhlines) { halfLine -= currentTiming->nhlines; } - fieldStart = currentTiming->prbOdd + currentTiming->equ * 3; + fieldStart = ((u32)currentTiming->equ << 2) - currentTiming->equ; fieldEnd = currentTiming->nhlines - currentTiming->psbOdd; - if (halfLine < fieldStart || halfLine >= fieldEnd) { + if (halfLine < fieldStart + currentTiming->prbOdd) { + *pixelY = -1; + } else if (halfLine >= fieldEnd) { *pixelY = -1; } else { - *pixelY = (halfLine - fieldStart) / 2; + *pixelY = (halfLine - fieldStart - currentTiming->prbOdd) & ~1U; } } else if (HorVer.nonInter == 2) { if (halfLine < currentTiming->nhlines) { - fieldStart = currentTiming->prbOdd + currentTiming->equ * 3; + fieldStart = ((u32)currentTiming->equ << 2) - currentTiming->equ; fieldEnd = currentTiming->nhlines - currentTiming->psbOdd; - if (halfLine < fieldStart || halfLine >= fieldEnd) { + if (halfLine < fieldStart + currentTiming->prbOdd) { + *pixelY = -1; + } else if (halfLine >= fieldEnd) { *pixelY = -1; } else { - *pixelY = (halfLine - fieldStart) / 2; + *pixelY = halfLine - fieldStart - currentTiming->prbOdd; } } else { fieldLine = halfLine - currentTiming->nhlines; - fieldStart = currentTiming->prbEven + currentTiming->equ * 3; + fieldStart = ((u32)currentTiming->equ << 2) - currentTiming->equ; fieldEnd = currentTiming->nhlines - currentTiming->psbEven; - if (fieldLine < fieldStart || fieldLine >= fieldEnd) { + if (fieldLine < fieldStart + currentTiming->prbEven) { + *pixelY = -1; + } else if (fieldLine >= fieldEnd) { *pixelY = -1; } else { - *pixelY = (fieldLine - fieldStart) / 2; + *pixelY = (fieldLine - fieldStart - currentTiming->prbEven) & ~1U; } } } diff --git a/libs/RVL_SDK/src/wad/wad.c b/libs/RVL_SDK/src/wad/wad.c index 33835973..a60f7e33 100644 --- a/libs/RVL_SDK/src/wad/wad.c +++ b/libs/RVL_SDK/src/wad/wad.c @@ -560,7 +560,8 @@ static s32 WAD_815BFFA8(WADImportLoopArgs* args) { s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 offset, u32 flags, WADProcessCallback processCallback) { - WADImportWorkspace workspace ALIGN32; + WADStream stream ALIGN32; + WADUnpackInfo unpackInfo; WADSaveDataHeader wadHeader ALIGN32; WADFileHeader backupHeader ALIGN32; WADImportTransfer transfer; @@ -590,8 +591,8 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o u32 sizeRemaining; u32 transferIdValue; s32 result = 0; - s32 streamOpened = FALSE; - s32 backupFileOpened = FALSE; + s32 streamOpened; + s32 backupFileOpened; s32 titleImportStarted = FALSE; s32 contentImportStarted = FALSE; s32 threadCreated = FALSE; @@ -624,9 +625,10 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o u32 threadPriority; const char* functionName = "WADImportEx"; - memset(&workspace.unpackInfo, 0, sizeof(WADUnpackInfo)); - memset(&transfer, 0, sizeof(transfer)); - if ((path == 0) || (allocator == 0) || (allocator->heap == 0)) { + streamOpened = FALSE; + backupFileOpened = FALSE; + memset(&unpackInfo, 0, sizeof(WADUnpackInfo)); + if ((allocator == 0) || (allocator->heap == 0) || (path == 0)) { result = -3000; goto cleanup; } @@ -637,29 +639,29 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o if (processCallback != 0) { processCallback(0, 0, FALSE); } - result = WADOpenStream(location, path, &workspace.stream, 0, 0); + result = WADOpenStream(location, path, &stream, 0, 0); streamOpened = TRUE; if (result != 0) { goto cleanup; } - result = WADReadStream(&workspace.stream, &wadHeaderBuffer, sizeof(wadHeader), offset); + result = WADReadStream(&stream, &wadHeaderBuffer, sizeof(wadHeader), offset); if (result != sizeof(wadHeader)) { result = -3005; goto cleanup; } - result = _WADUnpack(&wadHeader, &workspace.stream, &workspace.unpackInfo, allocator, offset, + result = _WADUnpack(&wadHeader, &stream, &unpackInfo, allocator, offset, flags, 0); if (result != 0) { goto cleanup; } - importedBytes = workspace.unpackInfo.contentOffset; - callbackTotal = workspace.unpackInfo.contentSize + workspace.unpackInfo.fileListSize; - if (((flags & 8) != 0) && (workspace.unpackInfo.titleMeta != 0)) { - if (workspace.unpackInfo.headerInfo != 2) { + importedBytes = unpackInfo.contentOffset; + callbackTotal = unpackInfo.contentSize + unpackInfo.fileListSize; + if (((flags & 8) != 0) && (unpackInfo.titleMeta != 0)) { + if (unpackInfo.headerInfo != 2) { result = -3001; goto cleanup; } - result = ES_GetTicketViews(workspace.unpackInfo.titleMeta->head.titleId, 0, + result = ES_GetTicketViews(unpackInfo.titleMeta->head.titleId, 0, &installedContentCount); if (result != 0) { goto cleanup; @@ -669,42 +671,42 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o goto cleanup; } } - if ((workspace.unpackInfo.titleMeta != 0) && - (((workspace.unpackInfo.size_0x18 == 1) && - (workspace.unpackInfo.size_0x0c == 1)) || - (workspace.unpackInfo.headerInfo == 3))) { - bootSize = (workspace.unpackInfo.titleMetaSize + 0x0F) & ~0x0F; + if ((unpackInfo.titleMeta != 0) && + (((unpackInfo.size_0x18 == 1) && + (unpackInfo.size_0x0c == 1)) || + (unpackInfo.headerInfo == 3))) { + bootSize = (unpackInfo.titleMetaSize + 0x0F) & ~0x0F; alignedSize = (bootSize + 0x1F) & ~0x1F; firstBuffer = _WADMemAlloc(allocator, alignedSize); if (firstBuffer == 0) { result = -3003; goto cleanup; } - result = WADReadStream(&workspace.stream, &firstBuffer, alignedSize, + result = WADReadStream(&stream, &firstBuffer, alignedSize, offset + importedBytes); if ((u32)result != alignedSize) { result = -3005; goto cleanup; } - result = ES_ImportBoot(workspace.unpackInfo.buffer_0x34, - workspace.unpackInfo.buffer_0x14, - workspace.unpackInfo.size_0x0c, - workspace.unpackInfo.titleMeta, - workspace.unpackInfo.sectionSize, - workspace.unpackInfo.buffer_0x14, - workspace.unpackInfo.size_0x0c, - workspace.unpackInfo.buffer_0x24, - workspace.unpackInfo.size_0x1c, firstBuffer, - workspace.unpackInfo.titleMetaSize); + result = ES_ImportBoot(unpackInfo.buffer_0x34, + unpackInfo.buffer_0x14, + unpackInfo.size_0x0c, + unpackInfo.titleMeta, + unpackInfo.sectionSize, + unpackInfo.buffer_0x14, + unpackInfo.size_0x0c, + unpackInfo.buffer_0x24, + unpackInfo.size_0x1c, firstBuffer, + unpackInfo.titleMetaSize); goto cleanup; } - if ((workspace.unpackInfo.titleMeta != 0) && - (workspace.unpackInfo.buffer_0x34 != 0)) { - result = ES_ImportTicket(workspace.unpackInfo.buffer_0x34, - workspace.unpackInfo.buffer_0x14, - workspace.unpackInfo.size_0x0c, - workspace.unpackInfo.buffer_0x24, - workspace.unpackInfo.size_0x1c, 0); + if ((unpackInfo.titleMeta != 0) && + (unpackInfo.buffer_0x34 != 0)) { + result = ES_ImportTicket(unpackInfo.buffer_0x34, + unpackInfo.buffer_0x14, + unpackInfo.size_0x0c, + unpackInfo.buffer_0x24, + unpackInfo.size_0x1c, 0); if (result != 0) { goto cleanup; } @@ -712,18 +714,18 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o if (processCallback != 0) { processCallback(0, callbackTotal, FALSE); } - if (workspace.unpackInfo.titleMeta != 0) { - if (workspace.unpackInfo.cidxMode == 0) { - contentCount = workspace.unpackInfo.titleMeta->head.numContents; + if (unpackInfo.titleMeta != 0) { + if (unpackInfo.cidxMode == 0) { + contentCount = unpackInfo.titleMeta->head.numContents; } else { - contentCount = _WADGetCidxCount(workspace.unpackInfo.contentIndex); - if (workspace.unpackInfo.titleMeta->head.numContents < contentCount) { + contentCount = _WADGetCidxCount(unpackInfo.contentIndex); + if (unpackInfo.titleMeta->head.numContents < contentCount) { result = -3001; goto cleanup; } } - result = ES_ListTmdContentsOnCard(workspace.unpackInfo.titleMeta, - workspace.unpackInfo.sectionSize, 0, + result = ES_ListTmdContentsOnCard(unpackInfo.titleMeta, + unpackInfo.sectionSize, 0, &installedContentCount); if ((result == 0) && (installedContentCount != 0)) { installedContentIds = _WADMemAlloc(allocator, installedContentCount * sizeof(ESContentId)); @@ -731,11 +733,11 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o result = -3003; goto cleanup; } - result = ES_ListTmdContentsOnCard(workspace.unpackInfo.titleMeta, - workspace.unpackInfo.sectionSize, + result = ES_ListTmdContentsOnCard(unpackInfo.titleMeta, + unpackInfo.sectionSize, installedContentIds, &installedContentCount); if ((result == 0) && - (ES_GetTmd(workspace.unpackInfo.titleMeta->head.titleId, 0, + (ES_GetTmd(unpackInfo.titleMeta->head.titleId, 0, &installedTmdSize) == 0)) { alignedSize = (installedTmdSize + 0x1F) & ~0x1F; installedTitleMeta = _WADMemAlloc(allocator, alignedSize); @@ -743,7 +745,7 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o result = -3003; goto cleanup; } - result = ES_GetTmd(workspace.unpackInfo.titleMeta->head.titleId, + result = ES_GetTmd(unpackInfo.titleMeta->head.titleId, installedTitleMeta, &installedTmdSize); if (result == 0) { importExistingTitle = FALSE; @@ -783,13 +785,13 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o sharedContentHashes = 0; } } - result = ES_ImportTitleInit(workspace.unpackInfo.titleMeta, - workspace.unpackInfo.sectionSize, - workspace.unpackInfo.buffer_0x14, - workspace.unpackInfo.size_0x0c, - workspace.unpackInfo.buffer_0x24, - workspace.unpackInfo.size_0x1c, - workspace.unpackInfo.headerInfo, 1); + result = ES_ImportTitleInit(unpackInfo.titleMeta, + unpackInfo.sectionSize, + unpackInfo.buffer_0x14, + unpackInfo.size_0x0c, + unpackInfo.buffer_0x24, + unpackInfo.size_0x1c, + unpackInfo.headerInfo, 1); if (result != 0) { goto cleanup; } @@ -799,15 +801,15 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o ESContentMeta* content; s32 titleMetaIndex = contentIndex; matchFound = FALSE; - if (workspace.unpackInfo.cidxMode != 0) { - titleMetaIndex = _WADGetCidx(workspace.unpackInfo.contentIndex, contentIndex); + if (unpackInfo.cidxMode != 0) { + titleMetaIndex = _WADGetCidx(unpackInfo.contentIndex, contentIndex); if ((titleMetaIndex < 0) || - ((s32)workspace.unpackInfo.titleMeta->head.numContents <= titleMetaIndex)) { + ((s32)unpackInfo.titleMeta->head.numContents <= titleMetaIndex)) { result = -3001; goto cleanup; } } - content = &workspace.unpackInfo.titleMeta->contents[titleMetaIndex]; + content = &unpackInfo.titleMeta->contents[titleMetaIndex]; if (!importExistingTitle) { for (listIndex = 0; listIndex < installedContentCount; listIndex++) { ESContentMeta* installedContent = &matchingContents[listIndex]; @@ -831,7 +833,7 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o if (matchFound) { importedBytes += (content->size + 0x0F) & ~0x0F; if (processCallback != 0) { - processCallback(importedBytes - workspace.unpackInfo.contentOffset, + processCallback(importedBytes - unpackInfo.contentOffset, callbackTotal, FALSE); } } else { @@ -861,7 +863,7 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o goto cleanup; } contentFd = ES_ImportContentBegin( - workspace.unpackInfo.titleMeta->head.titleId, content->cid); + unpackInfo.titleMeta->head.titleId, content->cid); result = contentFd; if (contentFd < 0) { goto cleanup; @@ -901,7 +903,7 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o break; } readSize = (chunkSize + 0x1F) & ~0x1F; - result = WADReadStream(&workspace.stream, + result = WADReadStream(&stream, &transfer.buffers[bufferIndex], readSize, offset + importedBytes); if ((u32)result != readSize) { @@ -919,7 +921,7 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o importedBytes += chunkSize; sizeRemaining -= chunkSize; if (processCallback != 0) { - processCallback(importedBytes - workspace.unpackInfo.contentOffset, + processCallback(importedBytes - unpackInfo.contentOffset, callbackTotal, FALSE); } bufferIndex ^= 1; @@ -941,7 +943,7 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o } contentImportStarted = FALSE; } - if (workspace.unpackInfo.type == 2) { + if (unpackInfo.type == 2) { importedBytes = (importedBytes + 0x3F) & ~0x3F; } } @@ -952,8 +954,8 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o } titleImportStarted = FALSE; } - if (((flags & 2) != 0) || (workspace.unpackInfo.fileListSize == 0) || - (workspace.unpackInfo.fileCount == 0)) { + if (((flags & 2) != 0) || (unpackInfo.fileListSize == 0) || + (unpackInfo.fileCount == 0)) { goto cleanup; } if (firstBuffer == 0) { @@ -971,15 +973,15 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o } } transferIdValue = _WADGetTransferId(transferId); - fileOffset = workspace.unpackInfo.fileOffset; + fileOffset = unpackInfo.fileOffset; if (((wadHeader.magic & 0xFFFFFF00) == 0x525A4400) && (wadHeader.fileSize == 0x10000)) { - result = _WADVerifySavedataZD(&wadHeader, &workspace.stream, allocator, - offset + workspace.unpackInfo.fileOffset); + result = _WADVerifySavedataZD(&wadHeader, &stream, allocator, + offset + unpackInfo.fileOffset); goto cleanup; } - for (pathFileIndex = 0; pathFileIndex < workspace.unpackInfo.fileCount; pathFileIndex++) { - result = WADReadStream(&workspace.stream, &fileHeaderBuffer, sizeof(backupHeader), + for (pathFileIndex = 0; pathFileIndex < unpackInfo.fileCount; pathFileIndex++) { + result = WADReadStream(&stream, &fileHeaderBuffer, sizeof(backupHeader), offset + fileOffset); if (result != sizeof(backupHeader)) { result = -3005; @@ -992,7 +994,7 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o } result = 0; if (_WADCanImportFile(&backupHeader, transferIdValue, - workspace.unpackInfo.fileNames, transferId) == 0) { + unpackInfo.fileNames, transferId) == 0) { if ((backupHeader.flags[2] == 1) && (backupHeader.fileSize != 0)) { fileOffset = (fileOffset + backupHeader.fileSize + 0x3F) & ~0x3F; @@ -1017,7 +1019,7 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o if (chunkSize > 0x10000) { chunkSize = 0x10000; } - result = WADReadStream(&workspace.stream, &firstBuffer, + result = WADReadStream(&stream, &firstBuffer, (chunkSize + 0x1F) & ~0x1F, offset + fileOffset); readSize = (chunkSize + 0x1F) & ~0x1F; @@ -1038,8 +1040,8 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o importedBytes += chunkSize; sizeRemaining -= chunkSize; if (processCallback != 0) { - processCallback(workspace.unpackInfo.fileListSize + - importedBytes - workspace.unpackInfo.fileOffset, + processCallback(unpackInfo.fileListSize + + importedBytes - unpackInfo.fileOffset, callbackTotal, FALSE); } fileOffset += chunkSize; @@ -1088,17 +1090,17 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o } fileOffset = (fileOffset + 0x3F) & ~0x3F; if ((backupHeader.fileSize == 0) && (processCallback != 0)) { - processCallback(workspace.unpackInfo.fileListSize + - importedBytes - workspace.unpackInfo.fileOffset, + processCallback(unpackInfo.fileListSize + + importedBytes - unpackInfo.fileOffset, callbackTotal, FALSE); } fileOffset += backupHeader.fileSize; fileOffset = (fileOffset + 0x3F) & ~0x3F; } - fileOffset = workspace.unpackInfo.fileOffset; - for (savedataFileIndex = 0; savedataFileIndex < workspace.unpackInfo.fileCount; + fileOffset = unpackInfo.fileOffset; + for (savedataFileIndex = 0; savedataFileIndex < unpackInfo.fileCount; savedataFileIndex++) { - fileReadResult = WADReadStream(&workspace.stream, &fileHeaderBuffer, + fileReadResult = WADReadStream(&stream, &fileHeaderBuffer, sizeof(backupHeader), offset + fileOffset); if (fileReadResult != sizeof(backupHeader)) { @@ -1108,7 +1110,7 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o result = 0; fileOffset = (fileOffset + 0xBF) & ~0x3F; if ((_WADCanImportFile(&backupHeader, transferIdValue, - workspace.unpackInfo.fileNames, transferId) != 0) && + unpackInfo.fileNames, transferId) != 0) && (((result = NANDPrivateGetStatus(backupHeader.name, &fileStatus)) != 0) || ((backupHeader.flags[0] & 0x20) == 0 && @@ -1121,7 +1123,7 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o } cleanup: - _WADFreeMemory(&workspace.unpackInfo, allocator); + _WADFreeMemory(&unpackInfo, allocator); if (firstBuffer != 0) { _WADMemFree(allocator, firstBuffer); } @@ -1147,7 +1149,7 @@ cleanup: NANDClose(&backupFile); } if (streamOpened) { - WADCloseStream(&workspace.stream); + WADCloseStream(&stream); } if (contentImportStarted) { ES_ImportContentEnd(contentFd); @@ -1161,12 +1163,12 @@ cleanup: if (processCallback != 0) { if (importedBytes == 0) { processCallback(0, 0, TRUE); - } else if ((callbackTotal == 0) || (workspace.unpackInfo.fileCount == 0)) { - processCallback(importedBytes - workspace.unpackInfo.contentOffset, callbackTotal, + } else if ((callbackTotal == 0) || (unpackInfo.fileCount == 0)) { + processCallback(importedBytes - unpackInfo.contentOffset, callbackTotal, TRUE); } else { - processCallback(workspace.unpackInfo.fileListSize + - importedBytes - workspace.unpackInfo.fileOffset, + processCallback(unpackInfo.fileListSize + + importedBytes - unpackInfo.fileOffset, callbackTotal, TRUE); } } @@ -1175,9 +1177,9 @@ cleanup: static s32 WAD_815C1288(WADExportLoopArgs* args) { ESFd fd = args->fd; - s32 result = 0; u32 remaining = args->size; u32 bufferIndex = 0; + s32 result = 0; WADImportTransfer* transfer = args->transfer; while ((remaining != 0) && (result == 0)) { @@ -1215,19 +1217,19 @@ s32 WADBackupEx(u64 titleId, u32 ticketId, MEMAllocator* allocator, char* path, WADExportLoopArgs threadArgs; OSThread exportThread; NANDFileInfo savedFile ALIGN32; - WADBackupHeaderBlock headerBlock ALIGN32; + WADBackupHeaderBlock headerBlock ALIGN64; ESContentMask existingContentMask ALIGN32; - ESContentId installedContentIds[520] ALIGN32; - u8 hashContext[0xC0] ALIGN32; + u8 hashContext[0x60] ALIGN64; u8 digest[0x40] ALIGN32; u8 signature[0x40] ALIGN32; u8 deviceCertificate[0x180] ALIGN32; ESCertSignature signerCertificate ALIGN32; - u8 encryptionIv[0x10] ALIGN32; + u8 encryptionIv[0x10] ALIGN32 = {0}; void* outputBuffer = 0; void* encryptionBuffer = 0; WADThreadStack* threadStack = 0; u32 flags = ticketId; + u64 savedataTitleId = 0; u32 titleMetaSize = 0; u32 contentDataSize = 0; u32 fileDataSize = 0; @@ -1257,10 +1259,6 @@ s32 WADBackupEx(u64 titleId, u32 ticketId, MEMAllocator* allocator, char* path, if (flags == 0) { flags = 0x0F; } - memset(&transfer, 0, sizeof(transfer)); - memset(&headerBlock, 0, sizeof(headerBlock)); - memset(&existingContentMask, 0, sizeof(existingContentMask)); - memset(encryptionIv, 0, sizeof(encryptionIv)); if (((flags & 3) == 0) || ((u32)(titleId >> 32) == 1) || (allocator == 0) || (sizeOut == 0)) { result = -3000; @@ -1299,11 +1297,6 @@ s32 WADBackupEx(u64 titleId, u32 ticketId, MEMAllocator* allocator, char* path, result = -3002; goto cleanup; } - result = ES_ListTitleContentsOnCard(titleId, installedContentIds, - &installedContentCount); - if (result != 0) { - goto cleanup; - } result = _WADCheckContents(titleMeta, &existingContentMask); if (result != 0) { goto cleanup; @@ -1319,7 +1312,7 @@ s32 WADBackupEx(u64 titleId, u32 ticketId, MEMAllocator* allocator, char* path, } if (strncmp(currentDirectory, "/title/", 7) == 0) { sscanf(currentDirectory, "/title/%x/%x", ¤tTitleHigh, ¤tTitleLow); - headerBlock.header.titleId = ((u64)currentTitleHigh << 32) | currentTitleLow; + savedataTitleId = ((u64)currentTitleHigh << 32) | currentTitleLow; } result = _WADBackupGetFiles(0, flags, allocator, &fileCount, 0); if (result != 0) { @@ -1364,6 +1357,8 @@ s32 WADBackupEx(u64 titleId, u32 ticketId, MEMAllocator* allocator, char* path, if (result != 0) { goto cleanup; } + memset(&headerBlock, 0, sizeof(headerBlock)); + headerBlock.header.titleId = savedataTitleId; headerBlock.header.hdrSize = sizeof(WADBackupHeader); headerBlock.header.wadType[0] = 'B'; headerBlock.header.wadType[1] = 'k'; @@ -1784,360 +1779,6 @@ static void _WADMemFree(MEMAllocator* allocator, void* buffer) { } #pragma dont_inline reset -s32 _WADGetCidxCount(const ESContentMask* contentMask) { - s32 count = 0; - u32 bitIndex = 0; - u32 groupIndex; - - for (groupIndex = 0; groupIndex < 0x80; groupIndex++) { - if ((contentMask->data[bitIndex >> 3] & (1 << (bitIndex & 7))) != 0) { - count++; - } - bitIndex++; - if ((contentMask->data[bitIndex >> 3] & (1 << (bitIndex & 7))) != 0) { - count++; - } - bitIndex++; - if ((contentMask->data[bitIndex >> 3] & (1 << (bitIndex & 7))) != 0) { - count++; - } - bitIndex++; - if ((contentMask->data[bitIndex >> 3] & (1 << (bitIndex & 7))) != 0) { - count++; - } - bitIndex++; - } - return count; -} - -static s32 _WADGetCidx(const ESContentMask* contentMask, u32 contentNumber) { - u32 remaining = contentNumber + 1; - s32 bitIndex = 0; - s32 nextIndex; - - for (bitIndex = 0; bitIndex < 0x200; bitIndex += 4) { - if ((contentMask->data[bitIndex >> 3] & (1 << (bitIndex & 7))) != 0) { - remaining--; - } - if (remaining == 0) { - return bitIndex; - } - nextIndex = bitIndex + 1; - if ((contentMask->data[nextIndex >> 3] & (1 << (nextIndex & 7))) != 0) { - remaining--; - } - if (remaining == 0) { - return nextIndex; - } - nextIndex = bitIndex + 2; - if ((contentMask->data[nextIndex >> 3] & (1 << (nextIndex & 7))) != 0) { - remaining--; - } - if (remaining == 0) { - return nextIndex; - } - nextIndex = bitIndex + 3; - if ((contentMask->data[nextIndex >> 3] & (1 << (nextIndex & 7))) != 0) { - remaining--; - } - if (remaining == 0) { - return nextIndex; - } - } - return -1; -} - -#pragma dont_inline on -static s32 _WADGetTransferId(void* transferId) { - NANDFileInfo fileInfo; - BOOL valid; - s32 result; - - valid = FALSE; - result = NANDPrivateOpen("/shared2/succession/transfer.id", &fileInfo, 1); - if (result == 0) { - valid = TRUE; - result = NANDRead(&fileInfo, transferId, 0x20); - if (result != 0x20) { - valid = FALSE; - } - NANDClose(&fileInfo); - } - return valid; -} - -static s32 _WADCanImportFile(const WADFileHeader* fileHeader, u32 transferId, const void* fileNames, - const void* transferIdBuffer) { - s32 result; - - result = strncmp(fileHeader->name, "nocopy", 6); - if (result == 0) { - return FALSE; - } - result = strncmp(fileHeader->name, "notransfer", 10); - if (result == 0) { - if ((transferId == 0) || (fileNames == 0) || (transferIdBuffer == 0)) { - return FALSE; - } - result = memcmp(fileNames, transferIdBuffer, 6); - return result == 0; - } - return TRUE; -} - -static s32 _WADBackupGetFiles(const char* directoryPath, u32 flags, MEMAllocator* allocator, - u32* fileCount, WADBackupFileHeader* files) { - char currentDirectory[NAND_MAX_PATH] ALIGN32; - char fullFilePath[NAND_MAX_PATH] ALIGN32; - char noCopyPath[NAND_MAX_PATH] ALIGN32; - char bannerPath[NAND_MAX_PATH] ALIGN32; - char* names = 0; - char* nameList = 0; - u32 nameCount = 0; - u32 nameIndex; - u32 totalFiles = 0; - u32 childFileCount; - u8 fileType; - NANDFileInfo fileInfo; - NANDStatus status; - s32 result; - - if ((allocator == 0) || (fileCount == 0)) { - return -3000; - } - result = NANDGetCurrentDir(currentDirectory); - if (result != 0) { - return result; - } - if (directoryPath == 0) { - directoryPath = "."; - } - result = NANDPrivateReadDir(directoryPath, 0, &nameCount); - if (result == -1) { - *fileCount = 0; - return 0; - } - if (result != 0) { - return result; - } - if (nameCount != 0) { - nameList = _WADMemAlloc(allocator, nameCount * 0x40); - if (nameList == 0) { - return -3003; - } - names = nameList; - result = NANDPrivateReadDir(directoryPath, nameList, &nameCount); - if (result != 0) { - goto cleanup; - } - for (nameIndex = 0; nameIndex < nameCount; nameIndex++) { - char* name = names; - WADBackupFileHeader* file = 0; - u32 fileSize = 0; - BOOL closeFile = FALSE; - - if ((flags & 4) != 0) { - if (directoryPath[0] == '\0') { - snprintf(fullFilePath, sizeof(fullFilePath), "/%s", name); - } else { - snprintf(fullFilePath, sizeof(fullFilePath), "%s/%s", directoryPath, name); - } - } else if (directoryPath[0] == '\0') { - snprintf(fullFilePath, sizeof(fullFilePath), "%s/%s", currentDirectory, name); - } else if (directoryPath[0] == '/') { - snprintf(fullFilePath, sizeof(fullFilePath), "%s/%s", directoryPath, name); - } else { - snprintf(fullFilePath, sizeof(fullFilePath), "%s/%s/%s", currentDirectory, - directoryPath, name); - } - result = NANDPrivateGetType(fullFilePath, &fileType); - if (result != 0) { - break; - } - if ((fileType == 2) && ((flags & 8) == 0)) { - goto nextName; - } - if (fileType == 1) { - result = NANDPrivateOpen(fullFilePath, &fileInfo, 1); - if (result != 0) { - break; - } - closeFile = TRUE; - result = NANDGetLength(&fileInfo, &fileSize); - if (result != 0) { - NANDClose(&fileInfo); - break; - } - result = NANDClose(&fileInfo); - closeFile = FALSE; - if (result != 0) { - break; - } - } else if (fileType != 2) { - goto nextName; - } - result = NANDPrivateGetStatus(fullFilePath, &status); - if (result != 0) { - break; - } - if ((flags & 0x10) == 0) { - if ((flags & 4) == 0) { - if (strcmp(directoryPath, ".") == 0) { - snprintf(noCopyPath, sizeof(noCopyPath), "%s/%s", currentDirectory, - "nocopy"); - snprintf(bannerPath, sizeof(bannerPath), "%s/%s", currentDirectory, - "banner.bin"); - if ((strcmp(fullFilePath, noCopyPath) == 0) && (fileType == 2)) { - goto nextName; - } - if ((strcmp(fullFilePath, bannerPath) == 0) && (fileType == 1)) { - goto nextName; - } - } - } else { - if ((strcmp(fullFilePath, "nocopy") == 0) && (fileType == 2)) { - goto nextName; - } - if ((strcmp(fullFilePath, "banner.bin") == 0) && (fileType == 1)) { - goto nextName; - } - } - } - if (files != 0) { - file = &files[totalFiles]; - file->magic = 0x3ADF17E; - file->fileSize = fileSize; - file->flags[0] = status.permission; - file->flags[1] = status.attribute; - file->flags[2] = fileType; - strncpy(file->path, fullFilePath, 0x40); - } - totalFiles++; - if (fileType == 2) { - childFileCount = 0; - result = _WADBackupGetFiles(fullFilePath, flags, allocator, &childFileCount, - files == 0 ? 0 : &files[totalFiles]); - if (result != 0) { - break; - } - totalFiles += childFileCount; - } - -nextName: - names += strlen(names) + 1; - if (closeFile) { - NANDClose(&fileInfo); - } - } - } - if (result == 0) { - *fileCount = totalFiles; - } - -cleanup: - if (nameList != 0) { - _WADMemFree(allocator, nameList); - } - return result; -} - -static s32 _WADBackupGetSize(u32 flags, ESTmdView* titleMeta, ESContentMask* contentMask, - u32 fileCount, WADBackupFileHeader* files, u32* titleMetaSize, - u32* contentDataSize, u32* fileDataSize, u32* totalSize) { - u32 titleSize = 0x80; - u32 contentSize = 0; - u32 filesSize = 0; - u32 selectedCount; - u32 contentIndex; - u32 fileIndex; - s32 result = 0; - - if ((titleMeta != 0) && (titleMetaSize != 0)) { - if (((flags & 1) != 0) || ((flags & 0x20) != 0)) { - result = ES_GetTmdSizeFromView(titleMeta, titleMetaSize); - if (result != 0) { - return result; - } - titleSize += (*titleMetaSize + 0x3F) & ~0x3F; - } - if ((flags & 1) != 0) { - selectedCount = _WADGetCidxCount(contentMask); - for (contentIndex = 0; contentIndex < selectedCount; contentIndex++) { - s32 tmdIndex = _WADGetCidx(contentMask, contentIndex); - if ((tmdIndex < 0) || (titleMeta->head.numContents <= tmdIndex)) { - return -3009; - } - contentSize += (titleMeta->contents[tmdIndex].size + 0x3F) & ~0x3F; - } - } - } else if ((titleMeta == 0) && (titleMetaSize != 0)) { - *titleMetaSize = 0; - } - if ((fileCount != 0) && (files != 0)) { - for (fileIndex = 0; fileIndex < fileCount; fileIndex++) { - filesSize += 0x80 + ((files[fileIndex].fileSize + 0x3F) & ~0x3F); - } - } - if (titleMetaSize != 0) { - *titleMetaSize = titleSize; - } - if (contentDataSize != 0) { - *contentDataSize = contentSize; - } - if (fileDataSize != 0) { - *fileDataSize = filesSize; - } - if (totalSize != 0) { - *totalSize = titleSize + contentSize + filesSize; - } - return result; -} -#pragma dont_inline reset - - -static void _WADFreeMemory(WADUnpackInfo* info, MEMAllocator* allocator) { - if ((info->size_0x18 != 0) && (info->buffer_0x14 != 0)) { - _WADMemFree(allocator, info->buffer_0x14); - info->buffer_0x14 = 0; - } - if ((info->size_0x28 != 0) && (info->buffer_0x24 != 0)) { - _WADMemFree(allocator, info->buffer_0x24); - info->buffer_0x24 = 0; - } - if ((info->size_0x38 != 0) && (info->buffer_0x34 != 0)) { - _WADMemFree(allocator, info->buffer_0x34); - info->buffer_0x34 = 0; - } - if ((info->titleMetaSize != 0) && (info->titleMeta != 0)) { - _WADMemFree(allocator, info->titleMeta); - info->titleMeta = 0; - } -} - -static void _WADRandPad(void* buffer, u32 size) { - u32* words = buffer; - u32 wordCount = size >> 2; - u32 remainder = size & 3; - u32 wordIndex; - - if (size == 0) { - return; - } - for (wordIndex = 0; wordIndex < wordCount; wordIndex++) { - words[wordIndex] = ((u32)rand() << 2) | ((u32)rand() << 17) | (rand() & 3); - } - if (remainder != 0) { - u32 randomWord = ((u32)rand() << 2) | ((u32)rand() << 17) | (rand() & 3); - u8* tail = (u8*)&words[wordCount]; - u8* randomBytes = (u8*)&randomWord; - u32 byteIndex; - - for (byteIndex = 0; byteIndex < remainder; byteIndex++) { - tail[byteIndex] = randomBytes[byteIndex]; - } - } -} - typedef struct WADContentPath { const char* name; void* handle; @@ -2152,6 +1793,13 @@ typedef union WADFileInfoView { } fields; } WADFileInfoView; +extern const u8 ca_ppki[]; +extern const u8 ms_ppki[]; +extern const u8 ca_dpki[]; +extern const u8 ms_dpki[]; +extern s32 SHA1Reset(void* context); +extern s32 SHA1Result(void* context, void* digest); + s32 WADOpenStream(WADLocation location, const char* path, WADStream* stream, u32 write, u32 offset) { stream->location = location; @@ -2211,7 +1859,11 @@ s32 WADOpenStream(WADLocation location, const char* path, WADStream* stream, u32 { s32 result; if (write == 0) { - return NANDPrivateOpen(path, &stream->handle.nand, 1) == 0 ? 0 : -3004; + result = NANDPrivateOpen(path, &stream->handle.nand, 1); + if (result != 0) { + return -3004; + } + return result; } result = NANDPrivateOpen(path, &stream->handle.nand, 3); if (result == -12) { @@ -2230,15 +1882,17 @@ s32 WADOpenStream(WADLocation location, const char* path, WADStream* stream, u32 u32 fileSize; fileSize = NANDSeek(&stream->handle.nand, 0, 2); - if (fileSize < offset) { + if (fileSize >= offset) { return -3004; } memset(zeroes, 0, sizeof(zeroes)); fileSize = offset - fileSize; while (fileSize != 0) { - u32 writeSize = fileSize; - if (writeSize > sizeof(zeroes)) { + u32 writeSize; + if (fileSize > sizeof(zeroes)) { writeSize = sizeof(zeroes); + } else { + writeSize = fileSize; } if (NANDWrite(&stream->handle.nand, zeroes, writeSize) != writeSize) { return -3004; @@ -2353,55 +2007,70 @@ void WADCloseStream(WADStream* stream) { s32 WADWriteStream(WADStream* stream, void* buffer, u32 size) { s32 result = size; - - if ((stream == 0) || (buffer == 0)) { + if (stream == 0 || buffer == 0) { result = -3000; - } else if (stream->location != WAD_LOCATION_NAND) { - if (stream->location == (WADLocation)0) { + } else { + switch (stream->location) { + case (WADLocation)0: memcpy(stream->handle.memoryBase, buffer, size); - } else if ((stream->location > WAD_LOCATION_NAND) && - (stream->location < WAD_LOCATION_CNT_DVD)) { + break; + case WAD_LOCATION_NAND: + result = NANDWrite(&stream->handle.nand, buffer, size); + break; + case WAD_LOCATION_SD_CARD: if (stream->handle.fa == 0) { result = -3000; } else { result = FAFwrite(buffer, 1, size, stream->handle.fa); } - } else { + break; + case WAD_LOCATION_DVD: + case WAD_LOCATION_CNT_DVD: + case WAD_LOCATION_CNT_NAND: + default: result = -3000; + break; } - } else { - result = NANDWrite(&stream->handle.nand, buffer, size); } return result; } s32 WADSeekStream(WADStream* stream, u32 offset, s32 origin) { s32 result; - - if ((stream == 0) || (origin < 0) || (origin > 2)) { + WADFileInfoView info; + if (stream == 0 || origin < 0 || origin > 2) { return -3000; } - if (stream->location == WAD_LOCATION_SD_CARD) { - WADFileInfoView info; + switch (stream->location) { + case WAD_LOCATION_CNT_DVD: + result = contentSeekDVD(&stream->handle.contentDvd, offset, origin); + break; + case WAD_LOCATION_CNT_NAND: + result = contentSeekNAND(&stream->handle.contentNand, offset, origin); + break; + case WAD_LOCATION_NAND: + result = NANDSeek(&stream->handle.nand, offset, origin); + break; + case WAD_LOCATION_SD_CARD: if (stream->handle.fa == 0) { return -3000; } result = FAFseek(stream->handle.fa, offset, origin); - if ((result == 0) && (FAFinfo(stream->handle.fa, &info.info) == 0)) { + if (result != 0) { + break; + } + result = FAFinfo(stream->handle.fa, &info.info); + if (result == 0) { result = info.fields.fileSize; } - return result; + break; + case (WADLocation)0: + case WAD_LOCATION_DVD: + default: + result = -3000; + break; } - if (stream->location < WAD_LOCATION_SD_CARD) { - if (stream->location > WAD_LOCATION_DVD) { - return NANDSeek(&stream->handle.nand, offset, origin); - } - } else if (stream->location == WAD_LOCATION_CNT_NAND) { - return contentSeekNAND(&stream->handle.contentNand, offset, origin); - } else if (stream->location < WAD_LOCATION_CNT_NAND) { - return contentSeekDVD(&stream->handle.contentDvd, offset, origin); - } - return -3000; + return result; } static s32 _WADGetTitleVer(WADHeader* header, ESTitleId* titleId, u16* titleVersion, s32 type, @@ -2731,6 +2400,7 @@ static s32 _WADUnpackBackup(WADHeader* header, WADStream* stream, WADUnpackInfo* s32 result; u32 sectionOffset; u32 alignedSize; + u32 titleMetaSize; if (backupHeader->wadVersion != 1) { return -3001; @@ -2749,18 +2419,19 @@ static s32 _WADUnpackBackup(WADHeader* header, WADStream* stream, WADUnpackInfo* } info->cidxMode = backupHeader->wadVersion; - if (backupHeader->wadVersion >= 1) { + if (info->cidxMode >= 1) { info->contentIndex = &backupHeader->cidx; } info->fileNames = backupHeader->deviceMac; sectionOffset = (backupHeader->hdrSize + WAD_STREAM_ALIGNMENT - 1) & ~(WAD_STREAM_ALIGNMENT - 1); - if (backupHeader->tmdSize != 0) { + titleMetaSize = backupHeader->tmdSize; + if (titleMetaSize != 0) { if ((flags & 4) == 0) { - info->sectionSize = backupHeader->tmdSize; + info->sectionSize = titleMetaSize; info->sectionOffset = sectionOffset; - alignedSize = WAD_ALIGN32(backupHeader->tmdSize); + alignedSize = WAD_ALIGN32(titleMetaSize); info->titleMeta = _WADMemAlloc(allocator, alignedSize); if (info->titleMeta == 0) { return -3003; @@ -2800,6 +2471,595 @@ static s32 _WADUnpackBackup(WADHeader* header, WADStream* stream, WADUnpackInfo* return 0; } +s32 _WADGetCidxCount(const ESContentMask* contentMask) { + s32 count = 0; + u32 bitIndex = 0; + u32 groupIndex; + + for (groupIndex = 0; groupIndex < 0x80; groupIndex++) { + if ((contentMask->data[bitIndex >> 3] & (1 << (bitIndex & 7))) != 0) { + count++; + } + bitIndex++; + if ((contentMask->data[bitIndex >> 3] & (1 << (bitIndex & 7))) != 0) { + count++; + } + bitIndex++; + if ((contentMask->data[bitIndex >> 3] & (1 << (bitIndex & 7))) != 0) { + count++; + } + bitIndex++; + if ((contentMask->data[bitIndex >> 3] & (1 << (bitIndex & 7))) != 0) { + count++; + } + bitIndex++; + } + return count; +} + +static s32 _WADGetCidx(const ESContentMask* contentMask, u32 contentNumber) { + u32 remaining = contentNumber + 1; + s32 bitIndex = 0; + s32 nextIndex; + + for (bitIndex = 0; bitIndex < 0x200; bitIndex += 4) { + if ((contentMask->data[bitIndex >> 3] & (1 << (bitIndex & 7))) != 0) { + remaining--; + } + if (remaining == 0) { + return bitIndex; + } + nextIndex = bitIndex + 1; + if ((contentMask->data[nextIndex >> 3] & (1 << (nextIndex & 7))) != 0) { + remaining--; + } + if (remaining == 0) { + return nextIndex; + } + nextIndex = bitIndex + 2; + if ((contentMask->data[nextIndex >> 3] & (1 << (nextIndex & 7))) != 0) { + remaining--; + } + if (remaining == 0) { + return nextIndex; + } + nextIndex = bitIndex + 3; + if ((contentMask->data[nextIndex >> 3] & (1 << (nextIndex & 7))) != 0) { + remaining--; + } + if (remaining == 0) { + return nextIndex; + } + } + return -1; +} + +#pragma dont_inline on +static s32 _WADGetTransferId(void* transferId) { + NANDFileInfo fileInfo; + BOOL valid; + s32 result; + + valid = FALSE; + result = NANDPrivateOpen("/shared2/succession/transfer.id", &fileInfo, 1); + if (result == 0) { + valid = TRUE; + result = NANDRead(&fileInfo, transferId, 0x20); + if (result != 0x20) { + valid = FALSE; + } + NANDClose(&fileInfo); + } + return valid; +} + +static s32 _WADCanImportFile(const WADFileHeader* fileHeader, u32 transferId, const void* fileNames, + const void* transferIdBuffer) { + s32 result; + + result = strncmp(fileHeader->name, "nocopy", 6); + if (result == 0) { + return FALSE; + } + result = strncmp(fileHeader->name, "notransfer", 10); + if (result == 0) { + if ((transferId == 0) || (fileNames == 0) || (transferIdBuffer == 0)) { + return FALSE; + } + result = memcmp(fileNames, transferIdBuffer, 6); + return result == 0; + } + return TRUE; +} + +static s32 _WADBackupGetFiles(const char* directoryPath, u32 flags, MEMAllocator* allocator, + u32* fileCount, WADBackupFileHeader* files) { + char currentDirectory[NAND_MAX_PATH] ALIGN32; + char fullFilePath[NAND_MAX_PATH] ALIGN32; + char noCopyPath[NAND_MAX_PATH] ALIGN32; + char bannerPath[NAND_MAX_PATH] ALIGN32; + char* names = 0; + char* nameList = 0; + u32 nameCount = 0; + u32 nameIndex; + u32 totalFiles = 0; + u32 childFileCount; + u8 fileType; + NANDFileInfo fileInfo; + NANDStatus status; + s32 result; + + if ((allocator == 0) || (fileCount == 0)) { + return -3000; + } + result = NANDGetCurrentDir(currentDirectory); + if (result != 0) { + return result; + } + if (directoryPath == 0) { + directoryPath = "."; + } + result = NANDPrivateReadDir(directoryPath, 0, &nameCount); + if (result == -1) { + *fileCount = 0; + return 0; + } + if (result != 0) { + return result; + } + if (nameCount != 0) { + nameList = _WADMemAlloc(allocator, nameCount * 0x40); + if (nameList == 0) { + return -3003; + } + names = nameList; + result = NANDPrivateReadDir(directoryPath, nameList, &nameCount); + if (result != 0) { + goto cleanup; + } + for (nameIndex = 0; nameIndex < nameCount; nameIndex++) { + char* name = names; + WADBackupFileHeader* file = 0; + u32 fileSize = 0; + BOOL closeFile = FALSE; + + if ((flags & 4) != 0) { + if (directoryPath[0] == '\0') { + snprintf(fullFilePath, sizeof(fullFilePath), "/%s", name); + } else { + snprintf(fullFilePath, sizeof(fullFilePath), "%s/%s", directoryPath, name); + } + } else if (directoryPath[0] == '\0') { + snprintf(fullFilePath, sizeof(fullFilePath), "%s/%s", currentDirectory, name); + } else if (directoryPath[0] == '/') { + snprintf(fullFilePath, sizeof(fullFilePath), "%s/%s", directoryPath, name); + } else { + snprintf(fullFilePath, sizeof(fullFilePath), "%s/%s/%s", currentDirectory, + directoryPath, name); + } + result = NANDPrivateGetType(fullFilePath, &fileType); + if (result != 0) { + break; + } + if ((fileType == 2) && ((flags & 8) == 0)) { + goto nextName; + } + if (fileType == 1) { + result = NANDPrivateOpen(fullFilePath, &fileInfo, 1); + if (result != 0) { + break; + } + closeFile = TRUE; + result = NANDGetLength(&fileInfo, &fileSize); + if (result != 0) { + NANDClose(&fileInfo); + break; + } + result = NANDClose(&fileInfo); + closeFile = FALSE; + if (result != 0) { + break; + } + } else if (fileType != 2) { + goto nextName; + } + result = NANDPrivateGetStatus(fullFilePath, &status); + if (result != 0) { + break; + } + if ((flags & 0x10) == 0) { + if ((flags & 4) == 0) { + if (strcmp(directoryPath, ".") == 0) { + snprintf(noCopyPath, sizeof(noCopyPath), "%s/%s", currentDirectory, + "nocopy"); + snprintf(bannerPath, sizeof(bannerPath), "%s/%s", currentDirectory, + "banner.bin"); + if ((strcmp(fullFilePath, noCopyPath) == 0) && (fileType == 2)) { + goto nextName; + } + if ((strcmp(fullFilePath, bannerPath) == 0) && (fileType == 1)) { + goto nextName; + } + } + } else { + if ((strcmp(fullFilePath, "nocopy") == 0) && (fileType == 2)) { + goto nextName; + } + if ((strcmp(fullFilePath, "banner.bin") == 0) && (fileType == 1)) { + goto nextName; + } + } + } + if (files != 0) { + file = &files[totalFiles]; + file->magic = 0x3ADF17E; + file->fileSize = fileSize; + file->flags[0] = status.permission; + file->flags[1] = status.attribute; + file->flags[2] = fileType; + strncpy(file->path, fullFilePath, 0x40); + } + totalFiles++; + if (fileType == 2) { + childFileCount = 0; + result = _WADBackupGetFiles(fullFilePath, flags, allocator, &childFileCount, + files == 0 ? 0 : &files[totalFiles]); + if (result != 0) { + break; + } + totalFiles += childFileCount; + } + +nextName: + names += strlen(names) + 1; + if (closeFile) { + NANDClose(&fileInfo); + } + } + } + if (result == 0) { + *fileCount = totalFiles; + } + +cleanup: + if (nameList != 0) { + _WADMemFree(allocator, nameList); + } + return result; +} + +static s32 _WADBackupGetSize(u32 flags, ESTmdView* titleMeta, ESContentMask* contentMask, + u32 fileCount, WADBackupFileHeader* files, u32* titleMetaSize, + u32* contentDataSize, u32* fileDataSize, u32* totalSize) { + u32 titleSize = 0x80; + u32 contentSize = 0; + u32 filesSize = 0; + u32 selectedCount; + u32 contentIndex; + u32 fileIndex; + s32 result = 0; + + if ((titleMeta != 0) && (titleMetaSize != 0)) { + if (((flags & 1) != 0) || ((flags & 0x20) != 0)) { + result = ES_GetTmdSizeFromView(titleMeta, titleMetaSize); + if (result != 0) { + return result; + } + titleSize += (*titleMetaSize + 0x3F) & ~0x3F; + } + if ((flags & 1) != 0) { + selectedCount = _WADGetCidxCount(contentMask); + for (contentIndex = 0; contentIndex < selectedCount; contentIndex++) { + s32 tmdIndex = _WADGetCidx(contentMask, contentIndex); + if ((tmdIndex < 0) || (titleMeta->head.numContents <= tmdIndex)) { + return -3009; + } + contentSize += ((u32)titleMeta->contents[tmdIndex].size + 0x3F) & ~0x3F; + } + } + } else if ((titleMeta == 0) && (titleMetaSize != 0)) { + *titleMetaSize = 0; + } + if ((fileCount != 0) && (files != 0)) { + for (fileIndex = 0; fileIndex < fileCount; fileIndex++) { + filesSize += 0x80; + filesSize += (files[fileIndex].fileSize + 0x3F) & ~0x3F; + } + } + if (titleMetaSize != 0) { + *titleMetaSize = titleSize; + } + if (contentDataSize != 0) { + *contentDataSize = contentSize; + } + if (fileDataSize != 0) { + *fileDataSize = filesSize; + } + if (totalSize != 0) { + *totalSize = titleSize + contentSize + filesSize; + } + return result; +} +#pragma dont_inline reset + + +static void _WADFreeMemory(WADUnpackInfo* info, MEMAllocator* allocator) { + if ((info->size_0x18 != 0) && (info->buffer_0x14 != 0)) { + _WADMemFree(allocator, info->buffer_0x14); + info->buffer_0x14 = 0; + } + if ((info->size_0x28 != 0) && (info->buffer_0x24 != 0)) { + _WADMemFree(allocator, info->buffer_0x24); + info->buffer_0x24 = 0; + } + if ((info->size_0x38 != 0) && (info->buffer_0x34 != 0)) { + _WADMemFree(allocator, info->buffer_0x34); + info->buffer_0x34 = 0; + } + if ((info->titleMetaSize != 0) && (info->titleMeta != 0)) { + _WADMemFree(allocator, info->titleMeta); + info->titleMeta = 0; + } +} + +static void _WADRandPad(void* buffer, u32 size) { + u32* words = buffer; + u32 wordCount = size >> 2; + u32 remainder = size & 3; + u32 wordIndex; + + if (size == 0) { + return; + } + for (wordIndex = 0; wordIndex < wordCount; wordIndex++) { + *words++ = (((u32)rand() << 17) | ((u32)rand() << 2)) | (rand() & 3); + } + if (remainder != 0) { + u8* tail = (u8*)words; + u8* randomBytes = (u8*)&wordCount; + u32 byteIndex; + + wordCount = (((u32)rand() << 17) | ((u32)rand() << 2)) | (rand() & 3); + for (byteIndex = 0; byteIndex < remainder; byteIndex++) { + *tail++ = *randomBytes++; + } + } +} + +static s32 WAD_815C43E0(WADHashThreadArgs* args) { + void* context = args->context; + s32 result = 0; + u32 remaining = args->size; + u32 bufferIndex = 0; + WADImportTransfer* transfer = args->transfer; + + while ((remaining != 0) && (result == 0)) { + u32 size = remaining; + OSMutex* mutex; + + if (remaining > transfer->chunkSize) { + size = transfer->chunkSize; + } + mutex = &transfer->mutex[bufferIndex]; + OSLockMutex(mutex); + while (transfer->ready[bufferIndex] == 0) { + OSWaitCond(&transfer->signalCond[bufferIndex], mutex); + } + result = SHA1Input(context, transfer->buffers[bufferIndex], size); + transfer->ready[bufferIndex] = 0; + if (result != 0) { + transfer->error = 1; + } + OSUnlockMutex(mutex); + OSSignalCond(&transfer->waitCond[bufferIndex]); + remaining -= size; + bufferIndex ^= 1; + } + return result; +} + +static s32 _WADHash(WADStream* stream, u32 offset, u32 size, void* context, void* buffer, + u32 chunkSize, void* secondBuffer, void* threadStack, u32 threadStackSize) { + s32 result = 0; + + if ((stream == 0) || (context == 0) || (buffer == 0)) { + return -3000; + } + if (size != WAD_ALIGN32(size)) { + return -3000; + } + if (((u32)buffer & 0x1F) != 0) { + return -3007; + } + if (chunkSize != WAD_ALIGN32(chunkSize)) { + return -3000; + } + if ((secondBuffer == 0) || (threadStack == 0)) { + u32 completed = 0; + + while (size != 0) { + void* readBuffer = buffer; + u32 readSize = size; + + if (size > chunkSize) { + readSize = chunkSize; + } + result = WADReadStream(stream, &readBuffer, readSize, offset + completed); + if ((u32)result != readSize) { + result = -3005; + break; + } + result = SHA1Input(context, readBuffer, readSize); + if (result != 0) { + break; + } + completed += readSize; + size -= readSize; + } + } else { + WADImportTransfer transfer; + WADHashThreadArgs args; + OSThread thread; + s32 threadResult = 0; + u32 completed = 0; + u32 bufferIndex = 0; + BOOL readFailed = FALSE; + + WAD_815C4A2C(&transfer, buffer, secondBuffer, chunkSize); + args.context = context; + args.size = size; + args.transfer = &transfer; + if (!OSCreateThread(&thread, (void* (*)(void*))WAD_815C43E0, &args, + (u8*)threadStack + threadStackSize, threadStackSize, + OSGetThreadPriority(OSGetCurrentThread()), 0)) { + return -3009; + } + OSResumeThread(&thread); + while ((size != 0) && (transfer.error == 0)) { + void* readBuffer; + u32 readSize = size; + OSMutex* mutex = &transfer.mutex[bufferIndex]; + + if (size > chunkSize) { + readSize = chunkSize; + } + OSLockMutex(mutex); + while ((transfer.ready[bufferIndex] != 0) && (transfer.error == 0)) { + OSWaitCond(&transfer.waitCond[bufferIndex], mutex); + } + if (transfer.error != 0) { + OSUnlockMutex(mutex); + break; + } + readBuffer = transfer.buffers[bufferIndex]; + result = WADReadStream(stream, &readBuffer, readSize, offset + completed); + if ((u32)result != readSize) { + OSMutex* otherMutex = &transfer.mutex[bufferIndex ^ 1]; + + OSLockMutex(otherMutex); + OSCancelThread(&thread); + OSJoinThread(&thread, 0); + OSUnlockMutex(otherMutex); + OSUnlockMutex(mutex); + result = -3005; + readFailed = TRUE; + break; + } + transfer.ready[bufferIndex] = readSize; + OSUnlockMutex(mutex); + OSSignalCond(&transfer.signalCond[bufferIndex]); + completed += readSize; + size -= readSize; + bufferIndex ^= 1; + } + if (!readFailed) { + if (!OSJoinThread(&thread, &threadResult)) { + result = -3009; + } else { + result = threadResult; + } + } + } + return result; +} + +s32 WADVerify(WADStream* stream, MEMAllocator* allocator, u32 offset, u32 size) { + void* readBuffer ALIGN64 = 0; + u8 digest[0x40] ALIGN64; + u8 hashContext[0x60] ALIGN64; + void* verificationBuffer = 0; + WADBackupSignature* backupSignature; + WADVerificationCertificateBundle* certificateBundle; + s32 result; + u32 signatureOffset; + + if (size != WAD_ALIGN32(size)) { + result = -3000; + } else if (size <= 0x340) { + result = -3000; + } else { + readBuffer = _WADMemAlloc(allocator, 0x8000); + if (readBuffer == 0) { + result = -3003; + } else if (((u32)readBuffer & 0x3F) != 0) { + result = -3007; + } else { + u32 hashSize = size - 0x340; + + signatureOffset = offset + hashSize; + SHA1Reset(hashContext); + result = _WADHash(stream, offset, hashSize, hashContext, + readBuffer, 0x8000, 0, 0, 0x1000); + if (result == 0) { + result = SHA1Result(hashContext, digest); + if (result == 0) { + result = WADReadStream(stream, &readBuffer, 0x340, + signatureOffset); + if (result == 0x340) { + verificationBuffer = _WADMemAlloc(allocator, 0xF80); + if (verificationBuffer == 0) { + result = -3003; + } else { + backupSignature = readBuffer; + certificateBundle = verificationBuffer; + memcpy(certificateBundle->caProduction, ca_ppki, 0x400); + memcpy(certificateBundle->msProduction, ms_ppki, 0x240); + memcpy(certificateBundle->caDevelopment, ca_dpki, 0x400); + memcpy(certificateBundle->msDevelopment, ms_dpki, 0x240); + memcpy(certificateBundle->firstCertificate, + ((WADBackupSignature*)readBuffer)->firstCertificate, 0x180); + memcpy(certificateBundle->secondCertificate, + ((WADBackupSignature*)readBuffer)->secondCertificate, 0x180); + result = ES_VerifySign(digest, 0x14, + readBuffer, verificationBuffer, + 0xF80); + } + } else { + result = -3005; + } + } + } + } + } + + if (readBuffer != 0) { + _WADMemFree(allocator, readBuffer); + } + if (verificationBuffer != 0) { + _WADMemFree(allocator, verificationBuffer); + } + return result; +} + +void WAD_815C4A2C(WADImportTransfer* transfer, void* firstBuffer, void* secondBuffer, + u32 chunkSize) { + transfer->chunkSize = chunkSize; + transfer->error = 0; + transfer->ready[1] = 0; + transfer->ready[0] = 0; + transfer->buffers[0] = firstBuffer; + transfer->buffers[1] = secondBuffer; + OSInitMutex(&transfer->mutex[0]); + OSInitMutex(&transfer->mutex[1]); + OSInitCond(&transfer->waitCond[0]); + OSInitCond(&transfer->waitCond[1]); + OSInitCond(&transfer->signalCond[0]); + OSInitCond(&transfer->signalCond[1]); +} + +#pragma dont_inline on +static u32 _WADIsTerminated(const char* text, u32 maxLength) { + u32 index; + + for (index = 0; index < maxLength; index++) { + if (text[index] == '\0') { + return TRUE; + } + } + return FALSE; +} +#pragma dont_inline reset + typedef struct WADSaveDataRecord { u8 reserved_0x00[0x4E]; char fileName[8]; @@ -3279,241 +3539,3 @@ cleanup: } return result; } - -extern const u8 ca_ppki[]; -extern const u8 ms_ppki[]; -extern const u8 ca_dpki[]; -extern const u8 ms_dpki[]; -extern s32 SHA1Reset(void* context); -extern s32 SHA1Result(void* context, void* digest); - -static s32 WAD_815C43E0(WADHashThreadArgs* args) { - void* context = args->context; - s32 result = 0; - u32 remaining = args->size; - u32 bufferIndex = 0; - WADImportTransfer* transfer = args->transfer; - - while ((remaining != 0) && (result == 0)) { - u32 size = remaining; - OSMutex* mutex; - - if (remaining > transfer->chunkSize) { - size = transfer->chunkSize; - } - mutex = &transfer->mutex[bufferIndex]; - OSLockMutex(mutex); - while (transfer->ready[bufferIndex] == 0) { - OSWaitCond(&transfer->signalCond[bufferIndex], mutex); - } - result = SHA1Input(context, transfer->buffers[bufferIndex], size); - transfer->ready[bufferIndex] = 0; - if (result != 0) { - transfer->error = 1; - } - OSUnlockMutex(mutex); - OSSignalCond(&transfer->waitCond[bufferIndex]); - remaining -= size; - bufferIndex ^= 1; - } - return result; -} - -static s32 _WADHash(WADStream* stream, u32 offset, u32 size, void* context, void* buffer, - u32 chunkSize, void* secondBuffer, void* threadStack, u32 threadStackSize) { - s32 result = 0; - - if ((stream == 0) || (context == 0) || (buffer == 0)) { - return -3000; - } - if (size != WAD_ALIGN32(size)) { - return -3000; - } - if (((u32)buffer & 0x1F) != 0) { - return -3007; - } - if (chunkSize != WAD_ALIGN32(chunkSize)) { - return -3000; - } - if ((secondBuffer == 0) || (threadStack == 0)) { - u32 completed = 0; - - while (size != 0) { - void* readBuffer = buffer; - u32 readSize = size; - - if (chunkSize < size) { - readSize = chunkSize; - } - result = WADReadStream(stream, &readBuffer, readSize, offset + completed); - if ((u32)result != readSize) { - result = -3005; - break; - } - result = SHA1Input(context, readBuffer, readSize); - if (result != 0) { - break; - } - completed += readSize; - size -= readSize; - } - } else { - WADImportTransfer transfer; - WADHashThreadArgs args; - OSThread thread; - s32 threadResult = 0; - u32 completed = 0; - u32 bufferIndex = 0; - BOOL readFailed = FALSE; - - WAD_815C4A2C(&transfer, buffer, secondBuffer, chunkSize); - args.context = context; - args.size = size; - args.transfer = &transfer; - if (!OSCreateThread(&thread, (void* (*)(void*))WAD_815C43E0, &args, - (u8*)threadStack + threadStackSize, threadStackSize, - OSGetThreadPriority(OSGetCurrentThread()), 0)) { - return -3009; - } - OSResumeThread(&thread); - while ((size != 0) && (transfer.error == 0)) { - void* readBuffer; - u32 readSize = size; - OSMutex* mutex = &transfer.mutex[bufferIndex]; - - if (chunkSize < size) { - readSize = chunkSize; - } - OSLockMutex(mutex); - while ((transfer.ready[bufferIndex] != 0) && (transfer.error == 0)) { - OSWaitCond(&transfer.waitCond[bufferIndex], mutex); - } - if (transfer.error != 0) { - OSUnlockMutex(mutex); - break; - } - readBuffer = transfer.buffers[bufferIndex]; - result = WADReadStream(stream, &readBuffer, readSize, offset + completed); - if ((u32)result != readSize) { - OSMutex* otherMutex = &transfer.mutex[bufferIndex ^ 1]; - - OSLockMutex(otherMutex); - OSCancelThread(&thread); - OSJoinThread(&thread, 0); - OSUnlockMutex(otherMutex); - OSUnlockMutex(mutex); - result = -3005; - readFailed = TRUE; - break; - } - transfer.ready[bufferIndex] = readSize; - OSUnlockMutex(mutex); - OSSignalCond(&transfer.signalCond[bufferIndex]); - completed += readSize; - size -= readSize; - bufferIndex ^= 1; - } - if (!readFailed) { - if (!OSJoinThread(&thread, &threadResult)) { - result = -3009; - } else { - result = threadResult; - } - } - } - return result; -} - -s32 WADVerify(WADStream* stream, MEMAllocator* allocator, u32 offset, u32 size) { - WADVerificationWorkspace workspace ALIGN64; - void* verificationBuffer = 0; - WADBackupSignature* backupSignature; - WADVerificationCertificateBundle* certificateBundle; - s32 result; - - workspace.readBuffer[0] = 0; - if (size != WAD_ALIGN32(size)) { - result = -3000; - } else if (size <= 0x340) { - result = -3000; - } else { - workspace.readBuffer[0] = _WADMemAlloc(allocator, 0x8000); - if (workspace.readBuffer[0] == 0) { - result = -3003; - } else if (((u32)workspace.readBuffer[0] & 0x3F) != 0) { - result = -3007; - } else { - u32 hashSize = size - 0x340; - - SHA1Reset(workspace.hashContext); - result = _WADHash(stream, offset, hashSize, workspace.hashContext, - workspace.readBuffer[0], 0x8000, 0, 0, 0x1000); - if (result == 0) { - result = SHA1Result(workspace.hashContext, workspace.digest); - if (result == 0) { - result = WADReadStream(stream, workspace.readBuffer, 0x340, - offset + hashSize); - if (result == 0x340) { - verificationBuffer = _WADMemAlloc(allocator, 0xF80); - if (verificationBuffer == 0) { - result = -3003; - } else { - backupSignature = workspace.readBuffer[0]; - certificateBundle = verificationBuffer; - memcpy(certificateBundle->caProduction, ca_ppki, 0x400); - memcpy(certificateBundle->msProduction, ms_ppki, 0x240); - memcpy(certificateBundle->caDevelopment, ca_dpki, 0x400); - memcpy(certificateBundle->msDevelopment, ms_dpki, 0x240); - memcpy(certificateBundle->firstCertificate, - backupSignature->firstCertificate, 0x180); - memcpy(certificateBundle->secondCertificate, - backupSignature->secondCertificate, 0x180); - result = ES_VerifySign(workspace.digest, 0x14, - workspace.readBuffer[0], verificationBuffer, - 0xF80); - } - } else { - result = -3005; - } - } - } - } - } - - if (workspace.readBuffer[0] != 0) { - _WADMemFree(allocator, workspace.readBuffer[0]); - } - if (verificationBuffer != 0) { - _WADMemFree(allocator, verificationBuffer); - } - return result; -} - -void WAD_815C4A2C(WADImportTransfer* transfer, void* firstBuffer, void* secondBuffer, - u32 chunkSize) { - transfer->chunkSize = chunkSize; - transfer->error = 0; - transfer->ready[1] = 0; - transfer->ready[0] = 0; - transfer->buffers[0] = firstBuffer; - transfer->buffers[1] = secondBuffer; - OSInitMutex(&transfer->mutex[0]); - OSInitMutex(&transfer->mutex[1]); - OSInitCond(&transfer->waitCond[0]); - OSInitCond(&transfer->waitCond[1]); - OSInitCond(&transfer->signalCond[0]); - OSInitCond(&transfer->signalCond[1]); -} - -#pragma dont_inline on -static u32 _WADIsTerminated(const char* text, u32 maxLength) { - u32 index; - - for (index = 0; index < maxLength; index++) { - if (text[index] == '\0') { - return TRUE; - } - } - return FALSE; -} -#pragma dont_inline reset diff --git a/tools/decomp-assist/sol-high-sdk-attempts.md b/tools/decomp-assist/sol-high-sdk-attempts.md new file mode 100644 index 00000000..e09deffb --- /dev/null +++ b/tools/decomp-assist/sol-high-sdk-attempts.md @@ -0,0 +1,718 @@ +# SDK matching attempts + +Baseline commit: c4f51491. Last matching source commit: 4c68814c629631198beb515e9747a8622f5145bd. + +All four units remain NonMatching. This is partial matching progress, not a completion or linking claim. +All target function bodies are present in target address order. The NUP boot-version mangled-name discrepancy remains open. Both pool_diff.py and the gate report identical string sequences for every unit. A stricter offset audit finds all seven VI literals 184 bytes earlier than the target; the other three units match offsets too. VI pool placement therefore remains open. +The final full gate rebuilt 43U from scratch, checked the target DOL hash, found zero baseline-exact regressions and zero forbidden/readability additions. The full captured output is in sol-high-sdk-gate.txt. + +Instruction-exact counts use the gate normalized instruction comparison; code/data bytes and fuzzy percentages use exact-name objdiff. Missing matched_data measures are displayed as zero. + +## libs/RVL_SDK/src/fa/driver/sd_drv + +sd_drv: exact 10 -> 10; code 2100 -> 2100/11752; data 408 -> 408/3592; fuzzy 70.5225 -> 94.6276. + +### pfd_st_inter_callback + +pfd_st_inter_callback: 93.840576%; instructions 65/69, diffs 55; inlined drive lookup and callback register assignment. +Compiled source-level attempts: 3. + +1. explicit driver retrieval in callback rather than shared helper: objdiff 93.840576 -> 93.840576; instructions [65, 69] -> [65, 69]; diffs 55 -> 55; restored. +2. device presence tested as unsigned pointer: objdiff 93.840576 -> 93.840576; instructions [65, 69] -> [65, 69]; diffs 55 -> 55; restored. +3. split drive-present and callback-present checks: objdiff 93.840576 -> 93.840576; instructions [65, 69] -> [65, 69]; diffs 55 -> 55; restored. + +### pfd_st_removal_callback + +pfd_st_removal_callback: 90.303030%; instructions 62/66, diffs 53; inlined drive lookup and callback scheduling. +Compiled source-level attempts: 3. + +1. explicit driver retrieval in callback rather than shared helper: objdiff 90.303030 -> 90.303030; instructions [62, 66] -> [62, 66]; diffs 53 -> 53; restored. +2. device presence tested as unsigned pointer: objdiff 90.303030 -> 90.303030; instructions [62, 66] -> [62, 66]; diffs 53 -> 53; restored. +3. split drive-present and callback-present checks: objdiff 90.303030 -> 90.303030; instructions [62, 66] -> [62, 66]; diffs 53 -> 53; restored. + +### pfd_sddrv_init + +pfd_sddrv_init: 86.104164%; instructions 143/144, diffs 138; mount-status branches and callback registration register allocation. +Compiled source-level attempts: 3. + +1. unsigned pointer comparisons and inverted already-initialized disk branch from asm: objdiff 86.104164 -> 86.104164; instructions [143, 144] -> [143, 144]; diffs 138 -> 138; restored. +2. exact flag equality instead of nonzero mask: objdiff 86.104164 -> 84.993060; instructions [143, 144] -> [145, 144]; diffs 138 -> 137; restored. +3. store mounted device before insertion flag test as target order: objdiff 86.104164 -> 81.909720; instructions [143, 144] -> [143, 144]; diffs 138 -> 138; restored. + +### pfd_sddrv_unmount + +pfd_sddrv_unmount: 90.000000%; instructions 12/13, diffs 9; mount-bit helper load and mask allocation. +Compiled source-level attempts: 3. + +1. explicit flag equals mount bit: objdiff 90.000000 -> 81.538460; instructions [12, 13] -> [13, 13]; diffs 9 -> 7; restored. +2. inline mount bit clear expanded: objdiff 90.000000 -> 90.000000; instructions [12, 13] -> [12, 13]; diffs 9 -> 9; restored. +3. conditional mount-bit clear through local driver pointer: compile rejected; compile failed, restored. +4. separate inline mounted-flag query from bit update: objdiff 90.000000 -> 90.000000; instructions [12, 13] -> [12, 13]; diffs 9 -> 9; restored. + +### pfd_sddrv_finalize + +pfd_sddrv_finalize: 92.877190%; instructions 58/57, diffs 19; inlined mount-bit clearing and error-return scheduling. +Compiled source-level attempts: 3. + +1. explicit flag equals mount bit: objdiff 92.263160 -> 92.877190; instructions [57, 57] -> [58, 57]; diffs 43 -> 19; kept. +2. inline mount bit clear expanded: objdiff 92.877190 -> 92.877190; instructions [58, 57] -> [58, 57]; diffs 19 -> 19; restored. +3. finalize preserves ejected flag as target and uses bit-equal initialized test: objdiff 92.877190 -> 91.491230; instructions [58, 57] -> [58, 57]; diffs 19 -> 50; restored. + +### pfd_sddrv_get_disk_info + +pfd_sddrv_get_disk_info: 97.916664%; instructions 98/96, diffs 2; original symbol ends before final two epilogue instructions; config unchanged. +Compiled source-level attempts: 3. + +1. combine mounted/ejected checks; metadata ends original symbol before final two epilogue instructions: objdiff 97.916664 -> 95.104164; instructions [98, 96] -> [96, 96]; diffs 2 -> 79; restored. +2. explicit return result after geometry success; metadata ends original symbol before final two epilogue instructions: objdiff 97.916664 -> 96.875000; instructions [98, 96] -> [97, 96]; diffs 2 -> 20; restored. +3. geometry zero assignments in chained expression; metadata ends original symbol before final two epilogue instructions: objdiff 97.916664 -> 97.895836; instructions [98, 96] -> [98, 96]; diffs 2 -> 4; restored. + +### pfd_sddrv_physical_read + +pfd_sddrv_physical_read: 99.921260%; instructions 127/127, diffs 2; two register-allocation differences in aligned read arguments. +Compiled source-level attempts: 3. + +1. aligned path uses input sector, current sector initialized only for unaligned loop: objdiff 99.921260 -> 98.346460; instructions [127, 127] -> [127, 127]; diffs 2 -> 43; restored. +2. aligned single-block call uses literal block count: objdiff 99.921260 -> 99.448820; instructions [127, 127] -> [127, 127]; diffs 2 -> 3; restored. +3. unaligned loop explicitly advances sector before buffer: objdiff 99.921260 -> 99.763780; instructions [127, 127] -> [127, 127]; diffs 2 -> 4; restored. + +### pfd_sddrv_physical_write + +pfd_sddrv_physical_write: 99.921260%; instructions 127/127, diffs 2; two register-allocation differences in aligned write arguments. +Compiled source-level attempts: 3. + +1. aligned path uses input sector, current sector initialized only for unaligned loop: objdiff 99.921260 -> 98.346460; instructions [127, 127] -> [127, 127]; diffs 2 -> 43; restored. +2. aligned single-block call uses literal block count: objdiff 99.921260 -> 99.448820; instructions [127, 127] -> [127, 127]; diffs 2 -> 3; restored. +3. unaligned loop explicitly advances sector before buffer: objdiff 99.921260 -> 99.763780; instructions [127, 127] -> [127, 127]; diffs 2 -> 4; restored. + +### pfd_sddrv_get_total_sectors + +pfd_sddrv_get_total_sectors: 90.937500%; instructions 79/80, diffs 43; CSD multiplier truncation and min/max scheduling. +Compiled source-level attempts: 3. + +1. CSD read-block multiplier held in u32 until min/max truncation: objdiff 90.937500 -> 90.937500; instructions [79, 80] -> [79, 80]; diffs 43 -> 43; restored. +2. explicit casts on each min/max intermediate: objdiff 90.937500 -> 90.437500; instructions [79, 80] -> [79, 80]; diffs 43 -> 46; restored. +3. CSD size assembly uses high bits as first operand: objdiff 90.937500 -> 87.375000; instructions [79, 80] -> [79, 80]; diffs 43 -> 42; restored. + +### pfd_sddrv_calc_mbr_bpb + +pfd_sddrv_calc_mbr_bpb: 91.768780%; instructions 171/173, diffs 85; nested reserved-sector convergence loops and register lifetimes. +Compiled source-level attempts: 3. + +1. re-derived 32-bit arithmetic, FAT classification before rounding, skip forbidden cluster interval: objdiff 20.606936 -> 91.433525; instructions [239, 173] -> [172, 173]; diffs 237 -> 126; kept. +2. reverse bounds comparison and pointer increment order; cache cluster size: objdiff 91.433525 -> 91.768780; instructions [172, 173] -> [171, 173]; diffs 126 -> 85; kept. +3. explicit outer-loop continuation and partition sector temporary: objdiff 91.768780 -> 90.265900; instructions [171, 173] -> [169, 173]; diffs 85 -> 85; restored. + +### pfd_sddrv_store_bpb_buf + +pfd_sddrv_store_bpb_buf: 99.342510%; instructions 327/327, diffs 8; serial-field aligned store instruction selection and literal-copy scheduling. +Compiled source-level attempts: 3. + +1. re-derived copy-before-fields control flow, guarded inline copies, endian macros preserve source reloads: objdiff 37.935780 -> 94.217125; instructions [244, 327] -> [327, 327]; diffs 316 -> 72; kept. +2. endian expression instead of intrinsic gives constant stores and byte-swap recognition: objdiff 94.217125 -> 97.088684; instructions [327, 327] -> [327, 327]; diffs 72 -> 27; kept. +3. initialize table pointer after memset and reverse comparison/increment order: objdiff 97.088684 -> 98.654434; instructions [327, 327] -> [327, 327]; diffs 27 -> 14; kept. + +### pfd_sddrv_store_mbr_buf + +pfd_sddrv_store_mbr_buf: 94.366330%; instructions 202/202, diffs 42; CHS arithmetic scheduling and byte-store grouping. +Compiled source-level attempts: 3. + +1. re-derived lookup error, typed CHS and partition type before memset, endian CHS stores: objdiff 51.099010 -> 94.183170; instructions [163, 202] -> [202, 202]; diffs 196 -> 46; kept. +2. pointer-first table iteration and reverse upper comparison: objdiff 94.183170 -> 94.366330; instructions [202, 202] -> [202, 202]; diffs 46 -> 42; kept. +3. compute CHS heads before sectors as target block: objdiff 94.366330 -> 94.366330; instructions [202, 202] -> [202, 202]; diffs 42 -> 44; restored. + +### pfd_sddrv_calc_fat32_mbr_bpb + +pfd_sddrv_calc_fat32_mbr_bpb: 89.909775%; instructions 132/133, diffs 55; FAT convergence loop countdown and sector reload scheduling. +Compiled source-level attempts: 3. + +1. reload total sectors after table helper and multiply cluster bits for remainder test: objdiff 85.571430 -> 89.909775; instructions [131, 133] -> [132, 133]; diffs 96 -> 55; kept. +2. load sectors after size lookup instead of before: objdiff 89.909775 -> 88.360900; instructions [132, 133] -> [132, 133]; diffs 55 -> 58; restored. +3. fat-sector countdown uses calculated count rather than separate attempts: objdiff 89.909775 -> 89.909775; instructions [132, 133] -> [132, 133]; diffs 55 -> 55; restored. + +### pfd_sddrv_store_fat32_mbr_buf + +pfd_sddrv_store_fat32_mbr_buf: 77.507250%; instructions 208/207, diffs 196; CHS widths and endian-store scheduling. +Compiled source-level attempts: 3. + +1. typed CHS cylinder/sector/head widths from clrlwi and fixed lookup status: objdiff 64.159424 -> 67.280190; instructions [193, 207] -> [202, 207]; diffs 167 -> 197; kept. +2. reverse range comparison and remove premature u16 truncation on CHS encoding: objdiff 67.280190 -> 77.507250; instructions [202, 207] -> [208, 207]; diffs 197 -> 196; kept. +3. end-of-partition local separate from type initialization: objdiff 77.507250 -> 77.507250; instructions [208, 207] -> [208, 207]; diffs 196 -> 196; restored. + +### pfd_sddrv_store_fat32_bpb_buf + +pfd_sddrv_store_fat32_bpb_buf: 96.494600%; instructions 374/370, diffs 353; four excess instructions in endian and literal-copy blocks. +Compiled source-level attempts: 3. + +1. re-derived FAT32 BPB flow: copy literals first, inline source-value stores and lookup error: objdiff 69.167564 -> 95.583786; instructions [311, 370] -> [374, 370]; diffs 347 -> 353; kept. +2. table upper bound first operand and pointer-first increment: objdiff 95.583786 -> 95.683784; instructions [374, 370] -> [374, 370]; diffs 353 -> 353; kept. +3. 32-bit endian expression high byte before third byte: objdiff 95.683784 -> 96.494600; instructions [374, 370] -> [374, 370]; diffs 353 -> 353; kept. + +### pfd_sddrv_build_fat32_mbr_bpb + +pfd_sddrv_build_fat32_mbr_bpb: 94.752250%; instructions 230/222, diffs 122; media-state checks and inlined reserved-sector helper branches. +Compiled source-level attempts: 4. + +1. reload partition start after calls as original block flow: objdiff 75.148650 -> 90.490990; instructions [222, 222] -> [230, 222]; diffs 211 -> 122; kept. +2. reserved boot buffer error moved into positive return branch: objdiff 90.490990 -> 90.490990; instructions [230, 222] -> [230, 222]; diffs 122 -> 122; restored. +3. reserved buffer writes unaligned branch first: objdiff 90.490990 -> 94.752250; instructions [230, 222] -> [230, 222]; diffs 122 -> 122; kept. +4. combine media-present and ejected failure blocks at each completed formatting stage: objdiff 94.752250 -> 90.968470; instructions [230, 222] -> [222, 222]; diffs 122 -> 149; restored. + +## libs/RVL_SDK/src/wad/wad + +wad: exact 21 -> 23; code 6680 -> 7080/24500; data 0 -> 0/528; fuzzy 77.3980 -> 80.8216. + +### WADImportGetBlocks + +WADImportGetBlocks: 76.553690%; instructions 263/298, diffs 288; inlined mask traversal, block arithmetic and stack-local layout. +Compiled source-level attempts: 3. + +1. separate real workspace locals in target order instead of aligned wrapper: objdiff 76.553690 -> 76.486580; instructions [263, 298] -> [263, 298]; diffs 288 -> 289; restored. +2. preserve 64-bit content rounding until final block-count conversion: objdiff 76.553690 -> 72.657715; instructions [263, 298] -> [275, 298]; diffs 288 -> 287; restored. +3. selected content pointer recalculated from incremented index: objdiff 76.553690 -> 75.795300; instructions [263, 298] -> [266, 298]; diffs 288 -> 287; restored. + +### WAD_815BFFA8 + +WAD_815BFFA8: 97.177420%; instructions 62/62, diffs 30; thread-loop callee-saved register allocation. +Compiled source-level attempts: 3. + +1. declare thread-loop locals in target lifetime order: compile rejected; compile failed, restored. +2. loop exits test result before remaining bytes: compile rejected; compile failed, restored. +3. conditional minimum declared in one expression: compile rejected; compile failed, restored. +4. declare thread-loop locals in target lifetime order: objdiff 97.177420 -> 96.290320; instructions [62, 62] -> [62, 62]; diffs 30 -> 36; restored. +5. loop exits test result before remaining bytes: objdiff 97.177420 -> 97.016130; instructions [62, 62] -> [62, 62]; diffs 30 -> 32; restored. +6. conditional minimum declared in one expression: objdiff 97.177420 -> 96.854836; instructions [62, 62] -> [62, 62]; diffs 30 -> 33; restored. + +### WADImportEx + +WADImportEx: 71.913610%; instructions 1147/1146, diffs 1121; stream/unpack stack offsets, inlined mask traversal and cleanup scheduling. +Compiled source-level attempts: 3. + +1. re-derived prologue workspace: separate stream and unpack info, remove duplicate transfer initialization: objdiff 70.807150 -> 71.912740; instructions [1151, 1146] -> [1147, 1146]; diffs 1119 -> 1120; kept. +2. separate stream/unpack local declaration order: objdiff 71.912740 -> 71.912740; instructions [1147, 1146] -> [1147, 1146]; diffs 1120 -> 1120; restored. +3. initial cleanup flags assigned after input buffers instead of grouped declarations: objdiff 71.912740 -> 71.913610; instructions [1147, 1146] -> [1147, 1146]; diffs 1120 -> 1121; kept. + +### WAD_815C1288 + +WAD_815C1288: 96.393440%; instructions 61/61, diffs 34; thread-loop register allocation and result scheduling. +Compiled source-level attempts: 3. + +1. declare thread-loop locals in target lifetime order: compile rejected; compile failed, restored. +2. loop exits test result before remaining bytes: compile rejected; compile failed, restored. +3. conditional minimum declared in one expression: compile rejected; compile failed, restored. +4. declare thread-loop locals in target lifetime order: objdiff 96.311480 -> 96.393440; instructions [61, 61] -> [61, 61]; diffs 35 -> 34; kept. +5. loop exits test result before remaining bytes: objdiff 96.393440 -> 96.229510; instructions [61, 61] -> [61, 61]; diffs 34 -> 36; restored. +6. conditional minimum declared in one expression: objdiff 96.393440 -> 96.311480; instructions [61, 61] -> [61, 61]; diffs 34 -> 35; restored. + +### WADBackupEx + +WADBackupEx: 59.089455%; instructions 909/1062, diffs 1049; export stack layout and nested content/file control flow. +Compiled source-level attempts: 3. + +1. re-derived export prologue and call sequence; _WADCheckContents owns content-ID enumeration: objdiff 55.484936 -> 59.645950; instructions [933, 1062] -> [906, 1062]; diffs 1040 -> 1041; kept. +2. SHA1 context correct storage size and 64-byte DMA alignment: objdiff 59.645950 -> 59.669490; instructions [906, 1062] -> [906, 1062]; diffs 1041 -> 1040; kept. +3. file encryption IV initialized from file header for each encrypted file: objdiff 59.669490 -> 59.669490; instructions [906, 1062] -> [906, 1062]; diffs 1040 -> 1040; restored. + +### _WADCheckContents + +_WADCheckContents: 96.705880%; instructions 85/85, diffs 19; nested content search register assignment. +Compiled source-level attempts: 3. + +1. cache per-content metadata pointer for nested search: compile rejected; compile failed, restored. +2. compare installed content ID with metadata operand first: compile rejected; compile failed, restored. +3. signed content counter preserves target comparisons: compile rejected; compile failed, restored. +4. cache per-content metadata pointer for nested search: compile rejected; compile failed, restored. +5. compare installed content ID with metadata operand first: objdiff 96.705880 -> 94.294120; instructions [85, 85] -> [85, 85]; diffs 19 -> 21; restored. +6. signed content counter preserves target comparisons: objdiff 96.705880 -> 95.294120; instructions [85, 85] -> [85, 85]; diffs 19 -> 21; restored. +7. target result reset only at end of each private-content search: objdiff 96.705880 -> 96.647060; instructions [85, 85] -> [85, 85]; diffs 19 -> 20; restored. + +### WADOpenStream + +WADOpenStream: 87.354680%; instructions 200/203, diffs 173; NAND extension loop, return scheduling and handle addressing. +Compiled source-level attempts: 3. + +1. re-derived NAND extension branch rejects existing length at or beyond requested offset: objdiff 85.310350 -> 85.334980; instructions [200, 203] -> [200, 203]; diffs 173 -> 173; kept. +2. NAND read-open explicit error return instead of conditional expression: objdiff 85.334980 -> 85.802956; instructions [200, 203] -> [199, 203]; diffs 173 -> 173; kept. +3. NAND extension chunk selected from constant-first branch: objdiff 85.802956 -> 87.354680; instructions [199, 203] -> [200, 203]; diffs 173 -> 173; kept. + +### WADWriteStream + +100.000000%; instructions 47/47, diffs 0. + +1. re-derived sparse switch dispatch retaining shared result register: compile rejected; compile failed, restored. +2. re-derived sparse switch dispatch retaining shared result register: objdiff 62.936170 -> 95.723404; instructions [43, 47] -> [45, 47]; diffs 35 -> 33; kept. +3. explicit unsupported stream cases preserve source switch decision tree: objdiff 95.723404 -> 100.000000; instructions [45, 47] -> [47, 47]; diffs 33 -> 0; kept. +4. stream dispatch returns early on invalid arguments before shared switch result: objdiff 100.000000 -> 100.000000; instructions [47, 47] -> [47, 47]; diffs 0 -> 0; restored. + +### WADSeekStream + +100.000000%; instructions 53/53, diffs 0. + +1. re-derived switch dispatch and FAFinfo failure return: compile rejected; compile failed, restored. +2. re-derived switch dispatch and FAFinfo failure return: objdiff 51.226414 -> 94.113205; instructions [56, 53] -> [54, 53]; diffs 51 -> 40; kept. +3. explicit unsupported stream cases preserve source switch decision tree: objdiff 94.113205 -> 100.000000; instructions [54, 53] -> [53, 53]; diffs 40 -> 0; kept. +4. stream dispatch returns early on invalid arguments before shared switch result: objdiff 100.000000 -> 100.000000; instructions [53, 53] -> [53, 53]; diffs 0 -> 0; restored. + +### _WADUnpackBackup + +_WADUnpackBackup: 94.867256%; instructions 116/113, diffs 89; section-size reloads and allocation ordering. +Compiled source-level attempts: 3. + +1. cache TMD size before flags branch for original source load order: compile rejected; compile failed, restored. +2. set size and aligned temporary before recording section offset: compile rejected; compile failed, restored. +3. backup content-index availability uses copied version field: compile rejected; compile failed, restored. +4. cache TMD size before flags branch for original source load order: objdiff 87.831856 -> 92.212390; instructions [118, 113] -> [117, 113]; diffs 91 -> 92; kept. +5. set size and aligned temporary before recording section offset: objdiff 92.212390 -> 92.212390; instructions [117, 113] -> [117, 113]; diffs 92 -> 92; restored. +6. backup content-index availability uses copied version field: objdiff 92.212390 -> 94.867256; instructions [117, 113] -> [116, 113]; diffs 92 -> 89; kept. + +### _WADGetCidxCount + +_WADGetCidxCount: 95.050000%; instructions 40/40, diffs 25; four-bit unrolled mask traversal register allocation; retained to preserve inlined consumers. +Compiled source-level attempts: 4. + +1. ordinary bit loop permits compiler unrolling with cumulative index: compile rejected; compile failed, restored. +2. loop counter preincrement instead of postincrement: compile rejected; compile failed, restored. +3. mask first argument in bitwise test: compile rejected; compile failed, restored. +4. ordinary bit loop permits compiler unrolling with cumulative index: objdiff 95.050000 -> 96.625000; instructions [40, 40] -> [40, 40]; diffs 25 -> 19; kept. +5. loop counter preincrement instead of postincrement: objdiff 96.625000 -> 96.625000; instructions [40, 40] -> [40, 40]; diffs 19 -> 19; restored. +6. mask first argument in bitwise test: objdiff 96.625000 -> 96.625000; instructions [40, 40] -> [40, 40]; diffs 19 -> 19; restored. +7. restore four-bit unrolled mask traversal to preserve inlined consumers; compare whole-unit code score: unit fuzzy 78.282936 -> 80.821550; kept. + +### _WADGetCidx + +_WADGetCidx: 99.142860%; instructions 56/56, diffs 8; four-bit cumulative index register assignment. +Compiled source-level attempts: 4. + +1. ordinary bit loop permits compiler unrolling with cumulative index: compile rejected; compile failed, restored. +2. loop counter preincrement instead of postincrement: compile rejected; compile failed, restored. +3. mask first argument in bitwise test: compile rejected; compile failed, restored. +4. ordinary bit loop permits compiler unrolling with cumulative index: objdiff 99.142860 -> 96.964290; instructions [56, 56] -> [56, 56]; diffs 8 -> 26; restored. +5. loop counter preincrement instead of postincrement: objdiff 99.142860 -> 99.142860; instructions [56, 56] -> [56, 56]; diffs 8 -> 8; restored. +6. mask first argument in bitwise test: objdiff 99.142860 -> 99.142860; instructions [56, 56] -> [56, 56]; diffs 8 -> 8; restored. +7. re-derived four-bit unroll uses one cumulative signed index as original blocks: objdiff 99.142860 -> 92.892860; instructions [56, 56] -> [56, 56]; diffs 8 -> 32; restored. + +### _WADBackupGetFiles + +_WADBackupGetFiles: 46.195583%; instructions 291/317, diffs 296; directory path buffers and deferred inline string comparisons. +Compiled source-level attempts: 3. + +1. stack path arrays use ordinary NAND local alignment rather than DMA alignment: objdiff 46.195583 -> 43.823345; instructions [291, 317] -> [286, 317]; diffs 296 -> 310; restored. +2. single path buffer reused for nocopy and banner comparisons in object block order: objdiff 46.195583 -> 44.567820; instructions [291, 317] -> [291, 317]; diffs 296 -> 295; restored. +3. file enumeration advances fixed-size NAND name slots: objdiff 46.195583 -> 44.274450; instructions [291, 317] -> [288, 317]; diffs 296 -> 296; restored. + +### _WADBackupGetSize + +_WADBackupGetSize: 86.774190%; instructions 161/155, diffs 155; content loop arithmetic and output-pointer scheduling. +Compiled source-level attempts: 3. + +1. re-derived TMD content size uses view contents and low 32-bit size: objdiff 68.554840 -> 86.774190; instructions [166, 155] -> [161, 155]; diffs 162 -> 155; kept. +2. selected count and output title pointer retained separately across contents loop: objdiff 86.774190 -> 86.774190; instructions [161, 155] -> [161, 155]; diffs 155 -> 155; restored. +3. pointer traversal for backup-file size accumulation: objdiff 86.774190 -> 84.419360; instructions [161, 155] -> [158, 155]; diffs 155 -> 147; restored. + +### _WADRandPad + +_WADRandPad: 87.307690%; instructions 89/91, diffs 62; partial-word byte-copy loop and random-expression scheduling. +Compiled source-level attempts: 5. + +1. re-derived advancing word and byte pointers with original random call ordering: compile rejected; compile failed, restored. +2. assign random partial word to same wordCount storage as target: compile rejected; compile failed, restored. +3. explicit random OR accumulation before low two bits: compile rejected; compile failed, restored. +4. re-derived advancing word and byte pointers with original random call ordering: objdiff 62.461540 -> 78.802200; instructions [88, 91] -> [83, 91]; diffs 82 -> 88; kept. +5. assign random partial word to same wordCount storage as target: compile rejected; compile failed, restored. +6. explicit random OR accumulation before low two bits: objdiff 78.802200 -> 79.021980; instructions [83, 91] -> [83, 91]; diffs 88 -> 88; kept. +7. restore sequential random calls with low-bit shift first: objdiff 79.021980 -> 78.802200; instructions [83, 91] -> [83, 91]; diffs 88 -> 88; restored. +8. reuse completed word count for partial random word after declarations: objdiff 79.021980 -> 83.780220; instructions [83, 91] -> [92, 91]; diffs 88 -> 75; kept. +9. consistent partial and complete random word expressions preserve observed call evaluation order: objdiff 83.780220 -> 83.890110; instructions [92, 91] -> [92, 91]; diffs 75 -> 75; kept. + +### WAD_815C43E0 + +WAD_815C43E0: 97.177420%; instructions 62/62, diffs 30; thread-loop callee-saved register allocation. +Compiled source-level attempts: 3. + +1. declare thread-loop locals in target lifetime order: compile rejected; compile failed, restored. +2. loop exits test result before remaining bytes: compile rejected; compile failed, restored. +3. conditional minimum declared in one expression: compile rejected; compile failed, restored. +4. declare thread-loop locals in target lifetime order: objdiff 97.177420 -> 96.290320; instructions [62, 62] -> [62, 62]; diffs 30 -> 36; restored. +5. loop exits test result before remaining bytes: objdiff 97.177420 -> 97.016130; instructions [62, 62] -> [62, 62]; diffs 30 -> 32; restored. +6. conditional minimum declared in one expression: objdiff 97.177420 -> 96.854836; instructions [62, 62] -> [62, 62]; diffs 30 -> 33; restored. + +### _WADHash + +_WADHash: 71.103096%; instructions 205/194, diffs 193; threaded/sequential hash control flow and cleanup locals. +Compiled source-level attempts: 3. + +1. re-derived shared result and read buffer; cancel path directly reaches common return: compile rejected; compile failed, restored. +2. byte completion variable shared across threaded and sequential paths: compile rejected; compile failed, restored. +3. choose chunk from remaining-first comparison: objdiff 71.000000 -> 71.103096; instructions [205, 194] -> [205, 194]; diffs 193 -> 193; kept. +4. sequential completion offset declared at function scope: objdiff 71.103096 -> 69.654640; instructions [205, 194] -> [204, 194]; diffs 193 -> 194; restored. +5. sequential read pointer retained across repeated reads: objdiff 71.103096 -> 69.731960; instructions [205, 194] -> [204, 194]; diffs 193 -> 193; restored. + +### WADVerify + +WADVerify: 86.863945%; instructions 133/147, diffs 131; hash/digest workspace offsets and original dormant cleanup blocks. +Compiled source-level attempts: 3. + +1. re-derived workspace: DMA-aligned pointer, digest and real SHA1 context locals: objdiff 51.312923 -> 52.068027; instructions [130, 147] -> [131, 147]; diffs 136 -> 131; kept. +2. certificate source pointer reloaded after each memcpy: objdiff 52.068027 -> 56.027210; instructions [131, 147] -> [132, 147]; diffs 131 -> 128; kept. +3. signature stream offset computed before hashing as target local: objdiff 56.027210 -> 86.863945; instructions [132, 147] -> [133, 147]; diffs 128 -> 131; kept. + +### WADImportDVDExForBS + +WADImportDVDExForBS: 89.330795%; instructions 259/263, diffs 245; inlined mask traversal and boot-import workspace offsets. +Compiled source-level attempts: 6. + +1. DVD read length unsigned comparison following cmplwi target: objdiff 75.908745 -> 75.908745; instructions [295, 263] -> [295, 263]; diffs 280 -> 280; restored. +2. DVD section offset initialized before result writes: objdiff 75.908745 -> 75.908745; instructions [295, 263] -> [295, 263]; diffs 280 -> 280; restored. +3. boot-import header and DVD info declaration order: objdiff 75.908745 -> 75.908745; instructions [295, 263] -> [295, 263]; diffs 280 -> 280; restored. +4. header parser workspace declared before file handle as original stack blocks: objdiff 75.908745 -> 75.806080; instructions [295, 263] -> [295, 263]; diffs 280 -> 281; restored. +5. re-derived constant-first content chunk selection: objdiff 75.908745 -> 75.889730; instructions [295, 263] -> [295, 263]; diffs 280 -> 280; restored. +6. content count iteration ordinary for-loop and explicit result initialization after open: objdiff 75.908745 -> 75.908745; instructions [295, 263] -> [295, 263]; diffs 280 -> 280; restored. + +## libs/RVL_SDK/src/vi/vi + +vi: exact 18 -> 18; code 2084 -> 2084/11296; data 400 -> 400/1944; fuzzy 83.3782 -> 85.1516. + +### OnShutdown + +OnShutdown: 81.039604%; instructions 97/101, diffs 44; four missing instructions in leading-zero/mask loop and return scheduling. +Compiled source-level attempts: 7. + +1. re-derived leading-zero helper keeps low mask as 64-bit intermediate: objdiff 80.633670 -> 80.633670; instructions [96, 101] -> [96, 101]; diffs 84 -> 84; restored. +2. explicit else in leading-zero helper preserves original block boundary: objdiff 80.633670 -> 79.544556; instructions [96, 101] -> [95, 101]; diffs 84 -> 86; restored. +3. natural 64-bit shift for clearing transferred register bit: objdiff 80.633670 -> 68.148510; instructions [96, 101] -> [90, 101]; diffs 84 -> 89; restored. +4. re-derived shutdown event switch: only events one through three begin shutdown: objdiff 80.633670 -> 72.366330; instructions [96, 101] -> [101, 101]; diffs 84 -> 86; restored. +5. re-derived signed event tree: event 4 through 6 succeeds; out-of-range returns unchanged false final argument: objdiff 80.633670 -> 80.841580; instructions [96, 101] -> [97, 101]; diffs 84 -> 48; kept. +6. separate immediate final-success return from ordinary event-success label: objdiff 80.841580 -> 79.643560; instructions [97, 101] -> [99, 101]; diffs 48 -> 90; restored. +7. capture retrace counter before first-shutdown state is stored: objdiff 80.841580 -> 81.039604; instructions [97, 101] -> [97, 101]; diffs 48 -> 44; kept. + +### __VIRetraceHandler + +__VIRetraceHandler: 88.813960%; instructions 501/516, diffs 407; interrupt context stack frame and vertical polling/acknowledgement scheduling. +Compiled source-level attempts: 3. + +1. re-derived vertical stability loops and retrace phase error test from individual interrupt blocks: objdiff 87.544570 -> 88.813960; instructions [502, 516] -> [501, 516]; diffs 443 -> 407; kept. +2. interrupt flags updated before hardware acknowledgement stores: objdiff 88.813960 -> 88.813960; instructions [501, 516] -> [501, 516]; diffs 407 -> 407; restored. +3. exception context placed after output pixel locals in interrupt stack frame: objdiff 88.813960 -> 88.813960; instructions [501, 516] -> [501, 516]; diffs 407 -> 407; restored. + +### __VIInit + +__VIInit: 87.469696%; instructions 121/132, diffs 122; original stack-backed TV initialization loop and scan register scheduling. +Compiled source-level attempts: 3. + +1. re-derived standard encoder TV reset by explicit enum switch: objdiff 86.446970 -> 87.431816; instructions [119, 132] -> [121, 132]; diffs 122 -> 122; kept. +2. re-derived progressive branch tests scan mode above one: objdiff 87.431816 -> 87.469696; instructions [121, 132] -> [121, 132]; diffs 122 -> 122; kept. +3. initial blanking register arithmetic grouped as original loads: objdiff 87.469696 -> 87.469696; instructions [121, 132] -> [121, 132]; diffs 122 -> 122; restored. + +### VIInit + +VIInit: 85.062130%; instructions 332/338, diffs 168; scan/field locals and hardware initialization scheduling. +Compiled source-level attempts: 3. + +1. re-derived scan mode held locally until interrupt restore: objdiff 82.547340 -> 85.062130; instructions [333, 338] -> [332, 338]; diffs 229 -> 168; kept. +2. framebuffer and pan heights reload current timing after adjusting display: objdiff 85.062130 -> 85.062130; instructions [332, 338] -> [332, 338]; diffs 168 -> 168; restored. +3. getTiming TV-mode argument sums shifted TV type and scan code: objdiff 85.062130 -> 85.062130; instructions [332, 338] -> [332, 338]; diffs 168 -> 168; restored. + +### VIWaitForRetrace + +VIWaitForRetrace: 90.476190%; instructions 21/21, diffs 2; two save/load instructions scheduled in the opposite order. +Compiled source-level attempts: 3. + +1. declare saved interrupt state with initializer: objdiff 90.476190 -> 90.476190; instructions [21, 21] -> [21, 21]; diffs 2 -> 2; restored. +2. retrace wait expressed as do while: objdiff 90.476190 -> 90.476190; instructions [21, 21] -> [21, 21]; diffs 2 -> 2; restored. +3. inline wait helper gives interrupt state independent lifetime: objdiff 90.476190 -> 90.476190; instructions [21, 21] -> [21, 21]; diffs 2 -> 2; restored. + +### setFbbRegs + +setFbbRegs: 79.419540%; instructions 160/174, diffs 74; inlined framebuffer parity/address control flow and alias-dependent reloads. +Compiled source-level attempts: 3. + +1. re-derived signed parity and explicit field-mode branches in framebuffer helper: objdiff 79.419540 -> 72.954025; instructions [160, 174] -> [164, 174]; diffs 74 -> 160; restored. +2. re-derived byte line stride computed before horizontal word offset: objdiff 79.419540 -> 79.419540; instructions [160, 174] -> [160, 174]; diffs 74 -> 74; restored. +3. top framebuffer sum grouped in target multiplication and address order: objdiff 79.419540 -> 71.942530; instructions [160, 174] -> [160, 174]; diffs 74 -> 98; restored. + +### setHorizontalRegs + +setHorizontalRegs: 62.232143%; instructions 49/56, diffs 55; compiler merges repeated changed-mask load/store pairs. +Compiled source-level attempts: 4. + +1. inline changed-mask helper preserves independent register writes: objdiff 62.232143 -> 10.339286; instructions [49, 56] -> [61, 56]; diffs 55 -> 61; restored. +2. inline pointer-based changed mask handles register-write aliasing: objdiff 62.232143 -> 0.000000; instructions [49, 56] -> [65, 56]; diffs 55 -> 65; restored. +3. explicit assignment for changed mask: objdiff 62.232143 -> 62.232143; instructions [49, 56] -> [49, 56]; diffs 55 -> 55; restored. +4. inline mask OR helper instead of runtime-index shift: objdiff 62.232143 -> 1.607143; instructions [49, 56] -> [65, 56]; diffs 55 -> 65; restored. + +### setVerticalRegs + +setVerticalRegs: 57.950497%; instructions 81/101, diffs 89; compiler merges repeated changed-mask stores and blanking arithmetic. +Compiled source-level attempts: 3. + +1. re-derived scan coefficients and signed display-position and size arithmetic: objdiff 48.237625 -> 57.900990; instructions [77, 101] -> [81, 101]; diffs 99 -> 89; kept. +2. re-derived changed-mark and vertical register source order: objdiff 57.900990 -> 57.950497; instructions [81, 101] -> [81, 101]; diffs 89 -> 89; kept. +3. cache shared blanking intervals within each parity branch: objdiff 57.950497 -> 53.049503; instructions [81, 101] -> [76, 101]; diffs 89 -> 92; restored. + +### VIConfigure + +VIConfigure: 74.807260%; instructions 423/441, diffs 367; inlined register helpers and scan/TV decision scheduling. +Compiled source-level attempts: 3. + +1. re-derived boot TV compatibility test in original short-circuit branch order: objdiff 74.034010 -> 74.124720; instructions [430, 441] -> [430, 441]; diffs 384 -> 376; kept. +2. re-derived progressive scan register uses cached non-interlace mode: objdiff 74.124720 -> 74.807260; instructions [430, 441] -> [423, 441]; diffs 376 -> 367; kept. +3. store scan mode before changed-mode flag and use summed TV mode argument: objdiff 74.807260 -> 74.807260; instructions [423, 441] -> [423, 441]; diffs 367 -> 367; restored. + +### VIConfigurePan + +VIConfigurePan: 88.807510%; instructions 208/213, diffs 194; inlined register helpers and timing-pointer scheduling. +Compiled source-level attempts: 3. + +1. re-derived single-field height branch using full original scan decision tree: objdiff 88.807510 -> 85.868546; instructions [208, 213] -> [211, 213]; diffs 194 -> 195; restored. +2. current timing retained before pan dimensions are written: objdiff 88.807510 -> 87.868546; instructions [208, 213] -> [208, 213]; diffs 194 -> 196; restored. +3. picture configuration updated before scaling registers: objdiff 88.807510 -> 46.004696; instructions [208, 213] -> [208, 213]; diffs 194 -> 192; restored. + +### VIFlush + +VIFlush: 78.913040%; instructions 69/69, diffs 20; leading-zero/mask-loop register allocation and initial state scheduling. +Compiled source-level attempts: 3. + +1. re-derived leading-zero helper keeps low mask as 64-bit intermediate: objdiff 78.913040 -> 78.913040; instructions [69, 69] -> [69, 69]; diffs 20 -> 20; restored. +2. explicit else in leading-zero helper preserves original block boundary: objdiff 78.913040 -> 77.318840; instructions [69, 69] -> [68, 69]; diffs 20 -> 51; restored. +3. natural 64-bit shift for clearing transferred register bit: objdiff 78.913040 -> 62.362320; instructions [69, 69] -> [63, 69]; diffs 20 -> 62; restored. + +### __VIDisplayPositionToXY + +__VIDisplayPositionToXY: 82.624115%; instructions 140/141, diffs 109; one missing instruction plus half-line bound scheduling. +Compiled source-level attempts: 3. + +1. re-derived each interlace and progressive bound block and pixel half-line masks: objdiff 68.404260 -> 82.517730; instructions [121, 141] -> [140, 141]; diffs 128 -> 112; kept. +2. single-scan branch reloads timing pointer after wrapping half-line: objdiff 82.517730 -> 81.780140; instructions [140, 141] -> [142, 141]; diffs 112 -> 125; restored. +3. express three equalization half-lines with shift and subtraction: objdiff 82.517730 -> 82.624115; instructions [140, 141] -> [140, 141]; diffs 112 -> 109; kept. + +## libs/RVL_SDK/src/nup/nup + +nup: exact 11 -> 13; code 2536 -> 2840/10764; data 128 -> 1720/1720; fuzzy 55.2887 -> 82.3081. + +### __nupParseServerInfo__FP14NUPContextInfoPcPcUx + +__nupParseServerInfo__FP14NUPContextInfoPcPcUx: 62.537610%; instructions 461/452, diffs 432; tag-helper inlining and parsed-title stack/register allocation. +Compiled source-level attempts: 3. + +1. re-derived parser extraction blocks and both title passes through ordinary inline tag helper: objdiff 0.000000 -> 62.537610; instructions [431, 452] -> [461, 452]; diffs 442 -> 432; kept. +2. upload audit validation preserves separate value comparisons before storing state: objdiff 62.537610 -> 58.984512; instructions [461, 452] -> [468, 452]; diffs 432 -> 442; restored. +3. signed title counting loop counter mirrors target signed comparisons: objdiff 62.537610 -> 62.537610; instructions [461, 452] -> [461, 452]; diffs 432 -> 432; restored. + +### __nupGetServerInfo__FPcPcUxPcPcUlPcUsUsUxUsPPc + +__nupGetServerInfo__FPcPcUxPcPcUlPcUsUsUxUsPPc: 96.758620%; instructions 147/145, diffs 116; request-size arithmetic and response allocation scheduling. +Compiled source-level attempts: 3. + +1. request allocation sum follows region country and message evaluation order: objdiff 95.200000 -> 95.131035; instructions [147, 145] -> [147, 145]; diffs 116 -> 116; restored. +2. response copy stored and reloaded through caller output: objdiff 95.200000 -> 96.758620; instructions [147, 145] -> [147, 145]; diffs 116 -> 116; kept. +3. endpoint declared before request pointer: objdiff 96.758620 -> 96.448270; instructions [147, 145] -> [147, 145]; diffs 116 -> 116; restored. + +### __nupSetAuditState__FUc + +100.000000%; instructions 31/31, diffs 0. + +1. re-derived delete success bypasses error conversion; create existing-dir uses conditional expression: objdiff 53.838710 -> 78.548386; instructions [32, 31] -> [33, 31]; diffs 26 -> 24; kept. +2. share the ordinary audit pathname literal before branch dispatch: objdiff 78.548386 -> 100.000000; instructions [33, 31] -> [31, 31]; diffs 24 -> 0; kept. + +### __nupBase64Encode__FPUcPUcUl + +__nupBase64Encode__FPUcPUcUl: 71.619050%; instructions 59/63, diffs 41; alphabet reloads and partial-group output control flow. +Compiled source-level attempts: 3. + +1. re-derived input step separated from bit accumulator update: objdiff 71.619050 -> 71.619050; instructions [59, 63] -> [59, 63]; diffs 41 -> 41; restored. +2. declare accumulator before alphabet lifetime: objdiff 71.619050 -> 70.111115; instructions [59, 63] -> [59, 63]; diffs 41 -> 51; restored. +3. advance individual output bytes through ordinary pointers: objdiff 71.619050 -> 71.619050; instructions [59, 63] -> [59, 63]; diffs 41 -> 41; restored. + +### __nupGetAuditData__FP14NUPContextInfoPPc + +__nupGetAuditData__FP14NUPContextInfoPPc: 91.741806%; instructions 236/244, diffs 160; encoded-length helper inlining and audit stack/register scheduling. +Compiled source-level attempts: 5. + +1. re-derived audit format word, product code terminator and selected-title search order: objdiff 27.905737 -> 91.741806; instructions [243, 244] -> [236, 244]; diffs 223 -> 160; kept. +2. encoded audit size derived from complete signed-record bytes: objdiff 91.741806 -> 91.741806; instructions [236, 244] -> [236, 244]; diffs 160 -> 160; restored. +3. typed record allocation and memcpy arguments preserve explicit field layout: objdiff 91.741806 -> 91.741806; instructions [236, 244] -> [236, 244]; diffs 160 -> 160; restored. +4. re-derived failed serial-number fallback copies empty string at original sdata offset 0x10, not unknown status text: objdiff 91.741806 -> 91.741806; instructions [236, 244] -> [236, 244]; diffs 160 -> 160; restored. +5. retain assembly-proven empty serial fallback: code unchanged, matched data improves 1688 to 1720 bytes: objdiff 91.741806 -> 91.741806; instructions [236, 244] -> [236, 244]; diffs 160 -> 160; kept. + +### __nupGetTmdView__FUxPP9ESTmdView + +100.000000%; instructions 45/45, diffs 0. + +1. query-only ES_GetTmdView fills its size output before allocation: objdiff 95.555560 -> 0.000000; instructions [47, 45] -> [0, 0]; diffs 42 -> -1; restored. +2. successful size query tested before absent-title case: objdiff 95.555560 -> 95.288890; instructions [47, 45] -> [47, 45]; diffs 42 -> 42; restored. +3. allocated view pointer assigned through caller field: objdiff 95.555560 -> 95.555560; instructions [47, 45] -> [47, 45]; diffs 42 -> 42; restored. +4. retain original out-of-line query helper while using API-written size output: objdiff 95.555560 -> 100.000000; instructions [47, 45] -> [45, 45]; diffs 42 -> 0; kept. + +### __nupGetBootVersion__FP14ESTitleVersion + +__nupGetBootVersion__FP14ESTitleVersion: unscored; instructions 153/163, diffs 101; original mangled name says one argument but target uses two; ABI retained; content-search scheduling also differs. +Compiled source-level attempts: 3. + +1. re-derived boot TMD guard and successful enumeration blocks; retains observed two-argument ABI: objdiff 0.000000 -> 0.000000; instructions [152, 163] -> [153, 163]; diffs 108 -> 101; kept. +2. boot version range check tests failure first as target: objdiff 0.000000 -> 0.000000; instructions [153, 163] -> [153, 163]; diffs 101 -> 101; restored. +3. content loop signed counter and installed count compared through unsigned lower bound: objdiff 0.000000 -> 0.000000; instructions [153, 163] -> [153, 163]; diffs 101 -> 101; restored. + +### __nupGetTitleSize__FP12NUPTitleInfo + +__nupGetTitleSize__FP12NUPTitleInfo: 73.942444%; instructions 133/139, diffs 114; inlined installed-content search and 64-bit block rounding scheduling. +Compiled source-level attempts: 3. + +1. re-derived title storage allocation adds each real NAND block separately: objdiff 69.856120 -> 70.064750; instructions [123, 139] -> [132, 139]; diffs 133 -> 132; kept. +2. re-derived content loop signed counter and reloads TMD from title metadata: objdiff 70.064750 -> 73.942444; instructions [132, 139] -> [133, 139]; diffs 132 -> 114; kept. +3. progress step stored before rounded installed-content addition: objdiff 73.942444 -> 72.892090; instructions [133, 139] -> [133, 139]; diffs 114 -> 114; restored. + +### __nupGetContentFull__FP14NUPContextInfoP12NUPTitleInfoPcUl + +__nupGetContentFull__FP14NUPContextInfoP12NUPTitleInfoPcUl: 94.204544%; instructions 88/88, diffs 25; equal instruction count; register assignment and response-allocation scheduling. +Compiled source-level attempts: 3. + +1. re-derived signed content-index search and full-width alignment addition: objdiff 86.318184 -> 93.193184; instructions [85, 88] -> [87, 88]; diffs 79 -> 80; kept. +2. response byte count recorded before returned allocation pointer: objdiff 93.193184 -> 93.238640; instructions [87, 88] -> [87, 88]; diffs 80 -> 80; kept. +3. response locals declared before URL pointer and index search: objdiff 93.238640 -> 94.204544; instructions [87, 88] -> [88, 88]; diffs 80 -> 25; kept. + +### __nupGetContentIncr__FP14NUPContextInfoP12NUPTitleInfoPc + +__nupGetContentIncr__FP14NUPContextInfoP12NUPTitleInfoPc: 90.421050%; instructions 109/114, diffs 105; installed-content helper inlining and import-error cleanup scheduling. +Compiled source-level attempts: 3. + +1. re-derived shared installed-content search helper returns membership with pointer traversal: objdiff 80.026310 -> 87.307014; instructions [106, 114] -> [109, 114]; diffs 103 -> 105; kept. +2. incremental content loop signed count and 64-bit round before import begin: objdiff 87.307014 -> 78.894740; instructions [109, 114] -> [111, 114]; diffs 105 -> 80; restored. +3. import file result captured before begin-error cleanup call: objdiff 87.307014 -> 90.421050; instructions [109, 114] -> [109, 114]; diffs 105 -> 105; kept. + +### __nupUpdateTitle__FP14NUPContextInfoP12NUPTitleInfoPcPc + +__nupUpdateTitle__FP14NUPContextInfoP12NUPTitleInfoPcPc: 94.962960%; instructions 133/135, diffs 88; NAND free-block comparison and shared error-return scheduling. +Compiled source-level attempts: 3. + +1. re-derived boot content fetch uses uncached content URL: objdiff 85.355550 -> 86.985180; instructions [135, 135] -> [135, 135]; diffs 100 -> 76; kept. +2. re-derived update-title control flow with common error return after every import block: objdiff 86.985180 -> 94.837036; instructions [135, 135] -> [133, 135]; diffs 76 -> 88; kept. +3. inode capacity condition places title progress operand first as original comparison: objdiff 94.837036 -> 94.962960; instructions [133, 135] -> [133, 135]; diffs 88 -> 88; kept. + +### __nupOp + +__nupOp: 90.413240%; instructions 422/438, diffs 391; boot/IOS/menu search and update loop scheduling; 16 target instructions remain unmatched. +Compiled source-level attempts: 3. + +1. re-derived every operation block, server ABI values, boot/IOS/menu update order and shared cleanup from target assembly: objdiff 47.045662 -> 90.150690; instructions [332, 438] -> [423, 438]; diffs 428 -> 267; kept. +2. capture validated boot version in 16-bit local before server call: objdiff 90.150690 -> 90.641556; instructions [423, 438] -> [421, 438]; diffs 267 -> 414; kept. +3. unsigned offset loop for final update pass preserves pointer traversal and title reloads: objdiff 90.641556 -> 90.175800; instructions [421, 438] -> [421, 438]; diffs 414 -> 414; restored. + +## Source-order audit + +```json +[ + { + "unit": "libs/RVL_SDK/src/fa/driver/sd_drv", + "target_functions": 26, + "source_functions_found": 26, + "missing": [], + "source_order_matches_object": true + }, + { + "unit": "libs/RVL_SDK/src/wad/wad", + "target_functions": 40, + "source_functions_found": 40, + "missing": [], + "source_order_matches_object": true + }, + { + "unit": "libs/RVL_SDK/src/vi/vi", + "target_functions": 30, + "source_functions_found": 30, + "missing": [], + "source_order_matches_object": true + }, + { + "unit": "libs/RVL_SDK/src/nup/nup", + "target_functions": 23, + "source_functions_found": 23, + "missing": [], + "source_order_matches_object": true + } +] +``` + +## String offset audit + +```json +[ + { + "unit": "libs/RVL_SDK/src/fa/driver/sd_drv", + "strings": 46, + "sequence_matches": true, + "offset_divergences": [] + }, + { + "unit": "libs/RVL_SDK/src/wad/wad", + "strings": 18, + "sequence_matches": true, + "offset_divergences": [] + }, + { + "unit": "libs/RVL_SDK/src/vi/vi", + "strings": 7, + "sequence_matches": true, + "offset_divergences": [ + { + "string_index": 0, + "source_offset": 772, + "original_offset": 956 + }, + { + "string_index": 1, + "source_offset": 816, + "original_offset": 1000 + }, + { + "string_index": 2, + "source_offset": 860, + "original_offset": 1044 + }, + { + "string_index": 3, + "source_offset": 904, + "original_offset": 1088 + }, + { + "string_index": 4, + "source_offset": 948, + "original_offset": 1132 + }, + { + "string_index": 5, + "source_offset": 992, + "original_offset": 1176 + }, + { + "string_index": 6, + "source_offset": 1036, + "original_offset": 1220 + } + ] + }, + { + "unit": "libs/RVL_SDK/src/nup/nup", + "strings": 28, + "sequence_matches": true, + "offset_divergences": [] + } +] +``` + +## Review corrections + +- WAD source blocks were reordered to original function address order while preserving effective inlining settings and all baseline exact functions. +- The partial random-word path draws one random word; an intermediate unused extra draw was removed during review. +- The original four-bit WAD count traversal was restored after whole-unit verification showed that the simple loop worsened its inlined consumers. The final whole-unit fuzzy score is higher. +- The NUP boot-version local passed to the server is initialized; size-output locals are consumed only after successful API queries. +- The NUP audit header stores a 32-bit format word. Its field name was corrected from titleId to format. +- The failed serial-number path copies the empty string identified at target small-data offset 0x10. Removing the duplicate unknown literal restores every NUP data section to 100 percent without changing instruction scores. +- VI shutdown event 4 through 6 returns success, and out-of-range events return the false final argument as observed in target assembly. + +## Uncertainties + +- pfd_sddrv_get_disk_info: original symbol size excludes the last addi/blr; no symbol metadata was changed. +- __nupGetBootVersion: original symbol name describes one argument, while target instructions and caller pass context in r3 and title in r4. The existing readable two-argument declaration was preserved, so exact-name objdiff cannot associate this function. +- Register allocation is an observed remaining difference in several functions, not proof that no source-level solution exists. +- WAD data remains zero matched bytes even with an identical string pool; no data placement tricks or linking changes were made. +- VI has 184 fewer bytes before its string pool. The original data after the 25 filter taps resembles three GX render-mode records, but no target reference proves their ownership or intended declarations. No unused objects were added merely to move the pool. pool_diff.py and gate.py compare string contents/order, not their offsets; the stricter audit above records this unresolved gap. diff --git a/tools/decomp-assist/sol-high-sdk-gate.txt b/tools/decomp-assist/sol-high-sdk-gate.txt new file mode 100644 index 00000000..ff5bc0e8 --- /dev/null +++ b/tools/decomp-assist/sol-high-sdk-gate.txt @@ -0,0 +1,99 @@ +full build: ok +main.dol sha1: 26116613f624061ba99c8d1a299aaa6efa85670d +[libs/RVL_SDK/src/fa/driver/sd_drv] pool: IDENTICAL +[libs/RVL_SDK/src/fa/driver/sd_drv] objdiff: code 2100/11752 data 408/3592 functions 10/26 fuzzy 94.6276 linked code 0 +[libs/RVL_SDK/src/fa/driver/sd_drv] instruction-exact functions: 10/26 +[libs/RVL_SDK/src/fa/driver/sd_drv] section .bss size 608 match 97.36842 +[libs/RVL_SDK/src/fa/driver/sd_drv] section .data size 2576 match 99.96117 +[libs/RVL_SDK/src/fa/driver/sd_drv] section .rodata size 368 match 100.0 +[libs/RVL_SDK/src/fa/driver/sd_drv] section .sbss size 40 match 100.0 +[libs/RVL_SDK/src/fa/driver/sd_drv] section .text size 11760 match 94.62764 +[libs/RVL_SDK/src/fa/driver/sd_drv] below 100: pfd_st_inter_callback 93.840576 +[libs/RVL_SDK/src/fa/driver/sd_drv] below 100: pfd_st_removal_callback 90.30303 +[libs/RVL_SDK/src/fa/driver/sd_drv] below 100: pfd_sddrv_init 86.104164 +[libs/RVL_SDK/src/fa/driver/sd_drv] below 100: pfd_sddrv_unmount 90.0 +[libs/RVL_SDK/src/fa/driver/sd_drv] below 100: pfd_sddrv_finalize 92.87719 +[libs/RVL_SDK/src/fa/driver/sd_drv] below 100: pfd_sddrv_get_disk_info 97.916664 +[libs/RVL_SDK/src/fa/driver/sd_drv] below 100: pfd_sddrv_physical_read 99.92126 +[libs/RVL_SDK/src/fa/driver/sd_drv] below 100: pfd_sddrv_physical_write 99.92126 +[libs/RVL_SDK/src/fa/driver/sd_drv] below 100: pfd_sddrv_get_total_sectors 90.9375 +[libs/RVL_SDK/src/fa/driver/sd_drv] below 100: pfd_sddrv_calc_mbr_bpb 91.76878 +[libs/RVL_SDK/src/fa/driver/sd_drv] below 100: pfd_sddrv_store_bpb_buf 99.34251 +[libs/RVL_SDK/src/fa/driver/sd_drv] below 100: pfd_sddrv_store_mbr_buf 94.36633 +[libs/RVL_SDK/src/fa/driver/sd_drv] below 100: pfd_sddrv_calc_fat32_mbr_bpb 89.909775 +[libs/RVL_SDK/src/fa/driver/sd_drv] below 100: pfd_sddrv_store_fat32_mbr_buf 77.50725 +[libs/RVL_SDK/src/fa/driver/sd_drv] below 100: pfd_sddrv_store_fat32_bpb_buf 96.4946 +[libs/RVL_SDK/src/fa/driver/sd_drv] below 100: pfd_sddrv_build_fat32_mbr_bpb 94.75225 +[libs/RVL_SDK/src/fa/driver/sd_drv] baseline: code 2100/11752 data 408 functions 10 fuzzy 70.5225 +[libs/RVL_SDK/src/wad/wad] pool: IDENTICAL +[libs/RVL_SDK/src/wad/wad] objdiff: code 7080/24500 data None/528 functions 23/40 fuzzy 80.8216 linked code 0 +[libs/RVL_SDK/src/wad/wad] instruction-exact functions: 23/40 +[libs/RVL_SDK/src/wad/wad] section .data size 464 match 99.34924 +[libs/RVL_SDK/src/wad/wad] section .sdata size 64 match 84.375 +[libs/RVL_SDK/src/wad/wad] section .text size 24500 match 80.82155 +[libs/RVL_SDK/src/wad/wad] below 100: WADImportGetBlocks 76.55369 +[libs/RVL_SDK/src/wad/wad] below 100: WAD_815BFFA8 97.17742 +[libs/RVL_SDK/src/wad/wad] below 100: WADImportEx 71.91361 +[libs/RVL_SDK/src/wad/wad] below 100: WAD_815C1288 96.39344 +[libs/RVL_SDK/src/wad/wad] below 100: WADBackupEx 59.089455 +[libs/RVL_SDK/src/wad/wad] below 100: _WADCheckContents 96.70588 +[libs/RVL_SDK/src/wad/wad] below 100: WADOpenStream 87.35468 +[libs/RVL_SDK/src/wad/wad] below 100: _WADUnpackBackup 94.867256 +[libs/RVL_SDK/src/wad/wad] below 100: _WADGetCidxCount 95.05 +[libs/RVL_SDK/src/wad/wad] below 100: _WADGetCidx 99.14286 +[libs/RVL_SDK/src/wad/wad] below 100: _WADBackupGetFiles 46.195583 +[libs/RVL_SDK/src/wad/wad] below 100: _WADBackupGetSize 86.77419 +[libs/RVL_SDK/src/wad/wad] below 100: _WADRandPad 87.30769 +[libs/RVL_SDK/src/wad/wad] below 100: WAD_815C43E0 97.17742 +[libs/RVL_SDK/src/wad/wad] below 100: _WADHash 71.103096 +[libs/RVL_SDK/src/wad/wad] below 100: WADVerify 86.863945 +[libs/RVL_SDK/src/wad/wad] below 100: WADImportDVDExForBS 89.330795 +[libs/RVL_SDK/src/wad/wad] baseline: code 6680/24500 data None functions 21 fuzzy 77.3980 +[libs/RVL_SDK/src/vi/vi] pool: IDENTICAL +[libs/RVL_SDK/src/vi/vi] objdiff: code 2084/11296 data 400/1944 functions 18/30 fuzzy 85.1516 linked code 0 +[libs/RVL_SDK/src/vi/vi] instruction-exact functions: 18/30 +[libs/RVL_SDK/src/vi/vi] section .bss size 368 match 100.0 +[libs/RVL_SDK/src/vi/vi] section .data size 1368 match 3.5053554 +[libs/RVL_SDK/src/vi/vi] section .sbss size 176 match 90.243904 +[libs/RVL_SDK/src/vi/vi] section .sdata size 32 match 100.0 +[libs/RVL_SDK/src/vi/vi] section .text size 11296 match 85.15156 +[libs/RVL_SDK/src/vi/vi] below 100: OnShutdown 81.039604 +[libs/RVL_SDK/src/vi/vi] below 100: __VIRetraceHandler 88.81396 +[libs/RVL_SDK/src/vi/vi] below 100: __VIInit 87.469696 +[libs/RVL_SDK/src/vi/vi] below 100: VIInit 85.06213 +[libs/RVL_SDK/src/vi/vi] below 100: VIWaitForRetrace 90.47619 +[libs/RVL_SDK/src/vi/vi] below 100: setFbbRegs 79.41954 +[libs/RVL_SDK/src/vi/vi] below 100: setHorizontalRegs 62.232143 +[libs/RVL_SDK/src/vi/vi] below 100: setVerticalRegs 57.950497 +[libs/RVL_SDK/src/vi/vi] below 100: VIConfigure 74.80726 +[libs/RVL_SDK/src/vi/vi] below 100: VIConfigurePan 88.80751 +[libs/RVL_SDK/src/vi/vi] below 100: VIFlush 78.91304 +[libs/RVL_SDK/src/vi/vi] below 100: __VIDisplayPositionToXY 82.624115 +[libs/RVL_SDK/src/vi/vi] baseline: code 2084/11296 data 400 functions 18 fuzzy 83.3782 +[libs/RVL_SDK/src/nup/nup] pool: IDENTICAL +[libs/RVL_SDK/src/nup/nup] objdiff: code 2840/10764 data 1720/1720 functions 13/23 fuzzy 82.3081 linked code 0 +[libs/RVL_SDK/src/nup/nup] instruction-exact functions: 13/23 +[libs/RVL_SDK/src/nup/nup] section .data size 1592 match 100.0 +[libs/RVL_SDK/src/nup/nup] section .rodata size 88 match 100.0 +[libs/RVL_SDK/src/nup/nup] section .sbss size 8 match 100.0 +[libs/RVL_SDK/src/nup/nup] section .sdata size 32 match 100.0 +[libs/RVL_SDK/src/nup/nup] section .text size 10764 match 82.30807 +[libs/RVL_SDK/src/nup/nup] below 100: __nupParseServerInfo__FP14NUPContextInfoPcPcUx 62.53761 +[libs/RVL_SDK/src/nup/nup] below 100: __nupGetServerInfo__FPcPcUxPcPcUlPcUsUsUxUsPPc 96.75862 +[libs/RVL_SDK/src/nup/nup] below 100: __nupBase64Encode__FPUcPUcUl 71.61905 +[libs/RVL_SDK/src/nup/nup] below 100: __nupGetAuditData__FP14NUPContextInfoPPc 91.741806 +[libs/RVL_SDK/src/nup/nup] below 100: __nupGetBootVersion__FP14ESTitleVersion None +[libs/RVL_SDK/src/nup/nup] below 100: __nupGetTitleSize__FP12NUPTitleInfo 73.942444 +[libs/RVL_SDK/src/nup/nup] below 100: __nupGetContentFull__FP14NUPContextInfoP12NUPTitleInfoPcUl 94.204544 +[libs/RVL_SDK/src/nup/nup] below 100: __nupGetContentIncr__FP14NUPContextInfoP12NUPTitleInfoPc 90.42105 +[libs/RVL_SDK/src/nup/nup] below 100: __nupUpdateTitle__FP14NUPContextInfoP12NUPTitleInfoPcPc 94.96296 +[libs/RVL_SDK/src/nup/nup] below 100: __nupOp 90.41324 +[libs/RVL_SDK/src/nup/nup] baseline: code 2536/10764 data 128 functions 11 fuzzy 55.2887 +regressions vs baseline: 0 +global matched_code_percent: 82.01012 -> 82.03362 +global fuzzy_match_percent: 93.25916 -> 93.48553 +global complete_code_percent: 57.20994 -> 57.20994 +global matched_data_percent: 89.09599 -> 89.18286 +forbidden patterns added (net, per file): 0 +readability warnings (net, per file; must be 0 in the final result): 0 +GATE PASS