Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/ianos/ianos.c
3694 views
1
/*
2
* Copyright (c) 2018 M4xw
3
* Copyright (c) 2018-2026 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 <storage/sd.h>
25
#include <utils/types.h>
26
27
#include <gfx_utils.h>
28
29
extern heap_t _heap;
30
31
static bdk_params_t _bdk_params = {
32
.gfx_con = (void *)&gfx_con,
33
.gfx_ctx = (void *)&gfx_ctxt,
34
.heap = &_heap,
35
.memcpy = (memcpy_t)&memcpy,
36
.memset = (memset_t)&memset,
37
38
.extension_magic = 0
39
};
40
41
static void *_ianos_alloc_cb(el_ctx *ctx, Elf_Addr phys, Elf_Addr virt, Elf_Addr size)
42
{
43
return (void *)virt;
44
}
45
46
static el_status _ianos_read_cb(el_ctx *ctx, void *dest, size_t nb, size_t offset)
47
{
48
memcpy(dest, (void *)(ctx->eaddr + offset), nb);
49
50
return EL_OK;
51
}
52
53
//TODO: Support shared libraries.
54
int ianos_loader(ianos_lib_t *lib, char *path)
55
{
56
el_ctx ctx;
57
lib->buf = NULL;
58
if (!lib->bdk)
59
lib->bdk = &_bdk_params;
60
61
// Read library.
62
ctx.eaddr = (Elf_Addr)sd_file_read(path, NULL);
63
if (!ctx.eaddr)
64
goto error;
65
66
ctx.pread = _ianos_read_cb;
67
68
if (el_init(&ctx))
69
goto error;
70
71
if (lib->type & IA_SHARED_LIB)
72
goto error; // No support for shared libs now.
73
74
// Set our relocated library's buffer.
75
switch (lib->type & ~IA_SHARED_LIB)
76
{
77
case IA_DRAM_LIB:
78
lib->buf = malloc(ctx.memsz); // Aligned to 0x10 by default.
79
break;
80
81
case IA_IRAM_LIB:
82
break;
83
84
case IA_AUTO_LIB: // Default to DRAM for now.
85
default:
86
lib->buf = malloc(ctx.memsz); // Aligned to 0x10 by default.
87
break;
88
}
89
90
if (!lib->buf)
91
goto error;
92
93
// Load and relocate library.
94
ctx.base_load_vaddr = ctx.base_load_paddr = (Elf_Addr)lib->buf;
95
if (el_load(&ctx, _ianos_alloc_cb))
96
goto error;
97
98
if (el_relocate(&ctx))
99
goto error;
100
101
free((void *)ctx.eaddr);
102
103
// Launch.
104
Elf_Addr epaddr = ctx.ehdr.e_entry + (Elf_Addr)lib->buf;
105
moduleEntrypoint ep = (moduleEntrypoint)epaddr;
106
ep(lib->private, lib->bdk);
107
108
return 0;
109
110
error:
111
free((void *)ctx.eaddr);
112
free(lib->buf);
113
114
return 1;
115
}
116
117
uintptr_t ianos_static_module(char *path, void *private)
118
{
119
el_ctx ctx;
120
Elf_Addr buf = 0;
121
Elf_Addr epaddr = 0;
122
123
// Read library.
124
ctx.eaddr = (Elf_Addr)sd_file_read(path, NULL);
125
if (!ctx.eaddr)
126
goto error;
127
128
ctx.pread = _ianos_read_cb;
129
130
// Initialize elfload context.
131
if (el_init(&ctx))
132
goto error;
133
134
// Set our relocated library's buffer.
135
buf = (Elf_Addr)malloc(ctx.memsz); // Aligned to 0x10 by default.
136
if (!buf)
137
goto error;
138
139
// Load and relocate library.
140
ctx.base_load_vaddr = ctx.base_load_paddr = buf;
141
if (el_load(&ctx, _ianos_alloc_cb))
142
goto error;
143
144
if (el_relocate(&ctx))
145
goto error;
146
147
free((void *)ctx.eaddr);
148
149
// Launch.
150
epaddr = ctx.ehdr.e_entry + buf;
151
moduleEntrypoint ep = (moduleEntrypoint)epaddr;
152
ep(private, &_bdk_params);
153
154
return (uintptr_t)epaddr;
155
156
error:
157
free((void *)ctx.eaddr);
158
free((void *)buf);
159
160
return 0;
161
}
162
163