- several minor fixes to FTP mode

- implement FTP restoring of big saves (bigger than the memory buffer)
- print some boot/debug messages
This commit is contained in:
msiewert76@gmail.com 2011-05-29 11:11:39 +00:00
parent afe6eb430a
commit c7bb848419
7 changed files with 243 additions and 119 deletions

View File

@ -294,16 +294,33 @@ void auxspi_erase(bool ir)
}
}
/*
void auxspi_wait_wip()
void auxspi_erase_sector(u32 sector, bool ir)
{
auxspi_disable_infrared();
auxspi_open(0);
auxspi_write(0x05);
uint8 sr;
do {
sr = auxspi_read();
} while (sr & 0x01);
auxspi_close();
uint8 type = auxspi_save_type(ir);
if (type == 3) {
if (ir)
auxspi_disable_infrared();
auxspi_open(0);
// set WEL (Write Enable Latch)
auxspi_write(0x06);
auxspi_close_lite();
if (ir)
auxspi_disable_infrared();
auxspi_open(0);
auxspi_write(0xd8);
auxspi_write(sector & 0xff);
auxspi_write((sector >> 8) & 0xff);
auxspi_write((sector >> 8) & 0xff);
auxspi_close_lite();
// wait for programming to finish
if (ir)
auxspi_disable_infrared();
auxspi_open(0);
auxspi_write(5);
do { REG_AUXSPIDATA = 0; auxspi_wait_busy(); } while (REG_AUXSPIDATA & 0x01); // WIP (Write In Progress) ?
auxspi_wait_busy();
auxspi_close();
}
}
*/

View File

