Home | History | Annotate | Line # | Download | only in mboot
mboot.c revision 1.8
      1 /*	$NetBSD: mboot.c,v 1.8 2009/03/14 15:36:15 dsl 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 int bootmain(int);
     36 
     37 struct iocs_readcap {
     38 	unsigned long	block;
     39 	unsigned long	size;
     40 };
     41 static inline int
     42 IOCS_BITSNS (int row)
     43 {
     44 	register unsigned int reg_d0 __asm ("%d0");
     45 
     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");
     52 
     53 	return reg_d0;
     54 }
     55 static inline void
     56 IOCS_B_PRINT (const char *str)
     57 {
     58 	__asm volatile ("moval %0,%%a1\n\t"
     59 			  "movel #0x21,%%d0\n\t"
     60 			  "trap #15\n\t"
     61 			  :
     62 			  : "a" ((int) str)
     63 			  : "%a1", "%d0");
     64 	return;
     65 }
     66 static inline int
     67 IOCS_S_READCAP (int id, struct iocs_readcap *cap)
     68 {
     69 	register int reg_d0 __asm ("%d0");
     70 
     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");
     81 
     82 	return reg_d0;
     83 }
     84 static inline int
     85 IOCS_S_READ (int pos, int blk, int id, int size, void *buf)
     86 {
     87 	register int reg_d0 __asm ("%d0");
     88 
     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");
    102 
    103 	return reg_d0;
    104 }
    105 
    106 static inline int
    107 IOCS_S_READEXT (int pos, int blk, int id, int size, void *buf)
    108 {
    109 	register int reg_d0 __asm ("%d0");
    110 
    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");
    124 
    125 	return reg_d0;
    126 }
    127 
    128 #define PART_BOOTABLE	0
    129 #define PART_UNUSED	1
    130 #define PART_INUSE	2
    131 
    132 
    133 
    134 int
    135 bootmain(int scsiid)
    136 {
    137 	struct iocs_readcap cap;
    138 	int size;
    139 
    140 	if (IOCS_BITSNS(0) & 1)	/* ESC key */
    141 		return 0;
    142 
    143 	if (IOCS_S_READCAP(scsiid, &cap) < 0) {
    144 		IOCS_B_PRINT("Error in reading.\r\n");
    145 		return 0;
    146 	}
    147 	size = cap.size >> 9;
    148 
    149 	{
    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;
    154 		}
    155 		if (label[0] != 0x58363853 ||
    156 		    label[1] != 0x43534931) {
    157 			IOCS_B_PRINT("Invalid disk.\r\n");
    158 			return 0;
    159 		}
    160 	}
    161 
    162 	{
    163 		struct cpu_disklabel *label = (void*) 0x3000;
    164 		int i, firstinuse=-1;
    165 
    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;
    169 		}
    170 		if (*((long*) &label->dosparts[0].dp_typname) != 0x5836384b) {
    171 			IOCS_B_PRINT("Invalid disk.\r\n");
    172 			return 0;
    173 		}
    174 
    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;
    180 		}
    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;
    202 			}
    203 			if (*((char*) 0x2400) != 0x60) {
    204 				IOCS_B_PRINT("Invalid disk.\r\n");
    205 				return 0;
    206 			}
    207 			__asm volatile ("movl %0,%%d4\n\t"
    208 				      "movl %1,%%d2\n\t"
    209 				      "jsr 0x2400"
    210 				      :
    211 				      : "g" (scsiid), "g"(start)
    212 				      : "d4");
    213 			return 0;
    214 		}
    215 		IOCS_B_PRINT ("No bootable partition.\r\n");
    216 		return 0;
    217 	}
    218 
    219 	return 0;
    220 }
    221