misc.c revision 1.3 1 1.3 christos /* $NetBSD: misc.c,v 1.3 2007/03/04 06:00:03 christos 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.2 cherry /* __FBSDID("$FreeBSD: src/sys/boot/common/misc.c,v 1.8.4.1 2004/09/03 19:25:40 iedowse Exp $"); */
31 1.1 cherry
32 1.1 cherry #include <lib/libsa/stand.h>
33 1.1 cherry #include <bootstrap.h>
34 1.1 cherry
35 1.1 cherry /*
36 1.1 cherry * Concatenate the (argc) elements of (argv) into a single string, and return
37 1.1 cherry * a copy of same.
38 1.1 cherry */
39 1.1 cherry char *
40 1.1 cherry unargv(int argc, char *argv[])
41 1.1 cherry {
42 1.1 cherry size_t hlong;
43 1.1 cherry int i;
44 1.1 cherry char *cp;
45 1.1 cherry
46 1.1 cherry for (hlong = 0, i = 0, hlong = 0; i < argc; i++)
47 1.1 cherry hlong += strlen(argv[i]) + 2;
48 1.1 cherry
49 1.1 cherry if(hlong == 0)
50 1.1 cherry return(NULL);
51 1.1 cherry
52 1.1 cherry cp = alloc(hlong);
53 1.1 cherry cp[0] = 0;
54 1.1 cherry for (i = 0; i < argc; i++) {
55 1.1 cherry strcat(cp, argv[i]);
56 1.1 cherry if (i < (argc - 1))
57 1.1 cherry strcat(cp, " ");
58 1.1 cherry }
59 1.1 cherry
60 1.1 cherry return(cp);
61 1.1 cherry }
62 1.1 cherry
63 1.1 cherry /*
64 1.1 cherry * Get the length of a string in kernel space
65 1.1 cherry */
66 1.1 cherry size_t
67 1.1 cherry strlenout(vaddr_t src)
68 1.1 cherry {
69 1.1 cherry char c;
70 1.1 cherry size_t len;
71 1.1 cherry
72 1.1 cherry for (len = 0; ; len++) {
73 1.1 cherry archsw.arch_copyout(src++, &c, 1);
74 1.1 cherry if (c == 0)
75 1.1 cherry break;
76 1.1 cherry }
77 1.1 cherry return(len);
78 1.1 cherry }
79 1.1 cherry
80 1.1 cherry /*
81 1.1 cherry * Make a duplicate copy of a string in kernel space
82 1.1 cherry */
83 1.1 cherry char *
84 1.1 cherry strdupout(vaddr_t str)
85 1.1 cherry {
86 1.1 cherry char *result, *cp;
87 1.1 cherry
88 1.1 cherry result = alloc(strlenout(str) + 1);
89 1.1 cherry for (cp = result; ;cp++) {
90 1.1 cherry archsw.arch_copyout(str++, cp, 1);
91 1.1 cherry if (*cp == 0)
92 1.1 cherry break;
93 1.1 cherry }
94 1.1 cherry return(result);
95 1.1 cherry }
96 1.1 cherry
97 1.1 cherry /* Zero a region in kernel space. */
98 1.1 cherry void
99 1.1 cherry kern_bzero(vaddr_t dest, size_t len)
100 1.1 cherry {
101 1.1 cherry char buf[256];
102 1.1 cherry size_t chunk, resid;
103 1.1 cherry
104 1.1 cherry bzero(buf, sizeof(buf));
105 1.1 cherry resid = len;
106 1.1 cherry while (resid > 0) {
107 1.1 cherry chunk = min(sizeof(buf), resid);
108 1.1 cherry archsw.arch_copyin(buf, dest, chunk);
109 1.1 cherry resid -= chunk;
110 1.1 cherry dest += chunk;
111 1.1 cherry }
112 1.1 cherry }
113 1.1 cherry
114 1.1 cherry /*
115 1.1 cherry * Read the specified part of a file to kernel space. Unlike regular
116 1.1 cherry * pread, the file pointer is advanced to the end of the read data,
117 1.1 cherry * and it just returns 0 if successful.
118 1.1 cherry */
119 1.1 cherry int
120 1.1 cherry kern_pread(int fd, vaddr_t dest, size_t len, off_t off)
121 1.1 cherry {
122 1.1 cherry ssize_t nread;
123 1.1 cherry
124 1.1 cherry if (lseek(fd, off, SEEK_SET) == -1) {
125 1.1 cherry printf("\nlseek failed\n");
126 1.1 cherry return (-1);
127 1.1 cherry }
128 1.1 cherry nread = archsw.arch_readin(fd, dest, len);
129 1.1 cherry if (nread != len) {
130 1.1 cherry printf("\nreadin failed\n");
131 1.1 cherry return (-1);
132 1.1 cherry }
133 1.1 cherry return (0);
134 1.1 cherry }
135 1.1 cherry
136 1.1 cherry /*
137 1.1 cherry * Read the specified part of a file to a malloced buffer. The file
138 1.1 cherry * pointer is advanced to the end of the read data.
139 1.1 cherry */
140 1.1 cherry void *
141 1.1 cherry alloc_pread(int fd, off_t off, size_t len)
142 1.1 cherry {
143 1.1 cherry void *buf;
144 1.1 cherry ssize_t nread;
145 1.1 cherry
146 1.1 cherry buf = alloc(len);
147 1.1 cherry if (buf == NULL) {
148 1.1 cherry printf("\nalloc(%d) failed\n", (int)len);
149 1.1 cherry return (NULL);
150 1.1 cherry }
151 1.1 cherry if (lseek(fd, off, SEEK_SET) == -1) {
152 1.1 cherry printf("\nlseek failed\n");
153 1.1 cherry free(buf);
154 1.1 cherry return (NULL);
155 1.1 cherry }
156 1.1 cherry nread = read(fd, buf, len);
157 1.1 cherry if (nread != len) {
158 1.1 cherry printf("\nread failed\n");
159 1.1 cherry free(buf);
160 1.1 cherry return (NULL);
161 1.1 cherry }
162 1.1 cherry return (buf);
163 1.1 cherry }
164 1.1 cherry
165 1.1 cherry /*
166 1.1 cherry * Display a region in traditional hexdump format.
167 1.1 cherry */
168 1.1 cherry void
169 1.3 christos hexdump(void *region, size_t len)
170 1.1 cherry {
171 1.3 christos void * line;
172 1.1 cherry int x, c;
173 1.1 cherry char lbuf[80];
174 1.1 cherry #define emit(fmt, args...) {sprintf(lbuf, fmt , ## args); pager_output(lbuf);}
175 1.1 cherry
176 1.1 cherry pager_open();
177 1.1 cherry for (line = region; line < (region + len); line += 16) {
178 1.1 cherry emit("%08lx ", (long) line);
179 1.1 cherry
180 1.1 cherry for (x = 0; x < 16; x++) {
181 1.1 cherry if ((line + x) < (region + len)) {
182 1.1 cherry emit("%02x ", *(u_int8_t *)(line + x));
183 1.1 cherry } else {
184 1.1 cherry emit("-- ");
185 1.1 cherry }
186 1.1 cherry if (x == 7)
187 1.1 cherry emit(" ");
188 1.1 cherry }
189 1.1 cherry emit(" |");
190 1.1 cherry for (x = 0; x < 16; x++) {
191 1.1 cherry if ((line + x) < (region + len)) {
192 1.1 cherry c = *(u_int8_t *)(line + x);
193 1.1 cherry if ((c < ' ') || (c > '~')) /* !isprint(c) */
194 1.1 cherry c = '.';
195 1.1 cherry emit("%c", c);
196 1.1 cherry } else {
197 1.1 cherry emit(" ");
198 1.1 cherry }
199 1.1 cherry }
200 1.1 cherry emit("|\n");
201 1.1 cherry }
202 1.1 cherry pager_close();
203 1.1 cherry }
204 1.1 cherry
205 1.1 cherry void
206 1.1 cherry dev_cleanup(void)
207 1.1 cherry {
208 1.1 cherry
209 1.1 cherry }
210