xref: /illumos-gate/usr/src/boot/common/misc.c (revision 22028508)
1199767f8SToomas Soome /*-
2199767f8SToomas Soome  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3199767f8SToomas Soome  * All rights reserved.
4199767f8SToomas Soome  *
5199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
6199767f8SToomas Soome  * modification, are permitted provided that the following conditions
7199767f8SToomas Soome  * are met:
8199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
9199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
10199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
11199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
12199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
13199767f8SToomas Soome  *
14199767f8SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15199767f8SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16199767f8SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17199767f8SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18199767f8SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19199767f8SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20199767f8SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21199767f8SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22199767f8SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23199767f8SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24199767f8SToomas Soome  * SUCH DAMAGE.
25199767f8SToomas Soome  */
26199767f8SToomas Soome 
27199767f8SToomas Soome #include <sys/cdefs.h>
28199767f8SToomas Soome #include <string.h>
29199767f8SToomas Soome #include <stand.h>
30199767f8SToomas Soome #include <bootstrap.h>
31402e3d8aSToomas Soome #ifndef BOOT2
32402e3d8aSToomas Soome #include <machine/cpufunc.h>
33402e3d8aSToomas Soome #include "ficl.h"
34402e3d8aSToomas Soome #endif
35199767f8SToomas Soome 
36199767f8SToomas Soome /*
37199767f8SToomas Soome  * Concatenate the (argc) elements of (argv) into a single string, and return
38199767f8SToomas Soome  * a copy of same.
39199767f8SToomas Soome  */
40199767f8SToomas Soome char *
unargv(int argc,char * argv[])41199767f8SToomas Soome unargv(int argc, char *argv[])
42199767f8SToomas Soome {
43199767f8SToomas Soome     size_t	hlong;
44199767f8SToomas Soome     int		i;
45199767f8SToomas Soome     char	*cp;
46199767f8SToomas Soome 
47199767f8SToomas Soome     for (i = 0, hlong = 0; i < argc; i++)
48199767f8SToomas Soome 	hlong += strlen(argv[i]) + 2;
49199767f8SToomas Soome 
50199767f8SToomas Soome     if(hlong == 0)
51199767f8SToomas Soome 	return(NULL);
52199767f8SToomas Soome 
53199767f8SToomas Soome     cp = malloc(hlong);
54199767f8SToomas Soome     cp[0] = 0;
55199767f8SToomas Soome     for (i = 0; i < argc; i++) {
56199767f8SToomas Soome 	strcat(cp, argv[i]);
57199767f8SToomas Soome 	if (i < (argc - 1))
58199767f8SToomas Soome 	  strcat(cp, " ");
59199767f8SToomas Soome     }
60402e3d8aSToomas Soome 
61199767f8SToomas Soome     return(cp);
62199767f8SToomas Soome }
63199767f8SToomas Soome 
64199767f8SToomas Soome /*
65199767f8SToomas Soome  * Get the length of a string in kernel space
66199767f8SToomas Soome  */
67199767f8SToomas Soome size_t
strlenout(vm_offset_t src)68199767f8SToomas Soome strlenout(vm_offset_t src)
69199767f8SToomas Soome {
70199767f8SToomas Soome     char	c;
71199767f8SToomas Soome     size_t	len;
72402e3d8aSToomas Soome 
73199767f8SToomas Soome     for (len = 0; ; len++) {
74199767f8SToomas Soome 	archsw.arch_copyout(src++, &c, 1);
75199767f8SToomas Soome 	if (c == 0)
76199767f8SToomas Soome 	    break;
77199767f8SToomas Soome     }
78199767f8SToomas Soome     return(len);
79199767f8SToomas Soome }
80199767f8SToomas Soome 
81199767f8SToomas Soome /*
82199767f8SToomas Soome  * Make a duplicate copy of a string in kernel space
83199767f8SToomas Soome  */
84199767f8SToomas Soome char *
strdupout(vm_offset_t str)85199767f8SToomas Soome strdupout(vm_offset_t str)
86199767f8SToomas Soome {
87199767f8SToomas Soome     char	*result, *cp;
88402e3d8aSToomas Soome 
89199767f8SToomas Soome     result = malloc(strlenout(str) + 1);
90199767f8SToomas Soome     for (cp = result; ;cp++) {
91199767f8SToomas Soome 	archsw.arch_copyout(str++, cp, 1);
92199767f8SToomas Soome 	if (*cp == 0)
93199767f8SToomas Soome 	    break;
94199767f8SToomas Soome     }
95199767f8SToomas Soome     return(result);
96199767f8SToomas Soome }
97199767f8SToomas Soome 
98199767f8SToomas Soome /* Zero a region in kernel space. */
99199767f8SToomas Soome void
kern_bzero(vm_offset_t dest,size_t len)100199767f8SToomas Soome kern_bzero(vm_offset_t dest, size_t len)
101199767f8SToomas Soome {
102199767f8SToomas Soome 	char buf[256];
103199767f8SToomas Soome 	size_t chunk, resid;
104199767f8SToomas Soome 
105199767f8SToomas Soome 	bzero(buf, sizeof(buf));
106199767f8SToomas Soome 	resid = len;
107199767f8SToomas Soome 	while (resid > 0) {
108199767f8SToomas Soome 		chunk = min(sizeof(buf), resid);
109199767f8SToomas Soome 		archsw.arch_copyin(buf, dest, chunk);
110199767f8SToomas Soome 		resid -= chunk;
111199767f8SToomas Soome 		dest += chunk;
112199767f8SToomas Soome 	}
113199767f8SToomas Soome }
114199767f8SToomas Soome 
115199767f8SToomas Soome /*
116199767f8SToomas Soome  * Read the specified part of a file to kernel space.  Unlike regular
117199767f8SToomas Soome  * pread, the file pointer is advanced to the end of the read data,
118199767f8SToomas Soome  * and it just returns 0 if successful.
119199767f8SToomas Soome  */
120199767f8SToomas Soome int
kern_pread(int fd,vm_offset_t dest,size_t len,off_t off)121199767f8SToomas Soome kern_pread(int fd, vm_offset_t dest, size_t len, off_t off)
122199767f8SToomas Soome {
123199767f8SToomas Soome 
124199767f8SToomas Soome 	if (lseek(fd, off, SEEK_SET) == -1) {
125199767f8SToomas Soome #ifdef DEBUG
126199767f8SToomas Soome 		printf("\nlseek failed\n");
127199767f8SToomas Soome #endif
128199767f8SToomas Soome 		return (-1);
129199767f8SToomas Soome 	}
130199767f8SToomas Soome 	if ((size_t)archsw.arch_readin(fd, dest, len) != len) {
131199767f8SToomas Soome #ifdef DEBUG
132199767f8SToomas Soome 		printf("\nreadin failed\n");
133199767f8SToomas Soome #endif
134199767f8SToomas Soome 		return (-1);
135199767f8SToomas Soome 	}
136199767f8SToomas Soome 	return (0);
137199767f8SToomas Soome }
138199767f8SToomas Soome 
139199767f8SToomas Soome /*
140199767f8SToomas Soome  * Read the specified part of a file to a malloced buffer.  The file
141199767f8SToomas Soome  * pointer is advanced to the end of the read data.
142199767f8SToomas Soome  */
143199767f8SToomas Soome void *
alloc_pread(int fd,off_t off,size_t len)144199767f8SToomas Soome alloc_pread(int fd, off_t off, size_t len)
145199767f8SToomas Soome {
146199767f8SToomas Soome 	void *buf;
147199767f8SToomas Soome 
148199767f8SToomas Soome 	buf = malloc(len);
149199767f8SToomas Soome 	if (buf == NULL) {
150199767f8SToomas Soome #ifdef DEBUG
151199767f8SToomas Soome 		printf("\nmalloc(%d) failed\n", (int)len);
152199767f8SToomas Soome #endif
153199767f8SToomas Soome 		return (NULL);
154199767f8SToomas Soome 	}
155199767f8SToomas Soome 	if (lseek(fd, off, SEEK_SET) == -1) {
156199767f8SToomas Soome #ifdef DEBUG
157199767f8SToomas Soome 		printf("\nlseek failed\n");
158199767f8SToomas Soome #endif
159199767f8SToomas Soome 		free(buf);
160199767f8SToomas Soome 		return (NULL);
161199767f8SToomas Soome 	}
162199767f8SToomas Soome 	if ((size_t)read(fd, buf, len) != len) {
163199767f8SToomas Soome #ifdef DEBUG
164199767f8SToomas Soome 		printf("\nread failed\n");
165199767f8SToomas Soome #endif
166199767f8SToomas Soome 		free(buf);
167199767f8SToomas Soome 		return (NULL);
168199767f8SToomas Soome 	}
169199767f8SToomas Soome 	return (buf);
170199767f8SToomas Soome }
171199767f8SToomas Soome 
172199767f8SToomas Soome /*
173199767f8SToomas Soome  * Display a region in traditional hexdump format.
174199767f8SToomas Soome  */
175199767f8SToomas Soome void
hexdump(caddr_t region,size_t len)176199767f8SToomas Soome hexdump(caddr_t region, size_t len)
177199767f8SToomas Soome {
178199767f8SToomas Soome     caddr_t	line;
179199767f8SToomas Soome     int		x, c;
180199767f8SToomas Soome     char	lbuf[80];
181199767f8SToomas Soome #define emit(fmt, args...)	{sprintf(lbuf, fmt , ## args); pager_output(lbuf);}
182199767f8SToomas Soome 
183199767f8SToomas Soome     pager_open();
184199767f8SToomas Soome     for (line = region; line < (region + len); line += 16) {
185199767f8SToomas Soome 	emit("%08lx  ", (long) line);
186402e3d8aSToomas Soome 
187199767f8SToomas Soome 	for (x = 0; x < 16; x++) {
188199767f8SToomas Soome 	    if ((line + x) < (region + len)) {
189199767f8SToomas Soome 		emit("%02x ", *(u_int8_t *)(line + x));
190199767f8SToomas Soome 	    } else {
191199767f8SToomas Soome 		emit("-- ");
192199767f8SToomas Soome 	    }
193199767f8SToomas Soome 	    if (x == 7)
194199767f8SToomas Soome 		emit(" ");
195199767f8SToomas Soome 	}
196199767f8SToomas Soome 	emit(" |");
197199767f8SToomas Soome 	for (x = 0; x < 16; x++) {
198199767f8SToomas Soome 	    if ((line + x) < (region + len)) {
199199767f8SToomas Soome 		c = *(u_int8_t *)(line + x);
200199767f8SToomas Soome 		if ((c < ' ') || (c > '~'))	/* !isprint(c) */
201199767f8SToomas Soome 		    c = '.';
202199767f8SToomas Soome 		emit("%c", c);
203199767f8SToomas Soome 	    } else {
204199767f8SToomas Soome 		emit(" ");
205199767f8SToomas Soome 	    }
206199767f8SToomas Soome 	}
207199767f8SToomas Soome 	emit("|\n");
208199767f8SToomas Soome     }
209199767f8SToomas Soome     pager_close();
210199767f8SToomas Soome }
211199767f8SToomas Soome 
212199767f8SToomas Soome void
dev_cleanup(void)213199767f8SToomas Soome dev_cleanup(void)
214199767f8SToomas Soome {
215199767f8SToomas Soome     int		i;
216199767f8SToomas Soome 
217199767f8SToomas Soome     /* Call cleanup routines */
218199767f8SToomas Soome     for (i = 0; devsw[i] != NULL; ++i)
219199767f8SToomas Soome 	if (devsw[i]->dv_cleanup != NULL)
220199767f8SToomas Soome 	    (devsw[i]->dv_cleanup)();
221199767f8SToomas Soome }
222402e3d8aSToomas Soome 
223402e3d8aSToomas Soome #ifndef BOOT2
224402e3d8aSToomas Soome /*
225402e3d8aSToomas Soome  * outb ( port# c -- )
226402e3d8aSToomas Soome  * Store a byte to I/O port number port#
227402e3d8aSToomas Soome  */
228402e3d8aSToomas Soome static void
ficlOutb(ficlVm * pVM)229402e3d8aSToomas Soome ficlOutb(ficlVm *pVM)
230402e3d8aSToomas Soome {
231402e3d8aSToomas Soome 	uint8_t c;
232402e3d8aSToomas Soome 	uint32_t port;
233402e3d8aSToomas Soome 
234402e3d8aSToomas Soome 	port = ficlStackPopUnsigned(ficlVmGetDataStack(pVM));
235402e3d8aSToomas Soome 	c = ficlStackPopInteger(ficlVmGetDataStack(pVM));
236402e3d8aSToomas Soome 	outb(port, c);
237402e3d8aSToomas Soome }
238402e3d8aSToomas Soome 
239402e3d8aSToomas Soome /*
240402e3d8aSToomas Soome  * inb ( port# -- c )
241402e3d8aSToomas Soome  * Fetch a byte from I/O port number port#
242402e3d8aSToomas Soome  */
243402e3d8aSToomas Soome static void
ficlInb(ficlVm * pVM)244402e3d8aSToomas Soome ficlInb(ficlVm *pVM)
245402e3d8aSToomas Soome {
246402e3d8aSToomas Soome 	uint8_t c;
247402e3d8aSToomas Soome 	uint32_t port;
248402e3d8aSToomas Soome 
249402e3d8aSToomas Soome 	port = ficlStackPopUnsigned(ficlVmGetDataStack(pVM));
250402e3d8aSToomas Soome 	c = inb(port);
251402e3d8aSToomas Soome 	ficlStackPushInteger(ficlVmGetDataStack(pVM), c);
252402e3d8aSToomas Soome }
253402e3d8aSToomas Soome 
254402e3d8aSToomas Soome static void
ficlCompileCpufunc(ficlSystem * pSys)255402e3d8aSToomas Soome ficlCompileCpufunc(ficlSystem *pSys)
256402e3d8aSToomas Soome {
257402e3d8aSToomas Soome 	ficlDictionary *dp = ficlSystemGetDictionary(pSys);
258402e3d8aSToomas Soome 
259402e3d8aSToomas Soome 	FICL_SYSTEM_ASSERT(pSys, dp);
260402e3d8aSToomas Soome 
261402e3d8aSToomas Soome 	(void) ficlDictionarySetPrimitive(dp, "outb", ficlOutb,
262402e3d8aSToomas Soome 	    FICL_WORD_DEFAULT);
263402e3d8aSToomas Soome 	(void) ficlDictionarySetPrimitive(dp, "inb", ficlInb,
264402e3d8aSToomas Soome 	    FICL_WORD_DEFAULT);
265402e3d8aSToomas Soome }
266402e3d8aSToomas Soome 
267402e3d8aSToomas Soome FICL_COMPILE_SET(ficlCompileCpufunc);
268402e3d8aSToomas Soome #endif
269