i386.c revision 1.1 1 /* $NetBSD: i386.c,v 1.1 2003/04/09 22:14:27 dsl Exp $ */
2
3 /*-
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by David Laight.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __RCSID("$NetBSD: i386.c,v 1.1 2003/04/09 22:14:27 dsl Exp $");
41
42 #if HAVE_CONFIG_H
43 #include "config.h"
44 #endif
45
46 #include <sys/param.h>
47
48 #include <assert.h>
49 #include <err.h>
50 #include <stddef.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55
56 #include "installboot.h"
57
58 /* Magic number also known by sys/arch/i386/stand/bootxx/Makefile.bootxx */
59 #define BOOT_MAGIC_1 ('x' << 24 | 0x86b << 12 | 'm' << 4 | 1)
60
61 int
62 i386_setboot(ib_params *params)
63 {
64 int retval;
65 char *bootstrapbuf;
66 uint bootstrapsize;
67 ssize_t rv;
68 uint32_t magic;
69
70 assert(params != NULL);
71 assert(params->fsfd != -1);
72 assert(params->filesystem != NULL);
73 assert(params->s1fd != -1);
74 assert(params->stage1 != NULL);
75
76 retval = 0;
77 bootstrapbuf = NULL;
78
79 if (params->flags & IB_STAGE1START) {
80 warnx("`-b bno' is not supported for %s",
81 params->machine->name);
82 goto done;
83 }
84 if (params->flags & IB_STAGE2START) {
85 warnx("`-B bno' is not supported for %s",
86 params->machine->name);
87 goto done;
88 }
89
90 /*
91 * Allocate a buffer, with space to round up the input file
92 * to the next block size boundary, and with space for the boot
93 * block.
94 */
95 bootstrapsize = roundup(params->s1stat.st_size, 512);
96
97 bootstrapbuf = malloc(bootstrapsize);
98 if (bootstrapbuf == NULL) {
99 warn("Allocating %u bytes", bootstrapsize);
100 goto done;
101 }
102 memset(bootstrapbuf, 0, bootstrapsize);
103
104 /* read the file into the buffer */
105 rv = pread(params->s1fd, bootstrapbuf, params->s1stat.st_size, 0);
106 if (rv == -1) {
107 warn("Reading `%s'", params->stage1);
108 return 0;
109 } else if (rv != params->s1stat.st_size) {
110 warnx("Reading `%s': short read", params->stage1);
111 return 0;
112 }
113
114 magic = *(uint32_t *)(bootstrapbuf + 512 * 2 + 4);
115 if (magic != BOOT_MAGIC_1) {
116 warnx("Invalid magic in stage1 boostrap %x != %x",
117 magic, BOOT_MAGIC_1);
118 goto done;
119 }
120
121 if (params->flags & IB_NOWRITE) {
122 retval = 1;
123 goto done;
124 }
125
126 /* Write pbr code to sector zero */
127 rv = pwrite(params->fsfd, bootstrapbuf, 512, 0);
128 if (rv == -1) {
129 warn("Writing `%s'", params->filesystem);
130 goto done;
131 } else if (rv != 512) {
132 warnx("Writing `%s': short write", params->filesystem);
133 goto done;
134 }
135
136 /* Skip disklabel and write bootxx to sectors 2 + */
137 rv = pwrite(params->fsfd, bootstrapbuf + 512 * 2,
138 bootstrapsize - 512 * 2, 512 * 2);
139 if (rv == -1) {
140 warn("Writing `%s'", params->filesystem);
141 goto done;
142 } else if (rv != bootstrapsize - 512 * 2) {
143 warnx("Writing `%s': short write", params->filesystem);
144 goto done;
145 }
146
147 retval = 1;
148
149 done:
150 if (bootstrapbuf)
151 free(bootstrapbuf);
152 return retval;
153 }
154