Home | History | Annotate | Line # | Download | only in evbarm
md.c revision 1.19
      1 /*	$NetBSD: md.c,v 1.19 2020/10/14 15:09:10 martin 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 piecewise manually
     87 	 * later, just fetch the kernel set, do not unpack it.
     88 	 */
     89 	set_noextract_set(SET_KERNEL_1);
     90 }
     91 
     92 bool
     93 md_get_info(struct install_partition_desc *install)
     94 {
     95 	int res;
     96 
     97 	if (pm->no_mbr || pm->no_part)
     98 		return true;
     99 
    100 again:
    101 	if (pm->parts == NULL) {
    102 
    103 		const struct disk_partitioning_scheme *ps =
    104 		    select_part_scheme(pm, NULL, true, NULL);
    105 
    106 		if (!ps)
    107 			return false;
    108 
    109 		struct disk_partitions *parts =
    110 		   (*ps->create_new_for_disk)(pm->diskdev,
    111 		   0, pm->dlsize, true, NULL);
    112 		if (!parts)
    113 			return false;
    114 
    115 		pm->parts = parts;
    116 		if (ps->size_limit > 0 && pm->dlsize > ps->size_limit)
    117 			pm->dlsize = ps->size_limit;
    118 	}
    119 
    120 	/*
    121 	 * If the selected scheme does not need two-stage partitioning
    122 	 * (like GPT), do not bother to edit the outer partitions.
    123 	 */
    124 	if (pm->parts->pscheme->secondary_partitions == NULL ||
    125 	    pm->parts->pscheme->secondary_scheme == NULL)
    126 		return true;
    127 
    128 	res = edit_outer_parts(pm->parts);
    129 	if (res == 0)
    130 		return false;
    131 	else if (res == 1)
    132 		return true;
    133 
    134 	pm->parts->pscheme->destroy_part_scheme(pm->parts);
    135 	pm->parts = NULL;
    136 	goto again;
    137 }
    138 
    139 /*
    140  * md back-end code for menu-driven BSD disklabel editor.
    141  */
    142 int
    143 md_make_bsd_partitions(struct install_partition_desc *install)
    144 {
    145 	return make_bsd_partitions(install);
    146 }
    147 
    148 /*
    149  * any additional partition validation
    150  */
    151 bool
    152 md_check_partitions(struct install_partition_desc *install)
    153 {
    154 	size_t i;
    155 
    156 	for (i = 0; i < install->num; i++)
    157 		if (install->infos[i].fs_type == FS_MSDOS)
    158 			return true;
    159 
    160 	msg_display(MSG_nomsdospart);
    161 	process_menu(MENU_ok, NULL);
    162 
    163 	return false;
    164 }
    165 
    166 /*
    167  * hook called before writing new disklabel.
    168  */
    169 bool
    170 md_pre_disklabel(struct install_partition_desc *install,
    171     struct disk_partitions *parts)
    172 {
    173 
    174 	/*
    175 	 * RAW_PART is 2 on evbarm and bad things happen if we
    176 	 * write the MBR first and then the disklabel - so postpone
    177 	 * the MBR to md_post_disklabel(), unlike other architecturs.
    178 	 */
    179 	return true;
    180 }
    181 
    182 /*
    183  * hook called after writing disklabel to new target disk.
    184  */
    185 bool
    186 md_post_disklabel(struct install_partition_desc *install,
    187     struct disk_partitions *parts)
    188 {
    189 	if (parts->parent == NULL)
    190 		return true;	/* no outer partitions */
    191 
    192 	parts = parts->parent;
    193 
    194 	msg_display_subst(MSG_dofdisk, 3, parts->disk,
    195 	    msg_string(parts->pscheme->name),
    196 	    msg_string(parts->pscheme->short_name));
    197 
    198 	/* write edited "MBR" onto disk. */
    199 	if (!parts->pscheme->write_to_disk(parts)) {
    200 		msg_display(MSG_wmbrfail);
    201 		process_menu(MENU_ok, NULL);
    202 		return false;
    203 	}
    204 	return true;
    205 }
    206 
    207 /*
    208  * hook called after upgrade() or install() has finished setting
    209  * up the target disk but immediately before the user is given the
    210  * ``disks are now set up'' message.
    211  */
    212 int
    213 md_post_newfs(struct install_partition_desc *install)
    214 {
    215 	return 0;
    216 }
    217 
    218 int
    219 evbarm_extract_finalize(int update)
    220 {
    221 	distinfo *dist;
    222 	char kernelbin[100];
    223 	int (*saved_fetch_fn)(const char *);
    224 #ifdef	_LP64
    225 #define EFIBOOT	"/usr/mdec/bootaa64.efi"
    226 #else
    227 #define EFIBOOT	"/usr/mdec/bootarm.efi"
    228 #endif
    229 
    230 	dist = get_set_distinfo(SET_KERNEL_1);
    231 	if (dist == NULL)
    232 		return 0;
    233 
    234 	saved_fetch_fn = fetch_fn;
    235 	extract_file_to(dist, false, "/", "./netbsd", false);
    236 	fetch_fn = NULL;
    237 	make_target_dir("/boot/EFI/boot");
    238 	if (target_file_exists_p(EFIBOOT))
    239 		cp_within_target(EFIBOOT, "/boot/EFI/boot", 0);
    240 
    241 	if (boardtype == BOARD_TYPE_ACPI) {
    242 		fetch_fn = saved_fetch_fn;
    243 		return 0;
    244 	}
    245 	if (boardtype == BOARD_TYPE_NORMAL) {
    246 		extract_file_to(dist, false, "/boot", "./netbsd.ub", false);
    247 		fetch_fn = saved_fetch_fn;
    248 		return 0;
    249 	}
    250 	if (boardtype == BOARD_TYPE_RPI) {
    251 		extract_file_to(dist, false, "/boot", "./netbsd.img", false);
    252 		fetch_fn = saved_fetch_fn;
    253 		snprintf(kernelbin, 100, "%s/netbsd.img", targetroot_mnt);
    254 		if (file_exists_p(kernelbin)) {
    255 			run_program(RUN_DISPLAY,
    256 			    "/bin/cp %s /targetroot/boot/kernel.img", kernelbin);
    257 		} else {
    258 			msg_display(MSG_rpikernelmissing);
    259 			process_menu(MENU_ok, NULL);
    260 			return 1;
    261 		}
    262 	}
    263 	fetch_fn = saved_fetch_fn;
    264 	return 0;
    265 }
    266 
    267 int
    268 md_post_extract(struct install_partition_desc *install)
    269 {
    270 
    271 	return 0;
    272 }
    273 
    274 void
    275 md_cleanup_install(struct install_partition_desc *install)
    276 {
    277 #ifndef DEBUG
    278 	enable_rc_conf();
    279 	add_rc_conf("sshd=YES\n");
    280 	add_rc_conf("dhcpcd=YES\n");
    281 #endif
    282 }
    283 
    284 int
    285 md_pre_update(struct install_partition_desc *install)
    286 {
    287 	return 1;
    288 }
    289 
    290 /* Upgrade support */
    291 int
    292 md_update(struct install_partition_desc *install)
    293 {
    294 	md_post_newfs(install);
    295 	return 1;
    296 }
    297 
    298 int
    299 md_pre_mount(struct install_partition_desc *install, size_t ndx)
    300 {
    301 	return 0;
    302 }
    303 
    304 int
    305 md_check_mbr(struct disk_partitions *parts, mbr_info_t *mbri, bool quiet)
    306 {
    307 	mbr_info_t *ext;
    308 	struct mbr_partition *part;
    309 	int i, hasboot=0;
    310 
    311 	for (ext = mbri; ext; ext = ext->extended) {
    312 		part = ext->mbr.mbr_parts;
    313 		for (i=0, hasboot=0; i < MBR_PART_COUNT; part++, i++) {
    314 			if (part->mbrp_type != MBR_PTYPE_FAT16L &&
    315 			    part->mbrp_type != MBR_PTYPE_FAT32L)
    316 				continue;
    317 			hasboot = 1;
    318 			break;
    319 		}
    320 	}
    321 	if (!hasboot) {
    322 		if (quiet)
    323 			return 2;
    324 		msg_display(MSG_nomsdospart);
    325 		return ask_reedit(parts);
    326 	}
    327 
    328 	return 2;
    329 }
    330 
    331 bool
    332 md_parts_use_wholedisk(struct disk_partitions *parts)
    333 {
    334 	struct disk_part_info boot_part = {
    335 		.size = boardtype == BOARD_TYPE_NORMAL ?
    336 		    PART_BOOT_LARGE/parts->bytes_per_sector :
    337 		    PART_BOOT/parts->bytes_per_sector,
    338 		.fs_type = PART_BOOT_TYPE, .fs_sub_type = MBR_PTYPE_FAT16L,
    339 	};
    340 
    341 	return parts_use_wholedisk(parts, 1, &boot_part);
    342 }
    343 
    344 /* returns false if no write-back of parts is required */
    345 bool
    346 md_mbr_update_check(struct disk_partitions *parts, mbr_info_t *mbri)
    347 {
    348 	return false;
    349 }
    350 
    351 #ifdef HAVE_GPT
    352 /*
    353  * New GPT partitions have been written, update bootloader or remember
    354  * data untill needed in md_post_newfs
    355  */
    356 bool
    357 md_gpt_post_write(struct disk_partitions *parts, part_id root_id,
    358     bool root_is_new, part_id efi_id, bool efi_is_new)
    359 {
    360 	return true;
    361 }
    362 #endif
    363 
    364 void
    365 evbarm_part_defaults(struct pm_devs *my_pm, struct part_usage_info *infos,
    366     size_t num_usage_infos)
    367 {
    368 	size_t i;
    369 
    370 	if (boardtype != BOARD_TYPE_NORMAL)
    371 		return;
    372 
    373 	for (i = 0; i < num_usage_infos; i++) {
    374 		if (infos[i].fs_type == PART_BOOT_TYPE &&
    375 		    infos[i].mount[0] != 0 &&
    376 		    strcmp(infos[i].mount, PART_BOOT_MOUNT) == 0) {
    377 			infos[i].size = PART_BOOT_LARGE /
    378 			    my_pm->parts->bytes_per_sector;
    379 			return;
    380 		}
    381 	}
    382 }
    383 
    384