This commit is contained in:
Maschell 2019-11-24 14:14:45 +01:00
parent 3e4e9687e2
commit ebe67519b3
8 changed files with 1315 additions and 260 deletions

3
.gitignore vendored
View File

@ -1,2 +1,5 @@
build/*
*.mod
sysapp.layout
sysapp.cbp
src/filelist.h

View File

@ -62,7 +62,7 @@ MACHDEP = -DESPRESSO -mcpu=750 -meabi -mhard-float
# -nostartfiles: Do not use the standard system startup files when linking
# -ffunction-sections: split up functions so linker can garbage collect
# -fdata-sections: split up data so linker can garbage collect
COMMON_CFLAGS := -O2 -Wall $(MACHDEP) -ffunction-sections -fdata-sections -Wl,-q $(COMMON_CFLAGS)
COMMON_CFLAGS := -O2 -Wall $(MACHDEP) -meabi -ffunction-sections -fdata-sections -Wl,-q $(COMMON_CFLAGS)
CFLAGS += -D__WIIU__ -D__WUT__
@ -102,6 +102,7 @@ LIBDIRS +=
#---------------------------------------------------------------------------------
ifneq ($(BUILD),$(notdir $(CURDIR)))
#---------------------------------------------------------------------------------
FILELIST := $(shell bash ./filelist.sh)
export PROJECTDIR := $(CURDIR)
export OUTPUT := $(CURDIR)/$(TARGETDIR)/$(TARGET)
export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
@ -238,6 +239,11 @@ $(OUTPUT) : $(OFILES)
%.ogg.o : %.ogg
@echo $(notdir $<)
@bin2s -a 32 $< | $(AS) -o $(@)
#---------------------------------------------------------------------------------
%.tga.o : %.tga
@echo $(notdir $<)
@bin2s -a 32 $< | $(AS) -o $(@)
#---------------------------------------------------------------------------------
###############################################################################
# Assembly listing rules

BIN
data/iconTex.tga Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

57
filelist.sh Normal file
View File

@ -0,0 +1,57 @@
#! /bin/bash
#
# Automatic resource file list generation
# Created by Dimok
outFile="./src/filelist.h"
count_old=$(cat $outFile 2>/dev/null | tr -d '\n\n' | sed 's/[^0-9]*\([0-9]*\).*/\1/')
count=0
if [[ $OSTYPE == darwin* ]];
then
for i in $(gfind ./data/ -maxdepth 1 -type f \( ! -printf "%f\n" \) | sort -f)
do
files[count]=$i
count=$((count+1))
done
else
for i in $(find ./data/ -maxdepth 1 -type f \( ! -printf "%f\n" \) | sort -f)
do
files[count]=$i
count=$((count+1))
done
fi
if [ "$count_old" != "$count" ] || [ ! -f $outFile ]
then
echo "Generating filelist.h for $count files." >&2
cat <<EOF > $outFile
/****************************************************************************
* This file is generated automatically.
* Includes $count files.
*
* NOTE:
* Any manual modification of this file will be overwriten by the generation.
****************************************************************************/
#ifndef _FILELIST_H_
#define _FILELIST_H_
EOF
for i in ${files[@]}
do
filename=${i%.*}
extension=${i##*.}
echo 'extern const unsigned char '$filename'_'$extension'[];' >> $outFile
echo 'extern const unsigned int '$filename'_'$extension'_size;' >> $outFile
echo '' >> $outFile
done
echo '#endif' >> $outFile
fi

View File

@ -8,7 +8,7 @@ TARGET := $(notdir $(CURDIR)).mod
SOURCES := src src/utils src/fs
# Data directories
DATA :=
DATA := data
# Include directories
INCLUDES := src
@ -47,4 +47,4 @@ EXTERNAL_LIBPATHS :=
# Will be added to the final include paths
# -IC:/library1/include
#---------------------------------------------------------------------------------
EXTERNAL_INCLUDE := -I$(WUT_ROOT)/include/libutilswut
EXTERNAL_INCLUDE :=

View File

@ -20,244 +20,105 @@
#include <map>
#include <utils/utils.h>
#include <fs/DirList.h>
#define TARGET_WIDTH (854)
#define TARGET_HEIGHT (480)
#include "romfs_dev.h"
#include "filelist.h"
WUPS_ALLOW_KERNEL()
void printVPADButtons(VPADStatus * buffer);
WUPS_PLUGIN_NAME("Vpad input logger");
WUPS_PLUGIN_DESCRIPTION("Prints information about vpad inputs and sensors");
WUPS_PLUGIN_VERSION("v1.0");
WUPS_PLUGIN_NAME("System Menu Launcher");
WUPS_PLUGIN_DESCRIPTION("Allows the user to load homebrew from the System Menu");
WUPS_PLUGIN_VERSION("0.1");
WUPS_PLUGIN_AUTHOR("Maschell");
WUPS_PLUGIN_LICENSE("GPL");
IOSHandle handles[100];
typedef struct WUT_PACKED FileInfos_ {
char path[256];
char name[256];
int32_t source;
bool romfsMounted;
} FileInfos;
struct WUT_PACKED ACPMetaData {
char bootmovie[80696];
char bootlogo[28604];
};
extern "C" {
extern void __init_wut_malloc();
extern void __fini_wut_malloc();
}
WUPS_FS_ACCESS()
FileInfos gFileInfos[100] __attribute__((section(".data")));
ACPMetaXml gLaunchXML __attribute__((section(".data")));
MCPTitleListType template_title __attribute__((section(".data")));
BOOL gHomebrewLaunched __attribute__((section(".data")));
WUPS_USE_WUT_CRT()
INITIALIZE_PLUGIN() {
memset((void*) &template_title,0,sizeof(template_title));
memset((void*) &gLaunchXML,0,sizeof(gLaunchXML));
memset((void*) &gFileInfos,0,sizeof(gFileInfos));
gHomebrewLaunched = FALSE;
}
ON_APPLICATION_START(args) {
__init_wut_malloc();
socket_lib_init();
log_init();
if(_SYSGetSystemApplicationTitleId(APP_ID_Updater) == OSGetTitleID()) {
gHomebrewLaunched = FALSE;
int EndsWith(const char *str, const char *suffix);
void listdir(const char *name, int indent) {
DEBUG_FUNCTION_LINE("Reading %s\n",name);
DIR *dir;
struct dirent *entry;
if (!(dir = opendir(name)))
return;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_DIR) {
char path[1024];
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
snprintf(path, sizeof(path), "%s%s", name, entry->d_name);
DEBUG_FUNCTION_LINE("%*s[%s]\n", indent, "", entry->d_name);
listdir(path, indent + 2);
} else {
DEBUG_FUNCTION_LINE("%*s- %s\n", indent, "", entry->d_name);
}
}
closedir(dir);
}
ON_APPLICATION_ENDING() {
__fini_wut_malloc();
ON_APPLICATION_START(args) {
socket_lib_init();
log_init();
DEBUG_FUNCTION_LINE("IN PLUGIN\n");
if(_SYSGetSystemApplicationTitleId(SYSTEM_APP_ID_HEALTH_AND_SAFETY) != OSGetTitleID()) {
DEBUG_FUNCTION_LINE("gHomebrewLaunched to FALSE\n");
gHomebrewLaunched = FALSE;
}
}
void unmountAllRomfs();
ON_APPLICATION_END() {
unmountAllRomfs();
}
void fillXmlForTitleID(uint32_t titleid_upper, uint32_t titleid_lower, ACPMetaXml* out_buf) {
out_buf->title_id = ((uint64_t)titleid_upper * 0x100000000) + titleid_lower;
// TODO
char buffer [25];
snprintf(buffer,25,"/custom/%08X%08X", titleid_upper,titleid_lower);
DEBUG_FUNCTION_LINE("%s\n",buffer);
strncpy(out_buf->longname_en,buffer,strlen(buffer));
strncpy(out_buf->shortname_en,buffer,strlen(buffer));
strncpy(out_buf->longname_en,gFileInfos[titleid_lower].name,511);
strncpy(out_buf->shortname_en,gFileInfos[titleid_lower].name,255);
strncpy(out_buf->publisher_en,gFileInfos[titleid_lower].name,255);
out_buf->e_manual = 1;
out_buf->e_manual_version = 1;
out_buf->e_manual_version = 0;
out_buf->title_version = 1;
out_buf->network_use = 1;
out_buf->launching_flag = 4;
out_buf->online_account_use = 1;
out_buf->os_version = 0x000500101000400A;
out_buf->region = 0xFFFFFFFF;
out_buf->common_save_size = 0x0000000001790000;
out_buf->group_id = 0x400;
out_buf->drc_use = 1;
out_buf->version = 1;
out_buf->reserved_flag0 = 0x00010001;
out_buf->reserved_flag6 = 0x00000003;
out_buf->pc_usk = 128;
strncpy(out_buf->product_code,"WUP-P-HBLD",strlen("WUP-P-HBLD"));
strncpy(out_buf->content_platform,"WUP",strlen("WUP"));
strncpy(out_buf->company_code,"0001",strlen("0001"));
}
DECL_FUNCTION(int32_t, MCP_TitleList, uint32_t handle, uint32_t* outTitleCount, MCPTitleListType* titleList, uint32_t size) {
int32_t result = real_MCP_TitleList(handle, outTitleCount, titleList, size);
uint32_t titlecount = *outTitleCount;
DirList dirList("sd:/wiiu/apps", ".rpx", DirList::Files | DirList::CheckSubfolders, 1);
dirList.SortList();
int j = 0;
for(int i = 0; i < dirList.GetFilecount(); i++) {
//! skip our own application in the listing
if(strcasecmp(dirList.GetFilename(i), "homebrew_launcher.elf") == 0)
continue;
//! skip our own application in the listing
if(strcasecmp(dirList.GetFilename(i), "homebrew_launcher.rpx") == 0)
continue;
//! skip hidden linux and mac files
if(dirList.GetFilename(i)[0] == '.' || dirList.GetFilename(i)[0] == '_')
continue;
char buffer [25];
snprintf(buffer,25,"/custom/%08X%08X", 0x0005000F, j);
strcpy(template_title.path,buffer);
const char * indexedDevice = "mlc";
strcpy(template_title.indexedDevice,indexedDevice);
template_title.appType = MCP_APP_TYPE_SystemApps; // for some reason we need to act like an system app.
template_title.titleId = 0x0005000F00000000 + j;
template_title.titleVersion = 1;
template_title.groupId = 0x400;
template_title.osVersion = OSGetOSID();
template_title.sdkVersion = __OSGetProcessSDKVersion();
template_title.unk0x60 = 1;
memcpy(&(titleList[titlecount]), &template_title,sizeof(template_title));
titlecount++;
j++;
}
*outTitleCount = titlecount;
for(int i = 0; i< *outTitleCount; i++) {
DEBUG_FUNCTION_LINE("%s %08X %016llX %08X %08X %s \n",titleList[i].path, titleList[i].appType, titleList[i].osVersion, titleList[i].sdkVersion,titleList[i].groupId,titleList[i].indexedDevice);
//dumpHex(&titleList[i],sizeof(MCPTitleListType));
}
return result;
}
DECL_FUNCTION(int32_t, MCP_GetTitleInfoByTitleAndDevice, uint32_t mcp_handle, uint32_t titleid_lower_1, uint32_t titleid_upper, uint32_t titleid_lower_2, uint32_t unknown, MCPTitleListType* title) {
if(gHomebrewLaunched) {
memcpy(title, &(template_title), sizeof(MCPTitleListType));
} else if(titleid_upper == 0x0005000F) {
char buffer [25];
snprintf(buffer,25,"/custom/%08X%08X", titleid_lower_2, titleid_upper);
strcpy(template_title.path,buffer);
template_title.titleId = 0x0005000F00000000 + titleid_lower_1;
memcpy(title, &(template_title), sizeof(MCPTitleListType));
DEBUG_FUNCTION_LINE("%s %08X %016llX %08X \n",title->path, title->appType, title->osVersion, title->sdkVersion);
return 0;
}
int result = real_MCP_GetTitleInfoByTitleAndDevice(mcp_handle, titleid_lower_1, titleid_upper, titleid_lower_2, unknown, title);
return result;
}
DECL_FUNCTION(int32_t, ACPCheckTitleLaunchByTitleListTypeEx, MCPTitleListType* title, uint32_t u2) {
if(title->titleId & 0x0005000F00000000) {
DEBUG_FUNCTION_LINE("Started homebrew\n");
gHomebrewLaunched = TRUE;
fillXmlForTitleID((title->titleId & 0xFFFFFFFF00000000) >> 32,(title->titleId & 0xFFFFFFFF), &gLaunchXML);
return 0;
}
int result = real_ACPCheckTitleLaunchByTitleListTypeEx(title, u2);
return result;
}
int EndsWith(const char *str, const char *suffix) {
if (!str || !suffix)
return 0;
size_t lenstr = strlen(str);
size_t lensuffix = strlen(suffix);
if (lensuffix > lenstr)
return 0;
return strncmp(str + lenstr - lensuffix, suffix, lensuffix) == 0;
}
DECL_FUNCTION(void, FSInit) {
socket_lib_init();
log_init();
OSDynLoad_Module sModuleHandle = NULL;
OSDynLoad_Acquire("nn_acp.rpl", &sModuleHandle);
void *functionptr = NULL;
OSDynLoad_FindExport(sModuleHandle, FALSE, "ACPGetTitleMetaDir", &functionptr);
DEBUG_FUNCTION_LINE("%08X %08X\n",functionptr, OSEffectiveToPhysical((uint32_t)functionptr));
return real_FSInit();
}
DECL_FUNCTION(int, FSOpenFile, FSClient *pClient, FSCmdBlock *pCmd, char *path, const char *mode, int *handle, int error) {
char * start = "/vol/storage_mlc01/sys/title/0005000F";
char * icon = "iconTex.tga";
if(EndsWith(path,icon)) {
if(gHomebrewLaunched || strncmp(path,start,strlen(start)) == 0) {
strcpy(path,"/vol/storage_mlc01/usr/title/00050000/10172000/meta/iconTex.tga");
}
}
int result = real_FSOpenFile(pClient, pCmd, path, mode, handle, error);
DEBUG_FUNCTION_LINE("%s! Result %08X %d\n",path,*handle, result);
return result;
}
DECL_FUNCTION(FSStatus, FSReadFile, FSClient *client, FSCmdBlock *block, uint8_t *buffer, uint32_t size, uint32_t count, FSFileHandle handle,uint32_t unk1, uint32_t flags) {
FSStatus result = real_FSReadFile(client, block, buffer, size, count, handle, unk1, flags);
DEBUG_FUNCTION_LINE("%08X %d\n", handle, result);
return result;
}
DECL_FUNCTION(FSStatus,FSBindMount,FSClient *client,
FSCmdBlock *cmd,
const char *source,
const char *target,
uint32_t flags) {
FSStatus result = real_FSBindMount(client, cmd, source, target, flags);
DEBUG_FUNCTION_LINE("%s %s %d\n", source,target, result);
return result;
}
DECL_FUNCTION(FSStatus, FSReadDir, FSClient *client, FSCmdBlock *block, FSDirectoryHandle handle, FSDirectoryEntry *entry, uint32_t flags) {
FSStatus result = real_FSReadDir(client, block, handle, entry, flags);
DEBUG_FUNCTION_LINE("%s %d\n", entry->name, result);
return result;
}
DECL_FUNCTION(int32_t, ACPGetTitleMetaXmlByDevice, uint32_t titleid_upper, uint32_t titleid_lower, ACPMetaXml* out_buf, uint32_t device, uint32_t u1) {
DEBUG_FUNCTION_LINE("\n");
int result = real_ACPGetTitleMetaXmlByDevice(titleid_upper, titleid_lower, out_buf, device,u1);
if(titleid_upper == 0x00050000 && titleid_lower == 0x13374842) {
//dumpHex((void*)out_buf,0x3500);
}
if(titleid_upper == 0x0005000F) {
fillXmlForTitleID(titleid_upper,titleid_lower, out_buf);
result = 0;
}
DEBUG_FUNCTION_LINE("TitleID: %08X%08X res:%016llX device: %d %08X = %08X \n",titleid_upper,titleid_lower,out_buf->title_id, device,u1,result);
return result;
}
DECL_FUNCTION(int32_t, ACPGetTitleMetaDirByDevice, uint32_t titleid_upper, uint32_t titleid_lower, char* out_buf, uint32_t size, int device) {
DEBUG_FUNCTION_LINE("\n");
int result = real_ACPGetTitleMetaDirByDevice(titleid_upper, titleid_lower, out_buf, size, device);
if(titleid_upper == 0x0005000F) {
strcpy(out_buf,"/vol/storage_mlc01/usr/title/00050000/10119b00");
return -1;
}
DEBUG_FUNCTION_LINE("TitleID: %08X%08X path:%s (%d)device: %d = %08X \n",titleid_upper,titleid_lower,out_buf,size, device,result);
return result;
}
// You must free the result if result is non-NULL.
char *str_replace(char *orig, char *rep, char *with) {
char *result; // the return string
@ -305,25 +166,354 @@ char *str_replace(char *orig, char *rep, char *with) {
return result;
}
DECL_FUNCTION(int32_t, MCP_TitleList, uint32_t handle, uint32_t* outTitleCount, MCPTitleListType* titleList, uint32_t size) {
int32_t result = real_MCP_TitleList(handle, outTitleCount, titleList, size);
uint32_t titlecount = *outTitleCount;
DirList dirList("fs:/vol/external01/wiiu/apps", ".rpx,.wbf", DirList::Files | DirList::CheckSubfolders, 1);
dirList.SortList();
int j = 0;
for(int i = 0; i < dirList.GetFilecount(); i++) {
//! skip our own application in the listing
if(strcasecmp(dirList.GetFilename(i), "homebrew_launcher.rpx") == 0) {
continue;
}
//! skip our own application in the listing
if(strcasecmp(dirList.GetFilename(i), "temp.rpx") == 0) {
continue;
}
//! skip hidden linux and mac files
if(dirList.GetFilename(i)[0] == '.' || dirList.GetFilename(i)[0] == '_') {
continue;
}
char buffer [25];
snprintf(buffer,25,"/custom/%08X%08X", 0x0005000F, j);
strcpy(template_title.path,buffer);
char * repl = (char*)"fs:/vol/external01/";
char * with = (char*)"";
char * input = (char*) dirList.GetFilepath(i);
char * path = str_replace(input,repl, with);
if(path != NULL) {
strncpy(gFileInfos[j].path,path, 255);
free(path);
}
strncpy(gFileInfos[j].name, dirList.GetFilename(i),255);
gFileInfos[j].source = 0; //SD Card;
//DEBUG_FUNCTION_LINE("%s\n",gFileInfos[j].path);
const char * indexedDevice = "mlc";
strcpy(template_title.indexedDevice,indexedDevice);
if(EndsWith(gFileInfos[j].name, ".wbf")) {
template_title.appType = MCP_APP_TYPE_GAME;
} else {
// System apps don't have a splash screen.
template_title.appType = MCP_APP_TYPE_SYSTEM_APPS;
}
template_title.titleId = 0x0005000F00000000 + j;
template_title.titleVersion = 1;
template_title.groupId = 0x400;
template_title.osVersion = OSGetOSID();
template_title.sdkVersion = __OSGetProcessSDKVersion();
template_title.unk0x60 = 0;
memcpy(&(titleList[titlecount]), &template_title,sizeof(template_title));
titlecount++;
j++;
}
*outTitleCount = titlecount;
return result;
}
DECL_FUNCTION(int32_t, MCP_GetTitleInfoByTitleAndDevice, uint32_t mcp_handle, uint32_t titleid_lower_1, uint32_t titleid_upper, uint32_t titleid_lower_2, uint32_t unknown, MCPTitleListType* title) {
if(gHomebrewLaunched) {
memcpy(title, &(template_title), sizeof(MCPTitleListType));
} else if(titleid_upper == 0x0005000F) {
char buffer [25];
snprintf(buffer,25,"/custom/%08X%08X", titleid_upper, titleid_lower_2);
strcpy(template_title.path,buffer);
template_title.titleId = 0x0005000F00000000 + titleid_lower_1;
memcpy(title, &(template_title), sizeof(MCPTitleListType));
return 0;
}
int result = real_MCP_GetTitleInfoByTitleAndDevice(mcp_handle, titleid_lower_1, titleid_upper, titleid_lower_2, unknown, title);
return result;
}
typedef struct __attribute((packed)) {
uint32_t command;
uint32_t target;
uint32_t filesize;
uint32_t fileoffset;
char path[256];
}
LOAD_REQUEST;
int32_t getRPXInfoForID(uint32_t id, romfs_fileInfo * info);
DECL_FUNCTION(int32_t, ACPCheckTitleLaunchByTitleListTypeEx, MCPTitleListType* title, uint32_t u2) {
DEBUG_FUNCTION_LINE("Started homebrew??\n");
if((title->titleId & 0x0005000F00000000) == 0x0005000F00000000) {
DEBUG_FUNCTION_LINE("Started homebrew\n");
gHomebrewLaunched = TRUE;
fillXmlForTitleID((title->titleId & 0xFFFFFFFF00000000) >> 32,(title->titleId & 0xFFFFFFFF), &gLaunchXML);
LOAD_REQUEST request;
memset(&request, 0, sizeof(request));
request.command = 0xFC; // IPC_CUSTOM_LOAD_CUSTOM_RPX;
request.target = 0; // LOAD_FILE_TARGET_SD_CARD
request.filesize = 0; // unknown
request.fileoffset = 0; //
romfs_fileInfo info;
int res = getRPXInfoForID((title->titleId & 0xFFFFFFFF),&info);
if(res >= 0) {
request.filesize = ((uint32_t*)&info.length)[1];
request.fileoffset = ((uint32_t*)&info.offset)[1];
}
DEBUG_FUNCTION_LINE("%d\n", res);
strncpy(request.path, gFileInfos[(uint32_t)(title->titleId & 0xFFFFFFFF)].path, 255);
DEBUG_FUNCTION_LINE("Loading file %s size: %08X offset: %08X\n", request.path, request.filesize, request.fileoffset);
DCFlushRange(&request, sizeof(LOAD_REQUEST));
int mcpFd = IOS_Open("/dev/mcp", (IOSOpenMode)0);
if(mcpFd >= 0) {
int out = 0;
IOS_Ioctl(mcpFd, 100, &request, sizeof(request), &out, sizeof(out));
IOS_Close(mcpFd);
}
return 0;
}
int result = real_ACPCheckTitleLaunchByTitleListTypeEx(title, u2);
return result;
}
int EndsWith(const char *str, const char *suffix) {
if (!str || !suffix)
return 0;
size_t lenstr = strlen(str);
size_t lensuffix = strlen(suffix);
if (lensuffix > lenstr)
return 0;
return strncmp(str + lenstr - lensuffix, suffix, lensuffix) == 0;
}
bool mountRomfs(uint32_t id) {
if(!gFileInfos[id].romfsMounted) {
char buffer [256];
snprintf(buffer,256,"fs:/vol/external01/%s", gFileInfos[id].path);
char romName [10];
snprintf(romName,10,"%08X", id);
DEBUG_FUNCTION_LINE("Mount %s as %s\n", buffer, romName);
if(romfsMount(romName,buffer) == 0) {
DEBUG_FUNCTION_LINE("Mounted successfully \n");
gFileInfos[id].romfsMounted = true;
return true;
} else {
return false;
}
}
return true;
}
void unmountRomfs(uint32_t id) {
if(gFileInfos[id].romfsMounted) {
char romName [10];
snprintf(romName,10,"%08X", id);
//DEBUG_FUNCTION_LINE("Unmounting %s\n", romName);
int res = romfsUnmount(romName);
//DEBUG_FUNCTION_LINE("res: %d\n",res);
gFileInfos[id].romfsMounted = false;
}
}
void unmountAllRomfs() {
for(int i = 0; i < 100; i++) {
unmountRomfs(i);
}
}
int32_t FSOpenFile_for_ID(uint32_t id, const char * filepath, int * handle) {
if(!mountRomfs(id)) {
return -1;
}
char romName [10];
snprintf(romName,10,"%08X", id);
char * test = (char *) malloc(strlen(filepath)+1);
char last = 0;
int j = 0;
for(int i = 0; filepath[i] != 0; i++) {
if(filepath[i] == '/' ) {
if(filepath[i] != last) {
test[j++] = filepath[i];
}
} else {
test[j++] = filepath[i];
}
last = filepath[i];
}
test[j++] = 0;
char buffer [256];
snprintf(buffer,256,"%s:/%s",romName, test);
int fd = open(buffer,0);
if(fd >= 0) {
DEBUG_FUNCTION_LINE("Opened %s from %s \n",buffer, romName );
*handle = 0xFF000000 + fd;
return 0;
}
return -2;
}
int32_t getRPXInfoForID(uint32_t id, romfs_fileInfo * info) {
if(!mountRomfs(id)) {
return -1;
}
DIR *dir;
struct dirent *entry;
char romName [10];
snprintf(romName,10,"%08X", id);
char root[12];
snprintf(root,12,"%08X:/", id);
if (!(dir = opendir(root))) {
return -2;
}
bool found = false;
int res = -3;
while ((entry = readdir(dir)) != NULL) {
if(EndsWith(entry->d_name, ".rpx")) {
if(romfs_GetFileInfoPerPath(romName, entry->d_name, info) >= 0) {
found = true;
res = 0;
}
break;
}
}
closedir(dir);
if(!found) {
return -4;
}
return res;
}
DECL_FUNCTION(int, FSOpenFile, FSClient *client, FSCmdBlock *block, char *path, const char *mode, int *handle, int error) {
char * start = "/vol/storage_mlc01/sys/title/0005000F";
char * icon = ".tga";
char * iconTex = "iconTex.tga";
char * sound = ".btsnd";
if(EndsWith(path,icon) || EndsWith(path,sound)) {
if(strncmp(path,start,strlen(start)) == 0/* || (gHomebrewLaunched && EndsWith(path,iconTex))*/) {
int res = FS_STATUS_NOT_FOUND;
if(EndsWith(path,iconTex)) {
// fallback to dummy icon if loaded homebrew is no .wbf
*handle = 0x1337;
res = FS_STATUS_OK;
}
uint32_t val;
char * id = path+1+strlen(start);
id[8] = 0;
char * ending = id+9;
sscanf(id,"%08X", &val);
if(FSOpenFile_for_ID(val, ending, handle) < 0) {
return res;
}
return FS_STATUS_OK;
}
}
int result = real_FSOpenFile(client, block, path, mode, handle, error);
return result;
}
DECL_FUNCTION(FSStatus, FSCloseFile, FSClient *client, FSCmdBlock *block, FSFileHandle handle, uint32_t flags) {
if(handle == 0x1337) {
return FS_STATUS_OK;
}
if((handle & 0xFF000000) == 0xFF000000) {
int32_t fd = (handle & 0x00FFFFFF);
close(fd);
return FS_STATUS_OK;
}
return real_FSCloseFile(client,block,handle,flags);
}
DECL_FUNCTION(FSStatus, FSReadFile, FSClient *client, FSCmdBlock *block, uint8_t *buffer, uint32_t size, uint32_t count, FSFileHandle handle,uint32_t unk1, uint32_t flags) {
if(handle == 0x1337) {
int cpySize = size*count;
if(iconTex_tga_size < cpySize) {
cpySize = iconTex_tga_size;
}
memcpy(buffer, iconTex_tga, cpySize);
return (FSStatus)(cpySize/size);
}
if((handle & 0xFF000000) == 0xFF000000) {
int32_t fd = (handle & 0x00FFFFFF);
return (FSStatus)(read(fd, buffer, size*count) /size);
}
FSStatus result = real_FSReadFile(client, block, buffer, size, count, handle, unk1, flags);
return result;
}
DECL_FUNCTION(int32_t, ACPGetTitleMetaXmlByDevice, uint32_t titleid_upper, uint32_t titleid_lower, ACPMetaXml* out_buf, uint32_t device, uint32_t u1) {
int result = real_ACPGetTitleMetaXmlByDevice(titleid_upper, titleid_lower, out_buf, device,u1);
if(titleid_upper == 0x0005000F) {
fillXmlForTitleID(titleid_upper,titleid_lower, out_buf);
result = 0;
}
return result;
}
DECL_FUNCTION(int32_t, ACPGetTitleMetaDirByDevice, uint32_t titleid_upper, uint32_t titleid_lower, char* out_buf, uint32_t size, int device) {
if(titleid_upper == 0x0005000F) {
snprintf(out_buf,53,"/vol/storage_mlc01/sys/title/%08X/%08X/meta", titleid_upper, titleid_lower);
return 0;
}
int result = real_ACPGetTitleMetaDirByDevice(titleid_upper, titleid_lower, out_buf, size, device);
return result;
}
DECL_FUNCTION(int32_t, _SYSLaunchTitleByPathFromLauncher, char* pathToLoad, uint32_t u2) {
DEBUG_FUNCTION_LINE("\n");
const char * start = "/custom/";
if(strncmp(pathToLoad,start,strlen(start)) == 0) {
strcpy(template_title.path,pathToLoad);
// always load H&S app
strcpy(pathToLoad,"/vol/storage_mlc01/usr/title/00050000/10119b00");
uint64_t titleID = _SYSGetSystemApplicationTitleId(SYSTEM_APP_ID_HEALTH_AND_SAFETY);
snprintf(pathToLoad,47,"/vol/storage_mlc01/sys/title/%08x/%08x", (uint32_t) (titleID >> 32), (uint32_t) (0x00000000FFFFFFFF & titleID));
}
int32_t result = real__SYSLaunchTitleByPathFromLauncher(pathToLoad, strlen(pathToLoad));
DEBUG_FUNCTION_LINE("%s %08X result %08X \n",pathToLoad,u2,result);
return result;
}
DECL_FUNCTION(int32_t, ACPGetLaunchMetaXml, ACPMetaXml * metaxml) {
DEBUG_FUNCTION_LINE("\n");
int result = real_ACPGetLaunchMetaXml(metaxml);
if(gHomebrewLaunched) {
memcpy(metaxml, &gLaunchXML, sizeof(gLaunchXML));
@ -331,52 +521,59 @@ DECL_FUNCTION(int32_t, ACPGetLaunchMetaXml, ACPMetaXml * metaxml) {
return result;
}
DECL_FUNCTION(uint32_t, ACPGetApplicationBox,uint32_t * u1, uint32_t * u2, uint32_t u3, uint32_t u4) {
if(u3 == 0x0005000F) {
uint64_t titleID = _SYSGetSystemApplicationTitleId(SYSTEM_APP_ID_HEALTH_AND_SAFETY);
u3 = (uint32_t) (titleID >> 32);
u4 = (uint32_t) (0x00000000FFFFFFFF & titleID);
}
uint32_t result = real_ACPGetApplicationBox(u1,u2,u3,u4);
return result;
}
DECL_FUNCTION(uint32_t, PatchChkStart__3RplFRCQ3_2nn6drmapp8StartArg, uint32_t * param ) {
if(param[2] == 0x0005000F) {
uint64_t titleID = _SYSGetSystemApplicationTitleId(SYSTEM_APP_ID_HEALTH_AND_SAFETY);
param[2] = (uint32_t) (titleID >> 32);
param[3] = (uint32_t) (0x00000000FFFFFFFF & titleID);
}
uint32_t result = real_PatchChkStart__3RplFRCQ3_2nn6drmapp8StartArg(param);
DEBUG_FUNCTION_LINE("%08X\n", result);
return result;
}
DECL_FUNCTION(uint32_t, MCP_RightCheckLaunchable, uint32_t * u1, uint32_t * u2, uint32_t u3, uint32_t u4, uint32_t u5) {
if(u3 == 0x0005000F) {
uint64_t titleID = _SYSGetSystemApplicationTitleId(SYSTEM_APP_ID_HEALTH_AND_SAFETY);
u3 = (uint32_t) (titleID >> 32);
u4 = (uint32_t) (0x00000000FFFFFFFF & titleID);
}
uint32_t result = real_MCP_RightCheckLaunchable(u1,u2,u3,u4,u5);
return result;
}
DECL_FUNCTION(int32_t, HBM_NN_ACP_ACPGetTitleMetaXmlByDevice, uint32_t titleid_upper, uint32_t titleid_lower, ACPMetaXml* metaxml, uint32_t device) {
if(gHomebrewLaunched) {
memcpy(metaxml, &gLaunchXML, sizeof(gLaunchXML));
return 0;
}
int result = real_HBM_NN_ACP_ACPGetTitleMetaXmlByDevice(titleid_upper, titleid_lower, metaxml, device);
return result;
}
DECL_FUNCTION(int32_t, EMANUAL_NN_ACP_ACPGetTitleMetaXml, uint32_t titleid_upper, uint32_t titleid_lower, ACPMetaXml* metaxml, uint32_t device) {
DEBUG_FUNCTION_LINE("%08X %08X %08X %08X\n",titleid_upper,titleid_lower,metaxml,device);
WUPS_MUST_REPLACE_PHYSICAL(HBM_NN_ACP_ACPGetTitleMetaXmlByDevice, 0x2E36CE44, 0x0E36CE44);
WUPS_MUST_REPLACE(ACPGetApplicationBox, WUPS_LOADER_LIBRARY_NN_ACP, ACPGetApplicationBox );
WUPS_MUST_REPLACE(PatchChkStart__3RplFRCQ3_2nn6drmapp8StartArg, WUPS_LOADER_LIBRARY_DRMAPP, PatchChkStart__3RplFRCQ3_2nn6drmapp8StartArg );
WUPS_MUST_REPLACE(MCP_RightCheckLaunchable, WUPS_LOADER_LIBRARY_COREINIT, MCP_RightCheckLaunchable );
if(gHomebrewLaunched) {
memcpy(metaxml, &gLaunchXML, sizeof(gLaunchXML));
return 0;
}
int result = real_EMANUAL_NN_ACP_ACPGetTitleMetaXml(titleid_upper, titleid_lower, metaxml, device);
WUPS_MUST_REPLACE(FSReadFile, WUPS_LOADER_LIBRARY_COREINIT, FSReadFile);
WUPS_MUST_REPLACE(FSOpenFile, WUPS_LOADER_LIBRARY_COREINIT, FSOpenFile);
WUPS_MUST_REPLACE(FSCloseFile, WUPS_LOADER_LIBRARY_COREINIT, FSCloseFile);
WUPS_MUST_REPLACE(MCP_TitleList, WUPS_LOADER_LIBRARY_COREINIT, MCP_TitleList);
WUPS_MUST_REPLACE(MCP_GetTitleInfoByTitleAndDevice, WUPS_LOADER_LIBRARY_COREINIT, MCP_GetTitleInfoByTitleAndDevice );
return result;
}
DECL_FUNCTION(int32_t, EMANUAL_NN_ACP_ACPGetTitleMetaDir, uint32_t titleid_upper, uint32_t titleid_lower, ACPMetaXml* metaxml, uint32_t device) {
DEBUG_FUNCTION_LINE("%08X %08X %08X %08X\n",titleid_upper,titleid_lower,metaxml,device);
if(gHomebrewLaunched) {
//memcpy(metaxml, &gLaunchXML, sizeof(gLaunchXML));
//return 0;
}
int result = real_EMANUAL_NN_ACP_ACPGetTitleMetaDir(titleid_upper, titleid_lower, metaxml, device);
return result;
}
WUPS_MUST_REPLACE_PHYSICAL(EMANUAL_NN_ACP_ACPGetTitleMetaDir, 0x4FA0A6F4, 0x0FA0A6F4);
WUPS_MUST_REPLACE_PHYSICAL(EMANUAL_NN_ACP_ACPGetTitleMetaXml, 0x4FA0C280, 0x0FA0C280);
WUPS_MUST_REPLACE_PHYSICAL(HBM_NN_ACP_ACPGetTitleMetaXmlByDevice, 0x2E36CE44, 0x0E36CE44);
WUPS_MUST_REPLACE(FSInit, WUPS_LOADER_LIBRARY_COREINIT, FSInit);
WUPS_MUST_REPLACE(FSBindMount, WUPS_LOADER_LIBRARY_COREINIT, FSBindMount);
WUPS_MUST_REPLACE(FSReadDir, WUPS_LOADER_LIBRARY_COREINIT, FSReadDir);
WUPS_MUST_REPLACE(FSReadFile, WUPS_LOADER_LIBRARY_COREINIT, FSReadFile);
WUPS_MUST_REPLACE(FSOpenFile, WUPS_LOADER_LIBRARY_COREINIT, FSOpenFile);
WUPS_MUST_REPLACE(MCP_TitleList, WUPS_LOADER_LIBRARY_COREINIT, MCP_TitleList);
WUPS_MUST_REPLACE(MCP_GetTitleInfoByTitleAndDevice, WUPS_LOADER_LIBRARY_COREINIT, MCP_GetTitleInfoByTitleAndDevice );
WUPS_MUST_REPLACE(ACPCheckTitleLaunchByTitleListTypeEx, WUPS_LOADER_LIBRARY_NN_ACP, ACPCheckTitleLaunchByTitleListTypeEx );
WUPS_MUST_REPLACE(ACPGetTitleMetaXmlByDevice, WUPS_LOADER_LIBRARY_NN_ACP, ACPGetTitleMetaXmlByDevice );
WUPS_MUST_REPLACE(ACPGetLaunchMetaXml, WUPS_LOADER_LIBRARY_NN_ACP, ACPGetLaunchMetaXml );
WUPS_MUST_REPLACE(ACPGetTitleMetaDirByDevice, WUPS_LOADER_LIBRARY_NN_ACP, ACPGetTitleMetaDirByDevice );
WUPS_MUST_REPLACE(_SYSLaunchTitleByPathFromLauncher, WUPS_LOADER_LIBRARY_SYSAPP, _SYSLaunchTitleByPathFromLauncher);
WUPS_MUST_REPLACE(ACPCheckTitleLaunchByTitleListTypeEx, WUPS_LOADER_LIBRARY_NN_ACP, ACPCheckTitleLaunchByTitleListTypeEx );
WUPS_MUST_REPLACE(ACPGetTitleMetaXmlByDevice, WUPS_LOADER_LIBRARY_NN_ACP, ACPGetTitleMetaXmlByDevice );
WUPS_MUST_REPLACE(ACPGetLaunchMetaXml, WUPS_LOADER_LIBRARY_NN_ACP, ACPGetLaunchMetaXml );
WUPS_MUST_REPLACE(ACPGetTitleMetaDirByDevice, WUPS_LOADER_LIBRARY_NN_ACP, ACPGetTitleMetaDirByDevice );
WUPS_MUST_REPLACE(_SYSLaunchTitleByPathFromLauncher, WUPS_LOADER_LIBRARY_SYSAPP, _SYSLaunchTitleByPathFromLauncher);

702
src/romfs_dev.c Normal file
View File

@ -0,0 +1,702 @@
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <sys/dirent.h>
#include <sys/iosupport.h>
#include <sys/param.h>
#include <unistd.h>
#include <stdbool.h>
#include "romfs_dev.h"
typedef enum {
RomfsSource_FileDescriptor,
} RomfsSource;
typedef struct romfs_mount {
devoptab_t device;
bool setup;
RomfsSource fd_type;
int32_t id;
int32_t fd;
time_t mtime;
uint64_t offset;
romfs_header header;
romfs_dir *cwd;
uint32_t *dirHashTable, *fileHashTable;
void *dirTable, *fileTable;
char name[32];
} romfs_mount;
extern int __system_argc;
extern char** __system_argv;
//static char __thread __component[PATH_MAX+1];
static char __component[PATH_MAX+1];
#define romFS_root(m) ((romfs_dir*)(m)->dirTable)
#define romFS_dir(m,x) ((romfs_dir*) ((uint8_t*)(m)->dirTable + (x)))
#define romFS_file(m,x) ((romfs_file*)((uint8_t*)(m)->fileTable + (x)))
#define romFS_none ((uint32_t)~0)
#define romFS_dir_mode (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH)
#define romFS_file_mode (S_IFREG | S_IRUSR | S_IRGRP | S_IROTH)
uint64_t swapLong(uint64_t X) {
uint64_t x = (uint64_t) X;
x = (x & 0x00000000FFFFFFFF) << 32 | (x & 0xFFFFFFFF00000000) >> 32;
x = (x & 0x0000FFFF0000FFFF) << 16 | (x & 0xFFFF0000FFFF0000) >> 16;
x = (x & 0x00FF00FF00FF00FF) << 8 | (x & 0xFF00FF00FF00FF00) >> 8;
return x;
}
#define REVERSE_SHORT(n) ((unsigned short) (((n & 0xFF) << 8) | \
((n & 0xFF00) >> 8)))
#define REVERSE_INT(n) ((unsigned int) (((n & 0xFF) << 24) | \
((n & 0xFF00) << 8) | \
((n & 0xFF0000) >> 8) | \
((n & 0xFF000000) >> 24)))
static ssize_t _romfs_read(romfs_mount *mount, uint64_t offset, void* buffer, uint64_t size) {
uint64_t pos = mount->offset + offset;
size_t _read = 0;
if(mount->fd_type == RomfsSource_FileDescriptor) {
off_t seek_offset = lseek(mount->fd, pos, SEEK_SET);
if(pos != seek_offset) {
return -1;
}
_read = read(mount->fd, buffer, size);
}
return _read;
}
static bool _romfs_read_chk(romfs_mount *mount, uint64_t offset, void* buffer, uint64_t size) {
return _romfs_read(mount, offset, buffer, size) == size;
}
//-----------------------------------------------------------------------------
static int romfs_open(struct _reent *r, void *fileStruct, const char *path, int flags, int mode);
static int romfs_close(struct _reent *r, void *fd);
static ssize_t romfs_read(struct _reent *r, void *fd, char *ptr, size_t len);
static off_t romfs_seek(struct _reent *r, void *fd, off_t pos, int dir);
static int romfs_fstat(struct _reent *r, void *fd, struct stat *st);
static int romfs_stat(struct _reent *r, const char *path, struct stat *st);
static int romfs_chdir(struct _reent *r, const char *path);
static DIR_ITER* romfs_diropen(struct _reent *r, DIR_ITER *dirState, const char *path);
static int romfs_dirreset(struct _reent *r, DIR_ITER *dirState);
static int romfs_dirnext(struct _reent *r, DIR_ITER *dirState, char *filename, struct stat *filestat);
static int romfs_dirclose(struct _reent *r, DIR_ITER *dirState);
typedef struct {
romfs_mount *mount;
romfs_file *file;
uint64_t offset, pos;
} romfs_fileobj;
typedef struct {
romfs_mount *mount;
romfs_dir* dir;
uint32_t state;
uint32_t childDir;
uint32_t childFile;
} romfs_diriter;
static devoptab_t romFS_devoptab = {
.structSize = sizeof(romfs_fileobj),
.open_r = romfs_open,
.close_r = romfs_close,
.read_r = romfs_read,
.seek_r = romfs_seek,
.fstat_r = romfs_fstat,
.stat_r = romfs_stat,
.chdir_r = romfs_chdir,
.dirStateSize = sizeof(romfs_diriter),
.diropen_r = romfs_diropen,
.dirreset_r = romfs_dirreset,
.dirnext_r = romfs_dirnext,
.dirclose_r = romfs_dirclose,
};
static bool romfs_initialised = false;
static romfs_mount romfs_mounts[32];
//-----------------------------------------------------------------------------
static int32_t romfsMountCommon(const char *name, romfs_mount *mount);
static void romfsInitMtime(romfs_mount *mount);
static void _romfsResetMount(romfs_mount *mount, int32_t id) {
memset(mount, 0, sizeof(*mount));
memcpy(&mount->device, &romFS_devoptab, sizeof(romFS_devoptab));
mount->device.name = mount->name;
mount->device.deviceData = mount;
mount->id = id;
}
static void _romfsInit(void) {
uint32_t i;
uint32_t total = sizeof(romfs_mounts) / sizeof(romfs_mount);
if(!romfs_initialised) {
for(i = 0; i < total; i++) {
_romfsResetMount(&romfs_mounts[i], i);
}
romfs_initialised = true;
}
}
static romfs_mount *romfsFindMount(const char *name) {
uint32_t i;
uint32_t total = sizeof(romfs_mounts) / sizeof(romfs_mount);
romfs_mount *mount = NULL;
_romfsInit();
for(i=0; i<total; i++) {
mount = &romfs_mounts[i];
if(name==NULL) { //Find an unused mount entry.
if(!mount->setup)
return mount;
} else if(mount->setup) { //Find the mount with the input name.
if(strncmp(mount->name, name, sizeof(mount->name))==0)
return mount;
}
}
return NULL;
}
__attribute__((weak)) const char* __romfs_path = NULL;
static romfs_mount* romfs_alloc(void) {
return romfsFindMount(NULL);
}
static void romfs_free(romfs_mount *mount) {
free(mount->fileTable);
free(mount->fileHashTable);
free(mount->dirTable);
free(mount->dirHashTable);
_romfsResetMount(mount, mount->id);
}
static void romfs_mountclose(romfs_mount *mount) {
if(mount->fd_type == RomfsSource_FileDescriptor) {
close(mount->fd);
}
romfs_free(mount);
}
int32_t romfsMount(const char *name, const char * filepath) {
romfs_mount *mount = romfs_alloc();
if(mount == NULL)
return 99;
// Regular RomFS
mount->fd_type = RomfsSource_FileDescriptor;
mount->fd = open(filepath, 0);
if (mount->fd == -1) {
romfs_free(mount);
return -1;
}
romfsInitMtime(mount);
return romfsMountCommon(name, mount);
_fail0:
romfs_mountclose(mount);
return 10;
}
int32_t romfsMountCommon(const char *name, romfs_mount *mount) {
memset(mount->name, 0, sizeof(mount->name));
strncpy(mount->name, name, sizeof(mount->name)-1);
if (_romfs_read(mount, 0, &mount->header, sizeof(mount->header)) != sizeof(mount->header))
goto fail;
mount->dirHashTable = (uint32_t*)malloc(swapLong(mount->header.dirHashTableSize));
if (!mount->dirHashTable)
goto fail;
if (!_romfs_read_chk(mount, swapLong(mount->header.dirHashTableOff), mount->dirHashTable, swapLong(mount->header.dirHashTableSize)))
goto fail;
mount->dirTable = malloc(swapLong(mount->header.dirTableSize));
if (!mount->dirTable)
goto fail;
if (!_romfs_read_chk(mount, swapLong(mount->header.dirTableOff), mount->dirTable, swapLong(mount->header.dirTableSize)))
goto fail;
mount->fileHashTable = (uint32_t*)malloc(swapLong(mount->header.fileHashTableSize));
if (!mount->fileHashTable)
goto fail;
if (!_romfs_read_chk(mount, swapLong(mount->header.fileHashTableOff), mount->fileHashTable, swapLong(mount->header.fileHashTableSize)))
goto fail;
mount->fileTable = malloc(swapLong(mount->header.fileTableSize));
if (!mount->fileTable)
goto fail;
if (!_romfs_read_chk(mount, swapLong(mount->header.fileTableOff), mount->fileTable, swapLong(mount->header.fileTableSize)))
goto fail;
mount->cwd = romFS_root(mount);
// add device if this is the first one
if(AddDevice(&mount->device) < 0)
goto fail;
mount->setup = true;
return 0;
fail:
romfs_mountclose(mount);
return 10;
}
static void romfsInitMtime(romfs_mount *mount) {
mount->mtime = time(NULL);
}
int32_t romfsUnmount(const char *name) {
romfs_mount *mount;
char tmpname[34];
mount = romfsFindMount(name);
if (mount == NULL)
return -1;
// Remove device
memset(tmpname, 0, sizeof(tmpname));
strncpy(tmpname, mount->name, sizeof(tmpname)-2);
strncat(tmpname, ":", sizeof(tmpname)-strlen(tmpname)-1);
RemoveDevice(tmpname);
romfs_mountclose(mount);
return 0;
}
//-----------------------------------------------------------------------------
static uint32_t calcHash(uint32_t parent, const uint8_t* name, uint32_t namelen, uint32_t total) {
uint32_t hash = parent ^ 123456789;
uint32_t i;
for (i = 0; i < namelen; i ++) {
hash = (hash >> 5) | (hash << 27);
hash ^= name[i];
}
return hash % total;
}
static romfs_dir* searchForDir(romfs_mount *mount, romfs_dir* parent, const uint8_t* name, uint32_t namelen) {
uint64_t parentOff = (uintptr_t)parent - (uintptr_t)mount->dirTable;
uint32_t hash = calcHash(parentOff, name, namelen, swapLong(mount->header.dirHashTableSize)/4);
romfs_dir* curDir = NULL;
uint32_t curOff;
for (curOff = REVERSE_INT(mount->dirHashTable[hash]); curOff != romFS_none; curOff = REVERSE_INT(curDir->nextHash)) {
curDir = romFS_dir(mount, curOff);
if (REVERSE_INT(curDir->parent) != parentOff)
continue;
if (REVERSE_INT(curDir->nameLen) != namelen)
continue;
if (memcmp(curDir->name, name, namelen) != 0)
continue;
return curDir;
}
return NULL;
}
static romfs_file* searchForFile(romfs_mount *mount, romfs_dir* parent, const uint8_t* name, uint32_t namelen) {
uint64_t parentOff = (uintptr_t)parent - (uintptr_t)mount->dirTable;
uint32_t hash = calcHash(parentOff, name, namelen, swapLong(mount->header.fileHashTableSize)/4);
romfs_file* curFile = NULL;
uint32_t curOff;
for (curOff = REVERSE_INT(mount->fileHashTable[hash]); curOff != romFS_none; curOff = REVERSE_INT(curFile->nextHash)) {
curFile = romFS_file(mount, curOff);
if (REVERSE_INT(curFile->parent) != parentOff)
continue;
if (REVERSE_INT(curFile->nameLen) != namelen)
continue;
if (memcmp(curFile->name, name, namelen) != 0)
continue;
return curFile;
}
return NULL;
}
static int navigateToDir(romfs_mount *mount, romfs_dir** ppDir, const char** pPath, bool isDir) {
char* colonPos = strchr(*pPath, ':');
if (colonPos)
*pPath = colonPos+1;
if (!**pPath)
return EILSEQ;
*ppDir = mount->cwd;
if (**pPath == '/') {
*ppDir = romFS_root(mount);
(*pPath)++;
}
while (**pPath) {
char* slashPos = strchr(*pPath, '/');
char* component = __component;
if (slashPos) {
uint32_t len = slashPos - *pPath;
if (!len)
return EILSEQ;
if (len > PATH_MAX)
return ENAMETOOLONG;
memcpy(component, *pPath, len);
component[len] = 0;
*pPath = slashPos+1;
} else if (isDir) {
component = (char*)*pPath;
*pPath += strlen(component);
} else
return 0;
if (component[0]=='.') {
if (!component[1])
continue;
if (component[1]=='.' && !component[2]) {
*ppDir = romFS_dir(mount, REVERSE_INT((*ppDir)->parent));
continue;
}
}
*ppDir = searchForDir(mount, *ppDir, (uint8_t*)component, strlen(component));
if (!*ppDir)
return EEXIST;
}
if (!isDir && !**pPath)
return EILSEQ;
return 0;
}
static ino_t dir_inode(romfs_mount *mount, romfs_dir *dir) {
return (uint32_t*)dir - (uint32_t*)mount->dirTable;
}
static off_t dir_size(romfs_dir *dir) {
return sizeof(romfs_dir) + (REVERSE_INT(dir->nameLen)+3)/4;
}
static nlink_t dir_nlink(romfs_mount *mount, romfs_dir *dir) {
nlink_t count = 2; // one for self, one for parent
uint32_t offset = REVERSE_INT(dir->childDir);
while(offset != romFS_none) {
romfs_dir *tmp = romFS_dir(mount, offset);
++count;
offset = REVERSE_INT(tmp->sibling);
}
offset = REVERSE_INT(dir->childFile);
while(offset != romFS_none) {
romfs_file *tmp = romFS_file(mount, offset);
++count;
offset = REVERSE_INT(tmp->sibling);
}
return count;
}
static ino_t file_inode(romfs_mount *mount, romfs_file *file) {
return ((uint32_t*)file - (uint32_t*)mount->fileTable) + swapLong(mount->header.dirTableSize)/4;
}
int romfs_GetFileInfoPerPath(const char* romfs, const char *path, romfs_fileInfo* out) {
if(out == NULL){
return -1;
}
romfs_mount* mount = (romfs_mount*)romfsFindMount(romfs);
if(mount == NULL){
return -2;
}
romfs_dir* curDir = NULL;
int errno2 = navigateToDir(mount, &curDir, &path, false);
if (errno2 != 0){
return -3;
}
romfs_file* file = searchForFile(mount, curDir, (uint8_t*)path, strlen(path));
if (!file) {
return -4;
}
out->length = swapLong(file->dataSize);
out->offset = swapLong(mount->header.fileDataOff) + swapLong(file->dataOff);
return 0;
}
//-----------------------------------------------------------------------------
int romfs_open(struct _reent *r, void *fileStruct, const char *path, int flags, int mode) {
romfs_fileobj* fileobj = (romfs_fileobj*)fileStruct;
fileobj->mount = (romfs_mount*)r->deviceData;
if ((flags & O_ACCMODE) != O_RDONLY) {
r->_errno = EROFS;
return -1;
}
romfs_dir* curDir = NULL;
r->_errno = navigateToDir(fileobj->mount, &curDir, &path, false);
if (r->_errno != 0)
return -1;
romfs_file* file = searchForFile(fileobj->mount, curDir, (uint8_t*)path, strlen(path));
if (!file) {
if(flags & O_CREAT)
r->_errno = EROFS;
else
r->_errno = ENOENT;
return -1;
} else if((flags & O_CREAT) && (flags & O_EXCL)) {
r->_errno = EEXIST;
return -1;
}
fileobj->file = file;
fileobj->offset = swapLong(fileobj->mount->header.fileDataOff) + swapLong(file->dataOff);
fileobj->pos = 0;
return 0;
}
int romfs_close(struct _reent *r, void *fd) {
return 0;
}
ssize_t romfs_read(struct _reent *r, void *fd, char *ptr, size_t len) {
romfs_fileobj* file = (romfs_fileobj*)fd;
uint64_t endPos = file->pos + len;
/* check if past end-of-file */
if(file->pos >= swapLong(file->file->dataSize))
return 0;
/* truncate the read to end-of-file */
if(endPos > swapLong(file->file->dataSize))
endPos = swapLong(file->file->dataSize);
len = endPos - file->pos;
ssize_t adv = _romfs_read(file->mount, file->offset + file->pos, ptr, len);
if(adv >= 0) {
file->pos += adv;
return adv;
}
r->_errno = EIO;
return -1;
}
off_t romfs_seek(struct _reent *r, void *fd, off_t pos, int dir) {
romfs_fileobj* file = (romfs_fileobj*)fd;
off_t start;
switch (dir) {
case SEEK_SET:
start = 0;
break;
case SEEK_CUR:
start = file->pos;
break;
case SEEK_END:
start = swapLong(file->file->dataSize);
break;
default:
r->_errno = EINVAL;
return -1;
}
/* don't allow negative position */
if(pos < 0) {
if(start + pos < 0) {
r->_errno = EINVAL;
return -1;
}
}
/* check for overflow */
else if(INT64_MAX - pos < start) {
r->_errno = EOVERFLOW;
return -1;
}
file->pos = start + pos;
return file->pos;
}
int romfs_fstat(struct _reent *r, void *fd, struct stat *st) {
romfs_fileobj* file = (romfs_fileobj*)fd;
memset(st, 0, sizeof(struct stat));
st->st_ino = file_inode(file->mount, file->file);
st->st_mode = romFS_file_mode;
st->st_nlink = 1;
st->st_size = (off_t)swapLong(file->file->dataSize);
st->st_blksize = 512;
st->st_blocks = (st->st_blksize + 511) / 512;
st->st_atime = st->st_mtime = st->st_ctime = file->mount->mtime;
return 0;
}
int romfs_stat(struct _reent *r, const char *path, struct stat *st) {
romfs_mount* mount = (romfs_mount*)r->deviceData;
romfs_dir* curDir = NULL;
r->_errno = navigateToDir(mount, &curDir, &path, false);
if(r->_errno != 0)
return -1;
romfs_dir* dir = searchForDir(mount, curDir, (uint8_t*)path, strlen(path));
if(dir) {
memset(st, 0, sizeof(*st));
st->st_ino = dir_inode(mount, dir);
st->st_mode = romFS_dir_mode;
st->st_nlink = dir_nlink(mount, dir);
st->st_size = dir_size(dir);
st->st_blksize = 512;
st->st_blocks = (st->st_blksize + 511) / 512;
st->st_atime = st->st_mtime = st->st_ctime = mount->mtime;
return 0;
}
romfs_file* file = searchForFile(mount, curDir, (uint8_t*)path, strlen(path));
if(file) {
memset(st, 0, sizeof(*st));
st->st_ino = file_inode(mount, file);
st->st_mode = romFS_file_mode;
st->st_nlink = 1;
st->st_size = swapLong(file->dataSize);
st->st_blksize = 512;
st->st_blocks = (st->st_blksize + 511) / 512;
st->st_atime = st->st_mtime = st->st_ctime = mount->mtime;
return 0;
}
r->_errno = ENOENT;
return 1;
}
int romfs_chdir(struct _reent *r, const char *path) {
romfs_mount* mount = (romfs_mount*)r->deviceData;
romfs_dir* curDir = NULL;
r->_errno = navigateToDir(mount, &curDir, &path, false);
if (r->_errno != 0)
return -1;
mount->cwd = curDir;
return 0;
}
DIR_ITER* romfs_diropen(struct _reent *r, DIR_ITER *dirState, const char *path) {
romfs_diriter* iter = (romfs_diriter*)(dirState->dirStruct);
romfs_dir* curDir = NULL;
iter->mount = (romfs_mount*)r->deviceData;
r->_errno = navigateToDir(iter->mount, &curDir, &path, true);
if(r->_errno != 0)
return NULL;
iter->dir = curDir;
iter->state = 0;
iter->childDir = REVERSE_INT(curDir->childDir);
iter->childFile = REVERSE_INT(curDir->childFile);
return dirState;
}
int romfs_dirreset(struct _reent *r, DIR_ITER *dirState) {
romfs_diriter* iter = (romfs_diriter*)(dirState->dirStruct);
iter->state = 0;
iter->childDir = REVERSE_INT(iter->dir->childDir);
iter->childFile = REVERSE_INT(iter->dir->childFile);
return 0;
}
int romfs_dirnext(struct _reent *r, DIR_ITER *dirState, char *filename, struct stat *filestat) {
romfs_diriter* iter = (romfs_diriter*)(dirState->dirStruct);
if(iter->state == 0) {
/* '.' entry */
memset(filestat, 0, sizeof(*filestat));
filestat->st_ino = dir_inode(iter->mount, iter->dir);
filestat->st_mode = romFS_dir_mode;
strcpy(filename, ".");
iter->state = 1;
return 0;
} else if(iter->state == 1) {
/* '..' entry */
romfs_dir* dir = romFS_dir(iter->mount, REVERSE_INT(iter->dir->parent));
memset(filestat, 0, sizeof(*filestat));
filestat->st_ino = dir_inode(iter->mount, dir);
filestat->st_mode = romFS_dir_mode;
strcpy(filename, "..");
iter->state = 2;
return 0;
}
if(iter->childDir != romFS_none) {
romfs_dir* dir = romFS_dir(iter->mount, iter->childDir);
iter->childDir = REVERSE_INT(dir->sibling);
memset(filestat, 0, sizeof(*filestat));
filestat->st_ino = dir_inode(iter->mount, dir);
filestat->st_mode = romFS_dir_mode;
memset(filename, 0, NAME_MAX);
if(REVERSE_INT(dir->nameLen) >= NAME_MAX) {
r->_errno = ENAMETOOLONG;
return -1;
}
strncpy(filename, (char*)dir->name, REVERSE_INT(dir->nameLen));
return 0;
} else if(iter->childFile != romFS_none) {
romfs_file* file = romFS_file(iter->mount, iter->childFile);
iter->childFile = REVERSE_INT(file->sibling);
memset(filestat, 0, sizeof(*filestat));
filestat->st_ino = file_inode(iter->mount, file);
filestat->st_mode = romFS_file_mode;
memset(filename, 0, NAME_MAX);
if(REVERSE_INT(file->nameLen) >= NAME_MAX) {
r->_errno = ENAMETOOLONG;
return -1;
}
strncpy(filename, (char*)file->name, REVERSE_INT(file->nameLen));
return 0;
}
r->_errno = ENOENT;
return -1;
}
int romfs_dirclose(struct _reent *r, DIR_ITER *dirState) {
return 0;
}

90
src/romfs_dev.h Normal file
View File

@ -0,0 +1,90 @@
/**
* @file romfs_dev.h
* @brief RomFS driver.
* @author yellows8
* @author mtheall
* @author fincs
* @copyright libnx Authors
*/
#pragma once
#include <wut.h>
/// RomFS header.
typedef struct {
uint64_t headerSize; ///< Size of the header.
uint64_t dirHashTableOff; ///< Offset of the directory hash table.
uint64_t dirHashTableSize; ///< Size of the directory hash table.
uint64_t dirTableOff; ///< Offset of the directory table.
uint64_t dirTableSize; ///< Size of the directory table.
uint64_t fileHashTableOff; ///< Offset of the file hash table.
uint64_t fileHashTableSize; ///< Size of the file hash table.
uint64_t fileTableOff; ///< Offset of the file table.
uint64_t fileTableSize; ///< Size of the file table.
uint64_t fileDataOff; ///< Offset of the file data.
} romfs_header;
/// RomFS directory.
typedef struct {
uint32_t parent; ///< Offset of the parent directory.
uint32_t sibling; ///< Offset of the next sibling directory.
uint32_t childDir; ///< Offset of the first child directory.
uint32_t childFile; ///< Offset of the first file.
uint32_t nextHash; ///< Directory hash table pointer.
uint32_t nameLen; ///< Name length.
uint8_t name[]; ///< Name. (UTF-8)
} romfs_dir;
/// RomFS file.
typedef struct {
uint32_t parent; ///< Offset of the parent directory.
uint32_t sibling; ///< Offset of the next sibling file.
uint64_t dataOff; ///< Offset of the file's data.
uint64_t dataSize; ///< Length of the file's data.
uint32_t nextHash; ///< File hash table pointer.
uint32_t nameLen; ///< Name length.
uint8_t name[]; ///< Name. (UTF-8)
} romfs_file;
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Mounts the Application's RomFS.
* @param name Device mount name.
*/
int32_t romfsMount(const char *name, const char * path);
/**
* @brief Mounts RomFS from an open file.
* @param file FsFile of the RomFS image.
* @param offset Offset of the RomFS within the file.
* @param name Device mount name.
bool romfsMountFromFile(FsFile file, uint64_t offset, const char *name);
*/
/*
static inline bool romfsInitFromFile(int32_t fd, uint64_t offset) {
return romfsMountFromFile(fd, offset, "romfs");
}*/
/// Unmounts the RomFS device.
int32_t romfsUnmount(const char *name);
/*
static inline bool romfsExit(void) {
return romfsUnmount("romfs");
}*/
/// RomFS file.
typedef struct {
uint64_t length; ///< Offset of the file's data.
uint64_t offset; ///< Length of the file's data.
} romfs_fileInfo;
int romfs_GetFileInfoPerPath(const char* romfs, const char *path, romfs_fileInfo* out);
#ifdef __cplusplus
}
#endif