1 | /*␊ |
2 | * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights␊ |
5 | * Reserved. This file contains Original Code and/or Modifications of␊ |
6 | * Original Code as defined in and that are subject to the Apple Public␊ |
7 | * Source License Version 2.0 (the "License"). You may not use this file␊ |
8 | * except in compliance with the License. Please obtain a copy of the␊ |
9 | * License at http://www.apple.com/publicsource and read it before using␊ |
10 | * this file.␊ |
11 | * ␊ |
12 | * The Original Code and all software distributed under the License are␊ |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the␊ |
17 | * License for the specific language governing rights and limitations␊ |
18 | * under the License.␊ |
19 | * ␊ |
20 | */␊ |
21 | ␊ |
22 | #if 1␊ |
23 | /*␊ |
24 | ␊ |
25 | Structures for a Flattened Device Tree ␊ |
26 | */␊ |
27 | ␊ |
28 | #define kPropNameLength 32␊ |
29 | ␊ |
30 | typedef struct DeviceTreeNodeProperty {␊ |
31 | ␉char name[kPropNameLength]; // NUL terminated property name␊ |
32 | ␉unsigned long length; // Length (bytes) of folloing prop value␊ |
33 | // unsigned long value[1]; // Variable length value of property␊ |
34 | // Padded to a multiple of a longword?␊ |
35 | } DeviceTreeNodeProperty;␊ |
36 | ␊ |
37 | typedef struct OpaqueDTEntry {␊ |
38 | ␉unsigned long nProperties; // Number of props[] elements (0 => end)␊ |
39 | ␉unsigned long nChildren; // Number of children[] elements␊ |
40 | // DeviceTreeNodeProperty props[];// array size == nProperties␊ |
41 | // DeviceTreeNode children[]; // array size == nChildren␊ |
42 | } DeviceTreeNode;␊ |
43 | ␊ |
44 | typedef char DTPropertyNameBuf[32];␊ |
45 | ␊ |
46 | // Entry Name Definitions (Entry Names are C-Strings).␊ |
47 | enum {␊ |
48 | kDTMaxEntryNameLength = 31 /* Max length of a C-String Entry Name (terminator not included) */␊ |
49 | };␊ |
50 | ␊ |
51 | /* length of DTEntryNameBuf = kDTMaxEntryNameLength +1*/␊ |
52 | typedef char DTEntryNameBuf[32];␊ |
53 | #endif␊ |
54 | ␊ |
55 | #include "libsaio.h"␊ |
56 | #include "device_tree.h"␊ |
57 | ␊ |
58 | #if DEBUG␊ |
59 | #define DPRINTF(args...) printf(args)␊ |
60 | void␊ |
61 | DT__PrintTree(Node *node);␊ |
62 | #else␊ |
63 | #define DPRINTF(args...)␊ |
64 | #endif␊ |
65 | ␊ |
66 | ␊ |
67 | #define RoundToLong(x)␉(((x) + 3) & ~3)␊ |
68 | ␊ |
69 | static struct _DTSizeInfo {␊ |
70 | ␉uint32_t␉numNodes;␊ |
71 | ␉uint32_t␉numProperties;␊ |
72 | ␉uint32_t␉totalPropertySize;␊ |
73 | } DTInfo;␊ |
74 | ␊ |
75 | #define kAllocSize 4096␊ |
76 | ␊ |
77 | static Node *rootNode;␊ |
78 | ␊ |
79 | static Node *freeNodes, *allocedNodes;␊ |
80 | static Property *freeProperties, *allocedProperties;␊ |
81 | ␊ |
82 | ␊ |
83 | //==============================================================================␊ |
84 | ␊ |
85 | Property *DT__AddProperty(Node *node, const char *name, uint32_t length, void *value)␊ |
86 | {␊ |
87 | ␉Property *prop;␊ |
88 | ␊ |
89 | ␉DPRINTF("DT__AddProperty([Node '%s'], '%s', %d, 0x%X)\n", DT__GetName(node), name, length, value);␊ |
90 | ␊ |
91 | ␉if (freeProperties == NULL) {␊ |
92 | ␉␉void *buf = malloc(kAllocSize);␊ |
93 | ␉␉int i;␊ |
94 | ␊ |
95 | ␉␉DPRINTF("DT__AddProperty: Allocating more free properties\n");␊ |
96 | ␊ |
97 | ␉␉if (buf == 0) {␊ |
98 | ␉␉␉return 0;␊ |
99 | ␉␉}␊ |
100 | ␊ |
101 | ␉␉bzero(buf, kAllocSize);␊ |
102 | ␉␉// Use the first property to record the allocated buffer␊ |
103 | ␉␉// for later freeing.␊ |
104 | ␉␉prop = (Property *)buf;␊ |
105 | ␉␉prop->next = allocedProperties;␊ |
106 | ␉␉allocedProperties = prop;␊ |
107 | ␉␉prop->value = buf;␊ |
108 | ␉␉prop++;␊ |
109 | ␊ |
110 | ␉␉for (i = 1; i < (kAllocSize / sizeof(Property)); i++) {␊ |
111 | ␉␉␉prop->next = freeProperties;␊ |
112 | ␉␉␉freeProperties = prop;␊ |
113 | ␉␉␉prop++;␊ |
114 | ␉␉}␊ |
115 | ␉}␊ |
116 | ␊ |
117 | ␉prop = freeProperties;␊ |
118 | ␉freeProperties = prop->next;␊ |
119 | ␊ |
120 | ␉prop->name = name;␊ |
121 | ␉prop->length = length;␊ |
122 | ␉prop->value = value;␊ |
123 | ␊ |
124 | ␉// Always add to end of list␊ |
125 | ␉if (node->properties == 0) {␊ |
126 | ␉␉node->properties = prop;␊ |
127 | ␉} else {␊ |
128 | ␉␉node->last_prop->next = prop;␊ |
129 | ␉}␊ |
130 | ␊ |
131 | ␉node->last_prop = prop;␊ |
132 | ␉prop->next = 0;␊ |
133 | ␊ |
134 | ␉DPRINTF("DT__AddProperty: done.\n");␊ |
135 | ␊ |
136 | ␉DTInfo.numProperties++;␊ |
137 | ␉DTInfo.totalPropertySize += RoundToLong(length);␊ |
138 | ␊ |
139 | ␉return prop;␊ |
140 | }␊ |
141 | ␊ |
142 | ␊ |
143 | //==============================================================================␊ |
144 | ␊ |
145 | Node *DT__AddChild(Node *parent, const char *name)␊ |
146 | {␊ |
147 | ␉Node *node;␊ |
148 | ␊ |
149 | ␉if (freeNodes == NULL)␊ |
150 | ␉{␊ |
151 | ␉␉void *buf = malloc(kAllocSize);␊ |
152 | ␊ |
153 | ␉␉if (buf == 0)␊ |
154 | ␉␉{␊ |
155 | ␉␉␉return 0;␊ |
156 | ␉␉}␊ |
157 | ␊ |
158 | ␉␉int i;␊ |
159 | ␊ |
160 | ␉␉DPRINTF("DT__AddChild: Allocating more free nodes\n");␊ |
161 | ␊ |
162 | ␉␉bzero(buf, kAllocSize);␊ |
163 | ␉␉node = (Node *)buf;␊ |
164 | ␊ |
165 | ␉␉// Use the first node to record the allocated buffer for later freeing.␊ |
166 | ␉␉node->next = allocedNodes;␊ |
167 | ␉␉allocedNodes = node;␊ |
168 | ␉␉node->children = (Node *)buf;␊ |
169 | ␉␉node++;␊ |
170 | ␊ |
171 | ␉␉for (i = 1; i < (kAllocSize / sizeof(Node)); i++)␊ |
172 | ␉␉{␊ |
173 | ␉␉␉node->next = freeNodes;␊ |
174 | ␉␉␉freeNodes = node;␊ |
175 | ␉␉␉node++;␊ |
176 | ␉␉}␊ |
177 | ␉}␊ |
178 | ␊ |
179 | ␉DPRINTF("DT__AddChild(0x%x, '%s')\n", parent, name);␊ |
180 | ␊ |
181 | ␉node = freeNodes;␊ |
182 | ␉freeNodes = node->next;␊ |
183 | ␊ |
184 | ␉DPRINTF("DT__AddChild: Got free node 0x%x\n", node);␊ |
185 | ␉DPRINTF("DT__AddChild: prop = 0x%x, children = 0x%x, next = 0x%x\n", node->properties, node->children, node->next);␊ |
186 | ␊ |
187 | ␉if (parent == NULL)␊ |
188 | ␉{␊ |
189 | ␉␉rootNode = node;␊ |
190 | ␉␉node->next = 0;␊ |
191 | ␉}␊ |
192 | ␉else␊ |
193 | ␉{␊ |
194 | ␉␉node->next = parent->children;␊ |
195 | ␉␉parent->children = node;␊ |
196 | ␉}␊ |
197 | ␊ |
198 | ␉DTInfo.numNodes++;␊ |
199 | ␉DT__AddProperty(node, "name", strlen(name) + 1, (void *) name);␊ |
200 | ␊ |
201 | ␉return node;␊ |
202 | }␊ |
203 | ␊ |
204 | ␊ |
205 | //==============================================================================␊ |
206 | ␊ |
207 | void DT__FreeProperty(Property *prop)␊ |
208 | {␊ |
209 | ␉prop->next = freeProperties;␊ |
210 | ␉freeProperties = prop;␊ |
211 | }␊ |
212 | ␊ |
213 | //==============================================================================␊ |
214 | ␊ |
215 | void DT__FreeNode(Node *node)␊ |
216 | {␊ |
217 | ␉node->next = freeNodes;␊ |
218 | ␉freeNodes = node;␊ |
219 | }␊ |
220 | ␊ |
221 | //==============================================================================␊ |
222 | ␊ |
223 | Node *DT__Initialize(void)␊ |
224 | {␊ |
225 | ␉//DPRINTF("DT__Initialize\n");␊ |
226 | ␊ |
227 | ␉freeNodes = 0;␊ |
228 | ␉allocedNodes = 0;␊ |
229 | ␉freeProperties = 0;␊ |
230 | ␉allocedProperties = 0;␊ |
231 | ␊ |
232 | ␉DTInfo.numNodes = 0;␊ |
233 | ␉DTInfo.numProperties = 0;␊ |
234 | ␉DTInfo.totalPropertySize = 0;␊ |
235 | ␊ |
236 | ␉rootNode = DT__AddChild(NULL, "/");␊ |
237 | ␉DPRINTF("DT__Initialize: done\n");␊ |
238 | ␉return rootNode;␊ |
239 | }␊ |
240 | ␊ |
241 | //==============================================================================␊ |
242 | ␊ |
243 | Node *DT__GetRootNode(void)␊ |
244 | {␊ |
245 | ␉return rootNode;␊ |
246 | }␊ |
247 | ␊ |
248 | //==============================================================================␊ |
249 | /*␊ |
250 | * Free up memory used by in-memory representation of device tree.␊ |
251 | */␊ |
252 | void DT__Finalize(void)␊ |
253 | {␊ |
254 | ␉Node *node;␊ |
255 | ␉Property *prop;␊ |
256 | ␊ |
257 | ␉DPRINTF("DT__Finalize\n");␊ |
258 | ␊ |
259 | ␉for (prop = allocedProperties; prop != NULL; prop = prop->next)␊ |
260 | ␉{␊ |
261 | ␉␉free((void *)(prop->name));␊ |
262 | ␉␉free(prop->value);␊ |
263 | ␉}␊ |
264 | ␊ |
265 | ␉allocedProperties = NULL;␊ |
266 | ␉freeProperties = NULL;␊ |
267 | ␊ |
268 | ␉for (node = allocedNodes; node != NULL; node = node->next)␊ |
269 | ␉{␊ |
270 | ␉␉free((void *)node->children);␊ |
271 | ␉}␊ |
272 | ␊ |
273 | ␉allocedNodes = NULL;␊ |
274 | ␉freeNodes = NULL;␊ |
275 | ␉rootNode = NULL;␊ |
276 | ␊ |
277 | ␉// XXX leaks any created strings␊ |
278 | ␉DTInfo.numNodes = 0;␊ |
279 | ␉DTInfo.numProperties = 0;␊ |
280 | ␉DTInfo.totalPropertySize = 0;␊ |
281 | }␊ |
282 | ␊ |
283 | //==============================================================================␊ |
284 | ␊ |
285 | static void *FlattenNodes(Node *node, void *buffer)␊ |
286 | {␊ |
287 | ␉Property *prop;␊ |
288 | ␉DeviceTreeNode *flatNode;␊ |
289 | ␉DeviceTreeNodeProperty *flatProp;␊ |
290 | ␉int count;␊ |
291 | ␊ |
292 | ␉if (node == 0)␊ |
293 | ␉{␊ |
294 | ␉␉return buffer;␊ |
295 | ␉}␊ |
296 | ␊ |
297 | ␉flatNode = (DeviceTreeNode *)buffer;␊ |
298 | ␉buffer += sizeof(DeviceTreeNode);␊ |
299 | ␊ |
300 | ␉for (count = 0, prop = node->properties; prop != 0; count++, prop = prop->next)␊ |
301 | ␉{␊ |
302 | ␉␉flatProp = (DeviceTreeNodeProperty *)buffer;␊ |
303 | ␉␉strncpy(flatProp->name, prop->name, kPropNameLength);␊ |
304 | ␉␉flatProp->length = prop->length;␊ |
305 | ␉␉buffer += sizeof(DeviceTreeNodeProperty);␊ |
306 | ␉␉bcopy(prop->value, buffer, prop->length);␊ |
307 | ␉␉buffer += RoundToLong(prop->length);␊ |
308 | ␉}␊ |
309 | ␊ |
310 | ␉flatNode->nProperties = count;␊ |
311 | ␊ |
312 | ␉for (count = 0, node = node->children; node != 0; count++, node = node->next)␊ |
313 | ␉{␊ |
314 | ␉␉buffer = FlattenNodes(node, buffer);␊ |
315 | ␉}␊ |
316 | ␊ |
317 | ␉flatNode->nChildren = count;␊ |
318 | ␊ |
319 | ␉return buffer;␊ |
320 | }␊ |
321 | ␊ |
322 | ␊ |
323 | /*==============================================================================␊ |
324 | * Flatten the in-memory representation of the device tree into a binary DT block.␊ |
325 | * To get the buffer size needed, call with result = 0.␊ |
326 | * To have a buffer allocated for you, call with *result = 0.␊ |
327 | * To use your own buffer, call with *result = &buffer.␊ |
328 | */␊ |
329 | ␊ |
330 | void DT__FlattenDeviceTree(void **buffer_p, uint32_t *length)␊ |
331 | {␊ |
332 | ␉uint32_t totalSize;␊ |
333 | ␉void * buf;␊ |
334 | ␊ |
335 | ␉DPRINTF("DT__FlattenDeviceTree(0x%x, 0x%x)\n", buffer_p, length);␊ |
336 | ␊ |
337 | #if DEBUG␊ |
338 | ␉if (buffer_p)␊ |
339 | ␉{␊ |
340 | ␉␉DT__PrintTree(rootNode);␊ |
341 | ␉}␊ |
342 | #endif␊ |
343 | ␊ |
344 | ␉totalSize = DTInfo.numNodes * sizeof(DeviceTreeNode) + ␊ |
345 | ␉␉DTInfo.numProperties * sizeof(DeviceTreeNodeProperty) +␊ |
346 | ␉␉DTInfo.totalPropertySize;␊ |
347 | ␊ |
348 | ␉DPRINTF("Total size 0x%x\n", totalSize);␊ |
349 | ␊ |
350 | ␉if (buffer_p != 0)␊ |
351 | ␉{␊ |
352 | ␉␉if (totalSize == 0)␊ |
353 | ␉␉{␊ |
354 | ␉␉␉buf = 0;␊ |
355 | ␉␉}␊ |
356 | ␉␉else␊ |
357 | ␉␉{␊ |
358 | ␉␉␉if (*buffer_p == 0)␊ |
359 | ␉␉␉{␊ |
360 | ␉␉␉␉buf = malloc(totalSize);␊ |
361 | ␉␉␉}␊ |
362 | ␉␉␉else␊ |
363 | ␉␉␉{␊ |
364 | ␉␉␉␉buf = *buffer_p;␊ |
365 | ␉␉␉}␊ |
366 | ␊ |
367 | ␉␉␉bzero(buf, totalSize);␊ |
368 | ␊ |
369 | ␉␉␉FlattenNodes(rootNode, buf);␊ |
370 | ␉␉}␊ |
371 | ␊ |
372 | ␉␉*buffer_p = buf;␊ |
373 | ␉}␊ |
374 | ␊ |
375 | ␉if (length)␊ |
376 | ␉{␊ |
377 | ␉␉*length = totalSize;␊ |
378 | ␉}␊ |
379 | }␊ |
380 | ␊ |
381 | //==============================================================================␊ |
382 | ␊ |
383 | char *DT__GetName(Node *node)␊ |
384 | {␊ |
385 | ␉Property *prop;␊ |
386 | ␊ |
387 | ␉//DPRINTF("DT__GetName(0x%x)\n", node);␊ |
388 | ␉//DPRINTF("Node properties = 0x%x\n", node->properties);␊ |
389 | ␉for (prop = node->properties; prop; prop = prop->next)␊ |
390 | ␉{␊ |
391 | ␉␉//DPRINTF("Prop '%s'\n", prop->name);␊ |
392 | ␉␉if (strcmp(prop->name, "name") == 0)␊ |
393 | ␉␉{␊ |
394 | ␉␉␉return prop->value;␊ |
395 | ␉␉}␊ |
396 | ␉}␊ |
397 | ␊ |
398 | ␉//DPRINTF("DT__GetName returns 0\n");␊ |
399 | ␉return "(null)";␊ |
400 | }␊ |
401 | ␊ |
402 | //==============================================================================␊ |
403 | // Bungo␊ |
404 | Property *DT__GetProperty(Node *node, const char *name)␊ |
405 | {␊ |
406 | ␉Property *prop;␊ |
407 | ␊ |
408 | ␉for (prop = node->properties; prop; prop = prop->next)␊ |
409 | ␉{␊ |
410 | ␉␉if (strcmp(prop->name, name) == 0)␊ |
411 | ␉␉{␊ |
412 | ␉␉␉return prop;␊ |
413 | ␉␉}␊ |
414 | ␉}␊ |
415 | ␊ |
416 | ␉return NULL;␊ |
417 | }␊ |
418 | ␊ |
419 | //==============================================================================␊ |
420 | ␊ |
421 | Node *DT__FindNode(const char *path, bool createIfMissing)␊ |
422 | {␊ |
423 | ␉Node *node, *child;␊ |
424 | ␉DTPropertyNameBuf nameBuf;␊ |
425 | ␉char *bp;␊ |
426 | ␉int i;␊ |
427 | ␊ |
428 | ␉DPRINTF("DT__FindNode('%s', %d)\n", path, createIfMissing);␊ |
429 | ␊ |
430 | ␉// Start at root␊ |
431 | ␉node = rootNode;␊ |
432 | ␊ |
433 | ␉DPRINTF("DT__FindNode: root = 0x%x\n", rootNode);␊ |
434 | ␊ |
435 | ␉while (node)␊ |
436 | ␉{␊ |
437 | ␉␉// Skip leading slash(es)␊ |
438 | ␉␉while (*path == '/')␊ |
439 | ␉␉{␊ |
440 | ␉␉␉path++;␊ |
441 | ␉␉}␊ |
442 | ␊ |
443 | ␉␉for (i = 0, bp = nameBuf; ++i < kDTMaxEntryNameLength && *path && *path != '/'; bp++, path++)␊ |
444 | ␉␉{␊ |
445 | ␉␉␉*bp = *path;␊ |
446 | ␉␉}␊ |
447 | ␊ |
448 | ␉␉*bp = '\0';␊ |
449 | ␊ |
450 | ␉␉if (nameBuf[0] == '\0')␊ |
451 | ␉␉{␊ |
452 | ␉␉␉// last path entry␊ |
453 | ␉␉␉break;␊ |
454 | ␉␉}␊ |
455 | ␊ |
456 | ␉␉DPRINTF("DT__FindNode: Node '%s'\n", nameBuf);␊ |
457 | ␊ |
458 | ␉␉for (child = node->children; child != 0; child = child->next)␊ |
459 | ␉␉{␊ |
460 | ␉␉␉DPRINTF("DT__FindNode: Child 0x%x\n", child);␊ |
461 | ␊ |
462 | ␉␉␉if (strcmp(DT__GetName(child), nameBuf) == 0)␊ |
463 | ␉␉␉{␊ |
464 | ␉␉␉␉break;␊ |
465 | ␉␉␉}␊ |
466 | ␉␉}␊ |
467 | ␊ |
468 | ␉␉if (child == 0 && createIfMissing)␊ |
469 | ␉␉{␊ |
470 | ␉␉␉char *str = malloc(strlen(nameBuf) + 1);␊ |
471 | ␉␉␉// XXX this will leak␊ |
472 | ␉␉␉strcpy(str, nameBuf);␊ |
473 | ␊ |
474 | ␉␉␉child = DT__AddChild(node, str);␊ |
475 | ␉␉␉DPRINTF("DT__FindNode: Creating node: %s\n", str);␊ |
476 | ␉␉}␊ |
477 | ␊ |
478 | ␉␉node = child;␊ |
479 | ␉}␊ |
480 | ␊ |
481 | ␉return node;␊ |
482 | }␊ |
483 | ␊ |
484 | #if DEBUG␊ |
485 | ␊ |
486 | //==============================================================================␊ |
487 | ␊ |
488 | void DT__PrintNode(Node *node, int level)␊ |
489 | {␊ |
490 | ␉char spaces[10], *cp = spaces;␊ |
491 | ␉Property *prop;␊ |
492 | ␊ |
493 | ␉if (level > 9)␊ |
494 | ␉{␊ |
495 | ␉␉level = 9;␊ |
496 | ␉}␊ |
497 | ␊ |
498 | ␉while (level--)␊ |
499 | ␉{␊ |
500 | ␉␉*cp++ = ' ';␊ |
501 | ␉}␊ |
502 | ␊ |
503 | ␉*cp = '\0';␊ |
504 | ␊ |
505 | ␉printf("%s===Node===\n", spaces);␊ |
506 | ␊ |
507 | ␉for (prop = node->properties; prop; prop = prop->next)␊ |
508 | ␉{␊ |
509 | ␉␉char c = *((char *)prop->value);␊ |
510 | ␉␉if (prop->length < 64 && (strcmp(prop->name, "name") == 0 || (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'))␊ |
511 | ␉␉{␊ |
512 | ␉␉␉printf("%s Property '%s' [%d] = '%s'\n", spaces, prop->name, prop->length, prop->value);␊ |
513 | ␉␉}␊ |
514 | ␉␉else␊ |
515 | ␉␉{␊ |
516 | ␉␉␉printf("%s Property '%s' [%d] = (data)\n", spaces, prop->name, prop->length);␊ |
517 | ␉␉}␊ |
518 | ␉}␊ |
519 | ␊ |
520 | ␉printf("%s==========\n", spaces);␊ |
521 | }␊ |
522 | ␊ |
523 | //==============================================================================␊ |
524 | ␊ |
525 | static void _PrintTree(Node *node, int level)␊ |
526 | {␊ |
527 | ␉DT__PrintNode(node, level);␊ |
528 | ␊ |
529 | ␉level++;␊ |
530 | ␊ |
531 | ␉for (node = node->children; node; node = node->next)␊ |
532 | ␉{␊ |
533 | ␉␉_PrintTree(node, level);␊ |
534 | ␉}␊ |
535 | }␊ |
536 | ␊ |
537 | //==============================================================================␊ |
538 | ␊ |
539 | void DT__PrintTree(Node *node)␊ |
540 | {␊ |
541 | ␉if (node == 0) node = rootNode;␊ |
542 | ␉_PrintTree(node, 0);␊ |
543 | }␊ |
544 | ␊ |
545 | //==============================================================================␊ |
546 | ␊ |
547 | void DT__PrintFlattenedNode(DTEntry entry, int level)␊ |
548 | {␊ |
549 | ␉char spaces[10], *cp = spaces;␊ |
550 | ␉DTPropertyIterator␉ propIter;␊ |
551 | ␉char *name;␊ |
552 | ␉void *prop;␊ |
553 | ␉int propSize;␊ |
554 | ␊ |
555 | ␉if (level > 9) level = 9;␊ |
556 | ␉while (level--) *cp++ = ' ';␊ |
557 | ␉␉*cp = '\0';␊ |
558 | ␊ |
559 | ␉printf("%s===Entry %p===\n", spaces, entry);␊ |
560 | ␉␉if (kSuccess != DTCreatePropertyIterator(entry, &propIter))␊ |
561 | ␉␉{␊ |
562 | ␉␉␉printf("Couldn't create property iterator\n");␊ |
563 | ␉␉␉return;␊ |
564 | ␉␉}␊ |
565 | ␉␉while( kSuccess == DTIterateProperties( propIter, &name))␊ |
566 | ␉␉{␊ |
567 | ␉␉␉if( kSuccess != DTGetProperty( entry, name, &prop, &propSize ))␊ |
568 | ␉␉␉␉continue;␊ |
569 | ␉␉printf("%s Property %s = %s\n", spaces, name, prop);␊ |
570 | ␉}␊ |
571 | ␉DTDisposePropertyIterator(propIter);␊ |
572 | ␊ |
573 | ␉printf("%s==========\n", spaces);␊ |
574 | }␊ |
575 | ␊ |
576 | //==============================================================================␊ |
577 | ␊ |
578 | static void _PrintFlattenedTree(DTEntry entry, int level)␊ |
579 | {␊ |
580 | ␉DTEntryIterator entryIter;␊ |
581 | ␊ |
582 | ␉PrintFlattenedNode(entry, level);␊ |
583 | ␊ |
584 | ␉if (kSuccess != DTCreateEntryIterator(entry, &entryIter))␊ |
585 | ␉{␊ |
586 | ␉␉printf("Couldn't create entry iterator\n");␊ |
587 | ␉␉return;␊ |
588 | ␉}␊ |
589 | ␉level++;␊ |
590 | ␉while (kSuccess == DTIterateEntries( entryIter, &entry ))␊ |
591 | ␉{␊ |
592 | ␉␉_PrintFlattenedTree(entry, level);␊ |
593 | ␉}␊ |
594 | ␉DTDisposeEntryIterator(entryIter);␊ |
595 | }␊ |
596 | ␊ |
597 | //==============================================================================␊ |
598 | ␊ |
599 | void DT__PrintFlattenedTree(DTEntry entry)␊ |
600 | {␊ |
601 | ␉_PrintFlattenedTree(entry, 0);␊ |
602 | }␊ |
603 | ␊ |
604 | //==============================================================================␊ |
605 | ␊ |
606 | int main(int argc, char **argv)␊ |
607 | {␊ |
608 | ␉DTEntry␉␉␉dtEntry;␊ |
609 | ␉DTPropertyIterator␉propIter;␊ |
610 | ␉DTEntryIterator␉␉entryIter;␊ |
611 | ␉void␉␉␉*prop;␊ |
612 | ␉int␉␉␉propSize;␊ |
613 | ␉char␉␉␉*name;␊ |
614 | ␉void␉␉␉*flatTree;␊ |
615 | ␉uint32_t␉␉flatSize;␊ |
616 | ␊ |
617 | ␉Node *node;␊ |
618 | ␊ |
619 | ␉node = AddChild(NULL, "device-tree");␊ |
620 | ␉AddProperty(node, "potato", 4, "foo");␊ |
621 | ␉AddProperty(node, "chemistry", 4, "bar");␊ |
622 | ␉AddProperty(node, "physics", 4, "baz");␊ |
623 | ␊ |
624 | ␉node = AddChild(node, "dev");␊ |
625 | ␉AddProperty(node, "one", 4, "one");␊ |
626 | ␉AddProperty(node, "two", 4, "two");␊ |
627 | ␉AddProperty(node, "three", 6, "three");␊ |
628 | ␊ |
629 | ␉node = AddChild(rootNode, "foo");␊ |
630 | ␉AddProperty(node, "aaa", 4, "aab");␊ |
631 | ␉AddProperty(node, "bbb", 4, "bbc");␊ |
632 | ␉AddProperty(node, "cccc", 6, "ccccd");␊ |
633 | ␊ |
634 | ␉node = FindNode("/this/is/a/test", 1);␊ |
635 | ␉AddProperty(node, "dddd", 12, "abcdefghijk");␊ |
636 | ␊ |
637 | ␉printf("In-memory tree:\n\n");␊ |
638 | ␊ |
639 | ␉PrintTree(rootNode);␊ |
640 | ␊ |
641 | ␉FlattenDeviceTree(&flatTree, &flatSize);␊ |
642 | ␊ |
643 | ␉printf("Flat tree = %p, size %d\n", flatTree, flatSize);␊ |
644 | ␊ |
645 | ␉dtEntry = (DTEntry)flatTree;␊ |
646 | ␊ |
647 | ␉printf("\n\nPrinting flat tree\n\n");␊ |
648 | ␊ |
649 | ␉DTInit(dtEntry);␊ |
650 | ␊ |
651 | ␉PrintFlattenedTree((DTEntry)flatTree);␊ |
652 | #if 0␊ |
653 | ␉printf("=== Entry %p ===\n", dtEntry);␊ |
654 | ␉if (kSuccess != DTCreatePropertyIterator(dtEntry, &propIter))␊ |
655 | ␉{␊ |
656 | ␉␉printf("Couldn't create property iterator\n");␊ |
657 | ␉␉return 1;␊ |
658 | ␉}␊ |
659 | ␉while( kSuccess == DTIterateProperties( propIter, &name))␊ |
660 | ␉{␊ |
661 | ␉␉if( kSuccess != DTGetProperty( dtEntry, name, &prop, &propSize ))␊ |
662 | ␉␉␉continue;␊ |
663 | ␉␉printf(" Property %s = %s\n", name, prop);␊ |
664 | }␊ |
665 | ␉DTDisposePropertyIterator(propIter);␊ |
666 | ␉printf("========\n");␊ |
667 | ␊ |
668 | ␉if (kSuccess != DTCreateEntryIterator(dtEntry, &entryIter))␊ |
669 | ␉{␊ |
670 | ␉␉printf("Couldn't create entry iterator\n");␊ |
671 | ␉␉return 1;␊ |
672 | ␉}␊ |
673 | ␉while (kSuccess == DTIterateEntries( entryIter, &dtEntry ))␊ |
674 | ␉{␊ |
675 | ␉␉printf("=== Entry %p ===\n", dtEntry);␊ |
676 | ␊ |
677 | ␉if (kSuccess != DTCreatePropertyIterator(dtEntry, &propIter))␊ |
678 | ␉{␊ |
679 | ␉␉printf("Couldn't create property iterator\n");␊ |
680 | ␉␉return 1;␊ |
681 | ␉}␊ |
682 | ␉␉while( kSuccess == DTIterateProperties( propIter, &name))␊ |
683 | ␉␉{␊ |
684 | ␉␉␉if( kSuccess != DTGetProperty( dtEntry, name, &prop, &propSize ))␊ |
685 | ␉␉␉␉continue;␊ |
686 | ␉␉␉printf(" Property %s = %s\n", name, prop);␊ |
687 | ␉␉}␊ |
688 | ␉␉DTDisposePropertyIterator(propIter);␊ |
689 | ␉␉printf("========\n");␊ |
690 | ␉}␊ |
691 | ␉DTDisposeEntryIterator(entryIter);␊ |
692 | #endif␊ |
693 | ␊ |
694 | ␉return 0;␊ |
695 | }␊ |
696 | ␊ |
697 | #endif␊ |
698 | ␊ |
699 | |