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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <libgen.h>
29 #include <malloc.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <strings.h>
36 #include <sys/mount.h>
37 #include <sys/mnttab.h>
38 #include <sys/dktp/fdisk.h>
39 #include <sys/dkio.h>
40 #include <sys/vtoc.h>
41 
42 #include <libintl.h>
43 #include <locale.h>
44 #include "message.h"
45 #include <errno.h>
46 #include <libfdisk.h>
47 #include <md5.h>
48 
49 #ifndef	TEXT_DOMAIN
50 #define	TEXT_DOMAIN	"SUNW_OST_OSCMD"
51 #endif
52 
53 #define	SECTOR_SIZE	0x200
54 #define	HASH_SIZE	0x10
55 #define	VERSION_SIZE	0x50
56 #define	STAGE2_MEMADDR	0x8000	/* loading addr of stage2 */
57 
58 #define	STAGE1_BPB_OFFSET	0x3
59 #define	STAGE1_BPB_SIZE		0x3B
60 #define	STAGE1_BOOT_DRIVE	0x40
61 #define	STAGE1_FORCE_LBA	0x41
62 #define	STAGE1_STAGE2_ADDRESS	0x42
63 #define	STAGE1_STAGE2_SECTOR	0x44
64 #define	STAGE1_STAGE2_SEGMENT	0x48
65 
66 #define	STAGE2_BLOCKLIST	(SECTOR_SIZE - 0x8)
67 #define	STAGE2_INSTALLPART	(SECTOR_SIZE + 0x8)
68 #define	STAGE2_FORCE_LBA	(SECTOR_SIZE + 0x11)
69 #define	STAGE2_VER_STRING	(SECTOR_SIZE + 0x12)
70 #define	STAGE2_SIGN_OFFSET	(SECTOR_SIZE + 0x60)
71 #define	STAGE2_PKG_VERSION	(SECTOR_SIZE + 0x70)
72 #define	STAGE2_BLKOFF		50	/* offset from start of fdisk part */
73 
74 static char extended_sig[] = "\xCC\xCC\xCC\xCC\xAA\xAA\xAA\xAA\xBB\xBB\xBB\xBB"
75 "\xBB\xBB\xBB\xBB";
76 
77 static int nowrite = 0;
78 static int write_mboot = 0;
79 static int force_mboot = 0;
80 static int getinfo = 0;
81 static int do_version = 0;
82 static int is_floppy = 0;
83 static int is_bootpar = 0;
84 static int strip = 0;
85 static int stage2_fd;
86 static int partition, slice = 0xff;
87 static char *device_p0;
88 static uint32_t stage2_first_sector, stage2_second_sector;
89 
90 
91 static char bpb_sect[SECTOR_SIZE];
92 static char boot_sect[SECTOR_SIZE];
93 static char stage1_buffer[SECTOR_SIZE];
94 static char stage2_buffer[2 * SECTOR_SIZE];
95 static char signature[HASH_SIZE];
96 static char verstring[VERSION_SIZE];
97 static unsigned int blocklist[SECTOR_SIZE / sizeof (unsigned int)];
98 
99 static int open_device(char *);
100 static void read_bpb_sect(int);
101 static void read_boot_sect(char *);
102 static void write_boot_sect(char *);
103 static void read_stage1_stage2(char *, char *);
104 static void modify_and_write_stage1(int);
105 static void modify_and_write_stage2(int);
106 static unsigned int get_start_sector(int);
107 static void copy_stage2(int, char *);
108 static char *get_raw_partition(char *);
109 static void usage(char *);
110 static void print_info();
111 static int read_stage2_info(int);
112 static void check_extended_support();
113 
114 extern int read_stage2_blocklist(int, unsigned int *);
115 
116 int
117 main(int argc, char *argv[])
118 {
119 	int dev_fd, opt, params = 3;
120 	char *stage1, *stage2, *device;
121 
122 	(void) setlocale(LC_ALL, "");
123 	(void) textdomain(TEXT_DOMAIN);
124 
125 	while ((opt = getopt(argc, argv, "fmneis:")) != EOF) {
126 		switch (opt) {
127 		case 'm':
128 			write_mboot = 1;
129 			break;
130 		case 'n':
131 			nowrite = 1;
132 			break;
133 		case 'f':
134 			force_mboot = 1;
135 			break;
136 		case 'i':
137 			getinfo = 1;
138 			params = 1;
139 			break;
140 		case 'e':
141 			strip = 1;
142 			break;
143 		case 's':
144 			do_version = 1;
145 			(void) snprintf(verstring, sizeof (verstring), "%s",
146 			    optarg);
147 			break;
148 		default:
149 			/* fall through to process non-optional args */
150 			break;
151 		}
152 	}
153 
154 	/* check arguments */
155 	if (argc != optind + params) {
156 		usage(argv[0]);
157 	}
158 
159 	if (nowrite) {
160 		(void) fprintf(stdout, DRY_RUN);
161 	}
162 
163 	if (params == 1) {
164 		device = strdup(argv[optind]);
165 		if (!device) {
166 			usage(argv[0]);
167 		}
168 	} else if (params == 3) {
169 		stage1 = strdup(argv[optind]);
170 		stage2 = strdup(argv[optind + 1]);
171 		device = strdup(argv[optind + 2]);
172 
173 		if (!stage1 || !stage2 || !device) {
174 			usage(argv[0]);
175 		}
176 	}
177 
178 	/* open and check device type */
179 	dev_fd = open_device(device);
180 
181 	if (getinfo) {
182 		if (read_stage2_info(dev_fd) != 0) {
183 			fprintf(stderr, "Unable to read extended information"
184 			    " from %s\n", device);
185 			exit(1);
186 		}
187 		print_info();
188 		(void) free(device);
189 		(void) close(dev_fd);
190 		return (0);
191 	}
192 
193 	/* read in stage1 and stage2 into buffer */
194 	read_stage1_stage2(stage1, stage2);
195 
196 	/* check if stage2 supports extended versioning */
197 	if (do_version)
198 		check_extended_support(stage2);
199 
200 	/* In the pcfs case, write a fresh stage2 */
201 	if (is_floppy || is_bootpar) {
202 		copy_stage2(dev_fd, device);
203 		read_bpb_sect(dev_fd);
204 	}
205 
206 	/* read in boot sector */
207 	if (!is_floppy)
208 		read_boot_sect(device);
209 
210 	/* modify stage1 based on grub needs */
211 	modify_and_write_stage1(dev_fd);
212 
213 	/* modify stage2 and write to media */
214 	modify_and_write_stage2(dev_fd);
215 
216 	if (!is_floppy && write_mboot)
217 		write_boot_sect(device);
218 
219 	(void) close(dev_fd);
220 	free(device);
221 	free(stage1);
222 	free(stage2);
223 
224 	return (0);
225 }
226 
227 static unsigned int
228 get_start_sector(int fd)
229 {
230 	static unsigned int start_sect = 0;
231 	uint32_t secnum, numsec;
232 	int i, pno, rval, ext_sol_part_found = 0;
233 	struct mboot *mboot;
234 	struct ipart *part;
235 	ext_part_t *epp;
236 
237 	if (start_sect)
238 		return (start_sect);
239 
240 	mboot = (struct mboot *)boot_sect;
241 	for (i = 0; i < FD_NUMPART; i++) {
242 		part = (struct ipart *)mboot->parts + i;
243 		if (is_bootpar) {
244 			if (part->systid == 0xbe)
245 				break;
246 		}
247 	}
248 
249 	/* Read extended partition to find a solaris partition */
250 	if ((rval = libfdisk_init(&epp, device_p0, NULL, FDISK_READ_DISK))
251 	    != FDISK_SUCCESS) {
252 		switch (rval) {
253 			/*
254 			 * FDISK_EBADLOGDRIVE and FDISK_ENOLOGDRIVE can
255 			 * be considered as soft errors and hence
256 			 * we do not exit
257 			 */
258 			case FDISK_EBADLOGDRIVE:
259 				break;
260 			case FDISK_ENOLOGDRIVE:
261 				break;
262 			case FDISK_ENOVGEOM:
263 				(void) fprintf(stderr, NO_VIRT_GEOM);
264 				exit(1);
265 				break;
266 			case FDISK_ENOPGEOM:
267 				(void) fprintf(stderr, NO_PHYS_GEOM);
268 				exit(1);
269 				break;
270 			case FDISK_ENOLGEOM:
271 				(void) fprintf(stderr, NO_LABEL_GEOM);
272 				exit(1);
273 				break;
274 			default:
275 				(void) fprintf(stderr, LIBFDISK_INIT_FAIL);
276 				exit(1);
277 				break;
278 		}
279 	}
280 
281 	rval = fdisk_get_solaris_part(epp, &pno, &secnum, &numsec);
282 	if (rval == FDISK_SUCCESS) {
283 		ext_sol_part_found = 1;
284 	}
285 	libfdisk_fini(&epp);
286 
287 	/*
288 	 * If there is no boot partition, find the solaris partition
289 	 */
290 
291 	if (i == FD_NUMPART) {
292 		struct part_info dkpi;
293 		struct extpart_info edkpi;
294 
295 		/*
296 		 * Get the solaris partition information from the device
297 		 * and compare the offset of S2 with offset of solaris partition
298 		 * from fdisk partition table.
299 		 */
300 		if (ioctl(fd, DKIOCEXTPARTINFO, &edkpi) < 0) {
301 			if (ioctl(fd, DKIOCPARTINFO, &dkpi) < 0) {
302 				(void) fprintf(stderr, PART_FAIL);
303 				exit(-1);
304 			} else {
305 				edkpi.p_start = dkpi.p_start;
306 			}
307 		}
308 
309 		for (i = 0; i < FD_NUMPART; i++) {
310 			part = (struct ipart *)mboot->parts + i;
311 
312 			if (part->relsect == 0) {
313 				(void) fprintf(stderr, BAD_PART, i);
314 				exit(-1);
315 			}
316 
317 			if (edkpi.p_start >= part->relsect &&
318 			    edkpi.p_start < (part->relsect + part->numsect)) {
319 				/* Found the partition */
320 				break;
321 			}
322 		}
323 	}
324 
325 	if ((i == FD_NUMPART) && (!ext_sol_part_found)) {
326 		(void) fprintf(stderr, BOOTPAR);
327 		exit(-1);
328 	}
329 
330 	/* get confirmation for -m */
331 	if (write_mboot && !force_mboot) {
332 		(void) fprintf(stdout, MBOOT_PROMPT);
333 		if (getchar() != 'y') {
334 			write_mboot = 0;
335 			(void) fprintf(stdout, MBOOT_NOT_UPDATED);
336 		}
337 	}
338 
339 	if (fdisk_is_dos_extended(part->systid)) {
340 		start_sect = secnum;
341 		partition = pno;
342 	} else {
343 		start_sect = part->relsect;
344 		partition = i;
345 	}
346 
347 	if (part->bootid != 128 && write_mboot == 0) {
348 		(void) fprintf(stdout, BOOTPAR_INACTIVE, i + 1);
349 	}
350 
351 	return (start_sect);
352 }
353 
354 static void
355 usage(char *progname)
356 {
357 	(void) fprintf(stderr, USAGE, basename(progname));
358 	exit(-1);
359 }
360 
361 static int
362 open_device(char *device)
363 {
364 	int dev_fd;
365 	struct stat stat;
366 	char *raw_part;
367 
368 	is_floppy = strncmp(device, "/dev/rdsk", strlen("/dev/rdsk")) &&
369 	    strncmp(device, "/dev/dsk", strlen("/dev/dsk"));
370 
371 	/* handle boot partition specification */
372 	if (!is_floppy && strstr(device, "p0:boot")) {
373 		is_bootpar = 1;
374 	}
375 
376 	raw_part = get_raw_partition(device);
377 
378 	if (nowrite)
379 		dev_fd = open(raw_part, O_RDONLY);
380 	else
381 		dev_fd = open(raw_part, O_RDWR);
382 
383 	if (dev_fd == -1 || fstat(dev_fd, &stat) != 0) {
384 		(void) fprintf(stderr, OPEN_FAIL, raw_part);
385 		exit(-1);
386 	}
387 	if (S_ISCHR(stat.st_mode) == 0) {
388 		(void) fprintf(stderr, NOT_RAW_DEVICE, raw_part);
389 		exit(-1);
390 	}
391 
392 	return (dev_fd);
393 }
394 
395 static void
396 read_stage1_stage2(char *stage1, char *stage2)
397 {
398 	int fd;
399 
400 	/* read the stage1 file from filesystem */
401 	fd = open(stage1, O_RDONLY);
402 	if (fd == -1 || read(fd, stage1_buffer, SECTOR_SIZE) != SECTOR_SIZE) {
403 		(void) fprintf(stderr, READ_FAIL_STAGE1, stage1);
404 		exit(-1);
405 	}
406 	(void) close(fd);
407 
408 	/* read first two blocks of stage 2 from filesystem */
409 	stage2_fd = open(stage2, O_RDONLY);
410 	if (stage2_fd == -1 ||
411 	    read(stage2_fd, stage2_buffer, 2 * SECTOR_SIZE)
412 	    != 2 * SECTOR_SIZE) {
413 		(void) fprintf(stderr, READ_FAIL_STAGE2, stage2);
414 		exit(-1);
415 	}
416 	/* leave the stage2 file open for later */
417 }
418 
419 static void
420 read_bpb_sect(int dev_fd)
421 {
422 	if (pread(dev_fd, bpb_sect, SECTOR_SIZE, 0) != SECTOR_SIZE) {
423 		(void) fprintf(stderr, READ_FAIL_BPB);
424 		exit(-1);
425 	}
426 }
427 
428 static void
429 read_boot_sect(char *device)
430 {
431 	static int read_mbr = 0;
432 	int i, fd;
433 	char save[2];
434 
435 	if (read_mbr)
436 		return;
437 	read_mbr = 1;
438 
439 	/* get the whole disk (p0) */
440 	i = strlen(device);
441 	save[0] = device[i - 2];
442 	save[1] = device[i - 1];
443 	device[i - 2] = 'p';
444 	device[i - 1] = '0';
445 
446 	device_p0 = strdup(device);
447 	fd = open(device, O_RDONLY);
448 	if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) {
449 		(void) fprintf(stderr, READ_FAIL_MBR, device);
450 		if (fd == -1)
451 			perror("open");
452 		else
453 			perror("read");
454 		exit(-1);
455 	}
456 	(void) close(fd);
457 	device[i - 2] = save[0];
458 	device[i - 1] = save[1];
459 }
460 
461 static void
462 write_boot_sect(char *device)
463 {
464 	int fd, len;
465 	char *raw, *end;
466 	struct stat stat;
467 
468 	/* make a copy and chop off ":boot" */
469 	raw = strdup(device);
470 	end = strstr(raw, "p0:boot");
471 	if (end)
472 		end[2] = 0;
473 
474 	/* open p0 (whole disk) */
475 	len = strlen(raw);
476 	raw[len - 2] = 'p';
477 	raw[len - 1] = '0';
478 	fd = open(raw, O_WRONLY);
479 	if (fd == -1 || fstat(fd, &stat) != 0) {
480 		(void) fprintf(stderr, OPEN_FAIL, raw);
481 		exit(-1);
482 	}
483 	if (!nowrite &&
484 	    pwrite(fd, stage1_buffer, SECTOR_SIZE, 0) != SECTOR_SIZE) {
485 		(void) fprintf(stderr, WRITE_FAIL_BOOTSEC);
486 		exit(-1);
487 	}
488 	(void) fprintf(stdout, WRITE_MBOOT);
489 	(void) close(fd);
490 }
491 
492 static void
493 modify_and_write_stage1(int dev_fd)
494 {
495 	if (is_floppy) {
496 		stage2_first_sector = blocklist[0];
497 		/* copy bios parameter block (for fat fs) */
498 		bcopy(bpb_sect + STAGE1_BPB_OFFSET,
499 		    stage1_buffer + STAGE1_BPB_OFFSET, STAGE1_BPB_SIZE);
500 	} else if (is_bootpar) {
501 		stage2_first_sector = get_start_sector(dev_fd) + blocklist[0];
502 		/* copy bios parameter block (for fat fs) and MBR */
503 		bcopy(bpb_sect + STAGE1_BPB_OFFSET,
504 		    stage1_buffer + STAGE1_BPB_OFFSET, STAGE1_BPB_SIZE);
505 		bcopy(boot_sect + BOOTSZ, stage1_buffer + BOOTSZ, 512 - BOOTSZ);
506 		*((unsigned char *)(stage1_buffer + STAGE1_FORCE_LBA)) = 1;
507 	} else {
508 		stage2_first_sector = get_start_sector(dev_fd) + STAGE2_BLKOFF;
509 		/* copy MBR to stage1 in case of overwriting MBR sector */
510 		bcopy(boot_sect + BOOTSZ, stage1_buffer + BOOTSZ, 512 - BOOTSZ);
511 		*((unsigned char *)(stage1_buffer + STAGE1_FORCE_LBA)) = 1;
512 	}
513 
514 	/* modify default stage1 file generated by GRUB */
515 	*((ulong_t *)(stage1_buffer + STAGE1_STAGE2_SECTOR))
516 	    = stage2_first_sector;
517 	*((ushort_t *)(stage1_buffer + STAGE1_STAGE2_ADDRESS))
518 	    = STAGE2_MEMADDR;
519 	*((ushort_t *)(stage1_buffer + STAGE1_STAGE2_SEGMENT))
520 	    = STAGE2_MEMADDR >> 4;
521 
522 	/*
523 	 * XXX the default grub distribution also:
524 	 * - Copy the possible MBR/extended part table
525 	 * - Set the boot drive of stage1
526 	 */
527 
528 	/* write stage1/pboot to 1st sector */
529 	if (!nowrite &&
530 	    pwrite(dev_fd, stage1_buffer, SECTOR_SIZE, 0) != SECTOR_SIZE) {
531 		(void) fprintf(stderr, WRITE_FAIL_PBOOT);
532 		exit(-1);
533 	}
534 
535 	if (is_floppy) {
536 		(void) fprintf(stdout, WRITE_BOOTSEC_FLOPPY);
537 	} else {
538 		(void) fprintf(stdout, WRITE_PBOOT,
539 		    partition, get_start_sector(dev_fd));
540 	}
541 }
542 
543 static void check_extended_support(char *stage2)
544 {
545 	char	*cmp = stage2_buffer + STAGE2_SIGN_OFFSET - 1;
546 
547 	if ((*cmp++ != '\xEE') && memcmp(cmp, extended_sig, HASH_SIZE) != 0) {
548 		fprintf(stderr, "%s does not support extended versioning\n",
549 		    stage2);
550 		do_version = 0;
551 	}
552 }
553 
554 
555 static void print_info()
556 {
557 	int	i;
558 
559 	if (strip) {
560 		fprintf(stdout, "%s\n", verstring);
561 	} else {
562 		fprintf(stdout, "Grub extended version information : %s\n",
563 		    verstring);
564 		fprintf(stdout, "Grub stage2 (MD5) signature : ");
565 	}
566 
567 	for (i = 0; i < HASH_SIZE; i++)
568 		fprintf(stdout, "%02x", (unsigned char)signature[i]);
569 
570 	fprintf(stdout, "\n");
571 }
572 
573 static int
574 read_stage2_info(int dev_fd)
575 {
576 	int 	ret;
577 	int	first_offset, second_offset;
578 	char	*sign;
579 
580 	if (is_floppy || is_bootpar) {
581 
582 		ret = pread(dev_fd, stage1_buffer, SECTOR_SIZE, 0);
583 		if (ret != SECTOR_SIZE) {
584 			perror("Error reading stage1 sector");
585 			return (1);
586 		}
587 
588 		first_offset = *((ulong_t *)(stage1_buffer +
589 		    STAGE1_STAGE2_SECTOR));
590 
591 		/* Start reading in the first sector of stage 2 */
592 
593 		ret = pread(dev_fd, stage2_buffer, SECTOR_SIZE, first_offset *
594 		    SECTOR_SIZE);
595 		if (ret != SECTOR_SIZE) {
596 			perror("Error reading stage2 first sector");
597 			return (1);
598 		}
599 
600 		/* From the block list section grab stage2 second sector */
601 
602 		second_offset = *((ulong_t *)(stage2_buffer +
603 		    STAGE2_BLOCKLIST));
604 
605 		ret = pread(dev_fd, stage2_buffer + SECTOR_SIZE, SECTOR_SIZE,
606 		    second_offset * SECTOR_SIZE);
607 		if (ret != SECTOR_SIZE) {
608 			perror("Error reading stage2 second sector");
609 			return (1);
610 		}
611 	} else {
612 		ret = pread(dev_fd, stage2_buffer, 2 * SECTOR_SIZE,
613 		    STAGE2_BLKOFF * SECTOR_SIZE);
614 		if (ret != 2 * SECTOR_SIZE) {
615 			perror("Error reading stage2 sectors");
616 			return (1);
617 		}
618 	}
619 
620 	sign = stage2_buffer + STAGE2_SIGN_OFFSET - 1;
621 	if (*sign++ != '\xEE')
622 		return (1);
623 	(void) memcpy(signature, sign, HASH_SIZE);
624 	sign = stage2_buffer + STAGE2_PKG_VERSION;
625 	(void) strncpy(verstring, sign, VERSION_SIZE);
626 	return (0);
627 }
628 
629 
630 static int
631 compute_and_write_md5hash(char *dest)
632 {
633 	struct stat	sb;
634 	char		*buffer;
635 
636 	if (fstat(stage2_fd, &sb) == -1)
637 		return (-1);
638 
639 	buffer = malloc(sb.st_size);
640 	if (buffer == NULL)
641 		return (-1);
642 
643 	if (lseek(stage2_fd, 0, SEEK_SET) == -1)
644 		return (-1);
645 	if (read(stage2_fd, buffer, sb.st_size) < 0)
646 		return (-1);
647 
648 	md5_calc(dest, buffer, sb.st_size);
649 	free(buffer);
650 	return (0);
651 }
652 
653 
654 #define	START_BLOCK(pos)	(*(ulong_t *)(pos))
655 #define	NUM_BLOCK(pos)		(*(ushort_t *)((pos) + 4))
656 #define	START_SEG(pos)		(*(ushort_t *)((pos) + 6))
657 
658 static void
659 modify_and_write_stage2(int dev_fd)
660 {
661 	int 	nrecord;
662 	off_t 	offset;
663 	char	*dest;
664 
665 	if (do_version) {
666 		dest = stage2_buffer + STAGE2_SIGN_OFFSET;
667 		if (compute_and_write_md5hash(dest) < 0)
668 			perror("MD5 operation");
669 		dest = stage2_buffer + STAGE2_PKG_VERSION;
670 		(void) strncpy(dest, verstring, VERSION_SIZE);
671 	}
672 
673 	if (is_floppy || is_bootpar) {
674 		int i = 0;
675 		uint32_t partition_offset;
676 		uint32_t install_addr = 0x8200;
677 		uchar_t *pos = (uchar_t *)stage2_buffer + STAGE2_BLOCKLIST;
678 
679 		stage2_first_sector = blocklist[0];
680 
681 		/* figure out the second sector */
682 		if (blocklist[1] > 1) {
683 			blocklist[0]++;
684 			blocklist[1]--;
685 		} else {
686 			i += 2;
687 		}
688 		stage2_second_sector = blocklist[i];
689 
690 		if (is_floppy)
691 			partition_offset = 0;
692 		else	/* solaris boot partition */
693 			partition_offset = get_start_sector(dev_fd);
694 
695 		/* install the blocklist at the end of stage2_buffer */
696 		while (blocklist[i]) {
697 			if (START_BLOCK(pos - 8) != 0 &&
698 			    START_BLOCK(pos - 8) != blocklist[i + 2]) {
699 				(void) fprintf(stderr, PCFS_FRAGMENTED);
700 				exit(-1);
701 			}
702 			START_BLOCK(pos) = blocklist[i] + partition_offset;
703 			START_SEG(pos) = (ushort_t)(install_addr >> 4);
704 			NUM_BLOCK(pos) = blocklist[i + 1];
705 			install_addr += blocklist[i + 1] * SECTOR_SIZE;
706 			pos -= 8;
707 			i += 2;
708 		}
709 
710 	} else {
711 		/*
712 		 * In a solaris partition, stage2 is written to contiguous
713 		 * blocks. So we update the starting block only.
714 		 */
715 		*((ulong_t *)(stage2_buffer + STAGE2_BLOCKLIST)) =
716 		    stage2_first_sector + 1;
717 	}
718 
719 	if (is_floppy) {
720 		/* modify the config file to add (fd0) */
721 		char *config_file = stage2_buffer + STAGE2_VER_STRING;
722 		while (*config_file++)
723 			;
724 		strcpy(config_file, "(fd0)/boot/grub/menu.lst");
725 	} else {
726 		/* force lba and set disk partition */
727 		*((unsigned char *) (stage2_buffer + STAGE2_FORCE_LBA)) = 1;
728 		*((long *)(stage2_buffer + STAGE2_INSTALLPART))
729 		    = (partition << 16) | (slice << 8) | 0xff;
730 	}
731 
732 	/* modification done, now do the writing */
733 	if (is_floppy || is_bootpar) {
734 		/* we rewrite block 0 and 1 and that's it */
735 		if (!nowrite &&
736 		    (pwrite(dev_fd, stage2_buffer, SECTOR_SIZE,
737 		    stage2_first_sector * SECTOR_SIZE) != SECTOR_SIZE ||
738 		    pwrite(dev_fd, stage2_buffer + SECTOR_SIZE, SECTOR_SIZE,
739 		    stage2_second_sector * SECTOR_SIZE) != SECTOR_SIZE)) {
740 			(void) fprintf(stderr, WRITE_FAIL_STAGE2);
741 			exit(-1);
742 		}
743 		(void) fprintf(stdout, WRITE_STAGE2_PCFS);
744 		return;
745 	}
746 
747 	/* for disk, write stage2 starting at STAGE2_BLKOFF sector */
748 	offset = STAGE2_BLKOFF;
749 
750 	/* write the modified first two sectors */
751 	if (!nowrite && pwrite(dev_fd, stage2_buffer, 2 * SECTOR_SIZE,
752 	    offset * SECTOR_SIZE) != 2 * SECTOR_SIZE) {
753 		(void) fprintf(stderr, WRITE_FAIL_STAGE2);
754 		exit(-1);
755 	}
756 
757 	/* write the remaining sectors */
758 	nrecord = 2;
759 	offset += 2;
760 	for (;;) {
761 		int nread, nwrite;
762 		nread = pread(stage2_fd, stage2_buffer, SECTOR_SIZE,
763 		    nrecord * SECTOR_SIZE);
764 		if (nread > 0 && !nowrite)
765 			nwrite = pwrite(dev_fd, stage2_buffer, SECTOR_SIZE,
766 			    offset * SECTOR_SIZE);
767 		else
768 			nwrite = SECTOR_SIZE;
769 		if (nread < 0 || nwrite != SECTOR_SIZE) {
770 			(void) fprintf(stderr, WRITE_FAIL_STAGE2_BLOCKS,
771 			    nread, nwrite);
772 			break;
773 		}
774 		if (nread > 0) {
775 			nrecord ++;
776 			offset ++;
777 		}
778 		if (nread < SECTOR_SIZE)
779 			break;	/* end of file */
780 	}
781 	(void) fprintf(stdout, WRITE_STAGE2_DISK,
782 	    partition, nrecord, STAGE2_BLKOFF, stage2_first_sector);
783 }
784 
785 static char *
786 get_raw_partition(char *device)
787 {
788 	int len;
789 	struct mboot *mboot;
790 	static char *raw = NULL;
791 
792 	if (raw)
793 		return (raw);
794 	raw = strdup(device);
795 
796 	if (is_floppy)
797 		return (raw);
798 
799 	if (is_bootpar) {
800 		int i;
801 		char *end = strstr(raw, "p0:boot");
802 
803 		end[2] = 0;		/* chop off :boot */
804 		read_boot_sect(raw);
805 		mboot = (struct mboot *)boot_sect;
806 		for (i = 0; i < FD_NUMPART; i++) {
807 			struct ipart *part = (struct ipart *)mboot->parts + i;
808 			if (part->systid == 0xbe)	/* solaris boot part */
809 				break;
810 		}
811 
812 		if (i == FD_NUMPART) {
813 			(void) fprintf(stderr, BOOTPAR_NOTFOUND, device);
814 			exit(-1);
815 		}
816 		end[1] = '1' + i;	/* set partition name */
817 		return (raw);
818 	}
819 
820 	/* For disk, remember slice and return whole fdisk partition  */
821 	len = strlen(raw);
822 	if (raw[len - 2] != 's' || raw[len - 1] == '2') {
823 		(void) fprintf(stderr, NOT_ROOT_SLICE);
824 		exit(-1);
825 	}
826 	slice = atoi(&raw[len - 1]);
827 
828 	raw[len - 2] = 's';
829 	raw[len - 1] = '2';
830 	return (raw);
831 }
832 
833 #define	TMP_MNTPT	"/tmp/installgrub_pcfs"
834 static void
835 copy_stage2(int dev_fd, char *device)
836 {
837 	FILE *mntfp;
838 	int i, pcfs_fp;
839 	char buf[SECTOR_SIZE];
840 	char *cp;
841 	struct mnttab mp = {0}, mpref = {0};
842 
843 	/* convert raw to block device name by removing the first 'r' */
844 	(void) strncpy(buf, device, sizeof (buf));
845 	buf[sizeof (buf) - 1] = 0;
846 	cp = strchr(buf, 'r');
847 	if (cp == NULL) {
848 		(void) fprintf(stderr, CONVERT_FAIL, device);
849 		exit(-1);
850 	}
851 	do {
852 		*cp = *(cp + 1);
853 	} while (*(++cp));
854 
855 	/* get the mount point, if any */
856 	mntfp = fopen("/etc/mnttab", "r");
857 	if (mntfp == NULL) {
858 		(void) fprintf(stderr, OPEN_FAIL_FILE, "/etc/mnttab");
859 		exit(-1);
860 	}
861 
862 	mpref.mnt_special = buf;
863 	if (getmntany(mntfp, &mp, &mpref) != 0) {
864 		char cmd[128];
865 
866 		/* not mounted, try remount */
867 		(void) mkdir(TMP_MNTPT, S_IRWXU);
868 		(void) snprintf(cmd, sizeof (cmd), "mount -F pcfs %s %s",
869 		    buf, TMP_MNTPT);
870 		(void) system(cmd);
871 		rewind(mntfp);
872 		bzero(&mp, sizeof (mp));
873 		if (getmntany(mntfp, &mp, &mpref) != 0) {
874 			(void) fprintf(stderr, MOUNT_FAIL, buf);
875 			exit(-1);
876 		}
877 	}
878 
879 	(void) snprintf(buf, sizeof (buf),
880 	    "%s/boot", mp.mnt_mountp);
881 	(void) mkdir(buf, S_IRWXU);
882 	(void) strcat(buf, "/grub");
883 	(void) mkdir(buf, S_IRWXU);
884 
885 	(void) strcat(buf, "/stage2");
886 	pcfs_fp = open(buf, O_WRONLY | O_CREAT, S_IRWXU);
887 	if (pcfs_fp == -1) {
888 		(void) fprintf(stderr, OPEN_FAIL_FILE, buf);
889 		perror("open:");
890 		(void) umount(TMP_MNTPT);
891 		exit(-1);
892 	}
893 
894 	/* write stage2 to pcfs */
895 	for (i = 0; ; i++) {
896 		int nread, nwrite;
897 		nread = pread(stage2_fd, buf, SECTOR_SIZE, i * SECTOR_SIZE);
898 		if (nowrite)
899 			nwrite = nread;
900 		else
901 			nwrite = pwrite(pcfs_fp, buf, nread, i * SECTOR_SIZE);
902 		if (nread < 0 || nwrite != nread) {
903 			(void) fprintf(stderr, WRITE_FAIL_STAGE2_BLOCKS,
904 			    nread, nwrite);
905 			break;
906 		}
907 		if (nread < SECTOR_SIZE)
908 			break;	/* end of file */
909 	}
910 	(void) close(pcfs_fp);
911 	(void) umount(TMP_MNTPT);
912 
913 	/*
914 	 * Now, get the blocklist from the device.
915 	 */
916 	bzero(blocklist, sizeof (blocklist));
917 	if (read_stage2_blocklist(dev_fd, blocklist) != 0)
918 		exit(-1);
919 }
920