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