1 1.13 tsutsui /* $NetBSD: md.c,v 1.13 2022/06/24 22:28:11 tsutsui Exp $ */ 2 1.1 dholland 3 1.1 dholland /* 4 1.1 dholland * Copyright 1997 Piermont Information Systems Inc. 5 1.1 dholland * All rights reserved. 6 1.1 dholland * 7 1.1 dholland * Based on code written by Philip A. Nelson for Piermont Information 8 1.1 dholland * Systems Inc. Modified by Minoura Makoto for x68k. 9 1.1 dholland * 10 1.1 dholland * Redistribution and use in source and binary forms, with or without 11 1.1 dholland * modification, are permitted provided that the following conditions 12 1.1 dholland * are met: 13 1.1 dholland * 1. Redistributions of source code must retain the above copyright 14 1.1 dholland * notice, this list of conditions and the following disclaimer. 15 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 dholland * notice, this list of conditions and the following disclaimer in the 17 1.1 dholland * documentation and/or other materials provided with the distribution. 18 1.1 dholland * 3. The name of Piermont Information Systems Inc. may not be used to endorse 19 1.1 dholland * or promote products derived from this software without specific prior 20 1.1 dholland * written permission. 21 1.1 dholland * 22 1.1 dholland * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS'' 23 1.1 dholland * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 1.1 dholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 1.1 dholland * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE 26 1.1 dholland * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 1.1 dholland * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 1.1 dholland * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 1.1 dholland * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 1.1 dholland * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 1.1 dholland * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 32 1.1 dholland * THE POSSIBILITY OF SUCH DAMAGE. 33 1.1 dholland */ 34 1.1 dholland 35 1.1 dholland /* md.c -- x68k machine specific routines */ 36 1.1 dholland /* This file is in close sync with pmax, sparc, and vax md.c */ 37 1.1 dholland 38 1.1 dholland #include <stdio.h> 39 1.1 dholland #include <unistd.h> 40 1.1 dholland #include <sys/ioctl.h> 41 1.1 dholland #include <sys/param.h> 42 1.1 dholland #include <util.h> 43 1.1 dholland 44 1.1 dholland #include "defs.h" 45 1.1 dholland #include "md.h" 46 1.1 dholland #include "msg_defs.h" 47 1.1 dholland #include "menu_defs.h" 48 1.1 dholland 49 1.1 dholland #ifdef notyet 50 1.1 dholland #undef NDOSPART 8 51 1.1 dholland #define NDOSPART 16 52 1.1 dholland typedef struct parttab { 53 1.1 dholland struct dos_partition dosparts[NDOSPART]; 54 1.1 dholland } parttab; 55 1.1 dholland 56 1.1 dholland parttab md_disklabel; 57 1.1 dholland int md_freepart; 58 1.1 dholland int md_nfreepart; 59 1.1 dholland #endif /* notyet */ 60 1.1 dholland 61 1.1 dholland int md_need_newdisk = 0; 62 1.1 dholland 63 1.1 dholland /* prototypes */ 64 1.1 dholland static int md_newdisk(void); 65 1.1 dholland 66 1.1 dholland void 67 1.1 dholland md_init(void) 68 1.1 dholland { 69 1.1 dholland } 70 1.1 dholland 71 1.1 dholland void 72 1.1 dholland md_init_set_status(int flags) 73 1.1 dholland { 74 1.1 dholland (void)flags; 75 1.1 dholland } 76 1.1 dholland 77 1.6 martin bool 78 1.6 martin md_get_info(struct install_partition_desc *install) 79 1.1 dholland { 80 1.1 dholland char buf[1024]; 81 1.1 dholland int fd; 82 1.1 dholland char dev_name[100]; 83 1.1 dholland struct disklabel disklabel; 84 1.1 dholland 85 1.2 martin snprintf(dev_name, 100, "/dev/r%sc", pm->diskdev); 86 1.1 dholland 87 1.1 dholland fd = open(dev_name, O_RDONLY, 0); 88 1.1 dholland if (fd < 0) { 89 1.1 dholland if (logfp) 90 1.1 dholland (void)fprintf(logfp, "Can't open %s\n", dev_name); 91 1.1 dholland endwin(); 92 1.1 dholland fprintf(stderr, "Can't open %s\n", dev_name); 93 1.1 dholland exit(1); 94 1.1 dholland } 95 1.1 dholland if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) { 96 1.1 dholland if (logfp) 97 1.1 dholland (void)fprintf(logfp, "Can't read disklabel on %s.\n", 98 1.1 dholland dev_name); 99 1.1 dholland endwin(); 100 1.1 dholland fprintf(stderr, "Can't read disklabel on %s.\n", dev_name); 101 1.1 dholland close(fd); 102 1.1 dholland exit(1); 103 1.1 dholland } 104 1.1 dholland if (disklabel.d_secsize != 512) { 105 1.1 dholland endwin(); 106 1.1 dholland fprintf(stderr, "Non-512byte/sector disk is not supported.\n"); 107 1.1 dholland close(fd); 108 1.1 dholland exit(1); 109 1.1 dholland } 110 1.1 dholland 111 1.2 martin pm->dlcyl = disklabel.d_ncylinders; 112 1.2 martin pm->dlhead = disklabel.d_ntracks; 113 1.2 martin pm->dlsec = disklabel.d_nsectors; 114 1.2 martin pm->sectorsize = disklabel.d_secsize; 115 1.2 martin pm->dlcylsize = disklabel.d_secpercyl; 116 1.2 martin pm->dlsize = pm->dlcyl*pm->dlhead*pm->dlsec; 117 1.1 dholland 118 1.1 dholland if (read(fd, buf, 1024) < 0) { 119 1.1 dholland endwin(); 120 1.1 dholland fprintf(stderr, "Can't read %s\n", dev_name); 121 1.1 dholland close(fd); 122 1.1 dholland exit(1); 123 1.1 dholland } 124 1.1 dholland if (memcmp(buf, "X68SCSI1", 8) != 0) 125 1.1 dholland md_need_newdisk = 1; 126 1.1 dholland #ifdef notyet 127 1.1 dholland else 128 1.1 dholland if (read(fd, md_disklabel, sizeof(md_disklabel)) < 0) { 129 1.1 dholland endwin(); 130 1.1 dholland fprintf(stderr, "Can't read %s\n", dev_name); 131 1.1 dholland close(fd); 132 1.1 dholland exit(1); 133 1.1 dholland } 134 1.1 dholland #endif 135 1.1 dholland /* preserve first 64 sectors for system. */ 136 1.2 martin pm->ptstart = 64; 137 1.1 dholland 138 1.1 dholland /* preserve existing partitions? */ 139 1.1 dholland 140 1.1 dholland close(fd); 141 1.1 dholland 142 1.6 martin return true; 143 1.1 dholland } 144 1.1 dholland 145 1.1 dholland /* 146 1.1 dholland * md back-end code for menu-driven BSD disklabel editor. 147 1.1 dholland */ 148 1.11 martin int 149 1.6 martin md_make_bsd_partitions(struct install_partition_desc *install) 150 1.1 dholland { 151 1.6 martin return make_bsd_partitions(install); 152 1.1 dholland } 153 1.1 dholland 154 1.1 dholland /* 155 1.1 dholland * any additional partition validation 156 1.1 dholland */ 157 1.6 martin bool 158 1.6 martin md_check_partitions(struct install_partition_desc *install) 159 1.1 dholland { 160 1.1 dholland /* X68k partitions must be in order of the range. */ 161 1.6 martin daddr_t last_end = 0; 162 1.6 martin size_t i; 163 1.6 martin char desc[STRSIZE]; 164 1.6 martin 165 1.6 martin for (i = 0; i < install->num; i++) { 166 1.6 martin if (i > 0) { 167 1.10 martin /* skip raw part and similar */ 168 1.10 martin if (install->infos[i].cur_flags & 169 1.10 martin (PTI_SEC_CONTAINER|PTI_PSCHEME_INTERNAL| 170 1.10 martin PTI_RAW_PART)) 171 1.10 martin continue; 172 1.10 martin 173 1.6 martin if (install->infos[i].cur_start < last_end) { 174 1.6 martin snprintf(desc, sizeof desc, 175 1.6 martin "%zu (%s)", i, 176 1.6 martin install->infos[i].mount); 177 1.7 christos msg_fmt_display(MSG_ordering, "%s", desc); 178 1.4 martin if (ask_yesno(NULL)) 179 1.6 martin return false; 180 1.1 dholland } 181 1.1 dholland } 182 1.6 martin last_end = install->infos[i].cur_start + install->infos[i].size; 183 1.1 dholland } 184 1.1 dholland 185 1.6 martin return true; 186 1.1 dholland } 187 1.1 dholland 188 1.1 dholland #ifdef notyet 189 1.1 dholland static int 190 1.1 dholland md_check_partitions(void) 191 1.1 dholland { 192 1.1 dholland int i, j; 193 1.1 dholland int preserve; 194 1.1 dholland 195 1.1 dholland /* check existing BSD partitions. */ 196 1.1 dholland for (i = 0; i < NDOSPART; i++) { 197 1.1 dholland if (md_disklabel.dosparts[i].dp_size == 0) 198 1.1 dholland break; 199 1.1 dholland if (memcmp(md_disklabel.dosparts[i].dp_typename, "Human68k", 8)) { 200 1.1 dholland msg_display(MSG_existing); 201 1.4 martin preserve = ask_noyes(NULL); 202 1.1 dholland break; 203 1.1 dholland } 204 1.1 dholland } 205 1.2 martin emptylabel(pm->bsdlabel); 206 1.2 martin pm->bsdlabel[C].pi_fstype = FS_UNUSED; 207 1.2 martin pm->bsdlabel[C].pi_offset = 0; 208 1.2 martin pm->bsdlabel[C].pi_size = pm->dlsize; 209 1.1 dholland for (i = 0, j = A; i < NDOSPART;) { 210 1.1 dholland if (j == C) { 211 1.1 dholland j++; 212 1.1 dholland continue; 213 1.1 dholland } 214 1.1 dholland if (!preserve && 215 1.1 dholland memcmp(md_disklabel.dosparts[i].dp_typename, 216 1.1 dholland "Human68k", 8)) { 217 1.1 dholland /* discard it. */ 218 1.1 dholland i++; 219 1.1 dholland continue; 220 1.1 dholland } 221 1.2 martin pm->bsdlabel[j].pi_fstype = (i == 1) ? FS_SWAP : FS_BSDFFS; 222 1.2 martin pm->bsdlabel[j].pi_offset = md_disklabel.dosparts[i].dp_start*2; 223 1.2 martin pm->bsdlabel[j].pi_size = md_disklabel.dosparts[i].dp_size*2; 224 1.1 dholland i++; 225 1.1 dholland j++; 226 1.1 dholland } 227 1.1 dholland if (j > 6) { 228 1.7 christos msg_fmt_display(MSG_nofreepart, "%s" pm->diskdev); 229 1.1 dholland return 0; 230 1.1 dholland } 231 1.1 dholland md_nfreepart = 8 - j; 232 1.1 dholland 233 1.1 dholland /* check for free space */ 234 1.2 martin fspm->ptsize = pm->bsdlabel[A].pi_offset - 64; 235 1.2 martin if (fpm->ptsize <= 0) { /* XXX: should not be 0; minfsdb? */ 236 1.7 christos msg_fmt_display(MSG_notfirst, "%s", pm->diskdev); 237 1.1 dholland process_menu(MENU_ok); 238 1.1 dholland exit(1); 239 1.1 dholland } 240 1.1 dholland 241 1.1 dholland /* Partitions should be preserved in md_make_bsdpartitions() */ 242 1.1 dholland } 243 1.1 dholland #endif /* notyet */ 244 1.1 dholland 245 1.1 dholland /* 246 1.1 dholland * hook called before writing new disklabel. 247 1.1 dholland */ 248 1.6 martin bool 249 1.6 martin md_pre_disklabel(struct install_partition_desc *install, 250 1.6 martin struct disk_partitions *parts) 251 1.1 dholland { 252 1.1 dholland if (md_need_newdisk) 253 1.1 dholland md_newdisk (); 254 1.6 martin return true; 255 1.1 dholland } 256 1.1 dholland 257 1.1 dholland /* 258 1.1 dholland * hook called after writing disklabel to new target disk. 259 1.1 dholland */ 260 1.6 martin bool 261 1.6 martin md_post_disklabel(struct install_partition_desc *install, 262 1.6 martin struct disk_partitions *parts) 263 1.1 dholland { 264 1.6 martin return true; 265 1.1 dholland } 266 1.1 dholland 267 1.13 tsutsui #ifdef DISKLABEL_NO_ONDISK_VERIFY 268 1.13 tsutsui /* 269 1.13 tsutsui * hook to check if disklabel returned by readdisklabel(9) via DIOCGDINFO 270 1.13 tsutsui * seems the default one, on ports that have no BSD disklabel on disks. 271 1.13 tsutsui */ 272 1.13 tsutsui bool 273 1.13 tsutsui md_disklabel_is_default(const struct disklabel *lp) 274 1.13 tsutsui { 275 1.13 tsutsui bool maybe_default = 276 1.13 tsutsui lp->d_npartitions == RAW_PART + 1 && 277 1.13 tsutsui lp->d_partitions[0].p_size == lp->d_partitions[RAW_PART].p_size && 278 1.13 tsutsui lp->d_partitions[0].p_fstype == FS_UNUSED && 279 1.13 tsutsui lp->d_bbsize == 0 && 280 1.13 tsutsui lp->d_sbsize == 0; 281 1.13 tsutsui 282 1.13 tsutsui return maybe_default; 283 1.13 tsutsui } 284 1.13 tsutsui #endif 285 1.13 tsutsui 286 1.1 dholland /* 287 1.1 dholland * hook called after upgrade() or install() has finished setting 288 1.1 dholland * up the target disk but immediately before the user is given the 289 1.1 dholland * ``disks are now set up'' message. 290 1.1 dholland * 291 1.1 dholland * On the x68k, we use this opportunity to install the boot blocks. 292 1.1 dholland */ 293 1.1 dholland int 294 1.6 martin md_post_newfs(struct install_partition_desc *install) 295 1.1 dholland { 296 1.1 dholland /* boot blocks ... */ 297 1.7 christos msg_fmt_display(MSG_dobootblks, "%s", pm->diskdev); 298 1.1 dholland cp_to_target("/usr/mdec/boot", "/boot"); 299 1.1 dholland if (run_program(RUN_DISPLAY | RUN_NO_CLEAR, 300 1.1 dholland "/usr/mdec/installboot.new /usr/mdec/sdboot_ufs /dev/r%sa", 301 1.2 martin pm->diskdev)) 302 1.1 dholland process_menu(MENU_ok, 303 1.5 joerg __UNCONST("Warning: disk is probably not bootable")); 304 1.9 martin 305 1.9 martin wclear(stdscr); 306 1.9 martin touchwin(stdscr); 307 1.9 martin clearok(stdscr, 1); 308 1.9 martin refresh(); 309 1.9 martin 310 1.1 dholland return 0; 311 1.1 dholland } 312 1.1 dholland 313 1.1 dholland int 314 1.12 martin md_post_extract(struct install_partition_desc *install, bool upgrade) 315 1.1 dholland { 316 1.1 dholland return 0; 317 1.1 dholland } 318 1.1 dholland 319 1.1 dholland void 320 1.6 martin md_cleanup_install(struct install_partition_desc *install) 321 1.1 dholland { 322 1.1 dholland #ifndef DEBUG 323 1.1 dholland enable_rc_conf(); 324 1.1 dholland #endif 325 1.1 dholland } 326 1.1 dholland 327 1.1 dholland int 328 1.6 martin md_pre_update(struct install_partition_desc *install) 329 1.1 dholland { 330 1.1 dholland return 1; 331 1.1 dholland } 332 1.1 dholland 333 1.1 dholland /* Upgrade support */ 334 1.1 dholland int 335 1.6 martin md_update(struct install_partition_desc *install) 336 1.1 dholland { 337 1.6 martin md_post_newfs(install); 338 1.1 dholland return 1; 339 1.1 dholland } 340 1.1 dholland 341 1.1 dholland static int 342 1.1 dholland md_newdisk(void) 343 1.1 dholland { 344 1.7 christos msg_fmt_display(MSG_newdisk, "%s%s", pm->diskdev, pm->diskdev); 345 1.1 dholland 346 1.1 dholland return run_program(RUN_FATAL|RUN_DISPLAY, 347 1.2 martin "/usr/mdec/newdisk -v %s", pm->diskdev); 348 1.1 dholland } 349 1.1 dholland 350 1.1 dholland 351 1.1 dholland int 352 1.8 martin md_pre_mount(struct install_partition_desc *install, size_t ndx) 353 1.1 dholland { 354 1.1 dholland return 0; 355 1.1 dholland } 356 1.6 martin 357 1.6 martin bool 358 1.6 martin md_parts_use_wholedisk(struct disk_partitions *parts) 359 1.6 martin { 360 1.6 martin return parts_use_wholedisk(parts, 0, NULL); 361 1.6 martin } 362 1.6 martin 363 1.6 martin #ifdef HAVE_GPT 364 1.6 martin bool 365 1.6 martin md_gpt_post_write(struct disk_partitions *parts, part_id root_id, 366 1.6 martin bool root_is_new, part_id efi_id, bool efi_is_new) 367 1.6 martin { 368 1.6 martin /* no GPT boot support, nothing needs to be done here */ 369 1.6 martin return true; 370 1.6 martin } 371 1.6 martin #endif 372 1.6 martin 373