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

Archive Download this file

Revision: 2331