xref: /titanic_41/usr/src/cmd/picl/plugins/sun4v/mdesc/init.c (revision 890e8ff10cfc85bc7d33064a9a30c3e8477b4813)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 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 uint8_t *md_bufp;
47 
48 md_t *
49 mdesc_devinit(void)
50 {
51 	int fh;
52 	int res;
53 	int size;
54 	int offset;
55 	md_t *mdp;
56 
57 	if (md_bufp != NULL)
58 		return (NULL);
59 
60 	fh = open(MDESC_PATH, O_RDONLY, 0);
61 	if (fh < 0) {
62 		return (NULL);
63 	}
64 
65 	size = SIZE;	/* initial size */
66 	offset = 0;
67 
68 	md_bufp = malloc(size);
69 	if (NULL == md_bufp) {
70 		return (NULL);
71 	}
72 
73 		/* OK read until we get a EOF */
74 
75 	do {
76 		int len;
77 
78 		len = size - offset;
79 
80 		while (len < SIZE) {
81 			size += SIZE;
82 			md_bufp = realloc(md_bufp, size);
83 			if (NULL == md_bufp)
84 				return (NULL);
85 			len = size - offset;
86 		}
87 
88 		do {
89 			res = read(fh, md_bufp + offset, len);
90 		} while ((res < 0) && (errno == EAGAIN));
91 
92 		if (res < 0) {
93 			free(md_bufp);
94 			return (NULL);
95 		}
96 
97 		offset += res;
98 	} while (res > 0);
99 
100 	(void) close(fh);
101 
102 	md_bufp = realloc(md_bufp, offset);
103 	if (NULL == md_bufp)
104 		return (NULL);
105 
106 	mdp = md_init_intern((uint64_t *)md_bufp, malloc, mdesc_free);
107 	if (NULL == mdp) {
108 		free(md_bufp);
109 		return (NULL);
110 	}
111 
112 	return (mdp);
113 }
114 
115 /*ARGSUSED*/
116 void
117 mdesc_free(void *bufp, size_t size)
118 {
119 	if (bufp)
120 		free(bufp);
121 }
122 
123 void
124 mdesc_devfini(md_t *mdp)
125 {
126 	if (mdp)
127 		(void) md_fini(mdp);
128 
129 	if (md_bufp)
130 		free(md_bufp);
131 	md_bufp = NULL;
132 }
133