summaryrefslogtreecommitdiff
path: root/src/core/loader
diff options
context:
space:
mode:
authorGravatar bunnei2014-07-04 13:11:09 -0400
committerGravatar bunnei2014-07-04 20:37:49 -0400
commit2c62d9255160b7302942d536c9c990b86c7fb907 (patch)
tree65496725f1557dd58f866a0313012ba4ed5f6aa3 /src/core/loader
parentQt: Updated open dialog to include NCCH formats. (diff)
downloadyuzu-2c62d9255160b7302942d536c9c990b86c7fb907.tar.gz
yuzu-2c62d9255160b7302942d536c9c990b86c7fb907.tar.xz
yuzu-2c62d9255160b7302942d536c9c990b86c7fb907.zip
Loader: Updated read methods to be const
- Required "file" handle to be made local and explicitly opened/closed as needed
Diffstat (limited to 'src/core/loader')
-rw-r--r--src/core/loader/loader.h10
-rw-r--r--src/core/loader/ncch.cpp118
-rw-r--r--src/core/loader/ncch.h15
3 files changed, 77 insertions, 66 deletions
diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h
index 7891c1ed7..c27b5b4b6 100644
--- a/src/core/loader/loader.h
+++ b/src/core/loader/loader.h
@@ -51,7 +51,7 @@ public:
51 * @param buffer Reference to buffer to store data 51 * @param buffer Reference to buffer to store data
52 * @return ResultStatus result of function 52 * @return ResultStatus result of function
53 */ 53 */
54 virtual ResultStatus ReadCode(std::vector<u8>& buffer) { 54 virtual ResultStatus ReadCode(std::vector<u8>& buffer) const {
55 return ResultStatus::ErrorNotImplemented; 55 return ResultStatus::ErrorNotImplemented;
56 } 56 }
57 57
@@ -60,7 +60,7 @@ public:
60 * @param buffer Reference to buffer to store data 60 * @param buffer Reference to buffer to store data
61 * @return ResultStatus result of function 61 * @return ResultStatus result of function
62 */ 62 */
63 virtual ResultStatus ReadIcon(std::vector<u8>& buffer) { 63 virtual ResultStatus ReadIcon(std::vector<u8>& buffer) const {
64 return ResultStatus::ErrorNotImplemented; 64 return ResultStatus::ErrorNotImplemented;
65 } 65 }
66 66
@@ -69,7 +69,7 @@ public:
69 * @param buffer Reference to buffer to store data 69 * @param buffer Reference to buffer to store data
70 * @return ResultStatus result of function 70 * @return ResultStatus result of function
71 */ 71 */
72 virtual ResultStatus ReadBanner(std::vector<u8>& buffer) { 72 virtual ResultStatus ReadBanner(std::vector<u8>& buffer) const {
73 return ResultStatus::ErrorNotImplemented; 73 return ResultStatus::ErrorNotImplemented;
74 } 74 }
75 75
@@ -78,7 +78,7 @@ public:
78 * @param buffer Reference to buffer to store data 78 * @param buffer Reference to buffer to store data
79 * @return ResultStatus result of function 79 * @return ResultStatus result of function
80 */ 80 */
81 virtual ResultStatus ReadLogo(std::vector<u8>& buffer) { 81 virtual ResultStatus ReadLogo(std::vector<u8>& buffer) const {
82 return ResultStatus::ErrorNotImplemented; 82 return ResultStatus::ErrorNotImplemented;
83 } 83 }
84 84
@@ -87,7 +87,7 @@ public:
87 * @param buffer Reference to buffer to store data 87 * @param buffer Reference to buffer to store data
88 * @return ResultStatus result of function 88 * @return ResultStatus result of function
89 */ 89 */
90 virtual ResultStatus ReadRomFS(std::vector<u8>& buffer) { 90 virtual ResultStatus ReadRomFS(std::vector<u8>& buffer) const {
91 return ResultStatus::ErrorNotImplemented; 91 return ResultStatus::ErrorNotImplemented;
92 } 92 }
93}; 93};
diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp
index 0e3f768ce..a82338904 100644
--- a/src/core/loader/ncch.cpp
+++ b/src/core/loader/ncch.cpp
@@ -113,15 +113,13 @@ AppLoader_NCCH::AppLoader_NCCH(const std::string& filename) {
113 113
114/// AppLoader_NCCH destructor 114/// AppLoader_NCCH destructor
115AppLoader_NCCH::~AppLoader_NCCH() { 115AppLoader_NCCH::~AppLoader_NCCH() {
116 if (file.IsOpen())
117 file.Close();
118} 116}
119 117
120/** 118/**
121 * Loads .code section into memory for booting 119 * Loads .code section into memory for booting
122 * @return ResultStatus result of function 120 * @return ResultStatus result of function
123 */ 121 */
124ResultStatus AppLoader_NCCH::LoadExec() { 122ResultStatus AppLoader_NCCH::LoadExec() const {
125 if (!is_loaded) 123 if (!is_loaded)
126 return ResultStatus::ErrorNotLoaded; 124 return ResultStatus::ErrorNotLoaded;
127 125
@@ -140,41 +138,48 @@ ResultStatus AppLoader_NCCH::LoadExec() {
140 * @param buffer Vector to read data into 138 * @param buffer Vector to read data into
141 * @return ResultStatus result of function 139 * @return ResultStatus result of function
142 */ 140 */
143ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) { 141ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) const {
144 // Iterate through the ExeFs archive until we find the .code file... 142 // Iterate through the ExeFs archive until we find the .code file...
145 for (int i = 0; i < kMaxSections; i++) { 143 File::IOFile file(filename, "rb");
146 // Load the specified section... 144 if (file.IsOpen()) {
147 if (strcmp((const char*)exefs_header.section[i].name, name) == 0) { 145 for (int i = 0; i < kMaxSections; i++) {
148 INFO_LOG(LOADER, "ExeFS section %d:", i); 146 // Load the specified section...
149 INFO_LOG(LOADER, " name: %s", exefs_header.section[i].name); 147 if (strcmp((const char*)exefs_header.section[i].name, name) == 0) {
150 INFO_LOG(LOADER, " offset: 0x%08X", exefs_header.section[i].offset); 148 INFO_LOG(LOADER, "ExeFS section %d:", i);
151 INFO_LOG(LOADER, " size: 0x%08X", exefs_header.section[i].size); 149 INFO_LOG(LOADER, " name: %s", exefs_header.section[i].name);
152 150 INFO_LOG(LOADER, " offset: 0x%08X", exefs_header.section[i].offset);
153 s64 section_offset = (exefs_header.section[i].offset + exefs_offset + 151 INFO_LOG(LOADER, " size: 0x%08X", exefs_header.section[i].size);
154 sizeof(ExeFs_Header) + ncch_offset); 152
155 file.Seek(section_offset, 0); 153 s64 section_offset = (exefs_header.section[i].offset + exefs_offset +
156 154 sizeof(ExeFs_Header)+ncch_offset);
157 // Section is compressed... 155 file.Seek(section_offset, 0);
158 if (i == 0 && is_compressed) { 156
159 // Read compressed .code section... 157 // Section is compressed...
160 std::unique_ptr<u8[]> temp_buffer(new u8[exefs_header.section[i].size]); 158 if (i == 0 && is_compressed) {
161 file.ReadBytes(&temp_buffer[0], exefs_header.section[i].size); 159 // Read compressed .code section...
162 160 std::unique_ptr<u8[]> temp_buffer(new u8[exefs_header.section[i].size]);
163 // Decompress .code section... 161 file.ReadBytes(&temp_buffer[0], exefs_header.section[i].size);
164 u32 decompressed_size = LZSS_GetDecompressedSize(&temp_buffer[0], 162
165 exefs_header.section[i].size); 163 // Decompress .code section...
166 buffer.resize(decompressed_size); 164 u32 decompressed_size = LZSS_GetDecompressedSize(&temp_buffer[0],
167 if (!LZSS_Decompress(&temp_buffer[0], exefs_header.section[i].size, &buffer[0], 165 exefs_header.section[i].size);
168 decompressed_size)) { 166 buffer.resize(decompressed_size);
169 return ResultStatus::ErrorInvalidFormat; 167 if (!LZSS_Decompress(&temp_buffer[0], exefs_header.section[i].size, &buffer[0],
168 decompressed_size)) {
169 return ResultStatus::ErrorInvalidFormat;
170 }
171 // Section is uncompressed...
170 } 172 }
171 // Section is uncompressed... 173 else {
172 } else { 174 buffer.resize(exefs_header.section[i].size);
173 buffer.resize(exefs_header.section[i].size); 175 file.ReadBytes(&buffer[0], exefs_header.section[i].size);
174 file.ReadBytes(&buffer[0], exefs_header.section[i].size); 176 }
177 return ResultStatus::Success;
175 } 178 }
176 return ResultStatus::Success;
177 } 179 }
180 } else {
181 ERROR_LOG(LOADER, "Unable to read file %s!", filename.c_str());
182 return ResultStatus::Error;
178 } 183 }
179 return ResultStatus::ErrorNotUsed; 184 return ResultStatus::ErrorNotUsed;
180} 185}
@@ -191,8 +196,7 @@ ResultStatus AppLoader_NCCH::Load() {
191 if (is_loaded) 196 if (is_loaded)
192 return ResultStatus::ErrorAlreadyLoaded; 197 return ResultStatus::ErrorAlreadyLoaded;
193 198
194 file = File::IOFile(filename, "rb"); 199 File::IOFile file(filename, "rb");
195
196 if (file.IsOpen()) { 200 if (file.IsOpen()) {
197 file.ReadBytes(&ncch_header, sizeof(NCCH_Header)); 201 file.ReadBytes(&ncch_header, sizeof(NCCH_Header));
198 202
@@ -235,6 +239,8 @@ ResultStatus AppLoader_NCCH::Load() {
235 LoadExec(); // Load the executable into memory for booting 239 LoadExec(); // Load the executable into memory for booting
236 240
237 return ResultStatus::Success; 241 return ResultStatus::Success;
242 } else {
243 ERROR_LOG(LOADER, "Unable to read file %s!", filename.c_str());
238 } 244 }
239 return ResultStatus::Error; 245 return ResultStatus::Error;
240} 246}
@@ -244,7 +250,7 @@ ResultStatus AppLoader_NCCH::Load() {
244 * @param buffer Reference to buffer to store data 250 * @param buffer Reference to buffer to store data
245 * @return ResultStatus result of function 251 * @return ResultStatus result of function
246 */ 252 */
247ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) { 253ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) const {
248 return LoadSectionExeFS(".code", buffer); 254 return LoadSectionExeFS(".code", buffer);
249} 255}
250 256
@@ -253,7 +259,7 @@ ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) {
253 * @param buffer Reference to buffer to store data 259 * @param buffer Reference to buffer to store data
254 * @return ResultStatus result of function 260 * @return ResultStatus result of function
255 */ 261 */
256ResultStatus AppLoader_NCCH::ReadIcon(std::vector<u8>& buffer) { 262ResultStatus AppLoader_NCCH::ReadIcon(std::vector<u8>& buffer) const {
257 return LoadSectionExeFS("icon", buffer); 263 return LoadSectionExeFS("icon", buffer);
258} 264}
259 265
@@ -262,7 +268,7 @@ ResultStatus AppLoader_NCCH::ReadIcon(std::vector<u8>& buffer) {
262 * @param buffer Reference to buffer to store data 268 * @param buffer Reference to buffer to store data
263 * @return ResultStatus result of function 269 * @return ResultStatus result of function
264 */ 270 */
265ResultStatus AppLoader_NCCH::ReadBanner(std::vector<u8>& buffer) { 271ResultStatus AppLoader_NCCH::ReadBanner(std::vector<u8>& buffer) const {
266 return LoadSectionExeFS("banner", buffer); 272 return LoadSectionExeFS("banner", buffer);
267} 273}
268 274
@@ -271,7 +277,7 @@ ResultStatus AppLoader_NCCH::ReadBanner(std::vector<u8>& buffer) {
271 * @param buffer Reference to buffer to store data 277 * @param buffer Reference to buffer to store data
272 * @return ResultStatus result of function 278 * @return ResultStatus result of function
273 */ 279 */
274ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) { 280ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) const {
275 return LoadSectionExeFS("logo", buffer); 281 return LoadSectionExeFS("logo", buffer);
276} 282}
277 283
@@ -280,24 +286,30 @@ ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) {
280 * @param buffer Reference to buffer to store data 286 * @param buffer Reference to buffer to store data
281 * @return ResultStatus result of function 287 * @return ResultStatus result of function
282 */ 288 */
283ResultStatus AppLoader_NCCH::ReadRomFS(std::vector<u8>& buffer) { 289ResultStatus AppLoader_NCCH::ReadRomFS(std::vector<u8>& buffer) const {
284 // Check if the NCCH has a RomFS... 290 File::IOFile file(filename, "rb");
285 if (ncch_header.romfs_offset != 0 && ncch_header.romfs_size != 0) { 291 if (file.IsOpen()) {
286 u32 romfs_offset = ncch_offset + (ncch_header.romfs_offset * kBlockSize) + 0x1000; 292 // Check if the NCCH has a RomFS...
287 u32 romfs_size = (ncch_header.romfs_size * kBlockSize) - 0x1000; 293 if (ncch_header.romfs_offset != 0 && ncch_header.romfs_size != 0) {
294 u32 romfs_offset = ncch_offset + (ncch_header.romfs_offset * kBlockSize) + 0x1000;
295 u32 romfs_size = (ncch_header.romfs_size * kBlockSize) - 0x1000;
288 296
289 INFO_LOG(LOADER, "RomFS offset: 0x%08X", romfs_offset); 297 INFO_LOG(LOADER, "RomFS offset: 0x%08X", romfs_offset);
290 INFO_LOG(LOADER, "RomFS size: 0x%08X", romfs_size); 298 INFO_LOG(LOADER, "RomFS size: 0x%08X", romfs_size);
291 299
292 buffer.resize(romfs_size); 300 buffer.resize(romfs_size);
293 301
294 file.Seek(romfs_offset, 0); 302 file.Seek(romfs_offset, 0);
295 file.ReadBytes(&buffer[0], romfs_size); 303 file.ReadBytes(&buffer[0], romfs_size);
296 304
297 return ResultStatus::Success; 305 return ResultStatus::Success;
306 }
307 NOTICE_LOG(LOADER, "RomFS unused");
308 return ResultStatus::ErrorNotUsed;
309 } else {
310 ERROR_LOG(LOADER, "Unable to read file %s!", filename.c_str());
298 } 311 }
299 NOTICE_LOG(LOADER, "RomFS unused"); 312 return ResultStatus::Error;
300 return ResultStatus::ErrorNotUsed;
301} 313}
302 314
303} // namespace Loader 315} // namespace Loader
diff --git a/src/core/loader/ncch.h b/src/core/loader/ncch.h
index 99753b244..c9a35228e 100644
--- a/src/core/loader/ncch.h
+++ b/src/core/loader/ncch.h
@@ -161,35 +161,35 @@ public:
161 * @param buffer Reference to buffer to store data 161 * @param buffer Reference to buffer to store data
162 * @return ResultStatus result of function 162 * @return ResultStatus result of function
163 */ 163 */
164 ResultStatus ReadCode(std::vector<u8>& buffer); 164 ResultStatus ReadCode(std::vector<u8>& buffer) const;
165 165
166 /** 166 /**
167 * Get the icon (typically icon section) of the application 167 * Get the icon (typically icon section) of the application
168 * @param buffer Reference to buffer to store data 168 * @param buffer Reference to buffer to store data
169 * @return ResultStatus result of function 169 * @return ResultStatus result of function
170 */ 170 */
171 ResultStatus ReadIcon(std::vector<u8>& buffer); 171 ResultStatus ReadIcon(std::vector<u8>& buffer) const;
172 172
173 /** 173 /**
174 * Get the banner (typically banner section) of the application 174 * Get the banner (typically banner section) of the application
175 * @param buffer Reference to buffer to store data 175 * @param buffer Reference to buffer to store data
176 * @return ResultStatus result of function 176 * @return ResultStatus result of function
177 */ 177 */
178 ResultStatus ReadBanner(std::vector<u8>& buffer); 178 ResultStatus ReadBanner(std::vector<u8>& buffer) const;
179 179
180 /** 180 /**
181 * Get the logo (typically logo section) of the application 181 * Get the logo (typically logo section) of the application
182 * @param buffer Reference to buffer to store data 182 * @param buffer Reference to buffer to store data
183 * @return ResultStatus result of function 183 * @return ResultStatus result of function
184 */ 184 */
185 ResultStatus ReadLogo(std::vector<u8>& buffer); 185 ResultStatus ReadLogo(std::vector<u8>& buffer) const;
186 186
187 /** 187 /**
188 * Get the RomFS of the application 188 * Get the RomFS of the application
189 * @param buffer Reference to buffer to store data 189 * @param buffer Reference to buffer to store data
190 * @return ResultStatus result of function 190 * @return ResultStatus result of function
191 */ 191 */
192 ResultStatus ReadRomFS(std::vector<u8>& buffer); 192 ResultStatus ReadRomFS(std::vector<u8>& buffer) const;
193 193
194private: 194private:
195 195
@@ -199,15 +199,14 @@ private:
199 * @param buffer Vector to read data into 199 * @param buffer Vector to read data into
200 * @return ResultStatus result of function 200 * @return ResultStatus result of function
201 */ 201 */
202 ResultStatus LoadSectionExeFS(const char* name, std::vector<u8>& buffer); 202 ResultStatus LoadSectionExeFS(const char* name, std::vector<u8>& buffer) const;
203 203
204 /** 204 /**
205 * Loads .code section into memory for booting 205 * Loads .code section into memory for booting
206 * @return ResultStatus result of function 206 * @return ResultStatus result of function
207 */ 207 */
208 ResultStatus LoadExec(); 208 ResultStatus LoadExec() const;
209 209
210 File::IOFile file;
211 std::string filename; 210 std::string filename;
212 211
213 bool is_loaded; 212 bool is_loaded;