1 /* $OpenBSD: boot1.c,v 1.7 2011/04/10 12:21:07 miod Exp $ */ 2 /* $NetBSD: boot1.c,v 1.1 2006/09/01 21:26:19 uwe Exp $ */ 3 4 /*- 5 * Copyright (c) 2003 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by David Laight. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/param.h> 34 #include <lib/libsa/stand.h> 35 #include <lib/libsa/ufs.h> 36 37 #include <sys/disklabel.h> 38 39 #define XSTR(x) #x 40 #define STR(x) XSTR(x) 41 42 static uint32_t bios_sector; 43 44 const char *boot1(uint32_t *); 45 void putstr(const char *str); 46 int raise(int sig); 47 int blkdevstrategy(void *, int, daddr32_t, size_t, void *, size_t *); 48 int blkdevopen(struct open_file *, ...); 49 int blkdevclose(struct open_file *); 50 51 52 extern struct disklabel ptn_disklabel; 53 54 struct fs_ops file_system[] = { 55 { ufs_open, ufs_close, ufs_read, ufs_write, ufs_seek, 56 ufs_stat, ufs_readdir }, 57 }; 58 int nfsys = nitems(file_system); 59 60 struct devsw devsw[] = { 61 { "dk", blkdevstrategy, blkdevopen, blkdevclose, noioctl }, 62 }; 63 int ndevs = nitems(devsw); 64 65 const char * 66 boot1(uint32_t *sector) 67 { 68 struct stat sb; 69 int fd = -1; 70 71 bios_sector = *sector; 72 73 putstr("\r\nOpenBSD/" MACHINE " Primary Bootstrap\r\n"); 74 75 do { 76 /* 77 * Nothing at the start of the MBR partition, fallback on 78 * partition 'a' from the disklabel in this MBR partition. 79 */ 80 if (ptn_disklabel.d_magic != DISKMAGIC) 81 break; 82 if (ptn_disklabel.d_magic2 != DISKMAGIC) 83 break; 84 if (ptn_disklabel.d_partitions[0].p_fstype == FS_UNUSED) 85 break; 86 87 bios_sector = ptn_disklabel.d_partitions[0].p_offset; 88 *sector = bios_sector; 89 fd = open("boot", 0); 90 } while (0); 91 92 if (fd == -1 || fstat(fd, &sb) == -1) 93 return "Can't open /boot.\r\n"; 94 95 #if 0 96 if (sb.st_size > SECONDARY_MAX_LOAD) 97 return "/boot too large.\r\n"; 98 #endif 99 100 if (read(fd, (void *)LOADADDRESS, sb.st_size) != sb.st_size) 101 return "/boot load failed.\r\n"; 102 103 if (*(uint32_t *)(LOADADDRESS + 4) != 0x20041110) 104 return "Invalid /boot file format.\r\n"; 105 106 return 0; 107 } 108 109 int 110 blkdevopen(struct open_file *f, ...) 111 { 112 return 0; 113 } 114 int 115 blkdevclose(struct open_file *f) 116 { 117 return 0; 118 } 119 120 int 121 blkdevstrategy(void *devdata, int flag, daddr32_t dblk, size_t size, void *buf, size_t *rsize) 122 { 123 124 if (flag != F_READ) 125 return EROFS; 126 127 if (size & (DEV_BSIZE - 1)) 128 return EINVAL; 129 130 if (rsize) 131 *rsize = size; 132 133 if (size != 0 && readsects(0x40, bios_sector + dblk, buf, 134 size / DEV_BSIZE) != 0) 135 return EIO; 136 137 return 0; 138 } 139 140 /* ARGUSED */ 141 int 142 raise(int sig) 143 { 144 145 return 0; 146 } 147 148 void 149 twiddle(void) 150 { 151 static int pos; 152 153 putchar("|/-\\"[pos++ & 3]); 154 putchar('\b'); 155 } 156 157 int 158 devopen(struct open_file *f, const char *fname, char **file) 159 { 160 *file = (char *)fname; 161 f->f_flags |= F_NODEV; 162 f->f_dev = &devsw[0]; 163 return (0); 164 } 165