x68k.c revision 1.2 1 1.2 lukem /* $NetBSD: x68k.c,v 1.2 2003/10/27 00:12:44 lukem Exp $ */
2 1.1 isaki
3 1.1 isaki /*-
4 1.1 isaki * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 1.1 isaki * All rights reserved.
6 1.1 isaki *
7 1.1 isaki * This code is derived from software contributed to The NetBSD Foundation
8 1.1 isaki * by Luke Mewburn of Wasabi Systems.
9 1.1 isaki *
10 1.1 isaki * Redistribution and use in source and binary forms, with or without
11 1.1 isaki * modification, are permitted provided that the following conditions
12 1.1 isaki * are met:
13 1.1 isaki * 1. Redistributions of source code must retain the above copyright
14 1.1 isaki * notice, this list of conditions and the following disclaimer.
15 1.1 isaki * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 isaki * notice, this list of conditions and the following disclaimer in the
17 1.1 isaki * documentation and/or other materials provided with the distribution.
18 1.1 isaki * 3. All advertising materials mentioning features or use of this software
19 1.1 isaki * must display the following acknowledgement:
20 1.1 isaki * This product includes software developed by the NetBSD
21 1.1 isaki * Foundation, Inc. and its contributors.
22 1.1 isaki * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 isaki * contributors may be used to endorse or promote products derived
24 1.1 isaki * from this software without specific prior written permission.
25 1.1 isaki *
26 1.1 isaki * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 isaki * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 isaki * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 isaki * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 isaki * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 isaki * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 isaki * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 isaki * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 isaki * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 isaki * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 isaki * POSSIBILITY OF SUCH DAMAGE.
37 1.1 isaki */
38 1.1 isaki
39 1.2 lukem #if HAVE_NBTOOL_CONFIG_H
40 1.2 lukem #include "nbtool_config.h"
41 1.2 lukem #endif
42 1.2 lukem
43 1.1 isaki #include <sys/cdefs.h>
44 1.2 lukem #if !defined(__lint)
45 1.2 lukem __RCSID("$NetBSD: x68k.c,v 1.2 2003/10/27 00:12:44 lukem Exp $");
46 1.1 isaki #endif /* !__lint */
47 1.1 isaki
48 1.1 isaki #include <sys/param.h>
49 1.1 isaki #include <sys/stat.h>
50 1.1 isaki
51 1.1 isaki #include <assert.h>
52 1.1 isaki #include <err.h>
53 1.1 isaki #include <stddef.h>
54 1.1 isaki #include <stdio.h>
55 1.1 isaki #include <stdlib.h>
56 1.1 isaki #include <string.h>
57 1.1 isaki #include <unistd.h>
58 1.1 isaki
59 1.1 isaki #include "installboot.h"
60 1.1 isaki
61 1.1 isaki #define X68K_LABELOFFSET 64
62 1.1 isaki #define X68K_LABELSIZE 404 /* reserve 16 partitions */
63 1.1 isaki
64 1.1 isaki static int x68k_clearheader(ib_params *, struct bbinfo_params *, uint8_t *);
65 1.1 isaki
66 1.1 isaki
67 1.1 isaki static struct bbinfo_params bbparams = {
68 1.1 isaki X68K_BBINFO_MAGIC,
69 1.1 isaki X68K_BOOT_BLOCK_OFFSET,
70 1.1 isaki X68K_BOOT_BLOCK_BLOCKSIZE,
71 1.1 isaki X68K_BOOT_BLOCK_MAX_SIZE,
72 1.1 isaki X68K_LABELOFFSET + X68K_LABELSIZE, /* XXX */
73 1.1 isaki BBINFO_BIG_ENDIAN,
74 1.1 isaki };
75 1.1 isaki
76 1.1 isaki int
77 1.1 isaki x68k_clearboot(ib_params *params)
78 1.1 isaki {
79 1.1 isaki
80 1.1 isaki assert(params != NULL);
81 1.1 isaki
82 1.1 isaki if (params->flags & IB_STAGE1START) {
83 1.1 isaki warnx("`-b bno' is not supported for %s",
84 1.1 isaki params->machine->name);
85 1.1 isaki return 0;
86 1.1 isaki }
87 1.1 isaki return shared_bbinfo_clearboot(params, &bbparams, x68k_clearheader);
88 1.1 isaki }
89 1.1 isaki
90 1.1 isaki static int
91 1.1 isaki x68k_clearheader(ib_params *params, struct bbinfo_params *bb_params,
92 1.1 isaki uint8_t *bb)
93 1.1 isaki {
94 1.1 isaki
95 1.1 isaki assert(params != NULL);
96 1.1 isaki assert(bb_params != NULL);
97 1.1 isaki assert(bb != NULL);
98 1.1 isaki
99 1.1 isaki memset(bb, 0, X68K_LABELOFFSET);
100 1.1 isaki return 1;
101 1.1 isaki }
102 1.1 isaki
103 1.1 isaki int
104 1.1 isaki x68k_setboot(ib_params *params)
105 1.1 isaki {
106 1.1 isaki struct stat bootstrapsb;
107 1.1 isaki char bb[X68K_BOOT_BLOCK_MAX_SIZE];
108 1.1 isaki char label[X68K_LABELSIZE];
109 1.1 isaki uint32_t s1start;
110 1.1 isaki int retval;
111 1.1 isaki ssize_t rv;
112 1.1 isaki
113 1.1 isaki assert(params != NULL);
114 1.1 isaki assert(params->fsfd != -1);
115 1.1 isaki assert(params->filesystem != NULL);
116 1.1 isaki assert(params->s1fd != -1);
117 1.1 isaki assert(params->stage1 != NULL);
118 1.1 isaki
119 1.1 isaki retval = 0;
120 1.1 isaki
121 1.1 isaki if (params->flags & IB_STAGE1START)
122 1.1 isaki s1start = params->s1start;
123 1.1 isaki else
124 1.1 isaki s1start = X68K_BOOT_BLOCK_OFFSET /
125 1.1 isaki X68K_BOOT_BLOCK_BLOCKSIZE;
126 1.1 isaki
127 1.1 isaki /* read disklabel on the target disk */
128 1.1 isaki rv = pread(params->fsfd, label, sizeof label,
129 1.1 isaki s1start * X68K_BOOT_BLOCK_BLOCKSIZE + X68K_LABELOFFSET);
130 1.1 isaki if (rv == -1) {
131 1.1 isaki warn("Reading `%s'", params->filesystem);
132 1.1 isaki goto done;
133 1.1 isaki } else if (rv != sizeof label) {
134 1.1 isaki warnx("Reading `%s': short read", params->filesystem);
135 1.1 isaki goto done;
136 1.1 isaki }
137 1.1 isaki
138 1.1 isaki if (fstat(params->s1fd, &bootstrapsb) == -1) {
139 1.1 isaki warn("Examining `%s'", params->stage1);
140 1.1 isaki goto done;
141 1.1 isaki }
142 1.1 isaki if (!S_ISREG(bootstrapsb.st_mode)) {
143 1.1 isaki warnx("`%s' must be a regular file", params->stage1);
144 1.1 isaki goto done;
145 1.1 isaki }
146 1.1 isaki
147 1.1 isaki /* read boot loader */
148 1.1 isaki memset(&bb, 0, sizeof bb);
149 1.1 isaki rv = read(params->s1fd, &bb, sizeof bb);
150 1.1 isaki if (rv == -1) {
151 1.1 isaki warn("Reading `%s'", params->stage1);
152 1.1 isaki goto done;
153 1.1 isaki }
154 1.1 isaki /* then, overwrite disklabel */
155 1.1 isaki memcpy(&bb[X68K_LABELOFFSET], &label, sizeof label);
156 1.1 isaki
157 1.1 isaki if (params->flags & IB_VERBOSE) {
158 1.1 isaki printf("Bootstrap start sector: %#x\n", s1start);
159 1.1 isaki printf("Bootstrap byte count: %#x\n", (unsigned)rv);
160 1.1 isaki printf("%sriting bootstrap\n",
161 1.1 isaki (params->flags & IB_NOWRITE) ? "Not w" : "W");
162 1.1 isaki }
163 1.1 isaki if (params->flags & IB_NOWRITE) {
164 1.1 isaki retval = 1;
165 1.1 isaki goto done;
166 1.1 isaki }
167 1.1 isaki
168 1.1 isaki /* write boot loader and disklabel into the target disk */
169 1.1 isaki rv = pwrite(params->fsfd, &bb, X68K_BOOT_BLOCK_MAX_SIZE,
170 1.1 isaki s1start * X68K_BOOT_BLOCK_BLOCKSIZE);
171 1.1 isaki if (rv == -1) {
172 1.1 isaki warn("Writing `%s'", params->filesystem);
173 1.1 isaki goto done;
174 1.1 isaki } else if (rv != X68K_BOOT_BLOCK_MAX_SIZE) {
175 1.1 isaki warnx("Writing `%s': short write", params->filesystem);
176 1.1 isaki goto done;
177 1.1 isaki } else
178 1.1 isaki retval = 1;
179 1.1 isaki
180 1.1 isaki done:
181 1.1 isaki return (retval);
182 1.1 isaki }
183