VirtualBox

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

Last change on this file since 12986 was 12635, checked in by vboxsync, 16 years ago

#1865: CFGM.

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