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