xref: /illumos-gate/usr/src/cmd/picl/plugins/sun4v/mdesc/init.c (revision 65a89a64c60f3061bbe2381edaacc81660af9a95)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <sys/types.h>
33 #include <alloca.h>
34 #include <sys/stat.h>
35 #include <malloc.h>
36 #include <fcntl.h>
37 #include <syslog.h>
38 #include <mdesc.h>
39 #include <string.h>
40 #include <errno.h>
41 
42 #define	MDESC_PATH	"/devices/pseudo/mdesc@0:mdesc"
43 #define	SIZE	8192
44 
45 static void mdesc_free(void *bufp, size_t size);
46 
47 md_t *
48 mdesc_devinit(void)
49 {
50 	int fh;
51 	uint8_t *bufp = NULL;
52 	int res;
53 	int size;
54 	int offset;
55 	md_t *mdp;
56 
57 	fh = open(MDESC_PATH, O_RDONLY, 0);
58 	if (fh < 0) {
59 		return (NULL);
60 	}
61 
62 	size = SIZE;	/* initial size */
63 	offset = 0;
64 
65 	bufp = malloc(size);
66 	if (NULL == bufp) {
67 		return (NULL);
68 	}
69 
70 		/* OK read until we get a EOF */
71 
72 	do {
73 		int len;
74 
75 		len = size - offset;
76 
77 		while (len < SIZE) {
78 			size += SIZE;
79 			bufp = realloc(bufp, size);
80 			if (NULL == bufp)
81 				return (NULL);
82 			len = size - offset;
83 		}
84 
85 		do {
86 			res = read(fh, bufp+offset, len);
87 		} while ((res < 0) && (errno == EAGAIN));
88 
89 		if (res < 0) {
90 			free(bufp);
91 			return (NULL);
92 		}
93 
94 		offset += res;
95 	} while (res > 0);
96 
97 	(void) close(fh);
98 
99 	bufp = realloc(bufp, offset);
100 	if (NULL == bufp)
101 		return (NULL);
102 
103 	mdp = md_init_intern((uint64_t *)bufp, malloc, mdesc_free);
104 	if (NULL == mdp) {
105 		free(bufp);
106 		return (NULL);
107 	}
108 
109 	return (mdp);
110 }
111 
112 /*ARGSUSED*/
113 void
114 mdesc_free(void *bufp, size_t size)
115 {
116 	free(bufp);
117 }
118