add a simple fork test
[qemu/linux-user.git] / device_tree.c
blob22386829deb6adf9ffa57f4cd865a4028b5e7875
1 /*
2 * Functions to help device tree manipulation using libfdt.
3 * It also provides functions to read entries from device tree proc
4 * interface.
6 * Copyright 2008 IBM Corporation.
7 * Authors: Jerone Young <jyoung5@us.ibm.com>
8 * Hollis Blanchard <hollisb@us.ibm.com>
10 * This work is licensed under the GNU GPL license version 2 or later.
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>
21 #include "config.h"
22 #include "qemu-common.h"
23 #include "sysemu.h"
24 #include "device_tree.h"
26 #include <libfdt.h>
28 void *load_device_tree(const char *filename_path, void *load_addr)
30 int dt_file_size;
31 int dt_file_load_size;
32 int new_dt_size;
33 int ret;
34 void *dt_file = NULL;
35 void *fdt;
37 dt_file_size = get_image_size(filename_path);
38 if (dt_file_size < 0) {
39 printf("Unable to get size of device tree file '%s'\n",
40 filename_path);
41 goto fail;
44 /* First allocate space in qemu for device tree */
45 dt_file = qemu_mallocz(dt_file_size);
46 if (dt_file == NULL) {
47 printf("Unable to allocate memory in qemu for device tree\n");
48 goto fail;
51 dt_file_load_size = load_image(filename_path, dt_file);
53 /* Second we place new copy of 2x size in guest memory
54 * This give us enough room for manipulation.
56 new_dt_size = dt_file_size * 2;
58 fdt = load_addr;
59 ret = fdt_open_into(dt_file, fdt, new_dt_size);
60 if (ret) {
61 printf("Unable to copy device tree in memory\n");
62 goto fail;
65 /* Check sanity of device tree */
66 if (fdt_check_header(fdt)) {
67 printf ("Device tree file loaded into memory is invalid: %s\n",
68 filename_path);
69 goto fail;
71 /* free qemu memory with old device tree */
72 qemu_free(dt_file);
73 return fdt;
75 fail:
76 qemu_free(dt_file);
77 return NULL;
80 int qemu_devtree_setprop(void *fdt, const char *node_path,
81 const char *property, uint32_t *val_array, int size)
83 int offset;
85 offset = fdt_path_offset(fdt, node_path);
86 if (offset < 0)
87 return offset;
89 return fdt_setprop(fdt, offset, property, val_array, size);
92 int qemu_devtree_setprop_cell(void *fdt, const char *node_path,
93 const char *property, uint32_t val)
95 int offset;
97 offset = fdt_path_offset(fdt, node_path);
98 if (offset < 0)
99 return offset;
101 return fdt_setprop_cell(fdt, offset, property, val);
104 int qemu_devtree_setprop_string(void *fdt, const char *node_path,
105 const char *property, const char *string)
107 int offset;
109 offset = fdt_path_offset(fdt, node_path);
110 if (offset < 0)
111 return offset;
113 return fdt_setprop_string(fdt, offset, property, string);