Fixed SD CARD mounting and file reading

This commit is contained in:
Maschell 2017-05-20 15:55:51 +02:00
parent cd06b1b44b
commit bd4a10baa6
4 changed files with 132 additions and 78 deletions

View File

@ -22,6 +22,7 @@
#include <map>
#include "wiiu/fs.h"
#include "wiiu/controller_patcher/utils/FSHelper.h"
#define FS_MOUNT_SOURCE_SIZE 0x300
#define FS_MAX_MOUNTPATH_SIZE 12
@ -64,58 +65,37 @@ void ConfigReader::freeFSHandles(){
}
}
// Mounting the sdcard without any external lib to be portable (Currently broken)
s32 ConfigReader::InitSDCard(){
if(HID_DEBUG){ printf("ConfigReader::InitSDCard(line %d): InitSDCard\n",__LINE__); }
void *mountSrc = malloc(FS_MOUNT_SOURCE_SIZE);
if(!mountSrc)
return -3;
char* mountPath = (char*) malloc(FS_MAX_MOUNTPATH_SIZE);
if(!mountPath) {
free(mountSrc);
return -4;
}
memset(mountSrc, 0, FS_MOUNT_SOURCE_SIZE);
memset(mountPath, 0, FS_MAX_MOUNTPATH_SIZE);
freeFSHandles();
int result = -1;
// get command and client
this->pClient = malloc(sizeof(FSClient));
this->pCmd = malloc(sizeof(FSCmdBlock));
s32 status = 0;
if(!pClient || !pCmd) {
// just in case free if not 0
if(pClient)
free(pClient);
if(pCmd)
free(pCmd);
return -2;
}
if (this->pClient && this->pCmd){
FSInit();
if(HID_DEBUG){ printf("ConfigReader::InitSDCard(line %d): FSInit done\n",__LINE__); }
FSInitCmdBlock((FSCmdBlock*)pCmd);
if(HID_DEBUG){ printf("ConfigReader::InitSDCard(line %d): Init CMD Block done\n",__LINE__); }
status = FSAddClientEx((FSClient*)pClient,0, -1);
if(HID_DEBUG){ printf("ConfigReader::InitSDCard(line %d): Added Client, result: %d\n",__LINE__,status); }
if ((status = FSGetMountSource((FSClient*)this->pClient,(FSCmdBlock*)this->pCmd, FS_MOUNT_SOURCE_SD, (FSMountSource *)mountSrc, 0)) == FS_STATUS_OK)
{
if(HID_DEBUG){ printf("ConfigReader::InitSDCard(line %d): \n",__LINE__); }
if ((status = FSMount((FSClient*)this->pClient,(FSCmdBlock*)this->pCmd, (FSMountSource*)mountSrc, mountPath, FS_MAX_MOUNTPATH_SIZE, 0x0400)) == FS_STATUS_OK)
{
if(HID_DEBUG){ printf("ConfigReader::InitSDCard(line %d): \n",__LINE__); }
free(mountSrc);free(mountPath);
return 0;
}else{
printf("ConfigReader::InitSDCard(line %d): error: FSMount failed %d\n",__LINE__,status);
free(mountSrc);free(mountPath);
return status;
FSAddClient((FSClient*)pClient, -1);
char *mountPath = NULL;
if((result = FS_Helper_MountFS(pClient, pCmd, &mountPath)) == 0) {
//free(mountPath);
}
}else{
printf("ConfigReader::InitSDCard(line %d): error: FSGetMountSource failed %d\n",__LINE__,status);
free(mountSrc);free(mountPath);
return status;
}
}
free(mountSrc);free(mountPath);
return -1;
return result;
}
std::vector<std::string> ConfigReader::ScanFolder(){
@ -145,6 +125,7 @@ std::vector<std::string> ConfigReader::ScanFolder(){
}
void ConfigReader::processFileList(std::vector<std::string> path){
for(std::vector<std::string>::iterator it = path.begin(); it != path.end(); ++it) {
printf("ConfigReader::processFileList(line %d): Reading %s\n",__LINE__,it->c_str());
std::string result = loadFileToString(*it);
@ -158,42 +139,20 @@ std::string ConfigReader::loadFileToString(std::string path){
FSFileHandle handle = 0;
s32 status = 0;
std::string strBuffer;
FSStat stats;
if((status = FSGetStat((FSClient*)this->pClient,(FSCmdBlock*)this->pCmd,path.c_str(),&stats,-1)) == FS_STATUS_OK){
uint8_t * file = (uint8_t *) malloc((sizeof(uint8_t)*stats.size)+1);
if(!file){
printf("ConfigReader::loadFileToString(line %d): error: Failed to allocate space for reading the file\n",__LINE__);
return "";
}
file[stats.size] = '\0';
if((status = FSOpenFile((FSClient*)this->pClient,(FSCmdBlock*)this->pCmd,path.c_str(),"r",&handle,-1)) == FS_STATUS_OK){
s32 total_read = 0;
s32 ret2 = 0;
while ((ret2 = FSReadFile((FSClient*)pClient,(FSCmdBlock*)pCmd, file+total_read, 1, stats.size-total_read, handle, 0, -1)) > 0){
total_read += ret2;
}
}else{
printf("ConfigReader::loadFileToString(line %d): error: (FSOpenFile) Couldn't open file (%s), error: %d",__LINE__,path.c_str(),status);
free(file);
file=NULL;
return "";
}
FSCloseFile((FSClient*)this->pClient,(FSCmdBlock*)this->pCmd,handle,-1);
strBuffer = std::string((char *)file);
free(file);
file = NULL;
char * result = NULL;
if(FS_Helper_GetFile(this->pClient,this->pCmd,path.c_str(), &result) == 0){
if(result != NULL){
strBuffer = std::string((char *)result);
free(result);
result = NULL;
//! remove all windows crap signs
strBuffer = CPStringTools::removeCharFromString(strBuffer,'\r');
strBuffer = CPStringTools::removeCharFromString(strBuffer,' ');
strBuffer = CPStringTools::removeCharFromString(strBuffer,'\t');
}else{
printf("ConfigReader::loadFileToString(line %d): error: (GetStat) Couldn't open file (%s), error: %d",__LINE__,path.c_str(),status);
}
}else{
printf("ConfigReader::loadFileToString(line %d): Failed to load %s\n",__LINE__,path.c_str());
}
return strBuffer;
}

View File

@ -512,7 +512,7 @@ bool ControllerPatcher::Init(){
}
//Broken
/*if(gConfig_done != HID_SDCARD_READ){
if(gConfig_done != HID_SDCARD_READ){
printf("ControllerPatcher::Init(line %d): Reading config files from SD Card\n",__LINE__);
ConfigReader* reader = ConfigReader::getInstance();
s32 status = 0;
@ -525,7 +525,7 @@ bool ControllerPatcher::Init(){
printf("ControllerPatcher::Init(line %d): SD mounting failed! %d\n",__LINE__,status);
}
ConfigReader::destroyInstance();
}*/
}
printf("ControllerPatcher::Init(line %d): Initializing the data for button remapping\n",__LINE__);
InitButtonMapping();

78
utils/FSHelper.c Normal file
View File

@ -0,0 +1,78 @@
#include <malloc.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <wiiu/fs.h>
#define FS_MAX_MOUNTPATH_SIZE 128
int FS_Helper_MountFS(void *pClient, void *pCmd, char **mount_path){
int result = -1;
void *mountSrc = malloc(sizeof(FSMountSource));
if(!mountSrc)
return -3;
char* mountPath = (char*) malloc(FS_MAX_MOUNTPATH_SIZE);
if(!mountPath) {
free(mountSrc);
return -4;
}
memset(mountSrc, 0, sizeof(FSMountSource));
memset(mountPath, 0, FS_MAX_MOUNTPATH_SIZE);
// Mount sdcard
if (FSGetMountSource(pClient, pCmd, FS_MOUNT_SOURCE_SD, mountSrc, -1) == 0)
{
result = FSMount(pClient, pCmd, mountSrc, mountPath, FS_MAX_MOUNTPATH_SIZE, -1);
if((result == 0) && mount_path) {
*mount_path = (char*)malloc(strlen(mountPath) + 1);
if(*mount_path)
strcpy(*mount_path, mountPath);
}
}
free(mountPath);
free(mountSrc);
return result;
}
int FS_Helper_GetFile(void * pClient,void * pCmd,const char * path, char *(*result)){
if(pClient == NULL || pCmd == NULL || path == NULL || result == NULL) return -2;
FSStat stats;
s32 status = -1;
s32 handle = 0;
if((status = FSGetStat(pClient,pCmd,path,&stats,-1)) == FS_STATUS_OK){
(*result) = (uint8_t *) memalign(0x40, (sizeof(uint8_t)*stats.size)+1);
if(!(*result)){
printf("FS_Helper_GetFile(line %d): error: Failed to allocate space for reading the file\n",__LINE__);
return -1;
}
(*result)[stats.size] = '\0';
if((status = FSOpenFile(pClient,pCmd,path,"r",&handle,-1)) == FS_STATUS_OK){
s32 total_read = 0;
s32 ret2 = 0;
char * cur_result_pointer = *result;
s32 sizeToRead = stats.size;
while ((ret2 = FSReadFile(pClient,pCmd, cur_result_pointer, 0x01, sizeToRead, handle, 0, -1)) > 0){
total_read += ret2;
cur_result_pointer += ret2;
sizeToRead -= ret2;
}
}else{
printf("FS_Helper_GetFile(line %d): error: (FSOpenFile) Couldn't open file (%s), error: %d",__LINE__,path,status);
free((*result));
(*result)=NULL;
return -1;
}
FSCloseFile(pClient,pCmd,handle,-1);
return 0;
}else{
printf("FS_Helper_GetFile(line %d): error: (GetStat) Couldn't open file (%s), error: %d",__LINE__,path,status);
}
return -1;
}

17
utils/FSHelper.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef __FSHELPER_H_
#define __FSHELPER_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <wiiu/types.h>
int FS_Helper_MountFS(void *pClient, void *pCmd, char **mount_path);
int FS_Helper_GetFile(void * pClient,void * pCmd,const char *, char **result);
#ifdef __cplusplus
}
#endif
#endif // __FSHELPER_H_