17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5342440ecSPrasad Singamsetty * Common Development and Distribution License (the "License").
6342440ecSPrasad Singamsetty * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22af28f636SEnrico Perla - Sun Microsystems * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate */
247c478bd9Sstevel@tonic-gate
257c478bd9Sstevel@tonic-gate #include <stdio.h>
267c478bd9Sstevel@tonic-gate #include <stdlib.h>
277c478bd9Sstevel@tonic-gate #include <unistd.h>
287c478bd9Sstevel@tonic-gate #include <sys/param.h>
297c478bd9Sstevel@tonic-gate #include <sys/bootvfs.h>
307c478bd9Sstevel@tonic-gate #include <sys/filep.h>
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate #include <libintl.h>
337c478bd9Sstevel@tonic-gate #include <locale.h>
347c478bd9Sstevel@tonic-gate #include "message.h"
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate * This file is glue layer to pcfs module in usr/src/common/fs/pcfs.c.
387c478bd9Sstevel@tonic-gate * It's main functionality is to get the stage file blocklist. It's
39af28f636SEnrico Perla - Sun Microsystems * used for installing grub on a Solaris boot partition.
407c478bd9Sstevel@tonic-gate */
417c478bd9Sstevel@tonic-gate extern struct boot_fs_ops bpcfs_ops;
427c478bd9Sstevel@tonic-gate struct boot_fs_ops *bfs_ops;
437c478bd9Sstevel@tonic-gate struct boot_fs_ops *bfs_tab[] = {&bpcfs_ops, NULL};
447c478bd9Sstevel@tonic-gate static int dev_fd;
457c478bd9Sstevel@tonic-gate int bootrd_debug = 0;
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate #define DEV_BSIZE 512
487c478bd9Sstevel@tonic-gate #define MAX_CHUNK 64
497c478bd9Sstevel@tonic-gate
50342440ecSPrasad Singamsetty static unsigned int *blocklist;
517c478bd9Sstevel@tonic-gate
52*d0b12b66SToomas Soome /* diskread_callback is set in filesystem module (pcfs.c) */
537c478bd9Sstevel@tonic-gate int (*diskread_callback)(int, int);
547c478bd9Sstevel@tonic-gate int (*fileread_callback)(int, int);
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate static int
add_stage2_block(int blocknum,int nblk)577c478bd9Sstevel@tonic-gate add_stage2_block(int blocknum, int nblk)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate static int i = -2;
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate if (i >= 0 && (blocklist[i] + blocklist[i + 1] == blocknum)) {
627c478bd9Sstevel@tonic-gate blocklist[i + 1] += nblk;
637c478bd9Sstevel@tonic-gate return (0);
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate i += 2;
677c478bd9Sstevel@tonic-gate if (i >= DEV_BSIZE / 8) {
687c478bd9Sstevel@tonic-gate fprintf(stderr, PCFS_FRAGMENTED);
697c478bd9Sstevel@tonic-gate exit(-1);
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate blocklist[i] = blocknum;
727c478bd9Sstevel@tonic-gate blocklist[i + 1] = nblk;
737c478bd9Sstevel@tonic-gate return (0);
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate * This one reads the ramdisk. If fi_memp is set, we copy the
787c478bd9Sstevel@tonic-gate * ramdisk content to the designated buffer. Otherwise, we
797c478bd9Sstevel@tonic-gate * do a "cached" read (set fi_memp to the actual ramdisk buffer).
807c478bd9Sstevel@tonic-gate */
817c478bd9Sstevel@tonic-gate int
diskread(fileid_t * filep)827c478bd9Sstevel@tonic-gate diskread(fileid_t *filep)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate int ret;
857c478bd9Sstevel@tonic-gate uint_t blocknum, diskloc;
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate blocknum = filep->fi_blocknum;
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate if (diskread_callback) {
907c478bd9Sstevel@tonic-gate diskread_callback(blocknum, filep->fi_count / DEV_BSIZE);
917c478bd9Sstevel@tonic-gate return (0);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate diskloc = blocknum * DEV_BSIZE;
957c478bd9Sstevel@tonic-gate if (filep->fi_memp == NULL) {
967c478bd9Sstevel@tonic-gate filep->fi_memp = malloc(filep->fi_count);
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate if (filep->fi_memp == NULL) {
997c478bd9Sstevel@tonic-gate fprintf(stderr, OUT_OF_MEMORY);
1007c478bd9Sstevel@tonic-gate return (-1);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate ret = pread(dev_fd, filep->fi_memp, filep->fi_count, diskloc);
1047c478bd9Sstevel@tonic-gate if (ret < 0)
1057c478bd9Sstevel@tonic-gate perror("diskread: pread");
1067c478bd9Sstevel@tonic-gate return (ret >= 0 ? 0 : -1);
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate void *
bkmem_alloc(size_t s)1107c478bd9Sstevel@tonic-gate bkmem_alloc(size_t s)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate return (malloc(s));
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1167c478bd9Sstevel@tonic-gate void
bkmem_free(void * p,size_t s)1177c478bd9Sstevel@tonic-gate bkmem_free(void *p, size_t s)
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate free(p);
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate static int
mountroot(char * name)1237c478bd9Sstevel@tonic-gate mountroot(char *name)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate int i;
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate /* try ops in bfs_tab and return the first successful one */
1287c478bd9Sstevel@tonic-gate for (i = 0; bfs_tab[i] != NULL; i++) {
1297c478bd9Sstevel@tonic-gate bfs_ops = bfs_tab[i];
1307c478bd9Sstevel@tonic-gate if (BRD_MOUNTROOT(bfs_ops, name) == 0)
1317c478bd9Sstevel@tonic-gate return (0);
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate return (-1);
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate static int
unmountroot()1377c478bd9Sstevel@tonic-gate unmountroot()
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate return (BRD_UNMOUNTROOT(bfs_ops));
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate static int
pcfs_glue_open(const char * filename,int flags)143af28f636SEnrico Perla - Sun Microsystems pcfs_glue_open(const char *filename, int flags)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate return (BRD_OPEN(bfs_ops, (char *)filename, flags));
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate static int
pcfs_glue_close(int fd)149af28f636SEnrico Perla - Sun Microsystems pcfs_glue_close(int fd)
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate return (BRD_CLOSE(bfs_ops, fd));
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate static ssize_t
pcfs_glue_read(int fd,void * buf,size_t size)155af28f636SEnrico Perla - Sun Microsystems pcfs_glue_read(int fd, void *buf, size_t size)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate return (BRD_READ(bfs_ops, fd, buf, size));
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate * Get the blocklist for stage2
1627c478bd9Sstevel@tonic-gate */
1637c478bd9Sstevel@tonic-gate int
read_stage2_blocklist(int device_fd,unsigned int * blkbuf)164342440ecSPrasad Singamsetty read_stage2_blocklist(int device_fd, unsigned int *blkbuf)
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate int i, fd, stage2_block;
1677c478bd9Sstevel@tonic-gate char buf[DEV_BSIZE];
1687c478bd9Sstevel@tonic-gate ssize_t size;
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate dev_fd = device_fd;
1717c478bd9Sstevel@tonic-gate if (mountroot("dummy") != 0) {
1727c478bd9Sstevel@tonic-gate fprintf(stderr, MOUNT_FAIL_PCFS);
1737c478bd9Sstevel@tonic-gate return (-1);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate
176af28f636SEnrico Perla - Sun Microsystems if ((fd = pcfs_glue_open("/boot/grub/stage2", 0)) == -1) {
1777c478bd9Sstevel@tonic-gate fprintf(stderr, OPEN_FAIL_PCFS);
1787c478bd9Sstevel@tonic-gate return (-1);
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate if (bootrd_debug)
1827c478bd9Sstevel@tonic-gate (void) printf("start reading stage2:\n");
1837c478bd9Sstevel@tonic-gate stage2_block = 0;
1847c478bd9Sstevel@tonic-gate blocklist = blkbuf;
1857c478bd9Sstevel@tonic-gate fileread_callback = add_stage2_block;
1867c478bd9Sstevel@tonic-gate for (;;) {
187af28f636SEnrico Perla - Sun Microsystems size = pcfs_glue_read(fd, buf, DEV_BSIZE);
1887c478bd9Sstevel@tonic-gate if (size != DEV_BSIZE)
1897c478bd9Sstevel@tonic-gate break;
1907c478bd9Sstevel@tonic-gate stage2_block++;
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate fileread_callback = NULL;
193af28f636SEnrico Perla - Sun Microsystems (void) pcfs_glue_close(fd);
1947c478bd9Sstevel@tonic-gate
1957c478bd9Sstevel@tonic-gate if (bootrd_debug) {
1967c478bd9Sstevel@tonic-gate (void) printf("last block size = %d\n", size);
1977c478bd9Sstevel@tonic-gate for (i = 0; blocklist[i] != 0; i += 2) {
1987c478bd9Sstevel@tonic-gate (void) printf("sectors: %d-%d\n",
1997c478bd9Sstevel@tonic-gate blocklist[i],
2007c478bd9Sstevel@tonic-gate blocklist[i] + blocklist[i + 1] - 1);
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate (void) printf("total blocks in stage 2: %d\n", stage2_block);
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate
2057c478bd9Sstevel@tonic-gate (void) unmountroot();
2067c478bd9Sstevel@tonic-gate return (0);
2077c478bd9Sstevel@tonic-gate }
208