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