How would I copy a file to the wiis nand using c++? (homebrew)
#1
my code (not doing anything, not copying any files)
#include <stdio.h>
#include <stdlib.h>
#include <gccore.h>
#include <grrlib.h>
#include <stdio.h>
#include <fstream>
#include <wiiuse/wpad.h>
#include <time.h>
#include "RobotoBold_ttf.h"
#include "pointer_png.h"
#include "background_png.h"
#include <unistd.h>
#include <ogcsys.h>
#define pressed_quit() (WPAD_ButtonsDown(0)&WPAD_BUTTON_HOME)
#define held() (WPAD_ButtonsHeld(0)&WPAD_BUTTON_A)
using namespace std;
#define WSP_POINTER_CORRECTION_X 200
#define WSP_POINTER_CORRECTION_Y 250
int main()
{
u32 white=0xFFFFFFFF;
u32 reserved=0xFFFFFFFF;
u32 selected=0xEEEEEE99;
const int screenWidth=640;
const int screenHeight=480;
const int defaultWidth=120;
float copyButtonWidth=defaultWidth*3;
int copyButtonHeight=defaultWidth;
int copyButtonX=(screenWidth/2)-(copyButtonWidth/2);
int copyButtonY=(screenHeight/2)-(copyButtonHeight/2);
GRRLIB_Init();
WPAD_Init();
WPAD_SetDataFormat(0, WPAD_FMT_BTNS_ACC_IR);
GRRLIB_ttfFont *robotoBold=GRRLIB_LoadTTF(RobotoBold_ttf, RobotoBold_ttf_size);
GRRLIB_texImg *pointer=GRRLIB_LoadTexture(pointer_png);
GRRLIB_texImg *background=GRRLIB_LoadTexture(background_png);
string copyNKWii="Copy MKWii Save To NKWii";
while (1)
{
WPAD_ScanPads();
ir_t ir;
ISFS_Initialize();
SYS_Init();
WPAD_IR(WPAD_CHAN_0, &ir);
GRRLIB_DrawImg(0, 0, background, 0, 1, 1, white);
int pointerX=(ir.sx-WSP_POINTER_CORRECTION_X);
int pointerY=(ir.sy-WSP_POINTER_CORRECTION_Y);
GRRLIB_Rectangle(copyButtonX, copyButtonY, copyButtonWidth, copyButtonHeight, reserved, true);
GRRLIB_PrintfTTF(copyButtonX+30, copyButtonY+(copyButtonHeight/2)-11, robotoBold, copyNKWii.c_str(), 23, 0x000000FF);
GRRLIB_DrawImg(pointerX, pointerY, pointer, ir.angle, 1, 1, 0xFFFFFFFF);
if (pointerX>=copyButtonX&&pointerX<=screenWidth-copyButtonX&&pointerY>=copyButtonY&&pointerY<=screenHeight-copyButtonY&&held()) {
reserved=0x0A0A0A87;
string defaultPath="nand:/title/00010004/524d4345/data/";
string nkwiiPath="nand:/title/00010004/4a4e4b57/data/";
string mkwiiFiles[]={"banner.bin", "rksys.dat", "wc24dl.vff", "wc24scr.vff"};
string srcPaths[]={defaultPath+mkwiiFiles[0], defaultPath+mkwiiFiles[1], defaultPath+mkwiiFiles[2], defaultPath+mkwiiFiles[3]};
string dstPaths[]={nkwiiPath+mkwiiFiles[0], nkwiiPath+mkwiiFiles[1], nkwiiPath+mkwiiFiles[2], nkwiiPath+mkwiiFiles[3]};
for (int loopCounter=0; loopCounter<4; loopCounter++) {
string src=srcPaths[loopCounter];
string dst=dstPaths[loopCounter];
std::ifstream  src_f(src, std::ios::binary);
    std::ofstream  dst_f(dst, std::ios::binary);
    std::ifstream lastFile(dstPaths[3], std::ios::binary);
    std::ifstream firstFile(to_string(ISFS_MAXPATH).c_str(), std::ios::binary);
    if (!firstFile) {
    copyNKWii="File Path Is Invalid.";
    }
    else {
    copyNKWii="File Path Is Vaild.";
    }
    if (lastFile) {
    copyNKWii="Files Succsesfully Copied.";
    }
    dst_f << src_f.rdbuf();
}
}
if (pointerX>=copyButtonX&&pointerX<=screenWidth-copyButtonX&&pointerY>=copyButtonY&&pointerY<=screenHeight-copyButtonY) {
reserved=selected;
}
else {
reserved=0xEAEAEA87;
}
if (pressed_quit())
{
break;
}
GRRLIB_Render();
}
GRRLIB_FreeTTF(robotoBold);
GRRLIB_FreeTexture(pointer);
GRRLIB_FreeTexture(background);
GRRLIB_Exit();
exit(0);
}
fyi, devkitpro doesn't show any errors
Reply
#2
I personally don't do any C/C++ work (just Assembly) but I asked around for those who do via Discord (ty Seeky & Joshua). Here's what I was told-

You need to use ISFS entirely to do your file copying.

So basically...

Allocate some memory (has to be 32 byte aligned)
ISFS Open (with Read perms)
ISFS Read (dump to memory)
ISFS Close

Now ISFS Open other file (with Write perms)
ISFS Write
ISFS Close

EDIT: You might run into a permissions error, if so here's this...

