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 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 237c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <sys/param.h> 307c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 317c478bd9Sstevel@tonic-gate #include <sys/bootconf.h> 327c478bd9Sstevel@tonic-gate #include <sys/bootvfs.h> 337c478bd9Sstevel@tonic-gate #include <sys/filep.h> 347c478bd9Sstevel@tonic-gate #include <sys/kobj.h> 357c478bd9Sstevel@tonic-gate #include <sys/varargs.h> 367c478bd9Sstevel@tonic-gate #include <sys/reboot.h> 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate extern void (*_kobj_printf)(void *, const char *fmt, ...); 397c478bd9Sstevel@tonic-gate extern int get_weakish_int(int *); 407c478bd9Sstevel@tonic-gate extern struct bootops *ops; 417c478bd9Sstevel@tonic-gate extern struct boot_fs_ops bufs_ops, bhsfs_ops; 427c478bd9Sstevel@tonic-gate extern int kmem_ready; 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate static uint64_t rd_start, rd_end; 457c478bd9Sstevel@tonic-gate struct boot_fs_ops *bfs_ops; 467c478bd9Sstevel@tonic-gate struct boot_fs_ops *bfs_tab[] = {&bufs_ops, &bhsfs_ops, NULL}; 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate #ifdef DEBUG 497c478bd9Sstevel@tonic-gate static uint64_t scratch_max; 507c478bd9Sstevel@tonic-gate #endif 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate #define _kmem_ready get_weakish_int(&kmem_ready) 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate /* 557c478bd9Sstevel@tonic-gate * This one reads the ramdisk. If fi_memp is set, we copy the 567c478bd9Sstevel@tonic-gate * ramdisk content to the designated buffer. Otherwise, we 577c478bd9Sstevel@tonic-gate * do a "cached" read (set fi_memp to the actual ramdisk buffer). 587c478bd9Sstevel@tonic-gate */ 597c478bd9Sstevel@tonic-gate int 607c478bd9Sstevel@tonic-gate diskread(fileid_t *filep) 617c478bd9Sstevel@tonic-gate { 627c478bd9Sstevel@tonic-gate uint_t blocknum; 637c478bd9Sstevel@tonic-gate caddr_t diskloc; 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate /* add in offset of root slice */ 667c478bd9Sstevel@tonic-gate blocknum = filep->fi_blocknum; 677c478bd9Sstevel@tonic-gate 68*2269adc8Sszhou diskloc = (caddr_t)(uintptr_t)rd_start + blocknum * DEV_BSIZE; 69*2269adc8Sszhou if (diskloc + filep->fi_count > (caddr_t)(uintptr_t)rd_end) { 707c478bd9Sstevel@tonic-gate _kobj_printf(ops, "diskread: start = 0x%p, size = 0x%x\n", 717c478bd9Sstevel@tonic-gate diskloc, filep->fi_count); 727c478bd9Sstevel@tonic-gate _kobj_printf(ops, "reading beyond end of ramdisk\n"); 737c478bd9Sstevel@tonic-gate return (-1); 747c478bd9Sstevel@tonic-gate } 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate if (filep->fi_memp) { 777c478bd9Sstevel@tonic-gate bcopy(diskloc, filep->fi_memp, filep->fi_count); 787c478bd9Sstevel@tonic-gate } else { 797c478bd9Sstevel@tonic-gate /* "cached" read */ 807c478bd9Sstevel@tonic-gate filep->fi_memp = diskloc; 817c478bd9Sstevel@tonic-gate } 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate return (0); 847c478bd9Sstevel@tonic-gate } 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate int 877c478bd9Sstevel@tonic-gate kobj_boot_mountroot() 887c478bd9Sstevel@tonic-gate { 897c478bd9Sstevel@tonic-gate int i; 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate if (BOP_GETPROPLEN(ops, "ramdisk_start") != 8 || 927c478bd9Sstevel@tonic-gate BOP_GETPROP(ops, "ramdisk_start", (void *)&rd_start) != 0 || 937c478bd9Sstevel@tonic-gate BOP_GETPROPLEN(ops, "ramdisk_end") != 8 || 947c478bd9Sstevel@tonic-gate BOP_GETPROP(ops, "ramdisk_end", (void *)&rd_end) != 0) { 957c478bd9Sstevel@tonic-gate _kobj_printf(ops, 967c478bd9Sstevel@tonic-gate "failed to get ramdisk from boot\n"); 977c478bd9Sstevel@tonic-gate return (-1); 987c478bd9Sstevel@tonic-gate } 997c478bd9Sstevel@tonic-gate #ifdef KOBJ_DEBUG 1007c478bd9Sstevel@tonic-gate _kobj_printf(ops, 1017c478bd9Sstevel@tonic-gate "ramdisk range: 0x%llx-%llx\n", rd_start, rd_end); 1027c478bd9Sstevel@tonic-gate #endif 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate for (i = 0; bfs_tab[i] != NULL; i++) { 1057c478bd9Sstevel@tonic-gate bfs_ops = bfs_tab[i]; 1067c478bd9Sstevel@tonic-gate if (BRD_MOUNTROOT(bfs_ops, "dummy") == 0) 1077c478bd9Sstevel@tonic-gate return (0); 1087c478bd9Sstevel@tonic-gate } 1097c478bd9Sstevel@tonic-gate _kobj_printf(ops, "failed to mount ramdisk from boot\n"); 1107c478bd9Sstevel@tonic-gate return (-1); 1117c478bd9Sstevel@tonic-gate } 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate void 1147c478bd9Sstevel@tonic-gate kobj_boot_unmountroot() 1157c478bd9Sstevel@tonic-gate { 1167c478bd9Sstevel@tonic-gate #ifdef DEBUG 1177c478bd9Sstevel@tonic-gate if (boothowto & RB_VERBOSE) 1187c478bd9Sstevel@tonic-gate _kobj_printf(ops, "boot scratch memory used: 0x%llx\n", 1197c478bd9Sstevel@tonic-gate scratch_max); 1207c478bd9Sstevel@tonic-gate #endif 1217c478bd9Sstevel@tonic-gate (void) BRD_UNMOUNTROOT(bfs_ops); 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate void * 1257c478bd9Sstevel@tonic-gate bkmem_alloc(size_t size) 1267c478bd9Sstevel@tonic-gate { 1277c478bd9Sstevel@tonic-gate /* allocate from boot scratch memory */ 1287c478bd9Sstevel@tonic-gate void *addr; 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate if (_kmem_ready) 1317c478bd9Sstevel@tonic-gate return (kobj_alloc(size, 0)); 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate addr = BOP_ALLOC(ops, 0, size, 0); 1347c478bd9Sstevel@tonic-gate #ifdef DEBUG 135*2269adc8Sszhou if (scratch_max < (uintptr_t)addr + size) 136*2269adc8Sszhou scratch_max = (uintptr_t)addr + size; 1377c478bd9Sstevel@tonic-gate #endif 1387c478bd9Sstevel@tonic-gate return (addr); 1397c478bd9Sstevel@tonic-gate } 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 1427c478bd9Sstevel@tonic-gate void 1437c478bd9Sstevel@tonic-gate bkmem_free(void *p, size_t size) 1447c478bd9Sstevel@tonic-gate { 1457c478bd9Sstevel@tonic-gate /* 1467c478bd9Sstevel@tonic-gate * Don't bother freeing scratch memory 1477c478bd9Sstevel@tonic-gate * Note that in amd64, BOP_ALLOC returns address 1487c478bd9Sstevel@tonic-gate * prepended with 0xffffffff, so we cast to 32-bit 1497c478bd9Sstevel@tonic-gate * before comparing. 1507c478bd9Sstevel@tonic-gate */ 1517c478bd9Sstevel@tonic-gate if ((uint32_t)(uintptr_t)p > MAGIC_PHYS) 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