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("Done [0x%x]\n", prop);␊ |
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 | void 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 | }␊ |
239 | ␊ |
240 | //==============================================================================␊ |
241 | /*␊ |
242 | * Free up memory used by in-memory representation of device tree.␊ |
243 | */␊ |
244 | void DT__Finalize(void)␊ |
245 | {␊ |
246 | ␉Node *node;␊ |
247 | ␉Property *prop;␊ |
248 | ␊ |
249 | ␉DPRINTF("DT__Finalize\n");␊ |
250 | ␊ |
251 | ␉for (prop = allocedProperties; prop != NULL; prop = prop->next)␊ |
252 | ␉{␊ |
253 | ␉␉free((void *)(prop->name));␊ |
254 | ␉␉free(prop->value);␊ |
255 | ␉}␊ |
256 | ␊ |
257 | ␉allocedProperties = NULL;␊ |
258 | ␉freeProperties = NULL;␊ |
259 | ␊ |
260 | ␉for (node = allocedNodes; node != NULL; node = node->next)␊ |
261 | ␉{␊ |
262 | ␉␉free((void *)node->children);␊ |
263 | ␉}␊ |
264 | ␊ |
265 | ␉allocedNodes = NULL;␊ |
266 | ␉freeNodes = NULL;␊ |
267 | ␉rootNode = NULL;␊ |
268 | ␊ |
269 | ␉// XXX leaks any created strings␊ |
270 | ␉DTInfo.numNodes = 0;␊ |
271 | ␉DTInfo.numProperties = 0;␊ |
272 | ␉DTInfo.totalPropertySize = 0;␊ |
273 | }␊ |
274 | ␊ |
275 | //==============================================================================␊ |
276 | ␊ |
277 | static void *FlattenNodes(Node *node, void *buffer)␊ |
278 | {␊ |
279 | ␉Property *prop;␊ |
280 | ␉DeviceTreeNode *flatNode;␊ |
281 | ␉DeviceTreeNodeProperty *flatProp;␊ |
282 | ␉int count;␊ |
283 | ␊ |
284 | ␉if (node == 0) {␊ |
285 | ␉␉return buffer;␊ |
286 | ␉}␊ |
287 | ␊ |
288 | ␉flatNode = (DeviceTreeNode *)buffer;␊ |
289 | ␉buffer += sizeof(DeviceTreeNode);␊ |
290 | ␊ |
291 | ␉for (count = 0, prop = node->properties; prop != 0; count++, prop = prop->next)␊ |
292 | ␉{␊ |
293 | ␉␉flatProp = (DeviceTreeNodeProperty *)buffer;␊ |
294 | ␉␉strncpy(flatProp->name, prop->name, kPropNameLength);␊ |
295 | ␉␉flatProp->length = prop->length;␊ |
296 | ␉␉buffer += sizeof(DeviceTreeNodeProperty);␊ |
297 | ␉␉bcopy(prop->value, buffer, prop->length);␊ |
298 | ␉␉buffer += RoundToLong(prop->length);␊ |
299 | ␉}␊ |
300 | ␊ |
301 | ␉flatNode->nProperties = count;␊ |
302 | ␊ |
303 | ␉for (count = 0, node = node->children; node != 0; count++, node = node->next)␊ |
304 | ␉{␊ |
305 | ␉␉buffer = FlattenNodes(node, buffer);␊ |
306 | ␉}␊ |
307 | ␊ |
308 | ␉flatNode->nChildren = count;␊ |
309 | ␊ |
310 | ␉return buffer;␊ |
311 | }␊ |
312 | ␊ |
313 | ␊ |
314 | /*==============================================================================␊ |
315 | * Flatten the in-memory representation of the device tree into a binary DT block.␊ |
316 | * To get the buffer size needed, call with result = 0.␊ |
317 | * To have a buffer allocated for you, call with *result = 0.␊ |
318 | * To use your own buffer, call with *result = &buffer.␊ |
319 | */␊ |
320 | ␊ |
321 | void DT__FlattenDeviceTree(void **buffer_p, uint32_t *length)␊ |
322 | {␊ |
323 | ␉uint32_t totalSize;␊ |
324 | ␉void * buf;␊ |
325 | ␊ |
326 | ␉DPRINTF("DT__FlattenDeviceTree(0x%x, 0x%x)\n", buffer_p, length);␊ |
327 | ␊ |
328 | #if DEBUG␊ |
329 | ␉if (buffer_p) {␊ |
330 | ␉␉DT__PrintTree(rootNode);␊ |
331 | ␉}␊ |
332 | #endif␊ |
333 | ␊ |
334 | ␉totalSize = DTInfo.numNodes * sizeof(DeviceTreeNode) + ␊ |
335 | ␉␉DTInfo.numProperties * sizeof(DeviceTreeNodeProperty) +␊ |
336 | ␉␉DTInfo.totalPropertySize;␊ |
337 | ␊ |
338 | ␉DPRINTF("Total size 0x%x\n", totalSize);␊ |
339 | ␊ |
340 | ␉if (buffer_p != 0)␊ |
341 | ␉{␊ |
342 | ␉␉if (totalSize == 0)␊ |
343 | ␉␉{␊ |
344 | ␉␉␉buf = 0;␊ |
345 | ␉␉}␊ |
346 | ␉␉else␊ |
347 | ␉␉{␊ |
348 | ␉␉␉if (*buffer_p == 0)␊ |
349 | ␉␉␉{␊ |
350 | ␉␉␉␉buf = malloc(totalSize);␊ |
351 | ␉␉␉}␊ |
352 | ␉␉␉else␊ |
353 | ␉␉␉{␊ |
354 | ␉␉␉␉buf = *buffer_p;␊ |
355 | ␉␉␉}␊ |
356 | ␊ |
357 | ␉␉␉bzero(buf, totalSize);␊ |
358 | ␊ |
359 | ␉␉␉FlattenNodes(rootNode, buf);␊ |
360 | ␉␉}␊ |
361 | ␊ |
362 | ␉␉*buffer_p = buf;␊ |
363 | ␉}␊ |
364 | ␊ |
365 | ␉if (length)␊ |
366 | ␉{␊ |
367 | ␉␉*length = totalSize;␊ |
368 | ␉}␊ |
369 | }␊ |
370 | ␊ |
371 | //==============================================================================␊ |
372 | ␊ |
373 | char *DT__GetName(Node *node)␊ |
374 | {␊ |
375 | ␉Property *prop;␊ |
376 | ␊ |
377 | ␉//DPRINTF("DT__GetName(0x%x)\n", node);␊ |
378 | ␉//DPRINTF("Node properties = 0x%x\n", node->properties);␊ |
379 | ␉for (prop = node->properties; prop; prop = prop->next)␊ |
380 | ␉{␊ |
381 | ␉␉//DPRINTF("Prop '%s'\n", prop->name);␊ |
382 | ␉␉if (strcmp(prop->name, "name") == 0)␊ |
383 | ␉␉{␊ |
384 | ␉␉␉return prop->value;␊ |
385 | ␉␉}␊ |
386 | ␉}␊ |
387 | ␊ |
388 | ␉//DPRINTF("DT__GetName returns 0\n");␊ |
389 | ␉return "(null)";␊ |
390 | }␊ |
391 | ␊ |
392 | //==============================================================================␊ |
393 | // Bungo␊ |
394 | Property *DT__GetProperty(Node *node, const char *name)␊ |
395 | {␊ |
396 | ␉Property *prop;␊ |
397 | ␊ |
398 | ␉for (prop = node->properties; prop; prop = prop->next)␊ |
399 | ␉{␊ |
400 | ␉␉if (strcmp(prop->name, name) == 0)␊ |
401 | ␉␉{␊ |
402 | ␉␉␉return prop;␊ |
403 | ␉␉}␊ |
404 | ␉}␊ |
405 | ␊ |
406 | ␉return NULL;␊ |
407 | }␊ |
408 | ␊ |
409 | //==============================================================================␊ |
410 | ␊ |
411 | Node *DT__FindNode(const char *path, bool createIfMissing)␊ |
412 | {␊ |
413 | ␉Node *node, *child;␊ |
414 | ␉DTPropertyNameBuf nameBuf;␊ |
415 | ␉char *bp;␊ |
416 | ␉int i;␊ |
417 | ␊ |
418 | ␉DPRINTF("DT__FindNode('%s', %d)\n", path, createIfMissing);␊ |
419 | ␊ |
420 | ␉// Start at root␊ |
421 | ␉node = rootNode;␊ |
422 | ␊ |
423 | ␉DPRINTF("DT__FindNode: root = 0x%x\n", rootNode);␊ |
424 | ␊ |
425 | ␉while (node)␊ |
426 | ␉{␊ |
427 | ␉␉// Skip leading slash(es)␊ |
428 | ␉␉while (*path == '/')␊ |
429 | ␉␉{␊ |
430 | ␉␉␉path++;␊ |
431 | ␉␉}␊ |
432 | ␊ |
433 | ␉␉for (i = 0, bp = nameBuf; ++i < kDTMaxEntryNameLength && *path && *path != '/'; bp++, path++)␊ |
434 | ␉␉{␊ |
435 | ␉␉␉*bp = *path;␊ |
436 | ␉␉}␊ |
437 | ␊ |
438 | ␉␉*bp = '\0';␊ |
439 | ␊ |
440 | ␉␉if (nameBuf[0] == '\0')␊ |
441 | ␉␉{␊ |
442 | ␉␉␉// last path entry␊ |
443 | ␉␉␉break;␊ |
444 | ␉␉}␊ |
445 | ␊ |
446 | ␉␉DPRINTF("DT__FindNode: Node '%s'\n", nameBuf);␊ |
447 | ␊ |
448 | ␉␉for (child = node->children; child != 0; child = child->next)␊ |
449 | ␉␉{␊ |
450 | ␉␉␉DPRINTF("DT__FindNode: Child 0x%x\n", child);␊ |
451 | ␊ |
452 | ␉␉␉if (strcmp(DT__GetName(child), nameBuf) == 0)␊ |
453 | ␉␉␉{␊ |
454 | ␉␉␉␉break;␊ |
455 | ␉␉␉}␊ |
456 | ␉␉}␊ |
457 | ␊ |
458 | ␉␉if (child == 0 && createIfMissing)␊ |
459 | ␉␉{␊ |
460 | ␉␉␉char *str = malloc(strlen(nameBuf) + 1);␊ |
461 | ␉␉␉// XXX this will leak␊ |
462 | ␉␉␉strcpy(str, nameBuf);␊ |
463 | ␊ |
464 | ␉␉␉child = DT__AddChild(node, str);␊ |
465 | ␉␉␉DPRINTF("DT__FindNode: Creating node: %s\n", str);␊ |
466 | ␉␉}␊ |
467 | ␊ |
468 | ␉␉node = child;␊ |
469 | ␉}␊ |
470 | ␊ |
471 | ␉return node;␊ |
472 | }␊ |
473 | ␊ |
474 | #if DEBUG␊ |
475 | ␊ |
476 | //==============================================================================␊ |
477 | ␊ |
478 | void DT__PrintNode(Node *node, int level)␊ |
479 | {␊ |
480 | ␉char spaces[10], *cp = spaces;␊ |
481 | ␉Property *prop;␊ |
482 | ␊ |
483 | ␉if (level > 9)␊ |
484 | ␉{␊ |
485 | ␉␉level = 9;␊ |
486 | ␉}␊ |
487 | ␊ |
488 | ␉while (level--)␊ |
489 | ␉{␊ |
490 | ␉␉*cp++ = ' ';␊ |
491 | ␉}␊ |
492 | ␊ |
493 | ␉*cp = '\0';␊ |
494 | ␊ |
495 | ␉printf("%s===Node===\n", spaces);␊ |
496 | ␊ |
497 | ␉for (prop = node->properties; prop; prop = prop->next)␊ |
498 | ␉{␊ |
499 | ␉␉char c = *((char *)prop->value);␊ |
500 | ␉␉if (prop->length < 64 && (strcmp(prop->name, "name") == 0 || (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'))␊ |
501 | ␉␉{␊ |
502 | ␉␉␉printf("%s Property '%s' [%d] = '%s'\n", spaces, prop->name, prop->length, prop->value);␊ |
503 | ␉␉}␊ |
504 | ␉␉else␊ |
505 | ␉␉{␊ |
506 | ␉␉␉printf("%s Property '%s' [%d] = (data)\n", spaces, prop->name, prop->length);␊ |
507 | ␉␉}␊ |
508 | ␉}␊ |
509 | ␊ |
510 | ␉printf("%s==========\n", spaces);␊ |
511 | }␊ |
512 | ␊ |
513 | //==============================================================================␊ |
514 | ␊ |
515 | static void _PrintTree(Node *node, int level)␊ |
516 | {␊ |
517 | ␉DT__PrintNode(node, level);␊ |
518 | ␊ |
519 | ␉level++;␊ |
520 | ␊ |
521 | ␉for (node = node->children; node; node = node->next)␊ |
522 | ␉{␊ |
523 | ␉␉_PrintTree(node, level);␊ |
524 | ␉}␊ |
525 | }␊ |
526 | ␊ |
527 | //==============================================================================␊ |
528 | ␊ |
529 | void DT__PrintTree(Node *node)␊ |
530 | {␊ |
531 | ␉if (node == 0) node = rootNode;␊ |
532 | ␉_PrintTree(node, 0);␊ |
533 | }␊ |
534 | ␊ |
535 | //==============================================================================␊ |
536 | ␊ |
537 | void DT__PrintFlattenedNode(DTEntry entry, int level)␊ |
538 | {␊ |
539 | ␉char spaces[10], *cp = spaces;␊ |
540 | ␉DTPropertyIterator␉ propIter;␊ |
541 | ␉char *name;␊ |
542 | ␉void *prop;␊ |
543 | ␉int propSize;␊ |
544 | ␊ |
545 | ␉if (level > 9) level = 9;␊ |
546 | ␉while (level--) *cp++ = ' ';␊ |
547 | ␉␉*cp = '\0';␊ |
548 | ␊ |
549 | ␉printf("%s===Entry %p===\n", spaces, entry);␊ |
550 | ␉␉if (kSuccess != DTCreatePropertyIterator(entry, &propIter))␊ |
551 | ␉␉{␊ |
552 | ␉␉␉printf("Couldn't create property iterator\n");␊ |
553 | ␉␉␉return;␊ |
554 | ␉␉}␊ |
555 | ␉␉while( kSuccess == DTIterateProperties( propIter, &name))␊ |
556 | ␉␉{␊ |
557 | ␉␉␉if( kSuccess != DTGetProperty( entry, name, &prop, &propSize ))␊ |
558 | ␉␉␉␉continue;␊ |
559 | ␉␉printf("%s Property %s = %s\n", spaces, name, prop);␊ |
560 | ␉}␊ |
561 | ␉DTDisposePropertyIterator(propIter);␊ |
562 | ␊ |
563 | ␉printf("%s==========\n", spaces);␊ |
564 | }␊ |
565 | ␊ |
566 | //==============================================================================␊ |
567 | ␊ |
568 | static void _PrintFlattenedTree(DTEntry entry, int level)␊ |
569 | {␊ |
570 | ␉DTEntryIterator entryIter;␊ |
571 | ␊ |
572 | ␉PrintFlattenedNode(entry, level);␊ |
573 | ␊ |
574 | ␉if (kSuccess != DTCreateEntryIterator(entry, &entryIter))␊ |
575 | ␉{␊ |
576 | ␉␉printf("Couldn't create entry iterator\n");␊ |
577 | ␉␉return;␊ |
578 | ␉}␊ |
579 | ␉level++;␊ |
580 | ␉while (kSuccess == DTIterateEntries( entryIter, &entry ))␊ |
581 | ␉{␊ |
582 | ␉␉_PrintFlattenedTree(entry, level);␊ |
583 | ␉}␊ |
584 | ␉DTDisposeEntryIterator(entryIter);␊ |
585 | }␊ |
586 | ␊ |
587 | //==============================================================================␊ |
588 | ␊ |
589 | void DT__PrintFlattenedTree(DTEntry entry)␊ |
590 | {␊ |
591 | ␉_PrintFlattenedTree(entry, 0);␊ |
592 | }␊ |
593 | ␊ |
594 | //==============================================================================␊ |
595 | ␊ |
596 | int 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 | |