Files
wii-ipl/src/system/iplNandManager.cpp
2026-07-09 17:47:55 +01:00

235 lines
8.2 KiB
C++

#include "config.h"
#include "system/iplNandManager.h"
#include "iplSystem.h"
#include "system/iplNandMeta.h"
#include <cstring>
#ifdef STAND_ALONE_BUILD
#include "utility/iplESMisc.h"
#endif // STAND_ALONE_BUILD
namespace ipl {
namespace nand {
static ESTicketView* spSysMenuTik = NULL;
Manager::Manager(EGG::Heap* heap) {
s32 ret;
mDescriptor = -1;
mpFSTBuffer = NULL;
// Init task thread
mpTask = EGG::TaskThread::create(0x80, 18, 0x10000, heap);
OSInitMessageQueue(&mMsgQueue, &mMessage, 0x80);
mpTask->setEndMessageQueue(&mMsgQueue);
// Clear file name
strncpy(msNandPath, "", sizeof(msNandPath));
ES_SetUid(SYSMENU_TITLE_ID);
// Open content archive
#ifdef STAND_ALONE_BUILD
spSysMenuTik = (ESTicketView*)System::getMem2Sys()->alloc(OSRoundUp32B(sizeof(ESTicketView)), -DEFAULT_ALIGN);
ret = utility::ESMisc::GetTicketView(System::getMem2Sys(), SYSMENU_TITLE_ID, spSysMenuTik, 0);
if (ret < ES_ERR_OK) {
IPLErrorLogAndDisplay(MESG_ERR_FILE, "ES", mDescriptor, 0xBAD);
}
mDescriptor = ES_OpenTitleContentFile(SYSMENU_TITLE_ID, spSysMenuTik, SYSMENU_CONTENT_ID);
#else
mDescriptor = ES_OpenContentFile(SYSMENU_CONTENT_ID);
#endif // STAND_ALONE_BUILD
if (mDescriptor < ES_ERR_OK) {
IPLErrorLogAndDisplay(MESG_ERR_FILE, "ES", mDescriptor, 43);
}
// Read archive header
ARCHeader header ALIGN32;
ret = ES_ReadContentFile(mDescriptor, &header, sizeof(ARCHeader));
if (ret < ES_ERR_OK) {
IPLErrorLogAndDisplay(MESG_ERR_FILE, "ES", ret, 52);
}
u32 bufSize = OSRoundUp32B(header.fileStart);
mpFSTBuffer = new (DEFAULT_ALIGN) u8[bufSize];
// Seek to file system table offset
ret = ES_SeekContentFile(mDescriptor, 0, NAND_SEEK_BEG);
if (ret < ES_ERR_OK) {
IPLErrorLogAndDisplay(MESG_ERR_FILE, "ES", ret, 66);
}
// Read file system table
ret = ES_ReadContentFile(mDescriptor, mpFSTBuffer, bufSize);
if (ret < ES_ERR_OK) {
IPLErrorLogAndDisplay(MESG_ERR_FILE, "ES", ret, 72);
}
// Setup ARC with the read file system table
ARCInitHandle(mpFSTBuffer, &mArc);
}
// Stub
void Manager::openContentsAll() {
}
void Manager::closeContentsAll() {
#ifdef STAND_ALONE_BUILD
System::getMem2Sys()->free(spSysMenuTik);
#endif
s32 ret = ES_CloseContentFile(mDescriptor);
if (ret != ES_ERR_OK) {
IPLErrorLogAndDisplay(MESG_ERR_FILE, "ES", ret, 102);
}
mDescriptor = -1;
}
File* Manager::readAsync(EGG::Heap* heap, const char* fileName, int offset, u32 length, bool bJamRequest) {
char fullName[NAND_MAX_PATH + 1];
// Set up path
strncpy(fullName, msNandPath, sizeof(msNandPath));
strncat(fullName, fileName, sizeof(msNandPath) - strlen(fullName));
File* file = new (heap, 4) File(heap, fullName, NULL, NULL, offset, length, false);
if (bJamRequest) {
mpTask->requestJam(doReadTask, file, NULL);
} else {
mpTask->request(doReadTask, file, NULL);
}
return file;
}
File* Manager::read(EGG::Heap* heap, const char* fileName, int offset, u32 length, bool bJamRequest) {
char fullName[NAND_MAX_PATH + 1];
// Set up path
strncpy(fullName, msNandPath, sizeof(msNandPath));
strncat(fullName, fileName, sizeof(msNandPath) - strlen(fullName));
File* file = new (heap, 4) File(heap, fullName, NULL, NULL, offset, length, false);
file->read();
return file;
}
SharedFile* Manager::readSharedAsync(EGG::Heap* heap, const char* fileName, int index, int offset, u32 length, ESTitleId titleId,
int ticketIdx) {
SharedFile* file = new (heap, 4) SharedFile(heap, fileName, index, offset, length, titleId, ticketIdx);
mpTask->request(doReadTask, file, NULL);
return file;
}
MetaFile* Manager::readMetaHeaderAsync(EGG::Heap* heap, ESTitleId titleId, int offset, u32 length, MetaFile::Callback callback,
void* callbackWork, int ticketIdx) {
MetaFile* file = new (heap, 4) MetaFile(heap, "", NULL, titleId, offset, length, callback, callbackWork, ticketIdx);
System::getTask3()->request(doReadTask, file, NULL);
return file;
}
MetaFile* Manager::readMetaBodyAsync(EGG::Heap* heap, const char* fileName, ARCHandle* arc, ESTitleId titleId, int offset, u32 length,
MetaFile::Callback callback, void* callbackWork, int ticketIdx) {
MetaFile* file = new (heap, 4) MetaFile(heap, fileName, arc, titleId, offset, length, callback, callbackWork, ticketIdx);
mpTask->request(doReadTask, file, NULL);
return file;
}
LayoutFile* Manager::readLayoutAsync(EGG::Heap* heap, const char* fileName, bool bJamRequest) {
LayoutFile* file = readLayout_(heap, fileName, NULL, false);
if (bJamRequest) {
OSReport("Jam Request*****************************************************************\n");
mpTask->requestJam(doReadTask, file, NULL);
} else {
mpTask->request(doReadTask, file, NULL);
}
return file;
}
LayoutFile* Manager::readLayout(EGG::Heap* heap, const char* fileName, bool bJamRequest) {
LayoutFile* file = readLayout_(heap, fileName, NULL, false);
file->read();
return file;
}
LayoutFile* Manager::readLayout_(EGG::Heap* heap, const char* fileName, ARCHandle* arc, bool bIsNand) {
char fullName[NAND_MAX_PATH + 1];
// Set up path
strncpy(fullName, msNandPath, sizeof(msNandPath));
strncat(fullName, "/layout", sizeof(msNandPath) - strlen(fullName));
LayoutFile* file = new (heap, 4) LayoutFile(heap, fullName, fileName, arc, bIsNand);
return file;
}
void Manager::doReadTask(void* work) {
reinterpret_cast<File*>(work)->read();
}
File* Manager::writeAsync(EGG::Heap* heap, const char* fileName, void* buffer, u32 length, u8 perms) {
File* file = write_(heap, fileName, buffer, length, perms);
mpTask->request(doWriteTask, file, NULL);
return file;
}
File* Manager::write(EGG::Heap* heap, const char* fileName, void* buffer, u32 length, u8 perms) {
File* file = write_(heap, fileName, buffer, length, perms);
file->write();
return file;
}
File* Manager::write_(EGG::Heap* heap, const char* fileName, void* buffer, u32 length, u8 perms) {
char fullName[NAND_MAX_PATH + 1];
strncpy(fullName, msNandPath, sizeof(msNandPath));
strncat(fullName, fileName, sizeof(msNandPath) - strlen(fullName));
File* file = new (heap, 4) File(heap, fileName, (u8*)buffer, length, perms);
return file;
}
void Manager::doWriteTask(void* work) {
reinterpret_cast<File*>(work)->write();
}
void Manager::sendToken(int token) {
mpTask->request(doSendTokenTask, NULL, (void*)token);
}
void Manager::doSendTokenTask(void* work) {
}
BOOL Manager::receiveToken(int* token) {
BOOL result = FALSE;
OSMessage msg = EGG::TaskThread::waitQueueMessage(mpTask->getEndMessageQueue(), &result);
if (result && msg != NULL) {
*token = reinterpret_cast<int>(msg);
}
return (result && msg != NULL) ? TRUE : FALSE;
}
} // namespace nand
} // namespace ipl