Home | History | Annotate | Line # | Download | only in mboot
mboot.c revision 1.5.78.1
      1 /*	$NetBSD: mboot.c,v 1.5.78.1 2008/05/16 02:23:27 yamt 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 struct iocs_readcap {
     36 	unsigned long	block;
     37 	unsigned long	size;
     38 };
     39 static inline int
     40 IOCS_BITSNS (int row)
     41 {
     42 	register unsigned int reg_d0 __asm ("%d0");
     43 
     44 	__asm volatile ("movel %1,%%d1\n\t"
     45 			  "movel #0x04,%0\n\t"
     46 			  "trap #15"
     47 			  : "=d" (reg_d0)
     48 			  : "ri" ((int) row)
     49 			  : "%d1");
     50 
     51 	return reg_d0;
     52 }
     53 static inline void
     54 IOCS_B_PRINT (const char *str)
     55 {
     56 	__asm volatile ("moval %0,%%a1\n\t"
     57 			  "movel #0x21,%%d0\n\t"
     58 			  "trap #15\n\t"
     59 			  :
     60 			  : "a" ((int) str)
     61 			  : "%a1", "%d0");
     62 	return;
     63 }
     64 static inline int
     65 IOCS_S_READCAP (int id, struct iocs_readcap *cap)
     66 {
     67 	register int reg_d0 __asm ("%d0");
     68 
     69 	__asm volatile ("moveml %%d4,%%sp@-\n\t"
     70 			  "movel %2,%%d4\n\t"
     71 			  "moval %3,%%a1\n\t"
     72 			  "movel #0x25,%%d1\n\t"
     73 			  "movel #0xf5,%%d0\n\t"
     74 			  "trap #15\n\t"
     75 			  "moveml %%sp@+,%%d4"
     76 			  : "=d" (reg_d0), "=m" (*cap)
     77 			  : "ri" (id), "g" ((int) cap)
     78 			  : "%d1", "%a1");
     79 
     80 	return reg_d0;
     81 }
     82 static inline int
     83 IOCS_S_READ (int pos, int blk, int id, int size, void *buf)
     84 {
     85 	register int reg_d0 __asm ("%d0");
     86 
     87 	__asm volatile ("moveml %%d3-%%d5,%%sp@-\n\t"
     88 			  "movel %2,%%d2\n\t"
     89 			  "movel %3,%%d3\n\t"
     90 			  "movel %4,%%d4\n\t"
     91 			  "movel %5,%%d5\n\t"
     92 			  "moval %6,%%a1\n\t"
     93 			  "movel #0x26,%%d1\n\t"
     94 			  "movel #0xf5,%%d0\n\t"
     95 			  "trap #15\n\t"
     96 			  "moveml %%sp@+,%%d3-%%d5"
     97 			  : "=d" (reg_d0), "=m" (*(char*) buf)
     98 			  : "ri" (pos), "ri" (blk), "ri" (id), "ri" (size), "g" ((int) buf)
     99 			  : "%d1", "%d2", "%a1");
    100 
    101 	return reg_d0;
    102 }
    103 
    104 static inline int
    105 IOCS_S_READEXT (int pos, int blk, int id, int size, void *buf)
    106 {
    107 	register int reg_d0 __asm ("%d0");
    108 
    109 	__asm volatile ("moveml %%d3-%%d5,%%sp@-\n\t"
    110 			  "movel %2,%%d2\n\t"
    111 			  "movel %3,%%d3\n\t"
    112 			  "movel %4,%%d4\n\t"
    113 			  "movel %5,%%d5\n\t"
    114 			  "moval %6,%%a1\n\t"
    115 			  "movel #0x26,%%d1\n\t"
    116 			  "movel #0xf5,%%d0\n\t"
    117 			  "trap #15\n\t"
    118 			  "moveml %%sp@+,%%d3-%%d5"
    119 			  : "=d" (reg_d0), "=m" (*(char*) buf)
    120 			  : "ri" (pos), "ri" (blk), "ri" (id), "ri" (size), "g" ((int) buf)
    121 			  : "%d1", "%d2", "%a1");
    122 
    123 	return reg_d0;
    124 }
    125 
    126 #define PART_BOOTABLE	0
    127 #define PART_UNUSED	1
    128 #define PART_INUSE	2
    129 
    130 
    131 
    132 int
    133 bootmain(scsiid)
    134 	int scsiid;
    135 {
    136 	struct iocs_readcap cap;
    137 	int size;
    138 
    139 	if (IOCS_BITSNS(0) & 1)	/* ESC key */
    140 		return 0;
    141 
    142 	if (IOCS_S_READCAP(scsiid, &cap) < 0) {
    143 		IOCS_B_PRINT("Error in reading.\r\n");
    144 		return 0;
    145 	}
    146 	size = cap.size >> 9;
    147 
    148 	{
    149 		long *label = (void*) 0x3000;
    150 		if (IOCS_S_READ(0, 1, scsiid, size, label) < 0) {
    151 			IOCS_B_PRINT("Error in reading.\r\n");
    152 			return 0;
    153 		}
    154 		if (label[0] != 0x58363853 ||
    155 		    label[1] != 0x43534931) {
    156 			IOCS_B_PRINT("Invalid disk.\r\n");
    157 			return 0;
    158 		}
    159 	}
    160 
    161 	{
    162 		struct cpu_disklabel *label = (void*) 0x3000;
    163 		int i, firstinuse=-1;
    164 
    165 		if (IOCS_S_READ(2<<(2-size), size?2:1, scsiid, size, label) < 0) {
    166 			IOCS_B_PRINT("Error in reading.\r\n");
    167 			return 0;
    168 		}
    169 		if (*((long*) &label->dosparts[0].dp_typname) != 0x5836384b) {
    170 			IOCS_B_PRINT("Invalid disk.\r\n");
    171 			return 0;
    172 		}
    173 
    174 		for (i = 1; i < NDOSPART; i++) {
    175 			if (label->dosparts[i].dp_flag == PART_BOOTABLE)
    176 				break;
    177 			else if (label->dosparts[i].dp_flag == PART_INUSE)
    178 				firstinuse = i;
    179 		}
    180 		if (i >= NDOSPART && firstinuse >= 0)
    181 			i = firstinuse;
    182 		if (i < NDOSPART) {
    183 			unsigned int start = label->dosparts[i].dp_start;
    184 			unsigned int start1 = start << (2-size);
    185 			int r;
    186 			if ((start1 & 0x1fffff) == 0x1fffff)
    187 				r = IOCS_S_READ(start1,
    188 						8>>size,
    189 						scsiid,
    190 						size,
    191 						(void*) 0x2400);
    192 			else
    193 				r = IOCS_S_READEXT(start1,
    194 						   8>>size,
    195 						   scsiid,
    196 						   size,
    197 						   (void*) 0x2400);
    198 			if (r < 0) {
    199 				IOCS_B_PRINT ("Error in reading.\r\n");
    200 				return 0;
    201 			}
    202 			if (*((char*) 0x2400) != 0x60) {
    203 				IOCS_B_PRINT("Invalid disk.\r\n");
    204 				return 0;
    205 			}
    206 			__asm volatile ("movl %0,%%d4\n\t"
    207 				      "movl %1,%%d2\n\t"
    208 				      "jsr 0x2400"
    209 				      :
    210 				      : "g" (scsiid), "g"(start)
    211 				      : "d4");
    212 			return 0;
    213 		}
    214 		IOCS_B_PRINT ("No bootable partition.\r\n");
    215 		return 0;
    216 	}
    217 
    218 	return 0;
    219 }
    220