Loader: Updated read methods to be const

- Required "file" handle to be made local and explicitly opened/closed as needed
This commit is contained in:
bunnei 2014-07-04 13:11:09 -04:00
parent 1ccdb6a769
commit 2c62d92551
5 changed files with 76 additions and 65 deletions

View File

@ -11,7 +11,7 @@
namespace FileSys { namespace FileSys {
Archive_RomFS::Archive_RomFS(Loader::AppLoader& app_loader) { Archive_RomFS::Archive_RomFS(const Loader::AppLoader& app_loader) {
// Load the RomFS from the app // Load the RomFS from the app
if (Loader::ResultStatus::Success != app_loader.ReadRomFS(raw_data)) { if (Loader::ResultStatus::Success != app_loader.ReadRomFS(raw_data)) {
WARN_LOG(FILESYS, "Unable to read RomFS!"); WARN_LOG(FILESYS, "Unable to read RomFS!");

View File

@ -19,7 +19,7 @@ namespace FileSys {
/// File system interface to the RomFS archive /// File system interface to the RomFS archive
class Archive_RomFS : public Archive { class Archive_RomFS : public Archive {
public: public:
Archive_RomFS(Loader::AppLoader& app_loader); Archive_RomFS(const Loader::AppLoader& app_loader);
~Archive_RomFS(); ~Archive_RomFS();
/** /**

View File

@ -51,7 +51,7 @@ public:
* @param buffer Reference to buffer to store data * @param buffer Reference to buffer to store data
* @return ResultStatus result of function * @return ResultStatus result of function
*/ */
virtual ResultStatus ReadCode(std::vector<u8>& buffer) { virtual ResultStatus ReadCode(std::vector<u8>& buffer) const {
return ResultStatus::ErrorNotImplemented; return ResultStatus::ErrorNotImplemented;
} }
@ -60,7 +60,7 @@ public:
* @param buffer Reference to buffer to store data * @param buffer Reference to buffer to store data
* @return ResultStatus result of function * @return ResultStatus result of function
*/ */
virtual ResultStatus ReadIcon(std::vector<u8>& buffer) { virtual ResultStatus ReadIcon(std::vector<u8>& buffer) const {
return ResultStatus::ErrorNotImplemented; return ResultStatus::ErrorNotImplemented;
} }
@ -69,7 +69,7 @@ public:
* @param buffer Reference to buffer to store data * @param buffer Reference to buffer to store data
* @return ResultStatus result of function * @return ResultStatus result of function
*/ */
virtual ResultStatus ReadBanner(std::vector<u8>& buffer) { virtual ResultStatus ReadBanner(std::vector<u8>& buffer) const {
return ResultStatus::ErrorNotImplemented; return ResultStatus::ErrorNotImplemented;
} }
@ -78,7 +78,7 @@ public:
* @param buffer Reference to buffer to store data * @param buffer Reference to buffer to store data
* @return ResultStatus result of function * @return ResultStatus result of function
*/ */
virtual ResultStatus ReadLogo(std::vector<u8>& buffer) { virtual ResultStatus ReadLogo(std::vector<u8>& buffer) const {
return ResultStatus::ErrorNotImplemented; return ResultStatus::ErrorNotImplemented;
} }
@ -87,7 +87,7 @@ public:
* @param buffer Reference to buffer to store data * @param buffer Reference to buffer to store data
* @return ResultStatus result of function * @return ResultStatus result of function
*/ */
virtual ResultStatus ReadRomFS(std::vector<u8>& buffer) { virtual ResultStatus ReadRomFS(std::vector<u8>& buffer) const {
return ResultStatus::ErrorNotImplemented; return ResultStatus::ErrorNotImplemented;
} }
}; };

View File

@ -113,15 +113,13 @@ AppLoader_NCCH::AppLoader_NCCH(const std::string& filename) {
/// AppLoader_NCCH destructor /// AppLoader_NCCH destructor
AppLoader_NCCH::~AppLoader_NCCH() { AppLoader_NCCH::~AppLoader_NCCH() {
if (file.IsOpen())
file.Close();
} }
/** /**
* Loads .code section into memory for booting * Loads .code section into memory for booting
* @return ResultStatus result of function * @return ResultStatus result of function
*/ */
ResultStatus AppLoader_NCCH::LoadExec() { ResultStatus AppLoader_NCCH::LoadExec() const {
if (!is_loaded) if (!is_loaded)
return ResultStatus::ErrorNotLoaded; return ResultStatus::ErrorNotLoaded;
@ -140,8 +138,10 @@ ResultStatus AppLoader_NCCH::LoadExec() {
* @param buffer Vector to read data into * @param buffer Vector to read data into
* @return ResultStatus result of function * @return ResultStatus result of function
*/ */
ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) { ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) const {
// Iterate through the ExeFs archive until we find the .code file... // Iterate through the ExeFs archive until we find the .code file...
File::IOFile file(filename, "rb");
if (file.IsOpen()) {
for (int i = 0; i < kMaxSections; i++) { for (int i = 0; i < kMaxSections; i++) {
// Load the specified section... // Load the specified section...
if (strcmp((const char*)exefs_header.section[i].name, name) == 0) { if (strcmp((const char*)exefs_header.section[i].name, name) == 0) {
@ -151,7 +151,7 @@ ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>&
INFO_LOG(LOADER, " size: 0x%08X", exefs_header.section[i].size); INFO_LOG(LOADER, " size: 0x%08X", exefs_header.section[i].size);
s64 section_offset = (exefs_header.section[i].offset + exefs_offset + s64 section_offset = (exefs_header.section[i].offset + exefs_offset +
sizeof(ExeFs_Header) + ncch_offset); sizeof(ExeFs_Header)+ncch_offset);
file.Seek(section_offset, 0); file.Seek(section_offset, 0);
// Section is compressed... // Section is compressed...
@ -169,13 +169,18 @@ ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>&
return ResultStatus::ErrorInvalidFormat; return ResultStatus::ErrorInvalidFormat;
} }
// Section is uncompressed... // Section is uncompressed...
} else { }
else {
buffer.resize(exefs_header.section[i].size); buffer.resize(exefs_header.section[i].size);
file.ReadBytes(&buffer[0], exefs_header.section[i].size); file.ReadBytes(&buffer[0], exefs_header.section[i].size);
} }
return ResultStatus::Success; return ResultStatus::Success;
} }
} }
} else {
ERROR_LOG(LOADER, "Unable to read file %s!", filename.c_str());
return ResultStatus::Error;
}
return ResultStatus::ErrorNotUsed; return ResultStatus::ErrorNotUsed;
} }
@ -191,8 +196,7 @@ ResultStatus AppLoader_NCCH::Load() {
if (is_loaded) if (is_loaded)
return ResultStatus::ErrorAlreadyLoaded; return ResultStatus::ErrorAlreadyLoaded;
file = File::IOFile(filename, "rb"); File::IOFile file(filename, "rb");
if (file.IsOpen()) { if (file.IsOpen()) {
file.ReadBytes(&ncch_header, sizeof(NCCH_Header)); file.ReadBytes(&ncch_header, sizeof(NCCH_Header));
@ -235,6 +239,8 @@ ResultStatus AppLoader_NCCH::Load() {
LoadExec(); // Load the executable into memory for booting LoadExec(); // Load the executable into memory for booting
return ResultStatus::Success; return ResultStatus::Success;
} else {
ERROR_LOG(LOADER, "Unable to read file %s!", filename.c_str());
} }
return ResultStatus::Error; return ResultStatus::Error;
} }
@ -244,7 +250,7 @@ ResultStatus AppLoader_NCCH::Load() {
* @param buffer Reference to buffer to store data * @param buffer Reference to buffer to store data
* @return ResultStatus result of function * @return ResultStatus result of function
*/ */
ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) { ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) const {
return LoadSectionExeFS(".code", buffer); return LoadSectionExeFS(".code", buffer);
} }
@ -253,7 +259,7 @@ ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) {
* @param buffer Reference to buffer to store data * @param buffer Reference to buffer to store data
* @return ResultStatus result of function * @return ResultStatus result of function
*/ */
ResultStatus AppLoader_NCCH::ReadIcon(std::vector<u8>& buffer) { ResultStatus AppLoader_NCCH::ReadIcon(std::vector<u8>& buffer) const {
return LoadSectionExeFS("icon", buffer); return LoadSectionExeFS("icon", buffer);
} }
@ -262,7 +268,7 @@ ResultStatus AppLoader_NCCH::ReadIcon(std::vector<u8>& buffer) {
* @param buffer Reference to buffer to store data * @param buffer Reference to buffer to store data
* @return ResultStatus result of function * @return ResultStatus result of function
*/ */
ResultStatus AppLoader_NCCH::ReadBanner(std::vector<u8>& buffer) { ResultStatus AppLoader_NCCH::ReadBanner(std::vector<u8>& buffer) const {
return LoadSectionExeFS("banner", buffer); return LoadSectionExeFS("banner", buffer);
} }
@ -271,7 +277,7 @@ ResultStatus AppLoader_NCCH::ReadBanner(std::vector<u8>& buffer) {
* @param buffer Reference to buffer to store data * @param buffer Reference to buffer to store data
* @return ResultStatus result of function * @return ResultStatus result of function
*/ */
ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) { ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) const {
return LoadSectionExeFS("logo", buffer); return LoadSectionExeFS("logo", buffer);
} }
@ -280,7 +286,9 @@ ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) {
* @param buffer Reference to buffer to store data * @param buffer Reference to buffer to store data
* @return ResultStatus result of function * @return ResultStatus result of function
*/ */
ResultStatus AppLoader_NCCH::ReadRomFS(std::vector<u8>& buffer) { ResultStatus AppLoader_NCCH::ReadRomFS(std::vector<u8>& buffer) const {
File::IOFile file(filename, "rb");
if (file.IsOpen()) {
// Check if the NCCH has a RomFS... // Check if the NCCH has a RomFS...
if (ncch_header.romfs_offset != 0 && ncch_header.romfs_size != 0) { if (ncch_header.romfs_offset != 0 && ncch_header.romfs_size != 0) {
u32 romfs_offset = ncch_offset + (ncch_header.romfs_offset * kBlockSize) + 0x1000; u32 romfs_offset = ncch_offset + (ncch_header.romfs_offset * kBlockSize) + 0x1000;
@ -298,6 +306,10 @@ ResultStatus AppLoader_NCCH::ReadRomFS(std::vector<u8>& buffer) {
} }
NOTICE_LOG(LOADER, "RomFS unused"); NOTICE_LOG(LOADER, "RomFS unused");
return ResultStatus::ErrorNotUsed; return ResultStatus::ErrorNotUsed;
} else {
ERROR_LOG(LOADER, "Unable to read file %s!", filename.c_str());
}
return ResultStatus::Error;
} }
} // namespace Loader } // namespace Loader

View File

@ -161,35 +161,35 @@ public:
* @param buffer Reference to buffer to store data * @param buffer Reference to buffer to store data
* @return ResultStatus result of function * @return ResultStatus result of function
*/ */
ResultStatus ReadCode(std::vector<u8>& buffer); ResultStatus ReadCode(std::vector<u8>& buffer) const;
/** /**
* Get the icon (typically icon section) of the application * Get the icon (typically icon section) of the application
* @param buffer Reference to buffer to store data * @param buffer Reference to buffer to store data
* @return ResultStatus result of function * @return ResultStatus result of function
*/ */
ResultStatus ReadIcon(std::vector<u8>& buffer); ResultStatus ReadIcon(std::vector<u8>& buffer) const;
/** /**
* Get the banner (typically banner section) of the application * Get the banner (typically banner section) of the application
* @param buffer Reference to buffer to store data * @param buffer Reference to buffer to store data
* @return ResultStatus result of function * @return ResultStatus result of function
*/ */
ResultStatus ReadBanner(std::vector<u8>& buffer); ResultStatus ReadBanner(std::vector<u8>& buffer) const;
/** /**
* Get the logo (typically logo section) of the application * Get the logo (typically logo section) of the application
* @param buffer Reference to buffer to store data * @param buffer Reference to buffer to store data
* @return ResultStatus result of function * @return ResultStatus result of function
*/ */
ResultStatus ReadLogo(std::vector<u8>& buffer); ResultStatus ReadLogo(std::vector<u8>& buffer) const;
/** /**
* Get the RomFS of the application * Get the RomFS of the application
* @param buffer Reference to buffer to store data * @param buffer Reference to buffer to store data
* @return ResultStatus result of function * @return ResultStatus result of function
*/ */
ResultStatus ReadRomFS(std::vector<u8>& buffer); ResultStatus ReadRomFS(std::vector<u8>& buffer) const;
private: private:
@ -199,15 +199,14 @@ private:
* @param buffer Vector to read data into * @param buffer Vector to read data into
* @return ResultStatus result of function * @return ResultStatus result of function
*/ */
ResultStatus LoadSectionExeFS(const char* name, std::vector<u8>& buffer); ResultStatus LoadSectionExeFS(const char* name, std::vector<u8>& buffer) const;
/** /**
* Loads .code section into memory for booting * Loads .code section into memory for booting
* @return ResultStatus result of function * @return ResultStatus result of function
*/ */
ResultStatus LoadExec(); ResultStatus LoadExec() const;
File::IOFile file;
std::string filename; std::string filename;
bool is_loaded; bool is_loaded;