summaryrefslogtreecommitdiffstats
path: root/src/core/loader/ncch.cpp
blob: 5b6f88604774ddc6cac3af2edb20a169d7e99484 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright 2014 Citra Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.

#include <memory>

#include "common/file_util.h"

#include "core/loader/ncch.h"
#include "core/hle/kernel/kernel.h"
#include "core/mem_map.h"

////////////////////////////////////////////////////////////////////////////////////////////////////
// Loader namespace

namespace Loader {

static const int kMaxSections   = 8;        ///< Maximum number of sections (files) in an ExeFs
static const int kBlockSize     = 0x200;    ///< Size of ExeFS blocks (in bytes)

/**
 * Get the decompressed size of an LZSS compressed ExeFS file
 * @param buffer Buffer of compressed file
 * @param size Size of compressed buffer
 * @return Size of decompressed buffer
 */
static u32 LZSS_GetDecompressedSize(u8* buffer, u32 size) {
    u32 offset_size = *(u32*)(buffer + size - 4);
    return offset_size + size;
}

/**
 * Decompress ExeFS file (compressed with LZSS)
 * @param compressed Compressed buffer
 * @param compressed_size Size of compressed buffer
 * @param decompressed Decompressed buffer
 * @param decompressed_size Size of decompressed buffer
 * @return True on success, otherwise false
 */
static bool LZSS_Decompress(u8* compressed, u32 compressed_size, u8* decompressed, u32 decompressed_size) {
    u8* footer = compressed + compressed_size - 8;
    u32 buffer_top_and_bottom = *(u32*)footer;
    u32 out = decompressed_size;
    u32 index = compressed_size - ((buffer_top_and_bottom >> 24) & 0xFF);
    u32 stop_index = compressed_size - (buffer_top_and_bottom & 0xFFFFFF);

    memset(decompressed, 0, decompressed_size);
    memcpy(decompressed, compressed, compressed_size);

    while(index > stop_index) {
       u8 control = compressed[--index];

        for(u32 i = 0; i < 8; i++) {
            if(index <= stop_index)
                break;
            if(index <= 0)
                break;
            if(out <= 0)
                break;

            if(control & 0x80) {
                // Check if compression is out of bounds
                if(index < 2) {
                    return false;
                }
                index -= 2;

                u32 segment_offset = compressed[index] | (compressed[index + 1] << 8);
                u32 segment_size = ((segment_offset >> 12) & 15) + 3;
                segment_offset &= 0x0FFF;
                segment_offset += 2;

                // Check if compression is out of bounds
                if(out < segment_size) {
                    return false;
                }
                for(u32 j = 0; j < segment_size; j++) {
                    // Check if compression is out of bounds
                    if(out + segment_offset >= decompressed_size) {
                        return false;
                    }

                    u8 data  = decompressed[out + segment_offset];
                    decompressed[--out] = data;
                }
            } else {
                // Check if compression is out of bounds
                if(out < 1) {
                    return false;
                }
                decompressed[--out] = compressed[--index];
            }
            control <<= 1;
        }
    }
    return true;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// AppLoader_NCCH class

/// AppLoader_NCCH constructor
AppLoader_NCCH::AppLoader_NCCH(const std::string& filename) {
    this->filename = filename;
    is_loaded = false;
    is_compressed = false;
    entry_point = 0;
    ncch_offset = 0;
    exefs_offset = 0;
}

/// AppLoader_NCCH destructor
AppLoader_NCCH::~AppLoader_NCCH() {
}

/**
 * Loads .code section into memory for booting
 * @return ResultStatus result of function
 */
ResultStatus AppLoader_NCCH::LoadExec() const {
    if (!is_loaded) 
        return ResultStatus::ErrorNotLoaded;

    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 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
 * @return ResultStatus result of function
 */
ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) const {
    // Iterate through the ExeFs archive until we find the .code file...
    FileUtil::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...
                }
                else {
                    buffer.resize(exefs_header.section[i].size);
                    file.ReadBytes(&buffer[0], exefs_header.section[i].size);
                }
                return ResultStatus::Success;
            }
        }
    } else {
        ERROR_LOG(LOADER, "Unable to read file %s!", filename.c_str());
        return ResultStatus::Error;
    }
    return ResultStatus::ErrorNotUsed;
} 

