diff --git a/README.md b/README.md index 7fca1c39..99fd18b5 100644 --- a/README.md +++ b/README.md @@ -53,9 +53,9 @@ Progress | Decompiled | Matched | Linked | Data | |:---:|:---:|:---:|:---:| -| 88.75% | 78.13% | 56.92% | 88.24% | +| 88.96% | 79.07% | 56.92% | 88.24% | -units 752/1027 complete, functions 11441/12563 matched. +units 752/1027 complete, functions 11492/12563 matched. decompiled = code with a C/C++ implementation (objdiff fuzzy), matched = byte-exact code, linked = code actually linked into the DOL, data = byte-exact data diff --git a/libs/RVL_SDK/src/fa/pf_dir.c b/libs/RVL_SDK/src/fa/pf_dir.c index 36003880..6f1017a7 100644 --- a/libs/RVL_SDK/src/fa/pf_dir.c +++ b/libs/RVL_SDK/src/fa/pf_dir.c @@ -102,6 +102,8 @@ typedef struct PFDIR_VOLUME_DIRS { typedef struct PFDIR_GLOBAL_STATE { pf_u8 state_prefix[0x40]; pf_s32 last_error; + pf_u8 driver_and_code_state[0x1C]; + pf_u32 setting; } PFDIR_GLOBAL_STATE; extern PFDIR_GLOBAL_STATE pf_vol_set; @@ -134,6 +136,26 @@ extern pf_s32 PFVOL_CheckCurrentDir(PF_VOLUME* volume, pf_u32 start_cluster); extern pf_s32 PFENT_RemoveEntry(PF_DIR_ENT* entry, PF_ENT_ITER* iter); extern pf_s32 PFFAT_FreeChain(PFDIR_FFD* ffd, pf_u32 start_cluster, pf_u32 chain_index, pf_u32 size); +static inline pf_u32 directory_is_open(PF_DIR_ENT* entry) { + PFDIR_VOLUME_DIRS* volume = (PFDIR_VOLUME_DIRS*)entry->p_vol; + pf_s32 index; + PFDIR_SDD* sdd = volume->sdds; + for (index = 0; index < 3; index++, sdd++) { + if ((sdd->stat & 1) != 0 && (sdd->stat & 2) != 0 && + entry->p_vol == sdd->dir_entry.p_vol && + entry->entry_sector == sdd->dir_entry.entry_sector && + entry->entry_offset == sdd->dir_entry.entry_offset) { return 1; } + } + return 0; +} + +extern void PFFAT_ResetFFD(PFDIR_FFD*, pf_u32*); +static inline pf_s32 release_directory_chain(PFDIR_FFD* ffd, pf_u32 cluster, pf_u32 count) { + pf_s32 error = PFFAT_FreeChain(ffd, cluster, -1U, count); + if (error != 0) { return error; } + return 0; +} + pf_s32 PFDIR_CheckDirIsEmpty(PF_ENT_ITER* iter, pf_u32* is_empty) { pf_u32 start_cluster = *iter->ffd.p_start_cluster; pf_s32 error; @@ -177,8 +199,8 @@ PFDIR_SDD* PFDIR_GetSDD(PF_VOLUME* volume, PF_DIR_ENT* entry) { pf_s32 i; pf_s32 remaining = 3; - for (i = 0; remaining != 0; i++, free_candidate++, remaining--) { - if ((volume_dirs->sdds[i].stat & 1) == 0 || (volume_dirs->sdds[i].stat & 2) == 0) { + for (i = 0; remaining != 0; remaining--) { + if ((volume_dirs->sdds[i].stat & 1) == 0 || ((volume_dirs->sdds[i].stat & 1) != 0 && (!volume_dirs->sdds[i].stat & 2) != 0)) { if (first_free_sdd == 0) { first_free_sdd = free_candidate; } @@ -187,6 +209,8 @@ PFDIR_SDD* PFDIR_GetSDD(PF_VOLUME* volume, PF_DIR_ENT* entry) { entry->entry_offset == volume_dirs->sdds[i].dir_entry.entry_offset) { return &volume_dirs->sdds[i]; } + free_candidate++; + i++; } if (first_free_sdd == 0) { return 0; @@ -198,12 +222,21 @@ PFDIR_SDD* PFDIR_GetSDD(PF_VOLUME* volume, PF_DIR_ENT* entry) { return first_free_sdd; } +static inline PFDIR_DIR* unused_directory(PF_VOLUME* volume) { + pf_s32 index; + for (index = 0; index < 3; index++) { + if ((((PFDIR_VOLUME_DIRS*)volume)->udds[index].stat & 1) == 0) { + return &((PFDIR_VOLUME_DIRS*)volume)->udds[index]; + } + } + return 0; +} + pf_s32 PFDIR_DoFsexecOpenDir(PF_DIR_ENT* entry, PF_ENT_ITER* iter, PFDIR_DIR** opened_dir) { PFDIR_VOLUME_DIRS* volume_dirs = (PFDIR_VOLUME_DIRS*)entry->p_vol; + pf_s32 i; PFDIR_SDD* sdd; PFDIR_DIR* dir; - PFDIR_DIR* scan_dir; - pf_s32 i; *opened_dir = 0; if ((entry->attr & 0x10) == 0) { @@ -213,15 +246,7 @@ pf_s32 PFDIR_DoFsexecOpenDir(PF_DIR_ENT* entry, PF_ENT_ITER* iter, PFDIR_DIR** o if (sdd == 0) { return 0x15; } - scan_dir = volume_dirs->udds; - for (i = 0; i < 3; i++, scan_dir++) { - if ((scan_dir->stat & 1) == 0) { - dir = &volume_dirs->udds[i]; - goto directory_available; - } - } - dir = 0; -directory_available: + dir = unused_directory((PF_VOLUME*)volume_dirs); if (dir == 0) { return 0x16; } @@ -239,9 +264,9 @@ directory_available: pf_s32 PFDIR_p_opendir(PF_VOLUME* volume, PFDIR_STR* path, PFDIR_DIR** opened_dir) { PF_ENT_ITER iter; PF_DIR_ENT entry; + pf_s32 i; PFDIR_SDD* sdd; PFDIR_DIR* dir; - pf_s32 i; pf_s32 error; if ((PFSTR_StrNCmp(path, (const pf_s8*)"\\", 1, 0, 1) == 0 || @@ -255,9 +280,6 @@ pf_s32 PFDIR_p_opendir(PF_VOLUME* volume, PFDIR_STR* path, PFDIR_DIR** opened_di } else if (PFSTR_StrNumChar(path, 1) == 2 && PFSTR_StrNCmp(path, (const pf_s8*)":", 1, 1, 1) == 0) { error = PFVOL_GetCurrentDir(volume, &entry); - if (error != 0) { - return error; - } goto open_directory; } else if (PFSTR_StrNumChar(path, 1) == 3 && PFSTR_StrNCmp(path, (const pf_s8*)":", 1, 1, 1) == 0 && @@ -288,14 +310,7 @@ open_directory: if (sdd == 0) { return 0x15; } - for (i = 0; i < 3; i++) { - if ((((PFDIR_VOLUME_DIRS*)volume)->udds[i].stat & 1) == 0) { - dir = &((PFDIR_VOLUME_DIRS*)volume)->udds[i]; - goto directory_available; - } - } - dir = 0; -directory_available: + dir = unused_directory(volume); if (dir == 0) { return 0x16; } @@ -349,17 +364,23 @@ pf_s32 PFDIR_p_readdir(PFDIR_DIR* dir, PFDIR_READ_RESULT* result) { return 0; } +extern pf_s32 PFENT_findEntry(PFDIR_FFD*, PF_DIR_ENT*, pf_u32, PFDIR_STR*, pf_u8, pf_u32*, pf_u32*); +extern pf_s32 PFFAT_GetSectorSpecified(PFDIR_FFD*, pf_u32, pf_u32, pf_u32*); +extern pf_s32 PFPATH_transformInUnicode(pf_u16*, const pf_s8*); +extern void PFPATH_SetSearchPattern(pf_s8*, pf_u16*, PFDIR_STR*); +extern pf_u16* pf_w_strcpy(pf_u16*, const pf_u16*); + pf_s32 PFDIR_p_mkdir(PF_VOLUME* p_vol, PF_STR* p_path, pf_u32 option, PF_DTA* p_dta) { pf_s32 err; - PF_STR full_path; - PF_STR dirname; + PFDIR_STR full_path; + PFDIR_STR dirname; pf_u16 dirname_len; + PF_ENT_ITER parent_iter; PF_DIR_ENT entry; PF_DIR_ENT parent_entry; - PF_ENT_ITER parent_iter; - PF_FFD ffd; + PFDIR_FFD ffd; + PFDIR_FAT_HINT fat_hint; pf_u32 pos; - PF_FAT_HINT fat_hint; pf_u32 prev_chain[2]; pf_u32 new_sector; @@ -368,7 +389,7 @@ pf_s32 PFDIR_p_mkdir(PF_VOLUME* p_vol, PF_STR* p_path, pf_u32 option, PF_DTA* p_ err = 0; new_sector = 0; - err = VFiPFPATH_SplitPath(p_path, &full_path, &dirname); + err = PFPATH_SplitPath((PFDIR_STR*)p_path, &full_path, &dirname); if (err != 0) { return err; } @@ -383,40 +404,39 @@ pf_s32 PFDIR_p_mkdir(PF_VOLUME* p_vol, PF_STR* p_path, pf_u32 option, PF_DTA* p_ if (p_dta != PF_NULL) { p_dta->parent_start_cluster = parent_entry.start_cluster; } - VFiPFFAT_InitFFD(&ffd, &fat_hint, p_vol, &parent_entry.start_cluster); - err = VFiPFENT_findEntry(&ffd, &entry, 0U, &dirname, 0x77U, PF_NULL, PF_NULL); + PFFAT_InitFFD(&ffd, &fat_hint, p_vol, &parent_entry.start_cluster); + err = PFENT_findEntry(&ffd, &entry, 0U, &dirname, 0x77U, PF_NULL, PF_NULL); if (err == 0) { return 8; } if (err != 3) { return err; } - dirname_len = VFiPFSTR_StrNumChar(&dirname, 1U); - if ((dirname_len > 0xFFU) || ((dirname_len + parent_entry.path_len) > 0x103U)) { - return 2; - } - err = VFiPFENT_InitENT(&entry, &dirname, 0x10U, 1U, &parent_entry, p_vol); + dirname_len = PFSTR_StrNumChar(&dirname, 1U); + if (dirname_len + parent_entry.path_len > 0x103U) { return 2; } + if (dirname_len > 0xFFU) { return 1; } + err = VFiPFENT_InitENT(&entry, (PF_STR*)&dirname, 0x10U, 1U, &parent_entry, p_vol); if (err == 0) { - VFiPFFAT_InitFFD(&ffd, &fat_hint, p_vol, &parent_entry.start_cluster); - err = VFiPFENT_allocateEntry(&entry, (entry.num_entry_LFNs + 1), &ffd, prev_chain, PF_NULL, 0x77U, &pos); + PFFAT_InitFFD(&ffd, &fat_hint, p_vol, &parent_entry.start_cluster); + err = VFiPFENT_allocateEntry(&entry, (entry.num_entry_LFNs + 1), (PF_FFD*)&ffd, prev_chain, PF_NULL, 0x77U, &pos); if (err == 0) { entry.start_cluster = 0; - VFiPFFAT_InitFFD(&ffd, &fat_hint, p_vol, &entry.start_cluster); - err = VFiPFFAT_GetSectorSpecified(&ffd, 0U, 1U, &new_sector); + PFFAT_InitFFD(&ffd, &fat_hint, p_vol, &entry.start_cluster); + err = PFFAT_GetSectorSpecified(&ffd, 0U, 1U, &new_sector); if (new_sector == -1U) { err = 6; } if (err == 0) { err = VFiPFENT_FillVoidEntryToSectors(p_vol, new_sector, p_vol->bpb.sectors_per_cluster, 1U, &entry, &parent_entry); if (err == 0) { - if ((entry.long_name[0] != 0) && ((VFipf_vol_set.setting & 2) == 2)) { + if ((entry.long_name[0] != 0) && ((pf_vol_set.setting & 2) == 2)) { VFiPFPATH_AdjustExtShortName(&entry.short_name[0], pos); entry.check_sum = VFiPFENT_CalcCheckSum(&entry); } err = VFiPFENT_UpdateEntry(&entry, prev_chain, 0U); } if (err != 0) { - VFiPFFAT_FreeChain(&ffd, entry.start_cluster, -1U, -1U); + PFFAT_FreeChain(&ffd, entry.start_cluster, -1U, -1U); } else if (p_dta != PF_NULL) { p_dta->p_file = PF_NULL; p_dta->p_dir = PF_NULL; @@ -428,16 +448,16 @@ pf_s32 PFDIR_p_mkdir(PF_VOLUME* p_vol, PF_STR* p_path, pf_u32 option, PF_DTA* p_ p_dta->Date = entry.modify_date; p_dta->FileSize = entry.file_size; p_dta->Attribute = entry.attr; - if (VFiPFSTR_GetCodeMode(&dirname) == 1) { - VFiPFPATH_SetSearchPattern(p_dta->reg_exp, PF_NULL, &dirname); + if (VFiPFSTR_GetCodeMode((PF_STR*)&dirname) == 1) { + PFPATH_SetSearchPattern(p_dta->reg_exp, PF_NULL, &dirname); p_dta->status |= 1; } else { - VFiPFPATH_SetSearchPattern(p_dta->reg_exp, ((PF_DTAW*)p_dta)->reg_expW, &dirname); + PFPATH_SetSearchPattern(p_dta->reg_exp, ((PF_DTAW*)p_dta)->reg_expW, &dirname); p_dta->status |= 2; } - VFipf_strcpy(p_dta->FileName, &entry.short_name[0]); + pf_strcpy(p_dta->FileName, &entry.short_name[0]); if ((p_dta->status & 2) == 2) { - VFiPFPATH_transformInUnicode(((PF_DTAW*)p_dta)->FileNameW, p_dta->FileName); + PFPATH_transformInUnicode(((PF_DTAW*)p_dta)->FileNameW, p_dta->FileName); } if (entry.long_name[0] == 0) { p_dta->ordinal = 0; @@ -449,17 +469,17 @@ pf_s32 PFDIR_p_mkdir(PF_VOLUME* p_vol, PF_STR* p_path, pf_u32 option, PF_DTA* p_ } else { p_dta->ordinal = 1; p_dta->check_sum = entry.check_sum; - if ((VFipf_vol_set.setting & 2) == 2) { - VFipf_vol_set.setting &= 0xFFFFFFFC; - VFipf_vol_set.setting |= 1; - VFiPFPATH_transformFromUnicodeToNormal(p_dta->LongName, entry.long_name); - VFipf_vol_set.setting &= 0xFFFFFFFC; - VFipf_vol_set.setting |= 2; + if ((pf_vol_set.setting & 2) == 2) { + pf_vol_set.setting &= 0xFFFFFFFC; + pf_vol_set.setting |= 1; + PFPATH_transformFromUnicodeToNormal(p_dta->LongName, entry.long_name); + pf_vol_set.setting &= 0xFFFFFFFC; + pf_vol_set.setting |= 2; } else { - VFiPFPATH_transformFromUnicodeToNormal(p_dta->LongName, entry.long_name); + PFPATH_transformFromUnicodeToNormal(p_dta->LongName, entry.long_name); } if ((p_dta->status & 2) == 2) { - VFipf_w_strcpy(((PF_DTAW*)p_dta)->LongNameW, entry.long_name); + pf_w_strcpy(((PF_DTAW*)p_dta)->LongNameW, entry.long_name); } } } @@ -524,7 +544,7 @@ pf_s32 PFDIR_p_chdir(PF_VOLUME* p_vol, PFDIR_STR* p_path_str) { return 0; } -pf_s32 PFDIR_p_chmod(PF_VOLUME* volume, PFDIR_STR* path, pf_u8 attributes) { +pf_s32 PFDIR_p_chmod(PF_VOLUME* volume, PFDIR_STR* path, pf_u32 attributes) { PFDIR_STR directory_path; PFDIR_STR filename; PF_ENT_ITER iter; @@ -544,52 +564,50 @@ pf_s32 PFDIR_p_chmod(PF_VOLUME* volume, PFDIR_STR* path, pf_u8 attributes) { if (error != 0) { return error; } - if (((entry.attr & 0x10) == 0 && (attributes & 0x10) != 0) || - ((entry.attr & 0x10) != 0 && (attributes & 0x10) == 0)) { + if (((entry.attr & 0x10) != 0 && (attributes & 0x10) == 0) || + ((entry.attr & 0x10) == 0 && (attributes & 0x10) != 0)) { return 10; } if ((attributes & 0x40) != 0) { - attributes &= (pf_u8)~0x40; + attributes = (pf_u8)(attributes & ~0x40); } - if ((entry.attr & 0x10) == 0) { - if (PFFILE_IsOpened(&entry) != 0) { - return 0x13; - } - } else { + if ((entry.attr & 0x10) != 0) { volume_dirs = (PFDIR_VOLUME_DIRS*)volume; - for (i = 0; i < 3; i++) { - PFDIR_SDD* sdd = &volume_dirs->sdds[i]; - if ((sdd->stat & 1) != 0 && (sdd->stat & 2) != 0 && - entry.p_vol == sdd->dir_entry.p_vol && - entry.entry_sector == sdd->dir_entry.entry_sector && - entry.entry_offset == sdd->dir_entry.entry_offset) { - return 0x13; - } - } - if ((attributes & 4) != 0 && (attributes & 0xF) != 0xF) { + if (directory_is_open(&entry) != 0) { return 0x13; } + + if ((attributes & 4) != 0 && (pf_s32)(attributes & 0xF) != 0xF) { return 10; } attributes |= 0x10; + entry.attr = attributes; + } else { + if (PFFILE_IsOpened(&entry) != 0) { + return 0x13; + } + entry.attr = attributes; } - create_time_ms = PFENT_getcurrentDateTimeForEnt(&date, &time); - entry.modify_date = date; - entry.modify_time = time; - entry.create_time_ms = create_time_ms; - entry.attr = attributes; - return PFENT_updateEntry(&entry, 0); + PFENT_getcurrentDateTimeForEnt(&entry.access_date, &time); + if (entry.file_size == 0 && (entry.attr & 0x10) == 0) { entry.start_cluster = 0; } + error = PFENT_updateEntry(&entry, 0); + if (error != 0) { return error; } + return 0; } pf_s32 PFDIR_p_rmdir(PF_VOLUME* volume, PFDIR_STR* path) { - PFDIR_VOLUME_DIRS* volume_dirs; + PFDIR_STR directory_path; + PFDIR_STR filename; + PFDIR_FAT_HINT saved_hint; PFDIR_FAT_HINT hint; PFDIR_FFD ffd; PF_ENT_ITER iter; PF_ENT_ITER directory_iter; PF_DIR_ENT entry; - pf_u32 is_empty; + pf_u32 is_empty = 0; pf_u32 i; pf_s32 error; + error = PFPATH_SplitPath(path, &directory_path, &filename); + if (error != 0) { return error; } error = PFENT_ITER_GetEntryOfPath(&iter, &entry, volume, path, 0); if (error != 0) { return error; @@ -597,30 +615,19 @@ pf_s32 PFDIR_p_rmdir(PF_VOLUME* volume, PFDIR_STR* path) { if ((entry.attr & 0x10) == 0 || (entry.attr & 1) != 0) { return 0x14; } - if (entry.start_cluster == 1 || - (volume->bpb.fat_type == FAT_32 && entry.start_cluster == volume->bpb.root_dir_cluster)) { - return 0x1C; - } - volume_dirs = (PFDIR_VOLUME_DIRS*)volume; - for (i = 0; i < 3; i++) { - PFDIR_SDD* sdd = &volume_dirs->sdds[i]; - if ((sdd->stat & 3) == 3 && entry.p_vol == sdd->dir_entry.p_vol && - entry.entry_sector == sdd->dir_entry.entry_sector && - entry.entry_offset == sdd->dir_entry.entry_offset) { - return 0x13; - } - } + directory_iter = iter; + saved_hint = *iter.ffd.p_hint; + if (directory_is_open(&entry) != 0) { return 0x13; } + error = PFVOL_CheckCurrentDir(volume, entry.start_cluster); - if (error != 0) { - return 0x1C; - } + if (error != 0 || entry.start_cluster == 1 || + (volume->bpb.fat_type == FAT_32 && entry.start_cluster == volume->bpb.root_dir_cluster)) { return 0x1C; } PFFAT_InitFFD(&ffd, &hint, volume, &entry.start_cluster); directory_iter.ffd = ffd; error = PFENT_ITER_IteratorInitialize(&directory_iter, 0); if (error != 0) { return error; } - is_empty = 0; error = PFDIR_CheckDirIsEmpty(&directory_iter, &is_empty); if (error != 0) { return error; @@ -628,18 +635,20 @@ pf_s32 PFDIR_p_rmdir(PF_VOLUME* volume, PFDIR_STR* path) { if (is_empty == 0) { return 0x1D; } + iter = directory_iter; + iter.ffd.p_hint = &saved_hint; error = PFENT_RemoveEntry(&entry, &iter); if (error != 0) { return error; } - return PFFAT_FreeChain(&iter.ffd, entry.start_cluster, -1U, -1U); + return release_directory_chain(&iter.ffd, entry.start_cluster, -1U); } pf_s32 PFDIR_p_fstat(PF_VOLUME* volume, PFDIR_STR* path, PFDIR_FILE_STAT* file_stat) { PF_ENT_ITER iter; PF_DIR_ENT entry; PFDIR_VOLUME_DIRS* volume_dirs = (PFDIR_VOLUME_DIRS*)volume; - pf_u32 index; + pf_s32 index = 0; pf_s32 error; error = PFENT_ITER_GetEntryOfPath(&iter, &entry, volume, path, 0); @@ -648,32 +657,32 @@ pf_s32 PFDIR_p_fstat(PF_VOLUME* volume, PFDIR_STR* path, PFDIR_FILE_STAT* file_s } if (volume_dirs->num_opened_files != 0) { for (index = 0; index < 5; index++) { - PFDIR_SFD* sfd = &volume_dirs->sfds[index]; - if ((sfd->stat & 1) != 0 && (sfd->stat & 2) != 0 && - entry.p_vol == sfd->dir_entry.p_vol && - entry.entry_sector == sfd->dir_entry.entry_sector && - entry.entry_offset == sfd->dir_entry.entry_offset) { - file_stat->file_size = sfd->dir_entry.file_size; - file_stat->access_date = sfd->dir_entry.access_date; - file_stat->modify_time = sfd->dir_entry.modify_time; - file_stat->modify_date = sfd->dir_entry.modify_date; - file_stat->create_time = sfd->dir_entry.create_time; - file_stat->create_date = sfd->dir_entry.create_date; - file_stat->small_letter_flag = sfd->dir_entry.small_letter_flag; - file_stat->attributes = sfd->dir_entry.attr; + if ((volume_dirs->sfds[index].stat & 1) != 0 && (volume_dirs->sfds[index].stat & 2) != 0 && + entry.p_vol == volume_dirs->sfds[index].dir_entry.p_vol && + entry.entry_sector == volume_dirs->sfds[index].dir_entry.entry_sector && + entry.entry_offset == volume_dirs->sfds[index].dir_entry.entry_offset) { + file_stat->file_size = volume_dirs->sfds[index].dir_entry.file_size; + file_stat->attributes = volume_dirs->sfds[index].dir_entry.attr; + file_stat->modify_time = volume_dirs->sfds[index].dir_entry.modify_time; + file_stat->modify_date = volume_dirs->sfds[index].dir_entry.modify_date; + file_stat->access_date = volume_dirs->sfds[index].dir_entry.access_date; + file_stat->create_time = volume_dirs->sfds[index].dir_entry.create_time; + file_stat->create_date = volume_dirs->sfds[index].dir_entry.create_date; + file_stat->small_letter_flag = volume_dirs->sfds[index].dir_entry.create_time_ms; break; } } } if (volume_dirs->num_opened_files == 0 || index == 5) { - file_stat->file_size = (entry.attr & 0x10) == 0 ? entry.file_size : 0; - file_stat->access_date = entry.access_date; + if ((entry.attr & 0x10) != 0) { file_stat->file_size = 0; } + else { file_stat->file_size = entry.file_size; } + file_stat->attributes = entry.attr; file_stat->modify_time = entry.modify_time; file_stat->modify_date = entry.modify_date; + file_stat->access_date = entry.access_date; file_stat->create_time = entry.create_time; file_stat->create_date = entry.create_date; - file_stat->small_letter_flag = entry.small_letter_flag; - file_stat->attributes = entry.attr; + file_stat->small_letter_flag = entry.create_time_ms; } return 0; } @@ -721,14 +730,18 @@ pf_s32 PFDIR_p_rename(PF_VOLUME* volume, PF_STR* old_path, PF_STR* new_path) { PFDIR_STR destination_directory; PFDIR_STR destination_name; pf_s8 normalized_name[512]; + pf_u32 code_mode; const pf_s8* name; pf_u32 entry_position; + pf_u16 filename_length; pf_s32 error; error = PFPATH_SplitPath((PFDIR_STR*)old_path, &source_directory, &source_name); if (error != 0) { return error; } + filename_length = PFSTR_StrNumChar(&source_name, 1); + if (filename_length > 0xFF) { return 1; } if (PFSTR_GetCodeMode(&source_name) == 2) { if (PFSTR_StrNumChar(&source_name, 1) > 0xFF) { return 1; @@ -755,7 +768,7 @@ pf_s32 PFDIR_p_rename(PF_VOLUME* volume, PF_STR* old_path, PF_STR* new_path) { if (error != 0) { return error; } - if (entry_position == 0) { + if (entry_position == 999999) { return 3; } if ((source_entry.attr & 0x10) != 0) { @@ -784,7 +797,7 @@ pf_s32 PFDIR_p_rename(PF_VOLUME* volume, PF_STR* old_path, PF_STR* new_path) { if (source_iter.ffd.start_cluster != destination_parent.start_cluster) { return 0x1F; } - if (PFSTR_GetCodeMode(&destination_name) == 2) { + if ((code_mode = PFSTR_GetCodeMode(&destination_name)) == 2) { if (PFSTR_StrNumChar(&destination_name, 1) > 0xFF) { return 1; } @@ -822,8 +835,10 @@ pf_s32 PFDIR_p_move(PF_VOLUME* volume, PF_STR* old_path, PF_STR* new_path) { PFDIR_STR destination_name; PFDIR_VOLUME_DIRS* volume_dirs; pf_s8 normalized_name[512]; + pf_u32 code_mode; pf_u32 entry_position; - pf_u32 index; + pf_u16 filename_length; + pf_s32 index = 0; pf_s32 error; error = PFENT_ITER_GetEntryOfPath(&source_iter, &source_entry, volume, @@ -839,7 +854,9 @@ pf_s32 PFDIR_p_move(PF_VOLUME* volume, PF_STR* old_path, PF_STR* new_path) { if (error != 0) { return error; } - if (PFSTR_GetCodeMode(&destination_name) == 2) { + filename_length = PFSTR_StrNumChar(&destination_name, 1); + if (filename_length > 0xFF) { return 1; } + if ((code_mode = PFSTR_GetCodeMode(&destination_name)) == 2) { if (PFSTR_StrNumChar(&destination_name, 1) > 0xFF) { return 1; } @@ -869,15 +886,7 @@ pf_s32 PFDIR_p_move(PF_VOLUME* volume, PF_STR* old_path, PF_STR* new_path) { source_entry.start_cluster == volume->bpb.root_dir_cluster)) { return 0x1C; } - for (index = 0; index < 3; index++) { - PFDIR_SDD* sdd = &volume_dirs->sdds[index]; - if ((sdd->stat & 1) != 0 && (sdd->stat & 2) != 0 && - sdd->dir_entry.p_vol == source_entry.p_vol && - sdd->dir_entry.entry_sector == source_entry.entry_sector && - sdd->dir_entry.entry_offset == source_entry.entry_offset) { - return 0x13; - } - } + if (directory_is_open(&source_entry) != 0) { return 0x13; } } else if (PFFILE_IsOpened(&source_entry) != 0) { return 0x13; } @@ -908,9 +917,10 @@ pf_s32 PFDIR_p_fsexec_remove(PF_DTA* data, PF_DIR_ENT* entry, PF_ENT_ITER* iter) PFDIR_VOLUME_DIRS* volume_dirs = (PFDIR_VOLUME_DIRS*)entry->p_vol; PFDIR_FAT_HINT hint; PF_ENT_ITER directory_iter; - pf_u32 is_empty; - pf_u32 index; - pf_u32 start_cluster = entry->start_cluster; + pf_u32 is_empty = 0; + pf_s32 index = 0; + pf_u32 start_cluster; + pf_u32 chain_size; pf_s32 error; if ((entry->attr & 1) != 0) { @@ -922,32 +932,30 @@ pf_s32 PFDIR_p_fsexec_remove(PF_DTA* data, PF_DIR_ENT* entry, PF_ENT_ITER* iter) entry->start_cluster == entry->p_vol->bpb.root_dir_cluster)) { return 0x1C; } - for (index = 0; index < 3; index++) { - PFDIR_SDD* sdd = &volume_dirs->sdds[index]; - if ((sdd->stat & 1) != 0 && (sdd->stat & 2) != 0 && - sdd->dir_entry.p_vol == entry->p_vol && - sdd->dir_entry.entry_sector == entry->entry_sector && - sdd->dir_entry.entry_offset == entry->entry_offset) { - return 0x13; - } - } + if (directory_is_open(entry) != 0) { return 0x13; } + directory_iter = *iter; - PFFAT_InitFFD(&directory_iter.ffd, &hint, entry->p_vol, &entry->start_cluster); - error = PFENT_ITER_IteratorInitialize(&directory_iter, 0); + hint = *iter->ffd.p_hint; + PFFAT_ResetFFD(&iter->ffd, &entry->start_cluster); + error = PFENT_ITER_IteratorInitialize(iter, 0); if (error != 0) { return error; } - is_empty = 0; - error = PFDIR_CheckDirIsEmpty(&directory_iter, &is_empty); + error = PFDIR_CheckDirIsEmpty(iter, &is_empty); if (error != 0) { return error; } if (is_empty == 0) { return 0x1D; } - } else if (PFFILE_IsOpened(entry) != 0) { - return 0x13; + *iter = directory_iter; + iter->ffd.p_hint = &hint; + chain_size = -1U; + } else { + if (PFFILE_IsOpened(entry) != 0) { return 0x13; } + chain_size = entry->file_size; } + start_cluster = entry->start_cluster; entry->num_entry_LFNs = data->num_entry_LFNs; entry->ordinal = data->ordinal; entry->check_sum = data->check_sum; @@ -955,21 +963,29 @@ pf_s32 PFDIR_p_fsexec_remove(PF_DTA* data, PF_DIR_ENT* entry, PF_ENT_ITER* iter) if (error != 0) { return error; } - return PFFAT_FreeChain(&iter->ffd, start_cluster, -1U, -1U); + return release_directory_chain(&iter->ffd, start_cluster, chain_size); } -pf_s32 PFDIR_p_fsexec_chmod(PF_DTA* data, PF_DIR_ENT* entry, pf_u32 attributes, pf_u32 mode) { + +pf_s32 PFDIR_p_fsexec_chmod(PF_DTA* data, PF_DIR_ENT* entry, pf_u32 mode, pf_u32 attributes) { PFDIR_VOLUME_DIRS* volume_dirs = (PFDIR_VOLUME_DIRS*)entry->p_vol; pf_u16 date; pf_u16 time; pf_u8 create_time_ms; - pf_u32 index; + pf_s32 index = 0; pf_s32 error; if ((attributes & 0x40) != 0) { - attributes &= ~0x40; + attributes = (pf_u8)(attributes & ~0x40); } - if ((entry->attr & 0x10) == 0) { + if ((entry->attr & 0x10) != 0) { + if ((attributes & 4) != 0) { + return 10; + } + if (directory_is_open(entry) != 0) { return 0x13; } + + entry->attr = attributes | 0x10; + } else { if ((attributes & 0x10) != 0) { return 10; } @@ -977,25 +993,8 @@ pf_s32 PFDIR_p_fsexec_chmod(PF_DTA* data, PF_DIR_ENT* entry, pf_u32 attributes, return 0x13; } entry->attr = attributes; - } else { - if ((attributes & 4) != 0) { - return 10; } - for (index = 0; index < 3; index++) { - PFDIR_SDD* sdd = &volume_dirs->sdds[index]; - if ((sdd->stat & 1) != 0 && (sdd->stat & 2) != 0 && - sdd->dir_entry.p_vol == entry->p_vol && - sdd->dir_entry.entry_sector == entry->entry_sector && - sdd->dir_entry.entry_offset == entry->entry_offset) { - return 0x13; - } - } - entry->attr = attributes | 0x10; - } - create_time_ms = PFENT_getcurrentDateTimeForEnt(&date, &time); - entry->modify_date = date; - entry->modify_time = time; - entry->create_time_ms = create_time_ms; + PFENT_getcurrentDateTimeForEnt(&entry->access_date, &time); error = PFENT_updateEntry(entry, 0); if (error != 0) { return error; @@ -1003,7 +1002,9 @@ pf_s32 PFDIR_p_fsexec_chmod(PF_DTA* data, PF_DIR_ENT* entry, pf_u32 attributes, if ((mode & 0x80000000) == 0) { data->Attribute = entry->attr; } - return PFCACHE_FlushDataCacheSpecific(entry->p_vol, 0); + error = PFCACHE_FlushDataCacheSpecific(entry->p_vol, 0); + if (error != 0) { return error; } + return 0; } pf_s32 PFDIR_p_fsexec_fopen(PF_DTA* data, PF_DIR_ENT* entry, PF_ENT_ITER* iter, pf_u32 flags, @@ -1062,9 +1063,9 @@ pf_s32 PFDIR_p_fsexec_opendir(PF_DTA* data, PF_DIR_ENT* entry, PF_ENT_ITER* iter } pf_s32 PFDIR_p_fsexec(PF_DTA* data, pf_u32 flags, pf_u32 mode) { + PF_ENT_ITER iter; PFDIR_FFD ffd; PFDIR_FAT_HINT hint; - PF_ENT_ITER iter; PF_DIR_ENT entry; PFDIR_STR pattern; pf_u32 logical_position; @@ -1074,32 +1075,34 @@ pf_s32 PFDIR_p_fsexec(PF_DTA* data, pf_u32 flags, pf_u32 mode) { const pf_s8* name; pf_s32 error; - if ((flags & 0x26) == 0 && (flags & 1) == 0) { + if ((flags & 6) == 0 && (flags & 1) == 0 && (flags & 0x10) == 0 && (flags & 0x20) == 0) { return 10; } - if ((flags & 0x26) != 0 || (flags & 1) != 0 || ((flags & 0x10) != 0 && mode != 2)) { + if ((flags & 6) != 0 || (flags & 1) != 0 || ((flags & 0x10) != 0 && mode != 2)) { error = PFVOL_CheckForWrite(data->p_vol); if (error != 0) { return error; } } + entry.start_cluster = data->parent_start_cluster; + entry.check_sum = 0; + PFFAT_InitFFD(&ffd, &hint, data->p_vol, &entry.start_cluster); + iter.p_vol = data->p_vol; + iter.ffd = ffd; start_position = data->parent_pos - 1; if ((flags & 0x80000000) == 0) { start_position -= data->num_entry_LFNs; } - PFFAT_InitFFD(&ffd, &hint, data->p_vol, &data->parent_start_cluster); - iter.p_vol = data->p_vol; - iter.ffd = ffd; error = PFENT_ITER_IteratorInitialize(&iter, start_position); if (error != 0) { return error; } - if ((data->status & 1) != 0) { + if ((data->status & 1) == 1) { code_mode = 1; name = data->FileName; } else { code_mode = 2; - if ((VFipf_vol_set.setting & 2) != 0) { + if ((pf_vol_set.setting & 2) != 0) { name = (const pf_s8*)((PF_DTAW*)data)->LongNameW; } else { name = (const pf_s8*)((PF_DTAW*)data)->FileNameW; @@ -1109,7 +1112,7 @@ pf_s32 PFDIR_p_fsexec(PF_DTA* data, pf_u32 flags, pf_u32 mode) { if (error != 0) { return error; } - if ((VFipf_vol_set.setting & 2) != 0) { + if ((pf_vol_set.setting & 2) != 0) { PFSTR_SetLocalStr(&pattern, (pf_u32)data->LongName); } else { PFSTR_SetLocalStr(&pattern, (pf_u32)data->FileName); @@ -1137,7 +1140,7 @@ pf_s32 PFDIR_p_fsexec(PF_DTA* data, pf_u32 flags, pf_u32 mode) { return error; } if ((flags & 6) != 0) { - return PFDIR_p_fsexec_chmod(data, &entry, flags, mode); + return PFDIR_p_fsexec_chmod(data, &entry, mode, flags); } if ((flags & 1) != 0) { return PFDIR_p_fsexec_remove(data, &entry, &iter); @@ -1316,7 +1319,7 @@ void PFDIR_opendir(PF_STR* path, PFDIR_DIR** dir) { pf_vol_set.last_error = error; ((PFDIR_VOLUME_DIRS*)volume)->last_error = error; } else { - ((PFDIR_VOLUME_DIRS*)volume)->num_opened_directories++; + ((PFDIR_VOLUME_DIRS*)volume)->num_opened_directories += 1; } } @@ -1507,9 +1510,15 @@ pf_s32 PFDIR_chdir(PF_STR* path) { if (error != 0) { pf_vol_set.last_error = error; ((PFDIR_VOLUME_DIRS*)volume)->last_error = error; - return error; + } else { + PFVOL_SetCurrentVolume(volume); } - PFVOL_SetCurrentVolume(volume); + return error; +} + +static inline pf_s32 set_directory_context(PF_VOLUME* volume, PF_DIR_ENT* entry) { + pf_s32 error = PFVOL_SetCurrentDir(volume, entry); + if (error != 0) { return error; } return 0; } @@ -1528,14 +1537,14 @@ pf_s32 PFDIR_fchdir(PFDIR_DIR* dir) { ((PFDIR_VOLUME_DIRS*)volume)->last_error = error; return error; } - error = PFVOL_SetCurrentDir(volume, &dir->p_sdd->dir_entry); + error = set_directory_context(volume, &dir->p_sdd->dir_entry); if (error != 0) { pf_vol_set.last_error = error; ((PFDIR_VOLUME_DIRS*)volume)->last_error = error; - return error; + } else { + PFVOL_SetCurrentVolume(volume); } - PFVOL_SetCurrentVolume(volume); - return 0; + return error; } void PFDIR_chmod(PF_STR* path, pf_u8 attributes) { @@ -1580,7 +1589,7 @@ void PFDIR_chdmod(PF_STR* path, pf_u8 attributes) { return; } volume_dirs->cache_signature = 0; - error = PFDIR_p_chmod(volume, (PFDIR_STR*)path, attributes | 0x10); + error = PFDIR_p_chmod(volume, (PFDIR_STR*)path, (pf_u8)(attributes | 0x10)); if (error != 0) { pf_vol_set.last_error = error; ((PFDIR_VOLUME_DIRS*)volume)->last_error = error; @@ -1608,7 +1617,7 @@ pf_s32 PFDIR_p_fsnext(PF_DTA* data) { pf_u32 logical_position = 0; pf_u32 entry_position = 0; pf_u32 code_mode; - pf_u32 index; + pf_s32 index = 0; pf_s32 error; if ((data->status & 1) == 1) { @@ -1621,63 +1630,55 @@ pf_s32 PFDIR_p_fsnext(PF_DTA* data) { if (error != 0) { return error; } - PFSTR_SetLocalStr(&pattern, 0); - if (entry_position < 0xF423F) { - if (data->parent_pos > 0xF423E) { - return 10; - } - ffd.start_cluster = data->parent_start_cluster; - PFFAT_InitFFD(&ffd, &hint, data->p_vol, &ffd.start_cluster); + PFSTR_SetLocalStr(&pattern, (pf_u32)data->reg_exp); + while (entry_position < 999999) { + if (data->parent_pos >= 999999) { return 10; } + entry.start_cluster = data->parent_start_cluster; + PFFAT_InitFFD(&ffd, &hint, data->p_vol, &entry.start_cluster); error = PFENT_findEntryPos(&ffd, &entry, data->parent_pos, &pattern, data->attr, 0, &logical_position, &entry_position); - if (error != 0) { - return error; - } + if (error != 0) { return error; } data->parent_pos = entry_position + 1; - if (entry_position == 0xF423F) { - return 3; - } + if (entry_position == 999999) { return 3; } } pf_strcpy(data->FileName, entry.short_name); if ((data->status & 2) == 2) { PFPATH_transformInUnicode(((PF_DTAW*)data)->FileNameW, data->FileName); } - if (entry.long_name[0] == 0) { - data->LongName[0] = 0; - if ((data->status & 2) == 2) { - ((PF_DTAW*)data)->LongNameW[0] = 0; - } - } else { - if ((VFipf_vol_set.setting & 2) == 2) { - VFipf_vol_set.setting &= 0xFFFFFFFC; - VFipf_vol_set.setting |= 1; + if (entry.long_name[0] != 0) { + if ((pf_vol_set.setting & 2) == 2) { + pf_vol_set.setting &= 0xFFFFFFFC; + pf_vol_set.setting |= 1; PFPATH_transformFromUnicodeToNormal(data->LongName, entry.long_name); - VFipf_vol_set.setting &= 0xFFFFFFFC; - VFipf_vol_set.setting |= 2; + pf_vol_set.setting &= 0xFFFFFFFC; + pf_vol_set.setting |= 2; } else { PFPATH_transformFromUnicodeToNormal(data->LongName, entry.long_name); } if ((data->status & 2) == 2) { pf_w_strcpy(((PF_DTAW*)data)->LongNameW, entry.long_name); } + } else { + data->LongName[0] = 0; + if ((data->status & 2) == 2) { + ((PF_DTAW*)data)->LongNameW[0] = 0; + } } if (((PFDIR_VOLUME_DIRS*)data->p_vol)->num_opened_files != 0) { - PFDIR_SFD* sfd = ((PFDIR_VOLUME_DIRS*)data->p_vol)->sfds; for (index = 0; index < 5; index++) { - if ((sfd->stat & 1) != 0 && (sfd->stat & 2) != 0 && - sfd->dir_entry.p_vol == entry.p_vol && - sfd->dir_entry.entry_sector == entry.entry_sector && - sfd->dir_entry.entry_offset == entry.entry_offset) { - data->FileSize = sfd->dir_entry.file_size; - data->Time = sfd->dir_entry.modify_time; - data->Date = sfd->dir_entry.modify_date; - data->Attribute = sfd->dir_entry.attr; - data->num_entry_LFNs = sfd->dir_entry.num_entry_LFNs; - data->ordinal = sfd->dir_entry.ordinal; - data->check_sum = sfd->dir_entry.check_sum; + if ((((PFDIR_VOLUME_DIRS*)data->p_vol)->sfds[index].stat & 1) != 0 && (((PFDIR_VOLUME_DIRS*)data->p_vol)->sfds[index].stat & 2) != 0 && + entry.p_vol == ((PFDIR_VOLUME_DIRS*)data->p_vol)->sfds[index].dir_entry.p_vol && + ((PFDIR_VOLUME_DIRS*)data->p_vol)->sfds[index].dir_entry.entry_sector == entry.entry_sector && + ((PFDIR_VOLUME_DIRS*)data->p_vol)->sfds[index].dir_entry.entry_offset == entry.entry_offset) { + data->FileSize = ((PFDIR_VOLUME_DIRS*)data->p_vol)->sfds[index].dir_entry.file_size; + data->Time = ((PFDIR_VOLUME_DIRS*)data->p_vol)->sfds[index].dir_entry.modify_time; + data->Date = ((PFDIR_VOLUME_DIRS*)data->p_vol)->sfds[index].dir_entry.modify_date; + data->Attribute = ((PFDIR_VOLUME_DIRS*)data->p_vol)->sfds[index].dir_entry.attr; + data->num_entry_LFNs = ((PFDIR_VOLUME_DIRS*)data->p_vol)->sfds[index].dir_entry.num_entry_LFNs; + data->ordinal = ((PFDIR_VOLUME_DIRS*)data->p_vol)->sfds[index].dir_entry.ordinal; + data->check_sum = ((PFDIR_VOLUME_DIRS*)data->p_vol)->sfds[index].dir_entry.check_sum; break; } - sfd++; } } if (((PFDIR_VOLUME_DIRS*)data->p_vol)->num_opened_files == 0 || index == 5) { diff --git a/libs/RVL_SDK/src/fa/pf_fat.c b/libs/RVL_SDK/src/fa/pf_fat.c index 57b7fdf5..97a65db5 100644 --- a/libs/RVL_SDK/src/fa/pf_fat.c +++ b/libs/RVL_SDK/src/fa/pf_fat.c @@ -358,8 +358,8 @@ s32 PFFAT_FindClusterLink(PFFAT_FFD* file, u32 chainIndex, u32* cluster, u32 nextCluster; s32 error; - *found = 0; nextCluster = -1; + *found = 0; if (file->cluster_link.position == 0) { return 0; } @@ -388,6 +388,7 @@ s32 PFFAT_FindClusterLink(PFFAT_FFD* file, u32 chainIndex, u32* cluster, &nextCluster); break; default: + nextCluster = -1; error = 15; break; } @@ -486,6 +487,7 @@ s32 PFFAT_ReadCluster(PFFAT_FFD* file, u32 cluster, u32 chainIndex, u32* nextCluster, u32* checkClusterLink) { s32 error; + u32 value; *nextCluster = -1; if (file->cluster_link.buffer != 0 && *checkClusterLink == 1) { @@ -515,22 +517,22 @@ s32 PFFAT_ReadCluster(PFFAT_FFD* file, u32 cluster, u32 chainIndex, if (error != 0) { return error; } - if (*nextCluster == 0) { + value = *nextCluster; + if (value == 0) { return 13; } - if (*nextCluster == -1) { + if (value == -1) { return 6; } - } - if (file->cluster_link.buffer != 0 && - *nextCluster != fat_special_values[file->p_vol->bpb.fat_type].eoc2) { + if (file->cluster_link.buffer != 0 && + value != fat_special_values[file->p_vol->bpb.fat_type].eoc2) { if (file->cluster_link.max_count > file->cluster_link.position && file->cluster_link.max_count != 0) { if (chainIndex == file->cluster_link.position * (file->cluster_link.interval + 1)) { file->cluster_link.interval_offset = 0; file->cluster_link.buffer[file->cluster_link.position] = - *nextCluster; + value; file->cluster_link.position++; } else { file->cluster_link.interval_offset++; @@ -538,6 +540,7 @@ s32 PFFAT_ReadCluster(PFFAT_FFD* file, u32 cluster, u32 chainIndex, file->cluster_link.save_index = chainIndex; } } + } return 0; } @@ -546,6 +549,7 @@ s32 PFFAT_ReadClusterWithBuf(PFFAT_FFD* file, u32 cluster, u32 chainIndex, PF_CACHE_PAGE* page) { s32 error; + u32 value; *nextCluster = -1; if (file->cluster_link.buffer != 0 && *checkClusterLink == 1) { @@ -576,22 +580,22 @@ s32 PFFAT_ReadClusterWithBuf(PFFAT_FFD* file, u32 cluster, u32 chainIndex, if (error != 0) { return error; } - if (*nextCluster == 0) { + value = *nextCluster; + if (value == 0) { return 13; } - if (*nextCluster == -1) { + if (value == -1) { return 6; } - } - if (file->cluster_link.buffer != 0 && - *nextCluster != fat_special_values[file->p_vol->bpb.fat_type].eoc2) { + if (file->cluster_link.buffer != 0 && + value != fat_special_values[file->p_vol->bpb.fat_type].eoc2) { if (file->cluster_link.max_count > file->cluster_link.position && file->cluster_link.max_count != 0) { if (chainIndex == file->cluster_link.position * (file->cluster_link.interval + 1)) { file->cluster_link.interval_offset = 0; file->cluster_link.buffer[file->cluster_link.position] = - *nextCluster; + value; file->cluster_link.position++; } else { file->cluster_link.interval_offset++; @@ -599,6 +603,7 @@ s32 PFFAT_ReadClusterWithBuf(PFFAT_FFD* file, u32 cluster, u32 chainIndex, file->cluster_link.save_index = chainIndex; } } + } return 0; } @@ -608,6 +613,7 @@ s32 PFFAT_WriteCluster(PFFAT_FFD* file, u32 cluster, u32 chainIndex, u32 accessCluster; u32 position; u32 divisor; + u32* linkBuffer; u32 precedingIndex; s32 error; @@ -641,14 +647,15 @@ s32 PFFAT_WriteCluster(PFFAT_FFD* file, u32 cluster, u32 chainIndex, if (error != 0) { return error; } - if (file->cluster_link.buffer != 0 && useClusterLink == 1) { + linkBuffer = file->cluster_link.buffer; + if (linkBuffer != 0 && useClusterLink == 1) { if (nextCluster == 0) { divisor = file->cluster_link.interval + 1; position = chainIndex / divisor; if (file->cluster_link.max_count >= position) { if (chainIndex % divisor == 0) { - file->cluster_link.buffer[position] = 0; + linkBuffer[position] = 0; } if (file->cluster_link.save_index >= chainIndex) { precedingIndex = chainIndex - 1; @@ -694,6 +701,7 @@ s32 PFFAT_WriteClusterWithBuf(PFFAT_FFD* file, u32 cluster, u32 chainIndex, u32 accessCluster; u32 position; u32 divisor; + u32* linkBuffer; u32 precedingIndex; s32 error; @@ -729,14 +737,15 @@ s32 PFFAT_WriteClusterWithBuf(PFFAT_FFD* file, u32 cluster, u32 chainIndex, if (error != 0) { return error; } - if (file->cluster_link.buffer != 0 && useClusterLink == 1) { + linkBuffer = file->cluster_link.buffer; + if (linkBuffer != 0 && useClusterLink == 1) { if (nextCluster == 0) { divisor = file->cluster_link.interval + 1; position = chainIndex / divisor; if (file->cluster_link.max_count >= position) { if (chainIndex % divisor == 0) { - file->cluster_link.buffer[position] = 0; + linkBuffer[position] = 0; } if (file->cluster_link.save_index >= chainIndex) { precedingIndex = chainIndex - 1; @@ -1108,14 +1117,25 @@ s32 PFFAT_GetClusterAllocatedInChain(PFFAT_FFD* file, u32 initialCluster, return 0; } +static inline s32 find_cluster_in_chain(PFFAT_FFD* file, u32 index, u32 mode, u32 count, u32 useFirst, u32* cluster) { + u32 firstCluster; + u32 lastCluster; + u32 candidate; + s32 error = PFFAT_GetClusterInChain(file, index, mode, count, &firstCluster, &lastCluster); + if (error != 0) { return error; } + candidate = useFirst ? firstCluster : lastCluster; + if (candidate >= 2 && candidate < file->p_vol->bpb.num_clusters + 2) { *cluster = candidate; } + return 0; +} + s32 PFFAT_GetClusterSpecified(PFFAT_FFD* file, u32 chainIndex, u32 mayAllocate, u32* cluster) { PFFAT_HINT* hint; u32 fatType; u32 found; - u32 locateEnd; u32 locateStart; + u32 locateEnd; s32 error; fatType = file->p_vol->bpb.fat_type; @@ -1144,20 +1164,7 @@ s32 PFFAT_GetClusterSpecified(PFFAT_FFD* file, u32 chainIndex, } } *cluster = -1; - error = PFFAT_GetClusterInChain(file, chainIndex, mayAllocate != 0, 0, - &locateStart, &locateEnd); - if (error == 0) { - goto cluster_lookup_succeeded; - } - goto cluster_lookup_complete; - -cluster_lookup_succeeded: - if (locateEnd >= 2 && locateEnd < file->p_vol->bpb.num_clusters + 2) { - *cluster = locateEnd; - } - error = 0; - -cluster_lookup_complete: + error = find_cluster_in_chain(file, chainIndex, mayAllocate != 0, 0, 0, cluster); if (error != 0) { return error; } @@ -1173,10 +1180,10 @@ cluster_lookup_complete: s32 PFFAT_GetClusterAllocated(PFFAT_FFD* file, u32 chainIndex, u32 numClusters, u32* cluster) { - u32 fatType; PFFAT_HINT* hint; - u32 locateEnd; + u32 fatType; u32 locateStart; + u32 locateEnd; s32 error; fatType = file->p_vol->bpb.fat_type; @@ -1189,15 +1196,7 @@ s32 PFFAT_GetClusterAllocated(PFFAT_FFD* file, u32 chainIndex, *file->p_start_cluster = file->p_vol->bpb.root_dir_cluster; } *cluster = -1; - error = PFFAT_GetClusterInChain(file, chainIndex, 2, numClusters, - &locateStart, &locateEnd); - if (error == 0) { - if (locateStart >= 2 && - locateStart < file->p_vol->bpb.num_clusters + 2) { - *cluster = locateStart; - } - error = 0; - } + error = find_cluster_in_chain(file, chainIndex, 2, numClusters, 1, cluster); if (error != 0) { return error; } @@ -1225,17 +1224,18 @@ s32 PFFAT_GetSector(PFFAT_FFD* file, u32 fileSectorIndex, u32 mode, u32 size, u32* sector) { PF_VOLUME* volume; + u32 fatType; u32 chainIndex; u32 cluster; - u32 numDivide; u32 numRemainder; - u32 numSectors; + u32 numDivide; u32 numClusters; + u32 numSectors; s32 error; volume = file->p_vol; - if (*file->p_start_cluster == 1 && - (u32)volume->bpb.fat_type <= FAT_16) { + fatType = volume->bpb.fat_type; + if (*file->p_start_cluster == 1 && fatType <= FAT_16) { error = PFFAT_GetSectorInRootDirRegion(volume, fileSectorIndex, sector); if (error != 0) { return error; @@ -1244,13 +1244,12 @@ s32 PFFAT_GetSector(PFFAT_FFD* file, u32 fileSectorIndex, u32 mode, } chainIndex = fileSectorIndex >> volume->bpb.log2_sectors_per_cluster; if (mode == 2) { - numDivide = size / volume->bpb.bytes_per_sector; - numRemainder = (size % volume->bpb.bytes_per_sector) != 0; - numSectors = numDivide + numRemainder; + numSectors = (size / volume->bpb.bytes_per_sector) + + ((size % volume->bpb.bytes_per_sector) != 0); numDivide = numSectors / volume->bpb.sectors_per_cluster; numRemainder = (numSectors % volume->bpb.sectors_per_cluster) != 0; - numClusters = numDivide + numRemainder; + numClusters = numRemainder + numDivide; error = PFFAT_GetClusterAllocated(file, chainIndex, numClusters, &cluster); if (error != 0) { @@ -1375,7 +1374,8 @@ s32 PFFAT_CountAllocatedClusters(PFFAT_FFD* file, u32 size, *numAllocatedClusters = file->p_hint->chain_index; cluster = file->p_hint->cluster; chainIndex = file->p_hint->chain_index + 1; - size -= file->p_hint->chain_index * bytesPerCluster; + size -= file->p_hint->chain_index * + (file->p_vol->bpb.bytes_per_sector * file->p_vol->bpb.sectors_per_cluster); } else { cluster = *file->p_start_cluster; chainIndex = 1; @@ -1520,9 +1520,9 @@ s32 PFFAT_FreeChain(PFFAT_FFD* file, u32 startCluster, u32 chainIndex, } if (nextCluster == 0) { s32 firstFlushError; - u32 fatSector; - u16 fatIndex; PF_CACHE_PAGE* flushPage; + u16 fatIndex; + u32 fatSector; firstFlushError = 0; flushPage = page; @@ -1857,7 +1857,8 @@ s32 PFFAT_RefreshFSINFO(PF_VOLUME* volume) if ((volume->fsi_flag & 4) != 0) { error = PFCACHE_AllocateDataPage(volume, -1, &page, &alreadyAllocated); - if (error == 0) { + if (error != 0) { goto finished; } + { error = PFSEC_ReadData( volume, page->p_buf, volume->bpb.fs_info_sector, 0, volume->bpb.bytes_per_sector, &completedBytes, 0); @@ -1896,6 +1897,7 @@ s32 PFFAT_RefreshFSINFO(PF_VOLUME* volume) PFCACHE_FreeDataPage(volume, page); } } +finished: return error; } diff --git a/libs/RVL_SDK/src/fa/pf_file.c b/libs/RVL_SDK/src/fa/pf_file.c index 97857189..c029c635 100644 --- a/libs/RVL_SDK/src/fa/pf_file.c +++ b/libs/RVL_SDK/src/fa/pf_file.c @@ -1,3 +1,4 @@ +#define PF_FA_STR_LAYOUT #include #include #include @@ -414,16 +415,24 @@ pf_s32 PFFILE_Cursor_Read(PFFILE_FILE* file, pf_u8* buffer, pf_u32 size, pf_u32* return 0; } +static inline void advance_write_cursor(PFFILE_FILE* file, pf_u32 size, pf_u32 sector) { + PF_VOLUME* volume = file == PF_NULL ? PF_NULL : file->p_sfd->dir_entry.p_vol; + pf_u32 position = file->cursor.offset_in_sector + size; + pf_u32 next_sector = sector + (position >> volume->bpb.log2_bytes_per_sector); + if (((volume->bpb.bytes_per_sector - 1) & position) != 0) { file->cursor.sector = next_sector; } + else { file->cursor.sector = -1; } + file->cursor.position += size; + if (file->cursor.position > file->p_sfd->dir_entry.file_size) { + file->p_sfd->dir_entry.file_size = file->cursor.position; + } + PFFILE_Cursor_Recalc(file); + PFCLUSTER_UpdateLastAccessCluster(file, next_sector); +} + pf_s32 PFFILE_Cursor_WriteHeadSector(PFFILE_FILE* file, const pf_u8* buffer, pf_u32 size, pf_u32* size_written) { pf_s32 error; pf_u32 success_size; - pf_u32 next_position; - pf_u32 new_position; - pf_u32 current_sector; - pf_u32 written_size; - pf_u32 sector_advance; - pf_u32 next_sector; PF_VOLUME* volume; *size_written = 0; @@ -452,24 +461,7 @@ pf_s32 PFFILE_Cursor_WriteHeadSector(PFFILE_FILE* file, const pf_u8* buffer, pf_ return error; } *size_written = success_size; - current_sector = file->cursor.sector; - written_size = success_size; - volume = file == PF_NULL ? PF_NULL : file->p_sfd->dir_entry.p_vol; - next_position = file->cursor.offset_in_sector + written_size; - sector_advance = next_position >> volume->bpb.log2_bytes_per_sector; - next_sector = current_sector + sector_advance; - if (((volume->bpb.bytes_per_sector - 1) & next_position) != 0) { - file->cursor.sector = next_sector; - } else { - file->cursor.sector = -1; - } - new_position = file->cursor.position + success_size; - file->cursor.position = new_position; - if (new_position > file->p_sfd->dir_entry.file_size) { - file->p_sfd->dir_entry.file_size = new_position; - } - PFFILE_Cursor_Recalc(file); - PFCLUSTER_UpdateLastAccessCluster(file); + advance_write_cursor(file, success_size, file->cursor.sector); if (success_size != size) { return error; } @@ -481,7 +473,6 @@ pf_s32 PFFILE_Cursor_WriteBodySectors(PFFILE_FILE* file, const pf_u8* buffer, pf pf_s32 error; pf_u32 num_sectors; pf_u32 success_size; - pf_u32 next_position; PF_VOLUME* volume; *size_written = 0; @@ -506,18 +497,7 @@ pf_s32 PFFILE_Cursor_WriteBodySectors(PFFILE_FILE* file, const pf_u8* buffer, pf return error; } *size_written = success_size; - next_position = file->cursor.offset_in_sector + success_size; - if (((volume->bpb.bytes_per_sector - 1) & next_position) != 0) { - file->cursor.sector += next_position >> volume->bpb.log2_bytes_per_sector; - } else { - file->cursor.sector = -1; - } - file->cursor.position += success_size; - if (file->cursor.position > file->p_sfd->dir_entry.file_size) { - file->p_sfd->dir_entry.file_size = file->cursor.position; - } - PFFILE_Cursor_Recalc(file); - PFCLUSTER_UpdateLastAccessCluster(file); + advance_write_cursor(file, success_size, file->cursor.sector); if (success_size != size - (size & (volume->bpb.bytes_per_sector - 1))) { return error; } @@ -528,12 +508,6 @@ pf_s32 PFFILE_Cursor_WriteTailSector(PFFILE_FILE* file, const pf_u8* buffer, pf_ pf_u32* size_written) { pf_s32 error; pf_u32 success_size; - pf_u32 next_position; - pf_u32 current_sector; - pf_u32 written_size; - pf_u32 sector_advance; - pf_u32 next_sector; - pf_u32 new_position; PF_VOLUME* volume; *size_written = 0; @@ -563,24 +537,7 @@ pf_s32 PFFILE_Cursor_WriteTailSector(PFFILE_FILE* file, const pf_u8* buffer, pf_ return error; } *size_written = success_size; - current_sector = file->cursor.sector; - written_size = success_size; - volume = file == PF_NULL ? PF_NULL : file->p_sfd->dir_entry.p_vol; - next_position = file->cursor.offset_in_sector + written_size; - sector_advance = next_position >> volume->bpb.log2_bytes_per_sector; - next_sector = current_sector + sector_advance; - if (((volume->bpb.bytes_per_sector - 1) & next_position) != 0) { - file->cursor.sector = next_sector; - } else { - file->cursor.sector = -1; - } - new_position = file->cursor.position + success_size; - file->cursor.position = new_position; - if (new_position > file->p_sfd->dir_entry.file_size) { - file->p_sfd->dir_entry.file_size = new_position; - } - PFFILE_Cursor_Recalc(file); - PFCLUSTER_UpdateLastAccessCluster(file); + advance_write_cursor(file, success_size, file->cursor.sector); if (success_size != size) { return error; } @@ -697,15 +654,16 @@ PFFILE_SFD* PFFILE_GetSFD(PF_VOLUME* volume, PF_DIR_ENT* entry) { pf_u32 index = 0; for (; index < 5; index++, sfd++) { - if ((sfd->stat & 1) == 0 || (sfd->stat & 2) == 0) { + if ((volume_dirs->sfds[index].stat & 1) == 0 || + ((volume_dirs->sfds[index].stat & 1) != 0 && (!volume_dirs->sfds[index].stat & 2) != 0)) { if (first_free_sfd == PF_NULL) { first_free_sfd = sfd; first_free_index = index; } - } else if (entry->p_vol == sfd->dir_entry.p_vol && - entry->entry_sector == sfd->dir_entry.entry_sector && - entry->entry_offset == sfd->dir_entry.entry_offset) { - return sfd; + } else if (entry->p_vol == volume_dirs->sfds[index].dir_entry.p_vol && + entry->entry_sector == volume_dirs->sfds[index].dir_entry.entry_sector && + entry->entry_offset == volume_dirs->sfds[index].dir_entry.entry_offset) { + return &volume_dirs->sfds[index]; } } @@ -744,22 +702,23 @@ PFFILE_SFD* PFFILE_GetSFD(PF_VOLUME* volume, PF_DIR_ENT* entry) { pf_s32 PFFILE_createEmptyFile(PF_VOLUME* volume, PF_DIR_ENT* entry, PF_STR* filename) { PF_DIR_ENT parent_entry = *entry; PFFILE_FFD ffd; - PF_FAT_HINT hint; + PFFILE_FAT_HINT hint; pf_u32 previous_sectors[2]; + pf_u32* previous_sector; + pf_u32 success_size; pf_u32 entry_position; - pf_u32 num_chars; - pf_u32 num_entries; + pf_s32 num_chars; + pf_u8 num_entries; + pf_u8 checksum; pf_u32 ordinal; pf_u32 sector; - pf_u32 success_size; pf_s32 error; pf_u8 short_entry_buffer[13]; pf_u8 long_entry_buffer[32]; num_chars = PFSTR_StrNumChar(filename, 1) & 0xFFFF; - if (num_chars + parent_entry.path_len >= 0x104 || num_chars >= 0x100) { - return 1; - } + if (num_chars + parent_entry.path_len > 0x103) { return 1; } + if (num_chars > 0xFF) { return 1; } PFFAT_InitFFD(&ffd, &hint, parent_entry.p_vol, &parent_entry.start_cluster); if (((PFFILE_VOLUME_DIRS*)volume)->num_opened_files >= 5) { return 0x15; @@ -768,9 +727,7 @@ pf_s32 PFFILE_createEmptyFile(PF_VOLUME* volume, PF_DIR_ENT* entry, PF_STR* file if (error != 0 && entry->short_name[0] == 0) { return 1; } - if (error == 0) { - entry->long_name[0] = 0; - } else { + if (error != 0) { error = PFENT_AdjustSFN(&parent_entry, entry->short_name); if (error != 0) { return error; @@ -780,8 +737,11 @@ pf_s32 PFFILE_createEmptyFile(PF_VOLUME* volume, PF_DIR_ENT* entry, PF_STR* file } else { pf_w_strcpy(entry->long_name, (const pf_u16*)PFSTR_GetStrPos(filename, 1)); } + } else { + entry->long_name[0] = 0; } - entry->start_cluster = volume->bpb.fat_type == FAT_32 ? 1 : 0; + if (volume->bpb.fat_type == FAT_32) { entry->start_cluster = 1; } + else { entry->start_cluster = 0; } entry->file_size = 0; entry->p_vol = volume; entry->small_letter_flag = 0; @@ -798,14 +758,16 @@ pf_s32 PFFILE_createEmptyFile(PF_VOLUME* volume, PF_DIR_ENT* entry, PF_STR* file if (error != 0) { return error; } - if ((pf_vol_set.file_settings & 2) != 0) { + if ((pf_vol_set.file_settings & 2) == 2) { PFPATH_AdjustExtShortName(entry->short_name, entry_position); } entry->num_entry_LFNs = num_entries; sector = entry->entry_sector; - for (ordinal = num_entries; ordinal != 0; ordinal--) { + checksum = PFENT_CalcCheckSum(entry); + previous_sector = previous_sectors; + for (ordinal = num_entries; ordinal >= 1; ordinal--) { PFENT_storeLFNEntryFieldsToBuf(long_entry_buffer, entry, ordinal, - PFENT_CalcCheckSum(entry), ordinal == num_entries); + checksum, ordinal == num_entries); error = PFSEC_WriteData(volume, long_entry_buffer, sector, entry->entry_offset, 0x20, &success_size, 0); if (error != 0) { @@ -817,8 +779,7 @@ pf_s32 PFFILE_createEmptyFile(PF_VOLUME* volume, PF_DIR_ENT* entry, PF_STR* file entry->entry_offset += 0x20; if (entry->entry_offset >= volume->bpb.bytes_per_sector) { entry->entry_offset = 0; - sector = previous_sectors[0]; - previous_sectors[0] = previous_sectors[1]; + sector = *previous_sector++; } } entry->entry_sector = sector; @@ -834,14 +795,25 @@ pf_s32 PFFILE_createEmptyFile(PF_VOLUME* volume, PF_DIR_ENT* entry, PF_STR* file return PFENT_updateEntry(entry, 1); } +static inline pf_u32 file_is_open(PF_DIR_ENT* entry) { + pf_s32 index; + for (index = 0; index < 5; index++) { + PFFILE_SFD* sfd = &((PFFILE_VOLUME_DIRS*)entry->p_vol)->sfds[index]; + if ((sfd->stat & 1) != 0 && (sfd->stat & 2) != 0 && + entry->p_vol == sfd->dir_entry.p_vol && entry->entry_sector == sfd->dir_entry.entry_sector && + entry->entry_offset == sfd->dir_entry.entry_offset) { return 1; } + } + return 0; +} + pf_s32 PFFILE_p_remove(PF_VOLUME* volume, PF_STR* path) { PF_STR directory; PF_STR filename; PF_DIR_ENT entry; PFFILE_ENT_ITER iter; - PFFILE_VOLUME_DIRS* volume_dirs = (PFFILE_VOLUME_DIRS*)volume; pf_s32 error; - pf_u32 index; + PFFILE_FAT_HINT hint; + pf_u32 start_cluster; error = PFPATH_SplitPath(path, &directory, &filename); if (error != 0) { @@ -851,39 +823,34 @@ pf_s32 PFFILE_p_remove(PF_VOLUME* volume, PF_STR* path) { if (error != 0) { return error; } + hint = *(PFFILE_FAT_HINT*)iter.ffd.p_hint; if ((entry.attr & 0x19) != 0) { return 0xB; } - for (index = 0; index < 5; index++) { - if ((volume_dirs->sfds[index].stat & 1) != 0 && - (volume_dirs->sfds[index].stat & 2) != 0 && - entry.p_vol == volume_dirs->sfds[index].dir_entry.p_vol && - entry.entry_sector == volume_dirs->sfds[index].dir_entry.entry_sector && - entry.entry_offset == volume_dirs->sfds[index].dir_entry.entry_offset) { - return 0x13; - } - } + if (file_is_open(&entry) != 0) { return 0x13; } + iter.ffd.p_hint = (PF_FAT_HINT*)&hint; + start_cluster = entry.start_cluster; error = PFENT_RemoveEntry(&entry, &iter); if (error != 0) { return error; } - return PFFAT_FreeChain(&iter.ffd, entry.start_cluster, 0xFFFFFFFF, entry.file_size); + return PFFAT_FreeChain(&iter.ffd, start_cluster, 0xFFFFFFFF, entry.file_size); } +static PFFILE_FILE* PFFILE_GetFreeUFD(PF_VOLUME* volume); + pf_s32 PFFILE_p_fopen(PF_VOLUME* volume, PF_STR* path, pf_u32 open_mode, PFFILE_FILE** result) { - PF_STR directory; PF_STR filename; + PF_STR directory; PF_DIR_ENT entry; PFFILE_ENT_ITER iter; PFFILE_FFD ffd; - PF_FAT_HINT hint; + PFFILE_FAT_HINT hint; PFFILE_SFD* sfd; PFFILE_FILE* file; pf_s8 normalized_filename[0x200]; - pf_u32 num_chars; - pf_u32 index; - pf_u32 success_size; + pf_s32 num_chars; pf_u16 access_time; pf_s32 error; @@ -908,49 +875,7 @@ pf_s32 PFFILE_p_fopen(PF_VOLUME* volume, PF_STR* path, pf_u32 open_mode, (const pf_u16*)PFSTR_GetStrPos(&filename, 1)); } PFSTR_SetLocalStr(&filename, normalized_filename); - if ((open_mode & 2) == 0) { - error = PFFILE_createEmptyFile(volume, &entry, &filename); - if (error == 8) { - if ((open_mode & 0x10) != 0) { - return 8; - } - if ((open_mode & 1) != 0) { - for (index = 0; index < 5; index++) { - if ((((PFFILE_VOLUME_DIRS*)entry.p_vol)->sfds[index].stat & 3) == 3 && - entry.p_vol == - ((PFFILE_VOLUME_DIRS*)entry.p_vol)->sfds[index].dir_entry.p_vol && - entry.entry_sector == - ((PFFILE_VOLUME_DIRS*)entry.p_vol)->sfds[index].dir_entry.entry_sector && - entry.entry_offset == - ((PFFILE_VOLUME_DIRS*)entry.p_vol)->sfds[index].dir_entry.entry_offset) { - return 8; - } - } - } - if ((entry.attr & 1) != 0 && - ((open_mode & 1) != 0 || (open_mode & 4) != 0 || (open_mode & 8) != 0)) { - return 0xA; - } - if ((entry.attr & 0x10) != 0) { - return 0x17; - } - if ((open_mode & 1) != 0) { - PFFAT_InitFFD(&ffd, &hint, entry.p_vol, &entry.start_cluster); - if (entry.start_cluster > 1 && entry.start_cluster != 0xFFFFFFFF && - entry.file_size != 0) { - PFFAT_FreeChain(&ffd, entry.start_cluster, 0xFFFFFFFF, entry.file_size); - } - entry.start_cluster = 0; - entry.file_size = 0; - PFENT_getcurrentDateTimeForEnt(&entry.modify_date, &entry.modify_time); - entry.access_date = entry.modify_date; - } else { - PFENT_getcurrentDateTimeForEnt(&entry.access_date, &access_time); - } - } else if (error != 0) { - return error; - } - } else { + if ((open_mode & 2) != 0) { PFFAT_InitFFD(&ffd, &hint, entry.p_vol, &entry.start_cluster); error = PFENT_findEntry(&ffd, &entry, 0, &filename, 0x7F, 0); if (error != 0) { @@ -963,37 +888,61 @@ pf_s32 PFFILE_p_fopen(PF_VOLUME* volume, PF_STR* path, pf_u32 open_mode, return 0x17; } PFENT_getcurrentDateTimeForEnt(&entry.access_date, &access_time); + } else { + error = PFFILE_createEmptyFile(volume, &entry, &filename); + if (error == 8) { + if ((open_mode & 0x10) != 0) { + return 8; + } + if ((open_mode & 1) != 0) { + if (file_is_open(&entry) != 0) { return 8; } + } + if ((entry.attr & 1) != 0 && + ((open_mode & 1) != 0 || (open_mode & 4) != 0 || (open_mode & 8) != 0)) { + return 0xA; + } + if ((entry.attr & 0x10) != 0) { + return 0x17; + } + if ((open_mode & 1) != 0) { + if (entry.start_cluster >= 2 && entry.start_cluster != 0xFFFFFFFF && + entry.file_size != 0) { + PFFAT_FreeChain(&iter.ffd, entry.start_cluster, 0xFFFFFFFF, entry.file_size); + } + entry.start_cluster = 0; + entry.file_size = 0; + PFENT_getcurrentDateTimeForEnt(&entry.modify_date, &entry.modify_time); + entry.access_date = entry.modify_date; + } else { + PFENT_getcurrentDateTimeForEnt(&entry.access_date, &access_time); + } + } else if (error != 0) { + return error; + } } volume = entry.p_vol; sfd = PFFILE_GetSFD(volume, &entry); if (sfd == PF_NULL) { return 0x15; } - file = PF_NULL; - for (index = 0; index < 5; index++) { - if ((((PFFILE_VOLUME_DIRS*)volume)->ufds[index].stat & 1) == 0) { - file = &((PFFILE_VOLUME_DIRS*)volume)->ufds[index]; - break; - } - } + file = PFFILE_GetFreeUFD(volume); if (file == PF_NULL) { return 0x16; } file->p_sfd = sfd; file->stat = 1; file->open_mode = open_mode; - file->last_access_data.state.last_access_chain_index = 0; file->last_access_data.state.last_error = 0; file->lock_count = 0; PFFAT_InitHint(&file->hint); - file->cursor.position = 0; file->cursor.sector = 0xFFFFFFFF; + file->cursor.position = 0; PFFILE_Cursor_Recalc(file); if ((open_mode & 1) != 0) { - sfd->stat |= 4; + file->p_sfd->stat |= 4; } - if ((open_mode & 4) != 0) { - file->cursor.position = entry.file_size; + if ((file->open_mode & 4) != 0) { + file->cursor.position = file->p_sfd->dir_entry.file_size; PFFILE_Cursor_Recalc(file); } sfd->handler_count++; @@ -1002,7 +951,26 @@ pf_s32 PFFILE_p_fopen(PF_VOLUME* volume, PF_STR* path, pf_u32 open_mode, } pf_s32 PFFILE_p_fread(PF_VOLUME* volume, pf_u8* buffer, pf_u32 size, pf_u32 count, - PFFILE_FILE* file, pf_u32* count_read); + PFFILE_FILE* file, pf_u32* count_read) { + pf_u32 bytes_read = 0; + pf_s32 error; + + *count_read = 0; + if ((file->open_mode & 8) == 0 && + ((file->open_mode & 1) != 0 || (file->open_mode & 4) != 0)) { + return 0xA; + } + if ((file->p_sfd->lock.mode & 3) != 0 && file->lock_count == 0) { + return 0x19; + } + if (file->cursor.position >= file->p_sfd->dir_entry.file_size) { + return 0x1C; + } + error = PFFILE_Cursor_Read(file, buffer, size * count, &bytes_read); + *count_read = bytes_read / size; + error &= (-error | error) >> 31; + return error; +} pf_s32 PFFILE_p_fwrite(PF_VOLUME* volume, const pf_u8* buffer, pf_u32 size, pf_u32 count, PFFILE_FILE* file, pf_u32* count_written) { @@ -1028,32 +996,30 @@ pf_s32 PFFILE_p_fwrite(PF_VOLUME* volume, const pf_u8* buffer, pf_u32 size, pf_u if (file->cursor.position > file->p_sfd->dir_entry.file_size) { remaining = file->cursor.position - file->p_sfd->dir_entry.file_size; file->cursor.position = file->p_sfd->dir_entry.file_size; - PFFILE_Cursor_Recalc(file); + file->cursor.file_sector_index = file->cursor.position >> volume->bpb.log2_bytes_per_sector; + file->cursor.offset_in_sector = file->cursor.position & (volume->bpb.bytes_per_sector - 1); error = PFCACHE_AllocateDataPage(volume, 0xFFFFFFFF, &page, &allocation_option); if (error != 0) { return error; } pf_memset(page->p_buf, 0, volume->bpb.bytes_per_sector); while (remaining != 0) { - if (remaining <= volume->bpb.bytes_per_sector) { + if (remaining > volume->bpb.bytes_per_sector) { + error = PFFILE_Cursor_Write(file, page->p_buf, volume->bpb.bytes_per_sector, &bytes_written); + if (error != 0) { return error; } + } else { error = PFFILE_Cursor_Write(file, page->p_buf, remaining, &bytes_written); - if (error != 0) { - return error; - } + if (error != 0) { return error; } break; } - error = PFFILE_Cursor_Write(file, page->p_buf, volume->bpb.bytes_per_sector, - &bytes_written); - if (error != 0) { - return error; - } remaining -= volume->bpb.bytes_per_sector; } PFCACHE_FreeDataPage(volume, page); } error = PFFILE_Cursor_Write(file, buffer, size * count, &bytes_written); *count_written = bytes_written / size; - return error; + if (error != 0) { return error; } + return 0; } pf_s32 PFFILE_p_fappend(PFFILE_FILE* file, pf_u32 size, pf_u32* size_appended) { @@ -1126,10 +1092,8 @@ pf_s32 PFFILE_p_combine(PF_VOLUME* volume, PF_STR* first_path, PF_STR* second_pa PFFILE_ENT_ITER second_iter_copy; PF_DIR_ENT first_entry; PF_DIR_ENT second_entry; - PF_FAT_HINT first_hint; - PF_FAT_HINT second_hint; - pf_u32 index; - pf_s32 found; + PFFILE_FAT_HINT first_hint; + PFFILE_FAT_HINT second_hint; pf_s32 error; error = PFENT_ITER_GetEntryOfPath(&first_iter, &first_entry, volume, first_path, 0); @@ -1146,41 +1110,12 @@ pf_s32 PFFILE_p_combine(PF_VOLUME* volume, PF_STR* first_path, PF_STR* second_pa if ((first_entry.attr & 1) != 0 || (second_entry.attr & 1) != 0) { return 0x18; } - found = 0; - for (index = 0; index < 5; index++) { - PFFILE_SFD* sfd = &((PFFILE_VOLUME_DIRS*)first_entry.p_vol)->sfds[index]; - - if ((sfd->stat & 1) != 0 && (sfd->stat & 2) != 0 && - first_entry.p_vol == sfd->dir_entry.p_vol && - first_entry.entry_sector == sfd->dir_entry.entry_sector && - first_entry.entry_offset == sfd->dir_entry.entry_offset) { - found = 1; - break; - } - } - if (found != 0) { - return 0x13; - } - found = 0; - for (index = 0; index < 5; index++) { - PFFILE_SFD* sfd = &((PFFILE_VOLUME_DIRS*)second_entry.p_vol)->sfds[index]; - - if ((sfd->stat & 1) != 0 && (sfd->stat & 2) != 0 && - second_entry.p_vol == sfd->dir_entry.p_vol && - second_entry.entry_sector == sfd->dir_entry.entry_sector && - second_entry.entry_offset == sfd->dir_entry.entry_offset) { - found = 1; - break; - } - } - if (found != 0) { - return 0x13; - } + if (file_is_open(&first_entry) != 0 || file_is_open(&second_entry) != 0) { return 0x13; } if (first_entry.entry_sector == second_entry.entry_sector && first_entry.entry_offset == second_entry.entry_offset) { return 0xA; } - if (0xFFFFFFFF - second_entry.file_size < first_entry.file_size) { + if (0xFFFFFFFF - first_entry.file_size < second_entry.file_size) { return 0x25; } PFFAT_InitHint(&first_hint); @@ -1204,24 +1139,20 @@ pf_s32 PFFILE_p_combine(PF_VOLUME* volume, PF_STR* first_path, PF_STR* second_pa pf_s32 PFFILE_p_divide(PF_VOLUME* volume, PF_STR* source_path, PF_STR* destination_path, pf_u32 split_size) { - PF_STR destination_directory; PF_STR destination_filename; + PF_STR destination_directory; PF_DIR_ENT source_entry; - PF_DIR_ENT source_entry_copy; PF_DIR_ENT destination_entry; + PF_DIR_ENT source_entry_copy; PFFILE_ENT_ITER source_iter; PFFILE_ENT_ITER destination_iter; - PFFILE_ENT_ITER destination_file_iter; - PFFILE_FFD source_ffd; - PFFILE_FFD destination_ffd; - PF_FAT_HINT source_hint; - PF_FAT_HINT destination_hint; + PFFILE_FAT_HINT source_hint; + PFFILE_FAT_HINT destination_hint; pf_s8 normalized_filename[0x200]; - pf_u32 original_start_cluster; pf_u32 original_size; - pf_u32 index; - pf_u16 current_time; + pf_u32 original_start_cluster; pf_s32 error; + pf_s32 divide_error; error = PFENT_ITER_GetEntryOfPath(&source_iter, &source_entry, volume, source_path, 0); if (error != 0) { @@ -1233,17 +1164,7 @@ pf_s32 PFFILE_p_divide(PF_VOLUME* volume, PF_STR* source_path, PF_STR* destinati if ((source_entry.attr & 1) != 0) { return 0x18; } - for (index = 0; index < 5; index++) { - if ((((PFFILE_VOLUME_DIRS*)source_entry.p_vol)->sfds[index].stat & 3) == 3 && - source_entry.p_vol == - ((PFFILE_VOLUME_DIRS*)source_entry.p_vol)->sfds[index].dir_entry.p_vol && - source_entry.entry_sector == - ((PFFILE_VOLUME_DIRS*)source_entry.p_vol)->sfds[index].dir_entry.entry_sector && - source_entry.entry_offset == - ((PFFILE_VOLUME_DIRS*)source_entry.p_vol)->sfds[index].dir_entry.entry_offset) { - return 0x13; - } - } + if (file_is_open(&source_entry) != 0) { return 0x13; } if (source_entry.file_size == 0) { return 0xA; } @@ -1255,7 +1176,7 @@ pf_s32 PFFILE_p_divide(PF_VOLUME* volume, PF_STR* source_path, PF_STR* destinati return 1; } if (PFSTR_GetCodeMode(&destination_filename) == 2) { - if ((PFSTR_StrNumChar(&destination_filename, 1) & 0xFFFF) > 0xFF) { + if ((pf_s32)(PFSTR_StrNumChar(&destination_filename, 1) & 0xFFFF) > 0xFF) { return 1; } PFPATH_transformFromUnicodeToNormal( @@ -1274,13 +1195,13 @@ pf_s32 PFFILE_p_divide(PF_VOLUME* volume, PF_STR* source_path, PF_STR* destinati if (error != 0) { return error; } - PFFAT_InitHint(&destination_hint); - PFFAT_InitFFD(&destination_ffd, &destination_hint, volume, &destination_entry.start_cluster); PFFAT_InitHint(&source_hint); - PFFAT_InitFFD(&source_ffd, &source_hint, volume, &source_entry.start_cluster); + PFFAT_InitFFD(&source_iter.ffd, &source_hint, volume, &source_entry.start_cluster); + PFFAT_InitHint(&destination_hint); + PFFAT_InitFFD(&destination_iter.ffd, &destination_hint, volume, &destination_entry.start_cluster); pf_memcpy(&source_entry_copy, &source_entry, sizeof(source_entry_copy)); - original_start_cluster = source_entry.start_cluster; original_size = source_entry.file_size; + original_start_cluster = source_entry.start_cluster; PFENT_getcurrentDateTimeForEnt(&source_entry.modify_date, &source_entry.modify_time); source_entry.access_date = source_entry.modify_date; if (split_size == 0) { @@ -1288,32 +1209,30 @@ pf_s32 PFFILE_p_divide(PF_VOLUME* volume, PF_STR* source_path, PF_STR* destinati } source_entry.file_size = split_size; error = PFENT_updateEntry(&source_entry, 1); - if (error == 0 && - ((((PFFILE_VOLUME_DIRS*)volume)->cache_control & 4) == 0 || - PFCACHE_FlushDataCacheSpecific(volume, 0) == 0)) { - error = PFCLUSTER_DivideFile(&source_iter, &destination_iter, &source_entry_copy, - &destination_entry, split_size); - if (error == 0) { - destination_entry.create_time = source_entry.modify_time; - destination_entry.create_date = source_entry.modify_date; - destination_entry.modify_time = source_entry.modify_time; - destination_entry.modify_date = source_entry.modify_date; - destination_entry.access_date = source_entry.access_date; - destination_entry.attr = source_entry.attr; - error = PFENT_updateEntry(&destination_entry, 1); - return error & (-error | error) >> 31; - } - PFCACHE_FlushFATCache(volume); - source_entry.start_cluster = original_start_cluster; - source_entry.file_size = original_size; - PFENT_updateEntry(&source_entry, 1); - error = PFENT_ITER_GetEntryOfPath(&destination_file_iter, &destination_entry, volume, - destination_path, 0); - if (error == 0) { - PFENT_RemoveEntry(&destination_entry, &destination_file_iter); - } + if (error != 0) { return error; } + if ((((PFFILE_VOLUME_DIRS*)volume)->cache_control & 4) != 0) { + error = PFCACHE_FlushDataCacheSpecific(volume, 0); + if (error != 0) { return error; } } - return error; + error = PFCLUSTER_DivideFile(&source_iter, &destination_iter, &source_entry_copy, + &destination_entry, split_size); + divide_error = error; + if (error != 0) { + PFCACHE_FlushFATCache(volume); + source_entry.file_size = original_size; + source_entry.start_cluster = original_start_cluster; + PFENT_updateEntry(&source_entry, 1); + PFENT_ITER_GetEntryOfPath(&destination_iter, &destination_entry, volume, + destination_path, 0); + PFENT_RemoveEntry(&destination_entry, &destination_iter); + return divide_error; + } + destination_entry.modify_date = source_entry.modify_date; + destination_entry.modify_time = source_entry.modify_time; + destination_entry.access_date = source_entry.access_date; + destination_entry.attr = source_entry.attr; + error = PFENT_updateEntry(&destination_entry, 1); + return error & (-error | error) >> 31; } pf_s32 PFFILE_p_cinsert(PF_VOLUME* volume, PF_STR* path, pf_u32 cluster_index, @@ -1403,20 +1322,15 @@ pf_s32 PFFILE_p_cdelete(PF_VOLUME* volume, PF_STR* path, pf_u32 cluster_index, pf_s32 PFFILE_p_flock(PFFILE_FILE* file, pf_u32 mode) { if ((mode & 3) != 0) { - if ((file->p_sfd->lock.mode & 3) != 0 && (mode & 0x10) != 0) { - if ((mode & 1) != 0) { - if ((file->p_sfd->lock.mode & 2) != 0) { - return 0x27; - } - } else if ((mode & 2) != 0 && (file->p_sfd->lock.mode & 1) != 0) { - return 0x27; - } - if (file->p_sfd->lock.owner != 0 && file->p_sfd->lock.owner != (PF_FILE*)file) { + if ((file->p_sfd->lock.mode & 3) != 0 && (mode & 8) != 0) { + if (((mode & 1) != 0 && (file->p_sfd->lock.mode & 2) != 0) || + ((mode & 2) != 0 && (file->p_sfd->lock.mode & 1) != 0) || + (file->p_sfd->lock.owner != 0 && file->p_sfd->lock.owner != (PF_FILE*)file)) { return 0x27; } } if ((mode & 1) != 0) { - if (file->p_sfd->lock.count == 0 || (file->p_sfd->lock.mode & 2) == 0) { + if (file->p_sfd->lock.count == 0 || (file->p_sfd->lock.mode & 2) != 0) { file->p_sfd->lock.wcount++; PF_LockFile((PF_FILE*)file, 0); file->p_sfd->lock.wcount--; @@ -1432,7 +1346,7 @@ pf_s32 PFFILE_p_flock(PFFILE_FILE* file, pf_u32 mode) { } file->p_sfd->lock.count++; file->lock_count++; - return 0; + goto complete; } if (file->lock_count == 0 || file->p_sfd->lock.count == 0) { return 0x27; @@ -1440,23 +1354,14 @@ pf_s32 PFFILE_p_flock(PFFILE_FILE* file, pf_u32 mode) { if ((file->p_sfd->lock.mode & 2) != 0 && file->p_sfd->lock.owner != (PF_FILE*)file) { return 0x27; } - if ((file->p_sfd->lock.mode & 1) != 0) { - file->p_sfd->lock.count -= file->lock_count; - file->lock_count = 0; - if (file->p_sfd->lock.count == 0) { - PF_UnLockFile((PF_FILE*)file); - } - file->p_sfd->lock.mode &= ~3; - } else { - if (file->p_sfd->lock.owner != (PF_FILE*)file) { - return 0x27; - } - file->p_sfd->lock.count = 0; - file->lock_count = 0; - file->p_sfd->lock.owner = 0; + file->lock_count--; + file->p_sfd->lock.count--; + if (file->p_sfd->lock.count == 0) { PF_UnLockFile((PF_FILE*)file); + if ((file->p_sfd->lock.mode & 2) != 0) { file->p_sfd->lock.owner = 0; } file->p_sfd->lock.mode &= ~3; } +complete: return 0; } @@ -1708,10 +1613,12 @@ close_file_complete: pf_s32 PFFILE_fread(pf_u8* buffer, pf_u32 size, pf_u32 count, PFFILE_FILE* file, pf_u32* count_read) { - PF_VOLUME* volume = file == PF_NULL ? PF_NULL : file->p_sfd->dir_entry.p_vol; + PF_VOLUME* volume; pf_s32 error; + pf_u32 completed_count; *count_read = 0; + volume = file == PF_NULL ? PF_NULL : file->p_sfd->dir_entry.p_vol; error = PFVOL_CheckForRead(volume); if (error != 0) { pf_vol_set.last_error = error; @@ -1724,7 +1631,8 @@ pf_s32 PFFILE_fread(pf_u8* buffer, pf_u32 size, pf_u32 count, return 0x26; } ((PFFILE_VOLUME_DIRS*)volume)->cache_signature = file; - error = PFFILE_p_fread(volume, buffer, size, count, file, count_read); + error = PFFILE_p_fread(volume, buffer, size, count, file, &completed_count); + *count_read = completed_count; ((PFFILE_VOLUME_DIRS*)volume)->cache_signature = PF_NULL; if (error != 0) { pf_vol_set.last_error = error; @@ -1869,8 +1777,8 @@ pf_s32 PFFILE_fsetclstlink(PFFILE_FILE* file, pf_u32 mode, file->p_sfd->ffd.cluster_link.interval_offset = 0; file->p_sfd->ffd.cluster_link.position = 0; file->p_sfd->ffd.cluster_link.save_index = 0; - if (file->p_sfd->dir_entry.file_size != 0 && settings->link_max != 0) { - settings->buffer[0] = *file->p_sfd->ffd.p_start_cluster; + if (file->p_sfd->dir_entry.file_size != 0 && file->p_sfd->ffd.cluster_link.max_count != 0) { + file->p_sfd->ffd.cluster_link.buffer[0] = *file->p_sfd->ffd.p_start_cluster; file->p_sfd->ffd.cluster_link.position++; } if (file->p_sfd->ffd.p_hint != PF_NULL) { @@ -2217,25 +2125,3 @@ pf_s32 PFFILE_flock(PFFILE_FILE* file, pf_u32 mode) { } return error; } - -pf_s32 PFFILE_p_fread(PF_VOLUME* volume, pf_u8* buffer, pf_u32 size, pf_u32 count, - PFFILE_FILE* file, pf_u32* count_read) { - pf_u32 bytes_read = 0; - pf_s32 error; - - *count_read = 0; - if ((file->open_mode & 8) == 0 && - ((file->open_mode & 1) != 0 || (file->open_mode & 4) != 0)) { - return 0xA; - } - if ((file->p_sfd->lock.mode & 3) != 0 && file->lock_count == 0) { - return 0x19; - } - if (file->cursor.position >= file->p_sfd->dir_entry.file_size) { - return 0x1C; - } - error = PFFILE_Cursor_Read(file, buffer, size * count, &bytes_read); - *count_read = bytes_read / size; - error &= (-error | error) >> 31; - return error; -} diff --git a/libs/RVL_SDK/src/fa/pf_volume.c b/libs/RVL_SDK/src/fa/pf_volume.c index 87af2a9f..9f8db06c 100644 --- a/libs/RVL_SDK/src/fa/pf_volume.c +++ b/libs/RVL_SDK/src/fa/pf_volume.c @@ -169,7 +169,8 @@ typedef struct PFVOL_CLUSTER_SETTING { struct PFVOL_VOLUME { PFVOL_BPB bpb; u32 num_free_clusters; - u8 gap_003C[0xD10]; + u32 last_free_cluster; + u8 gap_0040[0xD0C]; PFVOL_OPEN_FILE files[5]; u8 gap_0E3C[0x7E0]; u32 num_open_files; @@ -190,8 +191,10 @@ struct PFVOL_VOLUME { u32 last_driver_error; u32 reserved_1f88; u16 flags; - u16 options; + s8 drv_char; + u8 drive_state; u16 fsi_flag; + u16 reserved_1f92; u16 cluster_link_flags; u16 cluster_link_interval; u32* cluster_link_buffer; @@ -286,6 +289,95 @@ extern s32 PFCODE_CP932_isUnicodeMBchar(u16 value, s32 mode); extern void* pf_memset(void* dst, int value, u32 size); extern s32 pf_memcmp(const void* left, const void* right, u32 size); +static inline u32 check_context_registered(s32 context_id) { + u32 index; + for (index = 1; index < 4; index++) { + if ((pf_vol_set.context[index - 1].stat &= 1) != 0 && + context_id == pf_vol_set.context[index - 1].context_id) { return 1; } + } + return 0; +} +static inline s32 clear_mount(PFVOL_VOLUME* volume) { + s32 error; + if ((volume->flags & 8) == 0) { + error = PFVOL_DoMountVolume(volume); + if (error != 0) { return error; } + volume->fsi_flag &= ~7; + PFDRV_ClearUnmountRequested(volume); + pf_vol_set.num_mounted_volumes++; + } + PFDRV_ClearMountRequested(volume); + return 0; +} +static inline s32 clear_unmount(PFVOL_VOLUME* volume, u32 mode) { + s32 error = PFDRV_unmount(volume, mode); + if (error != 0) { return error; } + if (volume->flags & 8) { + volume->current_dir[0].stat = 0; + volume->current_dir[1].stat = 0; + volume->current_dir[2].stat = 0; + volume->current_dir[3].stat = 0; + } + volume->flags &= ~8; + return 0; +} +static inline s32 check_driver_requests(PFVOL_VOLUME* volume) { + s32 error; + if (PFDRV_IsInserted(volume) != 0) { + if (PFDRV_IsUnmountRequested(volume) != 0 && (volume->flags & 8) == 0) { + PFFILE_FinalizeAllFiles(volume); + PFDIR_FinalizeAllDirs(volume); + clear_unmount(volume, 1); + PFDRV_ClearUnmountRequested(volume); + pf_vol_set.num_mounted_volumes--; + } + if (PFDRV_IsMountRequested(volume) != 0) { + error = clear_mount(volume); + if (error != 0) { return error; } + } + } else { + if (PFDRV_IsUnmountRequested(volume) != 0 && (volume->flags & 8) == 0) { + PFFILE_FinalizeAllFiles(volume); + PFDIR_FinalizeAllDirs(volume); + clear_unmount(volume, 1); + PFDRV_ClearUnmountRequested(volume); + pf_vol_set.num_mounted_volumes--; + } + if (PFDRV_IsMountRequested(volume) != 0) { PFDRV_ClearMountRequested(volume); } + } + return 0; +} +static inline s32 init_current_dir(PFVOL_VOLUME* volume) { + s32 error; + s32 context_id; + u32 index; + if ((volume->flags & 8) == 0) { return 9; } + PFSYS_GetCurrentContextID(&context_id); + volume->current_dir[0].stat |= 1; + if (check_context_registered(context_id)) { + volume->current_dir[1].stat |= 1; + volume->current_dir[1].context_id = context_id; + } + for (index = 0; index < 4; index++) { + error = PFENT_GetRootDir(volume, &volume->current_dir[index].directory); + if (error != 0) { return error; } + } + return 0; +} +static inline s32 finalize_volume(PFVOL_VOLUME* volume) { + s32 error = PFDRV_finalize(volume); + if (error != 0) { return error; } + return 0; +} +static inline s32 copy_codeset(const PFVOL_CHARCODE* code_set) { + pf_vol_set.codeset.oem2unicode = code_set->oem2unicode; + pf_vol_set.codeset.unicode2oem = code_set->unicode2oem; + pf_vol_set.codeset.oem_char_width = code_set->oem_char_width; + pf_vol_set.codeset.is_oem_mb_char = code_set->is_oem_mb_char; + pf_vol_set.codeset.unicode_char_width = code_set->unicode_char_width; + pf_vol_set.codeset.is_unicode_mb_char = code_set->is_unicode_mb_char; + return 0; +} s32 PFVOL_DoMountVolume(PFVOL_VOLUME* volume) { s32 error; s32 context_id; @@ -310,29 +402,7 @@ s32 PFVOL_DoMountVolume(PFVOL_VOLUME* volume) { return error; } volume->flags |= 8; - if ((volume->flags & 8) == 0) { - return 9; - } - error = PFSYS_GetCurrentContextID(&context_id); - volume->current_dir[0].stat |= 1; - context_registered = 0; - for (i = 0; i < 3; i++) { - pf_vol_set.context[i].stat &= 1; - if (pf_vol_set.context[i].stat != 0 && pf_vol_set.context[i].context_id == context_id) { - context_registered = 1; - break; - } - } - if (context_registered != 0) { - volume->current_dir[0].stat |= 1; - volume->current_dir[0].context_id = context_id; - } - for (i = 0; i < 4; i++) { - error = PFENT_GetRootDir(volume, &volume->current_dir[i].directory); - if (error != 0) { - break; - } - } + error = init_current_dir(volume); if (error == 0 && (volume->flags & 0x40) != 0) { error = PFDRV_format(volume, volume->format_options); if (error == 0 && (volume->flags & 0x80) == 0) { @@ -363,19 +433,7 @@ s32 PFVOL_p_unmount(PFVOL_VOLUME* volume, u32 mode) { } if (error == 0 || (mode & 1) != 0) { PFCACHE_FreeAllCaches(volume); - error = PFDRV_unmount(volume, mode); - if (error != 0) { - goto unmount_done; - } - if ((volume->flags & 8) != 0) { - volume->current_dir[0].stat = 0; - volume->current_dir[1].stat = 0; - volume->current_dir[2].stat = 0; - volume->current_dir[3].stat = 0; - } - error = 0; - volume->flags &= ~8; -unmount_done: + error = clear_unmount(volume, mode); if (error != 0 && prior_error == 0) { prior_error = error; } @@ -388,74 +446,46 @@ unmount_done: return prior_error; } +static inline s32 initialize_fat(PFVOL_VOLUME* volume) { + s32 error = PFFAT_InitFATRegion(volume); + if (error != 0) { return error; } + error = PFENT_MakeRootDir(volume); + if (error != 0) { return error; } + if (volume->bpb.fat_type != 2) { volume->fsi_flag &= ~3; } + if (volume->bpb.fat_type == 2 && (volume->fsi_flag & 2) != 0) { + error = PFFAT_RefreshFSINFO(volume); + if (error != 0) { return error; } + } + return 0; +} s32 PFVOL_p_format(PFVOL_VOLUME* volume, const u8* format_options) { s32 error; u32 free_clusters; - - if (volume == 0) { - return 10; - } - if ((volume->flags & 2) != 0) { - return 11; - } + if (volume == 0) { return 10; } + if ((volume->flags & 2) != 0) { return 11; } error = PFDRV_format(volume, format_options); - if (error != 0) { - return error; - } + if (error != 0) { return error; } if ((volume->flags & 8) != 0) { PFCACHE_FreeAllCaches(volume); error = PFVOL_p_unmount(volume, 0); - if (error != 0) { - return error; - } + if (error != 0) { return error; } } - if ((volume->flags & 8) == 0) { - error = PFVOL_DoMountVolume(volume); - if (error != 0) { - goto mount_done; - } - volume->fsi_flag &= ~7; - PFDRV_ClearUnmountRequested(volume); - pf_vol_set.num_mounted_volumes++; - } - PFDRV_ClearMountRequested(volume); - error = 0; -mount_done: - if (error == 0) { - if ((volume->flags & 2) != 0) { - error = 11; - } else if ((volume->flags & 0x80) == 0) { - error = PFFAT_InitFATRegion(volume); - if (error == 0) { - error = PFENT_MakeRootDir(volume); - if (error == 0) { - if (volume->bpb.fat_type != 2) { - volume->fsi_flag &= ~3; - } - if ((volume->bpb.fat_type != 2 || (volume->fsi_flag & 2) == 0) || - (error = PFFAT_RefreshFSINFO(volume)) == 0) { - error = 0; - } - } - } + error = clear_mount(volume); + if (error != 0) { return error; } + if ((volume->flags & 2) != 0) { return 11; } + if ((volume->flags & 0x80) != 0) { + if (volume->bpb.fat_type == 2 && (volume->fsi_flag & 2) != 0) { + volume->fsi_flag |= 4; + PFFAT_RefreshFSINFO(volume); } else { - if (volume->bpb.fat_type == 2 && (volume->fsi_flag & 2) != 0) { - volume->fsi_flag |= 4; - PFFAT_RefreshFSINFO(volume); - } else { - volume->fsi_flag &= ~4; - if (volume->bpb.fat_type != 2) { - volume->fsi_flag &= ~7; - } - error = PFFAT_CountFreeClusters(volume, &free_clusters); - if (error != 0) { - return error; - } - } - error = 0; + volume->fsi_flag &= ~4; + if (volume->bpb.fat_type != 2) { volume->fsi_flag &= ~3; } + error = PFFAT_CountFreeClusters(volume, &free_clusters); + if (error != 0) { return error; } } + return 0; } - return error; + return initialize_fat(volume); } s32 PFVOL_p_setvol(PFVOL_VOLUME* volume, const s8* label) { @@ -506,8 +536,6 @@ s32 PFVOL_p_setvol(PFVOL_VOLUME* volume, const s8* label) { PFENT_getcurrentDateTimeForEnt(&entry.modify_date, &entry.modify_time); error = PFENT_updateEntry(&entry, 0); } - } else { - return error; } return error; } @@ -556,7 +584,7 @@ s32 PFVOL_p_rmvvol(PFVOL_VOLUME* volume) { u32 logical_position; u32 entry_position; u32 processed; - u8 deleted = 0xE5; + u8 deleted = (u8)"\xE5"[0]; s32 error; error = PFENT_GetRootDir(volume, &root_entry); @@ -627,161 +655,22 @@ s32 PFVOL_InitModule(u32 config, void* user_data) { (volume)->current_dir[3].stat = 0; \ } while (0) -#define PFVOL_UPDATE_DRIVER_REQUESTS(volume, result) \ - do { \ - if (PFDRV_IsInserted(volume) == 0) { \ - if (PFDRV_IsUnmountRequested(volume) != 0 && ((volume)->flags & 8) == 0) { \ - PFFILE_FinalizeAllFiles(volume); \ - PFDIR_FinalizeAllDirs(volume); \ - (result) = PFDRV_unmount(volume, 1); \ - if ((result) == 0) { \ - if (((volume)->flags & 8) != 0) { \ - PFVOL_CLEAR_CURRENT_DIRS(volume); \ - } \ - (volume)->flags &= ~8; \ - } \ - PFDRV_ClearUnmountRequested(volume); \ - pf_vol_set.num_mounted_volumes--; \ - } \ - if (PFDRV_IsMountRequested(volume) != 0) { \ - PFDRV_ClearMountRequested(volume); \ - } \ - (result) = 0; \ - } else { \ - if (PFDRV_IsUnmountRequested(volume) != 0 && ((volume)->flags & 8) == 0) { \ - PFFILE_FinalizeAllFiles(volume); \ - PFDIR_FinalizeAllDirs(volume); \ - (result) = PFDRV_unmount(volume, 1); \ - if ((result) == 0) { \ - if (((volume)->flags & 8) != 0) { \ - PFVOL_CLEAR_CURRENT_DIRS(volume); \ - } \ - (volume)->flags &= ~8; \ - } \ - PFDRV_ClearUnmountRequested(volume); \ - pf_vol_set.num_mounted_volumes--; \ - } \ - if (PFDRV_IsMountRequested(volume) != 0) { \ - if (((volume)->flags & 8) == 0) { \ - (result) = PFVOL_DoMountVolume(volume); \ - if ((result) == 0) { \ - (volume)->fsi_flag &= ~7; \ - PFDRV_ClearUnmountRequested(volume); \ - pf_vol_set.num_mounted_volumes++; \ - } \ - } \ - if (((volume)->flags & 8) != 0 || (result) == 0) { \ - PFDRV_ClearMountRequested(volume); \ - (result) = 0; \ - } \ - } else { \ - (result) = 0; \ - } \ - } \ - } while (0) +#define PFVOL_UPDATE_DRIVER_REQUESTS(volume, result) ((result) = check_driver_requests(volume)) -s32 PFVOL_CheckForRead(PFVOL_VOLUME* volume) { - s32 error; - - if (PFDRV_IsUnmountRequested(volume) != 0 && (volume->flags & 8) == 0) { - PFFILE_FinalizeAllFiles(volume); - PFDIR_FinalizeAllDirs(volume); - error = PFDRV_unmount(volume, 1); - if (error == 0) { - if ((volume->flags & 8) != 0) { - PFVOL_CLEAR_CURRENT_DIRS(volume); - } - volume->flags &= ~8; - } - PFDRV_ClearUnmountRequested(volume); - pf_vol_set.num_mounted_volumes--; - } - if (PFDRV_IsInserted(volume) == 0) { - if (PFDRV_IsMountRequested(volume) != 0) { - PFDRV_ClearMountRequested(volume); - } - } else if (PFDRV_IsMountRequested(volume) != 0) { - if ((volume->flags & 8) == 0) { - error = PFVOL_DoMountVolume(volume); - if (error != 0) { - goto check_result; - } - volume->fsi_flag &= ~7; - PFDRV_ClearUnmountRequested(volume); - pf_vol_set.num_mounted_volumes++; - } - PFDRV_ClearMountRequested(volume); - error = 0; - } - error = 0; -check_result: - if (error == 0) { - error = (((volume->flags >> 3) & 1) - 1) & 9; - } - return error; +static inline s32 check_read(PFVOL_VOLUME* volume) { + s32 error = check_driver_requests(volume); + if (error != 0) { return error; } + return (volume->flags & 8) ? 0 : 9; } - -s32 PFVOL_CheckForWrite(PFVOL_VOLUME* volume) { - s32 error = 0; - - if (PFDRV_IsInserted(volume) == 0) { - if (PFDRV_IsUnmountRequested(volume) != 0 && (volume->flags & 8) == 0) { - PFFILE_FinalizeAllFiles(volume); - PFDIR_FinalizeAllDirs(volume); - error = PFDRV_unmount(volume, 1); - if (error == 0) { - if ((volume->flags & 8) != 0) { - PFVOL_CLEAR_CURRENT_DIRS(volume); - } - volume->flags &= ~8; - } - PFDRV_ClearUnmountRequested(volume); - pf_vol_set.num_mounted_volumes--; - } - if (PFDRV_IsMountRequested(volume) != 0) { - PFDRV_ClearMountRequested(volume); - } - } else { - if (PFDRV_IsUnmountRequested(volume) != 0 && (volume->flags & 8) == 0) { - PFFILE_FinalizeAllFiles(volume); - PFDIR_FinalizeAllDirs(volume); - error = PFDRV_unmount(volume, 1); - if (error == 0) { - if ((volume->flags & 8) != 0) { - PFVOL_CLEAR_CURRENT_DIRS(volume); - } - volume->flags &= ~8; - } - PFDRV_ClearUnmountRequested(volume); - pf_vol_set.num_mounted_volumes--; - } - if (PFDRV_IsMountRequested(volume) != 0) { - if ((volume->flags & 8) == 0) { - error = PFVOL_DoMountVolume(volume); - if (error == 0) { - volume->fsi_flag &= ~7; - PFDRV_ClearUnmountRequested(volume); - pf_vol_set.num_mounted_volumes++; - PFDRV_ClearMountRequested(volume); - error = 0; - } - } else { - PFDRV_ClearMountRequested(volume); - error = 0; - } - if (error != 0) { - return error; - } - } - } - if ((volume->flags & 8) == 0) { - return 9; - } - if ((volume->flags & 2) != 0) { - return 11; - } +static inline s32 check_write(PFVOL_VOLUME* volume) { + s32 error = check_driver_requests(volume); + if (error != 0) { return error; } + if ((volume->flags & 8) == 0) { return 9; } + if ((volume->flags & 2) != 0) { return 11; } return 0; } +s32 PFVOL_CheckForRead(PFVOL_VOLUME* volume) { return check_read(volume); } +s32 PFVOL_CheckForWrite(PFVOL_VOLUME* volume) { return check_write(volume); } s32 PFVOL_CheckCurrentDir(PFVOL_VOLUME* volume, u32 start_cluster) { s32 context_id; @@ -825,16 +714,7 @@ s32 PFVOL_SetCurrentDir(PFVOL_VOLUME* volume, PFVOL_DIR_ENTRY* directory) { } } if (current_dir_index == 4) { - if (((pf_vol_set.context[0].stat &= 1) != 0 && - context_id == pf_vol_set.context[0].context_id) || - ((pf_vol_set.context[1].stat &= 1) != 0 && - context_id == pf_vol_set.context[1].context_id) || - ((pf_vol_set.context[2].stat &= 1) != 0 && - context_id == pf_vol_set.context[2].context_id)) { - context_registered = 1; - } else { - context_registered = 0; - } + context_registered = check_context_registered(context_id); if (context_registered != 0) { for (current_dir_index = 1; current_dir_index < 4; current_dir_index++) { if ((volume->current_dir[current_dir_index].stat & 1) == 0) { @@ -868,16 +748,7 @@ s32 PFVOL_GetCurrentDir(PFVOL_VOLUME* volume, PFVOL_DIR_ENTRY* directory) { } } if (current_dir_index == 4) { - if (((pf_vol_set.context[0].stat &= 1) != 0 && - context_id == pf_vol_set.context[0].context_id) || - ((pf_vol_set.context[1].stat &= 1) != 0 && - context_id == pf_vol_set.context[1].context_id) || - ((pf_vol_set.context[2].stat &= 1) != 0 && - context_id == pf_vol_set.context[2].context_id)) { - context_registered = 1; - } else { - context_registered = 0; - } + context_registered = check_context_registered(context_id); if (context_registered != 0) { for (current_dir_index = 1; current_dir_index < 4; current_dir_index++) { if ((volume->current_dir[current_dir_index].stat & 1) == 0) { @@ -909,14 +780,7 @@ void PFVOL_SetCurrentVolume(PFVOL_VOLUME* volume) { } } if (context_index == 4) { - for (context_index = 1; context_index < 4; context_index++) { - pf_vol_set.context[context_index - 1].stat &= 1; - if (pf_vol_set.context[context_index - 1].stat != 0 && - pf_vol_set.context[context_index - 1].context_id == context_id) { - context_registered = 1; - break; - } - } + context_registered = check_context_registered(context_id); if (context_registered != 0) { for (context_index = 1; context_index < 4; context_index++) { if ((pf_vol_set.context_volume[context_index - 1].stat & 1) == 0) { @@ -947,14 +811,7 @@ PFVOL_VOLUME* PFVOL_GetCurrentVolume(void) { } } if (context_index == 4) { - for (context_index = 1; context_index < 4; context_index++) { - pf_vol_set.context[context_index - 1].stat &= 1; - if (pf_vol_set.context[context_index - 1].stat != 0 && - pf_vol_set.context[context_index - 1].context_id == context_id) { - context_registered = 1; - break; - } - } + context_registered = check_context_registered(context_id); if (context_registered != 0) { for (context_index = 1; context_index < 4; context_index++) { if ((pf_vol_set.context_volume[context_index - 1].stat & 1) == 0) { @@ -1012,66 +869,11 @@ void PFVOL_setvol(s8 drive, const s8* label) { volume = &pf_vol_set.volumes[volume_index]; } label_length = PFSTR_StrLen(label); - error = PFDRV_IsInserted(volume); + error = check_write(volume); if (error != 0) { - if (PFDRV_IsUnmountRequested(volume) != 0 && (volume->flags & 8) == 0) { - PFFILE_FinalizeAllFiles(volume); - PFDIR_FinalizeAllDirs(volume); - error = PFDRV_unmount(volume, 1); - if (error == 0) { - if ((volume->flags & 8) != 0) { - PFVOL_CLEAR_CURRENT_DIRS(volume); - } - volume->flags &= ~8; - } - PFDRV_ClearUnmountRequested(volume); - pf_vol_set.num_mounted_volumes--; - } - error = PFDRV_IsMountRequested(volume); - if (error != 0) { - if ((volume->flags & 8) == 0) { - error = PFVOL_DoMountVolume(volume); - if (error == 0) { - volume->fsi_flag &= ~7; - PFDRV_ClearUnmountRequested(volume); - pf_vol_set.num_mounted_volumes++; - PFDRV_ClearMountRequested(volume); - error = 0; - } - } else { - PFDRV_ClearMountRequested(volume); - error = 0; - } - if (error != 0) { - goto save_error; - } - } - } else { - if (PFDRV_IsUnmountRequested(volume) != 0 && (volume->flags & 8) == 0) { - PFFILE_FinalizeAllFiles(volume); - PFDIR_FinalizeAllDirs(volume); - error = PFDRV_unmount(volume, 1); - if (error == 0) { - if ((volume->flags & 8) != 0) { - PFVOL_CLEAR_CURRENT_DIRS(volume); - } - volume->flags &= ~8; - } - PFDRV_ClearUnmountRequested(volume); - pf_vol_set.num_mounted_volumes--; - } - if (PFDRV_IsMountRequested(volume) != 0) { - PFDRV_ClearMountRequested(volume); - } - } - error = 0; - if ((volume->flags & 8) == 0) { - error = 9; - } else if ((volume->flags & 2) != 0) { - error = 11; - } - if (error != 0) { - goto save_error; + pf_vol_set.last_error = error; + volume->last_error = error; + return; } PFSTR_ToUpperNStr(label, 11, upper_label); if (label_length < 11) { @@ -1079,26 +881,25 @@ void PFVOL_setvol(s8 drive, const s8* label) { } error = PFVOL_p_setvol(volume, upper_label); if (error != 0) { - goto save_error; + pf_vol_set.last_error = error; + volume->last_error = error; + return; } error = PFCACHE_FlushFATCache(volume); - if (error != 0) { - goto save_error; - } - error = PFCACHE_FlushDataCacheSpecific(volume, 0); - if (error != 0) { - goto save_error; + if (error != 0) { pf_vol_set.last_error = error; volume->last_error = error; } + else { + error = PFCACHE_FlushDataCacheSpecific(volume, 0); + if (error != 0) { pf_vol_set.last_error = error; volume->last_error = error; } } + if (error != 0) { return; } error = PFDRV_StoreVolumeLabelToBPB(volume, upper_label); if (error != 0) { - goto save_error; + pf_vol_set.last_error = error; + volume->last_error = error; + return; } return; -save_error: - pf_vol_set.last_error = error; - volume->last_error = error; - return; } s32 PFVOL_getvol(s8 drive, PFVOL_VOLUME_INFO* volume_info) { @@ -1117,74 +918,20 @@ s32 PFVOL_getvol(s8 drive, PFVOL_VOLUME_INFO* volume_info) { volume->last_error = 3; return 3; } - if (PFDRV_IsInserted(volume) != 0) { - if (PFDRV_IsUnmountRequested(volume) != 0 && (volume->flags & 8) == 0) { - PFFILE_FinalizeAllFiles(volume); - PFDIR_FinalizeAllDirs(volume); - error = PFDRV_unmount(volume, 1); - if (error == 0) { - if ((volume->flags & 8) != 0) { - PFVOL_CLEAR_CURRENT_DIRS(volume); - } - volume->flags &= ~8; - } - PFDRV_ClearUnmountRequested(volume); - pf_vol_set.num_mounted_volumes--; - } - error = PFDRV_IsMountRequested(volume); - if (error != 0) { - if ((volume->flags & 8) == 0) { - error = PFVOL_DoMountVolume(volume); - if (error == 0) { - volume->fsi_flag &= ~7; - PFDRV_ClearUnmountRequested(volume); - pf_vol_set.num_mounted_volumes++; - PFDRV_ClearMountRequested(volume); - error = 0; - } - } else { - PFDRV_ClearMountRequested(volume); - error = 0; - } - if (error != 0) { - goto save_error; - } - } - } else { - if (PFDRV_IsUnmountRequested(volume) != 0 && (volume->flags & 8) == 0) { - PFFILE_FinalizeAllFiles(volume); - PFDIR_FinalizeAllDirs(volume); - error = PFDRV_unmount(volume, 1); - if (error == 0) { - if ((volume->flags & 8) != 0) { - PFVOL_CLEAR_CURRENT_DIRS(volume); - } - volume->flags &= ~8; - } - PFDRV_ClearUnmountRequested(volume); - pf_vol_set.num_mounted_volumes--; - } - if (PFDRV_IsMountRequested(volume) != 0) { - PFDRV_ClearMountRequested(volume); - } - } - error = 0; - if ((volume->flags & 8) == 0) { - error = 9; - } + error = check_read(volume); if (error != 0) { - goto save_error; + pf_vol_set.last_error = error; + volume->last_error = error; + return error; } error = PFVOL_p_getvol(volume, volume_info); if (error != 0) { - goto save_error; + pf_vol_set.last_error = error; + volume->last_error = error; + return error; } return 0; -save_error: - pf_vol_set.last_error = error; - volume->last_error = error; - return error; } s32 PFVOL_rmvvol(s8 drive) { @@ -1203,84 +950,32 @@ s32 PFVOL_rmvvol(s8 drive) { volume->last_error = 3; return 3; } - if (PFDRV_IsInserted(volume) != 0) { - if (PFDRV_IsUnmountRequested(volume) != 0 && (volume->flags & 8) == 0) { - PFFILE_FinalizeAllFiles(volume); - PFDIR_FinalizeAllDirs(volume); - error = PFDRV_unmount(volume, 1); - if (error == 0) { - if ((volume->flags & 8) != 0) { - PFVOL_CLEAR_CURRENT_DIRS(volume); - } - volume->flags &= ~8; - } - PFDRV_ClearUnmountRequested(volume); - pf_vol_set.num_mounted_volumes--; - } - error = PFDRV_IsMountRequested(volume); - if (error != 0) { - if ((volume->flags & 8) == 0) { - error = PFVOL_DoMountVolume(volume); - if (error == 0) { - volume->fsi_flag &= ~7; - PFDRV_ClearUnmountRequested(volume); - pf_vol_set.num_mounted_volumes++; - PFDRV_ClearMountRequested(volume); - error = 0; - } - } else { - PFDRV_ClearMountRequested(volume); - error = 0; - } - if (error != 0) { - goto save_error; - } - } - } else { - if (PFDRV_IsUnmountRequested(volume) != 0 && (volume->flags & 8) == 0) { - PFFILE_FinalizeAllFiles(volume); - PFDIR_FinalizeAllDirs(volume); - error = PFDRV_unmount(volume, 1); - if (error == 0) { - if ((volume->flags & 8) != 0) { - PFVOL_CLEAR_CURRENT_DIRS(volume); - } - volume->flags &= ~8; - } - PFDRV_ClearUnmountRequested(volume); - pf_vol_set.num_mounted_volumes--; - } - if (PFDRV_IsMountRequested(volume) != 0) { - PFDRV_ClearMountRequested(volume); - } - } - error = 0; - if ((volume->flags & 8) == 0) { - error = 9; - } else if ((volume->flags & 2) != 0) { - error = 11; - } + error = check_write(volume); if (error != 0) { - goto save_error; + pf_vol_set.last_error = error; + volume->last_error = error; + return error; } error = PFDRV_StoreVolumeLabelToBPB(volume, (const s8*)"NO NAME "); if (error != 0) { - goto save_error; + pf_vol_set.last_error = error; + volume->last_error = error; + return error; } error = PFVOL_p_rmvvol(volume); if (error != 0) { - goto save_error; + pf_vol_set.last_error = error; + volume->last_error = error; + return error; } error = PFCACHE_FlushDataCacheSpecific(volume, 0); if (error != 0) { - goto save_error; + pf_vol_set.last_error = error; + volume->last_error = error; + return error; } - return 0; - -save_error: - pf_vol_set.last_error = error; - volume->last_error = error; return error; + } s32 PFVOL_getdev(s8 drive, u32* device_info) { @@ -1294,77 +989,23 @@ s32 PFVOL_getdev(s8 drive, u32* device_info) { } else { volume = &pf_vol_set.volumes[volume_index]; } - if (PFDRV_IsInserted(volume) != 0) { - if (PFDRV_IsUnmountRequested(volume) != 0 && (volume->flags & 8) == 0) { - PFFILE_FinalizeAllFiles(volume); - PFDIR_FinalizeAllDirs(volume); - error = PFDRV_unmount(volume, 1); - if (error == 0) { - if ((volume->flags & 8) != 0) { - PFVOL_CLEAR_CURRENT_DIRS(volume); - } - volume->flags &= ~8; - } - PFDRV_ClearUnmountRequested(volume); - pf_vol_set.num_mounted_volumes--; - } - error = PFDRV_IsMountRequested(volume); - if (error != 0) { - if ((volume->flags & 8) == 0) { - error = PFVOL_DoMountVolume(volume); - if (error == 0) { - volume->fsi_flag &= ~7; - PFDRV_ClearUnmountRequested(volume); - pf_vol_set.num_mounted_volumes++; - PFDRV_ClearMountRequested(volume); - error = 0; - } - } else { - PFDRV_ClearMountRequested(volume); - error = 0; - } - if (error != 0) { - goto save_error; - } - } - } else { - if (PFDRV_IsUnmountRequested(volume) != 0 && (volume->flags & 8) == 0) { - PFFILE_FinalizeAllFiles(volume); - PFDIR_FinalizeAllDirs(volume); - error = PFDRV_unmount(volume, 1); - if (error == 0) { - if ((volume->flags & 8) != 0) { - PFVOL_CLEAR_CURRENT_DIRS(volume); - } - volume->flags &= ~8; - } - PFDRV_ClearUnmountRequested(volume); - pf_vol_set.num_mounted_volumes--; - } - if (PFDRV_IsMountRequested(volume) != 0) { - PFDRV_ClearMountRequested(volume); - } - } - error = 0; - if ((volume->flags & 8) == 0) { - error = 9; - } + error = check_read(volume); if (error != 0) { - goto save_error; + pf_vol_set.last_error = error; + volume->last_error = error; + return error; } device_info[2] = volume->bpb.bytes_per_sector; device_info[3] = volume->bpb.sectors_per_cluster; device_info[0] = volume->bpb.num_clusters; error = PFFAT_CountFreeClusters(volume, &device_info[1]); if (error != 0) { - goto save_error; + pf_vol_set.last_error = error; + volume->last_error = error; + return error; } return 0; -save_error: - pf_vol_set.last_error = error; - volume->last_error = error; - return error; } s32 PFVOL_buffering(s8 drive, u32 mode) { @@ -1378,183 +1019,91 @@ s32 PFVOL_buffering(s8 drive, u32 mode) { } else { volume = &pf_vol_set.volumes[volume_index]; } - if (PFDRV_IsInserted(volume) != 0) { - if (PFDRV_IsUnmountRequested(volume) != 0 && (volume->flags & 8) == 0) { - PFFILE_FinalizeAllFiles(volume); - PFDIR_FinalizeAllDirs(volume); - error = PFDRV_unmount(volume, 1); - if (error == 0) { - if ((volume->flags & 8) != 0) { - PFVOL_CLEAR_CURRENT_DIRS(volume); - } - volume->flags &= ~8; - } - PFDRV_ClearUnmountRequested(volume); - pf_vol_set.num_mounted_volumes--; - } - error = PFDRV_IsMountRequested(volume); - if (error != 0) { - if ((volume->flags & 8) == 0) { - error = PFVOL_DoMountVolume(volume); - if (error == 0) { - volume->fsi_flag &= ~7; - PFDRV_ClearUnmountRequested(volume); - pf_vol_set.num_mounted_volumes++; - PFDRV_ClearMountRequested(volume); - error = 0; - } - } else { - PFDRV_ClearMountRequested(volume); - error = 0; - } - if (error != 0) { - goto save_error; - } - } + error = check_write(volume); + if (error != 0) { pf_vol_set.last_error = error; volume->last_error = error; return error; } + if ((mode & 4) != 0 && (volume->cache_flags & 1) == 0) { + error = PFCACHE_FlushFATCache(volume); + if (error != 0) { pf_vol_set.last_error = error; volume->last_error = error; return error; } + } + if (mode & 1) { + error = PFCACHE_SetWriteThroughMode(volume); + if (error != 0) { pf_vol_set.last_error = error; volume->last_error = error; return error; } } else { - if (PFDRV_IsUnmountRequested(volume) != 0 && (volume->flags & 8) == 0) { - PFFILE_FinalizeAllFiles(volume); - PFDIR_FinalizeAllDirs(volume); - error = PFDRV_unmount(volume, 1); - if (error == 0) { - if ((volume->flags & 8) != 0) { - PFVOL_CLEAR_CURRENT_DIRS(volume); - } - volume->flags &= ~8; - } - PFDRV_ClearUnmountRequested(volume); - pf_vol_set.num_mounted_volumes--; - } - if (PFDRV_IsMountRequested(volume) != 0) { - PFDRV_ClearMountRequested(volume); - } - } - error = 0; - if ((volume->flags & 8) == 0) { - error = 9; - } else if ((volume->flags & 2) != 0) { - error = 11; - } - if (error == 0) { - if ((mode & 4) == 0 || (volume->cache_flags & 1) != 0) { - error = 0; - } else { - error = PFCACHE_FlushFATCache(volume); - if (error != 0) { - goto save_error; - } - } - if (error == 0) { - if ((mode & 1) == 0) { - error = PFCACHE_SetWriteBackMode(volume); - } else { - error = PFCACHE_SetWriteThroughMode(volume); - } - if (error != 0) { - goto save_error; - } - if ((mode & 4) != 0) { - volume->cache_flags |= 4; - } else { - volume->cache_flags &= ~4; - } - if ((mode & 2) != 0) { - volume->cache_flags |= 2; - } else { - volume->cache_flags &= ~2; - } - } - } else { - goto save_error; + error = PFCACHE_SetWriteBackMode(volume); + if (error != 0) { pf_vol_set.last_error = error; volume->last_error = error; return error; } } + if (mode & 2) { volume->cache_flags |= 2; } else { volume->cache_flags &= ~2; } + if (mode & 4) { volume->cache_flags |= 4; } else { volume->cache_flags &= ~4; } return 0; +} -save_error: - pf_vol_set.last_error = error; - volume->last_error = error; - return error; +static inline s32 initialize_volume_cache(PFVOL_VOLUME* volume, PFVOL_DRIVER* driver) { + s32 error = PFDRV_init(volume); + if (error != 0) { return error; } + PFCACHE_SetCache(volume, driver->cache->pages, driver->cache->buffers, + driver->cache->num_fat_pages, driver->cache->num_data_pages); + PFCACHE_SetFATBufferSize(volume, driver->cache->num_fat_buf_size); + PFCACHE_SetDataBufferSize(volume, driver->cache->num_data_buf_size); + return 0; +} + +static inline s32 attach_mount(PFVOL_VOLUME* volume, PFVOL_DRIVER* driver) { + s32 error = clear_mount(volume); + if (error != 0) { pf_vol_set.last_error = error; volume->last_error = error; return error; } + driver->stat |= 2; + return 0; } s32 PFVOL_attach(PFVOL_DRIVER* driver, s32 notify_command, void* callback_data) { - PFVOL_CACHE_SETTING* cache; - PFVOL_VOLUME* volume; s32 volume_index; + PFVOL_VOLUME* volume; + PFVOL_VOLUME* candidate; s32 error; - cache = driver->cache; - if (cache->num_fat_buf_size == 0) { - cache->num_fat_buf_size = 1; + if (driver->cache->num_fat_buf_size == 0) { + driver->cache->num_fat_buf_size = 1; } - if (cache->num_data_buf_size == 0) { - cache->num_data_buf_size = 1; + if (driver->cache->num_data_buf_size == 0) { + driver->cache->num_data_buf_size = 1; } - driver->drive = 0; driver->stat = 0; + driver->drive = 0; if (notify_command != 0 && fa_nanddrv_NotifyNANDFile(notify_command, callback_data) != 0) { pf_vol_set.last_error = 17; return 17; } - volume_index = 0; - volume = pf_vol_set.volumes; - while (volume_index < 26) { - if ((volume->flags & 1) == 0) { - break; - } - volume++; - volume_index++; - if ((volume->flags & 1) == 0) { - break; - } - volume++; - volume_index++; + candidate = pf_vol_set.volumes; + for (volume_index = 0; volume_index < 26; volume_index++) { + volume = candidate; + if ((candidate->flags & 1) == 0) { break; } + candidate++; } if (volume_index < 0 || volume_index >= 26 || pf_vol_set.num_attached_drives < 0 || pf_vol_set.num_attached_drives >= 26) { pf_vol_set.last_error = 4; return 4; } - volume->last_error = -1; - volume->last_driver_error = -1; + volume->num_free_clusters = -1; + volume->last_free_cluster = -1; pf_memset(volume, 0, 0x1FAC); volume->driver_partition = driver->partition; - volume->options = volume_index + 'A'; + volume->drv_char = (s16)volume_index + 'A'; volume->file_buffer_count = 1; volume->current_file_buffer = &volume->file_buffers; - error = PFDRV_init(volume); - if (error == 0) { - PFCACHE_SetCache(volume, cache->pages, cache->buffers, cache->num_fat_pages, - cache->num_data_pages); - PFCACHE_SetFATBufferSize(volume, cache->num_fat_buf_size); - PFCACHE_SetDataBufferSize(volume, cache->num_data_buf_size); - } + error = initialize_volume_cache(volume, driver); if (error != 0) { pf_vol_set.last_error = error; return error; } volume->flags |= 1; driver->stat |= 1; - volume->options = volume_index + 'A'; + volume->drv_char = volume_index + 'A'; driver->drive = volume_index + 'A'; pf_vol_set.num_attached_drives++; - if (PFDRV_IsInserted(volume) == 0) { - return 0; + if (PFDRV_IsInserted(volume) != 0) { + driver->stat |= 0x10; + attach_mount(volume, driver); } - driver->stat |= 0x10; - if ((volume->flags & 8) == 0) { - error = PFVOL_DoMountVolume(volume); - if (error == 0) { - volume->fsi_flag &= ~7; - PFDRV_ClearUnmountRequested(volume); - pf_vol_set.num_mounted_volumes++; - } - } - if (error == 0) { - PFDRV_ClearMountRequested(volume); - driver->stat |= 2; - return 0; - } - pf_vol_set.last_error = error; - volume->last_error = error; return 0; } @@ -1570,31 +1119,31 @@ s32 PFVOL_detach(s8 drive) { volume = &pf_vol_set.volumes[volume_index]; } if ((volume->flags & 8) != 0) { - error = 10; - pf_vol_set.last_error = error; - volume->last_error = error; - } else if ((volume->flags & 1) == 0) { - error = 10; - pf_vol_set.last_error = error; - volume->last_error = error; - } else { - error = PFDRV_finalize(volume); - if (error == 0) { - volume->flags &= ~1; - pf_vol_set.num_attached_drives--; - } else { - pf_vol_set.last_error = error; - volume->last_error = error; - } + pf_vol_set.last_error = 10; + volume->last_error = 10; + return 10; } - return error; + if ((volume->flags & 1) == 0) { + pf_vol_set.last_error = 10; + volume->last_error = 10; + return 10; + } + error = finalize_volume(volume); + if (error != 0) { + pf_vol_set.last_error = error; + volume->last_error = error; + return error; + } + volume->flags &= ~1; + pf_vol_set.num_attached_drives--; + return 0; } s32 PFVOL_format(s8 drive, const u8* format_options) { s16 volume_index; PFVOL_VOLUME* volume; s32 error; - u8 mount_requested; + u8 mount_requested = 0; volume_index = pf_toupper(drive) - 'A'; if (volume_index < 0 || volume_index >= 26) { @@ -1604,97 +1153,36 @@ s32 PFVOL_format(s8 drive, const u8* format_options) { } if ((volume->flags & 1) == 0) { error = 10; - goto save_error; + pf_vol_set.last_error = error; + volume->last_error = error; + return error; } if (PFDRV_IsInserted(volume) == 0) { error = 9; - goto save_error; - } - mount_requested = PFDRV_IsMountRequested(volume) != 0; - if (mount_requested != 0) { - PFDRV_ClearMountRequested(volume); - } - if (PFDRV_IsInserted(volume) != 0) { - if (PFDRV_IsUnmountRequested(volume) != 0 && (volume->flags & 8) == 0) { - PFFILE_FinalizeAllFiles(volume); - PFDIR_FinalizeAllDirs(volume); - error = PFDRV_unmount(volume, 1); - if (error == 0) { - if ((volume->flags & 8) != 0) { - PFVOL_CLEAR_CURRENT_DIRS(volume); - } - volume->flags &= ~8; - } - PFDRV_ClearUnmountRequested(volume); - pf_vol_set.num_mounted_volumes--; - } - error = PFDRV_IsMountRequested(volume); - if (error != 0) { - if ((volume->flags & 8) == 0) { - error = PFVOL_DoMountVolume(volume); - if (error == 0) { - volume->fsi_flag &= ~7; - PFDRV_ClearUnmountRequested(volume); - pf_vol_set.num_mounted_volumes++; - PFDRV_ClearMountRequested(volume); - error = 0; - } - } else { - PFDRV_ClearMountRequested(volume); - error = 0; - } - if (error != 0) { - goto after_mount; - } - } - } else { - if (PFDRV_IsUnmountRequested(volume) != 0 && (volume->flags & 8) == 0) { - PFFILE_FinalizeAllFiles(volume); - PFDIR_FinalizeAllDirs(volume); - error = PFDRV_unmount(volume, 1); - if (error == 0) { - if ((volume->flags & 8) != 0) { - PFVOL_CLEAR_CURRENT_DIRS(volume); - } - volume->flags &= ~8; - } - PFDRV_ClearUnmountRequested(volume); - pf_vol_set.num_mounted_volumes--; - } - if (PFDRV_IsMountRequested(volume) != 0) { - PFDRV_ClearMountRequested(volume); - } - } - error = 0; -after_mount: - if (error == 0) { - if (volume->num_open_files != 0) { - PFFILE_FinalizeAllFiles(volume); - PFCACHE_FreeAllCaches(volume); - } - if (volume->num_open_dirs != 0) { - PFDIR_FinalizeAllDirs(volume); - } - error = PFVOL_p_format(volume, format_options); - if (error == 0) { - volume->fsi_flag |= 4; - volume->num_free_clusters = volume->bpb.num_clusters - (volume->bpb.fat_type == 2); - return 0; - } - } - if (mount_requested) { - volume->flags |= 0x10; - } - if (error != 0) { pf_vol_set.last_error = error; volume->last_error = error; + return error; } - return error; - -save_error: - pf_vol_set.last_error = error; - volume->last_error = error; - return error; + if (PFDRV_IsMountRequested(volume) != 0) { + PFDRV_ClearMountRequested(volume); + mount_requested = 1; + } + error = check_driver_requests(volume); + if (error != 0) { + if (mount_requested) { volume->flags |= 0x10; } + pf_vol_set.last_error = error; volume->last_error = error; return error; + } + if (volume->num_open_files != 0) { PFFILE_FinalizeAllFiles(volume); PFCACHE_FreeAllCaches(volume); } + if (volume->num_open_dirs != 0) { PFDIR_FinalizeAllDirs(volume); } + error = PFVOL_p_format(volume, format_options); + if (error != 0) { + if (mount_requested) { volume->flags |= 0x10; } + pf_vol_set.last_error = error; volume->last_error = error; return error; + } + volume->fsi_flag |= 4; + if (volume->bpb.fat_type == 2) { volume->num_free_clusters = volume->bpb.num_clusters - 1; } + else { volume->num_free_clusters = volume->bpb.num_clusters; } + return 0; } s32 PFVOL_mount(s8 drive) { @@ -1708,27 +1196,13 @@ s32 PFVOL_mount(s8 drive) { } else { volume = &pf_vol_set.volumes[volume_index]; } - PFVOL_UPDATE_DRIVER_REQUESTS(volume, error); - if (error == 0) { - if (PFDRV_IsInserted(volume) != 0) { - if ((volume->flags & 8) == 0) { - error = PFVOL_DoMountVolume(volume); - if (error == 0) { - volume->fsi_flag &= ~7; - PFDRV_ClearUnmountRequested(volume); - pf_vol_set.num_mounted_volumes++; - PFDRV_ClearMountRequested(volume); - } - } - } else { - error = 9; - } - } - if (error != 0) { - pf_vol_set.last_error = error; - volume->last_error = error; - } - return error; + error = check_driver_requests(volume); + if (error != 0) { pf_vol_set.last_error = error; volume->last_error = error; return error; } + if (PFDRV_IsInserted(volume) == 0) { pf_vol_set.last_error = 9; volume->last_error = 9; return 9; } + if ((volume->flags & 8) != 0) { return 0; } + error = clear_mount(volume); + if (error != 0) { pf_vol_set.last_error = error; volume->last_error = error; return error; } + return 0; } s32 PFVOL_unmount(s8 drive, u32 mode) { @@ -1779,7 +1253,7 @@ s32 PFVOL_unmount(s8 drive, u32 mode) { return result; } -s32 PFVOL_setupfsi(s8 drive, u32 mode) { +s32 PFVOL_setupfsi(s8 drive, s32 mode) { s16 volume_index; PFVOL_VOLUME* volume; s32 error; @@ -1792,34 +1266,16 @@ s32 PFVOL_setupfsi(s8 drive, u32 mode) { } else { volume = &pf_vol_set.volumes[volume_index]; } - PFVOL_UPDATE_DRIVER_REQUESTS(volume, error); - if (error == 0 && (volume->flags & 8) == 0) { - error = 9; - } - if (error == 0) { - if (volume->bpb.fat_type == 2) { - if ((mode & 1) == 1) { - volume->fsi_flag |= 5; - } else if ((mode & 2) == 2) { - volume->fsi_flag &= ~1; - } - if ((mode & 4) == 4) { - volume->fsi_flag |= 2; - if ((volume->fsi_flag & 4) == 0) { - PFFAT_CountFreeClusters(volume, &free_clusters); - } - } else if ((mode & 8) == 8) { - volume->fsi_flag &= ~2; - } - } else { - error = 12; - } - } - if (error != 0) { - pf_vol_set.last_error = error; - volume->last_error = error; - } - return error; + error = check_read(volume); + if (error != 0) { pf_vol_set.last_error = error; volume->last_error = error; return error; } + if (volume->bpb.fat_type != 2) { pf_vol_set.last_error = 12; volume->last_error = 12; return 12; } + if ((mode & 1) == 1) { volume->fsi_flag |= 5; } + else if ((mode & 2) == 2) { volume->fsi_flag &= ~1; } + if ((mode & 4) == 4) { + volume->fsi_flag |= 2; + if ((volume->fsi_flag & 4) == 0) { PFFAT_CountFreeClusters(volume, &free_clusters); } + } else if ((mode & 8) == 8) { volume->fsi_flag &= ~2; } + return 0; } s32 PFVOL_setclstlink(s8 drive, s32 mode, PFVOL_CLUSTER_SETTING* setting) { @@ -1838,16 +1294,10 @@ s32 PFVOL_setclstlink(s8 drive, s32 mode, PFVOL_CLUSTER_SETTING* setting) { volume->last_error = 19; return 19; } - PFVOL_UPDATE_DRIVER_REQUESTS(volume, error); - if (error == 0) { - if ((volume->flags & 8) == 0) { - error = 9; - } else if ((volume->flags & 2) != 0) { - error = 11; - } - } - if (error == 0) { - if (mode == 1) { + error = check_read(volume); + if (error != 0) { pf_vol_set.last_error = error; volume->last_error = error; return error; } + { + if ((u32)mode == 1) { if (setting->buffer == 0) { pf_vol_set.last_error = 10; volume->last_error = 10; @@ -1866,12 +1316,7 @@ s32 PFVOL_setclstlink(s8 drive, s32 mode, PFVOL_CLUSTER_SETTING* setting) { volume->cluster_link_capacity = 0; } } - if (error != 0) { - pf_vol_set.last_error = error; - volume->last_error = error; - return error; - } - return 0; + return error; } s32 PFVOL_sync(s8 drive, s32 mode) { @@ -1879,7 +1324,7 @@ s32 PFVOL_sync(s8 drive, s32 mode) { PFVOL_VOLUME* volume; PFVOL_OPEN_FILE* file; s32 error; - u32 file_index; + u16 file_index; volume_index = pf_toupper(drive) - 'A'; if (volume_index < 0 || volume_index >= 26) { @@ -1887,40 +1332,19 @@ s32 PFVOL_sync(s8 drive, s32 mode) { } else { volume = &pf_vol_set.volumes[volume_index]; } - PFVOL_UPDATE_DRIVER_REQUESTS(volume, error); - if (error == 0) { - if ((volume->flags & 8) == 0) { - error = 9; - } else if ((volume->flags & 2) != 0) { - error = 11; + error = check_write(volume); + if (error != 0) { pf_vol_set.last_error = error; volume->last_error = error; return error; } + error = PFCACHE_FlushFATCache(volume); + if (error != 0) { pf_vol_set.last_error = error; volume->last_error = error; return error; } + for (file_index = 0; file_index < 5; file_index++) { + if ((volume->files[file_index].stat & 1) != 0) { + error = PFENT_updateEntry(&volume->files[file_index].stream->directory_entry, 1); + if (error != 0) { pf_vol_set.last_error = error; volume->last_error = error; return error; } } } - if (error == 0) { - error = PFCACHE_FlushFATCache(volume); - if (error == 0) { - file_index = 0; - do { - file = &volume->files[file_index]; - if ((file->stat & 1) != 0) { - error = PFENT_updateEntry(&file->stream->directory_entry, 1); - if (error != 0) { - pf_vol_set.last_error = error; - volume->last_error = error; - return error; - } - } - file_index++; - } while (file_index < 5); - error = PFCACHE_FlushDataCache(volume); - if (error == 0 && mode == 1) { - PFCACHE_FreeAllCaches(volume); - } - } - } - if (error != 0) { - pf_vol_set.last_error = error; - volume->last_error = error; - } + error = PFCACHE_FlushDataCache(volume); + if (error != 0) { pf_vol_set.last_error = error; volume->last_error = error; return error; } + if ((u32)mode == 1) { PFCACHE_FreeAllCaches(volume); } return error; } @@ -1935,16 +1359,13 @@ void PFVOL_settailbuf(s8 drive, u32 count, void* buffer) { } else { volume = &pf_vol_set.volumes[volume_index]; } - PFVOL_UPDATE_DRIVER_REQUESTS(volume, error); - if (error == 0 && (volume->flags & 8) == 0) { - error = 9; - } - if (error == 0) { - volume->file_buffer_count = count; - volume->current_file_buffer = buffer; - } else { + error = check_read(volume); + if (error != 0) { pf_vol_set.last_error = error; volume->last_error = error; + } else { + volume->file_buffer_count = count; + volume->current_file_buffer = buffer; } } @@ -1959,8 +1380,7 @@ s32 PFVOL_setvolcfg(s8 drive, u32* config) { } else if ((config[0] & 0x20000) != 0) { pf_vol_set.config &= ~0x10000; } - return 0; - } + } else { volume_index = pf_toupper(drive) - 'A'; if (volume_index < 0 || volume_index >= 26) { volume = 0; @@ -1973,7 +1393,7 @@ s32 PFVOL_setvolcfg(s8 drive, u32* config) { if (config[3] == 0) { config[3] = 1; } - PFVOL_UPDATE_DRIVER_REQUESTS(volume, error); + check_driver_requests(volume); if (volume->num_open_files != 0 || volume->num_open_dirs != 0) { pf_vol_set.last_error = 19; volume->last_error = 19; @@ -2001,6 +1421,7 @@ s32 PFVOL_setvolcfg(s8 drive, u32* config) { } else if ((config[1] & 2) != 0) { volume->reserved_1f88 &= ~1; } + } return 0; } @@ -2018,95 +1439,78 @@ s32 PFVOL_getvolcfg(s8 drive, u32* config) { config[1] = 0; config[2] = 0; config[3] = 0; - return 0; - } + } else { volume_index = pf_toupper(drive) - 'A'; if (volume_index < 0 || volume_index >= 26) { volume = 0; } else { volume = &pf_vol_set.volumes[volume_index]; } - PFVOL_UPDATE_DRIVER_REQUESTS(volume, error); + check_driver_requests(volume); config[0] = 0; config[1] = volume->reserved_1f88; - if ((volume->reserved_1f88 & 1) == 0) { + if ((volume->reserved_1f88 & 1) != 1) { config[1] |= 2; } config[2] = volume->num_fat_buf_size; config[3] = volume->num_data_buf_size; + } return 0; } s32 PFVOL_setcode(const PFVOL_CHARCODE* code_set) { - if (pf_vol_set.num_attached_drives > 0) { + if (pf_vol_set.num_mounted_volumes > 0) { pf_vol_set.last_error = 36; return 36; } - pf_vol_set.codeset.oem2unicode = code_set->oem2unicode; - pf_vol_set.codeset.unicode2oem = code_set->unicode2oem; - pf_vol_set.codeset.oem_char_width = code_set->oem_char_width; - pf_vol_set.codeset.is_oem_mb_char = code_set->is_oem_mb_char; - pf_vol_set.codeset.unicode_char_width = code_set->unicode_char_width; - pf_vol_set.codeset.is_unicode_mb_char = code_set->is_unicode_mb_char; - return 0; + return copy_codeset(code_set); } s32 PFVOL_regctx(void) { - PFVOL_CONTEXT* context; s32 context_id; - s32 error; + s32 free_context_index; u32 context_index; - u32 free_context_index = 0; - + s32 error; error = PFSYS_GetCurrentContextID(&context_id); - if (error != 0) { - pf_vol_set.last_error = 26; - return 26; + if (error != 0) { pf_vol_set.last_error = 26; return 26; } + free_context_index = 0; + for (context_index = 1; context_index < 4; context_index++) { + u32 stat; + stat = pf_vol_set.context[context_index - 1].stat & 1; + if (stat != 0 && pf_vol_set.context[context_index - 1].context_id == context_id) { break; } + if (stat == 0 && free_context_index == 0) { free_context_index = context_index; } } - context = pf_vol_set.context; - for (context_index = 1; context_index <= 3; context_index++) { - if ((context->stat & 1) != 0) { - if (context->context_id == context_id) { - return 0; - } - } else if (free_context_index == 0) { - free_context_index = context_index; - } - context++; + if (context_index == 4) { + if (free_context_index != 0) { + pf_vol_set.context[free_context_index - 1].stat = 1; + pf_vol_set.context[free_context_index - 1].context_id = context_id; + } else { pf_vol_set.last_error = 26; error = 26; } } - if (free_context_index == 0) { - pf_vol_set.last_error = 26; - return 26; - } - context = &pf_vol_set.context[free_context_index - 1]; - context->stat = 1; - context->context_id = context_id; - return 0; + return error; } s32 PFVOL_unregctx(void) { s32 context_id; s32 error; u32 context_index; - u32 volume_index; u32 current_dir_index; + u32 volume_index; u32 current_volume_index; - s32 found = 0; error = PFSYS_GetCurrentContextID(&context_id); if (error != 0) { pf_vol_set.last_error = 26; return 26; } - for (context_index = 0; context_index < 3; context_index++) { - if ((pf_vol_set.context[context_index].stat & 1) != 0 && - pf_vol_set.context[context_index].context_id == context_id) { - pf_vol_set.context[context_index].stat &= ~1; - found = 1; - for (current_volume_index = 0; current_volume_index < 3; current_volume_index++) { - if ((pf_vol_set.context_volume[current_volume_index].stat & 1) != 0 && - pf_vol_set.context_volume[current_volume_index].context_id == context_id) { - pf_vol_set.context_volume[current_volume_index].stat &= ~1; + for (context_index = 1; context_index < 4; context_index++) { + if ((pf_vol_set.context[context_index - 1].stat & 1) != 0 && + pf_vol_set.context[context_index - 1].context_id == context_id) { + pf_vol_set.context[context_index - 1].stat &= ~1; + for (current_volume_index = 1; current_volume_index < 4; current_volume_index++) { + if ((pf_vol_set.context_volume[current_volume_index - 1].stat & 1) != 0 && + pf_vol_set.context_volume[current_volume_index - 1].context_id == context_id) { + pf_vol_set.context_volume[current_volume_index - 1].stat &= ~1; + break; } } for (volume_index = 0; volume_index < 26; volume_index++) { @@ -2121,11 +1525,11 @@ s32 PFVOL_unregctx(void) { break; } } - if (found == 0) { + if (context_index == 4) { pf_vol_set.last_error = 26; - return 26; + error = 26; } - return 0; + return error; } s32 PFVOL_setencode(u32 mode) { diff --git a/tools/decomp-assist/attempts/fa-dir.md b/tools/decomp-assist/attempts/fa-dir.md new file mode 100644 index 00000000..5523a591 --- /dev/null +++ b/tools/decomp-assist/attempts/fa-dir.md @@ -0,0 +1,16 @@ +# fa/pf_dir + +Baseline 22/38 exact. VF source diff inspected before editing. Sources already contained all 38 bodies; several large bodies lacked fa-specific operations. + +Remaining attempts, instruction counts (source/target), retained source in the final commit: +- GetSDD: original flag condition 67/69; status-word negation 68/69; retain flag prerequisite 69/69 with two increment-order differences; update pointer in body, measured below. +- opendir: explicit += count update 39/39 two register differences; invert successful branch 39/39 nine differences; inline status helper 39/39 nine differences. Restored first form. +- p_fsnext: local pattern, loop and global setting correction 186/193; signed indexed file scan 191/193; while loop 193/193, 39 differences (loop-limit hoisting). +- p_rmdir: opened-directory helper 165/202; normalize FreeChain result 169/202; add path split and iterator/hint copies 186/202; restore saved iterator/hint before removal 203/202. Stack/copy scheduling remains. +- p_fsexec_remove: opened-directory helper 159/176; initialize a separate iterator 153/176; delay cluster load 151/176; reset supplied iterator and restore its saved state, with file size bound 178/176. Stack/control scheduling remains. +- p_mkdir: port API calls and fa FFD/STR/hint types 254/562; order iterator workspace and split name-length errors 256/562; order hint before position 256/562. Missing original inline entry initialization/allocation operations remain. +- p_rename: unconditional filename validation 190/625; swapped iterator workspaces and corrected end sentinel 191/625; explicit destination code-mode local and restored iterator order 191/625. Original long-name allocation/update operations remain missing. +- p_move: unconditional filename validation 211/631; use opened-directory helper and workspace order 215/631; explicit code-mode local and restored iterator order 215/631. Original cross-directory entry allocation/update remains missing. +- p_fsexec: correct flags and use entry start-cluster storage 206/280; move iterator setup before start-position selection 206/280; reorder workspaces and compare status explicitly 207/280. Original name-search/control paths still differ. + +New exact functions include directory allocation, open-by-path, chmod, stat, fsexec chmod and context changes. No shared headers changed. diff --git a/tools/decomp-assist/attempts/fa-fat.md b/tools/decomp-assist/attempts/fa-fat.md new file mode 100644 index 00000000..ab999ea4 --- /dev/null +++ b/tools/decomp-assist/attempts/fa-fat.md @@ -0,0 +1,13 @@ +# fa/pf_fat + +Baseline 22/35 exact. Diff against Matching vf source inspected first. Final source preserves all 35 function bodies and changes no headers. + +Remaining attempts, source/target instruction counts: +- DoAllocateChain: page-before-sector initialization (216/216, five register differences); initialize flush error after loading page (same); reorder page/sector locals (same); direct page buffer alias (217/216). Restored original 216/216. +- GetClusterAllocated: reorder hint/fat type loads (69/70, 45 differences); reorder first/last cluster locals (same); isolate range check in traversal helper (70/70, eight differences); restore original load ordering (70/70, six differences); reorder hint/fat type declarations (unchanged). Six pointer register differences remain. +- GetSector: preload FAT type before root check (88/88, seven register differences); swap quotient/remainder declaration order (same); reverse addition operand order (same); spell rounded sector count directly (same). Division span and quotient registers remain swapped. +- GetSectorSpecified: immediate error return (17/17, four differences, compiler uses arithmetic normalization); explicit success/error scopes (16/17); inline wrapper boundary (20/17). Restored original 16/17, one conditional branch expansion remains. +- FreeChain: remove page alias (260/254); reorder flush scan locals (256/254, 113 differences); cache FAT type before the loop (different register/control stream). Restored 256/254. Status lifetime and flush registers remain different. +- RefreshFSINFO: reorder endian expression (88/93); explicit FSINFO buffer local (87/93); shared allocation-failure exit (93/93, fourteen differences). Restored endian expression and retained common return; remaining allocation status branch and endian store scheduling differ. + +Other experiments yielded exact read/write cluster functions, cluster-link traversal, allocated-cluster counting and specified cluster traversal. Invalid intermediate code with an uninitialized alias was corrected immediately and is absent from the final source. diff --git a/tools/decomp-assist/attempts/fa-file.md b/tools/decomp-assist/attempts/fa-file.md new file mode 100644 index 00000000..aeb8375c --- /dev/null +++ b/tools/decomp-assist/attempts/fa-file.md @@ -0,0 +1,36 @@ +# fa/pf_file + +Baseline 32/45 exact, 10384/17820 code bytes. Compared the existing fa source against the Matching vf counterpart before changing it. Final source defines the existing PF_FA_STR_LAYOUT header guard locally; no shared headers changed. + +Remaining attempts, source/target instruction counts: +- GetSFD: corrected free-slot predicate and indexed return (113/113, 28 differences); indexed entry comparisons (113/113, 18 differences); move index initialization before pointer initialization and reverse loop updates (113/113, 42 differences); extract SFD initialization into an inline helper and remove the volume alias (113/113, 38 differences); restore original declarations and reverse loop increments (113/113, 18 differences, different increment ordering). Retained original declarations and loop order: 113/113, 18 differences. Callee-saved registers and two loop increments still differ; objdiff 99.09734%. +- fread: move p_fread definition into object order (96/67, still inlined); use a separate completed-count local (96/67); initialize that local to zero (96/67); introduce a separate read-status scope (96/67); put output initialization before volume lookup and remove the extra status scope (97/67, 28 comparison differences). Retained the last form, which follows the target's output and volume initialization order. The compiler inlines p_fread into this wrapper whereas the original calls it separately; objdiff 48.208954%. p_fread itself remains instruction exact. + +New exact functions: Cursor_WriteHeadSector, Cursor_WriteBodySectors, Cursor_WriteTailSector, createEmptyFile, p_remove, p_fopen, p_fwrite, p_combine, p_divide, p_flock, fsetclstlink. + +Key source corrections: pass the next sector to the last-access update; use the fa hint and string layouts; retain the removal iterator hint and original first cluster; compute the LFN checksum once and advance the previous-sector pointer; reuse the free-UFD helper; initialize the divide iterators directly and preserve its error through cleanup; propagate cache flush failures; use the target lock flags and release one lock at a time. + +The other units' remaining function attempts and evidence are in fa-volume.md, fa-dir.md and fa-fat.md. No configuration state changed. + +Remaining objdiff scores across the batch: +- PFVOL_p_setvol 98.82979%; reason and three or more attempts recorded in fa-volume.md. +- PFVOL_attach 98.72159%; reason and three or more attempts recorded in fa-volume.md. +- PFVOL_setcode 53.869564%; reason and three or more attempts recorded in fa-volume.md. +- PFVOL_regctx 99.0%; reason and three or more attempts recorded in fa-volume.md. +- PFDIR_GetSDD 99.710144%; reason and three or more attempts recorded in fa-dir.md. +- PFDIR_p_mkdir 24.046263%; reason and three or more attempts recorded in fa-dir.md. +- PFDIR_p_rmdir 81.4802%; reason and three or more attempts recorded in fa-dir.md. +- PFDIR_p_rename 24.0336%; reason and three or more attempts recorded in fa-dir.md. +- PFDIR_p_move 25.359747%; reason and three or more attempts recorded in fa-dir.md. +- PFDIR_p_fsnext 97.74612%; reason and three or more attempts recorded in fa-dir.md. +- PFDIR_p_fsexec_remove 98.46591%; reason and three or more attempts recorded in fa-dir.md. +- PFDIR_p_fsexec 63.478573%; reason and three or more attempts recorded in fa-dir.md. +- PFDIR_opendir 99.74359%; reason and three or more attempts recorded in fa-dir.md. +- PFFAT_DoAllocateChain 99.83796%; reason and three or more attempts recorded in fa-fat.md. +- PFFAT_GetClusterAllocated 99.57143%; reason and three or more attempts recorded in fa-fat.md. +- PFFAT_GetSector 99.431816%; reason and three or more attempts recorded in fa-fat.md. +- PFFAT_GetSectorSpecified 93.52941%; reason and three or more attempts recorded in fa-fat.md. +- PFFAT_FreeChain 97.95276%; reason and three or more attempts recorded in fa-fat.md. +- PFFAT_RefreshFSINFO 96.62366%; reason and three or more attempts recorded in fa-fat.md. +- PFFILE_GetSFD 99.09734%; register allocation and loop increment ordering. +- PFFILE_fread 48.208954%; compiler inlines the separate read operation. diff --git a/tools/decomp-assist/attempts/fa-volume.md b/tools/decomp-assist/attempts/fa-volume.md new file mode 100644 index 00000000..d9cf5e88 --- /dev/null +++ b/tools/decomp-assist/attempts/fa-volume.md @@ -0,0 +1,11 @@ +# fa/pf_volume + +Baseline: 7/37 exact, 1048/17992 code bytes. Matching vf source compared first; this fa variant has 26 volumes and three context slots. + +Remaining experiments (ctxdiff evidence in /tmp during matching): +- PFVOL_p_setvol: inline whole operation (93/94, different stack slots); normalize helper result (97/94, different stack slots); remove redundant root error else (93/94, only first conditional branch expansion). Retained original stack layout, 93/94. +- PFVOL_attach: correct free-cluster and drive fields (170/176); canonical search loop (169/176); split driver/cache initialization and mount status (173/176); reorder locals and signed drive conversion (174/176); separate mount helper (174/176, unchanged). Remaining status return boundary, 174/176. +- PFVOL_setcode: six named pointer assignments (23/23, 17 diffs); aggregate assignment (same); word loop (same); inline copy helper (same). Compiler schedules loads before stores and allocates a different base register. +- PFVOL_regctx: indexed context loop (79/75); initialize free index after system call (77/75); return system status (75/75, 20 register differences); reorder free/index declarations (75/75, 15 register differences); split masked status declaration and assignment (unchanged). Retained 75/75. + +Other corrected operations now instruction exact: driver request handling, read/write checks, formatting, context removal, label removal, cache flush status, buffering, mount/unmount and configuration. No shared headers changed.