1*380be89aStsutsui /* $NetBSD: md.c,v 1.16 2022/06/11 16:25:23 tsutsui Exp $ */
226165e63Sdholland
326165e63Sdholland /*
426165e63Sdholland * Copyright 1997 Piermont Information Systems Inc.
526165e63Sdholland * All rights reserved.
626165e63Sdholland *
726165e63Sdholland * Based on code written by Philip A. Nelson for Piermont Information
826165e63Sdholland * Systems Inc.
926165e63Sdholland *
1026165e63Sdholland * Redistribution and use in source and binary forms, with or without
1126165e63Sdholland * modification, are permitted provided that the following conditions
1226165e63Sdholland * are met:
1326165e63Sdholland * 1. Redistributions of source code must retain the above copyright
1426165e63Sdholland * notice, this list of conditions and the following disclaimer.
1526165e63Sdholland * 2. Redistributions in binary form must reproduce the above copyright
1626165e63Sdholland * notice, this list of conditions and the following disclaimer in the
1726165e63Sdholland * documentation and/or other materials provided with the distribution.
1826165e63Sdholland * 3. The name of Piermont Information Systems Inc. may not be used to endorse
1926165e63Sdholland * or promote products derived from this software without specific prior
2026165e63Sdholland * written permission.
2126165e63Sdholland *
2226165e63Sdholland * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
2326165e63Sdholland * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2426165e63Sdholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2526165e63Sdholland * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
2626165e63Sdholland * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2726165e63Sdholland * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2826165e63Sdholland * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2926165e63Sdholland * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3026165e63Sdholland * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3126165e63Sdholland * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3226165e63Sdholland * THE POSSIBILITY OF SUCH DAMAGE.
3326165e63Sdholland */
3426165e63Sdholland
3526165e63Sdholland /* md.c -- cobalt machine specific routines */
3626165e63Sdholland
3726165e63Sdholland #include <sys/param.h>
3826165e63Sdholland #include <sys/sysctl.h>
3926165e63Sdholland #include <stdio.h>
4026165e63Sdholland #include <util.h>
4126165e63Sdholland #include <machine/cpu.h>
4226165e63Sdholland
4326165e63Sdholland #include "defs.h"
4426165e63Sdholland #include "md.h"
4526165e63Sdholland #include "msg_defs.h"
4626165e63Sdholland #include "menu_defs.h"
4726165e63Sdholland
4826165e63Sdholland /*
4926165e63Sdholland * Firmware reognizes only Linux Ext2 REV 0, so we have to have
5026165e63Sdholland * a Linux Ext2 fs to store our native bootloader.
5126165e63Sdholland */
5226165e63Sdholland static int nobootfs = 0;
5326165e63Sdholland
5426165e63Sdholland void
md_init(void)5526165e63Sdholland md_init(void)
5626165e63Sdholland {
5726165e63Sdholland }
5826165e63Sdholland
5926165e63Sdholland void
md_init_set_status(int flags)6026165e63Sdholland md_init_set_status(int flags)
6126165e63Sdholland {
6226165e63Sdholland (void)flags;
6326165e63Sdholland }
6426165e63Sdholland
65dcc57e24Smartin bool
md_get_info(struct install_partition_desc * install)66dcc57e24Smartin md_get_info(struct install_partition_desc *install)
6726165e63Sdholland {
6880bf2417Smartin int res;
69b07bdc27Smartin
70b07bdc27Smartin if (pm->no_mbr || pm->no_part)
71b07bdc27Smartin return true;
72b07bdc27Smartin
7380bf2417Smartin again:
74b07bdc27Smartin if (pm->parts == NULL) {
75b07bdc27Smartin
76b07bdc27Smartin const struct disk_partitioning_scheme *ps =
77b07bdc27Smartin select_part_scheme(pm, NULL, true, NULL);
78b07bdc27Smartin
79b07bdc27Smartin if (!ps)
80c876c9c7Smartin return false;
81b07bdc27Smartin
82b07bdc27Smartin struct disk_partitions *parts =
83b07bdc27Smartin (*ps->create_new_for_disk)(pm->diskdev,
840ea03d33Smartin 0, pm->dlsize, true, NULL);
85b07bdc27Smartin if (!parts)
86b07bdc27Smartin return false;
87b07bdc27Smartin
88b07bdc27Smartin pm->parts = parts;
89b07bdc27Smartin if (ps->size_limit > 0 && pm->dlsize > ps->size_limit)
90b07bdc27Smartin pm->dlsize = ps->size_limit;
91b07bdc27Smartin }
92b07bdc27Smartin
9380bf2417Smartin res = set_bios_geom_with_mbr_guess(pm->parts);
9480bf2417Smartin if (res == 0)
9580bf2417Smartin return false;
9680bf2417Smartin else if (res == 1)
9780bf2417Smartin return true;
9880bf2417Smartin
9980bf2417Smartin pm->parts->pscheme->destroy_part_scheme(pm->parts);
10080bf2417Smartin pm->parts = NULL;
10180bf2417Smartin goto again;
10226165e63Sdholland }
10326165e63Sdholland
10426165e63Sdholland /*
10526165e63Sdholland * md back-end code for menu-driven BSD disklabel editor.
10626165e63Sdholland */
10780bf2417Smartin int
md_make_bsd_partitions(struct install_partition_desc * install)108dcc57e24Smartin md_make_bsd_partitions(struct install_partition_desc *install)
10926165e63Sdholland {
110dcc57e24Smartin return make_bsd_partitions(install);
11126165e63Sdholland }
11226165e63Sdholland
11326165e63Sdholland /*
11426165e63Sdholland * any additional partition validation
11526165e63Sdholland */
116dcc57e24Smartin bool
md_check_partitions(struct install_partition_desc * install)117dcc57e24Smartin md_check_partitions(struct install_partition_desc *install)
11826165e63Sdholland {
119dcc57e24Smartin size_t part;
12026165e63Sdholland
12126165e63Sdholland /* we need to find a boot partition, otherwise we can't write our
12226165e63Sdholland * bootloader. We make the assumption that the user hasn't done
12326165e63Sdholland * something stupid, like move it away from the MBR partition.
12426165e63Sdholland */
125dcc57e24Smartin for (part = 0; part < install->num; part++) {
126dcc57e24Smartin if (install->infos[part].fs_type == PART_BOOT_TYPE)
127dcc57e24Smartin return true;
12826165e63Sdholland }
12926165e63Sdholland
13026165e63Sdholland msg_display(MSG_nobootpartdisklabel);
13126165e63Sdholland process_menu(MENU_ok, NULL);
132dcc57e24Smartin return false;
13326165e63Sdholland }
13426165e63Sdholland
13526165e63Sdholland /*
13626165e63Sdholland * hook called before writing new disklabel.
13726165e63Sdholland */
138dcc57e24Smartin bool
md_pre_disklabel(struct install_partition_desc * install,struct disk_partitions * parts)139dcc57e24Smartin md_pre_disklabel(struct install_partition_desc *install,
140dcc57e24Smartin struct disk_partitions *parts)
14126165e63Sdholland {
14226165e63Sdholland
143dcc57e24Smartin if (parts->parent == NULL)
144dcc57e24Smartin return true; /* no outer partitions */
145dcc57e24Smartin
146dcc57e24Smartin parts = parts->parent;
147dcc57e24Smartin
148dcc57e24Smartin msg_display_subst(MSG_dofdisk, 3, parts->disk,
149dcc57e24Smartin msg_string(parts->pscheme->name),
150dcc57e24Smartin msg_string(parts->pscheme->short_name));
151dcc57e24Smartin
152dcc57e24Smartin /* write edited "MBR" onto disk. */
153dcc57e24Smartin if (!parts->pscheme->write_to_disk(parts)) {
15426165e63Sdholland msg_display(MSG_wmbrfail);
15526165e63Sdholland process_menu(MENU_ok, NULL);
156dcc57e24Smartin return false;
15726165e63Sdholland }
158dcc57e24Smartin return true;
15926165e63Sdholland }
16026165e63Sdholland
16126165e63Sdholland /*
16226165e63Sdholland * hook called after writing disklabel to new target disk.
16326165e63Sdholland */
164dcc57e24Smartin bool
md_post_disklabel(struct install_partition_desc * install,struct disk_partitions * parts)165dcc57e24Smartin md_post_disklabel(struct install_partition_desc *install,
166dcc57e24Smartin struct disk_partitions *parts)
16726165e63Sdholland {
168dcc57e24Smartin return true;
16926165e63Sdholland }
17026165e63Sdholland
17126165e63Sdholland /*
17226165e63Sdholland * hook called after upgrade() or install() has finished setting
17326165e63Sdholland * up the target disk but immediately before the user is given the
17426165e63Sdholland * ``disks are now set up'' message.
17526165e63Sdholland */
17626165e63Sdholland int
md_post_newfs(struct install_partition_desc * install)177dcc57e24Smartin md_post_newfs(struct install_partition_desc *install)
17826165e63Sdholland {
17926165e63Sdholland static const char *kernels[] = {
18026165e63Sdholland "vmlinux-nfsroot.gz",
18126165e63Sdholland "vmlinux.gz",
18226165e63Sdholland "vmlinux_RAQ.gz",
18326165e63Sdholland "vmlinux_raq-2800.gz"
18426165e63Sdholland };
18526165e63Sdholland static const char *bootfile = "boot.gz";
18626165e63Sdholland char bootdir[64];
18726165e63Sdholland unsigned int i;
18826165e63Sdholland
18926165e63Sdholland if (!nobootfs) {
190f9805030Schristos msg_fmt_display(msg_string(MSG_copybootloader), "%s",
191f9805030Schristos pm->diskdev);
19226165e63Sdholland
19326165e63Sdholland snprintf(bootdir, sizeof(bootdir), "%s/boot",
194dcc57e24Smartin target_expand(PART_BOOT_MOUNT));
19526165e63Sdholland run_program(0, "/bin/mkdir -p %s", bootdir);
19626165e63Sdholland run_program(0, "/bin/cp /usr/mdec/boot %s", bootdir);
19726165e63Sdholland run_program(0, "/bin/rm -f %s/%s", bootdir, bootfile);
19826165e63Sdholland run_program(0, "/usr/bin/gzip -9 %s/boot", bootdir);
19926165e63Sdholland for (i = 0; i < __arraycount(kernels); i++)
20026165e63Sdholland run_program(0, "/bin/ln -fs %s %s/%s",
20126165e63Sdholland bootfile, bootdir, kernels[i]);
20226165e63Sdholland }
20326165e63Sdholland
20426165e63Sdholland return 0;
20526165e63Sdholland }
20626165e63Sdholland
20726165e63Sdholland int
md_post_extract(struct install_partition_desc * install,bool upgrade)208b8b586ecSmartin md_post_extract(struct install_partition_desc *install, bool upgrade)
20926165e63Sdholland {
21026165e63Sdholland return 0;
21126165e63Sdholland }
21226165e63Sdholland
21326165e63Sdholland void
md_cleanup_install(struct install_partition_desc * install)214dcc57e24Smartin md_cleanup_install(struct install_partition_desc *install)
21526165e63Sdholland {
21626165e63Sdholland #ifndef DEBUG
21726165e63Sdholland enable_rc_conf();
21826165e63Sdholland #endif
21926165e63Sdholland }
22026165e63Sdholland
22126165e63Sdholland int
md_pre_update(struct install_partition_desc * install)222dcc57e24Smartin md_pre_update(struct install_partition_desc *install)
22326165e63Sdholland {
224dcc57e24Smartin size_t i;
22526165e63Sdholland
226dcc57e24Smartin /*
227dcc57e24Smartin * Verify the msdos partition exists and is big enough.
228dcc57e24Smartin */
229dcc57e24Smartin for (i = 0; i < install->num; i++) {
230dcc57e24Smartin if (install->infos[i].fs_type != PART_BOOT_TYPE)
23126165e63Sdholland continue;
232dcc57e24Smartin if (install->infos[i].size/512 >= PART_BOOT_MIN)
233dcc57e24Smartin break;
23426165e63Sdholland msg_display(MSG_boottoosmall);
235f9805030Schristos msg_fmt_display_add(MSG_nobootpart, "%d", 0);
23686ffd738Smartin if (!ask_yesno(NULL))
237dcc57e24Smartin return false;
23826165e63Sdholland nobootfs = 1;
239dcc57e24Smartin break;
24026165e63Sdholland }
241dcc57e24Smartin
242dcc57e24Smartin if (md_check_partitions(install) == 0)
24326165e63Sdholland nobootfs = 1;
244dcc57e24Smartin
24526165e63Sdholland return 1;
24626165e63Sdholland }
24726165e63Sdholland
24826165e63Sdholland /* Upgrade support */
24926165e63Sdholland int
md_update(struct install_partition_desc * install)250dcc57e24Smartin md_update(struct install_partition_desc *install)
25126165e63Sdholland {
252dcc57e24Smartin md_post_newfs(install);
25326165e63Sdholland return 1;
25426165e63Sdholland }
25526165e63Sdholland
25626165e63Sdholland int
md_check_mbr(struct disk_partitions * parts,mbr_info_t * mbri,bool quiet)257dcc57e24Smartin md_check_mbr(struct disk_partitions *parts, mbr_info_t *mbri, bool quiet)
25826165e63Sdholland {
25926165e63Sdholland mbr_info_t *ext;
26026165e63Sdholland struct mbr_partition *part;
26126165e63Sdholland int i;
26226165e63Sdholland
26326165e63Sdholland for (ext = mbri; ext; ext = ext->extended) {
26426165e63Sdholland part = ext->mbr.mbr_parts;
26526165e63Sdholland for (i = 0; i < MBR_PART_COUNT; part++, i++) {
26626165e63Sdholland if (part->mbrp_type == MBR_PTYPE_LNXEXT2) {
267da5a563bSmartin pm->bootstart = part->mbrp_start;
268da5a563bSmartin pm->bootsize = part->mbrp_size;
26926165e63Sdholland break;
27026165e63Sdholland }
27126165e63Sdholland }
27226165e63Sdholland }
273dcc57e24Smartin if (pm->bootsize < (PART_BOOT_MIN / 512)) {
274dcc57e24Smartin if (quiet)
27526165e63Sdholland return 0;
276dcc57e24Smartin msg_display(MSG_boottoosmall);
277dcc57e24Smartin return ask_reedit(parts);
27826165e63Sdholland }
279da5a563bSmartin if (pm->bootstart == 0 || pm->bootsize == 0) {
280dcc57e24Smartin if (quiet)
28126165e63Sdholland return 0;
282dcc57e24Smartin msg_display(MSG_nobootpart);
283dcc57e24Smartin return ask_reedit(parts);
28426165e63Sdholland }
28526165e63Sdholland return 2;
28626165e63Sdholland }
28726165e63Sdholland
288dcc57e24Smartin bool
md_parts_use_wholedisk(struct disk_partitions * parts)289dcc57e24Smartin md_parts_use_wholedisk(struct disk_partitions *parts)
29026165e63Sdholland {
291dcc57e24Smartin struct disk_part_info boot_part = {
292dcc57e24Smartin .size = PART_BOOT / 512,
293dcc57e24Smartin .fs_type = PART_BOOT_TYPE,
294436d8c16Stsutsui .fs_sub_type = MBR_PTYPE_LNXEXT2,
295dcc57e24Smartin .last_mounted = PART_BOOT_MOUNT,
296dcc57e24Smartin };
29726165e63Sdholland
298dcc57e24Smartin boot_part.nat_type = parts->pscheme->get_fs_part_type(
299*380be89aStsutsui PT_EXT2, boot_part.fs_type, boot_part.fs_sub_type);
30026165e63Sdholland
301dcc57e24Smartin return parts_use_wholedisk(parts, 1, &boot_part);
30226165e63Sdholland }
30326165e63Sdholland
30426165e63Sdholland int
md_pre_mount(struct install_partition_desc * install,size_t ndx)305d5a9dfe9Smartin md_pre_mount(struct install_partition_desc *install, size_t ndx)
30626165e63Sdholland {
30726165e63Sdholland return 0;
30826165e63Sdholland }
309dcc57e24Smartin
310dcc57e24Smartin bool
md_mbr_update_check(struct disk_partitions * parts,mbr_info_t * mbri)311dcc57e24Smartin md_mbr_update_check(struct disk_partitions *parts, mbr_info_t *mbri)
312dcc57e24Smartin {
313dcc57e24Smartin return false; /* no change, no need to write back */
314dcc57e24Smartin }
315dcc57e24Smartin
316dcc57e24Smartin #ifdef HAVE_GPT
317dcc57e24Smartin 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)318dcc57e24Smartin md_gpt_post_write(struct disk_partitions *parts, part_id root_id,
319dcc57e24Smartin bool root_is_new, part_id efi_id, bool efi_is_new)
320dcc57e24Smartin {
321dcc57e24Smartin /* no GPT boot support, nothing needs to be done here */
322dcc57e24Smartin return true;
323dcc57e24Smartin }
324dcc57e24Smartin #endif
325