diff options
Diffstat (limited to 'src/core')
43 files changed, 1729 insertions, 2101 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 634f4d572..207f39707 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -8,6 +8,7 @@ set(SRCS core.cpp system.cpp arm/disassembler/arm_disasm.cpp arm/disassembler/load_symbol_map.cpp + file_sys/archive_romfs.cpp arm/interpreter/arm_interpreter.cpp arm/interpreter/armcopro.cpp arm/interpreter/armemu.cpp @@ -29,17 +30,19 @@ set(SRCS core.cpp arm/interpreter/mmu/tlb.cpp arm/interpreter/mmu/wb.cpp arm/interpreter/mmu/xscale_copro.cpp - file_sys/directory_file_system.cpp - file_sys/meta_file_system.cpp hle/hle.cpp hle/config_mem.cpp hle/coprocessor.cpp hle/svc.cpp + hle/kernel/address_arbiter.cpp + hle/kernel/archive.cpp hle/kernel/event.cpp hle/kernel/kernel.cpp hle/kernel/mutex.cpp + hle/kernel/shared_memory.cpp hle/kernel/thread.cpp hle/service/apt.cpp + hle/service/fs.cpp hle/service/gsp.cpp hle/service/hid.cpp hle/service/ndm.cpp @@ -75,18 +78,21 @@ set(HEADERS core.h arm/interpreter/vfp/asm_vfp.h arm/interpreter/vfp/vfp.h arm/interpreter/vfp/vfp_helper.h - file_sys/directory_file_system.h - file_sys/file_sys.h - file_sys/meta_file_system.h + file_sys/archive.h + file_sys/archive_romfs.h hle/config_mem.h hle/coprocessor.h hle/hle.h hle/svc.h + hle/kernel/address_arbiter.h + hle/kernel/archive.h hle/kernel/kernel.h hle/kernel/mutex.h + hle/kernel/shared_memory.h hle/kernel/thread.h hle/function_wrappers.h hle/service/apt.h + hle/service/fs.h hle/service/gsp.h hle/service/hid.h hle/service/service.h diff --git a/src/core/arm/interpreter/armdefs.h b/src/core/arm/interpreter/armdefs.h index d8eae4d3f..1ff560fe7 100644 --- a/src/core/arm/interpreter/armdefs.h +++ b/src/core/arm/interpreter/armdefs.h @@ -70,13 +70,6 @@ #define LOWHIGH 1 #define HIGHLOW 2 -#ifndef u8 -#define u8 unsigned char -#define u16 unsigned short -#define u32 unsigned int -#define u64 unsigned long long -#endif /*u8 */ - //teawater add DBCT_TEST_SPEED 2005.10.04--------------------------------------- #include <signal.h> diff --git a/src/core/core.vcxproj b/src/core/core.vcxproj index e2216760a..ddc174c2c 100644 --- a/src/core/core.vcxproj +++ b/src/core/core.vcxproj @@ -162,16 +162,19 @@ <ClCompile Include="arm\interpreter\vfp\vfpsingle.cpp" /> <ClCompile Include="core.cpp" /> <ClCompile Include="core_timing.cpp" /> - <ClCompile Include="file_sys\directory_file_system.cpp" /> - <ClCompile Include="file_sys\meta_file_system.cpp" /> + <ClCompile Include="file_sys\archive_romfs.cpp" /> <ClCompile Include="hle\config_mem.cpp" /> <ClCompile Include="hle\coprocessor.cpp" /> <ClCompile Include="hle\hle.cpp" /> + <ClCompile Include="hle\kernel\address_arbiter.cpp" /> + <ClCompile Include="hle\kernel\archive.cpp" /> <ClCompile Include="hle\kernel\event.cpp" /> <ClCompile Include="hle\kernel\kernel.cpp" /> <ClCompile Include="hle\kernel\mutex.cpp" /> + <ClCompile Include="hle\kernel\shared_memory.cpp" /> <ClCompile Include="hle\kernel\thread.cpp" /> <ClCompile Include="hle\service\apt.cpp" /> + <ClCompile Include="hle\service\fs.cpp" /> <ClCompile Include="hle\service\gsp.cpp" /> <ClCompile Include="hle\service\hid.cpp" /> <ClCompile Include="hle\service\ndm.cpp" /> @@ -211,18 +214,21 @@ <ClInclude Include="arm\interpreter\vfp\vfp_helper.h" /> <ClInclude Include="core.h" /> <ClInclude Include="core_timing.h" /> - <ClInclude Include="file_sys\directory_file_system.h" /> - <ClInclude Include="file_sys\file_sys.h" /> - <ClInclude Include="file_sys\meta_file_system.h" /> + <ClInclude Include="file_sys\archive.h" /> + <ClInclude Include="file_sys\archive_romfs.h" /> <ClInclude Include="hle\config_mem.h" /> <ClInclude Include="hle\coprocessor.h" /> <ClInclude Include="hle\function_wrappers.h" /> <ClInclude Include="hle\hle.h" /> + <ClInclude Include="hle\kernel\address_arbiter.h" /> + <ClInclude Include="hle\kernel\archive.h" /> <ClInclude Include="hle\kernel\event.h" /> <ClInclude Include="hle\kernel\kernel.h" /> <ClInclude Include="hle\kernel\mutex.h" /> + <ClInclude Include="hle\kernel\shared_memory.h" /> <ClInclude Include="hle\kernel\thread.h" /> <ClInclude Include="hle\service\apt.h" /> + <ClInclude Include="hle\service\fs.h" /> <ClInclude Include="hle\service\gsp.h" /> <ClInclude Include="hle\service\hid.h" /> <ClInclude Include="hle\service\ndm.h" /> diff --git a/src/core/core.vcxproj.filters b/src/core/core.vcxproj.filters index 91d3292da..68ba9e50b 100644 --- a/src/core/core.vcxproj.filters +++ b/src/core/core.vcxproj.filters @@ -63,12 +63,6 @@ <ClCompile Include="arm\interpreter\thumbemu.cpp"> <Filter>arm\interpreter</Filter> </ClCompile> - <ClCompile Include="file_sys\directory_file_system.cpp"> - <Filter>file_sys</Filter> - </ClCompile> - <ClCompile Include="file_sys\meta_file_system.cpp"> - <Filter>file_sys</Filter> - </ClCompile> <ClCompile Include="hw\hw.cpp"> <Filter>hw</Filter> </ClCompile> @@ -176,6 +170,21 @@ <ClCompile Include="loader\elf.cpp"> <Filter>loader</Filter> </ClCompile> + <ClCompile Include="hle\kernel\archive.cpp"> + <Filter>hle\kernel</Filter> + </ClCompile> + <ClCompile Include="hle\service\fs.cpp"> + <Filter>hle\service</Filter> + </ClCompile> + <ClCompile Include="file_sys\archive_romfs.cpp"> + <Filter>file_sys</Filter> + </ClCompile> + <ClCompile Include="hle\kernel\shared_memory.cpp"> + <Filter>hle\kernel</Filter> + </ClCompile> + <ClCompile Include="hle\kernel\address_arbiter.cpp"> + <Filter>hle\kernel</Filter> + </ClCompile> </ItemGroup> <ItemGroup> <ClInclude Include="arm\disassembler\arm_disasm.h"> @@ -205,15 +214,6 @@ <ClInclude Include="arm\interpreter\skyeye_defs.h"> <Filter>arm\interpreter</Filter> </ClInclude> - <ClInclude Include="file_sys\directory_file_system.h"> - <Filter>file_sys</Filter> - </ClInclude> - <ClInclude Include="file_sys\file_sys.h"> - <Filter>file_sys</Filter> - </ClInclude> - <ClInclude Include="file_sys\meta_file_system.h"> - <Filter>file_sys</Filter> - </ClInclude> <ClInclude Include="hw\hw.h"> <Filter>hw</Filter> </ClInclude> @@ -314,6 +314,24 @@ <ClInclude Include="loader\elf.h"> <Filter>loader</Filter> </ClInclude> + <ClInclude Include="hle\kernel\archive.h"> + <Filter>hle\kernel</Filter> + </ClInclude> + <ClInclude Include="hle\service\fs.h"> + <Filter>hle\service</Filter> + </ClInclude> + <ClInclude Include="file_sys\archive.h"> + <Filter>file_sys</Filter> + </ClInclude> + <ClInclude Include="file_sys\archive_romfs.h"> + <Filter>file_sys</Filter> + </ClInclude> + <ClInclude Include="hle\kernel\shared_memory.h"> + <Filter>hle\kernel</Filter> + </ClInclude> + <ClInclude Include="hle\kernel\address_arbiter.h"> + <Filter>hle\kernel</Filter> + </ClInclude> </ItemGroup> <ItemGroup> <Text Include="CMakeLists.txt" /> diff --git a/src/core/file_sys/archive.h b/src/core/file_sys/archive.h new file mode 100644 index 000000000..ed2d83640 --- /dev/null +++ b/src/core/file_sys/archive.h @@ -0,0 +1,54 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#pragma once + +#include "common/common_types.h" + +#include "core/hle/kernel/kernel.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// FileSys namespace + +namespace FileSys { + +class Archive : NonCopyable { +public: + /// Supported archive types + enum class IdCode : u32 { + RomFS = 0x00000003, + SaveData = 0x00000004, + ExtSaveData = 0x00000006, + SharedExtSaveData = 0x00000007, + SystemSaveData = 0x00000008, + SDMC = 0x00000009, + SDMCWriteOnly = 0x0000000A, + }; + + Archive() { } + virtual ~Archive() { } + + /** + * Get the IdCode of the archive (e.g. RomFS, SaveData, etc.) + * @return IdCode of the archive + */ + virtual IdCode GetIdCode() const = 0; + + /** + * Read data from the archive + * @param offset Offset in bytes to start reading archive from + * @param length Length in bytes to read data from archive + * @param buffer Buffer to read data into + * @return Number of bytes read + */ + virtual size_t Read(const u64 offset, const u32 length, u8* buffer) const = 0; + + /** + * Get the size of the archive in bytes + * @return Size of the archive in bytes + */ + virtual size_t GetSize() const = 0; +}; + +} // namespace FileSys diff --git a/src/core/file_sys/archive_romfs.cpp b/src/core/file_sys/archive_romfs.cpp new file mode 100644 index 000000000..fd84b9c8c --- /dev/null +++ b/src/core/file_sys/archive_romfs.cpp @@ -0,0 +1,46 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#include "common/common_types.h" + +#include "core/file_sys/archive_romfs.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// FileSys namespace + +namespace FileSys { + +Archive_RomFS::Archive_RomFS(const Loader::AppLoader& app_loader) { + // Load the RomFS from the app + if (Loader::ResultStatus::Success != app_loader.ReadRomFS(raw_data)) { + WARN_LOG(FILESYS, "Unable to read RomFS!"); + } +} + +Archive_RomFS::~Archive_RomFS() { +} + +/** + * Read data from the archive + * @param offset Offset in bytes to start reading archive from + * @param length Length in bytes to read data from archive + * @param buffer Buffer to read data into + * @return Number of bytes read + */ +size_t Archive_RomFS::Read(const u64 offset, const u32 length, u8* buffer) const { + DEBUG_LOG(FILESYS, "called offset=%d, length=%d", offset, length); + memcpy(buffer, &raw_data[(u32)offset], length); + return length; +} + +/** + * Get the size of the archive in bytes + * @return Size of the archive in bytes + */ +size_t Archive_RomFS::GetSize() const { + ERROR_LOG(FILESYS, "(UNIMPLEMENTED)"); + return 0; +} + +} // namespace FileSys diff --git a/src/core/file_sys/archive_romfs.h b/src/core/file_sys/archive_romfs.h new file mode 100644 index 000000000..8a31190a9 --- /dev/null +++ b/src/core/file_sys/archive_romfs.h @@ -0,0 +1,50 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#pragma once + +#include <vector> + +#include "common/common_types.h" + +#include "core/file_sys/archive.h" +#include "core/loader/loader.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// FileSys namespace + +namespace FileSys { + +/// File system interface to the RomFS archive +class Archive_RomFS final : public Archive { +public: + Archive_RomFS(const Loader::AppLoader& app_loader); + ~Archive_RomFS() override; + + /** + * Get the IdCode of the archive (e.g. RomFS, SaveData, etc.) + * @return IdCode of the archive + */ + IdCode GetIdCode() const override { return IdCode::RomFS; }; + + /** + * Read data from the archive + * @param offset Offset in bytes to start reading archive from + * @param length Length in bytes to read data from archive + * @param buffer Buffer to read data into + * @return Number of bytes read + */ + size_t Read(const u64 offset, const u32 length, u8* buffer) const override; + + /** + * Get the size of the archive in bytes + * @return Size of the archive in bytes + */ + size_t GetSize() const override; + +private: + std::vector<u8> raw_data; +}; + +} // namespace FileSys diff --git a/src/core/file_sys/directory_file_system.cpp b/src/core/file_sys/directory_file_system.cpp deleted file mode 100644 index 6c6f33c2b..000000000 --- a/src/core/file_sys/directory_file_system.cpp +++ /dev/null @@ -1,669 +0,0 @@ -// Copyright (c) 2012- PPSSPP Project. - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, version 2.0 or later versions. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License 2.0 for more details. - -// A copy of the GPL 2.0 should have been included with the program. -// If not, see http://www.gnu.org/licenses/ - -// Official git repository and contact information can be found at -// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. - -#include "common/chunk_file.h" -#include "common/file_util.h" -#include "common/utf8.h" - -#include "core/file_sys/directory_file_system.h" - -#if EMU_PLATFORM == PLATFORM_WINDOWS -#include <windows.h> -#include <sys/stat.h> -#else -#include <dirent.h> -#include <unistd.h> -#include <sys/stat.h> -#include <ctype.h> -#endif - -#if HOST_IS_CASE_SENSITIVE -static bool FixFilenameCase(const std::string &path, std::string &filename) -{ - // Are we lucky? - if (File::Exists(path + filename)) - return true; - - size_t filenameSize = filename.size(); // size in bytes, not characters - for (size_t i = 0; i < filenameSize; i++) - { - filename[i] = tolower(filename[i]); - } - - //TODO: lookup filename in cache for "path" - - struct dirent_large { struct dirent entry; char padding[FILENAME_MAX+1]; } diren; - struct dirent_large; - struct dirent *result = NULL; - - DIR *dirp = opendir(path.c_str()); - if (!dirp) - return false; - - bool retValue = false; - - while (!readdir_r(dirp, (dirent*) &diren, &result) && result) - { - if (strlen(result->d_name) != filenameSize) - continue; - - size_t i; - for (i = 0; i < filenameSize; i++) - { - if (filename[i] != tolower(result->d_name[i])) - break; - } - - if (i < filenameSize) - continue; - - filename = result->d_name; - retValue = true; - } - - closedir(dirp); - - return retValue; -} - -bool FixPathCase(std::string& basePath, std::string &path, FixPathCaseBehavior behavior) -{ - size_t len = path.size(); - - if (len == 0) - return true; - - if (path[len - 1] == '/') - { - len--; - - if (len == 0) - return true; - } - - std::string fullPath; - fullPath.reserve(basePath.size() + len + 1); - fullPath.append(basePath); - - size_t start = 0; - while (start < len) - { - size_t i = path.find('/', start); - if (i == std::string::npos) - i = len; - - if (i > start) - { - std::string component = path.substr(start, i - start); - - // Fix case and stop on nonexistant path component - if (FixFilenameCase(fullPath, component) == false) { - // Still counts as success if partial matches allowed or if this - // is the last component and only the ones before it are required - return (behavior == FPC_PARTIAL_ALLOWED || (behavior == FPC_PATH_MUST_EXIST && i >= len)); - } - - path.replace(start, i - start, component); - - fullPath.append(component); - fullPath.append(1, '/'); - } - - start = i + 1; - } - - return true; -} - -#endif - -std::string DirectoryFileHandle::GetLocalPath(std::string& basePath, std::string localpath) -{ - if (localpath.empty()) - return basePath; - - if (localpath[0] == '/') - localpath.erase(0,1); - //Convert slashes -#ifdef _WIN32 - for (size_t i = 0; i < localpath.size(); i++) { - if (localpath[i] == '/') - localpath[i] = '\\'; - } -#endif - return basePath + localpath; -} - -bool DirectoryFileHandle::Open(std::string& basePath, std::string& fileName, FileAccess access) -{ -#if HOST_IS_CASE_SENSITIVE - if (access & (FILEACCESS_APPEND|FILEACCESS_CREATE|FILEACCESS_WRITE)) - { - DEBUG_LOG(FILESYS, "Checking case for path %s", fileName.c_str()); - if ( ! FixPathCase(basePath, fileName, FPC_PATH_MUST_EXIST) ) - return false; // or go on and attempt (for a better error code than just 0?) - } - // else we try fopen first (in case we're lucky) before simulating case insensitivity -#endif - - std::string fullName = GetLocalPath(basePath,fileName); - INFO_LOG(FILESYS,"Actually opening %s", fullName.c_str()); - - //TODO: tests, should append seek to end of file? seeking in a file opened for append? -#ifdef _WIN32 - // Convert parameters to Windows permissions and access - DWORD desired = 0; - DWORD sharemode = 0; - DWORD openmode = 0; - if (access & FILEACCESS_READ) { - desired |= GENERIC_READ; - sharemode |= FILE_SHARE_READ; - } - if (access & FILEACCESS_WRITE) { - desired |= GENERIC_WRITE; - sharemode |= FILE_SHARE_WRITE; - } - if (access & FILEACCESS_CREATE) { - openmode = OPEN_ALWAYS; - } else { - openmode = OPEN_EXISTING; - } - //Let's do it! - hFile = CreateFile(ConvertUTF8ToWString(fullName).c_str(), desired, sharemode, 0, openmode, 0, 0); - bool success = hFile != INVALID_HANDLE_VALUE; -#else - // Convert flags in access parameter to fopen access mode - const char *mode = NULL; - if (access & FILEACCESS_APPEND) { - if (access & FILEACCESS_READ) - mode = "ab+"; // append+read, create if needed - else - mode = "ab"; // append only, create if needed - } else if (access & FILEACCESS_WRITE) { - if (access & FILEACCESS_READ) { - // FILEACCESS_CREATE is ignored for read only, write only, and append - // because C++ standard fopen's nonexistant file creation can only be - // customized for files opened read+write - if (access & FILEACCESS_CREATE) - mode = "wb+"; // read+write, create if needed - else - mode = "rb+"; // read+write, but don't create - } else { - mode = "wb"; // write only, create if needed - } - } else { // neither write nor append, so default to read only - mode = "rb"; // read only, don't create - } - - hFile = fopen(fullName.c_str(), mode); - bool success = hFile != 0; -#endif - -#if HOST_IS_CASE_SENSITIVE - if (!success && - !(access & FILEACCESS_APPEND) && - !(access & FILEACCESS_CREATE) && - !(access & FILEACCESS_WRITE)) - { - if ( ! FixPathCase(basePath,fileName, FPC_PATH_MUST_EXIST) ) - return 0; // or go on and attempt (for a better error code than just 0?) - fullName = GetLocalPath(basePath,fileName); - const char* fullNameC = fullName.c_str(); - - DEBUG_LOG(FILESYS, "Case may have been incorrect, second try opening %s (%s)", fullNameC, fileName.c_str()); - - // And try again with the correct case this time -#ifdef _WIN32 - hFile = CreateFile(fullNameC, desired, sharemode, 0, openmode, 0, 0); - success = hFile != INVALID_HANDLE_VALUE; -#else - hFile = fopen(fullNameC, mode); - success = hFile != 0; -#endif - } -#endif - - return success; -} - -size_t DirectoryFileHandle::Read(u8* pointer, s64 size) -{ - size_t bytesRead = 0; -#ifdef _WIN32 - ::ReadFile(hFile, (LPVOID)pointer, (DWORD)size, (LPDWORD)&bytesRead, 0); -#else - bytesRead = fread(pointer, 1, size, hFile); -#endif - return bytesRead; -} - -size_t DirectoryFileHandle::Write(const u8* pointer, s64 size) -{ - size_t bytesWritten = 0; -#ifdef _WIN32 - ::WriteFile(hFile, (LPVOID)pointer, (DWORD)size, (LPDWORD)&bytesWritten, 0); -#else - bytesWritten = fwrite(pointer, 1, size, hFile); -#endif - return bytesWritten; -} - -size_t DirectoryFileHandle::Seek(s32 position, FileMove type) -{ -#ifdef _WIN32 - DWORD moveMethod = 0; - switch (type) { - case FILEMOVE_BEGIN: moveMethod = FILE_BEGIN; break; - case FILEMOVE_CURRENT: moveMethod = FILE_CURRENT; break; - case FILEMOVE_END: moveMethod = FILE_END; break; - } - DWORD newPos = SetFilePointer(hFile, (LONG)position, 0, moveMethod); - return newPos; -#else - int moveMethod = 0; - switch (type) { - case FILEMOVE_BEGIN: moveMethod = SEEK_SET; break; - case FILEMOVE_CURRENT: moveMethod = SEEK_CUR; break; - case FILEMOVE_END: moveMethod = SEEK_END; break; - } - fseek(hFile, position, moveMethod); - return ftell(hFile); -#endif -} - -void DirectoryFileHandle::Close() -{ -#ifdef _WIN32 - if (hFile != (HANDLE)-1) - CloseHandle(hFile); -#else - if (hFile != 0) - fclose(hFile); -#endif -} - -DirectoryFileSystem::DirectoryFileSystem(IHandleAllocator *_hAlloc, std::string _basePath) : basePath(_basePath) { - File::CreateFullPath(basePath); - hAlloc = _hAlloc; -} - -DirectoryFileSystem::~DirectoryFileSystem() { - for (auto iter = entries.begin(); iter != entries.end(); ++iter) { - iter->second.hFile.Close(); - } -} - -std::string DirectoryFileSystem::GetLocalPath(std::string localpath) { - if (localpath.empty()) - return basePath; - - if (localpath[0] == '/') - localpath.erase(0,1); - //Convert slashes -#ifdef _WIN32 - for (size_t i = 0; i < localpath.size(); i++) { - if (localpath[i] == '/') - localpath[i] = '\\'; - } -#endif - return basePath + localpath; -} - -bool DirectoryFileSystem::MkDir(const std::string &dirname) { -#if HOST_IS_CASE_SENSITIVE - // Must fix case BEFORE attempting, because MkDir would create - // duplicate (different case) directories - - std::string fixedCase = dirname; - if ( ! FixPathCase(basePath,fixedCase, FPC_PARTIAL_ALLOWED) ) - return false; - - return File::CreateFullPath(GetLocalPath(fixedCase)); -#else - return File::CreateFullPath(GetLocalPath(dirname)); -#endif -} - -bool DirectoryFileSystem::RmDir(const std::string &dirname) { - std::string fullName = GetLocalPath(dirname); - -#if HOST_IS_CASE_SENSITIVE - // Maybe we're lucky? - if (File::DeleteDirRecursively(fullName)) - return true; - - // Nope, fix case and try again - fullName = dirname; - if ( ! FixPathCase(basePath,fullName, FPC_FILE_MUST_EXIST) ) - return false; // or go on and attempt (for a better error code than just false?) - - fullName = GetLocalPath(fullName); -#endif - -/*#ifdef _WIN32 - return RemoveDirectory(fullName.c_str()) == TRUE; -#else - return 0 == rmdir(fullName.c_str()); -#endif*/ - return File::DeleteDirRecursively(fullName); -} - -int DirectoryFileSystem::RenameFile(const std::string &from, const std::string &to) { - std::string fullTo = to; - - // Rename ignores the path (even if specified) on to. - size_t chop_at = to.find_last_of('/'); - if (chop_at != to.npos) - fullTo = to.substr(chop_at + 1); - - // Now put it in the same directory as from. - size_t dirname_end = from.find_last_of('/'); - if (dirname_end != from.npos) - fullTo = from.substr(0, dirname_end + 1) + fullTo; - - // At this point, we should check if the paths match and give an already exists error. - if (from == fullTo) - return -1;//SCE_KERNEL_ERROR_ERRNO_FILE_ALREADY_EXISTS; - - std::string fullFrom = GetLocalPath(from); - -#if HOST_IS_CASE_SENSITIVE - // In case TO should overwrite a file with different case - if ( ! FixPathCase(basePath,fullTo, FPC_PATH_MUST_EXIST) ) - return -1; // or go on and attempt (for a better error code than just false?) -#endif - - fullTo = GetLocalPath(fullTo); - const char * fullToC = fullTo.c_str(); - -#ifdef _WIN32 - bool retValue = (MoveFile(ConvertUTF8ToWString(fullFrom).c_str(), ConvertUTF8ToWString(fullToC).c_str()) == TRUE); -#else - bool retValue = (0 == rename(fullFrom.c_str(), fullToC)); -#endif - -#if HOST_IS_CASE_SENSITIVE - if (! retValue) - { - // May have failed due to case sensitivity on FROM, so try again - fullFrom = from; - if ( ! FixPathCase(basePath,fullFrom, FPC_FILE_MUST_EXIST) ) - return -1; // or go on and attempt (for a better error code than just false?) - fullFrom = GetLocalPath(fullFrom); - -#ifdef _WIN32 - retValue = (MoveFile(fullFrom.c_str(), fullToC) == TRUE); -#else - retValue = (0 == rename(fullFrom.c_str(), fullToC)); -#endif - } -#endif - - // TODO: Better error codes. - return retValue ? 0 : -1;//SCE_KERNEL_ERROR_ERRNO_FILE_ALREADY_EXISTS; -} - -bool DirectoryFileSystem::RemoveFile(const std::string &filename) { - std::string fullName = GetLocalPath(filename); -#ifdef _WIN32 - bool retValue = (::DeleteFileA(fullName.c_str()) == TRUE); -#else - bool retValue = (0 == unlink(fullName.c_str())); -#endif - -#if HOST_IS_CASE_SENSITIVE - if (! retValue) - { - // May have failed due to case sensitivity, so try again - fullName = filename; - if ( ! FixPathCase(basePath,fullName, FPC_FILE_MUST_EXIST) ) - return false; // or go on and attempt (for a better error code than just false?) - fullName = GetLocalPath(fullName); - -#ifdef _WIN32 - retValue = (::DeleteFileA(fullName.c_str()) == TRUE); -#else - retValue = (0 == unlink(fullName.c_str())); -#endif - } -#endif - - return retValue; -} - -u32 DirectoryFileSystem::OpenFile(std::string filename, FileAccess access, const char *devicename) { - OpenFileEntry entry; - bool success = entry.hFile.Open(basePath,filename,access); - - if (!success) { -#ifdef _WIN32 - ERROR_LOG(FILESYS, "DirectoryFileSystem::OpenFile: FAILED, %i - access = %i", GetLastError(), (int)access); -#else - ERROR_LOG(FILESYS, "DirectoryFileSystem::OpenFile: FAILED, access = %i", (int)access); -#endif - //wwwwaaaaahh!! - return 0; - } else { -#ifdef _WIN32 - if (access & FILEACCESS_APPEND) - entry.hFile.Seek(0,FILEMOVE_END); -#endif - - u32 newHandle = hAlloc->GetNewHandle(); - entries[newHandle] = entry; - - return newHandle; - } -} - -void DirectoryFileSystem::CloseFile(u32 handle) { - EntryMap::iterator iter = entries.find(handle); - if (iter != entries.end()) { - hAlloc->FreeHandle(handle); - iter->second.hFile.Close(); - entries.erase(iter); - } else { - //This shouldn't happen... - ERROR_LOG(FILESYS,"Cannot close file that hasn't been opened: %08x", handle); - } -} - -bool DirectoryFileSystem::OwnsHandle(u32 handle) { - EntryMap::iterator iter = entries.find(handle); - return (iter != entries.end()); -} - -size_t DirectoryFileSystem::ReadFile(u32 handle, u8 *pointer, s64 size) { - EntryMap::iterator iter = entries.find(handle); - if (iter != entries.end()) - { - size_t bytesRead = iter->second.hFile.Read(pointer,size); - return bytesRead; - } else { - //This shouldn't happen... - ERROR_LOG(FILESYS,"Cannot read file that hasn't been opened: %08x", handle); - return 0; - } -} - -size_t DirectoryFileSystem::WriteFile(u32 handle, const u8 *pointer, s64 size) { - EntryMap::iterator iter = entries.find(handle); - if (iter != entries.end()) - { - size_t bytesWritten = iter->second.hFile.Write(pointer,size); - return bytesWritten; - } else { - //This shouldn't happen... - ERROR_LOG(FILESYS,"Cannot write to file that hasn't been opened: %08x", handle); - return 0; - } -} - -size_t DirectoryFileSystem::SeekFile(u32 handle, s32 position, FileMove type) { - EntryMap::iterator iter = entries.find(handle); - if (iter != entries.end()) { - return iter->second.hFile.Seek(position,type); - } else { - //This shouldn't happen... - ERROR_LOG(FILESYS,"Cannot seek in file that hasn't been opened: %08x", handle); - return 0; - } -} - -FileInfo DirectoryFileSystem::GetFileInfo(std::string filename) { - FileInfo x; - x.name = filename; - - std::string fullName = GetLocalPath(filename); - if (! File::Exists(fullName)) { -#if HOST_IS_CASE_SENSITIVE - if (! FixPathCase(basePath,filename, FPC_FILE_MUST_EXIST)) - return x; - fullName = GetLocalPath(filename); - - if (! File::Exists(fullName)) - return x; -#else - return x; -#endif - } - x.type = File::IsDirectory(fullName) ? FILETYPE_DIRECTORY : FILETYPE_NORMAL; - x.exists = true; - - if (x.type != FILETYPE_DIRECTORY) - { -#ifdef _WIN32 - struct _stat64i32 s; - _wstat64i32(ConvertUTF8ToWString(fullName).c_str(), &s); -#else - struct stat s; - stat(fullName.c_str(), &s); -#endif - - x.size = File::GetSize(fullName); - x.access = s.st_mode & 0x1FF; - localtime_r((time_t*)&s.st_atime,&x.atime); - localtime_r((time_t*)&s.st_ctime,&x.ctime); - localtime_r((time_t*)&s.st_mtime,&x.mtime); - } - - return x; -} - -bool DirectoryFileSystem::GetHostPath(const std::string &inpath, std::string &outpath) { - outpath = GetLocalPath(inpath); - return true; -} - -#ifdef _WIN32 -#define FILETIME_FROM_UNIX_EPOCH_US 11644473600000000ULL - -static void tmFromFiletime(tm &dest, FILETIME &src) -{ - u64 from_1601_us = (((u64) src.dwHighDateTime << 32ULL) + (u64) src.dwLowDateTime) / 10ULL; - u64 from_1970_us = from_1601_us - FILETIME_FROM_UNIX_EPOCH_US; - - time_t t = (time_t) (from_1970_us / 1000000UL); - localtime_r(&t, &dest); -} -#endif - -std::vector<FileInfo> DirectoryFileSystem::GetDirListing(std::string path) { - std::vector<FileInfo> myVector; -#ifdef _WIN32 - WIN32_FIND_DATA findData; - HANDLE hFind; - - std::string w32path = GetLocalPath(path) + "\\*.*"; - - hFind = FindFirstFile(ConvertUTF8ToWString(w32path).c_str(), &findData); - - if (hFind == INVALID_HANDLE_VALUE) { - return myVector; //the empty list - } - - while (true) { - FileInfo entry; - if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) - entry.type = FILETYPE_DIRECTORY; - else - entry.type = FILETYPE_NORMAL; - - // TODO: Make this more correct? - entry.access = entry.type == FILETYPE_NORMAL ? 0666 : 0777; - // TODO: is this just for .. or all subdirectories? Need to add a directory to the test - // to find out. Also why so different than the old test results? - if (!wcscmp(findData.cFileName, L"..") ) - entry.size = 4096; - else - entry.size = findData.nFileSizeLow | ((u64)findData.nFileSizeHigh<<32); - entry.name = ConvertWStringToUTF8(findData.cFileName); - tmFromFiletime(entry.atime, findData.ftLastAccessTime); - tmFromFiletime(entry.ctime, findData.ftCreationTime); - tmFromFiletime(entry.mtime, findData.ftLastWriteTime); - myVector.push_back(entry); - - int retval = FindNextFile(hFind, &findData); - if (!retval) - break; - } -#else - dirent *dirp; - std::string localPath = GetLocalPath(path); - DIR *dp = opendir(localPath.c_str()); - -#if HOST_IS_CASE_SENSITIVE - if(dp == NULL && FixPathCase(basePath,path, FPC_FILE_MUST_EXIST)) { - // May have failed due to case sensitivity, try again - localPath = GetLocalPath(path); - dp = opendir(localPath.c_str()); - } -#endif - - if (dp == NULL) { - ERROR_LOG(FILESYS,"Error opening directory %s\n",path.c_str()); - return myVector; - } - - while ((dirp = readdir(dp)) != NULL) { - FileInfo entry; - struct stat s; - std::string fullName = GetLocalPath(path) + "/"+dirp->d_name; - stat(fullName.c_str(), &s); - if (S_ISDIR(s.st_mode)) - entry.type = FILETYPE_DIRECTORY; - else - entry.type = FILETYPE_NORMAL; - entry.access = s.st_mode & 0x1FF; - entry.name = dirp->d_name; - entry.size = s.st_size; - localtime_r((time_t*)&s.st_atime,&entry.atime); - localtime_r((time_t*)&s.st_ctime,&entry.ctime); - localtime_r((time_t*)&s.st_mtime,&entry.mtime); - myVector.push_back(entry); - } - closedir(dp); -#endif - return myVector; -} - -void DirectoryFileSystem::DoState(PointerWrap &p) { - if (!entries.empty()) { - p.SetError(p.ERROR_WARNING); - ERROR_LOG(FILESYS, "FIXME: Open files during savestate, could go badly."); - } -} diff --git a/src/core/file_sys/directory_file_system.h b/src/core/file_sys/directory_file_system.h deleted file mode 100644 index 9af2854a2..000000000 --- a/src/core/file_sys/directory_file_system.h +++ /dev/null @@ -1,155 +0,0 @@ -// Copyright (c) 2012- PPSSPP Project. - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, version 2.0 or later versions. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License 2.0 for more details. - -// A copy of the GPL 2.0 should have been included with the program. -// If not, see http://www.gnu.org/licenses/ - -// Official git repository and contact information can be found at -// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. - -#pragma once - -// TODO: Remove the Windows-specific code, FILE is fine there too. - -#include <map> - -#include "core/file_sys/file_sys.h" - -#ifdef _WIN32 -typedef void * HANDLE; -#endif - -#if defined(__APPLE__) - -#if TARGET_OS_IPHONE -#define HOST_IS_CASE_SENSITIVE 1 -#elif TARGET_IPHONE_SIMULATOR -#define HOST_IS_CASE_SENSITIVE 0 -#else -// Mac OSX case sensitivity defaults off, but is user configurable (when -// creating a filesytem), so assume the worst: -#define HOST_IS_CASE_SENSITIVE 1 -#endif - -#elif defined(_WIN32) || defined(__SYMBIAN32__) -#define HOST_IS_CASE_SENSITIVE 0 - -#else // Android, Linux, BSD (and the rest?) -#define HOST_IS_CASE_SENSITIVE 1 - -#endif - -#if HOST_IS_CASE_SENSITIVE -enum FixPathCaseBehavior { - FPC_FILE_MUST_EXIST, // all path components must exist (rmdir, move from) - FPC_PATH_MUST_EXIST, // all except the last one must exist - still tries to fix last one (fopen, move to) - FPC_PARTIAL_ALLOWED, // don't care how many exist (mkdir recursive) -}; - -bool FixPathCase(std::string& basePath, std::string &path, FixPathCaseBehavior behavior); -#endif - -struct DirectoryFileHandle -{ -#ifdef _WIN32 - HANDLE hFile; -#else - FILE* hFile; -#endif - DirectoryFileHandle() - { -#ifdef _WIN32 - hFile = (HANDLE)-1; -#else - hFile = 0; -#endif - } - - std::string GetLocalPath(std::string& basePath, std::string localpath); - bool Open(std::string& basePath, std::string& fileName, FileAccess access); - size_t Read(u8* pointer, s64 size); - size_t Write(const u8* pointer, s64 size); - size_t Seek(s32 position, FileMove type); - void Close(); -}; - -class DirectoryFileSystem : public IFileSystem { -public: - DirectoryFileSystem(IHandleAllocator *_hAlloc, std::string _basePath); - ~DirectoryFileSystem(); - - void DoState(PointerWrap &p); - std::vector<FileInfo> GetDirListing(std::string path); - u32 OpenFile(std::string filename, FileAccess access, const char *devicename=NULL); - void CloseFile(u32 handle); - size_t ReadFile(u32 handle, u8 *pointer, s64 size); - size_t WriteFile(u32 handle, const u8 *pointer, s64 size); - size_t SeekFile(u32 handle, s32 position, FileMove type); - FileInfo GetFileInfo(std::string filename); - bool OwnsHandle(u32 handle); - - bool MkDir(const std::string &dirname); - bool RmDir(const std::string &dirname); - int RenameFile(const std::string &from, const std::string &to); - bool RemoveFile(const std::string &filename); - bool GetHostPath(const std::string &inpath, std::string &outpath); - -private: - struct OpenFileEntry { - DirectoryFileHandle hFile; - }; - - typedef std::map<u32, OpenFileEntry> EntryMap; - EntryMap entries; - std::string basePath; - IHandleAllocator *hAlloc; - - // In case of Windows: Translate slashes, etc. - std::string GetLocalPath(std::string localpath); -}; - -// VFSFileSystem: Ability to map in Android APK paths as well! Does not support all features, only meant for fonts. -// Very inefficient - always load the whole file on open. -class VFSFileSystem : public IFileSystem { -public: - VFSFileSystem(IHandleAllocator *_hAlloc, std::string _basePath); - ~VFSFileSystem(); - - void DoState(PointerWrap &p); - std::vector<FileInfo> GetDirListing(std::string path); - u32 OpenFile(std::string filename, FileAccess access, const char *devicename=NULL); - void CloseFile(u32 handle); - size_t ReadFile(u32 handle, u8 *pointer, s64 size); - size_t WriteFile(u32 handle, const u8 *pointer, s64 size); - size_t SeekFile(u32 handle, s32 position, FileMove type); - FileInfo GetFileInfo(std::string filename); - bool OwnsHandle(u32 handle); - - bool MkDir(const std::string &dirname); - bool RmDir(const std::string &dirname); - int RenameFile(const std::string &from, const std::string &to); - bool RemoveFile(const std::string &filename); - bool GetHostPath(const std::string &inpath, std::string &outpath); - -private: - struct OpenFileEntry { - u8 *fileData; - size_t size; - size_t seekPos; - }; - - typedef std::map<u32, OpenFileEntry> EntryMap; - EntryMap entries; - std::string basePath; - IHandleAllocator *hAlloc; - - std::string GetLocalPath(std::string localpath); -}; diff --git a/src/core/file_sys/file_sys.h b/src/core/file_sys/file_sys.h deleted file mode 100644 index bb8503e62..000000000 --- a/src/core/file_sys/file_sys.h +++ /dev/null @@ -1,138 +0,0 @@ -// Copyright (c) 2012- PPSSPP Project. - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, version 2.0 or later versions. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License 2.0 for more details. - -// A copy of the GPL 2.0 should have been included with the program. -// If not, see http://www.gnu.org/licenses/ - -// Official git repository and contact information can be found at -// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. - -#pragma once - -#include "common/common.h" -#include "common/chunk_file.h" - -enum FileAccess { - FILEACCESS_NONE=0, - FILEACCESS_READ=1, - FILEACCESS_WRITE=2, - FILEACCESS_APPEND=4, - FILEACCESS_CREATE=8 -}; - -enum FileMove { - FILEMOVE_BEGIN=0, - FILEMOVE_CURRENT=1, - FILEMOVE_END=2 -}; - -enum FileType { - FILETYPE_NORMAL=1, - FILETYPE_DIRECTORY=2 -}; - - -class IHandleAllocator { -public: - virtual ~IHandleAllocator() {} - virtual u32 GetNewHandle() = 0; - virtual void FreeHandle(u32 handle) = 0; -}; - -class SequentialHandleAllocator : public IHandleAllocator { -public: - SequentialHandleAllocator() : handle_(1) {} - virtual u32 GetNewHandle() { return handle_++; } - virtual void FreeHandle(u32 handle) {} -private: - int handle_; -}; - -struct FileInfo { - FileInfo() - : size(0), access(0), exists(false), type(FILETYPE_NORMAL), isOnSectorSystem(false), startSector(0), numSectors(0) {} - - void DoState(PointerWrap &p) { - auto s = p.Section("FileInfo", 1); - if (!s) - return; - - p.Do(name); - p.Do(size); - p.Do(access); - p.Do(exists); - p.Do(type); - p.Do(atime); - p.Do(ctime); - p.Do(mtime); - p.Do(isOnSectorSystem); - p.Do(startSector); - p.Do(numSectors); - p.Do(sectorSize); - } - - std::string name; - s64 size; - u32 access; //unix 777 - bool exists; - FileType type; - - tm atime; - tm ctime; - tm mtime; - - bool isOnSectorSystem; - u32 startSector; - u32 numSectors; - u32 sectorSize; -}; - - -class IFileSystem { -public: - virtual ~IFileSystem() {} - - virtual void DoState(PointerWrap &p) = 0; - virtual std::vector<FileInfo> GetDirListing(std::string path) = 0; - virtual u32 OpenFile(std::string filename, FileAccess access, const char *devicename=NULL) = 0; - virtual void CloseFile(u32 handle) = 0; - virtual size_t ReadFile(u32 handle, u8 *pointer, s64 size) = 0; - virtual size_t WriteFile(u32 handle, const u8 *pointer, s64 size) = 0; - virtual size_t SeekFile(u32 handle, s32 position, FileMove type) = 0; - virtual FileInfo GetFileInfo(std::string filename) = 0; - virtual bool OwnsHandle(u32 handle) = 0; - virtual bool MkDir(const std::string &dirname) = 0; - virtual bool RmDir(const std::string &dirname) = 0; - virtual int RenameFile(const std::string &from, const std::string &to) = 0; - virtual bool RemoveFile(const std::string &filename) = 0; - virtual bool GetHostPath(const std::string &inpath, std::string &outpath) = 0; -}; - - -class EmptyFileSystem : public IFileSystem { -public: - virtual void DoState(PointerWrap &p) {} - std::vector<FileInfo> GetDirListing(std::string path) {std::vector<FileInfo> vec; return vec;} - u32 OpenFile(std::string filename, FileAccess access, const char *devicename=NULL) {return 0;} - void CloseFile(u32 handle) {} - size_t ReadFile(u32 handle, u8 *pointer, s64 size) {return 0;} - size_t WriteFile(u32 handle, const u8 *pointer, s64 size) {return 0;} - size_t SeekFile(u32 handle, s32 position, FileMove type) {return 0;} - FileInfo GetFileInfo(std::string filename) {FileInfo f; return f;} - bool OwnsHandle(u32 handle) {return false;} - virtual bool MkDir(const std::string &dirname) {return false;} - virtual bool RmDir(const std::string &dirname) {return false;} - virtual int RenameFile(const std::string &from, const std::string &to) {return -1;} - virtual bool RemoveFile(const std::string &filename) {return false;} - virtual bool GetHostPath(const std::string &inpath, std::string &outpath) {return false;} -}; - - diff --git a/src/core/file_sys/meta_file_system.cpp b/src/core/file_sys/meta_file_system.cpp deleted file mode 100644 index 4347ff451..000000000 --- a/src/core/file_sys/meta_file_system.cpp +++ /dev/null @@ -1,519 +0,0 @@ -// Copyright (c) 2012- PPSSPP Project. - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, version 2.0 or later versions. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License 2.0 for more details. - -// A copy of the GPL 2.0 should have been included with the program. -// If not, see http://www.gnu.org/licenses/ - -// Official git repository and contact information can be found at -// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. - -#include <set> -#include <algorithm> - -#include "common/string_util.h" -#include "core/file_sys/meta_file_system.h" - -static bool ApplyPathStringToComponentsVector(std::vector<std::string> &vector, const std::string &pathString) -{ - size_t len = pathString.length(); - size_t start = 0; - - while (start < len) - { - size_t i = pathString.find('/', start); - if (i == std::string::npos) - i = len; - - if (i > start) - { - std::string component = pathString.substr(start, i - start); - if (component != ".") - { - if (component == "..") - { - if (vector.size() != 0) - { - vector.pop_back(); - } - else - { - // The PSP silently ignores attempts to .. to parent of root directory - WARN_LOG(FILESYS, "RealPath: ignoring .. beyond root - root directory is its own parent: \"%s\"", pathString.c_str()); - } - } - else - { - vector.push_back(component); - } - } - } - - start = i + 1; - } - - return true; -} - -/* - * Changes relative paths to absolute, removes ".", "..", and trailing "/" - * "drive:./blah" is absolute (ignore the dot) and "/blah" is relative (because it's missing "drive:") - * babel (and possibly other games) use "/directoryThatDoesNotExist/../directoryThatExists/filename" - */ -static bool RealPath(const std::string ¤tDirectory, const std::string &inPath, std::string &outPath) -{ - size_t inLen = inPath.length(); - if (inLen == 0) - { - WARN_LOG(FILESYS, "RealPath: inPath is empty"); - outPath = currentDirectory; - return true; - } - - size_t inColon = inPath.find(':'); - if (inColon + 1 == inLen) - { - // There's nothing after the colon, e.g. umd0: - this is perfectly valid. - outPath = inPath; - return true; - } - - bool relative = (inColon == std::string::npos); - - std::string prefix, inAfterColon; - std::vector<std::string> cmpnts; // path components - size_t outPathCapacityGuess = inPath.length(); - - if (relative) - { - size_t curDirLen = currentDirectory.length(); - if (curDirLen == 0) - { - ERROR_LOG(FILESYS, "RealPath: inPath \"%s\" is relative, but current directory is empty", inPath.c_str()); - return false; - } - - size_t curDirColon = currentDirectory.find(':'); - if (curDirColon == std::string::npos) - { - ERROR_LOG(FILESYS, "RealPath: inPath \"%s\" is relative, but current directory \"%s\" has no prefix", inPath.c_str(), currentDirectory.c_str()); - return false; - } - if (curDirColon + 1 == curDirLen) - { - ERROR_LOG(FILESYS, "RealPath: inPath \"%s\" is relative, but current directory \"%s\" is all prefix and no path. Using \"/\" as path for current directory.", inPath.c_str(), currentDirectory.c_str()); - } - else - { - const std::string curDirAfter = currentDirectory.substr(curDirColon + 1); - if (! ApplyPathStringToComponentsVector(cmpnts, curDirAfter) ) - { - ERROR_LOG(FILESYS,"RealPath: currentDirectory is not a valid path: \"%s\"", currentDirectory.c_str()); - return false; - } - - outPathCapacityGuess += curDirLen; - } - - prefix = currentDirectory.substr(0, curDirColon + 1); - inAfterColon = inPath; - } - else - { - prefix = inPath.substr(0, inColon + 1); - inAfterColon = inPath.substr(inColon + 1); - } - - // Special case: "disc0:" is different from "disc0:/", so keep track of the single slash. - if (inAfterColon == "/") - { - outPath = prefix + inAfterColon; - return true; - } - - if (! ApplyPathStringToComponentsVector(cmpnts, inAfterColon) ) - { - WARN_LOG(FILESYS, "RealPath: inPath is not a valid path: \"%s\"", inPath.c_str()); - return false; - } - - outPath.clear(); - outPath.reserve(outPathCapacityGuess); - - outPath.append(prefix); - - size_t numCmpnts = cmpnts.size(); - for (size_t i = 0; i < numCmpnts; i++) - { - outPath.append(1, '/'); - outPath.append(cmpnts[i]); - } - - return true; -} - -IFileSystem *MetaFileSystem::GetHandleOwner(u32 handle) -{ - std::lock_guard<std::recursive_mutex> guard(lock); - for (size_t i = 0; i < fileSystems.size(); i++) - { - if (fileSystems[i].system->OwnsHandle(handle)) - return fileSystems[i].system; //got it! - } - //none found? - return 0; -} - -bool MetaFileSystem::MapFilePath(const std::string &_inpath, std::string &outpath, MountPoint **system) -{ - std::lock_guard<std::recursive_mutex> guard(lock); - std::string realpath; - - // Special handling: host0:command.txt (as seen in Super Monkey Ball Adventures, for example) - // appears to mean the current directory on the UMD. Let's just assume the current directory. - std::string inpath = _inpath; - if (strncasecmp(inpath.c_str(), "host0:", strlen("host0:")) == 0) { - INFO_LOG(FILESYS, "Host0 path detected, stripping: %s", inpath.c_str()); - inpath = inpath.substr(strlen("host0:")); - } - - const std::string *currentDirectory = &startingDirectory; - - _assert_msg_(FILESYS, false, "must implement equiv of __KernelGetCurThread"); - - int currentThread = 0;//__KernelGetCurThread(); - currentDir_t::iterator it = currentDir.find(currentThread); - if (it == currentDir.end()) - { - //Attempt to emulate SCE_KERNEL_ERROR_NOCWD / 8002032C: may break things requiring fixes elsewhere - if (inpath.find(':') == std::string::npos /* means path is relative */) - { - lastOpenError = -1;//SCE_KERNEL_ERROR_NOCWD; - WARN_LOG(FILESYS, "Path is relative, but current directory not set for thread %i. returning 8002032C(SCE_KERNEL_ERROR_NOCWD) instead.", currentThread); - } - } - else - { - currentDirectory = &(it->second); - } - - if ( RealPath(*currentDirectory, inpath, realpath) ) - { - for (size_t i = 0; i < fileSystems.size(); i++) - { - size_t prefLen = fileSystems[i].prefix.size(); - if (strncasecmp(fileSystems[i].prefix.c_str(), realpath.c_str(), prefLen) == 0) - { - outpath = realpath.substr(prefLen); - *system = &(fileSystems[i]); - - INFO_LOG(FILESYS, "MapFilePath: mapped \"%s\" to prefix: \"%s\", path: \"%s\"", inpath.c_str(), fileSystems[i].prefix.c_str(), outpath.c_str()); - - return true; - } - } - } - - DEBUG_LOG(FILESYS, "MapFilePath: failed mapping \"%s\", returning false", inpath.c_str()); - return false; -} - -void MetaFileSystem::Mount(std::string prefix, IFileSystem *system) -{ - std::lock_guard<std::recursive_mutex> guard(lock); - MountPoint x; - x.prefix = prefix; - x.system = system; - fileSystems.push_back(x); -} - -void MetaFileSystem::Unmount(std::string prefix, IFileSystem *system) -{ - std::lock_guard<std::recursive_mutex> guard(lock); - MountPoint x; - x.prefix = prefix; - x.system = system; - fileSystems.erase(std::remove(fileSystems.begin(), fileSystems.end(), x), fileSystems.end()); -} - -void MetaFileSystem::Shutdown() -{ - std::lock_guard<std::recursive_mutex> guard(lock); - current = 6; - - // Ownership is a bit convoluted. Let's just delete everything once. - - std::set<IFileSystem *> toDelete; - for (size_t i = 0; i < fileSystems.size(); i++) { - toDelete.insert(fileSystems[i].system); - } - - for (auto iter = toDelete.begin(); iter != toDelete.end(); ++iter) - { - delete *iter; - } - - fileSystems.clear(); - currentDir.clear(); - startingDirectory = ""; -} - -u32 MetaFileSystem::OpenWithError(int &error, std::string filename, FileAccess access, const char *devicename) -{ - std::lock_guard<std::recursive_mutex> guard(lock); - u32 h = OpenFile(filename, access, devicename); - error = lastOpenError; - return h; -} - -u32 MetaFileSystem::OpenFile(std::string filename, FileAccess access, const char *devicename) -{ - std::lock_guard<std::recursive_mutex> guard(lock); - lastOpenError = 0; - std::string of; - MountPoint *mount; - if (MapFilePath(filename, of, &mount)) - { - return mount->system->OpenFile(of, access, mount->prefix.c_str()); - } - else - { - return 0; - } -} - -FileInfo MetaFileSystem::GetFileInfo(std::string filename) -{ - std::lock_guard<std::recursive_mutex> guard(lock); - std::string of; - IFileSystem *system; - if (MapFilePath(filename, of, &system)) - { - return system->GetFileInfo(of); - } - else - { - FileInfo bogus; // TODO - return bogus; - } -} - -bool MetaFileSystem::GetHostPath(const std::string &inpath, std::string &outpath) -{ - std::lock_guard<std::recursive_mutex> guard(lock); - std::string of; - IFileSystem *system; - if (MapFilePath(inpath, of, &system)) { - return system->GetHostPath(of, outpath); - } else { - return false; - } -} - -std::vector<FileInfo> MetaFileSystem::GetDirListing(std::string path) -{ - std::lock_guard<std::recursive_mutex> guard(lock); - std::string of; - IFileSystem *system; - if (MapFilePath(path, of, &system)) - { - return system->GetDirListing(of); - } - else - { - std::vector<FileInfo> empty; - return empty; - } -} - -void MetaFileSystem::ThreadEnded(int threadID) -{ - std::lock_guard<std::recursive_mutex> guard(lock); - currentDir.erase(threadID); -} - -int MetaFileSystem::ChDir(const std::string &dir) -{ - std::lock_guard<std::recursive_mutex> guard(lock); - // Retain the old path and fail if the arg is 1023 bytes or longer. - if (dir.size() >= 1023) - return -1;//SCE_KERNEL_ERROR_NAMETOOLONG; - - _assert_msg_(FILESYS, false, "must implement equiv of __KernelGetCurThread"); - - int curThread = 0; //__KernelGetCurThread(); - - std::string of; - MountPoint *mountPoint; - if (MapFilePath(dir, of, &mountPoint)) - { - currentDir[curThread] = mountPoint->prefix + of; - return 0; - } - else - { - for (size_t i = 0; i < fileSystems.size(); i++) - { - const std::string &prefix = fileSystems[i].prefix; - if (strncasecmp(prefix.c_str(), dir.c_str(), prefix.size()) == 0) - { - // The PSP is completely happy with invalid current dirs as long as they have a valid device. - WARN_LOG(FILESYS, "ChDir failed to map path \"%s\", saving as current directory anyway", dir.c_str()); - currentDir[curThread] = dir; - return 0; - } - } - - WARN_LOG(FILESYS, "ChDir failed to map device for \"%s\", failing", dir.c_str()); - return -1;//SCE_KERNEL_ERROR_NODEV; - } -} - -bool MetaFileSystem::MkDir(const std::string &dirname) -{ - std::lock_guard<std::recursive_mutex> guard(lock); - std::string of; - IFileSystem *system; - if (MapFilePath(dirname, of, &system)) - { - return system->MkDir(of); - } - else - { - return false; - } -} - -bool MetaFileSystem::RmDir(const std::string &dirname) -{ - std::lock_guard<std::recursive_mutex> guard(lock); - std::string of; - IFileSystem *system; - if (MapFilePath(dirname, of, &system)) - { - return system->RmDir(of); - } - else - { - return false; - } -} - -int MetaFileSystem::RenameFile(const std::string &from, const std::string &to) -{ - std::lock_guard<std::recursive_mutex> guard(lock); - std::string of; - std::string rf; - IFileSystem *osystem; - IFileSystem *rsystem = NULL; - if (MapFilePath(from, of, &osystem)) - { - // If it's a relative path, it seems to always use from's filesystem. - if (to.find(":/") != to.npos) - { - if (!MapFilePath(to, rf, &rsystem)) - return -1; - } - else - { - rf = to; - rsystem = osystem; - } - - if (osystem != rsystem) - return -1;//SCE_KERNEL_ERROR_XDEV; - - return osystem->RenameFile(of, rf); - } - else - { - return -1; - } -} - -bool MetaFileSystem::RemoveFile(const std::string &filename) -{ - std::lock_guard<std::recursive_mutex> guard(lock); - std::string of; - IFileSystem *system; - if (MapFilePath(filename, of, &system)) - { - return system->RemoveFile(of); - } - else - { - return false; - } -} - -void MetaFileSystem::CloseFile(u32 handle) -{ - std::lock_guard<std::recursive_mutex> guard(lock); - IFileSystem *sys = GetHandleOwner(handle); - if (sys) - sys->CloseFile(handle); -} - -size_t MetaFileSystem::ReadFile(u32 handle, u8 *pointer, s64 size) -{ - std::lock_guard<std::recursive_mutex> guard(lock); - IFileSystem *sys = GetHandleOwner(handle); - if (sys) - return sys->ReadFile(handle,pointer,size); - else - return 0; -} - -size_t MetaFileSystem::WriteFile(u32 handle, const u8 *pointer, s64 size) -{ - std::lock_guard<std::recursive_mutex> guard(lock); - IFileSystem *sys = GetHandleOwner(handle); - if (sys) - return sys->WriteFile(handle,pointer,size); - else - return 0; -} - -size_t MetaFileSystem::SeekFile(u32 handle, s32 position, FileMove type) -{ - std::lock_guard<std::recursive_mutex> guard(lock); - IFileSystem *sys = GetHandleOwner(handle); - if (sys) - return sys->SeekFile(handle,position,type); - else - return 0; -} - -void MetaFileSystem::DoState(PointerWrap &p) -{ - std::lock_guard<std::recursive_mutex> guard(lock); - - auto s = p.Section("MetaFileSystem", 1); - if (!s) - return; - - p.Do(current); - - // Save/load per-thread current directory map - p.Do(currentDir); - - u32 n = (u32) fileSystems.size(); - p.Do(n); - if (n != (u32) fileSystems.size()) - { - p.SetError(p.ERROR_FAILURE); - ERROR_LOG(FILESYS, "Savestate failure: number of filesystems doesn't match."); - return; - } - - for (u32 i = 0; i < n; ++i) - fileSystems[i].system->DoState(p); -} - diff --git a/src/core/file_sys/meta_file_system.h b/src/core/file_sys/meta_file_system.h deleted file mode 100644 index f358d8d5c..000000000 --- a/src/core/file_sys/meta_file_system.h +++ /dev/null @@ -1,110 +0,0 @@ -// Copyright (c) 2012- PPSSPP Project. - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, version 2.0 or later versions. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License 2.0 for more details. - -// A copy of the GPL 2.0 should have been included with the program. -// If not, see http://www.gnu.org/licenses/ - -// Official git repository and contact information can be found at -// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. - -#pragma once - -#include "common/std_mutex.h" - -#include "core/file_sys/file_sys.h" - -class MetaFileSystem : public IHandleAllocator, public IFileSystem -{ -private: - u32 current; - struct MountPoint - { - std::string prefix; - IFileSystem *system; - - bool operator == (const MountPoint &other) const - { - return prefix == other.prefix && system == other.system; - } - }; - std::vector<MountPoint> fileSystems; - - typedef std::map<int, std::string> currentDir_t; - currentDir_t currentDir; - - std::string startingDirectory; - int lastOpenError; - std::recursive_mutex lock; - -public: - MetaFileSystem() - { - current = 6; // what? - } - - void Mount(std::string prefix, IFileSystem *system); - void Unmount(std::string prefix, IFileSystem *system); - - void ThreadEnded(int threadID); - - void Shutdown(); - - u32 GetNewHandle() {return current++;} - void FreeHandle(u32 handle) {} - - virtual void DoState(PointerWrap &p); - - IFileSystem *GetHandleOwner(u32 handle); - bool MapFilePath(const std::string &inpath, std::string &outpath, MountPoint **system); - - inline bool MapFilePath(const std::string &_inpath, std::string &outpath, IFileSystem **system) - { - MountPoint *mountPoint; - if (MapFilePath(_inpath, outpath, &mountPoint)) - { - *system = mountPoint->system; - return true; - } - - return false; - } - - // Only possible if a file system is a DirectoryFileSystem or similar. - bool GetHostPath(const std::string &inpath, std::string &outpath); - - std::vector<FileInfo> GetDirListing(std::string path); - u32 OpenFile(std::string filename, FileAccess access, const char *devicename = NULL); - u32 OpenWithError(int &error, std::string filename, FileAccess access, const char *devicename = NULL); - void CloseFile(u32 handle); - size_t ReadFile(u32 handle, u8 *pointer, s64 size); - size_t WriteFile(u32 handle, const u8 *pointer, s64 size); - size_t SeekFile(u32 handle, s32 position, FileMove type); - FileInfo GetFileInfo(std::string filename); - bool OwnsHandle(u32 handle) {return false;} - inline size_t GetSeekPos(u32 handle) - { - return SeekFile(handle, 0, FILEMOVE_CURRENT); - } - - virtual int ChDir(const std::string &dir); - - virtual bool MkDir(const std::string &dirname); - virtual bool RmDir(const std::string &dirname); - virtual int RenameFile(const std::string &from, const std::string &to); - virtual bool RemoveFile(const std::string &filename); - - // TODO: void IoCtl(...) - - void SetStartingDirectory(const std::string &dir) { - std::lock_guard<std::recursive_mutex> guard(lock); - startingDirectory = dir; - } -}; diff --git a/src/core/hle/config_mem.h b/src/core/hle/config_mem.h index da396a3e6..fa01b5cdb 100644 --- a/src/core/hle/config_mem.h +++ b/src/core/hle/config_mem.h @@ -1,10 +1,10 @@ // Copyright 2014 Citra Emulator Project // Licensed under GPLv2 -// Refer to the license.txt file included. +// Refer to the license.txt file included. #pragma once -// Configuration memory stores various hardware/kernel configuration settings. This memory page is +// Configuration memory stores various hardware/kernel configuration settings. This memory page is // read-only for ARM11 processes. I'm guessing this would normally be written to by the firmware/ // bootrom. Because we're not emulating this, and essentially just "stubbing" the functionality, I'm // putting this as a subset of HLE for now. @@ -16,6 +16,6 @@ namespace ConfigMem { template <typename T> -inline void Read(T &var, const u32 addr); +void Read(T &var, const u32 addr); } // namespace diff --git a/src/core/hle/function_wrappers.h b/src/core/hle/function_wrappers.h index 0bed78653..ea603a1bb 100644 --- a/src/core/hle/function_wrappers.h +++ b/src/core/hle/function_wrappers.h @@ -39,9 +39,16 @@ template<s32 func(s32*, u32*, s32, bool, s64)> void Wrap() { RETURN(retval); } -// TODO(bunnei): Is this correct? Probably not +// TODO(bunnei): Is this correct? Probably not - Last parameter looks wrong for ArbitrateAddress template<s32 func(u32, u32, u32, u32, s64)> void Wrap() { - RETURN(func(PARAM(5), PARAM(1), PARAM(2), PARAM(3), (((s64)PARAM(4) << 32) | PARAM(0)))); + RETURN(func(PARAM(0), PARAM(1), PARAM(2), PARAM(3), (((s64)PARAM(5) << 32) | PARAM(4)))); +} + +template<s32 func(u32*)> void Wrap(){ + u32 param_1 = 0; + u32 retval = func(¶m_1); + Core::g_app_core->SetReg(1, param_1); + RETURN(retval); } template<s32 func(u32, s64)> void Wrap() { diff --git a/src/core/hle/kernel/address_arbiter.cpp b/src/core/hle/kernel/address_arbiter.cpp new file mode 100644 index 000000000..61717bbe4 --- /dev/null +++ b/src/core/hle/kernel/address_arbiter.cpp @@ -0,0 +1,87 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#include "common/common_types.h" + +#include "core/mem_map.h" + +#include "core/hle/hle.h" +#include "core/hle/kernel/address_arbiter.h" +#include "core/hle/kernel/thread.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Kernel namespace + +namespace Kernel { + +class AddressArbiter : public Object { +public: + const char* GetTypeName() const { return "Arbiter"; } + const char* GetName() const { return name.c_str(); } + + static Kernel::HandleType GetStaticHandleType() { return HandleType::AddressArbiter; } + Kernel::HandleType GetHandleType() const { return HandleType::AddressArbiter; } + + std::string name; ///< Name of address arbiter object (optional) + + /** + * Wait for kernel object to synchronize + * @param wait Boolean wait set if current thread should wait as a result of sync operation + * @return Result of operation, 0 on success, otherwise error code + */ + Result WaitSynchronization(bool* wait) { + // TODO(bunnei): ImplementMe + ERROR_LOG(OSHLE, "(UNIMPLEMENTED)"); + return 0; + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/// Arbitrate an address +Result ArbitrateAddress(Handle handle, ArbitrationType type, u32 address, s32 value) { + switch (type) { + + // Signal thread(s) waiting for arbitrate address... + case ArbitrationType::Signal: + // Negative value means resume all threads + if (value < 0) { + ArbitrateAllThreads(handle, address); + } else { + // Resume first N threads + for(int i = 0; i < value; i++) + ArbitrateHighestPriorityThread(handle, address); + } + HLE::Reschedule(__func__); + + // Wait current thread (acquire the arbiter)... + case ArbitrationType::WaitIfLessThan: + if ((s32)Memory::Read32(address) <= value) { + Kernel::WaitCurrentThread(WAITTYPE_ARB, handle); + HLE::Reschedule(__func__); + } + + default: + ERROR_LOG(KERNEL, "unknown type=%d", type); + return -1; + } + return 0; +} + +/// Create an address arbiter +AddressArbiter* CreateAddressArbiter(Handle& handle, const std::string& name) { + AddressArbiter* address_arbiter = new AddressArbiter; + handle = Kernel::g_object_pool.Create(address_arbiter); + address_arbiter->name = name; + return address_arbiter; +} + +/// Create an address arbiter +Handle CreateAddressArbiter(const std::string& name) { + Handle handle; + CreateAddressArbiter(handle, name); + return handle; +} + +} // namespace Kernel diff --git a/src/core/hle/kernel/address_arbiter.h b/src/core/hle/kernel/address_arbiter.h new file mode 100644 index 000000000..a483fe466 --- /dev/null +++ b/src/core/hle/kernel/address_arbiter.h @@ -0,0 +1,36 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#pragma once + +#include "common/common_types.h" + +#include "core/hle/kernel/kernel.h" + +// Address arbiters are an underlying kernel synchronization object that can be created/used via +// supervisor calls (SVCs). They function as sort of a global lock. Typically, games/other CTR +// applications use them as an underlying mechanism to implement thread-safe barriers, events, and +// semphores. + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Kernel namespace + +namespace Kernel { + +/// Address arbitration types +enum class ArbitrationType : u32 { + Signal, + WaitIfLessThan, + DecrementAndWaitIfLessThan, + WaitIfLessThanWithTimeout, + DecrementAndWaitIfLessThanWithTimeout, +}; + +/// Arbitrate an address +Result ArbitrateAddress(Handle handle, ArbitrationType type, u32 address, s32 value); + +/// Create an address arbiter +Handle CreateAddressArbiter(const std::string& name = "Unknown"); + +} // namespace FileSys diff --git a/src/core/hle/kernel/archive.cpp b/src/core/hle/kernel/archive.cpp new file mode 100644 index 000000000..76b2520da --- /dev/null +++ b/src/core/hle/kernel/archive.cpp @@ -0,0 +1,157 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#include "common/common_types.h" + +#include "core/file_sys/archive.h" +#include "core/hle/service/service.h" +#include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/archive.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Kernel namespace + +namespace Kernel { + +// Command to access archive file +enum class FileCommand : u32 { + Dummy1 = 0x000100C6, + Control = 0x040100C4, + OpenSubFile = 0x08010100, + Read = 0x080200C2, + Write = 0x08030102, + GetSize = 0x08040000, + SetSize = 0x08050080, + GetAttributes = 0x08060000, + SetAttributes = 0x08070040, + Close = 0x08080000, + Flush = 0x08090000, +}; + +class Archive : public Object { +public: + const char* GetTypeName() const { return "Archive"; } + const char* GetName() const { return name.c_str(); } + + static Kernel::HandleType GetStaticHandleType() { return HandleType::Archive; } + Kernel::HandleType GetHandleType() const { return HandleType::Archive; } + + std::string name; ///< Name of archive (optional) + FileSys::Archive* backend; ///< Archive backend interface + + /** + * Synchronize kernel object + * @param wait Boolean wait set if current thread should wait as a result of sync operation + * @return Result of operation, 0 on success, otherwise error code + */ + Result SyncRequest(bool* wait) { + u32* cmd_buff = Service::GetCommandBuffer(); + FileCommand cmd = static_cast<FileCommand>(cmd_buff[0]); + switch (cmd) { + + // Read from archive... + case FileCommand::Read: + { + u64 offset = cmd_buff[1] | ((u64) cmd_buff[2]) << 32; + u32 length = cmd_buff[3]; + u32 address = cmd_buff[5]; + cmd_buff[2] = backend->Read(offset, length, Memory::GetPointer(address)); + break; + } + + // Unknown command... + default: + ERROR_LOG(KERNEL, "Unknown command=0x%08X!", cmd); + return -1; + } + cmd_buff[1] = 0; // No error + return 0; + } + + /** + * Wait for kernel object to synchronize + * @param wait Boolean wait set if current thread should wait as a result of sync operation + * @return Result of operation, 0 on success, otherwise error code + */ + Result WaitSynchronization(bool* wait) { + // TODO(bunnei): ImplementMe + ERROR_LOG(OSHLE, "(UNIMPLEMENTED)"); + return 0; + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +std::map<FileSys::Archive::IdCode, Handle> g_archive_map; ///< Map of file archives by IdCode + +/** + * Opens an archive + * @param id_code IdCode of the archive to open + * @return Handle to archive if it exists, otherwise a null handle (0) + */ +Handle OpenArchive(FileSys::Archive::IdCode id_code) { + auto itr = g_archive_map.find(id_code); + if (itr == g_archive_map.end()) { + return 0; + } + return itr->second; +} + +/** + * Mounts an archive + * @param archive Pointer to the archive to mount + * @return Result of operation, 0 on success, otherwise error code + */ +Result MountArchive(Archive* archive) { + FileSys::Archive::IdCode id_code = archive->backend->GetIdCode(); + if (0 != OpenArchive(id_code)) { + ERROR_LOG(KERNEL, "Cannot mount two archives with the same ID code! (%d)", (int) id_code); + return -1; + } + g_archive_map[id_code] = archive->GetHandle(); + INFO_LOG(KERNEL, "Mounted archive %s", archive->GetName()); + return 0; +} + +/** + * Creates an Archive + * @param handle Handle to newly created archive object + * @param backend File system backend interface to the archive + * @param name Optional name of Archive + * @return Newly created Archive object + */ +Archive* CreateArchive(Handle& handle, FileSys::Archive* backend, const std::string& name) { + Archive* archive = new Archive; + handle = Kernel::g_object_pool.Create(archive); + archive->name = name; + archive->backend = backend; + + MountArchive(archive); + + return archive; +} + +/** + * Creates an Archive + * @param backend File system backend interface to the archive + * @param name Optional name of Archive + * @return Handle to newly created Archive object + */ +Handle CreateArchive(FileSys::Archive* backend, const std::string& name) { + Handle handle; + Archive* archive = CreateArchive(handle, backend, name); + return handle; +} + +/// Initialize archives +void ArchiveInit() { + g_archive_map.clear(); +} + +/// Shutdown archives +void ArchiveShutdown() { + g_archive_map.clear(); +} + +} // namespace Kernel diff --git a/src/core/hle/kernel/archive.h b/src/core/hle/kernel/archive.h new file mode 100644 index 000000000..3758e7061 --- /dev/null +++ b/src/core/hle/kernel/archive.h @@ -0,0 +1,38 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#pragma once + +#include "common/common_types.h" + +#include "core/hle/kernel/kernel.h" +#include "core/file_sys/archive.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Kernel namespace + +namespace Kernel { + +/** + * Opens an archive + * @param id_code IdCode of the archive to open + * @return Handle to archive if it exists, otherwise a null handle (0) + */ +Handle OpenArchive(FileSys::Archive::IdCode id_code); + +/** + * Creates an Archive + * @param backend File system backend interface to the archive + * @param name Optional name of Archive + * @return Handle to newly created Archive object + */ +Handle CreateArchive(FileSys::Archive* backend, const std::string& name); + +/// Initialize archives +void ArchiveInit(); + +/// Shutdown archives +void ArchiveShutdown(); + +} // namespace FileSys diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index cda183add..7d9bd261e 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -9,6 +9,7 @@ #include "core/core.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/thread.h" +#include "core/hle/kernel/archive.h" namespace Kernel { @@ -133,11 +134,13 @@ Object* ObjectPool::CreateByIDType(int type) { /// Initialize the kernel void Init() { Kernel::ThreadingInit(); + Kernel::ArchiveInit(); } /// Shutdown the kernel void Shutdown() { Kernel::ThreadingShutdown(); + Kernel::ArchiveShutdown(); g_object_pool.Clear(); // Free all kernel objects } diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index 3f15da0ac..d9afcdd25 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -26,9 +26,10 @@ enum class HandleType : u32 { Redirection = 6, Thread = 7, Process = 8, - Arbiter = 9, + AddressArbiter = 9, File = 10, Semaphore = 11, + Archive = 12, }; enum { diff --git a/src/core/hle/kernel/shared_memory.cpp b/src/core/hle/kernel/shared_memory.cpp new file mode 100644 index 000000000..52823048f --- /dev/null +++ b/src/core/hle/kernel/shared_memory.cpp @@ -0,0 +1,105 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#include "common/common.h" + +#include "core/mem_map.h" +#include "core/hle/kernel/shared_memory.h" + +namespace Kernel { + +class SharedMemory : public Object { +public: + const char* GetTypeName() const { return "SharedMemory"; } + + static Kernel::HandleType GetStaticHandleType() { return Kernel::HandleType::SharedMemory; } + Kernel::HandleType GetHandleType() const { return Kernel::HandleType::SharedMemory; } + + /** + * Wait for kernel object to synchronize + * @param wait Boolean wait set if current thread should wait as a result of sync operation + * @return Result of operation, 0 on success, otherwise error code + */ + Result WaitSynchronization(bool* wait) { + // TODO(bunnei): ImplementMe + ERROR_LOG(OSHLE, "(UNIMPLEMENTED)"); + return 0; + } + + u32 base_address; ///< Address of shared memory block in RAM + MemoryPermission permissions; ///< Permissions of shared memory block (SVC field) + MemoryPermission other_permissions; ///< Other permissions of shared memory block (SVC field) + std::string name; ///< Name of shared memory object (optional) +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/** + * Creates a shared memory object + * @param handle Handle of newly created shared memory object + * @param name Name of shared memory object + * @return Pointer to newly created shared memory object + */ +SharedMemory* CreateSharedMemory(Handle& handle, const std::string& name) { + SharedMemory* shared_memory = new SharedMemory; + handle = Kernel::g_object_pool.Create(shared_memory); + shared_memory->name = name; + return shared_memory; +} + +/** + * Creates a shared memory object + * @param name Optional name of shared memory object + * @return Handle of newly created shared memory object + */ +Handle CreateSharedMemory(const std::string& name) { + Handle handle; + CreateSharedMemory(handle, name); + return handle; +} + +/** + * Maps a shared memory block to an address in system memory + * @param handle Shared memory block handle + * @param address Address in system memory to map shared memory block to + * @param permissions Memory block map permissions (specified by SVC field) + * @param other_permissions Memory block map other permissions (specified by SVC field) + * @return Result of operation, 0 on success, otherwise error code + */ +Result MapSharedMemory(u32 handle, u32 address, MemoryPermission permissions, + MemoryPermission other_permissions) { + + if (address < Memory::SHARED_MEMORY_VADDR || address >= Memory::SHARED_MEMORY_VADDR_END) { + ERROR_LOG(KERNEL, "cannot map handle=0x%08X, address=0x%08X outside of shared mem bounds!", + handle); + return -1; + } + SharedMemory* shared_memory = Kernel::g_object_pool.GetFast<SharedMemory>(handle); + _assert_msg_(KERNEL, (shared_memory != nullptr), "handle 0x%08X is not valid!", handle); + + shared_memory->base_address = address; + shared_memory->permissions = permissions; + shared_memory->other_permissions = other_permissions; + + return 0; +} + +/** + * Gets a pointer to the shared memory block + * @param handle Shared memory block handle + * @param offset Offset from the start of the shared memory block to get pointer + * @return Pointer to the shared memory block from the specified offset + */ +u8* GetSharedMemoryPointer(Handle handle, u32 offset) { + SharedMemory* shared_memory = Kernel::g_object_pool.GetFast<SharedMemory>(handle); + _assert_msg_(KERNEL, (shared_memory != nullptr), "handle 0x%08X is not valid!", handle); + + if (0 != shared_memory->base_address) + return Memory::GetPointer(shared_memory->base_address + offset); + + ERROR_LOG(KERNEL, "memory block handle=0x%08X not mapped!", handle); + return nullptr; +} + +} // namespace diff --git a/src/core/hle/kernel/shared_memory.h b/src/core/hle/kernel/shared_memory.h new file mode 100644 index 000000000..5312b8854 --- /dev/null +++ b/src/core/hle/kernel/shared_memory.h @@ -0,0 +1,48 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#pragma once + +#include "common/common_types.h" + +#include "core/hle/kernel/kernel.h" + +namespace Kernel { + +/// Permissions for mapped shared memory blocks +enum class MemoryPermission : u32 { + None = 0, + Read = (1u << 0), + Write = (1u << 1), + ReadWrite = (Read | Write), + DontCare = (1u << 28) +}; + +/** + * Creates a shared memory object + * @param name Optional name of shared memory object + * @return Handle of newly created shared memory object + */ +Handle CreateSharedMemory(const std::string& name="Unknown"); + +/** + * Maps a shared memory block to an address in system memory + * @param handle Shared memory block handle + * @param address Address in system memory to map shared memory block to + * @param permissions Memory block map permissions (specified by SVC field) + * @param other_permissions Memory block map other permissions (specified by SVC field) + * @return Result of operation, 0 on success, otherwise error code + */ +Result MapSharedMemory(u32 handle, u32 address, MemoryPermission permissions, + MemoryPermission other_permissions); + +/** + * Gets a pointer to the shared memory block + * @param handle Shared memory block handle + * @param offset Offset from the start of the shared memory block to get pointer + * @return Pointer to the shared memory block from the specified offset + */ +u8* GetSharedMemoryPointer(Handle handle, u32 offset); + +} // namespace diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index ab5a5559e..86bbf29d0 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -188,6 +188,43 @@ void ChangeThreadState(Thread* t, ThreadStatus new_status) { } } +/// Arbitrate the highest priority thread that is waiting +Handle ArbitrateHighestPriorityThread(u32 arbiter, u32 address) { + Handle highest_priority_thread = 0; + s32 priority = THREADPRIO_LOWEST; + + // Iterate through threads, find highest priority thread that is waiting to be arbitrated... + for (const auto& handle : g_thread_queue) { + + // TODO(bunnei): Verify arbiter address... + if (!VerifyWait(handle, WAITTYPE_ARB, arbiter)) + continue; + + Thread* thread = g_object_pool.GetFast<Thread>(handle); + if(thread->current_priority <= priority) { + highest_priority_thread = handle; + priority = thread->current_priority; + } + } + // If a thread was arbitrated, resume it + if (0 != highest_priority_thread) + ResumeThreadFromWait(highest_priority_thread); + + return highest_priority_thread; +} + +/// Arbitrate all threads currently waiting +void ArbitrateAllThreads(u32 arbiter, u32 address) { + + // Iterate through threads, find highest priority thread that is waiting to be arbitrated... + for (const auto& handle : g_thread_queue) { + + // TODO(bunnei): Verify arbiter address... + if (VerifyWait(handle, WAITTYPE_ARB, arbiter)) + ResumeThreadFromWait(handle); + } +} + /// Calls a thread by marking it as "ready" (note: will not actually execute until current thread yields) void CallThread(Thread* t) { // Stop waiting diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h index 04914ba90..f2bfdfa1a 100644 --- a/src/core/hle/kernel/thread.h +++ b/src/core/hle/kernel/thread.h @@ -39,6 +39,7 @@ enum WaitType { WAITTYPE_VBLANK, WAITTYPE_MUTEX, WAITTYPE_SYNCH, + WAITTYPE_ARB, }; namespace Kernel { @@ -59,6 +60,12 @@ void StopThread(Handle thread, const char* reason); /// Resumes a thread from waiting by marking it as "ready" void ResumeThreadFromWait(Handle handle); +/// Arbitrate the highest priority thread that is waiting +Handle ArbitrateHighestPriorityThread(u32 arbiter, u32 address); + +/// Arbitrate all threads currently waiting... +void ArbitrateAllThreads(u32 arbiter, u32 address); + /// Gets the current thread handle Handle GetCurrentThreadHandle(); diff --git a/src/core/hle/service/apt.cpp b/src/core/hle/service/apt.cpp index a0012b5dd..e97e7dbf7 100644 --- a/src/core/hle/service/apt.cpp +++ b/src/core/hle/service/apt.cpp @@ -15,9 +15,16 @@ namespace APT_U { +/// Signals used by APT functions +enum class SignalType : u32 { + None = 0x0, + AppJustStarted = 0x1, + ReturningToApp = 0xB, + ExitingApp = 0xC, +}; + void Initialize(Service::Interface* self) { u32* cmd_buff = Service::GetCommandBuffer(); - DEBUG_LOG(KERNEL, "called"); cmd_buff[3] = Kernel::CreateEvent(RESETTYPE_ONESHOT, "APT_U:Menu"); // APT menu event handle cmd_buff[4] = Kernel::CreateEvent(RESETTYPE_ONESHOT, "APT_U:Pause"); // APT pause event handle @@ -26,6 +33,7 @@ void Initialize(Service::Interface* self) { Kernel::SetEventLocked(cmd_buff[4], false); // Fire start event cmd_buff[1] = 0; // No error + DEBUG_LOG(KERNEL, "called"); } void GetLockHandle(Service::Interface* self) { @@ -40,15 +48,29 @@ void Enable(Service::Interface* self) { u32* cmd_buff = Service::GetCommandBuffer(); u32 unk = cmd_buff[1]; // TODO(bunnei): What is this field used for? cmd_buff[1] = 0; // No error - ERROR_LOG(KERNEL, "(UNIMPEMENTED) called unk=0x%08X", unk); + WARN_LOG(KERNEL, "(STUBBED) called unk=0x%08X", unk); } void InquireNotification(Service::Interface* self) { u32* cmd_buff = Service::GetCommandBuffer(); u32 app_id = cmd_buff[2]; cmd_buff[1] = 0; // No error - cmd_buff[3] = 0; // Signal type - ERROR_LOG(KERNEL, "(UNIMPEMENTED) called app_id=0x%08X", app_id); + cmd_buff[2] = static_cast<u32>(SignalType::None); // Signal type + WARN_LOG(KERNEL, "(STUBBED) called app_id=0x%08X", app_id); +} + +void ReceiveParameter(Service::Interface* self) { + u32* cmd_buff = Service::GetCommandBuffer(); + u32 app_id = cmd_buff[1]; + u32 buffer_size = cmd_buff[2]; + cmd_buff[1] = 0; // No error + cmd_buff[2] = 0; + cmd_buff[3] = static_cast<u32>(SignalType::AppJustStarted); // Signal type + cmd_buff[4] = 0x10; + cmd_buff[5] = 0; + cmd_buff[6] = 0; + cmd_buff[7] = 0; + WARN_LOG(KERNEL, "(STUBBED) called app_id=0x%08X, buffer_size=0x%08X", app_id, buffer_size); } const Interface::FunctionInfo FunctionTable[] = { @@ -63,73 +85,73 @@ const Interface::FunctionInfo FunctionTable[] = { {0x00090040, nullptr, "IsRegistered"}, {0x000A0040, nullptr, "GetAttribute"}, {0x000B0040, InquireNotification, "InquireNotification"}, - {0x000C0104, nullptr, "SendParameter"}, - {0x000D0080, nullptr, "ReceiveParameter"}, - {0x000E0080, nullptr, "GlanceParameter"}, - {0x000F0100, nullptr, "CancelParameter"}, - {0x001000C2, nullptr, "DebugFunc"}, - {0x001100C0, nullptr, "MapProgramIdForDebug"}, - {0x00120040, nullptr, "SetHomeMenuAppletIdForDebug"}, - {0x00130000, nullptr, "GetPreparationState"}, - {0x00140040, nullptr, "SetPreparationState"}, - {0x00150140, nullptr, "PrepareToStartApplication"}, - {0x00160040, nullptr, "PreloadLibraryApplet"}, - {0x00170040, nullptr, "FinishPreloadingLibraryApplet"}, - {0x00180040, nullptr, "PrepareToStartLibraryApplet"}, - {0x00190040, nullptr, "PrepareToStartSystemApplet"}, - {0x001A0000, nullptr, "PrepareToStartNewestHomeMenu"}, - {0x001B00C4, nullptr, "StartApplication"}, - {0x001C0000, nullptr, "WakeupApplication"}, - {0x001D0000, nullptr, "CancelApplication"}, - {0x001E0084, nullptr, "StartLibraryApplet"}, - {0x001F0084, nullptr, "StartSystemApplet"}, - {0x00200044, nullptr, "StartNewestHomeMenu"}, - {0x00210000, nullptr, "OrderToCloseApplication"}, - {0x00220040, nullptr, "PrepareToCloseApplication"}, - {0x00230040, nullptr, "PrepareToJumpToApplication"}, - {0x00240044, nullptr, "JumpToApplication"}, - {0x002500C0, nullptr, "PrepareToCloseLibraryApplet"}, - {0x00260000, nullptr, "PrepareToCloseSystemApplet"}, - {0x00270044, nullptr, "CloseApplication"}, - {0x00280044, nullptr, "CloseLibraryApplet"}, - {0x00290044, nullptr, "CloseSystemApplet"}, - {0x002A0000, nullptr, "OrderToCloseSystemApplet"}, - {0x002B0000, nullptr, "PrepareToJumpToHomeMenu"}, - {0x002C0044, nullptr, "JumpToHomeMenu"}, - {0x002D0000, nullptr, "PrepareToLeaveHomeMenu"}, - {0x002E0044, nullptr, "LeaveHomeMenu"}, - {0x002F0040, nullptr, "PrepareToLeaveResidentApplet"}, - {0x00300044, nullptr, "LeaveResidentApplet"}, - {0x00310100, nullptr, "PrepareToDoApplicationJump"}, - {0x00320084, nullptr, "DoApplicationJump"}, - {0x00330000, nullptr, "GetProgramIdOnApplicationJump"}, - {0x00340084, nullptr, "SendDeliverArg"}, - {0x00350080, nullptr, "ReceiveDeliverArg"}, - {0x00360040, nullptr, "LoadSysMenuArg"}, - {0x00370042, nullptr, "StoreSysMenuArg"}, - {0x00380040, nullptr, "PreloadResidentApplet"}, - {0x00390040, nullptr, "PrepareToStartResidentApplet"}, - {0x003A0044, nullptr, "StartResidentApplet"}, - {0x003B0040, nullptr, "CancelLibraryApplet"}, - {0x003C0042, nullptr, "SendDspSleep"}, - {0x003D0042, nullptr, "SendDspWakeUp"}, - {0x003E0080, nullptr, "ReplySleepQuery"}, - {0x003F0040, nullptr, "ReplySleepNotificationComplete"}, - {0x00400042, nullptr, "SendCaptureBufferInfo"}, - {0x00410040, nullptr, "ReceiveCaptureBufferInfo"}, - {0x00420080, nullptr, "SleepSystem"}, - {0x00430040, nullptr, "NotifyToWait"}, - {0x00440000, nullptr, "GetSharedFont"}, - {0x00450040, nullptr, "GetWirelessRebootInfo"}, - {0x00460104, nullptr, "Wrap"}, - {0x00470104, nullptr, "Unwrap"}, - {0x00480100, nullptr, "GetProgramInfo"}, - {0x00490180, nullptr, "Reboot"}, - {0x004A0040, nullptr, "GetCaptureInfo"}, - {0x004B00C2, nullptr, "AppletUtility"}, - {0x004C0000, nullptr, "SetFatalErrDispMode"}, - {0x004D0080, nullptr, "GetAppletProgramInfo"}, - {0x004E0000, nullptr, "HardwareResetAsync"}, + {0x000C0104, nullptr, "SendParameter"}, + {0x000D0080, ReceiveParameter, "ReceiveParameter"}, + {0x000E0080, nullptr, "GlanceParameter"}, + {0x000F0100, nullptr, "CancelParameter"}, + {0x001000C2, nullptr, "DebugFunc"}, + {0x001100C0, nullptr, "MapProgramIdForDebug"}, + {0x00120040, nullptr, "SetHomeMenuAppletIdForDebug"}, + {0x00130000, nullptr, "GetPreparationState"}, + {0x00140040, nullptr, "SetPreparationState"}, + {0x00150140, nullptr, "PrepareToStartApplication"}, + {0x00160040, nullptr, "PreloadLibraryApplet"}, + {0x00170040, nullptr, "FinishPreloadingLibraryApplet"}, + {0x00180040, nullptr, "PrepareToStartLibraryApplet"}, + {0x00190040, nullptr, "PrepareToStartSystemApplet"}, + {0x001A0000, nullptr, "PrepareToStartNewestHomeMenu"}, + {0x001B00C4, nullptr, "StartApplication"}, + {0x001C0000, nullptr, "WakeupApplication"}, + {0x001D0000, nullptr, "CancelApplication"}, + {0x001E0084, nullptr, "StartLibraryApplet"}, + {0x001F0084, nullptr, "StartSystemApplet"}, + {0x00200044, nullptr, "StartNewestHomeMenu"}, + {0x00210000, nullptr, "OrderToCloseApplication"}, + {0x00220040, nullptr, "PrepareToCloseApplication"}, + {0x00230040, nullptr, "PrepareToJumpToApplication"}, + {0x00240044, nullptr, "JumpToApplication"}, + {0x002500C0, nullptr, "PrepareToCloseLibraryApplet"}, + {0x00260000, nullptr, "PrepareToCloseSystemApplet"}, + {0x00270044, nullptr, "CloseApplication"}, + {0x00280044, nullptr, "CloseLibraryApplet"}, + {0x00290044, nullptr, "CloseSystemApplet"}, + {0x002A0000, nullptr, "OrderToCloseSystemApplet"}, + {0x002B0000, nullptr, "PrepareToJumpToHomeMenu"}, + {0x002C0044, nullptr, "JumpToHomeMenu"}, + {0x002D0000, nullptr, "PrepareToLeaveHomeMenu"}, + {0x002E0044, nullptr, "LeaveHomeMenu"}, + {0x002F0040, nullptr, "PrepareToLeaveResidentApplet"}, + {0x00300044, nullptr, "LeaveResidentApplet"}, + {0x00310100, nullptr, "PrepareToDoApplicationJump"}, + {0x00320084, nullptr, "DoApplicationJump"}, + {0x00330000, nullptr, "GetProgramIdOnApplicationJump"}, + {0x00340084, nullptr, "SendDeliverArg"}, + {0x00350080, nullptr, "ReceiveDeliverArg"}, + {0x00360040, nullptr, "LoadSysMenuArg"}, + {0x00370042, nullptr, "StoreSysMenuArg"}, + {0x00380040, nullptr, "PreloadResidentApplet"}, + {0x00390040, nullptr, "PrepareToStartResidentApplet"}, + {0x003A0044, nullptr, "StartResidentApplet"}, + {0x003B0040, nullptr, "CancelLibraryApplet"}, + {0x003C0042, nullptr, "SendDspSleep"}, + {0x003D0042, nullptr, "SendDspWakeUp"}, + {0x003E0080, nullptr, "ReplySleepQuery"}, + {0x003F0040, nullptr, "ReplySleepNotificationComplete"}, + {0x00400042, nullptr, "SendCaptureBufferInfo"}, + {0x00410040, nullptr, "ReceiveCaptureBufferInfo"}, + {0x00420080, nullptr, "SleepSystem"}, + {0x00430040, nullptr, "NotifyToWait"}, + {0x00440000, nullptr, "GetSharedFont"}, + {0x00450040, nullptr, "GetWirelessRebootInfo"}, + {0x00460104, nullptr, "Wrap"}, + {0x00470104, nullptr, "Unwrap"}, + {0x00480100, nullptr, "GetProgramInfo"}, + {0x00490180, nullptr, "Reboot"}, + {0x004A0040, nullptr, "GetCaptureInfo"}, + {0x004B00C2, nullptr, "AppletUtility"}, + {0x004C0000, nullptr, "SetFatalErrDispMode"}, + {0x004D0080, nullptr, "GetAppletProgramInfo"}, + {0x004E0000, nullptr, "HardwareResetAsync"}, }; //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/core/hle/service/fs.cpp b/src/core/hle/service/fs.cpp new file mode 100644 index 000000000..5eabf36ad --- /dev/null +++ b/src/core/hle/service/fs.cpp @@ -0,0 +1,148 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#include "common/common.h" + +#include "core/loader/loader.h" +#include "core/hle/hle.h" +#include "core/hle/service/fs.h" +#include "core/hle/kernel/archive.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Namespace FS_User + +namespace FS_User { + +void Initialize(Service::Interface* self) { + u32* cmd_buff = Service::GetCommandBuffer(); + cmd_buff[1] = 0; // No error + DEBUG_LOG(KERNEL, "called"); +} + +void OpenFileDirectly(Service::Interface* self) { + u32* cmd_buff = Service::GetCommandBuffer(); + + FileSys::Archive::IdCode arch_id = static_cast<FileSys::Archive::IdCode>(cmd_buff[2]); + + // TODO(bunnei): Properly implement use of these... + //u32 transaction = cmd_buff[1]; + //u32 arch_lowpath_type = cmd_buff[3]; + //u32 arch_lowpath_sz = cmd_buff[4]; + //u32 file_lowpath_type = cmd_buff[5]; + //u32 file_lowpath_sz = cmd_buff[6]; + //u32 flags = cmd_buff[7]; + //u32 attr = cmd_buff[8]; + //u32 arch_lowpath_desc = cmd_buff[9]; + //u32 arch_lowpath_ptr = cmd_buff[10]; + //u32 file_lowpath_desc = cmd_buff[11]; + //u32 file_lowpath_ptr = cmd_buff[12]; + + Handle handle = Kernel::OpenArchive(arch_id); + if (0 != handle) { + cmd_buff[1] = 0; // No error + cmd_buff[3] = handle; + } + DEBUG_LOG(KERNEL, "called"); +} + +const Interface::FunctionInfo FunctionTable[] = { + {0x000100C6, nullptr, "Dummy1"}, + {0x040100C4, nullptr, "Control"}, + {0x08010002, Initialize, "Initialize"}, + {0x080201C2, nullptr, "OpenFile"}, + {0x08030204, OpenFileDirectly, "OpenFileDirectly"}, + {0x08040142, nullptr, "DeleteFile"}, + {0x08050244, nullptr, "RenameFile"}, + {0x08060142, nullptr, "DeleteDirectory"}, + {0x08070142, nullptr, "DeleteDirectoryRecursively"}, + {0x08080202, nullptr, "CreateFile"}, + {0x08090182, nullptr, "CreateDirectory"}, + {0x080A0244, nullptr, "RenameDirectory"}, + {0x080B0102, nullptr, "OpenDirectory"}, + {0x080C00C2, nullptr, "OpenArchive"}, + {0x080D0144, nullptr, "ControlArchive"}, + {0x080E0080, nullptr, "CloseArchive"}, + {0x080F0180, nullptr, "FormatThisUserSaveData"}, + {0x08100200, nullptr, "CreateSystemSaveData"}, + {0x08110040, nullptr, "DeleteSystemSaveData"}, + {0x08120080, nullptr, "GetFreeBytes"}, + {0x08130000, nullptr, "GetCardType"}, + {0x08140000, nullptr, "GetSdmcArchiveResource"}, + {0x08150000, nullptr, "GetNandArchiveResource"}, + {0x08160000, nullptr, "GetSdmcFatfsErro"}, + {0x08170000, nullptr, "IsSdmcDetected"}, + {0x08180000, nullptr, "IsSdmcWritable"}, + {0x08190042, nullptr, "GetSdmcCid"}, + {0x081A0042, nullptr, "GetNandCid"}, + {0x081B0000, nullptr, "GetSdmcSpeedInfo"}, + {0x081C0000, nullptr, "GetNandSpeedInfo"}, + {0x081D0042, nullptr, "GetSdmcLog"}, + {0x081E0042, nullptr, "GetNandLog"}, + {0x081F0000, nullptr, "ClearSdmcLog"}, + {0x08200000, nullptr, "ClearNandLog"}, + {0x08210000, nullptr, "CardSlotIsInserted"}, + {0x08220000, nullptr, "CardSlotPowerOn"}, + {0x08230000, nullptr, "CardSlotPowerOff"}, + {0x08240000, nullptr, "CardSlotGetCardIFPowerStatus"}, + {0x08250040, nullptr, "CardNorDirectCommand"}, + {0x08260080, nullptr, "CardNorDirectCommandWithAddress"}, + {0x08270082, nullptr, "CardNorDirectRead"}, + {0x082800C2, nullptr, "CardNorDirectReadWithAddress"}, + {0x08290082, nullptr, "CardNorDirectWrite"}, + {0x082A00C2, nullptr, "CardNorDirectWriteWithAddress"}, + {0x082B00C2, nullptr, "CardNorDirectRead_4xIO"}, + {0x082C0082, nullptr, "CardNorDirectCpuWriteWithoutVerify"}, + {0x082D0040, nullptr, "CardNorDirectSectorEraseWithoutVerify"}, + {0x082E0040, nullptr, "GetProductInfo"}, + {0x082F0040, nullptr, "GetProgramLaunchInfo"}, + {0x08300182, nullptr, "CreateExtSaveData"}, + {0x08310180, nullptr, "CreateSharedExtSaveData"}, + {0x08320102, nullptr, "ReadExtSaveDataIcon"}, + {0x08330082, nullptr, "EnumerateExtSaveData"}, + {0x08340082, nullptr, "EnumerateSharedExtSaveData"}, + {0x08350080, nullptr, "DeleteExtSaveData"}, + {0x08360080, nullptr, "DeleteSharedExtSaveData"}, + {0x08370040, nullptr, "SetCardSpiBaudRate"}, + {0x08380040, nullptr, "SetCardSpiBusMode"}, + {0x08390000, nullptr, "SendInitializeInfoTo9"}, + {0x083A0100, nullptr, "GetSpecialContentIndex"}, + {0x083B00C2, nullptr, "GetLegacyRomHeader"}, + {0x083C00C2, nullptr, "GetLegacyBannerData"}, + {0x083D0100, nullptr, "CheckAuthorityToAccessExtSaveData"}, + {0x083E00C2, nullptr, "QueryTotalQuotaSize"}, + {0x083F00C0, nullptr, "GetExtDataBlockSize"}, + {0x08400040, nullptr, "AbnegateAccessRight"}, + {0x08410000, nullptr, "DeleteSdmcRoot"}, + {0x08420040, nullptr, "DeleteAllExtSaveDataOnNand"}, + {0x08430000, nullptr, "InitializeCtrFileSystem"}, + {0x08440000, nullptr, "CreateSeed"}, + {0x084500C2, nullptr, "GetFormatInfo"}, + {0x08460102, nullptr, "GetLegacyRomHeader2"}, + {0x08470180, nullptr, "FormatCtrCardUserSaveData"}, + {0x08480042, nullptr, "GetSdmcCtrRootPath"}, + {0x08490040, nullptr, "GetArchiveResource"}, + {0x084A0002, nullptr, "ExportIntegrityVerificationSeed"}, + {0x084B0002, nullptr, "ImportIntegrityVerificationSeed"}, + {0x084C0242, nullptr, "FormatSaveData"}, + {0x084D0102, nullptr, "GetLegacySubBannerData"}, + {0x084E0342, nullptr, "UpdateSha256Context"}, + {0x084F0102, nullptr, "ReadSpecialFile"}, + {0x08500040, nullptr, "GetSpecialFileSize"}, + {0x08580000, nullptr, "GetMovableSedHashedKeyYRandomData"}, + {0x08610042, nullptr, "InitializeWithSdkVersion"}, + {0x08620040, nullptr, "SetPriority"}, + {0x08630000, nullptr, "GetPriority"}, +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Interface class + +Interface::Interface() { + Register(FunctionTable, ARRAY_SIZE(FunctionTable)); +} + +Interface::~Interface() { +} + +} // namespace diff --git a/src/core/hle/service/fs.h b/src/core/hle/service/fs.h new file mode 100644 index 000000000..34b0610ad --- /dev/null +++ b/src/core/hle/service/fs.h @@ -0,0 +1,31 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#pragma once + +#include "core/hle/service/service.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Namespace FS_User + +namespace FS_User { + +/// Interface to "fs:USER" service +class Interface : public Service::Interface { +public: + + Interface(); + + ~Interface(); + + /** + * Gets the string port name used by CTROS for the service + * @return Port name of service + */ + const char *GetPortName() const { + return "Ufs:"; + } +}; + +} // namespace diff --git a/src/core/hle/service/gsp.cpp b/src/core/hle/service/gsp.cpp index f75ba75c2..b20203e27 100644 --- a/src/core/hle/service/gsp.cpp +++ b/src/core/hle/service/gsp.cpp @@ -9,6 +9,7 @@ #include "core/mem_map.h" #include "core/hle/hle.h" #include "core/hle/kernel/event.h" +#include "core/hle/kernel/shared_memory.h" #include "core/hle/service/gsp.h" #include "core/hw/gpu.h" @@ -36,14 +37,19 @@ union GX_CmdBufferHeader { BitField<8,8,u32> number_commands; }; -/// Gets the address of the start (header) of a command buffer in GSP shared memory -static inline u32 GX_GetCmdBufferAddress(u32 thread_id) { - return (0x10002000 + 0x800 + (thread_id * 0x200)); -} +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Namespace GSP_GPU + +namespace GSP_GPU { + +Handle g_event = 0; +Handle g_shared_memory = 0; + +u32 g_thread_id = 0; /// Gets a pointer to the start (header) of a command buffer in GSP shared memory static inline u8* GX_GetCmdBufferPointer(u32 thread_id, u32 offset=0) { - return Memory::GetPointer(GX_GetCmdBufferAddress(thread_id) + offset); + return Kernel::GetSharedMemoryPointer(g_shared_memory, 0x800 + (thread_id * 0x200) + offset); } /// Finishes execution of a GSP command @@ -56,122 +62,183 @@ void GX_FinishCommand(u32 thread_id) { // TODO: Increment header->index? } -//////////////////////////////////////////////////////////////////////////////////////////////////// -// Namespace GSP_GPU +/// Write a GSP GPU hardware register +void WriteHWRegs(Service::Interface* self) { + u32* cmd_buff = Service::GetCommandBuffer(); + u32 reg_addr = cmd_buff[1]; + u32 size = cmd_buff[2]; -namespace GSP_GPU { + // TODO: Return proper error codes + if (reg_addr + size >= 0x420000) { + ERROR_LOG(GPU, "Write address out of range! (address=0x%08x, size=0x%08x)", reg_addr, size); + return; + } -Handle g_event_handle = 0; -u32 g_thread_id = 0; + // size should be word-aligned + if ((size % 4) != 0) { + ERROR_LOG(GPU, "Invalid size 0x%08x", size); + return; + } -enum { - REG_FRAMEBUFFER_1 = 0x00400468, - REG_FRAMEBUFFER_2 = 0x00400494, -}; + u32* src = (u32*)Memory::GetPointer(cmd_buff[0x4]); + + while (size > 0) { + GPU::Write<u32>(reg_addr + 0x1EB00000, *src); + + size -= 4; + ++src; + reg_addr += 4; + } +} /// Read a GSP GPU hardware register void ReadHWRegs(Service::Interface* self) { - static const u32 framebuffer_1[] = {GPU::PADDR_VRAM_TOP_LEFT_FRAME1, GPU::PADDR_VRAM_TOP_RIGHT_FRAME1}; - static const u32 framebuffer_2[] = {GPU::PADDR_VRAM_TOP_LEFT_FRAME2, GPU::PADDR_VRAM_TOP_RIGHT_FRAME2}; - u32* cmd_buff = Service::GetCommandBuffer(); u32 reg_addr = cmd_buff[1]; u32 size = cmd_buff[2]; - u32* dst = (u32*)Memory::GetPointer(cmd_buff[0x41]); - switch (reg_addr) { + // TODO: Return proper error codes + if (reg_addr + size >= 0x420000) { + ERROR_LOG(GPU, "Read address out of range! (address=0x%08x, size=0x%08x)", reg_addr, size); + return; + } - // NOTE: Calling SetFramebufferLocation here is a hack... Not sure the correct way yet to set - // whether the framebuffers should be in VRAM or GSP heap, but from what I understand, if the - // user application is reading from either of these registers, then its going to be in VRAM. + // size should be word-aligned + if ((size % 4) != 0) { + ERROR_LOG(GPU, "Invalid size 0x%08x", size); + return; + } - // Top framebuffer 1 addresses - case REG_FRAMEBUFFER_1: - GPU::SetFramebufferLocation(GPU::FRAMEBUFFER_LOCATION_VRAM); - memcpy(dst, framebuffer_1, size); - break; + u32* dst = (u32*)Memory::GetPointer(cmd_buff[0x41]); - // Top framebuffer 2 addresses - case REG_FRAMEBUFFER_2: - GPU::SetFramebufferLocation(GPU::FRAMEBUFFER_LOCATION_VRAM); - memcpy(dst, framebuffer_2, size); - break; + while (size > 0) { + GPU::Read<u32>(*dst, reg_addr + 0x1EB00000); - default: - ERROR_LOG(GSP, "unknown register read at address %08X", reg_addr); + size -= 4; + ++dst; + reg_addr += 4; } - } +/** + * GSP_GPU::RegisterInterruptRelayQueue service function + * Inputs: + * 1 : "Flags" field, purpose is unknown + * 3 : Handle to GSP synchronization event + * Outputs: + * 0 : Result of function, 0 on success, otherwise error code + * 2 : Thread index into GSP command buffer + * 4 : Handle to GSP shared memory + */ void RegisterInterruptRelayQueue(Service::Interface* self) { u32* cmd_buff = Service::GetCommandBuffer(); u32 flags = cmd_buff[1]; - u32 event_handle = cmd_buff[3]; - - _assert_msg_(GSP, (event_handle != 0), "called, but event is nullptr!"); + g_event = cmd_buff[3]; - g_event_handle = event_handle; + _assert_msg_(GSP, (g_event != 0), "handle is not valid!"); - Kernel::SetEventLocked(event_handle, false); + Kernel::SetEventLocked(g_event, false); - // Hack - This function will permanently set the state of the GSP event such that GPU command - // synchronization barriers always passthrough. Correct solution would be to set this after the + // Hack - This function will permanently set the state of the GSP event such that GPU command + // synchronization barriers always passthrough. Correct solution would be to set this after the // GPU as processed all queued up commands, but due to the emulator being single-threaded they // will always be ready. - Kernel::SetPermanentLock(event_handle, true); + Kernel::SetPermanentLock(g_event, true); - cmd_buff[2] = g_thread_id; // ThreadID + cmd_buff[0] = 0; // Result - no error + cmd_buff[2] = g_thread_id; // ThreadID + cmd_buff[4] = g_shared_memory; // GSP shared memory } /// This triggers handling of the GX command written to the command buffer in shared memory. void TriggerCmdReqQueue(Service::Interface* self) { + + // Utility function to convert register ID to address + auto WriteGPURegister = [](u32 id, u32 data) { + GPU::Write<u32>(0x1EF00000 + 4 * id, data); + }; + GX_CmdBufferHeader* header = (GX_CmdBufferHeader*)GX_GetCmdBufferPointer(g_thread_id); - u32* cmd_buff = (u32*)GX_GetCmdBufferPointer(g_thread_id, 0x20 + (header->index * 0x20)); + auto& command = *(const GXCommand*)GX_GetCmdBufferPointer(g_thread_id, 0x20 + (header->index * 0x20)); - switch (static_cast<GXCommandId>(cmd_buff[0])) { + switch (command.id) { // GX request DMA - typically used for copying memory from GSP heap to VRAM case GXCommandId::REQUEST_DMA: - memcpy(Memory::GetPointer(cmd_buff[2]), Memory::GetPointer(cmd_buff[1]), cmd_buff[3]); + memcpy(Memory::GetPointer(command.dma_request.dest_address), + Memory::GetPointer(command.dma_request.source_address), + command.dma_request.size); break; + // ctrulib homebrew sends all relevant command list data with this command, + // hence we do all "interesting" stuff here and do nothing in SET_COMMAND_LIST_FIRST. + // TODO: This will need some rework in the future. case GXCommandId::SET_COMMAND_LIST_LAST: - GPU::Write<u32>(GPU::Registers::CommandListAddress, cmd_buff[1] >> 3); - GPU::Write<u32>(GPU::Registers::CommandListSize, cmd_buff[2] >> 3); - GPU::Write<u32>(GPU::Registers::ProcessCommandList, 1); // TODO: Not sure if we are supposed to always write this + { + auto& params = command.set_command_list_last; + WriteGPURegister(GPU::Regs::CommandProcessor + 2, params.address >> 3); + WriteGPURegister(GPU::Regs::CommandProcessor, params.size >> 3); + WriteGPURegister(GPU::Regs::CommandProcessor + 4, 1); // TODO: Not sure if we are supposed to always write this .. seems to trigger processing though // TODO: Move this to GPU // TODO: Not sure what units the size is measured in - g_debugger.CommandListCalled(cmd_buff[1], (u32*)Memory::GetPointer(cmd_buff[1]), cmd_buff[2]); + g_debugger.CommandListCalled(params.address, + (u32*)Memory::GetPointer(params.address), + params.size); break; + } + // It's assumed that the two "blocks" behave equivalently. + // Presumably this is done simply to allow two memory fills to run in parallel. case GXCommandId::SET_MEMORY_FILL: + { + auto& params = command.memory_fill; + WriteGPURegister(GPU::Regs::MemoryFill, params.start1 >> 3); + WriteGPURegister(GPU::Regs::MemoryFill + 1, params.end1 >> 3); + WriteGPURegister(GPU::Regs::MemoryFill + 2, params.end1 - params.start1); + WriteGPURegister(GPU::Regs::MemoryFill + 3, params.value1); + + WriteGPURegister(GPU::Regs::MemoryFill + 4, params.start2 >> 3); + WriteGPURegister(GPU::Regs::MemoryFill + 5, params.end2 >> 3); + WriteGPURegister(GPU::Regs::MemoryFill + 6, params.end2 - params.start2); + WriteGPURegister(GPU::Regs::MemoryFill + 7, params.value2); break; + } + // TODO: Check if texture copies are implemented correctly.. case GXCommandId::SET_DISPLAY_TRANSFER: - break; - case GXCommandId::SET_TEXTURE_COPY: + { + auto& params = command.image_copy; + WriteGPURegister(GPU::Regs::DisplayTransfer, params.in_buffer_address >> 3); + WriteGPURegister(GPU::Regs::DisplayTransfer + 1, params.out_buffer_address >> 3); + WriteGPURegister(GPU::Regs::DisplayTransfer + 3, params.in_buffer_size); + WriteGPURegister(GPU::Regs::DisplayTransfer + 2, params.out_buffer_size); + WriteGPURegister(GPU::Regs::DisplayTransfer + 4, params.flags); + + // TODO: Should this only be ORed with 1 for texture copies? + // trigger transfer + WriteGPURegister(GPU::Regs::DisplayTransfer + 6, 1); break; + } + // TODO: Figure out what exactly SET_COMMAND_LIST_FIRST and SET_COMMAND_LIST_LAST + // are supposed to do. case GXCommandId::SET_COMMAND_LIST_FIRST: { - //u32* buf0_data = (u32*)Memory::GetPointer(cmd_buff[1]); - //u32* buf1_data = (u32*)Memory::GetPointer(cmd_buff[3]); - //u32* buf2_data = (u32*)Memory::GetPointer(cmd_buff[5]); break; } default: - ERROR_LOG(GSP, "unknown command 0x%08X", cmd_buff[0]); + ERROR_LOG(GSP, "unknown command 0x%08X", (int)command.id.Value()); } GX_FinishCommand(g_thread_id); } const Interface::FunctionInfo FunctionTable[] = { - {0x00010082, nullptr, "WriteHWRegs"}, + {0x00010082, WriteHWRegs, "WriteHWRegs"}, {0x00020084, nullptr, "WriteHWRegsWithMask"}, {0x00030082, nullptr, "WriteHWRegRepeat"}, {0x00040080, ReadHWRegs, "ReadHWRegs"}, @@ -208,6 +275,7 @@ const Interface::FunctionInfo FunctionTable[] = { Interface::Interface() { Register(FunctionTable, ARRAY_SIZE(FunctionTable)); + g_shared_memory = Kernel::CreateSharedMemory("GSPSharedMem"); } Interface::~Interface() { diff --git a/src/core/hle/service/gsp.h b/src/core/hle/service/gsp.h index 214de140f..a83cb4846 100644 --- a/src/core/hle/service/gsp.h +++ b/src/core/hle/service/gsp.h @@ -4,6 +4,7 @@ #pragma once +#include "common/bit_field.h" #include "core/hle/service/service.h" //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -12,21 +13,58 @@ namespace GSP_GPU { enum class GXCommandId : u32 { - REQUEST_DMA = 0x00000000, - SET_COMMAND_LIST_LAST = 0x00000001, - SET_MEMORY_FILL = 0x00000002, // TODO: Confirm? (lictru uses 0x01000102) - SET_DISPLAY_TRANSFER = 0x00000003, - SET_TEXTURE_COPY = 0x00000004, - SET_COMMAND_LIST_FIRST = 0x00000005, + REQUEST_DMA = 0x00, + SET_COMMAND_LIST_LAST = 0x01, + + // Fills a given memory range with a particular value + SET_MEMORY_FILL = 0x02, + + // Copies an image and optionally performs color-conversion or scaling. + // This is highly similar to the GameCube's EFB copy feature + SET_DISPLAY_TRANSFER = 0x03, + + // Conceptionally similar to SET_DISPLAY_TRANSFER and presumable uses the same hardware path + SET_TEXTURE_COPY = 0x04, + + SET_COMMAND_LIST_FIRST = 0x05, }; -union GXCommand { - struct { - GXCommandId id; - }; +struct GXCommand { + BitField<0, 8, GXCommandId> id; - u32 data[0x20]; + union { + struct { + u32 source_address; + u32 dest_address; + u32 size; + } dma_request; + + struct { + u32 address; + u32 size; + } set_command_list_last; + + struct { + u32 start1; + u32 value1; + u32 end1; + u32 start2; + u32 value2; + u32 end2; + } memory_fill; + + struct { + u32 in_buffer_address; + u32 out_buffer_address; + u32 in_buffer_size; + u32 out_buffer_size; + u32 flags; + } image_copy; + + u8 raw_data[0x1C]; + }; }; +static_assert(sizeof(GXCommand) == 0x20, "GXCommand struct has incorrect size"); /// Interface to "srv:" service class Interface : public Service::Interface { diff --git a/src/core/hle/service/service.cpp b/src/core/hle/service/service.cpp index 4a1ac857e..d3af2768a 100644 --- a/src/core/hle/service/service.cpp +++ b/src/core/hle/service/service.cpp @@ -10,6 +10,7 @@ #include "core/hle/service/service.h" #include "core/hle/service/apt.h" +#include "core/hle/service/fs.h" #include "core/hle/service/gsp.h" #include "core/hle/service/hid.h" #include "core/hle/service/ndm.h" @@ -71,6 +72,7 @@ void Init() { g_manager->AddService(new SRV::Interface); g_manager->AddService(new APT_U::Interface); + g_manager->AddService(new FS_User::Interface); g_manager->AddService(new GSP_GPU::Interface); g_manager->AddService(new HID_User::Interface); g_manager->AddService(new NDM_U::Interface); diff --git a/src/core/hle/svc.cpp b/src/core/hle/svc.cpp index 441d8ce8d..17967f260 100644 --- a/src/core/hle/svc.cpp +++ b/src/core/hle/svc.cpp @@ -9,9 +9,11 @@ #include "core/mem_map.h" +#include "core/hle/kernel/address_arbiter.h" #include "core/hle/kernel/event.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/mutex.h" +#include "core/hle/kernel/shared_memory.h" #include "core/hle/kernel/thread.h" #include "core/hle/function_wrappers.h" @@ -28,11 +30,6 @@ enum ControlMemoryOperation { MEMORY_OPERATION_GSP_HEAP = 0x00010003, }; -enum MapMemoryPermission { - MEMORY_PERMISSION_UNMAP = 0x00000000, - MEMORY_PERMISSION_NORMAL = 0x00000001, -}; - /// Map application or GSP heap memory Result ControlMemory(u32* out_addr, u32 operation, u32 addr0, u32 addr1, u32 size, u32 permissions) { DEBUG_LOG(SVC,"called operation=0x%08X, addr0=0x%08X, addr1=0x%08X, size=%08X, permissions=0x%08X", @@ -58,17 +55,21 @@ Result ControlMemory(u32* out_addr, u32 operation, u32 addr0, u32 addr1, u32 siz } /// Maps a memory block to specified address -Result MapMemoryBlock(Handle memblock, u32 addr, u32 mypermissions, u32 otherpermission) { +Result MapMemoryBlock(Handle handle, u32 addr, u32 permissions, u32 other_permissions) { DEBUG_LOG(SVC, "called memblock=0x08X, addr=0x%08X, mypermissions=0x%08X, otherpermission=%d", - memblock, addr, mypermissions, otherpermission); - switch (mypermissions) { - case MEMORY_PERMISSION_NORMAL: - case MEMORY_PERMISSION_NORMAL + 1: - case MEMORY_PERMISSION_NORMAL + 2: - Memory::MapBlock_Shared(memblock, addr, mypermissions); + handle, addr, permissions, other_permissions); + + Kernel::MemoryPermission permissions_type = static_cast<Kernel::MemoryPermission>(permissions); + switch (permissions_type) { + case Kernel::MemoryPermission::Read: + case Kernel::MemoryPermission::Write: + case Kernel::MemoryPermission::ReadWrite: + case Kernel::MemoryPermission::DontCare: + Kernel::MapSharedMemory(handle, addr, permissions_type, + static_cast<Kernel::MemoryPermission>(other_permissions)); break; default: - ERROR_LOG(OSHLE, "unknown permissions=0x%08X", mypermissions); + ERROR_LOG(OSHLE, "unknown permissions=0x%08X", permissions); } return 0; } @@ -175,18 +176,19 @@ Result WaitSynchronizationN(s32* out, Handle* handles, s32 handle_count, bool wa } /// Create an address arbiter (to allocate access to shared resources) -Result CreateAddressArbiter(void* arbiter) { - ERROR_LOG(SVC, "(UNIMPLEMENTED) called"); - Core::g_app_core->SetReg(1, 0xFABBDADD); +Result CreateAddressArbiter(u32* arbiter) { + DEBUG_LOG(SVC, "called"); + Handle handle = Kernel::CreateAddressArbiter(); + *arbiter = handle; return 0; } /// Arbitrate address -Result ArbitrateAddress(Handle arbiter, u32 addr, u32 _type, u32 value, s64 nanoseconds) { - ERROR_LOG(SVC, "(UNIMPLEMENTED) called"); - ArbitrationType type = (ArbitrationType)_type; - Memory::Write32(addr, type); - return 0; +Result ArbitrateAddress(Handle arbiter, u32 address, u32 type, u32 value, s64 nanoseconds) { + DEBUG_LOG(SVC, "called arbiter=0x%08X, address=0x%08X, type=0x%08X, value=0x%08X, " + "nanoseconds=%d", arbiter, address, type, value, nanoseconds); + return Kernel::ArbitrateAddress(arbiter, static_cast<Kernel::ArbitrationType>(type), address, + value); } /// Used to output a message on a debug hardware unit - does nothing on a retail unit diff --git a/src/core/hw/gpu.cpp b/src/core/hw/gpu.cpp index f0ca4eada..c00be2a83 100644 --- a/src/core/hw/gpu.cpp +++ b/src/core/hw/gpu.cpp @@ -15,48 +15,58 @@ namespace GPU { -Registers g_regs; +RegisterSet<u32, Regs> g_regs; u64 g_last_ticks = 0; ///< Last CPU ticks /** * Sets whether the framebuffers are in the GSP heap (FCRAM) or VRAM - * @param + * @param */ void SetFramebufferLocation(const FramebufferLocation mode) { switch (mode) { case FRAMEBUFFER_LOCATION_FCRAM: - g_regs.framebuffer_top_left_1 = PADDR_TOP_LEFT_FRAME1; - g_regs.framebuffer_top_left_2 = PADDR_TOP_LEFT_FRAME2; - g_regs.framebuffer_top_right_1 = PADDR_TOP_RIGHT_FRAME1; - g_regs.framebuffer_top_right_2 = PADDR_TOP_RIGHT_FRAME2; - g_regs.framebuffer_sub_left_1 = PADDR_SUB_FRAME1; - //g_regs.framebuffer_sub_left_2 = unknown; - g_regs.framebuffer_sub_right_1 = PADDR_SUB_FRAME2; - //g_regs.framebufferr_sub_right_2 = unknown; + { + auto& framebuffer_top = g_regs.Get<Regs::FramebufferTop>(); + auto& framebuffer_sub = g_regs.Get<Regs::FramebufferBottom>(); + + framebuffer_top.address_left1 = PADDR_TOP_LEFT_FRAME1; + framebuffer_top.address_left2 = PADDR_TOP_LEFT_FRAME2; + framebuffer_top.address_right1 = PADDR_TOP_RIGHT_FRAME1; + framebuffer_top.address_right2 = PADDR_TOP_RIGHT_FRAME2; + framebuffer_sub.address_left1 = PADDR_SUB_FRAME1; + //framebuffer_sub.address_left2 = unknown; + framebuffer_sub.address_right1 = PADDR_SUB_FRAME2; + //framebuffer_sub.address_right2 = unknown; break; + } case FRAMEBUFFER_LOCATION_VRAM: - g_regs.framebuffer_top_left_1 = PADDR_VRAM_TOP_LEFT_FRAME1; - g_regs.framebuffer_top_left_2 = PADDR_VRAM_TOP_LEFT_FRAME2; - g_regs.framebuffer_top_right_1 = PADDR_VRAM_TOP_RIGHT_FRAME1; - g_regs.framebuffer_top_right_2 = PADDR_VRAM_TOP_RIGHT_FRAME2; - g_regs.framebuffer_sub_left_1 = PADDR_VRAM_SUB_FRAME1; - //g_regs.framebuffer_sub_left_2 = unknown; - g_regs.framebuffer_sub_right_1 = PADDR_VRAM_SUB_FRAME2; - //g_regs.framebufferr_sub_right_2 = unknown; + { + auto& framebuffer_top = g_regs.Get<Regs::FramebufferTop>(); + auto& framebuffer_sub = g_regs.Get<Regs::FramebufferBottom>(); + + framebuffer_top.address_left1 = PADDR_VRAM_TOP_LEFT_FRAME1; + framebuffer_top.address_left2 = PADDR_VRAM_TOP_LEFT_FRAME2; + framebuffer_top.address_right1 = PADDR_VRAM_TOP_RIGHT_FRAME1; + framebuffer_top.address_right2 = PADDR_VRAM_TOP_RIGHT_FRAME2; + framebuffer_sub.address_left1 = PADDR_VRAM_SUB_FRAME1; + //framebuffer_sub.address_left2 = unknown; + framebuffer_sub.address_right1 = PADDR_VRAM_SUB_FRAME2; + //framebuffer_sub.address_right2 = unknown; break; } + } } /** * Gets the location of the framebuffers * @return Location of framebuffers as FramebufferLocation enum */ -const FramebufferLocation GetFramebufferLocation() { - if ((g_regs.framebuffer_top_right_1 & ~Memory::VRAM_MASK) == Memory::VRAM_PADDR) { +FramebufferLocation GetFramebufferLocation(u32 address) { + if ((address & ~Memory::VRAM_MASK) == Memory::VRAM_PADDR) { return FRAMEBUFFER_LOCATION_VRAM; - } else if ((g_regs.framebuffer_top_right_1 & ~Memory::FCRAM_MASK) == Memory::FCRAM_PADDR) { + } else if ((address & ~Memory::FCRAM_MASK) == Memory::FCRAM_PADDR) { return FRAMEBUFFER_LOCATION_FCRAM; } else { ERROR_LOG(GPU, "unknown framebuffer location!"); @@ -64,91 +74,161 @@ const FramebufferLocation GetFramebufferLocation() { return FRAMEBUFFER_LOCATION_UNKNOWN; } +u32 GetFramebufferAddr(const u32 address) { + switch (GetFramebufferLocation(address)) { + case FRAMEBUFFER_LOCATION_FCRAM: + return Memory::VirtualAddressFromPhysical_FCRAM(address); + case FRAMEBUFFER_LOCATION_VRAM: + return Memory::VirtualAddressFromPhysical_VRAM(address); + default: + ERROR_LOG(GPU, "unknown framebuffer location"); + } + return 0; +} + /** * Gets a read-only pointer to a framebuffer in memory * @param address Physical address of framebuffer * @return Returns const pointer to raw framebuffer */ const u8* GetFramebufferPointer(const u32 address) { - switch (GetFramebufferLocation()) { - case FRAMEBUFFER_LOCATION_FCRAM: - return (const u8*)Memory::GetPointer(Memory::VirtualAddressFromPhysical_FCRAM(address)); - case FRAMEBUFFER_LOCATION_VRAM: - return (const u8*)Memory::GetPointer(Memory::VirtualAddressFromPhysical_VRAM(address)); - default: - ERROR_LOG(GPU, "unknown framebuffer location"); - } - return NULL; + u32 addr = GetFramebufferAddr(address); + return (addr != 0) ? Memory::GetPointer(addr) : nullptr; } template <typename T> -inline void Read(T &var, const u32 addr) { - switch (addr) { - case Registers::FramebufferTopLeft1: - var = g_regs.framebuffer_top_left_1; - break; +inline void Read(T &var, const u32 raw_addr) { + u32 addr = raw_addr - 0x1EF00000; + int index = addr / 4; - case Registers::FramebufferTopLeft2: - var = g_regs.framebuffer_top_left_2; - break; + // Reads other than u32 are untested, so I'd rather have them abort than silently fail + if (index >= Regs::NumIds || !std::is_same<T,u32>::value) + { + ERROR_LOG(GPU, "unknown Read%d @ 0x%08X", sizeof(var) * 8, addr); + return; + } - case Registers::FramebufferTopRight1: - var = g_regs.framebuffer_top_right_1; - break; + var = g_regs[static_cast<Regs::Id>(addr / 4)]; +} - case Registers::FramebufferTopRight2: - var = g_regs.framebuffer_top_right_2; - break; +template <typename T> +inline void Write(u32 addr, const T data) { + addr -= 0x1EF00000; + int index = addr / 4; - case Registers::FramebufferSubLeft1: - var = g_regs.framebuffer_sub_left_1; - break; + // Writes other than u32 are untested, so I'd rather have them abort than silently fail + if (index >= Regs::NumIds || !std::is_same<T,u32>::value) + { + ERROR_LOG(GPU, "unknown Write%d 0x%08X @ 0x%08X", sizeof(data) * 8, data, addr); + return; + } - case Registers::FramebufferSubRight1: - var = g_regs.framebuffer_sub_right_1; - break; + g_regs[static_cast<Regs::Id>(index)] = data; - case Registers::CommandListSize: - var = g_regs.command_list_size; - break; + switch (static_cast<Regs::Id>(index)) { - case Registers::CommandListAddress: - var = g_regs.command_list_address; - break; + // Memory fills are triggered once the fill value is written. + // NOTE: This is not verified. + case Regs::MemoryFill + 3: + case Regs::MemoryFill + 7: + { + const auto& config = g_regs.Get<Regs::MemoryFill>(static_cast<Regs::Id>(index - 3)); - case Registers::ProcessCommandList: - var = g_regs.command_processing_enabled; - break; + // TODO: Not sure if this check should be done at GSP level instead + if (config.address_start) { + // TODO: Not sure if this algorithm is correct, particularly because it doesn't use the size member at all + u32* start = (u32*)Memory::GetPointer(config.GetStartAddress()); + u32* end = (u32*)Memory::GetPointer(config.GetEndAddress()); + for (u32* ptr = start; ptr < end; ++ptr) + *ptr = bswap32(config.value); // TODO: This is just a workaround to missing framebuffer format emulation - default: - ERROR_LOG(GPU, "unknown Read%d @ 0x%08X", sizeof(var) * 8, addr); + DEBUG_LOG(GPU, "MemoryFill from 0x%08x to 0x%08x", config.GetStartAddress(), config.GetEndAddress()); + } break; } -} - -template <typename T> -inline void Write(u32 addr, const T data) { - switch (static_cast<Registers::Id>(addr)) { - case Registers::CommandListSize: - g_regs.command_list_size = data; - break; - case Registers::CommandListAddress: - g_regs.command_list_address = data; + case Regs::DisplayTransfer + 6: + { + const auto& config = g_regs.Get<Regs::DisplayTransfer>(); + if (config.trigger & 1) { + u8* source_pointer = Memory::GetPointer(config.GetPhysicalInputAddress()); + u8* dest_pointer = Memory::GetPointer(config.GetPhysicalOutputAddress()); + + for (int y = 0; y < config.output_height; ++y) { + // TODO: Why does the register seem to hold twice the framebuffer width? + for (int x = 0; x < config.output_width / 2; ++x) { + struct { + int r, g, b, a; + } source_color = { 0, 0, 0, 0 }; + + switch (config.input_format) { + case Regs::FramebufferFormat::RGBA8: + { + // TODO: Most likely got the component order messed up. + u8* srcptr = source_pointer + x * 4 + y * config.input_width * 4 / 2; + source_color.r = srcptr[0]; // blue + source_color.g = srcptr[1]; // green + source_color.b = srcptr[2]; // red + source_color.a = srcptr[3]; // alpha + break; + } + + default: + ERROR_LOG(GPU, "Unknown source framebuffer format %x", config.input_format.Value()); + break; + } + + switch (config.output_format) { + /*case Regs::FramebufferFormat::RGBA8: + { + // TODO: Untested + u8* dstptr = (u32*)(dest_pointer + x * 4 + y * config.output_width * 4); + dstptr[0] = source_color.r; + dstptr[1] = source_color.g; + dstptr[2] = source_color.b; + dstptr[3] = source_color.a; + break; + }*/ + + case Regs::FramebufferFormat::RGB8: + { + // TODO: Most likely got the component order messed up. + u8* dstptr = dest_pointer + x * 3 + y * config.output_width * 3 / 2; + dstptr[0] = source_color.r; // blue + dstptr[1] = source_color.g; // green + dstptr[2] = source_color.b; // red + break; + } + + default: + ERROR_LOG(GPU, "Unknown destination framebuffer format %x", config.output_format.Value()); + break; + } + } + } + + DEBUG_LOG(GPU, "DisplayTriggerTransfer: 0x%08x bytes from 0x%08x(%dx%d)-> 0x%08x(%dx%d), dst format %x", + config.output_height * config.output_width * 4, + config.GetPhysicalInputAddress(), (int)config.input_width, (int)config.input_height, + config.GetPhysicalOutputAddress(), (int)config.output_width, (int)config.output_height, + config.output_format.Value()); + } break; + } - case Registers::ProcessCommandList: - g_regs.command_processing_enabled = data; - if (g_regs.command_processing_enabled & 1) + case Regs::CommandProcessor + 4: + { + const auto& config = g_regs.Get<Regs::CommandProcessor>(); + if (config.trigger & 1) { - // u32* buffer = (u32*)Memory::GetPointer(g_regs.command_list_address << 3); - ERROR_LOG(GPU, "Beginning %x bytes of commands from address %x", g_regs.command_list_size, g_regs.command_list_address << 3); + // u32* buffer = (u32*)Memory::GetPointer(config.address << 3); + ERROR_LOG(GPU, "Beginning 0x%08x bytes of commands from address 0x%08x", config.size, config.address << 3); // TODO: Process command list! } break; + } default: - ERROR_LOG(GPU, "unknown Write%d 0x%08X @ 0x%08X", sizeof(data) * 8, data, addr); break; } } @@ -180,7 +260,24 @@ void Update() { /// Initialize hardware void Init() { g_last_ticks = Core::g_app_core->GetTicks(); - SetFramebufferLocation(FRAMEBUFFER_LOCATION_FCRAM); +// SetFramebufferLocation(FRAMEBUFFER_LOCATION_FCRAM); + SetFramebufferLocation(FRAMEBUFFER_LOCATION_VRAM); + + auto& framebuffer_top = g_regs.Get<Regs::FramebufferTop>(); + auto& framebuffer_sub = g_regs.Get<Regs::FramebufferBottom>(); + // TODO: Width should be 240 instead? + framebuffer_top.width = 480; + framebuffer_top.height = 400; + framebuffer_top.stride = 480*3; + framebuffer_top.color_format = Regs::FramebufferFormat::RGB8; + framebuffer_top.active_fb = 0; + + framebuffer_sub.width = 480; + framebuffer_sub.height = 400; + framebuffer_sub.stride = 480*3; + framebuffer_sub.color_format = Regs::FramebufferFormat::RGB8; + framebuffer_sub.active_fb = 0; + NOTICE_LOG(GPU, "initialized OK"); } diff --git a/src/core/hw/gpu.h b/src/core/hw/gpu.h index 3314ba989..42f18a0e7 100644 --- a/src/core/hw/gpu.h +++ b/src/core/hw/gpu.h @@ -5,43 +5,168 @@ #pragma once #include "common/common_types.h" +#include "common/bit_field.h" +#include "common/register_set.h" namespace GPU { static const u32 kFrameCycles = 268123480 / 60; ///< 268MHz / 60 frames per second static const u32 kFrameTicks = kFrameCycles / 3; ///< Approximate number of instructions/frame -struct Registers { +// MMIO region 0x1EFxxxxx +struct Regs { enum Id : u32 { - FramebufferTopLeft1 = 0x1EF00468, // Main LCD, first framebuffer for 3D left - FramebufferTopLeft2 = 0x1EF0046C, // Main LCD, second framebuffer for 3D left - FramebufferTopRight1 = 0x1EF00494, // Main LCD, first framebuffer for 3D right - FramebufferTopRight2 = 0x1EF00498, // Main LCD, second framebuffer for 3D right - FramebufferSubLeft1 = 0x1EF00568, // Sub LCD, first framebuffer - FramebufferSubLeft2 = 0x1EF0056C, // Sub LCD, second framebuffer - FramebufferSubRight1 = 0x1EF00594, // Sub LCD, unused first framebuffer - FramebufferSubRight2 = 0x1EF00598, // Sub LCD, unused second framebuffer - - CommandListSize = 0x1EF018E0, - CommandListAddress = 0x1EF018E8, - ProcessCommandList = 0x1EF018F0, + MemoryFill = 0x00004, // + 5,6,7; second block at 8-11 + + FramebufferTop = 0x00117, // + 11a,11b,11c,11d(?),11e...126 + FramebufferBottom = 0x00157, // + 15a,15b,15c,15d(?),15e...166 + + DisplayTransfer = 0x00300, // + 301,302,303,304,305,306 + + CommandProcessor = 0x00638, // + 63a,63c + + NumIds = 0x01000 + }; + + template<Id id> + struct Struct; + + enum class FramebufferFormat : u32 { + RGBA8 = 0, + RGB8 = 1, + RGB565 = 2, + RGB5A1 = 3, + RGBA4 = 4, + }; +}; + +template<> +struct Regs::Struct<Regs::MemoryFill> { + u32 address_start; + u32 address_end; // ? + u32 size; + u32 value; // ? + + inline u32 GetStartAddress() const { + return address_start * 8; + } + + inline u32 GetEndAddress() const { + return address_end * 8; + } +}; +static_assert(sizeof(Regs::Struct<Regs::MemoryFill>) == 0x10, "Structure size and register block length don't match"); + +template<> +struct Regs::Struct<Regs::FramebufferTop> { + using Format = Regs::FramebufferFormat; + + union { + u32 size; + + BitField< 0, 16, u32> width; + BitField<16, 16, u32> height; + }; + + u32 pad0[2]; + + u32 address_left1; + u32 address_left2; + + union { + u32 format; + + BitField< 0, 3, Format> color_format; + }; + + u32 pad1; + + union { + u32 active_fb; + + // 0: Use parameters ending with "1" + // 1: Use parameters ending with "2" + BitField<0, 1, u32> second_fb_active; + }; + + u32 pad2[5]; + + // Distance between two pixel rows, in bytes + u32 stride; + + u32 address_right1; + u32 address_right2; +}; + +template<> +struct Regs::Struct<Regs::FramebufferBottom> : public Regs::Struct<Regs::FramebufferTop> { +}; +static_assert(sizeof(Regs::Struct<Regs::FramebufferTop>) == 0x40, "Structure size and register block length don't match"); + +template<> +struct Regs::Struct<Regs::DisplayTransfer> { + using Format = Regs::FramebufferFormat; + + u32 input_address; + u32 output_address; + + inline u32 GetPhysicalInputAddress() const { + return input_address * 8; + } + + inline u32 GetPhysicalOutputAddress() const { + return output_address * 8; + } + + union { + u32 output_size; + + BitField< 0, 16, u32> output_width; + BitField<16, 16, u32> output_height; + }; + + union { + u32 input_size; + + BitField< 0, 16, u32> input_width; + BitField<16, 16, u32> input_height; }; - u32 framebuffer_top_left_1; - u32 framebuffer_top_left_2; - u32 framebuffer_top_right_1; - u32 framebuffer_top_right_2; - u32 framebuffer_sub_left_1; - u32 framebuffer_sub_left_2; - u32 framebuffer_sub_right_1; - u32 framebuffer_sub_right_2; - - u32 command_list_size; - u32 command_list_address; - u32 command_processing_enabled; + union { + u32 flags; + + BitField< 0, 1, u32> flip_data; // flips input data horizontally (TODO) if true + BitField< 8, 3, Format> input_format; + BitField<12, 3, Format> output_format; + BitField<16, 1, u32> output_tiled; // stores output in a tiled format + }; + + u32 unknown; + + // it seems that writing to this field triggers the display transfer + u32 trigger; }; +static_assert(sizeof(Regs::Struct<Regs::DisplayTransfer>) == 0x1C, "Structure size and register block length don't match"); + +template<> +struct Regs::Struct<Regs::CommandProcessor> { + // command list size + u32 size; + + u32 pad0; + + // command list address + u32 address; -extern Registers g_regs; + u32 pad1; + + // it seems that writing to this field triggers command list processing + u32 trigger; +}; +static_assert(sizeof(Regs::Struct<Regs::CommandProcessor>) == 0x14, "Structure size and register block length don't match"); + + +extern RegisterSet<u32, Regs> g_regs; enum { TOP_ASPECT_X = 0x5, @@ -51,23 +176,35 @@ enum { TOP_WIDTH = 400, BOTTOM_WIDTH = 320, - // Physical addresses in FCRAM used by ARM9 applications - these are correct for real hardware - PADDR_FRAMEBUFFER_SEL = 0x20184E59, - PADDR_TOP_LEFT_FRAME1 = 0x20184E60, + // Physical addresses in FCRAM (chosen arbitrarily) + PADDR_TOP_LEFT_FRAME1 = 0x201D4C00, + PADDR_TOP_LEFT_FRAME2 = 0x202D4C00, + PADDR_TOP_RIGHT_FRAME1 = 0x203D4C00, + PADDR_TOP_RIGHT_FRAME2 = 0x204D4C00, + PADDR_SUB_FRAME1 = 0x205D4C00, + PADDR_SUB_FRAME2 = 0x206D4C00, + // Physical addresses in FCRAM used by ARM9 applications +/* PADDR_TOP_LEFT_FRAME1 = 0x20184E60, PADDR_TOP_LEFT_FRAME2 = 0x201CB370, PADDR_TOP_RIGHT_FRAME1 = 0x20282160, PADDR_TOP_RIGHT_FRAME2 = 0x202C8670, PADDR_SUB_FRAME1 = 0x202118E0, - PADDR_SUB_FRAME2 = 0x20249CF0, - - // Physical addresses in VRAM - I'm not sure how these are actually allocated (so not real) - PADDR_VRAM_FRAMEBUFFER_SEL = 0x18184E59, - PADDR_VRAM_TOP_LEFT_FRAME1 = 0x18184E60, - PADDR_VRAM_TOP_LEFT_FRAME2 = 0x181CB370, + PADDR_SUB_FRAME2 = 0x20249CF0,*/ + + // Physical addresses in VRAM + // TODO: These should just be deduced from the ones above + PADDR_VRAM_TOP_LEFT_FRAME1 = 0x181D4C00, + PADDR_VRAM_TOP_LEFT_FRAME2 = 0x182D4C00, + PADDR_VRAM_TOP_RIGHT_FRAME1 = 0x183D4C00, + PADDR_VRAM_TOP_RIGHT_FRAME2 = 0x184D4C00, + PADDR_VRAM_SUB_FRAME1 = 0x185D4C00, + PADDR_VRAM_SUB_FRAME2 = 0x186D4C00, + // Physical addresses in VRAM used by ARM9 applications +/* PADDR_VRAM_TOP_LEFT_FRAME2 = 0x181CB370, PADDR_VRAM_TOP_RIGHT_FRAME1 = 0x18282160, PADDR_VRAM_TOP_RIGHT_FRAME2 = 0x182C8670, PADDR_VRAM_SUB_FRAME1 = 0x182118E0, - PADDR_VRAM_SUB_FRAME2 = 0x18249CF0, + PADDR_VRAM_SUB_FRAME2 = 0x18249CF0,*/ }; /// Framebuffer location @@ -79,7 +216,7 @@ enum FramebufferLocation { /** * Sets whether the framebuffers are in the GSP heap (FCRAM) or VRAM - * @param + * @param */ void SetFramebufferLocation(const FramebufferLocation mode); @@ -90,16 +227,18 @@ void SetFramebufferLocation(const FramebufferLocation mode); */ const u8* GetFramebufferPointer(const u32 address); +u32 GetFramebufferAddr(const u32 address); + /** * Gets the location of the framebuffers */ -const FramebufferLocation GetFramebufferLocation(); +FramebufferLocation GetFramebufferLocation(u32 address); template <typename T> -inline void Read(T &var, const u32 addr); +void Read(T &var, const u32 addr); template <typename T> -inline void Write(u32 addr, const T data); +void Write(u32 addr, const T data); /// Update hardware void Update(); diff --git a/src/core/hw/hw.h b/src/core/hw/hw.h index 92e9304ca..1055ed94f 100644 --- a/src/core/hw/hw.h +++ b/src/core/hw/hw.h @@ -9,10 +9,10 @@ namespace HW { template <typename T> -inline void Read(T &var, const u32 addr); +void Read(T &var, const u32 addr); template <typename T> -inline void Write(u32 addr, const T data); +void Write(u32 addr, const T data); /// Update hardware void Update(); diff --git a/src/core/loader/elf.h b/src/core/loader/elf.h index d3cbf414d..5ae88439a 100644 --- a/src/core/loader/elf.h +++ b/src/core/loader/elf.h @@ -13,16 +13,16 @@ namespace Loader { /// Loads an ELF/AXF file -class AppLoader_ELF : public AppLoader { +class AppLoader_ELF final : public AppLoader { public: AppLoader_ELF(const std::string& filename); - ~AppLoader_ELF(); + ~AppLoader_ELF() override; /** * Load the bootable file * @return ResultStatus result of function */ - ResultStatus Load(); + ResultStatus Load() override; private: std::string filename; diff --git a/src/core/loader/loader.cpp b/src/core/loader/loader.cpp index 96cb81de0..2b42e3c64 100644 --- a/src/core/loader/loader.cpp +++ b/src/core/loader/loader.cpp @@ -4,9 +4,11 @@ #include <memory> +#include "core/file_sys/archive_romfs.h" #include "core/loader/loader.h" #include "core/loader/elf.h" #include "core/loader/ncch.h" +#include "core/hle/kernel/archive.h" //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -51,14 +53,20 @@ ResultStatus LoadFile(const std::string& filename) { switch (IdentifyFile(filename)) { // Standard ELF file format... - case FileType::ELF: { + case FileType::ELF: return AppLoader_ELF(filename).Load(); - } // NCCH/NCSD container formats... case FileType::CXI: case FileType::CCI: { - return AppLoader_NCCH(filename).Load(); + AppLoader_NCCH app_loader(filename); + + // Load application and RomFS + if (ResultStatus::Success == app_loader.Load()) { + Kernel::CreateArchive(new FileSys::Archive_RomFS(app_loader), "RomFS"); + return ResultStatus::Success; + } + break; } // Error occurred durring IdentifyFile... @@ -70,7 +78,6 @@ ResultStatus LoadFile(const std::string& filename) { default: return ResultStatus::ErrorInvalidFormat; } - return ResultStatus::Error; } diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h index 95f16fcb1..4ba10de52 100644 --- a/src/core/loader/loader.h +++ b/src/core/loader/loader.h @@ -32,6 +32,7 @@ enum class ResultStatus { ErrorNotLoaded, ErrorNotUsed, ErrorAlreadyLoaded, + ErrorMemoryAllocationFailed, }; /// Interface for loading an application @@ -48,60 +49,48 @@ public: /** * Get the code (typically .code section) of the application - * @param error ResultStatus result of function - * @return Reference to code buffer + * @param buffer Reference to buffer to store data + * @return ResultStatus result of function */ - virtual const std::vector<u8>& ReadCode(ResultStatus& error) const { - error = ResultStatus::ErrorNotImplemented; - return code; + virtual ResultStatus ReadCode(std::vector<u8>& buffer) const { + return ResultStatus::ErrorNotImplemented; } /** * Get the icon (typically icon section) of the application - * @param error ResultStatus result of function - * @return Reference to icon buffer + * @param buffer Reference to buffer to store data + * @return ResultStatus result of function */ - virtual const std::vector<u8>& ReadIcon(ResultStatus& error) const { - error = ResultStatus::ErrorNotImplemented; - return icon; + virtual ResultStatus ReadIcon(std::vector<u8>& buffer) const { + return ResultStatus::ErrorNotImplemented; } /** * Get the banner (typically banner section) of the application - * @param error ResultStatus result of function - * @return Reference to banner buffer + * @param buffer Reference to buffer to store data + * @return ResultStatus result of function */ - virtual const std::vector<u8>& ReadBanner(ResultStatus& error) const { - error = ResultStatus::ErrorNotImplemented; - return banner; + virtual ResultStatus ReadBanner(std::vector<u8>& buffer) const { + return ResultStatus::ErrorNotImplemented; } /** * Get the logo (typically logo section) of the application - * @param error ResultStatus result of function - * @return Reference to logo buffer + * @param buffer Reference to buffer to store data + * @return ResultStatus result of function */ - virtual const std::vector<u8>& ReadLogo(ResultStatus& error) const { - error = ResultStatus::ErrorNotImplemented; - return logo; + virtual ResultStatus ReadLogo(std::vector<u8>& buffer) const { + return ResultStatus::ErrorNotImplemented; } /** - * Get the RomFs archive of the application - * @param error ResultStatus result of function - * @return Reference to RomFs archive buffer + * Get the RomFS of the application + * @param buffer Reference to buffer to store data + * @return ResultStatus result of function */ - virtual const std::vector<u8>& ReadRomFS(ResultStatus& error) const { - error = ResultStatus::ErrorNotImplemented; - return romfs; + virtual ResultStatus ReadRomFS(std::vector<u8>& buffer) const { + return ResultStatus::ErrorNotImplemented; } - -protected: - std::vector<u8> code; ///< ExeFS .code section - std::vector<u8> icon; ///< ExeFS .icon section - std::vector<u8> banner; ///< ExeFS .banner section - std::vector<u8> logo; ///< ExeFS .logo section - std::vector<u8> romfs; ///< RomFs archive }; /** diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp index 60505bdfa..ba27eb75a 100644 --- a/src/core/loader/ncch.cpp +++ b/src/core/loader/ncch.cpp @@ -113,76 +113,80 @@ AppLoader_NCCH::AppLoader_NCCH(const std::string& filename) { /// AppLoader_NCCH destructor AppLoader_NCCH::~AppLoader_NCCH() { - if (file.IsOpen()) - file.Close(); } /** * Loads .code section into memory for booting * @return ResultStatus result of function */ -ResultStatus AppLoader_NCCH::LoadExec() { +ResultStatus AppLoader_NCCH::LoadExec() const { if (!is_loaded) return ResultStatus::ErrorNotLoaded; - ResultStatus res; - code = ReadCode(res); - - if (ResultStatus::Success == res) { + std::vector<u8> code; + if (ResultStatus::Success == ReadCode(code)) { Memory::WriteBlock(entry_point, &code[0], code.size()); Kernel::LoadExec(entry_point); + return ResultStatus::Success; } - return res; + return ResultStatus::Error; } /** * Reads an application ExeFS section of an NCCH file into AppLoader (e.g. .code, .logo, etc.) * @param name Name of section to read out of NCCH file * @param buffer Vector to read data into - * @param error ResultStatus result of function - * @return Reference to buffer of data that was read + * @return ResultStatus result of function */ -const std::vector<u8>& AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>& buffer, - ResultStatus& error) { +ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) const { // Iterate through the ExeFs archive until we find the .code file... - for (int i = 0; i < kMaxSections; i++) { - // Load the specified section... - if (strcmp((const char*)exefs_header.section[i].name, name) == 0) { - INFO_LOG(LOADER, "ExeFS section %d:", i); - INFO_LOG(LOADER, " name: %s", exefs_header.section[i].name); - INFO_LOG(LOADER, " offset: 0x%08X", exefs_header.section[i].offset); - INFO_LOG(LOADER, " size: 0x%08X", exefs_header.section[i].size); - - s64 section_offset = (exefs_header.section[i].offset + exefs_offset + - sizeof(ExeFs_Header) + ncch_offset); - file.Seek(section_offset, 0); - - // Section is compressed... - if (i == 0 && is_compressed) { - // Read compressed .code section... - std::unique_ptr<u8[]> temp_buffer(new u8[exefs_header.section[i].size]); - file.ReadBytes(&temp_buffer[0], exefs_header.section[i].size); - - // Decompress .code section... - u32 decompressed_size = LZSS_GetDecompressedSize(&temp_buffer[0], - exefs_header.section[i].size); - buffer.resize(decompressed_size); - if (!LZSS_Decompress(&temp_buffer[0], exefs_header.section[i].size, &buffer[0], - decompressed_size)) { - error = ResultStatus::ErrorInvalidFormat; - return buffer; + File::IOFile file(filename, "rb"); + if (file.IsOpen()) { + for (int i = 0; i < kMaxSections; i++) { + // Load the specified section... + if (strcmp((const char*)exefs_header.section[i].name, name) == 0) { + INFO_LOG(LOADER, "ExeFS section %d:", i); + INFO_LOG(LOADER, " name: %s", exefs_header.section[i].name); + INFO_LOG(LOADER, " offset: 0x%08X", exefs_header.section[i].offset); + INFO_LOG(LOADER, " size: 0x%08X", exefs_header.section[i].size); + + s64 section_offset = (exefs_header.section[i].offset + exefs_offset + + sizeof(ExeFs_Header)+ncch_offset); + file.Seek(section_offset, 0); + + // Section is compressed... + if (i == 0 && is_compressed) { + // Read compressed .code section... + std::unique_ptr<u8[]> temp_buffer; + try { + temp_buffer.reset(new u8[exefs_header.section[i].size]); + } catch (std::bad_alloc&) { + return ResultStatus::ErrorMemoryAllocationFailed; + } + file.ReadBytes(&temp_buffer[0], exefs_header.section[i].size); + + // Decompress .code section... + u32 decompressed_size = LZSS_GetDecompressedSize(&temp_buffer[0], + exefs_header.section[i].size); + buffer.resize(decompressed_size); + if (!LZSS_Decompress(&temp_buffer[0], exefs_header.section[i].size, &buffer[0], + decompressed_size)) { + return ResultStatus::ErrorInvalidFormat; + } + // Section is uncompressed... } - // Section is uncompressed... - } else { - buffer.resize(exefs_header.section[i].size); - file.ReadBytes(&buffer[0], exefs_header.section[i].size); + else { + buffer.resize(exefs_header.section[i].size); + file.ReadBytes(&buffer[0], exefs_header.section[i].size); + } + return ResultStatus::Success; } - error = ResultStatus::Success; - return buffer; } + } else { + ERROR_LOG(LOADER, "Unable to read file %s!", filename.c_str()); + return ResultStatus::Error; } - error = ResultStatus::ErrorNotUsed; - return buffer; + return ResultStatus::ErrorNotUsed; } /** @@ -197,8 +201,7 @@ ResultStatus AppLoader_NCCH::Load() { if (is_loaded) return ResultStatus::ErrorAlreadyLoaded; - file = File::IOFile(filename, "rb"); - + File::IOFile file(filename, "rb"); if (file.IsOpen()) { file.ReadBytes(&ncch_header, sizeof(NCCH_Header)); @@ -241,72 +244,77 @@ ResultStatus AppLoader_NCCH::Load() { LoadExec(); // Load the executable into memory for booting return ResultStatus::Success; + } else { + ERROR_LOG(LOADER, "Unable to read file %s!", filename.c_str()); } return ResultStatus::Error; } /** * Get the code (typically .code section) of the application - * @param error ResultStatus result of function - * @return Reference to code buffer + * @param buffer Reference to buffer to store data + * @return ResultStatus result of function */ -const std::vector<u8>& AppLoader_NCCH::ReadCode(ResultStatus& error) { - return LoadSectionExeFS(".code", code, error); +ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) const { + return LoadSectionExeFS(".code", buffer); } /** * Get the icon (typically icon section) of the application - * @param error ResultStatus result of function - * @return Reference to icon buffer + * @param buffer Reference to buffer to store data + * @return ResultStatus result of function */ -const std::vector<u8>& AppLoader_NCCH::ReadIcon(ResultStatus& error) { - return LoadSectionExeFS("icon", icon, error); +ResultStatus AppLoader_NCCH::ReadIcon(std::vector<u8>& buffer) const { + return LoadSectionExeFS("icon", buffer); } /** * Get the banner (typically banner section) of the application - * @param error ResultStatus result of function - * @return Reference to banner buffer + * @param buffer Reference to buffer to store data + * @return ResultStatus result of function */ -const std::vector<u8>& AppLoader_NCCH::ReadBanner(ResultStatus& error) { - return LoadSectionExeFS("banner", banner, error); +ResultStatus AppLoader_NCCH::ReadBanner(std::vector<u8>& buffer) const { + return LoadSectionExeFS("banner", buffer); } /** * Get the logo (typically logo section) of the application - * @param error ResultStatus result of function - * @return Reference to logo buffer + * @param buffer Reference to buffer to store data + * @return ResultStatus result of function */ -const std::vector<u8>& AppLoader_NCCH::ReadLogo(ResultStatus& error) { - return LoadSectionExeFS("logo", logo, error); +ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) const { + return LoadSectionExeFS("logo", buffer); } /** - * Get the RomFs archive of the application - * @param error ResultStatus result of function - * @return Reference to RomFs archive buffer + * Get the RomFS of the application + * @param buffer Reference to buffer to store data + * @return ResultStatus result of function */ -const std::vector<u8>& AppLoader_NCCH::ReadRomFS(ResultStatus& error) { - // Check if the NCCH has a RomFS... - if (ncch_header.romfs_offset != 0 && ncch_header.romfs_size != 0) { - u32 romfs_offset = ncch_offset + (ncch_header.romfs_offset * kBlockSize) + 0x1000; - u32 romfs_size = (ncch_header.romfs_size * kBlockSize) - 0x1000; +ResultStatus AppLoader_NCCH::ReadRomFS(std::vector<u8>& buffer) const { + File::IOFile file(filename, "rb"); + if (file.IsOpen()) { + // Check if the NCCH has a RomFS... + if (ncch_header.romfs_offset != 0 && ncch_header.romfs_size != 0) { + u32 romfs_offset = ncch_offset + (ncch_header.romfs_offset * kBlockSize) + 0x1000; + u32 romfs_size = (ncch_header.romfs_size * kBlockSize) - 0x1000; - INFO_LOG(LOADER, "RomFS offset: 0x%08X", romfs_offset); - INFO_LOG(LOADER, "RomFS size: 0x%08X", romfs_size); + INFO_LOG(LOADER, "RomFS offset: 0x%08X", romfs_offset); + INFO_LOG(LOADER, "RomFS size: 0x%08X", romfs_size); - romfs.resize(romfs_size); + buffer.resize(romfs_size); - file.Seek(romfs_offset, 0); - file.ReadBytes(&romfs[0], romfs_size); + file.Seek(romfs_offset, 0); + file.ReadBytes(&buffer[0], romfs_size); - error = ResultStatus::Success; - return romfs; - } else { + return ResultStatus::Success; + } NOTICE_LOG(LOADER, "RomFS unused"); + return ResultStatus::ErrorNotUsed; + } else { + ERROR_LOG(LOADER, "Unable to read file %s!", filename.c_str()); } - error = ResultStatus::ErrorNotUsed; - return romfs; + return ResultStatus::Error; } } // namespace Loader diff --git a/src/core/loader/ncch.h b/src/core/loader/ncch.h index bf65425a4..29b59aa11 100644 --- a/src/core/loader/ncch.h +++ b/src/core/loader/ncch.h @@ -145,51 +145,51 @@ struct ExHeader_Header{ namespace Loader { /// Loads an NCCH file (e.g. from a CCI, or the first NCCH in a CXI) -class AppLoader_NCCH : public AppLoader { +class AppLoader_NCCH final : public AppLoader { public: AppLoader_NCCH(const std::string& filename); - ~AppLoader_NCCH(); + ~AppLoader_NCCH() override; /** * Load the application * @return ResultStatus result of function */ - ResultStatus Load(); + ResultStatus Load() override; /** * Get the code (typically .code section) of the application - * @param error ResultStatus result of function - * @return Reference to code buffer + * @param buffer Reference to buffer to store data + * @return ResultStatus result of function */ - const std::vector<u8>& ReadCode(ResultStatus& error); + ResultStatus ReadCode(std::vector<u8>& buffer) const override; /** * Get the icon (typically icon section) of the application - * @param error ResultStatus result of function - * @return Reference to icon buffer + * @param buffer Reference to buffer to store data + * @return ResultStatus result of function */ - const std::vector<u8>& ReadIcon(ResultStatus& error); + ResultStatus ReadIcon(std::vector<u8>& buffer) const override; /** * Get the banner (typically banner section) of the application - * @param error ResultStatus result of function - * @return Reference to banner buffer + * @param buffer Reference to buffer to store data + * @return ResultStatus result of function */ - const std::vector<u8>& ReadBanner(ResultStatus& error); + ResultStatus ReadBanner(std::vector<u8>& buffer) const override; /** * Get the logo (typically logo section) of the application - * @param error ResultStatus result of function - * @return Reference to logo buffer + * @param buffer Reference to buffer to store data + * @return ResultStatus result of function */ - const std::vector<u8>& ReadLogo(ResultStatus& error); + ResultStatus ReadLogo(std::vector<u8>& buffer) const override; /** - * Get the RomFs archive of the application - * @param error ResultStatus result of function - * @return Reference to RomFs archive buffer + * Get the RomFS of the application + * @param buffer Reference to buffer to store data + * @return ResultStatus result of function */ - const std::vector<u8>& ReadRomFS(ResultStatus& error); + ResultStatus ReadRomFS(std::vector<u8>& buffer) const override; private: @@ -197,19 +197,16 @@ private: * Reads an application ExeFS section of an NCCH file into AppLoader (e.g. .code, .logo, etc.) * @param name Name of section to read out of NCCH file * @param buffer Vector to read data into - * @param error ResultStatus result of function - * @return Reference to buffer of data that was read + * @return ResultStatus result of function */ - const std::vector<u8>& LoadSectionExeFS(const char* name, std::vector<u8>& buffer, - ResultStatus& error); + ResultStatus LoadSectionExeFS(const char* name, std::vector<u8>& buffer) const; /** * Loads .code section into memory for booting * @return ResultStatus result of function */ - ResultStatus LoadExec(); + ResultStatus LoadExec() const; - File::IOFile file; std::string filename; bool is_loaded; diff --git a/src/core/mem_map.h b/src/core/mem_map.h index d5899e4bb..12941f558 100644 --- a/src/core/mem_map.h +++ b/src/core/mem_map.h @@ -128,6 +128,12 @@ extern u8* g_exefs_code; ///< ExeFS:/.code is loaded here void Init(); void Shutdown(); +template <typename T> +inline void Read(T &var, const u32 addr); + +template <typename T> +inline void Write(u32 addr, const T data); + u8 Read8(const u32 addr); u16 Read16(const u32 addr); u32 Read32(const u32 addr); @@ -144,14 +150,6 @@ void WriteBlock(const u32 addr, const u8* data, const int size); u8* GetPointer(const u32 Address); /** - * Maps a block of memory in shared memory - * @param handle Handle to map memory block for - * @param addr Address to map memory block to - * @param permissions Memory map permissions - */ -u32 MapBlock_Shared(u32 handle, u32 addr,u32 permissions) ; - -/** * Maps a block of memory on the heap * @param size Size of block in bytes * @param operation Memory map operation type diff --git a/src/core/mem_map_funcs.cpp b/src/core/mem_map_funcs.cpp index 37913119e..0342122df 100644 --- a/src/core/mem_map_funcs.cpp +++ b/src/core/mem_map_funcs.cpp @@ -41,7 +41,7 @@ u32 _VirtualAddress(const u32 addr) { } template <typename T> -inline void _Read(T &var, const u32 addr) { +inline void Read(T &var, const u32 addr) { // TODO: Figure out the fastest order of tests for both read and write (they are probably different). // TODO: Make sure this represents the mirrors in a correct way. // Could just do a base-relative read, too.... TODO @@ -91,7 +91,7 @@ inline void _Read(T &var, const u32 addr) { } template <typename T> -inline void _Write(u32 addr, const T data) { +inline void Write(u32 addr, const T data) { u32 vaddr = _VirtualAddress(addr); // Kernel memory command buffer @@ -178,28 +178,6 @@ u8 *GetPointer(const u32 addr) { } /** - * Maps a block of memory in shared memory - * @param handle Handle to map memory block for - * @param addr Address to map memory block to - * @param permissions Memory map permissions - */ -u32 MapBlock_Shared(u32 handle, u32 addr,u32 permissions) { - MemoryBlock block; - - block.handle = handle; - block.base_address = addr; - block.permissions = permissions; - - if (g_shared_map.size() > 0) { - const MemoryBlock last_block = g_shared_map.rbegin()->second; - block.address = last_block.address + last_block.size; - } - g_shared_map[block.GetVirtualAddress()] = block; - - return block.GetVirtualAddress(); -} - -/** * Maps a block of memory on the heap * @param size Size of block in bytes * @param operation Memory map operation type @@ -247,25 +225,25 @@ u32 MapBlock_HeapGSP(u32 size, u32 operation, u32 permissions) { u8 Read8(const u32 addr) { u8 _var = 0; - _Read<u8>(_var, addr); + Read<u8>(_var, addr); return (u8)_var; } u16 Read16(const u32 addr) { u16_le _var = 0; - _Read<u16_le>(_var, addr); + Read<u16_le>(_var, addr); return (u16)_var; } u32 Read32(const u32 addr) { u32_le _var = 0; - _Read<u32_le>(_var, addr); + Read<u32_le>(_var, addr); return _var; } u64 Read64(const u32 addr) { u64_le _var = 0; - _Read<u64_le>(_var, addr); + Read<u64_le>(_var, addr); return _var; } @@ -278,19 +256,19 @@ u32 Read16_ZX(const u32 addr) { } void Write8(const u32 addr, const u8 data) { - _Write<u8>(addr, data); + Write<u8>(addr, data); } void Write16(const u32 addr, const u16 data) { - _Write<u16_le>(addr, data); + Write<u16_le>(addr, data); } void Write32(const u32 addr, const u32 data) { - _Write<u32_le>(addr, data); + Write<u32_le>(addr, data); } void Write64(const u32 addr, const u64 data) { - _Write<u64_le>(addr, data); + Write<u64_le>(addr, data); } void WriteBlock(const u32 addr, const u8* data, const int size) { diff --git a/src/core/system.cpp b/src/core/system.cpp index 9b1e96888..43d0eef2c 100644 --- a/src/core/system.cpp +++ b/src/core/system.cpp @@ -15,7 +15,6 @@ namespace System { volatile State g_state; -MetaFileSystem g_ctr_file_system; void UpdateState(State state) { } @@ -45,7 +44,6 @@ void Shutdown() { CoreTiming::Shutdown(); VideoCore::Shutdown(); Kernel::Shutdown(); - g_ctr_file_system.Shutdown(); } } // namespace diff --git a/src/core/system.h b/src/core/system.h index 09f1f6ebe..8f8ddf87b 100644 --- a/src/core/system.h +++ b/src/core/system.h @@ -5,7 +5,6 @@ #pragma once #include "common/emu_window.h" -#include "core/file_sys/meta_file_system.h" //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -24,7 +23,6 @@ typedef enum { } State; extern volatile State g_state; -extern MetaFileSystem g_ctr_file_system; void UpdateState(State state); void Init(EmuWindow* emu_window); |