summaryrefslogtreecommitdiffstats
path: root/arch/ppc/coffboot/start.c
blob: 6fc1fc1fbcdce56625be2056ac4df801fa8e08a0 (plain)
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
 * Copyright (C) Paul Mackerras 1997.
 *
 * 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.
 */
#include <stdarg.h>

int (*prom)();

void *chosen_handle;
void *stdin;
void *stdout;
void *stderr;

void exit(void);
void *finddevice(const char *name);
int getprop(void *phandle, const char *name, void *buf, int buflen);
void printk(char *fmt, ...);

void
start(int a1, int a2, void *promptr)
{
    prom = (int (*)()) promptr;
    chosen_handle = finddevice("/chosen");
    if (chosen_handle == (void *) -1)
	exit();
    if (getprop(chosen_handle, "stdout", &stdout, sizeof(stdout)) != 4)
	exit();
    stderr = stdout;
    if (getprop(chosen_handle, "stdin", &stdin, sizeof(stdin)) != 4)
	exit();

    boot(a1, a2, promptr);
    for (;;)
	exit();
}

int
write(void *handle, void *ptr, int nb)
{
    struct prom_args {
	char *service;
	int nargs;
	int nret;
	void *ihandle;
	void *addr;
	int len;
	int actual;
    } args;

    args.service = "write";
    args.nargs = 3;
    args.nret = 1;
    args.ihandle = handle;
    args.addr = ptr;
    args.len = nb;
    args.actual = -1;
    (*prom)(&args);
    return args.actual;
}

int writestring(void *f, char *ptr, int nb)
{
	int w = 0, i;
	char *ret = "\r";

	for (i = 0; i < nb; ++i) {
		if (ptr[i] == '\n') {
			if (i > w) {
				write(f, ptr + w, i - w);
				w = i;
			}
			write(f, ret, 1);
		}
	}
	if (w < nb)
		write(f, ptr + w, nb - w);
	return nb;
}

int
read(void *handle, void *ptr, int nb)
{
    struct prom_args {
	char *service;
	int nargs;
	int nret;
	void *ihandle;
	void *addr;
	int len;
	int actual;
    } args;

    args.service = "read";
    args.nargs = 3;
    args.nret = 1;
    args.ihandle = handle;
    args.addr = ptr;
    args.len = nb;
    args.actual = -1;
    (*prom)(&args);
    return args.actual;
}

void
exit()
{
    struct prom_args {
	char *service;
    } args;

    for (;;) {
	args.service = "exit";
	(*prom)(&args);
    }
}

void
pause()
{
    struct prom_args {
	char *service;
    } args;

    args.service = "enter";
    (*prom)(&args);
}

void *
finddevice(const char *name)
{
    struct prom_args {
	char *service;
	int nargs;
	int nret;
	const char *devspec;
	void *phandle;
    } args;

    args.service = "finddevice";
    args.nargs = 1;
    args.nret = 1;
    args.devspec = name;
    args.phandle = (void *) -1;
    (*prom)(&args);
    return args.phandle;
}

void *
claim(unsigned int virt, unsigned int size, unsigned int align)
{
    struct prom_args {
	char *service;
	int nargs;
	int nret;
	unsigned int virt;
	unsigned int size;
	unsigned int align;
	void *ret;
    } args;

    args.service = "claim";
    args.nargs = 3;
    args.nret = 1;
    args.virt = virt;
    args.size = size;
    args.align = align;
    (*prom)(&args);
    return args.ret;
}

int
getprop(void *phandle, const char *name, void *buf, int buflen)
{
    struct prom_args {
	char *service;
	int nargs;
	int nret;
	void *phandle;
	const char *name;
	void *buf;
	int buflen;
	int size;
    } args;

    args.service = "getprop";
    args.nargs = 4;
    args.nret = 1;
    args.phandle = phandle;
    args.name = name;
    args.buf = buf;
    args.buflen = buflen;
    args.size = -1;
    (*prom)(&args);
    return args.size;
}

int
putc(int c, void *f)
{
    char ch = c;

    return writestring(f, &ch, 1) == 1? c: -1;
}

int
putchar(int c)
{
    return putc(c, stdout);
}

int
fputs(char *str, void *f)
{
    int n = strlen(str);

    return writestring(f, str, n) == n? 0: -1;
}

int
readchar()
{
    char ch;

    for (;;) {
	switch (read(stdin, &ch, 1)) {
	case 1:
	    return ch;
	case -1:
	    printk("read(stdin) returned -1\n");
	    return -1;
	}
    }
}

static char line[256];
static char *lineptr;
static int lineleft;

int
getchar()
{
    int c;

    if (lineleft == 0) {
	lineptr = line;
	for (;;) {
	    c = readchar();
	    if (c == -1 || c == 4)
		break;
	    if (c == '\r' || c == '\n') {
		*lineptr++ = '\n';
		putchar('\n');
		break;
	    }
	    switch (c) {
	    case 0177:
	    case '\b':
		if (lineptr > line) {
		    putchar('\b');
		    putchar(' ');
		    putchar('\b');
		    --lineptr;
		}
		break;
	    case 'U' & 0x1F:
		while (lineptr > line) {
		    putchar('\b');
		    putchar(' ');
		    putchar('\b');
		    --lineptr;
		}
		break;
	    default:
		if (lineptr >= &line[sizeof(line) - 1])
		    putchar('\a');
		else {
		    putchar(c);
		    *lineptr++ = c;
		}
	    }
	}
	lineleft = lineptr - line;
	lineptr = line;
    }
    if (lineleft == 0)
	return -1;
    --lineleft;
    return *lineptr++;
}

extern int vsprintf(char *buf, const char *fmt, va_list args);
static char sprint_buf[1024];

void
printk(char *fmt, ...)
{
	va_list args;
	int n;

	va_start(args, fmt);
	n = vsprintf(sprint_buf, fmt, args);
	va_end(args);
	writestring(stdout, sprint_buf, n);
}

int
printf(char *fmt, ...)
{
	va_list args;
	int n;

	va_start(args, fmt);
	n = vsprintf(sprint_buf, fmt, args);
	va_end(args);
	writestring(stdout, sprint_buf, n);
	return n;
}