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