VirtualBox

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

Last change on this file since 20374 was 19466, checked in by vboxsync, 16 years ago

tstVMM,CFGM: Hacked together a TM testcase in tstVMM.

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