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