tools/llvm: Do not build with symbols
[minix3.git] / usr.sbin / installboot / arch / macppc.c
blob1f4e88beb93cfae6e1ed65784347215b115fd944
1 /* $NetBSD: macppc.c,v 1.11 2008/05/24 19:15:21 tsutsui Exp $ */
3 /*-
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Luke Mewburn.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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"
34 #endif
36 #include <sys/cdefs.h>
37 #if !defined(__lint)
38 __RCSID("$NetBSD: macppc.c,v 1.11 2008/05/24 19:15:21 tsutsui Exp $");
39 #endif /* !__lint */
41 #include <sys/param.h>
42 #ifndef HAVE_NBTOOL_CONFIG_H
43 #include <sys/ioctl.h>
44 #include <sys/dkio.h>
45 #include <errno.h>
46 #endif
48 #include <assert.h>
49 #include <err.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <unistd.h>
54 #include "installboot.h"
56 static struct bbinfo_params bbparams = {
57 MACPPC_BBINFO_MAGIC,
58 MACPPC_BOOT_BLOCK_OFFSET,
59 MACPPC_BOOT_BLOCK_BLOCKSIZE,
60 MACPPC_BOOT_BLOCK_MAX_SIZE,
62 BBINFO_BIG_ENDIAN,
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,
72 IB_STAGE2START };
74 static int
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));
84 static int
85 macppc_setboot(ib_params *params)
88 assert(params != NULL);
90 return (shared_bbinfo_setboot(params, &bbparams, writeapplepartmap));
94 static int
95 writeapplepartmap(ib_params *params, struct bbinfo_params *bb_params,
96 uint8_t *bb)
98 struct apple_drvr_map dm;
99 struct apple_part_map_entry pme;
100 int rv;
102 assert (params != NULL);
103 assert (bb_params != NULL);
104 assert (bb != NULL);
106 if (params->flags & IB_NOWRITE)
107 return (1);
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);
113 return (0);
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);
120 #ifdef DIOCWLABEL
121 if (rv == -1 && errno == EROFS) {
123 * block 0 is LABELSECTOR which might be protected by
124 * bounds_check_with_label(9).
126 int enable;
128 enable = 1;
129 rv = ioctl(params->fsfd, DIOCWLABEL, &enable);
130 if (rv != 0) {
131 warn("Cannot enable writes to the label sector");
132 return 0;
135 rv = pwrite(params->fsfd, &dm, MACPPC_BOOT_BLOCK_BLOCKSIZE, 0);
137 /* Reset write-protect. */
138 enable = 0;
139 (void)ioctl(params->fsfd, DIOCWLABEL, &enable);
141 #endif
142 if (rv != MACPPC_BOOT_BLOCK_BLOCKSIZE) {
143 warn("Can't write sector 0 of `%s'", params->filesystem);
144 return (0);
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'",
160 params->filesystem);
161 return (0);
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'",
181 params->filesystem);
182 return (0);
185 return (1);