bootyy.c revision 1.4.8.1 1 1.4.8.1 skrll /* $NetBSD: bootyy.c,v 1.4.8.1 2009/01/19 13:16:57 skrll 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.4.8.1 skrll int copyboot(struct open_file *, char *);
51 1.4.8.1 skrll
52 1.1 fredette /* This determines the largest boot program we can load. */
53 1.1 fredette #define MAXBLOCKNUM 64
54 1.1 fredette
55 1.1 fredette /*
56 1.1 fredette * The block size used in the Sun Network Disk protocol.
57 1.1 fredette */
58 1.1 fredette #define SUNND_BSIZE (512)
59 1.1 fredette
60 1.1 fredette /*
61 1.1 fredette * The first block number we request. This is related to historical
62 1.1 fredette * values; in 4.2BSD, the disklabel and the first stage boot program
63 1.1 fredette * are together called the bootblocks, and take up the first BBSIZE
64 1.1 fredette * bytes of the disk. In 4.2BSD, BBSIZE was 8192 and the disk device
65 1.1 fredette * block size was 512 bytes (see the definition of SUNND_BSIZE below),
66 1.1 fredette * making the number of blocks taken up by the bootblocks (BBSIZE /
67 1.1 fredette * SUNND_BSIZE). This value for FIRSTBLOCKNUM assumes that the
68 1.1 fredette * second-stage boot begins immediately after the bootblocks. The
69 1.1 fredette * second-stage boot always runs contiguously for MAXBLOCKNUM blocks.
70 1.1 fredette * FIRSTBLOCKNUM can be increased, and MAXBLOCKNUM can be adjusted, as
71 1.1 fredette * long as they match up with however you're putting together the disk
72 1.1 fredette * image that is being served to this client.
73 1.1 fredette */
74 1.1 fredette #define FIRSTBLOCKNUM (8192 / SUNND_BSIZE)
75 1.1 fredette
76 1.1 fredette int block_size = SUNND_BSIZE; /* default */
77 1.1 fredette
78 1.2 chs int
79 1.2 chs main(void)
80 1.1 fredette {
81 1.1 fredette struct open_file f;
82 1.1 fredette void *entry;
83 1.1 fredette char *addr;
84 1.4.8.1 skrll int error;
85 1.1 fredette
86 1.1 fredette #ifdef DEBUG
87 1.1 fredette printf("bootyy: open...\n");
88 1.1 fredette #endif
89 1.1 fredette f.f_flags = F_RAW;
90 1.1 fredette if (devopen(&f, 0, &addr)) {
91 1.1 fredette printf("bootyy: devopen failed\n");
92 1.4.8.1 skrll return 1;
93 1.1 fredette }
94 1.1 fredette
95 1.2 chs addr = (char *)KERN_LOADADDR;
96 1.1 fredette error = copyboot(&f, addr);
97 1.1 fredette f.f_dev->dv_close(&f);
98 1.1 fredette if (!error) {
99 1.1 fredette #ifdef DEBUG
100 1.1 fredette printf("bootyy: start 0x%x\n", (long)addr);
101 1.1 fredette #endif
102 1.1 fredette entry = addr;
103 1.1 fredette chain_to(entry);
104 1.1 fredette }
105 1.1 fredette /* copyboot had a problem... */
106 1.4.8.1 skrll return 0;
107 1.1 fredette }
108 1.1 fredette
109 1.2 chs int
110 1.2 chs copyboot(struct open_file *fp, char *addr)
111 1.1 fredette {
112 1.4.8.1 skrll size_t n;
113 1.4.8.1 skrll int i, blknum;
114 1.1 fredette char *buf;
115 1.1 fredette
116 1.1 fredette /* Need to use a buffer that can be mapped into DVMA space. */
117 1.1 fredette buf = alloc(block_size);
118 1.1 fredette if (!buf)
119 1.1 fredette panic("bootyy: alloc failed");
120 1.1 fredette
121 1.1 fredette for (i = 0, blknum = FIRSTBLOCKNUM; i < MAXBLOCKNUM; i++, blknum++) {
122 1.1 fredette
123 1.1 fredette #ifdef DEBUG
124 1.1 fredette printf("bootyy: block # %d = %d\n", i, blknum);
125 1.1 fredette #endif
126 1.1 fredette if ((fp->f_dev->dv_strategy)(fp->f_devdata, F_READ,
127 1.1 fredette blknum, block_size, buf, &n))
128 1.1 fredette {
129 1.1 fredette printf("bootyy: read failed\n");
130 1.1 fredette return -1;
131 1.1 fredette }
132 1.1 fredette if (n != block_size) {
133 1.1 fredette printf("bootyy: short read\n");
134 1.1 fredette return -1;
135 1.1 fredette }
136 1.2 chs memcpy(addr, buf, block_size);
137 1.1 fredette addr += block_size;
138 1.1 fredette }
139 1.1 fredette
140 1.1 fredette return 0;
141 1.1 fredette }
142