1 /* $NetBSD: macppc.c,v 1.11 2008/05/24 19:15:21 tsutsui Exp $ */
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
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: macppc.c,v 1.11 2008/05/24 19:15:21 tsutsui Exp $");
41 #include <sys/param.h>
42 #ifndef HAVE_NBTOOL_CONFIG_H
43 #include <sys/ioctl.h>
54 #include "installboot.h"
56 static struct bbinfo_params bbparams
= {
58 MACPPC_BOOT_BLOCK_OFFSET
,
59 MACPPC_BOOT_BLOCK_BLOCKSIZE
,
60 MACPPC_BOOT_BLOCK_MAX_SIZE
,
65 static int writeapplepartmap(ib_params
*, struct bbinfo_params
*, uint8_t *);
67 static int macppc_clearboot(ib_params
*);
68 static int macppc_setboot(ib_params
*);
70 struct ib_mach ib_mach_macppc
=
71 { "macppc", macppc_setboot
, macppc_clearboot
, no_editboot
,
75 macppc_clearboot(ib_params
*params
)
78 assert(params
!= NULL
);
80 /* XXX: maybe clear the apple partition map too? */
81 return (shared_bbinfo_clearboot(params
, &bbparams
, NULL
));
85 macppc_setboot(ib_params
*params
)
88 assert(params
!= NULL
);
90 return (shared_bbinfo_setboot(params
, &bbparams
, writeapplepartmap
));
95 writeapplepartmap(ib_params
*params
, struct bbinfo_params
*bb_params
,
98 struct apple_drvr_map dm
;
99 struct apple_part_map_entry pme
;
102 assert (params
!= NULL
);
103 assert (bb_params
!= NULL
);
106 if (params
->flags
& IB_NOWRITE
)
109 /* block 0: driver map */
110 if (pread(params
->fsfd
, &dm
, MACPPC_BOOT_BLOCK_BLOCKSIZE
, 0) !=
111 MACPPC_BOOT_BLOCK_BLOCKSIZE
) {
112 warn("Can't read sector 0 of `%s'", params
->filesystem
);
115 dm
.sbSig
= htobe16(APPLE_DRVR_MAP_MAGIC
);
116 dm
.sbBlockSize
= htobe16(512);
117 dm
.sbBlkCount
= htobe32(0);
119 rv
= pwrite(params
->fsfd
, &dm
, MACPPC_BOOT_BLOCK_BLOCKSIZE
, 0);
121 if (rv
== -1 && errno
== EROFS
) {
123 * block 0 is LABELSECTOR which might be protected by
124 * bounds_check_with_label(9).
129 rv
= ioctl(params
->fsfd
, DIOCWLABEL
, &enable
);
131 warn("Cannot enable writes to the label sector");
135 rv
= pwrite(params
->fsfd
, &dm
, MACPPC_BOOT_BLOCK_BLOCKSIZE
, 0);
137 /* Reset write-protect. */
139 (void)ioctl(params
->fsfd
, DIOCWLABEL
, &enable
);
142 if (rv
!= MACPPC_BOOT_BLOCK_BLOCKSIZE
) {
143 warn("Can't write sector 0 of `%s'", params
->filesystem
);
147 /* block 1: Apple Partition Map */
148 memset(&pme
, 0, sizeof(pme
));
149 pme
.pmSig
= htobe16(APPLE_PART_MAP_ENTRY_MAGIC
);
150 pme
.pmMapBlkCnt
= htobe32(2);
151 pme
.pmPyPartStart
= htobe32(1);
152 pme
.pmPartBlkCnt
= htobe32(2);
153 pme
.pmDataCnt
= htobe32(2);
154 strlcpy(pme
.pmPartName
, "Apple", sizeof(pme
.pmPartName
));
155 strlcpy(pme
.pmPartType
, "Apple_partition_map", sizeof(pme
.pmPartType
));
156 pme
.pmPartStatus
= htobe32(0x37);
157 if (pwrite(params
->fsfd
, &pme
, MACPPC_BOOT_BLOCK_BLOCKSIZE
,
158 1 * MACPPC_BOOT_BLOCK_BLOCKSIZE
) != MACPPC_BOOT_BLOCK_BLOCKSIZE
) {
159 warn("Can't write Apple Partition Map into sector 1 of `%s'",
164 /* block 2: NetBSD partition */
165 memset(&pme
, 0, sizeof(pme
));
166 pme
.pmSig
= htobe16(APPLE_PART_MAP_ENTRY_MAGIC
);
167 pme
.pmMapBlkCnt
= htobe32(2);
168 pme
.pmPyPartStart
= htobe32(4);
169 pme
.pmPartBlkCnt
= htobe32(0x7fffffff);
170 pme
.pmDataCnt
= htobe32(0x7fffffff);
171 strlcpy(pme
.pmPartName
, "NetBSD", sizeof(pme
.pmPartName
));
172 strlcpy(pme
.pmPartType
, "NetBSD/macppc", sizeof(pme
.pmPartType
));
173 pme
.pmPartStatus
= htobe32(0x3b);
174 pme
.pmBootSize
= htobe32(roundup(params
->s1stat
.st_size
, 512));
175 pme
.pmBootLoad
= htobe32(0x4000);
176 pme
.pmBootEntry
= htobe32(0x4000);
177 strlcpy(pme
.pmProcessor
, "PowerPC", sizeof(pme
.pmProcessor
));
178 if (pwrite(params
->fsfd
, &pme
, MACPPC_BOOT_BLOCK_BLOCKSIZE
,
179 2 * MACPPC_BOOT_BLOCK_BLOCKSIZE
) != MACPPC_BOOT_BLOCK_BLOCKSIZE
) {
180 warn("Can't write Apple Partition Map into sector 2 of `%s'",