isfshax/stage2/main.c
rw-r-r-0644 99cded2762 lolserial improvements
allow debug output to be suspended, drop assembly version
2021-06-01 13:19:41 +02:00

150 lines
3.2 KiB
C

/*
* ISFSHAX
*
* Copyright (C) 2021 rw-r-r-0644 <rwrr0644@gmail.com>
*
* Based on code from Minute and Mini:
*
* Copyright (C) 2017 Ash Logan <quarktheawesome@gmail.com>
* Copyright (C) 2016 SALT
* Copyright (C) 2016 Daz Jones <daz@dazzozo.com>
*
* Copyright (C) 2008, 2009 Haxx Enterprises <bushing@gmail.com>
* Copyright (C) 2008, 2009 Sven Peter <svenpeter@gmail.com>
* Copyright (C) 2008, 2009 Hector Martin "marcan" <marcan@marcansoft.com>
* Copyright (C) 2009 Andre Heider "dhewg" <dhewg@wiibrew.org>
* Copyright (C) 2009 John Kelley <wiidev@kelley.ca>
*
* 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 <stdlib.h>
#include "memory.h"
#include "irq.h"
#include "crypto.h"
#include "smc.h"
#include "latte.h"
#include "utils.h"
#include "isfs/isfshax.h"
#include "sdcard.h"
#include "fatfs/ff.h"
#include "nand.h"
#include "isfs/isfs.h"
#include "ancast.h"
#include "debug.h"
#include "lolserial.h"
u32 load_payload_sd(void)
{
static FATFS fatfs;
UINT btr, br;
FRESULT res;
FIL file;
u32 vector = 0;
ancast_iop_clear((u8*)ANCAST_ADDRESS_IOP);
sdcard_init();
res = f_mount(&fatfs, "0:", 1);
if (res)
goto error_mount;
res = f_open(&file, "isfshax.bin", FA_READ);
if (res)
goto error_open;
btr = f_size(&file);
if (!btr)
goto error_read;
res = f_read(&file, (u8*)ANCAST_ADDRESS_IOP, btr, &br);
if (res || (btr != br))
goto error_read;
vector = ancast_iop_load((u8*)ANCAST_ADDRESS_IOP, btr);
error_read:
f_close(&file);
error_open:
f_mount(0, "0:", 0);
error_mount:
sdcard_exit();
return vector;
}
u32 load_payload_nand(void)
{
isfs_file file;
size_t btr, br;
int res;
u32 vector = 0;
ancast_iop_clear((u8*)ANCAST_ADDRESS_IOP);
res = isfs_init();
if (res)
return vector;
res = isfs_open(&file, "slc:/sys/isfshax.bin");
if (res)
goto error_open;
btr = file.fst->size;
if (!btr)
goto error_read;
res = isfs_read(&file, (u8*)ANCAST_ADDRESS_IOP, btr, &br);
if (res || (btr != br))
goto error_read;
vector = ancast_iop_load((u8*)ANCAST_ADDRESS_IOP, btr);
error_read:
isfs_close(&file);
error_open:
isfs_fini();
return vector;
}
u32 _main(void)
{
u32 vector = 0;
lolserial_init();
DEBUG("isfshax start\n");
//mem_initialize();
irq_initialize();
crypto_read_otp();
nand_initialize();
/* repair isfshax superblocks ecc errors, if present */
DEBUG("isfshax_refresh\n");
isfshax_refresh();
/* attempt to load the payload from SD, then NAND */
DEBUG("load_payload_sd\n");
lolserial_suspend();
vector = load_payload_sd();
lolserial_resume();
if (!vector) {
DEBUG("load_payload_nand\n");
lolserial_suspend();
vector = load_payload_nand();
lolserial_resume();
}
DEBUG("vector: %08lX\n", vector);
nand_deinitialize();
irq_shutdown();
//mem_shutdown();
/* failed to load the payload from SD or NAND -> shutdown */
if (!vector)
smc_shutdown(false);
return vector;
}