2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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
22 * @APPLE_LICENSE_HEADER_END@
25 * Bootparams lookup - netinfo only
26 * Copyright (C) 1989 by NeXT, Inc.
29 #include <mach/mach.h>
32 #include <rpc/types.h>
34 #include <bootparams.h>
37 #include "_lu_types.h"
41 free_bootparams_data(struct bootparamsent
*b
)
45 if (b
== NULL
) return;
47 if (b
->bp_name
!= NULL
) free(b
->bp_name
);
49 param
= b
->bp_bootparams
;
52 while (*param
!= NULL
) free(*param
++);
53 free(b
->bp_bootparams
);
58 free_bootparams(struct bootparamsent
*b
)
60 if (b
== NULL
) return;
61 free_bootparams_data(b
);
66 free_lu_thread_info_bootparams(void *x
)
68 struct lu_thread_info
*tdata
;
70 if (x
== NULL
) return;
72 tdata
= (struct lu_thread_info
*)x
;
74 if (tdata
->lu_entry
!= NULL
)
76 free_bootparams((struct bootparamsent
*)tdata
->lu_entry
);
77 tdata
->lu_entry
= NULL
;
80 _lu_data_free_vm_xdr(tdata
);
85 static struct bootparamsent
*
86 extract_bootparams(XDR
*xdr
)
88 int i
, j
, nkeys
, nvals
, status
;
90 struct bootparamsent
*b
;
92 if (xdr
== NULL
) return NULL
;
94 if (!xdr_int(xdr
, &nkeys
)) return NULL
;
96 b
= (struct bootparamsent
*)calloc(1, sizeof(struct bootparamsent
));
98 for (i
= 0; i
< nkeys
; i
++)
104 status
= _lu_xdr_attribute(xdr
, &key
, &vals
, &nvals
);
119 if ((b
->bp_name
== NULL
) && (!strcmp("name", key
)))
121 b
->bp_name
= vals
[0];
124 else if ((b
->bp_bootparams
== NULL
) && (!strcmp("bootparams", key
)))
126 b
->bp_bootparams
= vals
;
134 for (; j
< nvals
; j
++) free(vals
[j
]);
139 if (b
->bp_name
== NULL
) b
->bp_name
= strdup("");
140 if (b
->bp_bootparams
== NULL
) b
->bp_bootparams
= (char **)calloc(1, sizeof(char *));
146 recycle_bootparams(struct lu_thread_info
*tdata
, struct bootparamsent
*in
)
148 struct bootparamsent
*b
;
150 if (tdata
== NULL
) return;
151 b
= (struct bootparamsent
*)tdata
->lu_entry
;
156 tdata
->lu_entry
= NULL
;
159 if (tdata
->lu_entry
== NULL
)
161 tdata
->lu_entry
= in
;
165 free_bootparams_data(b
);
167 b
->bp_name
= in
->bp_name
;
168 b
->bp_bootparams
= in
->bp_bootparams
;
173 static struct bootparamsent
*
174 lu_bootparams_getbyname(const char *name
)
176 struct bootparamsent
*b
;
178 char namebuf
[_LU_MAXLUSTRLEN
+ BYTES_PER_XDR_UNIT
];
183 static int proc
= -1;
188 if (_lookup_link(_lu_port
, "bootparams_getbyname", &proc
)
195 xdrmem_create(&outxdr
, namebuf
, sizeof(namebuf
), XDR_ENCODE
);
196 if (!xdr__lu_string(&outxdr
, (_lu_string
*)&name
))
198 xdr_destroy(&outxdr
);
202 size
= xdr_getpos(&outxdr
);
203 xdr_destroy(&outxdr
);
208 if (_lookup_all(_lu_port
, proc
, (unit
*)namebuf
, size
, &lookup_buf
,
209 &datalen
) != KERN_SUCCESS
)
214 datalen
*= BYTES_PER_XDR_UNIT
;
215 if ((lookup_buf
== NULL
) || (datalen
== 0)) return NULL
;
217 xdrmem_create(&inxdr
, lookup_buf
, datalen
, XDR_DECODE
);
220 if (!xdr_int(&inxdr
, &count
))
223 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
230 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
234 b
= extract_bootparams(&inxdr
);
236 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
242 lu_bootparams_endent(void)
244 struct lu_thread_info
*tdata
;
246 tdata
= _lu_data_create_key(_lu_data_key_bootparams
, free_lu_thread_info_bootparams
);
247 _lu_data_free_vm_xdr(tdata
);
251 lu_bootparams_setent(void)
253 lu_bootparams_endent();
256 static struct bootparamsent
*
257 lu_bootparams_getent(void)
259 struct bootparamsent
*b
;
260 static int proc
= -1;
261 struct lu_thread_info
*tdata
;
263 tdata
= _lu_data_create_key(_lu_data_key_bootparams
, free_lu_thread_info_bootparams
);
266 tdata
= (struct lu_thread_info
*)calloc(1, sizeof(struct lu_thread_info
));
267 _lu_data_set_key(_lu_data_key_bootparams
, tdata
);
270 if (tdata
->lu_vm
== NULL
)
274 if (_lookup_link(_lu_port
, "bootparams_getent", &proc
) != KERN_SUCCESS
)
276 lu_bootparams_endent();
281 if (_lookup_all(_lu_port
, proc
, NULL
, 0, &(tdata
->lu_vm
), &(tdata
->lu_vm_length
)) != KERN_SUCCESS
)
283 lu_bootparams_endent();
287 /* mig stubs measure size in words (4 bytes) */
288 tdata
->lu_vm_length
*= 4;
290 if (tdata
->lu_xdr
!= NULL
)
292 xdr_destroy(tdata
->lu_xdr
);
295 tdata
->lu_xdr
= (XDR
*)calloc(1, sizeof(XDR
));
297 xdrmem_create(tdata
->lu_xdr
, tdata
->lu_vm
, tdata
->lu_vm_length
, XDR_DECODE
);
298 if (!xdr_int(tdata
->lu_xdr
, &tdata
->lu_vm_cursor
))
300 lu_bootparams_endent();
305 if (tdata
->lu_vm_cursor
== 0)
307 lu_bootparams_endent();
311 b
= extract_bootparams(tdata
->lu_xdr
);
314 lu_bootparams_endent();
318 tdata
->lu_vm_cursor
--;
323 struct bootparamsent
*
324 bootparams_getbyname(const char *name
)
326 struct bootparamsent
*res
;
327 struct lu_thread_info
*tdata
;
329 tdata
= _lu_data_create_key(_lu_data_key_bootparams
, free_lu_thread_info_bootparams
);
332 tdata
= (struct lu_thread_info
*)calloc(1, sizeof(struct lu_thread_info
));
333 _lu_data_set_key(_lu_data_key_bootparams
, tdata
);
338 res
= lu_bootparams_getbyname(name
);
339 recycle_bootparams(tdata
, res
);
340 return (struct bootparamsent
*)tdata
->lu_entry
;
346 struct bootparamsent
*
347 bootparams_getent(void)
349 struct bootparamsent
*res
;
350 struct lu_thread_info
*tdata
;
352 tdata
= _lu_data_create_key(_lu_data_key_bootparams
, free_lu_thread_info_bootparams
);
355 tdata
= (struct lu_thread_info
*)calloc(1, sizeof(struct lu_thread_info
));
356 _lu_data_set_key(_lu_data_key_bootparams
, tdata
);
361 res
= lu_bootparams_getent();
362 recycle_bootparams(tdata
, res
);
363 return (struct bootparamsent
*)tdata
->lu_entry
;
370 bootparams_setent(void)
372 if (_lu_running()) lu_bootparams_setent();
376 bootparams_endent(void)
378 if (_lu_running()) lu_bootparams_endent();