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
29 #include <bootstrap.h>
32 * Concatenate the (argc) elements of (argv) into a single string, and return
36 unargv(int argc
, char *argv
[])
42 for (i
= 0, hlong
= 0; i
< argc
; i
++)
43 hlong
+= strlen(argv
[i
]) + 2;
50 for (i
= 0; i
< argc
; i
++) {
60 * Get the length of a string in kernel space
63 strlenout(vm_offset_t src
)
68 for (len
= 0; ; len
++) {
69 archsw
.arch_copyout(src
++, &c
, 1);
77 * Make a duplicate copy of a string in kernel space
80 strdupout(vm_offset_t str
)
84 result
= malloc(strlenout(str
) + 1);
85 for (cp
= result
; ;cp
++) {
86 archsw
.arch_copyout(str
++, cp
, 1);
93 /* Zero a region in kernel space. */
95 kern_bzero(vm_offset_t dest
, size_t len
)
100 bzero(buf
, sizeof(buf
));
103 chunk
= min(sizeof(buf
), resid
);
104 archsw
.arch_copyin(buf
, dest
, chunk
);
111 * Read the specified part of a file to kernel space. Unlike regular
112 * pread, the file pointer is advanced to the end of the read data,
113 * and it just returns 0 if successful.
116 kern_pread(readin_handle_t fd
, vm_offset_t dest
, size_t len
, off_t off
)
119 if (VECTX_LSEEK(fd
, off
, SEEK_SET
) == -1) {
121 printf("\nlseek failed\n");
125 if ((size_t)archsw
.arch_readin(fd
, dest
, len
) != len
) {
127 printf("\nreadin failed\n");
135 * Read the specified part of a file to a malloced buffer. The file
136 * pointer is advanced to the end of the read data.
138 /* coverity[ -tainted_data_return ] */
140 alloc_pread(readin_handle_t fd
, off_t off
, size_t len
)
147 printf("\nmalloc(%d) failed\n", (int)len
);
152 if (VECTX_LSEEK(fd
, off
, SEEK_SET
) == -1) {
154 printf("\nlseek failed\n");
159 if ((size_t)VECTX_READ(fd
, buf
, len
) != len
) {
161 printf("\nread failed\n");
170 * mount new rootfs and unmount old, set "currdev" environment variable.
172 int mount_currdev(struct env_var
*ev
, int flags
, const void *value
)
176 /* mount new rootfs */
177 rv
= mount(value
, "/", 0, NULL
);
180 * Note we unmount any previously mounted fs only after
181 * successfully mounting the new because we do not want to
182 * end up with unmounted rootfs.
184 if (ev
->ev_value
!= NULL
)
185 unmount(ev
->ev_value
, 0);
186 env_setenv(ev
->ev_name
, flags
| EV_NOHOOK
, value
, NULL
, NULL
);
192 * Set currdev to suit the value being supplied in (value)
195 gen_setcurrdev(struct env_var
*ev
, int flags
, const void *value
)
197 struct devdesc
*ncurr
;
200 if ((rv
= devparse(&ncurr
, value
, NULL
)) != 0)
204 return (mount_currdev(ev
, flags
, value
));
208 * Wrapper to set currdev and loaddev at the same time.
211 set_currdev(const char *devname
)
214 env_setenv("currdev", EV_VOLATILE
, devname
, gen_setcurrdev
,
217 * Don't execute hook here; the loaddev hook makes it immutable
218 * once we've determined what the proper currdev is.
220 env_setenv("loaddev", EV_VOLATILE
| EV_NOHOOK
, devname
, env_noset
,