Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/ianos/ianos.c
1476 views
1
/*
2
* Copyright (c) 2018 M4xw
3
* Copyright (c) 2018-2019 CTCaer
4
*
5
* This program is free software; you can redistribute it and/or modify it
6
* under the terms and conditions of the GNU General Public License,
7
* version 2, as published by the Free Software Foundation.
8
*
9
* This program is distributed in the hope it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12
* more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
*/
17
18
#include <string.h>
19
20
#include "ianos.h"
21
#include "elfload/elfload.h"
22
#include <module.h>
23
#include <mem/heap.h>
24
#include <power/max7762x.h>
25
#include <storage/sd.h>
26
#include <utils/types.h>
27
28
#include <gfx_utils.h>
29
30
#define IRAM_LIB_ADDR 0x4002B000
31
#define DRAM_LIB_ADDR 0xE0000000
32
33
extern heap_t _heap;
34
35
void *elfBuf = NULL;
36
void *fileBuf = NULL;
37
38
static void _ianos_call_ep(moduleEntrypoint_t entrypoint, void *moduleConfig)
39
{
40
bdkParams_t bdkParameters = (bdkParams_t)malloc(sizeof(struct _bdkParams_t));
41
bdkParameters->gfxCon = (void *)&gfx_con;
42
bdkParameters->gfxCtx = (void *)&gfx_ctxt;
43
bdkParameters->memcpy = (memcpy_t)&memcpy;
44
bdkParameters->memset = (memset_t)&memset;
45
bdkParameters->sharedHeap = &_heap;
46
47
// Extra functions.
48
bdkParameters->extension_magic = IANOS_EXT0;
49
bdkParameters->reg_voltage_set = (reg_voltage_set_t)&max7762x_regulator_set_voltage;
50
51
entrypoint(moduleConfig, bdkParameters);
52
}
53
54
static void *_ianos_alloc_cb(el_ctx *ctx, Elf_Addr phys, Elf_Addr virt, Elf_Addr size)
55
{
56
(void)ctx;
57
(void)phys;
58
(void)size;
59
return (void *)virt;
60
}
61
62
static bool _ianos_read_cb(el_ctx *ctx, void *dest, size_t numberBytes, size_t offset)
63
{
64
(void)ctx;
65
66
memcpy(dest, fileBuf + offset, numberBytes);
67
68
return true;
69
}
70
71
//TODO: Support shared libraries.
72
uintptr_t ianos_loader(char *path, elfType_t type, void *moduleConfig)
73
{
74
el_ctx ctx;
75
uintptr_t epaddr = 0;
76
77
// Read library.
78
fileBuf = sd_file_read(path, NULL);
79
80
if (!fileBuf)
81
goto out;
82
83
ctx.pread = _ianos_read_cb;
84
85
if (el_init(&ctx))
86
goto out;
87
88
// Set our relocated library's buffer.
89
switch (type & 0xFFFF)
90
{
91
case EXEC_ELF:
92
case AR64_ELF:
93
elfBuf = (void *)DRAM_LIB_ADDR;
94
break;
95
default:
96
elfBuf = malloc(ctx.memsz); // Aligned to 0x10 by default.
97
}
98
99
if (!elfBuf)
100
goto out;
101
102
// Load and relocate library.
103
ctx.base_load_vaddr = ctx.base_load_paddr = (uintptr_t)elfBuf;
104
if (el_load(&ctx, _ianos_alloc_cb))
105
goto out_free;
106
107
if (el_relocate(&ctx))
108
goto out_free;
109
110
// Launch.
111
epaddr = ctx.ehdr.e_entry + (uintptr_t)elfBuf;
112
moduleEntrypoint_t ep = (moduleEntrypoint_t)epaddr;
113
114
_ianos_call_ep(ep, moduleConfig);
115
116
out_free:
117
free(fileBuf);
118
elfBuf = NULL;
119
fileBuf = NULL;
120
121
out:
122
return epaddr;
123
}
124