boot.c revision 1.1
1/* $NetBSD: boot.c,v 1.1 2025/11/16 20:11:47 jmcneill Exp $ */
2
3/*-
4 * Copyright (c) 2025 Jared McNeill <jmcneill@invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <lib/libsa/stand.h>
30#include <lib/libsa/loadfile.h>
31
32#include <powerpc/include/psl.h>
33#include <powerpc/include/spr.h>
34
35#include "cache.h"
36#include "console.h"
37#include "gpio.h"
38#include "miniipc.h"
39#include "sdmmc.h"
40#include "timer.h"
41
42static const char * const names[] = {
43	"netbsd", "netbsd.gz",
44	"onetbsd", "onetbsd.gz",
45	"netbsd.old", "onetbsd.old.gz",
46};
47#define NUMNAMES	__arraycount(names)
48
49static int	exec_netbsd(const char *);
50
51int
52main(void)
53{
54	extern uint8_t edata[], end[];
55	int curname;
56
57	memset(&edata, 0, end - edata);	/* clear BSS */
58
59	console_init();
60
61	gpio_set(GPIO_SLOT_LED);
62
63	if (!miniipc_probe()) {
64		panic("MINI IPC not found!");
65	}
66
67	sdmmc_init();
68
69	for (curname = 0; curname < NUMNAMES; curname++) {
70		printf("booting %s ", names[curname]);
71		exec_netbsd(names[curname]);
72	}
73
74	panic("No bootable kernel found");
75
76	return 0;
77}
78
79static int
80exec_netbsd(const char *fname)
81{
82	u_long marks[MARK_MAX];
83	void (*entry)(void);
84	int fd;
85
86	memset(marks, 0, sizeof(marks));
87	fd = loadfile(fname, marks, LOAD_KERNEL);
88	if (fd == -1) {
89		return -1;
90	}
91
92	gpio_clear(GPIO_SLOT_LED);
93
94	entry = (void *)marks[MARK_ENTRY];
95	cache_dcbf((void *)marks[MARK_START],
96		   marks[MARK_END] - marks[MARK_START]);
97	cache_icbi((void *)marks[MARK_START],
98		   marks[MARK_END] - marks[MARK_START]);
99
100	entry();
101	panic("Unexpected return from kernel");
102}
103
104__dead void
105_rtt(void)
106{
107	int led = 0;
108	for (;;) {
109		if (led) {
110			gpio_set(GPIO_SLOT_LED);
111		} else {
112			gpio_clear(GPIO_SLOT_LED);
113		}
114		timer_udelay(1000000);
115		led ^= 1;
116	}
117}
118