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("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("Done [0x%x]\n", prop);␊ |
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("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("Got free node 0x%x\n", node);␊ |
187 | ␉DPRINTF("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 | void␊ |
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 | }␊ |
244 | ␊ |
245 | //==============================================================================␊ |
246 | /*␊ |
247 | * Free up memory used by in-memory representation of device tree.␊ |
248 | */␊ |
249 | void␊ |
250 | DT__Finalize(void)␊ |
251 | {␊ |
252 | ␉Node *node;␊ |
253 | ␉Property *prop;␊ |
254 | ␊ |
255 | ␉DPRINTF("DT__Finalize\n");␊ |
256 | ␊ |
257 | ␉for (prop = allocedProperties; prop != NULL; prop = prop->next)␊ |
258 | ␉{␊ |
259 | ␉␉free(prop->value);␊ |
260 | ␉}␊ |
261 | ␊ |
262 | ␉allocedProperties = NULL;␊ |
263 | ␉freeProperties = NULL;␊ |
264 | ␊ |
265 | ␉for (node = allocedNodes; node != NULL; node = node->next)␊ |
266 | ␉{␊ |
267 | ␉␉free((void *)node->children);␊ |
268 | ␉}␊ |
269 | ␊ |
270 | ␉allocedNodes = NULL;␊ |
271 | ␉freeNodes = NULL;␊ |
272 | ␉rootNode = NULL;␊ |
273 | ␊ |
274 | ␉// XXX leaks any created strings␊ |
275 | ␉DTInfo.numNodes = 0;␊ |
276 | ␉DTInfo.numProperties = 0;␊ |
277 | ␉DTInfo.totalPropertySize = 0;␊ |
278 | }␊ |
279 | ␊ |
280 | //==============================================================================␊ |
281 | ␊ |
282 | static void *␊ |
283 | FlattenNodes(Node *node, void *buffer)␊ |
284 | {␊ |
285 | ␉Property *prop;␊ |
286 | ␉DeviceTreeNode *flatNode;␊ |
287 | ␉DeviceTreeNodeProperty *flatProp;␊ |
288 | ␉int count;␊ |
289 | ␊ |
290 | ␉if (node == 0) {␊ |
291 | ␉␉return buffer;␊ |
292 | ␉}␊ |
293 | ␊ |
294 | ␉flatNode = (DeviceTreeNode *)buffer;␊ |
295 | ␉buffer += sizeof(DeviceTreeNode);␊ |
296 | ␊ |
297 | ␉for (count = 0, prop = node->properties; prop != 0; count++, prop = prop->next)␊ |
298 | ␉{␊ |
299 | ␉␉flatProp = (DeviceTreeNodeProperty *)buffer;␊ |
300 | ␉␉strncpy(flatProp->name, prop->name, kPropNameLength);␊ |
301 | ␉␉flatProp->length = prop->length;␊ |
302 | ␉␉buffer += sizeof(DeviceTreeNodeProperty);␊ |
303 | ␉␉bcopy(prop->value, buffer, prop->length);␊ |
304 | ␉␉buffer += RoundToLong(prop->length);␊ |
305 | ␉}␊ |
306 | ␊ |
307 | ␉flatNode->nProperties = count;␊ |
308 | ␊ |
309 | ␉for (count = 0, node = node->children; node != 0; count++, node = node->next)␊ |
310 | ␉{␊ |
311 | ␉␉buffer = FlattenNodes(node, buffer);␊ |
312 | ␉}␊ |
313 | ␊ |
314 | ␉flatNode->nChildren = count;␊ |
315 | ␊ |
316 | ␉return buffer;␊ |
317 | }␊ |
318 | ␊ |
319 | ␊ |
320 | /*==============================================================================␊ |
321 | * Flatten the in-memory representation of the device tree into a binary DT block.␊ |
322 | * To get the buffer size needed, call with result = 0.␊ |
323 | * To have a buffer allocated for you, call with *result = 0.␊ |
324 | * To use your own buffer, call with *result = &buffer.␊ |
325 | */␊ |
326 | ␊ |
327 | void␊ |
328 | DT__FlattenDeviceTree(void **buffer_p, uint32_t *length)␊ |
329 | {␊ |
330 | ␉uint32_t totalSize;␊ |
331 | ␉void * buf;␊ |
332 | ␊ |
333 | ␉DPRINTF("DT__FlattenDeviceTree(0x%x, 0x%x)\n", buffer_p, length);␊ |
334 | ␊ |
335 | #if DEBUG␊ |
336 | ␉if (buffer_p) {␊ |
337 | ␉␉DT__PrintTree(rootNode);␊ |
338 | ␉}␊ |
339 | #endif␊ |
340 | ␊ |
341 | ␉totalSize = DTInfo.numNodes * sizeof(DeviceTreeNode) + ␊ |
342 | ␉␉DTInfo.numProperties * sizeof(DeviceTreeNodeProperty) +␊ |
343 | ␉␉DTInfo.totalPropertySize;␊ |
344 | ␊ |
345 | ␉DPRINTF("Total size 0x%x\n", totalSize);␊ |
346 | ␊ |
347 | ␉if (buffer_p != 0)␊ |
348 | ␉{␊ |
349 | ␉␉if (totalSize == 0)␊ |
350 | ␉␉{␊ |
351 | ␉␉␉buf = 0;␊ |
352 | ␉␉}␊ |
353 | ␉␉else␊ |
354 | ␉␉{␊ |
355 | ␉␉␉if (*buffer_p == 0)␊ |
356 | ␉␉␉{␊ |
357 | ␉␉␉␉buf = malloc(totalSize);␊ |
358 | ␉␉␉}␊ |
359 | ␉␉␉else␊ |
360 | ␉␉␉{␊ |
361 | ␉␉␉␉buf = *buffer_p;␊ |
362 | ␉␉␉}␊ |
363 | ␊ |
364 | ␉␉␉bzero(buf, totalSize);␊ |
365 | ␊ |
366 | ␉␉␉FlattenNodes(rootNode, buf);␊ |
367 | ␉␉}␊ |
368 | ␊ |
369 | ␉␉*buffer_p = buf;␊ |
370 | ␉}␊ |
371 | ␊ |
372 | ␉if (length)␊ |
373 | ␉{␊ |
374 | ␉␉*length = totalSize;␊ |
375 | ␉}␊ |
376 | }␊ |
377 | ␊ |
378 | //==============================================================================␊ |
379 | ␊ |
380 | char *␊ |
381 | DT__GetName(Node *node)␊ |
382 | {␊ |
383 | ␉Property *prop;␊ |
384 | ␊ |
385 | ␉//DPRINTF("DT__GetName(0x%x)\n", node);␊ |
386 | ␉//DPRINTF("Node properties = 0x%x\n", node->properties);␊ |
387 | ␉for (prop = node->properties; prop; prop = prop->next)␊ |
388 | ␉{␊ |
389 | ␉␉//DPRINTF("Prop '%s'\n", prop->name);␊ |
390 | ␉␉if (strcmp(prop->name, "name") == 0)␊ |
391 | ␉␉{␊ |
392 | ␉␉␉return prop->value;␊ |
393 | ␉␉}␊ |
394 | ␉}␊ |
395 | ␊ |
396 | ␉//DPRINTF("DT__GetName returns 0\n");␊ |
397 | ␉return "(null)";␊ |
398 | }␊ |
399 | ␊ |
400 | //==============================================================================␊ |
401 | ␊ |
402 | Node *␊ |
403 | DT__FindNode(const char *path, bool createIfMissing)␊ |
404 | {␊ |
405 | ␉Node *node, *child;␊ |
406 | ␉DTPropertyNameBuf nameBuf;␊ |
407 | ␉char *bp;␊ |
408 | ␉int i;␊ |
409 | ␊ |
410 | ␉DPRINTF("DT__FindNode('%s', %d)\n", path, createIfMissing);␊ |
411 | ␊ |
412 | ␉// Start at root␊ |
413 | ␉node = rootNode;␊ |
414 | ␊ |
415 | ␉DPRINTF("root = 0x%x\n", rootNode);␊ |
416 | ␊ |
417 | ␉while (node)␊ |
418 | ␉{␊ |
419 | ␉␉// Skip leading slash␊ |
420 | ␉␉while (*path == '/')␊ |
421 | ␉␉{␊ |
422 | ␉␉␉path++;␊ |
423 | ␉␉}␊ |
424 | ␊ |
425 | ␉␉for (i = 0, bp = nameBuf; ++i < kDTMaxEntryNameLength && *path && *path != '/'; bp++, path++)␊ |
426 | ␉␉{␊ |
427 | ␉␉␉*bp = *path;␊ |
428 | ␉␉}␊ |
429 | ␊ |
430 | ␉␉*bp = '\0';␊ |
431 | ␊ |
432 | ␉if (nameBuf[0] == '\0')␊ |
433 | ␉{␊ |
434 | ␉␉// last path entry␊ |
435 | ␉␉break;␊ |
436 | ␉}␊ |
437 | ␊ |
438 | ␉DPRINTF("Node '%s'\n", nameBuf);␊ |
439 | ␊ |
440 | ␉for (child = node->children; child != 0; child = child->next)␊ |
441 | ␉{␊ |
442 | ␉␉DPRINTF("Child 0x%x\n", child);␊ |
443 | ␊ |
444 | ␉␉if (strcmp(DT__GetName(child), nameBuf) == 0)␊ |
445 | ␉␉{␊ |
446 | ␉␉␉break;␊ |
447 | ␉␉}␊ |
448 | ␉}␊ |
449 | ␊ |
450 | if (child == 0 && createIfMissing)␊ |
451 | ␉{␊ |
452 | ␉␉DPRINTF("Creating node\n");␊ |
453 | ␊ |
454 | ␉␉char *str = malloc(strlen(nameBuf) + 1);␊ |
455 | ␉␉// XXX this will leak␊ |
456 | ␉␉strcpy(str, nameBuf);␊ |
457 | ␊ |
458 | ␉␉child = DT__AddChild(node, str);␊ |
459 | ␉}␊ |
460 | ␊ |
461 | ␉␉node = child;␊ |
462 | ␉}␊ |
463 | ␊ |
464 | ␉return node;␊ |
465 | }␊ |
466 | ␊ |
467 | #if DEBUG␊ |
468 | ␊ |
469 | //==============================================================================␊ |
470 | ␊ |
471 | void␊ |
472 | DT__PrintNode(Node *node, int level)␊ |
473 | {␊ |
474 | ␉char spaces[10], *cp = spaces;␊ |
475 | ␉Property *prop;␊ |
476 | ␊ |
477 | ␉if (level > 9)␊ |
478 | ␉{␊ |
479 | ␉␉level = 9;␊ |
480 | ␉}␊ |
481 | ␊ |
482 | ␉while (level--)␊ |
483 | ␉{␊ |
484 | ␉␉*cp++ = ' ';␊ |
485 | ␉}␊ |
486 | ␊ |
487 | ␉*cp = '\0';␊ |
488 | ␊ |
489 | ␉printf("%s===Node===\n", spaces);␊ |
490 | ␊ |
491 | ␉for (prop = node->properties; prop; prop = prop->next)␊ |
492 | ␉{␊ |
493 | ␉␉char c = *((char *)prop->value);␊ |
494 | ␉␉if (prop->length < 64 && (strcmp(prop->name, "name") == 0 || (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'))␊ |
495 | ␉␉{␊ |
496 | ␉␉␉printf("%s Property '%s' [%d] = '%s'\n", spaces, prop->name, prop->length, prop->value);␊ |
497 | ␉␉}␊ |
498 | ␉␉else␊ |
499 | ␉␉{␊ |
500 | ␉␉␉printf("%s Property '%s' [%d] = (data)\n", spaces, prop->name, prop->length);␊ |
501 | ␉␉}␊ |
502 | ␉}␊ |
503 | ␊ |
504 | ␉printf("%s==========\n", spaces);␊ |
505 | }␊ |
506 | ␊ |
507 | //==============================================================================␊ |
508 | ␊ |
509 | static void␊ |
510 | _PrintTree(Node *node, int level)␊ |
511 | {␊ |
512 | ␉DT__PrintNode(node, level);␊ |
513 | ␊ |
514 | ␉level++;␊ |
515 | ␊ |
516 | ␉for (node = node->children; node; node = node->next)␊ |
517 | ␉{␊ |
518 | ␉␉_PrintTree(node, level);␊ |
519 | ␉}␊ |
520 | }␊ |
521 | ␊ |
522 | //==============================================================================␊ |
523 | ␊ |
524 | void␊ |
525 | DT__PrintTree(Node *node)␊ |
526 | {␊ |
527 | ␉if (node == 0) node = rootNode;␊ |
528 | ␉_PrintTree(node, 0);␊ |
529 | }␊ |
530 | ␊ |
531 | //==============================================================================␊ |
532 | ␊ |
533 | void␊ |
534 | DT__PrintFlattenedNode(DTEntry entry, int level)␊ |
535 | {␊ |
536 | ␉char spaces[10], *cp = spaces;␊ |
537 | ␉DTPropertyIterator␉ propIter;␊ |
538 | ␉char *name;␊ |
539 | ␉void *prop;␊ |
540 | ␉int propSize;␊ |
541 | ␊ |
542 | ␉if (level > 9) level = 9;␊ |
543 | ␉while (level--) *cp++ = ' ';␊ |
544 | ␉␉*cp = '\0';␊ |
545 | ␊ |
546 | ␉printf("%s===Entry %p===\n", spaces, entry);␊ |
547 | ␉␉if (kSuccess != DTCreatePropertyIterator(entry, &propIter))␊ |
548 | ␉␉{␊ |
549 | ␉␉␉printf("Couldn't create property iterator\n");␊ |
550 | ␉␉␉return;␊ |
551 | ␉␉}␊ |
552 | ␉␉while( kSuccess == DTIterateProperties( propIter, &name))␊ |
553 | ␉␉{␊ |
554 | ␉␉␉if( kSuccess != DTGetProperty( entry, name, &prop, &propSize ))␊ |
555 | ␉␉␉␉continue;␊ |
556 | ␉␉printf("%s Property %s = %s\n", spaces, name, prop);␊ |
557 | ␉}␊ |
558 | ␉DTDisposePropertyIterator(propIter);␊ |
559 | ␊ |
560 | ␉printf("%s==========\n", spaces);␊ |
561 | }␊ |
562 | ␊ |
563 | //==============================================================================␊ |
564 | ␊ |
565 | static void␊ |
566 | _PrintFlattenedTree(DTEntry entry, int level)␊ |
567 | {␊ |
568 | ␉DTEntryIterator entryIter;␊ |
569 | ␊ |
570 | ␉PrintFlattenedNode(entry, level);␊ |
571 | ␊ |
572 | ␉if (kSuccess != DTCreateEntryIterator(entry, &entryIter))␊ |
573 | ␉{␊ |
574 | ␉␉printf("Couldn't create entry iterator\n");␊ |
575 | ␉␉return;␊ |
576 | ␉}␊ |
577 | ␉level++;␊ |
578 | ␉while (kSuccess == DTIterateEntries( entryIter, &entry ))␊ |
579 | ␉{␊ |
580 | ␉␉_PrintFlattenedTree(entry, level);␊ |
581 | ␉}␊ |
582 | ␉DTDisposeEntryIterator(entryIter);␊ |
583 | }␊ |
584 | ␊ |
585 | //==============================================================================␊ |
586 | ␊ |
587 | void␊ |
588 | DT__PrintFlattenedTree(DTEntry entry)␊ |
589 | {␊ |
590 | ␉_PrintFlattenedTree(entry, 0);␊ |
591 | }␊ |
592 | ␊ |
593 | //==============================================================================␊ |
594 | ␊ |
595 | int␊ |
596 | main(int argc, char **argv)␊ |
597 | {␊ |
598 | ␉DTEntry␉␉␉dtEntry;␊ |
599 | ␉DTPropertyIterator␉propIter;␊ |
600 | ␉DTEntryIterator␉␉entryIter;␊ |
601 | ␉void␉␉␉*prop;␊ |
602 | ␉int␉␉␉propSize;␊ |
603 | ␉char␉␉␉*name;␊ |
604 | ␉void␉␉␉*flatTree;␊ |
605 | ␉uint32_t␉␉flatSize;␊ |
606 | ␊ |
607 | ␉Node *node;␊ |
608 | ␊ |
609 | ␉node = AddChild(NULL, "device-tree");␊ |
610 | ␉AddProperty(node, "potato", 4, "foo");␊ |
611 | ␉AddProperty(node, "chemistry", 4, "bar");␊ |
612 | ␉AddProperty(node, "physics", 4, "baz");␊ |
613 | ␊ |
614 | ␉node = AddChild(node, "dev");␊ |
615 | ␉AddProperty(node, "one", 4, "one");␊ |
616 | ␉AddProperty(node, "two", 4, "two");␊ |
617 | ␉AddProperty(node, "three", 6, "three");␊ |
618 | ␊ |
619 | ␉node = AddChild(rootNode, "foo");␊ |
620 | ␉AddProperty(node, "aaa", 4, "aab");␊ |
621 | ␉AddProperty(node, "bbb", 4, "bbc");␊ |
622 | ␉AddProperty(node, "cccc", 6, "ccccd");␊ |
623 | ␊ |
624 | ␉node = FindNode("/this/is/a/test", 1);␊ |
625 | ␉AddProperty(node, "dddd", 12, "abcdefghijk");␊ |
626 | ␊ |
627 | ␉printf("In-memory tree:\n\n");␊ |
628 | ␊ |
629 | ␉PrintTree(rootNode);␊ |
630 | ␊ |
631 | ␉FlattenDeviceTree(&flatTree, &flatSize);␊ |
632 | ␊ |
633 | ␉printf("Flat tree = %p, size %d\n", flatTree, flatSize);␊ |
634 | ␊ |
635 | ␉dtEntry = (DTEntry)flatTree;␊ |
636 | ␊ |
637 | ␉printf("\n\nPrinting flat tree\n\n");␊ |
638 | ␊ |
639 | ␉DTInit(dtEntry);␊ |
640 | ␊ |
641 | ␉PrintFlattenedTree((DTEntry)flatTree);␊ |
642 | #if 0␊ |
643 | ␉printf("=== Entry %p ===\n", dtEntry);␊ |
644 | ␉if (kSuccess != DTCreatePropertyIterator(dtEntry, &propIter))␊ |
645 | ␉{␊ |
646 | ␉␉printf("Couldn't create property iterator\n");␊ |
647 | ␉␉return 1;␊ |
648 | ␉}␊ |
649 | ␉while( kSuccess == DTIterateProperties( propIter, &name))␊ |
650 | ␉{␊ |
651 | ␉␉if( kSuccess != DTGetProperty( dtEntry, name, &prop, &propSize ))␊ |
652 | ␉␉␉continue;␊ |
653 | ␉␉printf(" Property %s = %s\n", name, prop);␊ |
654 | }␊ |
655 | ␉DTDisposePropertyIterator(propIter);␊ |
656 | ␉printf("========\n");␊ |
657 | ␊ |
658 | ␉if (kSuccess != DTCreateEntryIterator(dtEntry, &entryIter))␊ |
659 | ␉{␊ |
660 | ␉␉printf("Couldn't create entry iterator\n");␊ |
661 | ␉␉return 1;␊ |
662 | ␉}␊ |
663 | ␉while (kSuccess == DTIterateEntries( entryIter, &dtEntry ))␊ |
664 | ␉{␊ |
665 | ␉␉printf("=== Entry %p ===\n", dtEntry);␊ |
666 | ␊ |
667 | ␉if (kSuccess != DTCreatePropertyIterator(dtEntry, &propIter))␊ |
668 | ␉{␊ |
669 | ␉␉printf("Couldn't create property iterator\n");␊ |
670 | ␉␉return 1;␊ |
671 | ␉}␊ |
672 | ␉␉while( kSuccess == DTIterateProperties( propIter, &name))␊ |
673 | ␉␉{␊ |
674 | ␉␉␉if( kSuccess != DTGetProperty( dtEntry, name, &prop, &propSize ))␊ |
675 | ␉␉␉␉continue;␊ |
676 | ␉␉␉printf(" Property %s = %s\n", name, prop);␊ |
677 | ␉␉}␊ |
678 | ␉␉DTDisposePropertyIterator(propIter);␊ |
679 | ␉␉printf("========\n");␊ |
680 | ␉}␊ |
681 | ␉DTDisposeEntryIterator(entryIter);␊ |
682 | #endif␊ |
683 | ␊ |
684 | ␉return 0;␊ |
685 | }␊ |
686 | ␊ |
687 | #endif␊ |
688 | ␊ |
689 | |