blob: 426a63155cf3fa7706367bcd9ed540d0aac8d8f1 [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"
23#include "sysemu.h"
24#include "device_tree.h"
Blue Swirl39b7f202009-09-22 17:51:36 +000025#include "hw/loader.h"
aurel32f652e6a2008-12-16 10:43:48 +000026
27#include <libfdt.h>
28
pbrook7ec632b2009-04-10 16:23:59 +000029void *load_device_tree(const char *filename_path, int *sizep)
aurel32f652e6a2008-12-16 10:43:48 +000030{
pbrook7ec632b2009-04-10 16:23:59 +000031 int dt_size;
aurel32f652e6a2008-12-16 10:43:48 +000032 int dt_file_load_size;
aurel32f652e6a2008-12-16 10:43:48 +000033 int ret;
pbrook7ec632b2009-04-10 16:23:59 +000034 void *fdt = NULL;
aurel32f652e6a2008-12-16 10:43:48 +000035
pbrook7ec632b2009-04-10 16:23:59 +000036 *sizep = 0;
37 dt_size = get_image_size(filename_path);
38 if (dt_size < 0) {
aurel32f652e6a2008-12-16 10:43:48 +000039 printf("Unable to get size of device tree file '%s'\n",
40 filename_path);
41 goto fail;
42 }
43
pbrook7ec632b2009-04-10 16:23:59 +000044 /* Expand to 2x size to give enough room for manipulation. */
45 dt_size *= 2;
aurel32f652e6a2008-12-16 10:43:48 +000046 /* First allocate space in qemu for device tree */
pbrook7ec632b2009-04-10 16:23:59 +000047 fdt = qemu_mallocz(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:
pbrook7ec632b2009-04-10 16:23:59 +000072 qemu_free(fdt);
aurel32f652e6a2008-12-16 10:43:48 +000073 return NULL;
74}
75
76int qemu_devtree_setprop(void *fdt, const char *node_path,
77 const char *property, uint32_t *val_array, int size)
78{
79 int offset;
80
81 offset = fdt_path_offset(fdt, node_path);
82 if (offset < 0)
83 return offset;
84
85 return fdt_setprop(fdt, offset, property, val_array, size);
86}
87
88int qemu_devtree_setprop_cell(void *fdt, const char *node_path,
89 const char *property, uint32_t val)
90{
91 int offset;
92
93 offset = fdt_path_offset(fdt, node_path);
94 if (offset < 0)
95 return offset;
96
97 return fdt_setprop_cell(fdt, offset, property, val);
98}
99
100int qemu_devtree_setprop_string(void *fdt, const char *node_path,
101 const char *property, const char *string)
102{
103 int offset;
104
105 offset = fdt_path_offset(fdt, node_path);
106 if (offset < 0)
107 return offset;
108
109 return fdt_setprop_string(fdt, offset, property, string);
110}