This repository has been archived on 2022-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
Luma3DS-3GX/haxloader/source/main.c

92 lines
2.9 KiB
C
Raw Normal View History

2016-09-23 18:07:45 +02:00
/*
2016-09-23 18:26:39 +02:00
* This file is part of Luma3DS
2016-09-23 18:07:45 +02:00
* Copyright (C) 2016 Aurora Wright, TuxSH
*
* 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 3 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
2016-09-23 18:26:39 +02:00
* Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified
2016-09-23 18:07:45 +02:00
* reasonable legal notices or author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
*/
#include "memory.h"
#include "cache.h"
2016-09-23 19:23:44 +02:00
#include "i2c.h"
2016-09-23 18:07:45 +02:00
#include "types.h"
#include "fatfs/ff.h"
#include "../../build/bundled.h"
static FATFS fs;
2016-09-23 19:23:44 +02:00
void main(void)
2016-09-23 18:07:45 +02:00
{
if(f_mount(&fs, "0:", 0) == FR_OK)
{
FIL pathFile,
payload;
2016-09-23 18:26:39 +02:00
bool foundPayload = false;
2016-09-23 18:07:45 +02:00
2016-10-17 18:27:48 +02:00
if(f_open(&pathFile, "luma/path.txt", FA_READ) == FR_OK)
2016-09-23 18:07:45 +02:00
{
u32 pathSize = f_size(&pathFile);
if(pathSize > 5 && pathSize < 58)
2016-09-23 18:07:45 +02:00
{
2016-09-23 19:20:42 +02:00
char path[pathSize + 1];
2016-09-23 18:07:45 +02:00
unsigned int read;
f_read(&pathFile, path, pathSize, &read);
if(path[pathSize - 1] == 0xA) pathSize--;
if(path[pathSize - 1] == 0xD) pathSize--;
if(pathSize > 5 && pathSize < 56 && path[0] == '/' && memcmp(&path[pathSize - 4], ".bin", 4) == 0)
2016-09-23 18:07:45 +02:00
{
path[pathSize] = 0;
2016-09-23 18:26:39 +02:00
foundPayload = f_open(&payload, path, FA_READ) == FR_OK;
2016-09-23 18:07:45 +02:00
}
}
f_close(&pathFile);
}
2016-09-23 19:20:42 +02:00
if(!foundPayload) foundPayload = f_open(&payload, "arm9loaderhax.bin", FA_READ) == FR_OK;
2016-09-23 18:07:45 +02:00
2016-09-23 18:26:39 +02:00
if(foundPayload)
2016-09-23 18:07:45 +02:00
{
2016-11-14 20:49:59 +01:00
u32 *loaderAddress = (u32 *)0x24FFFE00;
2016-09-23 18:26:39 +02:00
void *payloadAddress = (void *)0x24F00000;
u32 payloadSize = f_size(&payload);
memcpy(loaderAddress, loader_bin, loader_bin_size);
loaderAddress[1] = payloadSize;
2016-09-23 18:07:45 +02:00
unsigned int read;
f_read(&payload, payloadAddress, payloadSize, &read);
2016-09-23 18:26:39 +02:00
f_close(&payload);
2016-09-23 18:07:45 +02:00
2016-10-17 18:27:48 +02:00
if((u32)read == payloadSize)
{
flushDCacheRange(loaderAddress, loader_bin_size);
flushICacheRange(loaderAddress, loader_bin_size);
2016-09-23 18:07:45 +02:00
2016-10-17 18:27:48 +02:00
((void (*)())loaderAddress)();
}
2016-09-23 18:07:45 +02:00
}
}
2016-09-23 19:23:44 +02:00
//Ensure that all memory transfers have completed and that the data cache has been flushed
flushEntireDCache();
i2cWriteRegister(I2C_DEV_MCU, 0x20, 1 << 0);
2016-09-23 18:07:45 +02:00
while(true);
}