2018-09-07 16:00:13 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Atmosphère-NX
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope 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/>.
|
|
|
|
*/
|
|
|
|
|
2018-05-07 00:02:23 +01:00
|
|
|
#include "chainloader.h"
|
|
|
|
|
2018-05-08 15:51:43 +01:00
|
|
|
int g_chainloader_argc = 0;
|
2018-05-07 22:32:45 +01:00
|
|
|
char g_chainloader_arg_data[CHAINLOADER_ARG_DATA_MAX_SIZE] = {0};
|
|
|
|
chainloader_entry_t g_chainloader_entries[CHAINLOADER_MAX_ENTRIES] = {0}; /* keep them sorted */
|
|
|
|
size_t g_chainloader_num_entries = 0;
|
|
|
|
uintptr_t g_chainloader_entrypoint = 0;
|
2018-05-07 00:02:23 +01:00
|
|
|
|
|
|
|
#pragma GCC optimize (3)
|
2018-05-07 22:32:45 +01:00
|
|
|
|
|
|
|
static void *xmemmove(void *dst, const void *src, size_t len)
|
|
|
|
{
|
|
|
|
const uint8_t *src8 = (const uint8_t *)src;
|
|
|
|
uint8_t *dst8 = (uint8_t *)dst;
|
|
|
|
|
|
|
|
if (dst8 < src8) {
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
dst8[i] = src8[i];
|
|
|
|
}
|
2018-07-30 04:21:30 +01:00
|
|
|
} else if (dst8 > src8) {
|
2018-05-07 22:32:45 +01:00
|
|
|
for (size_t i = len; len > 0; len--)
|
|
|
|
dst8[i - 1] = src8[i - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2018-05-08 15:51:43 +01:00
|
|
|
void relocate_and_chainload_main(void) {
|
2018-05-07 22:32:45 +01:00
|
|
|
for(size_t i = 0; i < g_chainloader_num_entries; i++) {
|
|
|
|
chainloader_entry_t *entry = &g_chainloader_entries[i];
|
|
|
|
xmemmove((void *)entry->load_address, (const void *)entry->src_address, entry->size);
|
2018-05-07 00:02:23 +01:00
|
|
|
}
|
|
|
|
|
2018-05-08 15:51:43 +01:00
|
|
|
((void (*)(int, void *))g_chainloader_entrypoint)(g_chainloader_argc, g_chainloader_arg_data);
|
2018-05-07 00:02:23 +01:00
|
|
|
}
|