Sync usage with man page.
[netbsd-mini2440.git] / sys / arch / ia64 / stand / common / misc.c
blobedf80d90e5b8ffb9eeb8f177ba8c5b9969e5d7e3
1 /* $NetBSD: misc.c,v 1.4 2009/03/18 16:00:12 cegger Exp $ */
3 /*-
4 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
26 * SUCH DAMAGE.
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
38 * a copy of same.
40 char *
41 unargv(int argc, char *argv[])
43 size_t hlong;
44 int i;
45 char *cp;
47 for (hlong = 0, i = 0, hlong = 0; i < argc; i++)
48 hlong += strlen(argv[i]) + 2;
50 if(hlong == 0)
51 return(NULL);
53 cp = alloc(hlong);
54 cp[0] = 0;
55 for (i = 0; i < argc; i++) {
56 strcat(cp, argv[i]);
57 if (i < (argc - 1))
58 strcat(cp, " ");
61 return(cp);
65 * Get the length of a string in kernel space
67 size_t
68 strlenout(vaddr_t src)
70 char c;
71 size_t len;
73 for (len = 0; ; len++) {
74 archsw.arch_copyout(src++, &c, 1);
75 if (c == 0)
76 break;
78 return(len);
82 * Make a duplicate copy of a string in kernel space
84 char *
85 strdupout(vaddr_t str)
87 char *result, *cp;
89 result = alloc(strlenout(str) + 1);
90 for (cp = result; ;cp++) {
91 archsw.arch_copyout(str++, cp, 1);
92 if (*cp == 0)
93 break;
95 return(result);
98 /* Zero a region in kernel space. */
99 void
100 kern_bzero(vaddr_t dest, size_t len)
102 char buf[256];
103 size_t chunk, resid;
105 memset(buf, 0, sizeof(buf));
106 resid = len;
107 while (resid > 0) {
108 chunk = min(sizeof(buf), resid);
109 archsw.arch_copyin(buf, dest, chunk);
110 resid -= chunk;
111 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)
123 ssize_t nread;
125 if (lseek(fd, off, SEEK_SET) == -1) {
126 printf("\nlseek failed\n");
127 return (-1);
129 nread = archsw.arch_readin(fd, dest, len);
130 if (nread != len) {
131 printf("\nreadin failed\n");
132 return (-1);
134 return (0);
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.
141 void *
142 alloc_pread(int fd, off_t off, size_t len)
144 void *buf;
145 ssize_t nread;
147 buf = alloc(len);
148 if (buf == NULL) {
149 printf("\nalloc(%d) failed\n", (int)len);
150 return (NULL);
152 if (lseek(fd, off, SEEK_SET) == -1) {
153 printf("\nlseek failed\n");
154 free(buf);
155 return (NULL);
157 nread = read(fd, buf, len);
158 if (nread != len) {
159 printf("\nread failed\n");
160 free(buf);
161 return (NULL);
163 return (buf);
167 * Display a region in traditional hexdump format.
169 void
170 hexdump(void *region, size_t len)
172 void * line;
173 int x, c;
174 char lbuf[80];
175 #define emit(fmt, args...) {sprintf(lbuf, fmt , ## args); pager_output(lbuf);}
177 pager_open();
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));
184 } else {
185 emit("-- ");
187 if (x == 7)
188 emit(" ");
190 emit(" |");
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) */
195 c = '.';
196 emit("%c", c);
197 } else {
198 emit(" ");
201 emit("|\n");
203 pager_close();
206 void
207 dev_cleanup(void)