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 /* 2219397407SSherry Moore * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 24*0181461bSKeith M Wesolowski * Copyright 2013 Joyent, Inc. All rights reserved. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 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; 40*0181461bSKeith M Wesolowski extern struct boot_fs_ops bufs_ops, bhsfs_ops, bbootfs_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; 45*0181461bSKeith M Wesolowski struct boot_fs_ops *bfs_tab[] = {&bufs_ops, &bhsfs_ops, &bbootfs_ops, NULL}; 467c478bd9Sstevel@tonic-gate 4719397407SSherry Moore static uintptr_t scratch_max = 0; 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate #define _kmem_ready get_weakish_int(&kmem_ready) 507c478bd9Sstevel@tonic-gate 51*0181461bSKeith M Wesolowski int 52*0181461bSKeith M Wesolowski BRD_MOUNTROOT(struct boot_fs_ops *ops, char *str) 53*0181461bSKeith M Wesolowski { 54*0181461bSKeith M Wesolowski return (ops->fsw_mountroot(str)); 55*0181461bSKeith M Wesolowski } 56*0181461bSKeith M Wesolowski 57*0181461bSKeith M Wesolowski int 58*0181461bSKeith M Wesolowski BRD_UNMOUNTROOT(struct boot_fs_ops *ops) 59*0181461bSKeith M Wesolowski { 60*0181461bSKeith M Wesolowski if (bfs_ops != &bbootfs_ops) 61*0181461bSKeith M Wesolowski bbootfs_ops.fsw_closeall(1); 62*0181461bSKeith M Wesolowski 63*0181461bSKeith M Wesolowski return (ops->fsw_unmountroot()); 64*0181461bSKeith M Wesolowski } 65*0181461bSKeith M Wesolowski 66*0181461bSKeith M Wesolowski int 67*0181461bSKeith M Wesolowski BRD_OPEN(struct boot_fs_ops *ops, char *file, int flags) 68*0181461bSKeith M Wesolowski { 69*0181461bSKeith M Wesolowski int len = strlen(SYSTEM_BOOT_PATH); 70*0181461bSKeith M Wesolowski int fd; 71*0181461bSKeith M Wesolowski 72*0181461bSKeith M Wesolowski /* 73*0181461bSKeith M Wesolowski * Our policy is that we try bootfs first. If bootfs is the only 74*0181461bSKeith M Wesolowski * filesystem, that's the end of it. Otherwise we will fall back to 75*0181461bSKeith M Wesolowski * the normal root (i.e., ramdisk) filesystem at this point and try 76*0181461bSKeith M Wesolowski * again if the file does not exist in bootfs. 77*0181461bSKeith M Wesolowski */ 78*0181461bSKeith M Wesolowski fd = bbootfs_ops.fsw_open(file, flags); 79*0181461bSKeith M Wesolowski 80*0181461bSKeith M Wesolowski if (bfs_ops == &bbootfs_ops) 81*0181461bSKeith M Wesolowski return (fd); 82*0181461bSKeith M Wesolowski 83*0181461bSKeith M Wesolowski if (strncmp(file, SYSTEM_BOOT_PATH, len) == 0 || fd >= 0) 84*0181461bSKeith M Wesolowski return ((fd < 0) ? fd : (fd | BFD_F_SYSTEM_BOOT)); 85*0181461bSKeith M Wesolowski 86*0181461bSKeith M Wesolowski return (ops->fsw_open(file, flags)); 87*0181461bSKeith M Wesolowski } 88*0181461bSKeith M Wesolowski 89*0181461bSKeith M Wesolowski int 90*0181461bSKeith M Wesolowski BRD_CLOSE(struct boot_fs_ops *ops, int fd) 91*0181461bSKeith M Wesolowski { 92*0181461bSKeith M Wesolowski if (fd & BFD_F_SYSTEM_BOOT) 93*0181461bSKeith M Wesolowski return (bbootfs_ops.fsw_close(fd & ~BFD_F_SYSTEM_BOOT)); 94*0181461bSKeith M Wesolowski 95*0181461bSKeith M Wesolowski return (ops->fsw_close(fd)); 96*0181461bSKeith M Wesolowski } 97*0181461bSKeith M Wesolowski 98*0181461bSKeith M Wesolowski ssize_t 99*0181461bSKeith M Wesolowski BRD_READ(struct boot_fs_ops *ops, int fd, caddr_t buf, size_t len) 100*0181461bSKeith M Wesolowski { 101*0181461bSKeith M Wesolowski if (fd & BFD_F_SYSTEM_BOOT) { 102*0181461bSKeith M Wesolowski return (bbootfs_ops.fsw_read(fd & ~BFD_F_SYSTEM_BOOT, 103*0181461bSKeith M Wesolowski buf, len)); 104*0181461bSKeith M Wesolowski } 105*0181461bSKeith M Wesolowski 106*0181461bSKeith M Wesolowski return (ops->fsw_read(fd, buf, len)); 107*0181461bSKeith M Wesolowski } 108*0181461bSKeith M Wesolowski 109*0181461bSKeith M Wesolowski off_t 110*0181461bSKeith M Wesolowski BRD_SEEK(struct boot_fs_ops *ops, int fd, off_t addr, int whence) 111*0181461bSKeith M Wesolowski { 112*0181461bSKeith M Wesolowski if (fd & BFD_F_SYSTEM_BOOT) { 113*0181461bSKeith M Wesolowski return (bbootfs_ops.fsw_lseek(fd & ~BFD_F_SYSTEM_BOOT, 114*0181461bSKeith M Wesolowski addr, whence)); 115*0181461bSKeith M Wesolowski } 116*0181461bSKeith M Wesolowski 117*0181461bSKeith M Wesolowski return (ops->fsw_lseek(fd, addr, whence)); 118*0181461bSKeith M Wesolowski } 119*0181461bSKeith M Wesolowski 120*0181461bSKeith M Wesolowski int 121*0181461bSKeith M Wesolowski BRD_FSTAT(struct boot_fs_ops *ops, int fd, struct bootstat *bsp) 122*0181461bSKeith M Wesolowski { 123*0181461bSKeith M Wesolowski if (fd & BFD_F_SYSTEM_BOOT) 124*0181461bSKeith M Wesolowski return (bbootfs_ops.fsw_fstat(fd & ~BFD_F_SYSTEM_BOOT, bsp)); 125*0181461bSKeith M Wesolowski 126*0181461bSKeith M Wesolowski return (ops->fsw_fstat(fd, bsp)); 127*0181461bSKeith M Wesolowski } 128*0181461bSKeith M Wesolowski 1297c478bd9Sstevel@tonic-gate /* 1307c478bd9Sstevel@tonic-gate * This one reads the ramdisk. If fi_memp is set, we copy the 1317c478bd9Sstevel@tonic-gate * ramdisk content to the designated buffer. Otherwise, we 1327c478bd9Sstevel@tonic-gate * do a "cached" read (set fi_memp to the actual ramdisk buffer). 1337c478bd9Sstevel@tonic-gate */ 1347c478bd9Sstevel@tonic-gate int 1357c478bd9Sstevel@tonic-gate diskread(fileid_t *filep) 1367c478bd9Sstevel@tonic-gate { 1377c478bd9Sstevel@tonic-gate uint_t blocknum; 1387c478bd9Sstevel@tonic-gate caddr_t diskloc; 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate /* add in offset of root slice */ 1417c478bd9Sstevel@tonic-gate blocknum = filep->fi_blocknum; 1427c478bd9Sstevel@tonic-gate 1432269adc8Sszhou diskloc = (caddr_t)(uintptr_t)rd_start + blocknum * DEV_BSIZE; 1442269adc8Sszhou if (diskloc + filep->fi_count > (caddr_t)(uintptr_t)rd_end) { 1457c478bd9Sstevel@tonic-gate _kobj_printf(ops, "diskread: start = 0x%p, size = 0x%x\n", 1467c478bd9Sstevel@tonic-gate diskloc, filep->fi_count); 1477c478bd9Sstevel@tonic-gate _kobj_printf(ops, "reading beyond end of ramdisk\n"); 1487c478bd9Sstevel@tonic-gate return (-1); 1497c478bd9Sstevel@tonic-gate } 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate if (filep->fi_memp) { 1527c478bd9Sstevel@tonic-gate bcopy(diskloc, filep->fi_memp, filep->fi_count); 1537c478bd9Sstevel@tonic-gate } else { 1547c478bd9Sstevel@tonic-gate /* "cached" read */ 1557c478bd9Sstevel@tonic-gate filep->fi_memp = diskloc; 1567c478bd9Sstevel@tonic-gate } 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate return (0); 1597c478bd9Sstevel@tonic-gate } 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate int 1627c478bd9Sstevel@tonic-gate kobj_boot_mountroot() 1637c478bd9Sstevel@tonic-gate { 1647c478bd9Sstevel@tonic-gate int i; 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate if (BOP_GETPROPLEN(ops, "ramdisk_start") != 8 || 1677c478bd9Sstevel@tonic-gate BOP_GETPROP(ops, "ramdisk_start", (void *)&rd_start) != 0 || 1687c478bd9Sstevel@tonic-gate BOP_GETPROPLEN(ops, "ramdisk_end") != 8 || 1697c478bd9Sstevel@tonic-gate BOP_GETPROP(ops, "ramdisk_end", (void *)&rd_end) != 0) { 1707c478bd9Sstevel@tonic-gate _kobj_printf(ops, 1717c478bd9Sstevel@tonic-gate "failed to get ramdisk from boot\n"); 1727c478bd9Sstevel@tonic-gate return (-1); 1737c478bd9Sstevel@tonic-gate } 1747c478bd9Sstevel@tonic-gate #ifdef KOBJ_DEBUG 1757c478bd9Sstevel@tonic-gate _kobj_printf(ops, 1767c478bd9Sstevel@tonic-gate "ramdisk range: 0x%llx-%llx\n", rd_start, rd_end); 1777c478bd9Sstevel@tonic-gate #endif 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate for (i = 0; bfs_tab[i] != NULL; i++) { 1807c478bd9Sstevel@tonic-gate bfs_ops = bfs_tab[i]; 1817c478bd9Sstevel@tonic-gate if (BRD_MOUNTROOT(bfs_ops, "dummy") == 0) 1827c478bd9Sstevel@tonic-gate return (0); 1837c478bd9Sstevel@tonic-gate } 1847c478bd9Sstevel@tonic-gate _kobj_printf(ops, "failed to mount ramdisk from boot\n"); 1857c478bd9Sstevel@tonic-gate return (-1); 1867c478bd9Sstevel@tonic-gate } 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate void 1897c478bd9Sstevel@tonic-gate kobj_boot_unmountroot() 1907c478bd9Sstevel@tonic-gate { 1917c478bd9Sstevel@tonic-gate #ifdef DEBUG 1927c478bd9Sstevel@tonic-gate if (boothowto & RB_VERBOSE) 193ae115bc7Smrj _kobj_printf(ops, "boot scratch memory used: 0x%lx\n", 1947c478bd9Sstevel@tonic-gate scratch_max); 1957c478bd9Sstevel@tonic-gate #endif 1967c478bd9Sstevel@tonic-gate (void) BRD_UNMOUNTROOT(bfs_ops); 1977c478bd9Sstevel@tonic-gate } 1987c478bd9Sstevel@tonic-gate 199ae115bc7Smrj /* 200ae115bc7Smrj * Boot time wrappers for memory allocators. Called for both permanent 201ae115bc7Smrj * and temporary boot memory allocations. We have to track which allocator 202ae115bc7Smrj * (boot or kmem) was used so that we know how to free. 203ae115bc7Smrj */ 2047c478bd9Sstevel@tonic-gate void * 2057c478bd9Sstevel@tonic-gate bkmem_alloc(size_t size) 2067c478bd9Sstevel@tonic-gate { 2077c478bd9Sstevel@tonic-gate /* allocate from boot scratch memory */ 2087c478bd9Sstevel@tonic-gate void *addr; 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate if (_kmem_ready) 2117c478bd9Sstevel@tonic-gate return (kobj_alloc(size, 0)); 2127c478bd9Sstevel@tonic-gate 213ae115bc7Smrj /* 214ae115bc7Smrj * Remember the highest BOP_ALLOC allocated address and don't free 215ae115bc7Smrj * anything below it. 216ae115bc7Smrj */ 2177c478bd9Sstevel@tonic-gate addr = BOP_ALLOC(ops, 0, size, 0); 2182269adc8Sszhou if (scratch_max < (uintptr_t)addr + size) 2192269adc8Sszhou scratch_max = (uintptr_t)addr + size; 2207c478bd9Sstevel@tonic-gate return (addr); 2217c478bd9Sstevel@tonic-gate } 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 2247c478bd9Sstevel@tonic-gate void 2257c478bd9Sstevel@tonic-gate bkmem_free(void *p, size_t size) 2267c478bd9Sstevel@tonic-gate { 2277c478bd9Sstevel@tonic-gate /* 228ae115bc7Smrj * Free only if it's not boot scratch memory. 2297c478bd9Sstevel@tonic-gate */ 230ae115bc7Smrj if ((uintptr_t)p >= scratch_max) 2317c478bd9Sstevel@tonic-gate kobj_free(p, size); 2327c478bd9Sstevel@tonic-gate } 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 2357c478bd9Sstevel@tonic-gate void 2367c478bd9Sstevel@tonic-gate kobj_printf(char *fmt, ...) 2377c478bd9Sstevel@tonic-gate { 2387c478bd9Sstevel@tonic-gate va_list adx; 2397c478bd9Sstevel@tonic-gate 2407c478bd9Sstevel@tonic-gate va_start(adx, fmt); 2417c478bd9Sstevel@tonic-gate _kobj_printf(ops, fmt, adx); 2427c478bd9Sstevel@tonic-gate va_end(adx); 2437c478bd9Sstevel@tonic-gate } 244