]> git.saurik.com Git - apple/xnu.git/blob - pexpert/i386/fakePPCDeviceTree.c
xnu-792.17.14.tar.gz
[apple/xnu.git] / pexpert / i386 / fakePPCDeviceTree.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <pexpert/protos.h>
30
31 #include "fakePPCStructs.h"
32
33 boot_args fakePPCBootArgs = { .Version = kBootArgsVersion };
34
35 void * createdt(dt_init * template, long * retSize)
36 {
37 dt_init * next;
38 size_t size, allocSize;
39 vm_address_t out, saveout;
40 void * source;
41
42 // calc size of expanded data
43 for ( next = template, allocSize = 0;
44 next;
45 next++ )
46 {
47 if ( next->nodeInit.zero == 0 )
48 {
49 if( next->nodeInit.nProps == 0) break;
50 allocSize += 2 * sizeof( long);
51 }
52 else if ( next->dataInit.one == 1 )
53 {
54 allocSize += *(next->dataInit.length);
55 }
56 else if ( next->stringInit.two == 2 )
57 {
58 dt_data *dp = (dt_data *)(next->stringInit.data);
59 allocSize += (32 + 4 + 3 + dp->length) & (-4);
60 }
61 else
62 {
63 allocSize += (32 + 4 + 3 + next->propInit.length) & (-4);
64 }
65 }
66 saveout = out = (vm_address_t) kalloc(allocSize);
67 if ( out == 0 ) return 0;
68
69 // copy out
70 for ( next = template; next; next++)
71 {
72 if ( next->nodeInit.zero == 0 )
73 {
74 if ( next->nodeInit.nProps == 0 ) break;
75 source = &next->nodeInit.nProps;
76 size = 2 * sizeof( long);
77 }
78 else if ( next->dataInit.one == 1 )
79 {
80 *((long *)next->dataInit.address) = out;
81 source = 0;
82 size = *(next->dataInit.length);
83 }
84 else if ( next->stringInit.two == 2 )
85 {
86 dt_data *dp = (dt_data *)next->stringInit.data;
87 bcopy( (void *)(uintptr_t)next->stringInit.name, (void *)out, 32);
88 out += 32;
89 size = dp->length;
90 *(long *)out = size;
91 out += sizeof(long);
92 source = (char *)dp->address;
93 size = (size + 3) & (-4);
94 }
95 else
96 {
97 bcopy( (void *)(uintptr_t)next->propInit.name, (void *)out, 32);
98 out += 32;
99 size = next->propInit.length;
100 *(long *)out = size;
101 out += sizeof(long);
102 if ( size == 4 )
103 source = &next->propInit.value;
104 else {
105 source = next->propInit.value;
106 size = (size + 3) & (-4);
107 }
108 }
109 if ( source )
110 bcopy(source, (void *)out, size);
111 else
112 bzero((void *)out, size);
113 out += size;
114 }
115
116 if( allocSize != (out - saveout))
117 printf("WARNING: DT corrupt (%x)\n", (out - saveout) - allocSize);
118
119 *retSize = allocSize;
120 return( (void *)saveout );
121 }
122
123 unsigned char *nptr;
124
125 #define kPropNameLength 32
126
127 typedef struct property_t {
128 char name[kPropNameLength]; // NUL terminated property name
129 unsigned long length; // Length (bytes) of folloing prop value
130 unsigned long *value; // Variable length value of property
131 } property_t;
132
133 typedef struct node_t {
134 unsigned long nProperties; // Number of props[] elements (0 => end)
135 unsigned long nChildren; // Number of children[] elements
136 property_t *props; // array size == nProperties
137 struct node_t *children; // array size == nChildren
138 } node_t;
139
140
141 unsigned int indent = 0;
142
143 void printdt()
144 {
145 node_t *nodeptr = (node_t *)nptr;
146 unsigned long num_props = nodeptr->nProperties;
147 unsigned long len;
148 unsigned int i, j;
149 unsigned char *sptr;
150
151 nptr = (unsigned char *)&nodeptr->props;
152 for (i=0; i < num_props; i++)
153 {
154 for (j = 0; j < indent; j++)
155 printf(" ");
156 printf("'");
157 printf("%s", nptr);
158 nptr+=32;
159 len = *((unsigned long*)nptr);
160 nptr += 4;
161 printf("'\t\t(%ld) '", len);
162 sptr = nptr;
163 for (j = 0; j < len; j++)
164 printf("%2.2x", *nptr++);
165 printf("'\t('%s')\n", sptr);
166 if (len % 4)
167 nptr += (4 - (len % 4));
168 }
169 for (i=0; i<nodeptr->nChildren; i++)
170 {
171 indent++;
172 printdt();
173 indent--;
174 }
175 }
176