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