1*24b1487dStsutsui /* $NetBSD: md.c,v 1.13 2022/06/24 22:28:11 tsutsui Exp $ */
250dbef1aSdholland
350dbef1aSdholland /*
450dbef1aSdholland * Copyright 1997 Piermont Information Systems Inc.
550dbef1aSdholland * All rights reserved.
650dbef1aSdholland *
750dbef1aSdholland * Based on code written by Philip A. Nelson for Piermont Information
850dbef1aSdholland * Systems Inc. Modified by Minoura Makoto for x68k.
950dbef1aSdholland *
1050dbef1aSdholland * Redistribution and use in source and binary forms, with or without
1150dbef1aSdholland * modification, are permitted provided that the following conditions
1250dbef1aSdholland * are met:
1350dbef1aSdholland * 1. Redistributions of source code must retain the above copyright
1450dbef1aSdholland * notice, this list of conditions and the following disclaimer.
1550dbef1aSdholland * 2. Redistributions in binary form must reproduce the above copyright
1650dbef1aSdholland * notice, this list of conditions and the following disclaimer in the
1750dbef1aSdholland * documentation and/or other materials provided with the distribution.
1850dbef1aSdholland * 3. The name of Piermont Information Systems Inc. may not be used to endorse
1950dbef1aSdholland * or promote products derived from this software without specific prior
2050dbef1aSdholland * written permission.
2150dbef1aSdholland *
2250dbef1aSdholland * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
2350dbef1aSdholland * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2450dbef1aSdholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2550dbef1aSdholland * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
2650dbef1aSdholland * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2750dbef1aSdholland * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2850dbef1aSdholland * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2950dbef1aSdholland * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3050dbef1aSdholland * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3150dbef1aSdholland * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3250dbef1aSdholland * THE POSSIBILITY OF SUCH DAMAGE.
3350dbef1aSdholland */
3450dbef1aSdholland
3550dbef1aSdholland /* md.c -- x68k machine specific routines */
3650dbef1aSdholland /* This file is in close sync with pmax, sparc, and vax md.c */
3750dbef1aSdholland
3850dbef1aSdholland #include <stdio.h>
3950dbef1aSdholland #include <unistd.h>
4050dbef1aSdholland #include <sys/ioctl.h>
4150dbef1aSdholland #include <sys/param.h>
4250dbef1aSdholland #include <util.h>
4350dbef1aSdholland
4450dbef1aSdholland #include "defs.h"
4550dbef1aSdholland #include "md.h"
4650dbef1aSdholland #include "msg_defs.h"
4750dbef1aSdholland #include "menu_defs.h"
4850dbef1aSdholland
4950dbef1aSdholland #ifdef notyet
5050dbef1aSdholland #undef NDOSPART 8
5150dbef1aSdholland #define NDOSPART 16
5250dbef1aSdholland typedef struct parttab {
5350dbef1aSdholland struct dos_partition dosparts[NDOSPART];
5450dbef1aSdholland } parttab;
5550dbef1aSdholland
5650dbef1aSdholland parttab md_disklabel;
5750dbef1aSdholland int md_freepart;
5850dbef1aSdholland int md_nfreepart;
5950dbef1aSdholland #endif /* notyet */
6050dbef1aSdholland
6150dbef1aSdholland int md_need_newdisk = 0;
6250dbef1aSdholland
6350dbef1aSdholland /* prototypes */
6450dbef1aSdholland static int md_newdisk(void);
6550dbef1aSdholland
6650dbef1aSdholland void
md_init(void)6750dbef1aSdholland md_init(void)
6850dbef1aSdholland {
6950dbef1aSdholland }
7050dbef1aSdholland
7150dbef1aSdholland void
md_init_set_status(int flags)7250dbef1aSdholland md_init_set_status(int flags)
7350dbef1aSdholland {
7450dbef1aSdholland (void)flags;
7550dbef1aSdholland }
7650dbef1aSdholland
774103857bSmartin bool
md_get_info(struct install_partition_desc * install)784103857bSmartin md_get_info(struct install_partition_desc *install)
7950dbef1aSdholland {
8050dbef1aSdholland char buf[1024];
8150dbef1aSdholland int fd;
8250dbef1aSdholland char dev_name[100];
8350dbef1aSdholland struct disklabel disklabel;
8450dbef1aSdholland
854b2364d9Smartin snprintf(dev_name, 100, "/dev/r%sc", pm->diskdev);
8650dbef1aSdholland
8750dbef1aSdholland fd = open(dev_name, O_RDONLY, 0);
8850dbef1aSdholland if (fd < 0) {
8950dbef1aSdholland if (logfp)
9050dbef1aSdholland (void)fprintf(logfp, "Can't open %s\n", dev_name);
9150dbef1aSdholland endwin();
9250dbef1aSdholland fprintf(stderr, "Can't open %s\n", dev_name);
9350dbef1aSdholland exit(1);
9450dbef1aSdholland }
9550dbef1aSdholland if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
9650dbef1aSdholland if (logfp)
9750dbef1aSdholland (void)fprintf(logfp, "Can't read disklabel on %s.\n",
9850dbef1aSdholland dev_name);
9950dbef1aSdholland endwin();
10050dbef1aSdholland fprintf(stderr, "Can't read disklabel on %s.\n", dev_name);
10150dbef1aSdholland close(fd);
10250dbef1aSdholland exit(1);
10350dbef1aSdholland }
10450dbef1aSdholland if (disklabel.d_secsize != 512) {
10550dbef1aSdholland endwin();
10650dbef1aSdholland fprintf(stderr, "Non-512byte/sector disk is not supported.\n");
10750dbef1aSdholland close(fd);
10850dbef1aSdholland exit(1);
10950dbef1aSdholland }
11050dbef1aSdholland
1114b2364d9Smartin pm->dlcyl = disklabel.d_ncylinders;
1124b2364d9Smartin pm->dlhead = disklabel.d_ntracks;
1134b2364d9Smartin pm->dlsec = disklabel.d_nsectors;
1144b2364d9Smartin pm->sectorsize = disklabel.d_secsize;
1154b2364d9Smartin pm->dlcylsize = disklabel.d_secpercyl;
1164b2364d9Smartin pm->dlsize = pm->dlcyl*pm->dlhead*pm->dlsec;
11750dbef1aSdholland
11850dbef1aSdholland if (read(fd, buf, 1024) < 0) {
11950dbef1aSdholland endwin();
12050dbef1aSdholland fprintf(stderr, "Can't read %s\n", dev_name);
12150dbef1aSdholland close(fd);
12250dbef1aSdholland exit(1);
12350dbef1aSdholland }
12450dbef1aSdholland if (memcmp(buf, "X68SCSI1", 8) != 0)
12550dbef1aSdholland md_need_newdisk = 1;
12650dbef1aSdholland #ifdef notyet
12750dbef1aSdholland else
12850dbef1aSdholland if (read(fd, md_disklabel, sizeof(md_disklabel)) < 0) {
12950dbef1aSdholland endwin();
13050dbef1aSdholland fprintf(stderr, "Can't read %s\n", dev_name);
13150dbef1aSdholland close(fd);
13250dbef1aSdholland exit(1);
13350dbef1aSdholland }
13450dbef1aSdholland #endif
13550dbef1aSdholland /* preserve first 64 sectors for system. */
1364b2364d9Smartin pm->ptstart = 64;
13750dbef1aSdholland
13850dbef1aSdholland /* preserve existing partitions? */
13950dbef1aSdholland
14050dbef1aSdholland close(fd);
14150dbef1aSdholland
1424103857bSmartin return true;
14350dbef1aSdholland }
14450dbef1aSdholland
14550dbef1aSdholland /*
14650dbef1aSdholland * md back-end code for menu-driven BSD disklabel editor.
14750dbef1aSdholland */
148957b5cd6Smartin int
md_make_bsd_partitions(struct install_partition_desc * install)1494103857bSmartin md_make_bsd_partitions(struct install_partition_desc *install)
15050dbef1aSdholland {
1514103857bSmartin return make_bsd_partitions(install);
15250dbef1aSdholland }
15350dbef1aSdholland
15450dbef1aSdholland /*
15550dbef1aSdholland * any additional partition validation
15650dbef1aSdholland */
1574103857bSmartin bool
md_check_partitions(struct install_partition_desc * install)1584103857bSmartin md_check_partitions(struct install_partition_desc *install)
15950dbef1aSdholland {
16050dbef1aSdholland /* X68k partitions must be in order of the range. */
1614103857bSmartin daddr_t last_end = 0;
1624103857bSmartin size_t i;
1634103857bSmartin char desc[STRSIZE];
16450dbef1aSdholland
1654103857bSmartin for (i = 0; i < install->num; i++) {
1664103857bSmartin if (i > 0) {
167bef52f43Smartin /* skip raw part and similar */
168bef52f43Smartin if (install->infos[i].cur_flags &
169bef52f43Smartin (PTI_SEC_CONTAINER|PTI_PSCHEME_INTERNAL|
170bef52f43Smartin PTI_RAW_PART))
171bef52f43Smartin continue;
172bef52f43Smartin
1734103857bSmartin if (install->infos[i].cur_start < last_end) {
1744103857bSmartin snprintf(desc, sizeof desc,
1754103857bSmartin "%zu (%s)", i,
1764103857bSmartin install->infos[i].mount);
17724ecf24eSchristos msg_fmt_display(MSG_ordering, "%s", desc);
178e21052b4Smartin if (ask_yesno(NULL))
1794103857bSmartin return false;
18050dbef1aSdholland }
18150dbef1aSdholland }
1824103857bSmartin last_end = install->infos[i].cur_start + install->infos[i].size;
18350dbef1aSdholland }
18450dbef1aSdholland
1854103857bSmartin return true;
18650dbef1aSdholland }
18750dbef1aSdholland
18850dbef1aSdholland #ifdef notyet
18950dbef1aSdholland static int
md_check_partitions(void)19050dbef1aSdholland md_check_partitions(void)
19150dbef1aSdholland {
19250dbef1aSdholland int i, j;
19350dbef1aSdholland int preserve;
19450dbef1aSdholland
19550dbef1aSdholland /* check existing BSD partitions. */
19650dbef1aSdholland for (i = 0; i < NDOSPART; i++) {
19750dbef1aSdholland if (md_disklabel.dosparts[i].dp_size == 0)
19850dbef1aSdholland break;
19950dbef1aSdholland if (memcmp(md_disklabel.dosparts[i].dp_typename, "Human68k", 8)) {
20050dbef1aSdholland msg_display(MSG_existing);
201e21052b4Smartin preserve = ask_noyes(NULL);
20250dbef1aSdholland break;
20350dbef1aSdholland }
20450dbef1aSdholland }
2054b2364d9Smartin emptylabel(pm->bsdlabel);
2064b2364d9Smartin pm->bsdlabel[C].pi_fstype = FS_UNUSED;
2074b2364d9Smartin pm->bsdlabel[C].pi_offset = 0;
2084b2364d9Smartin pm->bsdlabel[C].pi_size = pm->dlsize;
20950dbef1aSdholland for (i = 0, j = A; i < NDOSPART;) {
21050dbef1aSdholland if (j == C) {
21150dbef1aSdholland j++;
21250dbef1aSdholland continue;
21350dbef1aSdholland }
21450dbef1aSdholland if (!preserve &&
21550dbef1aSdholland memcmp(md_disklabel.dosparts[i].dp_typename,
21650dbef1aSdholland "Human68k", 8)) {
21750dbef1aSdholland /* discard it. */
21850dbef1aSdholland i++;
21950dbef1aSdholland continue;
22050dbef1aSdholland }
2214b2364d9Smartin pm->bsdlabel[j].pi_fstype = (i == 1) ? FS_SWAP : FS_BSDFFS;
2224b2364d9Smartin pm->bsdlabel[j].pi_offset = md_disklabel.dosparts[i].dp_start*2;
2234b2364d9Smartin pm->bsdlabel[j].pi_size = md_disklabel.dosparts[i].dp_size*2;
22450dbef1aSdholland i++;
22550dbef1aSdholland j++;
22650dbef1aSdholland }
22750dbef1aSdholland if (j > 6) {
22824ecf24eSchristos msg_fmt_display(MSG_nofreepart, "%s" pm->diskdev);
22950dbef1aSdholland return 0;
23050dbef1aSdholland }
23150dbef1aSdholland md_nfreepart = 8 - j;
23250dbef1aSdholland
23350dbef1aSdholland /* check for free space */
2344b2364d9Smartin fspm->ptsize = pm->bsdlabel[A].pi_offset - 64;
2354b2364d9Smartin if (fpm->ptsize <= 0) { /* XXX: should not be 0; minfsdb? */
23624ecf24eSchristos msg_fmt_display(MSG_notfirst, "%s", pm->diskdev);
23750dbef1aSdholland process_menu(MENU_ok);
23850dbef1aSdholland exit(1);
23950dbef1aSdholland }
24050dbef1aSdholland
24150dbef1aSdholland /* Partitions should be preserved in md_make_bsdpartitions() */
24250dbef1aSdholland }
24350dbef1aSdholland #endif /* notyet */
24450dbef1aSdholland
24550dbef1aSdholland /*
24650dbef1aSdholland * hook called before writing new disklabel.
24750dbef1aSdholland */
2484103857bSmartin bool
md_pre_disklabel(struct install_partition_desc * install,struct disk_partitions * parts)2494103857bSmartin md_pre_disklabel(struct install_partition_desc *install,
2504103857bSmartin struct disk_partitions *parts)
25150dbef1aSdholland {
25250dbef1aSdholland if (md_need_newdisk)
25350dbef1aSdholland md_newdisk ();
2544103857bSmartin return true;
25550dbef1aSdholland }
25650dbef1aSdholland
25750dbef1aSdholland /*
25850dbef1aSdholland * hook called after writing disklabel to new target disk.
25950dbef1aSdholland */
2604103857bSmartin bool
md_post_disklabel(struct install_partition_desc * install,struct disk_partitions * parts)2614103857bSmartin md_post_disklabel(struct install_partition_desc *install,
2624103857bSmartin struct disk_partitions *parts)
26350dbef1aSdholland {
2644103857bSmartin return true;
26550dbef1aSdholland }
26650dbef1aSdholland
267*24b1487dStsutsui #ifdef DISKLABEL_NO_ONDISK_VERIFY
268*24b1487dStsutsui /*
269*24b1487dStsutsui * hook to check if disklabel returned by readdisklabel(9) via DIOCGDINFO
270*24b1487dStsutsui * seems the default one, on ports that have no BSD disklabel on disks.
271*24b1487dStsutsui */
272*24b1487dStsutsui bool
md_disklabel_is_default(const struct disklabel * lp)273*24b1487dStsutsui md_disklabel_is_default(const struct disklabel *lp)
274*24b1487dStsutsui {
275*24b1487dStsutsui bool maybe_default =
276*24b1487dStsutsui lp->d_npartitions == RAW_PART + 1 &&
277*24b1487dStsutsui lp->d_partitions[0].p_size == lp->d_partitions[RAW_PART].p_size &&
278*24b1487dStsutsui lp->d_partitions[0].p_fstype == FS_UNUSED &&
279*24b1487dStsutsui lp->d_bbsize == 0 &&
280*24b1487dStsutsui lp->d_sbsize == 0;
281*24b1487dStsutsui
282*24b1487dStsutsui return maybe_default;
283*24b1487dStsutsui }
284*24b1487dStsutsui #endif
285*24b1487dStsutsui
28650dbef1aSdholland /*
28750dbef1aSdholland * hook called after upgrade() or install() has finished setting
28850dbef1aSdholland * up the target disk but immediately before the user is given the
28950dbef1aSdholland * ``disks are now set up'' message.
29050dbef1aSdholland *
29150dbef1aSdholland * On the x68k, we use this opportunity to install the boot blocks.
29250dbef1aSdholland */
29350dbef1aSdholland int
md_post_newfs(struct install_partition_desc * install)2944103857bSmartin md_post_newfs(struct install_partition_desc *install)
29550dbef1aSdholland {
29650dbef1aSdholland /* boot blocks ... */
29724ecf24eSchristos msg_fmt_display(MSG_dobootblks, "%s", pm->diskdev);
29850dbef1aSdholland cp_to_target("/usr/mdec/boot", "/boot");
29950dbef1aSdholland if (run_program(RUN_DISPLAY | RUN_NO_CLEAR,
30050dbef1aSdholland "/usr/mdec/installboot.new /usr/mdec/sdboot_ufs /dev/r%sa",
3014b2364d9Smartin pm->diskdev))
30250dbef1aSdholland process_menu(MENU_ok,
303b77ea2f7Sjoerg __UNCONST("Warning: disk is probably not bootable"));
30442fb6d79Smartin
30542fb6d79Smartin wclear(stdscr);
30642fb6d79Smartin touchwin(stdscr);
30742fb6d79Smartin clearok(stdscr, 1);
30842fb6d79Smartin refresh();
30942fb6d79Smartin
31050dbef1aSdholland return 0;
31150dbef1aSdholland }
31250dbef1aSdholland
31350dbef1aSdholland int
md_post_extract(struct install_partition_desc * install,bool upgrade)3144204f810Smartin md_post_extract(struct install_partition_desc *install, bool upgrade)
31550dbef1aSdholland {
31650dbef1aSdholland return 0;
31750dbef1aSdholland }
31850dbef1aSdholland
31950dbef1aSdholland void
md_cleanup_install(struct install_partition_desc * install)3204103857bSmartin md_cleanup_install(struct install_partition_desc *install)
32150dbef1aSdholland {
32250dbef1aSdholland #ifndef DEBUG
32350dbef1aSdholland enable_rc_conf();
32450dbef1aSdholland #endif
32550dbef1aSdholland }
32650dbef1aSdholland
32750dbef1aSdholland int
md_pre_update(struct install_partition_desc * install)3284103857bSmartin md_pre_update(struct install_partition_desc *install)
32950dbef1aSdholland {
33050dbef1aSdholland return 1;
33150dbef1aSdholland }
33250dbef1aSdholland
33350dbef1aSdholland /* Upgrade support */
33450dbef1aSdholland int
md_update(struct install_partition_desc * install)3354103857bSmartin md_update(struct install_partition_desc *install)
33650dbef1aSdholland {
3374103857bSmartin md_post_newfs(install);
33850dbef1aSdholland return 1;
33950dbef1aSdholland }
34050dbef1aSdholland
34150dbef1aSdholland static int
md_newdisk(void)34250dbef1aSdholland md_newdisk(void)
34350dbef1aSdholland {
34424ecf24eSchristos msg_fmt_display(MSG_newdisk, "%s%s", pm->diskdev, pm->diskdev);
34550dbef1aSdholland
34650dbef1aSdholland return run_program(RUN_FATAL|RUN_DISPLAY,
3474b2364d9Smartin "/usr/mdec/newdisk -v %s", pm->diskdev);
34850dbef1aSdholland }
34950dbef1aSdholland
35050dbef1aSdholland
35150dbef1aSdholland int
md_pre_mount(struct install_partition_desc * install,size_t ndx)3524f30cbf3Smartin md_pre_mount(struct install_partition_desc *install, size_t ndx)
35350dbef1aSdholland {
35450dbef1aSdholland return 0;
35550dbef1aSdholland }
3564103857bSmartin
3574103857bSmartin bool
md_parts_use_wholedisk(struct disk_partitions * parts)3584103857bSmartin md_parts_use_wholedisk(struct disk_partitions *parts)
3594103857bSmartin {
3604103857bSmartin return parts_use_wholedisk(parts, 0, NULL);
3614103857bSmartin }
3624103857bSmartin
3634103857bSmartin #ifdef HAVE_GPT
3644103857bSmartin bool
md_gpt_post_write(struct disk_partitions * parts,part_id root_id,bool root_is_new,part_id efi_id,bool efi_is_new)3654103857bSmartin md_gpt_post_write(struct disk_partitions *parts, part_id root_id,
3664103857bSmartin bool root_is_new, part_id efi_id, bool efi_is_new)
3674103857bSmartin {
3684103857bSmartin /* no GPT boot support, nothing needs to be done here */
3694103857bSmartin return true;
3704103857bSmartin }
3714103857bSmartin #endif
3724103857bSmartin
373