]> git.saurik.com Git - apple/libinfo.git/blob - lookup.subproj/lu_bootparam.c
Libinfo-78.2.tar.gz
[apple/libinfo.git] / lookup.subproj / lu_bootparam.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * Bootparams lookup - netinfo only
26 * Copyright (C) 1989 by NeXT, Inc.
27 */
28 #include <stdlib.h>
29 #include <mach/mach.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <rpc/types.h>
33 #include <rpc/xdr.h>
34 #include <bootparams.h>
35
36 #include "lookup.h"
37 #include "_lu_types.h"
38 #include "lu_utils.h"
39
40 static lookup_state bp_state = LOOKUP_CACHE;
41 static struct bootparamsent global_bp;
42 static int global_free = 1;
43 static char *bp_data = NULL;
44 static unsigned bp_datalen;
45 static int bp_nentries;
46 static int bp_start = 1;
47 static XDR bp_xdr;
48
49 static void
50 freeold(void)
51 {
52 int i;
53 if (global_free == 1) return;
54
55 free(global_bp.bp_name);
56
57 for (i = 0; global_bp.bp_bootparams[i] != NULL; i++)
58 free(global_bp.bp_bootparams[i]);
59
60 global_free = 1;
61 }
62
63 static void
64 convert_bootparamsent(_lu_bootparams_ent *lu_bpent)
65 {
66 int i, len;
67
68 freeold();
69
70 global_bp.bp_name = strdup(lu_bpent->bootparams_name);
71
72 len = lu_bpent->bootparams_keyvalues.bootparams_keyvalues_len;
73 global_bp.bp_bootparams = (char **)malloc((len + 1) * sizeof(char *));
74
75 for (i = 0; i < len; i++)
76 {
77 global_bp.bp_bootparams[i] =
78 strdup(lu_bpent->bootparams_keyvalues.bootparams_keyvalues_val[i]);
79 }
80
81 global_bp.bp_bootparams[len] = NULL;
82
83 global_free = 0;
84 }
85
86 static struct bootparamsent *
87 lu_bootparams_getbyname(const char *name)
88 {
89 unsigned datalen;
90 char namebuf[_LU_MAXLUSTRLEN + BYTES_PER_XDR_UNIT];
91 unit lookup_buf[MAX_INLINE_UNITS];
92 XDR outxdr;
93 XDR inxdr;
94 int size;
95 _lu_bootparams_ent_ptr lu_bpent;
96 static int proc = -1;
97
98 if (proc < 0)
99 {
100 if (_lookup_link(_lu_port, "bootparams_getbyname", &proc)
101 != KERN_SUCCESS)
102 {
103 return (NULL);
104 }
105 }
106
107 xdrmem_create(&outxdr, namebuf, sizeof(namebuf), XDR_ENCODE);
108 if (!xdr__lu_string(&outxdr, &name))
109 {
110 xdr_destroy(&outxdr);
111 return (NULL);
112 }
113
114 size = xdr_getpos(&outxdr);
115 xdr_destroy(&outxdr);
116
117 datalen = MAX_INLINE_UNITS;
118 if (_lookup_one(_lu_port, proc, (unit *)namebuf, size, lookup_buf,
119 &datalen) != KERN_SUCCESS)
120 {
121 return (NULL);
122 }
123
124 datalen *= BYTES_PER_XDR_UNIT;
125 xdrmem_create(&inxdr, lookup_buf, datalen,
126 XDR_DECODE);
127 lu_bpent = NULL;
128 if (!xdr__lu_bootparams_ent_ptr(&inxdr, &lu_bpent) || (lu_bpent == NULL))
129 {
130 xdr_destroy(&inxdr);
131 return (NULL);
132 }
133
134 xdr_destroy(&inxdr);
135
136 convert_bootparamsent(lu_bpent);
137 xdr_free(xdr__lu_bootparams_ent_ptr, &lu_bpent);
138 return (&global_bp);
139 }
140
141 static void
142 lu_bootparams_endent(void)
143 {
144 bp_nentries = 0;
145 if (bp_data != NULL)
146 {
147 freeold();
148 vm_deallocate(mach_task_self(), (vm_address_t)bp_data, bp_datalen);
149 bp_data = NULL;
150 }
151 }
152
153 static void
154 lu_bootparams_setent(void)
155 {
156 lu_bootparams_endent();
157 bp_start = 1;
158 }
159
160 static struct bootparamsent *
161 lu_bootparams_getent(void)
162 {
163 static int proc = -1;
164 _lu_bootparams_ent lu_bpent;
165
166 if (bp_start == 1)
167 {
168 bp_start = 0;
169
170 if (proc < 0)
171 {
172 if (_lookup_link(_lu_port, "bootparams_getent", &proc)
173 != KERN_SUCCESS)
174 {
175 lu_bootparams_endent();
176 return (NULL);
177 }
178 }
179
180 if (_lookup_all(_lu_port, proc, NULL, 0, &bp_data, &bp_datalen)
181 != KERN_SUCCESS)
182 {
183 lu_bootparams_endent();
184 return (NULL);
185 }
186
187 #ifdef NOTDEF
188 /* NOTDEF because OOL buffers are counted in bytes with untyped IPC */
189 bp_datalen *= BYTES_PER_XDR_UNIT;
190 #endif
191 xdrmem_create(&bp_xdr, bp_data, bp_datalen,
192 XDR_DECODE);
193 if (!xdr_int(&bp_xdr, &bp_nentries))
194 {
195 xdr_destroy(&bp_xdr);
196 lu_bootparams_endent();
197 return (NULL);
198 }
199 }
200
201 if (bp_nentries == 0)
202 {
203 xdr_destroy(&bp_xdr);
204 lu_bootparams_endent();
205 return (NULL);
206 }
207
208 bzero(&lu_bpent, sizeof(lu_bpent));
209 if (!xdr__lu_bootparams_ent(&bp_xdr, &lu_bpent))
210 {
211 xdr_destroy(&bp_xdr);
212 lu_bootparams_endent();
213 return (NULL);
214 }
215
216 bp_nentries--;
217 convert_bootparamsent(&lu_bpent);
218 xdr_free(xdr__lu_bootparams_ent, &lu_bpent);
219 return (&global_bp);
220 }
221
222 struct bootparamsent *
223 bootparams_getbyname(const char *name)
224 {
225 if (_lu_running()) return (lu_bootparams_getbyname(name));
226 return (NULL);
227 }
228
229 struct bootparamsent *
230 bootparams_getent(void)
231 {
232 if (_lu_running()) return (lu_bootparams_getent());
233 return (NULL);
234 }
235
236 void
237 bootparams_setent(void)
238 {
239 if (_lu_running()) lu_bootparams_setent();
240 }
241
242 void
243 bootparams_endent(void)
244 {
245 if (_lu_running()) lu_bootparams_endent();
246 }