Merge remote-tracking branch 'origin/master'
[unleashed/lotheac.git] / kernel / krtld / bootrd.c
blob161c0a26f59a5b053a33e995fea4050263bef249
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 * Copyright 2013 Joyent, Inc. All rights reserved.
28 #include <sys/param.h>
29 #include <sys/sunddi.h>
30 #include <sys/bootconf.h>
31 #include <sys/bootvfs.h>
32 #include <sys/filep.h>
33 #include <sys/kobj.h>
34 #include <sys/varargs.h>
35 #include <sys/reboot.h>
37 extern void (*_kobj_printf)(void *, const char *fmt, ...);
38 extern void (*_vkobj_printf)(void *, const char *fmt, va_list);
39 extern int get_weakish_int(int *);
40 extern struct bootops *ops;
41 extern struct boot_fs_ops bufs_ops, bbootfs_ops, bcpio_ops;
42 extern int kmem_ready;
44 static uint64_t rd_start, rd_end;
45 struct boot_fs_ops *bfs_ops;
46 struct boot_fs_ops *bfs_tab[] = {
47 &bufs_ops, &bbootfs_ops, &bcpio_ops, NULL,
50 static uintptr_t scratch_max = 0;
52 #define _kmem_ready get_weakish_int(&kmem_ready)
54 int
55 BRD_MOUNTROOT(struct boot_fs_ops *ops, char *str)
57 return (ops->fsw_mountroot(str));
60 int
61 BRD_UNMOUNTROOT(struct boot_fs_ops *ops)
63 if (bfs_ops != &bbootfs_ops)
64 bbootfs_ops.fsw_closeall(1);
66 return (ops->fsw_unmountroot());
69 int
70 BRD_OPEN(struct boot_fs_ops *ops, char *file, int flags)
72 int len = strlen(SYSTEM_BOOT_PATH);
73 int fd;
76 * Our policy is that we try bootfs first. If bootfs is the only
77 * filesystem, that's the end of it. Otherwise we will fall back to
78 * the normal root (i.e., ramdisk) filesystem at this point and try
79 * again if the file does not exist in bootfs.
81 fd = bbootfs_ops.fsw_open(file, flags);
83 if (bfs_ops == &bbootfs_ops)
84 return (fd);
86 if (strncmp(file, SYSTEM_BOOT_PATH, len) == 0 || fd >= 0)
87 return ((fd < 0) ? fd : (fd | BFD_F_SYSTEM_BOOT));
89 return (ops->fsw_open(file, flags));
92 int
93 BRD_CLOSE(struct boot_fs_ops *ops, int fd)
95 if (fd & BFD_F_SYSTEM_BOOT)
96 return (bbootfs_ops.fsw_close(fd & ~BFD_F_SYSTEM_BOOT));
98 return (ops->fsw_close(fd));
101 ssize_t
102 BRD_READ(struct boot_fs_ops *ops, int fd, caddr_t buf, size_t len)
104 if (fd & BFD_F_SYSTEM_BOOT) {
105 return (bbootfs_ops.fsw_read(fd & ~BFD_F_SYSTEM_BOOT,
106 buf, len));
109 return (ops->fsw_read(fd, buf, len));
112 off_t
113 BRD_SEEK(struct boot_fs_ops *ops, int fd, off_t addr, int whence)
115 if (fd & BFD_F_SYSTEM_BOOT) {
116 return (bbootfs_ops.fsw_lseek(fd & ~BFD_F_SYSTEM_BOOT,
117 addr, whence));
120 return (ops->fsw_lseek(fd, addr, whence));
124 BRD_FSTAT(struct boot_fs_ops *ops, int fd, struct bootstat *bsp)
126 if (fd & BFD_F_SYSTEM_BOOT)
127 return (bbootfs_ops.fsw_fstat(fd & ~BFD_F_SYSTEM_BOOT, bsp));
129 return (ops->fsw_fstat(fd, bsp));
133 * This one reads the ramdisk. If fi_memp is set, we copy the
134 * ramdisk content to the designated buffer. Otherwise, we
135 * do a "cached" read (set fi_memp to the actual ramdisk buffer).
138 diskread(fileid_t *filep)
140 uint_t blocknum;
141 caddr_t diskloc;
143 /* add in offset of root slice */
144 blocknum = filep->fi_blocknum;
146 diskloc = (caddr_t)(uintptr_t)rd_start + blocknum * DEV_BSIZE;
147 if (diskloc + filep->fi_count > (caddr_t)(uintptr_t)rd_end) {
148 _kobj_printf(ops, "diskread: start = 0x%p, size = 0x%x\n",
149 diskloc, filep->fi_count);
150 _kobj_printf(ops, "reading beyond end of ramdisk\n");
151 return (-1);
154 if (filep->fi_memp) {
155 bcopy(diskloc, filep->fi_memp, filep->fi_count);
156 } else {
157 /* "cached" read */
158 filep->fi_memp = diskloc;
161 return (0);
165 kobj_boot_mountroot()
167 int i;
169 if (BOP_GETPROPLEN(ops, "ramdisk_start") != 8 ||
170 BOP_GETPROP(ops, "ramdisk_start", (void *)&rd_start) != 0 ||
171 BOP_GETPROPLEN(ops, "ramdisk_end") != 8 ||
172 BOP_GETPROP(ops, "ramdisk_end", (void *)&rd_end) != 0) {
173 _kobj_printf(ops,
174 "failed to get ramdisk from boot\n");
175 return (-1);
177 #ifdef KOBJ_DEBUG
178 _kobj_printf(ops,
179 "ramdisk range: 0x%llx-%llx\n", rd_start, rd_end);
180 #endif
183 * We have a range of virtual addresses which are the boot archive.
185 for (i = 0; bfs_tab[i] != NULL; i++) {
186 bfs_ops = bfs_tab[i];
187 if (BRD_MOUNTROOT(bfs_ops, "dummy") == 0)
188 return (0);
191 _kobj_printf(ops, "failed to mount ramdisk from boot\n");
192 return (-1);
195 void
196 kobj_boot_unmountroot()
198 #ifdef DEBUG
199 if (boothowto & RB_VERBOSE)
200 _kobj_printf(ops, "boot scratch memory used: 0x%lx\n",
201 scratch_max);
202 #endif
203 (void) BRD_UNMOUNTROOT(bfs_ops);
207 * Boot time wrappers for memory allocators. Called for both permanent
208 * and temporary boot memory allocations. We have to track which allocator
209 * (boot or kmem) was used so that we know how to free.
211 void *
212 bkmem_alloc(size_t size)
214 /* allocate from boot scratch memory */
215 void *addr;
217 if (_kmem_ready)
218 return (kobj_alloc(size, 0));
221 * Remember the highest BOP_ALLOC allocated address and don't free
222 * anything below it.
224 addr = BOP_ALLOC(ops, 0, size, 0);
225 if (scratch_max < (uintptr_t)addr + size)
226 scratch_max = (uintptr_t)addr + size;
227 return (addr);
230 /*ARGSUSED*/
231 void
232 bkmem_free(void *p, size_t size)
235 * Free only if it's not boot scratch memory.
237 if ((uintptr_t)p >= scratch_max)
238 kobj_free(p, size);
241 /*PRINTFLIKE1*/
242 void
243 kobj_printf(char *fmt, ...)
245 va_list adx;
247 va_start(adx, fmt);
248 _vkobj_printf(ops, fmt, adx);
249 va_end(adx);