2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Bootparams lookup - netinfo only
27 * Copyright (C) 1989 by NeXT, Inc.
30 #include <mach/mach.h>
33 #include <rpc/types.h>
35 #include <bootparams.h>
38 #include "_lu_types.h"
42 free_bootparams_data(struct bootparamsent
*b
)
46 if (b
== NULL
) return;
48 if (b
->bp_name
!= NULL
) free(b
->bp_name
);
50 param
= b
->bp_bootparams
;
53 while (*param
!= NULL
) free(*param
++);
54 free(b
->bp_bootparams
);
59 free_bootparams(struct bootparamsent
*b
)
61 if (b
== NULL
) return;
62 free_bootparams_data(b
);
67 free_lu_thread_info_bootparams(void *x
)
69 struct lu_thread_info
*tdata
;
71 if (x
== NULL
) return;
73 tdata
= (struct lu_thread_info
*)x
;
75 if (tdata
->lu_entry
!= NULL
)
77 free_bootparams((struct bootparamsent
*)tdata
->lu_entry
);
78 tdata
->lu_entry
= NULL
;
81 _lu_data_free_vm_xdr(tdata
);
86 static struct bootparamsent
*
87 extract_bootparams(XDR
*xdr
)
89 int i
, j
, nkeys
, nvals
, status
;
91 struct bootparamsent
*b
;
93 if (xdr
== NULL
) return NULL
;
95 if (!xdr_int(xdr
, &nkeys
)) return NULL
;
97 b
= (struct bootparamsent
*)calloc(1, sizeof(struct bootparamsent
));
99 for (i
= 0; i
< nkeys
; i
++)
105 status
= _lu_xdr_attribute(xdr
, &key
, &vals
, &nvals
);
120 if ((b
->bp_name
== NULL
) && (!strcmp("name", key
)))
122 b
->bp_name
= vals
[0];
125 else if ((b
->bp_bootparams
== NULL
) && (!strcmp("bootparams", key
)))
127 b
->bp_bootparams
= vals
;
135 for (; j
< nvals
; j
++) free(vals
[j
]);
140 if (b
->bp_name
== NULL
) b
->bp_name
= strdup("");
141 if (b
->bp_bootparams
== NULL
) b
->bp_bootparams
= (char **)calloc(1, sizeof(char *));
147 recycle_bootparams(struct lu_thread_info
*tdata
, struct bootparamsent
*in
)
149 struct bootparamsent
*b
;
151 if (tdata
== NULL
) return;
152 b
= (struct bootparamsent
*)tdata
->lu_entry
;
157 tdata
->lu_entry
= NULL
;
160 if (tdata
->lu_entry
== NULL
)
162 tdata
->lu_entry
= in
;
166 free_bootparams_data(b
);
168 b
->bp_name
= in
->bp_name
;
169 b
->bp_bootparams
= in
->bp_bootparams
;
174 static struct bootparamsent
*
175 lu_bootparams_getbyname(const char *name
)
177 struct bootparamsent
*b
;
179 char namebuf
[_LU_MAXLUSTRLEN
+ BYTES_PER_XDR_UNIT
];
184 static int proc
= -1;
189 if (_lookup_link(_lu_port
, "bootparams_getbyname", &proc
)
196 xdrmem_create(&outxdr
, namebuf
, sizeof(namebuf
), XDR_ENCODE
);
197 if (!xdr__lu_string(&outxdr
, (_lu_string
*)&name
))
199 xdr_destroy(&outxdr
);
203 size
= xdr_getpos(&outxdr
);
204 xdr_destroy(&outxdr
);
209 if (_lookup_all(_lu_port
, proc
, (unit
*)namebuf
, size
, &lookup_buf
,
210 &datalen
) != KERN_SUCCESS
)
215 datalen
*= BYTES_PER_XDR_UNIT
;
216 if ((lookup_buf
== NULL
) || (datalen
== 0)) return NULL
;
218 xdrmem_create(&inxdr
, lookup_buf
, datalen
, XDR_DECODE
);
221 if (!xdr_int(&inxdr
, &count
))
224 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
231 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
235 b
= extract_bootparams(&inxdr
);
237 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
243 lu_bootparams_endent(void)
245 struct lu_thread_info
*tdata
;
247 tdata
= _lu_data_create_key(_lu_data_key_bootparams
, free_lu_thread_info_bootparams
);
248 _lu_data_free_vm_xdr(tdata
);
252 lu_bootparams_setent(void)
254 lu_bootparams_endent();
257 static struct bootparamsent
*
258 lu_bootparams_getent(void)
260 struct bootparamsent
*b
;
261 static int proc
= -1;
262 struct lu_thread_info
*tdata
;
264 tdata
= _lu_data_create_key(_lu_data_key_bootparams
, free_lu_thread_info_bootparams
);
267 tdata
= (struct lu_thread_info
*)calloc(1, sizeof(struct lu_thread_info
));
268 _lu_data_set_key(_lu_data_key_bootparams
, tdata
);
271 if (tdata
->lu_vm
== NULL
)
275 if (_lookup_link(_lu_port
, "bootparams_getent", &proc
) != KERN_SUCCESS
)
277 lu_bootparams_endent();
282 if (_lookup_all(_lu_port
, proc
, NULL
, 0, &(tdata
->lu_vm
), &(tdata
->lu_vm_length
)) != KERN_SUCCESS
)
284 lu_bootparams_endent();
288 /* mig stubs measure size in words (4 bytes) */
289 tdata
->lu_vm_length
*= 4;
291 if (tdata
->lu_xdr
!= NULL
)
293 xdr_destroy(tdata
->lu_xdr
);
296 tdata
->lu_xdr
= (XDR
*)calloc(1, sizeof(XDR
));
298 xdrmem_create(tdata
->lu_xdr
, tdata
->lu_vm
, tdata
->lu_vm_length
, XDR_DECODE
);
299 if (!xdr_int(tdata
->lu_xdr
, &tdata
->lu_vm_cursor
))
301 lu_bootparams_endent();
306 if (tdata
->lu_vm_cursor
== 0)
308 lu_bootparams_endent();
312 b
= extract_bootparams(tdata
->lu_xdr
);
315 lu_bootparams_endent();
319 tdata
->lu_vm_cursor
--;
324 struct bootparamsent
*
325 bootparams_getbyname(const char *name
)
327 struct bootparamsent
*res
;
328 struct lu_thread_info
*tdata
;
330 tdata
= _lu_data_create_key(_lu_data_key_bootparams
, free_lu_thread_info_bootparams
);
333 tdata
= (struct lu_thread_info
*)calloc(1, sizeof(struct lu_thread_info
));
334 _lu_data_set_key(_lu_data_key_bootparams
, tdata
);
339 res
= lu_bootparams_getbyname(name
);
340 recycle_bootparams(tdata
, res
);
341 return (struct bootparamsent
*)tdata
->lu_entry
;
347 struct bootparamsent
*
348 bootparams_getent(void)
350 struct bootparamsent
*res
;
351 struct lu_thread_info
*tdata
;
353 tdata
= _lu_data_create_key(_lu_data_key_bootparams
, free_lu_thread_info_bootparams
);
356 tdata
= (struct lu_thread_info
*)calloc(1, sizeof(struct lu_thread_info
));
357 _lu_data_set_key(_lu_data_key_bootparams
, tdata
);
362 res
= lu_bootparams_getent();
363 recycle_bootparams(tdata
, res
);
364 return (struct bootparamsent
*)tdata
->lu_entry
;
371 bootparams_setent(void)
373 if (_lu_running()) lu_bootparams_setent();
377 bootparams_endent(void)
379 if (_lu_running()) lu_bootparams_endent();