misc.c revision 1.6
1/* $NetBSD: misc.c,v 1.6 2012/12/27 20:21:51 martin 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 */ 43char * 44unargv(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 */ 70size_t 71strlenout(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 */ 87char * 88strdupout(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. */ 102void 103kern_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 */ 123int 124kern_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 */ 144void * 145alloc_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/* 170 * Display a region in traditional hexdump format. 171 */ 172void 173hexdump(void *region, size_t len) 174{ 175 void * line; 176 int x, c; 177 char lbuf[80]; 178#define emit(fmt, args...) {sprintf(lbuf, fmt , ## args); pager_output(lbuf);} 179 180 pager_open(); 181 for (line = region; line < (region + len); line += 16) { 182 emit("%08lx ", (long) line); 183 184 for (x = 0; x < 16; x++) { 185 if ((line + x) < (region + len)) { 186 emit("%02x ", *(u_int8_t *)(line + x)); 187 } else { 188 emit("-- "); 189 } 190 if (x == 7) 191 emit(" "); 192 } 193 emit(" |"); 194 for (x = 0; x < 16; x++) { 195 if ((line + x) < (region + len)) { 196 c = *(u_int8_t *)(line + x); 197 if ((c < ' ') || (c > '~')) /* !isprint(c) */ 198 c = '.'; 199 emit("%c", c); 200 } else { 201 emit(" "); 202 } 203 } 204 emit("|\n"); 205 } 206 pager_close(); 207} 208 209void 210dev_cleanup(void) 211{ 212 213} 214