xref: /illumos-gate/usr/src/boot/common/misc.c (revision 22028508fd28d36ff74dc02c5774a8ba1f0db045)
1*22028508SToomas Soome /*-
2*22028508SToomas Soome  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3*22028508SToomas Soome  * All rights reserved.
4*22028508SToomas Soome  *
5*22028508SToomas Soome  * Redistribution and use in source and binary forms, with or without
6*22028508SToomas Soome  * modification, are permitted provided that the following conditions
7*22028508SToomas Soome  * are met:
8*22028508SToomas Soome  * 1. Redistributions of source code must retain the above copyright
9*22028508SToomas Soome  *    notice, this list of conditions and the following disclaimer.
10*22028508SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
11*22028508SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
12*22028508SToomas Soome  *    documentation and/or other materials provided with the distribution.
13*22028508SToomas Soome  *
14*22028508SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*22028508SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*22028508SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*22028508SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*22028508SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*22028508SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*22028508SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*22028508SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*22028508SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*22028508SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*22028508SToomas Soome  * SUCH DAMAGE.
25*22028508SToomas Soome  */
26*22028508SToomas Soome 
27*22028508SToomas Soome #include <sys/cdefs.h>
28*22028508SToomas Soome #include <string.h>
29*22028508SToomas Soome #include <stand.h>
30*22028508SToomas Soome #include <bootstrap.h>
31*22028508SToomas Soome #ifndef BOOT2
32*22028508SToomas Soome #include <machine/cpufunc.h>
33*22028508SToomas Soome #include "ficl.h"
34*22028508SToomas Soome #endif
35*22028508SToomas Soome 
36*22028508SToomas Soome /*
37*22028508SToomas Soome  * Concatenate the (argc) elements of (argv) into a single string, and return
38*22028508SToomas Soome  * a copy of same.
39*22028508SToomas Soome  */
40*22028508SToomas Soome char *
unargv(int argc,char * argv[])41*22028508SToomas Soome unargv(int argc, char *argv[])
42*22028508SToomas Soome {
43*22028508SToomas Soome     size_t	hlong;
44*22028508SToomas Soome     int		i;
45*22028508SToomas Soome     char	*cp;
46*22028508SToomas Soome 
47*22028508SToomas Soome     for (i = 0, hlong = 0; i < argc; i++)
48*22028508SToomas Soome 	hlong += strlen(argv[i]) + 2;
49*22028508SToomas Soome 
50*22028508SToomas Soome     if(hlong == 0)
51*22028508SToomas Soome 	return(NULL);
52*22028508SToomas Soome 
53*22028508SToomas Soome     cp = malloc(hlong);
54*22028508SToomas Soome     cp[0] = 0;
55*22028508SToomas Soome     for (i = 0; i < argc; i++) {
56*22028508SToomas Soome 	strcat(cp, argv[i]);
57*22028508SToomas Soome 	if (i < (argc - 1))
58*22028508SToomas Soome 	  strcat(cp, " ");
59*22028508SToomas Soome     }
60*22028508SToomas Soome 
61*22028508SToomas Soome     return(cp);
62*22028508SToomas Soome }
63*22028508SToomas Soome 
64*22028508SToomas Soome /*
65*22028508SToomas Soome  * Get the length of a string in kernel space
66*22028508SToomas Soome  */
67*22028508SToomas Soome size_t
strlenout(vm_offset_t src)68*22028508SToomas Soome strlenout(vm_offset_t src)
69*22028508SToomas Soome {
70*22028508SToomas Soome     char	c;
71*22028508SToomas Soome     size_t	len;
72*22028508SToomas Soome 
73*22028508SToomas Soome     for (len = 0; ; len++) {
74*22028508SToomas Soome 	archsw.arch_copyout(src++, &c, 1);
75*22028508SToomas Soome 	if (c == 0)
76*22028508SToomas Soome 	    break;
77*22028508SToomas Soome     }
78*22028508SToomas Soome     return(len);
79*22028508SToomas Soome }
80*22028508SToomas Soome 
81*22028508SToomas Soome /*
82*22028508SToomas Soome  * Make a duplicate copy of a string in kernel space
83*22028508SToomas Soome  */
84*22028508SToomas Soome char *
strdupout(vm_offset_t str)85*22028508SToomas Soome strdupout(vm_offset_t str)
86*22028508SToomas Soome {
87*22028508SToomas Soome     char	*result, *cp;
88*22028508SToomas Soome 
89*22028508SToomas Soome     result = malloc(strlenout(str) + 1);
90*22028508SToomas Soome     for (cp = result; ;cp++) {
91*22028508SToomas Soome 	archsw.arch_copyout(str++, cp, 1);
92*22028508SToomas Soome 	if (*cp == 0)
93*22028508SToomas Soome 	    break;
94*22028508SToomas Soome     }
95*22028508SToomas Soome     return(result);
96*22028508SToomas Soome }
97*22028508SToomas Soome 
98*22028508SToomas Soome /* Zero a region in kernel space. */
99*22028508SToomas Soome void
kern_bzero(vm_offset_t dest,size_t len)100*22028508SToomas Soome kern_bzero(vm_offset_t dest, size_t len)
101*22028508SToomas Soome {
102*22028508SToomas Soome 	char buf[256];
103*22028508SToomas Soome 	size_t chunk, resid;
104*22028508SToomas Soome 
105*22028508SToomas Soome 	bzero(buf, sizeof(buf));
106*22028508SToomas Soome 	resid = len;
107*22028508SToomas Soome 	while (resid > 0) {
108*22028508SToomas Soome 		chunk = min(sizeof(buf), resid);
109*22028508SToomas Soome 		archsw.arch_copyin(buf, dest, chunk);
110*22028508SToomas Soome 		resid -= chunk;
111*22028508SToomas Soome 		dest += chunk;
112*22028508SToomas Soome 	}
113*22028508SToomas Soome }
114*22028508SToomas Soome 
115*22028508SToomas Soome /*
116*22028508SToomas Soome  * Read the specified part of a file to kernel space.  Unlike regular
117*22028508SToomas Soome  * pread, the file pointer is advanced to the end of the read data,
118*22028508SToomas Soome  * and it just returns 0 if successful.
119*22028508SToomas Soome  */
120*22028508SToomas Soome int
kern_pread(int fd,vm_offset_t dest,size_t len,off_t off)121*22028508SToomas Soome kern_pread(int fd, vm_offset_t dest, size_t len, off_t off)
122*22028508SToomas Soome {
123*22028508SToomas Soome 
124*22028508SToomas Soome 	if (lseek(fd, off, SEEK_SET) == -1) {
125*22028508SToomas Soome #ifdef DEBUG
126*22028508SToomas Soome 		printf("\nlseek failed\n");
127*22028508SToomas Soome #endif
128*22028508SToomas Soome 		return (-1);
129*22028508SToomas Soome 	}
130*22028508SToomas Soome 	if ((size_t)archsw.arch_readin(fd, dest, len) != len) {
131*22028508SToomas Soome #ifdef DEBUG
132*22028508SToomas Soome 		printf("\nreadin failed\n");
133*22028508SToomas Soome #endif
134*22028508SToomas Soome 		return (-1);
135*22028508SToomas Soome 	}
136*22028508SToomas Soome 	return (0);
137*22028508SToomas Soome }
138*22028508SToomas Soome 
139*22028508SToomas Soome /*
140*22028508SToomas Soome  * Read the specified part of a file to a malloced buffer.  The file
141*22028508SToomas Soome  * pointer is advanced to the end of the read data.
142*22028508SToomas Soome  */
143*22028508SToomas Soome void *
alloc_pread(int fd,off_t off,size_t len)144*22028508SToomas Soome alloc_pread(int fd, off_t off, size_t len)
145*22028508SToomas Soome {
146*22028508SToomas Soome 	void *buf;
147*22028508SToomas Soome 
148*22028508SToomas Soome 	buf = malloc(len);
149*22028508SToomas Soome 	if (buf == NULL) {
150*22028508SToomas Soome #ifdef DEBUG
151*22028508SToomas Soome 		printf("\nmalloc(%d) failed\n", (int)len);
152*22028508SToomas Soome #endif
153*22028508SToomas Soome 		return (NULL);
154*22028508SToomas Soome 	}
155*22028508SToomas Soome 	if (lseek(fd, off, SEEK_SET) == -1) {
156*22028508SToomas Soome #ifdef DEBUG
157*22028508SToomas Soome 		printf("\nlseek failed\n");
158*22028508SToomas Soome #endif
159*22028508SToomas Soome 		free(buf);
160*22028508SToomas Soome 		return (NULL);
161*22028508SToomas Soome 	}
162*22028508SToomas Soome 	if ((size_t)read(fd, buf, len) != len) {
163*22028508SToomas Soome #ifdef DEBUG
164*22028508SToomas Soome 		printf("\nread failed\n");
165*22028508SToomas Soome #endif
166*22028508SToomas Soome 		free(buf);
167*22028508SToomas Soome 		return (NULL);
168*22028508SToomas Soome 	}
169*22028508SToomas Soome 	return (buf);
170*22028508SToomas Soome }
171*22028508SToomas Soome 
172*22028508SToomas Soome /*
173*22028508SToomas Soome  * Display a region in traditional hexdump format.
174*22028508SToomas Soome  */
175*22028508SToomas Soome void
hexdump(caddr_t region,size_t len)176*22028508SToomas Soome hexdump(caddr_t region, size_t len)
177*22028508SToomas Soome {
178*22028508SToomas Soome     caddr_t	line;
179*22028508SToomas Soome     int		x, c;
180*22028508SToomas Soome     char	lbuf[80];
181*22028508SToomas Soome #define emit(fmt, args...)	{sprintf(lbuf, fmt , ## args); pager_output(lbuf);}
182*22028508SToomas Soome 
183*22028508SToomas Soome     pager_open();
184*22028508SToomas Soome     for (line = region; line < (region + len); line += 16) {
185*22028508SToomas Soome 	emit("%08lx  ", (long) line);
186*22028508SToomas Soome 
187*22028508SToomas Soome 	for (x = 0; x < 16; x++) {
188*22028508SToomas Soome 	    if ((line + x) < (region + len)) {
189*22028508SToomas Soome 		emit("%02x ", *(u_int8_t *)(line + x));
190*22028508SToomas Soome 	    } else {
191*22028508SToomas Soome 		emit("-- ");
192*22028508SToomas Soome 	    }
193*22028508SToomas Soome 	    if (x == 7)
194*22028508SToomas Soome 		emit(" ");
195*22028508SToomas Soome 	}
196*22028508SToomas Soome 	emit(" |");
197*22028508SToomas Soome 	for (x = 0; x < 16; x++) {
198*22028508SToomas Soome 	    if ((line + x) < (region + len)) {
199*22028508SToomas Soome 		c = *(u_int8_t *)(line + x);
200*22028508SToomas Soome 		if ((c < ' ') || (c > '~'))	/* !isprint(c) */
201*22028508SToomas Soome 		    c = '.';
202*22028508SToomas Soome 		emit("%c", c);
203*22028508SToomas Soome 	    } else {
204*22028508SToomas Soome 		emit(" ");
205*22028508SToomas Soome 	    }
206*22028508SToomas Soome 	}
207*22028508SToomas Soome 	emit("|\n");
208*22028508SToomas Soome     }
209*22028508SToomas Soome     pager_close();
210*22028508SToomas Soome }
211*22028508SToomas Soome 
212*22028508SToomas Soome void
dev_cleanup(void)213*22028508SToomas Soome dev_cleanup(void)
214*22028508SToomas Soome {
215*22028508SToomas Soome     int		i;
216*22028508SToomas Soome 
217*22028508SToomas Soome     /* Call cleanup routines */
218*22028508SToomas Soome     for (i = 0; devsw[i] != NULL; ++i)
219*22028508SToomas Soome 	if (devsw[i]->dv_cleanup != NULL)
220*22028508SToomas Soome 	    (devsw[i]->dv_cleanup)();
221*22028508SToomas Soome }
222*22028508SToomas Soome 
223*22028508SToomas Soome #ifndef BOOT2
224*22028508SToomas Soome /*
225*22028508SToomas Soome  * outb ( port# c -- )
226*22028508SToomas Soome  * Store a byte to I/O port number port#
227*22028508SToomas Soome  */
228*22028508SToomas Soome static void
ficlOutb(ficlVm * pVM)229*22028508SToomas Soome ficlOutb(ficlVm *pVM)
230*22028508SToomas Soome {
231*22028508SToomas Soome 	uint8_t c;
232*22028508SToomas Soome 	uint32_t port;
233*22028508SToomas Soome 
234*22028508SToomas Soome 	port = ficlStackPopUnsigned(ficlVmGetDataStack(pVM));
235*22028508SToomas Soome 	c = ficlStackPopInteger(ficlVmGetDataStack(pVM));
236*22028508SToomas Soome 	outb(port, c);
237*22028508SToomas Soome }
238*22028508SToomas Soome 
239*22028508SToomas Soome /*
240*22028508SToomas Soome  * inb ( port# -- c )
241*22028508SToomas Soome  * Fetch a byte from I/O port number port#
242*22028508SToomas Soome  */
243*22028508SToomas Soome static void
ficlInb(ficlVm * pVM)244*22028508SToomas Soome ficlInb(ficlVm *pVM)
245*22028508SToomas Soome {
246*22028508SToomas Soome 	uint8_t c;
247*22028508SToomas Soome 	uint32_t port;
248*22028508SToomas Soome 
249*22028508SToomas Soome 	port = ficlStackPopUnsigned(ficlVmGetDataStack(pVM));
250*22028508SToomas Soome 	c = inb(port);
251*22028508SToomas Soome 	ficlStackPushInteger(ficlVmGetDataStack(pVM), c);
252*22028508SToomas Soome }
253*22028508SToomas Soome 
254*22028508SToomas Soome static void
ficlCompileCpufunc(ficlSystem * pSys)255*22028508SToomas Soome ficlCompileCpufunc(ficlSystem *pSys)
256*22028508SToomas Soome {
257*22028508SToomas Soome 	ficlDictionary *dp = ficlSystemGetDictionary(pSys);
258*22028508SToomas Soome 
259*22028508SToomas Soome 	FICL_SYSTEM_ASSERT(pSys, dp);
260*22028508SToomas Soome 
261*22028508SToomas Soome 	(void) ficlDictionarySetPrimitive(dp, "outb", ficlOutb,
262*22028508SToomas Soome 	    FICL_WORD_DEFAULT);
263*22028508SToomas Soome 	(void) ficlDictionarySetPrimitive(dp, "inb", ficlInb,
264*22028508SToomas Soome 	    FICL_WORD_DEFAULT);
265*22028508SToomas Soome }
266*22028508SToomas Soome 
267*22028508SToomas Soome FICL_COMPILE_SET(ficlCompileCpufunc);
268*22028508SToomas Soome #endif
269