root / device_tree.c @ a8a358bf
History | View | Annotate | Download (2.6 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 | 7ec632b4 | pbrook | void *load_device_tree(const char *filename_path, int *sizep) |
29 | f652e6af | aurel32 | { |
30 | 7ec632b4 | pbrook | int dt_size;
|
31 | f652e6af | aurel32 | int dt_file_load_size;
|
32 | f652e6af | aurel32 | int ret;
|
33 | 7ec632b4 | pbrook | void *fdt = NULL; |
34 | f652e6af | aurel32 | |
35 | 7ec632b4 | pbrook | *sizep = 0;
|
36 | 7ec632b4 | pbrook | dt_size = get_image_size(filename_path); |
37 | 7ec632b4 | pbrook | if (dt_size < 0) { |
38 | f652e6af | aurel32 | printf("Unable to get size of device tree file '%s'\n",
|
39 | f652e6af | aurel32 | filename_path); |
40 | f652e6af | aurel32 | goto fail;
|
41 | f652e6af | aurel32 | } |
42 | f652e6af | aurel32 | |
43 | 7ec632b4 | pbrook | /* Expand to 2x size to give enough room for manipulation. */
|
44 | 7ec632b4 | pbrook | dt_size *= 2;
|
45 | f652e6af | aurel32 | /* First allocate space in qemu for device tree */
|
46 | 7ec632b4 | pbrook | fdt = qemu_mallocz(dt_size); |
47 | f652e6af | aurel32 | |
48 | 7ec632b4 | pbrook | dt_file_load_size = load_image(filename_path, fdt); |
49 | 7ec632b4 | pbrook | if (dt_file_load_size < 0) { |
50 | 7ec632b4 | pbrook | printf("Unable to open device tree file '%s'\n",
|
51 | 7ec632b4 | pbrook | filename_path); |
52 | 7ec632b4 | pbrook | goto fail;
|
53 | 7ec632b4 | pbrook | } |
54 | f652e6af | aurel32 | |
55 | 7ec632b4 | pbrook | ret = fdt_open_into(fdt, fdt, 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 | 7ec632b4 | pbrook | *sizep = dt_size; |
68 | f652e6af | aurel32 | return fdt;
|
69 | f652e6af | aurel32 | |
70 | f652e6af | aurel32 | fail:
|
71 | 7ec632b4 | pbrook | qemu_free(fdt); |
72 | f652e6af | aurel32 | return NULL; |
73 | f652e6af | aurel32 | } |
74 | f652e6af | aurel32 | |
75 | f652e6af | aurel32 | int qemu_devtree_setprop(void *fdt, const char *node_path, |
76 | f652e6af | aurel32 | const char *property, uint32_t *val_array, int size) |
77 | f652e6af | aurel32 | { |
78 | f652e6af | aurel32 | int offset;
|
79 | f652e6af | aurel32 | |
80 | f652e6af | aurel32 | offset = fdt_path_offset(fdt, node_path); |
81 | f652e6af | aurel32 | if (offset < 0) |
82 | f652e6af | aurel32 | return offset;
|
83 | f652e6af | aurel32 | |
84 | f652e6af | aurel32 | return fdt_setprop(fdt, offset, property, val_array, size);
|
85 | f652e6af | aurel32 | } |
86 | f652e6af | aurel32 | |
87 | f652e6af | aurel32 | int qemu_devtree_setprop_cell(void *fdt, const char *node_path, |
88 | f652e6af | aurel32 | const char *property, uint32_t val) |
89 | f652e6af | aurel32 | { |
90 | f652e6af | aurel32 | int offset;
|
91 | f652e6af | aurel32 | |
92 | f652e6af | aurel32 | offset = fdt_path_offset(fdt, node_path); |
93 | f652e6af | aurel32 | if (offset < 0) |
94 | f652e6af | aurel32 | return offset;
|
95 | f652e6af | aurel32 | |
96 | f652e6af | aurel32 | return fdt_setprop_cell(fdt, offset, property, val);
|
97 | f652e6af | aurel32 | } |
98 | f652e6af | aurel32 | |
99 | f652e6af | aurel32 | int qemu_devtree_setprop_string(void *fdt, const char *node_path, |
100 | f652e6af | aurel32 | const char *property, const char *string) |
101 | f652e6af | aurel32 | { |
102 | f652e6af | aurel32 | int offset;
|
103 | f652e6af | aurel32 | |
104 | f652e6af | aurel32 | offset = fdt_path_offset(fdt, node_path); |
105 | f652e6af | aurel32 | if (offset < 0) |
106 | f652e6af | aurel32 | return offset;
|
107 | f652e6af | aurel32 | |
108 | f652e6af | aurel32 | return fdt_setprop_string(fdt, offset, property, string);
|
109 | f652e6af | aurel32 | } |