2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
30 #include <bootstrap.h>
33 * Concatenate the (argc) elements of (argv) into a single string, and return
37 unargv(int argc, char *argv[])
43 for (i = 0, hlong = 0; i < argc; i++)
44 hlong += strlen(argv[i]) + 2;
51 for (i = 0; i < argc; i++) {
61 * Get the length of a string in kernel space
64 strlenout(vm_offset_t src)
69 for (len = 0; ; len++) {
70 archsw.arch_copyout(src++, &c, 1);
78 * Make a duplicate copy of a string in kernel space
81 strdupout(vm_offset_t str)
85 result = malloc(strlenout(str) + 1);
86 for (cp = result; ;cp++) {
87 archsw.arch_copyout(str++, cp, 1);
94 /* Zero a region in kernel space. */
96 kern_bzero(vm_offset_t dest, size_t len)
101 bzero(buf, sizeof(buf));
104 chunk = min(sizeof(buf), resid);
105 archsw.arch_copyin(buf, dest, chunk);
112 * Read the specified part of a file to kernel space. Unlike regular
113 * pread, the file pointer is advanced to the end of the read data,
114 * and it just returns 0 if successful.
117 kern_pread(readin_handle_t fd, vm_offset_t dest, size_t len, off_t off)
120 if (VECTX_LSEEK(fd, off, SEEK_SET) == -1) {
122 printf("\nlseek failed\n");
126 if ((size_t)archsw.arch_readin(fd, dest, len) != len) {
128 printf("\nreadin failed\n");
136 * Read the specified part of a file to a malloced buffer. The file
137 * pointer is advanced to the end of the read data.
139 /* coverity[ -tainted_data_return ] */
141 alloc_pread(readin_handle_t fd, off_t off, size_t len)
148 printf("\nmalloc(%d) failed\n", (int)len);
153 if (VECTX_LSEEK(fd, off, SEEK_SET) == -1) {
155 printf("\nlseek failed\n");
160 if ((size_t)VECTX_READ(fd, buf, len) != len) {
162 printf("\nread failed\n");
171 * mount new rootfs and unmount old, set "currdev" environment variable.
173 int mount_currdev(struct env_var *ev, int flags, const void *value)
177 /* mount new rootfs */
178 rv = mount(value, "/", 0, NULL);
181 * Note we unmount any previously mounted fs only after
182 * successfully mounting the new because we do not want to
183 * end up with unmounted rootfs.
185 if (ev->ev_value != NULL)
186 unmount(ev->ev_value, 0);
187 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
193 * Set currdev to suit the value being supplied in (value)
196 gen_setcurrdev(struct env_var *ev, int flags, const void *value)
198 struct devdesc *ncurr;
201 if ((rv = devparse(&ncurr, value, NULL)) != 0)
205 return (mount_currdev(ev, flags, value));
209 * Wrapper to set currdev and loaddev at the same time.
212 set_currdev(const char *devname)
215 env_setenv("currdev", EV_VOLATILE, devname, gen_setcurrdev,
218 * Don't execute hook here; the loaddev hook makes it immutable
219 * once we've determined what the proper currdev is.
221 env_setenv("loaddev", EV_VOLATILE | EV_NOHOOK, devname, env_noset,