Sync usage with man page.
[netbsd-mini2440.git] / sys / arch / x68k / stand / mboot / mboot.c
blob4c94fbbc7d8b8853518190cfb119132aee3ef6a9
1 /* $NetBSD: mboot.c,v 1.7 2009/01/12 07:56:31 tsutsui Exp $ */
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Minoura Makoto.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/types.h>
33 #include <machine/disklabel.h>
35 int bootmain(int);
37 struct iocs_readcap {
38 unsigned long block;
39 unsigned long size;
41 static inline int
42 IOCS_BITSNS (int row)
44 register unsigned int reg_d0 __asm ("%d0");
46 __asm volatile ("movel %1,%%d1\n\t"
47 "movel #0x04,%0\n\t"
48 "trap #15"
49 : "=d" (reg_d0)
50 : "ri" ((int) row)
51 : "%d1");
53 return reg_d0;
55 static inline void
56 IOCS_B_PRINT (const char *str)
58 __asm volatile ("moval %0,%%a1\n\t"
59 "movel #0x21,%%d0\n\t"
60 "trap #15\n\t"
62 : "a" ((int) str)
63 : "%a1", "%d0");
64 return;
66 static inline int
67 IOCS_S_READCAP (int id, struct iocs_readcap *cap)
69 register int reg_d0 __asm ("%d0");
71 __asm volatile ("moveml %%d4,%%sp@-\n\t"
72 "movel %2,%%d4\n\t"
73 "moval %3,%%a1\n\t"
74 "movel #0x25,%%d1\n\t"
75 "movel #0xf5,%%d0\n\t"
76 "trap #15\n\t"
77 "moveml %%sp@+,%%d4"
78 : "=d" (reg_d0), "=m" (*cap)
79 : "ri" (id), "g" ((int) cap)
80 : "%d1", "%a1");
82 return reg_d0;
84 static inline int
85 IOCS_S_READ (int pos, int blk, int id, int size, void *buf)
87 register int reg_d0 __asm ("%d0");
89 __asm volatile ("moveml %%d3-%%d5,%%sp@-\n\t"
90 "movel %2,%%d2\n\t"
91 "movel %3,%%d3\n\t"
92 "movel %4,%%d4\n\t"
93 "movel %5,%%d5\n\t"
94 "moval %6,%%a1\n\t"
95 "movel #0x26,%%d1\n\t"
96 "movel #0xf5,%%d0\n\t"
97 "trap #15\n\t"
98 "moveml %%sp@+,%%d3-%%d5"
99 : "=d" (reg_d0), "=m" (*(char*) buf)
100 : "ri" (pos), "ri" (blk), "ri" (id), "ri" (size), "g" ((int) buf)
101 : "%d1", "%d2", "%a1");
103 return reg_d0;
106 static inline int
107 IOCS_S_READEXT (int pos, int blk, int id, int size, void *buf)
109 register int reg_d0 __asm ("%d0");
111 __asm volatile ("moveml %%d3-%%d5,%%sp@-\n\t"
112 "movel %2,%%d2\n\t"
113 "movel %3,%%d3\n\t"
114 "movel %4,%%d4\n\t"
115 "movel %5,%%d5\n\t"
116 "moval %6,%%a1\n\t"
117 "movel #0x26,%%d1\n\t"
118 "movel #0xf5,%%d0\n\t"
119 "trap #15\n\t"
120 "moveml %%sp@+,%%d3-%%d5"
121 : "=d" (reg_d0), "=m" (*(char*) buf)
122 : "ri" (pos), "ri" (blk), "ri" (id), "ri" (size), "g" ((int) buf)
123 : "%d1", "%d2", "%a1");
125 return reg_d0;
128 #define PART_BOOTABLE 0
129 #define PART_UNUSED 1
130 #define PART_INUSE 2
135 bootmain(int scsiid)
137 struct iocs_readcap cap;
138 int size;
140 if (IOCS_BITSNS(0) & 1) /* ESC key */
141 return 0;
143 if (IOCS_S_READCAP(scsiid, &cap) < 0) {
144 IOCS_B_PRINT("Error in reading.\r\n");
145 return 0;
147 size = cap.size >> 9;
150 long *label = (void*) 0x3000;
151 if (IOCS_S_READ(0, 1, scsiid, size, label) < 0) {
152 IOCS_B_PRINT("Error in reading.\r\n");
153 return 0;
155 if (label[0] != 0x58363853 ||
156 label[1] != 0x43534931) {
157 IOCS_B_PRINT("Invalid disk.\r\n");
158 return 0;
163 struct cpu_disklabel *label = (void*) 0x3000;
164 int i, firstinuse=-1;
166 if (IOCS_S_READ(2<<(2-size), size?2:1, scsiid, size, label) < 0) {
167 IOCS_B_PRINT("Error in reading.\r\n");
168 return 0;
170 if (*((long*) &label->dosparts[0].dp_typname) != 0x5836384b) {
171 IOCS_B_PRINT("Invalid disk.\r\n");
172 return 0;
175 for (i = 1; i < NDOSPART; i++) {
176 if (label->dosparts[i].dp_flag == PART_BOOTABLE)
177 break;
178 else if (label->dosparts[i].dp_flag == PART_INUSE)
179 firstinuse = i;
181 if (i >= NDOSPART && firstinuse >= 0)
182 i = firstinuse;
183 if (i < NDOSPART) {
184 unsigned int start = label->dosparts[i].dp_start;
185 unsigned int start1 = start << (2-size);
186 int r;
187 if ((start1 & 0x1fffff) == 0x1fffff)
188 r = IOCS_S_READ(start1,
189 8>>size,
190 scsiid,
191 size,
192 (void*) 0x2400);
193 else
194 r = IOCS_S_READEXT(start1,
195 8>>size,
196 scsiid,
197 size,
198 (void*) 0x2400);
199 if (r < 0) {
200 IOCS_B_PRINT ("Error in reading.\r\n");
201 return 0;
203 if (*((char*) 0x2400) != 0x60) {
204 IOCS_B_PRINT("Invalid disk.\r\n");
205 return 0;
207 __asm volatile ("movl %0,%%d4\n\t"
208 "movl %1,%%d2\n\t"
209 "jsr 0x2400"
211 : "g" (scsiid), "g"(start)
212 : "d4");
213 return 0;
215 IOCS_B_PRINT ("No bootable partition.\r\n");
216 return 0;
219 return 0;