1*4204f810Smartin /* $NetBSD: md.c,v 1.14 2022/01/29 16:01:20 martin 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.
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 -- prep machine specific routines */
3650dbef1aSdholland
3750dbef1aSdholland #include <sys/param.h>
3850dbef1aSdholland #include <sys/sysctl.h>
3950dbef1aSdholland #include <stdio.h>
4050dbef1aSdholland #include <util.h>
4150dbef1aSdholland #include <machine/cpu.h>
4250dbef1aSdholland
4350dbef1aSdholland #include "defs.h"
4450dbef1aSdholland #include "md.h"
4550dbef1aSdholland #include "msg_defs.h"
4650dbef1aSdholland #include "menu_defs.h"
4750dbef1aSdholland #include "endian.h"
4850dbef1aSdholland
494103857bSmartin int prep_nobootfix = 0, prep_rawdevfix = 0;
504103857bSmartin size_t prep_bootpart = ~0U;
5150dbef1aSdholland
5250dbef1aSdholland void
md_init(void)5350dbef1aSdholland md_init(void)
5450dbef1aSdholland {
5550dbef1aSdholland }
5650dbef1aSdholland
5750dbef1aSdholland void
md_init_set_status(int flags)5850dbef1aSdholland md_init_set_status(int flags)
5950dbef1aSdholland {
6050dbef1aSdholland (void)flags;
6150dbef1aSdholland }
6250dbef1aSdholland
634103857bSmartin bool
md_get_info(struct install_partition_desc * install)644103857bSmartin md_get_info(struct install_partition_desc *install)
6550dbef1aSdholland {
66957b5cd6Smartin int res;
67e11b8d25Smartin
68e11b8d25Smartin if (pm->no_mbr || pm->no_part)
69e11b8d25Smartin return true;
70e11b8d25Smartin
71957b5cd6Smartin again:
72e11b8d25Smartin if (pm->parts == NULL) {
73e11b8d25Smartin
74e11b8d25Smartin const struct disk_partitioning_scheme *ps =
75e11b8d25Smartin select_part_scheme(pm, NULL, true, NULL);
76e11b8d25Smartin
77e11b8d25Smartin if (!ps)
786fec6798Smartin return false;
79e11b8d25Smartin
80e11b8d25Smartin struct disk_partitions *parts =
81e11b8d25Smartin (*ps->create_new_for_disk)(pm->diskdev,
8286906049Smartin 0, pm->dlsize, true, NULL);
83e11b8d25Smartin if (!parts)
84e11b8d25Smartin return false;
85e11b8d25Smartin
86e11b8d25Smartin pm->parts = parts;
87e11b8d25Smartin if (ps->size_limit > 0 && pm->dlsize > ps->size_limit)
88e11b8d25Smartin pm->dlsize = ps->size_limit;
89e11b8d25Smartin }
90e11b8d25Smartin
91957b5cd6Smartin res = set_bios_geom_with_mbr_guess(pm->parts);
92957b5cd6Smartin if (res == 0)
93957b5cd6Smartin return false;
94957b5cd6Smartin else if (res == 1)
95957b5cd6Smartin return true;
96957b5cd6Smartin
97957b5cd6Smartin pm->parts->pscheme->destroy_part_scheme(pm->parts);
98957b5cd6Smartin pm->parts = NULL;
99957b5cd6Smartin goto again;
10050dbef1aSdholland }
10150dbef1aSdholland
10250dbef1aSdholland /*
10350dbef1aSdholland * md back-end code for menu-driven BSD disklabel editor.
10450dbef1aSdholland */
105957b5cd6Smartin int
md_make_bsd_partitions(struct install_partition_desc * install)1064103857bSmartin md_make_bsd_partitions(struct install_partition_desc *install)
10750dbef1aSdholland {
1084103857bSmartin return make_bsd_partitions(install);
10950dbef1aSdholland }
11050dbef1aSdholland
11150dbef1aSdholland /*
11250dbef1aSdholland * any additional partition validation
11350dbef1aSdholland */
1144103857bSmartin bool
md_check_partitions(struct install_partition_desc * install)1154103857bSmartin md_check_partitions(struct install_partition_desc *install)
11650dbef1aSdholland {
1174103857bSmartin size_t part;
11850dbef1aSdholland
11950dbef1aSdholland /* we need to find a boot partition, otherwise we can't write our
12050dbef1aSdholland * "bootblock". We make the assumption that the user hasn't done
12150dbef1aSdholland * something stupid, like move it away from the MBR partition.
12250dbef1aSdholland */
1234103857bSmartin for (part = 0; part < install->num; part++)
1244103857bSmartin if (install->infos[part].fs_type == FS_BOOT) {
12550dbef1aSdholland prep_bootpart = part;
1264103857bSmartin return true;
12750dbef1aSdholland }
12850dbef1aSdholland
12950dbef1aSdholland msg_display(MSG_prepnobootpart);
13050dbef1aSdholland process_menu(MENU_ok, NULL);
1314103857bSmartin return false;
13250dbef1aSdholland }
13350dbef1aSdholland
13450dbef1aSdholland /*
13550dbef1aSdholland * hook called before writing new disklabel.
13650dbef1aSdholland */
1374103857bSmartin bool
md_pre_disklabel(struct install_partition_desc * install,struct disk_partitions * parts)1384103857bSmartin md_pre_disklabel(struct install_partition_desc *install,
1394103857bSmartin struct disk_partitions *parts)
14050dbef1aSdholland {
14150dbef1aSdholland
1424103857bSmartin if (parts->parent == NULL)
1434103857bSmartin return true; /* no outer partitions */
1444103857bSmartin
1454103857bSmartin parts = parts->parent;
1464103857bSmartin
1474103857bSmartin msg_display_subst(MSG_dofdisk, 3, parts->disk,
1484103857bSmartin msg_string(parts->pscheme->name),
1494103857bSmartin msg_string(parts->pscheme->short_name));
1504103857bSmartin
1514103857bSmartin /* write edited "MBR" onto disk. */
1524103857bSmartin if (!parts->pscheme->write_to_disk(parts)) {
15350dbef1aSdholland msg_display(MSG_wmbrfail);
15450dbef1aSdholland process_menu(MENU_ok, NULL);
1554103857bSmartin return false;
15650dbef1aSdholland }
1574103857bSmartin return true;
15850dbef1aSdholland }
15950dbef1aSdholland
16050dbef1aSdholland /*
16150dbef1aSdholland * hook called after writing disklabel to new target disk.
16250dbef1aSdholland */
1634103857bSmartin bool
md_post_disklabel(struct install_partition_desc * install,struct disk_partitions * parts)1644103857bSmartin md_post_disklabel(struct install_partition_desc *install,
1654103857bSmartin struct disk_partitions *parts)
16650dbef1aSdholland {
1674103857bSmartin return true;
16850dbef1aSdholland }
16950dbef1aSdholland
17050dbef1aSdholland /*
17150dbef1aSdholland * hook called after upgrade() or install() has finished setting
17250dbef1aSdholland * up the target disk but immediately before the user is given the
17350dbef1aSdholland * ``disks are now set up'' message.
17450dbef1aSdholland */
17550dbef1aSdholland int
md_post_newfs(struct install_partition_desc * install)1764103857bSmartin md_post_newfs(struct install_partition_desc *install)
17750dbef1aSdholland {
17850dbef1aSdholland return 0;
17950dbef1aSdholland }
18050dbef1aSdholland
18150dbef1aSdholland int
md_post_extract(struct install_partition_desc * install,bool upgrade)182*4204f810Smartin md_post_extract(struct install_partition_desc *install, bool upgrade)
18350dbef1aSdholland {
18450dbef1aSdholland char rawdev[100], bootpart[100], bootloader[100];
185f92de306Smartin int contype;
18650dbef1aSdholland
18750dbef1aSdholland /* if we can't make it bootable, just punt */
18850dbef1aSdholland if (prep_nobootfix)
18950dbef1aSdholland return 0;
19050dbef1aSdholland
191f92de306Smartin process_menu(MENU_prepconsole, &contype);
192f92de306Smartin if (contype == 1)
19350dbef1aSdholland snprintf(bootloader, 100, "/usr/mdec/boot_com0");
19450dbef1aSdholland else
19550dbef1aSdholland snprintf(bootloader, 100, "/usr/mdec/boot");
19650dbef1aSdholland
19724ecf24eSchristos snprintf(rawdev, 100, "/dev/r%s%c", pm->diskdev,
19824ecf24eSchristos (char)('a' + getrawpartition()));
19924ecf24eSchristos snprintf(bootpart, 100, "/dev/r%s%c", pm->diskdev,
20024ecf24eSchristos (char)('a' + prep_bootpart));
20150dbef1aSdholland if (prep_rawdevfix)
20250dbef1aSdholland run_program(RUN_DISPLAY|RUN_CHROOT,
20350dbef1aSdholland "/usr/mdec/mkbootimage -b %s -k /netbsd "
20450dbef1aSdholland "-r %s /.bootimage", bootloader, rawdev);
20550dbef1aSdholland else
20650dbef1aSdholland run_program(RUN_DISPLAY|RUN_CHROOT,
20750dbef1aSdholland "/usr/mdec/mkbootimage -s -b %s -k /netbsd /.bootimage",
20850dbef1aSdholland bootloader);
20950dbef1aSdholland run_program(RUN_DISPLAY|RUN_CHROOT, "/bin/dd if=/.bootimage of=%s "
21050dbef1aSdholland "bs=512 conv=sync", bootpart);
21150dbef1aSdholland
21250dbef1aSdholland return 0;
21350dbef1aSdholland }
21450dbef1aSdholland
21550dbef1aSdholland void
md_cleanup_install(struct install_partition_desc * install)2164103857bSmartin md_cleanup_install(struct install_partition_desc *install)
21750dbef1aSdholland {
21850dbef1aSdholland #ifndef DEBUG
21950dbef1aSdholland enable_rc_conf();
22050dbef1aSdholland #endif
22150dbef1aSdholland run_program(0, "rm -f %s", target_expand("/.bootimage"));
22250dbef1aSdholland }
22350dbef1aSdholland
22450dbef1aSdholland int
md_pre_update(struct install_partition_desc * install)2254103857bSmartin md_pre_update(struct install_partition_desc *install)
22650dbef1aSdholland {
2274103857bSmartin size_t i;
22850dbef1aSdholland
22950dbef1aSdholland /* do a sanity check of the partition table */
2304103857bSmartin for (i = 0; i < install->num; i++) {
2314103857bSmartin if (install->infos[i].fs_type != PART_BOOT_TYPE)
23250dbef1aSdholland continue;
23324ecf24eSchristos if (install->infos[i].size < (int)(MIN_PREP_BOOT/512)) {
23450dbef1aSdholland msg_display(MSG_preptoosmall);
23524ecf24eSchristos msg_fmt_display_add(MSG_prepnobootpart, "%d", 0);
236e21052b4Smartin if (!ask_yesno(NULL))
23750dbef1aSdholland return 0;
23850dbef1aSdholland prep_nobootfix = 1;
23950dbef1aSdholland }
2404103857bSmartin if (install->infos[i].cur_start == 0)
24150dbef1aSdholland prep_rawdevfix = 1;
24250dbef1aSdholland }
2434103857bSmartin if (!md_check_partitions(install))
24450dbef1aSdholland prep_nobootfix = 1;
24550dbef1aSdholland return 1;
24650dbef1aSdholland }
24750dbef1aSdholland
24850dbef1aSdholland /* Upgrade support */
24950dbef1aSdholland int
md_update(struct install_partition_desc * install)2504103857bSmartin md_update(struct install_partition_desc *install)
25150dbef1aSdholland {
2524103857bSmartin md_post_newfs(install);
25350dbef1aSdholland return 1;
25450dbef1aSdholland }
25550dbef1aSdholland
25650dbef1aSdholland int
md_check_mbr(struct disk_partitions * parts,mbr_info_t * mbri,bool quiet)2574103857bSmartin md_check_mbr(struct disk_partitions *parts, mbr_info_t *mbri, bool quiet)
25850dbef1aSdholland {
25950dbef1aSdholland mbr_info_t *ext;
26050dbef1aSdholland struct mbr_partition *part;
26150dbef1aSdholland int i;
26250dbef1aSdholland
26350dbef1aSdholland for (ext = mbri; ext; ext = ext->extended) {
26450dbef1aSdholland part = ext->mbr.mbr_parts;
26550dbef1aSdholland for (i = 0; i < MBR_PART_COUNT; part++, i++) {
26650dbef1aSdholland if (part->mbrp_type != MBR_PTYPE_PREP)
26750dbef1aSdholland continue;
2684b2364d9Smartin pm->bootstart = part->mbrp_start;
2694b2364d9Smartin pm->bootsize = part->mbrp_size;
27050dbef1aSdholland break;
27150dbef1aSdholland }
27250dbef1aSdholland }
27324ecf24eSchristos if (pm->bootsize < (int)(MIN_PREP_BOOT/512)) {
27450dbef1aSdholland msg_display(MSG_preptoosmall);
2754103857bSmartin return ask_reedit(parts);
27650dbef1aSdholland }
2774b2364d9Smartin if (pm->bootstart == 0 || pm->bootsize == 0) {
2784103857bSmartin if (quiet)
27950dbef1aSdholland return 0;
2804103857bSmartin msg_display(MSG_nopreppart);
2814103857bSmartin return ask_reedit(parts);
28250dbef1aSdholland }
28350dbef1aSdholland return 2;
28450dbef1aSdholland }
28550dbef1aSdholland
2864103857bSmartin bool
md_parts_use_wholedisk(struct disk_partitions * parts)2874103857bSmartin md_parts_use_wholedisk(struct disk_partitions *parts)
28850dbef1aSdholland {
2894103857bSmartin struct disk_part_info boot_part = {
2904103857bSmartin .size = PART_BOOT / 512,
2914103857bSmartin .fs_type = PART_BOOT_TYPE,
2924103857bSmartin };
29350dbef1aSdholland
2944103857bSmartin boot_part.nat_type = parts->pscheme->get_fs_part_type(
295015df285Smartin PT_root, boot_part.fs_type, boot_part.fs_sub_type);
2964103857bSmartin
2974103857bSmartin return parts_use_wholedisk(parts, 1, &boot_part);
29850dbef1aSdholland }
29950dbef1aSdholland
30050dbef1aSdholland int
md_pre_mount(struct install_partition_desc * install,size_t ndx)3014f30cbf3Smartin md_pre_mount(struct install_partition_desc *install, size_t ndx)
30250dbef1aSdholland {
30350dbef1aSdholland return 0;
30450dbef1aSdholland }
3054103857bSmartin
3064103857bSmartin bool
md_mbr_update_check(struct disk_partitions * parts,mbr_info_t * mbri)3074103857bSmartin md_mbr_update_check(struct disk_partitions *parts, mbr_info_t *mbri)
3084103857bSmartin {
3094103857bSmartin return false; /* no change, no need to write back */
3104103857bSmartin }
3114103857bSmartin
3124103857bSmartin #ifdef HAVE_GPT
3134103857bSmartin 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)3144103857bSmartin md_gpt_post_write(struct disk_partitions *parts, part_id root_id,
3154103857bSmartin bool root_is_new, part_id efi_id, bool efi_is_new)
3164103857bSmartin {
3174103857bSmartin /* no GPT boot support, nothing needs to be done here */
3184103857bSmartin return true;
3194103857bSmartin }
3204103857bSmartin #endif
3214103857bSmartin
322