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