Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 2658