macppc.c revision 1.8.18.1 1 /* $NetBSD: macppc.c,v 1.8.18.1 2008/05/18 12:36:19 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Luke Mewburn.
9 *
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.
18 *
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.
30 */
31
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35
36 #include <sys/cdefs.h>
37 #if !defined(__lint)
38 __RCSID("$NetBSD: macppc.c,v 1.8.18.1 2008/05/18 12:36:19 yamt Exp $");
39 #endif /* !__lint */
40
41 #include <sys/param.h>
42 #ifndef HAVE_NBTOOL_CONFIG_H
43 #include <sys/ioctl.h>
44 #include <sys/dkio.h>
45 #endif
46
47 #include <assert.h>
48 #include <err.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <unistd.h>
52
53 #include "installboot.h"
54
55 static struct bbinfo_params bbparams = {
56 MACPPC_BBINFO_MAGIC,
57 MACPPC_BOOT_BLOCK_OFFSET,
58 MACPPC_BOOT_BLOCK_BLOCKSIZE,
59 MACPPC_BOOT_BLOCK_MAX_SIZE,
60 0,
61 BBINFO_BIG_ENDIAN,
62 };
63
64 static int writeapplepartmap(ib_params *, struct bbinfo_params *, uint8_t *);
65
66 static int macppc_clearboot(ib_params *);
67 static int macppc_setboot(ib_params *);
68
69 struct ib_mach ib_mach_macppc =
70 { "macppc", macppc_setboot, macppc_clearboot, no_editboot,
71 IB_STAGE2START };
72
73 static int
74 macppc_clearboot(ib_params *params)
75 {
76
77 assert(params != NULL);
78
79 /* XXX: maybe clear the apple partition map too? */
80 return (shared_bbinfo_clearboot(params, &bbparams, NULL));
81 }
82
83 static int
84 macppc_setboot(ib_params *params)
85 {
86
87 assert(params != NULL);
88
89 return (shared_bbinfo_setboot(params, &bbparams, writeapplepartmap));
90 }
91
92
93 static int
94 writeapplepartmap(ib_params *params, struct bbinfo_params *bb_params,
95 uint8_t *bb)
96 {
97 struct apple_drvr_map dm;
98 struct apple_part_map_entry pme;
99 int rv;
100 #ifdef DIOCWLABEL
101 int enable;
102 #endif
103
104 assert (params != NULL);
105 assert (bb_params != NULL);
106 assert (bb != NULL);
107
108 if (params->flags & IB_NOWRITE)
109 return (1);
110
111 /* block 0: driver map */
112 if (pread(params->fsfd, &dm, MACPPC_BOOT_BLOCK_BLOCKSIZE, 0) !=
113 MACPPC_BOOT_BLOCK_BLOCKSIZE) {
114 warn("Can't read sector 0 of `%s'", params->filesystem);
115 return (0);
116 }
117 dm.sbSig = htobe16(APPLE_DRVR_MAP_MAGIC);
118 dm.sbBlockSize = htobe16(512);
119 dm.sbBlkCount = htobe32(0);
120
121 #ifdef DIOCWLABEL
122 /*
123 * block 0 is LABELSECTOR which might be protected by
124 * bounds_check_with_label(9).
125 */
126 enable = 1;
127 rv = ioctl(params->fsfd, DIOCWLABEL, &enable);
128 if (rv != 0) {
129 warn("Cannot enable writes to the label sector");
130 return 0;
131 }
132 #endif
133 rv = pwrite(params->fsfd, &dm, MACPPC_BOOT_BLOCK_BLOCKSIZE, 0);
134
135 #ifdef DIOCWLABEL
136 /* Reset write-protect. */
137 enable = 0;
138 (void)ioctl(params->fsfd, DIOCWLABEL, &enable);
139 #endif
140 if (rv != MACPPC_BOOT_BLOCK_BLOCKSIZE) {
141 warn("Can't write sector 0 of `%s'", params->filesystem);
142 return (0);
143 }
144
145 /* block 1: Apple Partition Map */
146 memset(&pme, 0, sizeof(pme));
147 pme.pmSig = htobe16(APPLE_PART_MAP_ENTRY_MAGIC);
148 pme.pmMapBlkCnt = htobe32(2);
149 pme.pmPyPartStart = htobe32(1);
150 pme.pmPartBlkCnt = htobe32(2);
151 pme.pmDataCnt = htobe32(2);
152 strlcpy(pme.pmPartName, "Apple", sizeof(pme.pmPartName));
153 strlcpy(pme.pmPartType, "Apple_partition_map", sizeof(pme.pmPartType));
154 pme.pmPartStatus = htobe32(0x37);
155 if (pwrite(params->fsfd, &pme, MACPPC_BOOT_BLOCK_BLOCKSIZE,
156 1 * MACPPC_BOOT_BLOCK_BLOCKSIZE) != MACPPC_BOOT_BLOCK_BLOCKSIZE) {
157 warn("Can't write Apple Partition Map into sector 1 of `%s'",
158 params->filesystem);
159 return (0);
160 }
161
162 /* block 2: NetBSD partition */
163 memset(&pme, 0, sizeof(pme));
164 pme.pmSig = htobe16(APPLE_PART_MAP_ENTRY_MAGIC);
165 pme.pmMapBlkCnt = htobe32(2);
166 pme.pmPyPartStart = htobe32(4);
167 pme.pmPartBlkCnt = htobe32(0x7fffffff);
168 pme.pmDataCnt = htobe32(0x7fffffff);
169 strlcpy(pme.pmPartName, "NetBSD", sizeof(pme.pmPartName));
170 strlcpy(pme.pmPartType, "NetBSD/macppc", sizeof(pme.pmPartType));
171 pme.pmPartStatus = htobe32(0x3b);
172 pme.pmBootSize = htobe32(roundup(params->s1stat.st_size, 512));
173 pme.pmBootLoad = htobe32(0x4000);
174 pme.pmBootEntry = htobe32(0x4000);
175 strlcpy(pme.pmProcessor, "PowerPC", sizeof(pme.pmProcessor));
176 if (pwrite(params->fsfd, &pme, MACPPC_BOOT_BLOCK_BLOCKSIZE,
177 2 * MACPPC_BOOT_BLOCK_BLOCKSIZE) != MACPPC_BOOT_BLOCK_BLOCKSIZE) {
178 warn("Can't write Apple Partition Map into sector 2 of `%s'",
179 params->filesystem);
180 return (0);
181 }
182
183 return (1);
184 }
185