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 *␊ |
86 | DT__AddProperty(Node *node, const char *name, uint32_t length, void *value)␊ |
87 | {␊ |
88 | ␉Property *prop;␊ |
89 | ␊ |
90 | ␉DPRINTF("DT__AddProperty([Node '%s'], '%s', %d, 0x%X)\n", DT__GetName(node), name, length, value);␊ |
91 | ␊ |
92 | ␉if (freeProperties == NULL) {␊ |
93 | ␉␉void *buf = malloc(kAllocSize);␊ |
94 | ␉␉int i;␊ |
95 | ␊ |
96 | ␉␉DPRINTF("DT__AddProperty: Allocating more free properties\n");␊ |
97 | ␊ |
98 | ␉␉if (buf == 0) {␊ |
99 | ␉␉␉return 0;␊ |
100 | ␉␉}␊ |
101 | ␊ |
102 | ␉␉bzero(buf, kAllocSize);␊ |
103 | ␉␉// Use the first property to record the allocated buffer␊ |
104 | ␉␉// for later freeing.␊ |
105 | ␉␉prop = (Property *)buf;␊ |
106 | ␉␉prop->next = allocedProperties;␊ |
107 | ␉␉allocedProperties = prop;␊ |
108 | ␉␉prop->value = buf;␊ |
109 | ␉␉prop++;␊ |
110 | ␊ |
111 | ␉␉for (i = 1; i < (kAllocSize / sizeof(Property)); i++) {␊ |
112 | ␉␉␉prop->next = freeProperties;␊ |
113 | ␉␉␉freeProperties = prop;␊ |
114 | ␉␉␉prop++;␊ |
115 | ␉␉}␊ |
116 | ␉}␊ |
117 | ␊ |
118 | ␉prop = freeProperties;␊ |
119 | ␉freeProperties = prop->next;␊ |
120 | ␊ |
121 | ␉prop->name = name;␊ |
122 | ␉prop->length = length;␊ |
123 | ␉prop->value = value;␊ |
124 | ␊ |
125 | ␉// Always add to end of list␊ |
126 | ␉if (node->properties == 0) {␊ |
127 | ␉␉node->properties = prop;␊ |
128 | ␉} else {␊ |
129 | ␉␉node->last_prop->next = prop;␊ |
130 | ␉}␊ |
131 | ␊ |
132 | ␉node->last_prop = prop;␊ |
133 | ␉prop->next = 0;␊ |
134 | ␊ |
135 | ␉DPRINTF("DT__AddProperty: done.\n");␊ |
136 | ␊ |
137 | ␉DTInfo.numProperties++;␊ |
138 | ␉DTInfo.totalPropertySize += RoundToLong(length);␊ |
139 | ␊ |
140 | ␉return prop;␊ |
141 | }␊ |
142 | ␊ |
143 | ␊ |
144 | //==============================================================================␊ |
145 | ␊ |
146 | Node *␊ |
147 | DT__AddChild(Node *parent, const char *name)␊ |
148 | {␊ |
149 | ␉Node *node;␊ |
150 | ␊ |
151 | ␉if (freeNodes == NULL)␊ |
152 | ␉{␊ |
153 | ␉␉void *buf = malloc(kAllocSize);␊ |
154 | ␊ |
155 | ␉␉if (buf == 0)␊ |
156 | ␉␉{␊ |
157 | ␉␉␉return 0;␊ |
158 | ␉␉}␊ |
159 | ␊ |
160 | ␉␉int i;␊ |
161 | ␊ |
162 | ␉␉DPRINTF("DT__AddChild: Allocating more free nodes\n");␊ |
163 | ␊ |
164 | ␉␉bzero(buf, kAllocSize);␊ |
165 | ␉␉node = (Node *)buf;␊ |
166 | ␊ |
167 | ␉␉// Use the first node to record the allocated buffer for later freeing.␊ |
168 | ␉␉node->next = allocedNodes;␊ |
169 | ␉␉allocedNodes = node;␊ |
170 | ␉␉node->children = (Node *)buf;␊ |
171 | ␉␉node++;␊ |
172 | ␊ |
173 | ␉␉for (i = 1; i < (kAllocSize / sizeof(Node)); i++)␊ |
174 | ␉␉{␊ |
175 | ␉␉␉node->next = freeNodes;␊ |
176 | ␉␉␉freeNodes = node;␊ |
177 | ␉␉␉node++;␊ |
178 | ␉␉}␊ |
179 | ␉}␊ |
180 | ␊ |
181 | ␉DPRINTF("DT__AddChild(0x%x, '%s')\n", parent, name);␊ |
182 | ␊ |
183 | ␉node = freeNodes;␊ |
184 | ␉freeNodes = node->next;␊ |
185 | ␊ |
186 | ␉DPRINTF("DT__AddChild: Got free node 0x%x\n", node);␊ |
187 | ␉DPRINTF("DT__AddChild: prop = 0x%x, children = 0x%x, next = 0x%x\n", node->properties, node->children, node->next);␊ |
188 | ␊ |
189 | ␉if (parent == NULL)␊ |
190 | ␉{␊ |
191 | ␉␉rootNode = node;␊ |
192 | ␉␉node->next = 0;␊ |
193 | ␉}␊ |
194 | ␉else␊ |
195 | ␉{␊ |
196 | ␉␉node->next = parent->children;␊ |
197 | ␉␉parent->children = node;␊ |
198 | ␉}␊ |
199 | ␊ |
200 | ␉DTInfo.numNodes++;␊ |
201 | ␉DT__AddProperty(node, "name", strlen(name) + 1, (void *) name);␊ |
202 | ␊ |
203 | ␉return node;␊ |
204 | }␊ |
205 | ␊ |
206 | ␊ |
207 | //==============================================================================␊ |
208 | ␊ |
209 | void␊ |
210 | DT__FreeProperty(Property *prop)␊ |
211 | {␊ |
212 | ␉prop->next = freeProperties;␊ |
213 | ␉freeProperties = prop;␊ |
214 | }␊ |
215 | ␊ |
216 | //==============================================================================␊ |
217 | ␊ |
218 | void␊ |
219 | DT__FreeNode(Node *node)␊ |
220 | {␊ |
221 | ␉node->next = freeNodes;␊ |
222 | ␉freeNodes = node;␊ |
223 | }␊ |
224 | ␊ |
225 | //==============================================================================␊ |
226 | ␊ |
227 | Node *␊ |
228 | DT__Initialize(void)␊ |
229 | {␊ |
230 | //DPRINTF("DT__Initialize\n");␊ |
231 | ␊ |
232 | freeNodes = 0;␊ |
233 | allocedNodes = 0;␊ |
234 | freeProperties = 0;␊ |
235 | allocedProperties = 0;␊ |
236 | ␊ |
237 | DTInfo.numNodes = 0;␊ |
238 | DTInfo.numProperties = 0;␊ |
239 | DTInfo.totalPropertySize = 0;␊ |
240 | ␊ |
241 | rootNode = DT__AddChild(NULL, "/");␊ |
242 | DPRINTF("DT__Initialize: done\n");␊ |
243 | return rootNode;␊ |
244 | }␊ |
245 | ␊ |
246 | //==============================================================================␊ |
247 | ␊ |
248 | Node *␊ |
249 | DT__GetRootNode(void)␊ |
250 | {␊ |
251 | return rootNode;␊ |
252 | }␊ |
253 | ␊ |
254 | //==============================================================================␊ |
255 | /*␊ |
256 | * Free up memory used by in-memory representation of device tree.␊ |
257 | */␊ |
258 | void␊ |
259 | DT__Finalize(void)␊ |
260 | {␊ |
261 | ␉Node *node;␊ |
262 | ␉Property *prop;␊ |
263 | ␊ |
264 | ␉DPRINTF("DT__Finalize\n");␊ |
265 | ␊ |
266 | ␉for (prop = allocedProperties; prop != NULL; prop = prop->next)␊ |
267 | ␉{␊ |
268 | ␉␉free(prop->value);␊ |
269 | ␉}␊ |
270 | ␊ |
271 | ␉allocedProperties = NULL;␊ |
272 | ␉freeProperties = NULL;␊ |
273 | ␊ |
274 | ␉for (node = allocedNodes; node != NULL; node = node->next)␊ |
275 | ␉{␊ |
276 | ␉␉free((void *)node->children);␊ |
277 | ␉}␊ |
278 | ␊ |
279 | ␉allocedNodes = NULL;␊ |
280 | ␉freeNodes = NULL;␊ |
281 | ␉rootNode = NULL;␊ |
282 | ␊ |
283 | ␉// XXX leaks any created strings␊ |
284 | ␉DTInfo.numNodes = 0;␊ |
285 | ␉DTInfo.numProperties = 0;␊ |
286 | ␉DTInfo.totalPropertySize = 0;␊ |
287 | }␊ |
288 | ␊ |
289 | //==============================================================================␊ |
290 | ␊ |
291 | static void *␊ |
292 | FlattenNodes(Node *node, void *buffer)␊ |
293 | {␊ |
294 | ␉Property *prop;␊ |
295 | ␉DeviceTreeNode *flatNode;␊ |
296 | ␉DeviceTreeNodeProperty *flatProp;␊ |
297 | ␉int count;␊ |
298 | ␊ |
299 | ␉if (node == 0) {␊ |
300 | ␉␉return buffer;␊ |
301 | ␉}␊ |
302 | ␊ |
303 | ␉flatNode = (DeviceTreeNode *)buffer;␊ |
304 | ␉buffer += sizeof(DeviceTreeNode);␊ |
305 | ␊ |
306 | ␉for (count = 0, prop = node->properties; prop != 0; count++, prop = prop->next)␊ |
307 | ␉{␊ |
308 | ␉␉flatProp = (DeviceTreeNodeProperty *)buffer;␊ |
309 | ␉␉strncpy(flatProp->name, prop->name, kPropNameLength);␊ |
310 | ␉␉flatProp->length = prop->length;␊ |
311 | ␉␉buffer += sizeof(DeviceTreeNodeProperty);␊ |
312 | ␉␉bcopy(prop->value, buffer, prop->length);␊ |
313 | ␉␉buffer += RoundToLong(prop->length);␊ |
314 | ␉}␊ |
315 | ␊ |
316 | ␉flatNode->nProperties = count;␊ |
317 | ␊ |
318 | ␉for (count = 0, node = node->children; node != 0; count++, node = node->next)␊ |
319 | ␉{␊ |
320 | ␉␉buffer = FlattenNodes(node, buffer);␊ |
321 | ␉}␊ |
322 | ␊ |
323 | ␉flatNode->nChildren = count;␊ |
324 | ␊ |
325 | ␉return buffer;␊ |
326 | }␊ |
327 | ␊ |
328 | ␊ |
329 | /*==============================================================================␊ |
330 | * Flatten the in-memory representation of the device tree into a binary DT block.␊ |
331 | * To get the buffer size needed, call with result = 0.␊ |
332 | * To have a buffer allocated for you, call with *result = 0.␊ |
333 | * To use your own buffer, call with *result = &buffer.␊ |
334 | */␊ |
335 | ␊ |
336 | void␊ |
337 | DT__FlattenDeviceTree(void **buffer_p, uint32_t *length)␊ |
338 | {␊ |
339 | ␉uint32_t totalSize;␊ |
340 | ␉void * buf;␊ |
341 | ␊ |
342 | ␉DPRINTF("DT__FlattenDeviceTree(0x%x, 0x%x)\n", buffer_p, length);␊ |
343 | ␊ |
344 | #if DEBUG␊ |
345 | ␉if (buffer_p) {␊ |
346 | ␉␉DT__PrintTree(rootNode);␊ |
347 | ␉}␊ |
348 | #endif␊ |
349 | ␊ |
350 | ␉totalSize = DTInfo.numNodes * sizeof(DeviceTreeNode) + ␊ |
351 | ␉␉DTInfo.numProperties * sizeof(DeviceTreeNodeProperty) +␊ |
352 | ␉␉DTInfo.totalPropertySize;␊ |
353 | ␊ |
354 | ␉DPRINTF("Total size 0x%x\n", totalSize);␊ |
355 | ␊ |
356 | ␉if (buffer_p != 0)␊ |
357 | ␉{␊ |
358 | ␉␉if (totalSize == 0)␊ |
359 | ␉␉{␊ |
360 | ␉␉␉buf = 0;␊ |
361 | ␉␉}␊ |
362 | ␉␉else␊ |
363 | ␉␉{␊ |
364 | ␉␉␉if (*buffer_p == 0)␊ |
365 | ␉␉␉{␊ |
366 | ␉␉␉␉buf = malloc(totalSize);␊ |
367 | ␉␉␉}␊ |
368 | ␉␉␉else␊ |
369 | ␉␉␉{␊ |
370 | ␉␉␉␉buf = *buffer_p;␊ |
371 | ␉␉␉}␊ |
372 | ␊ |
373 | ␉␉␉bzero(buf, totalSize);␊ |
374 | ␊ |
375 | ␉␉␉FlattenNodes(rootNode, buf);␊ |
376 | ␉␉}␊ |
377 | ␊ |
378 | ␉␉*buffer_p = buf;␊ |
379 | ␉}␊ |
380 | ␊ |
381 | ␉if (length)␊ |
382 | ␉{␊ |
383 | ␉␉*length = totalSize;␊ |
384 | ␉}␊ |
385 | }␊ |
386 | ␊ |
387 | //==============================================================================␊ |
388 | ␊ |
389 | char *␊ |
390 | DT__GetName(Node *node)␊ |
391 | {␊ |
392 | ␉Property *prop;␊ |
393 | ␊ |
394 | ␉//DPRINTF("DT__GetName(0x%x)\n", node);␊ |
395 | ␉//DPRINTF("Node properties = 0x%x\n", node->properties);␊ |
396 | ␉for (prop = node->properties; prop; prop = prop->next)␊ |
397 | ␉{␊ |
398 | ␉␉//DPRINTF("Prop '%s'\n", prop->name);␊ |
399 | ␉␉if (strcmp(prop->name, "name") == 0)␊ |
400 | ␉␉{␊ |
401 | ␉␉␉return prop->value;␊ |
402 | ␉␉}␊ |
403 | ␉}␊ |
404 | ␊ |
405 | ␉//DPRINTF("DT__GetName returns 0\n");␊ |
406 | ␉return "(null)";␊ |
407 | }␊ |
408 | ␊ |
409 | //==============================================================================␊ |
410 | // Bungo␊ |
411 | Property *␊ |
412 | DT__GetProperty(Node *node, const char *name)␊ |
413 | {␊ |
414 | ␉Property *prop;␊ |
415 | ␊ |
416 | ␉for (prop = node->properties; prop; prop = prop->next)␊ |
417 | ␉{␊ |
418 | ␉␉if (strcmp(prop->name, name) == 0)␊ |
419 | ␉␉{␊ |
420 | ␉␉␉return prop;␊ |
421 | ␉␉}␊ |
422 | ␉}␊ |
423 | ␊ |
424 | ␉return NULL;␊ |
425 | }␊ |
426 | ␊ |
427 | //==============================================================================␊ |
428 | ␊ |
429 | Node *␊ |
430 | DT__FindNode(const char *path, bool createIfMissing)␊ |
431 | {␊ |
432 | ␉Node *node, *child;␊ |
433 | ␉DTPropertyNameBuf nameBuf;␊ |
434 | ␉char *bp;␊ |
435 | ␉int i;␊ |
436 | ␊ |
437 | ␉DPRINTF("DT__FindNode('%s', %d)\n", path, createIfMissing);␊ |
438 | ␊ |
439 | ␉// Start at root␊ |
440 | ␉node = rootNode;␊ |
441 | ␊ |
442 | ␉DPRINTF("DT__FindNode: root = 0x%x\n", rootNode);␊ |
443 | ␊ |
444 | ␉while (node)␊ |
445 | ␉{␊ |
446 | ␉␉// Skip leading slash(es)␊ |
447 | ␉␉while (*path == '/')␊ |
448 | ␉␉{␊ |
449 | ␉␉␉path++;␊ |
450 | ␉␉}␊ |
451 | ␊ |
452 | ␉␉for (i = 0, bp = nameBuf; ++i < kDTMaxEntryNameLength && *path && *path != '/'; bp++, path++)␊ |
453 | ␉␉{␊ |
454 | ␉␉␉*bp = *path;␊ |
455 | ␉␉}␊ |
456 | ␊ |
457 | ␉␉*bp = '\0';␊ |
458 | ␊ |
459 | if (nameBuf[0] == '\0')␊ |
460 | {␊ |
461 | // last path entry␊ |
462 | break;␊ |
463 | }␊ |
464 | ␊ |
465 | DPRINTF("DT__FindNode: Node '%s'\n", nameBuf);␊ |
466 | ␊ |
467 | for (child = node->children; child != 0; child = child->next)␊ |
468 | {␊ |
469 | DPRINTF("DT__FindNode: Child 0x%x\n", child);␊ |
470 | ␊ |
471 | if (strcmp(DT__GetName(child), nameBuf) == 0)␊ |
472 | {␊ |
473 | break;␊ |
474 | }␊ |
475 | }␊ |
476 | ␊ |
477 | if (child == 0 && createIfMissing)␊ |
478 | {␊ |
479 | char *str = malloc(strlen(nameBuf) + 1);␊ |
480 | // XXX this will leak␊ |
481 | strcpy(str, nameBuf);␊ |
482 | ␊ |
483 | child = DT__AddChild(node, str);␊ |
484 | DPRINTF("DT__FindNode: Creating node: %s\n", str);␊ |
485 | }␊ |
486 | ␊ |
487 | ␉␉node = child;␊ |
488 | ␉}␊ |
489 | ␊ |
490 | ␉return node;␊ |
491 | }␊ |
492 | ␊ |
493 | #if DEBUG␊ |
494 | ␊ |
495 | //==============================================================================␊ |
496 | ␊ |
497 | void␊ |
498 | DT__PrintNode(Node *node, int level)␊ |
499 | {␊ |
500 | ␉char spaces[10], *cp = spaces;␊ |
501 | ␉Property *prop;␊ |
502 | ␊ |
503 | ␉if (level > 9)␊ |
504 | ␉{␊ |
505 | ␉␉level = 9;␊ |
506 | ␉}␊ |
507 | ␊ |
508 | ␉while (level--)␊ |
509 | ␉{␊ |
510 | ␉␉*cp++ = ' ';␊ |
511 | ␉}␊ |
512 | ␊ |
513 | ␉*cp = '\0';␊ |
514 | ␊ |
515 | ␉printf("%s===Node===\n", spaces);␊ |
516 | ␊ |
517 | ␉for (prop = node->properties; prop; prop = prop->next)␊ |
518 | ␉{␊ |
519 | ␉␉char c = *((char *)prop->value);␊ |
520 | ␉␉if (prop->length < 64 && (strcmp(prop->name, "name") == 0 || (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'))␊ |
521 | ␉␉{␊ |
522 | ␉␉␉printf("%s Property '%s' [%d] = '%s'\n", spaces, prop->name, prop->length, prop->value);␊ |
523 | ␉␉}␊ |
524 | ␉␉else␊ |
525 | ␉␉{␊ |
526 | ␉␉␉printf("%s Property '%s' [%d] = (data)\n", spaces, prop->name, prop->length);␊ |
527 | ␉␉}␊ |
528 | ␉}␊ |
529 | ␊ |
530 | ␉printf("%s==========\n", spaces);␊ |
531 | }␊ |
532 | ␊ |
533 | //==============================================================================␊ |
534 | ␊ |
535 | static void␊ |
536 | _PrintTree(Node *node, int level)␊ |
537 | {␊ |
538 | ␉DT__PrintNode(node, level);␊ |
539 | ␊ |
540 | ␉level++;␊ |
541 | ␊ |
542 | ␉for (node = node->children; node; node = node->next)␊ |
543 | ␉{␊ |
544 | ␉␉_PrintTree(node, level);␊ |
545 | ␉}␊ |
546 | }␊ |
547 | ␊ |
548 | //==============================================================================␊ |
549 | ␊ |
550 | void␊ |
551 | DT__PrintTree(Node *node)␊ |
552 | {␊ |
553 | ␉if (node == 0) node = rootNode;␊ |
554 | ␉_PrintTree(node, 0);␊ |
555 | }␊ |
556 | ␊ |
557 | //==============================================================================␊ |
558 | ␊ |
559 | void␊ |
560 | DT__PrintFlattenedNode(DTEntry entry, int level)␊ |
561 | {␊ |
562 | ␉char spaces[10], *cp = spaces;␊ |
563 | ␉DTPropertyIterator␉ propIter;␊ |
564 | ␉char *name;␊ |
565 | ␉void *prop;␊ |
566 | ␉int propSize;␊ |
567 | ␊ |
568 | ␉if (level > 9) level = 9;␊ |
569 | ␉while (level--) *cp++ = ' ';␊ |
570 | ␉␉*cp = '\0';␊ |
571 | ␊ |
572 | ␉printf("%s===Entry %p===\n", spaces, entry);␊ |
573 | ␉␉if (kSuccess != DTCreatePropertyIterator(entry, &propIter))␊ |
574 | ␉␉{␊ |
575 | ␉␉␉printf("Couldn't create property iterator\n");␊ |
576 | ␉␉␉return;␊ |
577 | ␉␉}␊ |
578 | ␉␉while( kSuccess == DTIterateProperties( propIter, &name))␊ |
579 | ␉␉{␊ |
580 | ␉␉␉if( kSuccess != DTGetProperty( entry, name, &prop, &propSize ))␊ |
581 | ␉␉␉␉continue;␊ |
582 | ␉␉printf("%s Property %s = %s\n", spaces, name, prop);␊ |
583 | ␉}␊ |
584 | ␉DTDisposePropertyIterator(propIter);␊ |
585 | ␊ |
586 | ␉printf("%s==========\n", spaces);␊ |
587 | }␊ |
588 | ␊ |
589 | //==============================================================================␊ |
590 | ␊ |
591 | static void␊ |
592 | _PrintFlattenedTree(DTEntry entry, int level)␊ |
593 | {␊ |
594 | ␉DTEntryIterator entryIter;␊ |
595 | ␊ |
596 | ␉PrintFlattenedNode(entry, level);␊ |
597 | ␊ |
598 | ␉if (kSuccess != DTCreateEntryIterator(entry, &entryIter))␊ |
599 | ␉{␊ |
600 | ␉␉printf("Couldn't create entry iterator\n");␊ |
601 | ␉␉return;␊ |
602 | ␉}␊ |
603 | ␉level++;␊ |
604 | ␉while (kSuccess == DTIterateEntries( entryIter, &entry ))␊ |
605 | ␉{␊ |
606 | ␉␉_PrintFlattenedTree(entry, level);␊ |
607 | ␉}␊ |
608 | ␉DTDisposeEntryIterator(entryIter);␊ |
609 | }␊ |
610 | ␊ |
611 | //==============================================================================␊ |
612 | ␊ |
613 | void␊ |
614 | DT__PrintFlattenedTree(DTEntry entry)␊ |
615 | {␊ |
616 | ␉_PrintFlattenedTree(entry, 0);␊ |
617 | }␊ |
618 | ␊ |
619 | //==============================================================================␊ |
620 | ␊ |
621 | int␊ |
622 | main(int argc, char **argv)␊ |
623 | {␊ |
624 | ␉DTEntry␉␉␉dtEntry;␊ |
625 | ␉DTPropertyIterator␉propIter;␊ |
626 | ␉DTEntryIterator␉␉entryIter;␊ |
627 | ␉void␉␉␉*prop;␊ |
628 | ␉int␉␉␉propSize;␊ |
629 | ␉char␉␉␉*name;␊ |
630 | ␉void␉␉␉*flatTree;␊ |
631 | ␉uint32_t␉␉flatSize;␊ |
632 | ␊ |
633 | ␉Node *node;␊ |
634 | ␊ |
635 | ␉node = AddChild(NULL, "device-tree");␊ |
636 | ␉AddProperty(node, "potato", 4, "foo");␊ |
637 | ␉AddProperty(node, "chemistry", 4, "bar");␊ |
638 | ␉AddProperty(node, "physics", 4, "baz");␊ |
639 | ␊ |
640 | ␉node = AddChild(node, "dev");␊ |
641 | ␉AddProperty(node, "one", 4, "one");␊ |
642 | ␉AddProperty(node, "two", 4, "two");␊ |
643 | ␉AddProperty(node, "three", 6, "three");␊ |
644 | ␊ |
645 | ␉node = AddChild(rootNode, "foo");␊ |
646 | ␉AddProperty(node, "aaa", 4, "aab");␊ |
647 | ␉AddProperty(node, "bbb", 4, "bbc");␊ |
648 | ␉AddProperty(node, "cccc", 6, "ccccd");␊ |
649 | ␊ |
650 | ␉node = FindNode("/this/is/a/test", 1);␊ |
651 | ␉AddProperty(node, "dddd", 12, "abcdefghijk");␊ |
652 | ␊ |
653 | ␉printf("In-memory tree:\n\n");␊ |
654 | ␊ |
655 | ␉PrintTree(rootNode);␊ |
656 | ␊ |
657 | ␉FlattenDeviceTree(&flatTree, &flatSize);␊ |
658 | ␊ |
659 | ␉printf("Flat tree = %p, size %d\n", flatTree, flatSize);␊ |
660 | ␊ |
661 | ␉dtEntry = (DTEntry)flatTree;␊ |
662 | ␊ |
663 | ␉printf("\n\nPrinting flat tree\n\n");␊ |
664 | ␊ |
665 | ␉DTInit(dtEntry);␊ |
666 | ␊ |
667 | ␉PrintFlattenedTree((DTEntry)flatTree);␊ |
668 | #if 0␊ |
669 | ␉printf("=== Entry %p ===\n", dtEntry);␊ |
670 | ␉if (kSuccess != DTCreatePropertyIterator(dtEntry, &propIter))␊ |
671 | ␉{␊ |
672 | ␉␉printf("Couldn't create property iterator\n");␊ |
673 | ␉␉return 1;␊ |
674 | ␉}␊ |
675 | ␉while( kSuccess == DTIterateProperties( propIter, &name))␊ |
676 | ␉{␊ |
677 | ␉␉if( kSuccess != DTGetProperty( dtEntry, name, &prop, &propSize ))␊ |
678 | ␉␉␉continue;␊ |
679 | ␉␉printf(" Property %s = %s\n", name, prop);␊ |
680 | }␊ |
681 | ␉DTDisposePropertyIterator(propIter);␊ |
682 | ␉printf("========\n");␊ |
683 | ␊ |
684 | ␉if (kSuccess != DTCreateEntryIterator(dtEntry, &entryIter))␊ |
685 | ␉{␊ |
686 | ␉␉printf("Couldn't create entry iterator\n");␊ |
687 | ␉␉return 1;␊ |
688 | ␉}␊ |
689 | ␉while (kSuccess == DTIterateEntries( entryIter, &dtEntry ))␊ |
690 | ␉{␊ |
691 | ␉␉printf("=== Entry %p ===\n", dtEntry);␊ |
692 | ␊ |
693 | ␉if (kSuccess != DTCreatePropertyIterator(dtEntry, &propIter))␊ |
694 | ␉{␊ |
695 | ␉␉printf("Couldn't create property iterator\n");␊ |
696 | ␉␉return 1;␊ |
697 | ␉}␊ |
698 | ␉␉while( kSuccess == DTIterateProperties( propIter, &name))␊ |
699 | ␉␉{␊ |
700 | ␉␉␉if( kSuccess != DTGetProperty( dtEntry, name, &prop, &propSize ))␊ |
701 | ␉␉␉␉continue;␊ |
702 | ␉␉␉printf(" Property %s = %s\n", name, prop);␊ |
703 | ␉␉}␊ |
704 | ␉␉DTDisposePropertyIterator(propIter);␊ |
705 | ␉␉printf("========\n");␊ |
706 | ␉}␊ |
707 | ␉DTDisposeEntryIterator(entryIter);␊ |
708 | #endif␊ |
709 | ␊ |
710 | ␉return 0;␊ |
711 | }␊ |
712 | ␊ |
713 | #endif␊ |
714 | ␊ |
715 | |