root / device_tree.c @ 927e3a4e
History | View | Annotate | Download (2.7 kB)
1 | f652e6af | aurel32 | /*
|
---|---|---|---|
2 | f652e6af | aurel32 | * Functions to help device tree manipulation using libfdt.
|
3 | f652e6af | aurel32 | * It also provides functions to read entries from device tree proc
|
4 | f652e6af | aurel32 | * interface.
|
5 | f652e6af | aurel32 | *
|
6 | f652e6af | aurel32 | * Copyright 2008 IBM Corporation.
|
7 | f652e6af | aurel32 | * Authors: Jerone Young <jyoung5@us.ibm.com>
|
8 | f652e6af | aurel32 | * Hollis Blanchard <hollisb@us.ibm.com>
|
9 | f652e6af | aurel32 | *
|
10 | f652e6af | aurel32 | * This work is licensed under the GNU GPL license version 2 or later.
|
11 | f652e6af | aurel32 | *
|
12 | f652e6af | aurel32 | */
|
13 | f652e6af | aurel32 | |
14 | f652e6af | aurel32 | #include <stdio.h> |
15 | f652e6af | aurel32 | #include <sys/types.h> |
16 | f652e6af | aurel32 | #include <sys/stat.h> |
17 | f652e6af | aurel32 | #include <fcntl.h> |
18 | f652e6af | aurel32 | #include <unistd.h> |
19 | f652e6af | aurel32 | #include <stdlib.h> |
20 | f652e6af | aurel32 | |
21 | f652e6af | aurel32 | #include "config.h" |
22 | f652e6af | aurel32 | #include "qemu-common.h" |
23 | f652e6af | aurel32 | #include "sysemu.h" |
24 | f652e6af | aurel32 | #include "device_tree.h" |
25 | f652e6af | aurel32 | |
26 | f652e6af | aurel32 | #include <libfdt.h> |
27 | f652e6af | aurel32 | |
28 | f652e6af | aurel32 | void *load_device_tree(const char *filename_path, void *load_addr) |
29 | f652e6af | aurel32 | { |
30 | f652e6af | aurel32 | int dt_file_size;
|
31 | f652e6af | aurel32 | int dt_file_load_size;
|
32 | f652e6af | aurel32 | int new_dt_size;
|
33 | f652e6af | aurel32 | int ret;
|
34 | f652e6af | aurel32 | void *dt_file = NULL; |
35 | f652e6af | aurel32 | void *fdt;
|
36 | f652e6af | aurel32 | |
37 | f652e6af | aurel32 | dt_file_size = get_image_size(filename_path); |
38 | f652e6af | aurel32 | if (dt_file_size < 0) { |
39 | f652e6af | aurel32 | printf("Unable to get size of device tree file '%s'\n",
|
40 | f652e6af | aurel32 | filename_path); |
41 | f652e6af | aurel32 | goto fail;
|
42 | f652e6af | aurel32 | } |
43 | f652e6af | aurel32 | |
44 | f652e6af | aurel32 | /* First allocate space in qemu for device tree */
|
45 | f652e6af | aurel32 | dt_file = qemu_mallocz(dt_file_size); |
46 | f652e6af | aurel32 | |
47 | f652e6af | aurel32 | dt_file_load_size = load_image(filename_path, dt_file); |
48 | f652e6af | aurel32 | |
49 | f652e6af | aurel32 | /* Second we place new copy of 2x size in guest memory
|
50 | f652e6af | aurel32 | * This give us enough room for manipulation.
|
51 | f652e6af | aurel32 | */
|
52 | f652e6af | aurel32 | new_dt_size = dt_file_size * 2;
|
53 | f652e6af | aurel32 | |
54 | f652e6af | aurel32 | fdt = load_addr; |
55 | f652e6af | aurel32 | ret = fdt_open_into(dt_file, fdt, new_dt_size); |
56 | f652e6af | aurel32 | if (ret) {
|
57 | f652e6af | aurel32 | printf("Unable to copy device tree in memory\n");
|
58 | f652e6af | aurel32 | goto fail;
|
59 | f652e6af | aurel32 | } |
60 | f652e6af | aurel32 | |
61 | f652e6af | aurel32 | /* Check sanity of device tree */
|
62 | f652e6af | aurel32 | if (fdt_check_header(fdt)) {
|
63 | f652e6af | aurel32 | printf ("Device tree file loaded into memory is invalid: %s\n",
|
64 | f652e6af | aurel32 | filename_path); |
65 | f652e6af | aurel32 | goto fail;
|
66 | f652e6af | aurel32 | } |
67 | f652e6af | aurel32 | /* free qemu memory with old device tree */
|
68 | f652e6af | aurel32 | qemu_free(dt_file); |
69 | f652e6af | aurel32 | return fdt;
|
70 | f652e6af | aurel32 | |
71 | f652e6af | aurel32 | fail:
|
72 | f652e6af | aurel32 | qemu_free(dt_file); |
73 | f652e6af | aurel32 | return NULL; |
74 | f652e6af | aurel32 | } |
75 | f652e6af | aurel32 | |
76 | f652e6af | aurel32 | int qemu_devtree_setprop(void *fdt, const char *node_path, |
77 | f652e6af | aurel32 | const char *property, uint32_t *val_array, int size) |
78 | f652e6af | aurel32 | { |
79 | f652e6af | aurel32 | int offset;
|
80 | f652e6af | aurel32 | |
81 | f652e6af | aurel32 | offset = fdt_path_offset(fdt, node_path); |
82 | f652e6af | aurel32 | if (offset < 0) |
83 | f652e6af | aurel32 | return offset;
|
84 | f652e6af | aurel32 | |
85 | f652e6af | aurel32 | return fdt_setprop(fdt, offset, property, val_array, size);
|
86 | f652e6af | aurel32 | } |
87 | f652e6af | aurel32 | |
88 | f652e6af | aurel32 | int qemu_devtree_setprop_cell(void *fdt, const char *node_path, |
89 | f652e6af | aurel32 | const char *property, uint32_t val) |
90 | f652e6af | aurel32 | { |
91 | f652e6af | aurel32 | int offset;
|
92 | f652e6af | aurel32 | |
93 | f652e6af | aurel32 | offset = fdt_path_offset(fdt, node_path); |
94 | f652e6af | aurel32 | if (offset < 0) |
95 | f652e6af | aurel32 | return offset;
|
96 | f652e6af | aurel32 | |
97 | f652e6af | aurel32 | return fdt_setprop_cell(fdt, offset, property, val);
|
98 | f652e6af | aurel32 | } |
99 | f652e6af | aurel32 | |
100 | f652e6af | aurel32 | int qemu_devtree_setprop_string(void *fdt, const char *node_path, |
101 | f652e6af | aurel32 | const char *property, const char *string) |
102 | f652e6af | aurel32 | { |
103 | f652e6af | aurel32 | int offset;
|
104 | f652e6af | aurel32 | |
105 | f652e6af | aurel32 | offset = fdt_path_offset(fdt, node_path); |
106 | f652e6af | aurel32 | if (offset < 0) |
107 | f652e6af | aurel32 | return offset;
|
108 | f652e6af | aurel32 | |
109 | f652e6af | aurel32 | return fdt_setprop_string(fdt, offset, property, string);
|
110 | f652e6af | aurel32 | } |