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