/**
 * Loads an NCCH file (e.g. from a CCI, or the first NCCH in a CXI)
 * @param error_string Pointer to string to put error message if an error has occurred
 * @todo Move NCSD parsing out of here and create a separate function for loading these
 * @return True on success, otherwise false
 */
ResultStatus AppLoader_NCCH::Load() {
    INFO_LOG(LOADER, "Loading NCCH file %s...", filename.c_str());

    if (is_loaded)
        return ResultStatus::ErrorAlreadyLoaded;

    FileUtil::IOFile file(filename, "rb");
    if (file.IsOpen()) {
        file.ReadBytes(&ncch_header, sizeof(NCCH_Header));

        // Skip NCSD header and load first NCCH (NCSD is just a container of NCCH files)...
        if (0 == memcmp(&ncch_header.magic, "NCSD", 4)) {
            WARN_LOG(LOADER, "Only loading the first (bootable) NCCH within the NCSD file!");
            ncch_offset = 0x4000;
            file.Seek(ncch_offset, 0);
            file.ReadBytes(&ncch_header, sizeof(NCCH_Header));
        }
        
        // Verify we are loading the correct file type...
        if (0 != memcmp(&ncch_header.magic, "NCCH", 4))
            return ResultStatus::ErrorInvalidFormat;

        // Read ExHeader...

        file.ReadBytes(&exheader_header, sizeof(ExHeader_Header));

        is_compressed = (exheader_header.codeset_info.flags.flag & 1) == 1;
        entry_point = exheader_header.codeset_info.text.address;

        INFO_LOG(LOADER, "Name:            %s", exheader_header.codeset_info.name);
        INFO_LOG(LOADER, "Code compressed: %s", is_compressed ? "yes" : "no");
        INFO_LOG(LOADER, "Entry point:     0x%08X", entry_point);

        // Read ExeFS...

        exefs_offset = ncch_header.exefs_offset * kBlockSize;
        u32 exefs_size = ncch_header.exefs_size * kBlockSize;

        INFO_LOG(LOADER, "ExeFS offset:    0x%08X", exefs_offset);
        INFO_LOG(LOADER, "ExeFS size:      0x%08X", exefs_size);

        file.Seek(exefs_offset + ncch_offset, 0);
        file.ReadBytes(&exefs_header, sizeof(ExeFs_Header));

        is_loaded = true; // Set state to loaded

        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 buffer Reference to buffer to store data
 * @return ResultStatus result of function
 */
ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) const {
    return LoadSectionExeFS(".code", buffer);
}

/**
 * Get the icon (typically icon section) of the application
 * @param buffer Reference to buffer to store data
 * @return ResultStatus result of function
 */
ResultStatus AppLoader_NCCH::ReadIcon(std::vector<u8>& buffer) const {
    return LoadSectionExeFS("icon", buffer);
}

/**
 * Get the banner (typically banner section) of the application
 * @param buffer Reference to buffer to store data
 * @return ResultStatus result of function
 */
ResultStatus AppLoader_NCCH::ReadBanner(std::vector<u8>& buffer) const {
    return LoadSectionExeFS("banner", buffer);
}

/**
 * Get the logo (typically logo section) of the application
 * @param buffer Reference to buffer to store data
 * @return ResultStatus result of function
 */
ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) const {
    return LoadSectionExeFS("logo", buffer);
}

/**
 * Get the RomFS of the application
 * @param buffer Reference to buffer to store data
 * @return ResultStatus result of function
 */
ResultStatus AppLoader_NCCH::ReadRomFS(std::vector<u8>& buffer) const {
    FileUtil::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);

            buffer.resize(romfs_size);

            file.Seek(romfs_offset, 0);
            file.ReadBytes(&buffer[0], romfs_size);

            return ResultStatus::Success;
        }
        NOTICE_LOG(LOADER, "RomFS unused");
        return ResultStatus::ErrorNotUsed;
    } else {
        ERROR_LOG(LOADER, "Unable to read file %s!", filename.c_str());
    }
    return ResultStatus::Error;
}

} // namespace Loader