1 /* $NetBSD: md.c,v 1.4 2015/01/02 19:43:13 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. 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 -- luna68k machine specific routines */ 36 37 #include <sys/types.h> 38 #include <sys/disklabel.h> 39 #include <sys/ioctl.h> 40 #include <sys/param.h> 41 #include <sys/stat.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <curses.h> 45 #include <unistd.h> 46 #include <fcntl.h> 47 #include <util.h> 48 49 #include "defs.h" 50 #include "md.h" 51 #include "msg_defs.h" 52 #include "menu_defs.h" 53 54 #define PART_BOOT_BSIZE 4096 55 #define PART_BOOT_FSIZE 512 56 57 void 58 md_init(void) 59 { 60 } 61 62 void 63 md_init_set_status(int flags) 64 { 65 66 (void)flags; 67 } 68 69 int 70 md_get_info(void) 71 { 72 struct disklabel disklabel; 73 int fd; 74 char dev_name[100]; 75 76 snprintf(dev_name, sizeof(dev_name), "/dev/r%sc", pm->diskdev); 77 78 fd = open(dev_name, O_RDONLY, 0); 79 if (fd < 0) { 80 endwin(); 81 fprintf (stderr, "Can't open %s\n", dev_name); 82 exit(1); 83 } 84 if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) { 85 endwin(); 86 fprintf (stderr, "Can't read disklabel on %s.\n", dev_name); 87 close(fd); 88 exit(1); 89 } 90 close(fd); 91 92 pm->dlcyl = disklabel.d_ncylinders; 93 pm->dlhead = disklabel.d_ntracks; 94 pm->dlsec = disklabel.d_nsectors; 95 pm->sectorsize = disklabel.d_secsize; 96 pm->dlcylsize = disklabel.d_secpercyl; 97 98 /* 99 * Compute whole disk size. Take max of (pm->dlcyl*pm->dlhead*pm->dlsec) 100 * and secperunit, just in case the disk is already labelled. 101 * (If our new label's RAW_PART size ends up smaller than the 102 * in-core RAW_PART size value, updating the label will fail.) 103 */ 104 pm->dlsize = pm->dlcyl * pm->dlhead * pm->dlsec; 105 if (disklabel.d_secperunit > pm->dlsize) 106 pm->dlsize = disklabel.d_secperunit; 107 108 return 1; 109 } 110 111 /* 112 * md back-end code for menu-driven BSD disklabel editor. 113 */ 114 int 115 md_make_bsd_partitions(void) 116 { 117 118 return make_bsd_partitions(); 119 } 120 121 /* 122 * any additional partition validation 123 */ 124 int 125 md_check_partitions(void) 126 { 127 128 /* 129 * Make sure that a boot partition (old 4.3BSD UFS) is prepared 130 * properly for our native bootloader. 131 */ 132 if (pm->bsdlabel[PART_BOOT].pi_fstype != FS_BSDFFS || 133 (pm->bsdlabel[PART_BOOT].pi_flags & PIF_NEWFS) == 0) { 134 msg_display(MSG_nobootpartdisklabel); 135 process_menu(MENU_ok, NULL); 136 return 0; 137 } 138 return 1; 139 } 140 141 /* 142 * hook called before writing new disklabel. 143 */ 144 int 145 md_pre_disklabel(void) 146 { 147 148 return 0; 149 } 150 151 /* 152 * hook called after writing disklabel to new target disk. 153 */ 154 int 155 md_post_disklabel(void) 156 { 157 return 0; 158 } 159 160 static int 161 copy_bootloader(void) 162 { 163 const char *mntdir = "/mnt2"; 164 165 msg_display(MSG_copybootloader, pm->diskdev); 166 if (!run_program(RUN_SILENT | RUN_ERROR_OK, 167 "mount /dev/%s%c %s", pm->diskdev, 'a' + PART_BOOT, mntdir)) { 168 mnt2_mounted = 1; 169 run_program(0, "/bin/cp /usr/mdec/boot %s", mntdir); 170 run_program(RUN_SILENT | RUN_ERROR_OK, "umount %s", mntdir); 171 mnt2_mounted = 0; 172 } else { 173 /* XXX print proper error message */ 174 return 1; 175 } 176 return 0; 177 } 178 179 /* 180 * hook called after install() has finished setting up the target disk 181 * but immediately before the user is given the ``disks are now set up'' 182 * message. 183 */ 184 int 185 md_post_newfs(void) 186 { 187 188 if (run_program(RUN_DISPLAY | RUN_PROGRESS, 189 "/sbin/newfs -V2 -O 0 -b %d -f %d /dev/r%s%c", 190 PART_BOOT_BSIZE, PART_BOOT_FSIZE, pm->diskdev, 'a' + PART_BOOT)) 191 return 1; 192 return copy_bootloader(); 193 } 194 195 int 196 md_post_extract(void) 197 { 198 199 return 0; 200 } 201 202 void 203 md_cleanup_install(void) 204 { 205 206 #ifndef DEBUG 207 enable_rc_conf(); 208 #endif 209 msg_display(MSG_howtoboot); 210 process_menu(MENU_ok, NULL); 211 } 212 213 int 214 md_pre_update(void) 215 { 216 return 1; 217 } 218 219 /* Upgrade support */ 220 int 221 md_update(void) 222 { 223 const char *mntdir = "/mnt2"; 224 char bootpath[MAXPATHLEN]; 225 struct stat sb; 226 bool hasboot = false; 227 228 /* 229 * Check if there is a boot UFS parttion and it has the old bootloader. 230 * We'll update bootloader only if the old one was installed. 231 */ 232 if (!run_program(RUN_SILENT | RUN_ERROR_OK, 233 "mount -r /dev/%s%c %s", pm->diskdev, 'a' + PART_BOOT, mntdir)) { 234 mnt2_mounted = 1; 235 snprintf(bootpath, sizeof(bootpath), "%s/%s", mntdir, "boot"); 236 if (stat(bootpath, &sb) == 0 && S_ISREG(sb.st_mode)) 237 hasboot = true; 238 run_program(RUN_SILENT | RUN_ERROR_OK, "umount %s", mntdir); 239 mnt2_mounted = 0; 240 if (hasboot) 241 (void)copy_bootloader(); 242 } 243 return 1; 244 } 245 246 int 247 md_pre_mount() 248 { 249 return 0; 250 } 251