macppc.c revision 1.2 1 /* $NetBSD: macppc.c,v 1.2 2002/05/15 13:38:42 lukem 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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #if defined(__RCSID) && !defined(__lint)
41 __RCSID("$NetBSD: macppc.c,v 1.2 2002/05/15 13:38:42 lukem Exp $");
42 #endif /* !__lint */
43
44 #if HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47
48 #include <sys/param.h>
49
50 #include <assert.h>
51 #include <err.h>
52 #include <stdio.h>
53 #include <string.h>
54 #include <unistd.h>
55
56 #include "installboot.h"
57
58 static struct bbinfo_params bbparams = {
59 MACPPC_BBINFO_MAGIC,
60 MACPPC_BOOT_BLOCK_OFFSET,
61 MACPPC_BOOT_BLOCK_BLOCKSIZE,
62 MACPPC_BOOT_BLOCK_MAX_SIZE,
63 0,
64 0,
65 };
66
67 static int writeapplepartmap(ib_params *, struct bbinfo_params *, char *);
68
69
70 int
71 macppc_clearboot(ib_params *params)
72 {
73
74 assert(params != NULL);
75
76 if (params->flags & IB_STAGE1START) {
77 warnx("`-b bno' is not supported for %s",
78 params->machine->name);
79 return (0);
80 }
81 return (shared_bbinfo_clearboot(params, &bbparams));
82 }
83
84 static int
85 writeapplepartmap(ib_params *params, struct bbinfo_params *bb_params, char *bb)
86 {
87 struct apple_drvr_map dm;
88 struct apple_part_map_entry pme;
89
90 assert (params != NULL);
91 assert (bb_params != NULL);
92 assert (bb != NULL);
93
94 if (params->flags & IB_NOWRITE)
95 return (1);
96
97 /* block 0: driver map */
98 if (pread(params->fsfd, &dm, MACPPC_BOOT_BLOCK_BLOCKSIZE, 0) !=
99 MACPPC_BOOT_BLOCK_BLOCKSIZE) {
100 warn("Can't read sector 0 of `%s'", params->filesystem);
101 return (0);
102 }
103 dm.sbSig = APPLE_DRVR_MAP_MAGIC;
104 dm.sbBlockSize = 512;
105 dm.sbBlkCount = 0;
106 if (pwrite(params->fsfd, &dm, MACPPC_BOOT_BLOCK_BLOCKSIZE, 0) !=
107 MACPPC_BOOT_BLOCK_BLOCKSIZE) {
108 warn("Can't write sector 0 of `%s'", params->filesystem);
109 return (0);
110 }
111
112 /* block 1: Apple Partition Map */
113 memset(&pme, 0, sizeof(pme));
114 pme.pmSig = APPLE_PART_MAP_ENTRY_MAGIC;
115 pme.pmMapBlkCnt = 2;
116 pme.pmPyPartStart = 1;
117 pme.pmPartBlkCnt = pme.pmDataCnt = 2;
118 strlcpy(pme.pmPartName, "Apple", sizeof(pme.pmPartName));
119 strlcpy(pme.pmPartType, "Apple_partition_map", sizeof(pme.pmPartType));
120 pme.pmPartStatus = 0x37;
121 if (pwrite(params->fsfd, &pme, MACPPC_BOOT_BLOCK_BLOCKSIZE,
122 1 * MACPPC_BOOT_BLOCK_BLOCKSIZE) != MACPPC_BOOT_BLOCK_BLOCKSIZE) {
123 warn("Can't write Apple Partition Map into sector 1 of `%s'",
124 params->filesystem);
125 return (0);
126 }
127
128 /* block 2: NetBSD partition */
129 memset(&pme, 0, sizeof(pme));
130 pme.pmSig = APPLE_PART_MAP_ENTRY_MAGIC;
131 pme.pmMapBlkCnt = 2;
132 pme.pmPyPartStart = 4;
133 pme.pmPartBlkCnt = pme.pmDataCnt = 0x7fffffff;
134 strlcpy(pme.pmPartName, "NetBSD", sizeof(pme.pmPartName));
135 strlcpy(pme.pmPartType, "NetBSD/macppc", sizeof(pme.pmPartType));
136 pme.pmPartStatus = 0x3b;
137 pme.pmBootSize = 0x400;
138 pme.pmBootLoad = 0x4000;
139 pme.pmBootEntry = 0x4000;
140 strlcpy(pme.pmProcessor, "PowerPC", sizeof(pme.pmProcessor));
141 if (pwrite(params->fsfd, &pme, MACPPC_BOOT_BLOCK_BLOCKSIZE,
142 2 * MACPPC_BOOT_BLOCK_BLOCKSIZE) != MACPPC_BOOT_BLOCK_BLOCKSIZE) {
143 warn("Can't write Apple Partition Map into sector 2 of `%s'",
144 params->filesystem);
145 return (0);
146 }
147
148 return (1);
149 }
150
151 int
152 macppc_setboot(ib_params *params)
153 {
154
155 assert(params != NULL);
156
157 if (params->flags & IB_STAGE1START) {
158 warnx("`-b bno' is not supported for %s",
159 params->machine->name);
160 return (0);
161 }
162 return (shared_bbinfo_setboot(params, &bbparams, writeapplepartmap));
163 }
164