2018-08-21 02:12:03 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 M4xw
|
2019-06-30 01:49:33 +01:00
|
|
|
* Copyright (c) 2018-2019 CTCaer
|
2018-08-21 02:12:03 +01:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "ianos.h"
|
2020-06-14 14:45:45 +01:00
|
|
|
#include "elfload/elfload.h"
|
|
|
|
#include <module.h>
|
|
|
|
#include <mem/heap.h>
|
2021-04-11 08:18:47 +01:00
|
|
|
#include <power/max7762x.h>
|
2020-06-14 14:45:45 +01:00
|
|
|
#include <storage/nx_sd.h>
|
|
|
|
#include <utils/types.h>
|
|
|
|
|
|
|
|
#include <gfx_utils.h>
|
2018-08-21 02:12:03 +01:00
|
|
|
|
|
|
|
#define IRAM_LIB_ADDR 0x4002B000
|
|
|
|
#define DRAM_LIB_ADDR 0xE0000000
|
|
|
|
|
|
|
|
extern heap_t _heap;
|
|
|
|
|
|
|
|
void *elfBuf = NULL;
|
|
|
|
void *fileBuf = NULL;
|
|
|
|
|
|
|
|
static void _ianos_call_ep(moduleEntrypoint_t entrypoint, void *moduleConfig)
|
|
|
|
{
|
|
|
|
bdkParams_t bdkParameters = (bdkParams_t)malloc(sizeof(struct _bdkParams_t));
|
2020-06-14 14:45:45 +01:00
|
|
|
bdkParameters->gfxCon = (void *)&gfx_con;
|
|
|
|
bdkParameters->gfxCtx = (void *)&gfx_ctxt;
|
2018-08-21 02:12:03 +01:00
|
|
|
bdkParameters->memcpy = (memcpy_t)&memcpy;
|
|
|
|
bdkParameters->memset = (memset_t)&memset;
|
|
|
|
bdkParameters->sharedHeap = &_heap;
|
|
|
|
|
2021-04-11 08:18:47 +01:00
|
|
|
// Extra functions.
|
2021-04-11 08:30:24 +01:00
|
|
|
bdkParameters->extension_magic = IANOS_EXT0;
|
2021-04-11 08:18:47 +01:00
|
|
|
bdkParameters->reg_voltage_set = (reg_voltage_set_t)&max7762x_regulator_set_voltage;
|
|
|
|
|
2018-08-21 02:12:03 +01:00
|
|
|
entrypoint(moduleConfig, bdkParameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *_ianos_alloc_cb(el_ctx *ctx, Elf_Addr phys, Elf_Addr virt, Elf_Addr size)
|
|
|
|
{
|
|
|
|
(void)ctx;
|
|
|
|
(void)phys;
|
|
|
|
(void)size;
|
|
|
|
return (void *)virt;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool _ianos_read_cb(el_ctx *ctx, void *dest, size_t numberBytes, size_t offset)
|
|
|
|
{
|
|
|
|
(void)ctx;
|
|
|
|
|
|
|
|
memcpy(dest, fileBuf + offset, numberBytes);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: Support shared libraries.
|
2020-06-14 11:17:26 +01:00
|
|
|
uintptr_t ianos_loader(char *path, elfType_t type, void *moduleConfig)
|
2018-08-21 02:12:03 +01:00
|
|
|
{
|
2020-06-14 11:17:26 +01:00
|
|
|
el_ctx ctx;
|
2019-06-30 01:49:33 +01:00
|
|
|
uintptr_t epaddr = 0;
|
2018-08-21 02:12:03 +01:00
|
|
|
|
2020-06-14 11:17:26 +01:00
|
|
|
if (!sd_mount())
|
|
|
|
goto elfLoadFinalOut;
|
2018-08-21 02:12:03 +01:00
|
|
|
|
2020-06-14 11:17:26 +01:00
|
|
|
// Read library.
|
2019-03-16 21:35:43 +00:00
|
|
|
fileBuf = sd_file_read(path, NULL);
|
2018-08-21 02:12:03 +01:00
|
|
|
|
|
|
|
if (!fileBuf)
|
|
|
|
goto elfLoadFinalOut;
|
|
|
|
|
|
|
|
ctx.pread = _ianos_read_cb;
|
|
|
|
|
|
|
|
if (el_init(&ctx))
|
|
|
|
goto elfLoadFinalOut;
|
|
|
|
|
|
|
|
// Set our relocated library's buffer.
|
|
|
|
switch (type & 0xFFFF)
|
|
|
|
{
|
|
|
|
case EXEC_ELF:
|
|
|
|
case AR64_ELF:
|
|
|
|
elfBuf = (void *)DRAM_LIB_ADDR;
|
|
|
|
break;
|
|
|
|
default:
|
2019-08-28 00:08:12 +01:00
|
|
|
elfBuf = malloc(ctx.memsz); // Aligned to 0x10 by default.
|
2018-08-21 02:12:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!elfBuf)
|
|
|
|
goto elfLoadFinalOut;
|
|
|
|
|
|
|
|
// Load and relocate library.
|
|
|
|
ctx.base_load_vaddr = ctx.base_load_paddr = (uintptr_t)elfBuf;
|
|
|
|
if (el_load(&ctx, _ianos_alloc_cb))
|
|
|
|
goto elfFreeOut;
|
|
|
|
|
|
|
|
if (el_relocate(&ctx))
|
|
|
|
goto elfFreeOut;
|
|
|
|
|
|
|
|
// Launch.
|
2019-06-30 01:49:33 +01:00
|
|
|
epaddr = ctx.ehdr.e_entry + (uintptr_t)elfBuf;
|
2018-08-21 02:12:03 +01:00
|
|
|
moduleEntrypoint_t ep = (moduleEntrypoint_t)epaddr;
|
|
|
|
|
|
|
|
_ianos_call_ep(ep, moduleConfig);
|
|
|
|
|
|
|
|
elfFreeOut:
|
|
|
|
free(fileBuf);
|
|
|
|
elfBuf = NULL;
|
|
|
|
fileBuf = NULL;
|
|
|
|
|
|
|
|
elfLoadFinalOut:
|
2019-06-30 01:49:33 +01:00
|
|
|
return epaddr;
|
2018-08-21 02:12:03 +01:00
|
|
|
}
|