VirtualBox

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

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

CFGM: remove obsolete code (now handled in Main)

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

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