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