1 /* $NetBSD: md.c,v 1.7 2020/10/12 16:14:32 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 -- arm32 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_acorn.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 static int filecore_checksum(u_char *); 53 54 void 55 md_init(void) 56 { 57 } 58 59 void 60 md_init_set_status(int flags) 61 { 62 (void)flags; 63 } 64 65 bool 66 md_get_info(struct install_partition_desc *install) 67 { 68 struct disklabel disklabel; 69 int fd; 70 char dev_name[100]; 71 static unsigned char bb[DEV_BSIZE]; 72 struct filecore_bootblock *fcbb = (struct filecore_bootblock *)bb; 73 int offset = 0; 74 75 snprintf(dev_name, 100, "/dev/r%s%c", pm->diskdev, 'a' + getrawpartition()); 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 90 if (lseek(fd, (off_t)FILECORE_BOOT_SECTOR * DEV_BSIZE, SEEK_SET) < 0 91 || read(fd, bb, sizeof(bb)) - sizeof(bb) != 0) { 92 endwin(); 93 fprintf(stderr, "%s", msg_string(MSG_badreadbb)); 94 close(fd); 95 exit(1); 96 } 97 98 /* Check if table is valid. */ 99 if (filecore_checksum(bb) == fcbb->checksum) { 100 /* 101 * Check for NetBSD/arm32 (RiscBSD) partition marker. 102 * If found the NetBSD disklabel location is easy. 103 */ 104 105 offset = (fcbb->partition_cyl_low + 106 (fcbb->partition_cyl_high << 8)) * 107 fcbb->heads * fcbb->secspertrack; 108 109 if (fcbb->partition_type == PARTITION_FORMAT_RISCBSD) 110 ; 111 else if (fcbb->partition_type == PARTITION_FORMAT_RISCIX) { 112 /* 113 * Ok we need to read the RISCiX partition table and 114 * search for a partition named RiscBSD, NetBSD or 115 * Empty: 116 */ 117 118 struct riscix_partition_table *riscix_part = 119 (struct riscix_partition_table *)bb; 120 struct riscix_partition *part; 121 int loop; 122 123 if (lseek(fd, (off_t)offset * DEV_BSIZE, SEEK_SET) < 0 124 || read(fd, bb, sizeof(bb)) - sizeof(bb) != 0) { 125 endwin(); 126 fprintf(stderr, "%s", 127 msg_string(MSG_badreadriscix)); 128 close(fd); 129 exit(1); 130 } 131 132 /* Break out as soon as we find a suitable partition */ 133 for (loop = 0; loop < NRISCIX_PARTITIONS; ++loop) { 134 part = &riscix_part->partitions[loop]; 135 if (strcmp((char *)part->rp_name, "RiscBSD") == 0 136 || strcmp((char *)part->rp_name, "NetBSD") == 0 137 || strcmp((char *)part->rp_name, "Empty:") == 0) { 138 offset = part->rp_start; 139 break; 140 } 141 } 142 if (loop == NRISCIX_PARTITIONS) { 143 /* 144 * Valid filecore boot block, RISCiX partition 145 * table but no NetBSD partition. We should 146 * leave this disc alone. 147 */ 148 endwin(); 149 fprintf(stderr, "%s", 150 msg_string(MSG_notnetbsdriscix)); 151 close(fd); 152 exit(1); 153 } 154 } else { 155 /* 156 * Valid filecore boot block and no non-ADFS partition. 157 * This means that the whole disc is allocated for ADFS 158 * so do not trash ! If the user really wants to put a 159 * NetBSD disklabel on the disc then they should remove 160 * the filecore boot block first with dd. 161 */ 162 endwin(); 163 fprintf(stderr, "%s", msg_string(MSG_notnetbsd)); 164 close(fd); 165 exit(1); 166 } 167 } 168 close(fd); 169 170 pm->dlcyl = disklabel.d_ncylinders; 171 pm->dlhead = disklabel.d_ntracks; 172 pm->dlsec = disklabel.d_nsectors; 173 pm->sectorsize = disklabel.d_secsize; 174 pm->dlcylsize = disklabel.d_secpercyl; 175 176 /* 177 * Compute whole disk size. Take max of (pm->dlcyl*pm->dlhead*pm->dlsec) 178 * and secperunit, just in case the disk is already labelled. 179 * (If our new label's RAW_PART size ends up smaller than the 180 * in-core RAW_PART size value, updating the label will fail.) 181 */ 182 pm->dlsize = pm->dlcyl*pm->dlhead*pm->dlsec; 183 if (disklabel.d_secperunit > pm->dlsize) 184 pm->dlsize = disklabel.d_secperunit; 185 186 pm->ptstart = offset; 187 /* endwin(); 188 printf("pm->dlcyl=%d\n", pm->dlcyl); 189 printf("pm->dlhead=%d\n", pm->dlhead); 190 printf("pm->dlsec=%d\n", pm->dlsec); 191 printf("secsz=%d\n", pm->sectorsize); 192 printf("cylsz=%d\n", pm->dlcylsize); 193 printf("dlsz=%d\n", pm->dlsize); 194 printf("pstart=%d\n", pm->ptstart); 195 printf("pstart=%d\n", partsize); 196 printf("secpun=%d\n", disklabel.d_secperunit); 197 backtowin();*/ 198 199 return true; 200 } 201 202 /* 203 * md back-end code for menu-driven BSD disklabel editor. 204 */ 205 int 206 md_make_bsd_partitions(struct install_partition_desc *install) 207 { 208 return make_bsd_partitions(install); 209 } 210 211 /* 212 * any additional partition validation 213 */ 214 bool 215 md_check_partitions(struct install_partition_desc *install) 216 { 217 return 1; 218 } 219 220 /* 221 * hook called before writing new disklabel. 222 */ 223 bool 224 md_pre_disklabel(struct install_partition_desc *install, 225 struct disk_partitions *part) 226 { 227 return 0; 228 } 229 230 /* 231 * hook called after writing disklabel to new target disk. 232 */ 233 bool 234 md_post_disklabel(struct install_partition_desc *install, 235 struct disk_partitions *part) 236 { 237 return true; 238 } 239 240 /* 241 * hook called after upgrade() or install() has finished setting 242 * up the target disk but immediately before the user is given the 243 * ``disks are now set up'' message. 244 */ 245 int 246 md_post_newfs(struct install_partition_desc *install) 247 { 248 return 0; 249 } 250 251 int 252 md_post_extract(struct install_partition_desc *install) 253 { 254 return 0; 255 } 256 257 void 258 md_cleanup_install(struct install_partition_desc *install) 259 { 260 #ifndef DEBUG 261 enable_rc_conf(); 262 #endif 263 } 264 265 int 266 md_pre_update(struct install_partition_desc *install) 267 { 268 return 1; 269 } 270 271 /* Upgrade support */ 272 int 273 md_update(struct install_partition_desc *install) 274 { 275 md_post_newfs(install); 276 return 1; 277 } 278 279 /* 280 * static int filecore_checksum(u_char *bootblock) 281 * 282 * Calculates the filecore boot block checksum. This is used to validate 283 * a filecore boot block on the disk. If a boot block is validated then 284 * it is used to locate the partition table. If the boot block is not 285 * validated, it is assumed that the whole disk is NetBSD. 286 * 287 * The basic algorithm is: 288 * 289 * for (each byte in block, excluding checksum) { 290 * sum += byte; 291 * if (sum > 255) 292 * sum -= 255; 293 * } 294 * 295 * That's equivalent to summing all of the bytes in the block 296 * (excluding the checksum byte, of course), then calculating the 297 * checksum as "cksum = sum - ((sum - 1) / 255) * 255)". That 298 * expression may or may not yield a faster checksum function, 299 * but it's easier to reason about. 300 * 301 * Note that if you have a block filled with bytes of a single 302 * value "X" (regardless of that value!) and calculate the cksum 303 * of the block (excluding the checksum byte), you will _always_ 304 * end up with a checksum of X. (Do the math; that can be derived 305 * from the checksum calculation function!) That means that 306 * blocks which contain bytes which all have the same value will 307 * always checksum properly. That's a _very_ unlikely occurrence 308 * (probably impossible, actually) for a valid filecore boot block, 309 * so we treat such blocks as invalid. 310 */ 311 312 static int 313 filecore_checksum(u_char *bootblock) 314 { 315 u_char byte0, accum_diff; 316 u_int sum; 317 int i; 318 319 sum = 0; 320 accum_diff = 0; 321 byte0 = bootblock[0]; 322 323 /* 324 * Sum the contents of the block, keeping track of whether 325 * or not all bytes are the same. If 'accum_diff' ends up 326 * being zero, all of the bytes are, in fact, the same. 327 */ 328 for (i = 0; i < 511; ++i) { 329 sum += bootblock[i]; 330 accum_diff |= bootblock[i] ^ byte0; 331 } 332 333 /* 334 * Check to see if the checksum byte is the same as the 335 * rest of the bytes, too. (Note that if all of the bytes 336 * are the same except the checksum, a checksum compare 337 * won't succeed, but that's not our problem.) 338 */ 339 accum_diff |= bootblock[i] ^ byte0; 340 341 /* All bytes in block are the same; call it invalid. */ 342 if (accum_diff == 0) 343 return (-1); 344 345 return (sum - ((sum - 1) / 255) * 255); 346 } 347 348 int 349 md_pre_mount(struct install_partition_desc *install, size_t ndx) 350 { 351 return 0; 352 } 353 354 #ifdef HAVE_GPT 355 /* 356 * New GPT partitions have been written, update bootloader or remember 357 * data untill needed in md_post_newfs 358 */ 359 bool 360 md_gpt_post_write(struct disk_partitions *parts, part_id root_id, 361 bool root_is_new, part_id efi_id, bool efi_is_new) 362 { 363 364 return true; 365 } 366 #endif 367 368 bool 369 md_parts_use_wholedisk(struct disk_partitions *parts) 370 { 371 return parts_use_wholedisk(parts, 0, NULL); 372 } 373