aboutsummaryrefslogtreecommitdiff
path: root/stand/powerpc/ofw/ofwfdt.c
blob: fa0ff4165f4ade6ea1a6cdc59167f06207d5fa59 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*-
 * Copyright (C) 2014-2015 Nathan Whitehorn
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <stand.h>
#include <sys/param.h>
#include <fdt_platform.h>
#include <openfirm.h>
#include <libfdt.h>
#include "bootstrap.h"

extern int command_fdt_internal(int argc, char *argv[]);

static int
OF_hasprop(phandle_t node, const char *prop)
{
	return (OF_getproplen(node, (char *)prop) > 0);
}

static void
add_node_to_fdt(void *buffer, phandle_t node, int fdt_offset)
{
	int i, child_offset, error;
	char name[255], *lastprop, *subname;
	void *propbuf;
	ssize_t proplen;

	lastprop = NULL;
	while (OF_nextprop(node, lastprop, name) > 0) {
		proplen = OF_getproplen(node, name);

		/* Detect and correct for errors and strangeness */
		if (proplen < 0)
			proplen = 0;
		if (proplen > 1024)
			proplen = 1024;

		propbuf = malloc(proplen);
		if (propbuf == NULL) {
			printf("Cannot allocate memory for prop %s\n", name);
			return;
		}
		OF_getprop(node, name, propbuf, proplen);
		error = fdt_setprop(buffer, fdt_offset, name, propbuf, proplen);
		free(propbuf);
		lastprop = name;
		if (error)
			printf("Error %d adding property %s to "
			    "node %d\n", error, name, fdt_offset);
	}

	if (!OF_hasprop(node, "phandle") && !OF_hasprop(node, "linux,phandle")
	    && !OF_hasprop(node, "ibm,phandle"))
		fdt_setprop(buffer, fdt_offset, "phandle", &node, sizeof(node));

	for (node = OF_child(node); node > 0; node = OF_peer(node)) {
		OF_package_to_path(node, name, sizeof(name));
		subname = strrchr(name, '/');
		subname++;
		child_offset = fdt_add_subnode(buffer, fdt_offset, subname);
		if (child_offset < 0) {
			printf("Error %d adding node %s (%s), skipping\n",
			    child_offset, name, subname);
			continue;
		}
	
                add_node_to_fdt(buffer, node, child_offset);
	}
}

static void
ofwfdt_fixups(void *fdtp)
{
	int offset, len, i;
	phandle_t node;
	ihandle_t rtas;
	const void *prop;

	/*
	 * Instantiate and add reservations for RTAS state if present
	 */

	offset = fdt_path_offset(fdtp, "/rtas");
	if (offset > 0) {
		uint32_t base;
		void *rtasmem;
		char path[255];

		node = OF_finddevice("/rtas");
		OF_package_to_path(node, path, sizeof(path));
		OF_getprop(node, "rtas-size", &len, sizeof(len));

		/* Allocate memory */
		rtasmem = OF_claim(0, len, 4096);

		/* Instantiate RTAS */
		rtas = OF_open(path);
		base = 0;
		OF_call_method("instantiate-rtas", rtas, 1, 1, (cell_t)rtas,
		    &base);

		/* Store info to FDT using Linux convention */
		base = cpu_to_fdt32(base);
		fdt_setprop(fdtp, offset, "linux,rtas-entry", &base,
		    sizeof(base));
		base = cpu_to_fdt32((uint32_t)rtasmem);
		offset = fdt_path_offset(fdtp, "/rtas");
		fdt_setprop(fdtp, offset, "linux,rtas-base", &base,
		    sizeof(base));

		/* Mark RTAS private data area reserved */
		fdt_add_mem_rsv(fdtp, base, len);
	} else {
		/*
		 * Remove /memory/available properties, which reflect long-gone
		 * OF state. Note that this doesn't work if we need RTAS still,
		 * since that's part of the firmware.
		 */
		offset = fdt_path_offset(fdtp, "/memory@0");
		if (offset > 0)
			fdt_delprop(fdtp, offset, "available");
	}

	
	/*
	 * Convert stored ihandles under /chosen to xref phandles
	 */
	offset = fdt_path_offset(fdtp, "/chosen");
	if (offset > 0) {
		const char *chosenprops[] = {"stdout", "stdin", "mmu", "cpu",
		    NULL};
		const uint32_t *ihand;
		for (i = 0; chosenprops[i] != NULL; i++) {
			ihand = fdt_getprop(fdtp, offset, chosenprops[i], &len);
			if (ihand != NULL && len == sizeof(*ihand)) {
				node = OF_instance_to_package(
				    fdt32_to_cpu(*ihand));
				if (OF_hasprop(node, "phandle"))
					OF_getprop(node, "phandle", &node,
					    sizeof(node));
				else if (OF_hasprop(node, "linux,phandle"))
					OF_getprop(node, "linux,phandle", &node,
					    sizeof(node));
				else if (OF_hasprop(node, "ibm,phandle"))
					OF_getprop(node, "ibm,phandle", &node,
					    sizeof(node));
				node = cpu_to_fdt32(node);
				fdt_setprop(fdtp, offset, chosenprops[i], &node,
				    sizeof(node));
			}

			/* Refind node in case it moved */
			offset = fdt_path_offset(fdtp, "/chosen");
		}
	}
}

int
fdt_platform_load_dtb(void)
{
        void *buffer;
        size_t buflen = 409600;

        buffer = malloc(buflen);
        fdt_create_empty_tree(buffer, buflen);
        add_node_to_fdt(buffer, OF_peer(0), fdt_path_offset(buffer, "/"));
        ofwfdt_fixups(buffer);
        fdt_pack(buffer);

        fdt_load_dtb_addr(buffer);
        free(buffer);

        return (0);
}

void
fdt_platform_load_overlays(void)
{

}

void
fdt_platform_fixups(void)
{

}

static int
command_fdt(int argc, char *argv[])
{
 
	return (command_fdt_internal(argc, argv));
}
 
COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt);