1*4204f810Smartin /* $NetBSD: md.c,v 1.8 2022/01/29 16:01:18 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 -- ews4800mips machine specific routines */
3650dbef1aSdholland
3750dbef1aSdholland #include <sys/types.h>
3850dbef1aSdholland #include <sys/ioctl.h>
3950dbef1aSdholland #include <sys/param.h>
4050dbef1aSdholland #include <stdio.h>
4150dbef1aSdholland #include <curses.h>
4250dbef1aSdholland #include <unistd.h>
4350dbef1aSdholland #include <fcntl.h>
4450dbef1aSdholland #include <util.h>
4550dbef1aSdholland
4650dbef1aSdholland #include "defs.h"
4750dbef1aSdholland #include "md.h"
4850dbef1aSdholland #include "msg_defs.h"
4950dbef1aSdholland #include "menu_defs.h"
5050dbef1aSdholland
5150dbef1aSdholland static int ews4800mips_boot_offset(void);
5250dbef1aSdholland
5350dbef1aSdholland void
md_init(void)5450dbef1aSdholland md_init(void)
5550dbef1aSdholland {
5650dbef1aSdholland }
5750dbef1aSdholland
5850dbef1aSdholland void
md_init_set_status(int flags)5950dbef1aSdholland md_init_set_status(int flags)
6050dbef1aSdholland {
6150dbef1aSdholland (void)flags;
6250dbef1aSdholland }
6350dbef1aSdholland
644103857bSmartin bool
md_get_info(struct install_partition_desc * install)654103857bSmartin md_get_info(struct install_partition_desc *install)
6650dbef1aSdholland {
6750dbef1aSdholland struct disklabel disklabel;
6850dbef1aSdholland int fd;
6950dbef1aSdholland char dev_name[100];
7050dbef1aSdholland
714b2364d9Smartin snprintf(dev_name, 100, "/dev/r%s%c", pm->diskdev, 'a' + getrawpartition());
7250dbef1aSdholland
7350dbef1aSdholland fd = open(dev_name, O_RDONLY, 0);
7450dbef1aSdholland if (fd < 0) {
7550dbef1aSdholland endwin();
7650dbef1aSdholland fprintf (stderr, "Can't open %s\n", dev_name);
7750dbef1aSdholland exit(1);
7850dbef1aSdholland }
7950dbef1aSdholland if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
8050dbef1aSdholland endwin();
8150dbef1aSdholland fprintf (stderr, "Can't read disklabel on %s.\n", dev_name);
8250dbef1aSdholland close(fd);
8350dbef1aSdholland exit(1);
8450dbef1aSdholland }
8550dbef1aSdholland close(fd);
8650dbef1aSdholland
874b2364d9Smartin pm->dlcyl = disklabel.d_ncylinders;
884b2364d9Smartin pm->dlhead = disklabel.d_ntracks;
894b2364d9Smartin pm->dlsec = disklabel.d_nsectors;
904b2364d9Smartin pm->sectorsize = disklabel.d_secsize;
914b2364d9Smartin pm->dlcylsize = disklabel.d_secpercyl;
9250dbef1aSdholland
9350dbef1aSdholland /*
944b2364d9Smartin * Compute whole disk size. Take max of (pm->dlcyl*pm->dlhead*pm->dlsec)
9550dbef1aSdholland * and secperunit, just in case the disk is already labelled.
9650dbef1aSdholland * (If our new label's RAW_PART size ends up smaller than the
9750dbef1aSdholland * in-core RAW_PART size value, updating the label will fail.)
9850dbef1aSdholland */
994b2364d9Smartin pm->dlsize = pm->dlcyl * pm->dlhead * pm->dlsec;
1004b2364d9Smartin if (disklabel.d_secperunit > pm->dlsize)
1014b2364d9Smartin pm->dlsize = disklabel.d_secperunit;
10250dbef1aSdholland
1034103857bSmartin return true;
10450dbef1aSdholland }
10550dbef1aSdholland
10650dbef1aSdholland /*
10750dbef1aSdholland * md back-end code for menu-driven BSD disklabel editor.
10850dbef1aSdholland */
109957b5cd6Smartin int
md_make_bsd_partitions(struct install_partition_desc * install)1104103857bSmartin md_make_bsd_partitions(struct install_partition_desc *install)
11150dbef1aSdholland {
11250dbef1aSdholland
1134103857bSmartin return make_bsd_partitions(install);
11450dbef1aSdholland }
11550dbef1aSdholland
11650dbef1aSdholland /*
11750dbef1aSdholland * any additional partition validation
11850dbef1aSdholland */
1194103857bSmartin bool
md_check_partitions(struct install_partition_desc * install)1204103857bSmartin md_check_partitions(struct install_partition_desc *install)
12150dbef1aSdholland {
1224103857bSmartin return true;
12350dbef1aSdholland }
12450dbef1aSdholland
12550dbef1aSdholland /*
12650dbef1aSdholland * hook called before writing new disklabel.
12750dbef1aSdholland */
1284103857bSmartin bool
md_pre_disklabel(struct install_partition_desc * install,struct disk_partitions * parts)1294103857bSmartin md_pre_disklabel(struct install_partition_desc *install,
1304103857bSmartin struct disk_partitions *parts)
13150dbef1aSdholland {
1324103857bSmartin part_id part;
1334103857bSmartin struct disk_part_info info;
1344103857bSmartin daddr_t boot_offset = ews4800mips_boot_offset();
13550dbef1aSdholland
136811d5a8bSmsaitoh /* make sure the boot partition is at the right offset */
1374103857bSmartin for (part = 0; part < parts->num_part; part++) {
1384103857bSmartin if (!parts->pscheme->get_part_info(parts, part, &info))
1394103857bSmartin continue;
1404103857bSmartin if (info.flags & (PTI_SEC_CONTAINER|PTI_WHOLE_DISK|
1414103857bSmartin PTI_PSCHEME_INTERNAL|PTI_RAW_PART))
1424103857bSmartin continue;
1434103857bSmartin if (info.fs_type != PART_BOOT_TYPE)
1444103857bSmartin continue;
1454103857bSmartin if (info.start == boot_offset)
1464103857bSmartin continue;
1474103857bSmartin info.start = boot_offset;
1484103857bSmartin parts->pscheme->set_part_info(parts, part, &info, NULL);
1494103857bSmartin }
1504103857bSmartin
1514103857bSmartin return true;
15250dbef1aSdholland }
15350dbef1aSdholland
15450dbef1aSdholland /*
15550dbef1aSdholland * hook called after writing disklabel to new target disk.
15650dbef1aSdholland */
1574103857bSmartin bool
md_post_disklabel(struct install_partition_desc * install,struct disk_partitions * parts)1584103857bSmartin md_post_disklabel(struct install_partition_desc *install,
1594103857bSmartin struct disk_partitions *parts)
16050dbef1aSdholland {
1614103857bSmartin return true;
16250dbef1aSdholland }
16350dbef1aSdholland
16450dbef1aSdholland /*
16550dbef1aSdholland * hook called after upgrade() or install() has finished setting
16650dbef1aSdholland * up the target disk but immediately before the user is given the
16750dbef1aSdholland * ``disks are now set up'' message.
16850dbef1aSdholland *
16950dbef1aSdholland * On the ews4800mips, we use this opportunity to install the boot blocks.
17050dbef1aSdholland */
17150dbef1aSdholland int
md_post_newfs(struct install_partition_desc * install)1724103857bSmartin md_post_newfs(struct install_partition_desc *install)
17350dbef1aSdholland {
17450dbef1aSdholland int flags;
17550dbef1aSdholland
17650dbef1aSdholland flags = RUN_DISPLAY | RUN_FATAL;
17750dbef1aSdholland cp_to_target("/usr/mdec/boot", "/stand/boot");
17850dbef1aSdholland run_program(flags, "/usr/sbin/installboot /dev/r%s%c %s",
1794b2364d9Smartin pm->diskdev, 'a' + getrawpartition(),
18050dbef1aSdholland "/usr/mdec/bootxx_bfs");
18150dbef1aSdholland
18250dbef1aSdholland return 0;
18350dbef1aSdholland }
18450dbef1aSdholland
18550dbef1aSdholland int
md_post_extract(struct install_partition_desc * install,bool upgrade)186*4204f810Smartin md_post_extract(struct install_partition_desc *install, bool upgrade)
18750dbef1aSdholland {
18850dbef1aSdholland return 0;
18950dbef1aSdholland }
19050dbef1aSdholland
19150dbef1aSdholland void
md_cleanup_install(struct install_partition_desc * install)1924103857bSmartin md_cleanup_install(struct install_partition_desc *install)
19350dbef1aSdholland {
19450dbef1aSdholland #ifndef DEBUG
19550dbef1aSdholland enable_rc_conf();
19650dbef1aSdholland #endif
19750dbef1aSdholland }
19850dbef1aSdholland
19950dbef1aSdholland int
md_pre_update(struct install_partition_desc * install)2004103857bSmartin md_pre_update(struct install_partition_desc *install)
20150dbef1aSdholland {
20250dbef1aSdholland return 1;
20350dbef1aSdholland }
20450dbef1aSdholland
20550dbef1aSdholland /* Upgrade support */
20650dbef1aSdholland int
md_update(struct install_partition_desc * install)2074103857bSmartin md_update(struct install_partition_desc *install)
20850dbef1aSdholland {
2094103857bSmartin md_post_newfs(install);
21050dbef1aSdholland return 1;
21150dbef1aSdholland }
21250dbef1aSdholland
21350dbef1aSdholland static int
ews4800mips_boot_offset(void)21450dbef1aSdholland ews4800mips_boot_offset(void)
21550dbef1aSdholland {
2164b2364d9Smartin return pm->dlcylsize;
21750dbef1aSdholland }
21850dbef1aSdholland
21950dbef1aSdholland /*
22050dbef1aSdholland * used in bsddisklabel.c as BOOT_SIZE
22150dbef1aSdholland */
22250dbef1aSdholland int
ews4800mips_boot_size(void)22350dbef1aSdholland ews4800mips_boot_size(void)
22450dbef1aSdholland {
22550dbef1aSdholland int i;
22650dbef1aSdholland
22750dbef1aSdholland /*
2284b2364d9Smartin * pm->dlcylsize : PDINFO block
2294b2364d9Smartin * pm->dlcylsize : 100 block
23050dbef1aSdholland */
2314b2364d9Smartin i = pm->dlcylsize + 100;
23250dbef1aSdholland if (i >= 1024) /* XXX bsddisklabel.c hack. convert to byte count. */
2334b2364d9Smartin i = pm->dlcylsize * pm->sectorsize * 2;
23450dbef1aSdholland
23550dbef1aSdholland return i;
23650dbef1aSdholland }
23750dbef1aSdholland
23850dbef1aSdholland /*
23950dbef1aSdholland * used in bsddisklabel.c as SYSVBFS_SIZE
24050dbef1aSdholland */
24150dbef1aSdholland int
ews4800mips_sysvbfs_size(void)24250dbef1aSdholland ews4800mips_sysvbfs_size(void)
24350dbef1aSdholland {
24450dbef1aSdholland return (8 * 1024 * 1024) / 512; /* 8MB */
24550dbef1aSdholland }
24650dbef1aSdholland
24750dbef1aSdholland int
md_pre_mount(struct install_partition_desc * install,size_t ndx)2484f30cbf3Smartin md_pre_mount(struct install_partition_desc *install, size_t ndx)
24950dbef1aSdholland {
25050dbef1aSdholland return 0;
25150dbef1aSdholland }
2524103857bSmartin
2534103857bSmartin bool
md_parts_use_wholedisk(struct disk_partitions * parts)2544103857bSmartin md_parts_use_wholedisk(struct disk_partitions *parts)
2554103857bSmartin {
2564103857bSmartin struct disk_part_info boot_part = {
2574103857bSmartin .size = PART_BOOT / 512,
2584103857bSmartin .fs_type = PART_BOOT_TYPE,
2594103857bSmartin };
2604103857bSmartin
2614103857bSmartin boot_part.nat_type = parts->pscheme->get_fs_part_type(
262015df285Smartin PT_root, boot_part.fs_type, boot_part.fs_sub_type);
2634103857bSmartin
2644103857bSmartin return parts_use_wholedisk(parts, 1, &boot_part);
2654103857bSmartin }
2664103857bSmartin
2674103857bSmartin #ifdef HAVE_GPT
2684103857bSmartin 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)2694103857bSmartin md_gpt_post_write(struct disk_partitions *parts, part_id root_id,
2704103857bSmartin bool root_is_new, part_id efi_id, bool efi_is_new)
2714103857bSmartin {
2724103857bSmartin /* no GPT boot support, nothing needs to be done here */
2734103857bSmartin return true;
2744103857bSmartin }
2754103857bSmartin #endif
2764103857bSmartin
277