Merge tag 'v3.10.13' into lsk/v3.10/topic/kvm
[firefly-linux-kernel-4.4.55.git] / arch / arm / boot / compressed / atags_to_fdt.c
1 #include <asm/setup.h>
2 #include <libfdt.h>
3
4 #if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_EXTEND)
5 #define do_extend_cmdline 1
6 #else
7 #define do_extend_cmdline 0
8 #endif
9
10 static int node_offset(void *fdt, const char *node_path)
11 {
12         int offset = fdt_path_offset(fdt, node_path);
13         if (offset == -FDT_ERR_NOTFOUND)
14                 offset = fdt_add_subnode(fdt, 0, node_path);
15         return offset;
16 }
17
18 static int setprop(void *fdt, const char *node_path, const char *property,
19                    uint32_t *val_array, int size)
20 {
21         int offset = node_offset(fdt, node_path);
22         if (offset < 0)
23                 return offset;
24         return fdt_setprop(fdt, offset, property, val_array, size);
25 }
26
27 static int setprop_string(void *fdt, const char *node_path,
28                           const char *property, const char *string)
29 {
30         int offset = node_offset(fdt, node_path);
31         if (offset < 0)
32                 return offset;
33         return fdt_setprop_string(fdt, offset, property, string);
34 }
35
36 static int setprop_cell(void *fdt, const char *node_path,
37                         const char *property, uint32_t val)
38 {
39         int offset = node_offset(fdt, node_path);
40         if (offset < 0)
41                 return offset;
42         return fdt_setprop_cell(fdt, offset, property, val);
43 }
44
45 static const void *getprop(const void *fdt, const char *node_path,
46                            const char *property, int *len)
47 {
48         int offset = fdt_path_offset(fdt, node_path);
49
50         if (offset == -FDT_ERR_NOTFOUND)
51                 return NULL;
52
53         return fdt_getprop(fdt, offset, property, len);
54 }
55
56 static uint32_t get_cell_size(const void *fdt)
57 {
58         int len;
59         uint32_t cell_size = 1;
60         const uint32_t *size_len =  getprop(fdt, "/", "#size-cells", &len);
61
62         if (size_len)
63                 cell_size = fdt32_to_cpu(*size_len);
64         return cell_size;
65 }
66
67 static void merge_fdt_bootargs(void *fdt, const char *fdt_cmdline)
68 {
69         char cmdline[COMMAND_LINE_SIZE];
70         const char *fdt_bootargs;
71         char *ptr = cmdline;
72         int len = 0;
73
74         /* copy the fdt command line into the buffer */
75         fdt_bootargs = getprop(fdt, "/chosen", "bootargs", &len);
76         if (fdt_bootargs)
77                 if (len < COMMAND_LINE_SIZE) {
78                         memcpy(ptr, fdt_bootargs, len);
79                         /* len is the length of the string
80                          * including the NULL terminator */
81                         ptr += len - 1;
82                 }
83
84         /* and append the ATAG_CMDLINE */
85         if (fdt_cmdline) {
86                 len = strlen(fdt_cmdline);
87                 if (ptr - cmdline + len + 2 < COMMAND_LINE_SIZE) {
88                         *ptr++ = ' ';
89                         memcpy(ptr, fdt_cmdline, len);
90                         ptr += len;
91                 }
92         }
93         *ptr = '\0';
94
95         setprop_string(fdt, "/chosen", "bootargs", cmdline);
96 }
97
98 /*
99  * Convert and fold provided ATAGs into the provided FDT.
100  *
101  * REturn values:
102  *    = 0 -> pretend success
103  *    = 1 -> bad ATAG (may retry with another possible ATAG pointer)
104  *    < 0 -> error from libfdt
105  */
106 int atags_to_fdt(void *atag_list, void *fdt, int total_space)
107 {
108         struct tag *atag = atag_list;
109         /* In the case of 64 bits memory size, need to reserve 2 cells for
110          * address and size for each bank */
111         uint32_t mem_reg_property[2 * 2 * NR_BANKS];
112         int memcount = 0;
113         int ret, memsize;
114
115         /* make sure we've got an aligned pointer */
116         if ((u32)atag_list & 0x3)
117                 return 1;
118
119         /* if we get a DTB here we're done already */
120         if (*(u32 *)atag_list == fdt32_to_cpu(FDT_MAGIC))
121                return 0;
122
123         /* validate the ATAG */
124         if (atag->hdr.tag != ATAG_CORE ||
125             (atag->hdr.size != tag_size(tag_core) &&
126              atag->hdr.size != 2))
127                 return 1;
128
129         /* let's give it all the room it could need */
130         ret = fdt_open_into(fdt, fdt, total_space);
131         if (ret < 0)
132                 return ret;
133
134         for_each_tag(atag, atag_list) {
135                 if (atag->hdr.tag == ATAG_CMDLINE) {
136                         /* Append the ATAGS command line to the device tree
137                          * command line.
138                          * NB: This means that if the same parameter is set in
139                          * the device tree and in the tags, the one from the
140                          * tags will be chosen.
141                          */
142                         if (do_extend_cmdline)
143                                 merge_fdt_bootargs(fdt,
144                                                    atag->u.cmdline.cmdline);
145                         else
146                                 setprop_string(fdt, "/chosen", "bootargs",
147                                                atag->u.cmdline.cmdline);
148                 } else if (atag->hdr.tag == ATAG_MEM) {
149                         if (memcount >= sizeof(mem_reg_property)/4)
150                                 continue;
151                         if (!atag->u.mem.size)
152                                 continue;
153                         memsize = get_cell_size(fdt);
154
155                         if (memsize == 2) {
156                                 /* if memsize is 2, that means that
157                                  * each data needs 2 cells of 32 bits,
158                                  * so the data are 64 bits */
159                                 uint64_t *mem_reg_prop64 =
160                                         (uint64_t *)mem_reg_property;
161                                 mem_reg_prop64[memcount++] =
162                                         cpu_to_fdt64(atag->u.mem.start);
163                                 mem_reg_prop64[memcount++] =
164                                         cpu_to_fdt64(atag->u.mem.size);
165                         } else {
166                                 mem_reg_property[memcount++] =
167                                         cpu_to_fdt32(atag->u.mem.start);
168                                 mem_reg_property[memcount++] =
169                                         cpu_to_fdt32(atag->u.mem.size);
170                         }
171
172                 } else if (atag->hdr.tag == ATAG_INITRD2) {
173                         uint32_t initrd_start, initrd_size;
174                         initrd_start = atag->u.initrd.start;
175                         initrd_size = atag->u.initrd.size;
176                         setprop_cell(fdt, "/chosen", "linux,initrd-start",
177                                         initrd_start);
178                         setprop_cell(fdt, "/chosen", "linux,initrd-end",
179                                         initrd_start + initrd_size);
180                 }
181         }
182
183         if (memcount) {
184                 setprop(fdt, "/memory", "reg", mem_reg_property,
185                         4 * memcount * memsize);
186         }
187
188         return fdt_pack(fdt);
189 }