md.c revision 1.6 1 1.6 martin /* $NetBSD: md.c,v 1.6 2019/06/12 06:20:21 martin Exp $ */
2 1.1 dholland
3 1.1 dholland /*
4 1.1 dholland * Copyright 1997 Piermont Information Systems Inc.
5 1.1 dholland * All rights reserved.
6 1.1 dholland *
7 1.1 dholland * Based on code written by Philip A. Nelson for Piermont Information
8 1.1 dholland * Systems Inc.
9 1.1 dholland *
10 1.1 dholland * Redistribution and use in source and binary forms, with or without
11 1.1 dholland * modification, are permitted provided that the following conditions
12 1.1 dholland * are met:
13 1.1 dholland * 1. Redistributions of source code must retain the above copyright
14 1.1 dholland * notice, this list of conditions and the following disclaimer.
15 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 dholland * notice, this list of conditions and the following disclaimer in the
17 1.1 dholland * documentation and/or other materials provided with the distribution.
18 1.1 dholland * 3. The name of Piermont Information Systems Inc. may not be used to endorse
19 1.1 dholland * or promote products derived from this software without specific prior
20 1.1 dholland * written permission.
21 1.1 dholland *
22 1.1 dholland * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
23 1.1 dholland * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 dholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 dholland * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
26 1.1 dholland * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 1.1 dholland * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 1.1 dholland * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 1.1 dholland * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 1.1 dholland * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 1.1 dholland * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 1.1 dholland * THE POSSIBILITY OF SUCH DAMAGE.
33 1.1 dholland */
34 1.1 dholland
35 1.1 dholland /* md.c -- mac68k machine specific routines */
36 1.1 dholland
37 1.1 dholland #include <stdio.h>
38 1.1 dholland #include <util.h>
39 1.1 dholland #include <unistd.h>
40 1.1 dholland #include <fcntl.h>
41 1.1 dholland #include <sys/ioctl.h>
42 1.1 dholland #include <sys/utsname.h>
43 1.1 dholland #include <machine/int_fmtio.h>
44 1.1 dholland
45 1.1 dholland #include "defs.h"
46 1.1 dholland #include "md.h"
47 1.1 dholland #include "msg_defs.h"
48 1.1 dholland #include "menu_defs.h"
49 1.1 dholland
50 1.1 dholland static int stricmp(const char *c1, const char *c2);
51 1.1 dholland static void setpartition(struct apple_part_map_entry *, char *, int);
52 1.1 dholland static int getFreeLabelEntry(char *);
53 1.1 dholland static char *getFstype(struct apple_part_map_entry *, int, char *);
54 1.1 dholland static char *getUse(struct apple_part_map_entry *, int, char *);
55 1.1 dholland static char *getName(struct apple_part_map_entry *, int, char *);
56 1.1 dholland static int findStdType(int, char *, int, int *, int);
57 1.1 dholland static int check_for_errors(void);
58 1.1 dholland static int edit_diskmap(void);
59 1.1 dholland #ifdef MD_DEBUG_SORT_MERGE
60 1.1 dholland static int md_debug_dump(char *);
61 1.1 dholland #endif
62 1.1 dholland
63 1.1 dholland int blk_size;
64 1.1 dholland
65 1.1 dholland MAP_TYPE map_types[] = {
66 1.1 dholland {MAP_RESERVED, APPLE_PART_TYPE_DRIVER},
67 1.1 dholland {MAP_RESERVED, APPLE_PART_TYPE_DRIVER43},
68 1.1 dholland {MAP_RESERVED, APPLE_PART_TYPE_DRIVERATA},
69 1.1 dholland {MAP_RESERVED, APPLE_PART_TYPE_FWB_COMPONENT},
70 1.1 dholland {MAP_MACOS, APPLE_PART_TYPE_MAC},
71 1.1 dholland {MAP_NETBSD, APPLE_PART_TYPE_NETBSD},
72 1.1 dholland {MAP_RESERVED, APPLE_PART_TYPE_PARTMAP},
73 1.1 dholland {MAP_OTHER, APPLE_PART_TYPE_SCRATCH},
74 1.1 dholland {MAP_NETBSD, APPLE_PART_TYPE_UNIX},
75 1.1 dholland {MAP_EOL, NULL}
76 1.1 dholland };
77 1.1 dholland
78 1.1 dholland MAP map = {0, 0, 0, 0, 0, 0, 0, 0, {0}, NULL};
79 1.1 dholland
80 1.1 dholland struct apple_part_map_entry new_map[] =
81 1.1 dholland {
82 1.1 dholland { APPLE_PART_MAP_ENTRY_MAGIC, 0xa5a5, 6, 1, NEW_MAP_SIZE & 0x7e,
83 1.1 dholland "Apple", "Apple_Partition_Map", 0, NEW_MAP_SIZE, 0x37,
84 1.1 dholland 0, 0, 0, 0, 0, 0, 0, {0}, {0}, {0}},
85 1.1 dholland { APPLE_PART_MAP_ENTRY_MAGIC, 0, 6, 64, 32,
86 1.1 dholland "Macintosh", "Apple_Driver", 0, 0, 0x37,
87 1.1 dholland 0, 0, 0, 0, 0, 0, 0, {0}, {0}, {0}},
88 1.1 dholland { APPLE_PART_MAP_ENTRY_MAGIC, 0, 6, 96, 64,
89 1.1 dholland "Macintosh", "Apple_Driver43", 0, 0, 0x37,
90 1.1 dholland 0, 0, 0, 0, 0, 0, 0, {0}, {0}, {0}},
91 1.1 dholland { APPLE_PART_MAP_ENTRY_MAGIC, 0, 6, 160, 64,
92 1.1 dholland "Macintosh", "Apple_Driver_ATA", 0, 0, 0x37,
93 1.1 dholland 0, 0, 0, 0, 0, 0, 0, {0}, {0}, {0}},
94 1.1 dholland { APPLE_PART_MAP_ENTRY_MAGIC, 0, 6, 224, 4096,
95 1.1 dholland "untitled", "Apple_HFS", 0, 0, 0x37,
96 1.1 dholland 0, 0, 0, 0, 0, 0, 0, {0}, {0}, {0}},
97 1.1 dholland { APPLE_PART_MAP_ENTRY_MAGIC, 0, 6,4320, 0,
98 1.1 dholland "untitled", "Apple_Free", 0, 0, 0x37,
99 1.1 dholland 0, 0, 0, 0, 0, 0, 0, {0}, {0}, {0}}
100 1.1 dholland };
101 1.1 dholland
102 1.1 dholland void
103 1.1 dholland md_init(void)
104 1.1 dholland {
105 1.1 dholland }
106 1.1 dholland
107 1.1 dholland void
108 1.1 dholland md_init_set_status(int flags)
109 1.1 dholland {
110 1.1 dholland struct utsname instsys;
111 1.1 dholland
112 1.1 dholland (void)flags;
113 1.1 dholland
114 1.1 dholland /*
115 1.1 dholland * Get the name of the Install Kernel we are running under and
116 1.1 dholland * enable the installation of the corresponding GENERIC kernel.
117 1.1 dholland *
118 1.1 dholland * Note: In md.h the two kernels are disabled. If they are
119 1.1 dholland * enabled there the logic here needs to be switched.
120 1.1 dholland */
121 1.1 dholland uname(&instsys);
122 1.1 dholland if (strstr(instsys.version, "(INSTALLSBC)"))
123 1.1 dholland /*
124 1.1 dholland * Running the SBC Installation Kernel, so enable GENERICSBC
125 1.1 dholland */
126 1.1 dholland set_kernel_set(SET_KERNEL_2);
127 1.1 dholland else
128 1.1 dholland /*
129 1.1 dholland * Running the GENERIC Installation Kernel, so enable GENERIC
130 1.1 dholland */
131 1.1 dholland set_kernel_set(SET_KERNEL_1);
132 1.1 dholland }
133 1.1 dholland
134 1.6 martin bool
135 1.6 martin md_get_info(struct install_partition_desc *install)
136 1.1 dholland {
137 1.1 dholland struct disklabel disklabel;
138 1.1 dholland int fd, i;
139 1.1 dholland char dev_name[100];
140 1.1 dholland struct apple_part_map_entry block;
141 1.1 dholland
142 1.1 dholland snprintf(dev_name, sizeof(dev_name), "/dev/r%s%c",
143 1.2 martin pm->diskdev, 'a' + getrawpartition());
144 1.1 dholland
145 1.1 dholland /*
146 1.1 dholland * Open the disk as a raw device
147 1.1 dholland */
148 1.1 dholland fd = open(dev_name, O_RDONLY, 0);
149 1.1 dholland if (fd < 0) {
150 1.1 dholland endwin();
151 1.1 dholland fprintf (stderr, "Can't open %s\n", dev_name);
152 1.1 dholland exit(1);
153 1.1 dholland }
154 1.1 dholland /*
155 1.1 dholland * Try to get the default disklabel info for the device
156 1.1 dholland */
157 1.1 dholland if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
158 1.1 dholland endwin();
159 1.1 dholland fprintf (stderr, "Can't read disklabel on %s\n", dev_name);
160 1.1 dholland close(fd);
161 1.1 dholland exit(1);
162 1.1 dholland }
163 1.1 dholland /*
164 1.1 dholland * Get the disk parameters from the disk driver. It should have
165 1.1 dholland * obained them by querying the disk itself.
166 1.1 dholland */
167 1.1 dholland blk_size = disklabel.d_secsize;
168 1.2 martin pm->dlcyl = disklabel.d_ncylinders;
169 1.2 martin pm->dlhead = disklabel.d_ntracks;
170 1.2 martin pm->dlsec = disklabel.d_nsectors;
171 1.1 dholland /*
172 1.1 dholland * Just in case, initialize the structures we'll need if we
173 1.1 dholland * need to completely initialize the disk.
174 1.1 dholland */
175 1.2 martin pm->dlsize = disklabel.d_secperunit;
176 1.5 dholland /*
177 1.5 dholland * XXX this code is broken: it accesses off the end of new_map[],
178 1.5 dholland * because NEW_MAP_SIZE is substantially larger than the number of
179 1.5 dholland * entries in new_map[]. Based on the description of struct
180 1.5 dholland * apple_part_map_entry in sys/bootblock.h, and the usage of it in
181 1.5 dholland * new_map[], NEW_MAP_SIZE is expected to be a block count, not an
182 1.5 dholland * entry count. As far I can tell the logic here is just wrong; it
183 1.5 dholland * needs someone with platform knowledge to sort it out.
184 1.5 dholland *
185 1.5 dholland * Note that nothing uses the data this writes into new_map[] so
186 1.5 dholland * disabling it should have no adverse consequences.
187 1.5 dholland *
188 1.5 dholland * - dholland 20160530
189 1.5 dholland */
190 1.5 dholland #if 0 /* XXX broken */
191 1.1 dholland for (i=0;i<NEW_MAP_SIZE;i++) {
192 1.1 dholland if (i > 0)
193 1.1 dholland new_map[i].pmPyPartStart = new_map[i-1].pmPyPartStart +
194 1.1 dholland new_map[i-1].pmPartBlkCnt;
195 1.1 dholland new_map[i].pmDataCnt = new_map[i].pmPartBlkCnt;
196 1.1 dholland if (new_map[i].pmPartBlkCnt == 0) {
197 1.2 martin new_map[i].pmPartBlkCnt = pm->dlsize;
198 1.2 martin new_map[i].pmDataCnt = pm->dlsize;
199 1.1 dholland break;
200 1.1 dholland }
201 1.2 martin pm->dlsize -= new_map[i].pmPartBlkCnt;
202 1.1 dholland }
203 1.5 dholland #endif /* 0 - broken */
204 1.2 martin pm->dlsize = disklabel.d_secperunit;
205 1.1 dholland #if 0
206 1.2 martin msg_display(MSG_dldebug, blk_size, pm->dlcyl, pm->dlhead, pm->dlsec, pm->dlsize);
207 1.1 dholland process_menu(MENU_ok, NULL);
208 1.1 dholland #endif
209 1.1 dholland map.size = 0;
210 1.1 dholland /*
211 1.1 dholland * Verify the disk has been initialized for MacOS use by checking
212 1.1 dholland * to see if the disk have a Boot Block
213 1.1 dholland */
214 1.1 dholland if (lseek(fd, (off_t)0 * blk_size, SEEK_SET) < 0 ||
215 1.1 dholland read(fd, &block, sizeof(block)) - sizeof(block) != 0 ||
216 1.1 dholland block.pmSig != 0x4552) {
217 1.1 dholland process_menu(MENU_nodiskmap, NULL);
218 1.1 dholland }
219 1.1 dholland else {
220 1.1 dholland /*
221 1.1 dholland * Scan for the Partition Map entry that describes the Partition
222 1.1 dholland * Map itself. We need to know the number of blocks allocated
223 1.1 dholland * to it and the number currently in use.
224 1.1 dholland */
225 1.1 dholland for (i=0;i<MAXMAXPARTITIONS;i++) {
226 1.1 dholland lseek(fd, (off_t)(i+1) * blk_size, SEEK_SET);
227 1.1 dholland read(fd, &block, sizeof(block));
228 1.1 dholland if (stricmp("Apple_partition_map", (char *)block.pmPartType) == 0) {
229 1.1 dholland map.size = block.pmPartBlkCnt;
230 1.1 dholland map.in_use_cnt = block.pmMapBlkCnt;
231 1.1 dholland map.blk = (struct apple_part_map_entry *)malloc(map.size * blk_size);
232 1.1 dholland break;
233 1.1 dholland }
234 1.1 dholland }
235 1.1 dholland lseek(fd, (off_t)1 * blk_size, SEEK_SET);
236 1.1 dholland read(fd, map.blk, map.size * blk_size);
237 1.1 dholland }
238 1.1 dholland close(fd);
239 1.1 dholland
240 1.1 dholland return edit_diskmap();
241 1.1 dholland }
242 1.1 dholland
243 1.1 dholland /*
244 1.1 dholland * md back-end code for menu-driven BSD disklabel editor.
245 1.1 dholland */
246 1.6 martin bool
247 1.6 martin md_make_bsd_partitions(struct install_partition_desc *install)
248 1.1 dholland {
249 1.6 martin int rv;
250 1.6 martin #if 0 // XXX
251 1.1 dholland FILE *f;
252 1.6 martin int i, j, pl;
253 1.1 dholland EBZB *bzb;
254 1.6 martin #endif
255 1.1 dholland
256 1.1 dholland /*
257 1.1 dholland * Scan for any problems and report them before continuing.
258 1.1 dholland * The user can abort installation and we'll take them back
259 1.1 dholland * to the main menu; continue ignoring the warnings, or
260 1.1 dholland * ask to reedit the Disk Partition Map.
261 1.1 dholland */
262 1.1 dholland while (1) {
263 1.1 dholland if (check_for_errors()) {
264 1.4 martin process_menu (MENU_sanity, &rv);
265 1.4 martin if (rv < 0)
266 1.6 martin return false;
267 1.4 martin else if (rv)
268 1.1 dholland break;
269 1.1 dholland edit_diskmap();
270 1.1 dholland } else
271 1.1 dholland break;
272 1.1 dholland }
273 1.1 dholland
274 1.6 martin #if 0 // XXX
275 1.1 dholland /* Build standard partitions */
276 1.2 martin memset(&pm->bsdlabel, 0, sizeof pm->bsdlabel);
277 1.1 dholland
278 1.1 dholland /*
279 1.1 dholland * The mac68k port has a predefined partition for "c" which
280 1.1 dholland * is the size of the disk, everything else is unused.
281 1.1 dholland */
282 1.2 martin pm->bsdlabel[RAW_PART].pi_size = pm->dlsize;
283 1.1 dholland /*
284 1.1 dholland * Now, scan through the Disk Partition Map and transfer the
285 1.1 dholland * information into the incore disklabel.
286 1.1 dholland */
287 1.1 dholland for (i=0;i<map.usable_cnt;i++) {
288 1.1 dholland j = map.mblk[i];
289 1.1 dholland bzb = (EBZB *)&map.blk[j].pmBootArgs[0];
290 1.1 dholland if (bzb->flags.part) {
291 1.1 dholland pl = bzb->flags.part - 'a';
292 1.1 dholland switch (whichType(&map.blk[j])) {
293 1.1 dholland case HFS_PART:
294 1.2 martin pm->bsdlabel[pl].pi_fstype = FS_HFS;
295 1.2 martin strcpy (pm->bsdlabel[pl].pi_mount, (char *)bzb->mount_point);
296 1.1 dholland break;
297 1.1 dholland case ROOT_PART:
298 1.1 dholland case UFS_PART:
299 1.2 martin pm->bsdlabel[pl].pi_fstype = FS_BSDFFS;
300 1.2 martin strcpy (pm->bsdlabel[pl].pi_mount, (char *)bzb->mount_point);
301 1.2 martin pm->bsdlabel[pl].pi_flags |= PIF_NEWFS | PIF_MOUNT;
302 1.1 dholland break;
303 1.1 dholland case SWAP_PART:
304 1.2 martin pm->bsdlabel[pl].pi_fstype = FS_SWAP;
305 1.1 dholland break;
306 1.1 dholland case SCRATCH_PART:
307 1.2 martin pm->bsdlabel[pl].pi_fstype = FS_OTHER;
308 1.2 martin strcpy (pm->bsdlabel[pl].pi_mount, (char *)bzb->mount_point);
309 1.1 dholland default:
310 1.1 dholland break;
311 1.1 dholland }
312 1.2 martin if (pm->bsdlabel[pl].pi_fstype != FS_UNUSED) {
313 1.2 martin pm->bsdlabel[pl].pi_size = map.blk[j].pmPartBlkCnt;
314 1.2 martin pm->bsdlabel[pl].pi_offset = map.blk[j].pmPyPartStart;
315 1.2 martin if (pm->bsdlabel[pl].pi_fstype != FS_SWAP) {
316 1.2 martin pm->bsdlabel[pl].pi_frag = 8;
317 1.2 martin pm->bsdlabel[pl].pi_fsize = 1024;
318 1.1 dholland }
319 1.1 dholland }
320 1.1 dholland }
321 1.1 dholland }
322 1.1 dholland
323 1.1 dholland /* Disk name - don't bother asking, just use the physical name*/
324 1.2 martin strcpy (pm->bsddiskname, pm->diskdev);
325 1.1 dholland
326 1.1 dholland #ifdef DEBUG
327 1.1 dholland f = fopen ("/tmp/disktab", "w");
328 1.1 dholland #else
329 1.1 dholland f = fopen ("/etc/disktab", "w");
330 1.1 dholland #endif
331 1.1 dholland if (f == NULL) {
332 1.1 dholland endwin();
333 1.1 dholland (void) fprintf (stderr, "Could not open /etc/disktab");
334 1.1 dholland exit (1);
335 1.1 dholland }
336 1.2 martin (void)fprintf (f, "%s|NetBSD installation generated:\\\n", pm->bsddiskname);
337 1.2 martin (void)fprintf (f, "\t:dt=%s:ty=winchester:\\\n", pm->disktype);
338 1.2 martin (void)fprintf (f, "\t:nc#%d:nt#%d:ns#%d:\\\n", pm->dlcyl, pm->dlhead, pm->dlsec);
339 1.2 martin (void)fprintf (f, "\t:sc#%d:su#%" PRIu32 ":\\\n", pm->dlhead*pm->dlsec, (uint32_t)pm->dlsize);
340 1.2 martin (void)fprintf (f, "\t:se#%d:%s\\\n", blk_size, pm->doessf);
341 1.1 dholland for (i=0; i<8; i++) {
342 1.2 martin if (pm->bsdlabel[i].pi_fstype == FS_HFS)
343 1.1 dholland (void)fprintf (f, "\t:p%c#%d:o%c#%d:t%c=macos:",
344 1.2 martin 'a'+i, pm->bsdlabel[i].pi_size,
345 1.2 martin 'a'+i, pm->bsdlabel[i].pi_offset,
346 1.1 dholland 'a'+i);
347 1.1 dholland else
348 1.1 dholland (void)fprintf (f, "\t:p%c#%d:o%c#%d:t%c=%s:",
349 1.2 martin 'a'+i, pm->bsdlabel[i].pi_size,
350 1.2 martin 'a'+i, pm->bsdlabel[i].pi_offset,
351 1.2 martin 'a'+i, getfslabelname(pm->bsdlabel[i].pi_fstype));
352 1.2 martin if (pm->bsdlabel[i].pi_fstype == FS_BSDFFS)
353 1.1 dholland (void)fprintf (f, "b%c#%d:f%c#%d",
354 1.2 martin 'a'+i, pm->bsdlabel[i].pi_fsize * pm->bsdlabel[i].pi_frag,
355 1.2 martin 'a'+i, pm->bsdlabel[i].pi_fsize);
356 1.1 dholland if (i < 7)
357 1.1 dholland (void)fprintf (f, "\\\n");
358 1.1 dholland else
359 1.1 dholland (void)fprintf (f, "\n");
360 1.1 dholland }
361 1.1 dholland fclose (f);
362 1.6 martin #endif
363 1.1 dholland
364 1.1 dholland /* Everything looks OK. */
365 1.6 martin return true;
366 1.1 dholland }
367 1.1 dholland
368 1.1 dholland /*
369 1.1 dholland * any additional partition validation
370 1.1 dholland */
371 1.6 martin bool
372 1.6 martin md_check_partitions(struct install_partition_desc *install)
373 1.1 dholland {
374 1.6 martin return true;
375 1.1 dholland }
376 1.1 dholland
377 1.1 dholland /*
378 1.1 dholland * hook called before writing new disklabel.
379 1.1 dholland */
380 1.6 martin bool
381 1.6 martin md_pre_disklabel(struct install_partition_desc *install,
382 1.6 martin struct disk_partitions *parts)
383 1.1 dholland {
384 1.1 dholland int fd;
385 1.1 dholland char dev_name[100];
386 1.1 dholland struct disklabel lp;
387 1.1 dholland Block0 new_block0 = {APPLE_DRVR_MAP_MAGIC, 512,
388 1.1 dholland 0, 0, 0, 0, 0, 0, 0, 0, {0}};
389 1.1 dholland
390 1.1 dholland /*
391 1.1 dholland * Danger Will Robinson! We're about to turn that nice MacOS disk
392 1.1 dholland * into an expensive doorstop...
393 1.1 dholland */
394 1.1 dholland printf ("%s", msg_string (MSG_dodiskmap));
395 1.1 dholland
396 1.2 martin snprintf (dev_name, sizeof(dev_name), "/dev/r%sc", pm->diskdev);
397 1.1 dholland /*
398 1.1 dholland * Open the disk as a raw device
399 1.1 dholland */
400 1.1 dholland if ((fd = open(dev_name, O_WRONLY, 0)) < 0) {
401 1.1 dholland endwin();
402 1.1 dholland fprintf(stderr, "Can't open %s to rewrite the Disk Map\n", dev_name);
403 1.1 dholland exit (1);
404 1.1 dholland }
405 1.1 dholland /*
406 1.1 dholland * First check the pmSigPad field of the first block in the incore
407 1.1 dholland * Partition Map. It should be zero, but if it's 0xa5a5 that means
408 1.1 dholland * we need to write out Block0 too.
409 1.1 dholland */
410 1.1 dholland if (map.blk[0].pmSigPad == 0xa5a5) {
411 1.1 dholland if (lseek (fd, (off_t)0 * blk_size, SEEK_SET) < 0) {
412 1.1 dholland endwin();
413 1.1 dholland fprintf (stderr, "Can't position to write Block0\n");
414 1.1 dholland close (fd);
415 1.1 dholland exit (1);
416 1.1 dholland }
417 1.2 martin new_block0.sbBlkCount = pm->dlsize; /* Set disk size */
418 1.1 dholland if (write (fd, &new_block0, blk_size) != blk_size) {
419 1.1 dholland endwin();
420 1.1 dholland fprintf (stderr, "I/O error writing Block0\n");
421 1.1 dholland close (fd);
422 1.1 dholland exit (1);
423 1.1 dholland }
424 1.1 dholland map.blk[0].pmSigPad = 0;
425 1.1 dholland }
426 1.1 dholland if (lseek (fd, (off_t)1 * blk_size, SEEK_SET) < 0) {
427 1.1 dholland endwin();
428 1.1 dholland fprintf (stderr, "Can't position disk to rewrite Disk Map\n");
429 1.1 dholland close (fd);
430 1.1 dholland exit (1);
431 1.1 dholland }
432 1.1 dholland if (write (fd, map.blk, map.size * blk_size) != (map.size * blk_size)) {
433 1.1 dholland endwin();
434 1.1 dholland fprintf(stderr, "I/O error writing Disk Map\n");
435 1.1 dholland close (fd);
436 1.1 dholland exit (1);
437 1.1 dholland }
438 1.1 dholland fsync(fd);
439 1.1 dholland /*
440 1.1 dholland * Well, if we get here the dirty deed has been done.
441 1.1 dholland *
442 1.1 dholland * Now we need to force the incore disk table to get updated. This
443 1.1 dholland * should be done by disklabel -- which is normally called right after
444 1.1 dholland * we return -- but may be commented out for the mac68k port. We'll
445 1.1 dholland * instead update the incore table by forcing a dummy write here. This
446 1.2 martin * relies on a change in the mac68k-specific write_disklabel() routine.
447 1.1 dholland * If that change doesn't exist nothing bad happens here. If disklabel
448 1.1 dholland * properly updates the ondisk and incore labels everything still
449 1.1 dholland * works. Only if we fail here and if disklabel fails are we in
450 1.1 dholland * in a state where we've updated the disk but not the incore and
451 1.1 dholland * a reboot is necessary.
452 1.1 dholland *
453 1.1 dholland * First, we grab a copy of the incore label as it existed before
454 1.1 dholland * we did anything to it. Then we invoke the "write label" ioctl to
455 1.1 dholland * rewrite it to disk. As a result, the ondisk partition map is
456 1.1 dholland * re-read and the incore label is reconstructed from it. If
457 1.1 dholland * disklabel() is then called to update again, either that fails
458 1.1 dholland * because the mac68k port doesn't support native disklabels, or it
459 1.1 dholland * succeeds and writes out a new ondisk copy.
460 1.1 dholland */
461 1.1 dholland ioctl(fd, DIOCGDINFO, &lp); /* Get the current disk label */
462 1.1 dholland ioctl(fd, DIOCWDINFO, &lp); /* Write it out again */
463 1.1 dholland
464 1.1 dholland close (fd);
465 1.6 martin return true;
466 1.1 dholland }
467 1.1 dholland
468 1.1 dholland /*
469 1.1 dholland * hook called after writing disklabel to new target disk.
470 1.1 dholland */
471 1.6 martin bool
472 1.6 martin md_post_disklabel(struct install_partition_desc *install,
473 1.6 martin struct disk_partitions *parts)
474 1.1 dholland {
475 1.6 martin #if 0 // XXX
476 1.1 dholland struct disklabel updated_label;
477 1.1 dholland int fd, i, no_match;
478 1.1 dholland char dev_name[100], buf[80];
479 1.1 dholland const char *fst[] = {"free", "swap", " v6 ", " v7 ", "sysv", "v71k",
480 1.1 dholland " v8 ", "ffs ", "dos ", "lfs ", "othr", "hpfs",
481 1.1 dholland "9660", "boot", "ados", "hfs ", "fcor", "ex2f",
482 1.1 dholland "ntfs", "raid", "ccd "};
483 1.1 dholland
484 1.2 martin snprintf(dev_name, sizeof(dev_name), "/dev/r%sc", pm->diskdev);
485 1.1 dholland /*
486 1.1 dholland * Open the disk as a raw device
487 1.1 dholland */
488 1.1 dholland if ((fd = open(dev_name, O_RDONLY, 0)) < 0)
489 1.6 martin return false;
490 1.1 dholland /*
491 1.1 dholland * Get the "new" label to see if we were successful. If we aren't
492 1.1 dholland * we'll return an error to keep from destroying the user's disk.
493 1.1 dholland */
494 1.1 dholland ioctl(fd, DIOCGDINFO, &updated_label);
495 1.1 dholland close(fd);
496 1.1 dholland /*
497 1.1 dholland * Make sure the in-core label matches the on-disk one
498 1.1 dholland */
499 1.1 dholland no_match = 0;
500 1.1 dholland for (i=0;i<MAXPARTITIONS;i++) {
501 1.1 dholland if (i > updated_label.d_npartitions)
502 1.1 dholland break;
503 1.2 martin if (pm->bsdlabel[i].pi_size != updated_label.d_partitions[i].p_size)
504 1.1 dholland no_match = 1;
505 1.2 martin if (pm->bsdlabel[i].pi_size) {
506 1.2 martin if (pm->bsdlabel[i].pi_offset != updated_label.d_partitions[i].p_offset)
507 1.1 dholland no_match = 1;
508 1.2 martin if (pm->bsdlabel[i].pi_fstype != updated_label.d_partitions[i].p_fstype)
509 1.1 dholland no_match = 1;
510 1.1 dholland }
511 1.1 dholland if (no_match)
512 1.1 dholland break;
513 1.1 dholland }
514 1.1 dholland /*
515 1.1 dholland * If the labels don't match, tell the user why
516 1.1 dholland */
517 1.1 dholland if (no_match) {
518 1.1 dholland msg_clear();
519 1.1 dholland msg_display(MSG_label_error);
520 1.1 dholland msg_table_add(MSG_dump_line,
521 1.1 dholland " in-core: offset size type on-disk: offset size type");
522 1.1 dholland for (i=0;i<MAXPARTITIONS;i++) {
523 1.1 dholland sprintf(buf, " %c:%13.8x%10.8x%5s%16.8x%10.8x%5s", i+'a',
524 1.2 martin pm->bsdlabel[i].pi_offset, pm->bsdlabel[i].pi_size,
525 1.2 martin fst[pm->bsdlabel[i].pi_fstype],
526 1.1 dholland updated_label.d_partitions[i].p_offset,
527 1.1 dholland updated_label.d_partitions[i].p_size,
528 1.1 dholland fst[updated_label.d_partitions[i].p_fstype]);
529 1.1 dholland msg_table_add(MSG_dump_line, buf);
530 1.1 dholland }
531 1.1 dholland process_menu(MENU_ok2, NULL);
532 1.1 dholland }
533 1.6 martin return no_match == 0;
534 1.6 martin #else
535 1.6 martin return true;
536 1.6 martin #endif
537 1.1 dholland }
538 1.1 dholland
539 1.1 dholland /*
540 1.1 dholland * hook called after upgrade() or install() has finished setting
541 1.1 dholland * up the target disk but immediately before the user is given the
542 1.1 dholland * ``disks are now set up'' message.
543 1.1 dholland */
544 1.1 dholland int
545 1.6 martin md_post_newfs(struct install_partition_desc *install)
546 1.1 dholland {
547 1.1 dholland return 0;
548 1.1 dholland }
549 1.1 dholland
550 1.1 dholland int
551 1.6 martin md_post_extract(struct install_partition_desc *install)
552 1.1 dholland {
553 1.1 dholland return 0;
554 1.1 dholland }
555 1.1 dholland
556 1.1 dholland void
557 1.6 martin md_cleanup_install(struct install_partition_desc *install)
558 1.1 dholland {
559 1.1 dholland #ifndef DEBUG
560 1.1 dholland enable_rc_conf();
561 1.1 dholland #endif
562 1.1 dholland }
563 1.1 dholland
564 1.1 dholland int
565 1.6 martin md_pre_update(struct install_partition_desc *install)
566 1.1 dholland {
567 1.1 dholland return 1;
568 1.1 dholland }
569 1.1 dholland
570 1.1 dholland /* Upgrade support */
571 1.1 dholland int
572 1.6 martin md_update(struct install_partition_desc *install)
573 1.1 dholland {
574 1.6 martin md_post_newfs(install);
575 1.1 dholland return 1;
576 1.1 dholland }
577 1.1 dholland
578 1.1 dholland /*
579 1.1 dholland * Compare lexigraphically two strings
580 1.1 dholland */
581 1.1 dholland static int
582 1.1 dholland stricmp(s1, s2)
583 1.1 dholland const char *s1;
584 1.1 dholland const char *s2;
585 1.1 dholland {
586 1.1 dholland char c1, c2;
587 1.1 dholland
588 1.1 dholland while (1) {
589 1.1 dholland c1 = tolower((unsigned char)*s1++);
590 1.1 dholland c2 = tolower((unsigned char)*s2++);
591 1.1 dholland if (c1 < c2) return -1;
592 1.1 dholland if (c1 > c2) return 1;
593 1.1 dholland if (c1 == 0) return 0;
594 1.1 dholland }
595 1.1 dholland }
596 1.1 dholland
597 1.1 dholland static void
598 1.1 dholland setpartition(part, in_use, slot)
599 1.1 dholland struct apple_part_map_entry *part;
600 1.1 dholland char in_use[];
601 1.1 dholland int slot;
602 1.1 dholland {
603 1.1 dholland EBZB *bzb;
604 1.1 dholland
605 1.1 dholland bzb = (EBZB *)&part->pmBootArgs[0];
606 1.1 dholland in_use[slot] = 1;
607 1.1 dholland bzb->flags.used = 1;
608 1.1 dholland bzb->flags.part = 'a' + slot;
609 1.1 dholland }
610 1.1 dholland
611 1.1 dholland /*
612 1.1 dholland * Find an entry in a use array that is unused and return it or
613 1.1 dholland * -1 if no entry is available
614 1.1 dholland */
615 1.1 dholland static int
616 1.1 dholland getFreeLabelEntry(slots)
617 1.1 dholland char *slots;
618 1.1 dholland {
619 1.1 dholland int i;
620 1.1 dholland
621 1.1 dholland for ( i = 0; i < MAXPARTITIONS; i++) {
622 1.1 dholland if (i != RAW_PART && slots[i] == 0)
623 1.1 dholland return i;
624 1.1 dholland }
625 1.1 dholland return -1;
626 1.1 dholland }
627 1.1 dholland
628 1.1 dholland /*
629 1.1 dholland * Figure out what type type of the given partition is and return it.
630 1.1 dholland */
631 1.1 dholland int
632 1.1 dholland whichType(part)
633 1.1 dholland struct apple_part_map_entry *part;
634 1.1 dholland {
635 1.1 dholland MAP_TYPE *map_entry = (MAP_TYPE *)&map_types;
636 1.1 dholland EBZB *bzb;
637 1.1 dholland char partyp[32];
638 1.1 dholland int type, maxsiz, entry_type = MAP_OTHER;
639 1.1 dholland
640 1.1 dholland bzb = (EBZB *)&part->pmBootArgs[0];
641 1.1 dholland if (part->pmSig != APPLE_PART_MAP_ENTRY_MAGIC)
642 1.1 dholland return 0;
643 1.1 dholland maxsiz = sizeof(part->pmPartType);
644 1.1 dholland if (maxsiz > (int)sizeof(partyp))
645 1.1 dholland maxsiz = sizeof(partyp);
646 1.1 dholland strncpy(partyp, (char *)part->pmPartType, maxsiz);
647 1.1 dholland partyp[maxsiz-1] = '\0';
648 1.1 dholland
649 1.1 dholland /*
650 1.1 dholland * Find out how to treat the partition type under NetBSD
651 1.1 dholland */
652 1.1 dholland while (map_entry->type != MAP_EOL) {
653 1.1 dholland if (stricmp(map_entry->name, partyp) == 0) {
654 1.1 dholland entry_type = map_entry->type;
655 1.1 dholland break;
656 1.1 dholland }
657 1.1 dholland map_entry++;
658 1.1 dholland }
659 1.1 dholland
660 1.1 dholland /*
661 1.1 dholland * Now classify the use for NetBSD
662 1.1 dholland */
663 1.1 dholland if (entry_type == MAP_RESERVED)
664 1.1 dholland type = 0;
665 1.1 dholland else if (entry_type == MAP_NETBSD) {
666 1.1 dholland if (bzb->magic != APPLE_BZB_MAGIC)
667 1.1 dholland type = 0;
668 1.1 dholland else if (bzb->type == APPLE_BZB_TYPEFS) {
669 1.1 dholland if (bzb->flags.root)
670 1.1 dholland type = ROOT_PART;
671 1.1 dholland else if (bzb->flags.usr)
672 1.1 dholland type = UFS_PART;
673 1.1 dholland else
674 1.1 dholland type = SCRATCH_PART;
675 1.1 dholland } else if (bzb->type == APPLE_BZB_TYPESWAP)
676 1.1 dholland type = SWAP_PART;
677 1.1 dholland else
678 1.1 dholland type = SCRATCH_PART;
679 1.1 dholland } else if (entry_type == MAP_MACOS)
680 1.1 dholland type = HFS_PART;
681 1.1 dholland else
682 1.1 dholland type = SCRATCH_PART;
683 1.1 dholland return type;
684 1.1 dholland }
685 1.1 dholland
686 1.1 dholland static char *
687 1.1 dholland getFstype(part, len_type, type)
688 1.1 dholland struct apple_part_map_entry *part;
689 1.1 dholland int len_type;
690 1.1 dholland char *type;
691 1.1 dholland {
692 1.1 dholland *type = '\0';
693 1.1 dholland switch(whichType(part)) {
694 1.1 dholland case ROOT_PART:
695 1.1 dholland case UFS_PART:
696 1.1 dholland strncpy(type, "4.2BSD", len_type);
697 1.1 dholland break;
698 1.1 dholland case SWAP_PART:
699 1.1 dholland strncpy(type, "swap", len_type);
700 1.1 dholland break;
701 1.1 dholland case HFS_PART:
702 1.1 dholland strncpy(type, "HFS", len_type);
703 1.1 dholland break;
704 1.1 dholland case SCRATCH_PART:
705 1.1 dholland default:
706 1.1 dholland break;
707 1.1 dholland }
708 1.1 dholland return (type);
709 1.1 dholland }
710 1.1 dholland
711 1.1 dholland static char *
712 1.1 dholland getUse(part, len_use, use)
713 1.1 dholland struct apple_part_map_entry *part;
714 1.1 dholland int len_use;
715 1.1 dholland char *use;
716 1.1 dholland {
717 1.1 dholland EBZB *bzb;
718 1.1 dholland char partyp[32];
719 1.1 dholland
720 1.1 dholland *use = '\0';
721 1.1 dholland bzb = (EBZB *)&part->pmBootArgs[0];
722 1.1 dholland switch(whichType(part)) {
723 1.1 dholland case ROOT_PART:
724 1.1 dholland if (bzb->flags.usr)
725 1.1 dholland strncpy(use, "Root&Usr", len_use);
726 1.1 dholland else
727 1.1 dholland strncpy(use, "Root", len_use);
728 1.1 dholland break;
729 1.1 dholland case UFS_PART:
730 1.1 dholland strncpy(use, "Usr", len_use);
731 1.1 dholland break;
732 1.1 dholland case SWAP_PART:
733 1.1 dholland break;
734 1.1 dholland case HFS_PART:
735 1.1 dholland strncpy(use, "MacOS", len_use);
736 1.1 dholland break;
737 1.1 dholland case SCRATCH_PART:
738 1.1 dholland strncpy(partyp, (char *)part->pmPartType, sizeof(partyp));
739 1.1 dholland partyp[sizeof(partyp)-1] = '\0';
740 1.1 dholland if (stricmp("Apple_Free", partyp) == 0)
741 1.1 dholland strncpy(use, "Free", len_use);
742 1.1 dholland else if (stricmp("Apple_Scratch", partyp) == 0)
743 1.1 dholland strncpy(use, "Scratch", len_use);
744 1.1 dholland else if (stricmp("Apple_MFS", partyp) == 0)
745 1.1 dholland strncpy(use, "MFS", len_use);
746 1.1 dholland else if (stricmp("Apple_PRODOS", partyp) == 0)
747 1.1 dholland strncpy(use, "PRODOS", len_use);
748 1.1 dholland else
749 1.1 dholland strncpy(use, "unknown", len_use);
750 1.1 dholland default:
751 1.1 dholland break;
752 1.1 dholland }
753 1.1 dholland return(use);
754 1.1 dholland }
755 1.1 dholland
756 1.1 dholland static char *
757 1.1 dholland getName(part, len_name, name)
758 1.1 dholland struct apple_part_map_entry *part;
759 1.1 dholland int len_name;
760 1.1 dholland char *name;
761 1.1 dholland {
762 1.1 dholland EBZB *bzb;
763 1.1 dholland int fd;
764 1.1 dholland off_t seek;
765 1.1 dholland char dev_name[100], macosblk[512];
766 1.1 dholland
767 1.1 dholland *name = '\0';
768 1.1 dholland bzb = (EBZB *)&part->pmBootArgs[0];
769 1.1 dholland switch(whichType(part)) {
770 1.1 dholland case SCRATCH_PART:
771 1.1 dholland case ROOT_PART:
772 1.1 dholland case UFS_PART:
773 1.1 dholland strncpy(name, (char *)bzb->mount_point, len_name);
774 1.1 dholland break;
775 1.1 dholland case SWAP_PART:
776 1.1 dholland break;
777 1.1 dholland case HFS_PART:
778 1.1 dholland /*
779 1.1 dholland * OK, this is stupid but it's damn nice to know!
780 1.1 dholland */
781 1.2 martin snprintf (dev_name, sizeof(dev_name), "/dev/r%sc", pm->diskdev);
782 1.1 dholland /*
783 1.1 dholland * Open the disk as a raw device
784 1.1 dholland */
785 1.1 dholland if ((fd = open(dev_name, O_RDONLY, 0)) >= 0) {
786 1.1 dholland seek = (off_t)part->pmPyPartStart + (off_t)2;
787 1.1 dholland seek *= (off_t)blk_size;
788 1.1 dholland lseek(fd, seek, SEEK_SET);
789 1.1 dholland read(fd, &macosblk, sizeof(macosblk));
790 1.1 dholland macosblk[37+32] = '\0';
791 1.1 dholland strncpy(name, (char *)bzb->mount_point, len_name);
792 1.1 dholland strncat(name, " (", len_name-strlen(name));
793 1.1 dholland strncat(name, &macosblk[37], len_name-strlen(name));
794 1.1 dholland strncat(name, ")", len_name-strlen(name));
795 1.1 dholland close(fd);
796 1.1 dholland }
797 1.1 dholland break;
798 1.1 dholland default:
799 1.1 dholland break;
800 1.1 dholland }
801 1.1 dholland return(name);
802 1.1 dholland }
803 1.1 dholland
804 1.1 dholland /*
805 1.1 dholland * Find the first occurance of a Standard Type partition and
806 1.1 dholland * mark it for use along with the default mount slot.
807 1.1 dholland */
808 1.1 dholland static int
809 1.1 dholland findStdType(num_parts, in_use, type, count, alt)
810 1.1 dholland int num_parts;
811 1.1 dholland char in_use[];
812 1.1 dholland int type;
813 1.1 dholland int *count;
814 1.1 dholland int alt;
815 1.1 dholland {
816 1.1 dholland EBZB *bzb;
817 1.1 dholland int i;
818 1.1 dholland
819 1.1 dholland for (i = 0; i < num_parts; i++) {
820 1.1 dholland bzb = (EBZB *)&map.blk[i].pmBootArgs[0];
821 1.1 dholland if (whichType(&map.blk[i]) != type || bzb->flags.used)
822 1.1 dholland continue;
823 1.1 dholland if (type == ROOT_PART) {
824 1.1 dholland if (alt >= 0 && alt != bzb->cluster)
825 1.1 dholland continue;
826 1.1 dholland setpartition(&map.blk[i], in_use, 0);
827 1.1 dholland strcpy ((char *)bzb->mount_point, "/");
828 1.1 dholland *count += 1;
829 1.1 dholland } else if (type == UFS_PART) {
830 1.1 dholland if (alt >= 0 && alt != bzb->cluster)
831 1.1 dholland continue;
832 1.1 dholland setpartition(&map.blk[i], in_use, 6);
833 1.1 dholland if (bzb->mount_point[0] == '\0')
834 1.1 dholland strcpy ((char *)bzb->mount_point, "/usr");
835 1.1 dholland *count += 1;
836 1.1 dholland } else if (type == SWAP_PART) {
837 1.1 dholland setpartition(&map.blk[i], in_use, 1);
838 1.1 dholland *count += 1;
839 1.1 dholland }
840 1.1 dholland return 0;
841 1.1 dholland }
842 1.1 dholland return -1;
843 1.1 dholland }
844 1.1 dholland
845 1.1 dholland /*
846 1.1 dholland * Reset the flags and reserved fields in the selected partition.
847 1.1 dholland * This functions isn't called to process any of the reserved partitions
848 1.1 dholland * where the boot code for MacOS is stored, so (hopefully) we won't
849 1.1 dholland * do more damage that we're trying to avoid. Eventually the NetBSD
850 1.1 dholland * Boot Code will need to go into a partition too, but that should go
851 1.1 dholland * into a reserved partition as well. I'd suggest using a partition
852 1.1 dholland * named something like "NetBSD_Boot" with a pmPartName of "Macintosh".
853 1.1 dholland * The Apple Start Manager (in ROM) will then recognize the partition
854 1.1 dholland * as the one containing the system bootstrip for the volume.
855 1.1 dholland */
856 1.1 dholland void
857 1.1 dholland reset_part_flags(part)
858 1.1 dholland struct apple_part_map_entry *part;
859 1.1 dholland {
860 1.1 dholland EBZB *bzb;
861 1.1 dholland
862 1.1 dholland /*
863 1.1 dholland * Clear out the MacOS fields that might be used for booting just
864 1.1 dholland * in case we've clobbered the boot code.
865 1.1 dholland */
866 1.1 dholland part->pmLgDataStart = 0;
867 1.1 dholland part->pmPartStatus = 0x77; /* make sure the partition shows up */
868 1.1 dholland part->pmLgBootStart = 0;
869 1.1 dholland part->pmBootSize = 0;
870 1.1 dholland part->pmBootLoad = 0;
871 1.1 dholland part->pmBootLoad2 = 0;
872 1.1 dholland part->pmBootEntry = 0;
873 1.1 dholland part->pmBootEntry2 = 0;
874 1.1 dholland part->pmBootCksum = 0;
875 1.1 dholland
876 1.1 dholland /*
877 1.1 dholland * Clear out all the NetBSD fields too. We only clear out the ones
878 1.1 dholland * that should get reset during our processing.
879 1.1 dholland */
880 1.1 dholland bzb = (EBZB *)&part->pmBootArgs[0];
881 1.1 dholland bzb->magic = 0;
882 1.1 dholland bzb->cluster = 0;
883 1.1 dholland bzb->inode = 0;
884 1.1 dholland bzb->type = 0;
885 1.1 dholland bzb->flags.root = 0;
886 1.1 dholland bzb->flags.usr = 0;
887 1.1 dholland bzb->flags.crit = 0;
888 1.1 dholland bzb->flags.slice = 0;
889 1.1 dholland bzb->flags.used = 0;
890 1.1 dholland return;
891 1.1 dholland }
892 1.1 dholland
893 1.1 dholland /*
894 1.1 dholland * sortmerge:
895 1.1 dholland * 1) Moves all the Partition Map entries to the front of the Map.
896 1.1 dholland * This is required because some disk formatters leave holes.
897 1.1 dholland * 2) Sorts all entries by ascending start block number.
898 1.1 dholland * Needed so the NetBSD algorithm for finding partitions works
899 1.1 dholland * consistently from a user perspective.
900 1.1 dholland * 3) Collapse multiple adjected "free" entries into a single entry.
901 1.1 dholland * 4) Identify the NetBSD mount_points.
902 1.1 dholland */
903 1.1 dholland void
904 1.1 dholland sortmerge(void)
905 1.1 dholland {
906 1.1 dholland struct apple_part_map_entry tmp_blk;
907 1.1 dholland char in_use[MAXPARTITIONS];
908 1.1 dholland int i, j;
909 1.1 dholland EBZB *bzb;
910 1.1 dholland
911 1.1 dholland /*
912 1.1 dholland * Step 1, squeeze out the holes that some disk formatters leave in
913 1.1 dholland * the Map. Also convert any "old" Map entries to the new entry
914 1.1 dholland * type. Also clear out our used flag which is used to indicte
915 1.1 dholland * we've mapped the partition.
916 1.1 dholland */
917 1.1 dholland map.in_use_cnt = 0;
918 1.1 dholland for (i=0;i<map.size-1;i++) {
919 1.1 dholland if (map.blk[i].pmSig == 0x5453)
920 1.1 dholland map.blk[i].pmSig = APPLE_PART_MAP_ENTRY_MAGIC;
921 1.1 dholland if (map.blk[i].pmSig != APPLE_PART_MAP_ENTRY_MAGIC) {
922 1.1 dholland for (j=i+1;j<map.size;j++) {
923 1.1 dholland if (map.blk[j].pmSig == 0x5453)
924 1.1 dholland map.blk[j].pmSig = APPLE_PART_MAP_ENTRY_MAGIC;
925 1.1 dholland if (map.blk[j].pmSig == APPLE_PART_MAP_ENTRY_MAGIC) {
926 1.1 dholland memcpy (&map.blk[i], &map.blk[j], sizeof(map.blk[i]));
927 1.1 dholland map.blk[j].pmSig = 0;
928 1.1 dholland break;
929 1.1 dholland }
930 1.1 dholland }
931 1.1 dholland } else {
932 1.1 dholland map.in_use_cnt += 1;
933 1.1 dholland bzb = (EBZB *)&map.blk[i].pmBootArgs[0];
934 1.1 dholland bzb->flags.used = 0;
935 1.1 dholland bzb->flags.part = 0;
936 1.1 dholland }
937 1.1 dholland }
938 1.1 dholland
939 1.1 dholland /*
940 1.1 dholland * Step 2, sort by ascending starting block number. Since
941 1.1 dholland * we've already removed the holes we only need to
942 1.1 dholland * deal with the in_use count of blocks.
943 1.1 dholland */
944 1.1 dholland for (i=0;i<map.in_use_cnt-1;i++) {
945 1.1 dholland for (j=i+1;j<map.in_use_cnt;j++) {
946 1.1 dholland if (map.blk[i].pmPyPartStart > map.blk[j].pmPyPartStart) {
947 1.1 dholland memcpy (&tmp_blk, &map.blk[i], sizeof(tmp_blk));
948 1.1 dholland memcpy (&map.blk[i], &map.blk[j], sizeof(map.blk[i]));
949 1.1 dholland memcpy (&map.blk[j], &tmp_blk, sizeof(map.blk[j]));
950 1.1 dholland }
951 1.1 dholland }
952 1.1 dholland }
953 1.1 dholland
954 1.1 dholland /*
955 1.1 dholland * Step 3, merge adjacent free space
956 1.1 dholland */
957 1.1 dholland for (i=0;i<map.in_use_cnt-1;i++) {
958 1.1 dholland if (stricmp("Apple_Free", (char *)map.blk[i].pmPartType) == 0 &&
959 1.1 dholland stricmp("Apple_Free", (char *)map.blk[i+1].pmPartType) == 0) {
960 1.1 dholland map.blk[i].pmPartBlkCnt += map.blk[i+1].pmPartBlkCnt;
961 1.1 dholland map.blk[i].pmDataCnt += map.blk[i+1].pmDataCnt;
962 1.1 dholland map.blk[i+1].pmSig = 0;
963 1.1 dholland for (j=i+1;j<map.in_use_cnt-1;j++) {
964 1.1 dholland memcpy (&map.blk[j], &map.blk[j+1], sizeof(map.blk[j]));
965 1.1 dholland map.blk[j+1].pmSig = 0;
966 1.1 dholland }
967 1.1 dholland map.in_use_cnt -= 1;
968 1.1 dholland }
969 1.1 dholland }
970 1.1 dholland
971 1.1 dholland /*
972 1.1 dholland * Step 4, try to identify the mount points for the partitions
973 1.1 dholland * and adjust the pmMapBlkCnt in each Map entry. Set
974 1.1 dholland * up the display array for the non-reserved partitions,
975 1.1 dholland * and count the number of NetBSD usable partitions
976 1.1 dholland */
977 1.1 dholland map.hfs_cnt = 0;
978 1.1 dholland map.root_cnt = 0;
979 1.1 dholland map.swap_cnt = 0;
980 1.1 dholland map.usr_cnt = 0;
981 1.1 dholland map.usable_cnt = 0;
982 1.1 dholland /*
983 1.1 dholland * Clear out record of partition slots already in use
984 1.1 dholland */
985 1.1 dholland memset(&in_use, 0, sizeof(in_use));
986 1.1 dholland for (i=0,j=0;i<map.in_use_cnt;i++) {
987 1.1 dholland map.blk[i].pmSig = APPLE_PART_MAP_ENTRY_MAGIC;
988 1.1 dholland map.blk[i].pmMapBlkCnt = map.in_use_cnt;
989 1.1 dholland /*
990 1.1 dholland * Since MAXPARTITIONS == 8 for mac68k, and we do not display
991 1.1 dholland * the c partition, we only need 7 partition slots on the screen.
992 1.1 dholland * If/when MAXPARTITIONS is changed, the "Edit Disk Partition Map"
993 1.1 dholland * needs to be a scrollable view of the partition table.
994 1.1 dholland */
995 1.1 dholland if (whichType(&map.blk[i]) && (j < MAXPARTITIONS - 1)) {
996 1.1 dholland map.mblk[j++] = i;
997 1.1 dholland map.usable_cnt += 1;
998 1.1 dholland }
999 1.1 dholland }
1000 1.1 dholland /*
1001 1.1 dholland * Fill in standard partitions. Look for a Cluster "0" first and use
1002 1.1 dholland * it, otherwise take any Cluster value.
1003 1.1 dholland */
1004 1.1 dholland if (findStdType(map.in_use_cnt, in_use, ROOT_PART, &map.root_cnt, 0))
1005 1.1 dholland findStdType(map.in_use_cnt, in_use, ROOT_PART, &map.root_cnt, -1);
1006 1.1 dholland if (findStdType(map.in_use_cnt, in_use, UFS_PART, &map.usr_cnt, 0))
1007 1.1 dholland findStdType(map.in_use_cnt, in_use, UFS_PART, &map.usr_cnt, -1);
1008 1.1 dholland if (findStdType(map.in_use_cnt, in_use, SWAP_PART, &map.swap_cnt, 0))
1009 1.1 dholland findStdType(map.in_use_cnt, in_use, SWAP_PART, &map.swap_cnt, -1);
1010 1.1 dholland
1011 1.1 dholland #ifdef MD_DEBUG_SORT_MERGE
1012 1.1 dholland md_debug_dump("After marking Standard Types");
1013 1.1 dholland #endif
1014 1.1 dholland /*
1015 1.1 dholland * Now fill in the remaining partitions counting them by type and
1016 1.1 dholland * assigning them the slot the where the kernel should map them.
1017 1.1 dholland * This will be where they are displayed in the Edit Map.
1018 1.1 dholland */
1019 1.1 dholland for (i=0; i < map.in_use_cnt; i++) {
1020 1.1 dholland bzb = (EBZB *)&map.blk[i].pmBootArgs[0];
1021 1.1 dholland if (bzb->flags.used == 0) {
1022 1.1 dholland if ((j = getFreeLabelEntry(in_use)) < 0)
1023 1.1 dholland break;
1024 1.1 dholland switch (whichType(&map.blk[i])) {
1025 1.1 dholland case ROOT_PART:
1026 1.1 dholland map.root_cnt += 1;
1027 1.1 dholland setpartition(&map.blk[i], in_use, j);
1028 1.1 dholland break;
1029 1.1 dholland case UFS_PART:
1030 1.1 dholland map.usr_cnt += 1;
1031 1.1 dholland setpartition(&map.blk[i], in_use, j);
1032 1.1 dholland break;
1033 1.1 dholland case SWAP_PART:
1034 1.1 dholland map.swap_cnt += 1;
1035 1.1 dholland setpartition(&map.blk[i], in_use, j);
1036 1.1 dholland break;
1037 1.1 dholland case HFS_PART:
1038 1.1 dholland map.hfs_cnt += 1;
1039 1.1 dholland setpartition(&map.blk[i], in_use, j);
1040 1.1 dholland break;
1041 1.1 dholland case SCRATCH_PART:
1042 1.1 dholland setpartition(&map.blk[i], in_use, j);
1043 1.1 dholland default:
1044 1.1 dholland break;
1045 1.1 dholland }
1046 1.1 dholland }
1047 1.1 dholland }
1048 1.1 dholland #ifdef MD_DEBUG_SORT_MERGE
1049 1.1 dholland md_debug_dump("After sort merge");
1050 1.1 dholland #endif
1051 1.1 dholland return;
1052 1.1 dholland }
1053 1.1 dholland
1054 1.1 dholland void
1055 1.1 dholland disp_selected_part(sel)
1056 1.1 dholland int sel;
1057 1.1 dholland {
1058 1.1 dholland int i,j;
1059 1.1 dholland char fstyp[16], use[16], name[32];
1060 1.1 dholland EBZB *bzb;
1061 1.1 dholland
1062 1.6 martin msg_table_add(MSG_mac68k_part_header);
1063 1.1 dholland for (i=0;i<map.usable_cnt;i++) {
1064 1.1 dholland if (i == sel) msg_standout();
1065 1.1 dholland j = map.mblk[i];
1066 1.1 dholland getFstype(&map.blk[j], sizeof(fstyp), fstyp);
1067 1.1 dholland getUse(&map.blk[j], sizeof(use), use);
1068 1.1 dholland getName(&map.blk[j], sizeof(name), name);
1069 1.1 dholland bzb = (EBZB *)&map.blk[j].pmBootArgs[0];
1070 1.2 martin msg_table_add(MSG_part_row, pm->diskdev,
1071 1.1 dholland bzb->flags.part, map.blk[j].pmPyPartStart,
1072 1.1 dholland map.blk[j].pmPartBlkCnt, fstyp, use, name);
1073 1.1 dholland if (i == sel) msg_standend();
1074 1.1 dholland }
1075 1.1 dholland return;
1076 1.1 dholland }
1077 1.1 dholland
1078 1.1 dholland /*
1079 1.1 dholland * check for any anomalies on the requested setup
1080 1.1 dholland */
1081 1.1 dholland static int
1082 1.1 dholland check_for_errors()
1083 1.1 dholland {
1084 1.1 dholland int i, j;
1085 1.1 dholland int errs = 0;
1086 1.1 dholland
1087 1.1 dholland errs = (!map.root_cnt) || (map.root_cnt > 1) || (!map.swap_cnt) ||
1088 1.1 dholland (map.swap_cnt > 1);
1089 1.1 dholland
1090 1.1 dholland for (i=0;i<map.usable_cnt;i++) {
1091 1.1 dholland j = map.mblk[i];
1092 1.2 martin if (map.blk[j].pmPyPartStart > pm->dlsize)
1093 1.1 dholland errs++;
1094 1.2 martin if ((map.blk[j].pmPyPartStart + map.blk[j].pmPartBlkCnt) > pm->dlsize + 1)
1095 1.1 dholland errs++;
1096 1.1 dholland }
1097 1.1 dholland return(errs);
1098 1.1 dholland }
1099 1.1 dholland
1100 1.1 dholland /*
1101 1.1 dholland * check for and report anomalies on the requested setup
1102 1.1 dholland */
1103 1.1 dholland void
1104 1.1 dholland report_errors()
1105 1.1 dholland {
1106 1.1 dholland int i, j;
1107 1.1 dholland int errs = 0;
1108 1.1 dholland EBZB *bzb;
1109 1.1 dholland
1110 1.1 dholland if (!map.root_cnt) {
1111 1.1 dholland msg_display_add(MSG_disksetup_no_root);
1112 1.1 dholland errs++;
1113 1.1 dholland }
1114 1.1 dholland if (map.root_cnt > 1) {
1115 1.1 dholland msg_display_add(MSG_disksetup_multiple_roots);
1116 1.1 dholland errs++;
1117 1.1 dholland }
1118 1.1 dholland if (!map.swap_cnt) {
1119 1.1 dholland msg_display_add(MSG_disksetup_no_swap);
1120 1.1 dholland errs++;
1121 1.1 dholland }
1122 1.1 dholland if (map.swap_cnt > 1) {
1123 1.1 dholland msg_display_add(MSG_disksetup_multiple_swaps);
1124 1.1 dholland errs++;
1125 1.1 dholland }
1126 1.1 dholland for (i=0;i<map.usable_cnt;i++) {
1127 1.1 dholland j = map.mblk[i];
1128 1.2 martin if (map.blk[j].pmPyPartStart > pm->dlsize) {
1129 1.1 dholland bzb = (EBZB *)&map.blk[j].pmBootArgs[0];
1130 1.1 dholland msg_display_add(MSG_disksetup_part_beginning,
1131 1.2 martin pm->diskdev, bzb->flags.part);
1132 1.1 dholland errs++;
1133 1.1 dholland }
1134 1.2 martin if ((map.blk[j].pmPyPartStart + map.blk[j].pmPartBlkCnt) > pm->dlsize) {
1135 1.1 dholland bzb = (EBZB *)&map.blk[j].pmBootArgs[0];
1136 1.1 dholland msg_display_add(MSG_disksetup_part_size,
1137 1.2 martin pm->diskdev, bzb->flags.part);
1138 1.1 dholland errs++;
1139 1.1 dholland }
1140 1.1 dholland }
1141 1.1 dholland if (!errs)
1142 1.1 dholland msg_display_add(MSG_disksetup_noerrors);
1143 1.1 dholland return;
1144 1.1 dholland }
1145 1.1 dholland
1146 1.1 dholland static int
1147 1.1 dholland edit_diskmap(void)
1148 1.1 dholland {
1149 1.1 dholland int i;
1150 1.1 dholland
1151 1.1 dholland /* Ask full/part */
1152 1.6 martin msg_display (MSG_mac68k_fullpart, pm->diskdev);
1153 1.1 dholland process_menu (MENU_fullpart, NULL);
1154 1.1 dholland
1155 1.1 dholland map.selected = 0;
1156 1.1 dholland sortmerge();
1157 1.1 dholland
1158 1.1 dholland /* If blowing away the whole disk, let user know if there
1159 1.1 dholland * are any active disk partitions */
1160 1.1 dholland if (usefull) {
1161 1.1 dholland if (map.usable_cnt > (map.root_cnt+map.swap_cnt+map.usr_cnt)) {
1162 1.6 martin msg_display (MSG_mac68k_ovrwrite);
1163 1.3 martin if (!ask_noyes(NULL)) {
1164 1.1 dholland endwin();
1165 1.1 dholland return 0;
1166 1.1 dholland }
1167 1.1 dholland }
1168 1.1 dholland /*
1169 1.1 dholland * mark all non-reserved partitions as "free"
1170 1.1 dholland * then sort and merge the map into something sensible
1171 1.1 dholland */
1172 1.1 dholland for (i=0;i<map.size;i++)
1173 1.1 dholland if (whichType(&map.blk[i]))
1174 1.1 dholland strcpy ((char *)map.blk[i].pmPartType, "Apple_Free");
1175 1.1 dholland sortmerge();
1176 1.1 dholland }
1177 1.1 dholland process_menu (MENU_editparttable, NULL);
1178 1.1 dholland return 1;
1179 1.1 dholland }
1180 1.1 dholland
1181 1.1 dholland #ifdef MD_DEBUG_SORT_MERGE
1182 1.1 dholland static int
1183 1.1 dholland md_debug_dump(title)
1184 1.1 dholland char *title;
1185 1.1 dholland {
1186 1.1 dholland char buf[96], type;
1187 1.1 dholland char fstyp[16], use[16], name[64];
1188 1.4 martin int i, j, rv;
1189 1.1 dholland EBZB *bzb;
1190 1.1 dholland
1191 1.1 dholland msg_clear();
1192 1.1 dholland sprintf(buf, "Apple Disk Partition Map: %s", title);
1193 1.1 dholland msg_table_add(MSG_dump_line, buf);
1194 1.1 dholland msg_table_add(MSG_dump_line,
1195 1.1 dholland "slot base fstype use name");
1196 1.1 dholland for (i=0;i<map.in_use_cnt;i++) {
1197 1.1 dholland j = whichType(&map.blk[i]);
1198 1.1 dholland getFstype(&map.blk[i], sizeof(fstyp), fstyp);
1199 1.1 dholland getUse(&map.blk[i], sizeof(use), use);
1200 1.1 dholland getName(&map.blk[i], sizeof(name), name);
1201 1.1 dholland bzb = (EBZB *) &map.blk[i].pmBootArgs[0];
1202 1.1 dholland type = bzb->flags.part;
1203 1.1 dholland if (type < 'a' || type > 'h') type = '?';
1204 1.1 dholland if (j == 0) strcpy (name, "reserved for Apple");
1205 1.1 dholland sprintf(buf, " %02d:%c %08x %8s %10s %s", i+1, type,
1206 1.1 dholland map.blk[i].pmPyPartStart, fstyp, use, name);
1207 1.1 dholland msg_table_add(MSG_dump_line, buf);
1208 1.1 dholland }
1209 1.4 martin process_menu(MENU_okabort, &rv);
1210 1.1 dholland msg_clear();
1211 1.4 martin return rv;
1212 1.1 dholland }
1213 1.1 dholland #endif /* MD_DEBUG_SORT_MERGE */
1214 1.1 dholland
1215 1.1 dholland int
1216 1.6 martin md_pre_mount(struct install_partition_desc *install)
1217 1.1 dholland {
1218 1.1 dholland return 0;
1219 1.1 dholland }
1220 1.6 martin
1221 1.6 martin bool
1222 1.6 martin md_parts_use_wholedisk(struct disk_partitions *parts)
1223 1.6 martin {
1224 1.6 martin return parts_use_wholedisk(parts, 0, NULL);
1225 1.6 martin }
1226 1.6 martin
1227 1.6 martin #ifdef HAVE_GPT
1228 1.6 martin bool
1229 1.6 martin md_gpt_post_write(struct disk_partitions *parts, part_id root_id,
1230 1.6 martin bool root_is_new, part_id efi_id, bool efi_is_new)
1231 1.6 martin {
1232 1.6 martin /* no GPT boot support, nothing needs to be done here */
1233 1.6 martin return true;
1234 1.6 martin }
1235 1.6 martin #endif
1236 1.6 martin
1237