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