WiiFlow_Lite/source/loader/wbfs_ext.c

262 lines
5.8 KiB
C
Raw Normal View History

2012-01-21 21:57:41 +01:00
// WBFS FAT by oggzee
#include <stdio.h>
#include <unistd.h>
#include <ogcsys.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/statvfs.h>
#include <ctype.h>
#include "wbfs.h"
#include "wdvd.h"
#include "splits.h"
#include "wbfs_ext.h"
#include "utils.h"
#include "disc.h"
#include "devicemounter/sdhc.h"
#include "devicemounter/usbstorage.h"
#include "fileOps/fileOps.h"
#include "gecko/gecko.hpp"
#include "libwbfs/libwbfs.h"
#include "loader/utils.h"
#include "loader/sys.h"
#include "gui/fmt.h"
2012-01-21 21:57:41 +01:00
#define TITLE_LEN 64
2013-02-18 13:05:18 +01:00
char wbfs_drive_tmp[16];
2012-01-21 21:57:41 +01:00
char wbfs_fs_drive[16];
char invalid_path[] = "/\\:|<>?*\"'";
split_info_t split;
struct statvfs wbfs_ext_vfs;
#define STRCOPY(DEST,SRC) strcopy(DEST,SRC,sizeof(DEST))
char* strcopy(char *dest, const char *src, int size)
{
strncpy(dest,src,size);
dest[size-1] = 0;
return dest;
}
wbfs_disc_t* WBFS_Ext_OpenDisc(u8 *discid, char *fname)
{
bool sd = strstr(fname, "sd") != NULL;
2012-01-21 21:57:41 +01:00
if (strcasecmp(strrchr(fname,'.'), ".iso") == 0)
{
// .iso file
// create a fake wbfs_disc
int fd = open(fname, O_RDONLY);
if (fd == -1) return NULL;
wbfs_disc_t *iso_file = malloc(sizeof(wbfs_disc_t));
memset(iso_file, 0, sizeof(wbfs_disc_t));
if (iso_file == NULL)
return NULL;
2012-01-21 21:57:41 +01:00
// mark with a special wbfs_part
wbfs_iso_file.wbfs_sec_sz = sd ? 512 : USBStorage2_GetSectorSize();
2012-01-21 21:57:41 +01:00
iso_file->p = &wbfs_iso_file;
iso_file->header = (void*)fd;
return iso_file;
}
wbfs_t *part = WBFS_Ext_OpenPart(fname);
if(!part)
return NULL;
2012-01-21 21:57:41 +01:00
return wbfs_open_disc(part, discid);
}
void WBFS_Ext_CloseDisc(wbfs_disc_t* disc)
{
if (!disc) return;
wbfs_t *part = disc->p;
// is this really a .iso file?
if (part == &wbfs_iso_file)
{
close((int)disc->header);
free(disc);
2012-01-21 21:57:41 +01:00
return;
}
wbfs_close_disc(disc);
WBFS_Ext_ClosePart(part);
}
s32 WBFS_Ext_DiskSpace(f32 *used, f32 *free)
{
*used = 0;
*free = 0;
static s32 wbfs_ext_vfs_have = 0, wbfs_ext_vfs_lba = 0, wbfs_ext_vfs_dev = 0;
2012-01-21 21:57:41 +01:00
// statvfs is slow, so cache values
if (!wbfs_ext_vfs_have || wbfs_ext_vfs_lba != (s32)wbfs_part_lba || wbfs_ext_vfs_dev != wbfsDev )
2012-01-21 21:57:41 +01:00
{
if(statvfs(wbfs_fs_drive, &wbfs_ext_vfs))
return 0;
wbfs_ext_vfs_have = 1;
wbfs_ext_vfs_lba = wbfs_part_lba;
wbfs_ext_vfs_dev = wbfsDev;
}
/* FS size in GB */
f32 size = (f32)wbfs_ext_vfs.f_frsize * (f32)wbfs_ext_vfs.f_blocks / GB_SIZE;
*free = (f32)wbfs_ext_vfs.f_frsize * (f32)wbfs_ext_vfs.f_bfree / GB_SIZE;
*used = size - *free;
return 0;
}
wbfs_t* WBFS_Ext_OpenPart(char *fname)
{
bool sd = strstr(fname, "sd") != NULL;
2012-01-21 21:57:41 +01:00
if(split_open(&split, fname) < 0)
return NULL;
wbfs_set_force_mode(1);
wbfs_t *part = wbfs_open_partition(split_read_sector, 0, //readonly //split_write_sector,
&split, sd ? 512 : USBStorage2_GetSectorSize(), split.total_sec, 0, 0);
wbfs_set_force_mode(0);
2012-01-21 21:57:41 +01:00
if (!part)
split_close(&split);
2012-01-21 21:57:41 +01:00
return part;
}
void WBFS_Ext_ClosePart(wbfs_t* part)
{
if (!part) return;
split_info_t *s = (split_info_t*)part->callback_data;
wbfs_close(part);
if (s) split_close(s);
}
s32 WBFS_Ext_RemoveGame(u8 *discid, char *gamepath)
{
DIR *dir_iter;
struct dirent *ent;
char file[MAX_FAT_PATH];
char folder[MAX_FAT_PATH];
STRCOPY(folder, gamepath);
char *p = strrchr(folder, '/');
if(p) *p = 0;
2012-01-21 21:57:41 +01:00
dir_iter = opendir(folder);
if (!dir_iter) return 0;
while((ent = readdir(dir_iter)) != NULL)
2012-01-21 21:57:41 +01:00
{
if(strstr(ent->d_name, (char*)discid) != NULL)
{
snprintf(file, sizeof(file), "%s/%s", folder, ent->d_name);
remove(file);
}
2012-01-21 21:57:41 +01:00
}
closedir(dir_iter);
if(strlen(folder) > 11)
unlink(folder);
2012-01-21 21:57:41 +01:00
return 0;
}
s32 WBFS_Ext_AddGame(progress_callback_t spinner, void *spinner_data)
{
struct discHdr header ATTRIBUTE_ALIGN(32);
char *illegal = "\"*/:<>?\\|";
char *cp;
char *cleantitle;
2012-01-21 21:57:41 +01:00
char folder[MAX_FAT_PATH];
memset(folder, 0, MAX_FAT_PATH);
2012-01-21 21:57:41 +01:00
char gamepath[MAX_FAT_PATH];
memset(gamepath, 0, MAX_FAT_PATH);
2012-01-21 21:57:41 +01:00
Disc_ReadHeader(&header);
asprintf(&cleantitle, header.title);
2012-08-17 17:48:11 +02:00
for(cp = strpbrk(cleantitle, illegal); cp; cp = strpbrk(cp, illegal))
*cp = '_';
2013-02-18 13:05:18 +01:00
strncpy(wbfs_drive_tmp, wbfs_fs_drive, strlen(wbfs_fs_drive) - 1);
strncpy(folder, fmt(wii_games_dir, wbfs_drive_tmp), sizeof(folder));
2012-08-17 17:48:11 +02:00
fsop_MakeFolder(folder);
strncpy(folder, fmt("%s/%s [%s]", folder, cleantitle, header.id), sizeof(folder));
2012-08-17 17:48:11 +02:00
fsop_MakeFolder(folder);
free(cleantitle);
2012-01-21 21:57:41 +01:00
strncpy(gamepath, fmt("%s/%s.wbfs", folder, header.id), sizeof(gamepath));
2012-01-21 21:57:41 +01:00
u64 size = (u64)143432*2*0x8000ULL;
u32 n_sector = size / 512;
if(split_create(&split, gamepath, OPT_split_size, size, true))
return -1;
//force create first file
u32 scnt = 0;
if (split_get_file(&split, 0, &scnt, 0) < 0)
{
split_close(&split);
return -1;
}
wbfs_t *part = wbfs_open_partition(split_read_sector, split_write_sector, &split, 512, n_sector, 0, 1);
2012-01-21 21:57:41 +01:00
if (!part)
{
split_close(&split);
return -1;
}
extern wbfs_t *hdd;
wbfs_t *old_hdd = hdd;
hdd = part; // used by spinner
2012-08-17 17:48:11 +02:00
s32 ret = wbfs_add_disc(part, __WBFS_ReadDVD, NULL, spinner, spinner_data, REMOVE_UPDATE_PARTITION, 0);
2012-01-21 21:57:41 +01:00
hdd = old_hdd;
if(ret == 0) wbfs_trim(part);
WBFS_Ext_ClosePart(part);
if(ret < 0) WBFS_Ext_RemoveGame(NULL, gamepath);
return ret < 0 ? ret : 0;
}
2012-08-17 17:48:11 +02:00
s32 WBFS_Ext_DVD_Size(u64 *comp_size, u64 *real_size)
2012-01-21 21:57:41 +01:00
{
2012-08-17 17:48:11 +02:00
s32 ret;
u32 comp_sec = 0, last_sec = 0;
wbfs_t *part = NULL;
2012-01-21 21:57:41 +01:00
u64 size = (u64)143432*2*0x8000ULL;
u32 n_sector = size / 512;
2012-08-17 17:48:11 +02:00
u32 wii_sec_sz;
2012-01-21 21:57:41 +01:00
// init a temporary dummy part
// as a placeholder for wbfs_size_disc
2012-08-17 17:48:11 +02:00
part = wbfs_open_partition(0, 0, NULL, 512, n_sector, 0, 1);
if(!part)
return -1;
2012-08-17 17:48:11 +02:00
wii_sec_sz = part->wii_sec_sz;
2012-01-21 21:57:41 +01:00
2012-08-17 17:48:11 +02:00
/* Add game to device */
ret = wbfs_size_disc(part, __WBFS_ReadDVD, NULL, REMOVE_UPDATE_PARTITION, &comp_sec, &last_sec);
2012-01-21 21:57:41 +01:00
wbfs_close(part);
2012-08-17 17:48:11 +02:00
if(ret < 0)
return ret;
2012-01-21 21:57:41 +01:00
2012-08-17 17:48:11 +02:00
*comp_size = (u64)wii_sec_sz * comp_sec;
*real_size = (u64)wii_sec_sz * last_sec;
2012-01-21 21:57:41 +01:00
return 0;
}