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