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