1 /* $NetBSD: md.c,v 1.3 2015/01/02 19:43:14 abs 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 process_menu(MENU_yesno, NULL); 180 if (yesno) 181 return 0; 182 } 183 start = pm->bsdlabel[part].pi_offset; 184 } 185 } 186 187 return 1; 188 } 189 190 #ifdef notyet 191 static int 192 md_check_partitions(void) 193 { 194 int i, j; 195 int preserve; 196 197 /* check existing BSD partitions. */ 198 for (i = 0; i < NDOSPART; i++) { 199 if (md_disklabel.dosparts[i].dp_size == 0) 200 break; 201 if (memcmp(md_disklabel.dosparts[i].dp_typename, "Human68k", 8)) { 202 msg_display(MSG_existing); 203 process_menu(MENU_noyes); 204 preserve = yesno; 205 break; 206 } 207 } 208 emptylabel(pm->bsdlabel); 209 pm->bsdlabel[C].pi_fstype = FS_UNUSED; 210 pm->bsdlabel[C].pi_offset = 0; 211 pm->bsdlabel[C].pi_size = pm->dlsize; 212 for (i = 0, j = A; i < NDOSPART;) { 213 if (j == C) { 214 j++; 215 continue; 216 } 217 if (!preserve && 218 memcmp(md_disklabel.dosparts[i].dp_typename, 219 "Human68k", 8)) { 220 /* discard it. */ 221 i++; 222 continue; 223 } 224 pm->bsdlabel[j].pi_fstype = (i == 1) ? FS_SWAP : FS_BSDFFS; 225 pm->bsdlabel[j].pi_offset = md_disklabel.dosparts[i].dp_start*2; 226 pm->bsdlabel[j].pi_size = md_disklabel.dosparts[i].dp_size*2; 227 i++; 228 j++; 229 } 230 if (j > 6) { 231 msg_display(MSG_nofreepart, pm->diskdev); 232 return 0; 233 } 234 md_nfreepart = 8 - j; 235 236 /* check for free space */ 237 fspm->ptsize = pm->bsdlabel[A].pi_offset - 64; 238 if (fpm->ptsize <= 0) { /* XXX: should not be 0; minfsdb? */ 239 msg_display(MSG_notfirst, pm->diskdev); 240 process_menu(MENU_ok); 241 exit(1); 242 } 243 244 /* Partitions should be preserved in md_make_bsdpartitions() */ 245 } 246 #endif /* notyet */ 247 248 /* 249 * hook called before writing new disklabel. 250 */ 251 int 252 md_pre_disklabel(void) 253 { 254 if (md_need_newdisk) 255 md_newdisk (); 256 return 0; 257 } 258 259 /* 260 * hook called after writing disklabel to new target disk. 261 */ 262 int 263 md_post_disklabel(void) 264 { 265 return 0; 266 } 267 268 /* 269 * hook called after upgrade() or install() has finished setting 270 * up the target disk but immediately before the user is given the 271 * ``disks are now set up'' message. 272 * 273 * On the x68k, we use this opportunity to install the boot blocks. 274 */ 275 int 276 md_post_newfs(void) 277 { 278 /* boot blocks ... */ 279 msg_display(MSG_dobootblks, pm->diskdev); 280 cp_to_target("/usr/mdec/boot", "/boot"); 281 if (run_program(RUN_DISPLAY | RUN_NO_CLEAR, 282 "/usr/mdec/installboot.new /usr/mdec/sdboot_ufs /dev/r%sa", 283 pm->diskdev)) 284 process_menu(MENU_ok, 285 deconst("Warning: disk is probably not bootable")); 286 return 0; 287 } 288 289 int 290 md_post_extract(void) 291 { 292 return 0; 293 } 294 295 void 296 md_cleanup_install(void) 297 { 298 #ifndef DEBUG 299 enable_rc_conf(); 300 #endif 301 } 302 303 int 304 md_pre_update(void) 305 { 306 return 1; 307 } 308 309 /* Upgrade support */ 310 int 311 md_update(void) 312 { 313 md_post_newfs(); 314 return 1; 315 } 316 317 static int 318 md_newdisk(void) 319 { 320 msg_display(MSG_newdisk, pm->diskdev, pm->diskdev); 321 322 return run_program(RUN_FATAL|RUN_DISPLAY, 323 "/usr/mdec/newdisk -v %s", pm->diskdev); 324 } 325 326 327 int 328 md_pre_mount() 329 { 330 return 0; 331 } 332