1 /* $NetBSD: md.c,v 1.4 2015/05/10 10:14:03 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. Modified by Minoura Makoto for x68k. 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 -- x68k machine specific routines */ 36 /* This file is in close sync with pmax, sparc, and vax md.c */ 37 38 #include <stdio.h> 39 #include <unistd.h> 40 #include <sys/disklabel.h> 41 #include <sys/ioctl.h> 42 #include <sys/param.h> 43 #include <util.h> 44 45 #include "defs.h" 46 #include "md.h" 47 #include "msg_defs.h" 48 #include "menu_defs.h" 49 50 #ifdef notyet 51 #undef NDOSPART 8 52 #define NDOSPART 16 53 typedef struct parttab { 54 struct dos_partition dosparts[NDOSPART]; 55 } parttab; 56 57 parttab md_disklabel; 58 int md_freepart; 59 int md_nfreepart; 60 #endif /* notyet */ 61 62 int md_need_newdisk = 0; 63 64 /* prototypes */ 65 static int md_newdisk(void); 66 67 void 68 md_init(void) 69 { 70 } 71 72 void 73 md_init_set_status(int flags) 74 { 75 (void)flags; 76 } 77 78 int 79 md_get_info(void) 80 { 81 char buf[1024]; 82 int fd; 83 char dev_name[100]; 84 struct disklabel disklabel; 85 86 snprintf(dev_name, 100, "/dev/r%sc", pm->diskdev); 87 88 fd = open(dev_name, O_RDONLY, 0); 89 if (fd < 0) { 90 if (logfp) 91 (void)fprintf(logfp, "Can't open %s\n", dev_name); 92 endwin(); 93 fprintf(stderr, "Can't open %s\n", dev_name); 94 exit(1); 95 } 96 if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) { 97 if (logfp) 98 (void)fprintf(logfp, "Can't read disklabel on %s.\n", 99 dev_name); 100 endwin(); 101 fprintf(stderr, "Can't read disklabel on %s.\n", dev_name); 102 close(fd); 103 exit(1); 104 } 105 if (disklabel.d_secsize != 512) { 106 endwin(); 107 fprintf(stderr, "Non-512byte/sector disk is not supported.\n"); 108 close(fd); 109 exit(1); 110 } 111 112 pm->dlcyl = disklabel.d_ncylinders; 113 pm->dlhead = disklabel.d_ntracks; 114 pm->dlsec = disklabel.d_nsectors; 115 pm->sectorsize = disklabel.d_secsize; 116 pm->dlcylsize = disklabel.d_secpercyl; 117 pm->dlsize = pm->dlcyl*pm->dlhead*pm->dlsec; 118 119 if (read(fd, buf, 1024) < 0) { 120 endwin(); 121 fprintf(stderr, "Can't read %s\n", dev_name); 122 close(fd); 123 exit(1); 124 } 125 if (memcmp(buf, "X68SCSI1", 8) != 0) 126 md_need_newdisk = 1; 127 #ifdef notyet 128 else 129 if (read(fd, md_disklabel, sizeof(md_disklabel)) < 0) { 130 endwin(); 131 fprintf(stderr, "Can't read %s\n", dev_name); 132 close(fd); 133 exit(1); 134 } 135 #endif 136 /* preserve first 64 sectors for system. */ 137 pm->ptstart = 64; 138 139 /* preserve existing partitions? */ 140 141 close(fd); 142 143 return 1; 144 } 145 146 /* 147 * md back-end code for menu-driven BSD disklabel editor. 148 */ 149 int 150 md_make_bsd_partitions(void) 151 { 152 return(make_bsd_partitions()); 153 } 154 155 /* 156 * any additional partition validation 157 */ 158 int 159 md_check_partitions(void) 160 { 161 /* X68k partitions must be in order of the range. */ 162 int part, last = PART_A-1; 163 uint32_t start = 0; 164 165 for (part = PART_A; part < 8; part++) { 166 if (part == PART_C) 167 continue; 168 if (last >= PART_A && pm->bsdlabel[part].pi_size > 0) { 169 msg_display(MSG_emptypart, part+'a'); 170 process_menu(MENU_ok, NULL); 171 return 0; 172 } 173 if (pm->bsdlabel[part].pi_size == 0) { 174 if (last < PART_A) 175 last = part; 176 } else { 177 if (start >= pm->bsdlabel[part].pi_offset) { 178 msg_display(MSG_ordering, part+'a'); 179 if (ask_yesno(NULL)) 180 return 0; 181 } 182 start = pm->bsdlabel[part].pi_offset; 183 } 184 } 185 186 return 1; 187 } 188 189 #ifdef notyet 190 static int 191 md_check_partitions(void) 192 { 193 int i, j; 194 int preserve; 195 196 /* check existing BSD partitions. */ 197 for (i = 0; i < NDOSPART; i++) { 198 if (md_disklabel.dosparts[i].dp_size == 0) 199 break; 200 if (memcmp(md_disklabel.dosparts[i].dp_typename, "Human68k", 8)) { 201 msg_display(MSG_existing); 202 preserve = ask_noyes(NULL); 203 break; 204 } 205 } 206 emptylabel(pm->bsdlabel); 207 pm->bsdlabel[C].pi_fstype = FS_UNUSED; 208 pm->bsdlabel[C].pi_offset = 0; 209 pm->bsdlabel[C].pi_size = pm->dlsize; 210 for (i = 0, j = A; i < NDOSPART;) { 211 if (j == C) { 212 j++; 213 continue; 214 } 215 if (!preserve && 216 memcmp(md_disklabel.dosparts[i].dp_typename, 217 "Human68k", 8)) { 218 /* discard it. */ 219 i++; 220 continue; 221 } 222 pm->bsdlabel[j].pi_fstype = (i == 1) ? FS_SWAP : FS_BSDFFS; 223 pm->bsdlabel[j].pi_offset = md_disklabel.dosparts[i].dp_start*2; 224 pm->bsdlabel[j].pi_size = md_disklabel.dosparts[i].dp_size*2; 225 i++; 226 j++; 227 } 228 if (j > 6) { 229 msg_display(MSG_nofreepart, pm->diskdev); 230 return 0; 231 } 232 md_nfreepart = 8 - j; 233 234 /* check for free space */ 235 fspm->ptsize = pm->bsdlabel[A].pi_offset - 64; 236 if (fpm->ptsize <= 0) { /* XXX: should not be 0; minfsdb? */ 237 msg_display(MSG_notfirst, pm->diskdev); 238 process_menu(MENU_ok); 239 exit(1); 240 } 241 242 /* Partitions should be preserved in md_make_bsdpartitions() */ 243 } 244 #endif /* notyet */ 245 246 /* 247 * hook called before writing new disklabel. 248 */ 249 int 250 md_pre_disklabel(void) 251 { 252 if (md_need_newdisk) 253 md_newdisk (); 254 return 0; 255 } 256 257 /* 258 * hook called after writing disklabel to new target disk. 259 */ 260 int 261 md_post_disklabel(void) 262 { 263 return 0; 264 } 265 266 /* 267 * hook called after upgrade() or install() has finished setting 268 * up the target disk but immediately before the user is given the 269 * ``disks are now set up'' message. 270 * 271 * On the x68k, we use this opportunity to install the boot blocks. 272 */ 273 int 274 md_post_newfs(void) 275 { 276 /* boot blocks ... */ 277 msg_display(MSG_dobootblks, pm->diskdev); 278 cp_to_target("/usr/mdec/boot", "/boot"); 279 if (run_program(RUN_DISPLAY | RUN_NO_CLEAR, 280 "/usr/mdec/installboot.new /usr/mdec/sdboot_ufs /dev/r%sa", 281 pm->diskdev)) 282 process_menu(MENU_ok, 283 deconst("Warning: disk is probably not bootable")); 284 return 0; 285 } 286 287 int 288 md_post_extract(void) 289 { 290 return 0; 291 } 292 293 void 294 md_cleanup_install(void) 295 { 296 #ifndef DEBUG 297 enable_rc_conf(); 298 #endif 299 } 300 301 int 302 md_pre_update(void) 303 { 304 return 1; 305 } 306 307 /* Upgrade support */ 308 int 309 md_update(void) 310 { 311 md_post_newfs(); 312 return 1; 313 } 314 315 static int 316 md_newdisk(void) 317 { 318 msg_display(MSG_newdisk, pm->diskdev, pm->diskdev); 319 320 return run_program(RUN_FATAL|RUN_DISPLAY, 321 "/usr/mdec/newdisk -v %s", pm->diskdev); 322 } 323 324 325 int 326 md_pre_mount() 327 { 328 return 0; 329 } 330