Home | History | Annotate | Line # | Download | only in evbarm
md.c revision 1.8.2.8
      1 /*	$NetBSD: md.c,v 1.8.2.8 2022/02/02 04:25:40 msaitoh Exp $ */
      2 
      3 /*
      4  * Copyright 1997 Piermont Information Systems Inc.
      5  * All rights reserved.
      6  *
      7  * Based on code written by Philip A. Nelson for Piermont Information
      8  * Systems Inc.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. The name of Piermont Information Systems Inc. may not be used to endorse
     19  *    or promote products derived from this software without specific prior
     20  *    written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
     23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
     26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     32  * THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 /* md.c -- shark machine specific routines */
     36 
     37 #include <stdio.h>
     38 #include <curses.h>
     39 #include <unistd.h>
     40 #include <fcntl.h>
     41 #include <util.h>
     42 #include <sys/types.h>
     43 #include <sys/ioctl.h>
     44 #include <sys/param.h>
     45 #include <sys/sysctl.h>
     46 
     47 #include "defs.h"
     48 #include "md.h"
     49 #include "msg_defs.h"
     50 #include "menu_defs.h"
     51 
     52 int boardtype = BOARD_TYPE_NORMAL;
     53 
     54 #define	SBSA_MODEL_STR	"netbsd,generic-acpi"
     55 #define	RPI_MODEL_STR	"raspberrypi,"
     56 
     57 void
     58 md_init(void)
     59 {
     60 	static const int mib[2] = {CTL_HW, HW_MODEL};
     61 	size_t len;
     62 	char *cpu_model;
     63 
     64 	sysctl(mib, 2, NULL, &len, NULL, 0);
     65 	cpu_model = malloc(len);
     66 	sysctl(mib, 2, cpu_model, &len, NULL, 0);
     67 
     68 	if (strstr(cpu_model, RPI_MODEL_STR) != NULL)
     69 		/* this is some kind of Raspberry Pi */
     70 		boardtype = BOARD_TYPE_RPI;
     71 	else if (strstr(cpu_model, SBSA_MODEL_STR) != NULL)
     72 		/* some SBSA compatible machine */
     73 		boardtype = BOARD_TYPE_ACPI;
     74 	else
     75 		/* unknown, assume u-boot + dtb */
     76 		boardtype = BOARD_TYPE_NORMAL;
     77 
     78 	free(cpu_model);
     79 }
     80 
     81 void
     82 md_init_set_status(int flags)
     83 {
     84 
     85 	/*
     86 	 * we will extract kernel variants and DTB files piecwise
     87 	 * manually later, just fetch the kernel set, do not
     88 	 * unpack it.
     89 	 */
     90 	set_kernel_set(SET_KERNEL_1);
     91 	set_noextract_set(SET_KERNEL_1);
     92 }
     93 
     94 bool
     95 md_get_info(struct install_partition_desc *install)
     96 {
     97 	int res;
     98 
     99 	if (pm->no_mbr || pm->no_part)
    100 		return true;
    101 
    102 again:
    103 	if (pm->parts == NULL) {
    104 
    105 		const struct disk_partitioning_scheme *ps =
    106 		    select_part_scheme(pm, NULL, true, NULL);
    107 
    108 		if (!ps)
    109 			return false;
    110 
    111 		struct disk_partitions *parts =
    112 		   (*ps->create_new_for_disk)(pm->diskdev,
    113 		   0, pm->dlsize, true, NULL);
    114 		if (!parts)
    115 			return false;
    116 
    117 		pm->parts = parts;
    118 		if (ps->size_limit > 0 && pm->dlsize > ps->size_limit)
    119 			pm->dlsize = ps->size_limit;
    120 	}
    121 
    122 	/*
    123 	 * If the selected scheme does not need two-stage partitioning
    124 	 * (like GPT), do not bother to edit the outer partitions.
    125 	 */
    126 	if (pm->parts->pscheme->secondary_partitions == NULL ||
    127 	    pm->parts->pscheme->secondary_scheme == NULL)
    128 		return true;
    129 
    130 	res = edit_outer_parts(pm->parts);
    131 	if (res == 0)
    132 		return false;
    133 	else if (res == 1)
    134 		return true;
    135 
    136 	pm->parts->pscheme->destroy_part_scheme(pm->parts);
    137 	pm->parts = NULL;
    138 	goto again;
    139 }
    140 
    141 /*
    142  * md back-end code for menu-driven BSD disklabel editor.
    143  */
    144 int
    145 md_make_bsd_partitions(struct install_partition_desc *install)
    146 {
    147 	return make_bsd_partitions(install);
    148 }
    149 
    150 /*
    151  * any additional partition validation
    152  */
    153 bool
    154 md_check_partitions(struct install_partition_desc *install)
    155 {
    156 	size_t i;
    157 
    158 	for (i = 0; i < install->num; i++)
    159 		if (install->infos[i].fs_type == FS_MSDOS)
    160 			return true;
    161 
    162 	msg_display(MSG_nomsdospart);
    163 	process_menu(MENU_ok, NULL);
    164 
    165 	return false;
    166 }
    167 
    168 /*
    169  * hook called before writing new disklabel.
    170  */
    171 bool
    172 md_pre_disklabel(struct install_partition_desc *install,
    173     struct disk_partitions *parts)
    174 {
    175 
    176 	/*
    177 	 * RAW_PART is 2 on evbarm and bad things happen if we
    178 	 * write the MBR first and then the disklabel - so postpone
    179 	 * the MBR to md_post_disklabel(), unlike other architecturs.
    180 	 */
    181 	return true;
    182 }
    183 
    184 /*
    185  * hook called after writing disklabel to new target disk.
    186  */
    187 bool
    188 md_post_disklabel(struct install_partition_desc *install,
    189     struct disk_partitions *parts)
    190 {
    191 	if (parts->parent == NULL)
    192 		return true;	/* no outer partitions */
    193 
    194 	parts = parts->parent;
    195 
    196 	msg_display_subst(MSG_dofdisk, 3, parts->disk,
    197 	    msg_string(parts->pscheme->name),
    198 	    msg_string(parts->pscheme->short_name));
    199 
    200 	/* write edited "MBR" onto disk. */
    201 	if (!parts->pscheme->write_to_disk(parts)) {
    202 		msg_display(MSG_wmbrfail);
    203 		process_menu(MENU_ok, NULL);
    204 		return false;
    205 	}
    206 	return true;
    207 }
    208 
    209 /*
    210  * hook called after upgrade() or install() has finished setting
    211  * up the target disk but immediately before the user is given the
    212  * ``disks are now set up'' message.
    213  */
    214 int
    215 md_post_newfs(struct install_partition_desc *install)
    216 {
    217 	return 0;
    218 }
    219 
    220 int
    221 evbarm_extract_finalize(int update)
    222 {
    223 	distinfo *dist;
    224 	char kernelbin[100];
    225 	int (*saved_fetch_fn)(const char *);
    226 #ifdef	_LP64
    227 #define EFIBOOT	"/usr/mdec/bootaa64.efi"
    228 #else
    229 #define EFIBOOT	"/usr/mdec/bootarm.efi"
    230 #endif
    231 
    232 	dist = get_set_distinfo(SET_KERNEL_1);
    233 	if (dist == NULL)
    234 		return 0;
    235 
    236 	saved_fetch_fn = fetch_fn;
    237 	extract_file_to(dist, false, "/", "./netbsd", false);
    238 	fetch_fn = NULL;
    239 	make_target_dir("/boot/EFI/boot");
    240 	if (target_file_exists_p(EFIBOOT))
    241 		cp_within_target(EFIBOOT, "/boot/EFI/boot", 0);
    242 
    243 	if (boardtype == BOARD_TYPE_ACPI) {
    244 		fetch_fn = saved_fetch_fn;
    245 		return 0;
    246 	}
    247 	if (boardtype == BOARD_TYPE_NORMAL) {
    248 		make_target_dir("/boot/dtb");
    249 		extract_file_to(dist, false, "/boot/dtb", "*.dt*", false);
    250 		extract_file_to(dist, false, "/boot", "./netbsd.ub", false);
    251 		fetch_fn = saved_fetch_fn;
    252 		return 0;
    253 	}
    254 	if (boardtype == BOARD_TYPE_RPI) {
    255 		extract_file_to(dist, false, "/boot", "./netbsd.img", false);
    256 		extract_file_to(dist, false, "/boot", "./bcm*.dtb", false);
    257 		fetch_fn = saved_fetch_fn;
    258 		snprintf(kernelbin, 100, "%s/netbsd.img", targetroot_mnt);
    259 		if (file_exists_p(kernelbin)) {
    260 			run_program(RUN_DISPLAY,
    261 			    "/bin/cp %s /targetroot/boot/kernel.img", kernelbin);
    262 		} else {
    263 			msg_display(MSG_rpikernelmissing);
    264 			process_menu(MENU_ok, NULL);
    265 			return 1;
    266 		}
    267 	}
    268 	fetch_fn = saved_fetch_fn;
    269 	return 0;
    270 }
    271 
    272 int
    273 md_post_extract(struct install_partition_desc *install, bool upgrade)
    274 {
    275 
    276 	return 0;
    277 }
    278 
    279 void
    280 md_cleanup_install(struct install_partition_desc *install)
    281 {
    282 #ifndef DEBUG
    283 	enable_rc_conf();
    284 	add_rc_conf("sshd=YES\n");
    285 	add_rc_conf("dhcpcd=YES\n");
    286 #endif
    287 }
    288 
    289 int
    290 md_pre_update(struct install_partition_desc *install)
    291 {
    292 	return 1;
    293 }
    294 
    295 /* Upgrade support */
    296 int
    297 md_update(struct install_partition_desc *install)
    298 {
    299 	md_post_newfs(install);
    300 	return 1;
    301 }
    302 
    303 int
    304 md_pre_mount(struct install_partition_desc *install, size_t ndx)
    305 {
    306 	return 0;
    307 }
    308 
    309 int
    310 md_check_mbr(struct disk_partitions *parts, mbr_info_t *mbri, bool quiet)
    311 {
    312 	mbr_info_t *ext;
    313 	struct mbr_partition *part;
    314 	int i, hasboot=0;
    315 
    316 	for (ext = mbri; ext; ext = ext->extended) {
    317 		part = ext->mbr.mbr_parts;
    318 		for (i=0, hasboot=0; i < MBR_PART_COUNT; part++, i++) {
    319 			if (part->mbrp_type != MBR_PTYPE_FAT16L &&
    320 			    part->mbrp_type != MBR_PTYPE_FAT32L)
    321 				continue;
    322 			hasboot = 1;
    323 			break;
    324 		}
    325 	}
    326 	if (!hasboot) {
    327 		if (quiet)
    328 			return 2;
    329 		msg_display(MSG_nomsdospart);
    330 		return ask_reedit(parts);
    331 	}
    332 
    333 	return 2;
    334 }
    335 
    336 bool
    337 md_parts_use_wholedisk(struct disk_partitions *parts)
    338 {
    339 	struct disk_part_info boot_part = {
    340 		.size = boardtype == BOARD_TYPE_NORMAL ?
    341 		    PART_BOOT_LARGE/parts->bytes_per_sector :
    342 		    PART_BOOT/parts->bytes_per_sector,
    343 		.fs_type = PART_BOOT_TYPE,
    344 		.fs_sub_type = boardtype == BOARD_TYPE_NORMAL ?
    345 		    MBR_PTYPE_FAT32L : MBR_PTYPE_FAT16L,
    346 	};
    347 
    348 	return parts_use_wholedisk(parts, 1, &boot_part);
    349 }
    350 
    351 /* returns false if no write-back of parts is required */
    352 bool
    353 md_mbr_update_check(struct disk_partitions *parts, mbr_info_t *mbri)
    354 {
    355 	return false;
    356 }
    357 
    358 #ifdef HAVE_GPT
    359 /*
    360  * New GPT partitions have been written, update bootloader or remember
    361  * data untill needed in md_post_newfs
    362  */
    363 bool
    364 md_gpt_post_write(struct disk_partitions *parts, part_id root_id,
    365     bool root_is_new, part_id efi_id, bool efi_is_new)
    366 {
    367 	return true;
    368 }
    369 #endif
    370 
    371 void
    372 evbarm_part_defaults(struct pm_devs *my_pm, struct part_usage_info *infos,
    373     size_t num_usage_infos)
    374 {
    375 	size_t i;
    376 
    377 	for (i = 0; i < num_usage_infos; i++) {
    378 		if (infos[i].fs_type == PART_BOOT_TYPE &&
    379 		    infos[i].mount[0] != 0 &&
    380 		    strcmp(infos[i].mount, PART_BOOT_MOUNT) == 0) {
    381 			infos[i].size = boardtype == BOARD_TYPE_NORMAL ?
    382 			    PART_BOOT_LARGE/my_pm->parts->bytes_per_sector :
    383 			    PART_BOOT/my_pm->parts->bytes_per_sector;
    384 			infos[i].fs_version = boardtype == BOARD_TYPE_NORMAL ?
    385 			    MBR_PTYPE_FAT32L : MBR_PTYPE_FAT16L;
    386 			return;
    387 		}
    388 	}
    389 }
    390 
    391