1*eece741bSmartin /* $NetBSD: md.c,v 1.16 2022/10/02 10:21:36 martin Exp $ */
250dbef1aSdholland
350dbef1aSdholland /*
450dbef1aSdholland * Copyright 1997,2002 Piermont Information Systems Inc.
550dbef1aSdholland * All rights reserved.
650dbef1aSdholland *
750dbef1aSdholland * Based on code written by Philip A. Nelson for Piermont Information
850dbef1aSdholland * Systems Inc.
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 -- landisk machine specific routines */
3650dbef1aSdholland
3750dbef1aSdholland #include <sys/param.h>
3850dbef1aSdholland #include <sys/sysctl.h>
3950dbef1aSdholland #include <stdio.h>
4050dbef1aSdholland #include <util.h>
4150dbef1aSdholland
4250dbef1aSdholland #include "defs.h"
4350dbef1aSdholland #include "md.h"
4450dbef1aSdholland #include "msg_defs.h"
4550dbef1aSdholland #include "menu_defs.h"
4650dbef1aSdholland
4750dbef1aSdholland void
md_init(void)4850dbef1aSdholland md_init(void)
4950dbef1aSdholland {
5050dbef1aSdholland }
5150dbef1aSdholland
5250dbef1aSdholland void
md_init_set_status(int flags)5350dbef1aSdholland md_init_set_status(int flags)
5450dbef1aSdholland {
5550dbef1aSdholland (void)flags;
5650dbef1aSdholland }
5750dbef1aSdholland
584103857bSmartin bool
md_get_info(struct install_partition_desc * install)594103857bSmartin md_get_info(struct install_partition_desc *install)
6050dbef1aSdholland {
61957b5cd6Smartin int res;
62e11b8d25Smartin
63e11b8d25Smartin if (pm->no_mbr || pm->no_part)
64e11b8d25Smartin return true;
65e11b8d25Smartin
66957b5cd6Smartin again:
67e11b8d25Smartin if (pm->parts == NULL) {
68e11b8d25Smartin
69e11b8d25Smartin const struct disk_partitioning_scheme *ps =
70e11b8d25Smartin select_part_scheme(pm, NULL, true, NULL);
71e11b8d25Smartin
72e11b8d25Smartin if (!ps)
736fec6798Smartin return false;
74e11b8d25Smartin
75e11b8d25Smartin struct disk_partitions *parts =
76e11b8d25Smartin (*ps->create_new_for_disk)(pm->diskdev,
7786906049Smartin 0, pm->dlsize, true, NULL);
78e11b8d25Smartin if (!parts)
79e11b8d25Smartin return false;
80e11b8d25Smartin
81e11b8d25Smartin pm->parts = parts;
82e11b8d25Smartin if (ps->size_limit > 0 && pm->dlsize > ps->size_limit)
83e11b8d25Smartin pm->dlsize = ps->size_limit;
84e11b8d25Smartin }
85e11b8d25Smartin
86957b5cd6Smartin res = set_bios_geom_with_mbr_guess(pm->parts);
87957b5cd6Smartin if (res == 0)
88957b5cd6Smartin return false;
89957b5cd6Smartin else if (res == 1)
90957b5cd6Smartin return true;
91957b5cd6Smartin
92957b5cd6Smartin pm->parts->pscheme->destroy_part_scheme(pm->parts);
93957b5cd6Smartin pm->parts = NULL;
94957b5cd6Smartin goto again;
9550dbef1aSdholland }
9650dbef1aSdholland
9750dbef1aSdholland /*
9850dbef1aSdholland * md back-end code for menu-driven BSD disklabel editor.
9950dbef1aSdholland */
100957b5cd6Smartin int
md_make_bsd_partitions(struct install_partition_desc * install)1014103857bSmartin md_make_bsd_partitions(struct install_partition_desc *install)
10250dbef1aSdholland {
1034103857bSmartin return make_bsd_partitions(install);
10450dbef1aSdholland }
10550dbef1aSdholland
10650dbef1aSdholland /*
10750dbef1aSdholland * any additional partition validation
10850dbef1aSdholland */
1094103857bSmartin bool
md_check_partitions(struct install_partition_desc * install)1104103857bSmartin md_check_partitions(struct install_partition_desc *install)
11150dbef1aSdholland {
1124103857bSmartin return true;
11350dbef1aSdholland }
11450dbef1aSdholland
11550dbef1aSdholland /*
11650dbef1aSdholland * hook called before writing new disklabel.
11750dbef1aSdholland */
1184103857bSmartin bool
md_pre_disklabel(struct install_partition_desc * install,struct disk_partitions * parts)1194103857bSmartin md_pre_disklabel(struct install_partition_desc *install,
1204103857bSmartin struct disk_partitions *parts)
12150dbef1aSdholland {
12250dbef1aSdholland
1234103857bSmartin if (parts->parent == NULL)
1244103857bSmartin return true; /* no outer partitions */
12550dbef1aSdholland
1264103857bSmartin parts = parts->parent;
1274103857bSmartin
1284103857bSmartin msg_display_subst(MSG_dofdisk, 3, parts->disk,
1294103857bSmartin msg_string(parts->pscheme->name),
1304103857bSmartin msg_string(parts->pscheme->short_name));
1314103857bSmartin
1324103857bSmartin /* write edited "MBR" onto disk. */
133*eece741bSmartin if (!parts->pscheme->write_to_disk(parts) ||
134*eece741bSmartin run_program(RUN_SILENT | RUN_ERROR_OK,
135*eece741bSmartin "/sbin/fdisk -f -i -c /usr/mdec/mbr %s", parts->disk)) {
13650dbef1aSdholland msg_display(MSG_wmbrfail);
13750dbef1aSdholland process_menu(MENU_ok, NULL);
1384103857bSmartin return false;
13950dbef1aSdholland }
1404103857bSmartin return true;
14150dbef1aSdholland }
14250dbef1aSdholland
14350dbef1aSdholland /*
14450dbef1aSdholland * hook called after writing disklabel to new target disk.
14550dbef1aSdholland */
1464103857bSmartin bool
md_post_disklabel(struct install_partition_desc * install,struct disk_partitions * parts)1474103857bSmartin md_post_disklabel(struct install_partition_desc *install,
1484103857bSmartin struct disk_partitions *parts)
14950dbef1aSdholland {
1504103857bSmartin return true;
15150dbef1aSdholland }
15250dbef1aSdholland
15350dbef1aSdholland /*
15450dbef1aSdholland * hook called after upgrade() or install() has finished setting
15550dbef1aSdholland * up the target disk but immediately before the user is given the
15650dbef1aSdholland * ``disks are now set up'' message.
15750dbef1aSdholland *
15850dbef1aSdholland * On the landisk, we use this opportunity to install the boot blocks.
15950dbef1aSdholland */
16050dbef1aSdholland int
md_post_newfs(struct install_partition_desc * install)1614103857bSmartin md_post_newfs(struct install_partition_desc *install)
16250dbef1aSdholland {
16350dbef1aSdholland char *bootxx;
16450dbef1aSdholland int error;
16550dbef1aSdholland
16624ecf24eSchristos msg_fmt_display(MSG_dobootblks, "%s", pm->diskdev);
16750dbef1aSdholland cp_to_target("/usr/mdec/boot", "/boot");
1684103857bSmartin bootxx = bootxx_name(install);
16950dbef1aSdholland if (bootxx != NULL) {
170c7fa9ad5Sgson error = run_program(RUN_DISPLAY,
171*eece741bSmartin "/usr/sbin/installboot -v /dev/r%sa %s", pm->diskdev, bootxx);
17250dbef1aSdholland free(bootxx);
17350dbef1aSdholland } else
17450dbef1aSdholland error = -1;
17550dbef1aSdholland
17650dbef1aSdholland if (error != 0)
17750dbef1aSdholland process_menu(MENU_ok,
178b77ea2f7Sjoerg __UNCONST("Warning: disk is probably not bootable"));
17950dbef1aSdholland
18076be3ddeSmartin return 0;
18150dbef1aSdholland }
18250dbef1aSdholland
18350dbef1aSdholland int
md_post_extract(struct install_partition_desc * install,bool upgrade)1844204f810Smartin md_post_extract(struct install_partition_desc *install, bool upgrade)
18550dbef1aSdholland {
18650dbef1aSdholland return 0;
18750dbef1aSdholland }
18850dbef1aSdholland
18950dbef1aSdholland void
md_cleanup_install(struct install_partition_desc * install)1904103857bSmartin md_cleanup_install(struct install_partition_desc *install)
19150dbef1aSdholland {
19250dbef1aSdholland #ifndef DEBUG
19350dbef1aSdholland enable_rc_conf();
19450dbef1aSdholland #endif
19550dbef1aSdholland }
19650dbef1aSdholland
19750dbef1aSdholland int
md_pre_update(struct install_partition_desc * install)1984103857bSmartin md_pre_update(struct install_partition_desc *install)
19950dbef1aSdholland {
20050dbef1aSdholland return 1;
20150dbef1aSdholland }
20250dbef1aSdholland
20350dbef1aSdholland /* Upgrade support */
20450dbef1aSdholland int
md_update(struct install_partition_desc * install)2054103857bSmartin md_update(struct install_partition_desc *install)
20650dbef1aSdholland {
2074103857bSmartin md_post_newfs(install);
20850dbef1aSdholland return 1;
20950dbef1aSdholland }
21050dbef1aSdholland
21150dbef1aSdholland int
md_check_mbr(struct disk_partitions * parts,mbr_info_t * mbri,bool quiet)2124103857bSmartin md_check_mbr(struct disk_partitions *parts, mbr_info_t *mbri, bool quiet)
21350dbef1aSdholland {
21450dbef1aSdholland return 2;
21550dbef1aSdholland }
21650dbef1aSdholland
2174103857bSmartin bool
md_parts_use_wholedisk(struct disk_partitions * parts)2184103857bSmartin md_parts_use_wholedisk(struct disk_partitions *parts)
21950dbef1aSdholland {
2204103857bSmartin return parts_use_wholedisk(parts, 0, NULL);
22150dbef1aSdholland }
22250dbef1aSdholland
22350dbef1aSdholland
22450dbef1aSdholland int
md_pre_mount(struct install_partition_desc * install,size_t ndx)2254f30cbf3Smartin md_pre_mount(struct install_partition_desc *install, size_t ndx)
22650dbef1aSdholland {
22750dbef1aSdholland return 0;
22850dbef1aSdholland }
2294103857bSmartin
2304103857bSmartin bool
md_mbr_update_check(struct disk_partitions * parts,mbr_info_t * mbri)2314103857bSmartin md_mbr_update_check(struct disk_partitions *parts, mbr_info_t *mbri)
2324103857bSmartin {
2334103857bSmartin return false; /* no change, no need to write back */
2344103857bSmartin }
2354103857bSmartin
2364103857bSmartin #ifdef HAVE_GPT
2374103857bSmartin 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)2384103857bSmartin md_gpt_post_write(struct disk_partitions *parts, part_id root_id,
2394103857bSmartin bool root_is_new, part_id efi_id, bool efi_is_new)
2404103857bSmartin {
2414103857bSmartin /* no GPT boot support, nothing needs to be done here */
2424103857bSmartin return true;
2434103857bSmartin }
2444103857bSmartin #endif
2454103857bSmartin
246