i386.c revision 1.1 1 1.1 dsl /* $NetBSD: i386.c,v 1.1 2003/04/09 22:14:27 dsl 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.1 dsl __RCSID("$NetBSD: i386.c,v 1.1 2003/04/09 22:14:27 dsl Exp $");
41 1.1 dsl
42 1.1 dsl #if HAVE_CONFIG_H
43 1.1 dsl #include "config.h"
44 1.1 dsl #endif
45 1.1 dsl
46 1.1 dsl #include <sys/param.h>
47 1.1 dsl
48 1.1 dsl #include <assert.h>
49 1.1 dsl #include <err.h>
50 1.1 dsl #include <stddef.h>
51 1.1 dsl #include <stdio.h>
52 1.1 dsl #include <stdlib.h>
53 1.1 dsl #include <string.h>
54 1.1 dsl #include <unistd.h>
55 1.1 dsl
56 1.1 dsl #include "installboot.h"
57 1.1 dsl
58 1.1 dsl /* Magic number also known by sys/arch/i386/stand/bootxx/Makefile.bootxx */
59 1.1 dsl #define BOOT_MAGIC_1 ('x' << 24 | 0x86b << 12 | 'm' << 4 | 1)
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.1 dsl
70 1.1 dsl assert(params != NULL);
71 1.1 dsl assert(params->fsfd != -1);
72 1.1 dsl assert(params->filesystem != NULL);
73 1.1 dsl assert(params->s1fd != -1);
74 1.1 dsl assert(params->stage1 != NULL);
75 1.1 dsl
76 1.1 dsl retval = 0;
77 1.1 dsl bootstrapbuf = NULL;
78 1.1 dsl
79 1.1 dsl if (params->flags & IB_STAGE1START) {
80 1.1 dsl warnx("`-b bno' is not supported for %s",
81 1.1 dsl params->machine->name);
82 1.1 dsl goto done;
83 1.1 dsl }
84 1.1 dsl if (params->flags & IB_STAGE2START) {
85 1.1 dsl warnx("`-B bno' is not supported for %s",
86 1.1 dsl params->machine->name);
87 1.1 dsl goto done;
88 1.1 dsl }
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.1 dsl if (magic != BOOT_MAGIC_1) {
116 1.1 dsl warnx("Invalid magic in stage1 boostrap %x != %x",
117 1.1 dsl magic, BOOT_MAGIC_1);
118 1.1 dsl goto done;
119 1.1 dsl }
120 1.1 dsl
121 1.1 dsl if (params->flags & IB_NOWRITE) {
122 1.1 dsl retval = 1;
123 1.1 dsl goto done;
124 1.1 dsl }
125 1.1 dsl
126 1.1 dsl /* Write pbr code to sector zero */
127 1.1 dsl rv = pwrite(params->fsfd, bootstrapbuf, 512, 0);
128 1.1 dsl if (rv == -1) {
129 1.1 dsl warn("Writing `%s'", params->filesystem);
130 1.1 dsl goto done;
131 1.1 dsl } else if (rv != 512) {
132 1.1 dsl warnx("Writing `%s': short write", params->filesystem);
133 1.1 dsl goto done;
134 1.1 dsl }
135 1.1 dsl
136 1.1 dsl /* Skip disklabel and write bootxx to sectors 2 + */
137 1.1 dsl rv = pwrite(params->fsfd, bootstrapbuf + 512 * 2,
138 1.1 dsl bootstrapsize - 512 * 2, 512 * 2);
139 1.1 dsl if (rv == -1) {
140 1.1 dsl warn("Writing `%s'", params->filesystem);
141 1.1 dsl goto done;
142 1.1 dsl } else if (rv != bootstrapsize - 512 * 2) {
143 1.1 dsl warnx("Writing `%s': short write", params->filesystem);
144 1.1 dsl goto done;
145 1.1 dsl }
146 1.1 dsl
147 1.1 dsl retval = 1;
148 1.1 dsl
149 1.1 dsl done:
150 1.1 dsl if (bootstrapbuf)
151 1.1 dsl free(bootstrapbuf);
152 1.1 dsl return retval;
153 1.1 dsl }
154