xref: /titanic_50/usr/src/uts/common/krtld/bootrd.c (revision ae115bc77f6fcde83175c75b4206dc2e50747966)
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
5*ae115bc7Smrj  * Common Development and Distribution License (the "License").
6*ae115bc7Smrj  * 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 /*
22*ae115bc7Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/param.h>
297c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
307c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
317c478bd9Sstevel@tonic-gate #include <sys/bootvfs.h>
327c478bd9Sstevel@tonic-gate #include <sys/filep.h>
337c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
347c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
357c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate extern void (*_kobj_printf)(void *, const char *fmt, ...);
387c478bd9Sstevel@tonic-gate extern int get_weakish_int(int *);
397c478bd9Sstevel@tonic-gate extern struct bootops *ops;
407c478bd9Sstevel@tonic-gate extern struct boot_fs_ops bufs_ops, bhsfs_ops;
417c478bd9Sstevel@tonic-gate extern int kmem_ready;
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate static uint64_t rd_start, rd_end;
447c478bd9Sstevel@tonic-gate struct boot_fs_ops *bfs_ops;
457c478bd9Sstevel@tonic-gate struct boot_fs_ops *bfs_tab[] = {&bufs_ops, &bhsfs_ops, NULL};
467c478bd9Sstevel@tonic-gate 
47*ae115bc7Smrj static uintptr_t scratch_max;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #define	_kmem_ready	get_weakish_int(&kmem_ready)
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * This one reads the ramdisk. If fi_memp is set, we copy the
537c478bd9Sstevel@tonic-gate  * ramdisk content to the designated buffer. Otherwise, we
547c478bd9Sstevel@tonic-gate  * do a "cached" read (set fi_memp to the actual ramdisk buffer).
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate int
577c478bd9Sstevel@tonic-gate diskread(fileid_t *filep)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	uint_t blocknum;
607c478bd9Sstevel@tonic-gate 	caddr_t diskloc;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	/* add in offset of root slice */
637c478bd9Sstevel@tonic-gate 	blocknum = filep->fi_blocknum;
647c478bd9Sstevel@tonic-gate 
652269adc8Sszhou 	diskloc = (caddr_t)(uintptr_t)rd_start + blocknum * DEV_BSIZE;
662269adc8Sszhou 	if (diskloc + filep->fi_count > (caddr_t)(uintptr_t)rd_end) {
677c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "diskread: start = 0x%p, size = 0x%x\n",
687c478bd9Sstevel@tonic-gate 		    diskloc, filep->fi_count);
697c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "reading beyond end of ramdisk\n");
707c478bd9Sstevel@tonic-gate 		return (-1);
717c478bd9Sstevel@tonic-gate 	}
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	if (filep->fi_memp) {
747c478bd9Sstevel@tonic-gate 		bcopy(diskloc, filep->fi_memp, filep->fi_count);
757c478bd9Sstevel@tonic-gate 	} else {
767c478bd9Sstevel@tonic-gate 		/* "cached" read */
777c478bd9Sstevel@tonic-gate 		filep->fi_memp = diskloc;
787c478bd9Sstevel@tonic-gate 	}
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	return (0);
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate int
847c478bd9Sstevel@tonic-gate kobj_boot_mountroot()
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate 	int i;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	if (BOP_GETPROPLEN(ops, "ramdisk_start") != 8 ||
897c478bd9Sstevel@tonic-gate 	    BOP_GETPROP(ops, "ramdisk_start", (void *)&rd_start) != 0 ||
907c478bd9Sstevel@tonic-gate 	    BOP_GETPROPLEN(ops, "ramdisk_end") != 8 ||
917c478bd9Sstevel@tonic-gate 	    BOP_GETPROP(ops, "ramdisk_end", (void *)&rd_end) != 0) {
927c478bd9Sstevel@tonic-gate 		_kobj_printf(ops,
937c478bd9Sstevel@tonic-gate 		    "failed to get ramdisk from boot\n");
947c478bd9Sstevel@tonic-gate 		return (-1);
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate #ifdef KOBJ_DEBUG
977c478bd9Sstevel@tonic-gate 	_kobj_printf(ops,
987c478bd9Sstevel@tonic-gate 	    "ramdisk range: 0x%llx-%llx\n", rd_start, rd_end);
997c478bd9Sstevel@tonic-gate #endif
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	for (i = 0; bfs_tab[i] != NULL; i++) {
1027c478bd9Sstevel@tonic-gate 		bfs_ops = bfs_tab[i];
1037c478bd9Sstevel@tonic-gate 		if (BRD_MOUNTROOT(bfs_ops, "dummy") == 0)
1047c478bd9Sstevel@tonic-gate 			return (0);
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate 	_kobj_printf(ops, "failed to mount ramdisk from boot\n");
1077c478bd9Sstevel@tonic-gate 	return (-1);
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate void
1117c478bd9Sstevel@tonic-gate kobj_boot_unmountroot()
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1147c478bd9Sstevel@tonic-gate 	if (boothowto & RB_VERBOSE)
115*ae115bc7Smrj 		_kobj_printf(ops, "boot scratch memory used: 0x%lx\n",
1167c478bd9Sstevel@tonic-gate 		    scratch_max);
1177c478bd9Sstevel@tonic-gate #endif
1187c478bd9Sstevel@tonic-gate 	(void) BRD_UNMOUNTROOT(bfs_ops);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
121*ae115bc7Smrj /*
122*ae115bc7Smrj  * Boot time wrappers for memory allocators. Called for both permanent
123*ae115bc7Smrj  * and temporary boot memory allocations. We have to track which allocator
124*ae115bc7Smrj  * (boot or kmem) was used so that we know how to free.
125*ae115bc7Smrj  */
1267c478bd9Sstevel@tonic-gate void *
1277c478bd9Sstevel@tonic-gate bkmem_alloc(size_t size)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	/* allocate from boot scratch memory */
1307c478bd9Sstevel@tonic-gate 	void *addr;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	if (_kmem_ready)
1337c478bd9Sstevel@tonic-gate 		return (kobj_alloc(size, 0));
1347c478bd9Sstevel@tonic-gate 
135*ae115bc7Smrj 	/*
136*ae115bc7Smrj 	 * Remember the highest BOP_ALLOC allocated address and don't free
137*ae115bc7Smrj 	 * anything below it.
138*ae115bc7Smrj 	 */
1397c478bd9Sstevel@tonic-gate 	addr = BOP_ALLOC(ops, 0, size, 0);
1402269adc8Sszhou 	if (scratch_max < (uintptr_t)addr + size)
1412269adc8Sszhou 		scratch_max = (uintptr_t)addr + size;
1427c478bd9Sstevel@tonic-gate 	return (addr);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1467c478bd9Sstevel@tonic-gate void
1477c478bd9Sstevel@tonic-gate bkmem_free(void *p, size_t size)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	/*
150*ae115bc7Smrj 	 * Free only if it's not boot scratch memory.
1517c478bd9Sstevel@tonic-gate 	 */
152*ae115bc7Smrj 	if ((uintptr_t)p >= scratch_max)
1537c478bd9Sstevel@tonic-gate 		kobj_free(p, size);
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1577c478bd9Sstevel@tonic-gate void
1587c478bd9Sstevel@tonic-gate kobj_printf(char *fmt, ...)
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate 	va_list adx;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	va_start(adx, fmt);
1637c478bd9Sstevel@tonic-gate 	_kobj_printf(ops, fmt, adx);
1647c478bd9Sstevel@tonic-gate 	va_end(adx);
1657c478bd9Sstevel@tonic-gate }
166