1 /**	$MirOS: src/sys/arch/sparc/sparc/autoconf.c,v 1.7 2007/06/11 21:12:00 tg Exp $	*/
2 /*	$OpenBSD: autoconf.c,v 1.59 2003/06/23 09:23:31 miod Exp $	*/
3 /*	$NetBSD: autoconf.c,v 1.73 1997/07/29 09:41:53 fair Exp $ */
4 
5 /*
6  * Copyright (c) 1996
7  *    The President and Fellows of Harvard College. All rights reserved.
8  * Copyright (c) 1992, 1993
9  *	The Regents of the University of California.  All rights reserved.
10  *
11  * This software was developed by the Computer Systems Engineering group
12  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
13  * contributed to Berkeley.
14  *
15  * All advertising materials mentioning features or use of this software
16  * must display the following acknowledgement:
17  *	This product includes software developed by Harvard University.
18  *	This product includes software developed by the University of
19  *	California, Lawrence Berkeley Laboratory.
20  *
21  * Redistribution and use in source and binary forms, with or without
22  * modification, are permitted provided that the following conditions
23  * are met:
24  * 1. Redistributions of source code must retain the above copyright
25  *    notice, this list of conditions and the following disclaimer.
26  * 2. Redistributions in binary form must reproduce the above copyright
27  *    notice, this list of conditions and the following disclaimer in the
28  *    documentation and/or other materials provided with the distribution.
29  * 3. Neither the name of the University nor the names of its contributors
30  *    may be used to endorse or promote products derived from this software
31  *    without specific prior written permission.
32  *
33  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43  * SUCH DAMAGE.
44  *
45  *	@(#)autoconf.c	8.4 (Berkeley) 10/1/93
46  */
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/buf.h>
51 #include <sys/disklabel.h>
52 #include <sys/device.h>
53 #include <sys/disk.h>
54 #include <sys/dkstat.h>
55 #include <sys/conf.h>
56 #include <sys/reboot.h>
57 #include <sys/socket.h>
58 #include <sys/malloc.h>
59 #include <sys/queue.h>
60 #include <sys/user.h>
61 
62 #include <net/if.h>
63 
64 #include <dev/cons.h>
65 
66 #include <uvm/uvm_extern.h>
67 
68 #include <machine/autoconf.h>
69 #include <machine/bsd_openprom.h>
70 #ifdef SUN4
71 #include <machine/oldmon.h>
72 #include <machine/idprom.h>
73 #include <sparc/sparc/memreg.h>
74 #endif
75 #include <machine/cpu.h>
76 #include <machine/ctlreg.h>
77 #include <machine/pmap.h>
78 #include <sparc/sparc/asm.h>
79 #include <sparc/sparc/cpuvar.h>
80 #include <sparc/sparc/timerreg.h>
81 
82 #ifdef DDB
83 #include <machine/db_machdep.h>
84 #include <ddb/db_sym.h>
85 #include <ddb/db_extern.h>
86 #endif
87 
88 
89 /*
90  * The following several variables are related to
91  * the configuration process, and are used in initializing
92  * the machine.
93  */
94 int	fbnode;		/* node ID of ROM's console frame buffer */
95 int	optionsnode;	/* node ID of ROM's options */
96 int	mmu_3l;		/* SUN4_400 models have a 3-level MMU */
97 char	root_devname[16];
98 
99 #ifdef KGDB
100 extern	int kgdb_debug_panic;
101 #endif
102 
103 static	int rootnode;
104 void	setroot(void);
105 static	char *str2hex(char *, int *);
106 static	int getstr(char *, int);
107 int	findblkmajor(struct device *);
108 char	*findblkname(int);
109 static	struct device *getdisk(char *, int, int, dev_t *);
110 static	int mbprint(void *, const char *);
111 static	void crazymap(char *, int *);
112 int	st_crazymap(int);
113 void	swapconf(void);
114 void	sync_crash(void);
115 int	mainbus_match(struct device *, void *, void *);
116 static	void mainbus_attach(struct device *, struct device *, void *);
117 
118 struct	bootpath bootpath[8];
119 int	nbootpath;
120 static	void bootpath_build(void);
121 static	void bootpath_fake(struct bootpath *, char *);
122 static	void bootpath_print(struct bootpath *);
123 int	search_prom(int, char *);
124 char	mainbus_model[30];
125 
126 /*
127  * The mountroot_hook is provided as a mechanism for devices to perform
128  * a special function if they're the root device, such as the floppy
129  * drive ejecting the current disk and prompting for a filesystem floppy.
130  */
131 struct mountroot_hook {
132 	LIST_ENTRY(mountroot_hook) mr_link;
133 	struct	device *mr_device;
134 	void	(*mr_func)(struct device *);
135 };
136 LIST_HEAD(, mountroot_hook) mrh_list;
137 
138 #ifdef RAMDISK_HOOKS
139 static struct device fakerdrootdev = { DV_DISK, {}, NULL, 0, "rd0", NULL };
140 #endif
141 
142 /* Translate SBus interrupt level to processor IPL */
143 int	intr_sbus2ipl_4c[] = {
144 	0, 1, 2, 3, 5, 7, 8, 9
145 };
146 int	intr_sbus2ipl_4m[] = {
147 	0, 2, 3, 5, 7, 9, 11, 13
148 };
149 
150 /*
151  * Most configuration on the SPARC is done by matching OPENPROM Forth
152  * device names with our internal names.
153  */
154 int
matchbyname(parent,vcf,aux)155 matchbyname(parent, vcf, aux)
156 	struct device *parent;
157 	void *aux, *vcf;
158 {
159 	struct cfdata *cf = vcf;
160 	struct confargs *ca = aux;
161 
162 	if (CPU_ISSUN4) {
163 		printf("WARNING: matchbyname not valid on sun4!");
164 		printf("%s\n", cf->cf_driver->cd_name);
165 		return (0);
166 	}
167 	return (strcmp(cf->cf_driver->cd_name, ca->ca_ra.ra_name) == 0);
168 }
169 
170 /*
171  * Convert hex ASCII string to a value.  Returns updated pointer.
172  * Depends on ASCII order (this *is* machine-dependent code, you know).
173  */
174 static char *
str2hex(str,vp)175 str2hex(str, vp)
176 	register char *str;
177 	register int *vp;
178 {
179 	register int v, c;
180 
181 	for (v = 0;; v = v * 16 + c, str++) {
182 		c = *(u_char *)str;
183 		if (c <= '9') {
184 			if ((c -= '0') < 0)
185 				break;
186 		} else if (c <= 'F') {
187 			if ((c -= 'A' - 10) < 10)
188 				break;
189 		} else if (c <= 'f') {
190 			if ((c -= 'a' - 10) < 10)
191 				break;
192 		} else
193 			break;
194 	}
195 	*vp = v;
196 	return (str);
197 }
198 
199 #ifdef SUN4
200 struct promvec promvecdat;
201 struct om_vector *oldpvec = (struct om_vector *)PROM_BASE;
202 #endif
203 
204 /*
205  * locore.s code calls bootstrap() just before calling main(), after double
206  * mapping the kernel to high memory and setting up the trap base register.
207  * We must finish mapping the kernel properly and glean any bootstrap info.
208  */
209 void
bootstrap()210 bootstrap()
211 {
212 #if defined(SUN4)
213 	if (CPU_ISSUN4) {
214 		extern void oldmon_w_cmd(u_long, char *);
215 
216 		/*
217 		 * XXX:
218 		 * The promvec is bogus. We need to build a
219 		 * fake one from scratch as soon as possible.
220 		 */
221 		bzero(&promvecdat, sizeof promvecdat);
222 		promvec = &promvecdat;
223 
224 		promvec->pv_stdin = oldpvec->inSource;
225 		promvec->pv_stdout = oldpvec->outSink;
226 		promvec->pv_putchar = oldpvec->putChar;
227 		promvec->pv_putstr = oldpvec->fbWriteStr;
228 		promvec->pv_nbgetchar = oldpvec->mayGet;
229 		promvec->pv_getchar = oldpvec->getChar;
230 		promvec->pv_romvec_vers = 0;		/* eek! */
231 		promvec->pv_reboot = oldpvec->reBoot;
232 		promvec->pv_abort = oldpvec->abortEntry;
233 		promvec->pv_setctxt = oldpvec->setcxsegmap;
234 		promvec->pv_v0bootargs = (struct v0bootargs **)(oldpvec->bootParam);
235 		promvec->pv_halt = oldpvec->exitToMon;
236 
237 		/*
238 		 * Discover parts of the machine memory organization
239 		 * that we need this early.
240 		 */
241 		if (oldpvec->romvecVersion >= 2)
242 			*oldpvec->vector_cmd = oldmon_w_cmd;
243 	}
244 #endif /* SUN4 */
245 
246 	bzero(&cpuinfo, sizeof(struct cpu_softc));
247 	cpuinfo.master = 1;
248 	getcpuinfo(&cpuinfo, 0);
249 
250 	pmap_bootstrap(cpuinfo.mmu_ncontext,
251 		       cpuinfo.mmu_nregion,
252 		       cpuinfo.mmu_nsegment);
253 	/* Moved zs_kgdb_init() to zs.c:consinit() */
254 #ifdef DDB
255 	db_machine_init();
256 	ddb_init();
257 #endif
258 
259 	/*
260 	 * On sun4ms we have to do some nasty stuff here. We need to map
261 	 * in the interrupt registers (since we need to find out where
262 	 * they are from the PROM, since they aren't in a fixed place), and
263 	 * disable all interrupts. We can't do this easily from locore
264 	 * since the PROM is ugly to use from assembly. We also need to map
265 	 * in the counter registers because we can't disable the level 14
266 	 * (statclock) interrupt, so we need a handler early on (ugh).
267 	 *
268 	 * NOTE: We *demand* the psl to stay at splhigh() at least until
269 	 * we get here. The system _cannot_ take interrupts until we map
270 	 * the interrupt registers.
271 	 */
272 
273 #if defined(SUN4M)
274 #define getpte4m(va)	lda(((va) & 0xFFFFF000) | ASI_SRMMUFP_L3, ASI_SRMMUFP)
275 
276 	/* First we'll do the interrupt registers */
277 	if (CPU_ISSUN4M) {
278 		register int node;
279 		struct romaux ra;
280 		register u_int pte;
281 		register int i;
282 		extern void setpte4m(u_int, u_int);
283 		extern struct timer_4m *timerreg_4m;
284 		extern struct counter_4m *counterreg_4m;
285 
286 		if ((node = opennode("/obio/interrupt")) == 0)
287 		    if ((node=search_prom(findroot(),"interrupt"))==0)
288 			panic("bootstrap: could not get interrupt "
289 			      "node from prom");
290 
291 		if (!romprop(&ra, "interrupt", node))
292 		    panic("bootstrap: could not get interrupt properties");
293 		if (ra.ra_nvaddrs < 2)
294 		    panic("bootstrap: less than 2 interrupt regs. available");
295 		if (ra.ra_nvaddrs > 5)
296 		    panic("bootstrap: cannot support capability of > 4 CPUs");
297 
298 		for (i = 0; i < ra.ra_nvaddrs - 1; i++) {
299 
300 			pte = getpte4m((u_int)ra.ra_vaddrs[i]);
301 			if ((pte & SRMMU_TETYPE) != SRMMU_TEPTE)
302 			    panic("bootstrap: PROM has invalid mapping for "
303 				  "processor interrupt register %d",i);
304 			pte |= PPROT_S;
305 
306 			/* Duplicate existing mapping */
307 
308 			setpte4m(PI_INTR_VA + (_MAXNBPG * i), pte);
309 		}
310 
311 		/*
312 		 * That was the processor register...now get system register;
313 		 * it is the last returned by the PROM
314 		 */
315 		pte = getpte4m((u_int)ra.ra_vaddrs[i]);
316 		if ((pte & SRMMU_TETYPE) != SRMMU_TEPTE)
317 		    panic("bootstrap: PROM has invalid mapping for system "
318 			  "interrupt register");
319 		pte |= PPROT_S;
320 
321 		setpte4m(SI_INTR_VA, pte);
322 
323 		/* Now disable interrupts */
324 		ienab_bis(SINTR_MA);
325 
326 		/* Send all interrupts to primary processor */
327 		*((u_int *)ICR_ITR) = 0;
328 
329 #ifdef DEBUG
330 /*		printf("SINTR: mask: 0x%x, pend: 0x%x\n", *(int *)ICR_SI_MASK,
331 		       *(int *)ICR_SI_PEND);
332 */
333 #endif
334 
335 		/*
336 		 * Now map in the counters
337 		 * (XXX: fix for multiple CPUs! We assume 1)
338 		 * The processor register is the first; the system is the last.
339 		 * See also timerattach() in clock.c.
340 		 * This shouldn't be necessary; we ought to keep interrupts off
341 		 * and/or disable the (level 14) counter...
342 		 */
343 
344 		if ((node = opennode("/obio/counter")) == 0)
345 		    if ((node=search_prom(findroot(),"counter"))==0)
346 			panic("bootstrap: could not find counter in OPENPROM");
347 
348 		if (!romprop(&ra, "counter", node))
349 			panic("bootstrap: could not find counter properties");
350 
351 		counterreg_4m = (struct counter_4m *)ra.ra_vaddrs[0];
352 		timerreg_4m = (struct timer_4m *)ra.ra_vaddrs[ra.ra_nvaddrs-1];
353 	}
354 #endif /* SUN4M */
355 
356 	if (CPU_ISSUN4OR4C) {
357 		/* Map Interrupt Enable Register */
358 		pmap_kenter_pa(INTRREG_VA,
359 			   INT_ENABLE_REG_PHYSADR | PMAP_NC | PMAP_OBIO,
360 			   VM_PROT_READ | VM_PROT_WRITE);
361 		pmap_update(pmap_kernel());
362 		/* Disable all interrupts */
363 		*((unsigned char *)INTRREG_VA) = 0;
364 	}
365 }
366 
367 /*
368  * bootpath_build: build a bootpath. Used when booting a generic
369  * kernel to find our root device.  Newer proms give us a bootpath,
370  * for older proms we have to create one.  An element in a bootpath
371  * has 4 fields: name (device name), val[0], val[1], and val[2]. Note that:
372  * Interpretation of val[] is device-dependent. Some examples:
373  *
374  * if (val[0] == -1) {
375  *	val[1] is a unit number    (happens most often with old proms)
376  * } else {
377  *	[sbus device] val[0] is a sbus slot, and val[1] is an sbus offset
378  *	[scsi disk] val[0] is target, val[1] is lun, val[2] is partition
379  *	[scsi tape] val[0] is target, val[1] is lun, val[2] is file #
380  * }
381  *
382  */
383 
384 static void
bootpath_build()385 bootpath_build()
386 {
387 	register char *cp, *pp;
388 	register struct bootpath *bp;
389 
390 	/*
391 	 * On SS1s, promvec->pv_v0bootargs->ba_argv[1] contains the flags
392 	 * that were given after the boot command.  On SS2s, pv_v0bootargs
393 	 * is NULL but *promvec->pv_v2bootargs.v2_bootargs points to
394 	 * "vmunix -s" or whatever.
395 	 * XXX	DO THIS BEFORE pmap_bootstrap?
396 	 */
397 	bzero(bootpath, sizeof(bootpath));
398 	bp = bootpath;
399 	if (promvec->pv_romvec_vers < 2) {
400 		/*
401 		 * Grab boot device name and values.  build fake bootpath.
402 		 */
403 		cp = (*promvec->pv_v0bootargs)->ba_argv[0];
404 
405 		if (cp != NULL)
406 			bootpath_fake(bp, cp);
407 
408 		bootpath_print(bootpath);
409 
410 		/* Setup pointer to boot flags */
411 		cp = (*promvec->pv_v0bootargs)->ba_argv[1];
412 		if (cp == NULL || *cp != '-')
413 			return;
414 	} else {
415 		/*
416 		 * Grab boot path from PROM
417 		 */
418 		cp = *promvec->pv_v2bootargs.v2_bootpath;
419 		while (cp != NULL && *cp == '/') {
420 			/* Step over '/' */
421 			++cp;
422 			/* Extract name */
423 			pp = bp->name;
424 			while (*cp != '@' && *cp != '/' && *cp != '\0')
425 				*pp++ = *cp++;
426 			*pp = '\0';
427 			if (*cp == '@') {
428 				cp = str2hex(++cp, &bp->val[0]);
429 				if (*cp == ',')
430 					cp = str2hex(++cp, &bp->val[1]);
431 				if (*cp == ':')
432 					/* XXX - we handle just one char */
433 					bp->val[2] = *++cp - 'a', ++cp;
434 			} else {
435 				bp->val[0] = -1; /* no #'s: assume unit 0, no
436 							sbus offset/adddress */
437 			}
438 			++bp;
439 			++nbootpath;
440 		}
441 		bp->name[0] = 0;
442 
443 		bootpath_print(bootpath);
444 
445 		/* Setup pointer to boot flags */
446 		cp = *promvec->pv_v2bootargs.v2_bootargs;
447 		if (cp == NULL)
448 			return;
449 		while (*cp != '-')
450 			if (*cp++ == '\0')
451 				return;
452 	}
453 	for (;;) {
454 		switch (*++cp) {
455 
456 		case '\0':
457 			return;
458 
459 		case 'a':
460 			boothowto |= RB_ASKNAME;
461 			break;
462 
463 		case 'b':
464 			boothowto |= RB_DFLTROOT;
465 			break;
466 
467 		case 'c':
468 			boothowto |= RB_CONFIG;
469 			break;
470 
471 		case 'd':	/* kgdb - always on zs	XXX */
472 #ifdef KGDB
473 			boothowto |= RB_KDB;	/* XXX unused */
474 			kgdb_debug_panic = 1;
475 			kgdb_connect(1);
476 #elif DDB
477 			Debugger();
478 #else
479 			printf("kernel has no debugger\n");
480 #endif
481 			break;
482 
483 		case 's':
484 			boothowto |= RB_SINGLE;
485 			break;
486 		}
487 	}
488 }
489 
490 /*
491  * Fake a ROM generated bootpath.
492  * The argument 'cp' points to a string such as "xd(0,0,0)netbsd"
493  */
494 
495 static void
bootpath_fake(bp,cp)496 bootpath_fake(bp, cp)
497 	struct bootpath *bp;
498 	char *cp;
499 {
500 	register char *pp;
501 	int v0val[3];
502 
503 #define BP_APPEND(BP,N,V0,V1,V2) { \
504 	strlcpy((BP)->name, N, sizeof (BP)->name); \
505 	(BP)->val[0] = (V0); \
506 	(BP)->val[1] = (V1); \
507 	(BP)->val[2] = (V2); \
508 	(BP)++; \
509 	nbootpath++; \
510 }
511 
512 #if defined(SUN4)
513 	if (CPU_ISSUN4M) {
514 		printf("twas brillig..\n");
515 		return;
516 	}
517 #endif
518 
519 	pp = cp + 2;
520 	v0val[0] = v0val[1] = v0val[2] = 0;
521 	if (*pp == '(' 					/* for vi: ) */
522  	    && *(pp = str2hex(++pp, &v0val[0])) == ','
523 	    && *(pp = str2hex(++pp, &v0val[1])) == ',')
524 		(void)str2hex(++pp, &v0val[2]);
525 
526 #if defined(SUN4)
527 	if (CPU_ISSUN4) {
528 		char tmpname[8];
529 
530 		/*
531 		 *  xylogics VME dev: xd, xy, xt
532 		 *  fake looks like: /vmel0/xdc0/xd@1,0
533 		 */
534 		if (cp[0] == 'x') {
535 			if (cp[1] == 'd') {/* xd? */
536 				BP_APPEND(bp, "vmel", -1, 0, 0);
537 			} else {
538 				BP_APPEND(bp, "vmes", -1, 0, 0);
539 			}
540 			snprintf(tmpname,sizeof tmpname,"x%cc", cp[1]); /* e.g. xdc */
541 			BP_APPEND(bp, tmpname,-1, v0val[0], 0);
542 			snprintf(tmpname,sizeof tmpname,"%c%c", cp[0], cp[1]);
543 			BP_APPEND(bp, tmpname,v0val[1], v0val[2], 0); /* e.g. xd */
544 			return;
545 		}
546 
547 		/*
548 		 * ethernet: ie, le (rom supports only obio?)
549 		 * fake looks like: /obio0/le0
550 		 */
551 		if ((cp[0] == 'i' || cp[0] == 'l') && cp[1] == 'e')  {
552 			BP_APPEND(bp, "obio", -1, 0, 0);
553 			snprintf(tmpname,sizeof tmpname,"%c%c", cp[0], cp[1]);
554 			BP_APPEND(bp, tmpname, -1, 0, 0);
555 			return;
556 		}
557 
558 		/*
559 		 * scsi: sd, st, sr
560 		 * assume: 4/100 = sw: /obio0/sw0/sd@0,0:a
561 		 * 4/200 & 4/400 = si/sc: /vmes0/si0/sd@0,0:a
562  		 * 4/300 = esp: /obio0/esp0/sd@0,0:a
563 		 * (note we expect sc to mimic an si...)
564 		 */
565 		if (cp[0] == 's' &&
566 			(cp[1] == 'd' || cp[1] == 't' || cp[1] == 'r')) {
567 
568 			int  target, lun;
569 
570 			switch (cpuinfo.cpu_type) {
571 			case CPUTYP_4_200:
572 			case CPUTYP_4_400:
573 				BP_APPEND(bp, "vmes", -1, 0, 0);
574 				BP_APPEND(bp, "si", -1, v0val[0], 0);
575 				break;
576 			case CPUTYP_4_100:
577 				BP_APPEND(bp, "obio", -1, 0, 0);
578 				BP_APPEND(bp, "sw", -1, v0val[0], 0);
579 				break;
580 			case CPUTYP_4_300:
581 				BP_APPEND(bp, "obio", -1, 0, 0);
582 				BP_APPEND(bp, "esp", -1, v0val[0], 0);
583 				break;
584 			default:
585 				panic("bootpath_fake: unknown system type %d",
586 				      cpuinfo.cpu_type);
587 			}
588 			/*
589 			 * Deal with target/lun encodings.
590 			 * Note: more special casing in dk_establish().
591 			 */
592 			if (oldpvec->monId[0] > '1') {
593 				target = v0val[1] >> 3; /* new format */
594 				lun    = v0val[1] & 0x7;
595 			} else {
596 				target = v0val[1] >> 2; /* old format */
597 				lun    = v0val[1] & 0x3;
598 			}
599 			snprintf(tmpname, sizeof tmpname, "%c%c", cp[0], cp[1]);
600 			BP_APPEND(bp, tmpname, target, lun, v0val[2]);
601 			return;
602 		}
603 
604 		return; /* didn't grok bootpath, no change */
605 	}
606 #endif /* SUN4 */
607 
608 #if defined(SUN4C)
609 	/*
610 	 * sun4c stuff
611 	 */
612 
613 	/*
614 	 * floppy: fd
615 	 * fake looks like: /fd@0,0:a
616 	 */
617 	if (cp[0] == 'f' && cp[1] == 'd') {
618 		/*
619 		 * Assume 'fd(c,u,p)' means:
620 		 * partition 'p' on floppy drive 'u' on controller 'c'
621 		 */
622 		BP_APPEND(bp, "fd", v0val[0], v0val[1], v0val[2]);
623 		return;
624 	}
625 
626 	/*
627 	 * ethernet: le
628 	 * fake looks like: /sbus0/le0
629 	 */
630 	if (cp[0] == 'l' && cp[1] == 'e') {
631 		BP_APPEND(bp, "sbus", -1, 0, 0);
632 		BP_APPEND(bp, "le", -1, v0val[0], 0);
633 		return;
634 	}
635 
636 	/*
637 	 * scsi: sd, st, sr
638 	 * fake looks like: /sbus0/esp0/sd@3,0:a
639 	 */
640 	if (cp[0] == 's' && (cp[1] == 'd' || cp[1] == 't' || cp[1] == 'r')) {
641 		char tmpname[8];
642 		int  target, lun;
643 
644 		BP_APPEND(bp, "sbus", -1, 0, 0);
645 		BP_APPEND(bp, "esp", -1, v0val[0], 0);
646 		if (cp[1] == 'r')
647 			snprintf(tmpname, sizeof tmpname, "cd"); /* netbsd uses 'cd', not 'sr'*/
648 		else
649 			snprintf(tmpname, sizeof tmpname, "%c%c", cp[0], cp[1]);
650 		/* XXX - is TARGET/LUN encoded in v0val[1]? */
651 		target = v0val[1];
652 		lun = 0;
653 		BP_APPEND(bp, tmpname, target, lun, v0val[2]);
654 		return;
655 	}
656 #endif /* SUN4C */
657 
658 
659 	/*
660 	 * unknown; return
661 	 */
662 
663 #undef BP_APPEND
664 }
665 
666 /*
667  * print out the bootpath
668  * the %x isn't 0x%x because the Sun EPROMs do it this way, and
669  * consistency with the EPROMs is probably better here.
670  */
671 
672 static void
bootpath_print(bp)673 bootpath_print(bp)
674 	struct bootpath *bp;
675 {
676 	printf("bootpath: ");
677 	while (bp->name[0]) {
678 		if (bp->val[0] == -1)
679 			printf("/%s%x", bp->name, bp->val[1]);
680 		else
681 			printf("/%s@%x,%x", bp->name, bp->val[0], bp->val[1]);
682 		if (bp->val[2] != 0)
683 			printf(":%c", bp->val[2] + 'a');
684 		bp++;
685 	}
686 	printf("\n");
687 }
688 
689 
690 /*
691  * save or read a bootpath pointer from the boothpath store.
692  *
693  * XXX. required because of SCSI... we don't have control over the "sd"
694  * device, so we can't set boot device there.   we patch in with
695  * dk_establish(), and use this to recover the bootpath.
696  */
697 
698 struct bootpath *
bootpath_store(storep,bp)699 bootpath_store(storep, bp)
700 	int storep;
701 	struct bootpath *bp;
702 {
703 	static struct bootpath *save;
704 	struct bootpath *retval;
705 
706 	retval = save;
707 	if (storep)
708 		save = bp;
709 
710 	return (retval);
711 }
712 
713 /*
714  * Set up the sd target mappings for non SUN4 PROMs.
715  * Find out about the real SCSI target, given the PROM's idea of the
716  * target of the (boot) device (i.e., the value in bp->v0val[0]).
717  */
718 static void
crazymap(prop,map)719 crazymap(prop, map)
720 	char *prop;
721 	int *map;
722 {
723 	int i;
724 	char *propval;
725 
726 	if (!CPU_ISSUN4 && promvec->pv_romvec_vers < 2) {
727 		/*
728 		 * Machines with real v0 proms have an 's[dt]-targets' property
729 		 * which contains the mapping for us to use. v2 proms donot
730 		 * require remapping.
731 		 */
732 		propval = getpropstring(optionsnode, prop);
733 		if (propval == NULL || strlen(propval) != 8) {
734  build_default_map:
735 			printf("WARNING: %s map is bogus, using default\n",
736 				prop);
737 			for (i = 0; i < 8; ++i)
738 				map[i] = i;
739 			i = map[0];
740 			map[0] = map[3];
741 			map[3] = i;
742 			return;
743 		}
744 		for (i = 0; i < 8; ++i) {
745 			map[i] = propval[i] - '0';
746 			if (map[i] < 0 ||
747 			    map[i] >= 8)
748 				goto build_default_map;
749 		}
750 	} else {
751 		/*
752 		 * Set up the identity mapping for old sun4 monitors
753 		 * and v[2-] OpenPROMs. Note: dkestablish() does the
754 		 * SCSI-target juggling for sun4 monitors.
755 		 */
756 		for (i = 0; i < 8; ++i)
757 			map[i] = i;
758 	}
759 }
760 
761 int
sd_crazymap(n)762 sd_crazymap(n)
763 	int	n;
764 {
765 	static int prom_sd_crazymap[8]; /* static: compute only once! */
766 	static int init = 0;
767 
768 	if (init == 0) {
769 		crazymap("sd-targets", prom_sd_crazymap);
770 		init = 1;
771 	}
772 	return prom_sd_crazymap[n];
773 }
774 
775 int
st_crazymap(n)776 st_crazymap(n)
777 	int	n;
778 {
779 	static int prom_st_crazymap[8]; /* static: compute only once! */
780 	static int init = 0;
781 
782 	if (init == 0) {
783 		crazymap("st-targets", prom_st_crazymap);
784 		init = 1;
785 	}
786 	return prom_st_crazymap[n];
787 }
788 
789 /*
790  * Determine mass storage and memory configuration for a machine.
791  * We get the PROM's root device and make sure we understand it, then
792  * attach it as 'mainbus0'.  We also set up to handle the PROM 'sync'
793  * command.
794  */
795 void
cpu_configure()796 cpu_configure()
797 {
798 	struct confargs oca;
799 	register int node = 0;
800 	register char *cp;
801 	int s;
802 	extern struct user *proc0paddr;
803 
804 	/* Initialize the mountroot_hook list. */
805 	LIST_INIT(&mrh_list);
806 
807 	/* build the bootpath */
808 	bootpath_build();
809 
810 	if (boothowto & RB_CONFIG) {
811 #ifdef BOOT_CONFIG
812 		user_config();
813 #else
814 		printf("kernel does not support -c; continuing..\n");
815 #endif
816 	}
817 
818 #if defined(SUN4)
819 	if (CPU_ISSUN4) {
820 		extern struct cfdata cfdata[];
821 		extern struct cfdriver memreg_cd, obio_cd;
822 		struct cfdata *cf, *memregcf = NULL;
823 		register short *p;
824 		struct rom_reg rr;
825 
826 		for (cf = cfdata; memregcf==NULL && cf->cf_driver; cf++) {
827 			if (cf->cf_driver != &memreg_cd ||
828 				cf->cf_loc[0] == -1) /* avoid sun4m memreg0 */
829 				continue;
830 			/*
831 			 * On the 4/100 obio addresses must be mapped at
832 			 * 0x0YYYYYYY, but alias higher up (we avoid the
833 			 * alias condition because it causes pmap difficulties)
834 			 * XXX: We also assume that 4/[23]00 obio addresses
835 			 * must be 0xZYYYYYYY, where (Z != 0)
836 			 * make sure we get the correct memreg cfdriver!
837 			 */
838 			if (cpuinfo.cpu_type == CPUTYP_4_100 &&
839 			    (cf->cf_loc[0] & 0xf0000000))
840 				continue;
841 			if (cpuinfo.cpu_type != CPUTYP_4_100 &&
842 			    !(cf->cf_loc[0] & 0xf0000000))
843 				continue;
844 			for (p = cf->cf_parents; memregcf==NULL && *p >= 0; p++)
845 				if (cfdata[*p].cf_driver == &obio_cd)
846 					memregcf = cf;
847 		}
848 		if (memregcf == NULL)
849 			panic("cpu_configure: no memreg found!");
850 
851 		rr.rr_iospace = PMAP_OBIO;
852 		rr.rr_paddr = (void *)memregcf->cf_loc[0];
853 		rr.rr_len = NBPG;
854 		par_err_reg = (u_int *)bus_map(&rr, NBPG);
855 		if (par_err_reg == NULL)
856 			panic("cpu_configure: ROM hasn't mapped memreg!");
857 	}
858 #endif
859 #if defined(SUN4C)
860 	if (CPU_ISSUN4C) {
861 		node = findroot();
862 		cp = getpropstring(node, "device_type");
863 		if (strcmp(cp, "cpu") != 0)
864 			panic("PROM root device type = %s (need CPU)", cp);
865 	}
866 #endif
867 #if defined(SUN4M)
868 	if (CPU_ISSUN4M)
869 		node = findroot();
870 #endif
871 
872 	*promvec->pv_synchook = sync_crash;
873 
874 	oca.ca_ra.ra_node = node;
875 	oca.ca_ra.ra_name = cp = "mainbus";
876 	if (config_rootfound(cp, (void *)&oca) == NULL)
877 		panic("mainbus not configured");
878 
879 	/* Enable device interrupts */
880 #if defined(SUN4M)
881 	if (CPU_ISSUN4M)
882 		ienab_bic(SINTR_MA);
883 #endif
884 #if defined(SUN4) || defined(SUN4C)
885 	if (CPU_ISSUN4OR4C)
886 		ienab_bis(IE_ALLIE);
887 #endif
888 	(void)spl0();
889 
890 	/*
891 	 * Configure swap area and related system
892 	 * parameter based on device(s) used.
893 	 */
894 	setroot();
895 	swapconf();
896 	cold = 0;
897 
898 	/*
899 	 * Re-zero proc0's user area, to nullify the effect of the
900 	 * stack running into it during auto-configuration.
901 	 * XXX - should fix stack usage.
902 	 */
903 	s = splhigh();
904 	bzero(proc0paddr, sizeof(struct user));
905 
906 	pmap_redzone();
907 	splx(s);
908 }
909 
910 /*
911  * Console 'sync' command.  SunOS just does a 'panic: zero' so I guess
912  * no one really wants anything fancy...
913  */
914 void
sync_crash()915 sync_crash()
916 {
917 
918 	panic("PROM sync command");
919 }
920 
921 char *
clockfreq(freq)922 clockfreq(freq)
923 	register int freq;
924 {
925 	register char *p;
926 	static char buf[10];
927 
928 	freq /= 1000;
929 	snprintf(buf, sizeof buf, "%d", freq / 1000);
930 	freq %= 1000;
931 	if (freq) {
932 		freq += 1000;	/* now in 1000..1999 */
933 		p = buf + strlen(buf);
934 		snprintf(p, buf + sizeof buf - p, "%d", freq);
935 		*p = '.';	/* now buf = %d.%3d */
936 	}
937 	return (buf);
938 }
939 
940 /* ARGSUSED */
941 static int
mbprint(aux,name)942 mbprint(aux, name)
943 	void *aux;
944 	const char *name;
945 {
946 	register struct confargs *ca = aux;
947 
948 	if (name)
949 		printf("%s at %s", ca->ca_ra.ra_name, name);
950 	if (ca->ca_ra.ra_paddr)
951 		printf(" %saddr 0x%x", ca->ca_ra.ra_iospace ? "io" : "",
952 		    (int)ca->ca_ra.ra_paddr);
953 	return (UNCONF);
954 }
955 
956 int
findroot()957 findroot()
958 {
959 	register int node;
960 
961 	if ((node = rootnode) == 0 && (node = nextsibling(0)) == 0)
962 		panic("no PROM root device");
963 	rootnode = node;
964 	return (node);
965 }
966 
967 /*
968  * Given a 'first child' node number, locate the node with the given name.
969  * Return the node number, or 0 if not found.
970  */
971 int
findnode(first,name)972 findnode(first, name)
973 	int first;
974 	register const char *name;
975 {
976 	register int node;
977 
978 	for (node = first; node; node = nextsibling(node))
979 		if (strcmp(getpropstring(node, "name"), name) == 0)
980 			return (node);
981 	return (0);
982 }
983 
984 /*
985  * Fill in a romaux.  Returns 1 on success, 0 if the register property
986  * was not the right size.
987  */
988 int
romprop(rp,cp,node)989 romprop(rp, cp, node)
990 	register struct romaux *rp;
991 	const char *cp;
992 	register int node;
993 {
994 	int len, n;
995 	union { char regbuf[256]; struct rom_reg rr[RA_MAXREG]; } u;
996 	static const char pl[] = "property length";
997 
998 	bzero(u.regbuf, sizeof u);
999 	len = getprop(node, "reg", (void *)u.regbuf, sizeof(u.regbuf));
1000 	if (len == -1 &&
1001 	    node_has_property(node, "device_type") &&
1002 	    strcmp(getpropstring(node, "device_type"), "hierarchical") == 0)
1003 		len = 0;
1004 	if (len % sizeof(struct rom_reg)) {
1005 		printf("%s \"reg\" %s = %d (need multiple of %lu)\n",
1006 			cp, pl, len, sizeof(struct rom_reg));
1007 		return (0);
1008 	}
1009 	if (len > RA_MAXREG * sizeof(struct rom_reg))
1010 		printf("warning: %s \"reg\" %s %d > %lu, excess ignored\n",
1011 		    cp, pl, len, RA_MAXREG * sizeof(struct rom_reg));
1012 	rp->ra_node = node;
1013 	rp->ra_name = cp;
1014 	rp->ra_nreg = len / sizeof(struct rom_reg);
1015 	bcopy(u.rr, rp->ra_reg, len);
1016 
1017 	len = getprop(node, "address", (void *)rp->ra_vaddrs,
1018 		      sizeof(rp->ra_vaddrs));
1019 	if (len == -1) {
1020 		rp->ra_vaddr = 0;	/* XXX - driver compat */
1021 		len = 0;
1022 	}
1023 	if (len & 3) {
1024 		printf("%s \"address\" %s = %d (need multiple of 4)\n",
1025 		    cp, pl, len);
1026 		len = 0;
1027 	}
1028 	rp->ra_nvaddrs = len >> 2;
1029 
1030 	len = getprop(node, "intr", (void *)&rp->ra_intr, sizeof rp->ra_intr);
1031 	if (len == -1)
1032 		len = 0;
1033 
1034 	/*
1035 	 * Some SBus cards only provide an "interrupts" properly, listing
1036 	 * SBus levels. But since obio devices will usually also provide
1037 	 * both properties, only check for "interrupts" last.
1038 	 */
1039 	if (len == 0) {
1040 		u_int32_t *interrupts;
1041 		len = getproplen(node, "interrupts");
1042 		if (len > 0 &&
1043 		    (interrupts = malloc(len, M_TEMP, M_NOWAIT)) != NULL) {
1044 			/* Build rom_intr structures from the list */
1045 			getprop(node, "interrupts", interrupts, len);
1046 			len /= sizeof(u_int32_t);
1047 			for (n = 0; n < len; n++) {
1048 				rp->ra_intr[n].int_pri = CPU_ISSUN4M ?
1049 				    intr_sbus2ipl_4m[interrupts[n]] :
1050 				    intr_sbus2ipl_4c[interrupts[n]];
1051 				rp->ra_intr[n].int_vec = 0;
1052 			};
1053 			len *= sizeof(struct rom_intr);
1054 			free(interrupts, M_TEMP);
1055 		} else
1056 			len = 0;
1057 	}
1058 
1059 	if (len & 7) {
1060 		printf("%s \"intr\" %s = %d (need multiple of 8)\n",
1061 		    cp, pl, len);
1062 		len = 0;
1063 	}
1064 	rp->ra_nintr = len >>= 3;
1065 	/* SPARCstation interrupts are not hardware-vectored */
1066 	while (--len >= 0) {
1067 		if (rp->ra_intr[len].int_vec) {
1068 			printf("WARNING: %s interrupt %d has nonzero vector\n",
1069 			    cp, len);
1070 			break;
1071 		}
1072 #if defined(SUN4M)
1073 		if (CPU_ISSUN4M) {
1074 			/* What's in these high bits anyway? */
1075 			rp->ra_intr[len].int_pri &= 0xf;
1076 		}
1077 #endif
1078 
1079 	}
1080 	return (1);
1081 }
1082 
1083 int
mainbus_match(parent,self,aux)1084 mainbus_match(parent, self, aux)
1085 	struct device *parent;
1086 	void *self;
1087 	void *aux;
1088 {
1089 	struct cfdata *cf = self;
1090 	register struct confargs *ca = aux;
1091 	register struct romaux *ra = &ca->ca_ra;
1092 
1093 	return (strcmp(cf->cf_driver->cd_name, ra->ra_name) == 0);
1094 }
1095 
1096 int autoconf_nzs = 0;	/* must be global so obio.c can see it */
1097 
1098 /*
1099  * Attach the mainbus.
1100  *
1101  * Our main job is to attach the CPU (the root node we got in cpu_configure())
1102  * and iterate down the list of 'mainbus devices' (children of that node).
1103  * We also record the 'node id' of the default frame buffer, if any.
1104  */
1105 static void
mainbus_attach(parent,dev,aux)1106 mainbus_attach(parent, dev, aux)
1107 	struct device *parent, *dev;
1108 	void *aux;
1109 {
1110 	struct confargs oca;
1111 	register const char *const *ssp, *sp = NULL;
1112 	struct confargs *ca = aux;
1113 #if defined(SUN4C) || defined(SUN4M)
1114 	register int node0, node;
1115 	const char *const *openboot_special;
1116 #define L1A_HACK		/* XXX hack to allow L1-A during autoconf */
1117 #ifdef L1A_HACK
1118 	int audio = 0;
1119 #endif
1120 #endif
1121 #if defined(SUN4)
1122 	static const char *const oldmon_special[] = {
1123 		"vmel",
1124 		"vmes",
1125 		"led",
1126 		NULL
1127 	};
1128 #endif /* SUN4 */
1129 
1130 #if defined(SUN4C)
1131 	static const char *const openboot_special4c[] = {
1132 		/* find these first (end with empty string) */
1133 		"memory-error",	/* as early as convenient, in case of error */
1134 		"eeprom",
1135 		"counter-timer",
1136 		"auxiliary-io",
1137 		"",
1138 
1139 		/* ignore these (end with NULL) */
1140 		"aliases",
1141 		"interrupt-enable",
1142 		"memory",
1143 		"openprom",
1144 		"options",
1145 		"packages",
1146 		"virtual-memory",
1147 		NULL
1148 	};
1149 #else
1150 #define openboot_special4c	((void *)0)
1151 #endif
1152 #if defined(SUN4M)
1153 	static const char *const openboot_special4m[] = {
1154 		/* find these first */
1155 		"obio",		/* smart enough to get eeprom/etc mapped */
1156 		"",
1157 
1158 		/* ignore these (end with NULL) */
1159 		/*
1160 		 * These are _root_ devices to ignore. Others must be handled
1161 		 * elsewhere.
1162 		 */
1163 		"SUNW,sx",		/* XXX: no driver for SX yet */
1164 		"eccmemctl",
1165 		"virtual-memory",
1166 		"aliases",
1167 		"memory",
1168 		"openprom",
1169 		"options",
1170 		"packages",
1171 		/* we also skip any nodes with device_type == "cpu" */
1172 		NULL
1173 	};
1174 #else
1175 #define openboot_special4m	((void *)0)
1176 #endif
1177 
1178 	if (CPU_ISSUN4)
1179 		snprintf(mainbus_model, sizeof mainbus_model,
1180 			"SUN-4/%d series", cpuinfo.classlvl);
1181 	else
1182 		strlcat(mainbus_model, getpropstring(ca->ca_ra.ra_node,"name"),
1183 			sizeof mainbus_model);
1184 	printf(": %s\n", mainbus_model);
1185 
1186 	/*
1187 	 * Locate and configure the 'early' devices.  These must be
1188 	 * configured before we can do the rest.  For instance, the
1189 	 * EEPROM contains the Ethernet address for the LANCE chip.
1190 	 * If the device cannot be located or configured, panic.
1191 	 */
1192 
1193 #if defined(SUN4)
1194 	if (CPU_ISSUN4) {
1195 		/* Configure the CPU. */
1196 		bzero(&oca, sizeof(oca));
1197 		oca.ca_ra.ra_name = "cpu";
1198 		(void)config_found(dev, (void *)&oca, mbprint);
1199 
1200 		/* Start at the beginning of the bootpath */
1201 		bzero(&oca, sizeof(oca));
1202 		oca.ca_ra.ra_bp = bootpath;
1203 
1204 		oca.ca_bustype = BUS_MAIN;
1205 		oca.ca_ra.ra_name = "obio";
1206 		if (config_found(dev, (void *)&oca, mbprint) == NULL)
1207 			panic("obio missing");
1208 
1209 		for (ssp = oldmon_special; (sp = *ssp) != NULL; ssp++) {
1210 			oca.ca_bustype = BUS_MAIN;
1211 			oca.ca_ra.ra_name = sp;
1212 			(void)config_found(dev, (void *)&oca, mbprint);
1213 		}
1214 		return;
1215 	}
1216 #endif
1217 
1218 /*
1219  * The rest of this routine is for OBP machines exclusively.
1220  */
1221 #if defined(SUN4C) || defined(SUN4M)
1222 
1223 	openboot_special = CPU_ISSUN4M
1224 				? openboot_special4m
1225 				: openboot_special4c;
1226 
1227 	node = ca->ca_ra.ra_node;	/* i.e., the root node */
1228 
1229 	/* the first early device to be configured is the cpu */
1230 	if (CPU_ISSUN4M) {
1231 		/* XXX - what to do on multiprocessor machines? */
1232 		register const char *cp;
1233 
1234 		for (node = firstchild(node); node; node = nextsibling(node)) {
1235 			cp = getpropstring(node, "device_type");
1236 			if (strcmp(cp, "cpu") == 0) {
1237 				bzero(&oca, sizeof(oca));
1238 				oca.ca_ra.ra_node = node;
1239 				oca.ca_ra.ra_name = "cpu";
1240 				oca.ca_ra.ra_paddr = 0;
1241 				oca.ca_ra.ra_nreg = 0;
1242 				config_found(dev, (void *)&oca, mbprint);
1243 			}
1244 		}
1245 	} else if (CPU_ISSUN4C) {
1246 		bzero(&oca, sizeof(oca));
1247 		oca.ca_ra.ra_node = node;
1248 		oca.ca_ra.ra_name = "cpu";
1249 		oca.ca_ra.ra_paddr = 0;
1250 		oca.ca_ra.ra_nreg = 0;
1251 		config_found(dev, (void *)&oca, mbprint);
1252 	}
1253 
1254 	node = ca->ca_ra.ra_node;	/* re-init root node */
1255 
1256 	if (promvec->pv_romvec_vers <= 2) {
1257 		/*
1258 		 * Revision 1 prom will always return a framebuffer device
1259 		 * node if a framebuffer is installed, even if console is
1260 		 * set to serial.
1261 		 */
1262 		if (*promvec->pv_stdout != PROMDEV_SCREEN)
1263 			fbnode = 0;
1264 		else {
1265 			/* remember which frame buffer is the console */
1266 			fbnode = getpropint(node, "fb", 0);
1267 		}
1268 	}
1269 
1270 	/* Find the "options" node */
1271 	node0 = firstchild(node);
1272 	optionsnode = findnode(node0, "options");
1273 	if (optionsnode == 0)
1274 		panic("no options in OPENPROM");
1275 
1276 	/* Start at the beginning of the bootpath */
1277 	oca.ca_ra.ra_bp = bootpath;
1278 
1279 	for (ssp = openboot_special; *(sp = *ssp) != 0; ssp++) {
1280 		if ((node = findnode(node0, sp)) == 0) {
1281 			printf("could not find %s in OPENPROM\n", sp);
1282 			panic(sp);
1283 		}
1284 		oca.ca_bustype = BUS_MAIN;
1285 		if (!romprop(&oca.ca_ra, sp, node) ||
1286 		    (config_found(dev, (void *)&oca, mbprint) == NULL))
1287 			panic(sp);
1288 	}
1289 
1290 	/*
1291 	 * Configure the rest of the devices, in PROM order.  Skip
1292 	 * PROM entries that are not for devices, or which must be
1293 	 * done before we get here.
1294 	 */
1295 	for (node = node0; node; node = nextsibling(node)) {
1296 		register const char *cp;
1297 
1298 #if defined(SUN4M)
1299 		if (CPU_ISSUN4M) /* skip the CPUs */
1300 			if (node_has_property(node, "device_type") &&
1301 			    !strcmp(getpropstring(node, "device_type"), "cpu"))
1302 				continue;
1303 #endif
1304 		cp = getpropstring(node, "name");
1305 		for (ssp = openboot_special; (sp = *ssp) != NULL; ssp++)
1306 			if (strcmp(cp, sp) == 0)
1307 				break;
1308 		if (sp == NULL && romprop(&oca.ca_ra, cp, node)) {
1309 #ifdef L1A_HACK
1310 			if (strcmp(cp, "audio") == 0)
1311 				audio = 1;
1312 			if (strcmp(cp, "zs") == 0)
1313 				autoconf_nzs++;
1314 			if (/*audio &&*/ autoconf_nzs >= 2)	/*XXX*/
1315 				splx(11 << 8);		/*XXX*/
1316 #endif
1317 			oca.ca_bustype = BUS_MAIN;
1318 			(void) config_found(dev, (void *)&oca, mbprint);
1319 		}
1320 	}
1321 #endif /* SUN4C || SUN4M */
1322 }
1323 
1324 struct cfattach mainbus_ca = {
1325 	sizeof(struct device), mainbus_match, mainbus_attach
1326 };
1327 
1328 struct cfdriver mainbus_cd = {
1329 	NULL, "mainbus", DV_DULL
1330 };
1331 
1332 /*
1333  * findzs() is called from the zs driver (which is, at least in theory,
1334  * generic to any machine with a Zilog ZSCC chip).  It should return the
1335  * address of the corresponding zs channel.  It may not fail, and it
1336  * may be called before the VM code can be used.  Here we count on the
1337  * FORTH PROM to map in the required zs chips.
1338  */
1339 void *
findzs(zs)1340 findzs(zs)
1341 	int zs;
1342 {
1343 
1344 #if defined(SUN4)
1345 #define ZS0_PHYS	0xf1000000
1346 #define ZS1_PHYS	0xf0000000
1347 #define ZS2_PHYS	0xe0000000
1348 
1349 	if (CPU_ISSUN4) {
1350 		struct rom_reg rr;
1351 		register void *vaddr;
1352 
1353 		switch (zs) {
1354 		case 0:
1355 			rr.rr_paddr = (void *)ZS0_PHYS;
1356 			break;
1357 		case 1:
1358 			rr.rr_paddr = (void *)ZS1_PHYS;
1359 			break;
1360 		case 2:
1361 			rr.rr_paddr = (void *)ZS2_PHYS;
1362 			break;
1363 		default:
1364 			panic("findzs: unknown zs device %d", zs);
1365 		}
1366 
1367 		rr.rr_iospace = PMAP_OBIO;
1368 		rr.rr_len = NBPG;
1369 		vaddr = bus_map(&rr, NBPG);
1370 		if (vaddr)
1371 			return (vaddr);
1372 	}
1373 #endif
1374 
1375 #if defined(SUN4C) || defined(SUN4M)
1376 	if (CPU_ISSUN4COR4M) {
1377 		int node;
1378 
1379 		node = firstchild(findroot());
1380 		if (CPU_ISSUN4M) { /* zs is in "obio" tree on Sun4M */
1381 			node = findnode(node, "obio");
1382 			if (!node)
1383 				panic("findzs: no obio node");
1384 			node = firstchild(node);
1385 		}
1386 		while ((node = findnode(node, "zs")) != 0) {
1387 			int vaddrs[10];
1388 
1389 			if (getpropint(node, "slave", -1) != zs) {
1390 				node = nextsibling(node);
1391 				continue;
1392 			}
1393 
1394 			/*
1395 			 * On some machines (e.g. the Voyager), the zs
1396 			 * device has multi-valued register properties.
1397 			 */
1398 			if (getprop(node, "address",
1399 			    (void *)vaddrs, sizeof(vaddrs)) != 0)
1400 				return ((void *)vaddrs[0]);
1401 		}
1402 		return (NULL);
1403 	}
1404 #endif
1405 	panic("findzs: cannot find zs%d", zs);
1406 	/* NOTREACHED */
1407 }
1408 
1409 int
makememarr(ap,max,which)1410 makememarr(ap, max, which)
1411 	register struct memarr *ap;
1412 	int max, which;
1413 {
1414 #if defined(SUN4C) || defined(SUN4M)
1415 	struct v2rmi {
1416 		int	zero;
1417 		int	addr;
1418 		int	len;
1419 	} v2rmi[200];		/* version 2 rom meminfo layout */
1420 #define	MAXMEMINFO (sizeof(v2rmi) / sizeof(*v2rmi))
1421 	register struct v0mlist *mp;
1422 	register int i, node, len;
1423 	char *prop;
1424 #endif
1425 
1426 #if defined(SUN4)
1427 	if (CPU_ISSUN4) {
1428 		switch (which) {
1429 		case MEMARR_AVAILPHYS:
1430 			ap[0].addr = 0;
1431 			ap[0].len = *oldpvec->memoryAvail;
1432 			break;
1433 		case MEMARR_TOTALPHYS:
1434 			ap[0].addr = 0;
1435 			ap[0].len = *oldpvec->memorySize;
1436 			break;
1437 		default:
1438 			printf("pre_panic: makememarr");
1439 			break;
1440 		}
1441 		return (1);
1442 	}
1443 #endif
1444 #if defined(SUN4C) || defined(SUN4M)
1445 	switch (i = promvec->pv_romvec_vers) {
1446 
1447 	case 0:
1448 		/*
1449 		 * Version 0 PROMs use a linked list to describe these
1450 		 * guys.
1451 		 */
1452 		switch (which) {
1453 
1454 		case MEMARR_AVAILPHYS:
1455 			mp = *promvec->pv_v0mem.v0_physavail;
1456 			break;
1457 
1458 		case MEMARR_TOTALPHYS:
1459 			mp = *promvec->pv_v0mem.v0_phystot;
1460 			break;
1461 
1462 		default:
1463 			panic("makememarr");
1464 		}
1465 		for (i = 0; mp != NULL; mp = mp->next, i++) {
1466 			if (i >= max)
1467 				goto overflow;
1468 			ap->addr = (u_int)mp->addr;
1469 			ap->len = mp->nbytes;
1470 			ap++;
1471 		}
1472 		break;
1473 
1474 	default:
1475 		printf("makememarr: hope version %d PROM is like version 2\n",
1476 		    i);
1477 		/* FALLTHROUGH */
1478 
1479         case 3:
1480 	case 2:
1481 		/*
1482 		 * Version 2 PROMs use a property array to describe them.
1483 		 */
1484 		if (max > MAXMEMINFO) {
1485 			printf("makememarr: limited to %lu\n", MAXMEMINFO);
1486 			max = MAXMEMINFO;
1487 		}
1488 		if ((node = findnode(firstchild(findroot()), "memory")) == 0)
1489 			panic("makememarr: cannot find \"memory\" node");
1490 		switch (which) {
1491 
1492 		case MEMARR_AVAILPHYS:
1493 			prop = "available";
1494 			break;
1495 
1496 		case MEMARR_TOTALPHYS:
1497 			prop = "reg";
1498 			break;
1499 
1500 		default:
1501 			panic("makememarr");
1502 		}
1503 		len = getprop(node, prop, (void *)v2rmi, sizeof v2rmi) /
1504 		    sizeof(struct v2rmi);
1505 		for (i = 0; i < len; i++) {
1506 			if (i >= max)
1507 				goto overflow;
1508 			ap->addr = v2rmi[i].addr;
1509 			ap->len = v2rmi[i].len;
1510 			ap++;
1511 		}
1512 		break;
1513 	}
1514 
1515 	/*
1516 	 * Success!  (Hooray)
1517 	 */
1518 	if (i == 0)
1519 		panic("makememarr: no memory found");
1520 	return (i);
1521 
1522 overflow:
1523 	/*
1524 	 * Oops, there are more things in the PROM than our caller
1525 	 * provided space for.  Truncate any extras.
1526 	 */
1527 	printf("makememarr: WARNING: lost some memory\n");
1528 	return (i);
1529 #endif
1530 }
1531 
1532 /*
1533  * Internal form of getprop().  Returns the actual length.
1534  */
1535 int
getprop(node,name,buf,bufsiz)1536 getprop(node, name, buf, bufsiz)
1537 	int node;
1538 	char *name;
1539 	void *buf;
1540 	register int bufsiz;
1541 {
1542 #if defined(SUN4C) || defined(SUN4M)
1543 	register struct nodeops *no;
1544 	register int len;
1545 #endif
1546 
1547 #if defined(SUN4)
1548 	if (CPU_ISSUN4) {
1549 		printf("WARNING: getprop not valid on sun4! %s\n", name);
1550 		return (0);
1551 	}
1552 #endif
1553 
1554 #if defined(SUN4C) || defined(SUN4M)
1555 	no = promvec->pv_nodeops;
1556 	len = no->no_proplen(node, name);
1557 	if (len > bufsiz) {
1558 		printf("node 0x%x property %s length %d > %d\n",
1559 		    node, name, len, bufsiz);
1560 #ifdef DEBUG
1561 		panic("getprop");
1562 #else
1563 		return (0);
1564 #endif
1565 	}
1566 	no->no_getprop(node, name, buf);
1567 	return (len);
1568 #endif
1569 }
1570 
1571 /*
1572  * Internal form of proplen().  Returns the property length.
1573  */
1574 int
getproplen(node,name)1575 getproplen(node, name)
1576 	int node;
1577 	char *name;
1578 {
1579 	register struct nodeops *no = promvec->pv_nodeops;
1580 
1581 	return (no->no_proplen(node, name));
1582 }
1583 
1584 /*
1585  * Return a string property.  There is a (small) limit on the length;
1586  * the string is fetched into a static buffer which is overwritten on
1587  * subsequent calls.
1588  */
1589 char *
getpropstring(node,name)1590 getpropstring(node, name)
1591 	int node;
1592 	char *name;
1593 {
1594 	register int len;
1595 	static char stringbuf[32];
1596 
1597 	len = getprop(node, name, (void *)stringbuf, sizeof stringbuf - 1);
1598 	if (len == -1)
1599 		len = 0;
1600 	stringbuf[len] = '\0';	/* usually unnecessary */
1601 	return (stringbuf);
1602 }
1603 
1604 /*
1605  * Fetch an integer (or pointer) property.
1606  * The return value is the property, or the default if there was none.
1607  */
1608 int
getpropint(node,name,deflt)1609 getpropint(node, name, deflt)
1610 	int node;
1611 	char *name;
1612 	int deflt;
1613 {
1614 	register int len;
1615 	char intbuf[16];
1616 
1617 	len = getprop(node, name, (void *)intbuf, sizeof intbuf);
1618 	if (len != 4)
1619 		return (deflt);
1620 	return (*(int *)intbuf);
1621 }
1622 
1623 /*
1624  * OPENPROM functions.  These are here mainly to hide the OPENPROM interface
1625  * from the rest of the kernel.
1626  */
1627 int
firstchild(node)1628 firstchild(node)
1629 	int node;
1630 {
1631 
1632 	return (promvec->pv_nodeops->no_child(node));
1633 }
1634 
1635 int
nextsibling(node)1636 nextsibling(node)
1637 	int node;
1638 {
1639 
1640 	return (promvec->pv_nodeops->no_nextnode(node));
1641 }
1642 
1643 u_int      hexatoi(const char *);
1644 
1645 /* The following recursively searches a PROM tree for a given node */
1646 int
search_prom(rootnode,name)1647 search_prom(rootnode, name)
1648         register int rootnode;
1649         register char *name;
1650 {
1651         register int rtnnode;
1652         register int node = rootnode;
1653 
1654         if (node == findroot() || !strcmp("hierarchical",
1655                                           getpropstring(node, "device_type")))
1656             node = firstchild(node);
1657 
1658         if (!node)
1659             panic("search_prom: null node");
1660 
1661         do {
1662                 if (strcmp(getpropstring(node, "name"),name) == 0)
1663                     return node;
1664 
1665                 if (node_has_property(node,"device_type") &&
1666                     (!strcmp(getpropstring(node, "device_type"),"hierarchical")
1667                      || !strcmp(getpropstring(node, "name"),"iommu"))
1668                     && (rtnnode = search_prom(node, name)) != 0)
1669                         return rtnnode;
1670 
1671         } while ((node = nextsibling(node)));
1672 
1673         return 0;
1674 }
1675 
1676 /* The following are used primarily in consinit() */
1677 
1678 int
opennode(path)1679 opennode(path)		/* translate phys. device path to node */
1680 	register char *path;
1681 {
1682 	register int fd;
1683 
1684 	if (promvec->pv_romvec_vers < 2) {
1685 		printf("WARNING: opennode not valid on sun4! %s\n", path);
1686 		return (0);
1687 	}
1688 	fd = promvec->pv_v2devops.v2_open(path);
1689 	if (fd == 0)
1690 		return 0;
1691 	return promvec->pv_v2devops.v2_fd_phandle(fd);
1692 }
1693 
1694 int
node_has_property(node,prop)1695 node_has_property(node, prop)	/* returns 1 if node has given property */
1696 	register int node;
1697 	register const char *prop;
1698 {
1699 
1700 	return ((*promvec->pv_nodeops->no_proplen)(node, (caddr_t)prop) != -1);
1701 }
1702 
1703 /* Pass a string to the FORTH PROM to be interpreted */
1704 void
rominterpret(s)1705 rominterpret(s)
1706 	register char *s;
1707 {
1708 
1709 	if (promvec->pv_romvec_vers < 2)
1710 		promvec->pv_fortheval.v0_eval(strlen(s), s);
1711 	else
1712 		promvec->pv_fortheval.v2_eval(s);
1713 }
1714 
1715 /*
1716  * Try to figure out where the PROM stores the cursor row & column
1717  * variables.  Returns nonzero on error.
1718  */
1719 int
romgetcursoraddr(rowp,colp)1720 romgetcursoraddr(rowp, colp)
1721 	register int **rowp, **colp;
1722 {
1723 	char buf[100];
1724 
1725 	/*
1726 	 * line# and column# are global in older proms (rom vector < 2)
1727 	 * and in some newer proms.  They are local in version 2.9.  The
1728 	 * correct cutoff point is unknown, as yet; we use 2.9 here.
1729 	 */
1730 	if (promvec->pv_romvec_vers < 2 || promvec->pv_printrev < 0x00020009)
1731 		snprintf(buf, sizeof buf,
1732 		    "' line# >body >user %lx ! ' column# >body >user %lx !",
1733 		    (u_long)rowp, (u_long)colp);
1734 	else
1735 		snprintf(buf, sizeof buf,
1736 		    "stdout @ is my-self addr line# %lx ! addr column# %lx !",
1737 		    (u_long)rowp, (u_long)colp);
1738 	*rowp = *colp = NULL;
1739 	rominterpret(buf);
1740 	return (*rowp == NULL || *colp == NULL);
1741 }
1742 
1743 void
romhalt()1744 romhalt()
1745 {
1746 	if (CPU_ISSUN4COR4M)
1747 		*promvec->pv_synchook = NULL;
1748 
1749 	promvec->pv_halt();
1750 	panic("PROM exit failed");
1751 }
1752 
1753 void
romboot(str)1754 romboot(str)
1755 	char *str;
1756 {
1757 	if (CPU_ISSUN4COR4M)
1758 		*promvec->pv_synchook = NULL;
1759 
1760 	promvec->pv_reboot(str);
1761 	panic("PROM boot failed");
1762 }
1763 
1764 void
callrom()1765 callrom()
1766 {
1767 
1768 #if 0			/* sun4c FORTH PROMs do this for us */
1769 	if (CPU_ISSUN4)
1770 		fb_unblank();
1771 #endif
1772 	promvec->pv_abort();
1773 }
1774 
1775 /*
1776  * Configure swap space and related parameters.
1777  */
1778 void
swapconf()1779 swapconf()
1780 {
1781 	register struct swdevt *swp;
1782 	register int nblks;
1783 
1784 	for (swp = swdevt; swp->sw_dev != NODEV; swp++)
1785 		if (bdevsw[major(swp->sw_dev)].d_psize) {
1786 			nblks =
1787 			  (*bdevsw[major(swp->sw_dev)].d_psize)(swp->sw_dev);
1788 			if (nblks != -1 &&
1789 			    (swp->sw_nblks == 0 || swp->sw_nblks > nblks))
1790 				swp->sw_nblks = nblks;
1791 			swp->sw_nblks = ctod(dtoc(swp->sw_nblks));
1792 		}
1793 	dumpconf();
1794 }
1795 
1796 #if 0 /* Unused */
1797 dev_t	bootdev;
1798 #endif
1799 
1800 struct nam2blk {
1801 	char *name;
1802 	int maj;
1803 } nam2blk[] = {
1804 	{ "xy",		 3 },
1805 	{ "sd",		 7 },
1806 	{ "xd",		10 },
1807 	{ "st",		11 },
1808 	{ "fd",		16 },
1809 	{ "rd",		17 },
1810 	{ "cd",		18 },
1811 	{ "raid",	25 },
1812 };
1813 
1814 int
findblkmajor(dv)1815 findblkmajor(dv)
1816 	struct device *dv;
1817 {
1818 	char *name = dv->dv_xname;
1819 	int i;
1820 
1821 	for (i = 0; i < sizeof(nam2blk)/sizeof(nam2blk[0]); ++i)
1822 		if (strncmp(name, nam2blk[i].name, strlen(nam2blk[i].name)) == 0)
1823 			return (nam2blk[i].maj);
1824 	return (-1);
1825 }
1826 
1827 char *
findblkname(maj)1828 findblkname(maj)
1829 	int maj;
1830 {
1831 	int i;
1832 
1833 	for (i = 0; i < sizeof(nam2blk)/sizeof(nam2blk[0]); ++i)
1834 		if (nam2blk[i].maj == maj)
1835 			return (nam2blk[i].name);
1836 	return (NULL);
1837 }
1838 
1839 static struct device *
getdisk(str,len,defpart,devp)1840 getdisk(str, len, defpart, devp)
1841 	char *str;
1842 	int len, defpart;
1843 	dev_t *devp;
1844 {
1845 	register struct device *dv;
1846 
1847 	if ((dv = parsedisk(str, len, defpart, devp)) == NULL) {
1848 		printf("use one of:");
1849 #ifdef RAMDISK_HOOKS
1850 		printf(" %s[a-p]", fakerdrootdev.dv_xname);
1851 #endif
1852 		for (dv = alldevs.tqh_first; dv != NULL;
1853 		    dv = dv->dv_list.tqe_next) {
1854 			if (dv->dv_class == DV_DISK)
1855 				printf(" %s[a-p]", dv->dv_xname);
1856 #ifdef NFSCLIENT
1857 			if (dv->dv_class == DV_IFNET)
1858 				printf(" %s", dv->dv_xname);
1859 #endif
1860 		}
1861 		printf("\n");
1862 	}
1863 	return (dv);
1864 }
1865 
1866 struct device *
parsedisk(str,len,defpart,devp)1867 parsedisk(str, len, defpart, devp)
1868 	char *str;
1869 	int len, defpart;
1870 	dev_t *devp;
1871 {
1872 	register struct device *dv;
1873 	register char *cp, c;
1874 	int majdev, unit, part;
1875 
1876 	if (len == 0)
1877 		return (NULL);
1878 	cp = str + len - 1;
1879 	c = *cp;
1880 	if (c >= 'a' && (c - 'a') < MAXPARTITIONS) {
1881 		part = c - 'a';
1882 		*cp = '\0';
1883 	} else
1884 		part = defpart;
1885 
1886 #ifdef RAMDISK_HOOKS
1887 	if (strcmp(str, fakerdrootdev.dv_xname) == 0) {
1888 		dv = &fakerdrootdev;
1889 		goto gotdisk;
1890 	}
1891 #endif
1892 
1893 	for (dv = alldevs.tqh_first; dv != NULL; dv = dv->dv_list.tqe_next) {
1894 		if (dv->dv_class == DV_DISK &&
1895 		    strcmp(str, dv->dv_xname) == 0) {
1896 #ifdef RAMDISK_HOOKS
1897 gotdisk:
1898 #endif
1899 			majdev = findblkmajor(dv);
1900 			unit = dv->dv_unit;
1901 			if (majdev < 0)
1902 				panic("parsedisk");
1903 			*devp = MAKEDISKDEV(majdev, unit, part);
1904 			break;
1905 		}
1906 #ifdef NFSCLIENT
1907 		if (dv->dv_class == DV_IFNET &&
1908 		    strcmp(str, dv->dv_xname) == 0) {
1909 			*devp = NODEV;
1910 			break;
1911 		}
1912 #endif
1913 	}
1914 
1915 	*cp = c;
1916 	return (dv);
1917 }
1918 
1919 void
1920 mountroot_hook_establish(func, dev)
1921 	void (*func)(struct device *);
1922 	struct device *dev;
1923 {
1924 	struct mountroot_hook *mrhp;
1925 
1926 	mrhp = (struct mountroot_hook *)malloc(sizeof(struct mountroot_hook),
1927 	    M_DEVBUF, M_NOWAIT);
1928 	if (mrhp == NULL)
1929 		panic("no memory for mountroot_hook");
1930 
1931 	bzero(mrhp, sizeof(struct mountroot_hook));
1932 	mrhp->mr_device = dev;
1933 	mrhp->mr_func = func;
1934 	LIST_INSERT_HEAD(&mrh_list, mrhp, mr_link);
1935 }
1936 
1937 /*
1938  * Attempt to find the device from which we were booted.
1939  * If we can do so, and not instructed not to do so,
1940  * change rootdev to correspond to the load device.
1941  *
1942  * XXX Actually, swap and root must be on the same type of device,
1943  * (ie. DV_DISK or DV_IFNET) because of how (*mountroot) is written.
1944  * That should be fixed.
1945  */
1946 void
setroot()1947 setroot()
1948 {
1949 	register struct swdevt *swp;
1950 	register struct device *dv;
1951 	register int len, majdev, unit, part;
1952 	dev_t nrootdev, nswapdev = NODEV;
1953 	char buf[128];
1954 	dev_t temp;
1955 	struct mountroot_hook *mrhp;
1956 	struct device *bootdv;
1957 	struct bootpath *bp;
1958 #if defined(NFSCLIENT)
1959 	extern char *nfsbootdevname;
1960 #endif
1961 	extern int rootdev_override;
1962 
1963 	if (rootdev_override)
1964 		mountroot = dk_mountroot;
1965 
1966 	bp = nbootpath == 0 ? NULL : &bootpath[nbootpath-1];
1967 #ifdef RAMDISK_HOOKS
1968 	bootdv = &fakerdrootdev;
1969 #else
1970 	bootdv = (bp == NULL) ? NULL : bp->dev;
1971 #endif
1972 
1973 	part = (bp == NULL) ? 0 : bp->val[2];
1974 	/*
1975 	 * (raid) device auto-configuration could have returned
1976 	 * the root device's id in rootdev.  Check this case.
1977 	 */
1978 	if (rootdev != NODEV) {
1979 		majdev = major(rootdev);
1980 		unit = DISKUNIT(rootdev);
1981 		part = DISKPART(rootdev);
1982 
1983 		len = snprintf(buf, sizeof buf, "%s%d", findblkname(majdev),
1984 			unit);
1985 		if (len >= sizeof(buf))
1986 			panic("setroot: device name too long");
1987 
1988 		bootdv = getdisk(buf, len, part, &rootdev);
1989 	}
1990 
1991 	/*
1992 	 * If 'swap generic' and we couldn't determine boot device,
1993 	 * ask the user.
1994 	 */
1995 	if (mountroot == NULL && bootdv == NULL)
1996 		boothowto |= RB_ASKNAME;
1997 
1998 	if (boothowto & RB_ASKNAME) {
1999 		for (;;) {
2000 			printf("root device ");
2001 			if (bootdv != NULL)
2002 				printf("(default %s%c)",
2003 					bootdv->dv_xname,
2004 					bootdv->dv_class == DV_DISK
2005 						? part+'a' : ' ');
2006 			printf(": ");
2007 			len = getstr(buf, sizeof(buf));
2008 			if (len == 0 && bootdv != NULL) {
2009 				strlcpy(buf, bootdv->dv_xname, sizeof buf);
2010 				len = strlen(buf);
2011 			}
2012 			if (len > 0 && buf[len - 1] == '*') {
2013 				buf[--len] = '\0';
2014 				dv = getdisk(buf, len, 1, &nrootdev);
2015 				if (dv != NULL) {
2016 					bootdv = dv;
2017 					nswapdev = nrootdev;
2018 					goto gotswap;
2019 				}
2020 			}
2021 			dv = getdisk(buf, len, part, &nrootdev);
2022 			if (dv != NULL) {
2023 				bootdv = dv;
2024 				break;
2025 			}
2026 		}
2027 
2028 		/*
2029 		 * because swap must be on same device as root, for
2030 		 * network devices this is easy.
2031 		 */
2032 		if (bootdv->dv_class == DV_IFNET) {
2033 			goto gotswap;
2034 		}
2035 		for (;;) {
2036 			printf("swap device ");
2037 			if (bootdv != NULL)
2038 				printf("(default %s%c)",
2039 					bootdv->dv_xname,
2040 					bootdv->dv_class == DV_DISK?'b':' ');
2041 			printf(": ");
2042 			len = getstr(buf, sizeof(buf));
2043 			if (len == 0 && bootdv != NULL) {
2044 				switch (bootdv->dv_class) {
2045 				case DV_IFNET:
2046 					nswapdev = NODEV;
2047 					break;
2048 				case DV_DISK:
2049 					nswapdev = MAKEDISKDEV(major(nrootdev),
2050 					    DISKUNIT(nrootdev), 1);
2051 					break;
2052 				case DV_TAPE:
2053 				case DV_TTY:
2054 				case DV_DULL:
2055 				case DV_CPU:
2056 					break;
2057 				}
2058 				break;
2059 			}
2060 			dv = getdisk(buf, len, 1, &nswapdev);
2061 			if (dv) {
2062 				if (dv->dv_class == DV_IFNET)
2063 					nswapdev = NODEV;
2064 				break;
2065 			}
2066 		}
2067 gotswap:
2068 		rootdev = nrootdev;
2069 		dumpdev = nswapdev;
2070 		swdevt[0].sw_dev = nswapdev;
2071 		/* swdevt[1].sw_dev = NODEV; */
2072 
2073 	} else if (mountroot == NULL) {
2074 
2075 		/*
2076 		 * 'swap generic': Use the device the ROM told us to use.
2077 		 */
2078 		majdev = findblkmajor(bootdv);
2079 		if (majdev >= 0) {
2080 			/*
2081 			 * Root and swap are on a disk.
2082 			 * val[2] of the boot device is the partition number.
2083 			 * Assume swap is on partition b.
2084 			 */
2085 			unit = bootdv->dv_unit;
2086 			rootdev = MAKEDISKDEV(majdev, unit, part);
2087 			nswapdev = dumpdev = MAKEDISKDEV(major(rootdev),
2088 			    DISKUNIT(rootdev), 1);
2089 		} else {
2090 			/*
2091 			 * Root and swap are on a net.
2092 			 */
2093 			nswapdev = dumpdev = NODEV;
2094 		}
2095 		swdevt[0].sw_dev = nswapdev;
2096 		/* swdevt[1].sw_dev = NODEV; */
2097 
2098 	} else {
2099 
2100 		/*
2101 		 * 'root DEV swap DEV': honour rootdev/swdevt.
2102 		 * rootdev/swdevt/mountroot already properly set.
2103 		 */
2104 		if (bootdv->dv_class == DV_DISK) {
2105 			snprintf(root_devname, 16, "%s%c",
2106 			    bootdv->dv_xname, part + 'a');
2107 			printf("root on %s\n", root_devname);
2108 		}
2109 		majdev = major(rootdev);
2110 		unit = DISKUNIT(rootdev);
2111 		part = DISKPART(rootdev);
2112 		goto gotroot;
2113 	}
2114 
2115 	switch (bootdv->dv_class) {
2116 #if defined(NFSCLIENT)
2117 	case DV_IFNET:
2118 		mountroot = nfs_mountroot;
2119 		nfsbootdevname = bootdv->dv_xname;
2120 		return;
2121 #endif
2122 	case DV_DISK:
2123 		mountroot = dk_mountroot;
2124 		majdev = major(rootdev);
2125 		unit = DISKUNIT(rootdev);
2126 		part = DISKPART(rootdev);
2127 		snprintf(root_devname, 16, "%s%c",
2128 		    bootdv->dv_xname, part + 'a');
2129 		printf("root on %s\n", root_devname);
2130 		break;
2131 	default:
2132 		printf("can't figure root, hope your kernel is right\n");
2133 		return;
2134 	}
2135 
2136 	/*
2137 	 * Make the swap partition on the root drive the primary swap.
2138 	 */
2139 	temp = NODEV;
2140 	for (swp = swdevt; swp->sw_dev != NODEV; swp++) {
2141 		if (majdev == major(swp->sw_dev) &&
2142 		    unit == DISKUNIT(swp->sw_dev)) {
2143 			temp = swdevt[0].sw_dev;
2144 			swdevt[0].sw_dev = swp->sw_dev;
2145 			swp->sw_dev = temp;
2146 			break;
2147 		}
2148 	}
2149 	if (swp->sw_dev != NODEV) {
2150 		/*
2151 		 * If dumpdev was the same as the old primary swap device,
2152 		 * move it to the new primary swap device.
2153 		 */
2154 		if (temp == dumpdev)
2155 			dumpdev = swdevt[0].sw_dev;
2156 	}
2157 
2158  gotroot:
2159 	/*
2160 	 * Find mountroot hook and execute.
2161 	 */
2162 	for (mrhp = mrh_list.lh_first; mrhp != NULL;
2163 	     mrhp = mrhp->mr_link.le_next)
2164 		if (mrhp->mr_device == bootdv) {
2165 			if (findblkmajor(mrhp->mr_device) == major(rootdev))
2166 				(*mrhp->mr_func)(bootdv);
2167 			else
2168 				(*mrhp->mr_func)(NULL);
2169 			break;
2170 		}
2171 
2172 }
2173 
2174 static int
getstr(cp,size)2175 getstr(cp, size)
2176 	register char *cp;
2177 	register int size;
2178 {
2179 	register char *lp;
2180 	register int c;
2181 	register int len;
2182 
2183 	lp = cp;
2184 	len = 0;
2185 	for (;;) {
2186 		c = cngetc();
2187 		switch (c) {
2188 		case '\n':
2189 		case '\r':
2190 			printf("\n");
2191 			*lp++ = '\0';
2192 			return (len);
2193 		case '\b':
2194 		case '\177':
2195 		case '#':
2196 			if (len) {
2197 				--len;
2198 				--lp;
2199 				printf("\b \b");
2200 			}
2201 			continue;
2202 		case '@':
2203 		case 'u'&037:
2204 			len = 0;
2205 			lp = cp;
2206 			printf("\n");
2207 			continue;
2208 		default:
2209 			if (len + 1 >= size || c < ' ') {
2210 				printf("\007");
2211 				continue;
2212 			}
2213 			printf("%c", c);
2214 			++len;
2215 			*lp++ = c;
2216 		}
2217 	}
2218 }
2219 
2220 
2221 /*
2222  * find a device matching "name" and unit number
2223  */
2224 struct device *
getdevunit(name,unit)2225 getdevunit(name, unit)
2226 	char *name;
2227 	int unit;
2228 {
2229 	struct device *dev = alldevs.tqh_first;
2230 	char num[10], fullname[16];
2231 	int lunit;
2232 
2233 	/* compute length of name and decimal expansion of unit number */
2234 	snprintf(num, sizeof num, "%d", unit);
2235 	lunit = strlen(num);
2236 	if (strlen(name) + lunit >= sizeof(fullname) - 1)
2237 		panic("config_attach: device name too long");
2238 
2239 	strlcpy(fullname, name, sizeof fullname);
2240 	strlcat(fullname, num, sizeof fullname);
2241 
2242 	while (strcmp(dev->dv_xname, fullname) != 0) {
2243 		if ((dev = dev->dv_list.tqe_next) == NULL)
2244 			return NULL;
2245 	}
2246 	return dev;
2247 }
2248 
2249 u_int
hexatoi(nptr)2250 hexatoi(nptr)			/* atoi assuming hex, no 0x */
2251 	const char *nptr;
2252 {
2253 	u_int retval;
2254 	str2hex((char *)nptr, &retval);
2255 	return retval;
2256 }
2257