1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <stdio.h> 29 #include <stdlib.h> 30 #include <libgen.h> 31 #include <malloc.h> 32 #include <string.h> 33 #include <sys/types.h> 34 #include <sys/stat.h> 35 #include <fcntl.h> 36 #include <unistd.h> 37 #include <strings.h> 38 #include <sys/mount.h> 39 #include <sys/mnttab.h> 40 #include <sys/dktp/fdisk.h> 41 #include <sys/dkio.h> 42 #include <sys/vtoc.h> 43 44 #include <libintl.h> 45 #include <locale.h> 46 #include "message.h" 47 #include <errno.h> 48 49 #ifndef TEXT_DOMAIN 50 #define TEXT_DOMAIN "SUNW_OST_OSCMD" 51 #endif 52 53 #define SECTOR_SIZE 0x200 54 #define STAGE2_MEMADDR 0x8000 /* loading addr of stage2 */ 55 56 #define STAGE1_BPB_OFFSET 0x3 57 #define STAGE1_BPB_SIZE 0x3B 58 #define STAGE1_BOOT_DRIVE 0x40 59 #define STAGE1_FORCE_LBA 0x41 60 #define STAGE1_STAGE2_ADDRESS 0x42 61 #define STAGE1_STAGE2_SECTOR 0x44 62 #define STAGE1_STAGE2_SEGMENT 0x48 63 64 #define STAGE2_BLOCKLIST (SECTOR_SIZE - 0x8) 65 #define STAGE2_INSTALLPART (SECTOR_SIZE + 0x8) 66 #define STAGE2_FORCE_LBA (SECTOR_SIZE + 0x11) 67 #define STAGE2_VER_STRING (SECTOR_SIZE + 0x12) 68 #define STAGE2_BLKOFF 50 /* offset from start of fdisk part */ 69 70 static int nowrite = 0; 71 static int write_mboot = 0; 72 static int force_mboot = 0; 73 static int is_floppy = 0; 74 static int is_bootpar = 0; 75 static int stage2_fd; 76 static int partition, slice = 0xff; 77 static int stage2_first_sector, stage2_second_sector; 78 79 80 static char bpb_sect[SECTOR_SIZE]; 81 static char boot_sect[SECTOR_SIZE]; 82 static char stage1_buffer[SECTOR_SIZE]; 83 static char stage2_buffer[2 * SECTOR_SIZE]; 84 static int blocklist[SECTOR_SIZE / sizeof (int)]; 85 86 static int open_device(char *); 87 static void read_bpb_sect(int); 88 static void read_boot_sect(char *); 89 static void write_boot_sect(char *); 90 static void read_stage1_stage2(char *, char *); 91 static void modify_and_write_stage1(int); 92 static void modify_and_write_stage2(int); 93 static int get_start_sector(int); 94 static void copy_stage2(int, char *); 95 static char *get_raw_partition(char *); 96 static void usage(char *); 97 98 extern int read_stage2_blocklist(int, int *); 99 100 int 101 main(int argc, char *argv[]) 102 { 103 int dev_fd, opt; 104 char *stage1, *stage2, *device; 105 106 (void) setlocale(LC_ALL, ""); 107 (void) textdomain(TEXT_DOMAIN); 108 109 while ((opt = getopt(argc, argv, "fmn")) != EOF) { 110 switch (opt) { 111 case 'm': 112 write_mboot = 1; 113 break; 114 case 'n': 115 nowrite = 1; 116 break; 117 case 'f': 118 force_mboot = 1; 119 break; 120 default: 121 /* fall through to process non-optional args */ 122 break; 123 } 124 } 125 126 /* check arguments */ 127 if (argc != optind + 3) { 128 usage(argv[0]); 129 } 130 131 if (nowrite) { 132 (void) fprintf(stdout, DRY_RUN); 133 } 134 135 stage1 = strdup(argv[optind]); 136 stage2 = strdup(argv[optind + 1]); 137 device = strdup(argv[optind + 2]); 138 139 if (!stage1 || !stage2 || !device) { 140 usage(argv[0]); 141 } 142 143 /* open and check device type */ 144 dev_fd = open_device(device); 145 146 /* read in stage1 and stage2 into buffer */ 147 read_stage1_stage2(stage1, stage2); 148 149 /* In the pcfs case, write a fresh stage2 */ 150 if (is_floppy || is_bootpar) { 151 copy_stage2(dev_fd, device); 152 read_bpb_sect(dev_fd); 153 } 154 155 /* read in boot sector */ 156 if (!is_floppy) 157 read_boot_sect(device); 158 159 /* modify stage1 based on grub needs */ 160 modify_and_write_stage1(dev_fd); 161 162 /* modify stage2 and write to media */ 163 modify_and_write_stage2(dev_fd); 164 165 if (!is_floppy && write_mboot) 166 write_boot_sect(device); 167 (void) close(dev_fd); 168 169 return (0); 170 } 171 172 static int 173 get_start_sector(int fd) 174 { 175 static int start_sect = 0; 176 177 int i; 178 struct mboot *mboot; 179 struct ipart *part; 180 181 if (start_sect) 182 return (start_sect); 183 184 mboot = (struct mboot *)boot_sect; 185 for (i = 0; i < FD_NUMPART; i++) { 186 part = (struct ipart *)mboot->parts + i; 187 if (is_bootpar) { 188 if (part->systid == 0xbe) 189 break; 190 } 191 } 192 193 /* 194 * If there is no boot partition, find the solaris partition 195 */ 196 197 if (i == FD_NUMPART) { 198 struct part_info dkpi; 199 200 /* 201 * Get the solaris partition information from the device 202 * and compare the offset of S2 with offset of solaris partition 203 * from fdisk partition table. 204 */ 205 if (ioctl(fd, DKIOCPARTINFO, &dkpi) < 0) { 206 (void) fprintf(stderr, PART_FAIL); 207 exit(-1); 208 } 209 210 for (i = 0; i < FD_NUMPART; i++) { 211 part = (struct ipart *)mboot->parts + i; 212 213 if (part->relsect == 0) { 214 (void) fprintf(stderr, BAD_PART, i); 215 exit(-1); 216 } 217 if (dkpi.p_start >= part->relsect && 218 dkpi.p_start < (part->relsect + part->numsect)) { 219 /* Found the partition */ 220 break; 221 } 222 } 223 } 224 225 if (i == FD_NUMPART) { 226 (void) fprintf(stderr, BOOTPAR); 227 exit(-1); 228 } 229 230 /* get confirmation for -m */ 231 if (write_mboot && !force_mboot) { 232 (void) fprintf(stdout, MBOOT_PROMPT); 233 if (getchar() != 'y') { 234 write_mboot = 0; 235 (void) fprintf(stdout, MBOOT_NOT_UPDATED); 236 } 237 } 238 239 start_sect = part->relsect; 240 if (part->bootid != 128 && write_mboot == 0) { 241 (void) fprintf(stdout, BOOTPAR_INACTIVE, i + 1); 242 } 243 244 partition = i; 245 return (start_sect); 246 } 247 248 static void 249 usage(char *progname) 250 { 251 (void) fprintf(stderr, USAGE, basename(progname)); 252 exit(-1); 253 } 254 255 static int 256 open_device(char *device) 257 { 258 int dev_fd; 259 struct stat stat; 260 char *raw_part; 261 262 is_floppy = strncmp(device, "/dev/rdsk", strlen("/dev/rdsk")) && 263 strncmp(device, "/dev/dsk", strlen("/dev/dsk")); 264 265 /* handle boot partition specification */ 266 if (!is_floppy && strstr(device, "p0:boot")) { 267 is_bootpar = 1; 268 } 269 270 raw_part = get_raw_partition(device); 271 272 if (nowrite) 273 dev_fd = open(raw_part, O_RDONLY); 274 else 275 dev_fd = open(raw_part, O_RDWR); 276 277 if (dev_fd == -1 || fstat(dev_fd, &stat) != 0) { 278 (void) fprintf(stderr, OPEN_FAIL, raw_part); 279 exit(-1); 280 } 281 if (S_ISCHR(stat.st_mode) == 0) { 282 (void) fprintf(stderr, NOT_RAW_DEVICE, raw_part); 283 exit(-1); 284 } 285 286 return (dev_fd); 287 } 288 289 static void 290 read_stage1_stage2(char *stage1, char *stage2) 291 { 292 int fd; 293 294 /* read the stage1 file from filesystem */ 295 fd = open(stage1, O_RDONLY); 296 if (fd == -1 || read(fd, stage1_buffer, SECTOR_SIZE) != SECTOR_SIZE) { 297 (void) fprintf(stderr, READ_FAIL_STAGE1, stage1); 298 exit(-1); 299 } 300 (void) close(fd); 301 302 /* read first two blocks of stage 2 from filesystem */ 303 stage2_fd = open(stage2, O_RDONLY); 304 if (stage2_fd == -1 || 305 read(stage2_fd, stage2_buffer, 2 * SECTOR_SIZE) 306 != 2 * SECTOR_SIZE) { 307 (void) fprintf(stderr, READ_FAIL_STAGE2, stage2); 308 exit(-1); 309 } 310 /* leave the stage2 file open for later */ 311 } 312 313 static void 314 read_bpb_sect(int dev_fd) 315 { 316 if (pread(dev_fd, bpb_sect, SECTOR_SIZE, 0) != SECTOR_SIZE) { 317 (void) fprintf(stderr, READ_FAIL_BPB); 318 exit(-1); 319 } 320 } 321 322 static void 323 read_boot_sect(char *device) 324 { 325 static int read_mbr = 0; 326 int i, fd; 327 char save[2]; 328 329 if (read_mbr) 330 return; 331 read_mbr = 1; 332 333 /* get the whole disk (p0) */ 334 i = strlen(device); 335 save[0] = device[i - 2]; 336 save[1] = device[i - 1]; 337 device[i - 2] = 'p'; 338 device[i - 1] = '0'; 339 340 fd = open(device, O_RDONLY); 341 if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) { 342 (void) fprintf(stderr, READ_FAIL_MBR, device); 343 if (fd == -1) 344 perror("open"); 345 else 346 perror("read"); 347 exit(-1); 348 } 349 (void) close(fd); 350 device[i - 2] = save[0]; 351 device[i - 1] = save[1]; 352 } 353 354 static void 355 write_boot_sect(char *device) 356 { 357 int fd, len; 358 char *raw, *end; 359 struct stat stat; 360 361 /* make a copy and chop off ":boot" */ 362 raw = strdup(device); 363 end = strstr(raw, "p0:boot"); 364 if (end) 365 end[2] = 0; 366 367 /* open p0 (whole disk) */ 368 len = strlen(raw); 369 raw[len - 2] = 'p'; 370 raw[len - 1] = '0'; 371 fd = open(raw, O_WRONLY); 372 if (fd == -1 || fstat(fd, &stat) != 0) { 373 (void) fprintf(stderr, OPEN_FAIL, raw); 374 exit(-1); 375 } 376 if (!nowrite && 377 pwrite(fd, stage1_buffer, SECTOR_SIZE, 0) != SECTOR_SIZE) { 378 (void) fprintf(stderr, WRITE_FAIL_BOOTSEC); 379 exit(-1); 380 } 381 (void) fprintf(stdout, WRITE_MBOOT); 382 (void) close(fd); 383 } 384 385 static void 386 modify_and_write_stage1(int dev_fd) 387 { 388 if (is_floppy) { 389 stage2_first_sector = blocklist[0]; 390 /* copy bios parameter block (for fat fs) */ 391 bcopy(bpb_sect + STAGE1_BPB_OFFSET, 392 stage1_buffer + STAGE1_BPB_OFFSET, STAGE1_BPB_SIZE); 393 } else if (is_bootpar) { 394 stage2_first_sector = get_start_sector(dev_fd) + blocklist[0]; 395 /* copy bios parameter block (for fat fs) and MBR */ 396 bcopy(bpb_sect + STAGE1_BPB_OFFSET, 397 stage1_buffer + STAGE1_BPB_OFFSET, STAGE1_BPB_SIZE); 398 bcopy(boot_sect + BOOTSZ, stage1_buffer + BOOTSZ, 512 - BOOTSZ); 399 *((unsigned char *)(stage1_buffer + STAGE1_FORCE_LBA)) = 1; 400 } else { 401 stage2_first_sector = get_start_sector(dev_fd) + STAGE2_BLKOFF; 402 /* copy MBR to stage1 in case of overwriting MBR sector */ 403 bcopy(boot_sect + BOOTSZ, stage1_buffer + BOOTSZ, 512 - BOOTSZ); 404 *((unsigned char *)(stage1_buffer + STAGE1_FORCE_LBA)) = 1; 405 } 406 407 /* modify default stage1 file generated by GRUB */ 408 *((ulong_t *)(stage1_buffer + STAGE1_STAGE2_SECTOR)) 409 = stage2_first_sector; 410 *((ushort_t *)(stage1_buffer + STAGE1_STAGE2_ADDRESS)) 411 = STAGE2_MEMADDR; 412 *((ushort_t *)(stage1_buffer + STAGE1_STAGE2_SEGMENT)) 413 = STAGE2_MEMADDR >> 4; 414 415 /* 416 * XXX the default grub distribution also: 417 * - Copy the possible MBR/extended part table 418 * - Set the boot drive of stage1 419 */ 420 421 /* write stage1/pboot to 1st sector */ 422 if (!nowrite && 423 pwrite(dev_fd, stage1_buffer, SECTOR_SIZE, 0) != SECTOR_SIZE) { 424 (void) fprintf(stderr, WRITE_FAIL_PBOOT); 425 exit(-1); 426 } 427 428 if (is_floppy) { 429 (void) fprintf(stdout, WRITE_BOOTSEC_FLOPPY); 430 } else { 431 (void) fprintf(stdout, WRITE_PBOOT, 432 partition, get_start_sector(dev_fd)); 433 } 434 } 435 436 #define START_BLOCK(pos) (*(ulong_t *)(pos)) 437 #define NUM_BLOCK(pos) (*(ushort_t *)((pos) + 4)) 438 #define START_SEG(pos) (*(ushort_t *)((pos) + 6)) 439 440 static void 441 modify_and_write_stage2(int dev_fd) 442 { 443 int nrecord; 444 off_t offset; 445 446 if (is_floppy || is_bootpar) { 447 int i = 0; 448 uint_t partition_offset; 449 uint_t install_addr = 0x8200; 450 uchar_t *pos = (uchar_t *)stage2_buffer + STAGE2_BLOCKLIST; 451 452 stage2_first_sector = blocklist[0]; 453 454 /* figure out the second sector */ 455 if (blocklist[1] > 1) { 456 blocklist[0]++; 457 blocklist[1]--; 458 } else { 459 i += 2; 460 } 461 stage2_second_sector = blocklist[i]; 462 463 if (is_floppy) 464 partition_offset = 0; 465 else /* solaris boot partition */ 466 partition_offset = get_start_sector(dev_fd); 467 468 /* install the blocklist at the end of stage2_buffer */ 469 while (blocklist[i]) { 470 if (START_BLOCK(pos - 8) != 0 && 471 START_BLOCK(pos - 8) != blocklist[i + 2]) { 472 (void) fprintf(stderr, PCFS_FRAGMENTED); 473 exit(-1); 474 } 475 START_BLOCK(pos) = blocklist[i] + partition_offset; 476 START_SEG(pos) = (ushort_t)(install_addr >> 4); 477 NUM_BLOCK(pos) = blocklist[i + 1]; 478 install_addr += blocklist[i + 1] * SECTOR_SIZE; 479 pos -= 8; 480 i += 2; 481 } 482 483 } else { 484 /* 485 * In a solaris partition, stage2 is written to contiguous 486 * blocks. So we update the starting block only. 487 */ 488 *((ulong_t *)(stage2_buffer + STAGE2_BLOCKLIST)) = 489 stage2_first_sector + 1; 490 } 491 492 if (is_floppy) { 493 /* modify the config file to add (fd0) */ 494 char *config_file = stage2_buffer + STAGE2_VER_STRING; 495 while (*config_file++) 496 ; 497 strcpy(config_file, "(fd0)/boot/grub/menu.lst"); 498 } else { 499 /* force lba and set disk partition */ 500 *((unsigned char *) (stage2_buffer + STAGE2_FORCE_LBA)) = 1; 501 *((long *)(stage2_buffer + STAGE2_INSTALLPART)) 502 = (partition << 16) | (slice << 8) | 0xff; 503 } 504 505 /* modification done, now do the writing */ 506 if (is_floppy || is_bootpar) { 507 /* we rewrite block 0 and 1 and that's it */ 508 if (!nowrite && 509 (pwrite(dev_fd, stage2_buffer, SECTOR_SIZE, 510 stage2_first_sector * SECTOR_SIZE) != SECTOR_SIZE || 511 pwrite(dev_fd, stage2_buffer + SECTOR_SIZE, SECTOR_SIZE, 512 stage2_second_sector * SECTOR_SIZE) != SECTOR_SIZE)) { 513 (void) fprintf(stderr, WRITE_FAIL_STAGE2); 514 exit(-1); 515 } 516 (void) fprintf(stdout, WRITE_STAGE2_PCFS); 517 return; 518 } 519 520 /* for disk, write stage2 starting at STAGE2_BLKOFF sector */ 521 offset = STAGE2_BLKOFF; 522 523 /* write the modified first two sectors */ 524 if (!nowrite && pwrite(dev_fd, stage2_buffer, 2 * SECTOR_SIZE, 525 offset * SECTOR_SIZE) != 2 * SECTOR_SIZE) { 526 (void) fprintf(stderr, WRITE_FAIL_STAGE2); 527 exit(-1); 528 } 529 530 /* write the remaining sectors */ 531 nrecord = 2; 532 offset += 2; 533 for (;;) { 534 int nread, nwrite; 535 nread = pread(stage2_fd, stage2_buffer, SECTOR_SIZE, 536 nrecord * SECTOR_SIZE); 537 if (nread > 0 && !nowrite) 538 nwrite = pwrite(dev_fd, stage2_buffer, SECTOR_SIZE, 539 offset * SECTOR_SIZE); 540 else 541 nwrite = SECTOR_SIZE; 542 if (nread < 0 || nwrite != SECTOR_SIZE) { 543 (void) fprintf(stderr, WRITE_FAIL_STAGE2_BLOCKS, 544 nread, nwrite); 545 break; 546 } 547 if (nread > 0) { 548 nrecord ++; 549 offset ++; 550 } 551 if (nread < SECTOR_SIZE) 552 break; /* end of file */ 553 } 554 (void) fprintf(stdout, WRITE_STAGE2_DISK, 555 partition, nrecord, STAGE2_BLKOFF, stage2_first_sector); 556 } 557 558 static char * 559 get_raw_partition(char *device) 560 { 561 int len; 562 struct mboot *mboot; 563 static char *raw = NULL; 564 565 if (raw) 566 return (raw); 567 raw = strdup(device); 568 569 if (is_floppy) 570 return (raw); 571 572 if (is_bootpar) { 573 int i; 574 char *end = strstr(raw, "p0:boot"); 575 576 end[2] = 0; /* chop off :boot */ 577 read_boot_sect(raw); 578 mboot = (struct mboot *)boot_sect; 579 for (i = 0; i < FD_NUMPART; i++) { 580 struct ipart *part = (struct ipart *)mboot->parts + i; 581 if (part->systid == 0xbe) /* solaris boot part */ 582 break; 583 } 584 585 if (i == FD_NUMPART) { 586 (void) fprintf(stderr, BOOTPAR_NOTFOUND, device); 587 exit(-1); 588 } 589 end[1] = '1' + i; /* set partition name */ 590 return (raw); 591 } 592 593 /* For disk, remember slice and return whole fdisk partition */ 594 len = strlen(raw); 595 if (raw[len - 2] != 's' || raw[len - 1] == '2') { 596 (void) fprintf(stderr, NOT_ROOT_SLICE); 597 exit(-1); 598 } 599 slice = atoi(&raw[len - 1]); 600 601 raw[len - 2] = 's'; 602 raw[len - 1] = '2'; 603 return (raw); 604 } 605 606 #define TMP_MNTPT "/tmp/installgrub_pcfs" 607 static void 608 copy_stage2(int dev_fd, char *device) 609 { 610 FILE *mntfp; 611 int i, pcfs_fp; 612 char buf[SECTOR_SIZE]; 613 char *cp; 614 struct mnttab mp = {0}, mpref = {0}; 615 616 /* convert raw to block device name by removing the first 'r' */ 617 (void) strncpy(buf, device, sizeof (buf)); 618 buf[sizeof (buf) - 1] = 0; 619 cp = strchr(buf, 'r'); 620 if (cp == NULL) { 621 (void) fprintf(stderr, CONVERT_FAIL, device); 622 exit(-1); 623 } 624 do { 625 *cp = *(cp + 1); 626 } while (*(++cp)); 627 628 /* get the mount point, if any */ 629 mntfp = fopen("/etc/mnttab", "r"); 630 if (mntfp == NULL) { 631 (void) fprintf(stderr, OPEN_FAIL_FILE, "/etc/mnttab"); 632 exit(-1); 633 } 634 635 mpref.mnt_special = buf; 636 if (getmntany(mntfp, &mp, &mpref) != 0) { 637 char cmd[128]; 638 639 /* not mounted, try remount */ 640 (void) mkdir(TMP_MNTPT, S_IRWXU); 641 (void) snprintf(cmd, sizeof (cmd), "mount -F pcfs %s %s", 642 buf, TMP_MNTPT); 643 (void) system(cmd); 644 rewind(mntfp); 645 bzero(&mp, sizeof (mp)); 646 if (getmntany(mntfp, &mp, &mpref) != 0) { 647 (void) fprintf(stderr, MOUNT_FAIL, buf); 648 exit(-1); 649 } 650 } 651 652 (void) snprintf(buf, sizeof (buf), 653 "%s/boot", mp.mnt_mountp); 654 (void) mkdir(buf, S_IRWXU); 655 (void) strcat(buf, "/grub"); 656 (void) mkdir(buf, S_IRWXU); 657 658 (void) strcat(buf, "/stage2"); 659 pcfs_fp = open(buf, O_WRONLY | O_CREAT, S_IRWXU); 660 if (pcfs_fp == -1) { 661 (void) fprintf(stderr, OPEN_FAIL_FILE, buf); 662 perror("open:"); 663 (void) umount(TMP_MNTPT); 664 exit(-1); 665 } 666 667 /* write stage2 to pcfs */ 668 for (i = 0; ; i++) { 669 int nread, nwrite; 670 nread = pread(stage2_fd, buf, SECTOR_SIZE, i * SECTOR_SIZE); 671 if (nowrite) 672 nwrite = nread; 673 else 674 nwrite = pwrite(pcfs_fp, buf, nread, i * SECTOR_SIZE); 675 if (nread < 0 || nwrite != nread) { 676 (void) fprintf(stderr, WRITE_FAIL_STAGE2_BLOCKS, 677 nread, nwrite); 678 break; 679 } 680 if (nread < SECTOR_SIZE) 681 break; /* end of file */ 682 } 683 (void) close(pcfs_fp); 684 (void) umount(TMP_MNTPT); 685 686 /* 687 * Now, get the blocklist from the device. 688 */ 689 bzero(blocklist, sizeof (blocklist)); 690 if (read_stage2_blocklist(dev_fd, blocklist) != 0) 691 exit(-1); 692 } 693