1 /* $NetBSD: misc.c,v 1.4 2009/03/18 16:00:12 cegger Exp $ */
4 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 /* __FBSDID("$FreeBSD: src/sys/boot/common/misc.c,v 1.8.4.1 2004/09/03 19:25:40 iedowse Exp $"); */
32 #include <lib/libsa/stand.h>
33 #include <lib/libsa/loadfile.h>
34 #include <bootstrap.h>
37 * Concatenate the (argc) elements of (argv) into a single string, and return
41 unargv(int argc
, char *argv
[])
47 for (hlong
= 0, i
= 0, hlong
= 0; i
< argc
; i
++)
48 hlong
+= strlen(argv
[i
]) + 2;
55 for (i
= 0; i
< argc
; i
++) {
65 * Get the length of a string in kernel space
68 strlenout(vaddr_t src
)
73 for (len
= 0; ; len
++) {
74 archsw
.arch_copyout(src
++, &c
, 1);
82 * Make a duplicate copy of a string in kernel space
85 strdupout(vaddr_t str
)
89 result
= alloc(strlenout(str
) + 1);
90 for (cp
= result
; ;cp
++) {
91 archsw
.arch_copyout(str
++, cp
, 1);
98 /* Zero a region in kernel space. */
100 kern_bzero(vaddr_t dest
, size_t len
)
105 memset(buf
, 0, sizeof(buf
));
108 chunk
= min(sizeof(buf
), resid
);
109 archsw
.arch_copyin(buf
, dest
, chunk
);
116 * Read the specified part of a file to kernel space. Unlike regular
117 * pread, the file pointer is advanced to the end of the read data,
118 * and it just returns 0 if successful.
121 kern_pread(int fd
, vaddr_t dest
, size_t len
, off_t off
)
125 if (lseek(fd
, off
, SEEK_SET
) == -1) {
126 printf("\nlseek failed\n");
129 nread
= archsw
.arch_readin(fd
, dest
, len
);
131 printf("\nreadin failed\n");
138 * Read the specified part of a file to a malloced buffer. The file
139 * pointer is advanced to the end of the read data.
142 alloc_pread(int fd
, off_t off
, size_t len
)
149 printf("\nalloc(%d) failed\n", (int)len
);
152 if (lseek(fd
, off
, SEEK_SET
) == -1) {
153 printf("\nlseek failed\n");
157 nread
= read(fd
, buf
, len
);
159 printf("\nread failed\n");
167 * Display a region in traditional hexdump format.
170 hexdump(void *region
, size_t len
)
175 #define emit(fmt, args...) {sprintf(lbuf, fmt , ## args); pager_output(lbuf);}
178 for (line
= region
; line
< (region
+ len
); line
+= 16) {
179 emit("%08lx ", (long) line
);
181 for (x
= 0; x
< 16; x
++) {
182 if ((line
+ x
) < (region
+ len
)) {
183 emit("%02x ", *(u_int8_t
*)(line
+ x
));
191 for (x
= 0; x
< 16; x
++) {
192 if ((line
+ x
) < (region
+ len
)) {
193 c
= *(u_int8_t
*)(line
+ x
);
194 if ((c
< ' ') || (c
> '~')) /* !isprint(c) */