@ -43,10 +43,9 @@ void auxspi_read_data(uint32 addr, uint8* buf, uint16 cnt, uint8 type = 0, bool
void auxspi_write_data(uint32 addr, uint8 *buf, uint16 cnt, uint8 type = 0, bool ir = false);
void auxspi_disable_infrared();
void auxspi_erase(bool ir = false);
//void auxspi_wait_wip();
void auxspi_erase_sector(u32 sector, bool ir = false);
bool auxspi_has_infrared();
//void auxspi_gpio_init_save();
bool auxspi_is_unknown_type3();

View File

@ -74,6 +74,7 @@ void displayPrintUpper()
consoleSetWindow(&upperScreen, 0, 0, 32, 24);
consoleClear();
iprintf("Mode :\n");
iprintf("Memory :\n");
iprintf("--- SLOT 1 ---------------------");
iprintf("Game ID :\n");
iprintf("Game name:\n");
@ -90,9 +91,9 @@ void displayPrintUpper()
}
// print upper screen
consoleSetWindow(&upperScreen, 10, 2, 22, 4);
consoleSetWindow(&upperScreen, 10, 3, 22, 4);
consoleClear();
consoleSetWindow(&upperScreen, 10, 7, 22, 4);
consoleSetWindow(&upperScreen, 10, 8, 22, 4);
consoleClear();
// fetch cartridge header (maybe, calling "cardReadHeader" on a FC messes with libfat!)
@ -128,9 +129,12 @@ void displayPrintUpper()
}
consoleClear();
iprintf("%s", name);
// 0.5) memory buffer size
consoleSetWindow(&upperScreen, 10, 1, 20, 1);
iprintf("%i kB", size_buf >> 10);
// 1) The cart id.
consoleSetWindow(&upperScreen, 10, 2, 22, 1);
consoleSetWindow(&upperScreen, 10, 3, 22, 1);
sprintf(&name[0], "----");
if (slot_1_type == 2) {
sprintf(&name[0], "Flash Card");
@ -142,7 +146,7 @@ void displayPrintUpper()
iprintf("%s", name);
// 2) The cart name.
consoleSetWindow(&upperScreen, 10, 3, 22, 1);
consoleSetWindow(&upperScreen, 10, 4, 22, 1);
sprintf(&name[0], "----");
if (slot_1_type == 2) {
sprintf(&name[0], "Flash Card");
@ -154,7 +158,7 @@ void displayPrintUpper()
iprintf("%s", name);
// 3) The save type
consoleSetWindow(&upperScreen, 10, 4, 22, 1);
consoleSetWindow(&upperScreen, 10, 5, 22, 1);
sprintf(&name[0], "----");
if (slot_1_type == 2) {
sprintf(&name[0], "Flash Card");
@ -183,7 +187,7 @@ void displayPrintUpper()
iprintf("%s", name);
// 4) Special properties (infrared device...)
consoleSetWindow(&upperScreen, 10, 5, 22, 1);
consoleSetWindow(&upperScreen, 10, 6, 22, 1);
consoleClear();
memset(&name[0], 0, MAXPATHLEN);
if (slot_1_type == 1) {
@ -195,7 +199,7 @@ void displayPrintUpper()
// Slot 2 status
// 5) GBA game id
consoleSetWindow(&upperScreen, 10, 7, 22, 1);
consoleSetWindow(&upperScreen, 10, 8, 22, 1);
consoleClear();
memset(&name[0], 0, MAXPATHLEN);
if (ezflash) {
@ -217,7 +221,7 @@ void displayPrintUpper()
iprintf("%s", name);
// 6) GBA game name
consoleSetWindow(&upperScreen, 10, 8, 22, 1);
consoleSetWindow(&upperScreen, 10, 9, 22, 1);
consoleClear();
memset(&name[0], 0, MAXPATHLEN);
if (ezflash)
@ -232,7 +236,7 @@ void displayPrintUpper()
iprintf(name);
// 7) GBA save size
consoleSetWindow(&upperScreen, 10, 9, 22, 1);
consoleSetWindow(&upperScreen, 10, 10, 22, 1);
consoleClear();
memset(&name[0], 0, MAXPATHLEN);
if (ezflash)
@ -264,7 +268,7 @@ void displayPrintUpper()
iprintf(name);
// 8) GBA special stuff
consoleSetWindow(&upperScreen, 10, 10, 22, 1);
consoleSetWindow(&upperScreen, 10, 11, 22, 1);
consoleClear();
memset(&name[0], 0, MAXPATHLEN);
if (ezflash)

View File

@ -37,12 +37,13 @@
#include "display.h"
#include "strings.h"
#include "globals.h"
#include "fileselect.h"
extern u8 *data;
extern u32 size_buf;
//extern u8 *data;
//extern u32 size_buf;
// ---------------------------------------------------------------------------------
void ftpGetFileList(const char *dir, netbuf *ctrl, int &num)

View File

@ -1451,3 +1451,97 @@ GLOBALDEF int FtpAccessBuf(const char *path, int typ, int mode, netbuf *nControl
return 1;
}
/*
* FtpAccessPos - return a handle for a data stream, starting at some offset "pos"
*
* return 1 if successful, 0 otherwise
*/
GLOBALDEF int FtpAccessPos(const char *path, int typ, int mode, int pos, netbuf *nControl,
netbuf **nData)
{
char buf[256];
int dir;
if ((path == NULL) &&
((typ == FTPLIB_FILE_WRITE) || (typ == FTPLIB_FILE_READ)))
{
sprintf(nControl->response,
"Missing path argument for file transfer\n");
return 0;
}
sprintf(buf, "TYPE %c", mode);
if (!FtpSendCmd(buf, '2', nControl))
return 0;
switch (typ)
{
case FTPLIB_DIR:
strcpy(buf,"NLST");
dir = FTPLIB_READ;
break;
case FTPLIB_DIR_VERBOSE:
strcpy(buf,"LIST");
dir = FTPLIB_READ;
break;
case FTPLIB_FILE_READ:
strcpy(buf,"RETR");
dir = FTPLIB_READ;
break;
case FTPLIB_FILE_WRITE:
strcpy(buf,"STOR");
dir = FTPLIB_WRITE;
break;
default:
sprintf(nControl->response, "Invalid open type %d\n", typ);
return 0;
}
if (path != NULL)
{
int i = strlen(buf);
buf[i++] = ' ';
if ((strlen(path) + i) >= sizeof(buf))
return 0;
strcpy(&buf[i],path);
}
if (FtpOpenPort(nControl, nData, mode, dir) == -1)
return 0;
// seek start position
char buf2[256];
sprintf(buf2, "REST %i", pos);
if (!FtpSendCmd(buf2, '3', nControl))
{
FtpClose(*nData);
*nData = NULL;
return 0;
}
// start transfer
if (!FtpSendCmd(buf, '1', nControl))
{
FtpClose(*nData);
*nData = NULL;
return 0;
}
(*nData)->ctrl = nControl;
nControl->data = *nData;
if (nControl->cmode == FTPLIB_PORT)
{
if (!FtpAcceptConnection(*nData,nControl))
{
FtpClose(*nData);
*nData = NULL;
nControl->data = NULL;
return 0;
}
}
return 1;
}
GLOBALREF int FtpCloseAccess(netbuf *nControl, netbuf *nData)
{
char buf[256];
strcpy(buf,"ABOR");
// seek start position
if (!FtpSendCmd(buf, '2', nControl))
{
return 0;
}
return 1;
}

View File

@ -132,6 +132,11 @@ GLOBALREF int FtpAccessBuf(const char *path, int typ, int mode, netbuf *nControl
netbuf **nData);
GLOBALREF int FtpDirBuf(char *buf, int size, const char *path, netbuf *nControl);
// added by Pokedoc
GLOBALREF int FtpAccessPos(const char *path, int typ, int pos, int mode, netbuf *nControl,
netbuf **nData);
GLOBALREF int FtpCloseAccess(netbuf *nControl, netbuf *nData);
#ifdef __cplusplus
};
#endif

View File

@ -57,6 +57,16 @@ using namespace std;
static u32 pitch = 0x40000;
// ---------------------------------------------------------------------
u8 log2trunc(u32 size0)
{
u8 size = 1;
while (size0 > (1 << size)) {
size++;
}
return size;
}
// ---------------------------------------------------------------------
bool swap_cart()
{
@ -407,18 +417,12 @@ void hwRestore3in1()
displayMessageF(STR_HW_SELECT_FILE); // lower screen is used for file browser
fileSelect("/", path, fname, 0, false, false);
char msg[256];
// This does not have to be translated.
// FIXME: make this more meaningful!
sprintf(msg, "%s/%s", path, fname);
FILE *file = fopen(msg, "rb");
uint32 size0 = fileSize(msg);
uint8 size = 1;
// crude log2 function
while (size0 > (1 << size)) {
size++;
}
uint8 size = log2trunc(size0);
int size_blocks = 1 << max(0, int8(size) - 18); // ... in units of 0x40000 bytes - that's 256 kB
displayMessage2F(STR_HW_3IN1_FORMAT_NOR);
@ -647,12 +651,12 @@ void hwBackupFTP(bool dlp)
netbuf *buf, *ndata;
int j;
static int jmax = 10;
bool ir = (slot_1_type == 1) ? true : false;
// Dump save and write it to FTP server
// First: swap card
if (!dlp)
swap_cart();
bool ir = (slot_1_type == 1) ? true : false;
displayPrintUpper();
uint8 size = auxspi_save_size_log_2(ir);
int size_blocks = 1 << max(0, (int8(size) - 18)); // ... in units of 0x40000 bytes - that's 256 kB
@ -669,10 +673,9 @@ void hwBackupFTP(bool dlp)
while(1);
}
displayMessage2F(STR_HW_FTP_SEEK_FTP);
char fullname[512];
sprintf(fullname, "%s:%i", ftp_ip, ftp_port);
sprintf(txt, "%s:%i", ftp_ip, ftp_port);
j = 0;
while (!FtpConnect(fullname, &buf)) {
while (!FtpConnect(txt, &buf)) {
j++;
if (j >= jmax) {
displayWarning2F(STR_HW_FTP_ERR_FTP);
@ -697,6 +700,8 @@ void hwBackupFTP(bool dlp)
displayMessageF(STR_HW_SELECT_FILE_OW);
displayStateF(STR_HW_FTP_DIR);
fileSelect("/", fdir, fname, buf, true, false);
displayMessageF(STR_EMPTY);
displayStateF(STR_EMPTY);
bool newfile;
if (!fname[0])
newfile = true;
@ -725,15 +730,11 @@ void hwBackupFTP(bool dlp)
// Fourth: dump save
displayStateF(STR_EMPTY);
// This may not have to be translated.
// FIXME: make this more meaningful!
sprintf(fullname, "%s%s", fdir, fname);
FtpAccess(fname, FTPLIB_FILE_WRITE, FTPLIB_IMAGE, buf, &ndata);
u32 length = 0x200;
if (size < 9)
length = 1 << size;
for (int i = 0; i < (1 << (size - 9)); i++) {
displayProgressBar(i+1, (size_blocks << 6));
u32 length = 1 << 9;
int num_ftp_blocks = 1 << (size - 9);
for (int i = 0; i < num_ftp_blocks; i++) {
displayProgressBar(i+1, num_ftp_blocks);
auxspi_read_data(i << 9, (u8*)&data[0], length, type, ir);
u32 out = 0;
while (out < length) {
@ -751,8 +752,8 @@ void hwBackupFTP(bool dlp)
}
}
}
FtpClose(ndata);
FtpQuit(ndata);
FtpCloseAccess(buf, ndata);
FtpQuit(buf);
Wifi_DisconnectAP();
@ -760,6 +761,64 @@ void hwBackupFTP(bool dlp)
displayMessage2F(STR_HW_PLEASE_REBOOT);
while(1);
}
displayProgressBar(0,0);
displayMessageF(STR_EMPTY);
}
// an internal function used to read big files from the FTP server
bool hwRestoreFTPPartial(u32 ofs, u32 size, u32 type, netbuf *ndata, bool ir)
{
int num_blocks_ftp = 1 << (size - 9);
u8 *pdata = data;
for (int i = 0; i < num_blocks_ftp; i++) {
displayProgressBar(i+1, num_blocks_ftp);
int in = 0;
while (in < 512) {
int delta = FtpRead((u8*)&pdata[in], 512-in, ndata);
in += delta;
if (delta < 512)
displayStateF(STR_HW_FTP_SLOW);
else
displayStateF(STR_EMPTY);
}
pdata += 512;
}
// Write to game (safe mode)
u32 LEN = 0, num_blocks = 0, shift = 0;
switch (type) {
case 1:
shift = 4; // 16 bytes
break;
case 2:
shift = 5; // 32 bytes
break;
case 3:
shift = 8; // 256 bytes
break;
default:
return false;
}
LEN = 1 << shift;
num_blocks = 1 << (size - shift);
if (type == 3) {
displayMessage2F(STR_HW_FORMAT_GAME);
u32 sector = ofs >> 16;
u32 num = max(u32(0), size-16);
for (int i = 0; i < (1 << num); i++) {
displayProgressBar(i+1, 1 << num);
auxspi_erase_sector(sector+i, ir);
}
displayProgressBar(0,0);
}
displayMessage2F(STR_HW_WRITE_GAME);
for (int i = 0; i < (1 << (size - shift)); i++) {
displayProgressBar(i+1, 1 << (size - shift));
auxspi_write_data(ofs + (i << shift), ((u8*)data)+(i<<shift), LEN, type, ir);
}
return true;
}
void hwRestoreFTP(bool dlp)
@ -767,7 +826,6 @@ void hwRestoreFTP(bool dlp)
netbuf *buf, *ndata;
int j;
static int jmax = 10;
bool ir = (slot_1_type == 1) ? true : false;
// Dump save and write it to FTP server
// First: connect to FTP server
@ -804,91 +862,34 @@ void hwRestoreFTP(bool dlp)
char fname[256] ="";
memset(fname, 0, 256);
displayMessageF(STR_HW_SELECT_FILE);
displayStateF(STR_STR, "FTP: dir");
displayStateF(STR_HW_FTP_DIR);
fileSelect("/", fdir, fname, buf, false, false);
displayMessageF(STR_EMPTY);
displayStateF(STR_EMPTY);
// Third: swap card
if (!dlp)
swap_cart();
displayPrintUpper();
bool ir = (slot_1_type == 1) ? true : false;
uint8 size = auxspi_save_size_log_2(ir);
uint8 type = auxspi_save_type(ir);
// Fourth: read file
displayStateF(STR_EMPTY);
FtpChdir(fdir, buf);
// FIXME: make this more meaningful!
sprintf(fullname, "%s%s", fdir, fname);
u32 LEN = 0, num_blocks = 0, shift = 0;
switch (type) {
case 1:
shift = 4; // 16 bytes
break;
case 2:
shift = 5; // 32 bytes
break;
case 3:
shift = 8; // 256 bytes
break;
default:
return;
}
LEN = 1 << shift;
int num_blocks_ftp = 1 << (size - 9);
num_blocks = 1 << (size - shift);
// if our save is small enough to fit in memory, use secure mode (i.e. read full file before erasing anything
bool insecure = ((1 << size) > size_buf);
if (insecure) {
#if 0
displayMessageF("Save larger than available RAM.\nRestoring will be dangerous!\n\nPress Start + Select to proceed");
while (!(keysCurrent() & (KEY_START | KEY_SELECT)));
#else
// TODO: translate this message!
displayMessage2F(STR_STR, "Save larger than available RAM.\nRestoring is buggy, so\nI will stop here.\n(Try Rudolphs tools.)");
while (1);
#endif
}
if ((type == 3) && (insecure)) {
displayMessage2F(STR_HW_FORMAT_GAME);
auxspi_erase(ir);
}
u8 data_log2 = log2trunc(size_buf);
int num_read_blocks = (size > data_log2) ? (1 << (size - data_log2)) : 1;
int len_block = min(data_log2, size);
FtpAccess(fname, FTPLIB_FILE_READ, FTPLIB_IMAGE, buf, &ndata);
u8 *pdata = data;
for (int i = 0; i < num_blocks_ftp; i++) {
displayProgressBar(i+1, num_blocks_ftp);
int in = 0;
while (in < 512) {
in += FtpRead((u8*)&pdata[in], 512-in, ndata);
if (in < 512)
displayStateF(STR_HW_FTP_SLOW);
else
displayStateF(STR_EMPTY);
}
// does not fit into memory: unsafe mode
// TODO: reactivate this!
if (insecure) {
displayStateF(STR_STR, "Writing save (insecure!)");
for (int j = 0; j < 1 << (9-shift); j++) {
auxspi_write_data((i << 9)+(j << shift), ((u8*)pdata)+(j<<shift), LEN, type, ir);
}
}
pdata += 512;
}
// Write to game (safe mode)
if (!insecure) {
if (type == 3) {
displayMessage2F(STR_HW_FORMAT_GAME);
auxspi_erase(ir);
}
for (int i = 0; i < (1 << (size - shift)); i++) {
displayMessage2F(STR_HW_WRITE_GAME);
displayProgressBar(i+1, 1 << (size - shift));
auxspi_write_data(i << shift, ((u8*)data)+(i<<shift), LEN, type, ir);
}
// read save in multiple blocks, if required
for (int i = 0; i < num_read_blocks; i++) {
char ctr[32];
sprintf(ctr, "%i/%i", i+1, num_read_blocks);
displayMessage2F(STR_HW_READ_FILE, fname);
displayMessageF(STR_STR, ctr);
hwRestoreFTPPartial(i << len_block, len_block, type, ndata, ir);
}
FtpClose(ndata);
FtpQuit(ndata);
FtpQuit(buf);
Wifi_DisconnectAP();
@ -896,6 +897,9 @@ void hwRestoreFTP(bool dlp)
displayMessage2F(STR_HW_PLEASE_REBOOT);
while(1);
}
displayProgressBar(0,0);
displayMessageF(STR_EMPTY);
}
// ------------------------------------------------------------