usbloadergx/source/mload/fatffs_util.c

312 lines
7.2 KiB
C
Raw Normal View History

/*
2010-09-24 02:48:03 +02:00
From Custom IOS Module (FAT)
2010-09-24 02:48:03 +02:00
Copyright (C) 2009 Waninkoko.
Copyright (C) 2010 Hermes.
2010-09-24 02:48:03 +02:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
2010-09-24 02:48:03 +02:00
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2010-09-24 02:48:03 +02:00
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "fatffs_util.h"
#include <string.h>
#include <sys/dir.h>
#include <sys/stat.h>
#include <stdio.h>
#include <malloc.h>
extern u32 nand_mode;
2010-09-24 02:48:03 +02:00
s32 FAT_DeleteDir(const char *dirpath)
{
DIR_ITER *dir;
s32 ret;
/* Open directory */
2010-09-24 02:48:03 +02:00
dir = diropen(dirpath);
if (!dir) return -1;
/* Read entries */
2010-09-24 02:48:03 +02:00
for (;;)
{
2010-09-24 02:48:03 +02:00
char filename[256], newpath[256];
struct stat filestat;
/* Read entry */
2010-09-24 02:48:03 +02:00
if (dirnext(dir, filename, &filestat)) break;
/* Non valid entry */
2010-09-24 02:48:03 +02:00
if (filename[0] == '.') continue;
/* Generate entry path */
2010-09-24 02:48:03 +02:00
strcpy(newpath, dirpath);
strcat(newpath, "/");
strcat(newpath, filename);
/* Delete directory contents */
2010-09-24 02:48:03 +02:00
if (filestat.st_mode & S_IFDIR) FAT_DeleteDir(newpath);
/* Delete object */
2010-09-24 02:48:03 +02:00
ret = remove(newpath);
/* Error */
2010-09-24 02:48:03 +02:00
if (ret != 0) break;
}
/* Close directory */
2010-09-24 02:48:03 +02:00
dirclose(dir);
return 0;
}
static int global_error = 0;
static char temp_read_buffer[16384] ATTRIBUTE_ALIGN( 32 );
2010-09-24 02:48:03 +02:00
s32 _FFS_to_FAT_Copy(const char *ffsdirpath, const char *fatdirpath)
{
int n;
u32 blocks, ionodes;
int pos = 0;
char *list;
s32 ret;
u32 ionodes_temp;
2010-09-24 02:48:03 +02:00
if (ISFS_GetUsage(ffsdirpath, &blocks, &ionodes))
{
global_error = -1;
return -1;
}
2010-09-24 02:48:03 +02:00
list = memalign(32, ionodes * 13);
2010-09-24 02:48:03 +02:00
if (!list)
{
global_error = -2;
return -2;
}
2010-09-24 02:48:03 +02:00
if (ISFS_ReadDir(ffsdirpath, list, &ionodes))
{
free(list);
global_error = -3;
return -3;
}
2010-09-24 02:48:03 +02:00
if (ionodes) mkdir(fatdirpath, S_IRWXO | S_IRWXG | S_IRWXU);
/* Read entries */
2010-09-24 02:48:03 +02:00
for (n = 0; n < ionodes; n++)
{
2010-09-24 02:48:03 +02:00
char * filename;
char newffspath[256], newfatpath[256];
/* Read entry */
filename = &list[pos];
2010-09-24 02:48:03 +02:00
pos += strlen(&list[pos]) + 1;
/* Non valid entry */
2010-09-24 02:48:03 +02:00
if (filename[0] == '.') continue;
/* Generate entry path */
2010-09-24 02:48:03 +02:00
strcpy(newffspath, ffsdirpath);
strcat(newffspath, "/");
strcat(newffspath, filename);
2010-09-24 02:48:03 +02:00
strcpy(newfatpath, fatdirpath);
strcat(newfatpath, "/");
strcat(newfatpath, filename);
2010-09-24 02:48:03 +02:00
ret = ISFS_ReadDir(newffspath, NULL, &ionodes_temp);
if (ret == 0) // it is a directory
{
2010-09-24 02:48:03 +02:00
_FFS_to_FAT_Copy(newffspath, newfatpath);
2010-09-24 02:48:03 +02:00
if (global_error)
{
free(list);
return global_error;
}
}
else // copy the file
{
int fd;
FILE *fp;
2010-09-24 02:48:03 +02:00
fd = ISFS_Open(newffspath, ISFS_OPEN_READ);
if (fd < 0)
{
global_error = -4;
2010-09-24 02:48:03 +02:00
free(list);
return global_error;
}
else
{
int len;
2010-09-24 02:48:03 +02:00
fp = fopen(newfatpath, "w");
if (!fd)
{
ISFS_Close(fd);
global_error = -5;
free(list);
return global_error;
}
2010-09-24 02:48:03 +02:00
len = ISFS_Seek(fd, 0, 2);
//if(len<0) {ISFS_Close(fd);global_error=-6;free(list);return global_error;}
2010-09-24 02:48:03 +02:00
ISFS_Seek(fd, 0, 0);
2010-09-24 02:48:03 +02:00
while (len > 0)
{
2010-09-24 02:48:03 +02:00
ret = len;
if (len > 16384) ret = 16384;
if (ISFS_Read(fd, temp_read_buffer, ret) != ret)
{
global_error = -7;
break;
}
2010-09-24 02:48:03 +02:00
if (fwrite(temp_read_buffer, 1, ret, fp) != ret)
{
global_error = -8;
break;
}
len -= ret;
}
2010-09-24 02:48:03 +02:00
fclose(fp);
ISFS_Close(fd);
2010-09-24 02:48:03 +02:00
if (global_error)
{
free(list);
return global_error;
}
}
}
}
2010-09-24 02:48:03 +02:00
free(list);
return 0;
}
2010-09-24 02:48:03 +02:00
s32 FFS_to_FAT_Copy(const char *ffsdirpath, const char *fatdirpath)
{
u32 blocks, ionodes;
int ret;
char create_dir[256];
ISFS_Initialize();
2010-09-24 02:48:03 +02:00
ret = ISFS_GetUsage(ffsdirpath, &blocks, &ionodes);
2010-09-24 02:48:03 +02:00
if (ret == 0)
{
int n = 0;
// creating the path directory
2010-09-24 02:48:03 +02:00
strcpy(create_dir, fatdirpath);
2010-09-24 02:48:03 +02:00
while (create_dir[n] != 0 && create_dir[n] != '/')
n++;
2010-09-24 02:48:03 +02:00
if (create_dir[n] == '/') n++;
2010-09-24 02:48:03 +02:00
while (create_dir[n] != 0)
{
2010-09-24 02:48:03 +02:00
if (create_dir[n] == '/')
{
create_dir[n] = 0;
2010-09-24 02:48:03 +02:00
mkdir(create_dir, S_IRWXO | S_IRWXG | S_IRWXU);
create_dir[n] = '/';
}
n++;
}
global_error = 0;
// copy files
2010-09-24 02:48:03 +02:00
_FFS_to_FAT_Copy(ffsdirpath, fatdirpath);
ret = global_error = 0;
}
else ret = -101;
ISFS_Deinitialize();
return ret;
}
static char temp_cad[512];
2010-09-24 02:48:03 +02:00
void create_FAT_FFS_Directory(struct discHdr *header)
{
2010-09-24 02:48:03 +02:00
char device[2][4] = { "sd:", "ud:" };
2010-09-24 02:48:03 +02:00
if (!header) return;
2010-09-24 02:48:03 +02:00
sprintf((char *) temp_cad, "%s/nand%c", &device[(nand_mode & 2) != 0][0], (nand_mode & 0xc) ? 49
+ ((nand_mode >> 2) & 3) : '\0');
2010-09-24 02:48:03 +02:00
sprintf((char *) temp_cad + 32, "%2.2x%2.2x%2.2x%2.2x", header->id[0], header->id[1], header->id[2], header->id[3]);
2010-09-24 02:48:03 +02:00
sprintf((char *) temp_cad + 64, "%s/title/00010000/%s", temp_cad, temp_cad + 32);
sprintf((char *) temp_cad + 128, "%s/title/00010004/%s", temp_cad, temp_cad + 32);
sprintf((char *) temp_cad + 256, "/title/00010000/%s", temp_cad + 32);
sprintf((char *) temp_cad + 384, "/title/00010004/%s", temp_cad + 32);
}
2010-09-24 02:48:03 +02:00
int test_FAT_game(char * directory)
{
DIR_ITER * dir = NULL;
2010-09-24 02:48:03 +02:00
dir = diropen(directory);
2010-09-24 02:48:03 +02:00
if (dir)
{
dirclose(dir);
return 1;
}
return 0;
}
2010-09-24 02:48:03 +02:00
char *get_FAT_directory1(void)
{
return temp_cad + 64;
}
2010-09-24 02:48:03 +02:00
char *get_FAT_directory2(void)
{
return temp_cad + 128;
}
2010-09-24 02:48:03 +02:00
char *get_FFS_directory1(void)
{
return temp_cad + 256;
}
2010-09-24 02:48:03 +02:00
char *get_FFS_directory2(void)
{
return temp_cad + 384;
}