VirtualBox

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

Last change on this file since 74795 was 73912, checked in by vboxsync, 6 years ago

CFGM: comment indent fix.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 96.6 KB
Line 
1/* $Id: CFGM.cpp 73912 2018-08-27 12:19:32Z vboxsync $ */
2/** @file
3 * CFGM - Configuration Manager.
4 */
5
6/*
7 * Copyright (C) 2006-2017 Oracle Corporation
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
18/** @page pg_cfgm CFGM - The Configuration Manager
19 *
20 * The configuration manager is a directory containing the VM configuration at
21 * run time. It works in a manner similar to the windows registry - it's like a
22 * file system hierarchy, but the files (values) live in a separate name space
23 * and can include the path separators.
24 *
25 * The configuration is normally created via a callback passed to VMR3Create()
26 * via the pfnCFGMConstructor parameter. To make testcase writing a bit simpler,
27 * we allow the callback to be NULL, in which case a simple default
28 * configuration will be created by CFGMR3ConstructDefaultTree(). The
29 * Console::configConstructor() method in Main/ConsoleImpl2.cpp creates the
30 * configuration from the XML.
31 *
32 * Devices, drivers, services and other PDM stuff are given their own subtree
33 * where they are protected from accessing information of any parents. This is
34 * is implemented via the CFGMR3SetRestrictedRoot() API.
35 *
36 * Data validation beyond the basic primitives is left to the caller. The caller
37 * is in a better position to know the proper validation rules of the individual
38 * properties.
39 *
40 * @see grp_cfgm
41 *
42 *
43 * @section sec_cfgm_primitives Data Primitives
44 *
45 * CFGM supports the following data primitives:
46 * - Integers. Representation is unsigned 64-bit. Boolean, unsigned and
47 * small integers, and pointers are all represented using this primitive.
48 * - Zero terminated character strings. These are of course UTF-8.
49 * - Variable length byte strings. This can be used to get/put binary
50 * objects like for instance RTMAC.
51 *
52 */
53
54
55/*********************************************************************************************************************************
56* Header Files *
57*********************************************************************************************************************************/
58#define LOG_GROUP LOG_GROUP_CFGM
59#include <VBox/vmm/cfgm.h>
60#include <VBox/vmm/dbgf.h>
61#include <VBox/vmm/mm.h>
62#include "CFGMInternal.h"
63#include <VBox/vmm/vm.h>
64#include <VBox/vmm/uvm.h>
65#include <VBox/err.h>
66
67#include <VBox/log.h>
68#include <iprt/assert.h>
69#include <iprt/mem.h>
70#include <iprt/param.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(PVM pVM, PCFGMLEAF pLeaf);
86
87
88/** @todo replace pVM for pUVM !*/
89
90/**
91 * Allocator wrapper.
92 *
93 * @returns Pointer to the allocated memory, NULL on failure.
94 * @param pVM The cross context VM structure, if the tree
95 * is associated with one.
96 * @param enmTag The allocation tag.
97 * @param cb The size of the allocation.
98 */
99static void *cfgmR3MemAlloc(PVM pVM, MMTAG enmTag, size_t cb)
100{
101 if (pVM)
102 return MMR3HeapAlloc(pVM, enmTag, cb);
103 return RTMemAlloc(cb);
104}
105
106
107/**
108 * Free wrapper.
109 *
110 * @returns Pointer to the allocated memory, NULL on failure.
111 * @param pVM The cross context VM structure, if the tree
112 * is associated with one.
113 * @param pv The memory block to free.
114 */
115static void cfgmR3MemFree(PVM pVM, void *pv)
116{
117 if (pVM)
118 MMR3HeapFree(pv);
119 else
120 RTMemFree(pv);
121}
122
123
124/**
125 * String allocator wrapper.
126 *
127 * @returns Pointer to the allocated memory, NULL on failure.
128 * @param pVM The cross context VM structure, if the tree
129 * is associated with one.
130 * @param enmTag The allocation tag.
131 * @param cbString The size of the allocation, terminator included.
132 */
133static char *cfgmR3StrAlloc(PVM pVM, MMTAG enmTag, size_t cbString)
134{
135 if (pVM)
136 return (char *)MMR3HeapAlloc(pVM, enmTag, cbString);
137 return (char *)RTStrAlloc(cbString);
138}
139
140
141/**
142 * String free wrapper.
143 *
144 * @returns Pointer to the allocated memory, NULL on failure.
145 * @param pVM The cross context VM structure, if the tree
146 * is associated with one.
147 * @param pszString The memory block to free.
148 */
149static void cfgmR3StrFree(PVM pVM, char *pszString)
150{
151 if (pVM)
152 MMR3HeapFree(pszString);
153 else
154 RTStrFree(pszString);
155}
156
157
158/**
159 * Frees one node, leaving any children or leaves to the caller.
160 *
161 * @param pNode The node structure to free.
162 */
163static void cfgmR3FreeNodeOnly(PCFGMNODE pNode)
164{
165 pNode->pFirstLeaf = NULL;
166 pNode->pFirstChild = NULL;
167 pNode->pNext = NULL;
168 pNode->pPrev = NULL;
169 if (!pNode->pVM)
170 RTMemFree(pNode);
171 else
172 {
173 pNode->pVM = NULL;
174 MMR3HeapFree(pNode);
175 }
176}
177
178
179
180
181/**
182 * Constructs the configuration for the VM.
183 *
184 * This should only be called used once.
185 *
186 * @returns VBox status code.
187 * @param pVM The cross context VM structure.
188 * @param pfnCFGMConstructor Pointer to callback function for constructing
189 * the VM configuration tree. This is called on
190 * the EMT.
191 * @param pvUser The user argument passed to pfnCFGMConstructor.
192 * @thread EMT.
193 * @internal
194 */
195VMMR3DECL(int) CFGMR3Init(PVM pVM, PFNCFGMCONSTRUCTOR pfnCFGMConstructor, void *pvUser)
196{
197 LogFlow(("CFGMR3Init: pfnCFGMConstructor=%p pvUser=%p\n", pfnCFGMConstructor, pvUser));
198
199 /*
200 * Init data members.
201 */
202 pVM->cfgm.s.pRoot = NULL;
203
204 /*
205 * Register DBGF into item.
206 */
207 int rc = DBGFR3InfoRegisterInternal(pVM, "cfgm", "Dumps a part of the CFGM tree. The argument indicates where to start.",
208 cfgmR3Info);
209 AssertRCReturn(rc,rc);
210
211 /*
212 * Root Node.
213 */
214 PCFGMNODE pRoot = (PCFGMNODE)MMR3HeapAllocZ(pVM, MM_TAG_CFGM, sizeof(*pRoot));
215 if (!pRoot)
216 return VERR_NO_MEMORY;
217 pRoot->pVM = pVM;
218 pRoot->cchName = 0;
219 pVM->cfgm.s.pRoot = pRoot;
220
221 /*
222 * Call the constructor if specified, if not use the default one.
223 */
224 if (pfnCFGMConstructor)
225 rc = pfnCFGMConstructor(pVM->pUVM, pVM, pvUser);
226 else
227 rc = CFGMR3ConstructDefaultTree(pVM);
228 if (RT_SUCCESS(rc))
229 {
230 Log(("CFGMR3Init: Successfully constructed the configuration\n"));
231 CFGMR3Dump(CFGMR3GetRoot(pVM));
232 }
233 else
234 AssertMsgFailed(("Constructor failed with rc=%Rrc pfnCFGMConstructor=%p\n", rc, pfnCFGMConstructor));
235
236 return rc;
237}
238
239
240/**
241 * Terminates the configuration manager.
242 *
243 * @returns VBox status code.
244 * @param pVM The cross context VM structure.
245 * @internal
246 */
247VMMR3DECL(int) CFGMR3Term(PVM pVM)
248{
249 CFGMR3RemoveNode(pVM->cfgm.s.pRoot);
250 pVM->cfgm.s.pRoot = NULL;
251 return 0;
252}
253
254
255/**
256 * Gets the root node for the VM.
257 *
258 * @returns Pointer to root node.
259 * @param pVM The cross context VM structure.
260 */
261VMMR3DECL(PCFGMNODE) CFGMR3GetRoot(PVM pVM)
262{
263 return pVM->cfgm.s.pRoot;
264}
265
266
267/**
268 * Gets the root node for the VM.
269 *
270 * @returns Pointer to root node.
271 * @param pUVM The user mode VM structure.
272 */
273VMMR3DECL(PCFGMNODE) CFGMR3GetRootU(PUVM pUVM)
274{
275 UVM_ASSERT_VALID_EXT_RETURN(pUVM, NULL);
276 PVM pVM = pUVM->pVM;
277 AssertReturn(pVM, NULL);
278 return pVM->cfgm.s.pRoot;
279}
280
281
282/**
283 * Gets the parent of a CFGM node.
284 *
285 * @returns Pointer to the parent node.
286 * @returns NULL if pNode is Root or pNode is the start of a
287 * restricted subtree (use CFGMR3GetParentEx() for that).
288 *
289 * @param pNode The node which parent we query.
290 */
291VMMR3DECL(PCFGMNODE) CFGMR3GetParent(PCFGMNODE pNode)
292{
293 if (pNode && !pNode->fRestrictedRoot)
294 return pNode->pParent;
295 return NULL;
296}
297
298
299/**
300 * Gets the parent of a CFGM node.
301 *
302 * @returns Pointer to the parent node.
303 * @returns NULL if pNode is Root or pVM is not correct.
304 *
305 * @param pVM The cross context VM structure. Used as token that
306 * the caller is trusted.
307 * @param pNode The node which parent we query.
308 */
309VMMR3DECL(PCFGMNODE) CFGMR3GetParentEx(PVM pVM, PCFGMNODE pNode)
310{
311 if (pNode && pNode->pVM == pVM)
312 return pNode->pParent;
313 return NULL;
314}
315
316
317/**
318 * Query a child node.
319 *
320 * @returns Pointer to the specified node.
321 * @returns NULL if node was not found or pNode is NULL.
322 * @param pNode Node pszPath is relative to.
323 * @param pszPath Path to the child node or pNode.
324 * It's good style to end this with '/'.
325 */
326VMMR3DECL(PCFGMNODE) CFGMR3GetChild(PCFGMNODE pNode, const char *pszPath)
327{
328 PCFGMNODE pChild;
329 int rc = cfgmR3ResolveNode(pNode, pszPath, &pChild);
330 if (RT_SUCCESS(rc))
331 return pChild;
332 return NULL;
333}
334
335
336/**
337 * Query a child node by a format string.
338 *
339 * @returns Pointer to the specified node.
340 * @returns NULL if node was not found or pNode is NULL.
341 * @param pNode Node pszPath is relative to.
342 * @param pszPathFormat Path to the child node or pNode.
343 * It's good style to end this with '/'.
344 * @param ... Arguments to pszPathFormat.
345 */
346VMMR3DECL(PCFGMNODE) CFGMR3GetChildF(PCFGMNODE pNode, const char *pszPathFormat, ...)
347{
348 va_list Args;
349 va_start(Args, pszPathFormat);
350 PCFGMNODE pRet = CFGMR3GetChildFV(pNode, pszPathFormat, Args);
351 va_end(Args);
352 return pRet;
353}
354
355
356/**
357 * Query a child node by a format string.
358 *
359 * @returns Pointer to the specified node.
360 * @returns NULL if node was not found or pNode is NULL.
361 * @param pNode Node pszPath is relative to.
362 * @param pszPathFormat Path to the child node or pNode.
363 * It's good style to end this with '/'.
364 * @param Args Arguments to pszPathFormat.
365 */
366VMMR3DECL(PCFGMNODE) CFGMR3GetChildFV(PCFGMNODE pNode, const char *pszPathFormat, va_list Args)
367{
368 char *pszPath;
369 RTStrAPrintfV(&pszPath, pszPathFormat, Args);
370 if (pszPath)
371 {
372 PCFGMNODE pChild;
373 int rc = cfgmR3ResolveNode(pNode, pszPath, &pChild);
374 RTStrFree(pszPath);
375 if (RT_SUCCESS(rc))
376 return pChild;
377 }
378 return NULL;
379}
380
381
382/**
383 * Gets the first child node.
384 * Use this to start an enumeration of child nodes.
385 *
386 * @returns Pointer to the first child.
387 * @returns NULL if no children.
388 * @param pNode Node to enumerate children for.
389 */
390VMMR3DECL(PCFGMNODE) CFGMR3GetFirstChild(PCFGMNODE pNode)
391{
392 return pNode ? pNode->pFirstChild : NULL;
393}
394
395
396/**
397 * Gets the next sibling node.
398 * Use this to continue an enumeration.
399 *
400 * @returns Pointer to the first child.
401 * @returns NULL if no children.
402 * @param pCur Node to returned by a call to CFGMR3GetFirstChild()
403 * or successive calls to this function.
404 */
405VMMR3DECL(PCFGMNODE) CFGMR3GetNextChild(PCFGMNODE pCur)
406{
407 return pCur ? pCur->pNext : NULL;
408}
409
410
411/**
412 * Gets the name of the current node.
413 * (Needed for enumeration.)
414 *
415 * @returns VBox status code.
416 * @param pCur Node to returned by a call to CFGMR3GetFirstChild()
417 * or successive calls to CFGMR3GetNextChild().
418 * @param pszName Where to store the node name.
419 * @param cchName Size of the buffer pointed to by pszName (with terminator).
420 */
421VMMR3DECL(int) CFGMR3GetName(PCFGMNODE pCur, char *pszName, size_t cchName)
422{
423 int rc;
424 if (pCur)
425 {
426 if (cchName > pCur->cchName)
427 {
428 rc = VINF_SUCCESS;
429 memcpy(pszName, pCur->szName, pCur->cchName + 1);
430 }
431 else
432 rc = VERR_CFGM_NOT_ENOUGH_SPACE;
433 }
434 else
435 rc = VERR_CFGM_NO_NODE;
436 return rc;
437}
438
439
440/**
441 * Gets the length of the current node's name.
442 * (Needed for enumeration.)
443 *
444 * @returns Node name length in bytes including the terminating null char.
445 * @returns 0 if pCur is NULL.
446 * @param pCur Node to returned by a call to CFGMR3GetFirstChild()
447 * or successive calls to CFGMR3GetNextChild().
448 */
449VMMR3DECL(size_t) CFGMR3GetNameLen(PCFGMNODE pCur)
450{
451 return pCur ? pCur->cchName + 1 : 0;
452}
453
454
455/**
456 * Validates that the child nodes are within a set of valid names.
457 *
458 * @returns true if all names are found in pszzAllowed.
459 * @returns false if not.
460 * @param pNode The node which children should be examined.
461 * @param pszzValid List of valid names separated by '\\0' and ending with
462 * a double '\\0'.
463 *
464 * @deprecated Use CFGMR3ValidateConfig.
465 */
466VMMR3DECL(bool) CFGMR3AreChildrenValid(PCFGMNODE pNode, const char *pszzValid)
467{
468 if (pNode)
469 {
470 for (PCFGMNODE pChild = pNode->pFirstChild; pChild; pChild = pChild->pNext)
471 {
472 /* search pszzValid for the name */
473 const char *psz = pszzValid;
474 while (*psz)
475 {
476 size_t cch = strlen(psz);
477 if ( cch == pChild->cchName
478 && !memcmp(psz, pChild->szName, cch))
479 break;
480
481 /* next */
482 psz += cch + 1;
483 }
484
485 /* if at end of pszzValid we didn't find it => failure */
486 if (!*psz)
487 {
488 AssertMsgFailed(("Couldn't find '%s' in the valid values\n", pChild->szName));
489 return false;
490 }
491 }
492 }
493
494 /* all ok. */
495 return true;
496}
497
498
499/**
500 * Gets the first value of a node.
501 * Use this to start an enumeration of values.
502 *
503 * @returns Pointer to the first value.
504 * @param pCur The node (Key) which values to enumerate.
505 */
506VMMR3DECL(PCFGMLEAF) CFGMR3GetFirstValue(PCFGMNODE pCur)
507{
508 return pCur ? pCur->pFirstLeaf : NULL;
509}
510
511/**
512 * Gets the next value in enumeration.
513 *
514 * @returns Pointer to the next value.
515 * @param pCur The current value as returned by this function or CFGMR3GetFirstValue().
516 */
517VMMR3DECL(PCFGMLEAF) CFGMR3GetNextValue(PCFGMLEAF pCur)
518{
519 return pCur ? pCur->pNext : NULL;
520}
521
522/**
523 * Get the value name.
524 * (Needed for enumeration.)
525 *
526 * @returns VBox status code.
527 * @param pCur Value returned by a call to CFGMR3GetFirstValue()
528 * or successive calls to CFGMR3GetNextValue().
529 * @param pszName Where to store the value name.
530 * @param cchName Size of the buffer pointed to by pszName (with terminator).
531 */
532VMMR3DECL(int) CFGMR3GetValueName(PCFGMLEAF pCur, char *pszName, size_t cchName)
533{
534 int rc;
535 if (pCur)
536 {
537 if (cchName > pCur->cchName)
538 {
539 rc = VINF_SUCCESS;
540 memcpy(pszName, pCur->szName, pCur->cchName + 1);
541 }
542 else
543 rc = VERR_CFGM_NOT_ENOUGH_SPACE;
544 }
545 else
546 rc = VERR_CFGM_NO_NODE;
547 return rc;
548}
549
550
551/**
552 * Gets the length of the current node's name.
553 * (Needed for enumeration.)
554 *
555 * @returns Value name length in bytes including the terminating null char.
556 * @returns 0 if pCur is NULL.
557 * @param pCur Value returned by a call to CFGMR3GetFirstValue()
558 * or successive calls to CFGMR3GetNextValue().
559 */
560VMMR3DECL(size_t) CFGMR3GetValueNameLen(PCFGMLEAF pCur)
561{
562 return pCur ? pCur->cchName + 1 : 0;
563}
564
565
566/**
567 * Gets the value type.
568 * (For enumeration.)
569 *
570 * @returns VBox status code.
571 * @param pCur Value returned by a call to CFGMR3GetFirstValue()
572 * or successive calls to CFGMR3GetNextValue().
573 */
574VMMR3DECL(CFGMVALUETYPE) CFGMR3GetValueType(PCFGMLEAF pCur)
575{
576 Assert(pCur);
577 return pCur->enmType;
578}
579
580
581/**
582 * Validates that the values are within a set of valid names.
583 *
584 * @returns true if all names are found in pszzValid.
585 * @returns false if not.
586 * @param pNode The node which values should be examined.
587 * @param pszzValid List of valid names separated by '\\0' and ending with
588 * a double '\\0'.
589 * @deprecated Use CFGMR3ValidateConfig.
590 */
591VMMR3DECL(bool) CFGMR3AreValuesValid(PCFGMNODE pNode, const char *pszzValid)
592{
593 if (pNode)
594 {
595 for (PCFGMLEAF pLeaf = pNode->pFirstLeaf; pLeaf; pLeaf = pLeaf->pNext)
596 {
597 /* search pszzValid for the name */
598 const char *psz = pszzValid;
599 while (*psz)
600 {
601 size_t cch = strlen(psz);
602 if ( cch == pLeaf->cchName
603 && !memcmp(psz, pLeaf->szName, cch))
604 break;
605
606 /* next */
607 psz += cch + 1;
608 }
609
610 /* if at end of pszzValid we didn't find it => failure */
611 if (!*psz)
612 {
613 AssertMsgFailed(("Couldn't find '%s' in the valid values\n", pLeaf->szName));
614 return false;
615 }
616 }
617 }
618
619 /* all ok. */
620 return true;
621}
622
623
624/**
625 * Checks if the given value exists.
626 *
627 * @returns true if it exists, false if not.
628 * @param pNode Which node to search for pszName in.
629 * @param pszName The name of the value we seek.
630 */
631VMMR3DECL(bool) CFGMR3Exists(PCFGMNODE pNode, const char *pszName)
632{
633 PCFGMLEAF pLeaf;
634 int rc = cfgmR3ResolveLeaf(pNode, pszName, &pLeaf);
635 return RT_SUCCESS_NP(rc);
636}
637
638
639/**
640 * Query value type.
641 *
642 * @returns VBox status code.
643 * @param pNode Which node to search for pszName in.
644 * @param pszName Name of an integer value.
645 * @param penmType Where to store the type.
646 */
647VMMR3DECL(int) CFGMR3QueryType(PCFGMNODE pNode, const char *pszName, PCFGMVALUETYPE penmType)
648{
649 PCFGMLEAF pLeaf;
650 int rc = cfgmR3ResolveLeaf(pNode, pszName, &pLeaf);
651 if (RT_SUCCESS(rc))
652 {
653 if (penmType)
654 *penmType = pLeaf->enmType;
655 }
656 return rc;
657}
658
659
660/**
661 * Query value size.
662 * This works on all types of values.
663 *
664 * @returns VBox status code.
665 * @param pNode Which node to search for pszName in.
666 * @param pszName Name of an integer value.
667 * @param pcb Where to store the value size.
668 */
669VMMR3DECL(int) CFGMR3QuerySize(PCFGMNODE pNode, const char *pszName, size_t *pcb)
670{
671 PCFGMLEAF pLeaf;
672 int rc = cfgmR3ResolveLeaf(pNode, pszName, &pLeaf);
673 if (RT_SUCCESS(rc))
674 {
675 switch (pLeaf->enmType)
676 {
677 case CFGMVALUETYPE_INTEGER:
678 *pcb = sizeof(pLeaf->Value.Integer.u64);
679 break;
680
681 case CFGMVALUETYPE_STRING:
682 *pcb = pLeaf->Value.String.cb;
683 break;
684
685 case CFGMVALUETYPE_BYTES:
686 *pcb = pLeaf->Value.Bytes.cb;
687 break;
688
689 default:
690 rc = VERR_CFGM_IPE_1;
691 AssertMsgFailed(("Invalid value type %d\n", pLeaf->enmType));
692 break;
693 }
694 }
695 return rc;
696}
697
698
699/**
700 * Query integer value.
701 *
702 * @returns VBox status code.
703 * @param pNode Which node to search for pszName in.
704 * @param pszName Name of an integer value.
705 * @param pu64 Where to store the integer value.
706 */
707VMMR3DECL(int) CFGMR3QueryInteger(PCFGMNODE pNode, const char *pszName, uint64_t *pu64)
708{
709 PCFGMLEAF pLeaf;
710 int rc = cfgmR3ResolveLeaf(pNode, pszName, &pLeaf);
711 if (RT_SUCCESS(rc))
712 {
713 if (pLeaf->enmType == CFGMVALUETYPE_INTEGER)
714 *pu64 = pLeaf->Value.Integer.u64;
715 else
716 rc = VERR_CFGM_NOT_INTEGER;
717 }
718 return rc;
719}
720
721
722/**
723 * Query integer value with default.
724 *
725 * @returns VBox status code.
726 * @param pNode Which node to search for pszName in.
727 * @param pszName Name of an integer value.
728 * @param pu64 Where to store the integer value. This is set to the default on failure.
729 * @param u64Def The default value. This is always set.
730 */
731VMMR3DECL(int) CFGMR3QueryIntegerDef(PCFGMNODE pNode, const char *pszName, uint64_t *pu64, uint64_t u64Def)
732{
733 PCFGMLEAF pLeaf;
734 int rc = cfgmR3ResolveLeaf(pNode, pszName, &pLeaf);
735 if (RT_SUCCESS(rc))
736 {
737 if (pLeaf->enmType == CFGMVALUETYPE_INTEGER)
738 *pu64 = pLeaf->Value.Integer.u64;
739 else
740 rc = VERR_CFGM_NOT_INTEGER;
741 }
742
743 if (RT_FAILURE(rc))
744 {
745 *pu64 = u64Def;
746 if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
747 rc = VINF_SUCCESS;
748 }
749
750 return rc;
751}
752
753
754/**
755 * Query zero terminated character value.
756 *
757 * @returns VBox status code.
758 * @param pNode Which node to search for pszName in.
759 * @param pszName Name of a zero terminate character value.
760 * @param pszString Where to store the string.
761 * @param cchString Size of the string buffer. (Includes terminator.)
762 */
763VMMR3DECL(int) CFGMR3QueryString(PCFGMNODE pNode, const char *pszName, char *pszString, size_t cchString)
764{
765 PCFGMLEAF pLeaf;
766 int rc = cfgmR3ResolveLeaf(pNode, pszName, &pLeaf);
767 if (RT_SUCCESS(rc))
768 {
769 if (pLeaf->enmType == CFGMVALUETYPE_STRING)
770 {
771 size_t cbSrc = pLeaf->Value.String.cb;
772 if (cchString >= cbSrc)
773 {
774 memcpy(pszString, pLeaf->Value.String.psz, cbSrc);
775 memset(pszString + cbSrc, 0, cchString - cbSrc);
776 }
777 else
778 rc = VERR_CFGM_NOT_ENOUGH_SPACE;
779 }
780 else
781 rc = VERR_CFGM_NOT_STRING;
782 }
783 return rc;
784}
785
786
787/**
788 * Query zero terminated character value with default.
789 *
790 * @returns VBox status code.
791 * @param pNode Which node to search for pszName in.
792 * @param pszName Name of a zero terminate character value.
793 * @param pszString Where to store the string. This will not be set on overflow error.
794 * @param cchString Size of the string buffer. (Includes terminator.)
795 * @param pszDef The default value.
796 */
797VMMR3DECL(int) CFGMR3QueryStringDef(PCFGMNODE pNode, const char *pszName, char *pszString, size_t cchString, const char *pszDef)
798{
799 PCFGMLEAF pLeaf;
800 int rc = cfgmR3ResolveLeaf(pNode, pszName, &pLeaf);
801 if (RT_SUCCESS(rc))
802 {
803 if (pLeaf->enmType == CFGMVALUETYPE_STRING)
804 {
805 size_t cbSrc = pLeaf->Value.String.cb;
806 if (cchString >= cbSrc)
807 {
808 memcpy(pszString, pLeaf->Value.String.psz, cbSrc);
809 memset(pszString + cbSrc, 0, cchString - cbSrc);
810 }
811 else
812 rc = VERR_CFGM_NOT_ENOUGH_SPACE;
813 }
814 else
815 rc = VERR_CFGM_NOT_STRING;
816 }
817
818 if (RT_FAILURE(rc) && rc != VERR_CFGM_NOT_ENOUGH_SPACE)
819 {
820 size_t cchDef = strlen(pszDef);
821 if (cchString > cchDef)
822 {
823 memcpy(pszString, pszDef, cchDef);
824 memset(pszString + cchDef, 0, cchString - cchDef);
825 if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
826 rc = VINF_SUCCESS;
827 }
828 else if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
829 rc = VERR_CFGM_NOT_ENOUGH_SPACE;
830 }
831
832 return rc;
833}
834
835
836/**
837 * Query byte string value.
838 *
839 * @returns VBox status code.
840 * @param pNode Which node to search for pszName in.
841 * @param pszName Name of a byte string value.
842 * @param pvData Where to store the binary data.
843 * @param cbData Size of buffer pvData points too.
844 */
845VMMR3DECL(int) CFGMR3QueryBytes(PCFGMNODE pNode, const char *pszName, void *pvData, size_t cbData)
846{
847 PCFGMLEAF pLeaf;
848 int rc = cfgmR3ResolveLeaf(pNode, pszName, &pLeaf);
849 if (RT_SUCCESS(rc))
850 {
851 if (pLeaf->enmType == CFGMVALUETYPE_BYTES)
852 {
853 if (cbData >= pLeaf->Value.Bytes.cb)
854 {
855 memcpy(pvData, pLeaf->Value.Bytes.pau8, pLeaf->Value.Bytes.cb);
856 memset((char *)pvData + pLeaf->Value.Bytes.cb, 0, cbData - pLeaf->Value.Bytes.cb);
857 }
858 else
859 rc = VERR_CFGM_NOT_ENOUGH_SPACE;
860 }
861 else
862 rc = VERR_CFGM_NOT_BYTES;
863 }
864 return rc;
865}
866
867
868/**
869 * Validate one level of a configuration node.
870 *
871 * This replaces the CFGMR3AreChildrenValid and CFGMR3AreValuesValid APIs.
872 *
873 * @returns VBox status code.
874 *
875 * When an error is returned, both VMSetError and AssertLogRelMsgFailed
876 * have been called. So, all the caller needs to do is to propagate
877 * the error status up to PDM.
878 *
879 * @param pNode The node to validate.
880 * @param pszNode The node path, always ends with a slash. Use
881 * "/" for the root config node.
882 * @param pszValidValues Patterns describing the valid value names. See
883 * RTStrSimplePatternMultiMatch for details on the
884 * pattern syntax.
885 * @param pszValidNodes Patterns describing the valid node (key) names.
886 * See RTStrSimplePatternMultiMatch for details on
887 * the pattern syntax.
888 * @param pszWho Who is calling.
889 * @param uInstance The instance number of the caller.
890 */
891VMMR3DECL(int) CFGMR3ValidateConfig(PCFGMNODE pNode, const char *pszNode,
892 const char *pszValidValues, const char *pszValidNodes,
893 const char *pszWho, uint32_t uInstance)
894{
895 /* Input validation. */
896 AssertPtrNullReturn(pNode, VERR_INVALID_POINTER);
897 AssertPtrReturn(pszNode, VERR_INVALID_POINTER);
898 Assert(*pszNode && pszNode[strlen(pszNode) - 1] == '/');
899 AssertPtrReturn(pszValidValues, VERR_INVALID_POINTER);
900 AssertPtrReturn(pszValidNodes, VERR_INVALID_POINTER);
901 AssertPtrReturn(pszWho, VERR_INVALID_POINTER);
902
903 if (pNode)
904 {
905 /*
906 * Enumerate the leaves and check them against pszValidValues.
907 */
908 for (PCFGMLEAF pLeaf = pNode->pFirstLeaf; pLeaf; pLeaf = pLeaf->pNext)
909 {
910 if (!RTStrSimplePatternMultiMatch(pszValidValues, RTSTR_MAX,
911 pLeaf->szName, pLeaf->cchName,
912 NULL))
913 {
914 AssertLogRelMsgFailed(("%s/%u: Value '%s%s' didn't match '%s'\n",
915 pszWho, uInstance, pszNode, pLeaf->szName, pszValidValues));
916 return VMSetError(pNode->pVM, VERR_CFGM_CONFIG_UNKNOWN_VALUE, RT_SRC_POS,
917 N_("Unknown configuration value '%s%s' found in the configuration of %s instance #%u"),
918 pszNode, pLeaf->szName, pszWho, uInstance);
919 }
920
921 }
922
923 /*
924 * Enumerate the child nodes and check them against pszValidNodes.
925 */
926 for (PCFGMNODE pChild = pNode->pFirstChild; pChild; pChild = pChild->pNext)
927 {
928 if (!RTStrSimplePatternMultiMatch(pszValidNodes, RTSTR_MAX,
929 pChild->szName, pChild->cchName,
930 NULL))
931 {
932 AssertLogRelMsgFailed(("%s/%u: Node '%s%s' didn't match '%s'\n",
933 pszWho, uInstance, pszNode, pChild->szName, pszValidNodes));
934 return VMSetError(pNode->pVM, VERR_CFGM_CONFIG_UNKNOWN_NODE, RT_SRC_POS,
935 N_("Unknown configuration node '%s%s' found in the configuration of %s instance #%u"),
936 pszNode, pChild->szName, pszWho, uInstance);
937 }
938 }
939 }
940
941 /* All is well. */
942 return VINF_SUCCESS;
943}
944
945
946
947/**
948 * Populates the CFGM tree with the default configuration.
949 *
950 * This assumes an empty tree and is intended for testcases and such that only
951 * need to do very small adjustments to the config.
952 *
953 * @returns VBox status code.
954 * @param pVM The cross context VM structure.
955 * @internal
956 */
957VMMR3DECL(int) CFGMR3ConstructDefaultTree(PVM pVM)
958{
959 int rc;
960 int rcAll = VINF_SUCCESS;
961#define UPDATERC() do { if (RT_FAILURE(rc) && RT_SUCCESS(rcAll)) rcAll = rc; } while (0)
962
963 PCFGMNODE pRoot = CFGMR3GetRoot(pVM);
964 AssertReturn(pRoot, VERR_WRONG_ORDER);
965
966 /*
967 * Create VM default values.
968 */
969 rc = CFGMR3InsertString(pRoot, "Name", "Default VM");
970 UPDATERC();
971 rc = CFGMR3InsertInteger(pRoot, "RamSize", 128U * _1M);
972 UPDATERC();
973 rc = CFGMR3InsertInteger(pRoot, "RamHoleSize", 512U * _1M);
974 UPDATERC();
975 rc = CFGMR3InsertInteger(pRoot, "TimerMillies", 10);
976 UPDATERC();
977 rc = CFGMR3InsertInteger(pRoot, "RawR3Enabled", 1);
978 UPDATERC();
979 /** @todo CFGM Defaults: RawR0, PATMEnabled and CASMEnabled needs attention later. */
980 rc = CFGMR3InsertInteger(pRoot, "RawR0Enabled", 1);
981 UPDATERC();
982 rc = CFGMR3InsertInteger(pRoot, "PATMEnabled", 1);
983 UPDATERC();
984 rc = CFGMR3InsertInteger(pRoot, "CSAMEnabled", 1);
985 UPDATERC();
986
987 /*
988 * PDM.
989 */
990 PCFGMNODE pPdm;
991 rc = CFGMR3InsertNode(pRoot, "PDM", &pPdm);
992 UPDATERC();
993 PCFGMNODE pDevices = NULL;
994 rc = CFGMR3InsertNode(pPdm, "Devices", &pDevices);
995 UPDATERC();
996 rc = CFGMR3InsertInteger(pDevices, "LoadBuiltin", 1); /* boolean */
997 UPDATERC();
998 PCFGMNODE pDrivers = NULL;
999 rc = CFGMR3InsertNode(pPdm, "Drivers", &pDrivers);
1000 UPDATERC();
1001 rc = CFGMR3InsertInteger(pDrivers, "LoadBuiltin", 1); /* boolean */
1002 UPDATERC();
1003
1004
1005 /*
1006 * Devices
1007 */
1008 pDevices = NULL;
1009 rc = CFGMR3InsertNode(pRoot, "Devices", &pDevices);
1010 UPDATERC();
1011 /* device */
1012 PCFGMNODE pDev = NULL;
1013 PCFGMNODE pInst = NULL;
1014 PCFGMNODE pCfg = NULL;
1015#if 0
1016 PCFGMNODE pLunL0 = NULL;
1017 PCFGMNODE pLunL1 = NULL;
1018#endif
1019
1020 /*
1021 * PC Arch.
1022 */
1023 rc = CFGMR3InsertNode(pDevices, "pcarch", &pDev);
1024 UPDATERC();
1025 rc = CFGMR3InsertNode(pDev, "0", &pInst);
1026 UPDATERC();
1027 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
1028 UPDATERC();
1029 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
1030 UPDATERC();
1031
1032 /*
1033 * PC Bios.
1034 */
1035 rc = CFGMR3InsertNode(pDevices, "pcbios", &pDev);
1036 UPDATERC();
1037 rc = CFGMR3InsertNode(pDev, "0", &pInst);
1038 UPDATERC();
1039 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
1040 UPDATERC();
1041 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
1042 UPDATERC();
1043 rc = CFGMR3InsertString(pCfg, "BootDevice0", "IDE");
1044 UPDATERC();
1045 rc = CFGMR3InsertString(pCfg, "BootDevice1", "NONE");
1046 UPDATERC();
1047 rc = CFGMR3InsertString(pCfg, "BootDevice2", "NONE");
1048 UPDATERC();
1049 rc = CFGMR3InsertString(pCfg, "BootDevice3", "NONE");
1050 UPDATERC();
1051 rc = CFGMR3InsertString(pCfg, "HardDiskDevice", "piix3ide");
1052 UPDATERC();
1053 rc = CFGMR3InsertString(pCfg, "FloppyDevice", "");
1054 UPDATERC();
1055 RTUUID Uuid;
1056 RTUuidClear(&Uuid);
1057 rc = CFGMR3InsertBytes(pCfg, "UUID", &Uuid, sizeof(Uuid));
1058 UPDATERC();
1059
1060 /*
1061 * PCI bus.
1062 */
1063 rc = CFGMR3InsertNode(pDevices, "pci", &pDev); /* piix3 */
1064 UPDATERC();
1065 rc = CFGMR3InsertNode(pDev, "0", &pInst);
1066 UPDATERC();
1067 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
1068 UPDATERC();
1069 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
1070 UPDATERC();
1071
1072 /*
1073 * PS/2 keyboard & mouse
1074 */
1075 rc = CFGMR3InsertNode(pDevices, "pckbd", &pDev);
1076 UPDATERC();
1077 rc = CFGMR3InsertNode(pDev, "0", &pInst);
1078 UPDATERC();
1079 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
1080 UPDATERC();
1081#if 0
1082 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0);
1083 UPDATERC();
1084 rc = CFGMR3InsertString(pLunL0, "Driver", "KeyboardQueue");
1085 UPDATERC();
1086 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg);
1087 UPDATERC();
1088 rc = CFGMR3InsertInteger(pCfg, "QueueSize", 64);
1089 UPDATERC();
1090 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1);
1091 UPDATERC();
1092 rc = CFGMR3InsertString(pLunL1, "Driver", "MainKeyboard");
1093 UPDATERC();
1094 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg);
1095 UPDATERC();
1096#endif
1097#if 0
1098 rc = CFGMR3InsertNode(pInst, "LUN#1", &pLunL0);
1099 UPDATERC();
1100 rc = CFGMR3InsertString(pLunL0, "Driver", "MouseQueue");
1101 UPDATERC();
1102 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg);
1103 UPDATERC();
1104 rc = CFGMR3InsertInteger(pCfg, "QueueSize", 128);
1105 UPDATERC();
1106 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1);
1107 UPDATERC();
1108 rc = CFGMR3InsertString(pLunL1, "Driver", "MainMouse");
1109 UPDATERC();
1110 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg);
1111 UPDATERC();
1112#endif
1113
1114 /*
1115 * i8254 Programmable Interval Timer And Dummy Speaker
1116 */
1117 rc = CFGMR3InsertNode(pDevices, "i8254", &pDev);
1118 UPDATERC();
1119 rc = CFGMR3InsertNode(pDev, "0", &pInst);
1120 UPDATERC();
1121#ifdef DEBUG
1122 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
1123 UPDATERC();
1124#endif
1125 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
1126 UPDATERC();
1127
1128 /*
1129 * i8259 Programmable Interrupt Controller.
1130 */
1131 rc = CFGMR3InsertNode(pDevices, "i8259", &pDev);
1132 UPDATERC();
1133 rc = CFGMR3InsertNode(pDev, "0", &pInst);
1134 UPDATERC();
1135 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
1136 UPDATERC();
1137 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
1138 UPDATERC();
1139
1140 /*
1141 * RTC MC146818.
1142 */
1143 rc = CFGMR3InsertNode(pDevices, "mc146818", &pDev);
1144 UPDATERC();
1145 rc = CFGMR3InsertNode(pDev, "0", &pInst);
1146 UPDATERC();
1147 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
1148 UPDATERC();
1149
1150 /*
1151 * VGA.
1152 */
1153 rc = CFGMR3InsertNode(pDevices, "vga", &pDev);
1154 UPDATERC();
1155 rc = CFGMR3InsertNode(pDev, "0", &pInst);
1156 UPDATERC();
1157 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
1158 UPDATERC();
1159 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
1160 UPDATERC();
1161 rc = CFGMR3InsertInteger(pCfg, "VRamSize", 4 * _1M);
1162 UPDATERC();
1163
1164 /* Bios logo. */
1165 rc = CFGMR3InsertInteger(pCfg, "FadeIn", 1);
1166 UPDATERC();
1167 rc = CFGMR3InsertInteger(pCfg, "FadeOut", 1);
1168 UPDATERC();
1169 rc = CFGMR3InsertInteger(pCfg, "LogoTime", 0);
1170 UPDATERC();
1171 rc = CFGMR3InsertString(pCfg, "LogoFile", "");
1172 UPDATERC();
1173
1174#if 0
1175 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0);
1176 UPDATERC();
1177 rc = CFGMR3InsertString(pLunL0, "Driver", "MainDisplay");
1178 UPDATERC();
1179#endif
1180
1181 /*
1182 * IDE controller.
1183 */
1184 rc = CFGMR3InsertNode(pDevices, "piix3ide", &pDev); /* piix3 */
1185 UPDATERC();
1186 rc = CFGMR3InsertNode(pDev, "0", &pInst);
1187 UPDATERC();
1188 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
1189 UPDATERC();
1190 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
1191 UPDATERC();
1192
1193 /*
1194 * VMMDev.
1195 */
1196 rc = CFGMR3InsertNode(pDevices, "VMMDev", &pDev);
1197 UPDATERC();
1198 rc = CFGMR3InsertNode(pDev, "0", &pInst);
1199 UPDATERC();
1200 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
1201 UPDATERC();
1202 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
1203 UPDATERC();
1204
1205
1206 /*
1207 * ...
1208 */
1209
1210#undef UPDATERC
1211 return rcAll;
1212}
1213
1214
1215
1216
1217/**
1218 * Resolves a path reference to a child node.
1219 *
1220 * @returns VBox status code.
1221 * @param pNode Which node to search for pszName in.
1222 * @param pszPath Path to the child node.
1223 * @param ppChild Where to store the pointer to the child node.
1224 */
1225static int cfgmR3ResolveNode(PCFGMNODE pNode, const char *pszPath, PCFGMNODE *ppChild)
1226{
1227 *ppChild = NULL;
1228 if (!pNode)
1229 return VERR_CFGM_NO_PARENT;
1230 PCFGMNODE pChild = NULL;
1231 for (;;)
1232 {
1233 /* skip leading slashes. */
1234 while (*pszPath == '/')
1235 pszPath++;
1236
1237 /* End of path? */
1238 if (!*pszPath)
1239 {
1240 if (!pChild)
1241 return VERR_CFGM_INVALID_CHILD_PATH;
1242 *ppChild = pChild;
1243 return VINF_SUCCESS;
1244 }
1245
1246 /* find end of component. */
1247 const char *pszNext = strchr(pszPath, '/');
1248 if (!pszNext)
1249 pszNext = strchr(pszPath, '\0');
1250 RTUINT cchName = pszNext - pszPath;
1251
1252 /* search child list. */
1253 pChild = pNode->pFirstChild;
1254 for ( ; pChild; pChild = pChild->pNext)
1255 if (pChild->cchName == cchName)
1256 {
1257 int iDiff = memcmp(pszPath, pChild->szName, cchName);
1258 if (iDiff <= 0)
1259 {
1260 if (iDiff != 0)
1261 pChild = NULL;
1262 break;
1263 }
1264 }
1265 if (!pChild)
1266 return VERR_CFGM_CHILD_NOT_FOUND;
1267
1268 /* next iteration */
1269 pNode = pChild;
1270 pszPath = pszNext;
1271 }
1272
1273 /* won't get here */
1274}
1275
1276
1277/**
1278 * Resolves a path reference to a child node.
1279 *
1280 * @returns VBox status code.
1281 * @param pNode Which node to search for pszName in.
1282 * @param pszName Name of a byte string value.
1283 * @param ppLeaf Where to store the pointer to the leaf node.
1284 */
1285static int cfgmR3ResolveLeaf(PCFGMNODE pNode, const char *pszName, PCFGMLEAF *ppLeaf)
1286{
1287 *ppLeaf = NULL;
1288 if (!pNode)
1289 return VERR_CFGM_NO_PARENT;
1290
1291 size_t cchName = strlen(pszName);
1292 PCFGMLEAF pLeaf = pNode->pFirstLeaf;
1293 while (pLeaf)
1294 {
1295 if (cchName == pLeaf->cchName)
1296 {
1297 int iDiff = memcmp(pszName, pLeaf->szName, cchName);
1298 if (iDiff <= 0)
1299 {
1300 if (iDiff != 0)
1301 break;
1302 *ppLeaf = pLeaf;
1303 return VINF_SUCCESS;
1304 }
1305 }
1306
1307 /* next */
1308 pLeaf = pLeaf->pNext;
1309 }
1310 return VERR_CFGM_VALUE_NOT_FOUND;
1311}
1312
1313
1314
1315/**
1316 * Creates a CFGM tree.
1317 *
1318 * This is intended for creating device/driver configs can be
1319 * passed around and later attached to the main tree in the
1320 * correct location.
1321 *
1322 * @returns Pointer to the root node, NULL on error (out of memory or invalid
1323 * VM handle).
1324 * @param pUVM The user mode VM handle. For testcase (and other
1325 * purposes, NULL can be used. However, the resulting
1326 * tree cannot be inserted into a tree that has a
1327 * non-NULL value. Using NULL can be usedful for
1328 * testcases and similar, non VMM uses.
1329 */
1330VMMR3DECL(PCFGMNODE) CFGMR3CreateTree(PUVM pUVM)
1331{
1332 if (pUVM)
1333 {
1334 UVM_ASSERT_VALID_EXT_RETURN(pUVM, NULL);
1335 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, NULL);
1336 }
1337
1338 PCFGMNODE pNew;
1339 if (pUVM)
1340 pNew = (PCFGMNODE)MMR3HeapAllocU(pUVM, MM_TAG_CFGM, sizeof(*pNew));
1341 else
1342 pNew = (PCFGMNODE)RTMemAlloc(sizeof(*pNew));
1343 if (pNew)
1344 {
1345 pNew->pPrev = NULL;
1346 pNew->pNext = NULL;
1347 pNew->pParent = NULL;
1348 pNew->pFirstChild = NULL;
1349 pNew->pFirstLeaf = NULL;
1350 pNew->pVM = pUVM ? pUVM->pVM : NULL;
1351 pNew->fRestrictedRoot = false;
1352 pNew->cchName = 0;
1353 pNew->szName[0] = 0;
1354 }
1355 return pNew;
1356}
1357
1358
1359/**
1360 * Duplicates a CFGM sub-tree or a full tree.
1361 *
1362 * @returns Pointer to the root node. NULL if we run out of memory or the
1363 * input parameter is NULL.
1364 * @param pRoot The root of the tree to duplicate.
1365 * @param ppCopy Where to return the root of the duplicate.
1366 */
1367VMMR3DECL(int) CFGMR3DuplicateSubTree(PCFGMNODE pRoot, PCFGMNODE *ppCopy)
1368{
1369 AssertPtrReturn(pRoot, VERR_INVALID_POINTER);
1370
1371 /*
1372 * Create a new tree.
1373 */
1374 PCFGMNODE pNewRoot = CFGMR3CreateTree(pRoot->pVM ? pRoot->pVM->pUVM : NULL);
1375 if (!pNewRoot)
1376 return VERR_NO_MEMORY;
1377
1378 /*
1379 * Duplicate the content.
1380 */
1381 int rc = VINF_SUCCESS;
1382 PCFGMNODE pSrcCur = pRoot;
1383 PCFGMNODE pDstCur = pNewRoot;
1384 for (;;)
1385 {
1386 if ( !pDstCur->pFirstChild
1387 && !pDstCur->pFirstLeaf)
1388 {
1389 /*
1390 * Values first.
1391 */
1392 /** @todo this isn't the most efficient way to do it. */
1393 for (PCFGMLEAF pLeaf = pSrcCur->pFirstLeaf; pLeaf && RT_SUCCESS(rc); pLeaf = pLeaf->pNext)
1394 rc = CFGMR3InsertValue(pDstCur, pLeaf);
1395
1396 /*
1397 * Insert immediate child nodes.
1398 */
1399 /** @todo this isn't the most efficient way to do it. */
1400 for (PCFGMNODE pChild = pSrcCur->pFirstChild; pChild && RT_SUCCESS(rc); pChild = pChild->pNext)
1401 rc = CFGMR3InsertNode(pDstCur, pChild->szName, NULL);
1402
1403 AssertLogRelRCBreak(rc);
1404 }
1405
1406 /*
1407 * Deep copy of the children.
1408 */
1409 if (pSrcCur->pFirstChild)
1410 {
1411 Assert(pDstCur->pFirstChild && !strcmp(pDstCur->pFirstChild->szName, pSrcCur->pFirstChild->szName));
1412 pSrcCur = pSrcCur->pFirstChild;
1413 pDstCur = pDstCur->pFirstChild;
1414 }
1415 /*
1416 * If it's the root node, we're done.
1417 */
1418 else if (pSrcCur == pRoot)
1419 break;
1420 else
1421 {
1422 /*
1423 * Upon reaching the end of a sibling list, we must ascend and
1424 * resume the sibiling walk on an previous level.
1425 */
1426 if (!pSrcCur->pNext)
1427 {
1428 do
1429 {
1430 pSrcCur = pSrcCur->pParent;
1431 pDstCur = pDstCur->pParent;
1432 } while (!pSrcCur->pNext && pSrcCur != pRoot);
1433 if (pSrcCur == pRoot)
1434 break;
1435 }
1436
1437 /*
1438 * Next sibling.
1439 */
1440 Assert(pDstCur->pNext && !strcmp(pDstCur->pNext->szName, pSrcCur->pNext->szName));
1441 pSrcCur = pSrcCur->pNext;
1442 pDstCur = pDstCur->pNext;
1443 }
1444 }
1445
1446 if (RT_FAILURE(rc))
1447 {
1448 CFGMR3RemoveNode(pNewRoot);
1449 return rc;
1450 }
1451
1452 *ppCopy = pNewRoot;
1453 return VINF_SUCCESS;
1454}
1455
1456
1457/**
1458 * Insert subtree.
1459 *
1460 * This function inserts (no duplication) a tree created by CFGMR3CreateTree()
1461 * into the main tree.
1462 *
1463 * The root node of the inserted subtree will need to be reallocated, which
1464 * effectually means that the passed in pSubTree handle becomes invalid
1465 * upon successful return. Use the value returned in ppChild instead
1466 * of pSubTree.
1467 *
1468 * @returns VBox status code.
1469 * @returns VERR_CFGM_NODE_EXISTS if the final child node name component exists.
1470 * @param pNode Parent node.
1471 * @param pszName Name or path of the new child node.
1472 * @param pSubTree The subtree to insert. Must be returned by CFGMR3CreateTree().
1473 * @param ppChild Where to store the address of the new child node. (optional)
1474 */
1475VMMR3DECL(int) CFGMR3InsertSubTree(PCFGMNODE pNode, const char *pszName, PCFGMNODE pSubTree, PCFGMNODE *ppChild)
1476{
1477 /*
1478 * Validate input.
1479 */
1480 AssertPtrReturn(pNode, VERR_INVALID_POINTER);
1481 AssertPtrReturn(pSubTree, VERR_INVALID_POINTER);
1482 AssertReturn(pNode != pSubTree, VERR_INVALID_PARAMETER);
1483 AssertReturn(!pSubTree->pParent, VERR_INVALID_PARAMETER);
1484 AssertReturn(pNode->pVM == pSubTree->pVM, VERR_INVALID_PARAMETER);
1485 Assert(!pSubTree->pNext);
1486 Assert(!pSubTree->pPrev);
1487
1488 /*
1489 * Use CFGMR3InsertNode to create a new node and then
1490 * re-attach the children and leaves of the subtree to it.
1491 */
1492 PCFGMNODE pNewChild;
1493 int rc = CFGMR3InsertNode(pNode, pszName, &pNewChild);
1494 if (RT_SUCCESS(rc))
1495 {
1496 Assert(!pNewChild->pFirstChild);
1497 Assert(!pNewChild->pFirstLeaf);
1498
1499 pNewChild->pFirstChild = pSubTree->pFirstChild;
1500 pNewChild->pFirstLeaf = pSubTree->pFirstLeaf;
1501 for (PCFGMNODE pChild = pNewChild->pFirstChild; pChild; pChild = pChild->pNext)
1502 pChild->pParent = pNewChild;
1503
1504 if (ppChild)
1505 *ppChild = pNewChild;
1506
1507 /* free the old subtree root */
1508 cfgmR3FreeNodeOnly(pSubTree);
1509 }
1510 return rc;
1511}
1512
1513
1514/**
1515 * Replaces a (sub-)tree with new one.
1516 *
1517 * This function removes the exiting (sub-)tree, completely freeing it in the
1518 * process, and inserts (no duplication) the specified tree. The tree can
1519 * either be created by CFGMR3CreateTree or CFGMR3DuplicateSubTree.
1520 *
1521 * @returns VBox status code.
1522 * @param pRoot The sub-tree to replace. This node will remain valid
1523 * after the call.
1524 * @param pNewRoot The tree to replace @a pRoot with. This not will
1525 * become invalid after a successful call.
1526 */
1527VMMR3DECL(int) CFGMR3ReplaceSubTree(PCFGMNODE pRoot, PCFGMNODE pNewRoot)
1528{
1529 /*
1530 * Validate input.
1531 */
1532 AssertPtrReturn(pRoot, VERR_INVALID_POINTER);
1533 AssertPtrReturn(pNewRoot, VERR_INVALID_POINTER);
1534 AssertReturn(pRoot != pNewRoot, VERR_INVALID_PARAMETER);
1535 AssertReturn(!pNewRoot->pParent, VERR_INVALID_PARAMETER);
1536 AssertReturn(pNewRoot->pVM == pRoot->pVM, VERR_INVALID_PARAMETER);
1537 AssertReturn(!pNewRoot->pNext, VERR_INVALID_PARAMETER);
1538 AssertReturn(!pNewRoot->pPrev, VERR_INVALID_PARAMETER);
1539
1540 /*
1541 * Free the current properties fo pRoot.
1542 */
1543 while (pRoot->pFirstChild)
1544 CFGMR3RemoveNode(pRoot->pFirstChild);
1545
1546 while (pRoot->pFirstLeaf)
1547 cfgmR3RemoveLeaf(pRoot, pRoot->pFirstLeaf);
1548
1549 /*
1550 * Copy all the properties from the new root to the current one.
1551 */
1552 pRoot->pFirstLeaf = pNewRoot->pFirstLeaf;
1553 pRoot->pFirstChild = pNewRoot->pFirstChild;
1554 for (PCFGMNODE pChild = pRoot->pFirstChild; pChild; pChild = pChild->pNext)
1555 pChild->pParent = pRoot;
1556
1557 cfgmR3FreeNodeOnly(pNewRoot);
1558
1559 return VINF_SUCCESS;
1560}
1561
1562
1563/**
1564 * Copies all values and keys from one tree onto another.
1565 *
1566 * The flags control what happens to keys and values with the same name
1567 * existing in both source and destination.
1568 *
1569 * @returns VBox status code.
1570 * @param pDstTree The destination tree.
1571 * @param pSrcTree The source tree.
1572 * @param fFlags Copy flags, see CFGM_COPY_FLAGS_XXX.
1573 */
1574VMMR3DECL(int) CFGMR3CopyTree(PCFGMNODE pDstTree, PCFGMNODE pSrcTree, uint32_t fFlags)
1575{
1576 /*
1577 * Input validation.
1578 */
1579 AssertPtrReturn(pSrcTree, VERR_INVALID_POINTER);
1580 AssertPtrReturn(pDstTree, VERR_INVALID_POINTER);
1581 AssertReturn(pDstTree != pSrcTree, VERR_INVALID_PARAMETER);
1582 AssertReturn(!(fFlags & ~(CFGM_COPY_FLAGS_VALUE_DISP_MASK | CFGM_COPY_FLAGS_KEY_DISP_MASK)), VERR_INVALID_PARAMETER);
1583 AssertReturn( (fFlags & CFGM_COPY_FLAGS_VALUE_DISP_MASK) != CFGM_COPY_FLAGS_RESERVED_VALUE_DISP_0
1584 && (fFlags & CFGM_COPY_FLAGS_VALUE_DISP_MASK) != CFGM_COPY_FLAGS_RESERVED_VALUE_DISP_1,
1585 VERR_INVALID_PARAMETER);
1586 AssertReturn((fFlags & CFGM_COPY_FLAGS_KEY_DISP_MASK) != CFGM_COPY_FLAGS_RESERVED_KEY_DISP,
1587 VERR_INVALID_PARAMETER);
1588
1589 /*
1590 * Copy the values.
1591 */
1592 int rc;
1593 for (PCFGMLEAF pValue = CFGMR3GetFirstValue(pSrcTree); pValue; pValue = CFGMR3GetNextValue(pValue))
1594 {
1595 rc = CFGMR3InsertValue(pDstTree, pValue);
1596 if (rc == VERR_CFGM_LEAF_EXISTS)
1597 {
1598 if ((fFlags & CFGM_COPY_FLAGS_VALUE_DISP_MASK) == CFGM_COPY_FLAGS_REPLACE_VALUES)
1599 {
1600 rc = CFGMR3RemoveValue(pDstTree, pValue->szName);
1601 if (RT_FAILURE(rc))
1602 break;
1603 rc = CFGMR3InsertValue(pDstTree, pValue);
1604 }
1605 else
1606 rc = VINF_SUCCESS;
1607 }
1608 AssertRCReturn(rc, rc);
1609 }
1610
1611 /*
1612 * Copy/merge the keys - merging results in recursion.
1613 */
1614 for (PCFGMNODE pSrcChild = CFGMR3GetFirstChild(pSrcTree); pSrcChild; pSrcChild = CFGMR3GetNextChild(pSrcChild))
1615 {
1616 PCFGMNODE pDstChild = CFGMR3GetChild(pDstTree, pSrcChild->szName);
1617 if ( pDstChild
1618 && (fFlags & CFGM_COPY_FLAGS_KEY_DISP_MASK) == CFGM_COPY_FLAGS_REPLACE_KEYS)
1619 {
1620 CFGMR3RemoveNode(pDstChild);
1621 pDstChild = NULL;
1622 }
1623 if (!pDstChild)
1624 {
1625 PCFGMNODE pChildCopy;
1626 rc = CFGMR3DuplicateSubTree(pSrcChild, &pChildCopy);
1627 AssertRCReturn(rc, rc);
1628 rc = CFGMR3InsertSubTree(pDstTree, pSrcChild->szName, pChildCopy, NULL);
1629 AssertRCReturnStmt(rc, CFGMR3RemoveNode(pChildCopy), rc);
1630 }
1631 else if ((fFlags & CFGM_COPY_FLAGS_KEY_DISP_MASK) == CFGM_COPY_FLAGS_MERGE_KEYS)
1632 {
1633 rc = CFGMR3CopyTree(pDstChild, pSrcChild, fFlags);
1634 AssertRCReturn(rc, rc);
1635 }
1636 }
1637
1638 return VINF_SUCCESS;
1639}
1640
1641
1642
1643/**
1644 * Compares two names.
1645 *
1646 * @returns Similar to memcpy.
1647 * @param pszName1 The first name.
1648 * @param cchName1 The length of the first name.
1649 * @param pszName2 The second name.
1650 * @param cchName2 The length of the second name.
1651 */
1652DECLINLINE(int) cfgmR3CompareNames(const char *pszName1, size_t cchName1, const char *pszName2, size_t cchName2)
1653{
1654 int iDiff;
1655 if (cchName1 <= cchName2)
1656 {
1657 iDiff = memcmp(pszName1, pszName2, cchName1);
1658 if (!iDiff && cchName1 < cchName2)
1659 iDiff = -1;
1660 }
1661 else
1662 {
1663 iDiff = memcmp(pszName1, pszName2, cchName2);
1664 if (!iDiff)
1665 iDiff = 1;
1666 }
1667 return iDiff;
1668}
1669
1670
1671/**
1672 * Insert a node.
1673 *
1674 * @returns VBox status code.
1675 * @returns VERR_CFGM_NODE_EXISTS if the final child node name component exists.
1676 * @param pNode Parent node.
1677 * @param pszName Name or path of the new child node.
1678 * @param ppChild Where to store the address of the new child node. (optional)
1679 */
1680VMMR3DECL(int) CFGMR3InsertNode(PCFGMNODE pNode, const char *pszName, PCFGMNODE *ppChild)
1681{
1682 int rc;
1683 if (pNode)
1684 {
1685 /*
1686 * If given a path we have to deal with it component by component.
1687 */
1688 while (*pszName == '/')
1689 pszName++;
1690 if (strchr(pszName, '/'))
1691 {
1692 char *pszDup = RTStrDup(pszName);
1693 if (pszDup)
1694 {
1695 char *psz = pszDup;
1696 for (;;)
1697 {
1698 /* Terminate at '/' and find the next component. */
1699 char *pszNext = strchr(psz, '/');
1700 if (pszNext)
1701 {
1702 *pszNext++ = '\0';
1703 while (*pszNext == '/')
1704 pszNext++;
1705 if (*pszNext == '\0')
1706 pszNext = NULL;
1707 }
1708
1709 /* does it exist? */
1710 PCFGMNODE pChild = CFGMR3GetChild(pNode, psz);
1711 if (!pChild)
1712 {
1713 /* no, insert it */
1714 rc = CFGMR3InsertNode(pNode, psz, &pChild);
1715 if (RT_FAILURE(rc))
1716 break;
1717 if (!pszNext)
1718 {
1719 if (ppChild)
1720 *ppChild = pChild;
1721 break;
1722 }
1723
1724 }
1725 /* if last component fail */
1726 else if (!pszNext)
1727 {
1728 rc = VERR_CFGM_NODE_EXISTS;
1729 break;
1730 }
1731
1732 /* next */
1733 pNode = pChild;
1734 psz = pszNext;
1735 }
1736 RTStrFree(pszDup);
1737 }
1738 else
1739 rc = VERR_NO_TMP_MEMORY;
1740 }
1741 /*
1742 * Not multicomponent, just make sure it's a non-zero name.
1743 */
1744 else if (*pszName)
1745 {
1746 /*
1747 * Check if already exists and find last node in chain.
1748 */
1749 size_t cchName = strlen(pszName);
1750 PCFGMNODE pPrev = NULL;
1751 PCFGMNODE pNext = pNode->pFirstChild;
1752 if (pNext)
1753 {
1754 for ( ; pNext; pPrev = pNext, pNext = pNext->pNext)
1755 {
1756 int iDiff = cfgmR3CompareNames(pszName, cchName, pNext->szName, pNext->cchName);
1757 if (iDiff <= 0)
1758 {
1759 if (!iDiff)
1760 return VERR_CFGM_NODE_EXISTS;
1761 break;
1762 }
1763 }
1764 }
1765
1766 /*
1767 * Allocate and init node.
1768 */
1769 PCFGMNODE pNew = (PCFGMNODE)cfgmR3MemAlloc(pNode->pVM, MM_TAG_CFGM, sizeof(*pNew) + cchName);
1770 if (pNew)
1771 {
1772 pNew->pParent = pNode;
1773 pNew->pFirstChild = NULL;
1774 pNew->pFirstLeaf = NULL;
1775 pNew->pVM = pNode->pVM;
1776 pNew->fRestrictedRoot = false;
1777 pNew->cchName = cchName;
1778 memcpy(pNew->szName, pszName, cchName + 1);
1779
1780 /*
1781 * Insert into child list.
1782 */
1783 pNew->pPrev = pPrev;
1784 if (pPrev)
1785 pPrev->pNext = pNew;
1786 else
1787 pNode->pFirstChild = pNew;
1788 pNew->pNext = pNext;
1789 if (pNext)
1790 pNext->pPrev = pNew;
1791
1792 if (ppChild)
1793 *ppChild = pNew;
1794 rc = VINF_SUCCESS;
1795 }
1796 else
1797 rc = VERR_NO_MEMORY;
1798 }
1799 else
1800 {
1801 rc = VERR_CFGM_INVALID_NODE_PATH;
1802 AssertMsgFailed(("Invalid path %s\n", pszName));
1803 }
1804 }
1805 else
1806 {
1807 rc = VERR_CFGM_NO_PARENT;
1808 AssertMsgFailed(("No parent! path %s\n", pszName));
1809 }
1810
1811 return rc;
1812}
1813
1814
1815/**
1816 * Insert a node, format string name.
1817 *
1818 * @returns VBox status code.
1819 * @param pNode Parent node.
1820 * @param ppChild Where to store the address of the new child node. (optional)
1821 * @param pszNameFormat Name of or path the new child node.
1822 * @param ... Name format arguments.
1823 */
1824VMMR3DECL(int) CFGMR3InsertNodeF(PCFGMNODE pNode, PCFGMNODE *ppChild, const char *pszNameFormat, ...)
1825{
1826 va_list Args;
1827 va_start(Args, pszNameFormat);
1828 int rc = CFGMR3InsertNodeFV(pNode, ppChild, pszNameFormat, Args);
1829 va_end(Args);
1830 return rc;
1831}
1832
1833
1834/**
1835 * Insert a node, format string name.
1836 *
1837 * @returns VBox status code.
1838 * @param pNode Parent node.
1839 * @param ppChild Where to store the address of the new child node. (optional)
1840 * @param pszNameFormat Name or path of the new child node.
1841 * @param Args Name format arguments.
1842 */
1843VMMR3DECL(int) CFGMR3InsertNodeFV(PCFGMNODE pNode, PCFGMNODE *ppChild, const char *pszNameFormat, va_list Args)
1844{
1845 int rc;
1846 char *pszName;
1847 RTStrAPrintfV(&pszName, pszNameFormat, Args);
1848 if (pszName)
1849 {
1850 rc = CFGMR3InsertNode(pNode, pszName, ppChild);
1851 RTStrFree(pszName);
1852 }
1853 else
1854 rc = VERR_NO_MEMORY;
1855 return rc;
1856}
1857
1858
1859/**
1860 * Marks the node as the root of a restricted subtree, i.e. the end of
1861 * a CFGMR3GetParent() journey.
1862 *
1863 * @param pNode The node to mark.
1864 */
1865VMMR3DECL(void) CFGMR3SetRestrictedRoot(PCFGMNODE pNode)
1866{
1867 if (pNode)
1868 pNode->fRestrictedRoot = true;
1869}
1870
1871
1872/**
1873 * Insert a node.
1874 *
1875 * @returns VBox status code.
1876 * @param pNode Parent node.
1877 * @param pszName Name of the new child node.
1878 * @param ppLeaf Where to store the new leaf.
1879 * The caller must fill in the enmType and Value fields!
1880 */
1881static int cfgmR3InsertLeaf(PCFGMNODE pNode, const char *pszName, PCFGMLEAF *ppLeaf)
1882{
1883 int rc;
1884 if (*pszName)
1885 {
1886 if (pNode)
1887 {
1888 /*
1889 * Check if already exists and find last node in chain.
1890 */
1891 size_t cchName = strlen(pszName);
1892 PCFGMLEAF pPrev = NULL;
1893 PCFGMLEAF pNext = pNode->pFirstLeaf;
1894 if (pNext)
1895 {
1896 for ( ; pNext; pPrev = pNext, pNext = pNext->pNext)
1897 {
1898 int iDiff = cfgmR3CompareNames(pszName, cchName, pNext->szName, pNext->cchName);
1899 if (iDiff <= 0)
1900 {
1901 if (!iDiff)
1902 return VERR_CFGM_LEAF_EXISTS;
1903 break;
1904 }
1905 }
1906 }
1907
1908 /*
1909 * Allocate and init node.
1910 */
1911 PCFGMLEAF pNew = (PCFGMLEAF)cfgmR3MemAlloc(pNode->pVM, MM_TAG_CFGM, sizeof(*pNew) + cchName);
1912 if (pNew)
1913 {
1914 pNew->cchName = cchName;
1915 memcpy(pNew->szName, pszName, cchName + 1);
1916
1917 /*
1918 * Insert into child list.
1919 */
1920 pNew->pPrev = pPrev;
1921 if (pPrev)
1922 pPrev->pNext = pNew;
1923 else
1924 pNode->pFirstLeaf = pNew;
1925 pNew->pNext = pNext;
1926 if (pNext)
1927 pNext->pPrev = pNew;
1928
1929 *ppLeaf = pNew;
1930 rc = VINF_SUCCESS;
1931 }
1932 else
1933 rc = VERR_NO_MEMORY;
1934 }
1935 else
1936 rc = VERR_CFGM_NO_PARENT;
1937 }
1938 else
1939 rc = VERR_CFGM_INVALID_CHILD_PATH;
1940 return rc;
1941}
1942
1943
1944/**
1945 * Removes a node.
1946 *
1947 * @param pNode The node to remove.
1948 */
1949VMMR3DECL(void) CFGMR3RemoveNode(PCFGMNODE pNode)
1950{
1951 if (pNode)
1952 {
1953 /*
1954 * Free children.
1955 */
1956 while (pNode->pFirstChild)
1957 CFGMR3RemoveNode(pNode->pFirstChild);
1958
1959 /*
1960 * Free leaves.
1961 */
1962 while (pNode->pFirstLeaf)
1963 cfgmR3RemoveLeaf(pNode, pNode->pFirstLeaf);
1964
1965 /*
1966 * Unlink ourselves.
1967 */
1968 if (pNode->pPrev)
1969 pNode->pPrev->pNext = pNode->pNext;
1970 else
1971 {
1972 if (pNode->pParent)
1973 pNode->pParent->pFirstChild = pNode->pNext;
1974 else if ( pNode->pVM /* might be a different tree */
1975 && pNode == pNode->pVM->cfgm.s.pRoot)
1976 pNode->pVM->cfgm.s.pRoot = NULL;
1977 }
1978 if (pNode->pNext)
1979 pNode->pNext->pPrev = pNode->pPrev;
1980
1981 /*
1982 * Free ourselves.
1983 */
1984 cfgmR3FreeNodeOnly(pNode);
1985 }
1986}
1987
1988
1989/**
1990 * Removes a leaf.
1991 *
1992 * @param pNode Parent node.
1993 * @param pLeaf Leaf to remove.
1994 */
1995static void cfgmR3RemoveLeaf(PCFGMNODE pNode, PCFGMLEAF pLeaf)
1996{
1997 if (pNode && pLeaf)
1998 {
1999 /*
2000 * Unlink.
2001 */
2002 if (pLeaf->pPrev)
2003 pLeaf->pPrev->pNext = pLeaf->pNext;
2004 else
2005 pNode->pFirstLeaf = pLeaf->pNext;
2006 if (pLeaf->pNext)
2007 pLeaf->pNext->pPrev = pLeaf->pPrev;
2008
2009 /*
2010 * Free value and node.
2011 */
2012 cfgmR3FreeValue(pNode->pVM, pLeaf);
2013 pLeaf->pNext = NULL;
2014 pLeaf->pPrev = NULL;
2015 cfgmR3MemFree(pNode->pVM, pLeaf);
2016 }
2017}
2018
2019
2020/**
2021 * Frees whatever resources the leaf value is owning.
2022 *
2023 * Use this before assigning a new value to a leaf.
2024 * The caller must either free the leaf or assign a new value to it.
2025 *
2026 * @param pVM The cross context VM structure, if the tree
2027 * is associated with one.
2028 * @param pLeaf Pointer to the leaf which value should be free.
2029 */
2030static void cfgmR3FreeValue(PVM pVM, PCFGMLEAF pLeaf)
2031{
2032 if (pLeaf)
2033 {
2034 switch (pLeaf->enmType)
2035 {
2036 case CFGMVALUETYPE_BYTES:
2037 cfgmR3MemFree(pVM, pLeaf->Value.Bytes.pau8);
2038 pLeaf->Value.Bytes.pau8 = NULL;
2039 pLeaf->Value.Bytes.cb = 0;
2040 break;
2041
2042 case CFGMVALUETYPE_STRING:
2043 cfgmR3StrFree(pVM, pLeaf->Value.String.psz);
2044 pLeaf->Value.String.psz = NULL;
2045 pLeaf->Value.String.cb = 0;
2046 break;
2047
2048 case CFGMVALUETYPE_INTEGER:
2049 break;
2050 }
2051 pLeaf->enmType = (CFGMVALUETYPE)0;
2052 }
2053}
2054
2055/**
2056 * Destroys a tree created with CFGMR3CreateTree or CFGMR3DuplicateSubTree.
2057 *
2058 * @returns VBox status code.
2059 * @param pRoot The root node of the tree.
2060 */
2061VMMR3DECL(int) CFGMR3DestroyTree(PCFGMNODE pRoot)
2062{
2063 if (!pRoot)
2064 return VINF_SUCCESS;
2065 AssertReturn(!pRoot->pParent, VERR_INVALID_PARAMETER);
2066 AssertReturn(!pRoot->pVM || pRoot != pRoot->pVM->cfgm.s.pRoot, VERR_ACCESS_DENIED);
2067
2068 CFGMR3RemoveNode(pRoot);
2069 return VINF_SUCCESS;
2070}
2071
2072
2073/**
2074 * Inserts a new integer value.
2075 *
2076 * @returns VBox status code.
2077 * @param pNode Parent node.
2078 * @param pszName Value name.
2079 * @param u64Integer The value.
2080 */
2081VMMR3DECL(int) CFGMR3InsertInteger(PCFGMNODE pNode, const char *pszName, uint64_t u64Integer)
2082{
2083 PCFGMLEAF pLeaf;
2084 int rc = cfgmR3InsertLeaf(pNode, pszName, &pLeaf);
2085 if (RT_SUCCESS(rc))
2086 {
2087 pLeaf->enmType = CFGMVALUETYPE_INTEGER;
2088 pLeaf->Value.Integer.u64 = u64Integer;
2089 }
2090 return rc;
2091}
2092
2093
2094/**
2095 * Inserts a new string value. This variant expects that the caller know the length
2096 * of the string already so we can avoid calling strlen() here.
2097 *
2098 * @returns VBox status code.
2099 * @param pNode Parent node.
2100 * @param pszName Value name.
2101 * @param pszString The value. Must not be NULL.
2102 * @param cchString The length of the string excluding the
2103 * terminator.
2104 */
2105VMMR3DECL(int) CFGMR3InsertStringN(PCFGMNODE pNode, const char *pszName, const char *pszString, size_t cchString)
2106{
2107 Assert(RTStrNLen(pszString, cchString) == cchString);
2108
2109 int rc;
2110 if (pNode)
2111 {
2112 /*
2113 * Allocate string object first.
2114 */
2115 char *pszStringCopy = (char *)cfgmR3StrAlloc(pNode->pVM, MM_TAG_CFGM_STRING, cchString + 1);
2116 if (pszStringCopy)
2117 {
2118 memcpy(pszStringCopy, pszString, cchString);
2119 pszStringCopy[cchString] = '\0';
2120
2121 /*
2122 * Create value leaf and set it to string type.
2123 */
2124 PCFGMLEAF pLeaf;
2125 rc = cfgmR3InsertLeaf(pNode, pszName, &pLeaf);
2126 if (RT_SUCCESS(rc))
2127 {
2128 pLeaf->enmType = CFGMVALUETYPE_STRING;
2129 pLeaf->Value.String.psz = pszStringCopy;
2130 pLeaf->Value.String.cb = cchString + 1;
2131 }
2132 else
2133 cfgmR3StrFree(pNode->pVM, pszStringCopy);
2134 }
2135 else
2136 rc = VERR_NO_MEMORY;
2137 }
2138 else
2139 rc = VERR_CFGM_NO_PARENT;
2140
2141 return rc;
2142}
2143
2144
2145/**
2146 * Inserts a new string value. Calls strlen(pszString) internally; if you know the
2147 * length of the string, CFGMR3InsertStringLengthKnown() is faster.
2148 *
2149 * @returns VBox status code.
2150 * @param pNode Parent node.
2151 * @param pszName Value name.
2152 * @param pszString The value.
2153 */
2154VMMR3DECL(int) CFGMR3InsertString(PCFGMNODE pNode, const char *pszName, const char *pszString)
2155{
2156 return CFGMR3InsertStringN(pNode, pszName, pszString, strlen(pszString));
2157}
2158
2159
2160/**
2161 * Same as CFGMR3InsertString except the string value given in RTStrPrintfV
2162 * fashion.
2163 *
2164 * @returns VBox status code.
2165 * @param pNode Parent node.
2166 * @param pszName Value name.
2167 * @param pszFormat The value given as a format string.
2168 * @param va Argument to pszFormat.
2169 */
2170VMMR3DECL(int) CFGMR3InsertStringFV(PCFGMNODE pNode, const char *pszName, const char *pszFormat, va_list va)
2171{
2172 int rc;
2173 if (pNode)
2174 {
2175 /*
2176 * Allocate string object first.
2177 */
2178 char *pszString;
2179 if (!pNode->pVM)
2180 pszString = RTStrAPrintf2(pszFormat, va);
2181 else
2182 pszString = MMR3HeapAPrintfVU(pNode->pVM->pUVM, MM_TAG_CFGM_STRING, pszFormat, va);
2183 if (pszString)
2184 {
2185 /*
2186 * Create value leaf and set it to string type.
2187 */
2188 PCFGMLEAF pLeaf;
2189 rc = cfgmR3InsertLeaf(pNode, pszName, &pLeaf);
2190 if (RT_SUCCESS(rc))
2191 {
2192 pLeaf->enmType = CFGMVALUETYPE_STRING;
2193 pLeaf->Value.String.psz = pszString;
2194 pLeaf->Value.String.cb = strlen(pszString) + 1;
2195 }
2196 else
2197 cfgmR3StrFree(pNode->pVM, pszString);
2198 }
2199 else
2200 rc = VERR_NO_MEMORY;
2201 }
2202 else
2203 rc = VERR_CFGM_NO_PARENT;
2204
2205 return rc;
2206}
2207
2208
2209/**
2210 * Same as CFGMR3InsertString except the string value given in RTStrPrintf
2211 * fashion.
2212 *
2213 * @returns VBox status code.
2214 * @param pNode Parent node.
2215 * @param pszName Value name.
2216 * @param pszFormat The value given as a format string.
2217 * @param ... Argument to pszFormat.
2218 */
2219VMMR3DECL(int) CFGMR3InsertStringF(PCFGMNODE pNode, const char *pszName, const char *pszFormat, ...)
2220{
2221 va_list va;
2222 va_start(va, pszFormat);
2223 int rc = CFGMR3InsertStringFV(pNode, pszName, pszFormat, va);
2224 va_end(va);
2225 return rc;
2226}
2227
2228
2229/**
2230 * Same as CFGMR3InsertString except the string value given as a UTF-16 string.
2231 *
2232 * @returns VBox status code.
2233 * @param pNode Parent node.
2234 * @param pszName Value name.
2235 * @param pwszValue The string value (UTF-16).
2236 */
2237VMMR3DECL(int) CFGMR3InsertStringW(PCFGMNODE pNode, const char *pszName, PCRTUTF16 pwszValue)
2238{
2239 char *pszValue;
2240 int rc = RTUtf16ToUtf8(pwszValue, &pszValue);
2241 if (RT_SUCCESS(rc))
2242 {
2243 rc = CFGMR3InsertString(pNode, pszName, pszValue);
2244 RTStrFree(pszValue);
2245 }
2246 return rc;
2247}
2248
2249
2250/**
2251 * Inserts a new integer value.
2252 *
2253 * @returns VBox status code.
2254 * @param pNode Parent node.
2255 * @param pszName Value name.
2256 * @param pvBytes The value.
2257 * @param cbBytes The value size.
2258 */
2259VMMR3DECL(int) CFGMR3InsertBytes(PCFGMNODE pNode, const char *pszName, const void *pvBytes, size_t cbBytes)
2260{
2261 int rc;
2262 if (pNode)
2263 {
2264 if (cbBytes == (RTUINT)cbBytes)
2265 {
2266 /*
2267 * Allocate string object first.
2268 */
2269 void *pvCopy = cfgmR3MemAlloc(pNode->pVM, MM_TAG_CFGM_STRING, cbBytes);
2270 if (pvCopy || !cbBytes)
2271 {
2272 memcpy(pvCopy, pvBytes, cbBytes);
2273
2274 /*
2275 * Create value leaf and set it to string type.
2276 */
2277 PCFGMLEAF pLeaf;
2278 rc = cfgmR3InsertLeaf(pNode, pszName, &pLeaf);
2279 if (RT_SUCCESS(rc))
2280 {
2281 pLeaf->enmType = CFGMVALUETYPE_BYTES;
2282 pLeaf->Value.Bytes.cb = cbBytes;
2283 pLeaf->Value.Bytes.pau8 = (uint8_t *)pvCopy;
2284 }
2285 else
2286 cfgmR3MemFree(pNode->pVM, pvCopy);
2287 }
2288 else
2289 rc = VERR_NO_MEMORY;
2290 }
2291 else
2292 rc = VERR_OUT_OF_RANGE;
2293 }
2294 else
2295 rc = VERR_CFGM_NO_PARENT;
2296
2297 return rc;
2298}
2299
2300
2301/**
2302 * Make a copy of the specified value under the given node.
2303 *
2304 * @returns VBox status code.
2305 * @param pNode Parent node.
2306 * @param pValue The value to copy and insert.
2307 */
2308VMMR3DECL(int) CFGMR3InsertValue(PCFGMNODE pNode, PCFGMLEAF pValue)
2309{
2310 int rc;
2311 switch (pValue->enmType)
2312 {
2313 case CFGMVALUETYPE_INTEGER:
2314 rc = CFGMR3InsertInteger(pNode, pValue->szName, pValue->Value.Integer.u64);
2315 break;
2316
2317 case CFGMVALUETYPE_BYTES:
2318 rc = CFGMR3InsertBytes(pNode, pValue->szName, pValue->Value.Bytes.pau8, pValue->Value.Bytes.cb);
2319 break;
2320
2321 case CFGMVALUETYPE_STRING:
2322 rc = CFGMR3InsertStringN(pNode, pValue->szName, pValue->Value.String.psz, pValue->Value.String.cb - 1);
2323 break;
2324
2325 default:
2326 rc = VERR_CFGM_IPE_1;
2327 AssertMsgFailed(("Invalid value type %d\n", pValue->enmType));
2328 break;
2329 }
2330 return rc;
2331}
2332
2333
2334/**
2335 * Remove a value.
2336 *
2337 * @returns VBox status code.
2338 * @param pNode Parent node.
2339 * @param pszName Name of the new child node.
2340 */
2341VMMR3DECL(int) CFGMR3RemoveValue(PCFGMNODE pNode, const char *pszName)
2342{
2343 PCFGMLEAF pLeaf;
2344 int rc = cfgmR3ResolveLeaf(pNode, pszName, &pLeaf);
2345 if (RT_SUCCESS(rc))
2346 cfgmR3RemoveLeaf(pNode, pLeaf);
2347 return rc;
2348}
2349
2350
2351
2352/*
2353 * -+- helper apis -+-
2354 */
2355
2356
2357/**
2358 * Query unsigned 64-bit integer value.
2359 *
2360 * @returns VBox status code.
2361 * @param pNode Which node to search for pszName in.
2362 * @param pszName Name of an integer value.
2363 * @param pu64 Where to store the integer value.
2364 */
2365VMMR3DECL(int) CFGMR3QueryU64(PCFGMNODE pNode, const char *pszName, uint64_t *pu64)
2366{
2367 return CFGMR3QueryInteger(pNode, pszName, pu64);
2368}
2369
2370
2371/**
2372 * Query unsigned 64-bit integer value with default.
2373 *
2374 * @returns VBox status code.
2375 * @param pNode Which node to search for pszName in.
2376 * @param pszName Name of an integer value.
2377 * @param pu64 Where to store the integer value. Set to default on failure.
2378 * @param u64Def The default value.
2379 */
2380VMMR3DECL(int) CFGMR3QueryU64Def(PCFGMNODE pNode, const char *pszName, uint64_t *pu64, uint64_t u64Def)
2381{
2382 return CFGMR3QueryIntegerDef(pNode, pszName, pu64, u64Def);
2383}
2384
2385
2386/**
2387 * Query signed 64-bit integer value.
2388 *
2389 * @returns VBox status code.
2390 * @param pNode Which node to search for pszName in.
2391 * @param pszName Name of an integer value.
2392 * @param pi64 Where to store the value.
2393 */
2394VMMR3DECL(int) CFGMR3QueryS64(PCFGMNODE pNode, const char *pszName, int64_t *pi64)
2395{
2396 uint64_t u64;
2397 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
2398 if (RT_SUCCESS(rc))
2399 *pi64 = (int64_t)u64;
2400 return rc;
2401}
2402
2403
2404/**
2405 * Query signed 64-bit integer value with default.
2406 *
2407 * @returns VBox status code.
2408 * @param pNode Which node to search for pszName in.
2409 * @param pszName Name of an integer value.
2410 * @param pi64 Where to store the value. Set to default on failure.
2411 * @param i64Def The default value.
2412 */
2413VMMR3DECL(int) CFGMR3QueryS64Def(PCFGMNODE pNode, const char *pszName, int64_t *pi64, int64_t i64Def)
2414{
2415 uint64_t u64;
2416 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, i64Def);
2417 *pi64 = (int64_t)u64;
2418 return rc;
2419}
2420
2421
2422/**
2423 * Query unsigned 32-bit integer value.
2424 *
2425 * @returns VBox status code.
2426 * @param pNode Which node to search for pszName in.
2427 * @param pszName Name of an integer value.
2428 * @param pu32 Where to store the value.
2429 */
2430VMMR3DECL(int) CFGMR3QueryU32(PCFGMNODE pNode, const char *pszName, uint32_t *pu32)
2431{
2432 uint64_t u64;
2433 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
2434 if (RT_SUCCESS(rc))
2435 {
2436 if (!(u64 & UINT64_C(0xffffffff00000000)))
2437 *pu32 = (uint32_t)u64;
2438 else
2439 rc = VERR_CFGM_INTEGER_TOO_BIG;
2440 }
2441 return rc;
2442}
2443
2444
2445/**
2446 * Query unsigned 32-bit integer value with default.
2447 *
2448 * @returns VBox status code.
2449 * @param pNode Which node to search for pszName in.
2450 * @param pszName Name of an integer value.
2451 * @param pu32 Where to store the value. Set to default on failure.
2452 * @param u32Def The default value.
2453 */
2454VMMR3DECL(int) CFGMR3QueryU32Def(PCFGMNODE pNode, const char *pszName, uint32_t *pu32, uint32_t u32Def)
2455{
2456 uint64_t u64;
2457 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, u32Def);
2458 if (RT_SUCCESS(rc))
2459 {
2460 if (!(u64 & UINT64_C(0xffffffff00000000)))
2461 *pu32 = (uint32_t)u64;
2462 else
2463 rc = VERR_CFGM_INTEGER_TOO_BIG;
2464 }
2465 if (RT_FAILURE(rc))
2466 *pu32 = u32Def;
2467 return rc;
2468}
2469
2470
2471/**
2472 * Query signed 32-bit integer value.
2473 *
2474 * @returns VBox status code.
2475 * @param pNode Which node to search for pszName in.
2476 * @param pszName Name of an integer value.
2477 * @param pi32 Where to store the value.
2478 */
2479VMMR3DECL(int) CFGMR3QueryS32(PCFGMNODE pNode, const char *pszName, int32_t *pi32)
2480{
2481 uint64_t u64;
2482 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
2483 if (RT_SUCCESS(rc))
2484 {
2485 if ( !(u64 & UINT64_C(0xffffffff80000000))
2486 || (u64 & UINT64_C(0xffffffff80000000)) == UINT64_C(0xffffffff80000000))
2487 *pi32 = (int32_t)u64;
2488 else
2489 rc = VERR_CFGM_INTEGER_TOO_BIG;
2490 }
2491 return rc;
2492}
2493
2494
2495/**
2496 * Query signed 32-bit integer value with default.
2497 *
2498 * @returns VBox status code.
2499 * @param pNode Which node to search for pszName in.
2500 * @param pszName Name of an integer value.
2501 * @param pi32 Where to store the value. Set to default on failure.
2502 * @param i32Def The default value.
2503 */
2504VMMR3DECL(int) CFGMR3QueryS32Def(PCFGMNODE pNode, const char *pszName, int32_t *pi32, int32_t i32Def)
2505{
2506 uint64_t u64;
2507 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, i32Def);
2508 if (RT_SUCCESS(rc))
2509 {
2510 if ( !(u64 & UINT64_C(0xffffffff80000000))
2511 || (u64 & UINT64_C(0xffffffff80000000)) == UINT64_C(0xffffffff80000000))
2512 *pi32 = (int32_t)u64;
2513 else
2514 rc = VERR_CFGM_INTEGER_TOO_BIG;
2515 }
2516 if (RT_FAILURE(rc))
2517 *pi32 = i32Def;
2518 return rc;
2519}
2520
2521
2522/**
2523 * Query unsigned 16-bit integer value.
2524 *
2525 * @returns VBox status code.
2526 * @param pNode Which node to search for pszName in.
2527 * @param pszName Name of an integer value.
2528 * @param pu16 Where to store the value.
2529 */
2530VMMR3DECL(int) CFGMR3QueryU16(PCFGMNODE pNode, const char *pszName, uint16_t *pu16)
2531{
2532 uint64_t u64;
2533 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
2534 if (RT_SUCCESS(rc))
2535 {
2536 if (!(u64 & UINT64_C(0xffffffffffff0000)))
2537 *pu16 = (int16_t)u64;
2538 else
2539 rc = VERR_CFGM_INTEGER_TOO_BIG;
2540 }
2541 return rc;
2542}
2543
2544
2545/**
2546 * Query unsigned 16-bit integer value with default.
2547 *
2548 * @returns VBox status code.
2549 * @param pNode Which node to search for pszName in.
2550 * @param pszName Name of an integer value.
2551 * @param pu16 Where to store the value. Set to default on failure.
2552 * @param u16Def The default value.
2553 */
2554VMMR3DECL(int) CFGMR3QueryU16Def(PCFGMNODE pNode, const char *pszName, uint16_t *pu16, uint16_t u16Def)
2555{
2556 uint64_t u64;
2557 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, u16Def);
2558 if (RT_SUCCESS(rc))
2559 {
2560 if (!(u64 & UINT64_C(0xffffffffffff0000)))
2561 *pu16 = (int16_t)u64;
2562 else
2563 rc = VERR_CFGM_INTEGER_TOO_BIG;
2564 }
2565 if (RT_FAILURE(rc))
2566 *pu16 = u16Def;
2567 return rc;
2568}
2569
2570
2571/**
2572 * Query signed 16-bit integer value.
2573 *
2574 * @returns VBox status code.
2575 * @param pNode Which node to search for pszName in.
2576 * @param pszName Name of an integer value.
2577 * @param pi16 Where to store the value.
2578 */
2579VMMR3DECL(int) CFGMR3QueryS16(PCFGMNODE pNode, const char *pszName, int16_t *pi16)
2580{
2581 uint64_t u64;
2582 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
2583 if (RT_SUCCESS(rc))
2584 {
2585 if ( !(u64 & UINT64_C(0xffffffffffff8000))
2586 || (u64 & UINT64_C(0xffffffffffff8000)) == UINT64_C(0xffffffffffff8000))
2587 *pi16 = (int16_t)u64;
2588 else
2589 rc = VERR_CFGM_INTEGER_TOO_BIG;
2590 }
2591 return rc;
2592}
2593
2594
2595/**
2596 * Query signed 16-bit integer value with default.
2597 *
2598 * @returns VBox status code.
2599 * @param pNode Which node to search for pszName in.
2600 * @param pszName Name of an integer value.
2601 * @param pi16 Where to store the value. Set to default on failure.
2602 * @param i16Def The default value.
2603 */
2604VMMR3DECL(int) CFGMR3QueryS16Def(PCFGMNODE pNode, const char *pszName, int16_t *pi16, int16_t i16Def)
2605{
2606 uint64_t u64;
2607 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, i16Def);
2608 if (RT_SUCCESS(rc))
2609 {
2610 if ( !(u64 & UINT64_C(0xffffffffffff8000))
2611 || (u64 & UINT64_C(0xffffffffffff8000)) == UINT64_C(0xffffffffffff8000))
2612 *pi16 = (int16_t)u64;
2613 else
2614 rc = VERR_CFGM_INTEGER_TOO_BIG;
2615 }
2616 if (RT_FAILURE(rc))
2617 *pi16 = i16Def;
2618 return rc;
2619}
2620
2621
2622/**
2623 * Query unsigned 8-bit integer value.
2624 *
2625 * @returns VBox status code.
2626 * @param pNode Which node to search for pszName in.
2627 * @param pszName Name of an integer value.
2628 * @param pu8 Where to store the value.
2629 */
2630VMMR3DECL(int) CFGMR3QueryU8(PCFGMNODE pNode, const char *pszName, uint8_t *pu8)
2631{
2632 uint64_t u64;
2633 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
2634 if (RT_SUCCESS(rc))
2635 {
2636 if (!(u64 & UINT64_C(0xffffffffffffff00)))
2637 *pu8 = (uint8_t)u64;
2638 else
2639 rc = VERR_CFGM_INTEGER_TOO_BIG;
2640 }
2641 return rc;
2642}
2643
2644
2645/**
2646 * Query unsigned 8-bit integer value with default.
2647 *
2648 * @returns VBox status code.
2649 * @param pNode Which node to search for pszName in.
2650 * @param pszName Name of an integer value.
2651 * @param pu8 Where to store the value. Set to default on failure.
2652 * @param u8Def The default value.
2653 */
2654VMMR3DECL(int) CFGMR3QueryU8Def(PCFGMNODE pNode, const char *pszName, uint8_t *pu8, uint8_t u8Def)
2655{
2656 uint64_t u64;
2657 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, u8Def);
2658 if (RT_SUCCESS(rc))
2659 {
2660 if (!(u64 & UINT64_C(0xffffffffffffff00)))
2661 *pu8 = (uint8_t)u64;
2662 else
2663 rc = VERR_CFGM_INTEGER_TOO_BIG;
2664 }
2665 if (RT_FAILURE(rc))
2666 *pu8 = u8Def;
2667 return rc;
2668}
2669
2670
2671/**
2672 * Query signed 8-bit integer value.
2673 *
2674 * @returns VBox status code.
2675 * @param pNode Which node to search for pszName in.
2676 * @param pszName Name of an integer value.
2677 * @param pi8 Where to store the value.
2678 */
2679VMMR3DECL(int) CFGMR3QueryS8(PCFGMNODE pNode, const char *pszName, int8_t *pi8)
2680{
2681 uint64_t u64;
2682 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
2683 if (RT_SUCCESS(rc))
2684 {
2685 if ( !(u64 & UINT64_C(0xffffffffffffff80))
2686 || (u64 & UINT64_C(0xffffffffffffff80)) == UINT64_C(0xffffffffffffff80))
2687 *pi8 = (int8_t)u64;
2688 else
2689 rc = VERR_CFGM_INTEGER_TOO_BIG;
2690 }
2691 return rc;
2692}
2693
2694
2695/**
2696 * Query signed 8-bit integer value with default.
2697 *
2698 * @returns VBox status code.
2699 * @param pNode Which node to search for pszName in.
2700 * @param pszName Name of an integer value.
2701 * @param pi8 Where to store the value. Set to default on failure.
2702 * @param i8Def The default value.
2703 */
2704VMMR3DECL(int) CFGMR3QueryS8Def(PCFGMNODE pNode, const char *pszName, int8_t *pi8, int8_t i8Def)
2705{
2706 uint64_t u64;
2707 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, i8Def);
2708 if (RT_SUCCESS(rc))
2709 {
2710 if ( !(u64 & UINT64_C(0xffffffffffffff80))
2711 || (u64 & UINT64_C(0xffffffffffffff80)) == UINT64_C(0xffffffffffffff80))
2712 *pi8 = (int8_t)u64;
2713 else
2714 rc = VERR_CFGM_INTEGER_TOO_BIG;
2715 }
2716 if (RT_FAILURE(rc))
2717 *pi8 = i8Def;
2718 return rc;
2719}
2720
2721
2722/**
2723 * Query boolean integer value.
2724 *
2725 * @returns VBox status code.
2726 * @param pNode Which node to search for pszName in.
2727 * @param pszName Name of an integer value.
2728 * @param pf Where to store the value.
2729 * @remark This function will interpret any non-zero value as true.
2730 */
2731VMMR3DECL(int) CFGMR3QueryBool(PCFGMNODE pNode, const char *pszName, bool *pf)
2732{
2733 uint64_t u64;
2734 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
2735 if (RT_SUCCESS(rc))
2736 *pf = u64 ? true : false;
2737 return rc;
2738}
2739
2740
2741/**
2742 * Query boolean integer value with default.
2743 *
2744 * @returns VBox status code.
2745 * @param pNode Which node to search for pszName in.
2746 * @param pszName Name of an integer value.
2747 * @param pf Where to store the value. Set to default on failure.
2748 * @param fDef The default value.
2749 * @remark This function will interpret any non-zero value as true.
2750 */
2751VMMR3DECL(int) CFGMR3QueryBoolDef(PCFGMNODE pNode, const char *pszName, bool *pf, bool fDef)
2752{
2753 uint64_t u64;
2754 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, fDef);
2755 *pf = u64 ? true : false;
2756 return rc;
2757}
2758
2759
2760/**
2761 * Query I/O port address value.
2762 *
2763 * @returns VBox status code.
2764 * @param pNode Which node to search for pszName in.
2765 * @param pszName Name of an integer value.
2766 * @param pPort Where to store the value.
2767 */
2768VMMR3DECL(int) CFGMR3QueryPort(PCFGMNODE pNode, const char *pszName, PRTIOPORT pPort)
2769{
2770 AssertCompileSize(RTIOPORT, 2);
2771 return CFGMR3QueryU16(pNode, pszName, pPort);
2772}
2773
2774
2775/**
2776 * Query I/O port address value with default.
2777 *
2778 * @returns VBox status code.
2779 * @param pNode Which node to search for pszName in.
2780 * @param pszName Name of an integer value.
2781 * @param pPort Where to store the value. Set to default on failure.
2782 * @param PortDef The default value.
2783 */
2784VMMR3DECL(int) CFGMR3QueryPortDef(PCFGMNODE pNode, const char *pszName, PRTIOPORT pPort, RTIOPORT PortDef)
2785{
2786 AssertCompileSize(RTIOPORT, 2);
2787 return CFGMR3QueryU16Def(pNode, pszName, pPort, PortDef);
2788}
2789
2790
2791/**
2792 * Query unsigned int address value.
2793 *
2794 * @returns VBox status code.
2795 * @param pNode Which node to search for pszName in.
2796 * @param pszName Name of an integer value.
2797 * @param pu Where to store the value.
2798 */
2799VMMR3DECL(int) CFGMR3QueryUInt(PCFGMNODE pNode, const char *pszName, unsigned int *pu)
2800{
2801 AssertCompileSize(unsigned int, 4);
2802 return CFGMR3QueryU32(pNode, pszName, (uint32_t *)pu);
2803}
2804
2805
2806/**
2807 * Query unsigned int address value with default.
2808 *
2809 * @returns VBox status code.
2810 * @param pNode Which node to search for pszName in.
2811 * @param pszName Name of an integer value.
2812 * @param pu Where to store the value. Set to default on failure.
2813 * @param uDef The default value.
2814 */
2815VMMR3DECL(int) CFGMR3QueryUIntDef(PCFGMNODE pNode, const char *pszName, unsigned int *pu, unsigned int uDef)
2816{
2817 AssertCompileSize(unsigned int, 4);
2818 return CFGMR3QueryU32Def(pNode, pszName, (uint32_t *)pu, uDef);
2819}
2820
2821
2822/**
2823 * Query signed int address value.
2824 *
2825 * @returns VBox status code.
2826 * @param pNode Which node to search for pszName in.
2827 * @param pszName Name of an integer value.
2828 * @param pi Where to store the value.
2829 */
2830VMMR3DECL(int) CFGMR3QuerySInt(PCFGMNODE pNode, const char *pszName, signed int *pi)
2831{
2832 AssertCompileSize(signed int, 4);
2833 return CFGMR3QueryS32(pNode, pszName, (int32_t *)pi);
2834}
2835
2836
2837/**
2838 * Query unsigned int address value with default.
2839 *
2840 * @returns VBox status code.
2841 * @param pNode Which node to search for pszName in.
2842 * @param pszName Name of an integer value.
2843 * @param pi Where to store the value. Set to default on failure.
2844 * @param iDef The default value.
2845 */
2846VMMR3DECL(int) CFGMR3QuerySIntDef(PCFGMNODE pNode, const char *pszName, signed int *pi, signed int iDef)
2847{
2848 AssertCompileSize(signed int, 4);
2849 return CFGMR3QueryS32Def(pNode, pszName, (int32_t *)pi, iDef);
2850}
2851
2852
2853/**
2854 * Query pointer integer value.
2855 *
2856 * @returns VBox status code.
2857 * @param pNode Which node to search for pszName in.
2858 * @param pszName Name of an integer value.
2859 * @param ppv Where to store the value.
2860 */
2861VMMR3DECL(int) CFGMR3QueryPtr(PCFGMNODE pNode, const char *pszName, void **ppv)
2862{
2863 uint64_t u64;
2864 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
2865 if (RT_SUCCESS(rc))
2866 {
2867 uintptr_t u = (uintptr_t)u64;
2868 if (u64 == u)
2869 *ppv = (void *)u;
2870 else
2871 rc = VERR_CFGM_INTEGER_TOO_BIG;
2872 }
2873 return rc;
2874}
2875
2876
2877/**
2878 * Query pointer integer value with default.
2879 *
2880 * @returns VBox status code.
2881 * @param pNode Which node to search for pszName in.
2882 * @param pszName Name of an integer value.
2883 * @param ppv Where to store the value. Set to default on failure.
2884 * @param pvDef The default value.
2885 */
2886VMMR3DECL(int) CFGMR3QueryPtrDef(PCFGMNODE pNode, const char *pszName, void **ppv, void *pvDef)
2887{
2888 uint64_t u64;
2889 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, (uintptr_t)pvDef);
2890 if (RT_SUCCESS(rc))
2891 {
2892 uintptr_t u = (uintptr_t)u64;
2893 if (u64 == u)
2894 *ppv = (void *)u;
2895 else
2896 rc = VERR_CFGM_INTEGER_TOO_BIG;
2897 }
2898 if (RT_FAILURE(rc))
2899 *ppv = pvDef;
2900 return rc;
2901}
2902
2903
2904/**
2905 * Query Guest Context pointer integer value.
2906 *
2907 * @returns VBox status code.
2908 * @param pNode Which node to search for pszName in.
2909 * @param pszName Name of an integer value.
2910 * @param pGCPtr Where to store the value.
2911 */
2912VMMR3DECL(int) CFGMR3QueryGCPtr(PCFGMNODE pNode, const char *pszName, PRTGCPTR pGCPtr)
2913{
2914 uint64_t u64;
2915 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
2916 if (RT_SUCCESS(rc))
2917 {
2918 RTGCPTR u = (RTGCPTR)u64;
2919 if (u64 == u)
2920 *pGCPtr = u;
2921 else
2922 rc = VERR_CFGM_INTEGER_TOO_BIG;
2923 }
2924 return rc;
2925}
2926
2927
2928/**
2929 * Query Guest Context pointer integer value with default.
2930 *
2931 * @returns VBox status code.
2932 * @param pNode Which node to search for pszName in.
2933 * @param pszName Name of an integer value.
2934 * @param pGCPtr Where to store the value. Set to default on failure.
2935 * @param GCPtrDef The default value.
2936 */
2937VMMR3DECL(int) CFGMR3QueryGCPtrDef(PCFGMNODE pNode, const char *pszName, PRTGCPTR pGCPtr, RTGCPTR GCPtrDef)
2938{
2939 uint64_t u64;
2940 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, GCPtrDef);
2941 if (RT_SUCCESS(rc))
2942 {
2943 RTGCPTR u = (RTGCPTR)u64;
2944 if (u64 == u)
2945 *pGCPtr = u;
2946 else
2947 rc = VERR_CFGM_INTEGER_TOO_BIG;
2948 }
2949 if (RT_FAILURE(rc))
2950 *pGCPtr = GCPtrDef;
2951 return rc;
2952}
2953
2954
2955/**
2956 * Query Guest Context unsigned pointer value.
2957 *
2958 * @returns VBox status code.
2959 * @param pNode Which node to search for pszName in.
2960 * @param pszName Name of an integer value.
2961 * @param pGCPtr Where to store the value.
2962 */
2963VMMR3DECL(int) CFGMR3QueryGCPtrU(PCFGMNODE pNode, const char *pszName, PRTGCUINTPTR pGCPtr)
2964{
2965 uint64_t u64;
2966 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
2967 if (RT_SUCCESS(rc))
2968 {
2969 RTGCUINTPTR u = (RTGCUINTPTR)u64;
2970 if (u64 == u)
2971 *pGCPtr = u;
2972 else
2973 rc = VERR_CFGM_INTEGER_TOO_BIG;
2974 }
2975 return rc;
2976}
2977
2978
2979/**
2980 * Query Guest Context unsigned pointer value with default.
2981 *
2982 * @returns VBox status code.
2983 * @param pNode Which node to search for pszName in.
2984 * @param pszName Name of an integer value.
2985 * @param pGCPtr Where to store the value. Set to default on failure.
2986 * @param GCPtrDef The default value.
2987 */
2988VMMR3DECL(int) CFGMR3QueryGCPtrUDef(PCFGMNODE pNode, const char *pszName, PRTGCUINTPTR pGCPtr, RTGCUINTPTR GCPtrDef)
2989{
2990 uint64_t u64;
2991 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, GCPtrDef);
2992 if (RT_SUCCESS(rc))
2993 {
2994 RTGCUINTPTR u = (RTGCUINTPTR)u64;
2995 if (u64 == u)
2996 *pGCPtr = u;
2997 else
2998 rc = VERR_CFGM_INTEGER_TOO_BIG;
2999 }
3000 if (RT_FAILURE(rc))
3001 *pGCPtr = GCPtrDef;
3002 return rc;
3003}
3004
3005
3006/**
3007 * Query Guest Context signed pointer value.
3008 *
3009 * @returns VBox status code.
3010 * @param pNode Which node to search for pszName in.
3011 * @param pszName Name of an integer value.
3012 * @param pGCPtr Where to store the value.
3013 */
3014VMMR3DECL(int) CFGMR3QueryGCPtrS(PCFGMNODE pNode, const char *pszName, PRTGCINTPTR pGCPtr)
3015{
3016 uint64_t u64;
3017 int rc = CFGMR3QueryInteger(pNode, pszName, &u64);
3018 if (RT_SUCCESS(rc))
3019 {
3020 RTGCINTPTR u = (RTGCINTPTR)u64;
3021 if (u64 == (uint64_t)u)
3022 *pGCPtr = u;
3023 else
3024 rc = VERR_CFGM_INTEGER_TOO_BIG;
3025 }
3026 return rc;
3027}
3028
3029
3030/**
3031 * Query Guest Context signed pointer value with default.
3032 *
3033 * @returns VBox status code.
3034 * @param pNode Which node to search for pszName in.
3035 * @param pszName Name of an integer value.
3036 * @param pGCPtr Where to store the value. Set to default on failure.
3037 * @param GCPtrDef The default value.
3038 */
3039VMMR3DECL(int) CFGMR3QueryGCPtrSDef(PCFGMNODE pNode, const char *pszName, PRTGCINTPTR pGCPtr, RTGCINTPTR GCPtrDef)
3040{
3041 uint64_t u64;
3042 int rc = CFGMR3QueryIntegerDef(pNode, pszName, &u64, GCPtrDef);
3043 if (RT_SUCCESS(rc))
3044 {
3045 RTGCINTPTR u = (RTGCINTPTR)u64;
3046 if (u64 == (uint64_t)u)
3047 *pGCPtr = u;
3048 else
3049 rc = VERR_CFGM_INTEGER_TOO_BIG;
3050 }
3051 if (RT_FAILURE(rc))
3052 *pGCPtr = GCPtrDef;
3053 return rc;
3054}
3055
3056
3057/**
3058 * Query zero terminated character value storing it in a
3059 * buffer allocated from the MM heap.
3060 *
3061 * @returns VBox status code.
3062 * @param pNode Which node to search for pszName in.
3063 * @param pszName Value name. This value must be of zero terminated character string type.
3064 * @param ppszString Where to store the string pointer.
3065 * Free this using MMR3HeapFree() (or RTStrFree if not
3066 * associated with a pUVM - see CFGMR3CreateTree).
3067 */
3068VMMR3DECL(int) CFGMR3QueryStringAlloc(PCFGMNODE pNode, const char *pszName, char **ppszString)
3069{
3070 size_t cbString;
3071 int rc = CFGMR3QuerySize(pNode, pszName, &cbString);
3072 if (RT_SUCCESS(rc))
3073 {
3074 char *pszString = cfgmR3StrAlloc(pNode->pVM, MM_TAG_CFGM_USER, cbString);
3075 if (pszString)
3076 {
3077 rc = CFGMR3QueryString(pNode, pszName, pszString, cbString);
3078 if (RT_SUCCESS(rc))
3079 *ppszString = pszString;
3080 else
3081 cfgmR3StrFree(pNode->pVM, pszString);
3082 }
3083 else
3084 rc = VERR_NO_MEMORY;
3085 }
3086 return rc;
3087}
3088
3089
3090/**
3091 * Query zero terminated character value storing it in a
3092 * buffer allocated from the MM heap.
3093 *
3094 * @returns VBox status code.
3095 * @param pNode Which node to search for pszName in. This cannot be
3096 * NULL if @a pszDef is not NULL, because we need
3097 * somewhere way to get to the VM in order to call
3098 * MMR3HeapStrDup.
3099 * @param pszName Value name. This value must be of zero terminated character string type.
3100 * @param ppszString Where to store the string pointer. Not set on failure.
3101 * Free this using MMR3HeapFree() (or RTStrFree if not
3102 * associated with a pUVM - see CFGMR3CreateTree).
3103 * @param pszDef The default return value. This can be NULL.
3104 */
3105VMMR3DECL(int) CFGMR3QueryStringAllocDef(PCFGMNODE pNode, const char *pszName, char **ppszString, const char *pszDef)
3106{
3107 Assert(pNode || !pszDef); /* We need pVM if we need to duplicate the string later. */
3108
3109 /*
3110 * (Don't call CFGMR3QuerySize and CFGMR3QueryStringDef here as the latter
3111 * cannot handle pszDef being NULL.)
3112 */
3113 PCFGMLEAF pLeaf;
3114 int rc = cfgmR3ResolveLeaf(pNode, pszName, &pLeaf);
3115 if (RT_SUCCESS(rc))
3116 {
3117 if (pLeaf->enmType == CFGMVALUETYPE_STRING)
3118 {
3119 size_t const cbSrc = pLeaf->Value.String.cb;
3120 char *pszString = cfgmR3StrAlloc(pNode->pVM, MM_TAG_CFGM_USER, cbSrc);
3121 if (pszString)
3122 {
3123 memcpy(pszString, pLeaf->Value.String.psz, cbSrc);
3124 *ppszString = pszString;
3125 }
3126 else
3127 rc = VERR_NO_MEMORY;
3128 }
3129 else
3130 rc = VERR_CFGM_NOT_STRING;
3131 }
3132 if (RT_FAILURE(rc))
3133 {
3134 if (!pszDef)
3135 *ppszString = NULL;
3136 else
3137 {
3138 size_t const cbDef = strlen(pszDef) + 1;
3139 *ppszString = cfgmR3StrAlloc(pNode->pVM, MM_TAG_CFGM_USER, cbDef);
3140 memcpy(*ppszString, pszDef, cbDef);
3141 }
3142 if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
3143 rc = VINF_SUCCESS;
3144 }
3145
3146 return rc;
3147}
3148
3149
3150/**
3151 * Dumps the configuration (sub)tree to the release log.
3152 *
3153 * @param pRoot The root node of the dump.
3154 */
3155VMMR3DECL(void) CFGMR3Dump(PCFGMNODE pRoot)
3156{
3157 bool fOldBuffered = RTLogRelSetBuffering(true /*fBuffered*/);
3158 LogRel(("************************* CFGM dump *************************\n"));
3159 cfgmR3Dump(pRoot, 0, DBGFR3InfoLogRelHlp());
3160 LogRel(("********************* End of CFGM dump **********************\n"));
3161 RTLogRelSetBuffering(fOldBuffered);
3162}
3163
3164
3165/**
3166 * Info handler, internal version.
3167 *
3168 * @param pVM The cross context VM structure.
3169 * @param pHlp Callback functions for doing output.
3170 * @param pszArgs Argument string. Optional and specific to the handler.
3171 */
3172static DECLCALLBACK(void) cfgmR3Info(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
3173{
3174 /*
3175 * Figure where to start.
3176 */
3177 PCFGMNODE pRoot = pVM->cfgm.s.pRoot;
3178 if (pszArgs && *pszArgs)
3179 {
3180 int rc = cfgmR3ResolveNode(pRoot, pszArgs, &pRoot);
3181 if (RT_FAILURE(rc))
3182 {
3183 pHlp->pfnPrintf(pHlp, "Failed to resolve CFGM path '%s', %Rrc", pszArgs, rc);
3184 return;
3185 }
3186 }
3187
3188 /*
3189 * Dump the specified tree.
3190 */
3191 pHlp->pfnPrintf(pHlp, "pRoot=%p:{", pRoot);
3192 cfgmR3DumpPath(pRoot, pHlp);
3193 pHlp->pfnPrintf(pHlp, "}\n");
3194 cfgmR3Dump(pRoot, 0, pHlp);
3195}
3196
3197
3198/**
3199 * Recursively prints a path name.
3200 */
3201static void cfgmR3DumpPath(PCFGMNODE pNode, PCDBGFINFOHLP pHlp)
3202{
3203 if (pNode->pParent)
3204 cfgmR3DumpPath(pNode->pParent, pHlp);
3205 pHlp->pfnPrintf(pHlp, "%s/", pNode->szName);
3206}
3207
3208
3209/**
3210 * Dumps a branch of a tree.
3211 */
3212static void cfgmR3Dump(PCFGMNODE pRoot, unsigned iLevel, PCDBGFINFOHLP pHlp)
3213{
3214 /*
3215 * Path.
3216 */
3217 pHlp->pfnPrintf(pHlp, "[");
3218 cfgmR3DumpPath(pRoot, pHlp);
3219 pHlp->pfnPrintf(pHlp, "] (level %d)%s\n", iLevel, pRoot->fRestrictedRoot ? " (restricted root)" : "");
3220
3221 /*
3222 * Values.
3223 */
3224 PCFGMLEAF pLeaf;
3225 size_t cchMax = 0;
3226 for (pLeaf = CFGMR3GetFirstValue(pRoot); pLeaf; pLeaf = CFGMR3GetNextValue(pLeaf))
3227 cchMax = RT_MAX(cchMax, pLeaf->cchName);
3228 for (pLeaf = CFGMR3GetFirstValue(pRoot); pLeaf; pLeaf = CFGMR3GetNextValue(pLeaf))
3229 {
3230 switch (CFGMR3GetValueType(pLeaf))
3231 {
3232 case CFGMVALUETYPE_INTEGER:
3233 {
3234 pHlp->pfnPrintf(pHlp, " %-*s <integer> = %#018llx (%'lld", (int)cchMax, pLeaf->szName, pLeaf->Value.Integer.u64, pLeaf->Value.Integer.u64);
3235 if ( ( pLeaf->cchName >= 4
3236 && !RTStrCmp(&pLeaf->szName[pLeaf->cchName - 4], "Size"))
3237 || ( pLeaf->cchName >= 2
3238 && !RTStrNCmp(pLeaf->szName, "cb", 2)) )
3239 {
3240 if (pLeaf->Value.Integer.u64 > _2M)
3241 pHlp->pfnPrintf(pHlp, ", %'lld MB", pLeaf->Value.Integer.u64 / _1M);
3242 else if (pLeaf->Value.Integer.u64 > _2K)
3243 pHlp->pfnPrintf(pHlp, ", %'lld KB", pLeaf->Value.Integer.u64 / _1K);
3244 if (pLeaf->Value.Integer.u64 > _2G)
3245 pHlp->pfnPrintf(pHlp, ", %'lld.%lld GB",
3246 pLeaf->Value.Integer.u64 / _1G,
3247 (pLeaf->Value.Integer.u64 % _1G) / (_1G / 10));
3248 }
3249 pHlp->pfnPrintf(pHlp, ")\n");
3250 break;
3251 }
3252
3253 case CFGMVALUETYPE_STRING:
3254 pHlp->pfnPrintf(pHlp, " %-*s <string> = \"%s\" (cb=%zu)\n", (int)cchMax, pLeaf->szName, pLeaf->Value.String.psz, pLeaf->Value.String.cb);
3255 break;
3256
3257 case CFGMVALUETYPE_BYTES:
3258 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);
3259 break;
3260
3261 default:
3262 AssertMsgFailed(("bad leaf!\n"));
3263 break;
3264 }
3265 }
3266 pHlp->pfnPrintf(pHlp, "\n");
3267
3268 /*
3269 * Children.
3270 */
3271 for (PCFGMNODE pChild = CFGMR3GetFirstChild(pRoot); pChild; pChild = CFGMR3GetNextChild(pChild))
3272 {
3273 Assert(pChild->pNext != pChild);
3274 Assert(pChild->pPrev != pChild);
3275 Assert(pChild->pPrev != pChild->pNext || !pChild->pPrev);
3276 Assert(pChild->pFirstChild != pChild);
3277 Assert(pChild->pParent == pRoot);
3278 cfgmR3Dump(pChild, iLevel + 1, pHlp);
3279 }
3280}
3281
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use