Home | History | Annotate | Line # | Download | only in boot
      1 /*	$NetBSD: boot.c,v 1.34 2026/11/25 09:32:35 isaki Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001 Minoura Makoto
      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 AUTHOR 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 AUTHOR 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 <sys/param.h>
     30 #include <machine/bootinfo.h>
     31 
     32 #include <lib/libsa/stand.h>
     33 #include <lib/libsa/loadfile.h>
     34 #include <lib/libsa/ufs.h>
     35 #include <lib/libsa/dev_net.h>
     36 #include <lib/libkern/libkern.h>
     37 
     38 #include "libx68k.h"
     39 #include "iocs.h"
     40 #include "switch.h"
     41 
     42 #define HEAP_START	((void*) 0x00080000)
     43 #define HEAP_END	((void*) 0x000fffff)
     44 #define EXSCSI_BDID	((void*) 0x00ea0001)
     45 #define SRAM_MEMSIZE	(*((long*) 0x00ed0008))
     46 
     47 char default_kernel[20] =
     48 #if defined(NETBOOT)
     49     "nfs:netbsd";
     50 #else
     51     "sd0a:netbsd";
     52 #endif
     53 int mpu;
     54 int hostadaptor;
     55 int console_device = -1;
     56 
     57 #ifdef DEBUG
     58 int debug = 1;
     59 #endif
     60 
     61 static void help(void);
     62 static int get_scsi_host_adapter(void);
     63 static void doboot(const char *, int);
     64 static void boot(char *);
     65 static void cmd_ls(char *);
     66 int bootmenu(void);
     67 void bootmain(int);
     68 extern int detectmpu(void);
     69 extern int badbaddr(void *);
     70 extern void exec_image2(unsigned int, unsigned int, unsigned int,
     71     int, int, int);
     72 
     73 extern struct fs_ops file_system_ustarfs[];
     74 extern struct fs_ops file_system_nfs[];
     75 
     76 /* from boot_ufs/bootmain.c */
     77 static int
     78 get_scsi_host_adapter(void)
     79 {
     80 	uint32_t bootinf;
     81 	char *bootrom;
     82 	int ha;
     83 
     84 	bootinf = IOCS_BOOTINF();
     85 	if (bootinf < 0xa0) {
     86 		/* boot from FD */
     87 		return 0;
     88 	}
     89 
     90 	/* Or, bootinf indicates the boot address */
     91 	bootrom = (char *)(bootinf & 0x00ffffe0);
     92 	/*
     93 	 * bootrom+0x24	"SCSIIN" ... Internal SCSI (spc0@)
     94 	 *		"SCSIEX" ... External SCSI (spc1@ or mha0@)
     95 	 */
     96 	if (*(u_short *)(bootrom + 0x24 + 4) == 0x494e) {	/* "IN" */
     97 		ha = (X68K_BOOT_SCSIIF_SPC << 4) | 0;
     98 	} else if (badbaddr(EXSCSI_BDID)) {
     99 		ha = (X68K_BOOT_SCSIIF_MHA << 4) | 0;
    100 	} else {
    101 		ha = (X68K_BOOT_SCSIIF_SPC << 4) | 1;
    102 	}
    103 
    104 	return ha;
    105 }
    106 
    107 static void
    108 help(void)
    109 {
    110 	printf("Usage:\n");
    111 	printf("boot [ha@][dev:][file] -[flags]\n");
    112 	printf(" ha:    spc0, spc1, mha0\n");
    113 	printf(" dev:   sd<ID><PART>, ID=0-7, PART=a-p\n");
    114 	printf("        cd<ID>a, ID=0-7\n");
    115 	printf("        fd<UNIT>a, UNIT=0-3, format is detected.\n");
    116 	printf("        nfs, first probed NE2000 is used.\n");
    117 	printf(" file:  netbsd, netbsd.gz, etc.\n");
    118 	printf(" flags: abdqsv\n");
    119 	printf("ls [dev:][directory]\n");
    120 	printf("switch [show | key=val]\n");
    121 	printf("halt\nreboot\n");
    122 }
    123 
    124 static void
    125 doboot(const char *file, int flags)
    126 {
    127 	u_long		marks[MARK_MAX];
    128 	int fd;
    129 	int ha;		/* host adaptor */
    130 	int dev;	/* device number in devspec[] */
    131 	int unit;
    132 	int part;
    133 	int bootdev;
    134 	int maj;
    135 	char *name;
    136 	short *p;
    137 	int loadflag;
    138 	struct fs_ops *fs;
    139 
    140 	printf("Starting %s, flags 0x%x\n", file, flags);
    141 
    142 	if (devparse(file, &ha, &dev, &unit, &part, &name) != 0) {
    143 		printf("XXX: unknown corruption in /boot.\n");
    144 	}
    145 
    146 #ifdef DEBUG
    147 	if (file[0] == 'n') {
    148 		printf("dev = %x, unit = %d, name = %s\n",
    149 		       dev, unit, name);
    150 	} else {
    151 		printf("ha = 0x%x, dev = %x, unit = %d, part = %c, name = %s\n",
    152 		       ha, dev, unit, part + 'a', name);
    153 	}
    154 #endif
    155 
    156 	if (dev == 3) {		/* netboot */
    157 		bootdev = X68K_MAKEBOOTDEV(X68K_MAJOR_NE, unit, 0);
    158 	} else if (dev == 2) {		/* FD */
    159 		bootdev = X68K_MAKEBOOTDEV(X68K_MAJOR_FD, unit & 3, 0);
    160 	} else {		/* SCSI */
    161 		if (ha != 0) {
    162 			hostadaptor = ha;
    163 		}
    164 		if (hostadaptor == 0) {
    165 			printf("host adaptor must be specified.\n");
    166 			return;
    167 		}
    168 
    169 		maj = (dev == 0) ? X68K_MAJOR_SD : X68K_MAJOR_CD;
    170 		bootdev = X68K_MAKESCSIBOOTDEV(maj,
    171 		    hostadaptor >> 4,
    172 		    hostadaptor & 15,
    173 		    unit & 7, 0, 0);
    174 	}
    175 #ifdef DEBUG
    176 	printf("boot device = %x\n", bootdev);
    177 	if (file[0] == 'n') {
    178 		printf("type = %x, if = %d, unit = %d\n",
    179 		       B_TYPE(bootdev),
    180 		       B_X68K_SCSI_IF(bootdev),
    181 		       B_X68K_SCSI_IF_UN(bootdev));
    182 	} else {
    183 		printf("type = %x, if = %d, unit = %d, id = %d, lun = %d, part = %c\n",
    184 		       B_TYPE(bootdev),
    185 		       B_X68K_SCSI_IF(bootdev),
    186 		       B_X68K_SCSI_IF_UN(bootdev),
    187 		       B_X68K_SCSI_ID(bootdev),
    188 		       B_X68K_SCSI_LUN(bootdev),
    189 		       B_X68K_SCSI_PART(bootdev) + 'a');
    190 	}
    191 #endif
    192 
    193 	/*
    194 	 * Choose the last entry of file_system[] at runtime.
    195 	 *
    196 	 * file_system[] is checked in turn from the beginning at all cases.
    197 	 * Trying FS_OPS(ustarfs) for non-ustarfs displays "@" (as the
    198 	 * mark which read a cylinder?).  OTOH, trying FS_OPS(nfs) for
    199 	 * non-nfs displays "must mount first" error message.
    200 	 * It is better that neither is displayed and in other words you
    201 	 * should not put these two into file_system[] at the same time.
    202 	 * Therefore I choose one of these here.
    203 	 */
    204 	if (file[0] == 'n') {
    205 		fs = &file_system_nfs[0];
    206 	} else {
    207 		fs = &file_system_ustarfs[0];
    208 	}
    209 	memcpy(&file_system[nfsys - 1], fs, sizeof(*fs));
    210 
    211 	loadflag = LOAD_KERNEL;
    212 	if (file[0] == 'f')
    213 		loadflag &= ~LOAD_BACKWARDS;
    214 
    215 	marks[MARK_START] = 0x100000;
    216 	if ((fd = loadfile(file, marks, loadflag)) == -1) {
    217 		printf("loadfile failed\n");
    218 		return;
    219 	}
    220 	close(fd);
    221 
    222 	p = ((short*) marks[MARK_ENTRY]) - 1;
    223 #ifdef DEBUG
    224 	printf("Kernel Version: 0x%x\n", *p);
    225 #endif
    226 	if (*p != 0x4e73 && *p != 0) {
    227 		/*
    228 		 * XXX temporary solution; compatibility loader
    229 		 * must be written.
    230 		 */
    231 		printf("This kernel is too new to be loaded by "
    232 		       "this version of /boot.\n");
    233 		return;
    234 	}
    235 
    236 	exec_image2(marks[MARK_START], 0, marks[MARK_ENTRY] - marks[MARK_START],
    237 	    marks[MARK_END] - marks[MARK_START], bootdev, flags);
    238 
    239 	return;
    240 }
    241 
    242 static void
    243 boot(char *arg)
    244 {
    245 	char filename[80];
    246 	char *p;
    247 	int flags = 0;
    248 
    249 	if (*arg == 0 || *arg == '-') {
    250 		strcpy(filename, default_kernel);
    251 		if (*arg == '-')
    252 			if (parseopts(arg, &flags) == 0) {
    253 				help();
    254 				return;
    255 			}
    256 		doboot(filename, flags);
    257 		return;
    258 	} else {
    259 		p = gettrailer(arg);
    260 		if (strchr(arg, ':')) {
    261 			strcpy(filename, arg);
    262 			if (arg[strlen(arg) - 1] == ':')
    263 				strcat(filename, "netbsd");
    264 		} else {
    265 			strcpy(filename, default_kernel);
    266 			strcpy(strchr(filename, ':') + 1, arg);
    267 		}
    268 		if (*p == '-') {
    269 			if (parseopts(p, &flags) == 0)
    270 				return;
    271 		} else if (*p != 0) {
    272 			help();
    273 			return;
    274 		}
    275 
    276 		doboot(filename, flags);
    277 		return;
    278 	}
    279 }
    280 
    281 static void
    282 cmd_ls(char *arg)
    283 {
    284 	char filename[80];
    285 
    286 	devopen_open_dir = 1;
    287 	if (*arg == 0) {
    288 		strcpy(filename, default_kernel);
    289 		strcpy(strchr(filename, ':')+1, "/");
    290 	} else if (strchr(arg, ':') == 0) {
    291 		strcpy(filename, default_kernel);
    292 		strcpy(strchr(filename, ':')+1, arg);
    293 	} else {
    294 		strcpy(filename, arg);
    295 		if (*(strchr(arg, ':')+1) == 0)
    296 			strcat(filename, "/");
    297 	}
    298 	ls(filename);
    299 	devopen_open_dir = 0;
    300 }
    301 
    302 int
    303 bootmenu(void)
    304 {
    305 	char input[80];
    306 	int n = 5, c;
    307 
    308 	printf("Press return to boot now, any other key for boot menu\n");
    309 	printf("booting %s - starting in %d seconds. ",
    310 		default_kernel, n);
    311 	while (n-- > 0 && (c = awaitkey_1sec()) == 0) {
    312 		printf("\r");
    313 		printf("booting %s - starting in %d seconds. ",
    314 		       default_kernel, n);
    315 	}
    316 	printf("\r");
    317 	printf("booting %s - starting in %d seconds. ", default_kernel, 0);
    318 	printf("\n");
    319 
    320 	if (c == 0 || c == '\r') {
    321 		doboot(default_kernel, 0);
    322 		printf("Could not start %s; ", default_kernel);
    323 		strcat(default_kernel, ".gz");
    324 		printf("trying %s.\n", default_kernel);
    325 		doboot(default_kernel, 0);
    326 		printf("Could not start %s; ", default_kernel);
    327 	}
    328 
    329 	printf("Please use the absolute unit# (e.g. SCSI ID)"
    330 	       " instead of the NetBSD logical #.\n");
    331 	for (;;) {
    332 		char *p, *options;
    333 
    334 		printf("> ");
    335 		kgets(input, sizeof(input));
    336 
    337 		for (p = &input[0]; p - &input[0] < 80 && *p == ' '; p++)
    338 			;
    339 		options = gettrailer(p);
    340 		if (strcmp("boot", p) == 0)
    341 			boot(options);
    342 		else if (strcmp("help", p) == 0 ||
    343 			 strcmp("?", p) == 0)
    344 			help();
    345 		else if (strcmp("halt", p) == 0 ||
    346 			 strcmp("reboot", p) == 0)
    347 			exit(0);
    348 		else if (strcmp("switch", p) == 0)
    349 			cmd_switch(options);
    350 		else if (strcmp("ls", p) == 0)
    351 			cmd_ls(options);
    352 		else
    353 			printf("Unknown command %s\n", p);
    354 	}
    355 }
    356 
    357 static u_int
    358 checkmemsize(void)
    359 {
    360 	u_int m;
    361 
    362 #define MIN_MB 4
    363 #define MAX_MB 12
    364 
    365 	for (m = MIN_MB; m <= MAX_MB; m++) {
    366 		if (badbaddr((void *)(m * 1024 * 1024 - 1))) {
    367 			/* no memory */
    368 			break;
    369 		}
    370 	}
    371 
    372 	return (m - 1) * 1024 * 1024;
    373 }
    374 
    375 extern const char bootprog_rev[];
    376 extern const char bootprog_name[];
    377 
    378 /*
    379  * Arguments from the boot block:
    380  *   bootdev - specifies the device from which /boot was read, in
    381  *		bootdev format.
    382  */
    383 void
    384 bootmain(int bootdev)
    385 {
    386 	u_int sram_memsize;
    387 	u_int probed_memsize;
    388 
    389 	console_device = consio_init(console_device);
    390 
    391 	hostadaptor = get_scsi_host_adapter();
    392 	rtc_offset = RTC_OFFSET;
    393 	try_bootp = 1;
    394 	mpu = detectmpu();
    395 
    396 	if (mpu < 3) {		/* not tested on 68020 */
    397 		printf("This MPU cannot run NetBSD.\n");
    398 		exit(1);
    399 	}
    400 	sram_memsize = SRAM_MEMSIZE;
    401 	if (sram_memsize < 4*1024*1024) {
    402 		printf("Main memory too small.\n");
    403 		exit(1);
    404 	}
    405 
    406 	setheap(HEAP_START, HEAP_END);
    407 
    408 #if !defined(NETBOOT)
    409 	switch (B_TYPE(bootdev)) {
    410 	case X68K_MAJOR_FD:
    411 		default_kernel[0] = 'f';
    412 		default_kernel[2] = '0' + B_UNIT(bootdev);
    413 		default_kernel[3] = 'a';
    414 		break;
    415 	case X68K_MAJOR_SD:
    416 	{
    417 		int part;
    418 
    419 		default_kernel[2] = '0' + B_X68K_SCSI_ID(bootdev);
    420 		part = sd_getbsdpartition(B_X68K_SCSI_ID(bootdev),
    421 		    B_X68K_SCSI_PART(bootdev));
    422 		if (part < 0)
    423 			goto unknown_bootdev;
    424 		default_kernel[3] = 'a' + part;
    425 		break;
    426 	}
    427 	case X68K_MAJOR_CD:
    428 		default_kernel[0] = 'c';
    429 		default_kernel[2] = '0' + B_X68K_SCSI_ID(bootdev);
    430 		default_kernel[3] = 'a';
    431 		break;
    432 	default:
    433 	unknown_bootdev:
    434 		printf("Warning: unknown boot device: %x\n", bootdev);
    435 	}
    436 #endif
    437 	print_title("%s, Revision %s\n", bootprog_name, bootprog_rev);
    438 
    439 	/* check actual memory size for machines with a dead SRAM battery */
    440 	probed_memsize = checkmemsize();
    441 	if (sram_memsize != probed_memsize) {
    442 		printf("\x1b[1mWarning: SRAM Memory Size (%d MB) "
    443 		    "is different from probed Memory Size (%d MB)\n"
    444 		    "         Check and reset SRAM values.\x1b[m\n\n",
    445 		    sram_memsize / (1024 * 1024),
    446 		    probed_memsize / (1024 * 1024));
    447 	}
    448 
    449 	bootmenu();
    450 }
    451