i386.c revision 1.5 1 1.5 bjh21 /* $NetBSD: i386.c,v 1.5 2003/04/18 14:16:42 bjh21 Exp $ */
2 1.1 dsl
3 1.1 dsl /*-
4 1.1 dsl * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 1.1 dsl * All rights reserved.
6 1.1 dsl *
7 1.1 dsl * This code is derived from software contributed to The NetBSD Foundation
8 1.1 dsl * by David Laight.
9 1.1 dsl *
10 1.1 dsl * Redistribution and use in source and binary forms, with or without
11 1.1 dsl * modification, are permitted provided that the following conditions
12 1.1 dsl * are met:
13 1.1 dsl * 1. Redistributions of source code must retain the above copyright
14 1.1 dsl * notice, this list of conditions and the following disclaimer.
15 1.1 dsl * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 dsl * notice, this list of conditions and the following disclaimer in the
17 1.1 dsl * documentation and/or other materials provided with the distribution.
18 1.1 dsl * 3. All advertising materials mentioning features or use of this software
19 1.1 dsl * must display the following acknowledgement:
20 1.1 dsl * This product includes software developed by the NetBSD
21 1.1 dsl * Foundation, Inc. and its contributors.
22 1.1 dsl * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 dsl * contributors may be used to endorse or promote products derived
24 1.1 dsl * from this software without specific prior written permission.
25 1.1 dsl *
26 1.1 dsl * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 dsl * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 dsl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 dsl * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 dsl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 dsl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 dsl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 dsl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 dsl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 dsl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 dsl * POSSIBILITY OF SUCH DAMAGE.
37 1.1 dsl */
38 1.1 dsl
39 1.1 dsl #include <sys/cdefs.h>
40 1.4 briggs #if defined(__RCSID) && !defined(__lint)
41 1.5 bjh21 __RCSID("$NetBSD: i386.c,v 1.5 2003/04/18 14:16:42 bjh21 Exp $");
42 1.4 briggs #endif /* __RCSID && !__lint */
43 1.1 dsl
44 1.1 dsl #if HAVE_CONFIG_H
45 1.1 dsl #include "config.h"
46 1.1 dsl #endif
47 1.1 dsl
48 1.1 dsl #include <sys/param.h>
49 1.1 dsl
50 1.1 dsl #include <assert.h>
51 1.1 dsl #include <err.h>
52 1.5 bjh21 #include <md5.h>
53 1.1 dsl #include <stddef.h>
54 1.1 dsl #include <stdio.h>
55 1.1 dsl #include <stdlib.h>
56 1.1 dsl #include <string.h>
57 1.1 dsl #include <unistd.h>
58 1.1 dsl
59 1.1 dsl #include "installboot.h"
60 1.1 dsl
61 1.1 dsl int
62 1.1 dsl i386_setboot(ib_params *params)
63 1.1 dsl {
64 1.1 dsl int retval;
65 1.1 dsl char *bootstrapbuf;
66 1.1 dsl uint bootstrapsize;
67 1.1 dsl ssize_t rv;
68 1.1 dsl uint32_t magic;
69 1.2 dsl struct i386_boot_params *bp;
70 1.2 dsl int i;
71 1.1 dsl
72 1.1 dsl assert(params != NULL);
73 1.1 dsl assert(params->fsfd != -1);
74 1.1 dsl assert(params->filesystem != NULL);
75 1.1 dsl assert(params->s1fd != -1);
76 1.1 dsl assert(params->stage1 != NULL);
77 1.1 dsl
78 1.1 dsl retval = 0;
79 1.1 dsl bootstrapbuf = NULL;
80 1.1 dsl
81 1.2 dsl /*
82 1.2 dsl * There is only 8k of space in a UFSv1 partition (and ustarfs)
83 1.2 dsl * so ensure we don't splat over anything important.
84 1.2 dsl */
85 1.2 dsl if (params->s1stat.st_size > 8192) {
86 1.2 dsl warnx("stage1 bootstrap `%s' is larger than 8192 bytes",
87 1.2 dsl params->stage1);
88 1.2 dsl return 0;
89 1.1 dsl }
90 1.1 dsl /*
91 1.1 dsl * Allocate a buffer, with space to round up the input file
92 1.1 dsl * to the next block size boundary, and with space for the boot
93 1.1 dsl * block.
94 1.1 dsl */
95 1.1 dsl bootstrapsize = roundup(params->s1stat.st_size, 512);
96 1.1 dsl
97 1.1 dsl bootstrapbuf = malloc(bootstrapsize);
98 1.1 dsl if (bootstrapbuf == NULL) {
99 1.1 dsl warn("Allocating %u bytes", bootstrapsize);
100 1.1 dsl goto done;
101 1.1 dsl }
102 1.1 dsl memset(bootstrapbuf, 0, bootstrapsize);
103 1.1 dsl
104 1.1 dsl /* read the file into the buffer */
105 1.1 dsl rv = pread(params->s1fd, bootstrapbuf, params->s1stat.st_size, 0);
106 1.1 dsl if (rv == -1) {
107 1.1 dsl warn("Reading `%s'", params->stage1);
108 1.1 dsl return 0;
109 1.1 dsl } else if (rv != params->s1stat.st_size) {
110 1.1 dsl warnx("Reading `%s': short read", params->stage1);
111 1.1 dsl return 0;
112 1.1 dsl }
113 1.1 dsl
114 1.1 dsl magic = *(uint32_t *)(bootstrapbuf + 512 * 2 + 4);
115 1.2 dsl if (magic != X86_BOOT_MAGIC_1) {
116 1.1 dsl warnx("Invalid magic in stage1 boostrap %x != %x",
117 1.2 dsl magic, X86_BOOT_MAGIC_1);
118 1.2 dsl goto done;
119 1.2 dsl }
120 1.2 dsl
121 1.2 dsl /* Fill in any user-specified options */
122 1.2 dsl bp = (void *)(bootstrapbuf + 512 * 2 + 8);
123 1.3 dsl if (le32toh(bp->bp_length) < sizeof *bp) {
124 1.2 dsl warnx("Patch area in stage1 bootstrap is too small");
125 1.1 dsl goto done;
126 1.2 dsl }
127 1.2 dsl if (params->flags & IB_TIMEOUT)
128 1.3 dsl bp->bp_timeout = htole32(params->timeout);
129 1.2 dsl if (params->flags & IB_RESETVIDEO)
130 1.3 dsl bp->bp_flags |= htole32(BP_RESET_VIDEO);
131 1.2 dsl if (params->flags & IB_CONSPEED)
132 1.3 dsl bp->bp_conspeed = htole32(params->conspeed);
133 1.2 dsl if (params->flags & IB_CONSOLE) {
134 1.2 dsl static const char *names[] = {
135 1.2 dsl "pc", "com0", "com1", "com2", "com3",
136 1.2 dsl "com0kbd", "com1kbd", "com2kbd", "com3kbd",
137 1.2 dsl NULL };
138 1.2 dsl for (i = 0; ; i++) {
139 1.2 dsl if (names[i] == NULL) {
140 1.2 dsl warnx("invalid console name, valid names are:");
141 1.2 dsl fprintf(stderr, "\t%s", names[0]);
142 1.2 dsl for (i = 1; names[i] != NULL; i++)
143 1.2 dsl fprintf(stderr, ", %s", names[i]);
144 1.2 dsl fprintf(stderr, "\n", names[0]);
145 1.2 dsl goto done;
146 1.2 dsl }
147 1.2 dsl if (strcmp(names[i], params->console) == 0)
148 1.2 dsl break;
149 1.2 dsl }
150 1.3 dsl bp->bp_consdev = htole32(i);
151 1.2 dsl }
152 1.2 dsl if (params->flags & IB_PASSWORD) {
153 1.2 dsl MD5_CTX md5ctx;
154 1.2 dsl MD5Init(&md5ctx);
155 1.2 dsl MD5Update(&md5ctx, params->password, strlen(params->password));
156 1.2 dsl MD5Final(bp->bp_password, &md5ctx);
157 1.3 dsl bp->bp_flags |= htole32(BP_PASSWORD);
158 1.1 dsl }
159 1.1 dsl
160 1.1 dsl if (params->flags & IB_NOWRITE) {
161 1.1 dsl retval = 1;
162 1.1 dsl goto done;
163 1.1 dsl }
164 1.1 dsl
165 1.1 dsl /* Write pbr code to sector zero */
166 1.1 dsl rv = pwrite(params->fsfd, bootstrapbuf, 512, 0);
167 1.1 dsl if (rv == -1) {
168 1.1 dsl warn("Writing `%s'", params->filesystem);
169 1.1 dsl goto done;
170 1.1 dsl } else if (rv != 512) {
171 1.1 dsl warnx("Writing `%s': short write", params->filesystem);
172 1.1 dsl goto done;
173 1.1 dsl }
174 1.1 dsl
175 1.1 dsl /* Skip disklabel and write bootxx to sectors 2 + */
176 1.1 dsl rv = pwrite(params->fsfd, bootstrapbuf + 512 * 2,
177 1.1 dsl bootstrapsize - 512 * 2, 512 * 2);
178 1.1 dsl if (rv == -1) {
179 1.1 dsl warn("Writing `%s'", params->filesystem);
180 1.1 dsl goto done;
181 1.1 dsl } else if (rv != bootstrapsize - 512 * 2) {
182 1.1 dsl warnx("Writing `%s': short write", params->filesystem);
183 1.1 dsl goto done;
184 1.1 dsl }
185 1.1 dsl
186 1.1 dsl retval = 1;
187 1.1 dsl
188 1.1 dsl done:
189 1.1 dsl if (bootstrapbuf)
190 1.1 dsl free(bootstrapbuf);
191 1.1 dsl return retval;
192 1.1 dsl }
193