-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.c
233 lines (216 loc) · 4.85 KB
/
interface.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "interface.h"
#include <stdint.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
extern "C" {
//------------------------------
// LIBC SUPPORT STUBS
//------------------------------
extern void* __ram_end__;
void* _sbrk(uint32_t increment)
{
extern uint8_t *end;
static uint8_t *heap_end = 0;
uint8_t *prev_heap_end;
if (heap_end == 0)
{
heap_end =(uint8_t*) &end;
}
prev_heap_end = heap_end;
heap_end += increment;
return prev_heap_end;
}
int _isatty(int fd)
{
return 1;
if (fd == 0)
{
return 1;
}
return 0;
}
int _open(const char* path, int flags, ...)
{
return -1;
}
int _write(int fd, const void *buf, uint32_t count)
{
k_write(1, (const uint8_t*) buf, count);
return count;
}
int _close(int fd)
{
return -1;
}
int _fstat(int fd, struct stat *buf)
{
return -1;
}
int _lseek(int fd, uint32_t offset, int whence)
{
return -1;
}
int _read(int fd, void *buf, uint32_t count)
{
return k_read(fd, (uint8_t*) buf, count);
}
void _exit()
{
}
void abort()
{
while(666);
}
int _getpid()
{
return 0;
}
int _kill(pid_t pid, int sig)
{
return -1;
}
/*
void _fini()
{
}
*/
//------------------------------
// _start and _start2 entrypoints
//------------------------------
extern uint32_t _sfixed;
extern uint32_t _efixed;
extern uint32_t _etext;
extern uint32_t _srelocate;
extern uint32_t _erelocate;
extern uint32_t _szero;
extern uint32_t _ezero;
extern uint32_t _sstack;
extern uint32_t _estack;
void *__dso_handle = 0;
extern void __libc_init_array();
extern int main();
void _init() {
}
void _start2();
void __attribute__(( naked , used )) _start()
{
asm volatile("ldr r0, =_start2\n\t"
"ldr r1, =_estack\n\t"
"bx lr" : : : "r0", "r1" );
}
/* This function is not intended to return! */
void __attribute__((used)) _start2()
{
//asm volatile(" LDR sp, =_estack");
uint32_t *pSrc, *pDest;
/* Move the relocate segment */
pSrc = &_etext;
pDest = &_srelocate;
if (pSrc != pDest) for (; pDest < &_erelocate;) *pDest++ = *pSrc++;
/* Clear the zero segment */
for (pDest = &_szero; pDest < &_ezero;) *pDest++ = 0;
__libc_init_array();
#if 0
char* ptr;
//This is a libc sanity check, many problems with malloc/stacks
//and weak symbol overrides are caught by this
printf("==[TESTSUITE_STARTING]==\n");
{
int x = 5;
int y = -5;
float z = 2.34;
printf("Test printf\n");
printf(" > %d, %d, %f\n", x, y, z);
}
{
printf("Test malloc\n");
ptr = (char*)malloc(512);
printf(" > 0x%08x\n", (unsigned int)ptr);
if (ptr == 0)
{
printf("Malloc fail\n");
while(1);
}
}
{
unsigned int x = 5;
int y = -5;
float z = 2.34;
char *s = "hello world";
printf("Test snprintf\n");
snprintf(ptr, 500, "%u %d %.2f '%s'\n", x, y, z, (char*)s);
printf(" > %s\n", ptr);
}
{
unsigned int x = 5;
int y = -5;
float z = 2.34;
char *s = "hello world";
char stackbuf[512];
printf("Test stack snprintf\n");
snprintf(stackbuf, 500, "%u %d %.2f '%s'\n", x, y, z, (char*)s);
printf(" > %s\n", stackbuf);
}
{
char* rv;
printf("Test gets\n");
rv = gets(ptr);
printf(" > 0x%08x '%s'\n", (uint32_t) rv, rv);
}
{
char* rv;
printf("Test gets2\n");
rv = gets(ptr);
printf(" > 0x%08x '%s'\n", (uint32_t) rv, rv);
}
{
char stackbuf[512];
char* rv;
printf("Test stack gets\n");
rv = gets(stackbuf);
printf(" > 0x%08x '%s'\n", (uint32_t) rv, rv);
}
printf("==[TESTSUITE COMPLETE]==\n");
#endif
printf("Booting payload 4.0:\n");
main();
// It's not technically the end of the world if main exits
// we would still execute any callbacks that it asked for
// don't _fini here obviously.
return;
}
//------------------------------
// Kernel ABI functions
//------------------------------
uint32_t __attribute__((naked)) k_get_kernel_version()
{
__syscall_body(ABI_ID_GET_KERNEL_VERSION);
}
int32_t __attribute__((naked)) k_write(uint32_t fd, uint8_t const *src, uint32_t size)
{
__syscall_body(ABI_ID_WRITE);
}
void __attribute__((naked)) k_yield()
{
__syscall_body(ABI_ID_YIELD);
}
int32_t __attribute__((naked)) k_read(uint32_t fd, uint8_t *dst, uint32_t size)
{
__syscall_body(ABI_ID_READ);
}
int32_t __attribute__((naked)) k_read_async(uint32_t fd, uint8_t *dst, uint32_t size, cb_i32_t cb, void* r)
{
__syscall_body(ABI_ID_READ_ASYNC);
}
uint8_t __attribute__((naked)) k_run_callback()
{
__syscall_body(ABI_ID_RUN_CALLBACK);
}
void __attribute__((naked)) k_wait_callback()
{
__syscall_body(ABI_ID_WAIT_CALLBACK);
}
}