1 /* $NetBSD: x68k.c,v 1.4 2008/04/28 20:24:16 martin Exp $ */
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Luke Mewburn of Wasabi Systems.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
36 #include <sys/cdefs.h>
38 __RCSID("$NetBSD: x68k.c,v 1.4 2008/04/28 20:24:16 martin Exp $");
41 #include <sys/param.h>
52 #include "installboot.h"
54 #define X68K_LABELOFFSET 64
55 #define X68K_LABELSIZE 404 /* reserve 16 partitions */
57 static int x68k_clearheader(ib_params
*, struct bbinfo_params
*, uint8_t *);
59 static int x68k_clearboot(ib_params
*);
60 static int x68k_setboot(ib_params
*);
62 struct ib_mach ib_mach_x68k
=
63 { "x68k", x68k_setboot
, x68k_clearboot
, no_editboot
,
64 IB_STAGE1START
| IB_STAGE2START
};
66 static struct bbinfo_params bbparams
= {
68 X68K_BOOT_BLOCK_OFFSET
,
69 X68K_BOOT_BLOCK_BLOCKSIZE
,
70 X68K_BOOT_BLOCK_MAX_SIZE
,
71 X68K_LABELOFFSET
+ X68K_LABELSIZE
, /* XXX */
76 x68k_clearboot(ib_params
*params
)
79 assert(params
!= NULL
);
81 if (params
->flags
& IB_STAGE1START
) {
82 warnx("`-b bno' is not supported for %s",
83 params
->machine
->name
);
86 return shared_bbinfo_clearboot(params
, &bbparams
, x68k_clearheader
);
90 x68k_clearheader(ib_params
*params
, struct bbinfo_params
*bb_params
,
94 assert(params
!= NULL
);
95 assert(bb_params
!= NULL
);
98 memset(bb
, 0, X68K_LABELOFFSET
);
103 x68k_setboot(ib_params
*params
)
105 struct stat bootstrapsb
;
106 char bb
[X68K_BOOT_BLOCK_MAX_SIZE
];
107 char label
[X68K_LABELSIZE
];
112 assert(params
!= NULL
);
113 assert(params
->fsfd
!= -1);
114 assert(params
->filesystem
!= NULL
);
115 assert(params
->s1fd
!= -1);
116 assert(params
->stage1
!= NULL
);
120 if (params
->flags
& IB_STAGE1START
)
121 s1start
= params
->s1start
;
123 s1start
= X68K_BOOT_BLOCK_OFFSET
/
124 X68K_BOOT_BLOCK_BLOCKSIZE
;
126 /* read disklabel on the target disk */
127 rv
= pread(params
->fsfd
, label
, sizeof label
,
128 s1start
* X68K_BOOT_BLOCK_BLOCKSIZE
+ X68K_LABELOFFSET
);
130 warn("Reading `%s'", params
->filesystem
);
132 } else if (rv
!= sizeof label
) {
133 warnx("Reading `%s': short read", params
->filesystem
);
137 if (fstat(params
->s1fd
, &bootstrapsb
) == -1) {
138 warn("Examining `%s'", params
->stage1
);
141 if (!S_ISREG(bootstrapsb
.st_mode
)) {
142 warnx("`%s' must be a regular file", params
->stage1
);
146 /* read boot loader */
147 memset(&bb
, 0, sizeof bb
);
148 rv
= read(params
->s1fd
, &bb
, sizeof bb
);
150 warn("Reading `%s'", params
->stage1
);
153 /* then, overwrite disklabel */
154 memcpy(&bb
[X68K_LABELOFFSET
], &label
, sizeof label
);
156 if (params
->flags
& IB_VERBOSE
) {
157 printf("Bootstrap start sector: %#x\n", s1start
);
158 printf("Bootstrap byte count: %#x\n", (unsigned)rv
);
159 printf("%sriting bootstrap\n",
160 (params
->flags
& IB_NOWRITE
) ? "Not w" : "W");
162 if (params
->flags
& IB_NOWRITE
) {
167 /* write boot loader and disklabel into the target disk */
168 rv
= pwrite(params
->fsfd
, &bb
, X68K_BOOT_BLOCK_MAX_SIZE
,
169 s1start
* X68K_BOOT_BLOCK_BLOCKSIZE
);
171 warn("Writing `%s'", params
->filesystem
);
173 } else if (rv
!= X68K_BOOT_BLOCK_MAX_SIZE
) {
174 warnx("Writing `%s': short write", params
->filesystem
);