1 /* $NetBSD: md.c,v 1.9 2019/08/14 12:55:36 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 46 #include "defs.h" 47 #include "md.h" 48 #include "msg_defs.h" 49 #include "menu_defs.h" 50 51 int boardtype = 0; 52 53 void 54 md_prelim_menu(void) 55 { 56 /* get the boardtype from the user */ 57 process_menu(MENU_prelim, NULL); 58 } 59 60 void 61 md_init(void) 62 { 63 } 64 65 void 66 md_init_set_status(int flags) 67 { 68 if (boardtype == BOARD_TYPE_RPI) 69 set_kernel_set(SET_KERNEL_RPI); 70 } 71 72 bool 73 md_get_info(struct install_partition_desc *install) 74 { 75 76 if (pm->no_mbr || pm->no_part) 77 return true; 78 79 if (pm->parts == NULL) { 80 81 const struct disk_partitioning_scheme *ps = 82 select_part_scheme(pm, NULL, true, NULL); 83 84 if (!ps) 85 return false; 86 87 struct disk_partitions *parts = 88 (*ps->create_new_for_disk)(pm->diskdev, 89 0, pm->dlsize, pm->dlsize, true); 90 if (!parts) 91 return false; 92 93 pm->parts = parts; 94 if (ps->size_limit > 0 && pm->dlsize > ps->size_limit) 95 pm->dlsize = ps->size_limit; 96 } 97 98 if (boardtype == BOARD_TYPE_RPI) 99 return set_bios_geom_with_mbr_guess(pm->parts); 100 101 return true; 102 } 103 104 /* 105 * md back-end code for menu-driven BSD disklabel editor. 106 */ 107 bool 108 md_make_bsd_partitions(struct install_partition_desc *install) 109 { 110 return make_bsd_partitions(install); 111 } 112 113 /* 114 * any additional partition validation 115 */ 116 bool 117 md_check_partitions(struct install_partition_desc *install) 118 { 119 size_t i; 120 121 if (boardtype == BOARD_TYPE_NORMAL) 122 return true; 123 if (boardtype == BOARD_TYPE_RPI) { 124 for (i = 0; i < install->num; i++) 125 if (install->infos[i].fs_type == FS_MSDOS) 126 return true; 127 128 msg_display(MSG_nomsdospart); 129 process_menu(MENU_ok, NULL); 130 } 131 return false; 132 } 133 134 /* 135 * hook called before writing new disklabel. 136 */ 137 bool 138 md_pre_disklabel(struct install_partition_desc *install, 139 struct disk_partitions *parts) 140 { 141 142 if (parts->parent == NULL) 143 return true; /* no outer partitions */ 144 145 parts = parts->parent; 146 147 msg_display_subst(MSG_dofdisk, 3, parts->disk, 148 msg_string(parts->pscheme->name), 149 msg_string(parts->pscheme->short_name)); 150 151 /* write edited "MBR" onto disk. */ 152 if (!parts->pscheme->write_to_disk(parts)) { 153 msg_display(MSG_wmbrfail); 154 process_menu(MENU_ok, NULL); 155 return false; 156 } 157 return true; 158 } 159 160 /* 161 * hook called after writing disklabel to new target disk. 162 */ 163 bool 164 md_post_disklabel(struct install_partition_desc *install, 165 struct disk_partitions *parts) 166 { 167 return true; 168 } 169 170 /* 171 * hook called after upgrade() or install() has finished setting 172 * up the target disk but immediately before the user is given the 173 * ``disks are now set up'' message. 174 */ 175 int 176 md_post_newfs(struct install_partition_desc *install) 177 { 178 return 0; 179 } 180 181 int 182 md_post_extract(struct install_partition_desc *install) 183 { 184 char kernelbin[100]; 185 186 if (boardtype == BOARD_TYPE_NORMAL) 187 return 0; 188 if (boardtype == BOARD_TYPE_RPI) { 189 snprintf(kernelbin, 100, "%s/netbsd.img", targetroot_mnt); 190 if (file_exists_p(kernelbin)) { 191 run_program(RUN_DISPLAY, 192 "/bin/cp %s /targetroot/boot/kernel.img", kernelbin); 193 } else { 194 msg_display(MSG_rpikernelmissing); 195 process_menu(MENU_ok, NULL); 196 return 1; 197 } 198 } 199 return 0; 200 } 201 202 void 203 md_cleanup_install(struct install_partition_desc *install) 204 { 205 #ifndef DEBUG 206 enable_rc_conf(); 207 add_rc_conf("sshd=YES\n"); 208 add_rc_conf("dhcpcd=YES\n"); 209 #endif 210 } 211 212 int 213 md_pre_update(struct install_partition_desc *install) 214 { 215 return 1; 216 } 217 218 /* Upgrade support */ 219 int 220 md_update(struct install_partition_desc *install) 221 { 222 md_post_newfs(install); 223 return 1; 224 } 225 226 int 227 md_pre_mount(struct install_partition_desc *install, size_t ndx) 228 { 229 return 0; 230 } 231 232 int 233 md_check_mbr(struct disk_partitions *parts, mbr_info_t *mbri, bool quiet) 234 { 235 mbr_info_t *ext; 236 struct mbr_partition *part; 237 int i, hasboot=0; 238 239 if (boardtype == BOARD_TYPE_NORMAL) 240 return 2; 241 /* raspi code */ 242 if (boardtype == BOARD_TYPE_RPI) { 243 for (ext = mbri; ext; ext = ext->extended) { 244 part = ext->mbr.mbr_parts; 245 for (i=0, hasboot=0; i < MBR_PART_COUNT; part++, i++) { 246 if (part->mbrp_type != MBR_PTYPE_FAT16L && 247 part->mbrp_type != MBR_PTYPE_FAT32L) 248 continue; 249 hasboot = 1; 250 break; 251 } 252 } 253 if (!hasboot) { 254 if (quiet) 255 return 2; 256 msg_display(MSG_nomsdospart); 257 return ask_reedit(parts); 258 } 259 } 260 return 2; 261 } 262 263 bool 264 md_parts_use_wholedisk(struct disk_partitions *parts) 265 { 266 part_id nbsd, boot; 267 struct disk_part_info info; 268 daddr_t offset; 269 270 /* 271 * XXX - set (U)EFI install depending on boardtype 272 */ 273 274 if (boardtype == BOARD_TYPE_NORMAL) { 275 /* this keeps it from creating /boot as msdos */ 276 pm->bootsize = 0; 277 return parts_use_wholedisk(parts, 0, NULL); 278 } 279 280 /* raspi code */ 281 if (boardtype == BOARD_TYPE_RPI) { 282 283 for (boot = 0; boot < parts->num_part; boot++) { 284 if (!parts->pscheme->get_part_info(parts, boot, &info)) 285 continue; 286 if (info.nat_type == NULL) 287 continue; 288 if (info.nat_type->generic_ptype == PT_FAT) 289 break; 290 } 291 292 if (boot >= parts->num_part) { 293 /* It's hopelessly corrupt, punt for now */ 294 msg_display(MSG_nomsdospart); 295 process_menu(MENU_ok, NULL); 296 return false; 297 } 298 pm->bootstart = info.start; 299 pm->bootsize = info.size; 300 offset = info.start + info.size + 1; 301 memset(&info, 0, sizeof info); 302 info.start = offset; 303 info.size = parts->pscheme->max_free_space_at(parts, offset); 304 info.nat_type = parts->pscheme->get_generic_part_type(PT_root); 305 info.fs_type = FS_BSDFFS; 306 info.fs_sub_type = 2; 307 308 nbsd = parts->pscheme->add_partition(parts, &info, NULL); 309 if (nbsd == NO_PART) 310 return false; 311 312 parts->pscheme->get_part_info(parts, nbsd, &info); 313 pm->ptstart = info.start; 314 pm->ptsize = info.size; 315 return true; 316 } 317 318 return parts_use_wholedisk(parts, 0, NULL); 319 } 320 321 /* returns false if no write-back of parts is required */ 322 bool 323 md_mbr_update_check(struct disk_partitions *parts, mbr_info_t *mbri) 324 { 325 return false; 326 } 327 328 #ifdef HAVE_GPT 329 /* 330 * New GPT partitions have been written, update bootloader or remember 331 * data untill needed in md_post_newfs 332 */ 333 bool 334 md_gpt_post_write(struct disk_partitions *parts, part_id root_id, 335 bool root_is_new, part_id efi_id, bool efi_is_new) 336 { 337 return true; 338 } 339 #endif 340