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