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