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