1*4204f810Smartin /* $NetBSD: md.c,v 1.22 2022/01/29 16:01:17 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 -- shark machine specific routines */
3650dbef1aSdholland
3750dbef1aSdholland #include <stdio.h>
3850dbef1aSdholland #include <curses.h>
3950dbef1aSdholland #include <unistd.h>
4050dbef1aSdholland #include <fcntl.h>
4150dbef1aSdholland #include <util.h>
4250dbef1aSdholland #include <sys/types.h>
4350dbef1aSdholland #include <sys/ioctl.h>
4450dbef1aSdholland #include <sys/param.h>
45c604f98eSmartin #include <sys/sysctl.h>
4650dbef1aSdholland
4750dbef1aSdholland #include "defs.h"
4850dbef1aSdholland #include "md.h"
4950dbef1aSdholland #include "msg_defs.h"
5050dbef1aSdholland #include "menu_defs.h"
5150dbef1aSdholland
52b2a9c378Smartin int boardtype = BOARD_TYPE_NORMAL;
5350dbef1aSdholland
54c604f98eSmartin #define SBSA_MODEL_STR "netbsd,generic-acpi"
55c604f98eSmartin #define RPI_MODEL_STR "raspberrypi,"
56c604f98eSmartin
5750dbef1aSdholland void
md_init(void)5850dbef1aSdholland md_init(void)
5950dbef1aSdholland {
60c604f98eSmartin static const int mib[2] = {CTL_HW, HW_MODEL};
61c604f98eSmartin size_t len;
62c604f98eSmartin char *cpu_model;
63b2a9c378Smartin
64c604f98eSmartin sysctl(mib, 2, NULL, &len, NULL, 0);
65c604f98eSmartin cpu_model = malloc(len);
66c604f98eSmartin sysctl(mib, 2, cpu_model, &len, NULL, 0);
67b2a9c378Smartin
68c604f98eSmartin if (strstr(cpu_model, RPI_MODEL_STR) != NULL)
69b2a9c378Smartin /* this is some kind of Raspberry Pi */
70b2a9c378Smartin boardtype = BOARD_TYPE_RPI;
71c604f98eSmartin else if (strstr(cpu_model, SBSA_MODEL_STR) != NULL)
72c604f98eSmartin /* some SBSA compatible machine */
73c604f98eSmartin boardtype = BOARD_TYPE_ACPI;
74c604f98eSmartin else
75c604f98eSmartin /* unknown, assume u-boot + dtb */
76c604f98eSmartin boardtype = BOARD_TYPE_NORMAL;
77c604f98eSmartin
78c604f98eSmartin free(cpu_model);
7950dbef1aSdholland }
8050dbef1aSdholland
8150dbef1aSdholland void
md_init_set_status(int flags)8250dbef1aSdholland md_init_set_status(int flags)
8350dbef1aSdholland {
84249ed7a6Smartin
85249ed7a6Smartin /*
86c34f2808Sjmcneill * we will extract kernel variants piecewise manually
87c34f2808Sjmcneill * later, just fetch the kernel set, do not unpack it.
88249ed7a6Smartin */
895bdad892Sjmcneill set_kernel_set(SET_KERNEL_1);
90249ed7a6Smartin set_noextract_set(SET_KERNEL_1);
9150dbef1aSdholland }
9250dbef1aSdholland
934103857bSmartin bool
md_get_info(struct install_partition_desc * install)944103857bSmartin md_get_info(struct install_partition_desc *install)
9550dbef1aSdholland {
96957b5cd6Smartin int res;
9750dbef1aSdholland
98e11b8d25Smartin if (pm->no_mbr || pm->no_part)
99e11b8d25Smartin return true;
100e11b8d25Smartin
101957b5cd6Smartin again:
102e11b8d25Smartin if (pm->parts == NULL) {
103e11b8d25Smartin
104e11b8d25Smartin const struct disk_partitioning_scheme *ps =
105e11b8d25Smartin select_part_scheme(pm, NULL, true, NULL);
106e11b8d25Smartin
107e11b8d25Smartin if (!ps)
1086fec6798Smartin return false;
109e11b8d25Smartin
110e11b8d25Smartin struct disk_partitions *parts =
111e11b8d25Smartin (*ps->create_new_for_disk)(pm->diskdev,
11286906049Smartin 0, pm->dlsize, true, NULL);
113e11b8d25Smartin if (!parts)
114e11b8d25Smartin return false;
115e11b8d25Smartin
116e11b8d25Smartin pm->parts = parts;
117e11b8d25Smartin if (ps->size_limit > 0 && pm->dlsize > ps->size_limit)
118e11b8d25Smartin pm->dlsize = ps->size_limit;
119e11b8d25Smartin }
120e11b8d25Smartin
121bb6bc013Smartin /*
122bb6bc013Smartin * If the selected scheme does not need two-stage partitioning
123bb6bc013Smartin * (like GPT), do not bother to edit the outer partitions.
124bb6bc013Smartin */
125bb6bc013Smartin if (pm->parts->pscheme->secondary_partitions == NULL ||
126bb6bc013Smartin pm->parts->pscheme->secondary_scheme == NULL)
127bb6bc013Smartin return true;
128957b5cd6Smartin
129957b5cd6Smartin res = edit_outer_parts(pm->parts);
130957b5cd6Smartin if (res == 0)
131957b5cd6Smartin return false;
132957b5cd6Smartin else if (res == 1)
133957b5cd6Smartin return true;
134957b5cd6Smartin
135957b5cd6Smartin pm->parts->pscheme->destroy_part_scheme(pm->parts);
136957b5cd6Smartin pm->parts = NULL;
137957b5cd6Smartin goto again;
13850dbef1aSdholland }
13950dbef1aSdholland
14050dbef1aSdholland /*
14150dbef1aSdholland * md back-end code for menu-driven BSD disklabel editor.
14250dbef1aSdholland */
143957b5cd6Smartin int
md_make_bsd_partitions(struct install_partition_desc * install)1444103857bSmartin md_make_bsd_partitions(struct install_partition_desc *install)
14550dbef1aSdholland {
1464103857bSmartin return make_bsd_partitions(install);
14750dbef1aSdholland }
14850dbef1aSdholland
14950dbef1aSdholland /*
15050dbef1aSdholland * any additional partition validation
15150dbef1aSdholland */
1524103857bSmartin bool
md_check_partitions(struct install_partition_desc * install)1534103857bSmartin md_check_partitions(struct install_partition_desc *install)
15450dbef1aSdholland {
1554103857bSmartin size_t i;
15650dbef1aSdholland
1574103857bSmartin for (i = 0; i < install->num; i++)
1584103857bSmartin if (install->infos[i].fs_type == FS_MSDOS)
1594103857bSmartin return true;
16050dbef1aSdholland
16150dbef1aSdholland msg_display(MSG_nomsdospart);
16250dbef1aSdholland process_menu(MENU_ok, NULL);
163249ed7a6Smartin
1644103857bSmartin return false;
16550dbef1aSdholland }
16650dbef1aSdholland
16750dbef1aSdholland /*
16850dbef1aSdholland * hook called before writing new disklabel.
16950dbef1aSdholland */
1704103857bSmartin bool
md_pre_disklabel(struct install_partition_desc * install,struct disk_partitions * parts)1714103857bSmartin md_pre_disklabel(struct install_partition_desc *install,
1724103857bSmartin struct disk_partitions *parts)
17350dbef1aSdholland {
17450dbef1aSdholland
175bb6bc013Smartin /*
176bb6bc013Smartin * RAW_PART is 2 on evbarm and bad things happen if we
177bb6bc013Smartin * write the MBR first and then the disklabel - so postpone
178bb6bc013Smartin * the MBR to md_post_disklabel(), unlike other architecturs.
179bb6bc013Smartin */
180bb6bc013Smartin return true;
181bb6bc013Smartin }
182bb6bc013Smartin
183bb6bc013Smartin /*
184bb6bc013Smartin * hook called after writing disklabel to new target disk.
185bb6bc013Smartin */
186bb6bc013Smartin bool
md_post_disklabel(struct install_partition_desc * install,struct disk_partitions * parts)187bb6bc013Smartin md_post_disklabel(struct install_partition_desc *install,
188bb6bc013Smartin struct disk_partitions *parts)
189bb6bc013Smartin {
1904103857bSmartin if (parts->parent == NULL)
1914103857bSmartin return true; /* no outer partitions */
19250dbef1aSdholland
1934103857bSmartin parts = parts->parent;
1944103857bSmartin
1954103857bSmartin msg_display_subst(MSG_dofdisk, 3, parts->disk,
1964103857bSmartin msg_string(parts->pscheme->name),
1974103857bSmartin msg_string(parts->pscheme->short_name));
1984103857bSmartin
1994103857bSmartin /* write edited "MBR" onto disk. */
2004103857bSmartin if (!parts->pscheme->write_to_disk(parts)) {
20150dbef1aSdholland msg_display(MSG_wmbrfail);
20250dbef1aSdholland process_menu(MENU_ok, NULL);
2034103857bSmartin return false;
20450dbef1aSdholland }
2054103857bSmartin return true;
20650dbef1aSdholland }
20750dbef1aSdholland
20850dbef1aSdholland /*
20950dbef1aSdholland * hook called after upgrade() or install() has finished setting
21050dbef1aSdholland * up the target disk but immediately before the user is given the
21150dbef1aSdholland * ``disks are now set up'' message.
21250dbef1aSdholland */
21350dbef1aSdholland int
md_post_newfs(struct install_partition_desc * install)2144103857bSmartin md_post_newfs(struct install_partition_desc *install)
21550dbef1aSdholland {
21650dbef1aSdholland return 0;
21750dbef1aSdholland }
21850dbef1aSdholland
21950dbef1aSdholland int
evbarm_extract_finalize(int update)220249ed7a6Smartin evbarm_extract_finalize(int update)
22150dbef1aSdholland {
222249ed7a6Smartin distinfo *dist;
22350dbef1aSdholland char kernelbin[100];
224249ed7a6Smartin int (*saved_fetch_fn)(const char *);
225249ed7a6Smartin #ifdef _LP64
226249ed7a6Smartin #define EFIBOOT "/usr/mdec/bootaa64.efi"
227249ed7a6Smartin #else
228249ed7a6Smartin #define EFIBOOT "/usr/mdec/bootarm.efi"
229249ed7a6Smartin #endif
23050dbef1aSdholland
231249ed7a6Smartin dist = get_set_distinfo(SET_KERNEL_1);
232249ed7a6Smartin if (dist == NULL)
23350dbef1aSdholland return 0;
234249ed7a6Smartin
235249ed7a6Smartin saved_fetch_fn = fetch_fn;
236249ed7a6Smartin extract_file_to(dist, false, "/", "./netbsd", false);
237249ed7a6Smartin fetch_fn = NULL;
238249ed7a6Smartin make_target_dir("/boot/EFI/boot");
239249ed7a6Smartin if (target_file_exists_p(EFIBOOT))
240249ed7a6Smartin cp_within_target(EFIBOOT, "/boot/EFI/boot", 0);
241249ed7a6Smartin
242249ed7a6Smartin if (boardtype == BOARD_TYPE_ACPI) {
243249ed7a6Smartin fetch_fn = saved_fetch_fn;
244249ed7a6Smartin return 0;
245249ed7a6Smartin }
246249ed7a6Smartin if (boardtype == BOARD_TYPE_NORMAL) {
247249ed7a6Smartin extract_file_to(dist, false, "/boot", "./netbsd.ub", false);
248249ed7a6Smartin fetch_fn = saved_fetch_fn;
249249ed7a6Smartin return 0;
250249ed7a6Smartin }
25150dbef1aSdholland if (boardtype == BOARD_TYPE_RPI) {
252249ed7a6Smartin extract_file_to(dist, false, "/boot", "./netbsd.img", false);
253249ed7a6Smartin fetch_fn = saved_fetch_fn;
254ba7c07c6Sskrll snprintf(kernelbin, 100, "%s/netbsd.img", targetroot_mnt);
25550dbef1aSdholland if (file_exists_p(kernelbin)) {
25650dbef1aSdholland run_program(RUN_DISPLAY,
25750dbef1aSdholland "/bin/cp %s /targetroot/boot/kernel.img", kernelbin);
25850dbef1aSdholland } else {
25950dbef1aSdholland msg_display(MSG_rpikernelmissing);
26050dbef1aSdholland process_menu(MENU_ok, NULL);
26150dbef1aSdholland return 1;
26250dbef1aSdholland }
26350dbef1aSdholland }
264249ed7a6Smartin fetch_fn = saved_fetch_fn;
265249ed7a6Smartin return 0;
266249ed7a6Smartin }
267249ed7a6Smartin
268249ed7a6Smartin int
md_post_extract(struct install_partition_desc * install,bool upgrade)269*4204f810Smartin md_post_extract(struct install_partition_desc *install, bool upgrade)
270249ed7a6Smartin {
271249ed7a6Smartin
27250dbef1aSdholland return 0;
27350dbef1aSdholland }
27450dbef1aSdholland
27550dbef1aSdholland void
md_cleanup_install(struct install_partition_desc * install)2764103857bSmartin md_cleanup_install(struct install_partition_desc *install)
27750dbef1aSdholland {
27850dbef1aSdholland #ifndef DEBUG
27950dbef1aSdholland enable_rc_conf();
28050dbef1aSdholland add_rc_conf("sshd=YES\n");
28150dbef1aSdholland add_rc_conf("dhcpcd=YES\n");
28250dbef1aSdholland #endif
28350dbef1aSdholland }
28450dbef1aSdholland
28550dbef1aSdholland int
md_pre_update(struct install_partition_desc * install)2864103857bSmartin md_pre_update(struct install_partition_desc *install)
28750dbef1aSdholland {
28850dbef1aSdholland return 1;
28950dbef1aSdholland }
29050dbef1aSdholland
29150dbef1aSdholland /* Upgrade support */
29250dbef1aSdholland int
md_update(struct install_partition_desc * install)2934103857bSmartin md_update(struct install_partition_desc *install)
29450dbef1aSdholland {
2954103857bSmartin md_post_newfs(install);
29650dbef1aSdholland return 1;
29750dbef1aSdholland }
29850dbef1aSdholland
29950dbef1aSdholland int
md_pre_mount(struct install_partition_desc * install,size_t ndx)3004f30cbf3Smartin md_pre_mount(struct install_partition_desc *install, size_t ndx)
30150dbef1aSdholland {
30250dbef1aSdholland return 0;
30350dbef1aSdholland }
30450dbef1aSdholland
30550dbef1aSdholland int
md_check_mbr(struct disk_partitions * parts,mbr_info_t * mbri,bool quiet)3064103857bSmartin md_check_mbr(struct disk_partitions *parts, mbr_info_t *mbri, bool quiet)
30750dbef1aSdholland {
30850dbef1aSdholland mbr_info_t *ext;
30950dbef1aSdholland struct mbr_partition *part;
31050dbef1aSdholland int i, hasboot=0;
31150dbef1aSdholland
31250dbef1aSdholland for (ext = mbri; ext; ext = ext->extended) {
31350dbef1aSdholland part = ext->mbr.mbr_parts;
31450dbef1aSdholland for (i=0, hasboot=0; i < MBR_PART_COUNT; part++, i++) {
315f449d0c4Sjoerg if (part->mbrp_type != MBR_PTYPE_FAT16L &&
316f449d0c4Sjoerg part->mbrp_type != MBR_PTYPE_FAT32L)
31750dbef1aSdholland continue;
31850dbef1aSdholland hasboot = 1;
31950dbef1aSdholland break;
32050dbef1aSdholland }
32150dbef1aSdholland }
32250dbef1aSdholland if (!hasboot) {
3234103857bSmartin if (quiet)
3244103857bSmartin return 2;
32550dbef1aSdholland msg_display(MSG_nomsdospart);
3264103857bSmartin return ask_reedit(parts);
32750dbef1aSdholland }
328249ed7a6Smartin
32950dbef1aSdholland return 2;
33050dbef1aSdholland }
33150dbef1aSdholland
3324103857bSmartin bool
md_parts_use_wholedisk(struct disk_partitions * parts)3334103857bSmartin md_parts_use_wholedisk(struct disk_partitions *parts)
33450dbef1aSdholland {
335249ed7a6Smartin struct disk_part_info boot_part = {
336249ed7a6Smartin .size = boardtype == BOARD_TYPE_NORMAL ?
3377714ed32Smartin PART_BOOT_LARGE/parts->bytes_per_sector :
3387714ed32Smartin PART_BOOT/parts->bytes_per_sector,
339e2b83173Smartin .fs_type = PART_BOOT_TYPE,
340e2b83173Smartin .fs_sub_type = boardtype == BOARD_TYPE_NORMAL ?
341e2b83173Smartin MBR_PTYPE_FAT32L : MBR_PTYPE_FAT16L,
342249ed7a6Smartin };
3434103857bSmartin
344249ed7a6Smartin return parts_use_wholedisk(parts, 1, &boot_part);
34550dbef1aSdholland }
3464103857bSmartin
3474103857bSmartin /* returns false if no write-back of parts is required */
3484103857bSmartin bool
md_mbr_update_check(struct disk_partitions * parts,mbr_info_t * mbri)3494103857bSmartin md_mbr_update_check(struct disk_partitions *parts, mbr_info_t *mbri)
3504103857bSmartin {
3514103857bSmartin return false;
3524103857bSmartin }
3534103857bSmartin
3544103857bSmartin #ifdef HAVE_GPT
3554103857bSmartin /*
3564103857bSmartin * New GPT partitions have been written, update bootloader or remember
3574103857bSmartin * data untill needed in md_post_newfs
3584103857bSmartin */
3594103857bSmartin 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)3604103857bSmartin md_gpt_post_write(struct disk_partitions *parts, part_id root_id,
3614103857bSmartin bool root_is_new, part_id efi_id, bool efi_is_new)
3624103857bSmartin {
3634103857bSmartin return true;
3644103857bSmartin }
3654103857bSmartin #endif
366249ed7a6Smartin
367249ed7a6Smartin void
evbarm_part_defaults(struct pm_devs * my_pm,struct part_usage_info * infos,size_t num_usage_infos)368249ed7a6Smartin evbarm_part_defaults(struct pm_devs *my_pm, struct part_usage_info *infos,
369249ed7a6Smartin size_t num_usage_infos)
370249ed7a6Smartin {
371249ed7a6Smartin size_t i;
372249ed7a6Smartin
373249ed7a6Smartin for (i = 0; i < num_usage_infos; i++) {
374249ed7a6Smartin if (infos[i].fs_type == PART_BOOT_TYPE &&
37529f96e81Smartin infos[i].mount[0] != 0 &&
376249ed7a6Smartin strcmp(infos[i].mount, PART_BOOT_MOUNT) == 0) {
377e2b83173Smartin infos[i].size = boardtype == BOARD_TYPE_NORMAL ?
378e2b83173Smartin PART_BOOT_LARGE/my_pm->parts->bytes_per_sector :
379e2b83173Smartin PART_BOOT/my_pm->parts->bytes_per_sector;
380e2b83173Smartin infos[i].fs_version = boardtype == BOARD_TYPE_NORMAL ?
381e2b83173Smartin MBR_PTYPE_FAT32L : MBR_PTYPE_FAT16L;
382249ed7a6Smartin return;
383249ed7a6Smartin }
384249ed7a6Smartin }
385249ed7a6Smartin }
386249ed7a6Smartin
387