xref: /netbsd-src/sys/arch/x68k/stand/xxboot/bootmain.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: bootmain.c,v 1.4 2012/11/17 19:10:46 tsutsui Exp $	*/
2 
3 /*-
4  * Copyright (c) 1993, 1994 Takumi Nakamura.
5  * Copyright (c) 1999, 2000 Itoh Yasufumi.
6  * Copyright (c) 2001 Minoura Makoto.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by Takumi Nakamura.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <machine/bootinfo.h>
39 #include <lib/libsa/stand.h>
40 #include <lib/libsa/loadfile.h>
41 
42 #include "libx68k.h"
43 #include "iocs.h"
44 #include "exec_image.h"
45 
46 #define EXSCSI_BDID	((void *)0x00ea0001)
47 #define BINF_ISFD(pbinf)	(*((uint8_t *)(pbinf) + 1) == 0)
48 
49 /* boot.S */
50 extern int badbaddr(volatile void *);
51 extern unsigned int ID;		/* target SCSI ID */
52 extern unsigned int BOOT_INFO;	/* result of IOCS(__BOOTINF) */
53 extern struct {
54 	struct fdfmt{
55 		uint8_t	N;	/* sector length 0: 128, ..., 3: 1K */
56 		uint8_t	C;	/* cylinder # */
57 		uint8_t	H;	/* head # */
58 		uint8_t	R;	/* sector # */
59 	} minsec, maxsec;
60 } FDSECMINMAX;			/* FD format type of the first track */
61 
62 /* for debug */
63 unsigned int startregs[16];
64 
65 static int get_scsi_host_adapter(char *);
66 void bootmain(void) __attribute__ ((__noreturn__));
67 
68 /*
69  * Check the type of SCSI interface
70  */
71 static int
72 get_scsi_host_adapter(char *devstr)
73 {
74 	uint8_t *bootrom;
75 	int ha;
76 
77 #ifdef XXBOOT_DEBUG
78 	*(uint32_t *)(devstr +  0) = '/' << 24 | 's' << 16 | 'p' << 8 | 'c';
79 #if defined(CDBOOT)
80 	*(uint32_t *)(devstr +  4) = '@' << 24 | '0' << 16 | '/' << 8 | 'c';
81 #else
82 	*(uint32_t *)(devstr +  4) = '@' << 24 | '0' << 16 | '/' << 8 | 's';
83 #endif
84 	*(uint32_t *)(devstr +  8) = 'd' << 24 | '@' << 16 | '0' << 8 | ',';
85 	*(uint32_t *)(devstr + 12) = '0' << 24 | ':' << 16 | 'a' << 8 | '\0';
86 #endif
87 
88 	bootrom = (uint8_t *)(BOOT_INFO & 0x00ffffe0);
89 	/*
90 	 * bootrom+0x24	"SCSIIN" ... Internal SCSI (spc@0)
91 	 *		"SCSIEX" ... External SCSI (spc@1 or mha@0)
92 	 */
93 	if (*(uint16_t *)(bootrom + 0x24 + 4) == 0x494e) {	/* "IN" */
94 		ha = (X68K_BOOT_SCSIIF_SPC << 4) | 0;
95 	} else if (badbaddr(EXSCSI_BDID)) {
96 		ha = (X68K_BOOT_SCSIIF_MHA << 4) | 0;
97 #ifdef XXBOOT_DEBUG
98 		*(uint32_t *)devstr = '/' << 24 | 'm' << 16 | 'h' << 8 | 'a';
99 #endif
100 	} else {
101 		ha = (X68K_BOOT_SCSIIF_SPC << 4) | 1;
102 #ifdef XXBOOT_DEBUG
103 		devstr[5] = '1';
104 #endif
105 	}
106 
107 	return ha;
108 }
109 
110 extern const char bootprog_name[], bootprog_rev[];
111 
112 void
113 bootmain(void)
114 {
115 	int bootdev, ha, fd;
116 	char bootdevstr[16];
117 	u_long marks[MARK_MAX];
118 
119 	IOCS_B_PRINT(bootprog_name);
120 	IOCS_B_PRINT(" rev.");
121 	IOCS_B_PRINT(bootprog_rev);
122 	IOCS_B_PRINT("\r\n");
123 
124 	ha = get_scsi_host_adapter(bootdevstr);
125 #ifdef XXBOOT_DEBUG
126 	bootdevstr[10] = '0' + (ID & 7);
127 	bootdevstr[14] = 'a';
128 #endif
129 
130 #if defined(CDBOOT)
131 	bootdev = X68K_MAKESCSIBOOTDEV(X68K_MAJOR_CD, ha >> 4, ha & 15,
132 				       ID & 7, 0, 0);
133 #elif defined(FDBOOT) || defined(SDBOOT)
134 	if (BINF_ISFD(&BOOT_INFO)) {
135 		/* floppy */
136 #ifdef XXBOOT_DEBUG
137 		*(uint32_t *)bootdevstr =
138 		    ('f' << 24 | 'd' << 16 | '@' << 8 | '0' + (BOOT_INFO & 3));
139 		bootdevstr[4] = '\0';
140 #endif
141 		/* fdNa for 1024 bytes/sector, fdNc for 512 bytes/sector */
142 		bootdev = X68K_MAKEBOOTDEV(X68K_MAJOR_FD, BOOT_INFO & 3,
143 		    (FDSECMINMAX.minsec.N == 3) ? 0 : 2);
144 	} else {
145 		/* SCSI */
146 		bootdev = X68K_MAKESCSIBOOTDEV(X68K_MAJOR_SD, ha >> 4, ha & 15,
147 		    ID & 7, 0, 0 /* XXX: assume partition a */);
148 	}
149 #else
150 	bootdev = 0;
151 #endif
152 
153 #ifdef XXBOOT_DEBUG
154 	IOCS_B_PRINT("boot device: ");
155 	IOCS_B_PRINT(bootdevstr);
156 #endif
157 	IOCS_B_PRINT("\r\n");
158 
159 	marks[MARK_START] = BOOT_TEXTADDR;
160 	fd = loadfile("x68k/boot", marks, LOAD_TEXT|LOAD_DATA|LOAD_BSS);
161 	if (fd < 0)
162 		fd = loadfile("boot", marks, LOAD_TEXT|LOAD_DATA|LOAD_BSS);
163 	if (fd >= 0) {
164 		close(fd);
165 		exec_image(BOOT_TEXTADDR, /* image loaded at */
166 			   BOOT_TEXTADDR, /* image executed at */
167 			   BOOT_TEXTADDR, /* XXX: entry point */
168 			   0, 		  /* XXX: image size */
169 			   bootdev, 0);	  /* arguments */
170 	}
171 	IOCS_B_PRINT("can't load the secondary bootstrap.");
172 	exit(0);
173 }
174 
175 int
176 devopen(struct open_file *f, const char *fname, char **file)
177 {
178 
179 	*file = __UNCONST(fname);
180 	return xxopen(f);
181 }
182