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