bootxx.c revision 1.1
11.1Sfredette/* $NetBSD: bootxx.c,v 1.1 2001/06/14 12:57:12 fredette Exp $ */ 21.1Sfredette 31.1Sfredette/*- 41.1Sfredette * Copyright (c) 1998 The NetBSD Foundation, Inc. 51.1Sfredette * All rights reserved. 61.1Sfredette * 71.1Sfredette * This code is derived from software contributed to The NetBSD Foundation 81.1Sfredette * by Paul Kranenburg. 91.1Sfredette * 101.1Sfredette * Redistribution and use in source and binary forms, with or without 111.1Sfredette * modification, are permitted provided that the following conditions 121.1Sfredette * are met: 131.1Sfredette * 1. Redistributions of source code must retain the above copyright 141.1Sfredette * notice, this list of conditions and the following disclaimer. 151.1Sfredette * 2. Redistributions in binary form must reproduce the above copyright 161.1Sfredette * notice, this list of conditions and the following disclaimer in the 171.1Sfredette * documentation and/or other materials provided with the distribution. 181.1Sfredette * 3. All advertising materials mentioning features or use of this software 191.1Sfredette * must display the following acknowledgement: 201.1Sfredette * This product includes software developed by the NetBSD 211.1Sfredette * Foundation, Inc. and its contributors. 221.1Sfredette * 4. Neither the name of The NetBSD Foundation nor the names of its 231.1Sfredette * contributors may be used to endorse or promote products derived 241.1Sfredette * from this software without specific prior written permission. 251.1Sfredette * 261.1Sfredette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 271.1Sfredette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 281.1Sfredette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 291.1Sfredette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 301.1Sfredette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 311.1Sfredette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 321.1Sfredette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 331.1Sfredette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 341.1Sfredette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 351.1Sfredette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 361.1Sfredette * POSSIBILITY OF SUCH DAMAGE. 371.1Sfredette */ 381.1Sfredette 391.1Sfredette/* 401.1Sfredette * This is a generic "first-stage" boot program. 411.1Sfredette * 421.1Sfredette * Note that this program has absolutely no filesystem knowledge! 431.1Sfredette * 441.1Sfredette * Instead, this uses a table of disk block numbers that are 451.1Sfredette * filled in by the installboot program such that this program 461.1Sfredette * can load the "second-stage" boot program. 471.1Sfredette */ 481.1Sfredette 491.1Sfredette#include <sys/param.h> 501.1Sfredette#include <machine/mon.h> 511.1Sfredette 521.1Sfredette#include <stand.h> 531.1Sfredette#include "libsa.h" 541.1Sfredette 551.1Sfredette/* 561.1Sfredette * This is the address where we load the second-stage boot loader. 571.1Sfredette */ 581.1Sfredette#define LOADADDR 0x4000 591.1Sfredette 601.1Sfredette/* This determines the largest boot program we can load. */ 611.1Sfredette#define MAXBLOCKNUM 64 621.1Sfredette 631.1Sfredette/* 641.1Sfredette * These three names are known by installboot. 651.1Sfredette * The block_table contains starting block numbers, 661.1Sfredette * in terms of 512-byte blocks. Each non-zero value 671.1Sfredette * will result in a read of block_size bytes. 681.1Sfredette */ 691.1Sfredetteint block_size = 512; /* default */ 701.1Sfredetteint block_count = MAXBLOCKNUM; /* length of table */ 711.1Sfredettedaddr_t block_table[MAXBLOCKNUM] = { 0 }; 721.1Sfredette 731.1Sfredette 741.1Sfredetteint 751.1Sfredettemain() 761.1Sfredette{ 771.1Sfredette struct open_file f; 781.1Sfredette void *entry; 791.1Sfredette char *addr; 801.1Sfredette int n, error; 811.1Sfredette 821.1Sfredette#ifdef DEBUG 831.1Sfredette printf("bootxx: open...\n"); 841.1Sfredette#endif 851.1Sfredette f.f_flags = F_RAW; 861.1Sfredette if (devopen(&f, 0, &addr)) { 871.1Sfredette printf("bootxx: devopen failed\n"); 881.1Sfredette return; 891.1Sfredette } 901.1Sfredette 911.1Sfredette addr = (char*)LOADADDR; 921.1Sfredette error = copyboot(&f, addr); 931.1Sfredette f.f_dev->dv_close(&f); 941.1Sfredette if (!error) { 951.1Sfredette#ifdef DEBUG 961.1Sfredette printf("bootxx: start 0x%x\n", (long)addr); 971.1Sfredette#endif 981.1Sfredette entry = addr; 991.1Sfredette chain_to(entry); 1001.1Sfredette } 1011.1Sfredette /* copyboot had a problem... */ 1021.1Sfredette return; 1031.1Sfredette} 1041.1Sfredette 1051.1Sfredetteint 1061.1Sfredettecopyboot(fp, addr) 1071.1Sfredette struct open_file *fp; 1081.1Sfredette char *addr; 1091.1Sfredette{ 1101.1Sfredette int n, i, blknum; 1111.1Sfredette char *buf; 1121.1Sfredette 1131.1Sfredette /* Need to use a buffer that can be mapped into DVMA space. */ 1141.1Sfredette buf = alloc(block_size); 1151.1Sfredette if (!buf) 1161.1Sfredette panic("bootxx: alloc failed"); 1171.1Sfredette 1181.1Sfredette for (i = 0; i < block_count; i++) { 1191.1Sfredette 1201.1Sfredette if ((blknum = block_table[i]) == 0) 1211.1Sfredette break; 1221.1Sfredette 1231.1Sfredette#ifdef DEBUG 1241.1Sfredette printf("bootxx: block # %d = %d\n", i, blknum); 1251.1Sfredette#endif 1261.1Sfredette if ((fp->f_dev->dv_strategy)(fp->f_devdata, F_READ, 1271.1Sfredette blknum, block_size, buf, &n)) 1281.1Sfredette { 1291.1Sfredette printf("bootxx: read failed\n"); 1301.1Sfredette return -1; 1311.1Sfredette } 1321.1Sfredette if (n != block_size) { 1331.1Sfredette printf("bootxx: short read\n"); 1341.1Sfredette return -1; 1351.1Sfredette } 1361.1Sfredette bcopy(buf, addr, block_size); 1371.1Sfredette addr += block_size; 1381.1Sfredette } 1391.1Sfredette 1401.1Sfredette return 0; 1411.1Sfredette} 1421.1Sfredette 143