1 /* $NetBSD: md.c,v 1.2 2014/08/03 16:09:39 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 -- shark machine specific routines */ 36 37 #include <stdio.h> 38 #include <curses.h> 39 #include <unistd.h> 40 #include <fcntl.h> 41 #include <util.h> 42 #include <sys/types.h> 43 #include <sys/disklabel.h> 44 #include <sys/ioctl.h> 45 #include <sys/param.h> 46 47 #include "defs.h" 48 #include "md.h" 49 #include "msg_defs.h" 50 #include "menu_defs.h" 51 52 int boardtype = 0, rpi_bootpart = PART_A; 53 54 void 55 md_prelim_menu(void) 56 { 57 /* get the boardtype from the user */ 58 process_menu(MENU_prelim, NULL); 59 } 60 61 void 62 md_init(void) 63 { 64 } 65 66 void 67 md_init_set_status(int flags) 68 { 69 if (boardtype == BOARD_TYPE_RPI) 70 set_kernel_set(SET_KERNEL_RPI); 71 } 72 73 int 74 md_get_info(void) 75 { 76 struct disklabel disklabel; 77 int fd; 78 char dev_name[100]; 79 80 if (boardtype == BOARD_TYPE_RPI) 81 return set_bios_geom_with_mbr_guess(); 82 83 if (pm->no_mbr) 84 return 1; 85 86 if (read_mbr(pm->diskdev, &mbr) < 0) 87 memset(&mbr.mbr, 0, sizeof(mbr.mbr)-2); 88 89 if (edit_mbr(&mbr) == 0) 90 return 0; 91 92 if (strncmp(pm->diskdev, "wd", 2) == 0) 93 pm->disktype = "ST506"; 94 else 95 pm->disktype = "SCSI"; 96 97 snprintf(dev_name, 100, "/dev/r%sc", pm->diskdev); 98 99 fd = open(dev_name, O_RDONLY, 0); 100 if (fd < 0) { 101 endwin(); 102 fprintf(stderr, "Can't open %s\n", dev_name); 103 exit(1); 104 } 105 if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) { 106 endwin(); 107 fprintf(stderr, "Can't read disklabel on %s.\n", dev_name); 108 close(fd); 109 exit(1); 110 } 111 close(fd); 112 113 pm->dlcyl = disklabel.d_ncylinders; 114 pm->dlhead = disklabel.d_ntracks; 115 pm->dlsec = disklabel.d_nsectors; 116 pm->sectorsize = disklabel.d_secsize; 117 pm->dlcylsize = disklabel.d_secpercyl; 118 119 /* 120 * Compute whole disk size. Take max of (pm->dlcyl*pm->dlhead*pm->dlsec) 121 * and secperunit, just in case the disk is already labelled. 122 * (If our new label's RAW_PART size ends up smaller than the 123 * in-core RAW_PART size value, updating the label will fail.) 124 */ 125 pm->dlsize = pm->dlcyl*pm->dlhead*pm->dlsec; 126 if (disklabel.d_secperunit > pm->dlsize) 127 pm->dlsize = disklabel.d_secperunit; 128 129 return 1; 130 } 131 132 /* 133 * md back-end code for menu-driven BSD disklabel editor. 134 */ 135 int 136 md_make_bsd_partitions(void) 137 { 138 return make_bsd_partitions(); 139 } 140 141 /* 142 * any additional partition validation 143 */ 144 int 145 md_check_partitions(void) 146 { 147 int part; 148 149 if (boardtype == BOARD_TYPE_NORMAL) 150 return 1; 151 if (boardtype == BOARD_TYPE_RPI) { 152 for (part = PART_A; part < MAXPARTITIONS; part++) 153 if (pm->bsdlabel[part].pi_fstype == FS_MSDOS) { 154 rpi_bootpart = part; 155 return 1; 156 } 157 158 msg_display(MSG_nomsdospart); 159 process_menu(MENU_ok, NULL); 160 } 161 return 0; 162 } 163 164 /* 165 * hook called before writing new disklabel. 166 */ 167 int 168 md_pre_disklabel(void) 169 { 170 if (pm->no_mbr) 171 return 0; 172 173 msg_display(MSG_dofdisk); 174 175 /* write edited MBR onto disk. */ 176 if (write_mbr(pm->diskdev, &mbr, 1) != 0) { 177 msg_display(MSG_wmbrfail); 178 process_menu(MENU_ok, NULL); 179 return 1; 180 } 181 return 0; 182 } 183 184 /* 185 * hook called after writing disklabel to new target disk. 186 */ 187 int 188 md_post_disklabel(void) 189 { 190 return 0; 191 } 192 193 /* 194 * hook called after upgrade() or install() has finished setting 195 * up the target disk but immediately before the user is given the 196 * ``disks are now set up'' message. 197 */ 198 int 199 md_post_newfs(void) 200 { 201 return 0; 202 } 203 204 int 205 md_post_extract(void) 206 { 207 char kernelbin[100]; 208 209 if (boardtype == BOARD_TYPE_NORMAL) 210 return 0; 211 if (boardtype == BOARD_TYPE_RPI) { 212 snprintf(kernelbin, 100, "%s/netbsd.bin", targetroot_mnt); 213 if (file_exists_p(kernelbin)) { 214 run_program(RUN_DISPLAY, 215 "/bin/cp %s /targetroot/boot/kernel.img", kernelbin); 216 } else { 217 msg_display(MSG_rpikernelmissing); 218 process_menu(MENU_ok, NULL); 219 return 1; 220 } 221 } 222 return 0; 223 } 224 225 void 226 md_cleanup_install(void) 227 { 228 #ifndef DEBUG 229 enable_rc_conf(); 230 add_rc_conf("sshd=YES\n"); 231 add_rc_conf("dhcpcd=YES\n"); 232 #endif 233 } 234 235 int 236 md_pre_update(void) 237 { 238 return 1; 239 } 240 241 /* Upgrade support */ 242 int 243 md_update(void) 244 { 245 md_post_newfs(); 246 return 1; 247 } 248 249 int 250 md_pre_mount() 251 { 252 return 0; 253 } 254 255 int 256 md_check_mbr(mbr_info_t *mbri) 257 { 258 mbr_info_t *ext; 259 struct mbr_partition *part; 260 int i, hasboot=0; 261 262 if (boardtype == BOARD_TYPE_NORMAL) 263 return 2; 264 /* raspi code */ 265 if (boardtype == BOARD_TYPE_RPI) { 266 for (ext = mbri; ext; ext = ext->extended) { 267 part = ext->mbr.mbr_parts; 268 for (i=0, hasboot=0; i < MBR_PART_COUNT; part++, i++) { 269 if (part->mbrp_type != MBR_PTYPE_FAT32L) 270 continue; 271 hasboot = 1; 272 break; 273 } 274 } 275 if (!hasboot) { 276 msg_display(MSG_nomsdospart); 277 msg_display_add(MSG_reeditpart, 0); 278 process_menu(MENU_yesno, NULL); 279 if (!yesno) 280 return 0; 281 return 1; 282 } 283 } 284 return 2; 285 } 286 287 int 288 md_mbr_use_wholedisk(mbr_info_t *mbri) 289 { 290 struct mbr_sector *mbrs = &mbri->mbr; 291 struct mbr_partition *part; 292 int offset; 293 294 if (boardtype == BOARD_TYPE_NORMAL) { 295 /* this keeps it from creating /boot as msdos */ 296 pm->bootsize = 0; 297 return mbr_use_wholedisk(mbri); 298 } 299 300 /* raspi code */ 301 if (boardtype == BOARD_TYPE_RPI) { 302 part = &mbrs->mbr_parts[0]; 303 if (part[0].mbrp_type != MBR_PTYPE_FAT32L) { 304 /* It's hopelessly corrupt, punt for now */ 305 msg_display(MSG_nomsdospart); 306 process_menu(MENU_ok, NULL); 307 return 0; 308 } 309 offset = part[0].mbrp_start + part[0].mbrp_size; 310 part[1].mbrp_type = MBR_PTYPE_NETBSD; 311 part[1].mbrp_size = pm->dlsize - offset; 312 part[1].mbrp_start = offset; 313 part[1].mbrp_flag = 0; 314 315 pm->ptstart = part[1].mbrp_start; 316 pm->ptsize = part[1].mbrp_size; 317 pm->bootstart = part[0].mbrp_start; 318 pm->bootsize = part[0].mbrp_size; 319 return 1; 320 } 321 322 return mbr_use_wholedisk(mbri); 323 } 324