1 /*-
2  * Copyright (C) 2014 Nathan Whitehorn
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <sys/types.h>
27 #include <fdt_platform.h>
28 #include <libfdt.h>
29 #include "kboot.h"
30 
31 /* Fix up wrong values added to the device tree by prom_init() in Linux */
32 
33 void
fdt_arch_fixups(void * fdtp)34 fdt_arch_fixups(void *fdtp)
35 {
36 	int offset, len;
37 	const void *prop;
38 
39 	/*
40 	 * Remove /memory/available properties, which reflect long-gone OF
41 	 * state
42 	 */
43 
44 	offset = fdt_path_offset(fdtp, "/memory@0");
45 	if (offset > 0)
46 		fdt_delprop(fdtp, offset, "available");
47 
48 	/*
49 	 * Add reservations for OPAL and RTAS state if present
50 	 */
51 
52 	offset = fdt_path_offset(fdtp, "/ibm,opal");
53 	if (offset > 0) {
54 		const uint64_t *base, *size;
55 		base = fdt_getprop(fdtp, offset, "opal-base-address",
56 		    &len);
57 		size = fdt_getprop(fdtp, offset, "opal-runtime-size",
58 		    &len);
59 		if (base != NULL && size != NULL)
60 			fdt_add_mem_rsv(fdtp, fdt64_to_cpu(*base),
61 			    fdt64_to_cpu(*size));
62 	}
63 	offset = fdt_path_offset(fdtp, "/rtas");
64 	if (offset > 0) {
65 		const uint32_t *base, *size;
66 		base = fdt_getprop(fdtp, offset, "linux,rtas-base", &len);
67 		size = fdt_getprop(fdtp, offset, "rtas-size", &len);
68 		if (base != NULL && size != NULL)
69 			fdt_add_mem_rsv(fdtp, fdt32_to_cpu(*base),
70 			    fdt32_to_cpu(*size));
71 	}
72 
73 	/*
74 	 * Patch up /chosen nodes so that the stored handles mean something,
75 	 * where possible.
76 	 */
77 	offset = fdt_path_offset(fdtp, "/chosen");
78 	if (offset > 0) {
79 		fdt_delprop(fdtp, offset, "cpu"); /* This node not meaningful */
80 
81 		offset = fdt_path_offset(fdtp, "/chosen");
82 		prop = fdt_getprop(fdtp, offset, "linux,stdout-package", &len);
83 		if (prop != NULL) {
84 			fdt_setprop(fdtp, offset, "stdout", prop, len);
85 			offset = fdt_path_offset(fdtp, "/chosen");
86 			fdt_setprop(fdtp, offset, "stdin", prop, len);
87 		}
88 	}
89 }
90