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