Code:
// Copyright 2010 Joseph Jordan <joe.ftpii@psychlaw.com.au>
// This code is licensed to you under the terms of the GNU GPL, version 2;
// see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
#include <gccore.h>
#include <ogc/machine/processor.h>
#include <string.h>
#include <wiiuse/wpad.h>
#include "IOSPatch.h"

static void disable_memory_protection() {
    write32(MEM_PROT, read32(MEM_PROT) & 0x0000FFFF);
}

static u32 apply_patch(char *name, const u8 *old, u32 old_size, const u8 *patch, u32 patch_size, u32 patch_offset) {
    u8 *ptr_start = (u8*)*((u32*)0x80003134), *ptr_end = (u8*)0x94000000;
    u32 found = 0;
    u8 *location = NULL;
    while (ptr_start < (ptr_end - patch_size)) {
        if (!memcmp(ptr_start, old, old_size)) {
            found++;
            location = ptr_start + patch_offset;
            u8 *start = location;
            u32 i;
            for (i = 0; i < patch_size; i++) {
                *location++ = patch[i];
            }
            DCFlushRange((u8 *)(((u32)start) >> 5 << 5), (patch_size >> 5 << 5) + 64);
            ICInvalidateRange((u8 *)(((u32)start) >> 5 << 5), (patch_size >> 5 << 5) + 64);
        }
        ptr_start++;
    }
    return found;
}

static const u8 di_readlimit_old[] = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x01, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x0A, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00,
    0x7E, 0xD4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08
};
static const u8 di_readlimit_patch[] = { 0x7e, 0xd4 };

const u8 isfs_permissions_old[] = { 0x42, 0x8B, 0xD0, 0x01, 0x25, 0x66 };
const u8 isfs_permissions_patch[] = { 0x42, 0x8B, 0xE0, 0x01, 0x25, 0x66 };
static const u8 setuid_old[] = { 0xD1, 0x2A, 0x1C, 0x39 };
static const u8 setuid_patch[] = { 0x46, 0xC0 };
const u8 es_identify_old[] = { 0x28, 0x03, 0xD1, 0x23 };
const u8 es_identify_patch[] = { 0x00, 0x00 };
const u8 hash_old[] = { 0x20, 0x07, 0x23, 0xA2 };
const u8 hash_patch[] = { 0x00 };
const u8 new_hash_old[] = { 0x20, 0x07, 0x4B, 0x0B };
const u8 addticket_vers_check[] = { 0xD2, 0x01, 0x4E, 0x56 };
const u8 addticket_patch[] = { 0xE0 };
const u8 es_set_ahbprot_pattern[] = { 0x68, 0x5B, 0x22, 0xEC, 0x00, 0x52, 0x18, 0x9B, 0x68, 0x1B, 0x46, 0x98, 0x07, 0xDB };
const u8 es_set_ahbprot_patch[]   = { 0x01 };

u32 IOSPATCH_Apply() {
    u32 count = 0;
    s32 ret = 0;
    
    if (HAVE_AHBPROT) {
        disable_memory_protection();
        ret = apply_patch("es_set_ahbprot", es_set_ahbprot_pattern, sizeof(es_set_ahbprot_pattern), es_set_ahbprot_patch, sizeof(es_set_ahbprot_patch), 25);
    }
    if (ret) {
        WPAD_Shutdown();
        IOS_ReloadIOS(IOS_GetVersion());
        WPAD_Init();
        WPAD_SetDataFormat(WPAD_CHAN_0, WPAD_FMT_BTNS_ACC_IR);
    } else {
        return 0;
    }
    
    if (HAVE_AHBPROT) {
        disable_memory_protection();
        //count += apply_patch("di_readlimit", di_readlimit_old, sizeof(di_readlimit_old), di_readlimit_patch, sizeof(di_readlimit_patch), 12);
        count += apply_patch("isfs_permissions", isfs_permissions_old, sizeof(isfs_permissions_old), isfs_permissions_patch, sizeof(isfs_permissions_patch), 0);
        count += apply_patch("es_setuid", setuid_old, sizeof(setuid_old), setuid_patch, sizeof(setuid_patch), 0);
        count += apply_patch("es_identify", es_identify_old, sizeof(es_identify_old), es_identify_patch, sizeof(es_identify_patch), 2);
        count += apply_patch("hash_check", hash_old, sizeof(hash_old), hash_patch, sizeof(hash_patch), 1);
        count += apply_patch("new_hash_check", new_hash_old, sizeof(new_hash_old), hash_patch, sizeof(hash_patch), 1);
        count += apply_patch("add ticket patch", addticket_vers_check, sizeof(addticket_vers_check), addticket_patch, sizeof(addticket_patch), 0);
    }
    return count;
}

Code:
// Copyright 2010 Joseph Jordan <joe.ftpii@psychlaw.com.au>
// This code is licensed to you under the terms of the GNU GPL, version 2;
// see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
#ifndef _IOSPATCH_H
#define _IOSPATCH_H

#define HAVE_AHBPROT ((*(vu32*)0xcd800064 == 0xFFFFFFFF) ? 1 : 0)
#define MEM_REG_BASE 0xd8b4000
#define MEM_PROT (MEM_REG_BASE + 0x20a)

#include <gccore.h>

u32 IOSPATCH_Apply();

#endif /* _IOSPATCH_H */
Reply
#3
thanks!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)