-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.c
180 lines (154 loc) · 3.77 KB
/
loader.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
#include <sys/stdbool.h>
#include <sys/reboot.h>
#include <stddef.h>
#include <stdint.h>
#include "serial.h"
#include "loader.h"
#include "lcd.h"
#define LINE_LEN 20
#define CHUNK_SIZE 100
#define RESET_VEC ((void *)0xA0000000)
#define _STRINGIFY(a) #a
#define STRINGIFY(a) _STRINGIFY(a)
static void go(void *addr) {
(*(void (*)(void))addr)();
}
static void reboot(void) {
go(RESET_VEC);
}
static size_t lengthFromDecimalString(int len, const char *buf) {
size_t result = 0;
for(int i = 0; i < len && buf[i]; i++) {
result *= 10;
result += buf[i] - '0';
}
return result;
}
static int fromhexnibble(char c) {
if (c >= '0' && c <= '9')
return c - '0';
else if (c >= 'a' && c <= 'f')
return c - 'a';
else if (c >= 'A' && c <= 'F')
return c - 'A';
else
return 0;
}
static char nibbletohex(char nibble) {
if (nibble <= 9)
return nibble + '0';
else
return nibble - 0xA + 'A';
}
static void printhex(char byte) {
serial_putchar(nibbletohex((byte >> 4) & 0xF));
serial_putchar(nibbletohex(byte & 0xF));
}
static void displayHexOnLCD(uint32_t p) {
char string[8];
for(int i = 0; i < sizeof(p)*2; i++) {
string[i] = nibbletohex(p >> ((sizeof(p)*8)-4));
p <<= 4;
}
lcd_print(string);
}
static void *addressFromHexString(int len, const char *buf) {
unsigned long result = 0;
for(int i = 0; i < len && buf[i]; i++) {
result <<= 4;
result += fromhexnibble(buf[i]);
}
return (void *)result;
}
static void bootKernel(void) {
serial_print("Commencing boot...\n\r");
go(LOAD_ADDR);
}
static void bootAddress(void) {
char line[LINE_LEN];
serial_print("Enter eight hex digit address: ");
int len = serial_getline(line, LINE_LEN);
void *l = addressFromHexString(len, line);
serial_print("Booting to ");
for(int i = 4; i > 0; i--) {
printhex(((int)l >> i) & 0xFF);
}
serial_print("...\n\r");
go(l);
}
static void loadFromSerialToAddress(void *addr) {
char line[LINE_LEN];
serial_print("Enter kernel size in bytes: ");
int len = serial_getline(line, LINE_LEN);
size_t size = lengthFromDecimalString(len, line);
displayHexOnLCD(size);
serial_print("Send kernel when ready.\n\r");
char *zone = (char *)addr;
//serial_putchar('.');
for(size_t o = 0; o < size; o++) {
displayHexOnLCD((uint32_t)(zone+o));
zone[o] = serial_getchar();
/*
if (o % CHUNK_SIZE == 0) {
serial_putchar('.');
}
*/
}
serial_print("\n\rDone loading.\n\r");
}
static void loadFromSerial(void) {
loadFromSerialToAddress(LOAD_ADDR);
bootKernel();
}
static void loadFromSD(void) {
serial_print("Booting from SD cards is not yet implemented.\n\r");
}
static void showKernel(void) {
const char *kernel = LOAD_ADDR;
for(int i = 0; i < 0xff; i++) {
if (i % 0xf == 0)
serial_print("\n\r");
printhex(kernel[i]);
}
}
static const void (*options[])(void) = {
NULL,
loadFromSerial,
loadFromSD,
reboot,
bootKernel,
bootAddress,
showKernel,
};
int main(void) {
lcd_print("Bootload");
serial_init();
serial_print("\n\r");
serial_print(">> NetBSD/sh3 Serial & SD Bootloader.\n\r");
serial_print(">> Built on " __DATE__ " " __TIME__ "\n\r");
char selection;
for(;;) {
/* Print the options */
serial_print("\n\r"
"\t1. Load kernel via serial\n\r"
"\t2. Boot from SD Card\n\r"
"\t3. Reboot\n\r"
"\t4. Boot to " STRINGIFY(LOAD_ADDR) "\n\r"
"\t5. Boot to address\n\r"
"\t6. Show start of kernel\n\r"
"\n\r"
"Choose an Option: ");
/* Get a selection */
selection = serial_getchar();
serial_putchar(selection);
serial_print("\n\r");
/* Act on the selection */
selection -= '0';
serial_print("\n\r");
if (selection > 0 && selection < sizeof(options)/sizeof(*options)) {
options[selection]();
} else {
serial_print("Invalid option.\n\r");
}
}
}