VirtualBox

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

Last change on this file since 24323 was 23587, checked in by vboxsync, 15 years ago

CFGM: Optimize the lookups.

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

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