VirtualBox

source: vbox/trunk/src/VBox/VMM/CFGM.cpp@ 9566

Last change on this file since 9566 was 9328, checked in by vboxsync, 16 years ago

typos.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 72.4 KB
Line 
1/* $Id: CFGM.cpp 9328 2008-06-02 21:24:07Z vboxsync $ */
2/** @file
3 * CFGM - Configuration Manager.
4 */
5
6/*
7 * Copyright (C) 2006-2008 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22/** @page pg_cfgm CFGM - The Configuration Manager
23 *
24 * The configuration manager is responsible for storing the configuration
25 * of the VM at run time. It is organized a bit like file hierarchy,
26 * except that the values live in a separate name space, i.e. value names
27 * can include path separators.
28 *
29 * The VMM user creates the configuration tree as part of the VMR3Create()
30 * call via the pfnCFGMConstructor callback argument. If this isn't specified
31 * a simple default tree is created by cfgmR3CreateDefaultTree(). When used
32 * in the normal setup, this function is found in Main/ConsoleImpl2.cpp.
33 * For the VBoxBFE case, see the VBoxBFE.cpp.
34 *
35 * Devices, drivers, services and other PDM stuff are given their own subtree
36 * where they are protected from accessing information of any parents. This is
37 * is implemented via the CFGMR3SetRestrictedRoot() API.
38 *
39 * Validating of the data obtained, except for validation of the primitive
40 * type, is left the caller. The caller is in a better position to know the
41 * proper validation rules of the individual properties.
42 *
43 *
44 * @section sec_cfgm_primitives Data Primitives
45 *
46 * CFGM supports the following data primitives:
47 * - Integers. Representation is unsigned 64-bit. Boolean, unsigned and
48 * small integers, and pointers are all represented using this primitive.
49 * - Zero terminated character strings. These are of course UTF-8.
50 * - Variable length byte strings. This can be used to get/put binary
51 * objects.
52 *
53 */
54
55/*******************************************************************************
56* Header Files *
57*******************************************************************************/
58#define LOG_GROUP LOG_GROUP_CFGM
59#include <VBox/cfgm.h>
60#include <VBox/dbgf.h>
61#include <VBox/mm.h>
62#include "CFGMInternal.h"
63#include <VBox/vm.h>
64#include <VBox/err.h>
65
66#include <VBox/log.h>
67#include <iprt/assert.h>
68#include <iprt/string.h>
69#include <iprt/uuid.h>
70
71
72/*******************************************************************************
73* Internal Functions *
74*******************************************************************************/
75static int cfgmR3CreateDefaultTree(PVM pVM);
76static void cfgmR3DumpPath(PCFGMNODE pNode, PCDBGFINFOHLP pHlp);
77static void cfgmR3Dump(PCFGMNODE pRoot, unsigned iLevel, PCDBGFINFOHLP pHlp);
78static DECLCALLBACK(void) cfgmR3Info(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
79static int cfgmR3ResolveNode(PCFGMNODE pNode, const char *pszPath, PCFGMNODE *ppChild);
80static int cfgmR3ResolveLeaf(PCFGMNODE pNode, const char *pszName, PCFGMLEAF *ppLeaf);
81static int cfgmR3InsertLeaf(PCFGMNODE pNode, const char *pszName, PCFGMLEAF *ppLeaf);
82static void cfgmR3RemoveLeaf(PCFGMNODE pNode, PCFGMLEAF pLeaf);
83static void cfgmR3FreeValue(PCFGMLEAF pLeaf);
84
85
86
87/**
88 * Constructs the configuration for the VM.
89 *
90 * @returns VBox status code.
91 * @param pVM Pointer to VM which configuration has not yet been loaded.
92 * @param pfnCFGMConstructor Pointer to callback function for constructing the VM configuration tree.
93 * This is called in the EM.
94 * @param pvUser The user argument passed to pfnCFGMConstructor.
95 */
96CFGMR3DECL(int) CFGMR3Init(PVM pVM, PFNCFGMCONSTRUCTOR pfnCFGMConstructor, void *pvUser)
97{
98 LogFlow(("CFGMR3Init: pfnCFGMConstructor=%p pvUser=%p\n", pfnCFGMConstructor, pvUser));
99
100 /*
101 * Init data members.
102 */
103 pVM->cfgm.s.offVM = RT_OFFSETOF(VM, cfgm);
104 pVM->cfgm.s.pRoot = NULL;
105
106 /*
107 * Register DBGF into item.
108 */
109 int rc = DBGFR3InfoRegisterInternal(pVM, "cfgm", "Dumps a part of the CFGM tree. The argument indicates where to start.", cfgmR3Info);
110 AssertRCReturn(rc,rc);
111
112 /*
113 * Create the configuration tree.
114 */
115 if (pfnCFGMConstructor)
116 {
117 /*
118 * Root Node.
119 */
120 PCFGMNODE pRoot = (PCFGMNODE)MMR3HeapAllocZ(pVM, MM_TAG_CFGM, sizeof(*pRoot));
121 if (!pRoot)
122 return VERR_NO_MEMORY;
123 pRoot->pVM = pVM;
124 pRoot->cchName = 0;
125 pVM->cfgm.s.pRoot = pRoot;
126
127 /*
128 * Call the constructor.
129 */
130 rc = pfnCFGMConstructor(pVM, pvUser);
131 }
132 else
133 rc = cfgmR3CreateDefaultTree(pVM);
134 if (VBOX_SUCCESS(rc))
135 {
136 Log(("CFGMR3Init: Successfully constructed the configuration\n"));
137 CFGMR3Dump(CFGMR3GetRoot(pVM));
138
139 }
140 else
141 NOT_DMIK(AssertMsgFailed(("Constructor failed with rc=%Vrc pfnCFGMConstructor=%p\n", rc, pfnCFGMConstructor)));
142
143 return rc;
144}
145
146
147/**
148 * Terminates the configuration manager.
149 *
150 * @returns VBox status code.
151 * @param pVM VM handle.
152 */
153CFGMR3DECL(int) CFGMR3Term(PVM pVM)
154{
155 CFGMR3RemoveNode(pVM->cfgm.s.pRoot);
156 return 0;
157}
158
159
160/**
161 * Gets the root node for the VM.
162 *
163 * @returns Pointer to root node.
164 * @param pVM VM handle.
165 */
166CFGMR3DECL(PCFGMNODE) CFGMR3GetRoot(PVM pVM)
167{
168 return pVM->cfgm.s.pRoot;
169}
170
171
172/**
173 * Gets the parent of a CFGM node.
174 *
175 * @returns Pointer to the parent node.
176 * @returns NULL if pNode is Root or pNode is the start of a
177 * restricted subtree (use CFGMr3GetParentEx() for that).
178 *
179 * @param pNode The node which parent we query.
180 */
181CFGMR3DECL(PCFGMNODE) CFGMR3GetParent(PCFGMNODE pNode)
182{
183 if (pNode && !pNode->fRestrictedRoot)
184 return pNode->pParent;
185 return NULL;
186}
187
188
189/**
190 * Gets the parent of a CFGM node.
191 *
192 * @returns Pointer to the parent node.
193 * @returns NULL if pNode is Root or pVM is not correct.
194 *
195 * @param pVM The VM handle, used as token that the caller is trusted.
196 * @param pNode The node which parent we query.
197 */
198CFGMR3DECL(PCFGMNODE) CFGMR3GetParentEx(PVM pVM, PCFGMNODE pNode)
199{
200 if (pNode && pNode->pVM == pVM)
201 return pNode->pParent;
202 return NULL;
203}
204
205
206/**
207 * Query a child node.
208 *
209 * @returns Pointer to the specified node.
210 * @returns NULL if node was not found or pNode is NULL.
211 * @param pNode Node pszPath is relative to.
212 * @param pszPath Path to the child node or pNode.
213 * It's good style to end this with '/'.
214 */
215CFGMR3DECL(PCFGMNODE) CFGMR3GetChild(PCFGMNODE pNode, const char *pszPath)
216{
217 PCFGMNODE pChild;
218 int rc = cfgmR3ResolveNode(pNode, pszPath, &pChild);
219 if (VBOX_SUCCESS(rc))
220 return pChild;
221 return NULL;
222}
223
224
225/**
226 * Query a child node by a format string.
227 *
228 * @returns Pointer to the specified node.
229 * @returns NULL if node was not found or pNode is NULL.
230 * @param pNode Node pszPath is relative to.
231 * @param pszPathFormat Path to the child node or pNode.
232 * It's good style to end this with '/'.
233 * @param ... Arguments to pszPathFormat.
234 */
235CFGMR3DECL(PCFGMNODE) CFGMR3GetChildF(PCFGMNODE pNode, const char *pszPathFormat, ...)
236{
237 va_list Args;
238 va_start(Args, pszPathFormat);
239 PCFGMNODE pRet = CFGMR3GetChildFV(pNode, pszPathFormat, Args);
240 va_end(Args);
241 return pRet;
242}
243
244
245/**
246 * Query a child node by a format string.
247 *
248 * @returns Pointer to the specified node.
249 * @returns NULL if node was not found or pNode is NULL.
250 * @param pNode Node pszPath is relative to.
251 * @param pszPathFormat Path to the child node or pNode.
252 * It's good style to end this with '/'.
253 * @param Args Arguments to pszPathFormat.
254 */
255CFGMR3DECL(PCFGMNODE) CFGMR3GetChildFV(PCFGMNODE pNode, const char *pszPathFormat, va_list Args)
256{
257 char *pszPath;
258 RTStrAPrintfV(&pszPath, pszPathFormat, Args);
259 if (pszPath)
260 {
261 PCFGMNODE pChild;
262 int rc = cfgmR3ResolveNode(pNode, pszPath, &pChild);
263 if (VBOX_SUCCESS(rc))
264 return pChild;
265 RTStrFree(pszPath);
266 }
267 return NULL;
268}
269
270
271/**
272 * Gets the first child node.
273 * Use this to start an enumeration of child nodes.
274 *
275 * @returns Pointer to the first child.
276 * @returns NULL if no children.
277 * @param pNode Node to enumerate children for.
278 */
279CFGMR3DECL(PCFGMNODE) CFGMR3GetFirstChild(PCFGMNODE pNode)
280{
281 return pNode ? pNode->pFirstChild : NULL;
282}
283
284
285/**
286 * Gets the next sibling node.
287 * Use this to continue an enumeration.
288 *
289 * @returns Pointer to the first child.
290 * @returns NULL if no children.
291 * @param pCur Node to returned by a call to CFGMR3GetFirstChild()
292 * or successive calls to this function.
293 */
294CFGMR3DECL(PCFGMNODE) CFGMR3GetNextChild(PCFGMNODE pCur)
295{
296 return pCur ? pCur->pNext : NULL;
297}
298
299
300/**
301 * Gets the name of the current node.
302 * (Needed for enumeration.)
303 *
304 * @returns VBox status code.
305 * @param pCur Node to returned by a call to CFGMR3GetFirstChild()
306 * or successive calls to CFGMR3GetNextChild().
307 * @param pszName Where to store the node name.
308 * @param cchName Size of the buffer pointed to by pszName (with terminator).
309 */
310CFGMR3DECL(int) CFGMR3GetName(PCFGMNODE pCur, char *pszName, size_t cchName)
311{
312 int rc;
313 if (pCur)
314 {
315 if (cchName > pCur->cchName)
316 {
317 rc = VINF_SUCCESS;
318 memcpy(pszName, pCur->szName, pCur->cchName + 1);
319 }
320 else
321 rc = VERR_CFGM_NOT_ENOUGH_SPACE;
322 }
323 else
324 rc = VERR_CFGM_NO_NODE;
325 return rc;
326}
327
328
329/**
330 * Gets the length of the current node's name.
331 * (Needed for enumeration.)
332 *
333 * @returns Node name length in bytes including the terminating null char.
334 * @returns 0 if pCur is NULL.
335 * @param pCur Node to returned by a call to CFGMR3GetFirstChild()
336 * or successive calls to CFGMR3GetNextChild().
337 */
338CFGMR3DECL(int) CFGMR3GetNameLen(PCFGMNODE pCur)
339{
340 return pCur ? pCur->cchName + 1 : 0;
341}
342
343
344/**
345 * Validates that the child nodes are within a set of valid names.
346 *
347 * @returns true if all names are found in pszzAllowed.
348 * @returns false if not.
349 * @param pNode The node which children should be examined.
350 * @param pszzValid List of valid names separated by '\\0' and ending with
351 * a double '\\0'.
352 */
353CFGMR3DECL(bool) CFGMR3AreChildrenValid(PCFGMNODE pNode, const char *pszzValid)
354{
355 if (pNode)
356 {
357 for (PCFGMNODE pChild = pNode->pFirstChild; pChild; pChild = pChild->pNext)
358 {
359 /* search pszzValid for the name */
360 const char *psz = pszzValid;
361 while (*psz)
362 {
363 size_t cch = strlen(psz);
364 if ( cch == pChild->cchName
365 && !memcmp(psz, pChild->szName, cch))
366 break;
367
368 /* next */
369 psz += cch + 1;
370 }
371
372 /* if at end of pszzValid we didn't find it => failure */
373 if (!*psz)
374 {
375 AssertMsgFailed(("Couldn't find '%s' in the valid values\n", pChild->szName));
376 return false;
377 }
378 }
379 }
380
381 /* all ok. */
382 return true;
383}
384
385
386/**
387 * Gets the first value of a node.
388 * Use this to start an enumeration of values.
389 *
390 * @returns Pointer to the first value.
391 * @param pCur The node (Key) which values to enumerate.
392 */
393CFGMR3DECL(PCFGMLEAF) CFGMR3GetFirstValue(PCFGMNODE pCur)
394{
395 return pCur ? pCur->pFirstLeaf : NULL;
396}
397
398/**
399 * Gets the next value in enumeration.
400 *
401 * @returns Pointer to the next value.
402 * @param pCur The current value as returned by this function or CFGMR3GetFirstValue().
403 */
404CFGMR3DECL(PCFGMLEAF) CFGMR3GetNextValue(PCFGMLEAF pCur)
405{
406 return pCur ? pCur->pNext : NULL;
407}
408
409/**
410 * Get the value name.
411 * (Needed for enumeration.)
412 *
413 * @returns VBox status code.
414 * @param pCur Value returned by a call to CFGMR3GetFirstValue()
415 * or successive calls to CFGMR3GetNextValue().
416 * @param pszName Where to store the value name.
417 * @param cchName Size of the buffer pointed to by pszName (with terminator).
418 */
419CFGMR3DECL(int) CFGMR3GetValueName(PCFGMLEAF pCur, char *pszName, size_t cchName)
420{
421 int rc;
422 if (pCur)
423 {
424 if (cchName > pCur->cchName)
425 {
426 rc = VINF_SUCCESS;
427 memcpy(pszName, pCur->szName, pCur->cchName + 1);
428 }
429 else
430 rc = VERR_CFGM_NOT_ENOUGH_SPACE;
431 }
432 else
433 rc = VERR_CFGM_NO_NODE;
434 return rc;
435}
436
437
438/**
439 * Gets the length of the current node's name.
440 * (Needed for enumeration.)
441 *
442 * @returns Value name length in bytes including the terminating null char.
443 * @returns 0 if pCur is NULL.
444 * @param pCur Value returned by a call to CFGMR3GetFirstValue()
445 * or successive calls to CFGMR3GetNextValue().
446 */
447CFGMR3DECL(int) CFGMR3GetValueNameLen(PCFGMLEAF pCur)
448{
449 return pCur ? pCur->cchName + 1 : 0;
450}
451
452/**
453 * Gets the value type.
454 * (For enumeration.)
455 *
456 * @returns VBox status code.
457 * @param pCur Value returned by a call to CFGMR3GetFirstValue()
458 * or successive calls to CFGMR3GetNextValue().
459 */
460CFGMR3DECL(CFGMVALUETYPE) CFGMR3GetValueType(PCFGMLEAF pCur)
461{
462 Assert(pCur);
463 return pCur->enmType;
464}
465
466
467/**
468 * Validates that the values are within a set of valid names.
469 *
470 * @returns true if all names are found in pszzAllowed.
471 * @returns false if not.
472 * @param pNode The node which values should be examined.
473 * @param pszzValid List of valid names separated by '\\0' and ending with
474 * a double '\\0'.
475 */
476CFGMR3DECL(bool) CFGMR3AreValuesValid(PCFGMNODE pNode, const char *pszzValid)
477{
478 if (pNode)
479 {
480 for (PCFGMLEAF pLeaf = pNode->pFirstLeaf; pLeaf; pLeaf = pLeaf->pNext)
481 {
482 /* search pszzValid for the name */
483 const char *psz = pszzValid;
484 while (*psz)
485 {
486 size_t cch = strlen(psz);
487 if ( cch == pLeaf->cchName
488 && !memcmp(psz, pLeaf->szName, cch))
489 break;
490
491 /* next */
492 psz += cch + 1;
493 }
494
495 /* if at end of pszzValid we didn't find it => failure */
496 if (!*psz)
497 {
498 AssertMsgFailed(("Couldn't find '%s' in the valid values\n", pLeaf->szName));
499 return false;
500 }
501 }
502 }
503
504 /* all ok. */
505 return true;
506}
507
508
509
510/**
511 * Query value type.
512 *
513 * @returns VBox status code.
514 * @param pNode Which node to search for pszName in.
515 * @param pszName Name of an integer value.
516 * @param penmType Where to store the type.
517 */
518CFGMR3DECL(int) CFGMR3QueryType(PCFGMNODE pNode, const char *pszName, PCFGMVALUETYPE penmType)
519{
520 PCFGMLEAF pLeaf;
521 int rc = cfgmR3ResolveLeaf(pNode, pszName, &pLeaf);
522 if (VBOX_SUCCESS(rc))
523 {
524 if (penmType)
525 *penmType = pLeaf->enmType;
526 }
527 return rc;
528}
529
530
531/**
532 * Query value size.
533 * This works on all types of values.
534 *
535 * @returns VBox status code.
536 * @param pNode Which node to search for pszName in.
537 * @param pszName Name of an integer value.
538 * @param pcb Where to store the value size.
539 */
540CFGMR3DECL(int) CFGMR3QuerySize(PCFGMNODE pNode, const char *pszName, size_t *pcb)
541{
542 PCFGMLEAF pLeaf;
543 int rc = cfgmR3ResolveLeaf(pNode, pszName, &pLeaf);
544 if (VBOX_SUCCESS(rc))
545 {
546 switch (pLeaf->enmType)
547 {
548 case CFGMVALUETYPE_INTEGER:
549 *pcb = sizeof(pLeaf->Value.Integer.u64);
550 break;
551
552 case CFGMVALUETYPE_STRING:
553 *pcb = pLeaf->Value.String.cch;
554 break;
555
556 case CFGMVALUETYPE_BYTES:
557 *pcb = pLeaf->Value.Bytes.cb;
558 break;
559
560 default:
561 rc = VERR_INTERNAL_ERROR;
562 AssertMsgFailed(("Invalid value type %d\n", pLeaf->enmType));
563 break;
564 }
565 }
566 return rc;
567}
568
569
570/**
571 * Query integer value.
572 *
573 * @returns VBox status code.
574 * @param pNode Which node to search for pszName in.
575 * @param pszName Name of an integer value.
576 * @param pu64 Where to store the integer value.
577 */
578CFGMR3DECL(int) CFGMR3QueryInteger(PCFGMNODE pNode, const char *pszName, uint64_t *pu64)
579{
580 PCFGMLEAF pLeaf;
581 int rc = cfgmR3ResolveLeaf(pNode, pszName, &pLeaf);
582 if (VBOX_SUCCESS(rc))
583 {
584 if (pLeaf->enmType == CFGMVALUETYPE_INTEGER)
585 *pu64 = pLeaf->Value.Integer.u64;
586 else
587 rc = VERR_CFGM_NOT_INTEGER;
588 }
589 return rc;
590}
591
592
593/**
594 * Query integer value with default.
595 *
596 * @returns VBox status code.
597 * @param pNode Which node to search for pszName in.
598 * @param pszName Name of an integer value.
599 * @param pu64 Where to store the integer value.
600 * @param u64Def The default value.
601 */
602CFGMR3DECL(int) CFGMR3QueryIntegerDef(PCFGMNODE pNode, const char *pszName, uint64_t *pu64, uint64_t u64Def)
603{
604 PCFGMLEAF pLeaf;
605 int rc = cfgmR3ResolveLeaf(pNode, pszName, &pLeaf);
606 if (VBOX_SUCCESS(rc))
607 {
608 if (pLeaf->enmType == CFGMVALUETYPE_INTEGER)
609 *pu64 = pLeaf->Value.Integer.u64;
610 else
611 rc = VERR_CFGM_NOT_INTEGER;
612 }
613 else if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
614 {
615 *pu64 = u64Def;
616 rc = VINF_SUCCESS;
617 }
618 return rc;
619}
620
621
622/**
623 * Query zero terminated character value.
624 *
625 * @returns VBox status code.
626 * @param pNode Which node to search for pszName in.
627 * @param pszName Name of a zero terminate character value.
628 * @param pszString Where to store the string.
629 * @param cchString Size of the string buffer. (Includes terminator.)
630 */
631CFGMR3DECL(int) CFGMR3QueryString(PCFGMNODE pNode, const char *pszName, char *pszString, size_t cchString)
632{
633 PCFGMLEAF pLeaf;
634 int rc = cfgmR3ResolveLeaf(pNode, pszName, &pLeaf);
635 if (VBOX_SUCCESS(rc))
636 {
637 if (pLeaf->enmType == CFGMVALUETYPE_STRING)
638 {
639 if (cchString >= pLeaf->Value.String.cch)
640 {
641 memcpy(pszString, pLeaf->Value.String.psz, pLeaf->Value.String.cch);
642 memset(pszString + pLeaf->Value.String.cch, 0, cchString - pLeaf->Value.String.cch);
643 }
644 else
645 rc = VERR_CFGM_NOT_ENOUGH_SPACE;
646 }
647 else
648 rc = VERR_CFGM_NOT_STRING;
649 }
650 return rc;
651}
652
653
654/**
655 * Query zero terminated character value with default.
656 *
657 * @returns VBox status code.
658 * @param pNode Which node to search for pszName in.
659 * @param pszName Name of a zero terminate character value.
660 * @param pszString Where to store the string.
661 * @param cchString Size of the string buffer. (Includes terminator.)
662 * @param pszDef The default value.
663 */
664CFGMR3DECL(int) CFGMR3QueryStringDef(PCFGMNODE pNode, const char *pszName, char *pszString, size_t cchString, const char *pszDef)
665{
666 PCFGMLEAF pLeaf;
667 int rc = cfgmR3ResolveLeaf(pNode, pszName, &pLeaf);
668 if (VBOX_SUCCESS(rc))
669 {
670 if (pLeaf->enmType == CFGMVALUETYPE_STRING)
671 {
672 if (cchString >= pLeaf->Value.String.cch)
673 {
674 memcpy(pszString, pLeaf->Value.String.psz, pLeaf->Value.String.cch);
675 memset(pszString + pLeaf->Value.String.cch, 0, cchString - pLeaf->Value.String.cch);
676 }
677 else
678 rc = VERR_CFGM_NOT_ENOUGH_SPACE;
679 }
680 else
681 rc = VERR_CFGM_NOT_STRING;
682 }
683 else if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
684 {
685 size_t cchDef = strlen(pszDef);
686 if (cchString > cchDef)
687 {
688 memcpy(pszString, pszDef, cchDef);
689 memset(pszString + cchDef, 0, cchString - cchDef);
690 rc = VINF_SUCCESS;
691 }
692 else
693 rc = VERR_CFGM_NOT_ENOUGH_SPACE;
694 }
695 return rc;
696}
697
698
699/**
700 * Query byte string value.
701 *
702 * @returns VBox status code.
703 * @param pNode Which node to search for pszName in.
704 * @param pszName Name of a byte string value.
705 * @param pvData Where to store the binary data.
706 * @param cbData Size of buffer pvData points too.
707 */
708CFGMR3DECL(int) CFGMR3QueryBytes(PCFGMNODE pNode, const char *pszName, void *pvData, size_t cbData)
709{
710 PCFGMLEAF pLeaf;
711 int rc = cfgmR3ResolveLeaf(pNode, pszName, &pLeaf);
712 if (VBOX_SUCCESS(rc))
713 {
714 if (pLeaf->enmType == CFGMVALUETYPE_BYTES)
715 {
716 if (cbData >= pLeaf->Value.Bytes.cb)
717 {
718 memcpy(pvData, pLeaf->Value.Bytes.pau8, pLeaf->Value.Bytes.cb);
719 memset((char *)pvData + pLeaf->Value.Bytes.cb, 0, cbData - pLeaf->Value.Bytes.cb);
720 }
721 else
722 rc = VERR_CFGM_NOT_ENOUGH_SPACE;
723 }
724 else
725 rc = VERR_CFGM_NOT_BYTES;
726 }
727 return rc;
728}
729
730
731/**
732 * Creates the default configuration.
733 * This assumes an empty tree.
734 *
735 * @returns VBox status code.
736 * @param pVM VM handle.
737 */
738static int cfgmR3CreateDefaultTree(PVM pVM)
739{
740 int rc;
741 int rcAll = VINF_SUCCESS;
742#define UPDATERC() do { if (VBOX_FAILURE(rc) && VBOX_SUCCESS(rcAll)) rcAll = rc; } while (0)
743
744 /*
745 * Root level.
746 */
747 PCFGMNODE pRoot = (PCFGMNODE)MMR3HeapAllocZ(pVM, MM_TAG_CFGM, sizeof(*pRoot));
748 if (!pRoot)
749 return VERR_NO_MEMORY;
750 pRoot->pVM = pVM;
751 pRoot->cchName = 0;
752
753 Assert(!pVM->cfgm.s.pRoot);
754 pVM->cfgm.s.pRoot = pRoot;
755
756 /*
757 * Create VM default values.
758 */
759 rc = CFGMR3InsertString(pRoot, "Name", "Default VM");
760 UPDATERC();
761 rc = CFGMR3InsertInteger(pRoot, "RamSize", 128 * _1M);
762 UPDATERC();
763 rc = CFGMR3InsertInteger(pRoot, "TimerMillies", 10);
764 UPDATERC();
765 rc = CFGMR3InsertInteger(pRoot, "RawR3Enabled", 1);
766 UPDATERC();
767 /** @todo CFGM Defaults: RawR0, PATMEnabled and CASMEnabled needs attention later. */
768 rc = CFGMR3InsertInteger(pRoot, "RawR0Enabled", 1);
769 UPDATERC();
770 rc = CFGMR3InsertInteger(pRoot, "PATMEnabled", 1);
771 UPDATERC();
772 rc = CFGMR3InsertInteger(pRoot, "CSAMEnabled", 1);
773 UPDATERC();
774
775 /*
776 * PDM.
777 */
778 PCFGMNODE pPdm;
779 rc = CFGMR3InsertNode(pRoot, "PDM", &pPdm);
780 UPDATERC();
781 PCFGMNODE pDevices = NULL;
782 rc = CFGMR3InsertNode(pPdm, "Devices", &pDevices);
783 UPDATERC();
784 rc = CFGMR3InsertInteger(pDevices, "LoadBuiltin", 1); /* boolean */
785 UPDATERC();
786 PCFGMNODE pDrivers = NULL;
787 rc = CFGMR3InsertNode(pPdm, "Drivers", &pDrivers);
788 UPDATERC();
789 rc = CFGMR3InsertInteger(pDrivers, "LoadBuiltin", 1); /* boolean */
790 UPDATERC();
791
792
793 /*
794 * Devices
795 */
796 pDevices = NULL;
797 rc = CFGMR3InsertNode(pRoot, "Devices", &pDevices);
798 UPDATERC();
799 /* device */
800 PCFGMNODE pDev = NULL;
801 PCFGMNODE pInst = NULL;
802 PCFGMNODE pCfg = NULL;
803#if 0
804 PCFGMNODE pLunL0 = NULL;
805 PCFGMNODE pLunL1 = NULL;
806#endif
807
808 /*
809 * PC Arch.
810 */
811 rc = CFGMR3InsertNode(pDevices, "pcarch", &pDev);
812 UPDATERC();
813 rc = CFGMR3InsertNode(pDev, "0", &pInst);
814 UPDATERC();
815 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
816 UPDATERC();
817 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
818 UPDATERC();
819
820 /*
821 * PC Bios.
822 */
823 rc = CFGMR3InsertNode(pDevices, "pcbios", &pDev);
824 UPDATERC();
825 rc = CFGMR3InsertNode(pDev, "0", &pInst);
826 UPDATERC();
827 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
828 UPDATERC();
829 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
830 UPDATERC();
831 rc = CFGMR3InsertInteger(pCfg, "RamSize", 128 * _1M);
832 UPDATERC();
833 rc = CFGMR3InsertString(pCfg, "BootDevice0", "IDE");
834 UPDATERC();
835 rc = CFGMR3InsertString(pCfg, "BootDevice1", "NONE");
836 UPDATERC();
837 rc = CFGMR3InsertString(pCfg, "BootDevice2", "NONE");
838 UPDATERC();
839 rc = CFGMR3InsertString(pCfg, "BootDevice3", "NONE");
840 UPDATERC();
841 rc = CFGMR3InsertString(pCfg, "HardDiskDevice", "piix3ide");
842 UPDATERC();
843 rc = CFGMR3InsertString(pCfg, "FloppyDevice", "");
844 UPDATERC();
845 RTUUID Uuid;
846 RTUuidClear(&Uuid);
847 rc = CFGMR3InsertBytes(pCfg, "UUID", &Uuid, sizeof(Uuid));
848 UPDATERC();
849
850 /*
851 * PCI bus.
852 */
853 rc = CFGMR3InsertNode(pDevices, "pci", &pDev); /* piix3 */
854 UPDATERC();
855 rc = CFGMR3InsertNode(pDev, "0", &pInst);
856 UPDATERC();
857 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
858 UPDATERC();
859 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
860 UPDATERC();
861
862 /*
863 * PS/2 keyboard & mouse
864 */
865 rc = CFGMR3InsertNode(pDevices, "pckbd", &pDev);
866 UPDATERC();
867 rc = CFGMR3InsertNode(pDev, "0", &pInst);
868 UPDATERC();
869 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
870 UPDATERC();
871#if 0
872 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0);
873 UPDATERC();
874 rc = CFGMR3InsertString(pLunL0, "Driver", "KeyboardQueue");
875 UPDATERC();
876 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg);
877 UPDATERC();
878 rc = CFGMR3InsertInteger(pCfg, "QueueSize", 64);
879 UPDATERC();
880 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1);
881 UPDATERC();
882 rc = CFGMR3InsertString(pLunL1, "Driver", "MainKeyboard");
883 UPDATERC();
884 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg);
885 UPDATERC();
886#endif
887#if 0
888 rc = CFGMR3InsertNode(pInst, "LUN#1", &pLunL0);
889 UPDATERC();
890 rc = CFGMR3InsertString(pLunL0, "Driver", "MouseQueue");
891 UPDATERC();
892 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg);
893 UPDATERC();
894 rc = CFGMR3InsertInteger(pCfg, "QueueSize", 128);
895 UPDATERC();
896 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1);
897 UPDATERC();
898 rc = CFGMR3InsertString(pLunL1, "Driver", "MainMouse");
899 UPDATERC();
900 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg);
901 UPDATERC();
902#endif
903
904 /*
905 * i8254 Programmable Interval Timer And Dummy Speaker
906 */
907 rc = CFGMR3InsertNode(pDevices, "i8254", &pDev);
908 UPDATERC();
909 rc = CFGMR3InsertNode(pDev, "0", &pInst);
910 UPDATERC();
911#ifdef DEBUG
912 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
913 UPDATERC();
914#endif
915 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
916 UPDATERC();
917
918 /*
919 * i8259 Programmable Interrupt Controller.
920 */
921 rc = CFGMR3InsertNode(pDevices, "i8259", &pDev);
922 UPDATERC();
923 rc = CFGMR3InsertNode(pDev, "0", &pInst);
924 UPDATERC();
925 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
926 UPDATERC();
927 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
928 UPDATERC();
929
930 /*
931 * RTC MC146818.
932 */
933 rc = CFGMR3InsertNode(pDevices, "mc146818", &pDev);
934 UPDATERC();
935 rc = CFGMR3InsertNode(pDev, "0", &pInst);
936 UPDATERC();
937 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
938 UPDATERC();
939
940 /*
941 * VGA.
942 */
943 rc = CFGMR3InsertNode(pDevices, "vga", &pDev);
944 UPDATERC();
945 rc = CFGMR3InsertNode(pDev, "0", &pInst);
946 UPDATERC();
947 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
948 UPDATERC();
949 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
950 UPDATERC();
951 rc = CFGMR3InsertInteger(pCfg, "VRamSize", 4 * _1M);
952 UPDATERC();
953
954 /* Bios logo. */
955 rc = CFGMR3InsertInteger(pCfg, "FadeIn", 1);
956 UPDATERC();
957 rc = CFGMR3InsertInteger(pCfg, "FadeOut", 1);
958 UPDATERC();
959 rc = CFGMR3InsertInteger(pCfg, "LogoTime", 0);
960 UPDATERC();
961 rc = CFGMR3InsertString(pCfg, "LogoFile", "");
962 UPDATERC();
963
964#if 0
965 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0);
966 UPDATERC();
967 rc = CFGMR3InsertString(pLunL0, "Driver", "MainDisplay");
968 UPDATERC();
969#endif
970
971 /*
972 * IDE controller.
973 */
974 rc = CFGMR3InsertNode(pDevices, "piix3ide", &pDev); /* piix3 */
975 UPDATERC();
976 rc = CFGMR3InsertNode(pDev, "0", &pInst);
977 UPDATERC();
978 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
979 UPDATERC();
980 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
981 UPDATERC();
982
983
984
985 /*
986 * ...
987 */
988
989#undef UPDATERC
990 return rcAll;
991}
992
993
994
995
996/**
997 * Resolves a path reference to a child node.
998 *
999 * @returns VBox status code.
1000 * @param pNode Which node to search for pszName in.
1001 * @param pszPath Path to the child node.
1002 * @param ppChild Where to store the pointer to the child node.
1003 */
1004static int cfgmR3ResolveNode(PCFGMNODE pNode, const char *pszPath, PCFGMNODE *ppChild)
1005{
1006 if (pNode)
1007 {
1008 PCFGMNODE pChild = NULL;
1009 for (;;)
1010 {
1011 /* skip leading slashes. */
1012 while (*pszPath == '/')
1013 pszPath++;
1014
1015 /* End of path? */
1016 if (!*pszPath)
1017 {
1018 if (!pChild)
1019 return VERR_CFGM_INVALID_CHILD_PATH;
1020 *ppChild = pChild;
1021 return VINF_SUCCESS;
1022 }
1023
1024 /* find end of component. */
1025 const char *pszNext = strchr(pszPath, '/');
1026 if (!pszNext)
1027 pszNext = strchr(pszPath, '\0');
1028 RTUINT cchName = pszNext - pszPath;
1029
1030 /* search child list. */
1031 pChild = pNode->pFirstChild;
1032 for ( ; pChild; pChild = pChild->pNext)
1033 if ( pChild->cchName == cchName
1034 && !memcmp(pszPath, pChild->szName, cchName) )
1035 break;
1036
1037 /* if not found, we're done. */
1038 if (!pChild)
1039 return VERR_CFGM_CHILD_NOT_FOUND;
1040
1041 /* next iteration */
1042 pNode = pChild;
1043 pszPath = pszNext;
1044 }
1045
1046 /* won't get here */
1047 }
1048 else
1049 return VERR_CFGM_NO_PARENT;
1050}
1051
1052
1053/**
1054 * Resolves a path reference to a child node.
1055 *
1056 * @returns VBox status code.
1057 * @param pNode Which node to search for pszName in.
1058 * @param pszName Name of a byte string value.
1059 * @param ppLeaf Where to store the pointer to the leaf node.
1060 */
1061static int cfgmR3ResolveLeaf(PCFGMNODE pNode, const char *pszName, PCFGMLEAF *ppLeaf)
1062{
1063 int rc;
1064 if (pNode)
1065 {
1066 RTUINT cchName = strlen(pszName);
1067 PCFGMLEAF pLeaf = pNode->pFirstLeaf;
1068 while (pLeaf)
1069 {
1070 if ( cchName == pLeaf->cchName
1071 && !memcmp(pszName, pLeaf->szName, cchName) )
1072 {
1073 *ppLeaf = pLeaf;
1074 return VINF_SUCCESS;
1075 }
1076
1077 /* next */
1078 pLeaf = pLeaf->pNext;
1079 }
1080 rc = VERR_CFGM_VALUE_NOT_FOUND;
1081 }
1082 else
1083 rc = VERR_CFGM_NO_PARENT;
1084 return rc;
1085}
1086
1087
1088
1089/**
1090 * Creates a CFGM tree.
1091 *
1092 * This is intended for creating device/driver configs can be
1093 * passed around and later attached to the main tree in the
1094 * correct location.
1095 *
1096 * @returns Pointer to the root node.
1097 * @param pVM The VM handle.
1098 */
1099CFGMR3DECL(PCFGMNODE) CFGMR3CreateTree(PVM pVM)
1100{
1101 PCFGMNODE pNew = (PCFGMNODE)MMR3HeapAlloc(pVM, MM_TAG_CFGM, sizeof(*pNew));
1102 if (pNew)
1103 {
1104 pNew->pPrev = NULL;
1105 pNew->pNext = NULL;
1106 pNew->pParent = NULL;
1107 pNew->pFirstChild = NULL;
1108 pNew->pFirstLeaf = NULL;
1109 pNew->pVM = pVM;
1110 pNew->fRestrictedRoot = false;
1111 pNew->cchName = 0;
1112 pNew->szName[0] = 0;
1113 }
1114 return pNew;
1115}
1116
1117
1118/**
1119 * Insert subtree.
1120 *
1121 * This function inserts (no duplication) a tree created by CFGMR3CreateTree()
1122 * into the main tree.
1123 *
1124 * The root node of the inserted subtree will need to be reallocated, which
1125 * effectually means that the passed in pSubTree handle becomes invalid
1126 * upon successful return. Use the value returned in ppChild instead
1127 * of pSubTree.
1128 *
1129 * @returns VBox status code.
1130 * @returns VERR_CFGM_NODE_EXISTS if the final child node name component exists.
1131 * @param pNode Parent node.
1132 * @param pszName Name or path of the new child node.
1133 * @param pSubTree The subtree to insert. Must be returned by CFGMR3CreateTree().
1134 * @param ppChild Where to store the address of the new child node. (optional)
1135 */
1136CFGMR3DECL(int) CFGMR3InsertSubTree(PCFGMNODE pNode, const char *pszName, PCFGMNODE pSubTree, PCFGMNODE *ppChild)
1137{
1138 /*
1139 * Validate input.
1140 */
1141 AssertPtrReturn(pSubTree, VERR_INVALID_POINTER);
1142 AssertReturn(!pSubTree->pParent, VERR_INVALID_PARAMETER);
1143 AssertReturn(pSubTree->pVM, VERR_INVALID_PARAMETER);
1144 AssertReturn(pSubTree->pParent != pSubTree->pVM->cfgm.s.pRoot, VERR_INVALID_PARAMETER);
1145 Assert(!pSubTree->pNext);
1146 Assert(!pSubTree->pPrev);
1147
1148 /*
1149 * Use CFGMR3InsertNode to create a new node and then
1150 * re-attach the children and leafs of the subtree to it.
1151 */
1152 PCFGMNODE pNewChild;
1153 int rc = CFGMR3InsertNode(pNode, pszName, &pNewChild);
1154 if (RT_SUCCESS(rc))
1155 {
1156 Assert(!pNewChild->pFirstChild);
1157 pNewChild->pFirstChild = pSubTree->pFirstChild;
1158 Assert(!pNewChild->pFirstLeaf);
1159 pNewChild->pFirstLeaf = pSubTree->pFirstLeaf;
1160 if (ppChild)
1161 *ppChild = pNewChild;
1162
1163 /* free the old subtree root */
1164 pSubTree->pVM = NULL;
1165 pSubTree->pFirstLeaf = NULL;
1166 pSubTree->pFirstChild = NULL;
1167 MMR3HeapFree(pSubTree);
1168 }
1169 return rc;
1170}
1171
1172
1173/**
1174 * Insert a node.
1175 *
1176 * @returns VBox status code.
1177 * @returns VERR_CFGM_NODE_EXISTS if the final child node name component exists.
1178 * @param pNode Parent node.
1179 * @param pszName Name or path of the new child node.
1180 * @param ppChild Where to store the address of the new child node. (optional)
1181 */
1182CFGMR3DECL(int) CFGMR3InsertNode(PCFGMNODE pNode, const char *pszName, PCFGMNODE *ppChild)
1183{
1184 int rc;
1185 if (pNode)
1186 {
1187 /*
1188 * If given a path we have to deal with it component by compontent.
1189 */
1190 while (*pszName == '/')
1191 pszName++;
1192 if (strchr(pszName, '/'))
1193 {
1194 char *pszDup = RTStrDup(pszName);
1195 if (pszDup)
1196 {
1197 char *psz = pszDup;
1198 for (;;)
1199 {
1200 /* Terminate at '/' and find the next component. */
1201 char *pszNext = strchr(psz, '/');
1202 if (pszNext)
1203 {
1204 *pszNext++ = '\0';
1205 while (*pszNext == '/')
1206 pszNext++;
1207 if (*pszNext == '\0')
1208 pszNext = NULL;
1209 }
1210
1211 /* does it exist? */
1212 PCFGMNODE pChild = CFGMR3GetChild(pNode, psz);
1213 if (!pChild)
1214 {
1215 /* no, insert it */
1216 rc = CFGMR3InsertNode(pNode, psz, &pChild);
1217 if (VBOX_FAILURE(rc))
1218 break;
1219 if (!pszNext)
1220 {
1221 if (ppChild)
1222 *ppChild = pChild;
1223 break;
1224 }
1225
1226 }
1227 /* if last component fail */
1228 else if (!pszNext)
1229 {
1230 rc = VERR_CFGM_NODE_EXISTS;
1231 break;
1232 }
1233
1234 /* next */
1235 pNode = pChild;
1236 psz = pszNext;
1237 }
1238 RTStrFree(pszDup);
1239 }
1240 else
1241 rc = VERR_NO_TMP_MEMORY;
1242 }
1243 /*
1244 * Not multicomponent, just make sure it's a non-zero name.
1245 */
1246 else if (*pszName)
1247 {
1248 /*
1249 * Check if already exists and find last node in chain.
1250 */
1251 size_t cchName = strlen(pszName);
1252 PCFGMNODE pPrev = pNode->pFirstChild;
1253 if (pPrev)
1254 {
1255 for (;; pPrev = pPrev->pNext)
1256 {
1257 if ( cchName == pPrev->cchName
1258 && !memcmp(pszName, pPrev->szName, cchName))
1259 return VERR_CFGM_NODE_EXISTS;
1260 if (!pPrev->pNext)
1261 break;
1262 }
1263 }
1264
1265 /*
1266 * Allocate and init node.
1267 */
1268 PCFGMNODE pNew = (PCFGMNODE)MMR3HeapAlloc(pNode->pVM, MM_TAG_CFGM, sizeof(*pNew) + cchName);
1269 if (pNew)
1270 {
1271 pNew->pParent = pNode;
1272 pNew->pFirstChild = NULL;
1273 pNew->pFirstLeaf = NULL;
1274 pNew->pVM = pNode->pVM;
1275 pNew->fRestrictedRoot = false;
1276 pNew->cchName = cchName;
1277 memcpy(pNew->szName, pszName, cchName + 1);
1278
1279 /*
1280 * Insert into child list.
1281 */
1282 pNew->pNext = NULL;
1283 pNew->pPrev = pPrev;
1284 if (pPrev)
1285 pPrev->pNext = pNew;
1286 else
1287 pNode->pFirstChild = pNew;
1288 if (ppChild)
1289 *ppChild = pNew;
1290 rc = VINF_SUCCESS;
1291 }
1292 else
1293 rc = VERR_NO_MEMORY;
1294 }
1295 else
1296 {
1297 rc = VERR_CFGM_INVALID_NODE_PATH;
1298 AssertMsgFailed(("Invalid path %s\n", pszName));
1299 }
1300 }
1301 else
1302 {
1303 rc = VERR_CFGM_NO_PARENT;
1304 AssertMsgFailed(("No parent! path %s\n", pszName));
1305 }
1306
1307 return rc;
1308}
1309
1310
1311/**
1312 * Insert a node, format string name.
1313 *
1314 * @returns VBox status code.
1315 * @param pNode Parent node.
1316 * @param ppChild Where to store the address of the new child node. (optional)
1317 * @param pszNameFormat Name of or path the new child node.
1318 * @param ... Name format arguments.
1319 */
1320CFGMR3DECL(int) CFGMR3InsertNodeF(PCFGMNODE pNode, PCFGMNODE *ppChild, const char *pszNameFormat, ...)
1321{
1322 va_list Args;
1323 va_start(Args, pszNameFormat);
1324 int rc = CFGMR3InsertNodeFV(pNode, ppChild, pszNameFormat, Args);
1325 va_end(Args);
1326 return rc;
1327}
1328
1329
1330/**
1331 * Insert a node, format string name.
1332 *
1333 * @returns VBox status code.
1334 * @param pNode Parent node.
1335 * @param ppChild Where to store the address of the new child node. (optional)
1336 * @param pszNameFormat Name or path of the new child node.
1337 * @param Args Name format arguments.
1338 */
1339CFGMR3DECL(int) CFGMR3InsertNodeFV(PCFGMNODE pNode, PCFGMNODE *ppChild, const char *pszNameFormat, va_list Args)
1340{
1341 int rc;
1342 char *pszName;
1343 RTStrAPrintfV(&pszName, pszNameFormat, Args);
1344 if (pszName)
1345 {
1346 rc = CFGMR3InsertNode(pNode, pszName, ppChild);
1347 RTStrFree(pszName);
1348 }
1349 else
1350 rc = VERR_NO_MEMORY;
1351 return rc;
1352}
1353
1354
1355/**
1356 * Marks the node as the root of a restricted subtree, i.e. the end of
1357 * a CFGMR3GetParent() journey.
1358 *
1359 * @param pNode The node to mark.
1360 */
1361CFGMR3DECL(void) CFGMR3SetRestrictedRoot(PCFGMNODE pNode)
1362{
1363 if (pNode)
1364 pNode->fRestrictedRoot = true;
1365}
1366
1367
1368/**
1369 * Insert a node.
1370 *
1371 * @returns VBox status code.
1372 * @param pNode Parent node.
1373 * @param pszName Name of the new child node.
1374 * @param ppLeaf Where to store the new leaf.
1375 * The caller must fill in the enmType and Value fields!
1376 */
1377static int cfgmR3InsertLeaf(PCFGMNODE pNode, const char *pszName, PCFGMLEAF *ppLeaf)
1378{
1379 int rc;
1380 if (*pszName)
1381 {
1382 if (pNode)
1383 {
1384 /*
1385 * Check if already exists and find last node in chain.
1386 */
1387 size_t cchName = strlen(pszName);
1388 PCFGMLEAF pPrev = pNode->pFirstLeaf;
1389 if (pPrev)
1390 {
1391 for (;; pPrev = pPrev->pNext)
1392 {
1393 if ( cchName == pPrev->cchName
1394 && !memcmp(pszName, pPrev->szName, cchName))
1395 return VERR_CFGM_LEAF_EXISTS;
1396 if (!pPrev->pNext)
1397 break;
1398 }
1399 }
1400
1401 /*
1402 * Allocate and init node.
1403 */
1404 PCFGMLEAF pNew = (PCFGMLEAF)MMR3HeapAlloc(pNode->pVM, MM_TAG_CFGM, sizeof(*pNew) + cchName);
1405 if (pNew)
1406 {
1407 pNew->cchName = cchName;
1408 memcpy(pNew->szName, pszName, cchName + 1);
1409
1410 /*
1411 * Insert into child list.
1412 */
1413 pNew->pNext = NULL;
1414 pNew->pPrev = pPrev;
1415 if (pPrev)
1416 pPrev->pNext = pNew;
1417 else
1418 pNode->pFirstLeaf = pNew;
1419 *ppLeaf = pNew;
1420 rc = VINF_SUCCESS;
1421 }
1422 else
1423 rc = VERR_NO_MEMORY;
1424 }
1425 else
1426 rc = VERR_CFGM_NO_PARENT;
1427 }
1428 else
1429 rc = VERR_CFGM_INVALID_CHILD_PATH;
1430 return rc;
1431}
1432
1433
1434/**
1435 * Remove a node.
1436 *
1437 * @param pNode Parent node.
1438 */
1439CFGMR3DECL(void) CFGMR3RemoveNode(PCFGMNODE pNode)
1440{
1441 if (pNode)
1442 {
1443 /*
1444 * Free children.
1445 */
1446 while (pNode->pFirstChild)
1447 CFGMR3RemoveNode(pNode->pFirstChild);
1448
1449 /*
1450 * Free leafs.
1451 */
1452 while (pNode->pFirstLeaf)
1453 cfgmR3RemoveLeaf(pNode, pNode->pFirstLeaf);
1454
1455 /*
1456 * Unlink ourselves.
1457 */
1458 if (pNode->pPrev)
1459 pNode->pPrev->pNext = pNode->pNext;
1460 else
1461 {
1462 if (pNode->pParent)
1463 pNode->pParent->pFirstChild = pNode->pNext;
1464 else if (pNode == pNode->pVM->cfgm.s.pRoot) /* might be a different tree */
1465 pNode->pVM->cfgm.s.pRoot = NULL;
1466 }
1467 if (pNode->pNext)
1468 pNode->pNext->pPrev = pNode->pPrev;
1469
1470 /*
1471 * Free ourselves. (bit of paranoia first)
1472 */
1473 pNode->pVM = NULL;
1474 pNode->pNext = NULL;
1475 pNode->pPrev = NULL;
1476 pNode->pParent = NULL;
1477 MMR3HeapFree(pNode);
1478 }
1479}
1480
1481
1482/**
1483 * Removes a leaf.
1484 *
1485 * @param pNode Parent node.
1486 * @param pLeaf Leaf to remove.
1487 */
1488static void cfgmR3RemoveLeaf(PCFGMNODE pNode, PCFGMLEAF pLeaf)
1489{
1490 if (pNode && pLeaf)
1491 {
1492 /*
1493 * Unlink.
1494 */
1495 if (pLeaf->pPrev)
1496 pLeaf->pPrev->pNext = pLeaf->pNext;
1497 else
1498 pNode->pFirstLeaf = pLeaf->pNext;
1499 if (pLeaf->pNext)
1500 pLeaf->pNext->pPrev = pLeaf->pPrev;
1501
1502 /*
1503 * Free value and node.
1504 */
1505 cfgmR3FreeValue(pLeaf);
1506 pLeaf->pNext = NULL;
1507 pLeaf->pPrev = NULL;
1508 MMR3HeapFree(pLeaf);
1509 }
1510}
1511
1512
1513/**
1514 * Frees whatever resources the leaf value is owning.
1515 *
1516 * Use this before assigning a new value to a leaf.
1517 * The caller must either free the leaf or assign a new value to it.
1518 *
1519 * @param pLeaf Pointer to the leaf which value should be free.
1520 */
1521static void cfgmR3FreeValue(PCFGMLEAF pLeaf)
1522{
1523 if (pLeaf)
1524 {
1525 switch (pLeaf->enmType)
1526 {
1527 case CFGMVALUETYPE_BYTES:
1528 MMR3HeapFree(pLeaf->Value.Bytes.pau8);
1529 pLeaf->Value.Bytes.pau8 = NULL;
1530 pLeaf->Value.Bytes.cb = 0;
1531 break;
1532
1533 case CFGMVALUETYPE_STRING:
1534 MMR3HeapFree(pLeaf->Value.String.psz);
1535 pLeaf->Value.String.psz = NULL;
1536 pLeaf->Value.String.cch = 0;
1537 break;
1538
1539 case CFGMVALUETYPE_INTEGER:
1540 break;
1541 }
1542 pLeaf->enmType = (CFGMVALUETYPE)0;
1543 }
1544}
1545
1546
1547/**
1548 * Inserts a new integer value.
1549 *
1550 * @returns VBox status code.
1551 * @param pNode Parent node.
1552 * @param pszName Value name.
1553 * @param u64Integer The value.
1554 */
1555CFGMR3DECL(int) CFGMR3InsertInteger(PCFGMNODE pNode, const char *pszName, uint64_t u64Integer)
1556{
1557 PCFGMLEAF pLeaf;
1558 int rc = cfgmR3InsertLeaf(pNode, pszName, &pLeaf);
1559 if (VBOX_SUCCESS(rc))
1560 {
1561 pLeaf->enmType = CFGMVALUETYPE_INTEGER;
1562 pLeaf->Value.Integer.u64 = u64Integer;
1563 }
1564 return rc;
1565}
1566
1567
1568/**
1569 * Inserts a new string value.
1570 *
1571 * @returns VBox status code.
1572 * @param pNode Parent node.
1573 * @param pszName Value name.
1574 * @param pszString The value.
1575 */
1576CFGMR3DECL(int) CFGMR3InsertString(PCFGMNODE pNode, const char *pszName, const char *pszString)
1577{
1578 int rc;
1579 if (pNode)
1580 {
1581 /*
1582 * Allocate string object first.
1583 */
1584 size_t cchString = strlen(pszString) + 1;
1585 char *pszStringCopy = (char *)MMR3HeapAlloc(pNode->pVM, MM_TAG_CFGM_STRING, RT_ALIGN_Z(cchString, 16));
1586 if (pszStringCopy)
1587 {
1588 memcpy(pszStringCopy, pszString, cchString);
1589
1590 /*
1591 * Create value leaf and set it to string type.
1592 */
1593 PCFGMLEAF pLeaf;
1594 rc = cfgmR3InsertLeaf(pNode, pszName, &pLeaf);
1595 if (VBOX_SUCCESS(rc))
1596 {
1597 pLeaf->enmType = CFGMVALUETYPE_STRING;
1598 pLeaf->Value.String.psz = pszStringCopy;
1599 pLeaf->Value.String.cch = cchString;
1600 }
1601 }
1602 else
1603 rc = VERR_NO_MEMORY;
1604 }
1605 else
1606 rc = VERR_CFGM_NO_PARENT;
1607
1608 return rc;
1609}
1610
1611
1612
1613/**
1614 * Inserts a new integer value.
1615 *
1616 * @returns VBox status code.
1617 * @param pNode Parent node.
1618 * @param pszName Value name.
1619 * @param pvBytes The value.
1620 * @param cbBytes The value size.
1621 */
1622CFGMR3DECL(int) CFGMR3InsertBytes(PCFGMNODE pNode, const char *pszName, const void *pvBytes, size_t cbBytes)
1623{
1624 int rc;
1625 if (pNode)
1626 {
1627 if (cbBytes == (RTUINT)cbBytes)
1628 {
1629 /*
1630 * Allocate string object first.
1631 */
1632 void *pvCopy = MMR3HeapAlloc(pNode->pVM, MM_TAG_CFGM_STRING, RT_ALIGN_Z(cbBytes, 16));
1633 if (pvCopy || !cbBytes)
1634 {
1635 memcpy(pvCopy, pvBytes, cbBytes);
1636
1637 /*
1638 * Create value leaf and set it to string type.
1639 */
1640 PCFGMLEAF pLeaf;
1641 rc = cfgmR3InsertLeaf(pNode, pszName, &pLeaf);
1642 if (VBOX_SUCCESS(rc))
1643 {
1644 pLeaf->enmType = CFGMVALUETYPE_BYTES;
1645 pLeaf->Value.Bytes.cb = cbBytes;
1646 pLeaf->Value.Bytes.pau8 = (uint8_t *)pvCopy;
1647 }
1648 }
1649 else
1650 rc = VERR_NO_MEMORY;
1651 }
1652 else
1653 rc = VERR_OUT_OF_RANGE;
1654 }
1655 else
1656 rc = VERR_CFGM_NO_PARENT;
1657
1658 return rc;
1659}
1660
1661
1662/**
1663 * Remove a value.
1664 *
1665 * @returns VBox status code.
1666 * @param pNode Parent node.
1667 * @param pszName Name of the new child node.
1668 */
1669CFGMR3DECL(int) CFGMR3RemoveValue(PCFGMNODE pNode, const char *pszName)
1670{
1671 PCFGMLEAF pLeaf;
1672 int rc = cfgmR3ResolveLeaf(pNode, pszName, &pLeaf);
1673 if (VBOX_SUCCESS(rc))
1674 cfgmR3RemoveLeaf(pNode, pLeaf);
1675 return rc;
1676}
1677
1678
1679
1680/*
1681 * -+- helper apis -+-
1682 */
1683
1684
1685/**
1686 * Query unsigned 64-bit integer value.
1687 *
1688 * @returns VBox status code.
1689 * @param pNode Which node to search for pszName in.
1690 * @param pszName Name of an integer value.
1691 * @param pu64 Where to store the integer value.
1692 */
1693CFGMR3DECL(int) CFGMR3QueryU64(PCFGMNODE pNode, const char *pszName, uint64_t *pu64)
1694{
1695 return CFGMR3QueryInteger(pNode, pszName, pu64);
1696}
1697
1698
1699/**
1700 * Query unsigned 64-bit integer value with default.
1701 *
1702 * @returns VBox status code.
1703 * @param pNode Which node to search for pszName in.
1704 * @param pszName Name of an integer value.
1705 * @param pu64 Where to store the integer value.
1706 * @param u64Def The default value.
1707 */
1708CFGMR3DECL(int) CFGMR3QueryU64Def(PCFGMNODE pNode, const char *pszName, uint64_t *pu64, uint64_t u64Def)
1709{
1710 return CFGMR3QueryIntegerDef(pNode, pszName, pu64, u64Def);
1711}
1712
1713
1714/**
1715 * Query signed 64-bit integer value.
1716 *
1717 * @returns VBox status code.
1718 * @param pNode Which node to search for pszName in.
1719 * @param pszName Name of an integer value.
1720 * @param pi64 Where to store the value.
1721 */
1722CFGMR3DECL(int) CFGMR3QueryS64(PCFGMNODE pNode, const char *pszName, int64_t *pi64)
1723{
1724 uint64_t u64;
1725 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
1726 if (VBOX_SUCCESS(rc))
1727 *pi64 = (int64_t)u64;
1728 return rc;
1729}
1730
1731
1732/**
1733 * Query signed 64-bit integer value with default.
1734 *
1735 * @returns VBox status code.
1736 * @param pNode Which node to search for pszName in.
1737 * @param pszName Name of an integer value.
1738 * @param pi64 Where to store the value.
1739 * @param i64Def The default value.
1740 */
1741CFGMR3DECL(int) CFGMR3QueryS64Def(PCFGMNODE pNode, const char *pszName, int64_t *pi64, int64_t i64Def)
1742{
1743 uint64_t u64;
1744 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, i64Def);
1745 if (VBOX_SUCCESS(rc))
1746 *pi64 = (int64_t)u64;
1747 return rc;
1748}
1749
1750
1751/**
1752 * Query unsigned 32-bit integer value.
1753 *
1754 * @returns VBox status code.
1755 * @param pNode Which node to search for pszName in.
1756 * @param pszName Name of an integer value.
1757 * @param pu32 Where to store the value.
1758 */
1759CFGMR3DECL(int) CFGMR3QueryU32(PCFGMNODE pNode, const char *pszName, uint32_t *pu32)
1760{
1761 uint64_t u64;
1762 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
1763 if (VBOX_SUCCESS(rc))
1764 {
1765 if (!(u64 & UINT64_C(0xffffffff00000000)))
1766 *pu32 = (uint32_t)u64;
1767 else
1768 rc = VERR_CFGM_INTEGER_TOO_BIG;
1769 }
1770 return rc;
1771}
1772
1773
1774/**
1775 * Query unsigned 32-bit integer value with default.
1776 *
1777 * @returns VBox status code.
1778 * @param pNode Which node to search for pszName in.
1779 * @param pszName Name of an integer value.
1780 * @param pu32 Where to store the value.
1781 * @param u32Def The default value.
1782 */
1783CFGMR3DECL(int) CFGMR3QueryU32Def(PCFGMNODE pNode, const char *pszName, uint32_t *pu32, uint32_t u32Def)
1784{
1785 uint64_t u64;
1786 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, u32Def);
1787 if (VBOX_SUCCESS(rc))
1788 {
1789 if (!(u64 & UINT64_C(0xffffffff00000000)))
1790 *pu32 = (uint32_t)u64;
1791 else
1792 rc = VERR_CFGM_INTEGER_TOO_BIG;
1793 }
1794 return rc;
1795}
1796
1797
1798/**
1799 * Query signed 32-bit integer value.
1800 *
1801 * @returns VBox status code.
1802 * @param pNode Which node to search for pszName in.
1803 * @param pszName Name of an integer value.
1804 * @param pi32 Where to store the value.
1805 */
1806CFGMR3DECL(int) CFGMR3QueryS32(PCFGMNODE pNode, const char *pszName, int32_t *pi32)
1807{
1808 uint64_t u64;
1809 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
1810 if (VBOX_SUCCESS(rc))
1811 {
1812 if ( !(u64 & UINT64_C(0xffffffff80000000))
1813 || (u64 & UINT64_C(0xffffffff80000000)) == UINT64_C(0xffffffff80000000))
1814 *pi32 = (int32_t)u64;
1815 else
1816 rc = VERR_CFGM_INTEGER_TOO_BIG;
1817 }
1818 return rc;
1819}
1820
1821
1822/**
1823 * Query signed 32-bit integer value with default.
1824 *
1825 * @returns VBox status code.
1826 * @param pNode Which node to search for pszName in.
1827 * @param pszName Name of an integer value.
1828 * @param pi32 Where to store the value.
1829 * @param i32Def The default value.
1830 */
1831CFGMR3DECL(int) CFGMR3QueryS32Def(PCFGMNODE pNode, const char *pszName, int32_t *pi32, int32_t i32Def)
1832{
1833 uint64_t u64;
1834 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, i32Def);
1835 if (VBOX_SUCCESS(rc))
1836 {
1837 if ( !(u64 & UINT64_C(0xffffffff80000000))
1838 || (u64 & UINT64_C(0xffffffff80000000)) == UINT64_C(0xffffffff80000000))
1839 *pi32 = (int32_t)u64;
1840 else
1841 rc = VERR_CFGM_INTEGER_TOO_BIG;
1842 }
1843 return rc;
1844}
1845
1846
1847/**
1848 * Query unsigned 16-bit integer value.
1849 *
1850 * @returns VBox status code.
1851 * @param pNode Which node to search for pszName in.
1852 * @param pszName Name of an integer value.
1853 * @param pu16 Where to store the value.
1854 */
1855CFGMR3DECL(int) CFGMR3QueryU16(PCFGMNODE pNode, const char *pszName, uint16_t *pu16)
1856{
1857 uint64_t u64;
1858 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
1859 if (VBOX_SUCCESS(rc))
1860 {
1861 if (!(u64 & UINT64_C(0xffffffffffff0000)))
1862 *pu16 = (int16_t)u64;
1863 else
1864 rc = VERR_CFGM_INTEGER_TOO_BIG;
1865 }
1866 return rc;
1867}
1868
1869
1870/**
1871 * Query unsigned 16-bit integer value with default.
1872 *
1873 * @returns VBox status code.
1874 * @param pNode Which node to search for pszName in.
1875 * @param pszName Name of an integer value.
1876 * @param pu16 Where to store the value.
1877 * @param i16Def The default value.
1878 */
1879CFGMR3DECL(int) CFGMR3QueryU16Def(PCFGMNODE pNode, const char *pszName, uint16_t *pu16, uint16_t u16Def)
1880{
1881 uint64_t u64;
1882 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, u16Def);
1883 if (VBOX_SUCCESS(rc))
1884 {
1885 if (!(u64 & UINT64_C(0xffffffffffff0000)))
1886 *pu16 = (int16_t)u64;
1887 else
1888 rc = VERR_CFGM_INTEGER_TOO_BIG;
1889 }
1890 return rc;
1891}
1892
1893
1894/**
1895 * Query signed 16-bit integer value.
1896 *
1897 * @returns VBox status code.
1898 * @param pNode Which node to search for pszName in.
1899 * @param pszName Name of an integer value.
1900 * @param pi16 Where to store the value.
1901 */
1902CFGMR3DECL(int) CFGMR3QueryS16(PCFGMNODE pNode, const char *pszName, int16_t *pi16)
1903{
1904 uint64_t u64;
1905 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
1906 if (VBOX_SUCCESS(rc))
1907 {
1908 if ( !(u64 & UINT64_C(0xffffffffffff8000))
1909 || (u64 & UINT64_C(0xffffffffffff8000)) == UINT64_C(0xffffffffffff8000))
1910 *pi16 = (int16_t)u64;
1911 else
1912 rc = VERR_CFGM_INTEGER_TOO_BIG;
1913 }
1914 return rc;
1915}
1916
1917
1918/**
1919 * Query signed 16-bit integer value with default.
1920 *
1921 * @returns VBox status code.
1922 * @param pNode Which node to search for pszName in.
1923 * @param pszName Name of an integer value.
1924 * @param pi16 Where to store the value.
1925 * @param i16Def The default value.
1926 */
1927CFGMR3DECL(int) CFGMR3QueryS16Def(PCFGMNODE pNode, const char *pszName, int16_t *pi16, int16_t i16Def)
1928{
1929 uint64_t u64;
1930 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, i16Def);
1931 if (VBOX_SUCCESS(rc))
1932 {
1933 if ( !(u64 & UINT64_C(0xffffffffffff8000))
1934 || (u64 & UINT64_C(0xffffffffffff8000)) == UINT64_C(0xffffffffffff8000))
1935 *pi16 = (int16_t)u64;
1936 else
1937 rc = VERR_CFGM_INTEGER_TOO_BIG;
1938 }
1939 return rc;
1940}
1941
1942
1943/**
1944 * Query unsigned 8-bit integer value.
1945 *
1946 * @returns VBox status code.
1947 * @param pNode Which node to search for pszName in.
1948 * @param pszName Name of an integer value.
1949 * @param pu8 Where to store the value.
1950 */
1951CFGMR3DECL(int) CFGMR3QueryU8(PCFGMNODE pNode, const char *pszName, uint8_t *pu8)
1952{
1953 uint64_t u64;
1954 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
1955 if (VBOX_SUCCESS(rc))
1956 {
1957 if (!(u64 & UINT64_C(0xffffffffffffff00)))
1958 *pu8 = (uint8_t)u64;
1959 else
1960 rc = VERR_CFGM_INTEGER_TOO_BIG;
1961 }
1962 return rc;
1963}
1964
1965
1966/**
1967 * Query unsigned 8-bit integer value with default.
1968 *
1969 * @returns VBox status code.
1970 * @param pNode Which node to search for pszName in.
1971 * @param pszName Name of an integer value.
1972 * @param pu8 Where to store the value.
1973 * @param u8Def The default value.
1974 */
1975CFGMR3DECL(int) CFGMR3QueryU8Def(PCFGMNODE pNode, const char *pszName, uint8_t *pu8, uint8_t u8Def)
1976{
1977 uint64_t u64;
1978 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, u8Def);
1979 if (VBOX_SUCCESS(rc))
1980 {
1981 if (!(u64 & UINT64_C(0xffffffffffffff00)))
1982 *pu8 = (uint8_t)u64;
1983 else
1984 rc = VERR_CFGM_INTEGER_TOO_BIG;
1985 }
1986 return rc;
1987}
1988
1989
1990/**
1991 * Query signed 8-bit integer value.
1992 *
1993 * @returns VBox status code.
1994 * @param pNode Which node to search for pszName in.
1995 * @param pszName Name of an integer value.
1996 * @param pi8 Where to store the value.
1997 */
1998CFGMR3DECL(int) CFGMR3QueryS8(PCFGMNODE pNode, const char *pszName, int8_t *pi8)
1999{
2000 uint64_t u64;
2001 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
2002 if (VBOX_SUCCESS(rc))
2003 {
2004 if ( !(u64 & UINT64_C(0xffffffffffffff80))
2005 || (u64 & UINT64_C(0xffffffffffffff80)) == UINT64_C(0xffffffffffffff80))
2006 *pi8 = (int8_t)u64;
2007 else
2008 rc = VERR_CFGM_INTEGER_TOO_BIG;
2009 }
2010 return rc;
2011}
2012
2013
2014/**
2015 * Query signed 8-bit integer value with default.
2016 *
2017 * @returns VBox status code.
2018 * @param pNode Which node to search for pszName in.
2019 * @param pszName Name of an integer value.
2020 * @param pi8 Where to store the value.
2021 * @param i8Def The default value.
2022 */
2023CFGMR3DECL(int) CFGMR3QueryS8Def(PCFGMNODE pNode, const char *pszName, int8_t *pi8, int8_t i8Def)
2024{
2025 uint64_t u64;
2026 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, i8Def);
2027 if (VBOX_SUCCESS(rc))
2028 {
2029 if ( !(u64 & UINT64_C(0xffffffffffffff80))
2030 || (u64 & UINT64_C(0xffffffffffffff80)) == UINT64_C(0xffffffffffffff80))
2031 *pi8 = (int8_t)u64;
2032 else
2033 rc = VERR_CFGM_INTEGER_TOO_BIG;
2034 }
2035 return rc;
2036}
2037
2038
2039/**
2040 * Query boolean integer value.
2041 *
2042 * @returns VBox status code.
2043 * @param pNode Which node to search for pszName in.
2044 * @param pszName Name of an integer value.
2045 * @param pf Where to store the value.
2046 * @remark This function will interpret any non-zero value as true.
2047 */
2048CFGMR3DECL(int) CFGMR3QueryBool(PCFGMNODE pNode, const char *pszName, bool *pf)
2049{
2050 uint64_t u64;
2051 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
2052 if (VBOX_SUCCESS(rc))
2053 *pf = u64 ? true : false;
2054 return rc;
2055}
2056
2057
2058/**
2059 * Query boolean integer value with default.
2060 *
2061 * @returns VBox status code.
2062 * @param pNode Which node to search for pszName in.
2063 * @param pszName Name of an integer value.
2064 * @param pf Where to store the value.
2065 * @param fDef The default value.
2066 * @remark This function will interpret any non-zero value as true.
2067 */
2068CFGMR3DECL(int) CFGMR3QueryBoolDef(PCFGMNODE pNode, const char *pszName, bool *pf, bool fDef)
2069{
2070 uint64_t u64;
2071 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, fDef);
2072 if (VBOX_SUCCESS(rc))
2073 *pf = u64 ? true : false;
2074 return rc;
2075}
2076
2077
2078/**
2079 * Query I/O port address value.
2080 *
2081 * @returns VBox status code.
2082 * @param pNode Which node to search for pszName in.
2083 * @param pszName Name of an integer value.
2084 * @param pPort Where to store the value.
2085 */
2086CFGMR3DECL(int) CFGMR3QueryPort(PCFGMNODE pNode, const char *pszName, PRTIOPORT pPort)
2087{
2088 AssertCompileSize(RTIOPORT, 2);
2089 return CFGMR3QueryU16(pNode, pszName, pPort);
2090}
2091
2092
2093/**
2094 * Query I/O port address value with default.
2095 *
2096 * @returns VBox status code.
2097 * @param pNode Which node to search for pszName in.
2098 * @param pszName Name of an integer value.
2099 * @param pPort Where to store the value.
2100 * @param PortDef The default value.
2101 */
2102CFGMR3DECL(int) CFGMR3QueryPortDef(PCFGMNODE pNode, const char *pszName, PRTIOPORT pPort, RTIOPORT PortDef)
2103{
2104 AssertCompileSize(RTIOPORT, 2);
2105 return CFGMR3QueryU16Def(pNode, pszName, pPort, PortDef);
2106}
2107
2108
2109/**
2110 * Query unsigned int address value.
2111 *
2112 * @returns VBox status code.
2113 * @param pNode Which node to search for pszName in.
2114 * @param pszName Name of an integer value.
2115 * @param pu Where to store the value.
2116 */
2117CFGMR3DECL(int) CFGMR3QueryUInt(PCFGMNODE pNode, const char *pszName, unsigned int *pu)
2118{
2119 AssertCompileSize(unsigned int, 4);
2120 return CFGMR3QueryU32(pNode, pszName, (uint32_t *)pu);
2121}
2122
2123
2124/**
2125 * Query unsigned int address value with default.
2126 *
2127 * @returns VBox status code.
2128 * @param pNode Which node to search for pszName in.
2129 * @param pszName Name of an integer value.
2130 * @param pu Where to store the value.
2131 * @param uDef The default value.
2132 */
2133CFGMR3DECL(int) CFGMR3QueryUIntDef(PCFGMNODE pNode, const char *pszName, unsigned int *pu, unsigned int uDef)
2134{
2135 AssertCompileSize(unsigned int, 4);
2136 return CFGMR3QueryU32Def(pNode, pszName, (uint32_t *)pu, uDef);
2137}
2138
2139
2140/**
2141 * Query signed int address value.
2142 *
2143 * @returns VBox status code.
2144 * @param pNode Which node to search for pszName in.
2145 * @param pszName Name of an integer value.
2146 * @param pi Where to store the value.
2147 */
2148CFGMR3DECL(int) CFGMR3QuerySInt(PCFGMNODE pNode, const char *pszName, signed int *pi)
2149{
2150 AssertCompileSize(signed int, 4);
2151 return CFGMR3QueryS32(pNode, pszName, (int32_t *)pi);
2152}
2153
2154
2155/**
2156 * Query unsigned int address value with default.
2157 *
2158 * @returns VBox status code.
2159 * @param pNode Which node to search for pszName in.
2160 * @param pszName Name of an integer value.
2161 * @param pi Where to store the value.
2162 * @param iDef The default value.
2163 */
2164CFGMR3DECL(int) CFGMR3QuerySIntDef(PCFGMNODE pNode, const char *pszName, signed int *pi, signed int iDef)
2165{
2166 AssertCompileSize(signed int, 4);
2167 return CFGMR3QueryS32Def(pNode, pszName, (int32_t *)pi, iDef);
2168}
2169
2170
2171/**
2172 * Query pointer integer value.
2173 *
2174 * @returns VBox status code.
2175 * @param pNode Which node to search for pszName in.
2176 * @param pszName Name of an integer value.
2177 * @param ppv Where to store the value.
2178 */
2179CFGMR3DECL(int) CFGMR3QueryPtr(PCFGMNODE pNode, const char *pszName, void **ppv)
2180{
2181 uint64_t u64;
2182 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
2183 if (VBOX_SUCCESS(rc))
2184 {
2185 uintptr_t u = (uintptr_t)u64;
2186 if (u64 == u)
2187 *ppv = (void *)u;
2188 else
2189 rc = VERR_CFGM_INTEGER_TOO_BIG;
2190 }
2191 return rc;
2192}
2193
2194
2195/**
2196 * Query pointer integer value with default.
2197 *
2198 * @returns VBox status code.
2199 * @param pNode Which node to search for pszName in.
2200 * @param pszName Name of an integer value.
2201 * @param ppv Where to store the value.
2202 * @param pvDef The default value.
2203 */
2204CFGMR3DECL(int) CFGMR3QueryPtrDef(PCFGMNODE pNode, const char *pszName, void **ppv, void *pvDef)
2205{
2206 uint64_t u64;
2207 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, (uintptr_t)pvDef);
2208 if (VBOX_SUCCESS(rc))
2209 {
2210 uintptr_t u = (uintptr_t)u64;
2211 if (u64 == u)
2212 *ppv = (void *)u;
2213 else
2214 rc = VERR_CFGM_INTEGER_TOO_BIG;
2215 }
2216 return rc;
2217}
2218
2219
2220/**
2221 * Query Guest Context pointer integer value.
2222 *
2223 * @returns VBox status code.
2224 * @param pNode Which node to search for pszName in.
2225 * @param pszName Name of an integer value.
2226 * @param pGCPtr Where to store the value.
2227 */
2228CFGMR3DECL(int) CFGMR3QueryGCPtr(PCFGMNODE pNode, const char *pszName, PRTGCPTR pGCPtr)
2229{
2230 uint64_t u64;
2231 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
2232 if (VBOX_SUCCESS(rc))
2233 {
2234 RTGCPTR u = (RTGCPTR)u64;
2235 if (u64 == u)
2236 *pGCPtr = u;
2237 else
2238 rc = VERR_CFGM_INTEGER_TOO_BIG;
2239 }
2240 return rc;
2241}
2242
2243
2244/**
2245 * Query Guest Context pointer integer value with default.
2246 *
2247 * @returns VBox status code.
2248 * @param pNode Which node to search for pszName in.
2249 * @param pszName Name of an integer value.
2250 * @param pGCPtr Where to store the value.
2251 * @param GCPtrDef The default value.
2252 */
2253CFGMR3DECL(int) CFGMR3QueryGCPtrDef(PCFGMNODE pNode, const char *pszName, PRTGCPTR pGCPtr, RTGCPTR GCPtrDef)
2254{
2255 uint64_t u64;
2256 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, GCPtrDef);
2257 if (VBOX_SUCCESS(rc))
2258 {
2259 RTGCPTR u = (RTGCPTR)u64;
2260 if (u64 == u)
2261 *pGCPtr = u;
2262 else
2263 rc = VERR_CFGM_INTEGER_TOO_BIG;
2264 }
2265 return rc;
2266}
2267
2268
2269/**
2270 * Query Guest Context unsigned pointer value.
2271 *
2272 * @returns VBox status code.
2273 * @param pNode Which node to search for pszName in.
2274 * @param pszName Name of an integer value.
2275 * @param pGCPtr Where to store the value.
2276 */
2277CFGMR3DECL(int) CFGMR3QueryGCPtrU(PCFGMNODE pNode, const char *pszName, PRTGCUINTPTR pGCPtr)
2278{
2279 uint64_t u64;
2280 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
2281 if (VBOX_SUCCESS(rc))
2282 {
2283 RTGCUINTPTR u = (RTGCUINTPTR)u64;
2284 if (u64 == u)
2285 *pGCPtr = u;
2286 else
2287 rc = VERR_CFGM_INTEGER_TOO_BIG;
2288 }
2289 return rc;
2290}
2291
2292
2293/**
2294 * Query Guest Context unsigned pointer value with default.
2295 *
2296 * @returns VBox status code.
2297 * @param pNode Which node to search for pszName in.
2298 * @param pszName Name of an integer value.
2299 * @param pGCPtr Where to store the value.
2300 * @param GCPtrDef The default value.
2301 */
2302CFGMR3DECL(int) CFGMR3QueryGCPtrUDef(PCFGMNODE pNode, const char *pszName, PRTGCUINTPTR pGCPtr, RTGCUINTPTR GCPtrDef)
2303{
2304 uint64_t u64;
2305 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, GCPtrDef);
2306 if (VBOX_SUCCESS(rc))
2307 {
2308 RTGCUINTPTR u = (RTGCUINTPTR)u64;
2309 if (u64 == u)
2310 *pGCPtr = u;
2311 else
2312 rc = VERR_CFGM_INTEGER_TOO_BIG;
2313 }
2314 return rc;
2315}
2316
2317
2318/**
2319 * Query Guest Context signed pointer value.
2320 *
2321 * @returns VBox status code.
2322 * @param pNode Which node to search for pszName in.
2323 * @param pszName Name of an integer value.
2324 * @param pGCPtr Where to store the value.
2325 */
2326CFGMR3DECL(int) CFGMR3QueryGCPtrS(PCFGMNODE pNode, const char *pszName, PRTGCINTPTR pGCPtr)
2327{
2328 uint64_t u64;
2329 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
2330 if (VBOX_SUCCESS(rc))
2331 {
2332 RTGCINTPTR u = (RTGCINTPTR)u64;
2333 if (u64 == (uint64_t)u)
2334 *pGCPtr = u;
2335 else
2336 rc = VERR_CFGM_INTEGER_TOO_BIG;
2337 }
2338 return rc;
2339}
2340
2341
2342/**
2343 * Query Guest Context signed pointer value with default.
2344 *
2345 * @returns VBox status code.
2346 * @param pNode Which node to search for pszName in.
2347 * @param pszName Name of an integer value.
2348 * @param pGCPtr Where to store the value.
2349 * @param GCPtrDef The default value.
2350 */
2351CFGMR3DECL(int) CFGMR3QueryGCPtrSDef(PCFGMNODE pNode, const char *pszName, PRTGCINTPTR pGCPtr, RTGCINTPTR GCPtrDef)
2352{
2353 uint64_t u64;
2354 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, GCPtrDef);
2355 if (VBOX_SUCCESS(rc))
2356 {
2357 RTGCINTPTR u = (RTGCINTPTR)u64;
2358 if (u64 == (uint64_t)u)
2359 *pGCPtr = u;
2360 else
2361 rc = VERR_CFGM_INTEGER_TOO_BIG;
2362 }
2363 return rc;
2364}
2365
2366
2367/**
2368 * Query zero terminated character value storing it in a
2369 * buffer allocated from the MM heap.
2370 *
2371 * @returns VBox status code.
2372 * @param pNode Which node to search for pszName in.
2373 * @param pszName Value name. This value must be of zero terminated character string type.
2374 * @param ppszString Where to store the string pointer.
2375 * Free this using MMR3HeapFree().
2376 */
2377CFGMR3DECL(int) CFGMR3QueryStringAlloc(PCFGMNODE pNode, const char *pszName, char **ppszString)
2378{
2379 size_t cch;
2380 int rc = CFGMR3QuerySize(pNode, pszName, &cch);
2381 if (VBOX_SUCCESS(rc))
2382 {
2383 char *pszString = (char *)MMR3HeapAlloc(pNode->pVM, MM_TAG_CFGM_USER, cch);
2384 if (pszString)
2385 {
2386 rc = CFGMR3QueryString(pNode, pszName, pszString, cch);
2387 if (VBOX_SUCCESS(rc))
2388 *ppszString = pszString;
2389 else
2390 MMR3HeapFree(pszString);
2391 }
2392 else
2393 rc = VERR_NO_MEMORY;
2394 }
2395 return rc;
2396}
2397
2398
2399/**
2400 * Query zero terminated character value storing it in a
2401 * buffer allocated from the MM heap.
2402 *
2403 * @returns VBox status code.
2404 * @param pNode Which node to search for pszName in.
2405 * @param pszName Value name. This value must be of zero terminated character string type.
2406 * @param ppszString Where to store the string pointer.
2407 * Free this using MMR3HeapFree().
2408 */
2409CFGMR3DECL(int) CFGMR3QueryStringAllocDef(PCFGMNODE pNode, const char *pszName, char **ppszString, const char *pszDef)
2410{
2411 size_t cch;
2412 int rc = CFGMR3QuerySize(pNode, pszName, &cch);
2413 if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
2414 {
2415 cch = strlen(pszDef) + 1;
2416 rc = VINF_SUCCESS;
2417 }
2418 if (VBOX_SUCCESS(rc))
2419 {
2420 char *pszString = (char *)MMR3HeapAlloc(pNode->pVM, MM_TAG_CFGM_USER, cch);
2421 if (pszString)
2422 {
2423 rc = CFGMR3QueryStringDef(pNode, pszName, pszString, cch, pszDef);
2424 if (VBOX_SUCCESS(rc))
2425 *ppszString = pszString;
2426 else
2427 MMR3HeapFree(pszString);
2428 }
2429 else
2430 rc = VERR_NO_MEMORY;
2431 }
2432 return rc;
2433}
2434
2435
2436/**
2437 * Dumps the configuration (sub)tree to the release log.
2438 *
2439 * @param pRoot The root node of the dump.
2440 */
2441CFGMR3DECL(void) CFGMR3Dump(PCFGMNODE pRoot)
2442{
2443 LogRel(("************************* CFGM dump *************************\n"));
2444 cfgmR3Info(pRoot->pVM, DBGFR3InfoLogRelHlp(), NULL);
2445 LogRel(("********************* End of CFGM dump **********************\n"));
2446}
2447
2448
2449/**
2450 * Info handler, internal version.
2451 *
2452 * @param pVM The VM handle.
2453 * @param pHlp Callback functions for doing output.
2454 * @param pszArgs Argument string. Optional and specific to the handler.
2455 */
2456static DECLCALLBACK(void) cfgmR3Info(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
2457{
2458 /*
2459 * Figure where to start.
2460 */
2461 PCFGMNODE pRoot = pVM->cfgm.s.pRoot;
2462 if (pszArgs && *pszArgs)
2463 {
2464 int rc = cfgmR3ResolveNode(pRoot, pszArgs, &pRoot);
2465 if (VBOX_FAILURE(rc))
2466 {
2467 pHlp->pfnPrintf(pHlp, "Failed to resolve CFGM path '%s', %Vrc", pszArgs, rc);
2468 return;
2469 }
2470 }
2471
2472 /*
2473 * Dump the specified tree.
2474 */
2475 pHlp->pfnPrintf(pHlp, "pRoot=%p:{", pRoot);
2476 cfgmR3DumpPath(pRoot, pHlp);
2477 pHlp->pfnPrintf(pHlp, "}\n");
2478 cfgmR3Dump(pRoot, 0, pHlp);
2479}
2480
2481
2482/**
2483 * Recursivly prints a path name.
2484 */
2485static void cfgmR3DumpPath(PCFGMNODE pNode, PCDBGFINFOHLP pHlp)
2486{
2487 if (pNode->pParent)
2488 cfgmR3DumpPath(pNode->pParent, pHlp);
2489 pHlp->pfnPrintf(pHlp, "%s/", pNode->szName);
2490}
2491
2492
2493/**
2494 * Dumps a branch of a tree.
2495 */
2496static void cfgmR3Dump(PCFGMNODE pRoot, unsigned iLevel, PCDBGFINFOHLP pHlp)
2497{
2498 /*
2499 * Path.
2500 */
2501 pHlp->pfnPrintf(pHlp, "[");
2502 cfgmR3DumpPath(pRoot, pHlp);
2503 pHlp->pfnPrintf(pHlp, "] (level %d)%s\n", iLevel, pRoot->fRestrictedRoot ? " (restricted root)" : "");
2504
2505 /*
2506 * Values.
2507 */
2508 PCFGMLEAF pLeaf;
2509 unsigned cchMax = 0;
2510 for (pLeaf = CFGMR3GetFirstValue(pRoot); pLeaf; pLeaf = CFGMR3GetNextValue(pLeaf))
2511 cchMax = RT_MAX(cchMax, pLeaf->cchName);
2512 for (pLeaf = CFGMR3GetFirstValue(pRoot); pLeaf; pLeaf = CFGMR3GetNextValue(pLeaf))
2513 {
2514 switch (CFGMR3GetValueType(pLeaf))
2515 {
2516 case CFGMVALUETYPE_INTEGER:
2517 pHlp->pfnPrintf(pHlp, " %-*s <integer> = %#018llx (%lld)\n", cchMax, pLeaf->szName, pLeaf->Value.Integer.u64, pLeaf->Value.Integer.u64);
2518 break;
2519
2520 case CFGMVALUETYPE_STRING:
2521 pHlp->pfnPrintf(pHlp, " %-*s <string> = \"%s\" (cch=%d)\n", cchMax, pLeaf->szName, pLeaf->Value.String.psz, pLeaf->Value.String.cch);
2522 break;
2523
2524 case CFGMVALUETYPE_BYTES:
2525 pHlp->pfnPrintf(pHlp, " %-*s <bytes> = \"%.*Vhxs\" (cb=%d)\n", cchMax, pLeaf->szName, pLeaf->Value.Bytes.cb, pLeaf->Value.Bytes.pau8, pLeaf->Value.Bytes.cb);
2526 break;
2527
2528 default:
2529 AssertMsgFailed(("bad leaf!\n"));
2530 break;
2531 }
2532 }
2533 pHlp->pfnPrintf(pHlp, "\n");
2534
2535 /*
2536 * Children.
2537 */
2538 for (PCFGMNODE pChild = CFGMR3GetFirstChild(pRoot); pChild; pChild = CFGMR3GetNextChild(pChild))
2539 {
2540 Assert(pChild->pNext != pChild);
2541 Assert(pChild->pPrev != pChild);
2542 Assert(pChild->pPrev != pChild->pNext || !pChild->pPrev);
2543 Assert(pChild->pFirstChild != pChild);
2544 Assert(pChild->pParent != pChild);
2545 cfgmR3Dump(pChild, iLevel + 1, pHlp);
2546 }
2547}
2548
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette