-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
212 lines (180 loc) · 3.74 KB
/
main.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
/*
* Copyright (C) 2011-2020 Jeff Kent <jeff@jkent.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <asm/types.h>
#include <baremetal/cache.h>
#include <baremetal/crc32.h>
#include <baremetal/linker.h>
#include <baremetal/mmu.h>
#include <driver/uart.h>
static void mem_write(void);
static void mem_read(void);
static void write_u8();
static void write_u16();
static void write_u32();
static void read_u8();
static void read_u16();
static void read_u32();
static void run(u32 exec_at);
static void run_kernel(u32 exec_at, u32 machine_type);
enum cmd_commands {
cmd_nop = 0,
cmd_write_u8,
cmd_write_u16,
cmd_write_u32,
cmd_read_u8,
cmd_read_u16,
cmd_read_u32,
cmd_mem_write,
cmd_mem_read,
cmd_run,
cmd_run_kernel,
END_OF_COMMANDS
};
int main(void)
{
init_crc32_table();
/* signal we are here */
uart0_writel(0x4e4f4d75); /* uMON */
while (1) {
u8 command = uart0_readb();
switch (command) {
case cmd_nop:
break;
case cmd_write_u8:
write_u8();
break;
case cmd_write_u16:
write_u16();
break;
case cmd_write_u32:
write_u32();
break;
case cmd_read_u8:
read_u8();
break;
case cmd_read_u16:
read_u16();
break;
case cmd_read_u32:
read_u32();
break;
case cmd_mem_write:
mem_write();
break;
case cmd_mem_read:
mem_read();
break;
case cmd_run:
run(uart0_readl());
break;
case cmd_run_kernel:
run_kernel(uart0_readl(), uart0_readl());
break;
}
}
return 0;
}
static void read_u8(void)
{
u8 *addr = (u8 *)uart0_readl();
uart0_writeb(*addr);
}
static void read_u16(void)
{
u16 *addr = (u16 *)uart0_readl();
uart0_writew(*addr);
}
static void read_u32(void)
{
u32 *addr = (u32 *)uart0_readl();
uart0_writel(*addr);
}
static void write_u8(void)
{
u8 *addr = (u8 *)uart0_readl();
*addr = uart0_readb();
}
static void write_u16(void)
{
u16 *addr = (u16 *)uart0_readl();
*addr = uart0_readw();
}
static void write_u32(void)
{
u32 *addr = (u32 *)uart0_readl();
*addr = uart0_readl();
}
static void mem_write(void)
{
u8 *addr, *p;
u32 size;
u32 crc = 0;
addr = (u8 *)uart0_readl();
size = uart0_readl();
p = addr;
while ((u32)addr + size > (u32)p) {
*p = uart0_readb();
crc = crc32(crc, *p);
p++;
}
uart0_writel(crc);
}
static void mem_read(void)
{
u8 *addr, *p;
u32 size;
u32 crc = 0;
addr = (u8 *)uart0_readl();
size = uart0_readl();
p = addr;
while ((u32)addr + size > (u32)p) {
uart0_writeb(*p);
crc = crc32(crc, *p);
p++;
}
uart0_writel(crc);
}
static void run(u32 exec_at)
{
#if defined(CONFIG_BAREMETAL_DCACHE)
dcache_disable();
#endif
#if defined(CONFIG_BAREMETAL_ICACHE)
icache_disable();
#endif
#if defined(CONFIG_BAREMETAL_MMU)
mmu_disable();
#endif
((void(*)())exec_at)();
}
static void run_kernel(u32 exec_at, u32 machine_type)
{
#if defined(CONFIG_BAREMETAL_DCACHE)
dcache_disable();
#endif
#if defined(CONFIG_BAREMETAL_ICACHE)
icache_disable();
#endif
#if defined(CONFIG_BAREMETAL_MMU)
mmu_disable();
#endif
void (*kernel)(int zero, int arch, u32 params);
u32 param_at = (u32)-1;
kernel = (void (*)(int, int, u32))exec_at;
kernel(0, machine_type, param_at);
}