x68k.c revision 1.4 1 1.4 martin /* $NetBSD: x68k.c,v 1.4 2008/04/28 20:24:16 martin 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 *
19 1.1 isaki * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 isaki * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 isaki * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 isaki * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 isaki * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 isaki * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 isaki * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 isaki * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 isaki * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 isaki * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 isaki * POSSIBILITY OF SUCH DAMAGE.
30 1.1 isaki */
31 1.1 isaki
32 1.2 lukem #if HAVE_NBTOOL_CONFIG_H
33 1.2 lukem #include "nbtool_config.h"
34 1.2 lukem #endif
35 1.2 lukem
36 1.1 isaki #include <sys/cdefs.h>
37 1.2 lukem #if !defined(__lint)
38 1.4 martin __RCSID("$NetBSD: x68k.c,v 1.4 2008/04/28 20:24:16 martin Exp $");
39 1.1 isaki #endif /* !__lint */
40 1.1 isaki
41 1.1 isaki #include <sys/param.h>
42 1.1 isaki #include <sys/stat.h>
43 1.1 isaki
44 1.1 isaki #include <assert.h>
45 1.1 isaki #include <err.h>
46 1.1 isaki #include <stddef.h>
47 1.1 isaki #include <stdio.h>
48 1.1 isaki #include <stdlib.h>
49 1.1 isaki #include <string.h>
50 1.1 isaki #include <unistd.h>
51 1.1 isaki
52 1.1 isaki #include "installboot.h"
53 1.1 isaki
54 1.1 isaki #define X68K_LABELOFFSET 64
55 1.1 isaki #define X68K_LABELSIZE 404 /* reserve 16 partitions */
56 1.1 isaki
57 1.1 isaki static int x68k_clearheader(ib_params *, struct bbinfo_params *, uint8_t *);
58 1.1 isaki
59 1.3 dsl static int x68k_clearboot(ib_params *);
60 1.3 dsl static int x68k_setboot(ib_params *);
61 1.3 dsl
62 1.3 dsl struct ib_mach ib_mach_x68k =
63 1.3 dsl { "x68k", x68k_setboot, x68k_clearboot, no_editboot,
64 1.3 dsl IB_STAGE1START | IB_STAGE2START };
65 1.1 isaki
66 1.1 isaki static struct bbinfo_params bbparams = {
67 1.1 isaki X68K_BBINFO_MAGIC,
68 1.1 isaki X68K_BOOT_BLOCK_OFFSET,
69 1.1 isaki X68K_BOOT_BLOCK_BLOCKSIZE,
70 1.1 isaki X68K_BOOT_BLOCK_MAX_SIZE,
71 1.1 isaki X68K_LABELOFFSET + X68K_LABELSIZE, /* XXX */
72 1.1 isaki BBINFO_BIG_ENDIAN,
73 1.1 isaki };
74 1.1 isaki
75 1.3 dsl static int
76 1.1 isaki x68k_clearboot(ib_params *params)
77 1.1 isaki {
78 1.1 isaki
79 1.1 isaki assert(params != NULL);
80 1.1 isaki
81 1.1 isaki if (params->flags & IB_STAGE1START) {
82 1.1 isaki warnx("`-b bno' is not supported for %s",
83 1.1 isaki params->machine->name);
84 1.1 isaki return 0;
85 1.1 isaki }
86 1.1 isaki return shared_bbinfo_clearboot(params, &bbparams, x68k_clearheader);
87 1.1 isaki }
88 1.1 isaki
89 1.1 isaki static int
90 1.1 isaki x68k_clearheader(ib_params *params, struct bbinfo_params *bb_params,
91 1.1 isaki uint8_t *bb)
92 1.1 isaki {
93 1.1 isaki
94 1.1 isaki assert(params != NULL);
95 1.1 isaki assert(bb_params != NULL);
96 1.1 isaki assert(bb != NULL);
97 1.1 isaki
98 1.1 isaki memset(bb, 0, X68K_LABELOFFSET);
99 1.1 isaki return 1;
100 1.1 isaki }
101 1.1 isaki
102 1.3 dsl static int
103 1.1 isaki x68k_setboot(ib_params *params)
104 1.1 isaki {
105 1.1 isaki struct stat bootstrapsb;
106 1.1 isaki char bb[X68K_BOOT_BLOCK_MAX_SIZE];
107 1.1 isaki char label[X68K_LABELSIZE];
108 1.1 isaki uint32_t s1start;
109 1.1 isaki int retval;
110 1.1 isaki ssize_t rv;
111 1.1 isaki
112 1.1 isaki assert(params != NULL);
113 1.1 isaki assert(params->fsfd != -1);
114 1.1 isaki assert(params->filesystem != NULL);
115 1.1 isaki assert(params->s1fd != -1);
116 1.1 isaki assert(params->stage1 != NULL);
117 1.1 isaki
118 1.1 isaki retval = 0;
119 1.1 isaki
120 1.1 isaki if (params->flags & IB_STAGE1START)
121 1.1 isaki s1start = params->s1start;
122 1.1 isaki else
123 1.1 isaki s1start = X68K_BOOT_BLOCK_OFFSET /
124 1.1 isaki X68K_BOOT_BLOCK_BLOCKSIZE;
125 1.1 isaki
126 1.1 isaki /* read disklabel on the target disk */
127 1.1 isaki rv = pread(params->fsfd, label, sizeof label,
128 1.1 isaki s1start * X68K_BOOT_BLOCK_BLOCKSIZE + X68K_LABELOFFSET);
129 1.1 isaki if (rv == -1) {
130 1.1 isaki warn("Reading `%s'", params->filesystem);
131 1.1 isaki goto done;
132 1.1 isaki } else if (rv != sizeof label) {
133 1.1 isaki warnx("Reading `%s': short read", params->filesystem);
134 1.1 isaki goto done;
135 1.1 isaki }
136 1.1 isaki
137 1.1 isaki if (fstat(params->s1fd, &bootstrapsb) == -1) {
138 1.1 isaki warn("Examining `%s'", params->stage1);
139 1.1 isaki goto done;
140 1.1 isaki }
141 1.1 isaki if (!S_ISREG(bootstrapsb.st_mode)) {
142 1.1 isaki warnx("`%s' must be a regular file", params->stage1);
143 1.1 isaki goto done;
144 1.1 isaki }
145 1.1 isaki
146 1.1 isaki /* read boot loader */
147 1.1 isaki memset(&bb, 0, sizeof bb);
148 1.1 isaki rv = read(params->s1fd, &bb, sizeof bb);
149 1.1 isaki if (rv == -1) {
150 1.1 isaki warn("Reading `%s'", params->stage1);
151 1.1 isaki goto done;
152 1.1 isaki }
153 1.1 isaki /* then, overwrite disklabel */
154 1.1 isaki memcpy(&bb[X68K_LABELOFFSET], &label, sizeof label);
155 1.1 isaki
156 1.1 isaki if (params->flags & IB_VERBOSE) {
157 1.1 isaki printf("Bootstrap start sector: %#x\n", s1start);
158 1.1 isaki printf("Bootstrap byte count: %#x\n", (unsigned)rv);
159 1.1 isaki printf("%sriting bootstrap\n",
160 1.1 isaki (params->flags & IB_NOWRITE) ? "Not w" : "W");
161 1.1 isaki }
162 1.1 isaki if (params->flags & IB_NOWRITE) {
163 1.1 isaki retval = 1;
164 1.1 isaki goto done;
165 1.1 isaki }
166 1.1 isaki
167 1.1 isaki /* write boot loader and disklabel into the target disk */
168 1.1 isaki rv = pwrite(params->fsfd, &bb, X68K_BOOT_BLOCK_MAX_SIZE,
169 1.1 isaki s1start * X68K_BOOT_BLOCK_BLOCKSIZE);
170 1.1 isaki if (rv == -1) {
171 1.1 isaki warn("Writing `%s'", params->filesystem);
172 1.1 isaki goto done;
173 1.1 isaki } else if (rv != X68K_BOOT_BLOCK_MAX_SIZE) {
174 1.1 isaki warnx("Writing `%s': short write", params->filesystem);
175 1.1 isaki goto done;
176 1.1 isaki } else
177 1.1 isaki retval = 1;
178 1.1 isaki
179 1.1 isaki done:
180 1.1 isaki return (retval);
181 1.1 isaki }
182