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