blob: dc69232f10da4be242a97f13d1d0e6422dc12c6e [file] [log] [blame]
aurel32f652e6a2008-12-16 10:43:48 +00001/*
2 * Functions to help device tree manipulation using libfdt.
3 * It also provides functions to read entries from device tree proc
4 * interface.
5 *
6 * Copyright 2008 IBM Corporation.
7 * Authors: Jerone Young <jyoung5@us.ibm.com>
8 * Hollis Blanchard <hollisb@us.ibm.com>
9 *
10 * This work is licensed under the GNU GPL license version 2 or later.
11 *
12 */
13
14#include <stdio.h>
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <fcntl.h>
18#include <unistd.h>
19#include <stdlib.h>
20
21#include "config.h"
22#include "qemu-common.h"
aurel32f652e6a2008-12-16 10:43:48 +000023#include "device_tree.h"
Blue Swirl39b7f202009-09-22 17:51:36 +000024#include "hw/loader.h"
aurel32f652e6a2008-12-16 10:43:48 +000025
26#include <libfdt.h>
27
pbrook7ec632b2009-04-10 16:23:59 +000028void *load_device_tree(const char *filename_path, int *sizep)
aurel32f652e6a2008-12-16 10:43:48 +000029{
pbrook7ec632b2009-04-10 16:23:59 +000030 int dt_size;
aurel32f652e6a2008-12-16 10:43:48 +000031 int dt_file_load_size;
aurel32f652e6a2008-12-16 10:43:48 +000032 int ret;
pbrook7ec632b2009-04-10 16:23:59 +000033 void *fdt = NULL;
aurel32f652e6a2008-12-16 10:43:48 +000034
pbrook7ec632b2009-04-10 16:23:59 +000035 *sizep = 0;
36 dt_size = get_image_size(filename_path);
37 if (dt_size < 0) {
aurel32f652e6a2008-12-16 10:43:48 +000038 printf("Unable to get size of device tree file '%s'\n",
39 filename_path);
40 goto fail;
41 }
42
pbrook7ec632b2009-04-10 16:23:59 +000043 /* Expand to 2x size to give enough room for manipulation. */
Alexander Grafded57c52011-07-23 10:54:11 +020044 dt_size += 10000;
pbrook7ec632b2009-04-10 16:23:59 +000045 dt_size *= 2;
aurel32f652e6a2008-12-16 10:43:48 +000046 /* First allocate space in qemu for device tree */
Anthony Liguori7267c092011-08-20 22:09:37 -050047 fdt = g_malloc0(dt_size);
aurel32f652e6a2008-12-16 10:43:48 +000048
pbrook7ec632b2009-04-10 16:23:59 +000049 dt_file_load_size = load_image(filename_path, fdt);
50 if (dt_file_load_size < 0) {
51 printf("Unable to open device tree file '%s'\n",
52 filename_path);
53 goto fail;
54 }
aurel32f652e6a2008-12-16 10:43:48 +000055
pbrook7ec632b2009-04-10 16:23:59 +000056 ret = fdt_open_into(fdt, fdt, dt_size);
aurel32f652e6a2008-12-16 10:43:48 +000057 if (ret) {
58 printf("Unable to copy device tree in memory\n");
59 goto fail;
60 }
61
62 /* Check sanity of device tree */
63 if (fdt_check_header(fdt)) {
64 printf ("Device tree file loaded into memory is invalid: %s\n",
65 filename_path);
66 goto fail;
67 }
pbrook7ec632b2009-04-10 16:23:59 +000068 *sizep = dt_size;
aurel32f652e6a2008-12-16 10:43:48 +000069 return fdt;
70
71fail:
Anthony Liguori7267c092011-08-20 22:09:37 -050072 g_free(fdt);
aurel32f652e6a2008-12-16 10:43:48 +000073 return NULL;
74}
75
Alexander Grafccbcfed2011-07-23 10:52:00 +020076static int findnode_nofail(void *fdt, const char *node_path)
aurel32f652e6a2008-12-16 10:43:48 +000077{
78 int offset;
79
80 offset = fdt_path_offset(fdt, node_path);
Alexander Grafccbcfed2011-07-23 10:52:00 +020081 if (offset < 0) {
82 fprintf(stderr, "%s Couldn't find node %s: %s\n", __func__, node_path,
83 fdt_strerror(offset));
84 exit(1);
85 }
aurel32f652e6a2008-12-16 10:43:48 +000086
Alexander Grafccbcfed2011-07-23 10:52:00 +020087 return offset;
88}
89
90int qemu_devtree_setprop(void *fdt, const char *node_path,
91 const char *property, void *val_array, int size)
92{
93 int r;
94
95 r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val_array, size);
96 if (r < 0) {
97 fprintf(stderr, "%s: Couldn't set %s/%s: %s\n", __func__, node_path,
98 property, fdt_strerror(r));
99 exit(1);
100 }
101
102 return r;
aurel32f652e6a2008-12-16 10:43:48 +0000103}
104
105int qemu_devtree_setprop_cell(void *fdt, const char *node_path,
106 const char *property, uint32_t val)
107{
Alexander Grafccbcfed2011-07-23 10:52:00 +0200108 int r;
aurel32f652e6a2008-12-16 10:43:48 +0000109
Alexander Grafccbcfed2011-07-23 10:52:00 +0200110 r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val);
111 if (r < 0) {
112 fprintf(stderr, "%s: Couldn't set %s/%s = %#08x: %s\n", __func__,
113 node_path, property, val, fdt_strerror(r));
114 exit(1);
115 }
aurel32f652e6a2008-12-16 10:43:48 +0000116
Alexander Grafccbcfed2011-07-23 10:52:00 +0200117 return r;
aurel32f652e6a2008-12-16 10:43:48 +0000118}
119
120int qemu_devtree_setprop_string(void *fdt, const char *node_path,
121 const char *property, const char *string)
122{
Alexander Grafccbcfed2011-07-23 10:52:00 +0200123 int r;
aurel32f652e6a2008-12-16 10:43:48 +0000124
Alexander Grafccbcfed2011-07-23 10:52:00 +0200125 r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string);
126 if (r < 0) {
127 fprintf(stderr, "%s: Couldn't set %s/%s = %s: %s\n", __func__,
128 node_path, property, string, fdt_strerror(r));
129 exit(1);
130 }
aurel32f652e6a2008-12-16 10:43:48 +0000131
Alexander Grafccbcfed2011-07-23 10:52:00 +0200132 return r;
aurel32f652e6a2008-12-16 10:43:48 +0000133}
Alexander Grafd69a8e62011-07-21 01:52:57 +0200134
135int qemu_devtree_nop_node(void *fdt, const char *node_path)
136{
Alexander Grafccbcfed2011-07-23 10:52:00 +0200137 int r;
Alexander Grafd69a8e62011-07-21 01:52:57 +0200138
Alexander Grafccbcfed2011-07-23 10:52:00 +0200139 r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path));
140 if (r < 0) {
141 fprintf(stderr, "%s: Couldn't nop node %s: %s\n", __func__, node_path,
142 fdt_strerror(r));
143 exit(1);
144 }
Alexander Grafd69a8e62011-07-21 01:52:57 +0200145
Alexander Grafccbcfed2011-07-23 10:52:00 +0200146 return r;
Alexander Grafd69a8e62011-07-21 01:52:57 +0200147}
Alexander Graf80ad7812011-07-22 13:55:37 +0200148
149int qemu_devtree_add_subnode(void *fdt, const char *name)
150{
Alexander Graf80ad7812011-07-22 13:55:37 +0200151 char *dupname = g_strdup(name);
152 char *basename = strrchr(dupname, '/');
153 int retval;
154
155 if (!basename) {
156 return -1;
157 }
158
159 basename[0] = '\0';
160 basename++;
161
Alexander Grafccbcfed2011-07-23 10:52:00 +0200162 retval = fdt_add_subnode(fdt, findnode_nofail(fdt, dupname), basename);
163 if (retval < 0) {
164 fprintf(stderr, "FDT: Failed to create subnode %s: %s\n", name,
165 fdt_strerror(retval));
166 exit(1);
Alexander Graf80ad7812011-07-22 13:55:37 +0200167 }
168
Alexander Graf80ad7812011-07-22 13:55:37 +0200169 g_free(dupname);
170 return retval;
171}