Chameleon

Chameleon Svn Source Tree

Root/branches/Bungo/i386/libsaio/device_tree.c

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

Archive Download this file

Revision: 2469