Chameleon

Chameleon Svn Source Tree

Root/trunk/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("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("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
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{
405Property *prop;
406
407for (prop = node->properties; prop; prop = prop->next)
408{
409if (strcmp(prop->name, name) == 0)
410{
411return prop;
412}
413}
414
415return 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("DT__FindNode: root = 0x%x\n", rootNode);
434
435while (node)
436{
437// Skip leading slash(es)
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("DT__FindNode: Node '%s'\n", nameBuf);
457
458for (child = node->children; child != 0; child = child->next)
459{
460DPRINTF("DT__FindNode: Child 0x%x\n", child);
461
462if (strcmp(DT__GetName(child), nameBuf) == 0)
463{
464break;
465}
466}
467
468if (child == 0 && createIfMissing)
469{
470char *str = malloc(strlen(nameBuf) + 1);
471// XXX this will leak
472strcpy(str, nameBuf);
473
474child = DT__AddChild(node, str);
475DPRINTF("DT__FindNode: Creating node: %s\n", str);
476}
477
478node = child;
479}
480
481return node;
482}
483
484#if DEBUG
485
486//==============================================================================
487
488void
489DT__PrintNode(Node *node, int level)
490{
491char spaces[10], *cp = spaces;
492Property *prop;
493
494if (level > 9)
495{
496level = 9;
497}
498
499while (level--)
500{
501*cp++ = ' ';
502}
503
504*cp = '\0';
505
506printf("%s===Node===\n", spaces);
507
508for (prop = node->properties; prop; prop = prop->next)
509{
510char c = *((char *)prop->value);
511if (prop->length < 64 && (strcmp(prop->name, "name") == 0 || (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'))
512{
513printf("%s Property '%s' [%d] = '%s'\n", spaces, prop->name, prop->length, prop->value);
514}
515else
516{
517printf("%s Property '%s' [%d] = (data)\n", spaces, prop->name, prop->length);
518}
519}
520
521printf("%s==========\n", spaces);
522}
523
524//==============================================================================
525
526static void
527_PrintTree(Node *node, int level)
528{
529DT__PrintNode(node, level);
530
531level++;
532
533for (node = node->children; node; node = node->next)
534{
535_PrintTree(node, level);
536}
537}
538
539//==============================================================================
540
541void
542DT__PrintTree(Node *node)
543{
544if (node == 0) node = rootNode;
545_PrintTree(node, 0);
546}
547
548//==============================================================================
549
550void
551DT__PrintFlattenedNode(DTEntry entry, int level)
552{
553char spaces[10], *cp = spaces;
554DTPropertyIterator propIter;
555char *name;
556void *prop;
557int propSize;
558
559if (level > 9) level = 9;
560while (level--) *cp++ = ' ';
561*cp = '\0';
562
563printf("%s===Entry %p===\n", spaces, entry);
564if (kSuccess != DTCreatePropertyIterator(entry, &propIter))
565{
566printf("Couldn't create property iterator\n");
567return;
568}
569while( kSuccess == DTIterateProperties( propIter, &name))
570{
571if( kSuccess != DTGetProperty( entry, name, &prop, &propSize ))
572continue;
573printf("%s Property %s = %s\n", spaces, name, prop);
574}
575DTDisposePropertyIterator(propIter);
576
577printf("%s==========\n", spaces);
578}
579
580//==============================================================================
581
582static void
583_PrintFlattenedTree(DTEntry entry, int level)
584{
585DTEntryIterator entryIter;
586
587PrintFlattenedNode(entry, level);
588
589if (kSuccess != DTCreateEntryIterator(entry, &entryIter))
590{
591printf("Couldn't create entry iterator\n");
592return;
593}
594level++;
595while (kSuccess == DTIterateEntries( entryIter, &entry ))
596{
597_PrintFlattenedTree(entry, level);
598}
599DTDisposeEntryIterator(entryIter);
600}
601
602//==============================================================================
603
604void
605DT__PrintFlattenedTree(DTEntry entry)
606{
607_PrintFlattenedTree(entry, 0);
608}
609
610//==============================================================================
611
612int
613main(int argc, char **argv)
614{
615DTEntrydtEntry;
616DTPropertyIteratorpropIter;
617DTEntryIteratorentryIter;
618void*prop;
619intpropSize;
620char*name;
621void*flatTree;
622uint32_tflatSize;
623
624Node *node;
625
626node = AddChild(NULL, "device-tree");
627AddProperty(node, "potato", 4, "foo");
628AddProperty(node, "chemistry", 4, "bar");
629AddProperty(node, "physics", 4, "baz");
630
631node = AddChild(node, "dev");
632AddProperty(node, "one", 4, "one");
633AddProperty(node, "two", 4, "two");
634AddProperty(node, "three", 6, "three");
635
636node = AddChild(rootNode, "foo");
637AddProperty(node, "aaa", 4, "aab");
638AddProperty(node, "bbb", 4, "bbc");
639AddProperty(node, "cccc", 6, "ccccd");
640
641node = FindNode("/this/is/a/test", 1);
642AddProperty(node, "dddd", 12, "abcdefghijk");
643
644printf("In-memory tree:\n\n");
645
646PrintTree(rootNode);
647
648FlattenDeviceTree(&flatTree, &flatSize);
649
650printf("Flat tree = %p, size %d\n", flatTree, flatSize);
651
652dtEntry = (DTEntry)flatTree;
653
654printf("\n\nPrinting flat tree\n\n");
655
656DTInit(dtEntry);
657
658PrintFlattenedTree((DTEntry)flatTree);
659#if 0
660printf("=== Entry %p ===\n", dtEntry);
661if (kSuccess != DTCreatePropertyIterator(dtEntry, &propIter))
662{
663printf("Couldn't create property iterator\n");
664return 1;
665}
666while( kSuccess == DTIterateProperties( propIter, &name))
667{
668if( kSuccess != DTGetProperty( dtEntry, name, &prop, &propSize ))
669continue;
670printf(" Property %s = %s\n", name, prop);
671 }
672DTDisposePropertyIterator(propIter);
673printf("========\n");
674
675if (kSuccess != DTCreateEntryIterator(dtEntry, &entryIter))
676{
677printf("Couldn't create entry iterator\n");
678return 1;
679}
680while (kSuccess == DTIterateEntries( entryIter, &dtEntry ))
681{
682printf("=== Entry %p ===\n", dtEntry);
683
684if (kSuccess != DTCreatePropertyIterator(dtEntry, &propIter))
685{
686printf("Couldn't create property iterator\n");
687return 1;
688}
689while( kSuccess == DTIterateProperties( propIter, &name))
690{
691if( kSuccess != DTGetProperty( dtEntry, name, &prop, &propSize ))
692continue;
693printf(" Property %s = %s\n", name, prop);
694}
695DTDisposePropertyIterator(propIter);
696printf("========\n");
697}
698DTDisposeEntryIterator(entryIter);
699#endif
700
701return 0;
702}
703
704#endif
705
706

Archive Download this file

Revision: 2538