misc.c revision 1.2.2.2 1 1.2.2.2 simonb /* $NetBSD: misc.c,v 1.2.2.2 2006/04/22 11:37:38 simonb Exp $ */
2 1.2.2.2 simonb
3 1.2.2.2 simonb /*-
4 1.2.2.2 simonb * Copyright (c) 1998 Michael Smith <msmith (at) freebsd.org>
5 1.2.2.2 simonb * All rights reserved.
6 1.2.2.2 simonb *
7 1.2.2.2 simonb * Redistribution and use in source and binary forms, with or without
8 1.2.2.2 simonb * modification, are permitted provided that the following conditions
9 1.2.2.2 simonb * are met:
10 1.2.2.2 simonb * 1. Redistributions of source code must retain the above copyright
11 1.2.2.2 simonb * notice, this list of conditions and the following disclaimer.
12 1.2.2.2 simonb * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.2.2 simonb * notice, this list of conditions and the following disclaimer in the
14 1.2.2.2 simonb * documentation and/or other materials provided with the distribution.
15 1.2.2.2 simonb *
16 1.2.2.2 simonb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.2.2.2 simonb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.2.2.2 simonb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.2.2.2 simonb * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.2.2.2 simonb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.2.2.2 simonb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.2.2.2 simonb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.2.2.2 simonb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.2.2.2 simonb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.2.2.2 simonb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.2.2.2 simonb * SUCH DAMAGE.
27 1.2.2.2 simonb */
28 1.2.2.2 simonb
29 1.2.2.2 simonb #include <sys/cdefs.h>
30 1.2.2.2 simonb
31 1.2.2.2 simonb #include <lib/libsa/stand.h>
32 1.2.2.2 simonb #include <bootstrap.h>
33 1.2.2.2 simonb
34 1.2.2.2 simonb /*
35 1.2.2.2 simonb * Concatenate the (argc) elements of (argv) into a single string, and return
36 1.2.2.2 simonb * a copy of same.
37 1.2.2.2 simonb */
38 1.2.2.2 simonb char *
39 1.2.2.2 simonb unargv(int argc, char *argv[])
40 1.2.2.2 simonb {
41 1.2.2.2 simonb size_t hlong;
42 1.2.2.2 simonb int i;
43 1.2.2.2 simonb char *cp;
44 1.2.2.2 simonb
45 1.2.2.2 simonb for (hlong = 0, i = 0, hlong = 0; i < argc; i++)
46 1.2.2.2 simonb hlong += strlen(argv[i]) + 2;
47 1.2.2.2 simonb
48 1.2.2.2 simonb if(hlong == 0)
49 1.2.2.2 simonb return(NULL);
50 1.2.2.2 simonb
51 1.2.2.2 simonb cp = alloc(hlong);
52 1.2.2.2 simonb cp[0] = 0;
53 1.2.2.2 simonb for (i = 0; i < argc; i++) {
54 1.2.2.2 simonb strcat(cp, argv[i]);
55 1.2.2.2 simonb if (i < (argc - 1))
56 1.2.2.2 simonb strcat(cp, " ");
57 1.2.2.2 simonb }
58 1.2.2.2 simonb
59 1.2.2.2 simonb return(cp);
60 1.2.2.2 simonb }
61 1.2.2.2 simonb
62 1.2.2.2 simonb /*
63 1.2.2.2 simonb * Get the length of a string in kernel space
64 1.2.2.2 simonb */
65 1.2.2.2 simonb size_t
66 1.2.2.2 simonb strlenout(vaddr_t src)
67 1.2.2.2 simonb {
68 1.2.2.2 simonb char c;
69 1.2.2.2 simonb size_t len;
70 1.2.2.2 simonb
71 1.2.2.2 simonb for (len = 0; ; len++) {
72 1.2.2.2 simonb archsw.arch_copyout(src++, &c, 1);
73 1.2.2.2 simonb if (c == 0)
74 1.2.2.2 simonb break;
75 1.2.2.2 simonb }
76 1.2.2.2 simonb return(len);
77 1.2.2.2 simonb }
78 1.2.2.2 simonb
79 1.2.2.2 simonb /*
80 1.2.2.2 simonb * Make a duplicate copy of a string in kernel space
81 1.2.2.2 simonb */
82 1.2.2.2 simonb char *
83 1.2.2.2 simonb strdupout(vaddr_t str)
84 1.2.2.2 simonb {
85 1.2.2.2 simonb char *result, *cp;
86 1.2.2.2 simonb
87 1.2.2.2 simonb result = alloc(strlenout(str) + 1);
88 1.2.2.2 simonb for (cp = result; ;cp++) {
89 1.2.2.2 simonb archsw.arch_copyout(str++, cp, 1);
90 1.2.2.2 simonb if (*cp == 0)
91 1.2.2.2 simonb break;
92 1.2.2.2 simonb }
93 1.2.2.2 simonb return(result);
94 1.2.2.2 simonb }
95 1.2.2.2 simonb
96 1.2.2.2 simonb /* Zero a region in kernel space. */
97 1.2.2.2 simonb void
98 1.2.2.2 simonb kern_bzero(vaddr_t dest, size_t len)
99 1.2.2.2 simonb {
100 1.2.2.2 simonb char buf[256];
101 1.2.2.2 simonb size_t chunk, resid;
102 1.2.2.2 simonb
103 1.2.2.2 simonb bzero(buf, sizeof(buf));
104 1.2.2.2 simonb resid = len;
105 1.2.2.2 simonb while (resid > 0) {
106 1.2.2.2 simonb chunk = min(sizeof(buf), resid);
107 1.2.2.2 simonb archsw.arch_copyin(buf, dest, chunk);
108 1.2.2.2 simonb resid -= chunk;
109 1.2.2.2 simonb dest += chunk;
110 1.2.2.2 simonb }
111 1.2.2.2 simonb }
112 1.2.2.2 simonb
113 1.2.2.2 simonb /*
114 1.2.2.2 simonb * Read the specified part of a file to kernel space. Unlike regular
115 1.2.2.2 simonb * pread, the file pointer is advanced to the end of the read data,
116 1.2.2.2 simonb * and it just returns 0 if successful.
117 1.2.2.2 simonb */
118 1.2.2.2 simonb int
119 1.2.2.2 simonb kern_pread(int fd, vaddr_t dest, size_t len, off_t off)
120 1.2.2.2 simonb {
121 1.2.2.2 simonb ssize_t nread;
122 1.2.2.2 simonb
123 1.2.2.2 simonb if (lseek(fd, off, SEEK_SET) == -1) {
124 1.2.2.2 simonb printf("\nlseek failed\n");
125 1.2.2.2 simonb return (-1);
126 1.2.2.2 simonb }
127 1.2.2.2 simonb nread = archsw.arch_readin(fd, dest, len);
128 1.2.2.2 simonb if (nread != len) {
129 1.2.2.2 simonb printf("\nreadin failed\n");
130 1.2.2.2 simonb return (-1);
131 1.2.2.2 simonb }
132 1.2.2.2 simonb return (0);
133 1.2.2.2 simonb }
134 1.2.2.2 simonb
135 1.2.2.2 simonb /*
136 1.2.2.2 simonb * Read the specified part of a file to a malloced buffer. The file
137 1.2.2.2 simonb * pointer is advanced to the end of the read data.
138 1.2.2.2 simonb */
139 1.2.2.2 simonb void *
140 1.2.2.2 simonb alloc_pread(int fd, off_t off, size_t len)
141 1.2.2.2 simonb {
142 1.2.2.2 simonb void *buf;
143 1.2.2.2 simonb ssize_t nread;
144 1.2.2.2 simonb
145 1.2.2.2 simonb buf = alloc(len);
146 1.2.2.2 simonb if (buf == NULL) {
147 1.2.2.2 simonb printf("\nalloc(%d) failed\n", (int)len);
148 1.2.2.2 simonb return (NULL);
149 1.2.2.2 simonb }
150 1.2.2.2 simonb if (lseek(fd, off, SEEK_SET) == -1) {
151 1.2.2.2 simonb printf("\nlseek failed\n");
152 1.2.2.2 simonb free(buf);
153 1.2.2.2 simonb return (NULL);
154 1.2.2.2 simonb }
155 1.2.2.2 simonb nread = read(fd, buf, len);
156 1.2.2.2 simonb if (nread != len) {
157 1.2.2.2 simonb printf("\nread failed\n");
158 1.2.2.2 simonb free(buf);
159 1.2.2.2 simonb return (NULL);
160 1.2.2.2 simonb }
161 1.2.2.2 simonb return (buf);
162 1.2.2.2 simonb }
163 1.2.2.2 simonb
164 1.2.2.2 simonb /*
165 1.2.2.2 simonb * Display a region in traditional hexdump format.
166 1.2.2.2 simonb */
167 1.2.2.2 simonb void
168 1.2.2.2 simonb hexdump(caddr_t region, size_t len)
169 1.2.2.2 simonb {
170 1.2.2.2 simonb caddr_t line;
171 1.2.2.2 simonb int x, c;
172 1.2.2.2 simonb char lbuf[80];
173 1.2.2.2 simonb #define emit(fmt, args...) {sprintf(lbuf, fmt , ## args); pager_output(lbuf);}
174 1.2.2.2 simonb
175 1.2.2.2 simonb pager_open();
176 1.2.2.2 simonb for (line = region; line < (region + len); line += 16) {
177 1.2.2.2 simonb emit("%08lx ", (long) line);
178 1.2.2.2 simonb
179 1.2.2.2 simonb for (x = 0; x < 16; x++) {
180 1.2.2.2 simonb if ((line + x) < (region + len)) {
181 1.2.2.2 simonb emit("%02x ", *(u_int8_t *)(line + x));
182 1.2.2.2 simonb } else {
183 1.2.2.2 simonb emit("-- ");
184 1.2.2.2 simonb }
185 1.2.2.2 simonb if (x == 7)
186 1.2.2.2 simonb emit(" ");
187 1.2.2.2 simonb }
188 1.2.2.2 simonb emit(" |");
189 1.2.2.2 simonb for (x = 0; x < 16; x++) {
190 1.2.2.2 simonb if ((line + x) < (region + len)) {
191 1.2.2.2 simonb c = *(u_int8_t *)(line + x);
192 1.2.2.2 simonb if ((c < ' ') || (c > '~')) /* !isprint(c) */
193 1.2.2.2 simonb c = '.';
194 1.2.2.2 simonb emit("%c", c);
195 1.2.2.2 simonb } else {
196 1.2.2.2 simonb emit(" ");
197 1.2.2.2 simonb }
198 1.2.2.2 simonb }
199 1.2.2.2 simonb emit("|\n");
200 1.2.2.2 simonb }
201 1.2.2.2 simonb pager_close();
202 1.2.2.2 simonb }
203 1.2.2.2 simonb
204 1.2.2.2 simonb void
205 1.2.2.2 simonb dev_cleanup(void)
206 1.2.2.2 simonb {
207 1.2.2.2 simonb
208 1.2.2.2 simonb }
209