Home | History | Annotate | Line # | Download | only in boot
boot.c revision 1.9
      1 /*	$NetBSD: boot.c,v 1.9 2002/04/13 07:53:53 tsutsui Exp $	*/
      2 
      3 /*-
      4  * Copyright (C) 1999 Tsubai Masanari.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. The name of the author may not be used to endorse or promote products
     15  *    derived from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <lib/libkern/libkern.h>
     30 #include <lib/libsa/stand.h>
     31 #include <lib/libsa/loadfile.h>
     32 
     33 #include <machine/apcall.h>
     34 #include <machine/bootinfo.h>
     35 #include <machine/cpu.h>
     36 #include <machine/romcall.h>
     37 
     38 void mips1_flushicache __P((void *, int));
     39 extern char _edata[], _end[];
     40 
     41 struct apbus_sysinfo *_sip;
     42 int apbus;
     43 
     44 char *devs[] = { "sd", "fh", "fd", NULL, NULL, "rd", "st" };
     45 char *kernels[] = { "/netbsd", "/netbsd.gz", NULL };
     46 
     47 #ifdef BOOT_DEBUG
     48 # define DPRINTF printf
     49 #else
     50 # define DPRINTF while (0) printf
     51 #endif
     52 
     53 void
     54 boot(a0, a1, a2, a3, a4, a5)
     55 	u_int a0, a1, a2, a3, a4, a5;
     56 {
     57 	int fd, i;
     58 	char *netbsd = "";
     59 	int maxmem;
     60 	u_long marks[MARK_MAX];
     61 	char devname[32], file[32];
     62 	void (*entry)();
     63 	struct btinfo_symtab bi_sym;
     64 	struct btinfo_bootarg bi_arg;
     65 	struct btinfo_bootpath bi_bpath;
     66 	struct btinfo_systype bi_sys;
     67 
     68 	/* Clear BSS. */
     69 	memset(_edata, 0, _end - _edata);
     70 
     71 	/*
     72 	 * XXX a3 contains:
     73 	 *     maxmem (nws-3xxx)
     74 	 *     argv   (apbus-based machine)
     75 	 */
     76 	if (a3 >= 0x80000000)
     77 		apbus = 1;
     78 	else
     79 		apbus = 0;
     80 
     81 	if (apbus)
     82 		_sip = (void *)a4;
     83 
     84 	printf("NetBSD/newsmips Secondary Boot\n");
     85 
     86 	if (apbus) {
     87 		char *bootdev = (char *)a1;
     88 		int argc = a2;
     89 		char **argv = (char **)a3;
     90 
     91 		DPRINTF("APbus-based system\n");
     92 
     93 		DPRINTF("argc = %d\n", argc);
     94 		for (i = 0; i < argc; i++) {
     95 			DPRINTF("argv[%d] = %s\n", i, argv[i]);
     96 			if (argv[i][0] != '-' && *netbsd == 0)
     97 				netbsd = argv[i];
     98 		}
     99 		maxmem = _sip->apbsi_memsize;
    100 		maxmem -= 0x100000;	/* reserve 1MB for ROM monitor */
    101 
    102 		DPRINTF("howto = 0x%x\n", a0);
    103 		DPRINTF("bootdev = %s\n", (char *)a1);
    104 		DPRINTF("bootname = %s\n", netbsd);
    105 		DPRINTF("maxmem = 0x%x\n", maxmem);
    106 
    107 		/* XXX use "sonic()" instead of "tftp()" */
    108 		if (strncmp(bootdev, "tftp", 4) == 0)
    109 			bootdev = "sonic";
    110 
    111 		strcpy(devname, bootdev);
    112 		if (strchr(devname, '(') == NULL)
    113 			strcat(devname, "()");
    114 	} else {
    115 		int bootdev = a1;
    116 		char *bootname = (char *)a2;
    117 		int ctlr, unit, part, type;
    118 
    119 		DPRINTF("HB system.\n");
    120 
    121 		/* bootname is "/boot" by default on HB system. */
    122 		if (bootname && strcmp(bootname, "/boot") != 0)
    123 			netbsd = bootname;
    124 		maxmem = a3;
    125 
    126 		DPRINTF("howto = 0x%x\n", a0);
    127 		DPRINTF("bootdev = 0x%x\n", a1);
    128 		DPRINTF("bootname = %s\n", netbsd);
    129 		DPRINTF("maxmem = 0x%x\n", maxmem);
    130 
    131 		ctlr = BOOTDEV_CTLR(bootdev);
    132 		unit = BOOTDEV_UNIT(bootdev);
    133 		part = BOOTDEV_PART(bootdev);
    134 		type = BOOTDEV_TYPE(bootdev);
    135 
    136 		if (devs[type] == NULL) {
    137 			printf("unknown bootdev (0x%x)\n", bootdev);
    138 			_rtt();
    139 		}
    140 
    141 		sprintf(devname, "%s(%d,%d,%d)", devs[type], ctlr, unit, part);
    142 	}
    143 
    144 	printf("Booting %s%s\n", devname, netbsd);
    145 
    146 	/* use user specified kernel name if exists */
    147 	if (*netbsd) {
    148 		kernels[0] = netbsd;
    149 		kernels[1] = NULL;
    150 	}
    151 
    152 	marks[MARK_START] = 0;
    153 
    154 	for (i = 0; kernels[i]; i++) {
    155 		sprintf(file, "%s%s", devname, kernels[i]);
    156 		DPRINTF("trying %s...\n", file);
    157 		fd = loadfile(file, marks, LOAD_KERNEL);
    158 		if (fd != -1)
    159 			break;
    160 	}
    161 	if (kernels[i] == NULL)
    162 		_rtt();
    163 
    164 	DPRINTF("entry = 0x%x\n", (int)marks[MARK_ENTRY]);
    165 	DPRINTF("ssym = 0x%x\n", (int)marks[MARK_SYM]);
    166 	DPRINTF("esym = 0x%x\n", (int)marks[MARK_END]);
    167 
    168 	bi_init(BOOTINFO_ADDR);
    169 
    170 	bi_sym.nsym = marks[MARK_NSYM];
    171 	bi_sym.ssym = marks[MARK_SYM];
    172 	bi_sym.esym = marks[MARK_END];
    173 	bi_add(&bi_sym, BTINFO_SYMTAB, sizeof(bi_sym));
    174 
    175 	bi_arg.howto = a0;
    176 	bi_arg.bootdev = a1;
    177 	bi_arg.maxmem = maxmem;
    178 	bi_arg.sip = (int)_sip;
    179 	bi_add(&bi_arg, BTINFO_BOOTARG, sizeof(bi_arg));
    180 
    181 	strcpy(bi_bpath.bootpath, file);
    182 	bi_add(&bi_bpath, BTINFO_BOOTPATH, sizeof(bi_bpath));
    183 
    184 	bi_sys.type = apbus ? NEWS5000 : NEWS3400;		/* XXX */
    185 	bi_add(&bi_sys, BTINFO_SYSTYPE, sizeof(bi_sys));
    186 
    187 	entry = (void *)marks[MARK_ENTRY];
    188 
    189 	if (apbus)
    190 		apcall_flushcache();
    191 	else
    192 		mips1_flushicache(entry, marks[MARK_SYM] - marks[MARK_ENTRY]);
    193 
    194 	printf("\n");
    195 	(*entry)(a0, a1, a2, a3, a4, a5);
    196 }
    197 
    198 void
    199 putchar(x)
    200 	int x;
    201 {
    202 	char c = x;
    203 
    204 	if (apbus)
    205 		apcall_write(1, &c, 1);
    206 	else
    207 		rom_write(1, &c, 1);
    208 }
    209 
    210 int
    211 getchar()
    212 {
    213 	unsigned char c = '\0';
    214 	int i;
    215 
    216 	for (;;) {
    217 		i = apbus ? apcall_read(1, &c, 1) : rom_read(1, &c, 1);
    218 		if (i == 1)
    219 			break;
    220 		if (i != -2 && i != 0)
    221 			return -1;
    222 	}
    223 	return c;
    224 }
    225 
    226 void
    227 _rtt()
    228 {
    229 	if (apbus)
    230 		apcall_exit(8);
    231 	else
    232 		rom_halt();
    233 
    234 	for (;;);
    235 }
    236