From 82e9007c9856685e37926b8b52fa1788720cc603 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Wed, 30 Sep 2026 04:20:05 +0000 Subject: [PATCH] fa: decompile 29 filesystem units from vf leads (worker dd4743d9) --- README.md | 4 +- libs/RVL_SDK/include/private/fa/pdm.h | 53 ++ libs/RVL_SDK/include/private/fa/pf_system.h | 17 + libs/RVL_SDK/include/private/fa/pf_volume.h | 96 ++++ libs/RVL_SDK/src/fa/driver/nand_drv.c | 535 ++++++++++++++++++ libs/RVL_SDK/src/fa/pdm_bpb.c | 99 ++++ libs/RVL_SDK/src/fa/pdm_disk.c | 161 ++++++ libs/RVL_SDK/src/fa/pdm_partition.c | 287 ++++++++++ libs/RVL_SDK/src/fa/pf_attach.c | 29 + libs/RVL_SDK/src/fa/pf_chdir.c | 31 + libs/RVL_SDK/src/fa/pf_cp932.c | 178 ++++++ libs/RVL_SDK/src/fa/pf_create.c | 36 ++ libs/RVL_SDK/src/fa/pf_detach.c | 13 + libs/RVL_SDK/src/fa/pf_fat12.c | 248 ++++++++ libs/RVL_SDK/src/fa/pf_fat16.c | 158 ++++++ libs/RVL_SDK/src/fa/pf_fat32.c | 168 ++++++ libs/RVL_SDK/src/fa/pf_filelock.c | 56 ++ libs/RVL_SDK/src/fa/pf_finfo.c | 13 + libs/RVL_SDK/src/fa/pf_fopen.c | 38 ++ libs/RVL_SDK/src/fa/pf_format.c | 13 + libs/RVL_SDK/src/fa/pf_fseek.c | 13 + libs/RVL_SDK/src/fa/pf_fsfirst.c | 30 + libs/RVL_SDK/src/fa/pf_fsnext.c | 15 + libs/RVL_SDK/src/fa/pf_getdev.c | 13 + libs/RVL_SDK/src/fa/pf_init_prfile2.c | 19 + libs/RVL_SDK/src/fa/pf_mkdir.c | 31 + libs/RVL_SDK/src/fa/pf_remove.c | 31 + libs/RVL_SDK/src/fa/pf_rmdir.c | 31 + libs/RVL_SDK/src/fa/pf_sector.c | 216 +++++++ libs/RVL_SDK/src/fa/pf_service.c | 24 + libs/RVL_SDK/src/fa/pf_sync.c | 13 + libs/RVL_SDK/src/fa/pf_system.c | 30 + libs/RVL_SDK/src/fa/pf_unmount.c | 13 + .../private/vf/PrFILE2/common/pf_str.h | 3 + .../vf/PrFILE2/local/pf_cp932_to_unicode.h | 44 +- tools/decomp-assist/fa_batch_attempts.md | 15 + 36 files changed, 2768 insertions(+), 6 deletions(-) create mode 100644 libs/RVL_SDK/include/private/fa/pdm.h create mode 100644 libs/RVL_SDK/include/private/fa/pf_system.h create mode 100644 libs/RVL_SDK/include/private/fa/pf_volume.h create mode 100644 libs/RVL_SDK/src/fa/driver/nand_drv.c create mode 100644 libs/RVL_SDK/src/fa/pdm_bpb.c create mode 100644 libs/RVL_SDK/src/fa/pdm_disk.c create mode 100644 libs/RVL_SDK/src/fa/pdm_partition.c create mode 100644 libs/RVL_SDK/src/fa/pf_attach.c create mode 100644 libs/RVL_SDK/src/fa/pf_chdir.c create mode 100644 libs/RVL_SDK/src/fa/pf_cp932.c create mode 100644 libs/RVL_SDK/src/fa/pf_create.c create mode 100644 libs/RVL_SDK/src/fa/pf_detach.c create mode 100644 libs/RVL_SDK/src/fa/pf_fat12.c create mode 100644 libs/RVL_SDK/src/fa/pf_fat16.c create mode 100644 libs/RVL_SDK/src/fa/pf_fat32.c create mode 100644 libs/RVL_SDK/src/fa/pf_filelock.c create mode 100644 libs/RVL_SDK/src/fa/pf_finfo.c create mode 100644 libs/RVL_SDK/src/fa/pf_fopen.c create mode 100644 libs/RVL_SDK/src/fa/pf_format.c create mode 100644 libs/RVL_SDK/src/fa/pf_fseek.c create mode 100644 libs/RVL_SDK/src/fa/pf_fsfirst.c create mode 100644 libs/RVL_SDK/src/fa/pf_fsnext.c create mode 100644 libs/RVL_SDK/src/fa/pf_getdev.c create mode 100644 libs/RVL_SDK/src/fa/pf_init_prfile2.c create mode 100644 libs/RVL_SDK/src/fa/pf_mkdir.c create mode 100644 libs/RVL_SDK/src/fa/pf_remove.c create mode 100644 libs/RVL_SDK/src/fa/pf_rmdir.c create mode 100644 libs/RVL_SDK/src/fa/pf_sector.c create mode 100644 libs/RVL_SDK/src/fa/pf_service.c create mode 100644 libs/RVL_SDK/src/fa/pf_sync.c create mode 100644 libs/RVL_SDK/src/fa/pf_system.c create mode 100644 libs/RVL_SDK/src/fa/pf_unmount.c create mode 100644 tools/decomp-assist/fa_batch_attempts.md diff --git a/README.md b/README.md index 968d85d1..ecd4f88f 100644 --- a/README.md +++ b/README.md @@ -53,9 +53,9 @@ Progress | Decompiled | Matched | Linked | Data | |:---:|:---:|:---:|:---:| -| 86.57% | 76.50% | 56.92% | 87.26% | +| 87.35% | 77.13% | 56.92% | 88.21% | -units 752/1027 complete, functions 11199/12563 matched. +units 752/1027 complete, functions 11293/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/include/private/fa/pdm.h b/libs/RVL_SDK/include/private/fa/pdm.h new file mode 100644 index 00000000..358704a4 --- /dev/null +++ b/libs/RVL_SDK/include/private/fa/pdm.h @@ -0,0 +1,53 @@ +#ifndef PF_FA_PDM_H +#define PF_FA_PDM_H +#include +typedef struct PDM_PARTITION PDM_PARTITION; +typedef struct PDM_CALLBACK { + void (*insert)(PDM_PARTITION*); + void (*eject)(PDM_PARTITION*); +} PDM_CALLBACK; +struct PDM_PARTITION { + pf_u32 status; + PDM_DISK* p_disk; + pf_u32 part_id; + pf_u32 start_sector; + pf_u32 total_sector; + pf_s32 driver_last_error; + pf_u32 lock_state[2]; + PDM_CALLBACK callbacks; +}; +struct PDM_DISK { + pf_u32 status; + PDM_DISK_TBL disk_tbl; + pf_s16 init_count; + pf_s16 mount_count; + PDM_DISK* lock_handle; + pf_u32 lock_count; + PDM_DISK_INFO disk_info; + PDM_PARTITION* p_cur_part; +}; +typedef struct PDM_DISK_SET { + pf_u16 num_partition; + pf_u16 num_allocated_disk; + PDM_DISK disk[26]; + PDM_PARTITION partition[26]; +} PDM_DISK_SET; +extern PDM_DISK_SET pdm_disk_set; +extern PDM_DISK_TBL pdm_drv_tbl[26]; +void pdm_part_set_driver_error_code(PDM_PARTITION*, pf_s32); +void pdm_part_set_change_media_state(PDM_DISK*, pf_bool); +pf_s32 pdm_disk_convert_sector_into_block(PDM_DISK*, pf_u32, pf_u32, pf_u32, pf_u32*, pf_u32*); +pf_s32 pdm_disk_convert_block_into_sector(PDM_DISK*, pf_u32, pf_u32, pf_u32, pf_u32*, pf_u32*); +pf_s32 pdm_disk_add_disk(PDM_INIT_DISK*, PDM_DISK**); +pf_s32 pdm_disk_del_disk(PDM_DISK*); +pf_s32 pdm_disk_set_disk(PDM_DISK*, PDM_PARTITION*); +pf_s32 pdm_disk_get_media_attribute(PDM_DISK*, PDM_DISK_INFO*); +pf_s32 pdm_disk_get_media_bps(PDM_DISK*, pf_u16*); +pf_s32 pdm_disk_init(PDM_DISK*); +pf_s32 pdm_disk_mount(PDM_DISK*); +pf_s32 pdm_disk_format(PDM_DISK*, const pf_u8*); +pf_s32 pdm_disk_physical_read(PDM_DISK*, pf_u8*, pf_u32, pf_u32, pf_u32, pf_u32*); +pf_s32 pdm_disk_physical_write(PDM_DISK*, const pf_u8*, pf_u32, pf_u32, pf_u32, pf_u32*); +pf_s32 pdm_disk_unmount(PDM_DISK*, pf_u32); +pf_s32 pdm_disk_finalize(PDM_DISK*); +#endif diff --git a/libs/RVL_SDK/include/private/fa/pf_system.h b/libs/RVL_SDK/include/private/fa/pf_system.h new file mode 100644 index 00000000..12607706 --- /dev/null +++ b/libs/RVL_SDK/include/private/fa/pf_system.h @@ -0,0 +1,17 @@ +#ifndef PF_FA_SYSTEM_LAYOUT_H +#define PF_FA_SYSTEM_LAYOUT_H +#include + +typedef struct PFSYS_SYSTEM_SET { + u32 initialized; + s32 task_id; + s32 mailbox_id; + u8 task_stack[0x2000]; + OSThread task; + OSMessageQueue mailbox; + OSMessage messages[20]; + u32 flock_count; +} PFSYS_SYSTEM_SET; + +extern PFSYS_SYSTEM_SET pf_sys_set; +#endif diff --git a/libs/RVL_SDK/include/private/fa/pf_volume.h b/libs/RVL_SDK/include/private/fa/pf_volume.h new file mode 100644 index 00000000..f7398c26 --- /dev/null +++ b/libs/RVL_SDK/include/private/fa/pf_volume.h @@ -0,0 +1,96 @@ +#ifndef PF_FA_VOLUME_LAYOUT_H +#define PF_FA_VOLUME_LAYOUT_H +#define PRFILE2_VFMOD_PF_VOLUME_H +#include +#include +#include +typedef struct PF_CHARCODE { + pf_s32 (*oem2unicode)(const pf_s8*, pf_u16*); + pf_s32 (*unicode2oem)(const pf_u16*, pf_s8*); + pf_s32 (*oem_char_width)(const pf_s8*); + pf_bool (*is_oem_mb_char)(const pf_s8, pf_bool); + pf_s32 (*unicode_char_width)(const pf_u16*); + pf_bool (*is_unicode_mb_char)(const pf_u16, pf_bool); +} PF_CHARCODE; + +typedef struct PF_BPB { + pf_u16 bytes_per_sector; + pf_u16 num_reserved_sectors; + pf_u16 num_root_dir_entries; + pf_u8 sectors_per_cluster; + pf_u8 num_FATs; + pf_u32 total_sectors; + pf_u32 sectors_per_FAT; + pf_u32 root_dir_cluster; + pf_u16 fs_info_sector; + pf_u16 backup_boot_sector; + pf_u16 ext_flags; + pf_u8 media; + PF_FAT_TYPE fat_type; + pf_u8 log2_bytes_per_sector; + pf_u8 log2_sectors_per_cluster; + pf_u8 num_active_FATs; + pf_u16 num_root_dir_sectors; + pf_u32 active_FAT_sector; + pf_u32 first_root_dir_sector; + pf_u32 first_data_sector; + pf_u32 num_clusters; +} PF_BPB; + +typedef struct PF_CUR_DIR { + pf_u32 stat; + pf_s32 context_id; + PF_DIR_ENT directory; +} PF_CUR_DIR; + + +typedef pf_s32 (*PF_VOLUME_CB)(pf_s32); +struct PF_VOLUME { + PF_BPB bpb; + pf_u32 num_free_clusters; + pf_u32 last_free_cluster; + pf_u8 file_and_directory_state[0x1624 - 0x40]; + PF_SECTOR_CACHE cache; + pf_s8 label[12]; + PF_CUR_DIR current_dir[4]; + struct { + pf_u32 tracker_size; + pf_u32 tracker_max; + pf_u32* tracker_bits; + } tail_entry; + pf_s32 last_error; + pf_s32 last_driver_error; + pf_u32 file_config; + pf_u16 flags; + pf_s8 drv_char; + pf_u16 fsi_flag; + PF_CLUSTER_LINK_VOL cluster_link; + void* p_part; + void (*p_callback)(); + const pf_u8* format_param; +}; +typedef struct PF_FA_CUR_VOLUME { + pf_u32 stat; + pf_s32 context_id; + PF_VOLUME* p_vol; +} PF_FA_CUR_VOLUME; +typedef struct PF_VOLUME_SET { + pf_u32 flags; + pf_u32 reserved; + PF_VOLUME* current_volume; + PF_FA_CUR_VOLUME context_volumes[3]; + pf_s32 num_attached_drives; + pf_s32 num_mounted_volumes; + pf_u32 config; + void* param; + pf_s32 last_error; + pf_s32 last_driver_error; + PF_CHARCODE codeset; + pf_u32 setting; + pf_s32 context_id; + pf_u32 context_state[2]; + struct { pf_u32 stat; pf_s32 context_id; } contexts[3]; + PF_VOLUME volumes[26]; +} PF_VOLUME_SET; +extern PF_VOLUME_SET pf_vol_set; +#endif diff --git a/libs/RVL_SDK/src/fa/driver/nand_drv.c b/libs/RVL_SDK/src/fa/driver/nand_drv.c new file mode 100644 index 00000000..e0b3105a --- /dev/null +++ b/libs/RVL_SDK/src/fa/driver/nand_drv.c @@ -0,0 +1,535 @@ +#include +#include +#include +extern void* pf_memset(void*, u8, u32); +extern void* pf_memcpy(void*, const void*, u32); +extern s32 pf_strcmp(const char*, const char*); +extern char* pf_strcpy(char*, const char*); +extern u32 pf_strlen(const char*); +extern void pdm_disk_notify_media_insert(PDM_DISK*); +typedef struct NAND_DISK_INFO { + PDM_DISK* disk; + char path[80]; + u32 file_size; + NANDFileInfo file; + u32 flags; + u32 reserved_sectors; + s32 last_error; +} NAND_DISK_INFO; +typedef struct NAND_SEMAPHORE { s32 count; s32 maximum; void* object; } NAND_SEMAPHORE; +extern s32 pfk_create_semaphore(NAND_SEMAPHORE*); +extern void pfk_get_semaphore(s32); +extern void pfk_release_semaphore(s32); +char NAND_Org_Path[64]; +NAND_DISK_INFO nanddisk_info[23]; +NAND_SEMAPHORE csem; +NAND_SEMAPHORE osem; +s32 Nanddisk_Internal_Info_Init; +s32 NAND_Init; +s32 fa_nanad_semid; + +static inline s32 find_disk(PDM_DISK* disk, NAND_DISK_INFO** result) { + NAND_DISK_INFO* info; + u16 index; + for (index = 0; index < 23; index++) { + info = &nanddisk_info[index]; + if (info->disk == disk) { *result = info; break; } + } + return index == 23 ? -22 : 0; +} +static inline s32 set_disk_error(PDM_DISK* disk, s32 error) { + u16 index; + for (index = 0; index < 23; index++) { + if (nanddisk_info[index].disk == disk) { nanddisk_info[index].last_error = error; break; } + } + return index == 23 ? -22 : 0; +} +static inline void store_disk_error(PDM_DISK* disk, s32 error) { + u16 index; + for (index = 0; index < 23; index++) { + if (nanddisk_info[index].disk == disk) { nanddisk_info[index].last_error = error; break; } + } +} +static inline NAND_DISK_INFO* find_unmounted_disk(PDM_DISK* disk) { + u16 index; + NAND_DISK_INFO* info; + for (index = 0; index < 23; index++) { + info = &nanddisk_info[index]; + if (info->disk == disk) { if (!(info->flags & 2)) { return info; } } + } + for (index = 0; index < 23; index++) { + info = &nanddisk_info[index]; + if (info->disk == 0) { if (info->path[0] != 0) { return info; } } + } + return 0; +} +static inline u16 read_u16(u8* buf, u32 offset) { + if ((u32)&buf[offset] & 1) { return (buf[offset + 1] << 8) | buf[offset]; } + return PF_SWAP_16(*(u16*)&buf[(offset + 1) & ~1]); +} +static inline void write_u16(u8* buf, u32 offset, u16 value) { + if ((u32)&buf[offset] & 1) { buf[offset] = value; buf[offset + 1] = value >> 8; } + else { *(u16*)&buf[(offset + 1) & ~1] = PF_SWAP_16(value); } +} +static inline void write_u32(u8* buf, u32 offset, u32 value) { + if ((u32)&buf[offset] & 3) { buf[offset] = value; buf[offset + 1] = value >> 8; buf[offset + 2] = value >> 16; buf[offset + 3] = value >> 24; } + else { *(u32*)&buf[offset] = PF_SWAP_32(value); } +} +static s32 fa_nanddrv_CreateNANDFileForDsk(NAND_DISK_INFO*); +static s32 fa_nanddrv_ParseCreateNANDFile(NAND_DISK_INFO*); +static s32 fa_nanddrv_VerifyBPB(u8*, u32*); +static s32 fa_nanddrv_BuildUpFSInfoSector(u8*); +static s32 fa_nanddrv_BuildUpBootSector(NAND_DISK_INFO*, u8*, PDM_FAT_TYPE*, u32); +static s32 fa_nanddrv_physical_read(u32, u8*, u32, u32, u32*, PDM_DISK*); +static s32 fa_nanddrv_physical_write(u32, const u8*, u32, u32, u32*, PDM_DISK*); + +static s32 fa_nanddrv_RelateNANDFileToDsk(PDM_DISK* disk, NAND_DISK_INFO* info, s32 format) { + s32 error = NANDChangeDir(NAND_Org_Path); + if (error != 0) { return error; } + if (info->flags & 4) { error = 0; } + else { error = NANDOpen(info->path, &info->file, 3); } + if (format) { error = -12; } + switch (error) { + case -12: + error = fa_nanddrv_CreateNANDFileForDsk(info); + if (error == 0) { info->disk = disk; } + break; + case 0: + info->disk = disk; + error = 0; + break; + } + return error; +} +static s32 fa_nanddrv_CreateNANDFileForDsk(NAND_DISK_INFO* info) { + u8 zeros[512] __attribute__((aligned(32))); + u8 boot[512] __attribute__((aligned(32))); + u8 fsinfo[512] __attribute__((aligned(32))); + PDM_FAT_TYPE type; + u32 clusters; + s32 error; + u32 remaining; + error = fa_nanddrv_ParseCreateNANDFile(info); + if (error != 0) { return error; } + pf_memset(boot, 0, 512); + clusters = 0; + fa_nanddrv_BuildUpBootSector(info, boot, &type, 0); + if (type != fa_nanddrv_VerifyBPB(boot, &clusters)) { fa_nanddrv_BuildUpBootSector(info, boot, &type, clusters); } + error = NANDWrite(&info->file, boot, 512); + if (error < 0) { NANDClose(&info->file); NANDDelete(info->path); return error; } + remaining = info->file_size - 512; + if (type == PDM_FAT_32) { + fa_nanddrv_BuildUpFSInfoSector(fsinfo); + error = NANDWrite(&info->file, fsinfo, 512); + if (error < 0) { NANDClose(&info->file); NANDDelete(info->path); return error; } + remaining -= 512; + } + pf_memset(zeros, 0, 512); + for (; remaining >= 512; remaining -= 512) { + error = NANDWrite(&info->file, zeros, 512); + if (error < 0) { NANDClose(&info->file); NANDDelete(info->path); return error; } + } + if (remaining) { + error = NANDWrite(&info->file, zeros, remaining); + if (error < 0) { NANDClose(&info->file); NANDDelete(info->path); return error; } + } + return 0; +} +static s32 fa_nanddrv_ParseCreateNANDFile(NAND_DISK_INFO* info) { + u16 length = 0; + char* path; + u16 position = 0; + char name[16]; + char* next; + s32 error; + if (info->flags & 4) { return 0; } + path = info->path; + pf_memset(name, 0, 16); + next = name; + for (;;) { + switch (*path) { + case '/': + if (length == 0) { + if (position == 0) { error = NANDChangeDir("/"); if (error != 0) { return error; } } + path++; + break; + } + if (path[1] == 0) { + if (length > 12) { return -20; } + error = NANDCreate(name, 0x30, 0); + if (error == 0 || error == -6) { + NANDOpen(name, &info->file, 3); + error = NANDChangeDir(NAND_Org_Path); + } + return error; + } + if (length > 11) { return -20; } + if (pf_strcmp(name, "..") != 0) { error = NANDCreateDir(name, 0x30, 0); } + else { error = 0; } + if (error == 0 || error == -6 || error == -1) { + error = NANDChangeDir(name); + if (error != 0) { return error; } + path++; + pf_memset(name, 0, 16); + next = name; + length = 0; + break; + } + return error; + case 0: + if (length > 12) { return -20; } + error = NANDCreate(name, 0x30, 0); + if (error == 0 || error == -6) { + NANDOpen(name, &info->file, 3); + error = NANDChangeDir(NAND_Org_Path); + } + return error; + } + *next = *path; + length++; + position++; + path++; + next++; + } +} +static s32 fa_nanddrv_VerifyBPB(u8* buf, u32* clusters) { + u8 sector_log = 0; + u8 cluster_log; + u16 value = 512; + u32 root; + u32 fat; + u32 total; + u16 root_sectors; + PDM_FAT_TYPE result; + u32 first; + while ((value >>= 1) != 0) { sector_log++; } + cluster_log = 0; + value = 1; + while ((value >>= 1) != 0) { cluster_log++; } + if ((u32)&buf[17] & 1) { root = (buf[18] << 8) | buf[17]; } + else { root = PF_SWAP_16(*(u16*)&buf[18]) << 5; } + root_sectors = (s32)(root + 511) >> sector_log; + fat = read_u16(buf, 22); + if (fat == 0) { fat = PF_SWAP_32(*(u32*)&buf[36]); } + first = root_sectors + fat * 2 + 1; + if ((u32)&buf[19] & 1) { total = (buf[20] << 8) | buf[19]; } + else { total = PF_SWAP_16(*(u16*)&buf[20]); } + if (total == 0) { total = PF_SWAP_32(*(u32*)&buf[32]); } + *clusters = total - first; + total = (u32)(total - first) >> cluster_log; + if (total < 4085) { return 0; } + result = PDM_FAT_32; + if ((u32)total < 65525) { result = PDM_FAT_16; } + return result; +} +#pragma dont_inline on +static s32 fa_nanddrv_BuildUpFSInfoSector(u8* buf) { + pf_memset(buf, 0, 512); + if ((u32)&buf[0] & 3) { buf[0] = 0x52; buf[1] = 0x52; buf[2] = 0x61; buf[3] = 0x41; } + else { *(u32*)&buf[0] = 0x52526141; } + if ((u32)&buf[484] & 3) { buf[484] = 0x72; buf[485] = 0x72; buf[486] = 0x41; buf[487] = 0x61; } + else { *(u32*)&buf[484] = 0x72724161; } + if ((u32)&buf[488] & 3) { buf[488] = 0xFF; buf[489] = 0xFF; buf[490] = 0xFF; buf[491] = 0xFF; } + else { *(u32*)&buf[488] = 0xFFFFFFFF; } + if ((u32)&buf[492] & 3) { buf[492] = 0xFF; buf[493] = 0xFF; buf[494] = 0xFF; buf[495] = 0xFF; } + else { *(u32*)&buf[492] = 0xFFFFFFFF; } + if ((u32)&buf[508] & 3) { buf[508] = 0x00; buf[509] = 0x00; buf[510] = 0x55; buf[511] = 0xAA; } + else { *(u32*)&buf[508] = 0x000055AA; } + return 0; +} +#pragma dont_inline reset +static s32 fa_nanddrv_BuildUpBootSector(NAND_DISK_INFO* info, u8* buf, PDM_FAT_TYPE* type, u32 clusters) { + u32 total; + u32 fat_sectors; + PDM_FAT_TYPE fat_type; + total = clusters ? clusters : info->file_size >> 9; + if (total < 4085) { fat_type = PDM_FAT_12; fat_sectors = (((total * 3) >> 1) + 511) >> 9; } + else if (total < 65525) { fat_type = PDM_FAT_16; fat_sectors = (total * 2 + 511) >> 9; } + else { fat_type = PDM_FAT_32; fat_sectors = (total * 4 + 511) >> 9; } + pf_memset(buf, 0, 512); + buf[0] = 0xEB; buf[2] = 0x90; buf[510] = 0x55; buf[511] = 0xAA; + write_u16(buf, 24, 63); + write_u16(buf, 26, 255); + write_u16(buf, 11, 512); + buf[13] = 1; + write_u16(buf, 14, 1); + buf[16] = 2; + write_u16(buf, 17, 128); + buf[21] = 0xF0; + if (total < 65536) { write_u16(buf, 19, total); write_u32(buf, 32, 0); } + else { write_u16(buf, 19, 0); write_u32(buf, 32, total); } + if (fat_type == PDM_FAT_32) { + write_u16(buf, 17, 0); + write_u32(buf, 36, fat_sectors); + write_u16(buf, 40, 0); write_u16(buf, 42, 0); + write_u32(buf, 44, 2); write_u16(buf, 48, 1); + buf[65] = 0; buf[66] = 0x29; + if ((u32)&buf[67] & 3) { buf[67] = 0x34; buf[68] = 0x12; buf[69] = 0; buf[70] = 0; } + else { *(u32*)&buf[76] = 0x34120000; } + } else { + write_u16(buf, 17, 128); write_u16(buf, 22, fat_sectors); + buf[37] = 0; buf[38] = 0x29; + if ((u32)&buf[39] & 3) { buf[39] = 0x34; buf[40] = 0x12; buf[41] = 0; buf[42] = 0; } + else { *(u32*)&buf[48] = 0x34120000; } + } + *type = fat_type; + return 0; +} +s32 fa_nanddrv_init(PDM_DISK* disk) { + if (disk == 0) { return -20; } + if (NAND_Init == 0) { + if (NANDInit() != 0) { return -21; } + if (NANDGetCurrentDir(NAND_Org_Path) != 0) { return -21; } + NAND_Init++; + } + pdm_disk_notify_media_insert(disk); + return 0; +} +s32 fa_nanddrv_mount(PDM_DISK* disk) { + NAND_DISK_INFO* info; + NAND_DISK_INFO* mounted; + s32 error; + pfk_get_semaphore(fa_nanad_semid); + if (disk == 0) { pfk_release_semaphore(fa_nanad_semid); return -20; } + if (find_disk(disk, &mounted) == 0 && (mounted->flags & 2)) { pfk_release_semaphore(fa_nanad_semid); return -21; } + info = find_unmounted_disk(disk); + if (info == 0) { pfk_release_semaphore(fa_nanad_semid); return -22; } + if (info->disk != 0) { error = 0; } + else { error = fa_nanddrv_RelateNANDFileToDsk(disk, info, 0); } + if (error == 0) { info->flags |= 2; } + pfk_release_semaphore(fa_nanad_semid); + return error; +} +s32 fa_nanddrv_format(PDM_DISK* disk, const u8* param) { + NAND_DISK_INFO* info = 0; + s32 error; + pfk_get_semaphore(fa_nanad_semid); + if (disk == 0) { pfk_release_semaphore(fa_nanad_semid); return -20; } + if (find_disk(disk, &info) != 0) { pfk_release_semaphore(fa_nanad_semid); return -20; } + info->flags |= 4; + error = fa_nanddrv_RelateNANDFileToDsk(disk, info, 1); + info->flags &= ~4; + pfk_release_semaphore(fa_nanad_semid); + return error; +} +s32 fa_nanddrv_pread(PDM_DISK* disk, u8* buf, u32 block, u32 count, u32* success) { + s32 error; + pfk_get_semaphore(fa_nanad_semid); + *success = 0; + if (disk == 0) { pfk_release_semaphore(fa_nanad_semid); return -20; } + if (buf == 0) { pfk_release_semaphore(fa_nanad_semid); return -20; } + error = fa_nanddrv_physical_read(count, buf, block, 512, success, disk); + if (error != 0) { pfk_release_semaphore(fa_nanad_semid); return error; } + pfk_release_semaphore(fa_nanad_semid); + return 0; +} +s32 fa_nanddrv_pwrite(PDM_DISK* disk, const u8* buf, u32 block, u32 count, u32* success) { + s32 error; + pfk_get_semaphore(fa_nanad_semid); + *success = 0; + if (disk == 0) { pfk_release_semaphore(fa_nanad_semid); return -20; } + if (buf == 0) { pfk_release_semaphore(fa_nanad_semid); return -20; } + error = fa_nanddrv_physical_write(count, buf, block, 512, success, disk); + if (error != 0) { pfk_release_semaphore(fa_nanad_semid); return error; } + pfk_release_semaphore(fa_nanad_semid); + return 0; +} +s32 fa_nanddrv_unmount(PDM_DISK* disk) { + NAND_DISK_INFO* info = 0; + u32 position; + s32 error; + s32 result; + pfk_get_semaphore(fa_nanad_semid); + if (disk == 0) { pfk_release_semaphore(fa_nanad_semid); return -20; } + error = set_disk_error(disk, 0); + if (error == 0) { + if (find_disk(disk, &info) != 0) { pfk_release_semaphore(fa_nanad_semid); return -20; } + info->flags &= ~2; + error = NANDTell(&info->file, &position); + if (error != 0) { result = error; goto flush_done; } + error = NANDClose(&info->file); + if (error < 0) { result = error; goto flush_done; } + error = NANDOpen(info->path, &info->file, 3); + if (error < 0) { result = error; goto flush_done; } + error = NANDSeek(&info->file, position, 0); + result = error < 0 ? error : 0; +flush_done: + error = result; + if (error < 0) { set_disk_error(disk, error); pfk_release_semaphore(fa_nanad_semid); return error; } + } + pfk_release_semaphore(fa_nanad_semid); + return error; +} +s32 fa_nanddrv_finalize(PDM_DISK* disk) { + NAND_DISK_INFO* info; + s32 error = 0; + u16 index; + pfk_get_semaphore(fa_nanad_semid); + if (disk == 0) { pfk_release_semaphore(fa_nanad_semid); return -20; } + for (index = 0; index < 23; index++) { + info = &nanddisk_info[index]; + if (info->disk == disk) { + error = NANDClose(&info->file); + pf_memset(info, 0, sizeof(*info)); + break; + } + } + pfk_release_semaphore(fa_nanad_semid); + if (error != 0) { return -20; } + return index == 23 ? -22 : 0; +} +s32 fa_nanddrv_get_disk_info(PDM_DISK* disk, PDM_DISK_INFO* disk_info) { + NAND_DISK_INFO* info = 0; + pfk_get_semaphore(fa_nanad_semid); + if (disk == 0 || disk_info == 0) { pfk_release_semaphore(fa_nanad_semid); return -20; } + if (find_disk(disk, &info) != 0) { pfk_release_semaphore(fa_nanad_semid); return -20; } + disk_info->total_sectors = info->file_size >> 9; + disk_info->cylinders = disk_info->total_sectors / 255 / 63 / 512; + disk_info->heads = 255; + disk_info->sectors_per_track = 63; + disk_info->bytes_per_sector = 512; + disk_info->media_attr = 0; + disk_info->format_param = 0; + pfk_release_semaphore(fa_nanad_semid); + return 0; +} +const PDM_FUNCTBL fa_nand_func = { + fa_nanddrv_init, fa_nanddrv_finalize, fa_nanddrv_mount, fa_nanddrv_unmount, + fa_nanddrv_format, fa_nanddrv_pread, fa_nanddrv_pwrite, fa_nanddrv_get_disk_info +}; +s32 fa_nanddrv_init_drv_tbl(PDM_DISK_TBL* table, u32 extension) { + table->ui_ext = extension; + table->p_func = (PDM_FUNCTBL*)&fa_nand_func; + return 0; +} +s32 fa_nanddrv_NotifyNANDFile(const char* path, u32 size) { + NAND_DISK_INFO* info; + u16 index; + if (pf_strlen(path) >= 76) { return -20; } + if (Nanddisk_Internal_Info_Init == 0) { + pf_memset(nanddisk_info, 0, sizeof(nanddisk_info)); + Nanddisk_Internal_Info_Init++; + csem.count = 1; + csem.maximum = 1; + csem.object = &osem; + fa_nanad_semid = pfk_create_semaphore(&csem); + if (fa_nanad_semid == 0) { return -21; } + } + for (index = 0; index < 23; index++) { + info = &nanddisk_info[index]; + if (pf_strcmp(info->path, path) == 0) { return 0; } + if (nanddisk_info[index].disk == 0) { + pf_strcpy(info->path, path); + info->file_size = size; + break; + } + } + return index == 23 ? -22 : 0; +} +static s32 fa_nanddrv_physical_read(u32 count, u8* buf, u32 block, u32 bps, u32* success, PDM_DISK* disk) { + u32 file_size; + u32 wanted; + u32 remaining; + s32 error; + NAND_DISK_INFO* info = 0; + u16 index; + u32 offset; + u32 position; + u8 work[512] __attribute__((aligned(32))); + for (index = 0; index < 23; index++) { + if (nanddisk_info[index].disk == disk) { info = &nanddisk_info[index]; break; } + } + if ((index == 23 ? -22 : 0) != 0) { return -20; } + remaining = count * bps; + file_size = info->file_size; + wanted = block * bps; + if (NANDTell(&info->file, &position) != 0 || position != wanted) { error = NANDSeek(&info->file, wanted, 0); } + else { error = 0; } + if (error >= 0) { + if (wanted + remaining > file_size) { + remaining = file_size - wanted; + offset = 0; + while (remaining >= 512) { + error = NANDRead(&info->file, work, 512); + if (error < 0) { return -22; } + pf_memcpy(buf + offset, work, error); + offset += error; remaining -= error; + } + if (remaining) { + error = NANDRead(&info->file, work, remaining); + if (error < 0) { return -22; } + pf_memcpy(buf + offset, work, error); + } + return -22; + } + offset = 0; + while (remaining >= 512) { + error = NANDRead(&info->file, work, 512); + if (error < 0) { store_disk_error(disk, error); return error; } + pf_memcpy(buf + offset, work, error); + offset += error; remaining -= error; + } + if (remaining) { + error = NANDRead(&info->file, work, remaining); + if (error < 0) { store_disk_error(disk, error); return error; } + pf_memcpy(buf + offset, work, error); + } + *success = count; + return 0; + } + store_disk_error(disk, error); + return error; +} +static s32 fa_nanddrv_physical_write(u32 count, const u8* buf, u32 block, u32 bps, u32* success, PDM_DISK* disk) { + u32 remaining; + u32 wanted; + u32 file_size; + NAND_DISK_INFO* info = 0; + u16 index; + u32 offset; + u32 position; + s32 error; + u8 work[512] __attribute__((aligned(32))); + for (index = 0; index < 23; index++) { + if (nanddisk_info[index].disk == disk) { info = &nanddisk_info[index]; break; } + } + if ((index == 23 ? -22 : 0) != 0) { return -20; } + remaining = count * bps; + file_size = info->file_size; + wanted = block * bps; + if (NANDTell(&info->file, &position) != 0 || position != wanted) { error = NANDSeek(&info->file, wanted, 0); } + else { error = 0; } + if (error >= 0) { + if (wanted + remaining > file_size) { + remaining = file_size - wanted; + offset = 0; + while (remaining >= 512) { + pf_memcpy(work, buf + offset, 512); + error = NANDWrite(&info->file, work, 512); + if (error < 0) { return -22; } + remaining -= error; offset += error; + } + if (remaining) { + pf_memcpy(work, buf + offset, remaining); + error = NANDWrite(&info->file, work, remaining); + } + return -22; + } + offset = 0; + while (remaining >= 512) { + pf_memcpy(work, buf + offset, 512); + error = NANDWrite(&info->file, work, 512); + if (error < 0) { store_disk_error(disk, error); return error; } + remaining -= error; offset += error; + } + if (remaining) { + pf_memcpy(work, buf + offset, remaining); + error = NANDWrite(&info->file, work, remaining); + if (error < 0) { store_disk_error(disk, error); return error; } + } + *success = count; + return 0; + } + store_disk_error(disk, error); + return error; +} diff --git a/libs/RVL_SDK/src/fa/pdm_bpb.c b/libs/RVL_SDK/src/fa/pdm_bpb.c new file mode 100644 index 00000000..6da33949 --- /dev/null +++ b/libs/RVL_SDK/src/fa/pdm_bpb.c @@ -0,0 +1,99 @@ +#define PF_FA_STR_LAYOUT +#include +#include +extern pf_bool pdm_part_is_boot_sector(pf_u8*); + +static inline pf_u16 read_boot_u16(pf_u8* buf, pf_u32 offset) { + if ((pf_u32)&buf[offset] & 1) { + return (buf[offset + 1] << 8) | buf[offset]; + } + return PF_SWAP_16(*(pf_u16*)&buf[(offset + 1) & ~1]); +} + +void pdm_bpb_CalcBPBFields(PF_VOLUME* p_vol) { + pf_u32 num_data_sectors; + pf_u16 val; + p_vol->bpb.log2_bytes_per_sector = 0; + val = p_vol->bpb.bytes_per_sector; + while (val >>= 1) { p_vol->bpb.log2_bytes_per_sector++; } + p_vol->bpb.log2_sectors_per_cluster = 0; + val = p_vol->bpb.sectors_per_cluster; + while (val >>= 1) { p_vol->bpb.log2_sectors_per_cluster++; } + p_vol->bpb.num_root_dir_sectors = ((p_vol->bpb.num_root_dir_entries << 5) + (p_vol->bpb.bytes_per_sector - 1)) >> p_vol->bpb.log2_bytes_per_sector; + p_vol->bpb.first_data_sector = (p_vol->bpb.num_FATs * p_vol->bpb.sectors_per_FAT) + (p_vol->bpb.num_root_dir_sectors + p_vol->bpb.num_reserved_sectors); + num_data_sectors = p_vol->bpb.total_sectors - p_vol->bpb.first_data_sector; + p_vol->bpb.num_clusters = num_data_sectors >> p_vol->bpb.log2_sectors_per_cluster; + if (p_vol->bpb.num_clusters < 0xFF5) { + p_vol->bpb.fat_type = FAT_12; + p_vol->bpb.first_root_dir_sector = p_vol->bpb.num_reserved_sectors + p_vol->bpb.num_FATs * p_vol->bpb.sectors_per_FAT; + } else if (p_vol->bpb.num_clusters < 0xFFF5) { + p_vol->bpb.fat_type = FAT_16; + p_vol->bpb.first_root_dir_sector = p_vol->bpb.num_reserved_sectors + p_vol->bpb.num_FATs * p_vol->bpb.sectors_per_FAT; + } else { + p_vol->bpb.fat_type = FAT_32; + p_vol->bpb.first_root_dir_sector = 0; + } +} + +pf_s32 pdm_bpb_InitVolumeWithBootSector(PF_VOLUME* p_vol, pf_u8* buf) { + pf_u32 total_sectors; + pf_s32 err = 0; + pf_u32 fat_sectors; + pf_u32 version; + if (!pdm_part_is_boot_sector(buf)) { + err = 3; + } else { + p_vol->bpb.bytes_per_sector = read_boot_u16(buf, 11); + p_vol->bpb.sectors_per_cluster = buf[13]; + p_vol->bpb.num_reserved_sectors = read_boot_u16(buf, 14); + p_vol->bpb.num_FATs = buf[16]; + p_vol->bpb.num_root_dir_entries = read_boot_u16(buf, 17); + p_vol->bpb.media = buf[21]; + total_sectors = read_boot_u16(buf, 19); + p_vol->bpb.total_sectors = total_sectors == 0 ? PF_SWAP_32(*(pf_u32*)&buf[32]) : total_sectors; + fat_sectors = read_boot_u16(buf, 22); + p_vol->bpb.sectors_per_FAT = fat_sectors == 0 ? PF_SWAP_32(*(pf_u32*)&buf[36]) : fat_sectors; + pdm_bpb_CalcBPBFields(p_vol); + switch (p_vol->bpb.fat_type) { + case FAT_12: + case FAT_16: + if (fat_sectors == 0) { err = 4; } + break; + case FAT_32: + if (total_sectors != 0 || fat_sectors != 0) { err = 4; } + version = read_boot_u16(buf, 42); + p_vol->bpb.ext_flags = read_boot_u16(buf, 40); + p_vol->bpb.root_dir_cluster = PF_SWAP_32(*(pf_u32*)&buf[44]); + p_vol->bpb.fs_info_sector = read_boot_u16(buf, 48); + p_vol->bpb.backup_boot_sector = read_boot_u16(buf, 50); + p_vol->bpb.first_root_dir_sector = p_vol->bpb.first_data_sector + ((p_vol->bpb.root_dir_cluster - 2) << p_vol->bpb.log2_sectors_per_cluster); + if (version != 0) { err = 4; } + break; + default: + err = 4; + break; + } + if (p_vol->bpb.ext_flags & 0x80) { + p_vol->bpb.num_active_FATs = 1; + p_vol->bpb.active_FAT_sector = p_vol->bpb.num_reserved_sectors + ((p_vol->bpb.ext_flags & 7) * p_vol->bpb.sectors_per_FAT); + } else { + p_vol->bpb.num_active_FATs = p_vol->bpb.num_FATs; + p_vol->bpb.active_FAT_sector = p_vol->bpb.num_reserved_sectors; + } + } + return err; +} + +pf_s32 pdm_bpb_InitVolumeWithFSINFOSector(PF_VOLUME* p_vol, pf_u8* buf, pf_u32* p_free_count, pf_u32* p_next_free) { + pf_u32 free_count; + pf_u32 next_free; + *p_free_count = -1; + *p_next_free = -1; + if (PF_SWAP_32(*(pf_u32*)&buf[0]) == 0x41615252 && PF_SWAP_32(*(pf_u32*)&buf[484]) == 0x61417272 && PF_SWAP_32(*(pf_u32*)&buf[508]) == 0xAA550000) { + free_count = PF_SWAP_32(*(pf_u32*)&buf[488]); + next_free = PF_SWAP_32(*(pf_u32*)&buf[492]); + if (free_count >= 2 && free_count < p_vol->bpb.num_clusters + 2) { *p_free_count = free_count; } + if (next_free >= 2 && next_free < p_vol->bpb.num_clusters + 2) { *p_next_free = next_free; } + } + return 0; +} diff --git a/libs/RVL_SDK/src/fa/pdm_disk.c b/libs/RVL_SDK/src/fa/pdm_disk.c new file mode 100644 index 00000000..86c0ffc4 --- /dev/null +++ b/libs/RVL_SDK/src/fa/pdm_disk.c @@ -0,0 +1,161 @@ +#include +extern void* pf_memset(void*, pf_u8, pf_u32); +PDM_DISK_SET pdm_disk_set; +PDM_DISK_TBL pdm_drv_tbl[26]; + +pf_s32 pdm_disk_convert_sector_into_block(PDM_DISK* p_disk, pf_u32 start, pf_u32 count, pf_u32 bytes_per_sector, pf_u32* p_start, pf_u32* p_count) { + pf_u32 ratio; + pf_s32 err = 0; + *p_start = start; + *p_count = count; + if (bytes_per_sector > 512) { + ratio = (pf_u16)bytes_per_sector >> 9; + if (ratio == 2) { *p_start <<= 1; } + else if (ratio == 4) { *p_start <<= 2; } + else if (ratio == 8) { *p_start <<= 3; } + if (ratio == 2) { *p_count <<= 1; } + else if (ratio == 4) { *p_count <<= 2; } + else if (ratio == 8) { *p_count <<= 3; } + } else if (bytes_per_sector < 512) { err = 4; } + return err; +} +pf_s32 pdm_disk_convert_block_into_sector(PDM_DISK* p_disk, pf_u32 start, pf_u32 count, pf_u32 bytes_per_sector, pf_u32* p_start, pf_u32* p_count) { + pf_u32 ratio; + pf_s32 err = 0; + *p_start = start; + *p_count = count; + if (bytes_per_sector > 512) { + ratio = (pf_u16)bytes_per_sector >> 9; + if (ratio == 2) { *p_start >>= 1; } + else if (ratio == 4) { *p_start >>= 2; } + else if (ratio == 8) { *p_start >>= 3; } + if (ratio == 2) { *p_count >>= 1; } + else if (ratio == 4) { *p_count >>= 2; } + else if (ratio == 8) { *p_count >>= 3; } + } else if (bytes_per_sector < 512) { err = 4; } + return err; +} +pf_s32 pdm_disk_init_disk_manager(void) { + pf_memset(&pdm_disk_set, 0, sizeof(pdm_disk_set)); + return 0; +} +pf_s32 pdm_disk_add_disk(PDM_INIT_DISK* p_init_disk_tbl, PDM_DISK** pp_disk) { + PDM_DISK_TBL* p_driver; + pf_u16 disk_no; + PDM_DISK* p_disk; + *pp_disk = PF_NULL; + if (pdm_disk_set.num_allocated_disk >= 26) { return 8; } + for (disk_no = 0; disk_no < 26; disk_no++) { + if (!(pdm_disk_set.disk[disk_no].status & 1)) { break; } + } + if (disk_no >= 26) { return 8; } + p_driver = &pdm_drv_tbl[disk_no]; + p_init_disk_tbl->p_func(p_driver, p_init_disk_tbl->ui_ext); + p_disk = &pdm_disk_set.disk[disk_no]; + p_disk->disk_tbl = *p_driver; + p_disk->status |= 1; + pdm_disk_set.num_allocated_disk++; + *pp_disk = p_disk; + return 0; +} +pf_s32 pdm_disk_del_disk(PDM_DISK* p_disk) { + if (p_disk->init_count != 0) { return 16; } + if (p_disk->mount_count != 0) { return 17; } + p_disk->status &= ~1; + pdm_disk_set.num_allocated_disk--; + return 0; +} +pf_s32 pdm_disk_set_disk(PDM_DISK* p_disk, PDM_PARTITION* p_part) { + p_disk->p_cur_part = p_part; + return 0; +} +pf_s32 pdm_disk_get_media_attribute(PDM_DISK* p_disk, PDM_DISK_INFO* p_info) { + *p_info = p_disk->disk_info; + return 0; +} +pf_s32 pdm_disk_get_media_bps(PDM_DISK* p_disk, pf_u16* p_bps) { + *p_bps = p_disk->disk_info.bytes_per_sector; + return 0; +} +pf_s32 pdm_disk_init(PDM_DISK* p_disk) { + pf_s32 err; + if (p_disk->init_count == 0) { + err = p_disk->disk_tbl.p_func->init(p_disk); + if (err != 0) { pdm_part_set_driver_error_code(p_disk->p_cur_part, err); return 21; } + } + p_disk->init_count++; + return 0; +} +pf_s32 pdm_disk_mount(PDM_DISK* p_disk) { + pf_s32 err; + if (p_disk->mount_count == 0) { + err = p_disk->disk_tbl.p_func->mount(p_disk); + if (err != 0) { pdm_part_set_driver_error_code(p_disk->p_cur_part, err); return 21; } + err = p_disk->disk_tbl.p_func->get_disk_info(p_disk, &p_disk->disk_info); + if (err != 0) { + pdm_part_set_driver_error_code(p_disk->p_cur_part, err); + p_disk->disk_tbl.p_func->unmount(p_disk); + return 21; + } + if (p_disk->disk_info.media_attr & 1) { p_disk->status |= 2; } + else { p_disk->status &= ~2; } + } + p_disk->mount_count++; + return 0; +} +pf_s32 pdm_disk_format(PDM_DISK* p_disk, const pf_u8* param) { + pf_s32 err; + err = p_disk->disk_tbl.p_func->format(p_disk, param); + if (err != 0) { pdm_part_set_driver_error_code(p_disk->p_cur_part, err); return 21; } + return 0; +} +pf_s32 pdm_disk_physical_read(PDM_DISK* p_disk, pf_u8* buf, pf_u32 sector, pf_u32 count, pf_u32 bps, pf_u32* p_success) { + pf_s32 err; + pf_u32 block; + pf_u32 num_blocks; + pf_u32 converted_sector; + err = pdm_disk_convert_sector_into_block(p_disk, sector, count, bps, &block, &num_blocks); + if (err != 0) { return err; } + err = p_disk->disk_tbl.p_func->physical_read(p_disk, buf, block, num_blocks, p_success); + pdm_disk_convert_block_into_sector(p_disk, block, *p_success, bps, &converted_sector, p_success); + if (err != 0) { pdm_part_set_driver_error_code(p_disk->p_cur_part, err); return 21; } + return 0; +} +pf_s32 pdm_disk_physical_write(PDM_DISK* p_disk, const pf_u8* buf, pf_u32 sector, pf_u32 count, pf_u32 bps, pf_u32* p_success) { + pf_s32 err; + pf_u32 block; + pf_u32 num_blocks; + pf_u32 converted_sector; + err = pdm_disk_convert_sector_into_block(p_disk, sector, count, bps, &block, &num_blocks); + if (err != 0) { return err; } + err = p_disk->disk_tbl.p_func->physical_write(p_disk, buf, block, num_blocks, p_success); + pdm_disk_convert_block_into_sector(p_disk, block, *p_success, bps, &converted_sector, p_success); + if (err != 0) { pdm_part_set_driver_error_code(p_disk->p_cur_part, err); return 21; } + return 0; +} +pf_s32 pdm_disk_unmount(PDM_DISK* p_disk, pf_u32 mode) { + pf_s32 err = 0; + if (p_disk->mount_count == 1) { + err = p_disk->disk_tbl.p_func->unmount(p_disk); + if (err != 0) { pdm_part_set_driver_error_code(p_disk->p_cur_part, err); err = 21; } + } else if (p_disk->mount_count == 0) { return 14; } + if (err == 0 || mode == 1) { p_disk->mount_count--; } + return err; +} +pf_s32 pdm_disk_finalize(PDM_DISK* p_disk) { + pf_s32 err; + if (p_disk->init_count == 1) { + err = p_disk->disk_tbl.p_func->finalize(p_disk); + if (err != 0) { pdm_part_set_driver_error_code(p_disk->p_cur_part, err); return 21; } + } else if (p_disk->init_count == 0) { return 13; } + p_disk->init_count--; + return 0; +} +void pdm_disk_notify_media_eject(PDM_DISK* p_disk) { + p_disk->status &= ~4; + pdm_part_set_change_media_state(p_disk, 0); +} +void pdm_disk_notify_media_insert(PDM_DISK* p_disk) { + p_disk->status |= 4; + pdm_part_set_change_media_state(p_disk, 1); +} diff --git a/libs/RVL_SDK/src/fa/pdm_partition.c b/libs/RVL_SDK/src/fa/pdm_partition.c new file mode 100644 index 00000000..74c05396 --- /dev/null +++ b/libs/RVL_SDK/src/fa/pdm_partition.c @@ -0,0 +1,287 @@ +#include +#include +#define MBR_WORD(buf, offset) (((pf_u32)(buf)[(offset) + 2] << 16) + (buf)[offset] + (((pf_u32)(buf)[(offset) + 3] << 24) + ((pf_u32)(buf)[(offset) + 1] << 8))) +static inline pf_u16 read_boot_u16(pf_u8* buf, pf_u32 offset) { + if ((pf_u32)&buf[offset] & 1) { return (buf[offset + 1] << 8) | buf[offset]; } + return PF_SWAP_16(*(pf_u16*)&buf[(offset + 1) & ~1]); +} + +pf_s32 pdm_part_is_master_boot_sector(pf_u8* buf, pf_u32 total, pf_bool* p_is_mbr) { + pf_s16 index; + pf_u32 start[4]; + pf_u32 count[4]; + *p_is_mbr = 0; + if (buf[510] != 0x55 || buf[511] != 0xAA) { return 2; } + for (index = 0; index < 4; index++) { + start[index] = 0; + start[index] = (((pf_u32)buf[456] << 16) + buf[454]) + (((pf_u32)buf[457] << 24) + ((pf_u32)buf[455] << 8)); + count[index] = (((pf_u32)buf[460] << 16) + buf[458]) + (((pf_u32)buf[461] << 24) + ((pf_u32)buf[459] << 8)); + if (start[index] != 0 && count[index] != 0) { + if (index == 0) { *p_is_mbr = 1; } + } else { + if (index == 0) { return 2; } + } + buf += 16; + } + for (index = 0; index < 4; index++) { + if (start[index] + count[index] > total) { + if (index == 0) { *p_is_mbr = 0; } + return 2; + } + } + return 0; +} +pf_bool pdm_part_is_boot_sector(pf_u8* buf) { + pf_u16 byte_per_sector; + pf_u16 sector_per_cluster; + pf_u8 media; + pf_bool is_boot; + + is_boot = PF_TRUE; + if ((buf[0x00] != 0xEB || buf[0x2] != 0x90) && buf[0x0] != 0xE9) { + is_boot = PF_FALSE; + } + if (buf[0x1FE] != 0x55 || buf[0x1FF] != 0xAA) { + is_boot = PF_FALSE; + } + byte_per_sector = read_boot_u16(buf, 0xB); + if (byte_per_sector != 0x200 && byte_per_sector != 0x400 && byte_per_sector != 0x800 && byte_per_sector != 0x1000) { + is_boot = PF_FALSE; + } + sector_per_cluster = buf[0xD]; + if (sector_per_cluster != 1 && sector_per_cluster != 2 && sector_per_cluster != 4 && sector_per_cluster != 8 && sector_per_cluster != 0x10 && + sector_per_cluster != 0x20 && sector_per_cluster != 0x40 && sector_per_cluster != 0x80) { + is_boot = PF_FALSE; + } + media = buf[0x15]; + if (media != 0xF0 && media != 0xF8 && media != 0xF9 && media != 0xFA && media != 0xFB && media != 0xFC && media != 0xFD && media != 0xFE && + media != 0xFF) { + is_boot = PF_FALSE; + } + return is_boot; +} + +pf_s32 pdm_part_get_start_sector(PDM_PARTITION* p_part) { + pf_u32 part_index = 0; + pf_u32 requested = p_part->part_id; + pf_u32 start[4]; + pf_u32 count[4]; + pf_u16 index; + pf_u16 extended_index; + pf_u32 base; + pf_u32 sector; + pf_u32 length; + pf_bool is_mbr; + pf_u32 success; + pf_s32 err; + pf_u8 buf[512] __attribute__((aligned(32))); + p_part->start_sector = 0; + p_part->total_sector = p_part->p_disk->disk_info.total_sectors; + err = pdm_disk_physical_read(p_part->p_disk, buf, 0, 1, 512, &success); + if (err != 0) { return err; } + pdm_part_is_master_boot_sector(buf, p_part->p_disk->disk_info.total_sectors, &is_mbr); + if (is_mbr) { + start[0] = MBR_WORD(buf, 454); + start[1] = MBR_WORD(buf, 470); + start[2] = MBR_WORD(buf, 486); + start[3] = MBR_WORD(buf, 502); + count[0] = MBR_WORD(buf, 458); + count[1] = MBR_WORD(buf, 474); + count[2] = MBR_WORD(buf, 490); + count[3] = MBR_WORD(buf, 506); + part_index = 0; + for (index = 0; index < 4; index++) { + sector = start[index]; + length = count[index]; + if (sector == 0 || length == 0) { return 7; } + err = pdm_disk_physical_read(p_part->p_disk, buf, sector, 1, 512, &success); + if (err != 0) { return err; } + pdm_part_is_master_boot_sector(buf, p_part->p_disk->disk_info.total_sectors, &is_mbr); + if (is_mbr) { + base = start[index]; + sector = base; + for (extended_index = 0; extended_index < 2; extended_index++) { + if (extended_index == 0) { + sector = sector + MBR_WORD(buf, 454); + length = MBR_WORD(buf, 458); + err = pdm_disk_physical_read(p_part->p_disk, buf, sector, 1, 512, &success); + if (err != 0) { return err; } + if (requested == part_index) { p_part->start_sector = sector; p_part->total_sector = length; return 0; } + part_index++; + } else { + sector = base + MBR_WORD(buf, 470); + length = MBR_WORD(buf, 474); + if (sector == 0 || length == 0) { break; } + err = pdm_disk_physical_read(p_part->p_disk, buf, sector, 1, 512, &success); + if (err != 0) { return err; } + pdm_part_is_master_boot_sector(buf, p_part->p_disk->disk_info.total_sectors, &is_mbr); + if (!is_mbr) { break; } + extended_index = 0; + } + } + } else { + if (requested == part_index) { p_part->start_sector = sector; p_part->total_sector = length; break; } + part_index++; + } + } + } else { + if (requested >= 2) { return 7; } + p_part->start_sector = part_index; + p_part->total_sector = p_part->p_disk->disk_info.total_sectors; + } + return 0; +} +pf_u32 pdm_part_chg_ltop(PDM_PARTITION* p_part, pf_u32 sector, pf_u32 bps) { + pf_u16 media_bps; + pf_u32 ratio; + pf_u32 start; + pdm_disk_get_media_bps(p_part->p_disk, &media_bps); + if (bps == media_bps) { return sector + p_part->start_sector; } + start = p_part->start_sector; + ratio = media_bps >> 9; + if (ratio == 2) { start <<= 1; } + else if (ratio == 4) { start <<= 2; } + else if (ratio == 8) { start <<= 3; } + ratio = (pf_u16)bps >> 9; + if (ratio == 2) { start >>= 1; } + else if (ratio == 4) { start >>= 2; } + else if (ratio == 8) { start >>= 3; } + return sector + start; +} +pf_s32 pdm_part_get_partition(PDM_DISK* p_disk, pf_u32 id, PDM_PARTITION** pp_part) { + pf_s32 err; + pf_u16 index; + *pp_part = PF_NULL; + if (pdm_disk_set.num_partition >= 26) { return 10; } + for (index = 0; index < 26; index++) { + if (!(pdm_disk_set.partition[index].status & 1)) { break; } + } + if (index == 26) { return 10; } + err = pdm_disk_set_disk(p_disk, &pdm_disk_set.partition[index]); + if (err != 0) { return err; } + pdm_disk_set.num_partition++; + pdm_disk_set.partition[index].status |= 1; + pdm_disk_set.partition[index].p_disk = p_disk; + pdm_disk_set.partition[index].part_id = id; + *pp_part = &pdm_disk_set.partition[index]; + return 0; +} +pf_s32 pdm_part_release_partition(PDM_PARTITION* p_part) { + p_part->status &= ~1; + pdm_disk_set.num_partition--; + return 0; +} +pf_bool pdm_part_is_attached_partition(PDM_PARTITION* p_part) { return (p_part->status >> 2) & 1; } +void pdm_part_set_driver_error_code(PDM_PARTITION* p_part, pf_s32 err) { p_part->driver_last_error = err; } +pf_s32 pdm_part_get_driver_error_code(PDM_PARTITION* p_part) { return p_part->driver_last_error; } +pf_s32 pdm_part_get_media_attribute(PDM_PARTITION* p_part, PDM_DISK_INFO* p_info) { + pf_s32 err = pdm_disk_get_media_attribute(p_part->p_disk, p_info); + if (err != 0) { return err; } + return 0; +} +pf_s32 pdm_part_init(PDM_PARTITION* p_part) { + pf_s32 err; + if (!(p_part->status & 4)) { + err = pdm_disk_set_disk(p_part->p_disk, p_part); + if (err != 0) { return err; } + err = pdm_disk_init(p_part->p_disk); + if (err != 0) { return err; } + } else { return 11; } + p_part->status |= 4; + return 0; +} +pf_s32 pdm_part_mount(PDM_PARTITION* p_part) { + pf_s32 err; + if (!(p_part->status & 8)) { + err = pdm_disk_set_disk(p_part->p_disk, p_part); + if (err != 0) { return err; } + err = pdm_disk_mount(p_part->p_disk); + if (err != 0) { return err; } + err = pdm_part_get_start_sector(p_part); + if (err != 0) { pdm_disk_unmount(p_part->p_disk, 0); return err; } + } else { return 12; } + p_part->status &= ~2; + p_part->status |= 8; + return 0; +} +pf_s32 pdm_part_format(PDM_PARTITION* p_part, const pf_u8* param) { + pf_s32 err; + err = pdm_disk_set_disk(p_part->p_disk, p_part); + if (err != 0) { return err; } + err = pdm_disk_format(p_part->p_disk, param); + if (err != 0) { return err; } + return 0; +} +pf_s32 pdm_part_logical_read(PDM_PARTITION* p_part, pf_u8* buf, pf_u32 sector, pf_u32 count, pf_u32 bps, pf_u32* p_success) { + pf_s32 err; + pf_u32 physical_sector; + if ((p_part->status & 8) != 8 || (p_part->status & 2) == 2) { return 15; } + err = pdm_disk_set_disk(p_part->p_disk, p_part); + if (err != 0) { return err; } + if (p_part->total_sector <= sector) { return 18; } + if (p_part->total_sector < sector + count) { count -= (sector + count) - p_part->total_sector; } + physical_sector = pdm_part_chg_ltop(p_part, sector, bps); + err = pdm_disk_physical_read(p_part->p_disk, buf, physical_sector, count, bps, p_success); + if (err != 0) { return err; } + return 0; +} +pf_s32 pdm_part_logical_write(PDM_PARTITION* p_part, const pf_u8* buf, pf_u32 sector, pf_u32 count, pf_u32 bps, pf_u32* p_success) { + pf_s32 err; + pf_u32 physical_sector; + if ((p_part->status & 8) != 8 || (p_part->status & 2) == 2) { return 15; } + err = pdm_disk_set_disk(p_part->p_disk, p_part); + if (err != 0) { return err; } + if (p_part->total_sector <= sector) { return 18; } + if (p_part->total_sector < sector + count) { count -= (sector + count) - p_part->total_sector; } + physical_sector = pdm_part_chg_ltop(p_part, sector, bps); + err = pdm_disk_physical_write(p_part->p_disk, buf, physical_sector, count, bps, p_success); + if (err != 0) { return err; } + return 0; +} +pf_s32 pdm_part_unmount(PDM_PARTITION* p_part, pf_u32 mode) { + pf_s32 err; + pf_s32 result = 0; + if (p_part->status & 8) { + err = pdm_disk_set_disk(p_part->p_disk, p_part); + if (err != 0) { return err; } + err = pdm_disk_unmount(p_part->p_disk, mode); + if (err != 0) { result = err; } + } else { result = 14; } + if (result == 0 || mode == 1) { p_part->status &= ~2; p_part->status &= ~8; } + return result; +} +pf_s32 pdm_part_finalize(PDM_PARTITION* p_part) { + pf_s32 err; + if (p_part->status & 4) { + err = pdm_disk_set_disk(p_part->p_disk, p_part); + if (err != 0) { return err; } + err = pdm_disk_finalize(p_part->p_disk); + if (err != 0) { return err; } + } else { return 13; } + p_part->status &= ~4; + return 0; +} +pf_s32 pdm_part_register_callback(PDM_PARTITION* p_part, PDM_CALLBACK* callbacks) { + p_part->callbacks.insert = callbacks->insert; + p_part->callbacks.eject = callbacks->eject; + return 0; +} +pf_s32 pdm_part_unregister_callback(PDM_PARTITION* p_part) { + p_part->callbacks.insert = PF_NULL; + p_part->callbacks.eject = PF_NULL; + return 0; +} +void pdm_part_set_change_media_state(PDM_DISK* p_disk, pf_bool is_inserted) { + pf_u16 index; + for (index = 0; index < 26; index++) { + if ((pdm_disk_set.partition[index].status & 1) && pdm_disk_set.partition[index].p_disk == p_disk) { + PDM_PARTITION* p_part = &pdm_disk_set.partition[index]; + p_part->status |= 2; + if (is_inserted == 0 && pdm_disk_set.partition[index].callbacks.eject != PF_NULL) { + pdm_disk_set.partition[index].callbacks.eject(p_part); + } else if (pdm_disk_set.partition[index].callbacks.insert != PF_NULL) { + pdm_disk_set.partition[index].callbacks.insert(p_part); + } + } + } +} diff --git a/libs/RVL_SDK/src/fa/pf_attach.c b/libs/RVL_SDK/src/fa/pf_attach.c new file mode 100644 index 00000000..986bb111 --- /dev/null +++ b/libs/RVL_SDK/src/fa/pf_attach.c @@ -0,0 +1,29 @@ +#include +#include + +typedef struct PFATTACH_NOTIFY { + pf_u8 driver_state[0x50]; + void* callback_data; +} PFATTACH_NOTIFY; +typedef struct PFATTACH_GLOBAL { + pf_u8 volume_state[0x40]; + pf_s32 last_error; +} PFATTACH_GLOBAL; +extern PFATTACH_GLOBAL pf_vol_set; +extern pf_s32 PFVOL_attach(PF_DRV_TBL*, PFATTACH_NOTIFY*, void*); +extern pf_s32 PFAPI_convertReturnValue(pf_s32); + +pf_int32 pf2_attach(PF_DRV_TBL** drv_tbl, PFATTACH_NOTIFY* notify) { + pf_s32 err; + if (drv_tbl == PF_NULL || !*drv_tbl) { + err = 10; + pf_vol_set.last_error = 10; + } else { + for (; *drv_tbl != PF_NULL; drv_tbl++) { + if (notify != PF_NULL) err = PFVOL_attach(*drv_tbl, notify, notify->callback_data); + else err = PFVOL_attach(*drv_tbl, PF_NULL, PF_NULL); + if (err != 0) return PFAPI_convertReturnValue(err); + } + } + return PFAPI_convertReturnValue(err); +} diff --git a/libs/RVL_SDK/src/fa/pf_chdir.c b/libs/RVL_SDK/src/fa/pf_chdir.c new file mode 100644 index 00000000..9d442b39 --- /dev/null +++ b/libs/RVL_SDK/src/fa/pf_chdir.c @@ -0,0 +1,31 @@ +#define PF_FA_STR_LAYOUT +#define VFiPFAPI_convertReturnValue PFAPI_convertReturnValue +#define VFiPFDIR_chdir PFDIR_chdir +#define VFiPFSTR_InitStr PFSTR_InitStr +#include + +typedef struct PFAPI_GLOBAL_STATE { + pf_u8 volume_state[0x40]; + pf_s32 last_error; +} PFAPI_GLOBAL_STATE; +extern PFAPI_GLOBAL_STATE pf_vol_set; + + +#include +#include + +#include + +pf_int32 pf2_chdir(const pf_ch8* sPath) { + pf_s32 err; + PF_STR path_str; + + err = VFiPFSTR_InitStr(&path_str, (pf_s8*)sPath, 1U); + if (err == 0) { + err = VFiPFDIR_chdir(&path_str); + } else { + pf_vol_set.last_error = err; + } + err = VFiPFAPI_convertReturnValue(err); + return err; +} diff --git a/libs/RVL_SDK/src/fa/pf_cp932.c b/libs/RVL_SDK/src/fa/pf_cp932.c new file mode 100644 index 00000000..a1c08bdb --- /dev/null +++ b/libs/RVL_SDK/src/fa/pf_cp932.c @@ -0,0 +1,178 @@ +#define PF_FA_CP932_TABLE +#define VFiPFCODE_CP932_OEM2Unicode PFCODE_CP932_OEM2Unicode +#define VFiPFCODE_CP932_OEMCharWidth PFCODE_CP932_OEMCharWidth +#define VFiPFCODE_CP932_Unicode2OEM PFCODE_CP932_Unicode2OEM +#define VFiPFCODE_CP932_UnicodeCharWidth PFCODE_CP932_UnicodeCharWidth +#define VFiPFCODE_CP932_isOEMMBchar PFCODE_CP932_isOEMMBchar +#define VFiPFCODE_CP932_isUnicodeMBchar PFCODE_CP932_isUnicodeMBchar +#define VFiPFCODE_Combine_Width PFCODE_Combine_Width +#define VFiPFCODE_Divide_Width PFCODE_Divide_Width + +#include +#include + +#include + +pf_s32 VFiPFCODE_CP932_OEM2Unicode(const pf_s8* cp932_src, pf_u16* uc_dst) { + pf_u8 cp932_lead = *cp932_src++; + pf_u8 cp932_trail = *cp932_src; + pf_s32 lead_index; + pf_s32 trail_index; + + if (cp932_lead < 0x80) { + *uc_dst = cp932_lead; + return VFiPFCODE_Combine_Width(1, 2); + } + if (cp932_lead >= 0xA1 && cp932_lead <= 0xDF) { + *uc_dst = cp932_lead + 0xFEC0; + return VFiPFCODE_Combine_Width(1, 2); + } + if (cp932_lead == 0x80 || cp932_lead == 0x85 || cp932_lead == 0x86 || cp932_lead == 0xA0 || cp932_lead == 0xEB || + cp932_lead == 0xEC || (cp932_lead >= 0xEF && cp932_lead <= 0xF9) || + (cp932_lead >= 0xFD && cp932_lead == 0xFF)) { + *uc_dst = '_'; + return VFiPFCODE_Combine_Width(1, 2); + } + if (cp932_trail >= 0xFD) { + *uc_dst = '_'; + return VFiPFCODE_Combine_Width(1, 2); + } + if (cp932_lead >= 0x81 && cp932_lead <= 0x84) { + lead_index = cp932_lead - 0x81; + } else if (cp932_lead >= 0x87 && cp932_lead <= 0x9F) { + lead_index = cp932_lead - 0x83; + } else if (cp932_lead >= 0xE0 && cp932_lead <= 0xEA) { + lead_index = cp932_lead - 0xC3; + } else if (cp932_lead >= 0xED && cp932_lead <= 0xEE) { + lead_index = cp932_lead - 0xC5; + } else if (cp932_lead >= 0xFA && cp932_lead <= 0xFC) { + lead_index = cp932_lead - 0xD0; + } else { + *uc_dst = '_'; + return VFiPFCODE_Combine_Width(1, 2); + } + + trail_index = cp932_trail - '@'; + if (trail_index < 0 || trail_index >= 189) { + *uc_dst = '_'; + return VFiPFCODE_Combine_Width(1, 2); + } + *uc_dst = cp932_to_unicode[lead_index][trail_index] & 0xFFFF; + if (*uc_dst == 0) { + *uc_dst = '_'; + return VFiPFCODE_Combine_Width(1, 2); + } + return VFiPFCODE_Combine_Width(2, 2); +} + +pf_s32 VFiPFCODE_CP932_Unicode2OEM(const pf_u16* uc_src, pf_s8* cp932_dst) { + pf_s32 i; + pf_s32 j; + pf_u16* p_table; + pf_u8 uc_lead; + pf_u8 uc_trail; + pf_u16 uc; + pf_u16 cp932; + + j = 0; + uc_lead = (pf_u8)(*uc_src >> 8); + uc_trail = (pf_u8)*uc_src; + if (uc_trail < 0x80 && uc_lead == 0) { + cp932_dst[0] = uc_trail; + cp932_dst[1] = 0; + return VFiPFCODE_Combine_Width(1, 2); + } + uc = PF_CODE_C_TO_WC(uc_lead, uc_trail); + if (0xFF61 <= uc && uc <= 0xFF9F) { + cp932 = uc - 0xFEC0; + cp932_dst[0] = cp932; + cp932_dst[1] = 0; + return VFiPFCODE_Combine_Width(1, 2); + } + if (uc == '_') { + cp932_dst[0] = '_'; + return VFiPFCODE_Combine_Width(1, 2); + } + + for (i = 0; i < 45; i++) { + for (j = 0, p_table = (pf_u16*)&cp932_to_unicode[i]; j < 189; j++, p_table++) { + if (*p_table == uc) { + break; + } + } + if (j < 189) { + break; + } + } + if (j == 189) { + cp932_dst[0] = '_'; + return VFiPFCODE_Combine_Width(1, 2); + } + + if (i >= 0 && i <= 3) { + cp932 = ((i + 0x81) << 8) | (j + '@'); + } else if (i >= 4 && i <= 0x1C) { + cp932 = ((i + 0x83) << 8) | (j + '@'); + } else if (i >= 0x1D && i <= '\'') { + cp932 = ((i + 0xC3) << 8) | (j + '@'); + } else if (i >= '(' && i <= ')') { + cp932 = ((i + 0xC5) << 8) | (j + '@'); + } else if (i >= '*' && i <= ',') { + cp932 = ((i + 0xD0) << 8) | (j + '@'); + } else { + cp932_dst[0] = '_'; + return VFiPFCODE_Combine_Width(1, 2); + } + + cp932_dst[0] = (pf_u8)(cp932 >> 8); + cp932_dst[1] = (pf_u8)cp932; + return VFiPFCODE_Combine_Width(2, 2); +} + +pf_s32 VFiPFCODE_CP932_OEMCharWidth(const pf_s8* buf) { + pf_u16 tmp; + pf_s32 width; + pf_s16 oem_width; + pf_s16 uni_width; + + width = VFiPFCODE_CP932_OEM2Unicode(buf, &tmp); + VFiPFCODE_Divide_Width(width, &oem_width, &uni_width); + return (pf_s32)oem_width; +} + +pf_bool VFiPFCODE_CP932_isOEMMBchar(pf_s8 cp932, pf_u32 num) { + pf_u8 code; + pf_bool is_mb; + + code = (pf_u8)cp932; + + switch (num) { + case 1: { + is_mb = (code >= 0x81 && code <= 0x9F) || (code >= 0xE0 && code <= 0xFC); + break; + } + case 2: { + is_mb = (code >= '@' && code <= '~') || (code >= 0x80 && code <= 0xFC); + break; + } + default: { + is_mb = PF_FALSE; + } + } + + return is_mb; +} + +pf_s32 VFiPFCODE_CP932_UnicodeCharWidth(const pf_u16* buf) { + pf_s8 tmp[2]; + pf_s32 width; + pf_s16 oem_width; + pf_s16 uni_width; + width = VFiPFCODE_CP932_Unicode2OEM(buf, tmp); + VFiPFCODE_Divide_Width(width, &oem_width, &uni_width); + return uni_width; +} + +pf_bool VFiPFCODE_CP932_isUnicodeMBchar(pf_u16 uc_src, pf_u32 num) { + return PF_FALSE; +} diff --git a/libs/RVL_SDK/src/fa/pf_create.c b/libs/RVL_SDK/src/fa/pf_create.c new file mode 100644 index 00000000..92deb96f --- /dev/null +++ b/libs/RVL_SDK/src/fa/pf_create.c @@ -0,0 +1,36 @@ +#define PF_FA_STR_LAYOUT +#define VFiPFAPI_convertReturnValue2NULL PFAPI_convertReturnValue2NULL +#define VFiPFFILE_fopen PFFILE_fopen +#define VFiPFSTR_InitStr PFSTR_InitStr +#include + +typedef struct PFAPI_GLOBAL_STATE { + pf_u8 volume_state[0x40]; + pf_s32 last_error; +} PFAPI_GLOBAL_STATE; +extern PFAPI_GLOBAL_STATE pf_vol_set; + + +#include +#include + +PF_FILE* pf2_create(const pf_ch8* path, pf_int32 mode) { + pf_u32 open_mode_create; + PF_FILE* p_file; + PF_STR path_str; + pf_s32 err; + + (void)mode; + + open_mode_create = 0x19; + + err = VFiPFSTR_InitStr(&path_str, (pf_s8*)path, 1U); + if (err == 0) { + err = VFiPFFILE_fopen(&path_str, open_mode_create, &p_file); + } else { + pf_vol_set.last_error = err; + } + p_file = VFiPFAPI_convertReturnValue2NULL(err, p_file); + + return p_file; +} diff --git a/libs/RVL_SDK/src/fa/pf_detach.c b/libs/RVL_SDK/src/fa/pf_detach.c new file mode 100644 index 00000000..fc03bf9e --- /dev/null +++ b/libs/RVL_SDK/src/fa/pf_detach.c @@ -0,0 +1,13 @@ +#define PF_FA_STR_LAYOUT +#define VFiPFAPI_convertReturnValue PFAPI_convertReturnValue +#define VFiPFVOL_detach PFVOL_detach +#include + +#include +#include + +pf_int32 pf2_detach(pf_ch8 drive) { + pf_s32 err = VFiPFVOL_detach(drive); + err = VFiPFAPI_convertReturnValue(err); + return err; +} diff --git a/libs/RVL_SDK/src/fa/pf_fat12.c b/libs/RVL_SDK/src/fa/pf_fat12.c new file mode 100644 index 00000000..182c6b12 --- /dev/null +++ b/libs/RVL_SDK/src/fa/pf_fat12.c @@ -0,0 +1,248 @@ +#define PF_FA_STR_LAYOUT +#define VFiPFCACHE_ReadFATPage PFCACHE_ReadFATPage +#define VFiPFCACHE_UpdateModifiedSector PFCACHE_UpdateModifiedSector +#define VFiPFFAT12_ReadFATEntry PFFAT12_ReadFATEntry +#define VFiPFFAT12_ReadFATEntryPage PFFAT12_ReadFATEntryPage +#define VFiPFFAT12_WriteFATEntry PFFAT12_WriteFATEntry +#define VFiPFFAT12_WriteFATEntryPage PFFAT12_WriteFATEntryPage +#define VFiPFFAT_UpdateFATEntry PFFAT_UpdateFATEntry +#define VFiPFSEC_ReadFAT PFSEC_ReadFAT +#define VFiPFSEC_WriteFAT PFSEC_WriteFAT +#include +#include +#include + +#include +#include + +#include + +pf_s32 VFiPFFAT12_ReadFATEntry(PF_VOLUME* p_vol, pf_u16 cluster, pf_u32* p_value) { + pf_u16 fat_sector; + pf_s32 err; + pf_u16 fat_offset; + pf_u16 offset_in_sector; + pf_u16 word; + pf_u8 buf[sizeof(pf_u16)]; + pf_u32 current_fat; + pf_s32 result; + + fat_offset = cluster + (cluster >> 1); + fat_sector = p_vol->bpb.active_FAT_sector + (fat_offset >> p_vol->bpb.log2_bytes_per_sector); + offset_in_sector = fat_offset & (p_vol->bpb.bytes_per_sector - 1); + if ((p_vol->bpb.ext_flags & 0x80) != 0) { + current_fat = p_vol->bpb.ext_flags & (0x01 | 0x02 | 0x04); + } else { + current_fat = 1; + } + + do { + if (offset_in_sector < (p_vol->bpb.bytes_per_sector - 1)) { + err = VFiPFSEC_ReadFAT(p_vol, &buf[0], (pf_u32)fat_sector, offset_in_sector, 2); + } else { + err = VFiPFSEC_ReadFAT(p_vol, &buf[0], (pf_u32)fat_sector, offset_in_sector, 1); + if (err == 0) { + err = VFiPFSEC_ReadFAT(p_vol, &buf[1], fat_sector + 1, 0U, 1); + } + } + if (err == 0x1000 && p_vol->p_callback != PF_NULL) { + result = ((PF_VOLUME_CB)p_vol->p_callback)(p_vol->last_driver_error); + if (result == 0) { + continue; + } + if (result == 1 && p_vol->bpb.num_active_FATs >= 2 && current_fat < p_vol->bpb.num_active_FATs) { + current_fat++; + fat_sector += (pf_u16)p_vol->bpb.sectors_per_FAT; + continue; + } + } + if (err != 0) { + *p_value = -1; + return err; + } + } while (err != 0); + + word = PF_SWAP_16(*(pf_u16*)buf); + if ((cluster & 0x01) != 0) { + *p_value = (word >> 4); + } else { + *p_value = word & 0xFFF; + } + return 0; +} + +pf_s32 VFiPFFAT12_WriteFATEntry(PF_VOLUME* p_vol, pf_u16 cluster, pf_u16 value) { + pf_s32 err; + pf_u16 fat_offset; + pf_u16 fat_sector; + pf_u16 offset_in_sector; + pf_u16 fat_sector2; + pf_u16 offset_in_sector2; + pf_u16 word; + pf_u16 current_fat; + pf_s32 result; + pf_u8 buf[sizeof(pf_u16)]; + + fat_offset = cluster + (cluster >> 1); + fat_sector = p_vol->bpb.active_FAT_sector + (fat_offset >> p_vol->bpb.log2_bytes_per_sector); + offset_in_sector = fat_offset & (p_vol->bpb.bytes_per_sector - 1); + if ((cluster & 0x01) != 0) { + err = VFiPFSEC_ReadFAT(p_vol, &buf[0], fat_sector, offset_in_sector, 1); + if (err != 0) { + return err; + } + word = (pf_u16)((value << 4) + (buf[0] & 0xF)); + } else { + fat_sector2 = p_vol->bpb.active_FAT_sector + ((fat_offset + 1) >> p_vol->bpb.log2_bytes_per_sector); + offset_in_sector2 = (fat_offset + 1) & (p_vol->bpb.bytes_per_sector - 1); + err = VFiPFSEC_ReadFAT(p_vol, &buf[0], fat_sector2, offset_in_sector2, 1); + if (err != 0) { + return err; + } + word = (pf_u16)buf[0] << 8; + word = (word & 0xF000) + (pf_u16)(value & 0xFFF); + } + *(pf_u16*)buf = PF_SWAP_16(word); + result = 0; + for (current_fat = 0; current_fat < p_vol->bpb.num_active_FATs; current_fat++) { + if (offset_in_sector < (p_vol->bpb.bytes_per_sector - 1)) { + err = VFiPFSEC_WriteFAT(p_vol, &buf[0], fat_sector, offset_in_sector, 2); + if (err != 0 && result == 0) { result = err; } + } else { + err = VFiPFSEC_WriteFAT(p_vol, &buf[0], fat_sector, offset_in_sector, 1U); + if (err != 0 && result == 0) { result = err; } + err = VFiPFSEC_WriteFAT(p_vol, &buf[1], fat_sector + 1, 0U, 1); + if (err != 0 && result == 0) { result = err; } + } + fat_sector += p_vol->bpb.sectors_per_FAT; + } + return result; +} + +pf_s32 PFFAT12_WriteFATEntryWithBuf(PF_VOLUME* p_vol, pf_s16 cluster, pf_u16 value, PF_CACHE_PAGE* p_page) { + pf_s32 err; + pf_u32 offset_in_sector; + pf_u16 current_fat; + pf_s32 fat_offset; + pf_u32 fat_sector; + pf_s32 result; + fat_offset = (pf_u16)(cluster + (cluster >> 1)); + fat_sector = (pf_u16)(p_vol->bpb.active_FAT_sector + (fat_offset >> p_vol->bpb.log2_bytes_per_sector)); + offset_in_sector = fat_offset & (p_vol->bpb.bytes_per_sector - 1); + err = 0; + result = 0; +#define FLUSH_FAT_BUFFER() \ + for (current_fat = 0; current_fat < p_vol->bpb.num_active_FATs; current_fat++) { \ + err = VFiPFSEC_WriteFAT(p_vol, p_page->p_buf, p_page->sector + current_fat * p_vol->bpb.sectors_per_FAT, 0, p_vol->bpb.bytes_per_sector); \ + if (err != 0 && result == 0) { result = err; } \ + } + if (p_page->sector != fat_sector) { + FLUSH_FAT_BUFFER(); + err = VFiPFSEC_ReadFAT(p_vol, p_page->p_buf, fat_sector, 0, p_vol->bpb.bytes_per_sector); + if (err != 0) { return err; } + p_page->sector = fat_sector; + } + if (cluster & 1) { + p_page->p_buf[(pf_u16)offset_in_sector] = (p_page->p_buf[(pf_u16)offset_in_sector] & 0xF) | ((value << 4) & 0xF0); + if ((pf_u16)offset_in_sector == (pf_u32)(p_vol->bpb.bytes_per_sector - 1)) { + FLUSH_FAT_BUFFER(); + offset_in_sector = fat_sector + 1; + err = VFiPFSEC_ReadFAT(p_vol, p_page->p_buf, offset_in_sector, 0, p_vol->bpb.bytes_per_sector); + if (err != 0) { return err; } + p_page->sector = offset_in_sector; + p_page->p_buf[0] = value >> 4; + } else { + ((pf_u16)offset_in_sector + p_page->p_buf)[1] = value >> 4; + } + } else { + p_page->p_buf[(pf_u16)offset_in_sector] = value; + if ((pf_u16)offset_in_sector == (pf_u32)(p_vol->bpb.bytes_per_sector - 1)) { + FLUSH_FAT_BUFFER(); + offset_in_sector = fat_sector + 1; + err = VFiPFSEC_ReadFAT(p_vol, p_page->p_buf, offset_in_sector, 0, p_vol->bpb.bytes_per_sector); + if (err != 0) { return err; } + p_page->sector = offset_in_sector; + p_page->p_buf[0] = (p_page->p_buf[0] & 0xF0) | ((pf_u8)(value >> 8)); + } else { + ((pf_u16)offset_in_sector + p_page->p_buf)[1] = (((pf_u16)offset_in_sector + p_page->p_buf)[1] & 0xF0) | ((pf_u8)(value >> 8)); + } + } + if (err != 0) { result = err; } + return result; +#undef FLUSH_FAT_BUFFER +} + +pf_s32 PFFAT12_ReadFATEntryWithBuf(PF_VOLUME* p_vol , pf_u16 cluster , pf_u32* p_value , + PF_CACHE_PAGE* p_page ) { + pf_s32 err; + pf_u32 current_fat; + pf_u32 sector; + pf_u32 offset; + pf_s32 result; + +#define LOAD_FAT_SECTOR(next_sector) \ + if (p_page->sector != sector + (next_sector)) { \ + if (p_page->option == 1) { \ + for (err = 0; (pf_u16)err < p_vol->bpb.num_active_FATs; err++) { \ + VFiPFSEC_WriteFAT(p_vol, p_page->p_buf, p_page->sector + (pf_u16)err * p_vol->bpb.sectors_per_FAT, 0, p_vol->bpb.bytes_per_sector); \ + } \ + } \ + err = VFiPFSEC_ReadFAT(p_vol, p_page->p_buf, sector + (next_sector), 0, p_vol->bpb.bytes_per_sector); \ + if (err != 0) { continue; } \ + p_page->sector = sector + (next_sector); \ + } + + offset = (pf_u16)(cluster + (cluster >> 1)); + sector = p_vol->bpb.active_FAT_sector + (offset >> p_vol->bpb.log2_bytes_per_sector) & 0xFFFF; + if ((p_vol->bpb.ext_flags & 0x80) != 0) { + current_fat = p_vol->bpb.ext_flags & (0x01 | 0x02 | 0x04); + } else { + current_fat = 1; + } + err = 0; + while (PF_TRUE) { + if (err == 0x1000 && p_vol->p_callback != PF_NULL) { + result = ((PF_VOLUME_CB)p_vol->p_callback)(p_vol->last_driver_error); + if (result == 0) { + err = 0; + continue; + } + if (result == 1 && p_vol->bpb.num_active_FATs >= 2 && current_fat < p_vol->bpb.num_active_FATs) { + current_fat++; + sector += p_vol->bpb.sectors_per_FAT; + err = 0; + continue; + } + } else { + LOAD_FAT_SECTOR(0); + offset &= p_vol->bpb.bytes_per_sector - 1; + if (offset == (p_vol->bpb.bytes_per_sector - 1)) { + *p_value = *(p_page->p_buf + offset); + if (p_page->option == 1) { + for (err = 0; (pf_u16)err < p_vol->bpb.num_active_FATs; err++) { + VFiPFSEC_WriteFAT(p_vol, p_page->p_buf, p_page->sector + (pf_u16)err * p_vol->bpb.sectors_per_FAT, 0, p_vol->bpb.bytes_per_sector); + } + } + err = VFiPFSEC_ReadFAT(p_vol, p_page->p_buf, sector + 1, 0, p_vol->bpb.bytes_per_sector); + if (err != 0) { continue; } + p_page->sector = sector + 1; + *p_value += (pf_u16)(*p_page->p_buf) << 8; + } else { + *p_value = ((pf_u16)p_page->p_buf[offset + 1] << 8) + *(p_page->p_buf + offset); + } + } + break; + } + if (err != 0) { + *p_value = -1; + return err; + } + if ((cluster & 0x01) != 0) { + *p_value = *p_value >> 4; + } else { + *p_value &= 0xFFF; + } + return 0; + +#undef LOAD_FAT_SECTOR +} diff --git a/libs/RVL_SDK/src/fa/pf_fat16.c b/libs/RVL_SDK/src/fa/pf_fat16.c new file mode 100644 index 00000000..c935b5d2 --- /dev/null +++ b/libs/RVL_SDK/src/fa/pf_fat16.c @@ -0,0 +1,158 @@ +#define PF_FA_STR_LAYOUT +#define VFiPFCACHE_ReadFATPage PFCACHE_ReadFATPage +#define VFiPFCACHE_UpdateModifiedSector PFCACHE_UpdateModifiedSector +#define VFiPFFAT16_ReadFATEntry PFFAT16_ReadFATEntry +#define VFiPFFAT16_ReadFATEntryPage PFFAT16_ReadFATEntryPage +#define VFiPFFAT16_WriteFATEntry PFFAT16_WriteFATEntry +#define VFiPFFAT16_WriteFATEntryPage PFFAT16_WriteFATEntryPage +#define VFiPFFAT_UpdateFATEntry PFFAT_UpdateFATEntry +#define VFiPFSEC_ReadFAT PFSEC_ReadFAT +#define VFiPFSEC_WriteFAT PFSEC_WriteFAT +#include +#include +#include + +#include +#include + +pf_s32 VFiPFFAT16_ReadFATEntry(PF_VOLUME* p_vol, pf_u32 cluster, pf_u32* p_value) { + pf_u32 fat_offset; + pf_u32 fat_sector; + pf_u16 offset_in_sector; + pf_u8 buf[sizeof(pf_u16)]; + pf_s32 err; + pf_u32 current_fat; + pf_s32 result; + + fat_offset = cluster * sizeof(pf_u16); + fat_sector = (pf_u16)(p_vol->bpb.active_FAT_sector + ((cluster * sizeof(pf_u16)) >> p_vol->bpb.log2_bytes_per_sector)); + offset_in_sector = fat_offset & (p_vol->bpb.bytes_per_sector - 1); + if ((p_vol->bpb.ext_flags & 0x80) != 0) { + current_fat = p_vol->bpb.ext_flags & (0x01 | 0x02 | 0x04); + } else { + current_fat = 1; + } + do { + err = VFiPFSEC_ReadFAT(p_vol, buf, fat_sector, offset_in_sector, 2); + if (err == 0x1000 && p_vol->p_callback != PF_NULL) { + result = ((PF_VOLUME_CB)p_vol->p_callback)(p_vol->last_driver_error); + if (result == 0) { + continue; + } + if (result == 1 && p_vol->bpb.num_active_FATs >= 2 && current_fat < p_vol->bpb.num_active_FATs) { + current_fat++; + fat_sector += p_vol->bpb.sectors_per_FAT; + continue; + } + } + if (err != 0) { + *p_value = -1; + return err; + } + } while (err != 0); + + *p_value = PF_SWAP_16(*(pf_u16*)buf); + return 0; +} + +pf_s32 VFiPFFAT16_WriteFATEntry(PF_VOLUME* p_vol, pf_u32 cluster, pf_u32 value) { + pf_s32 err; + pf_u32 fat_offset; + pf_u16 fat_sector; + pf_u16 offset_in_sector; + pf_u32 current_fat; + pf_s32 result; + pf_u8 buf[sizeof(pf_u16)]; + + fat_offset = cluster * sizeof(pf_u16); + fat_sector = p_vol->bpb.active_FAT_sector + ((cluster * sizeof(pf_u16)) >> p_vol->bpb.log2_bytes_per_sector); + offset_in_sector = fat_offset & (p_vol->bpb.bytes_per_sector - 1); + *(pf_u16*)buf = PF_SWAP_16(value); + result = 0; + for (current_fat = 0; current_fat < p_vol->bpb.num_active_FATs; current_fat++) { + err = VFiPFSEC_WriteFAT(p_vol, buf, fat_sector, offset_in_sector, 2); + if (err != 0 && result == 0) { + result = err; + } + fat_sector += p_vol->bpb.sectors_per_FAT; + } + return result; +} + +pf_s32 PFFAT16_WriteFATEntryWithBuf(PF_VOLUME* p_vol, pf_u32 cluster, pf_u32 value, PF_CACHE_PAGE* p_page) { + pf_u16 offset_in_sector; + pf_s32 err; + pf_u32 fat_offset; + pf_u16 fat_sector; + pf_u32 current_fat; + pf_s32 result; + fat_offset = cluster * sizeof(pf_u16); + fat_sector = (pf_u16)(p_vol->bpb.active_FAT_sector + (fat_offset >> p_vol->bpb.log2_bytes_per_sector)); + offset_in_sector = (pf_u16)(fat_offset & (p_vol->bpb.bytes_per_sector - 1)); + err = 0; + result = 0; + if (p_page->sector != fat_sector) { + for (current_fat = 0; current_fat < p_vol->bpb.num_active_FATs; current_fat++) { + err = VFiPFSEC_WriteFAT(p_vol, p_page->p_buf, p_page->sector + current_fat * p_vol->bpb.sectors_per_FAT, 0, p_vol->bpb.bytes_per_sector); + if (err != 0 && result == 0) { + result = err; + } + } + p_page->sector = fat_sector; + err = VFiPFSEC_ReadFAT(p_vol, p_page->p_buf, fat_sector, 0, p_vol->bpb.bytes_per_sector); + } + p_page->p_buf[offset_in_sector] = value; + (offset_in_sector + p_page->p_buf)[1] = value >> 8; + if (err != 0) { + result = err; + } + return result; +} + +pf_s32 PFFAT16_ReadFATEntryWithBuf(PF_VOLUME* p_vol, pf_u32 cluster, pf_u32* p_value, PF_CACHE_PAGE* p_page) { + pf_u32 sector; + pf_s32 err; + pf_u32 offset; + pf_u32 current_fat; + pf_s32 result; + + offset = cluster * sizeof(pf_u16); + sector = (pf_u16)(p_vol->bpb.active_FAT_sector + (offset >> p_vol->bpb.log2_bytes_per_sector)); + current_fat = 1; + if ((p_vol->bpb.ext_flags & 0x80) != 0) { + current_fat = p_vol->bpb.ext_flags & 7; + } + if (p_page->sector != sector) { + if (p_page->option == 1) { + pf_u16 fat; + for (fat = 0; fat < p_vol->bpb.num_active_FATs; fat++) { + VFiPFSEC_WriteFAT(p_vol, p_page->p_buf, p_page->sector + fat * p_vol->bpb.sectors_per_FAT, 0, p_vol->bpb.bytes_per_sector); + } + } + do { + err = VFiPFSEC_ReadFAT(p_vol, p_page->p_buf, sector, 0, p_vol->bpb.bytes_per_sector); + if (err != 0x1000 || p_vol->p_callback == PF_NULL) { + goto block_22; + } + result = ((PF_VOLUME_CB)p_vol->p_callback)(p_vol->last_driver_error); + if (result != 0) { + if (result == 1 && p_vol->bpb.num_active_FATs >= 2 && current_fat < p_vol->bpb.num_active_FATs) { + current_fat++; + sector += p_vol->bpb.sectors_per_FAT; + } else { + goto block_22; + } + } + continue; + block_22: + if (err != 0) { + return err; + } + p_page->sector = sector; + } while (err != 0); + } + offset &= p_vol->bpb.bytes_per_sector - 1; + *p_value = PF_SWAP_16(*((pf_u16*)(p_page->p_buf + offset))); + + return 0; +} diff --git a/libs/RVL_SDK/src/fa/pf_fat32.c b/libs/RVL_SDK/src/fa/pf_fat32.c new file mode 100644 index 00000000..329fdce0 --- /dev/null +++ b/libs/RVL_SDK/src/fa/pf_fat32.c @@ -0,0 +1,168 @@ +#define PF_FA_STR_LAYOUT +#define VFiPFCACHE_ReadFATPage PFCACHE_ReadFATPage +#define VFiPFCACHE_UpdateModifiedSector PFCACHE_UpdateModifiedSector +#define VFiPFFAT32_ReadFATEntry PFFAT32_ReadFATEntry +#define VFiPFFAT32_ReadFATEntryPage PFFAT32_ReadFATEntryPage +#define VFiPFFAT32_WriteFATEntry PFFAT32_WriteFATEntry +#define VFiPFFAT32_WriteFATEntryPage PFFAT32_WriteFATEntryPage +#define VFiPFFAT_UpdateFATEntry PFFAT_UpdateFATEntry +#define VFiPFSEC_ReadFAT PFSEC_ReadFAT +#define VFiPFSEC_WriteFAT PFSEC_WriteFAT +#include +#include +#include + +#include +#include + +pf_s32 VFiPFFAT32_ReadFATEntry(PF_VOLUME* p_vol, pf_u32 cluster, pf_u32* p_value) { + pf_u32 fat_offset; + pf_u32 fat_sector; + pf_u16 offset_in_sector; + pf_u8 buf[sizeof(pf_u32)]; + pf_s32 err; + pf_u32 current_fat; + pf_s32 result; + + fat_offset = cluster * sizeof(pf_u32); + fat_sector = (pf_u16)(p_vol->bpb.active_FAT_sector + (fat_offset >> p_vol->bpb.log2_bytes_per_sector)); + offset_in_sector = fat_offset & (p_vol->bpb.bytes_per_sector - 1); + if ((p_vol->bpb.ext_flags & 0x80) != 0) { + current_fat = p_vol->bpb.ext_flags & (0x01 | 0x02 | 0x04); + } else { + current_fat = 1; + } + do { + err = VFiPFSEC_ReadFAT(p_vol, buf, fat_sector, offset_in_sector, 4); + if (err == 0x1000 && p_vol->p_callback != PF_NULL) { + result = ((PF_VOLUME_CB)p_vol->p_callback)(p_vol->last_driver_error); + if (result == 0) { + continue; + } + if (result == 1 && p_vol->bpb.num_active_FATs >= 2 && current_fat < p_vol->bpb.num_active_FATs) { + current_fat++; + fat_sector += p_vol->bpb.sectors_per_FAT; + continue; + } + } + if (err != 0) { + *p_value = -1; + return err; + } + } while (err != 0); + + *p_value = PF_SWAP_32(*(pf_u32*)buf) & 0x0FFFFFFF; + return 0; +} + +pf_s32 VFiPFFAT32_WriteFATEntry(PF_VOLUME* p_vol, pf_u32 cluster, pf_u32 value) { + pf_s32 err; + pf_u32 fat_offset; + pf_u32 fat_sector; + pf_u16 offset_in_sector; + pf_u16 current_fat; + pf_s32 result; + pf_u8 buf[sizeof(pf_u32)]; + pf_u32 dword; + + fat_offset = cluster * sizeof(pf_u32); + fat_sector = (pf_u16)(p_vol->bpb.active_FAT_sector + (fat_offset >> p_vol->bpb.log2_bytes_per_sector)); + offset_in_sector = fat_offset & (p_vol->bpb.bytes_per_sector - 1); + err = VFiPFSEC_ReadFAT(p_vol, buf, fat_sector, offset_in_sector, 4); + if (err != 0) { + return err; + } + dword = (value & 0x0FFFFFFF) | (PF_SWAP_32(*(pf_u32*)buf) & 0xF0000000); + *(pf_u32*)buf = PF_SWAP_32(dword); + result = 0; + for (current_fat = 0; current_fat < p_vol->bpb.num_active_FATs; current_fat++) { + err = VFiPFSEC_WriteFAT(p_vol, buf, fat_sector, offset_in_sector, 4); + if (err != 0 && result == 0) { + result = err; + } + fat_sector += p_vol->bpb.sectors_per_FAT; + } + return result; +} + +pf_s32 PFFAT32_WriteFATEntryWithBuf(PF_VOLUME* p_vol, pf_u32 cluster, pf_u32 value, PF_CACHE_PAGE* p_page) { + pf_u32 merged_value; + pf_u16 offset_in_sector; + pf_s32 err; + pf_u32 fat_offset; + pf_u32 fat_sector; + pf_u16 current_fat; + pf_s32 result; + fat_offset = cluster * sizeof(pf_u32); + fat_sector = (pf_u16)(p_vol->bpb.active_FAT_sector + (fat_offset >> p_vol->bpb.log2_bytes_per_sector)); + offset_in_sector = (pf_u16)(fat_offset & (p_vol->bpb.bytes_per_sector - 1)); + err = 0; + result = 0; + if (p_page->sector != fat_sector) { + for (current_fat = 0; current_fat < p_vol->bpb.num_active_FATs; current_fat++) { + err = VFiPFSEC_WriteFAT(p_vol, p_page->p_buf, p_page->sector + current_fat * p_vol->bpb.sectors_per_FAT, 0, p_vol->bpb.bytes_per_sector); + if (err != 0 && result == 0) { + result = err; + } + } + err = VFiPFSEC_ReadFAT(p_vol, p_page->p_buf, fat_sector, 0, p_vol->bpb.bytes_per_sector); + if (err != 0) { + return err; + } + p_page->sector = fat_sector; + } + merged_value = (value & 0x0FFFFFFF) | (PF_SWAP_32(*(pf_u32*)&p_page->p_buf[offset_in_sector]) & 0xF0000000); + *(pf_u32*)&p_page->p_buf[offset_in_sector] = PF_SWAP_32(merged_value); + if (err != 0) { + result = err; + } + return result; +} + +pf_s32 PFFAT32_ReadFATEntryWithBuf(PF_VOLUME* p_vol, pf_u32 cluster, pf_u32* p_value, PF_CACHE_PAGE* p_page) { + pf_u32 sector; + pf_s32 err; + pf_u32 offset; + pf_u32 current_fat; + pf_s32 result; + + offset = cluster * sizeof(pf_u32); + sector = (pf_u16)(p_vol->bpb.active_FAT_sector + (offset >> p_vol->bpb.log2_bytes_per_sector)); + current_fat = 1; + if ((p_vol->bpb.ext_flags & 0x80) != 0) { + current_fat = p_vol->bpb.ext_flags & 7; + } + if (p_page->sector != sector) { + if (p_page->option == 1) { + pf_u16 fat; + for (fat = 0; fat < p_vol->bpb.num_active_FATs; fat++) { + VFiPFSEC_WriteFAT(p_vol, p_page->p_buf, p_page->sector + fat * p_vol->bpb.sectors_per_FAT, 0, p_vol->bpb.bytes_per_sector); + } + } + do { + err = VFiPFSEC_ReadFAT(p_vol, p_page->p_buf, sector, 0, p_vol->bpb.bytes_per_sector); + if (err != 0x1000 || p_vol->p_callback == PF_NULL) { + goto block_22; + } + result = ((PF_VOLUME_CB)p_vol->p_callback)(p_vol->last_driver_error); + if (result != 0) { + if (result == 1 && p_vol->bpb.num_active_FATs >= 2 && current_fat < p_vol->bpb.num_active_FATs) { + current_fat++; + sector += p_vol->bpb.sectors_per_FAT; + } else { + goto block_22; + } + } + continue; + block_22: + if (err != 0) { + return err; + } + p_page->sector = sector; + } while (err != 0); + } + offset &= p_vol->bpb.bytes_per_sector - 1; + *p_value = PF_SWAP_32(*((pf_u32*)(p_page->p_buf + offset))) & 0x0FFFFFFF; + + return 0; +} diff --git a/libs/RVL_SDK/src/fa/pf_filelock.c b/libs/RVL_SDK/src/fa/pf_filelock.c new file mode 100644 index 00000000..ff2e42b8 --- /dev/null +++ b/libs/RVL_SDK/src/fa/pf_filelock.c @@ -0,0 +1,56 @@ +#include +#include + +typedef struct PFFILELOCK_SFD { + pf_u8 descriptor[0x3c]; + PF_DIR_ENT dir_entry; + pf_u16 lock_mode; + pf_u16 lock_count; + pf_u32 writer_count; + void* owner; + pf_s32 resource; + OSSemaphore semaphore; +} PFFILELOCK_SFD; +typedef struct PFFILELOCK_FILE { + pf_u32 stat; + pf_u32 open_mode; + PFFILELOCK_SFD* p_sfd; +} PFFILELOCK_FILE; +typedef struct PFFILELOCK_SEMAPHORE_PARAM { + pf_u32 initial_count; + pf_u32 max_count; + OSSemaphore* semaphore; +} PFFILELOCK_SEMAPHORE_PARAM; +extern pf_s32 pfk_create_semaphore(PFFILELOCK_SEMAPHORE_PARAM*); +extern pf_s32 pfk_get_semaphore(pf_s32); +extern pf_s32 pfk_release_semaphore(pf_s32); +extern pf_s32 pfk_delete_semaphore(pf_s32); + +void PF_InitLockFile(void) { + pf_sys_set.flock_count = 0; +} +pf_s32 PF_LockFile(PFFILELOCK_FILE* p_file) { + pf_s32 result = -1; + PFFILELOCK_SEMAPHORE_PARAM param; + if (p_file->p_sfd->lock_count == 0 && p_file->p_sfd->resource == 0) { + if (pf_sys_set.flock_count < -1U) { + param.initial_count = 1; + param.max_count = 1; + param.semaphore = &p_file->p_sfd->semaphore; + p_file->p_sfd->resource = pfk_create_semaphore(¶m); + result = 0; + ++pf_sys_set.flock_count; + } else result = -1; + } + pfk_get_semaphore(p_file->p_sfd->resource); + return result; +} +pf_s32 PF_UnLockFile(PFFILELOCK_FILE* p_file) { + pfk_release_semaphore(p_file->p_sfd->resource); + if (p_file->p_sfd->lock_count == 0 && p_file->p_sfd->writer_count == 0 && pf_sys_set.flock_count != 0) { + pfk_delete_semaphore(p_file->p_sfd->resource); + p_file->p_sfd->resource = 0; + --pf_sys_set.flock_count; + } + return 0; +} diff --git a/libs/RVL_SDK/src/fa/pf_finfo.c b/libs/RVL_SDK/src/fa/pf_finfo.c new file mode 100644 index 00000000..7a5fcb2a --- /dev/null +++ b/libs/RVL_SDK/src/fa/pf_finfo.c @@ -0,0 +1,13 @@ +#define PF_FA_STR_LAYOUT +#define VFiPFAPI_convertReturnValue PFAPI_convertReturnValue +#define VFiPFFILE_finfo PFFILE_finfo +#include + +#include +#include + +pf_int32 pf2_finfo(PF_FILE* p_file, PF_INFO* p_info) { + pf_s32 err = VFiPFFILE_finfo(p_file, p_info); + err = VFiPFAPI_convertReturnValue(err); + return err; +} diff --git a/libs/RVL_SDK/src/fa/pf_fopen.c b/libs/RVL_SDK/src/fa/pf_fopen.c new file mode 100644 index 00000000..251bdf44 --- /dev/null +++ b/libs/RVL_SDK/src/fa/pf_fopen.c @@ -0,0 +1,38 @@ +#define PF_FA_STR_LAYOUT +#define VFiPFAPI_ParseOpenModeString PFAPI_ParseOpenModeString +#define VFiPFAPI_convertReturnValue2NULL PFAPI_convertReturnValue2NULL +#define VFiPFFILE_fopen PFFILE_fopen +#define VFiPFSTR_InitStr PFSTR_InitStr +#include + +typedef struct PFAPI_GLOBAL_STATE { + pf_u8 volume_state[0x40]; + pf_s32 last_error; +} PFAPI_GLOBAL_STATE; +extern PFAPI_GLOBAL_STATE pf_vol_set; + + +#include +#include + +PF_FILE* pf2_fopen(const pf_ch8* path, const pf_ch8* mode) { + pf_u32 open_mode; + PF_FILE* p_file; + PF_STR path_str; + pf_s32 err; + + open_mode = VFiPFAPI_ParseOpenModeString(mode); + if (open_mode == 0) { + pf_vol_set.last_error = 10; + p_file = PF_NULL; + } else { + err = VFiPFSTR_InitStr(&path_str, (pf_s8*)path, 1U); + if (err == 0) { + err = VFiPFFILE_fopen(&path_str, open_mode, &p_file); + } else { + pf_vol_set.last_error = err; + } + p_file = VFiPFAPI_convertReturnValue2NULL(err, p_file); + } + return p_file; +} diff --git a/libs/RVL_SDK/src/fa/pf_format.c b/libs/RVL_SDK/src/fa/pf_format.c new file mode 100644 index 00000000..65358bd8 --- /dev/null +++ b/libs/RVL_SDK/src/fa/pf_format.c @@ -0,0 +1,13 @@ +#define PF_FA_STR_LAYOUT +#define VFiPFAPI_convertReturnValue PFAPI_convertReturnValue +#define VFiPFVOL_format PFVOL_format +#include + +#include +#include + +pf_int32 pf2_format(pf_ch8 drive, const pf_ch8* param) { + pf_s32 err = VFiPFVOL_format(drive, (pf_u8*)param); + err = VFiPFAPI_convertReturnValue(err); + return err; +} diff --git a/libs/RVL_SDK/src/fa/pf_fseek.c b/libs/RVL_SDK/src/fa/pf_fseek.c new file mode 100644 index 00000000..b504cf24 --- /dev/null +++ b/libs/RVL_SDK/src/fa/pf_fseek.c @@ -0,0 +1,13 @@ +#define PF_FA_STR_LAYOUT +#define VFiPFAPI_convertReturnValue PFAPI_convertReturnValue +#define VFiPFFILE_fseek PFFILE_fseek +#include + +#include +#include + +pf_int32 pf2_fseek(PF_FILE* pFile, pf_s32 lOffset, pf_int32 nOrigin) { + pf_s32 err = VFiPFFILE_fseek(pFile, lOffset, nOrigin); + err = VFiPFAPI_convertReturnValue(err); + return err; +} diff --git a/libs/RVL_SDK/src/fa/pf_fsfirst.c b/libs/RVL_SDK/src/fa/pf_fsfirst.c new file mode 100644 index 00000000..01ad1f47 --- /dev/null +++ b/libs/RVL_SDK/src/fa/pf_fsfirst.c @@ -0,0 +1,30 @@ +#define PF_FA_STR_LAYOUT +#define VFiPFAPI_convertReturnValue PFAPI_convertReturnValue +#define VFiPFDIR_fsfirst PFDIR_fsfirst +#define VFiPFSTR_InitStr PFSTR_InitStr +#include + +typedef struct PFAPI_GLOBAL_STATE { + pf_u8 volume_state[0x40]; + pf_s32 last_error; +} PFAPI_GLOBAL_STATE; +extern PFAPI_GLOBAL_STATE pf_vol_set; + + +#include +#include + +pf_int32 pf2_fsfirst(const pf_ch8* path, pf_u8 attrs, PF_DTA* p_dta) { + pf_s32 err; + PF_STR path_str; + + err = VFiPFSTR_InitStr(&path_str, (pf_s8*)path, 1U); + if (err == 0) { + err = VFiPFDIR_fsfirst(&path_str, attrs, p_dta); + } else { + pf_vol_set.last_error = err; + } + err = VFiPFAPI_convertReturnValue(err); + + return err; +} diff --git a/libs/RVL_SDK/src/fa/pf_fsnext.c b/libs/RVL_SDK/src/fa/pf_fsnext.c new file mode 100644 index 00000000..4bf035eb --- /dev/null +++ b/libs/RVL_SDK/src/fa/pf_fsnext.c @@ -0,0 +1,15 @@ +#define PF_FA_STR_LAYOUT +#define VFiPFAPI_convertReturnValue PFAPI_convertReturnValue +#define VFiPFDIR_fsnext PFDIR_fsnext +#include + +#include +#include + +pf_int32 pf2_fsnext(PF_DTA* p_dta) { + pf_s32 err; + + err = VFiPFDIR_fsnext(p_dta); + err = VFiPFAPI_convertReturnValue(err); + return err; +} diff --git a/libs/RVL_SDK/src/fa/pf_getdev.c b/libs/RVL_SDK/src/fa/pf_getdev.c new file mode 100644 index 00000000..79c70e8a --- /dev/null +++ b/libs/RVL_SDK/src/fa/pf_getdev.c @@ -0,0 +1,13 @@ +#define PF_FA_STR_LAYOUT +#define VFiPFAPI_convertReturnValue PFAPI_convertReturnValue +#define VFiPFVOL_getdev PFVOL_getdev +#include + +#include +#include + +pf_int32 pf2_devinf(pf_ch8 drv_char, PF_DEV_INF* dev_inf) { + pf_s32 err = VFiPFVOL_getdev(drv_char, dev_inf); + err = VFiPFAPI_convertReturnValue(err); + return err; +} diff --git a/libs/RVL_SDK/src/fa/pf_init_prfile2.c b/libs/RVL_SDK/src/fa/pf_init_prfile2.c new file mode 100644 index 00000000..d325663a --- /dev/null +++ b/libs/RVL_SDK/src/fa/pf_init_prfile2.c @@ -0,0 +1,19 @@ +#define PF_FA_STR_LAYOUT +#define VFiPFAPI_convertReturnValue PFAPI_convertReturnValue +#define VFiPFFATFS_initializeFATFS PFFATFS_initializeFATFS +#define VFiPFSYS_initializeSYS PFSYS_initializeSYS +#include + +#include +#include +#include + + +pf_int32 pf2_init_prfile2(pf_s32 config, void* param) { + pf_s32 err = VFiPFFATFS_initializeFATFS(config, param); + if (err == 0) { + VFiPFSYS_initializeSYS(); + } + err = VFiPFAPI_convertReturnValue(err); + return err; +} diff --git a/libs/RVL_SDK/src/fa/pf_mkdir.c b/libs/RVL_SDK/src/fa/pf_mkdir.c new file mode 100644 index 00000000..a70a5fc3 --- /dev/null +++ b/libs/RVL_SDK/src/fa/pf_mkdir.c @@ -0,0 +1,31 @@ +#define PF_FA_STR_LAYOUT +#define VFiPFAPI_convertReturnValue PFAPI_convertReturnValue +#define VFiPFDIR_mkdir PFDIR_mkdir +#define VFiPFSTR_InitStr PFSTR_InitStr +#include + +typedef struct PFAPI_GLOBAL_STATE { + pf_u8 volume_state[0x40]; + pf_s32 last_error; +} PFAPI_GLOBAL_STATE; +extern PFAPI_GLOBAL_STATE pf_vol_set; + + +#include +#include + +#include + +pf_int32 pf2_mkdir(const pf_ch8* sPath) { + pf_s32 err; + PF_STR path_str; + + err = VFiPFSTR_InitStr(&path_str, (pf_s8*)sPath, 1U); + if (err == 0) { + err = VFiPFDIR_mkdir(&path_str); + } else { + pf_vol_set.last_error = err; + } + err = VFiPFAPI_convertReturnValue(err); + return err; +} diff --git a/libs/RVL_SDK/src/fa/pf_remove.c b/libs/RVL_SDK/src/fa/pf_remove.c new file mode 100644 index 00000000..e75fdcee --- /dev/null +++ b/libs/RVL_SDK/src/fa/pf_remove.c @@ -0,0 +1,31 @@ +#define PF_FA_STR_LAYOUT +#define VFiPFAPI_convertReturnValue PFAPI_convertReturnValue +#define VFiPFFILE_remove PFFILE_remove +#define VFiPFSTR_InitStr PFSTR_InitStr +#include + +typedef struct PFAPI_GLOBAL_STATE { + pf_u8 volume_state[0x40]; + pf_s32 last_error; +} PFAPI_GLOBAL_STATE; +extern PFAPI_GLOBAL_STATE pf_vol_set; + + +#include +#include + +#include + +pf_int32 pf2_remove(const pf_ch8* path) { + pf_s32 err; + PF_STR path_str; + + err = VFiPFSTR_InitStr(&path_str, (pf_s8*)path, 1U); + if (err == 0) { + err = VFiPFFILE_remove(&path_str); + } else { + pf_vol_set.last_error = err; + } + err = VFiPFAPI_convertReturnValue(err); + return err; +} diff --git a/libs/RVL_SDK/src/fa/pf_rmdir.c b/libs/RVL_SDK/src/fa/pf_rmdir.c new file mode 100644 index 00000000..bf2175a0 --- /dev/null +++ b/libs/RVL_SDK/src/fa/pf_rmdir.c @@ -0,0 +1,31 @@ +#define PF_FA_STR_LAYOUT +#define VFiPFAPI_convertReturnValue PFAPI_convertReturnValue +#define VFiPFDIR_rmdir PFDIR_rmdir +#define VFiPFSTR_InitStr PFSTR_InitStr +#include + +typedef struct PFAPI_GLOBAL_STATE { + pf_u8 volume_state[0x40]; + pf_s32 last_error; +} PFAPI_GLOBAL_STATE; +extern PFAPI_GLOBAL_STATE pf_vol_set; + + +#include +#include + +#include + +pf_int32 pf2_rmdir(const pf_ch8* sPath) { + pf_s32 err; + PF_STR path_str; + + err = VFiPFSTR_InitStr(&path_str, (pf_s8*)sPath, 1U); + if (err == 0) { + err = VFiPFDIR_rmdir(&path_str); + } else { + pf_vol_set.last_error = err; + } + err = VFiPFAPI_convertReturnValue(err); + return err; +} diff --git a/libs/RVL_SDK/src/fa/pf_sector.c b/libs/RVL_SDK/src/fa/pf_sector.c new file mode 100644 index 00000000..c91ad8b7 --- /dev/null +++ b/libs/RVL_SDK/src/fa/pf_sector.c @@ -0,0 +1,216 @@ +#define PF_FA_STR_LAYOUT +#define VFiPFCACHE_AllocateDataPage PFCACHE_AllocateDataPage +#define VFiPFCACHE_ReadDataNumSector PFCACHE_ReadDataNumSector +#define VFiPFCACHE_ReadDataPage PFCACHE_ReadDataPage +#define VFiPFCACHE_ReadDataPageAndFlushIfNeeded PFCACHE_ReadDataPageAndFlushIfNeeded +#define VFiPFCACHE_ReadFATPage PFCACHE_ReadFATPage +#define VFiPFCACHE_SearchDataCache PFCACHE_SearchDataCache +#define VFiPFCACHE_UpdateModifiedSector PFCACHE_UpdateModifiedSector +#define VFiPFCACHE_WriteDataNumSectorAndFreeIfNeeded PFCACHE_WriteDataNumSectorAndFreeIfNeeded +#define VFiPFCACHE_WriteDataPage PFCACHE_WriteDataPage +#define VFiPFCACHE_WriteFATPage PFCACHE_WriteFATPage +#define VFiPFDRV_IsInserted PFDRV_IsInserted +#define VFiPFDRV_IsWProtected PFDRV_IsWProtected +#define VFiPFSEC_ReadData PFSEC_ReadData +#define VFiPFSEC_ReadDataSector PFSEC_ReadDataSector +#define VFiPFSEC_ReadFAT PFSEC_ReadFAT +#define VFiPFSEC_WriteData PFSEC_WriteData +#define VFiPFSEC_WriteDataSector PFSEC_WriteDataSector +#define VFiPFSEC_WriteFAT PFSEC_WriteFAT +#define VFipf_memcpy pf_memcpy +#include +#include + + +#include +#include +#include + +pf_s32 VFiPFSEC_ReadFAT(PF_VOLUME* p_vol, pf_u8* p_buf, pf_u32 sector, pf_u16 offset, pf_u16 size) { + PF_CACHE_PAGE* p_page; + pf_s32 err; + + if ((p_vol->flags & 0x01) == 0 || VFiPFDRV_IsInserted(p_vol) == PF_FALSE) { + return 9; + } + if (sector >= p_vol->bpb.total_sectors || offset >= p_vol->bpb.bytes_per_sector || offset + size > p_vol->bpb.bytes_per_sector) { + return 10; + } + err = VFiPFCACHE_ReadFATPage(p_vol, sector, &p_page); + if (err != 0) { + return err; + } + VFipf_memcpy(p_buf, &p_page->p_buf[offset], size); + return 0; +} + +pf_s32 VFiPFSEC_ReadData(PF_VOLUME* p_vol, pf_u8* p_buf, pf_u32 sector, pf_u16 offset, pf_u32 size, pf_u32* p_success_size, pf_u32 set_sig) { + PF_CACHE_PAGE* p_page; + pf_u32 num_success; + pf_u32 num_sector; + pf_u32 adjust_sector; + pf_u32 rest_sector; + pf_s32 err; + + *p_success_size = 0; + if ((p_vol->flags & 0x01) == 0 || VFiPFDRV_IsInserted(p_vol) == PF_FALSE) { + return 9; + } + if ((sector >= p_vol->bpb.total_sectors || offset >= p_vol->bpb.bytes_per_sector || offset + size > p_vol->bpb.bytes_per_sector) && offset != 0) { + return 10; + } + if (offset != 0 || size < p_vol->bpb.bytes_per_sector) { + err = VFiPFCACHE_ReadDataPage(p_vol, sector, &p_page, set_sig); + if (err != 0) { + return err; + } + VFipf_memcpy(p_buf, &p_page->p_buf[offset], size); + *p_success_size = size; + } else if (offset == 0 && (size & (p_vol->bpb.bytes_per_sector - 1)) == 0) { + err = VFiPFCACHE_ReadDataNumSector(p_vol, p_buf, sector, size >> p_vol->bpb.log2_bytes_per_sector, &num_success); + if (err != 0) { + return err; + } + *p_success_size += num_success << p_vol->bpb.log2_bytes_per_sector; + if (num_success != (size >> p_vol->bpb.log2_bytes_per_sector)) { + return 17; + } + } else if (offset == 0) { + p_page = VFiPFCACHE_SearchDataCache(p_vol, sector); + if (p_page != PF_NULL) { + num_sector = (p_page->sector + p_page->size) - sector; + if (num_sector <= (size >> p_vol->bpb.log2_bytes_per_sector)) { + VFipf_memcpy(p_buf, p_page->p_buf, num_sector << p_vol->bpb.log2_bytes_per_sector); + *p_success_size += num_sector << p_vol->bpb.log2_bytes_per_sector; + size -= num_sector << p_vol->bpb.log2_bytes_per_sector; + sector += num_sector; + } else if (num_sector > (size >> p_vol->bpb.log2_bytes_per_sector)) { + VFipf_memcpy(p_buf, p_page->p_buf, size); + *p_success_size += size; + size = 0; + } + } + if (size != 0) { + adjust_sector = 0; + num_sector = size >> p_vol->bpb.log2_bytes_per_sector; + rest_sector = (sector + num_sector) % p_vol->cache.data_buff_size; + if (num_sector > rest_sector) { + adjust_sector = num_sector - rest_sector; + err = VFiPFCACHE_ReadDataNumSector(p_vol, &p_buf[*p_success_size], sector, adjust_sector, &num_success); + if (err != 0) { + return err; + } + *p_success_size += num_success << p_vol->bpb.log2_bytes_per_sector; + if (num_success != adjust_sector) { + return 17; + } + sector += adjust_sector; + num_sector -= adjust_sector; + } + if (num_sector != 0) { + err = VFiPFCACHE_ReadDataPage(p_vol, sector, &p_page, set_sig); + if (err != 0) { + return err; + } + VFipf_memcpy(&p_buf[*p_success_size], p_page->p_buf, num_sector << p_vol->bpb.log2_bytes_per_sector); + *p_success_size += num_sector << p_vol->bpb.log2_bytes_per_sector; + } + } + } else { + return 10; + } + + return 0; +} + +pf_s32 VFiPFSEC_WriteFAT(PF_VOLUME* p_vol, const pf_u8* p_buf, pf_u32 sector, pf_u16 offset, pf_u16 size) { + PF_CACHE_PAGE* p_page; + pf_s32 err; + + if ((p_vol->flags & 0x01) == 0 || !VFiPFDRV_IsInserted(p_vol) || (p_vol->flags & 2)) { + return 9; + } + if (sector >= p_vol->bpb.total_sectors || offset >= p_vol->bpb.bytes_per_sector || offset + size > p_vol->bpb.bytes_per_sector) { + return 10; + } + err = VFiPFCACHE_ReadFATPage(p_vol, sector, &p_page); + if (err != 0) { + return err; + } + VFipf_memcpy(&p_page->p_buf[offset], (pf_u8*)p_buf, size); + err = VFiPFCACHE_WriteFATPage(p_vol, p_page); + if (err != 0) { + return err; + } + return 0; +} + +pf_s32 VFiPFSEC_WriteData(PF_VOLUME* p_vol, const pf_u8* p_buf, pf_u32 sector, pf_u16 offset, pf_u32 size, pf_u32* p_success_size, pf_bool set_sig) { + pf_s32 err; + PF_CACHE_PAGE* p_page; + pf_u32 num_success; + pf_u32 num_sector; + pf_u32 adjust_sector; + pf_u32 rest_sector; + + *p_success_size = 0; + if ((p_vol->flags & 0x01) == 0 || !VFiPFDRV_IsInserted(p_vol) || (p_vol->flags & 2)) { + return 9; + } + if ((sector >= p_vol->bpb.total_sectors || offset >= p_vol->bpb.bytes_per_sector || offset + size > p_vol->bpb.bytes_per_sector) && offset != 0) { + return 10; + } + if (offset != 0 || size < p_vol->bpb.bytes_per_sector) { + err = VFiPFCACHE_ReadDataPageAndFlushIfNeeded(p_vol, sector, &p_page, set_sig); + if (err != 0) { + return err; + } + VFipf_memcpy(&p_page->p_buf[offset], (void*)p_buf, size); + err = VFiPFCACHE_WriteDataPage(p_vol, p_page, set_sig); + if (err != 0) { + return err; + } + *p_success_size = size; + } else if (offset == 0 && (size & (p_vol->bpb.bytes_per_sector - 1)) == 0) { + err = VFiPFCACHE_WriteDataNumSectorAndFreeIfNeeded(p_vol, p_buf, sector, size >> p_vol->bpb.log2_bytes_per_sector, &num_success); + if (err != 0) { + return err; + } + *p_success_size = num_success << p_vol->bpb.log2_bytes_per_sector; + if (num_success != (size >> p_vol->bpb.log2_bytes_per_sector)) { + return 17; + } + } else if (offset == 0) { + adjust_sector = 0; + num_sector = size >> p_vol->bpb.log2_bytes_per_sector; + rest_sector = (sector + num_sector) % p_vol->cache.data_buff_size; + if (num_sector > rest_sector) { + adjust_sector = num_sector - rest_sector; + err = VFiPFCACHE_WriteDataNumSectorAndFreeIfNeeded(p_vol, p_buf, sector, adjust_sector, &num_success); + if (err != 0) { + return err; + } + *p_success_size = num_success << p_vol->bpb.log2_bytes_per_sector; + if (num_success != adjust_sector) { + return 17; + } + sector += adjust_sector; + num_sector -= adjust_sector; + } + if (num_sector != 0) { + err = VFiPFCACHE_ReadDataPageAndFlushIfNeeded(p_vol, sector, &p_page, set_sig); + if (err != 0) { + return err; + } + VFipf_memcpy(p_page->p_buf, (pf_u8*)&p_buf[*p_success_size], num_sector << p_vol->bpb.log2_bytes_per_sector); + err = VFiPFCACHE_WriteDataPage(p_vol, p_page, set_sig); + if (err != 0) { + return err; + } + *p_success_size += num_sector << p_vol->bpb.log2_bytes_per_sector; + } + } else { + return 10; + } + return 0; +} diff --git a/libs/RVL_SDK/src/fa/pf_service.c b/libs/RVL_SDK/src/fa/pf_service.c new file mode 100644 index 00000000..743b6b25 --- /dev/null +++ b/libs/RVL_SDK/src/fa/pf_service.c @@ -0,0 +1,24 @@ +#define VFiPF_GET_LE_U16 PF_GET_LE_U16 +#define VFiPF_LE16_TO_U16_STR PF_LE16_TO_U16_STR +#include + +pf_u16 VFiPF_GET_LE_U16(const pf_u8* buf) { + return (pf_u16)((pf_u16)buf[1] << 8 | (pf_u16)buf[0]); +} + +void VFiPF_LE16_TO_U16_STR(pf_u8* sSrc, pf_u32 num) { + pf_u32 cnt; + pf_s8 tmp_src; + + for (cnt = 0; cnt < num; cnt += sizeof(pf_u16)) { + pf_s8* p = (pf_s8*)sSrc + cnt; + tmp_src = *p; +#ifdef DEBUG + *((pf_u8*)sSrc + cnt) = *((pf_u8*)sSrc + cnt + 1); + *((pf_u8*)sSrc + cnt + 1) = tmp_src; +#else + sSrc[cnt] = sSrc[cnt + 1]; + sSrc[cnt + 1] = tmp_src; +#endif + } +} diff --git a/libs/RVL_SDK/src/fa/pf_sync.c b/libs/RVL_SDK/src/fa/pf_sync.c new file mode 100644 index 00000000..3fdbe6d0 --- /dev/null +++ b/libs/RVL_SDK/src/fa/pf_sync.c @@ -0,0 +1,13 @@ +#define PF_FA_STR_LAYOUT +#define VFiPFAPI_convertReturnValue PFAPI_convertReturnValue +#define VFiPFVOL_sync PFVOL_sync +#include + +#include +#include + +pf_int32 pf2_sync(pf_ch8 drv_char, pf_s32 mode) { + pf_s32 err = VFiPFVOL_sync(drv_char, mode); + err = VFiPFAPI_convertReturnValue(err); + return err; +} diff --git a/libs/RVL_SDK/src/fa/pf_system.c b/libs/RVL_SDK/src/fa/pf_system.c new file mode 100644 index 00000000..6db079d6 --- /dev/null +++ b/libs/RVL_SDK/src/fa/pf_system.c @@ -0,0 +1,30 @@ +#include + +typedef struct PFSYS_DATE { u32 sys_year, sys_month, sys_day; } PFSYS_DATE; +typedef struct PFSYS_TIME { u32 sys_hour, sys_min, sys_sec, sys_ms; } PFSYS_TIME; +typedef struct PFSYS_VOLUME_SET { + u8 volume_state[0x64]; + s32 context_id; +} PFSYS_VOLUME_SET; +extern PFSYS_VOLUME_SET pf_vol_set; +extern void pfstub_init_stub(void); +PFSYS_SYSTEM_SET pf_sys_set; + +void PFSYS_initializeSYS(void) { + pfstub_init_stub(); +} +s32 PFSYS_GetCurrentContextID(s32* context_id) { + *context_id = pf_vol_set.context_id; + return 0; +} +void PFSYS_TimeStamp(PFSYS_DATE* sdate, PFSYS_TIME* stime) { + OSCalendarTime calendar; + OSTicksToCalendarTime(OSGetTime(), &calendar); + sdate->sys_year = calendar.year; + sdate->sys_month = calendar.mon + 1; + sdate->sys_day = calendar.mday; + stime->sys_hour = calendar.hour; + stime->sys_min = calendar.min; + stime->sys_sec = calendar.sec; + stime->sys_ms = 1; +} diff --git a/libs/RVL_SDK/src/fa/pf_unmount.c b/libs/RVL_SDK/src/fa/pf_unmount.c new file mode 100644 index 00000000..a2eb2848 --- /dev/null +++ b/libs/RVL_SDK/src/fa/pf_unmount.c @@ -0,0 +1,13 @@ +#define PF_FA_STR_LAYOUT +#define VFiPFAPI_convertReturnValue4unmount PFAPI_convertReturnValue4unmount +#define VFiPFVOL_unmount PFVOL_unmount +#include + +#include +#include + +pf_int32 pf2_unmount(pf_ch8 drive, pf_u32 mode) { + pf_s32 err = VFiPFVOL_unmount(drive, mode); + err = VFiPFAPI_convertReturnValue4unmount(err); + return err; +} diff --git a/libs/RevoEX/include/private/vf/PrFILE2/common/pf_str.h b/libs/RevoEX/include/private/vf/PrFILE2/common/pf_str.h index 8bbf7f77..6b858b6a 100644 --- a/libs/RevoEX/include/private/vf/PrFILE2/common/pf_str.h +++ b/libs/RevoEX/include/private/vf/PrFILE2/common/pf_str.h @@ -7,6 +7,9 @@ typedef struct PF_STR { const pf_s8* p_head; // 0x00 const pf_s8* p_tail; // 0x04 +#ifdef PF_FA_STR_LAYOUT + const pf_s8* p_current; +#endif pf_u32 code_mode; // 0x08 } PF_STR; diff --git a/libs/RevoEX/include/private/vf/PrFILE2/local/pf_cp932_to_unicode.h b/libs/RevoEX/include/private/vf/PrFILE2/local/pf_cp932_to_unicode.h index 0b049b11..6e1e2c99 100644 --- a/libs/RevoEX/include/private/vf/PrFILE2/local/pf_cp932_to_unicode.h +++ b/libs/RevoEX/include/private/vf/PrFILE2/local/pf_cp932_to_unicode.h @@ -128,7 +128,19 @@ static const pf_u16 cp932_to_unicode[45][189] = { 0x6A5F, 0x5E30, 0x6BC5, 0x6C17, 0x6C7D, 0x757F, 0x7948, 0x5B63, 0x7A00, 0x7D00, 0x5FBD, 0x898F, 0x8A18, 0x8CB4, 0x8D77, 0x8ECC, 0x8F1D, 0x98E2, 0x9A0E, 0x9B3C, 0x4E80, 0x507D, 0x5100, 0x5993, 0x5B9C, 0x622F, 0x6280, 0x64EC, 0x6B3A, 0x72A0, 0x7591, 0x7947, 0x7FA9, 0x87FB, 0x8ABC, 0x8B70, 0x63AC, 0x83CA, 0x97A0, 0x5409, 0x5403, 0x55AB, 0x6854, 0x6A58, 0x8A70, 0x7827, 0x6775, 0x9ECD, - 0x5374, 0x5BA2, 0x811A, 0x8650, 0x9006, 0x4E18, 0x4E45, 0x4EC7, 0x4F11, 0x53CA, 0x5438, 0x5BAE, 0x5F13, 0x6025, 0x6551, 0x0000, + 0x5374, 0x5BA2, +#ifdef PF_FA_CP932_TABLE + 0x0000 +#else + 0x811A +#endif +, +#ifdef PF_FA_CP932_TABLE + 0x0000 +#else + 0x8650 +#endif +, 0x9006, 0x4E18, 0x4E45, 0x4EC7, 0x4F11, 0x53CA, 0x5438, 0x5BAE, 0x5F13, 0x6025, 0x6551, 0x0000, 0x673D, 0x6C42, 0x6C72, 0x6CE3, 0x7078, 0x7403, 0x7A76, 0x7AAE, 0x7B08, 0x7D1A, 0x7CFE, 0x7D66, 0x65E7, 0x725B, 0x53BB, 0x5C45, 0x5DE8, 0x62D2, 0x62E0, 0x6319, 0x6E20, 0x865A, 0x8A31, 0x8DDD, 0x92F8, 0x6F01, 0x79A6, 0x9B5A, 0x4EA8, 0x4EAB, 0x4EAC, 0x4F9B, 0x4FA0, 0x50D1, 0x5147, 0x7AF6, 0x5171, 0x51F6, 0x5354, 0x5321, 0x537F, 0x53EB, 0x55AC, 0x5883, 0x5CE1, 0x5F37, 0x5F4A, 0x602F, @@ -236,8 +248,20 @@ static const pf_u16 cp932_to_unicode[45][189] = { 0x6065, 0x667A, 0x6C60, 0x75F4, 0x7A1A, 0x7F6E, 0x81F4, 0x8718, 0x9045, 0x99B3, 0x7BC9, 0x755C, 0x7AF9, 0x7B51, 0x84C4, 0x0000, 0x9010, 0x79E9, 0x7A92, 0x8336, 0x5AE1, 0x7740, 0x4E2D, 0x4EF2, 0x5B99, 0x5FE0, 0x62BD, 0x663C, 0x67F1, 0x6CE8, 0x866B, 0x8877, 0x8A3B, 0x914E, 0x92F3, 0x99D0, 0x6A17, 0x7026, 0x732A, 0x82E7, 0x8457, 0x8CAF, 0x4E01, 0x5146, 0x51CB, 0x558B, 0x5BF5, 0x5E16, - 0x5E33, 0x5E81, 0x5F14, 0x5F35, 0x5F6B, 0x5FB4, 0x61F2, 0x6311, 0x66A2, 0x671D, 0x6F6E, 0x7252, 0x753A, 0x773A, 0x8074, 0x8139, - 0x8178, 0x8776, 0x8ABF, 0x8ADC, 0x8D85, 0x8DF3, 0x929A, 0x9577, 0x9802, 0x9CE5, 0x52C5, 0x6357, 0x76F4, 0x6715, 0x6C88, 0x73CD, + 0x5E33, 0x5E81, 0x5F14, 0x5F35, 0x5F6B, 0x5FB4, 0x61F2, 0x6311, 0x66A2, 0x671D, 0x6F6E, 0x7252, 0x753A, 0x773A, 0x8074, +#ifdef PF_FA_CP932_TABLE + 0x0000 +#else + 0x8139 +#endif +, + +#ifdef PF_FA_CP932_TABLE + 0x0000 +#else + 0x8178 +#endif +, 0x8776, 0x8ABF, 0x8ADC, 0x8D85, 0x8DF3, 0x929A, 0x9577, 0x9802, 0x9CE5, 0x52C5, 0x6357, 0x76F4, 0x6715, 0x6C88, 0x73CD, 0x8CC3, 0x93AE, 0x9673, 0x6D25, 0x589C, 0x690E, 0x69CC, 0x8FFD, 0x939A, 0x75DB, 0x901A, 0x585A, 0x6802, 0x63B4, 0x69FB, 0x4F43, 0x6F2C, 0x67D8, 0x8FBB, 0x8526, 0x7DB4, 0x9354, 0x693F, 0x6F70, 0x576A, 0x58F7, 0x5B2C, 0x7D2C, 0x722A, 0x540A, 0x91E3, 0x9DB4, 0x4EAD, 0x4F4E, 0x505C, 0x5075, 0x5243, 0x8C9E, 0x5448, 0x5824, 0x5B9A, 0x5E1D, 0x5E95, 0x5EAD, 0x5EF7, 0x5F1F, 0x608C, 0x62B5, @@ -500,7 +524,19 @@ static const pf_u16 cp932_to_unicode[45][189] = { }, { - 0x968B, 0x8146, 0x813E, 0x8153, 0x8151, 0x80FC, 0x8171, 0x816E, 0x8165, 0x8166, 0x8174, 0x8183, 0x8188, 0x818A, 0x8180, 0x8182, + 0x968B, 0x8146, 0x813E, 0x8153, 0x8151, 0x80FC, 0x8171, 0x816E, 0x8165, +#ifdef PF_FA_CP932_TABLE + 0x0000 +#else + 0x8166 +#endif +, +#ifdef PF_FA_CP932_TABLE + 0x0000 +#else + 0x8174 +#endif +, 0x8183, 0x8188, 0x818A, 0x8180, 0x8182, 0x81A0, 0x8195, 0x81A4, 0x81A3, 0x815F, 0x8193, 0x81A9, 0x81B0, 0x81B5, 0x81BE, 0x81B8, 0x81BD, 0x81C0, 0x81C2, 0x81BA, 0x81C9, 0x81CD, 0x81D1, 0x81D9, 0x81D8, 0x81C8, 0x81DA, 0x81DF, 0x81E0, 0x81E7, 0x81FA, 0x81FB, 0x81FE, 0x8201, 0x8202, 0x8205, 0x8207, 0x820A, 0x820D, 0x8210, 0x8216, 0x8229, 0x822B, 0x8238, 0x8233, 0x8240, 0x8259, 0x8258, 0x825D, 0x825A, 0x825F, 0x8264, 0x0000, diff --git a/tools/decomp-assist/fa_batch_attempts.md b/tools/decomp-assist/fa_batch_attempts.md new file mode 100644 index 00000000..7e71376c --- /dev/null +++ b/tools/decomp-assist/fa_batch_attempts.md @@ -0,0 +1,15 @@ +# FA batch remaining instruction differences + +- PFSYS_TimeStamp: vf date structure and u16 outputs were replaced with the actual OS calendar and u32 outputs; first 29 instructions became identical. Explicit OSTime temporary plus explicit return produced the same 31 instructions. Restored direct expression. Target symbol ends after mtlr and excludes two actual epilogue instructions; symbol boundaries were preserved. +- PFFAT12_WriteFATEntryWithBuf: replaced vf page range logic with a single buffered sector and real mirrored writes (175/175, 19 differences). Signed cluster/offset arithmetic and counter declaration order reduced this to eight register and addition operand differences. A u16 sector offset introduced two truncation instructions (177/175); restored u32. Pointer-first versus offset-first indexing did not change addition operand selection. +- PFFAT12_ReadFATEntryWithBuf: replaced vf multi-sector cache range macro with single sector flush/read (155/155, 34 register differences). Initialized error in its declaration moved one instruction (35 differences); restored assignment at loop entry. Reordered error/current FAT/sector/offset declarations preserved 155 instructions with 34 register differences. +- PFFAT16_WriteFATEntryWithBuf: single buffered sector implementation produced 66/66 instructions with seven differences, two swapped registers and one addition operand order. Moving sector/offset declarations did not change allocation. Moving offset first and using offset-first pointer indexing did not change allocation. u16 offset and u16 sector types also preserved the same seven differences. + +pdm_disk_convert_block_into_sector: the target name contains a trailing backtick, which is not a C identifier. The ordinary C spelling has 44 instructions identical to the target. Tried the VF conversion expression, explicit 16-bit ratio extraction, and a 32-bit byte-size argument; instruction bytes remain identical. No symbol metadata or assembly alias was added. +pdm_part_is_master_boot_sector: indexed buffer offsets produced 90/84 instructions; advancing the buffer produced 86/84; high-to-low byte addition and paired addition forms both retained 86/84 with an extra saved register. Kept paired little-endian additions. +pdm_part_get_start_sector: separate start/count assignments produced 276/276 with 151 differences; interleaved assignments produced 146 differences; a decode loop reduced the function to 243/276 and was reverted. Remaining scheduling and register allocation differ. +pdm_part_chg_ltop: initial ratio/start temporaries produced 51/51 with 16 register differences; reversed declarations and loading start before ratio both retained those differences. The compiler exchanges r0 and r3 throughout the conversion. + +fa_nanddrv_ParseCreateNANDFile: direct error conjunctions and early path initialization produced 132/130 instructions; moving path initialization below the format check and switching on NANDCreate errors produced 134/130; success branches around file creation reduced this to 131/130; nesting the directory success path produced 130/130 with 10 branch/scheduling differences. The remaining block places the directory-error exit differently. +fa_nanddrv_NotifyNANDFile: indexed lookup produced 72/72 with 15 register differences; advancing a record pointer produced 74/72 and was reverted; testing the indexed disk field after strcmp retained the offset across the call and reduced differences to 12; reversing pointer/index declarations left those 12 differences. Saved arguments, record index, and record offset occupy different registers. +fa_nanddrv_physical_write: the shared lookup and error helpers produced 184/175 instructions; direct indexed lookup produced 183/175; a void error-store helper eliminated unused helper-result comparisons, giving 175/175 with 38 register differences; reordering remaining-size, file-size, and seek-offset declarations gave 42 differences and was reverted; moving the error declaration also left 42 differences. The retained version has 38 saved-register differences. -- 2.49.1