VirtualBox

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

Last change on this file since 22544 was 22526, checked in by vboxsync, 15 years ago

CFGM: Fixed string length bug in CFGMR3InsertStringFV.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 75.4 KB
Line 
1/* $Id: CFGM.cpp 22526 2009-08-27 13:52: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.cb;
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 size_t cbSrc = pLeaf->Value.String.cb;
641 if (cchString >= cbSrc)
642 {
643 memcpy(pszString, pLeaf->Value.String.psz, cbSrc);
644 memset(pszString + cbSrc, 0, cchString - cbSrc);
645 }
646 else
647 rc = VERR_CFGM_NOT_ENOUGH_SPACE;
648 }
649 else
650 rc = VERR_CFGM_NOT_STRING;
651 }
652 return rc;
653}
654
655
656/**
657 * Query zero terminated character value with default.
658 *
659 * @returns VBox status code.
660 * @param pNode Which node to search for pszName in.
661 * @param pszName Name of a zero terminate character value.
662 * @param pszString Where to store the string. This will not be set on overflow error.
663 * @param cchString Size of the string buffer. (Includes terminator.)
664 * @param pszDef The default value.
665 */
666VMMR3DECL(int) CFGMR3QueryStringDef(PCFGMNODE pNode, const char *pszName, char *pszString, size_t cchString, const char *pszDef)
667{
668 PCFGMLEAF pLeaf;
669 int rc = cfgmR3ResolveLeaf(pNode, pszName, &pLeaf);
670 if (RT_SUCCESS(rc))
671 {
672 if (pLeaf->enmType == CFGMVALUETYPE_STRING)
673 {
674 size_t cbSrc = pLeaf->Value.String.cb;
675 if (cchString >= cbSrc)
676 {
677 memcpy(pszString, pLeaf->Value.String.psz, cbSrc);
678 memset(pszString + cbSrc, 0, cchString - cbSrc);
679 }
680 else
681 rc = VERR_CFGM_NOT_ENOUGH_SPACE;
682 }
683 else
684 rc = VERR_CFGM_NOT_STRING;
685 }
686
687 if (RT_FAILURE(rc) && rc != VERR_CFGM_NOT_ENOUGH_SPACE)
688 {
689 size_t cchDef = strlen(pszDef);
690 if (cchString > cchDef)
691 {
692 memcpy(pszString, pszDef, cchDef);
693 memset(pszString + cchDef, 0, cchString - cchDef);
694 if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
695 rc = VINF_SUCCESS;
696 }
697 else if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
698 rc = VERR_CFGM_NOT_ENOUGH_SPACE;
699 }
700
701 return rc;
702}
703
704
705/**
706 * Query byte string value.
707 *
708 * @returns VBox status code.
709 * @param pNode Which node to search for pszName in.
710 * @param pszName Name of a byte string value.
711 * @param pvData Where to store the binary data.
712 * @param cbData Size of buffer pvData points too.
713 */
714VMMR3DECL(int) CFGMR3QueryBytes(PCFGMNODE pNode, const char *pszName, void *pvData, size_t cbData)
715{
716 PCFGMLEAF pLeaf;
717 int rc = cfgmR3ResolveLeaf(pNode, pszName, &pLeaf);
718 if (RT_SUCCESS(rc))
719 {
720 if (pLeaf->enmType == CFGMVALUETYPE_BYTES)
721 {
722 if (cbData >= pLeaf->Value.Bytes.cb)
723 {
724 memcpy(pvData, pLeaf->Value.Bytes.pau8, pLeaf->Value.Bytes.cb);
725 memset((char *)pvData + pLeaf->Value.Bytes.cb, 0, cbData - pLeaf->Value.Bytes.cb);
726 }
727 else
728 rc = VERR_CFGM_NOT_ENOUGH_SPACE;
729 }
730 else
731 rc = VERR_CFGM_NOT_BYTES;
732 }
733 return rc;
734}
735
736
737/**
738 * Populates the CFGM tree with the default configuration.
739 *
740 * This assumes an empty tree and is intended for testcases and such that only
741 * need to do very small adjustments to the config.
742 *
743 * @returns VBox status code.
744 * @param pVM VM handle.
745 */
746VMMR3DECL(int) CFGMR3ConstructDefaultTree(PVM pVM)
747{
748 int rc;
749 int rcAll = VINF_SUCCESS;
750#define UPDATERC() do { if (RT_FAILURE(rc) && RT_SUCCESS(rcAll)) rcAll = rc; } while (0)
751
752 PCFGMNODE pRoot = CFGMR3GetRoot(pVM);
753 AssertReturn(pRoot, VERR_WRONG_ORDER);
754
755 /*
756 * Create VM default values.
757 */
758 rc = CFGMR3InsertString(pRoot, "Name", "Default VM");
759 UPDATERC();
760 rc = CFGMR3InsertInteger(pRoot, "RamSize", 128U * _1M);
761 UPDATERC();
762 rc = CFGMR3InsertInteger(pRoot, "RamHoleSize", 512U * _1M);
763 UPDATERC();
764 rc = CFGMR3InsertInteger(pRoot, "TimerMillies", 10);
765 UPDATERC();
766 rc = CFGMR3InsertInteger(pRoot, "RawR3Enabled", 1);
767 UPDATERC();
768 /** @todo CFGM Defaults: RawR0, PATMEnabled and CASMEnabled needs attention later. */
769 rc = CFGMR3InsertInteger(pRoot, "RawR0Enabled", 1);
770 UPDATERC();
771 rc = CFGMR3InsertInteger(pRoot, "PATMEnabled", 1);
772 UPDATERC();
773 rc = CFGMR3InsertInteger(pRoot, "CSAMEnabled", 1);
774 UPDATERC();
775
776 /*
777 * PDM.
778 */
779 PCFGMNODE pPdm;
780 rc = CFGMR3InsertNode(pRoot, "PDM", &pPdm);
781 UPDATERC();
782 PCFGMNODE pDevices = NULL;
783 rc = CFGMR3InsertNode(pPdm, "Devices", &pDevices);
784 UPDATERC();
785 rc = CFGMR3InsertInteger(pDevices, "LoadBuiltin", 1); /* boolean */
786 UPDATERC();
787 PCFGMNODE pDrivers = NULL;
788 rc = CFGMR3InsertNode(pPdm, "Drivers", &pDrivers);
789 UPDATERC();
790 rc = CFGMR3InsertInteger(pDrivers, "LoadBuiltin", 1); /* boolean */
791 UPDATERC();
792
793
794 /*
795 * Devices
796 */
797 pDevices = NULL;
798 rc = CFGMR3InsertNode(pRoot, "Devices", &pDevices);
799 UPDATERC();
800 /* device */
801 PCFGMNODE pDev = NULL;
802 PCFGMNODE pInst = NULL;
803 PCFGMNODE pCfg = NULL;
804#if 0
805 PCFGMNODE pLunL0 = NULL;
806 PCFGMNODE pLunL1 = NULL;
807#endif
808
809 /*
810 * PC Arch.
811 */
812 rc = CFGMR3InsertNode(pDevices, "pcarch", &pDev);
813 UPDATERC();
814 rc = CFGMR3InsertNode(pDev, "0", &pInst);
815 UPDATERC();
816 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
817 UPDATERC();
818 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
819 UPDATERC();
820
821 /*
822 * PC Bios.
823 */
824 rc = CFGMR3InsertNode(pDevices, "pcbios", &pDev);
825 UPDATERC();
826 rc = CFGMR3InsertNode(pDev, "0", &pInst);
827 UPDATERC();
828 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
829 UPDATERC();
830 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
831 UPDATERC();
832 rc = CFGMR3InsertInteger(pCfg, "RamSize", 128U * _1M);
833 UPDATERC();
834 rc = CFGMR3InsertInteger(pCfg, "RamHoleSize", 512U * _1M);
835 UPDATERC();
836 rc = CFGMR3InsertString(pCfg, "BootDevice0", "IDE");
837 UPDATERC();
838 rc = CFGMR3InsertString(pCfg, "BootDevice1", "NONE");
839 UPDATERC();
840 rc = CFGMR3InsertString(pCfg, "BootDevice2", "NONE");
841 UPDATERC();
842 rc = CFGMR3InsertString(pCfg, "BootDevice3", "NONE");
843 UPDATERC();
844 rc = CFGMR3InsertString(pCfg, "HardDiskDevice", "piix3ide");
845 UPDATERC();
846 rc = CFGMR3InsertString(pCfg, "FloppyDevice", "");
847 UPDATERC();
848 RTUUID Uuid;
849 RTUuidClear(&Uuid);
850 rc = CFGMR3InsertBytes(pCfg, "UUID", &Uuid, sizeof(Uuid));
851 UPDATERC();
852
853 /*
854 * PCI bus.
855 */
856 rc = CFGMR3InsertNode(pDevices, "pci", &pDev); /* piix3 */
857 UPDATERC();
858 rc = CFGMR3InsertNode(pDev, "0", &pInst);
859 UPDATERC();
860 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
861 UPDATERC();
862 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
863 UPDATERC();
864
865 /*
866 * PS/2 keyboard & mouse
867 */
868 rc = CFGMR3InsertNode(pDevices, "pckbd", &pDev);
869 UPDATERC();
870 rc = CFGMR3InsertNode(pDev, "0", &pInst);
871 UPDATERC();
872 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
873 UPDATERC();
874#if 0
875 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0);
876 UPDATERC();
877 rc = CFGMR3InsertString(pLunL0, "Driver", "KeyboardQueue");
878 UPDATERC();
879 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg);
880 UPDATERC();
881 rc = CFGMR3InsertInteger(pCfg, "QueueSize", 64);
882 UPDATERC();
883 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1);
884 UPDATERC();
885 rc = CFGMR3InsertString(pLunL1, "Driver", "MainKeyboard");
886 UPDATERC();
887 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg);
888 UPDATERC();
889#endif
890#if 0
891 rc = CFGMR3InsertNode(pInst, "LUN#1", &pLunL0);
892 UPDATERC();
893 rc = CFGMR3InsertString(pLunL0, "Driver", "MouseQueue");
894 UPDATERC();
895 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg);
896 UPDATERC();
897 rc = CFGMR3InsertInteger(pCfg, "QueueSize", 128);
898 UPDATERC();
899 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1);
900 UPDATERC();
901 rc = CFGMR3InsertString(pLunL1, "Driver", "MainMouse");
902 UPDATERC();
903 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg);
904 UPDATERC();
905#endif
906
907 /*
908 * i8254 Programmable Interval Timer And Dummy Speaker
909 */
910 rc = CFGMR3InsertNode(pDevices, "i8254", &pDev);
911 UPDATERC();
912 rc = CFGMR3InsertNode(pDev, "0", &pInst);
913 UPDATERC();
914#ifdef DEBUG
915 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
916 UPDATERC();
917#endif
918 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
919 UPDATERC();
920
921 /*
922 * i8259 Programmable Interrupt Controller.
923 */
924 rc = CFGMR3InsertNode(pDevices, "i8259", &pDev);
925 UPDATERC();
926 rc = CFGMR3InsertNode(pDev, "0", &pInst);
927 UPDATERC();
928 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
929 UPDATERC();
930 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
931 UPDATERC();
932
933 /*
934 * RTC MC146818.
935 */
936 rc = CFGMR3InsertNode(pDevices, "mc146818", &pDev);
937 UPDATERC();
938 rc = CFGMR3InsertNode(pDev, "0", &pInst);
939 UPDATERC();
940 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
941 UPDATERC();
942
943 /*
944 * VGA.
945 */
946 rc = CFGMR3InsertNode(pDevices, "vga", &pDev);
947 UPDATERC();
948 rc = CFGMR3InsertNode(pDev, "0", &pInst);
949 UPDATERC();
950 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
951 UPDATERC();
952 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
953 UPDATERC();
954 rc = CFGMR3InsertInteger(pCfg, "VRamSize", 4 * _1M);
955 UPDATERC();
956
957 /* Bios logo. */
958 rc = CFGMR3InsertInteger(pCfg, "FadeIn", 1);
959 UPDATERC();
960 rc = CFGMR3InsertInteger(pCfg, "FadeOut", 1);
961 UPDATERC();
962 rc = CFGMR3InsertInteger(pCfg, "LogoTime", 0);
963 UPDATERC();
964 rc = CFGMR3InsertString(pCfg, "LogoFile", "");
965 UPDATERC();
966
967#if 0
968 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0);
969 UPDATERC();
970 rc = CFGMR3InsertString(pLunL0, "Driver", "MainDisplay");
971 UPDATERC();
972#endif
973
974 /*
975 * IDE controller.
976 */
977 rc = CFGMR3InsertNode(pDevices, "piix3ide", &pDev); /* piix3 */
978 UPDATERC();
979 rc = CFGMR3InsertNode(pDev, "0", &pInst);
980 UPDATERC();
981 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
982 UPDATERC();
983 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
984 UPDATERC();
985
986
987
988 /*
989 * ...
990 */
991
992#undef UPDATERC
993 return rcAll;
994}
995
996
997
998
999/**
1000 * Resolves a path reference to a child node.
1001 *
1002 * @returns VBox status code.
1003 * @param pNode Which node to search for pszName in.
1004 * @param pszPath Path to the child node.
1005 * @param ppChild Where to store the pointer to the child node.
1006 */
1007static int cfgmR3ResolveNode(PCFGMNODE pNode, const char *pszPath, PCFGMNODE *ppChild)
1008{
1009 if (pNode)
1010 {
1011 PCFGMNODE pChild = NULL;
1012 for (;;)
1013 {
1014 /* skip leading slashes. */
1015 while (*pszPath == '/')
1016 pszPath++;
1017
1018 /* End of path? */
1019 if (!*pszPath)
1020 {
1021 if (!pChild)
1022 return VERR_CFGM_INVALID_CHILD_PATH;
1023 *ppChild = pChild;
1024 return VINF_SUCCESS;
1025 }
1026
1027 /* find end of component. */
1028 const char *pszNext = strchr(pszPath, '/');
1029 if (!pszNext)
1030 pszNext = strchr(pszPath, '\0');
1031 RTUINT cchName = pszNext - pszPath;
1032
1033 /* search child list. */
1034 pChild = pNode->pFirstChild;
1035 for ( ; pChild; pChild = pChild->pNext)
1036 if ( pChild->cchName == cchName
1037 && !memcmp(pszPath, pChild->szName, cchName) )
1038 break;
1039
1040 /* if not found, we're done. */
1041 if (!pChild)
1042 return VERR_CFGM_CHILD_NOT_FOUND;
1043
1044 /* next iteration */
1045 pNode = pChild;
1046 pszPath = pszNext;
1047 }
1048
1049 /* won't get here */
1050 }
1051 else
1052 return VERR_CFGM_NO_PARENT;
1053}
1054
1055
1056/**
1057 * Resolves a path reference to a child node.
1058 *
1059 * @returns VBox status code.
1060 * @param pNode Which node to search for pszName in.
1061 * @param pszName Name of a byte string value.
1062 * @param ppLeaf Where to store the pointer to the leaf node.
1063 */
1064static int cfgmR3ResolveLeaf(PCFGMNODE pNode, const char *pszName, PCFGMLEAF *ppLeaf)
1065{
1066 int rc;
1067 if (pNode)
1068 {
1069 size_t cchName = strlen(pszName);
1070 PCFGMLEAF pLeaf = pNode->pFirstLeaf;
1071 while (pLeaf)
1072 {
1073 if ( cchName == pLeaf->cchName
1074 && !memcmp(pszName, pLeaf->szName, cchName) )
1075 {
1076 *ppLeaf = pLeaf;
1077 return VINF_SUCCESS;
1078 }
1079
1080 /* next */
1081 pLeaf = pLeaf->pNext;
1082 }
1083 rc = VERR_CFGM_VALUE_NOT_FOUND;
1084 }
1085 else
1086 rc = VERR_CFGM_NO_PARENT;
1087 return rc;
1088}
1089
1090
1091
1092/**
1093 * Creates a CFGM tree.
1094 *
1095 * This is intended for creating device/driver configs can be
1096 * passed around and later attached to the main tree in the
1097 * correct location.
1098 *
1099 * @returns Pointer to the root node.
1100 * @param pVM The VM handle.
1101 */
1102VMMR3DECL(PCFGMNODE) CFGMR3CreateTree(PVM pVM)
1103{
1104 PCFGMNODE pNew = (PCFGMNODE)MMR3HeapAlloc(pVM, MM_TAG_CFGM, sizeof(*pNew));
1105 if (pNew)
1106 {
1107 pNew->pPrev = NULL;
1108 pNew->pNext = NULL;
1109 pNew->pParent = NULL;
1110 pNew->pFirstChild = NULL;
1111 pNew->pFirstLeaf = NULL;
1112 pNew->pVM = pVM;
1113 pNew->fRestrictedRoot = false;
1114 pNew->cchName = 0;
1115 pNew->szName[0] = 0;
1116 }
1117 return pNew;
1118}
1119
1120
1121/**
1122 * Insert subtree.
1123 *
1124 * This function inserts (no duplication) a tree created by CFGMR3CreateTree()
1125 * into the main tree.
1126 *
1127 * The root node of the inserted subtree will need to be reallocated, which
1128 * effectually means that the passed in pSubTree handle becomes invalid
1129 * upon successful return. Use the value returned in ppChild instead
1130 * of pSubTree.
1131 *
1132 * @returns VBox status code.
1133 * @returns VERR_CFGM_NODE_EXISTS if the final child node name component exists.
1134 * @param pNode Parent node.
1135 * @param pszName Name or path of the new child node.
1136 * @param pSubTree The subtree to insert. Must be returned by CFGMR3CreateTree().
1137 * @param ppChild Where to store the address of the new child node. (optional)
1138 */
1139VMMR3DECL(int) CFGMR3InsertSubTree(PCFGMNODE pNode, const char *pszName, PCFGMNODE pSubTree, PCFGMNODE *ppChild)
1140{
1141 /*
1142 * Validate input.
1143 */
1144 AssertPtrReturn(pSubTree, VERR_INVALID_POINTER);
1145 AssertReturn(!pSubTree->pParent, VERR_INVALID_PARAMETER);
1146 AssertReturn(pSubTree->pVM, VERR_INVALID_PARAMETER);
1147 AssertReturn(pSubTree->pParent != pSubTree->pVM->cfgm.s.pRoot, VERR_INVALID_PARAMETER);
1148 Assert(!pSubTree->pNext);
1149 Assert(!pSubTree->pPrev);
1150
1151 /*
1152 * Use CFGMR3InsertNode to create a new node and then
1153 * re-attach the children and leafs of the subtree to it.
1154 */
1155 PCFGMNODE pNewChild;
1156 int rc = CFGMR3InsertNode(pNode, pszName, &pNewChild);
1157 if (RT_SUCCESS(rc))
1158 {
1159 Assert(!pNewChild->pFirstChild);
1160 pNewChild->pFirstChild = pSubTree->pFirstChild;
1161 Assert(!pNewChild->pFirstLeaf);
1162 pNewChild->pFirstLeaf = pSubTree->pFirstLeaf;
1163 if (ppChild)
1164 *ppChild = pNewChild;
1165
1166 /* free the old subtree root */
1167 pSubTree->pVM = NULL;
1168 pSubTree->pFirstLeaf = NULL;
1169 pSubTree->pFirstChild = NULL;
1170 MMR3HeapFree(pSubTree);
1171 }
1172 return rc;
1173}
1174
1175
1176/**
1177 * Insert a node.
1178 *
1179 * @returns VBox status code.
1180 * @returns VERR_CFGM_NODE_EXISTS if the final child node name component exists.
1181 * @param pNode Parent node.
1182 * @param pszName Name or path of the new child node.
1183 * @param ppChild Where to store the address of the new child node. (optional)
1184 */
1185VMMR3DECL(int) CFGMR3InsertNode(PCFGMNODE pNode, const char *pszName, PCFGMNODE *ppChild)
1186{
1187 int rc;
1188 if (pNode)
1189 {
1190 /*
1191 * If given a path we have to deal with it component by compontent.
1192 */
1193 while (*pszName == '/')
1194 pszName++;
1195 if (strchr(pszName, '/'))
1196 {
1197 char *pszDup = RTStrDup(pszName);
1198 if (pszDup)
1199 {
1200 char *psz = pszDup;
1201 for (;;)
1202 {
1203 /* Terminate at '/' and find the next component. */
1204 char *pszNext = strchr(psz, '/');
1205 if (pszNext)
1206 {
1207 *pszNext++ = '\0';
1208 while (*pszNext == '/')
1209 pszNext++;
1210 if (*pszNext == '\0')
1211 pszNext = NULL;
1212 }
1213
1214 /* does it exist? */
1215 PCFGMNODE pChild = CFGMR3GetChild(pNode, psz);
1216 if (!pChild)
1217 {
1218 /* no, insert it */
1219 rc = CFGMR3InsertNode(pNode, psz, &pChild);
1220 if (RT_FAILURE(rc))
1221 break;
1222 if (!pszNext)
1223 {
1224 if (ppChild)
1225 *ppChild = pChild;
1226 break;
1227 }
1228
1229 }
1230 /* if last component fail */
1231 else if (!pszNext)
1232 {
1233 rc = VERR_CFGM_NODE_EXISTS;
1234 break;
1235 }
1236
1237 /* next */
1238 pNode = pChild;
1239 psz = pszNext;
1240 }
1241 RTStrFree(pszDup);
1242 }
1243 else
1244 rc = VERR_NO_TMP_MEMORY;
1245 }
1246 /*
1247 * Not multicomponent, just make sure it's a non-zero name.
1248 */
1249 else if (*pszName)
1250 {
1251 /*
1252 * Check if already exists and find last node in chain.
1253 */
1254 size_t cchName = strlen(pszName);
1255 PCFGMNODE pPrev = pNode->pFirstChild;
1256 if (pPrev)
1257 {
1258 for (;; pPrev = pPrev->pNext)
1259 {
1260 if ( cchName == pPrev->cchName
1261 && !memcmp(pszName, pPrev->szName, cchName))
1262 return VERR_CFGM_NODE_EXISTS;
1263 if (!pPrev->pNext)
1264 break;
1265 }
1266 }
1267
1268 /*
1269 * Allocate and init node.
1270 */
1271 PCFGMNODE pNew = (PCFGMNODE)MMR3HeapAlloc(pNode->pVM, MM_TAG_CFGM, sizeof(*pNew) + cchName);
1272 if (pNew)
1273 {
1274 pNew->pParent = pNode;
1275 pNew->pFirstChild = NULL;
1276 pNew->pFirstLeaf = NULL;
1277 pNew->pVM = pNode->pVM;
1278 pNew->fRestrictedRoot = false;
1279 pNew->cchName = cchName;
1280 memcpy(pNew->szName, pszName, cchName + 1);
1281
1282 /*
1283 * Insert into child list.
1284 */
1285 pNew->pNext = NULL;
1286 pNew->pPrev = pPrev;
1287 if (pPrev)
1288 pPrev->pNext = pNew;
1289 else
1290 pNode->pFirstChild = pNew;
1291 if (ppChild)
1292 *ppChild = pNew;
1293 rc = VINF_SUCCESS;
1294 }
1295 else
1296 rc = VERR_NO_MEMORY;
1297 }
1298 else
1299 {
1300 rc = VERR_CFGM_INVALID_NODE_PATH;
1301 AssertMsgFailed(("Invalid path %s\n", pszName));
1302 }
1303 }
1304 else
1305 {
1306 rc = VERR_CFGM_NO_PARENT;
1307 AssertMsgFailed(("No parent! path %s\n", pszName));
1308 }
1309
1310 return rc;
1311}
1312
1313
1314/**
1315 * Insert a node, format string name.
1316 *
1317 * @returns VBox status code.
1318 * @param pNode Parent node.
1319 * @param ppChild Where to store the address of the new child node. (optional)
1320 * @param pszNameFormat Name of or path the new child node.
1321 * @param ... Name format arguments.
1322 */
1323VMMR3DECL(int) CFGMR3InsertNodeF(PCFGMNODE pNode, PCFGMNODE *ppChild, const char *pszNameFormat, ...)
1324{
1325 va_list Args;
1326 va_start(Args, pszNameFormat);
1327 int rc = CFGMR3InsertNodeFV(pNode, ppChild, pszNameFormat, Args);
1328 va_end(Args);
1329 return rc;
1330}
1331
1332
1333/**
1334 * Insert a node, format string name.
1335 *
1336 * @returns VBox status code.
1337 * @param pNode Parent node.
1338 * @param ppChild Where to store the address of the new child node. (optional)
1339 * @param pszNameFormat Name or path of the new child node.
1340 * @param Args Name format arguments.
1341 */
1342VMMR3DECL(int) CFGMR3InsertNodeFV(PCFGMNODE pNode, PCFGMNODE *ppChild, const char *pszNameFormat, va_list Args)
1343{
1344 int rc;
1345 char *pszName;
1346 RTStrAPrintfV(&pszName, pszNameFormat, Args);
1347 if (pszName)
1348 {
1349 rc = CFGMR3InsertNode(pNode, pszName, ppChild);
1350 RTStrFree(pszName);
1351 }
1352 else
1353 rc = VERR_NO_MEMORY;
1354 return rc;
1355}
1356
1357
1358/**
1359 * Marks the node as the root of a restricted subtree, i.e. the end of
1360 * a CFGMR3GetParent() journey.
1361 *
1362 * @param pNode The node to mark.
1363 */
1364VMMR3DECL(void) CFGMR3SetRestrictedRoot(PCFGMNODE pNode)
1365{
1366 if (pNode)
1367 pNode->fRestrictedRoot = true;
1368}
1369
1370
1371/**
1372 * Insert a node.
1373 *
1374 * @returns VBox status code.
1375 * @param pNode Parent node.
1376 * @param pszName Name of the new child node.
1377 * @param ppLeaf Where to store the new leaf.
1378 * The caller must fill in the enmType and Value fields!
1379 */
1380static int cfgmR3InsertLeaf(PCFGMNODE pNode, const char *pszName, PCFGMLEAF *ppLeaf)
1381{
1382 int rc;
1383 if (*pszName)
1384 {
1385 if (pNode)
1386 {
1387 /*
1388 * Check if already exists and find last node in chain.
1389 */
1390 size_t cchName = strlen(pszName);
1391 PCFGMLEAF pPrev = pNode->pFirstLeaf;
1392 if (pPrev)
1393 {
1394 for (;; pPrev = pPrev->pNext)
1395 {
1396 if ( cchName == pPrev->cchName
1397 && !memcmp(pszName, pPrev->szName, cchName))
1398 return VERR_CFGM_LEAF_EXISTS;
1399 if (!pPrev->pNext)
1400 break;
1401 }
1402 }
1403
1404 /*
1405 * Allocate and init node.
1406 */
1407 PCFGMLEAF pNew = (PCFGMLEAF)MMR3HeapAlloc(pNode->pVM, MM_TAG_CFGM, sizeof(*pNew) + cchName);
1408 if (pNew)
1409 {
1410 pNew->cchName = cchName;
1411 memcpy(pNew->szName, pszName, cchName + 1);
1412
1413 /*
1414 * Insert into child list.
1415 */
1416 pNew->pNext = NULL;
1417 pNew->pPrev = pPrev;
1418 if (pPrev)
1419 pPrev->pNext = pNew;
1420 else
1421 pNode->pFirstLeaf = pNew;
1422 *ppLeaf = pNew;
1423 rc = VINF_SUCCESS;
1424 }
1425 else
1426 rc = VERR_NO_MEMORY;
1427 }
1428 else
1429 rc = VERR_CFGM_NO_PARENT;
1430 }
1431 else
1432 rc = VERR_CFGM_INVALID_CHILD_PATH;
1433 return rc;
1434}
1435
1436
1437/**
1438 * Remove a node.
1439 *
1440 * @param pNode Parent node.
1441 */
1442VMMR3DECL(void) CFGMR3RemoveNode(PCFGMNODE pNode)
1443{
1444 if (pNode)
1445 {
1446 /*
1447 * Free children.
1448 */
1449 while (pNode->pFirstChild)
1450 CFGMR3RemoveNode(pNode->pFirstChild);
1451
1452 /*
1453 * Free leafs.
1454 */
1455 while (pNode->pFirstLeaf)
1456 cfgmR3RemoveLeaf(pNode, pNode->pFirstLeaf);
1457
1458 /*
1459 * Unlink ourselves.
1460 */
1461 if (pNode->pPrev)
1462 pNode->pPrev->pNext = pNode->pNext;
1463 else
1464 {
1465 if (pNode->pParent)
1466 pNode->pParent->pFirstChild = pNode->pNext;
1467 else if (pNode == pNode->pVM->cfgm.s.pRoot) /* might be a different tree */
1468 pNode->pVM->cfgm.s.pRoot = NULL;
1469 }
1470 if (pNode->pNext)
1471 pNode->pNext->pPrev = pNode->pPrev;
1472
1473 /*
1474 * Free ourselves. (bit of paranoia first)
1475 */
1476 pNode->pVM = NULL;
1477 pNode->pNext = NULL;
1478 pNode->pPrev = NULL;
1479 pNode->pParent = NULL;
1480 MMR3HeapFree(pNode);
1481 }
1482}
1483
1484
1485/**
1486 * Removes a leaf.
1487 *
1488 * @param pNode Parent node.
1489 * @param pLeaf Leaf to remove.
1490 */
1491static void cfgmR3RemoveLeaf(PCFGMNODE pNode, PCFGMLEAF pLeaf)
1492{
1493 if (pNode && pLeaf)
1494 {
1495 /*
1496 * Unlink.
1497 */
1498 if (pLeaf->pPrev)
1499 pLeaf->pPrev->pNext = pLeaf->pNext;
1500 else
1501 pNode->pFirstLeaf = pLeaf->pNext;
1502 if (pLeaf->pNext)
1503 pLeaf->pNext->pPrev = pLeaf->pPrev;
1504
1505 /*
1506 * Free value and node.
1507 */
1508 cfgmR3FreeValue(pLeaf);
1509 pLeaf->pNext = NULL;
1510 pLeaf->pPrev = NULL;
1511 MMR3HeapFree(pLeaf);
1512 }
1513}
1514
1515
1516/**
1517 * Frees whatever resources the leaf value is owning.
1518 *
1519 * Use this before assigning a new value to a leaf.
1520 * The caller must either free the leaf or assign a new value to it.
1521 *
1522 * @param pLeaf Pointer to the leaf which value should be free.
1523 */
1524static void cfgmR3FreeValue(PCFGMLEAF pLeaf)
1525{
1526 if (pLeaf)
1527 {
1528 switch (pLeaf->enmType)
1529 {
1530 case CFGMVALUETYPE_BYTES:
1531 MMR3HeapFree(pLeaf->Value.Bytes.pau8);
1532 pLeaf->Value.Bytes.pau8 = NULL;
1533 pLeaf->Value.Bytes.cb = 0;
1534 break;
1535
1536 case CFGMVALUETYPE_STRING:
1537 MMR3HeapFree(pLeaf->Value.String.psz);
1538 pLeaf->Value.String.psz = NULL;
1539 pLeaf->Value.String.cb = 0;
1540 break;
1541
1542 case CFGMVALUETYPE_INTEGER:
1543 break;
1544 }
1545 pLeaf->enmType = (CFGMVALUETYPE)0;
1546 }
1547}
1548
1549
1550/**
1551 * Inserts a new integer value.
1552 *
1553 * @returns VBox status code.
1554 * @param pNode Parent node.
1555 * @param pszName Value name.
1556 * @param u64Integer The value.
1557 */
1558VMMR3DECL(int) CFGMR3InsertInteger(PCFGMNODE pNode, const char *pszName, uint64_t u64Integer)
1559{
1560 PCFGMLEAF pLeaf;
1561 int rc = cfgmR3InsertLeaf(pNode, pszName, &pLeaf);
1562 if (RT_SUCCESS(rc))
1563 {
1564 pLeaf->enmType = CFGMVALUETYPE_INTEGER;
1565 pLeaf->Value.Integer.u64 = u64Integer;
1566 }
1567 return rc;
1568}
1569
1570
1571/**
1572 * Inserts a new string value.
1573 *
1574 * @returns VBox status code.
1575 * @param pNode Parent node.
1576 * @param pszName Value name.
1577 * @param pszString The value.
1578 */
1579VMMR3DECL(int) CFGMR3InsertString(PCFGMNODE pNode, const char *pszName, const char *pszString)
1580{
1581 int rc;
1582 if (pNode)
1583 {
1584 /*
1585 * Allocate string object first.
1586 */
1587 size_t cbString = strlen(pszString) + 1;
1588 char *pszStringCopy = (char *)MMR3HeapAlloc(pNode->pVM, MM_TAG_CFGM_STRING, cbString);
1589 if (pszStringCopy)
1590 {
1591 memcpy(pszStringCopy, pszString, cbString);
1592
1593 /*
1594 * Create value leaf and set it to string type.
1595 */
1596 PCFGMLEAF pLeaf;
1597 rc = cfgmR3InsertLeaf(pNode, pszName, &pLeaf);
1598 if (RT_SUCCESS(rc))
1599 {
1600 pLeaf->enmType = CFGMVALUETYPE_STRING;
1601 pLeaf->Value.String.psz = pszStringCopy;
1602 pLeaf->Value.String.cb = cbString;
1603 }
1604 else
1605 MMR3HeapFree(pszStringCopy);
1606 }
1607 else
1608 rc = VERR_NO_MEMORY;
1609 }
1610 else
1611 rc = VERR_CFGM_NO_PARENT;
1612
1613 return rc;
1614}
1615
1616
1617/**
1618 * Same as CFGMR3InsertString except the string value given in RTStrPrintfV
1619 * fashion.
1620 *
1621 * @returns VBox status code.
1622 * @param pNode Parent node.
1623 * @param pszName Value name.
1624 * @param pszFormat The value given as a format string.
1625 * @param va Argument to pszFormat.
1626 */
1627VMMR3DECL(int) CFGMR3InsertStringFV(PCFGMNODE pNode, const char *pszName, const char *pszFormat, va_list va)
1628{
1629 int rc;
1630 if (pNode)
1631 {
1632 /*
1633 * Allocate string object first.
1634 */
1635 char *pszString = MMR3HeapAPrintfVU(pNode->pVM->pUVM, MM_TAG_CFGM_STRING, pszFormat, va);
1636 if (pszString)
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_STRING;
1646 pLeaf->Value.String.psz = pszString;
1647 pLeaf->Value.String.cb = strlen(pszString) + 1;
1648 }
1649 else
1650 MMR3HeapFree(pszString);
1651 }
1652 else
1653 rc = VERR_NO_MEMORY;
1654 }
1655 else
1656 rc = VERR_CFGM_NO_PARENT;
1657
1658 return rc;
1659}
1660
1661
1662/**
1663 * Same as CFGMR3InsertString except the string value given in RTStrPrintf
1664 * fashion.
1665 *
1666 * @returns VBox status code.
1667 * @param pNode Parent node.
1668 * @param pszName Value name.
1669 * @param pszFormat The value given as a format string.
1670 * @param ... Argument to pszFormat.
1671 */
1672VMMR3DECL(int) CFGMR3InsertStringF(PCFGMNODE pNode, const char *pszName, const char *pszFormat, ...)
1673{
1674 va_list va;
1675 va_start(va, pszFormat);
1676 int rc = CFGMR3InsertStringFV(pNode, pszName, pszFormat, va);
1677 va_end(va);
1678 return rc;
1679}
1680
1681
1682/**
1683 * Same as CFGMR3InsertString except the string value given as a UTF-16 string.
1684 *
1685 * @returns VBox status code.
1686 * @param pNode Parent node.
1687 * @param pszName Value name.
1688 * @param pwszValue The string value (UTF-16).
1689 */
1690VMMR3DECL(int) CFGMR3InsertStringW(PCFGMNODE pNode, const char *pszName, PCRTUTF16 pwszValue)
1691{
1692 char *pszValue;
1693 int rc = RTUtf16ToUtf8(pwszValue, &pszValue);
1694 if (RT_SUCCESS(rc))
1695 {
1696 rc = CFGMR3InsertString(pNode, pszName, pszValue);
1697 RTStrFree(pszValue);
1698 }
1699 return rc;
1700}
1701
1702
1703/**
1704 * Inserts a new integer value.
1705 *
1706 * @returns VBox status code.
1707 * @param pNode Parent node.
1708 * @param pszName Value name.
1709 * @param pvBytes The value.
1710 * @param cbBytes The value size.
1711 */
1712VMMR3DECL(int) CFGMR3InsertBytes(PCFGMNODE pNode, const char *pszName, const void *pvBytes, size_t cbBytes)
1713{
1714 int rc;
1715 if (pNode)
1716 {
1717 if (cbBytes == (RTUINT)cbBytes)
1718 {
1719 /*
1720 * Allocate string object first.
1721 */
1722 void *pvCopy = MMR3HeapAlloc(pNode->pVM, MM_TAG_CFGM_STRING, cbBytes);
1723 if (pvCopy || !cbBytes)
1724 {
1725 memcpy(pvCopy, pvBytes, cbBytes);
1726
1727 /*
1728 * Create value leaf and set it to string type.
1729 */
1730 PCFGMLEAF pLeaf;
1731 rc = cfgmR3InsertLeaf(pNode, pszName, &pLeaf);
1732 if (RT_SUCCESS(rc))
1733 {
1734 pLeaf->enmType = CFGMVALUETYPE_BYTES;
1735 pLeaf->Value.Bytes.cb = cbBytes;
1736 pLeaf->Value.Bytes.pau8 = (uint8_t *)pvCopy;
1737 }
1738 }
1739 else
1740 rc = VERR_NO_MEMORY;
1741 }
1742 else
1743 rc = VERR_OUT_OF_RANGE;
1744 }
1745 else
1746 rc = VERR_CFGM_NO_PARENT;
1747
1748 return rc;
1749}
1750
1751
1752/**
1753 * Remove a value.
1754 *
1755 * @returns VBox status code.
1756 * @param pNode Parent node.
1757 * @param pszName Name of the new child node.
1758 */
1759VMMR3DECL(int) CFGMR3RemoveValue(PCFGMNODE pNode, const char *pszName)
1760{
1761 PCFGMLEAF pLeaf;
1762 int rc = cfgmR3ResolveLeaf(pNode, pszName, &pLeaf);
1763 if (RT_SUCCESS(rc))
1764 cfgmR3RemoveLeaf(pNode, pLeaf);
1765 return rc;
1766}
1767
1768
1769
1770/*
1771 * -+- helper apis -+-
1772 */
1773
1774
1775/**
1776 * Query unsigned 64-bit integer value.
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 pu64 Where to store the integer value.
1782 */
1783VMMR3DECL(int) CFGMR3QueryU64(PCFGMNODE pNode, const char *pszName, uint64_t *pu64)
1784{
1785 return CFGMR3QueryInteger(pNode, pszName, pu64);
1786}
1787
1788
1789/**
1790 * Query unsigned 64-bit integer value with default.
1791 *
1792 * @returns VBox status code.
1793 * @param pNode Which node to search for pszName in.
1794 * @param pszName Name of an integer value.
1795 * @param pu64 Where to store the integer value. Set to default on failure.
1796 * @param u64Def The default value.
1797 */
1798VMMR3DECL(int) CFGMR3QueryU64Def(PCFGMNODE pNode, const char *pszName, uint64_t *pu64, uint64_t u64Def)
1799{
1800 return CFGMR3QueryIntegerDef(pNode, pszName, pu64, u64Def);
1801}
1802
1803
1804/**
1805 * Query signed 64-bit integer value.
1806 *
1807 * @returns VBox status code.
1808 * @param pNode Which node to search for pszName in.
1809 * @param pszName Name of an integer value.
1810 * @param pi64 Where to store the value.
1811 */
1812VMMR3DECL(int) CFGMR3QueryS64(PCFGMNODE pNode, const char *pszName, int64_t *pi64)
1813{
1814 uint64_t u64;
1815 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
1816 if (RT_SUCCESS(rc))
1817 *pi64 = (int64_t)u64;
1818 return rc;
1819}
1820
1821
1822/**
1823 * Query signed 64-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 pi64 Where to store the value. Set to default on failure.
1829 * @param i64Def The default value.
1830 */
1831VMMR3DECL(int) CFGMR3QueryS64Def(PCFGMNODE pNode, const char *pszName, int64_t *pi64, int64_t i64Def)
1832{
1833 uint64_t u64;
1834 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, i64Def);
1835 if (RT_SUCCESS(rc))
1836 *pi64 = (int64_t)u64;
1837 return rc;
1838}
1839
1840
1841/**
1842 * Query unsigned 32-bit integer value.
1843 *
1844 * @returns VBox status code.
1845 * @param pNode Which node to search for pszName in.
1846 * @param pszName Name of an integer value.
1847 * @param pu32 Where to store the value.
1848 */
1849VMMR3DECL(int) CFGMR3QueryU32(PCFGMNODE pNode, const char *pszName, uint32_t *pu32)
1850{
1851 uint64_t u64;
1852 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
1853 if (RT_SUCCESS(rc))
1854 {
1855 if (!(u64 & UINT64_C(0xffffffff00000000)))
1856 *pu32 = (uint32_t)u64;
1857 else
1858 rc = VERR_CFGM_INTEGER_TOO_BIG;
1859 }
1860 return rc;
1861}
1862
1863
1864/**
1865 * Query unsigned 32-bit integer value with default.
1866 *
1867 * @returns VBox status code.
1868 * @param pNode Which node to search for pszName in.
1869 * @param pszName Name of an integer value.
1870 * @param pu32 Where to store the value. Set to default on failure.
1871 * @param u32Def The default value.
1872 */
1873VMMR3DECL(int) CFGMR3QueryU32Def(PCFGMNODE pNode, const char *pszName, uint32_t *pu32, uint32_t u32Def)
1874{
1875 uint64_t u64;
1876 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, u32Def);
1877 if (RT_SUCCESS(rc))
1878 {
1879 if (!(u64 & UINT64_C(0xffffffff00000000)))
1880 *pu32 = (uint32_t)u64;
1881 else
1882 rc = VERR_CFGM_INTEGER_TOO_BIG;
1883 }
1884 return rc;
1885}
1886
1887
1888/**
1889 * Query signed 32-bit integer value.
1890 *
1891 * @returns VBox status code.
1892 * @param pNode Which node to search for pszName in.
1893 * @param pszName Name of an integer value.
1894 * @param pi32 Where to store the value.
1895 */
1896VMMR3DECL(int) CFGMR3QueryS32(PCFGMNODE pNode, const char *pszName, int32_t *pi32)
1897{
1898 uint64_t u64;
1899 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
1900 if (RT_SUCCESS(rc))
1901 {
1902 if ( !(u64 & UINT64_C(0xffffffff80000000))
1903 || (u64 & UINT64_C(0xffffffff80000000)) == UINT64_C(0xffffffff80000000))
1904 *pi32 = (int32_t)u64;
1905 else
1906 rc = VERR_CFGM_INTEGER_TOO_BIG;
1907 }
1908 return rc;
1909}
1910
1911
1912/**
1913 * Query signed 32-bit integer value with default.
1914 *
1915 * @returns VBox status code.
1916 * @param pNode Which node to search for pszName in.
1917 * @param pszName Name of an integer value.
1918 * @param pi32 Where to store the value. Set to default on failure.
1919 * @param i32Def The default value.
1920 */
1921VMMR3DECL(int) CFGMR3QueryS32Def(PCFGMNODE pNode, const char *pszName, int32_t *pi32, int32_t i32Def)
1922{
1923 uint64_t u64;
1924 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, i32Def);
1925 if (RT_SUCCESS(rc))
1926 {
1927 if ( !(u64 & UINT64_C(0xffffffff80000000))
1928 || (u64 & UINT64_C(0xffffffff80000000)) == UINT64_C(0xffffffff80000000))
1929 *pi32 = (int32_t)u64;
1930 else
1931 rc = VERR_CFGM_INTEGER_TOO_BIG;
1932 }
1933 return rc;
1934}
1935
1936
1937/**
1938 * Query unsigned 16-bit integer value.
1939 *
1940 * @returns VBox status code.
1941 * @param pNode Which node to search for pszName in.
1942 * @param pszName Name of an integer value.
1943 * @param pu16 Where to store the value.
1944 */
1945VMMR3DECL(int) CFGMR3QueryU16(PCFGMNODE pNode, const char *pszName, uint16_t *pu16)
1946{
1947 uint64_t u64;
1948 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
1949 if (RT_SUCCESS(rc))
1950 {
1951 if (!(u64 & UINT64_C(0xffffffffffff0000)))
1952 *pu16 = (int16_t)u64;
1953 else
1954 rc = VERR_CFGM_INTEGER_TOO_BIG;
1955 }
1956 return rc;
1957}
1958
1959
1960/**
1961 * Query unsigned 16-bit integer value with default.
1962 *
1963 * @returns VBox status code.
1964 * @param pNode Which node to search for pszName in.
1965 * @param pszName Name of an integer value.
1966 * @param pu16 Where to store the value. Set to default on failure.
1967 * @param i16Def The default value.
1968 */
1969VMMR3DECL(int) CFGMR3QueryU16Def(PCFGMNODE pNode, const char *pszName, uint16_t *pu16, uint16_t u16Def)
1970{
1971 uint64_t u64;
1972 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, u16Def);
1973 if (RT_SUCCESS(rc))
1974 {
1975 if (!(u64 & UINT64_C(0xffffffffffff0000)))
1976 *pu16 = (int16_t)u64;
1977 else
1978 rc = VERR_CFGM_INTEGER_TOO_BIG;
1979 }
1980 return rc;
1981}
1982
1983
1984/**
1985 * Query signed 16-bit integer value.
1986 *
1987 * @returns VBox status code.
1988 * @param pNode Which node to search for pszName in.
1989 * @param pszName Name of an integer value.
1990 * @param pi16 Where to store the value.
1991 */
1992VMMR3DECL(int) CFGMR3QueryS16(PCFGMNODE pNode, const char *pszName, int16_t *pi16)
1993{
1994 uint64_t u64;
1995 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
1996 if (RT_SUCCESS(rc))
1997 {
1998 if ( !(u64 & UINT64_C(0xffffffffffff8000))
1999 || (u64 & UINT64_C(0xffffffffffff8000)) == UINT64_C(0xffffffffffff8000))
2000 *pi16 = (int16_t)u64;
2001 else
2002 rc = VERR_CFGM_INTEGER_TOO_BIG;
2003 }
2004 return rc;
2005}
2006
2007
2008/**
2009 * Query signed 16-bit integer value with default.
2010 *
2011 * @returns VBox status code.
2012 * @param pNode Which node to search for pszName in.
2013 * @param pszName Name of an integer value.
2014 * @param pi16 Where to store the value. Set to default on failure.
2015 * @param i16Def The default value.
2016 */
2017VMMR3DECL(int) CFGMR3QueryS16Def(PCFGMNODE pNode, const char *pszName, int16_t *pi16, int16_t i16Def)
2018{
2019 uint64_t u64;
2020 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, i16Def);
2021 if (RT_SUCCESS(rc))
2022 {
2023 if ( !(u64 & UINT64_C(0xffffffffffff8000))
2024 || (u64 & UINT64_C(0xffffffffffff8000)) == UINT64_C(0xffffffffffff8000))
2025 *pi16 = (int16_t)u64;
2026 else
2027 rc = VERR_CFGM_INTEGER_TOO_BIG;
2028 }
2029 return rc;
2030}
2031
2032
2033/**
2034 * Query unsigned 8-bit integer value.
2035 *
2036 * @returns VBox status code.
2037 * @param pNode Which node to search for pszName in.
2038 * @param pszName Name of an integer value.
2039 * @param pu8 Where to store the value.
2040 */
2041VMMR3DECL(int) CFGMR3QueryU8(PCFGMNODE pNode, const char *pszName, uint8_t *pu8)
2042{
2043 uint64_t u64;
2044 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
2045 if (RT_SUCCESS(rc))
2046 {
2047 if (!(u64 & UINT64_C(0xffffffffffffff00)))
2048 *pu8 = (uint8_t)u64;
2049 else
2050 rc = VERR_CFGM_INTEGER_TOO_BIG;
2051 }
2052 return rc;
2053}
2054
2055
2056/**
2057 * Query unsigned 8-bit integer value with default.
2058 *
2059 * @returns VBox status code.
2060 * @param pNode Which node to search for pszName in.
2061 * @param pszName Name of an integer value.
2062 * @param pu8 Where to store the value. Set to default on failure.
2063 * @param u8Def The default value.
2064 */
2065VMMR3DECL(int) CFGMR3QueryU8Def(PCFGMNODE pNode, const char *pszName, uint8_t *pu8, uint8_t u8Def)
2066{
2067 uint64_t u64;
2068 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, u8Def);
2069 if (RT_SUCCESS(rc))
2070 {
2071 if (!(u64 & UINT64_C(0xffffffffffffff00)))
2072 *pu8 = (uint8_t)u64;
2073 else
2074 rc = VERR_CFGM_INTEGER_TOO_BIG;
2075 }
2076 return rc;
2077}
2078
2079
2080/**
2081 * Query signed 8-bit integer value.
2082 *
2083 * @returns VBox status code.
2084 * @param pNode Which node to search for pszName in.
2085 * @param pszName Name of an integer value.
2086 * @param pi8 Where to store the value.
2087 */
2088VMMR3DECL(int) CFGMR3QueryS8(PCFGMNODE pNode, const char *pszName, int8_t *pi8)
2089{
2090 uint64_t u64;
2091 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
2092 if (RT_SUCCESS(rc))
2093 {
2094 if ( !(u64 & UINT64_C(0xffffffffffffff80))
2095 || (u64 & UINT64_C(0xffffffffffffff80)) == UINT64_C(0xffffffffffffff80))
2096 *pi8 = (int8_t)u64;
2097 else
2098 rc = VERR_CFGM_INTEGER_TOO_BIG;
2099 }
2100 return rc;
2101}
2102
2103
2104/**
2105 * Query signed 8-bit integer value with default.
2106 *
2107 * @returns VBox status code.
2108 * @param pNode Which node to search for pszName in.
2109 * @param pszName Name of an integer value.
2110 * @param pi8 Where to store the value. Set to default on failure.
2111 * @param i8Def The default value.
2112 */
2113VMMR3DECL(int) CFGMR3QueryS8Def(PCFGMNODE pNode, const char *pszName, int8_t *pi8, int8_t i8Def)
2114{
2115 uint64_t u64;
2116 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, i8Def);
2117 if (RT_SUCCESS(rc))
2118 {
2119 if ( !(u64 & UINT64_C(0xffffffffffffff80))
2120 || (u64 & UINT64_C(0xffffffffffffff80)) == UINT64_C(0xffffffffffffff80))
2121 *pi8 = (int8_t)u64;
2122 else
2123 rc = VERR_CFGM_INTEGER_TOO_BIG;
2124 }
2125 return rc;
2126}
2127
2128
2129/**
2130 * Query boolean integer value.
2131 *
2132 * @returns VBox status code.
2133 * @param pNode Which node to search for pszName in.
2134 * @param pszName Name of an integer value.
2135 * @param pf Where to store the value.
2136 * @remark This function will interpret any non-zero value as true.
2137 */
2138VMMR3DECL(int) CFGMR3QueryBool(PCFGMNODE pNode, const char *pszName, bool *pf)
2139{
2140 uint64_t u64;
2141 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
2142 if (RT_SUCCESS(rc))
2143 *pf = u64 ? true : false;
2144 return rc;
2145}
2146
2147
2148/**
2149 * Query boolean integer value with default.
2150 *
2151 * @returns VBox status code.
2152 * @param pNode Which node to search for pszName in.
2153 * @param pszName Name of an integer value.
2154 * @param pf Where to store the value. Set to default on failure.
2155 * @param fDef The default value.
2156 * @remark This function will interpret any non-zero value as true.
2157 */
2158VMMR3DECL(int) CFGMR3QueryBoolDef(PCFGMNODE pNode, const char *pszName, bool *pf, bool fDef)
2159{
2160 uint64_t u64;
2161 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, fDef);
2162 if (RT_SUCCESS(rc))
2163 *pf = u64 ? true : false;
2164 return rc;
2165}
2166
2167
2168/**
2169 * Query I/O port address value.
2170 *
2171 * @returns VBox status code.
2172 * @param pNode Which node to search for pszName in.
2173 * @param pszName Name of an integer value.
2174 * @param pPort Where to store the value.
2175 */
2176VMMR3DECL(int) CFGMR3QueryPort(PCFGMNODE pNode, const char *pszName, PRTIOPORT pPort)
2177{
2178 AssertCompileSize(RTIOPORT, 2);
2179 return CFGMR3QueryU16(pNode, pszName, pPort);
2180}
2181
2182
2183/**
2184 * Query I/O port address value with default.
2185 *
2186 * @returns VBox status code.
2187 * @param pNode Which node to search for pszName in.
2188 * @param pszName Name of an integer value.
2189 * @param pPort Where to store the value. Set to default on failure.
2190 * @param PortDef The default value.
2191 */
2192VMMR3DECL(int) CFGMR3QueryPortDef(PCFGMNODE pNode, const char *pszName, PRTIOPORT pPort, RTIOPORT PortDef)
2193{
2194 AssertCompileSize(RTIOPORT, 2);
2195 return CFGMR3QueryU16Def(pNode, pszName, pPort, PortDef);
2196}
2197
2198
2199/**
2200 * Query unsigned int address value.
2201 *
2202 * @returns VBox status code.
2203 * @param pNode Which node to search for pszName in.
2204 * @param pszName Name of an integer value.
2205 * @param pu Where to store the value.
2206 */
2207VMMR3DECL(int) CFGMR3QueryUInt(PCFGMNODE pNode, const char *pszName, unsigned int *pu)
2208{
2209 AssertCompileSize(unsigned int, 4);
2210 return CFGMR3QueryU32(pNode, pszName, (uint32_t *)pu);
2211}
2212
2213
2214/**
2215 * Query unsigned int address value with default.
2216 *
2217 * @returns VBox status code.
2218 * @param pNode Which node to search for pszName in.
2219 * @param pszName Name of an integer value.
2220 * @param pu Where to store the value. Set to default on failure.
2221 * @param uDef The default value.
2222 */
2223VMMR3DECL(int) CFGMR3QueryUIntDef(PCFGMNODE pNode, const char *pszName, unsigned int *pu, unsigned int uDef)
2224{
2225 AssertCompileSize(unsigned int, 4);
2226 return CFGMR3QueryU32Def(pNode, pszName, (uint32_t *)pu, uDef);
2227}
2228
2229
2230/**
2231 * Query signed int address value.
2232 *
2233 * @returns VBox status code.
2234 * @param pNode Which node to search for pszName in.
2235 * @param pszName Name of an integer value.
2236 * @param pi Where to store the value.
2237 */
2238VMMR3DECL(int) CFGMR3QuerySInt(PCFGMNODE pNode, const char *pszName, signed int *pi)
2239{
2240 AssertCompileSize(signed int, 4);
2241 return CFGMR3QueryS32(pNode, pszName, (int32_t *)pi);
2242}
2243
2244
2245/**
2246 * Query unsigned int address 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 pi Where to store the value. Set to default on failure.
2252 * @param iDef The default value.
2253 */
2254VMMR3DECL(int) CFGMR3QuerySIntDef(PCFGMNODE pNode, const char *pszName, signed int *pi, signed int iDef)
2255{
2256 AssertCompileSize(signed int, 4);
2257 return CFGMR3QueryS32Def(pNode, pszName, (int32_t *)pi, iDef);
2258}
2259
2260
2261/**
2262 * Query pointer integer value.
2263 *
2264 * @returns VBox status code.
2265 * @param pNode Which node to search for pszName in.
2266 * @param pszName Name of an integer value.
2267 * @param ppv Where to store the value.
2268 */
2269VMMR3DECL(int) CFGMR3QueryPtr(PCFGMNODE pNode, const char *pszName, void **ppv)
2270{
2271 uint64_t u64;
2272 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
2273 if (RT_SUCCESS(rc))
2274 {
2275 uintptr_t u = (uintptr_t)u64;
2276 if (u64 == u)
2277 *ppv = (void *)u;
2278 else
2279 rc = VERR_CFGM_INTEGER_TOO_BIG;
2280 }
2281 return rc;
2282}
2283
2284
2285/**
2286 * Query pointer integer value with default.
2287 *
2288 * @returns VBox status code.
2289 * @param pNode Which node to search for pszName in.
2290 * @param pszName Name of an integer value.
2291 * @param ppv Where to store the value. Set to default on failure.
2292 * @param pvDef The default value.
2293 */
2294VMMR3DECL(int) CFGMR3QueryPtrDef(PCFGMNODE pNode, const char *pszName, void **ppv, void *pvDef)
2295{
2296 uint64_t u64;
2297 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, (uintptr_t)pvDef);
2298 if (RT_SUCCESS(rc))
2299 {
2300 uintptr_t u = (uintptr_t)u64;
2301 if (u64 == u)
2302 *ppv = (void *)u;
2303 else
2304 rc = VERR_CFGM_INTEGER_TOO_BIG;
2305 }
2306 return rc;
2307}
2308
2309
2310/**
2311 * Query Guest Context pointer integer value.
2312 *
2313 * @returns VBox status code.
2314 * @param pNode Which node to search for pszName in.
2315 * @param pszName Name of an integer value.
2316 * @param pGCPtr Where to store the value.
2317 */
2318VMMR3DECL(int) CFGMR3QueryGCPtr(PCFGMNODE pNode, const char *pszName, PRTGCPTR pGCPtr)
2319{
2320 uint64_t u64;
2321 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
2322 if (RT_SUCCESS(rc))
2323 {
2324 RTGCPTR u = (RTGCPTR)u64;
2325 if (u64 == u)
2326 *pGCPtr = u;
2327 else
2328 rc = VERR_CFGM_INTEGER_TOO_BIG;
2329 }
2330 return rc;
2331}
2332
2333
2334/**
2335 * Query Guest Context pointer integer value with default.
2336 *
2337 * @returns VBox status code.
2338 * @param pNode Which node to search for pszName in.
2339 * @param pszName Name of an integer value.
2340 * @param pGCPtr Where to store the value. Set to default on failure.
2341 * @param GCPtrDef The default value.
2342 */
2343VMMR3DECL(int) CFGMR3QueryGCPtrDef(PCFGMNODE pNode, const char *pszName, PRTGCPTR pGCPtr, RTGCPTR GCPtrDef)
2344{
2345 uint64_t u64;
2346 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, GCPtrDef);
2347 if (RT_SUCCESS(rc))
2348 {
2349 RTGCPTR u = (RTGCPTR)u64;
2350 if (u64 == u)
2351 *pGCPtr = u;
2352 else
2353 rc = VERR_CFGM_INTEGER_TOO_BIG;
2354 }
2355 return rc;
2356}
2357
2358
2359/**
2360 * Query Guest Context unsigned pointer value.
2361 *
2362 * @returns VBox status code.
2363 * @param pNode Which node to search for pszName in.
2364 * @param pszName Name of an integer value.
2365 * @param pGCPtr Where to store the value.
2366 */
2367VMMR3DECL(int) CFGMR3QueryGCPtrU(PCFGMNODE pNode, const char *pszName, PRTGCUINTPTR pGCPtr)
2368{
2369 uint64_t u64;
2370 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
2371 if (RT_SUCCESS(rc))
2372 {
2373 RTGCUINTPTR u = (RTGCUINTPTR)u64;
2374 if (u64 == u)
2375 *pGCPtr = u;
2376 else
2377 rc = VERR_CFGM_INTEGER_TOO_BIG;
2378 }
2379 return rc;
2380}
2381
2382
2383/**
2384 * Query Guest Context unsigned pointer value with default.
2385 *
2386 * @returns VBox status code.
2387 * @param pNode Which node to search for pszName in.
2388 * @param pszName Name of an integer value.
2389 * @param pGCPtr Where to store the value. Set to default on failure.
2390 * @param GCPtrDef The default value.
2391 */
2392VMMR3DECL(int) CFGMR3QueryGCPtrUDef(PCFGMNODE pNode, const char *pszName, PRTGCUINTPTR pGCPtr, RTGCUINTPTR GCPtrDef)
2393{
2394 uint64_t u64;
2395 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, GCPtrDef);
2396 if (RT_SUCCESS(rc))
2397 {
2398 RTGCUINTPTR u = (RTGCUINTPTR)u64;
2399 if (u64 == u)
2400 *pGCPtr = u;
2401 else
2402 rc = VERR_CFGM_INTEGER_TOO_BIG;
2403 }
2404 return rc;
2405}
2406
2407
2408/**
2409 * Query Guest Context signed pointer value.
2410 *
2411 * @returns VBox status code.
2412 * @param pNode Which node to search for pszName in.
2413 * @param pszName Name of an integer value.
2414 * @param pGCPtr Where to store the value.
2415 */
2416VMMR3DECL(int) CFGMR3QueryGCPtrS(PCFGMNODE pNode, const char *pszName, PRTGCINTPTR pGCPtr)
2417{
2418 uint64_t u64;
2419 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
2420 if (RT_SUCCESS(rc))
2421 {
2422 RTGCINTPTR u = (RTGCINTPTR)u64;
2423 if (u64 == (uint64_t)u)
2424 *pGCPtr = u;
2425 else
2426 rc = VERR_CFGM_INTEGER_TOO_BIG;
2427 }
2428 return rc;
2429}
2430
2431
2432/**
2433 * Query Guest Context signed pointer value with default.
2434 *
2435 * @returns VBox status code.
2436 * @param pNode Which node to search for pszName in.
2437 * @param pszName Name of an integer value.
2438 * @param pGCPtr Where to store the value. Set to default on failure.
2439 * @param GCPtrDef The default value.
2440 */
2441VMMR3DECL(int) CFGMR3QueryGCPtrSDef(PCFGMNODE pNode, const char *pszName, PRTGCINTPTR pGCPtr, RTGCINTPTR GCPtrDef)
2442{
2443 uint64_t u64;
2444 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, GCPtrDef);
2445 if (RT_SUCCESS(rc))
2446 {
2447 RTGCINTPTR u = (RTGCINTPTR)u64;
2448 if (u64 == (uint64_t)u)
2449 *pGCPtr = u;
2450 else
2451 rc = VERR_CFGM_INTEGER_TOO_BIG;
2452 }
2453 return rc;
2454}
2455
2456
2457/**
2458 * Query zero terminated character value storing it in a
2459 * buffer allocated from the MM heap.
2460 *
2461 * @returns VBox status code.
2462 * @param pNode Which node to search for pszName in.
2463 * @param pszName Value name. This value must be of zero terminated character string type.
2464 * @param ppszString Where to store the string pointer.
2465 * Free this using MMR3HeapFree().
2466 */
2467VMMR3DECL(int) CFGMR3QueryStringAlloc(PCFGMNODE pNode, const char *pszName, char **ppszString)
2468{
2469 size_t cbString;
2470 int rc = CFGMR3QuerySize(pNode, pszName, &cbString);
2471 if (RT_SUCCESS(rc))
2472 {
2473 char *pszString = (char *)MMR3HeapAlloc(pNode->pVM, MM_TAG_CFGM_USER, cbString);
2474 if (pszString)
2475 {
2476 rc = CFGMR3QueryString(pNode, pszName, pszString, cbString);
2477 if (RT_SUCCESS(rc))
2478 *ppszString = pszString;
2479 else
2480 MMR3HeapFree(pszString);
2481 }
2482 else
2483 rc = VERR_NO_MEMORY;
2484 }
2485 return rc;
2486}
2487
2488
2489/**
2490 * Query zero terminated character value storing it in a
2491 * buffer allocated from the MM heap.
2492 *
2493 * @returns VBox status code.
2494 * @param pNode Which node to search for pszName in.
2495 * @param pszName Value name. This value must be of zero terminated character string type.
2496 * @param ppszString Where to store the string pointer. Not set on failure.
2497 * Free this using MMR3HeapFree().
2498 */
2499VMMR3DECL(int) CFGMR3QueryStringAllocDef(PCFGMNODE pNode, const char *pszName, char **ppszString, const char *pszDef)
2500{
2501 size_t cbString;
2502 int rc = CFGMR3QuerySize(pNode, pszName, &cbString);
2503 if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
2504 {
2505 cbString = strlen(pszDef) + 1;
2506 rc = VINF_SUCCESS;
2507 }
2508 if (RT_SUCCESS(rc))
2509 {
2510 char *pszString = (char *)MMR3HeapAlloc(pNode->pVM, MM_TAG_CFGM_USER, cbString);
2511 if (pszString)
2512 {
2513 rc = CFGMR3QueryStringDef(pNode, pszName, pszString, cbString, pszDef);
2514 if (RT_SUCCESS(rc))
2515 *ppszString = pszString;
2516 else
2517 MMR3HeapFree(pszString);
2518 }
2519 else
2520 rc = VERR_NO_MEMORY;
2521 }
2522 return rc;
2523}
2524
2525
2526/**
2527 * Dumps the configuration (sub)tree to the release log.
2528 *
2529 * @param pRoot The root node of the dump.
2530 */
2531VMMR3DECL(void) CFGMR3Dump(PCFGMNODE pRoot)
2532{
2533 LogRel(("************************* CFGM dump *************************\n"));
2534 cfgmR3Dump(pRoot, 0, DBGFR3InfoLogRelHlp());
2535 LogRel(("********************* End of CFGM dump **********************\n"));
2536}
2537
2538
2539/**
2540 * Info handler, internal version.
2541 *
2542 * @param pVM The VM handle.
2543 * @param pHlp Callback functions for doing output.
2544 * @param pszArgs Argument string. Optional and specific to the handler.
2545 */
2546static DECLCALLBACK(void) cfgmR3Info(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
2547{
2548 /*
2549 * Figure where to start.
2550 */
2551 PCFGMNODE pRoot = pVM->cfgm.s.pRoot;
2552 if (pszArgs && *pszArgs)
2553 {
2554 int rc = cfgmR3ResolveNode(pRoot, pszArgs, &pRoot);
2555 if (RT_FAILURE(rc))
2556 {
2557 pHlp->pfnPrintf(pHlp, "Failed to resolve CFGM path '%s', %Rrc", pszArgs, rc);
2558 return;
2559 }
2560 }
2561
2562 /*
2563 * Dump the specified tree.
2564 */
2565 pHlp->pfnPrintf(pHlp, "pRoot=%p:{", pRoot);
2566 cfgmR3DumpPath(pRoot, pHlp);
2567 pHlp->pfnPrintf(pHlp, "}\n");
2568 cfgmR3Dump(pRoot, 0, pHlp);
2569}
2570
2571
2572/**
2573 * Recursivly prints a path name.
2574 */
2575static void cfgmR3DumpPath(PCFGMNODE pNode, PCDBGFINFOHLP pHlp)
2576{
2577 if (pNode->pParent)
2578 cfgmR3DumpPath(pNode->pParent, pHlp);
2579 pHlp->pfnPrintf(pHlp, "%s/", pNode->szName);
2580}
2581
2582
2583/**
2584 * Dumps a branch of a tree.
2585 */
2586static void cfgmR3Dump(PCFGMNODE pRoot, unsigned iLevel, PCDBGFINFOHLP pHlp)
2587{
2588 /*
2589 * Path.
2590 */
2591 pHlp->pfnPrintf(pHlp, "[");
2592 cfgmR3DumpPath(pRoot, pHlp);
2593 pHlp->pfnPrintf(pHlp, "] (level %d)%s\n", iLevel, pRoot->fRestrictedRoot ? " (restricted root)" : "");
2594
2595 /*
2596 * Values.
2597 */
2598 PCFGMLEAF pLeaf;
2599 size_t cchMax = 0;
2600 for (pLeaf = CFGMR3GetFirstValue(pRoot); pLeaf; pLeaf = CFGMR3GetNextValue(pLeaf))
2601 cchMax = RT_MAX(cchMax, pLeaf->cchName);
2602 for (pLeaf = CFGMR3GetFirstValue(pRoot); pLeaf; pLeaf = CFGMR3GetNextValue(pLeaf))
2603 {
2604 switch (CFGMR3GetValueType(pLeaf))
2605 {
2606 case CFGMVALUETYPE_INTEGER:
2607 pHlp->pfnPrintf(pHlp, " %-*s <integer> = %#018llx (%lld)\n", (int)cchMax, pLeaf->szName, pLeaf->Value.Integer.u64, pLeaf->Value.Integer.u64);
2608 break;
2609
2610 case CFGMVALUETYPE_STRING:
2611 pHlp->pfnPrintf(pHlp, " %-*s <string> = \"%s\" (cb=%zu)\n", (int)cchMax, pLeaf->szName, pLeaf->Value.String.psz, pLeaf->Value.String.cb);
2612 break;
2613
2614 case CFGMVALUETYPE_BYTES:
2615 pHlp->pfnPrintf(pHlp, " %-*s <bytes> = \"%.*Rhxs\" (cb=%zu)\n", (int)cchMax, pLeaf->szName, pLeaf->Value.Bytes.cb, pLeaf->Value.Bytes.pau8, pLeaf->Value.Bytes.cb);
2616 break;
2617
2618 default:
2619 AssertMsgFailed(("bad leaf!\n"));
2620 break;
2621 }
2622 }
2623 pHlp->pfnPrintf(pHlp, "\n");
2624
2625 /*
2626 * Children.
2627 */
2628 for (PCFGMNODE pChild = CFGMR3GetFirstChild(pRoot); pChild; pChild = CFGMR3GetNextChild(pChild))
2629 {
2630 Assert(pChild->pNext != pChild);
2631 Assert(pChild->pPrev != pChild);
2632 Assert(pChild->pPrev != pChild->pNext || !pChild->pPrev);
2633 Assert(pChild->pFirstChild != pChild);
2634 Assert(pChild->pParent != pChild);
2635 cfgmR3Dump(pChild, iLevel + 1, pHlp);
2636 }
2637}
2638
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