bootyy.c revision 1.3.78.1 1 1.3.78.1 yamt /* $NetBSD: bootyy.c,v 1.3.78.1 2008/05/16 02:23:23 yamt Exp $ */
2 1.1 fredette
3 1.1 fredette /*-
4 1.1 fredette * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.1 fredette * All rights reserved.
6 1.1 fredette *
7 1.1 fredette * This code is derived from software contributed to The NetBSD Foundation
8 1.1 fredette * by Paul Kranenburg.
9 1.1 fredette *
10 1.1 fredette * Redistribution and use in source and binary forms, with or without
11 1.1 fredette * modification, are permitted provided that the following conditions
12 1.1 fredette * are met:
13 1.1 fredette * 1. Redistributions of source code must retain the above copyright
14 1.1 fredette * notice, this list of conditions and the following disclaimer.
15 1.1 fredette * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 fredette * notice, this list of conditions and the following disclaimer in the
17 1.1 fredette * documentation and/or other materials provided with the distribution.
18 1.1 fredette *
19 1.1 fredette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 fredette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 fredette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 fredette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 fredette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 fredette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 fredette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 fredette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 fredette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 fredette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 fredette * POSSIBILITY OF SUCH DAMAGE.
30 1.1 fredette */
31 1.1 fredette
32 1.1 fredette /*
33 1.1 fredette * This is a generic "first-stage" boot program.
34 1.1 fredette *
35 1.1 fredette * Note that this program has absolutely no filesystem knowledge!
36 1.1 fredette *
37 1.1 fredette * Instead, this uses a table of disk block numbers that are
38 1.1 fredette * filled in by the installboot program such that this program
39 1.1 fredette * can load the "second-stage" boot program.
40 1.1 fredette */
41 1.1 fredette
42 1.1 fredette #include <sys/param.h>
43 1.1 fredette #include <sys/time.h>
44 1.1 fredette #include <sys/exec.h>
45 1.1 fredette #include <machine/mon.h>
46 1.1 fredette
47 1.1 fredette #include <stand.h>
48 1.1 fredette #include "libsa.h"
49 1.1 fredette
50 1.1 fredette /* This determines the largest boot program we can load. */
51 1.1 fredette #define MAXBLOCKNUM 64
52 1.1 fredette
53 1.1 fredette /*
54 1.1 fredette * The block size used in the Sun Network Disk protocol.
55 1.1 fredette */
56 1.1 fredette #define SUNND_BSIZE (512)
57 1.1 fredette
58 1.1 fredette /*
59 1.1 fredette * The first block number we request. This is related to historical
60 1.1 fredette * values; in 4.2BSD, the disklabel and the first stage boot program
61 1.1 fredette * are together called the bootblocks, and take up the first BBSIZE
62 1.1 fredette * bytes of the disk. In 4.2BSD, BBSIZE was 8192 and the disk device
63 1.1 fredette * block size was 512 bytes (see the definition of SUNND_BSIZE below),
64 1.1 fredette * making the number of blocks taken up by the bootblocks (BBSIZE /
65 1.1 fredette * SUNND_BSIZE). This value for FIRSTBLOCKNUM assumes that the
66 1.1 fredette * second-stage boot begins immediately after the bootblocks. The
67 1.1 fredette * second-stage boot always runs contiguously for MAXBLOCKNUM blocks.
68 1.1 fredette * FIRSTBLOCKNUM can be increased, and MAXBLOCKNUM can be adjusted, as
69 1.1 fredette * long as they match up with however you're putting together the disk
70 1.1 fredette * image that is being served to this client.
71 1.1 fredette */
72 1.1 fredette #define FIRSTBLOCKNUM (8192 / SUNND_BSIZE)
73 1.1 fredette
74 1.1 fredette int block_size = SUNND_BSIZE; /* default */
75 1.1 fredette
76 1.2 chs int
77 1.2 chs main(void)
78 1.1 fredette {
79 1.1 fredette struct open_file f;
80 1.1 fredette void *entry;
81 1.1 fredette char *addr;
82 1.1 fredette int n, error;
83 1.1 fredette
84 1.1 fredette #ifdef DEBUG
85 1.1 fredette printf("bootyy: open...\n");
86 1.1 fredette #endif
87 1.1 fredette f.f_flags = F_RAW;
88 1.1 fredette if (devopen(&f, 0, &addr)) {
89 1.1 fredette printf("bootyy: devopen failed\n");
90 1.1 fredette return;
91 1.1 fredette }
92 1.1 fredette
93 1.2 chs addr = (char *)KERN_LOADADDR;
94 1.1 fredette error = copyboot(&f, addr);
95 1.1 fredette f.f_dev->dv_close(&f);
96 1.1 fredette if (!error) {
97 1.1 fredette #ifdef DEBUG
98 1.1 fredette printf("bootyy: start 0x%x\n", (long)addr);
99 1.1 fredette #endif
100 1.1 fredette entry = addr;
101 1.1 fredette chain_to(entry);
102 1.1 fredette }
103 1.1 fredette /* copyboot had a problem... */
104 1.1 fredette return;
105 1.1 fredette }
106 1.1 fredette
107 1.2 chs int
108 1.2 chs copyboot(struct open_file *fp, char *addr)
109 1.1 fredette {
110 1.1 fredette int n, i, blknum;
111 1.1 fredette char *buf;
112 1.1 fredette
113 1.1 fredette /* Need to use a buffer that can be mapped into DVMA space. */
114 1.1 fredette buf = alloc(block_size);
115 1.1 fredette if (!buf)
116 1.1 fredette panic("bootyy: alloc failed");
117 1.1 fredette
118 1.1 fredette for (i = 0, blknum = FIRSTBLOCKNUM; i < MAXBLOCKNUM; i++, blknum++) {
119 1.1 fredette
120 1.1 fredette #ifdef DEBUG
121 1.1 fredette printf("bootyy: block # %d = %d\n", i, blknum);
122 1.1 fredette #endif
123 1.1 fredette if ((fp->f_dev->dv_strategy)(fp->f_devdata, F_READ,
124 1.1 fredette blknum, block_size, buf, &n))
125 1.1 fredette {
126 1.1 fredette printf("bootyy: read failed\n");
127 1.1 fredette return -1;
128 1.1 fredette }
129 1.1 fredette if (n != block_size) {
130 1.1 fredette printf("bootyy: short read\n");
131 1.1 fredette return -1;
132 1.1 fredette }
133 1.2 chs memcpy(addr, buf, block_size);
134 1.1 fredette addr += block_size;
135 1.1 fredette }
136 1.1 fredette
137 1.1 fredette return 0;
138 1.1 fredette }
139