aboutsummaryrefslogtreecommitdiff
path: root/sys/boot/mips/beri/loader/main.c
blob: 2d201d8011ee921e905f3ac39102013045737d86 (plain) (blame)
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
/*-
 * Copyright (c) 2013-2014 Robert N. M. Watson
 * All rights reserved.
 *
 * This software was developed by SRI International and the University of
 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
 * ("CTSRD"), as part of the DARPA CRASH research programme.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/linker.h>
#include <sys/reboot.h>

#include <machine/bootinfo.h>
#include <machine/elf.h>

#include <stand.h>
#include <bootstrap.h>
#include <loader.h>
#include <mips.h>

#ifdef LOADER_USB_SUPPORT
#include <storage/umass_common.h>
#endif

static int	__elfN(exec)(struct preloaded_file *);
static void	extract_currdev(struct bootinfo *);

struct devsw *devsw[] = {
	&beri_cfi_disk,
	&beri_sdcard_disk,
#ifdef LOADER_USB_SUPPORT
	&umass_disk,
#endif
	NULL
};

struct arch_switch archsw;

struct file_format *file_formats[] = {
	&beri_elf,
	NULL
};

struct fs_ops *file_system[] = {
#ifdef LOADER_UFS_SUPPORT
	&ufs_fsops,
#endif
	NULL
};

struct console *consoles[] = {
	&altera_jtag_uart_console,
	NULL
};

extern void	__bss_start, __bss_end;
extern void	__heap_start, __heap_end;

static int
__elfN(exec)(struct preloaded_file *fp)
{

	return (EFTYPE);
}

/*
 * Capture arguments from boot2 for later reuse when launching the kernel.
 * Note that we choose not to maintain a pointer to boo2_bootinfop after
 * initial argument processing: this is because we might load the kernel over
 * the spot where boot2 was running, so we can't pass that pointer on to the
 * kernel.  To be on the safe side, never reference it outside of the body of
 * main(), instead preserving a copy.
 */
int		 boot2_argc;
char		**boot2_argv;
char		**boot2_envv;

struct bootinfo	boot2_bootinfo;

int
main(int argc, char *argv[], char *envv[], struct bootinfo *bootinfop)
{
	struct devsw **dp;

	/* NB: Must be sure to bzero() before using any globals. */
	bzero(&__bss_start, (uintptr_t)&__bss_end - (uintptr_t)&__bss_start);

	boot2_argc = argc;
	boot2_argv = argv;
	boot2_envv = envv;
	boot2_bootinfo = *bootinfop;	/* Copy rather than by reference. */

	setheap((void *)&__heap_start, (void *)&__heap_end);

	/*
	 * Pick up console settings from boot2; probe console.
	 */
	if (bootinfop->bi_boot2opts & RB_MULTIPLE) {
		if (bootinfop->bi_boot2opts & RB_SERIAL)
			setenv("console", "comconsole vidconsole", 1);
		else
			setenv("console", "vidconsole comconsole", 1);
	} else if (bootinfop->bi_boot2opts & RB_SERIAL)
		setenv("console", "comconsole", 1);
	else if (bootinfop->bi_boot2opts & RB_MUTE)
		setenv("console", "nullconsole", 1);
	cons_probe();
	setenv("LINES", "24", 1);

	printf("%s(%d, %p, %p, %p (%p))\n", __func__, argc, argv, envv,
	    bootinfop, (void *)bootinfop->bi_memsize);

	/*
	 * Initialise devices.
	 */
	for (dp = devsw; *dp != NULL; dp++) {
		if ((*dp)->dv_init != NULL)
			(*dp)->dv_init();
	}
	extract_currdev(bootinfop);

	printf("\n%s", bootprog_info);
#if 0
	printf("bootpath=\"%s\"\n", bootpath);
#endif

	interact(NULL);
	return (0);
}

static void
extract_currdev(struct bootinfo *bootinfop)
{
	const char *bootdev;

	/*
	 * Pick up boot device information from boot2.
	 *
	 * XXXRW: Someday: device units.
	 */
	switch(bootinfop->bi_boot_dev_type) {
	case BOOTINFO_DEV_TYPE_DRAM:
		bootdev = "dram0";
		break;

	case BOOTINFO_DEV_TYPE_CFI:
		bootdev = "cfi0";
		break;

	case BOOTINFO_DEV_TYPE_SDCARD:
		bootdev = "sdcard0";
		break;

	default:
		bootdev = NULL;
	}

	if (bootdev != NULL) {
		env_setenv("currdev", EV_VOLATILE, bootdev, NULL, env_nounset);
		env_setenv("loaddev", EV_VOLATILE, bootdev, env_noset,
		    env_nounset);
	}
}

void
abort(void)
{

	printf("error: loader abort\n");
	while (1);
}

void
exit(int code)
{

	printf("error: loader exit\n");
	while (1);
}

void
longjmperror(void)
{

	printf("error: loader longjmp error\n");
	while (1);
}

time_t
time(time_t *tloc)
{

	/* We can't provide time since UTC, so just provide time since boot. */
	return (cp0_count_get() / 100000000);
}

/*
 * Delay - in usecs
 *
 * NOTE: We are assuming that the CPU is running at 100MHz.
 */
void
delay(int usecs)
{
	uint32_t delta;
	uint32_t curr;
	uint32_t last;

	last = cp0_count_get();
	while (usecs > 0) {
		curr = cp0_count_get();
		delta = curr - last;
		while (usecs > 0 && delta >= 100) {
			usecs--;
			last += 100;
			delta -= 100;
		}
	}
}