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