1 /*        $NetBSD: mboot.c,v 1.12 2012/04/06 09:44:44 isaki Exp $     */
2 
3 /*-
4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Minoura Makoto.
9  *
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.
18  *
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.
30  */
31 
32 #include <sys/types.h>
33 #include <machine/disklabel.h>
34 
35 #include "iocs.h"
36 
37 int bootmain(int);
38 
39 #define PART_BOOTABLE         0
40 #define PART_UNUSED 1
41 #define PART_INUSE  2
42 
43 int
bootmain(int scsiid)44 bootmain(int scsiid)
45 {
46           struct iocs_readcap cap;
47           int size;
48 
49           if (IOCS_BITSNS(0) & 1)       /* ESC key */
50                     return 0;
51 
52           if (IOCS_S_READCAP(scsiid, &cap) < 0) {
53                     IOCS_B_PRINT(BOOT ": Error in reading.\r\n");
54                     return 0;
55           }
56           size = cap.size >> 9;
57 
58           {
59                     long *label = (void*) 0x3000;
60                     if (IOCS_S_READ(0, 1, scsiid, size, label) < 0) {
61                               IOCS_B_PRINT(BOOT ": Error in reading.\r\n");
62                               return 0;
63                     }
64                     if (label[0] != 0x58363853 ||
65                         label[1] != 0x43534931) {
66                               IOCS_B_PRINT(BOOT ": Invalid disk.\r\n");
67                               return 0;
68                     }
69           }
70 
71           {
72                     struct cpu_disklabel *label = (void*) 0x3000;
73                     int i, firstinuse=-1;
74                     unsigned char *t;
75 
76                     if (IOCS_S_READ(2<<(2-size), size?2:1, scsiid, size, label) < 0) {
77                               IOCS_B_PRINT(BOOT ": Error in reading.\r\n");
78                               return 0;
79                     }
80                     t = label->dosparts[0].dp_typname;
81                     if (t[0] != 'X' || t[1] != '6' || t[2] != '8' || t[3] != 'K') {
82                               IOCS_B_PRINT(BOOT ": Invalid disk.\r\n");
83                               return 0;
84                     }
85 
86                     for (i = 1; i < NDOSPART; i++) {
87                               if (label->dosparts[i].dp_flag == PART_BOOTABLE)
88                                         break;
89                               else if (label->dosparts[i].dp_flag == PART_INUSE)
90                                         firstinuse = i;
91                     }
92                     if (i >= NDOSPART && firstinuse >= 0)
93                               i = firstinuse;
94                     if (i < NDOSPART) {
95                               unsigned int start = label->dosparts[i].dp_start;
96                               unsigned int start1 = start << (2-size);
97                               int r;
98                               if ((start1 & 0x1fffff) == 0x1fffff)
99                                         r = IOCS_S_READ(start1,
100                                                             8>>size,
101                                                             scsiid,
102                                                             size,
103                                                             (void*) 0x2400);
104                               else
105                                         r = IOCS_S_READEXT(start1,
106                                                                8>>size,
107                                                                scsiid,
108                                                                size,
109                                                                (void*) 0x2400);
110                               if (r < 0) {
111                                         IOCS_B_PRINT(BOOT ": Error in reading.\r\n");
112                                         return 0;
113                               }
114                               if (*((char*) 0x2400) != 0x60) {
115                                         IOCS_B_PRINT(BOOT ": Invalid disk.\r\n");
116                                         return 0;
117                               }
118                               __asm volatile ("movl %0,%%d4\n\t"
119                                               "movl %1,%%d2\n\t"
120                                               "jsr 0x2400"
121                                               :
122                                               : "g" (scsiid), "g"(start)
123                                               : "d4");
124                               return 0;
125                     }
126                     IOCS_B_PRINT(BOOT ": No bootable partition.\r\n");
127                     return 0;
128           }
129 
130           return 0;
131 }
132