Initial import from FreeBSD RELENG_4:
[games.git] / sys / boot / common / misc.c
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/boot/common/misc.c,v 1.6.2.1 2000/07/06 00:34:02 ps Exp $
27  */
28
29 #include <string.h>
30 #include <stand.h>
31 #include <bootstrap.h>
32
33 /*
34  * Concatenate the (argc) elements of (argv) into a single string, and return
35  * a copy of same.
36  */
37 char *
38 unargv(int argc, char *argv[])
39 {
40     size_t      hlong;
41     int         i;
42     char        *cp;
43
44     for (hlong = 0, i = 0, hlong = 0; i < argc; i++)
45         hlong += strlen(argv[i]) + 2;
46
47     if(hlong == 0)
48         return(NULL);
49
50     cp = malloc(hlong);
51     cp[0] = 0;
52     for (i = 0; i < argc; i++) {
53         strcat(cp, argv[i]);
54         if (i < (argc - 1))
55           strcat(cp, " ");
56     }
57           
58     return(cp);
59 }
60
61 /*
62  * Get the length of a string in kernel space
63  */
64 size_t
65 strlenout(vm_offset_t src)
66 {
67     char        c;
68     size_t      len;
69     
70     for (len = 0; ; len++) {
71         archsw.arch_copyout(src++, &c, 1);
72         if (c == 0)
73             break;
74     }
75     return(len);
76 }
77
78 /*
79  * Make a duplicate copy of a string in kernel space
80  */
81 char *
82 strdupout(vm_offset_t str)
83 {
84     char        *result, *cp;
85     
86     result = malloc(strlenout(str) + 1);
87     for (cp = result; ;cp++) {
88         archsw.arch_copyout(str++, cp, 1);
89         if (*cp == 0)
90             break;
91     }
92     return(result);
93 }
94
95 /*
96  * Display a region in traditional hexdump format.
97  */
98 void
99 hexdump(caddr_t region, size_t len)
100 {
101     caddr_t     line;
102     int         x, c;
103     char        lbuf[80];
104 #define emit(fmt, args...)      {sprintf(lbuf, fmt , ## args); pager_output(lbuf);}
105
106     pager_open();
107     for (line = region; line < (region + len); line += 16) {
108         emit("%08lx  ", (long) line);
109         
110         for (x = 0; x < 16; x++) {
111             if ((line + x) < (region + len)) {
112                 emit("%02x ", *(u_int8_t *)(line + x));
113             } else {
114                 emit("-- ");
115             }
116             if (x == 7)
117                 emit(" ");
118         }
119         emit(" |");
120         for (x = 0; x < 16; x++) {
121             if ((line + x) < (region + len)) {
122                 c = *(u_int8_t *)(line + x);
123                 if ((c < ' ') || (c > '~'))     /* !isprint(c) */
124                     c = '.';
125                 emit("%c", c);
126             } else {
127                 emit(" ");
128             }
129         }
130         emit("|\n");
131     }
132     pager_close();
133 }
134
135 void
136 dev_cleanup(void)
137 {
138     int         i;
139
140     /* Call cleanup routines */
141     for (i = 0; devsw[i] != NULL; ++i)
142         if (devsw[i]->dv_cleanup != NULL)
143             (devsw[i]->dv_cleanup)();
144 }