-Removed a bunch of unnecessary code, mostly printf's

This commit is contained in:
Joostinonline 2014-02-28 01:44:22 +00:00
parent b1de0ff4aa
commit f7c14cb306
9 changed files with 33 additions and 600 deletions

View File

@ -11,9 +11,7 @@ extern "C" {
//use this just like printf(); //use this just like printf();
void gprintf(const char *str, ...); void gprintf(const char *str, ...);
void gsenddata(const u8 *data, int length, const char *filename); void gsenddata(const u8 *data, int length, const char *filename);
void ghexdump(void *d, int len);
bool InitGecko(); bool InitGecko();
void printfBoth( const char *str, ... );
#else #else
#define gprintf(...) #define gprintf(...)
#define gsenddata(...) #define gsenddata(...)

View File

@ -29,7 +29,6 @@ distribution.
#ifndef _WII_BASICS_H_ #ifndef _WII_BASICS_H_
#define _WII_BASICS_H_ #define _WII_BASICS_H_
// Turn upper and lower into a full title ID // Turn upper and lower into a full title ID
#define TITLE_ID(x,y) (((u64)(x) << 32) | (y)) #define TITLE_ID(x,y) (((u64)(x) << 32) | (y))
// Get upper or lower half of a title ID // Get upper or lower half of a title ID
@ -37,38 +36,7 @@ distribution.
// Turn upper and lower into a full title ID // Turn upper and lower into a full title ID
#define TITLE_LOWER(x) ((u32)(x)) #define TITLE_LOWER(x) ((u32)(x))
// be functions from segher's wii.git // be function from segher's wii.git
u16 be16(const u8 *p);
u32 be32(const u8 *p);
u64 be64(const u8 *p); u64 be64(const u8 *p);
u64 be34(const u8 *p);
// Do basic Wii init: Video, console, WPAD
void basicInit(void);
// Do our custom init: Identify and initialized ISFS driver
void miscInit(void);
void IdentSysMenu(void);
// Clean up after ourselves (Deinit ISFS)
void miscDeInit(void);
// Scan the pads and return buttons
u32 getButtons(void);
u32 wait_anyKey(void);
u32 wait_key(u32 button);
void hex_print_array16(const u8 *array, u32 size);
/* Reads a file from ISFS to an array in memory */
s32 ISFS_ReadFileToArray (const char *filepath, u8 *filearray, u32 max_size, u32 *file_size);
/* Writes from an array in memory to a file with ISFS */
s32 ISFS_WriteFileFromArray (const char *filepath, const u8 *filearray, u32 array_size, u32 ownerID, u16 groupID, u8 attr, u8 own_perm, u8 group_perm, u8 other_perm);
bool yes_or_no(void);
#endif #endif

View File

@ -24,13 +24,7 @@ void gprintf( const char *str, ... )
va_end(ap); va_end(ap);
usb_sendbuffer_safe( 1, astr, strlen(astr) ); usb_sendbuffer_safe( 1, astr, strlen(astr) );
} }
void printfBoth( const char *str, ... )
{
gprintf(str);
printf(str);
}
void gsenddata(const u8 *data, int length, const char *filename) void gsenddata(const u8 *data, int length, const char *filename)
{ {
@ -59,29 +53,6 @@ char ascii(char s) {
return s; return s;
} }
void ghexdump(void *d, int len) {
u8 *data;
int i, off;
data = (u8*)d;
gprintf("\n 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF");
gprintf("\n==== =============================================== ================\n");
for (off=0; off<len; off += 16) {
gprintf("%04x ",off);
for(i=0; i<16; i++)
if((i+off)>=len) gprintf(" ");
else gprintf("%02x ",data[off+i]);
gprintf(" ");
for(i=0; i<16; i++)
if((i+off)>=len) gprintf(" ");
else gprintf("%c",ascii(data[off+i]));
gprintf("\n");
}
}
bool InitGecko() bool InitGecko()
{ {
u32 geckoattached = usb_isgeckoalive(EXI_CHANNEL_1); u32 geckoattached = usb_isgeckoalive(EXI_CHANNEL_1);

View File

@ -49,21 +49,16 @@ s32 tcp_socket (void) {
s32 s, res; s32 s, res;
s = net_socket (PF_INET, SOCK_STREAM, 0); s = net_socket (PF_INET, SOCK_STREAM, 0);
if (s < 0) { if (s < 0) return s;
printf ("net_socket failed: %d\n", s);
return s;
}
res = net_fcntl (s, F_GETFL, 0); res = net_fcntl (s, F_GETFL, 0);
if (res < 0) { if (res < 0) {
printf ("F_GETFL failed: %d\n", res);
net_close (s); net_close (s);
return res; return res;
} }
res = net_fcntl (s, F_SETFL, res | 4); res = net_fcntl (s, F_SETFL, res | 4);
if (res < 0) { if (res < 0) {
printf ("F_SETFL failed: %d\n", res);
net_close (s); net_close (s);
return res; return res;
} }
@ -78,10 +73,7 @@ s32 tcp_connect (char *host, const u16 port) {
s64 t; s64 t;
hp = net_gethostbyname (host); hp = net_gethostbyname (host);
if (!hp || !(hp->h_addrtype == PF_INET)) { if (!hp || !(hp->h_addrtype == PF_INET)) return errno;
printf ("net_gethostbyname failed: %d\n", errno);
return errno;
}
s = tcp_socket (); s = tcp_socket ();
if (s < 0) if (s < 0)
@ -95,11 +87,8 @@ s32 tcp_connect (char *host, const u16 port) {
t = gettime (); t = gettime ();
while (true) { while (true) {
if (ticks_to_millisecs (diff_ticks (t, gettime ())) > if (ticks_to_millisecs (diff_ticks (t, gettime ())) > TCP_CONNECT_TIMEOUT) {
TCP_CONNECT_TIMEOUT) { net_close(s);
printf ("tcp_connect timeout\n");
net_close (s);
return -ETIMEDOUT; return -ETIMEDOUT;
} }
@ -115,9 +104,7 @@ s32 tcp_connect (char *host, const u16 port) {
continue; continue;
} }
net_close(s);
printf ("net_connect failed: %d\n", res);
net_close (s);
return res; return res;
} }
@ -150,11 +137,7 @@ char * tcp_readln (const s32 s, const u16 max_length, const u64 start_time, cons
continue; continue;
} }
if (res < 0) { if (res < 0) break;
printf ("tcp_readln failed: %d\n", res);
break;
}
if ((c > 0) && (buf[c - 1] == '\r') && (buf[c] == '\n')) { if ((c > 0) && (buf[c - 1] == '\r') && (buf[c] == '\n')) {
if (c == 1) { if (c == 1) {
@ -191,12 +174,7 @@ bool tcp_read (const s32 s, u8 **buffer, const u32 length) {
t = gettime (); t = gettime ();
while (left) { while (left) {
if (ticks_to_millisecs (diff_ticks (t, gettime ())) > if (ticks_to_millisecs (diff_ticks (t, gettime ())) > TCP_BLOCK_RECV_TIMEOUT) break;
TCP_BLOCK_RECV_TIMEOUT) {
printf ("tcp_read timeout\n");
break;
}
block = left; block = left;
if (block > 2048) if (block > 2048)
@ -210,11 +188,7 @@ bool tcp_read (const s32 s, u8 **buffer, const u32 length) {
continue; continue;
} }
if (res < 0) { if (res < 0) break;
printf ("net_read failed: %d\n", res);
break;
}
received += res; received += res;
left -= res; left -= res;

View File

@ -508,7 +508,6 @@ int wanin_mload_get_IOS_base()
// if(mload_init()<0) return -1; // if(mload_init()<0) return -1;
ret= IOS_IoctlvFormat(hid, mload_fd, MLOAD_GET_IOS_BASE, ":d", &ios, sizeof(ios)); ret= IOS_IoctlvFormat(hid, mload_fd, MLOAD_GET_IOS_BASE, ":d", &ios, sizeof(ios));
//printf("get_ios_base: %d %x\n", ret, ios.dipVersion);
if (ret == 0) { if (ret == 0) {
switch(ios.dipVersion) { switch(ios.dipVersion) {
case 0x48776F72: /* DIP: 07/11/08 14:34:26 */ case 0x48776F72: /* DIP: 07/11/08 14:34:26 */

View File

@ -166,7 +166,7 @@ s32 read_file_from_nand(char *filepath, u8 **buffer, u32 *filesize)
return 0; return 0;
} }
void zero_sig(signed_blob *sig) inline void zero_sig(signed_blob *sig)
{ {
u8 *sig_ptr = (u8 *)sig; u8 *sig_ptr = (u8 *)sig;
memset(sig_ptr + 4, 0, SIGNATURE_SIZE(sig)-4); memset(sig_ptr + 4, 0, SIGNATURE_SIZE(sig)-4);
@ -232,7 +232,7 @@ inline s32 RemoveBogusTMD(void)
// Check fake signatures (aka Trucha Bug) // Check fake signatures (aka Trucha Bug)
bool CheckFakeSignature(void) inline bool CheckFakeSignature(void)
{ {
s32 ret = ES_AddTicket((signed_blob *)ticket_dat, ticket_dat_size, (signed_blob *)certs, sizeof(certs), 0, 0); s32 ret = ES_AddTicket((signed_blob *)ticket_dat, ticket_dat_size, (signed_blob *)certs, sizeof(certs), 0, 0);
@ -242,7 +242,7 @@ bool CheckFakeSignature(void)
// Check if you can still call ES_DiVerify (aka ES_Identify) to make IOS think that you actually are a different title // Check if you can still call ES_DiVerify (aka ES_Identify) to make IOS think that you actually are a different title
bool CheckESIdentify(void) inline bool CheckESIdentify(void)
{ {
int ret = ES_Identify((signed_blob *)certs, sizeof(certs), (signed_blob *)tmd_dat, tmd_dat_size, (signed_blob *)ticket_dat, ticket_dat_size, NULL); int ret = ES_Identify((signed_blob *)certs, sizeof(certs), (signed_blob *)tmd_dat, tmd_dat_size, (signed_blob *)ticket_dat, ticket_dat_size, NULL);
return ((ret >= 0) || (ret == -2011)); return ((ret >= 0) || (ret == -2011));
@ -250,14 +250,14 @@ bool CheckESIdentify(void)
// Check flash access // Check flash access
bool CheckFlashAccess(void) inline bool CheckFlashAccess(void)
{ {
s32 ret = IOS_Open("/dev/flash", 1); s32 ret = IOS_Open("/dev/flash", 1);
if (ret >= 0) IOS_Close(ret); if (ret >= 0) IOS_Close(ret);
return (ret >= 0); return (ret >= 0);
} }
bool CheckMload(void) inline bool CheckMload(void)
{ {
int ret = IOS_Open("/dev/mload", 0); int ret = IOS_Open("/dev/mload", 0);
if (ret >= 0) IOS_Close(ret); if (ret >= 0) IOS_Close(ret);
@ -266,7 +266,7 @@ bool CheckMload(void)
// Check NAND access // Check NAND access
bool CheckNANDAccess(void) inline bool CheckNANDAccess(void)
{ {
int ret = IOS_Open("/ticket/00000001/00000002.tik", 1); int ret = IOS_Open("/ticket/00000001/00000002.tik", 1);
if (ret >= 0) IOS_Close(ret); if (ret >= 0) IOS_Close(ret);
@ -275,7 +275,7 @@ bool CheckNANDAccess(void)
// Check boot2 access // Check boot2 access
bool CheckBoot2Access(void) inline bool CheckBoot2Access(void)
{ {
int ret = IOS_Open("/dev/boot2", 1); int ret = IOS_Open("/dev/boot2", 1);
if (ret >= 0) IOS_Close(ret); if (ret >= 0) IOS_Close(ret);

View File

@ -55,39 +55,20 @@ int get_title_ios(u64 title) {
// This is a workaround added since some IOS (like 21) don't like our // This is a workaround added since some IOS (like 21) don't like our
// call to ES_GetStoredTMDSize // call to ES_GetStoredTMDSize
//printf("Error! ES_GetStoredTMDSize: %d\n", ret);
sprintf(filepath, "/title/%08x/%08x/content/title.tmd", TITLE_UPPER(title), TITLE_LOWER(title)); sprintf(filepath, "/title/%08x/%08x/content/title.tmd", TITLE_UPPER(title), TITLE_LOWER(title));
ret = ISFS_Open(filepath, ISFS_OPEN_READ); ret = ISFS_Open(filepath, ISFS_OPEN_READ);
if (ret <= 0) if (ret <= 0) return 0;
{
printf("Error! ISFS_Open (ret = %d)\n", ret);
return 0;
}
fd = ret; fd = ret;
ret = ISFS_Seek(fd, 0x184, 0); ret = ISFS_Seek(fd, 0x184, 0);
if (ret < 0)
{
printf("Error! ISFS_Seek (ret = %d)\n", ret);
return 0;
}
ret = ISFS_Read(fd,tmd_buf,8); ret = ISFS_Read(fd,tmd_buf,8);
if (ret < 0) if (ret < 0) return 0;
{
printf("Error! ISFS_Read (ret = %d)\n", ret);
return 0;
}
ret = ISFS_Close(fd); ret = ISFS_Close(fd);
if (ret < 0) if (ret < 0) return 0;
{
printf("Error! ISFS_Close (ret = %d)\n", ret);
return 0;
}
return be64(tmd_buf); return be64(tmd_buf);
@ -97,10 +78,8 @@ int get_title_ios(u64 title) {
// Some of this code adapted from bushing's title_lister.c // Some of this code adapted from bushing's title_lister.c
signed_blob *s_tmd = (signed_blob *)tmd_buf; signed_blob *s_tmd = (signed_blob *)tmd_buf;
ret = ES_GetStoredTMD(title, s_tmd, tmd_size); ret = ES_GetStoredTMD(title, s_tmd, tmd_size);
if (ret < 0){ if (ret < 0) return -1;
printf("Error! ES_GetStoredTMD: %d\n", ret);
return -1;
}
tmd *t = SIGNATURE_PAYLOAD(s_tmd); tmd *t = SIGNATURE_PAYLOAD(s_tmd);
return t->sys_version; return t->sys_version;
} }
@ -196,12 +175,13 @@ void formatDate(u32 date, char ReportBuffer[200][100]) {
sprintf(ReportBuffer[DVD], TXT_NoDVD); sprintf(ReportBuffer[DVD], TXT_NoDVD);
} }
void sort(u64 *titles, u32 cnt) { inline void sort(u64 *titles, u32 cnt) {
int i, j; int i, j;
u64 tmp;
for (i = 0; i < cnt -1; ++i) { for (i = 0; i < cnt -1; ++i) {
for (j = 0; j < cnt - i - 1; ++j) { for (j = 0; j < cnt - i - 1; ++j) {
if (titles[j] > titles[j + 1]) { if (titles[j] > titles[j + 1]) {
u64 tmp = titles[j]; tmp = titles[j];
titles[j] = titles[j + 1]; titles[j] = titles[j + 1];
titles[j + 1] = tmp; titles[j + 1] = tmp;
} }
@ -518,11 +498,7 @@ int main(int argc, char **argv)
ios[i].isStub = true; ios[i].isStub = true;
else else
{ {
if (ios[i].titleID != 256 && ios[i].titleID != 257 && ios[i].titleID != 512 && ios[i].titleID != 513 && titleSize < 0x100000) ios[i].isStub = (ios[i].titleID != 256 && ios[i].titleID != 257 && ios[i].titleID != 512 && ios[i].titleID != 513 && titleSize < 0x100000);
ios[i].isStub = true;
else
ios[i].isStub = false;
if (ios[i].isStub) { if (ios[i].isStub) {
gprintf("is stub\n"); gprintf("is stub\n");
logfile("is stub\r\n"); logfile("is stub\r\n");
@ -707,8 +683,7 @@ int main(int argc, char **argv)
printSuccess(MSG_UpdateSuccess); printSuccess(MSG_UpdateSuccess);
sleep(10); sleep(10);
deinitGUI(); deinitGUI();
if (*(u32*)0x80001800) exit(0); exit(0);
SYS_ResetSystem(SYS_RETURNTOMENU, 0, 0);
} else if (ret < 0) { } else if (ret < 0) {
printError(MSG_UpdateFail); printError(MSG_UpdateFail);
sleep(5); sleep(5);
@ -1158,8 +1133,7 @@ int main(int argc, char **argv)
// Unmount the SD Card // Unmount the SD Card
UnmountSD(); UnmountSD();
deinitGUI(); deinitGUI();
if(*LOADER_STUB) exit(0); exit(0);
SYS_ResetSystem(SYS_RETURNTOMENU, 0, 0);
} }
// Return to System Menu // Return to System Menu

View File

@ -62,16 +62,11 @@ s32 updateApp(void) {
ret = net_init(); ret = net_init();
if (ret < 0) if (ret < 0)
goto out; goto out;
char buf[128] = {0};
u32 http_status; u32 http_status;
u8* outbuf; u8* outbuf;
u32 length; u32 length;
const char *checkStr = "Version=";
snprintf(buf, sizeof(buf), "http://syscheck-hd.googlecode.com/svn/trunk/Version.txt");
ret = http_request(buf, 1 << 31); ret = http_request("http://syscheck-hd.googlecode.com/svn/trunk/Version.txt", 1 << 31);
if (!ret) if (!ret)
{ {
gprintf("Error making http request\n"); gprintf("Error making http request\n");
@ -86,9 +81,9 @@ s32 updateApp(void) {
goto out; goto out;
} }
if (!strncmp((char*)outbuf, checkStr, strlen(checkStr))) if (!strncmp((char*)outbuf, "Version=", sizeof("Version=")))
{ {
int version = atoi((char*)(outbuf + strlen(checkStr))); int version = atoi((char*)(outbuf + sizeof("Version=")));
gprintf("INT: %i\n", version); gprintf("INT: %i\n", version);
if (version > REVISION) { if (version > REVISION) {

View File

@ -29,459 +29,13 @@ distribution.
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <gccore.h> #include <gccore.h>
#include <wiiuse/wpad.h>
#include "wiibasics.h" inline u32 be32(const u8 *p)
//#include "id.h"
#define MAX_WIIMOTES 4
static void *xfb = NULL;
static GXRModeObj *rmode = NULL;
u16 be16(const u8 *p)
{
return (p[0] << 8) | p[1];
}
u32 be32(const u8 *p)
{ {
return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
} }
u64 be64(const u8 *p) inline u64 be64(const u8 *p)
{ {
return ((u64)be32(p) << 32) | be32(p + 4); return ((u64)be32(p) << 32) | be32(p + 4);
} }
u32 getTitleIDUID(u64 titleID){
s32 ret, i;
static u8 uid_buffer[0x1000] ATTRIBUTE_ALIGN(32);
u32 size;
ret = ISFS_ReadFileToArray("/sys/uid.sys", uid_buffer, 0x1000, &size);
if (ret < 0)
return 0;
for(i = 0; i < size; i+=12)
if (be64(&uid_buffer[i]) == titleID)
return be32(&uid_buffer[i+8]);
return 0;
}
u64 getUIDTitleID(u32 uid){
s32 ret, i;
static u8 uid_buffer[0x1000] ATTRIBUTE_ALIGN(32);
u32 size;
ret = ISFS_ReadFileToArray("/sys/uid.sys", uid_buffer, 0x1000, &size);
if (ret < 0)
return 0;
for(i = 8; i < size; i+=12)
if (be32(&uid_buffer[i]) == uid)
return be64(&uid_buffer[i-8]);
return 0;
}
/* Basic init taken pretty directly from the libOGC examples */
void basicInit(void)
{
// Initialise the video system
VIDEO_Init();
// Obtain the preferred video mode from the system
// This will correspond to the settings in the Wii menu
rmode = VIDEO_GetPreferredMode(NULL);
//rmode->viWidth = 678;
//rmode->viXOrigin = (VI_MAX_WIDTH_PAL - 678)/2;
GX_AdjustForOverscan(rmode, rmode, 32, 24);
// Allocate memory for the display in the uncached region
xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
// Initialise the console, required for printf
console_init(xfb,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
// Set up the video registers with the chosen mode
VIDEO_Configure(rmode);
// Tell the video hardware where our display memory is
VIDEO_SetNextFramebuffer(xfb);
// Make the display visible
VIDEO_SetBlack(FALSE);
// Flush the video register changes to the hardware
VIDEO_Flush();
// Wait for Video setup to complete
VIDEO_WaitVSync();
if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
// The console understands VT terminal escape codes
// This positions the cursor on row 2, column 0
// we can use variables for this with format codes too
// e.g. printf ("\x1b[%d;%dH", row, column );
printf("\x1b[2;0H");
}
void miscInit(void)
{
int ret;
// This function initialises the attached controllers
WPAD_Init();
// snip!
//Identify_SU();
printf("Initializing Filesystem driver...");
fflush(stdout);
ret = ISFS_Initialize();
if (ret < 0) {
printf("\nError! ISFS_Initialize (ret = %d)\n", ret);
wait_anyKey();
exit(1);
} else {
printf("OK!\n");
}
//IdentSysMenu();
}
void IdentSysMenu(void){
int ret;
// snip!
//Identify_SysMenu();
ret = ES_SetUID(TITLE_ID(1, 2));
if (ret < 0){
printf("SetUID fail %d", ret);
wait_anyKey();
exit(1);
}
printf("Initializing Filesystem driver...");
fflush(stdout);
ISFS_Deinitialize();
ret = ISFS_Initialize();
if (ret < 0) {
printf("\nError! ISFS_Initialize (ret = %d)\n", ret);
wait_anyKey();
exit(1);
} else {
printf("OK!\n");
}
}
void miscDeInit(void)
{
fflush(stdout);
ISFS_Deinitialize();
}
u32 getButtons(void)
{
WPAD_ScanPads();
return WPAD_ButtonsDown(0);
}
u32 wait_anyKey(void) {
u32 pressed;
while(!(pressed = getButtons())) {
VIDEO_WaitVSync();
}
if (pressed & WPAD_BUTTON_HOME) exit(1);
return pressed;
}
u32 wait_key(u32 button) {
u32 pressed;
do {
VIDEO_WaitVSync();
pressed = getButtons();
if (pressed & WPAD_BUTTON_HOME) exit(1);
} while(!(pressed & button));
return pressed;
}
char charASCII(u8 c) {
if (c < 0x20 || c > 0x7E)
return '.';
else
return (char)c;
}
void hex_print_array16(const u8 *array, u32 size){
u32 offset = 0;
u32 page_size = 0x100;
char line[17];
line[16] = 0;
if (size > page_size)
printf("Page 1 of %u", (size / page_size)+1);
while (offset < size){
if (!(offset % 16)) printf("\n0x%08X: ", offset);
printf("%02X", array[offset]);
line[offset % 16] = charASCII(array[offset]);
if (!(++offset % 2))
printf(" ");
if (!(offset % 16))
printf(line);
if (!(offset % page_size) && offset < size){
u32 pressed;
printf("\n\tPress a key for next page or B for finish\n");
pressed = wait_anyKey();
if (pressed & WPAD_BUTTON_HOME)
exit(1);
else if (pressed & WPAD_BUTTON_B)
return;
}
}
}
bool yes_or_no(void){
bool yes = 0;
u32 pressed;
printf("\t[*] (A) [YES] || [NO] (B)\n");
pressed = wait_anyKey();
if (pressed & WPAD_BUTTON_A) {
yes = 1;
}
else if (pressed & WPAD_BUTTON_B) {
yes = 0;
}
//u32 buttons = 0;
/*do {
yes = buttons & WPAD_BUTTON_LEFT;
if(yes)
printf("\r\x1b[K <\x1b[30m\x1b[47;1m [Yes] \x1b[37;1m\x1b[40m> [No] ");
else
printf("\r\x1b[K [Yes] <\x1b[30m\x1b[47;1m [No] \x1b[37;1m\x1b[40m> ");
} while ((buttons = wait_key(WPAD_BUTTON_A | WPAD_BUTTON_LEFT | WPAD_BUTTON_RIGHT))
&& (!(buttons & WPAD_BUTTON_A)));
printf("\n");*/
return yes;
}
/* Reads a file from ISFS to an array in memory */
s32 ISFS_ReadFileToArray (const char *filepath, u8 *filearray, u32 max_size, u32 *file_size) {
s32 ret, fd;
static fstats filestats ATTRIBUTE_ALIGN(32);
*file_size = 0;
ret = ISFS_Open(filepath, ISFS_OPEN_READ);
if (ret <= 0)
{
printf("Error! ISFS_Open (ret = %d)\n", ret);
return -1;
}
fd = ret;
ret = ISFS_GetFileStats(fd, &filestats);
if (ret < 0)
{
printf("Error! ISFS_GetFileStats (ret = %d)\n", ret);
return -1;
}
*file_size = filestats.file_length;
if (*file_size > max_size)
{
printf("File is too large! Size: %u Max: %u", *file_size, max_size);
return -1;
}
ret = ISFS_Read(fd, filearray, *file_size);
*file_size = ret;
if (ret < 0)
{
printf("Error! ISFS_Read (ret = %d)\n", ret);
return -1;
}
else if (ret != filestats.file_length)
{
printf("Error! ISFS_Read Only read: %d\n", ret);
return -1;
}
ret = ISFS_Close(fd);
if (ret < 0)
{
printf("Error! ISFS_Close (ret = %d)\n", ret);
return -1;
}
return 0;
}
/* Writes from an array in memory to a file with ISFS */
s32 ISFS_WriteFileFromArray (const char *filepath, const u8 *filearray, u32 array_size, u32 ownerID, u16 groupID, u8 attr, u8 own_perm, u8 group_perm, u8 other_perm){
s32 ret, fd = 0, out;
u64 currentTid;
u32 realownid;
u16 realgroupid;
u8 realattr, realownperm, realgroupperm, realotherperm;
static fstats filestats ATTRIBUTE_ALIGN(32);
out = 0;
ret = ISFS_Open(filepath, ISFS_OPEN_WRITE);
if (ret == -102){
printf("\tMaking file writable...\n");
ret = ISFS_SetAttr(filepath, ownerID, groupID, attr, 3, 3, 3);
if (ret < 0)
{
printf("Error! ISFS_SetAttr (ret = %d)\n", ret);
out = -1;
goto cleanup;
}
return ISFS_WriteFileFromArray(filepath, filearray, array_size, ownerID, groupID, attr, own_perm, group_perm, other_perm);
} else if (ret == -106){
printf("\tCreating file...\n");
ret = ISFS_CreateFile(filepath, attr, 3, 3, 3);
if (ret < 0){
printf("Error! ISFS_CreateFile (ret = %d)\n", ret);
out = -1;
goto cleanup;
}
return ISFS_WriteFileFromArray(filepath, filearray, array_size, ownerID, groupID, attr, own_perm, group_perm, other_perm);
} else if (ret <= 0) {
printf("Error! ISFS_Open WRITE (ret = %d)\n", ret);
out = -1;
goto cleanup;
}
fd = ret;
ret = ISFS_Seek(fd, 0, 0);
if (ret < 0) {
printf("Error! ISFS_Seek (ret = %d)\n", ret);
out = -1;
goto cleanup;
}
ret = ISFS_Write(fd, filearray, array_size);
if (ret < 0)
{
printf("Error! ISFS_Write (ret = %d)\n", ret);
out = -1;
goto cleanup;
}
if (ret != array_size)
{
printf("Filesize is wrong! Wrote:%u Expect:%u", filestats.file_length, array_size);
out = -1;
}
ret = ISFS_Close(fd);
if (ret < 0)
{
printf("Error! ISFS_Close (ret = %d)\n", ret);
return -1;
}
fd = 0;
/*
ret = ISFS_Open(filepath, ISFS_OPEN_READ);
if (ret <= 0) {
printf("Error! ISFS_Open READ (ret = %d)\n", ret);
out = -1;
goto cleanup;
}
fd = ret;
ret = ISFS_GetFileStats(fd, &filestats);
if (ret < 0)
{
printf("Error! ISFS_GetFileStats (ret = %d)\n", ret);
out = -1;
goto cleanup;
}
ret = ISFS_Close(fd);
if (ret < 0)
{
printf("Error! ISFS_Close (ret = %d)\n", ret);
return -1;
}
fd = 0;
*/
ret = ISFS_GetAttr(filepath, &realownid, &realgroupid, &realattr, &realownperm, &realgroupperm, &realotherperm);
if (ret < 0)
{
printf("Error! ISFS_GetAttr (ret = %d)\n", ret);
out = -1;
}
if (realownid != ownerID){
ret = ES_GetTitleID(&currentTid);
if (ret){
printf("Fail GetTitleID %d", ret);
if(wait_key(WPAD_BUTTON_A | WPAD_BUTTON_B) & WPAD_BUTTON_B)
goto cleanup;
}
ret = ES_SetUID(getUIDTitleID(ownerID));
if (ret){
printf("Couldn't set OwnerID, using current owner ID\n");
if(wait_key(WPAD_BUTTON_A | WPAD_BUTTON_B) & WPAD_BUTTON_B)
goto cleanup;
ownerID = realownid;
}
}
ret = ISFS_SetAttr(filepath, ownerID, groupID, attr, own_perm, group_perm, other_perm);
if (ret < 0)
{
printf("Error! ISFS_SetAttr (ret = %d)\n", ret);
out = -1;
goto cleanup;
}
if (realownid != ownerID){
ret = ES_SetUID(currentTid);
if (ret){
printf("Fail SetUID %d", ret);
if(wait_key(WPAD_BUTTON_A | WPAD_BUTTON_B) & WPAD_BUTTON_B)
goto cleanup;
}
}
cleanup:
if (fd) {
ret = ISFS_Close(fd);
if (ret < 0)
{
printf("Error! ISFS_Close (ret = %d)\n", ret);
return -1;
}
}
return out;
}