blob: 58c9ffd70cf5cdbf18afc19bcf1b33725b2f22a7 [file] [log] [blame]
Mayank Grover2676fa42019-04-16 18:43:03 +05301/* Copyright (c) 2015-2019, The Linux Foundation. All rights reserved.
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08002 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
12 * * Neither the name of The Linux Foundation nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28
29/* Supporting function of UpdateDeviceTree()
30 * Function adds memory map entries to the device tree binary
Jeevan Shriram17f173d2017-10-24 22:11:07 -070031 * dev_tree_add_mem_info() is called at every time when memory type matches
32 * conditions */
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080033
34#include "UpdateDeviceTree.h"
lijuangf0bbcad2017-08-16 16:59:18 +080035#include "AutoGen.h"
Jeevan Shriram17f173d2017-10-24 22:11:07 -070036#include <Library/UpdateDeviceTree.h>
Mayank Grover2676fa42019-04-16 18:43:03 +053037#include <Library/LocateDeviceTree.h>
Mayank Grover546377b2019-04-12 15:52:10 +053038#include <Library/BootLinux.h>
Jeevan Shriram17f173d2017-10-24 22:11:07 -070039#include <Protocol/EFIChipInfoTypes.h>
Jeevan Shriramf8fa2912018-11-02 13:16:47 -070040#include <Protocol/EFIDDRGetConfig.h>
Jeevan Shriram17f173d2017-10-24 22:11:07 -070041#include <Protocol/EFIRng.h>
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080042
Jeevan Shriram17f173d2017-10-24 22:11:07 -070043#define NUM_SPLASHMEM_PROP_ELEM 4
Mayank Groverabfc3532018-05-03 14:52:32 +053044#define DEFAULT_CELL_SIZE 2
Aravind Venkateswaranc0048032016-07-22 14:28:03 -070045
Jeevan Shriram17f173d2017-10-24 22:11:07 -070046STATIC struct FstabNode FstabTable = {"/firmware/android/fstab", "dev",
47 "/soc/"};
Mayank Grover546377b2019-04-12 15:52:10 +053048STATIC struct FstabNode DynamicFstabTable = {"/firmware/android/fstab",
49 "status",
50 ""};
Aravind Venkateswaranc0048032016-07-22 14:28:03 -070051STATIC struct DisplaySplashBufferInfo splashBuf;
Jeevan Shriram17f173d2017-10-24 22:11:07 -070052STATIC UINTN splashBufSize = sizeof (splashBuf);
Aravind Venkateswaranc0048032016-07-22 14:28:03 -070053
Jeevan Shriram17f173d2017-10-24 22:11:07 -070054STATIC VOID
55PrintSplashMemInfo (CONST CHAR8 *data, INT32 datalen)
Aravind Venkateswaranc0048032016-07-22 14:28:03 -070056{
Jeevan Shriram17f173d2017-10-24 22:11:07 -070057 UINT32 i, val[NUM_SPLASHMEM_PROP_ELEM] = {0};
Aravind Venkateswaranc0048032016-07-22 14:28:03 -070058
Jeevan Shriram17f173d2017-10-24 22:11:07 -070059 for (i = 0; (i < NUM_SPLASHMEM_PROP_ELEM) && datalen; i++) {
60 memcpy (&val[i], data, sizeof (UINT32));
61 val[i] = fdt32_to_cpu (val[i]);
62 data += sizeof (UINT32);
63 datalen -= sizeof (UINT32);
64 }
Aravind Venkateswaranc0048032016-07-22 14:28:03 -070065
Jeevan Shriram17f173d2017-10-24 22:11:07 -070066 DEBUG ((EFI_D_VERBOSE, "reg = <0x%08x 0x%08x 0x%08x 0x%08x>\n", val[0],
67 val[1], val[2], val[3]));
Aravind Venkateswaranc0048032016-07-22 14:28:03 -070068}
69
Jeevan Shriram17f173d2017-10-24 22:11:07 -070070STATIC EFI_STATUS
Jeevan Shriramf8fa2912018-11-02 13:16:47 -070071GetDDRInfo (UINT8 *DdrDeviceType)
72{
73 EFI_DDRGETINFO_PROTOCOL *DdrInfoIf;
74 struct ddr_details_entry_info DdrInfo;
75 EFI_STATUS Status;
76
77 Status = gBS->LocateProtocol (&gEfiDDRGetInfoProtocolGuid, NULL,
78 (VOID **)&DdrInfoIf);
79 if (Status != EFI_SUCCESS) {
80 DEBUG ((EFI_D_VERBOSE,
Parth Dixit489de782019-04-22 17:18:21 +053081 "INFO: Unable to get DDR Info protocol. DDR type not updated:%r\n",
Jeevan Shriramf8fa2912018-11-02 13:16:47 -070082 Status));
83 return Status;
84 }
85
86 Status = DdrInfoIf->GetDDRDetails (DdrInfoIf, &DdrInfo);
87 if (EFI_ERROR (Status)) {
Parth Dixit489de782019-04-22 17:18:21 +053088 DEBUG ((EFI_D_ERROR, "INFO: GetDDR details failed\n"));
89 return Status;
Jeevan Shriramf8fa2912018-11-02 13:16:47 -070090 }
91
92 *DdrDeviceType = DdrInfo.device_type;
93 DEBUG ((EFI_D_VERBOSE, "DDR deviceType:%d", *DdrDeviceType));
94 return Status;
95}
96
97STATIC EFI_STATUS
Jeevan Shriram17f173d2017-10-24 22:11:07 -070098GetKaslrSeed (UINT64 *KaslrSeed)
Runmin Wang8ecc3f62016-12-09 14:03:05 -080099{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700100 EFI_QCOM_RNG_PROTOCOL *RngIf;
101 EFI_STATUS Status;
Runmin Wang8ecc3f62016-12-09 14:03:05 -0800102
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700103 Status = gBS->LocateProtocol (&gQcomRngProtocolGuid, NULL, (VOID **)&RngIf);
104 if (Status != EFI_SUCCESS) {
105 DEBUG ((EFI_D_VERBOSE,
106 "Error locating PRNG protocol. Fail to generate Kaslr seed:%r\n",
107 Status));
108 return Status;
109 }
Runmin Wang8ecc3f62016-12-09 14:03:05 -0800110
Jeevan Shriramf039efb2018-08-24 12:42:23 -0700111 Status = RngIf->GetRNG (RngIf,
112 &gEfiRNGAlgRawGuid,
113 sizeof (UINTN),
114 (UINT8 *)KaslrSeed);
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700115 if (Status != EFI_SUCCESS) {
116 DEBUG ((EFI_D_VERBOSE,
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700117 "Error getting PRNG random number. Fail to generate Kaslr seed:%r\n",
118 Status));
119 *KaslrSeed = 0;
120 return Status;
121 }
Runmin Wang8ecc3f62016-12-09 14:03:05 -0800122
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700123 return Status;
Runmin Wang8ecc3f62016-12-09 14:03:05 -0800124}
125
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700126STATIC EFI_STATUS
127UpdateSplashMemInfo (VOID *fdt)
Aravind Venkateswaranc0048032016-07-22 14:28:03 -0700128{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700129 EFI_STATUS Status;
130 CONST struct fdt_property *Prop = NULL;
131 INT32 PropLen = 0;
132 INT32 ret = 0;
133 UINT32 offset;
134 CHAR8 *tmp = NULL;
135 UINT32 CONST SplashMemPropSize = NUM_SPLASHMEM_PROP_ELEM * sizeof (UINT32);
Aravind Venkateswaranc0048032016-07-22 14:28:03 -0700136
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700137 Status =
138 gRT->GetVariable ((CHAR16 *)L"DisplaySplashBufferInfo",
139 &gQcomTokenSpaceGuid, NULL, &splashBufSize, &splashBuf);
140 if (Status != EFI_SUCCESS) {
141 DEBUG ((EFI_D_ERROR, "Unable to get splash buffer info, %r\n", Status));
142 goto error;
143 }
Aravind Venkateswaranc0048032016-07-22 14:28:03 -0700144
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700145 DEBUG ((EFI_D_VERBOSE, "Version=%d\nAddr=0x%08x\nSize=0x%08x\n",
146 splashBuf.uVersion, splashBuf.uFrameAddr, splashBuf.uFrameSize));
Aravind Venkateswaranc0048032016-07-22 14:28:03 -0700147
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700148 /* Get offset of the splash memory reservation node */
149 ret = fdt_path_offset (fdt, "/reserved-memory/splash_region");
150 if (ret < 0) {
151 DEBUG ((EFI_D_ERROR, "ERROR: Could not get splash memory region node\n"));
152 return EFI_NOT_FOUND;
153 }
154 offset = ret;
155 DEBUG ((EFI_D_VERBOSE, "FB mem node name: %a\n",
156 fdt_get_name (fdt, offset, NULL)));
Aravind Venkateswaranc0048032016-07-22 14:28:03 -0700157
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700158 /* Get the property that specifies the splash memory details */
159 Prop = fdt_get_property (fdt, offset, "reg", &PropLen);
160 if (!Prop) {
161 DEBUG ((EFI_D_ERROR, "ERROR: Could not find the splash reg property\n"));
162 return EFI_NOT_FOUND;
163 }
Aravind Venkateswaranc0048032016-07-22 14:28:03 -0700164
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700165 /*
166 * The format of the "reg" field is as follows:
167 * <0x0 FBAddress 0x0 FBSize>
168 * The expected size of this property is 4 * sizeof(UINT32)
169 */
170 if (PropLen != SplashMemPropSize) {
171 DEBUG (
172 (EFI_D_ERROR,
173 "ERROR: splash mem reservation node size. Expected: %d, Actual: %d\n",
174 SplashMemPropSize, PropLen));
175 return EFI_BAD_BUFFER_SIZE;
176 }
Aravind Venkateswaranc0048032016-07-22 14:28:03 -0700177
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700178 DEBUG ((EFI_D_VERBOSE, "Splash memory region before updating:\n"));
179 PrintSplashMemInfo (Prop->data, PropLen);
Aravind Venkateswaranc0048032016-07-22 14:28:03 -0700180
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700181 /* First, update the FBAddress */
182 if (CHECK_ADD64 ((UINT64)Prop->data, sizeof (UINT32))) {
Mukesh Ojha82911f82018-04-30 16:45:07 +0530183 DEBUG ((EFI_D_ERROR, "ERROR: integer Overflow while updating FBAddress"));
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700184 return EFI_BAD_BUFFER_SIZE;
185 }
186 tmp = (CHAR8 *)Prop->data + sizeof (UINT32);
187 splashBuf.uFrameAddr = cpu_to_fdt32 (splashBuf.uFrameAddr);
188 memcpy (tmp, &splashBuf.uFrameAddr, sizeof (UINT32));
Aravind Venkateswaranc0048032016-07-22 14:28:03 -0700189
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700190 /* Next, update the FBSize */
191 if (CHECK_ADD64 ((UINT64)tmp, (2 * sizeof (UINT32)))) {
Mukesh Ojha82911f82018-04-30 16:45:07 +0530192 DEBUG ((EFI_D_ERROR, "ERROR: integer Overflow while updating FBSize"));
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700193 return EFI_BAD_BUFFER_SIZE;
194 }
195 tmp += (2 * sizeof (UINT32));
196 splashBuf.uFrameSize = cpu_to_fdt32 (splashBuf.uFrameSize);
197 memcpy (tmp, &splashBuf.uFrameSize, sizeof (UINT32));
Aravind Venkateswaranc0048032016-07-22 14:28:03 -0700198
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700199 /* Update the property value in place */
200 ret = fdt_setprop_inplace (fdt, offset, "reg", Prop->data, PropLen);
201 if (ret < 0) {
202 DEBUG ((EFI_D_ERROR, "ERROR: Could not update splash mem info\n"));
203 return EFI_NO_MAPPING;
204 }
Aravind Venkateswaranc0048032016-07-22 14:28:03 -0700205
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700206 DEBUG ((EFI_D_VERBOSE, "Splash memory region after updating:\n"));
207 PrintSplashMemInfo (Prop->data, PropLen);
Aravind Venkateswaranc0048032016-07-22 14:28:03 -0700208error:
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700209 return Status;
Aravind Venkateswaranc0048032016-07-22 14:28:03 -0700210}
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800211
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700212UINT32
213fdt_check_header_ext (VOID *fdt)
Runmin Wange74d8042016-07-19 18:09:07 -0700214{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700215 UINT64 fdt_start, fdt_end;
216 UINT32 sum;
217 fdt_start = (UINT64)fdt;
Runmin Wange74d8042016-07-19 18:09:07 -0700218
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700219 if (fdt_start + fdt_totalsize (fdt) <= fdt_start) {
220 return FDT_ERR_BADOFFSET;
221 }
222 fdt_end = fdt_start + fdt_totalsize (fdt);
Runmin Wange74d8042016-07-19 18:09:07 -0700223
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700224 if (!(sum = ADD_OF (fdt_off_dt_struct (fdt), fdt_size_dt_struct (fdt)))) {
225 return FDT_ERR_BADOFFSET;
226 } else {
227 if (CHECK_ADD64 (fdt_start, sum))
228 return FDT_ERR_BADOFFSET;
229 else if (fdt_start + sum > fdt_end)
230 return FDT_ERR_BADOFFSET;
231 }
232 if (!(sum = ADD_OF (fdt_off_dt_strings (fdt), fdt_size_dt_strings (fdt)))) {
233 return FDT_ERR_BADOFFSET;
234 } else {
235 if (CHECK_ADD64 (fdt_start, sum))
236 return FDT_ERR_BADOFFSET;
237 else if (fdt_start + sum > fdt_end)
238 return FDT_ERR_BADOFFSET;
239 }
240 if (fdt_start + fdt_off_mem_rsvmap (fdt) > fdt_end)
241 return FDT_ERR_BADOFFSET;
242 return 0;
Runmin Wange74d8042016-07-19 18:09:07 -0700243}
244
Jeevan Shriram27c8faa2017-09-27 12:05:53 -0700245STATIC
Jeevan Shriram067bbb52018-05-08 17:48:47 -0700246VOID
247UpdateGranuleInfo (VOID *fdt)
248{
249 EFI_STATUS Status = EFI_SUCCESS;
Bhanuprakash Modem7ee03122018-06-12 19:47:36 +0530250 INT32 GranuleNodeOffset;
Jeevan Shriram067bbb52018-05-08 17:48:47 -0700251 UINT32 GranuleSize;
252 INT32 Ret;
253
Parth Dixitfc028d92019-04-22 15:55:46 +0530254 Status = GetGranuleSize (&GranuleSize);
255 if (EFI_ERROR (Status)) {
256 DEBUG ((EFI_D_VERBOSE,
257 "Unable to get Granule Size, Status = %r\r\n",
258 Status));
Bhanuprakash Modem7ee03122018-06-12 19:47:36 +0530259 return;
260 }
261
Parth Dixitfc028d92019-04-22 15:55:46 +0530262 GranuleNodeOffset = fdt_path_offset (fdt, "/mem-offline");
263 if (GranuleNodeOffset < 0) {
264 DEBUG ((EFI_D_VERBOSE, "INFO: Could not find mem-offline node.\n"));
Jeevan Shriram067bbb52018-05-08 17:48:47 -0700265 return;
266 }
267
Jeevan Shriram067bbb52018-05-08 17:48:47 -0700268 Ret = fdt_setprop_u32 (fdt, GranuleNodeOffset, "granule", GranuleSize);
269 if (Ret) {
Parth Dixitfc028d92019-04-22 15:55:46 +0530270 DEBUG ((EFI_D_ERROR, "INFO: Granule size update failed.\n"));
Jeevan Shriram067bbb52018-05-08 17:48:47 -0700271 }
272}
273
274STATIC
Jeevan Shriram27c8faa2017-09-27 12:05:53 -0700275EFI_STATUS
Mayank Groverabfc3532018-05-03 14:52:32 +0530276QueryMemoryCellSize (IN VOID *Fdt, OUT UINT32 *MemoryCellLen)
277{
278 INT32 RootOffset;
279 INT32 PropLen;
280 UINT32 AddrCellSize = 0;
281 UINT32 SizeCellSize = 0;
282 UINT32 *Prop = NULL;
283
284 RootOffset = fdt_path_offset (Fdt, "/");
285 if (RootOffset < 0) {
286 DEBUG ((EFI_D_ERROR, "Error finding root offset\n"));
287 return EFI_NOT_FOUND;
288 }
289
290 /* Find address-cells size */
291 Prop = (UINT32 *) fdt_getprop (Fdt, RootOffset, "#address-cells", &PropLen);
292 if (Prop &&
293 PropLen > 0) {
294 AddrCellSize = fdt32_to_cpu (*Prop);
295 } else {
296 DEBUG ((EFI_D_ERROR, "Error finding #address-cells property\n"));
297 return EFI_NOT_FOUND;
298 }
299
300 /* Find size-cells size */
301 Prop =(UINT32 *) fdt_getprop (Fdt, RootOffset, "#size-cells", &PropLen);
302 if (Prop &&
303 PropLen > 0) {
304 SizeCellSize = fdt32_to_cpu (*Prop);
305 } else {
306 DEBUG ((EFI_D_ERROR, "Error finding #size-cells property\n"));
307 return EFI_NOT_FOUND;
308 }
309
310 if (AddrCellSize > DEFAULT_CELL_SIZE ||
311 SizeCellSize > DEFAULT_CELL_SIZE ||
312 SizeCellSize == 0 ||
313 AddrCellSize == 0) {
314 DEBUG ((EFI_D_ERROR, "Error unsupported cell size value: #address-cell %d" \
315 "#size-cell\n", AddrCellSize, SizeCellSize));
316 return EFI_INVALID_PARAMETER;
317 }
318
319 /* Make sure memory cell size and address cell size are same */
320 if (AddrCellSize == SizeCellSize) {
321 *MemoryCellLen = AddrCellSize;
322 } else {
323 DEBUG ((EFI_D_ERROR, "Mismatch memory address cell and size cell size\n"));
324 return EFI_INVALID_PARAMETER;
325 }
326
327 return EFI_SUCCESS;
328}
329
330STATIC
331EFI_STATUS
332AddMemMap (VOID *Fdt, UINT32 MemNodeOffset, BOOLEAN BootWith32Bit)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800333{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700334 EFI_STATUS Status = EFI_NOT_FOUND;
335 INT32 ret = 0;
336 RamPartitionEntry *RamPartitions = NULL;
337 UINT32 NumPartitions = 0;
338 UINT32 i = 0;
Mayank Groverabfc3532018-05-03 14:52:32 +0530339 UINT32 MemoryCellLen = 0;
340
341 Status = QueryMemoryCellSize (Fdt, &MemoryCellLen);
342 if (EFI_ERROR (Status)) {
343 DEBUG ((EFI_D_ERROR, "ERROR: Not a valid memory node found!\n"));
344 return Status;
345 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800346
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700347 Status = GetRamPartitions (&RamPartitions, &NumPartitions);
348 if (EFI_ERROR (Status)) {
349 DEBUG ((EFI_D_ERROR, "Error returned from GetRamPartitions %r\n", Status));
350 return Status;
351 }
352 if (!RamPartitions) {
353 DEBUG ((EFI_D_ERROR, "RamPartitions is NULL\n"));
354 return EFI_NOT_FOUND;
355 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800356
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700357 DEBUG ((EFI_D_INFO, "RAM Partitions\r\n"));
358 for (i = 0; i < NumPartitions; i++) {
359 DEBUG ((EFI_D_INFO, "Add Base: 0x%016lx Available Length: 0x%016lx \n",
360 RamPartitions[i].Base, RamPartitions[i].AvailableLength));
Jeevan Shriram27c8faa2017-09-27 12:05:53 -0700361
Mayank Groverabfc3532018-05-03 14:52:32 +0530362 if (MemoryCellLen == 1) {
363 ret = dev_tree_add_mem_info (Fdt, MemNodeOffset, RamPartitions[i].Base,
364 RamPartitions[i].AvailableLength);
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700365 } else {
Mayank Groverabfc3532018-05-03 14:52:32 +0530366 ret = dev_tree_add_mem_infoV64 (Fdt, MemNodeOffset,
367 RamPartitions[i].Base,
368 RamPartitions[i].AvailableLength);
Jeevan Shriram27c8faa2017-09-27 12:05:53 -0700369 }
370
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700371 if (ret) {
372 DEBUG ((EFI_D_ERROR, "Add Base: 0x%016lx Length: 0x%016lx Fail\n",
373 RamPartitions[i].Base, RamPartitions[i].AvailableLength));
374 }
375 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800376
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700377 FreePool (RamPartitions);
378 RamPartitions = NULL;
379
380 return EFI_SUCCESS;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800381}
382
383/* Supporting function of UpdateDeviceTree()
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700384 * Function first gets the RAM partition table, then passes the pointer to
385 * AddMemMap() */
Jeevan Shriram27c8faa2017-09-27 12:05:53 -0700386STATIC
387EFI_STATUS
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700388target_dev_tree_mem (VOID *fdt, UINT32 MemNodeOffset, BOOLEAN BootWith32Bit)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800389{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700390 EFI_STATUS Status;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800391
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700392 /* Get Available memory from partition table */
393 Status = AddMemMap (fdt, MemNodeOffset, BootWith32Bit);
Jeevan Shriram067bbb52018-05-08 17:48:47 -0700394 if (EFI_ERROR (Status)) {
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700395 DEBUG ((EFI_D_ERROR,
396 "Invalid memory configuration, check memory partition table: %r\n",
397 Status));
Jeevan Shriram067bbb52018-05-08 17:48:47 -0700398 goto out;
399 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800400
Jeevan Shriram067bbb52018-05-08 17:48:47 -0700401 UpdateGranuleInfo (fdt);
402
403out:
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700404 return Status;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800405}
406
407/* Supporting function of target_dev_tree_mem()
408 * Function to add the subsequent RAM partition info to the device tree */
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700409INT32
410dev_tree_add_mem_info (VOID *fdt, UINT32 offset, UINT32 addr, UINT32 size)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800411{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700412 STATIC INT32 mem_info_cnt = 0;
413 INT32 ret = 0;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800414
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700415 if (!mem_info_cnt) {
416 /* Replace any other reg prop in the memory node. */
417 ret = fdt_setprop_u32 (fdt, offset, "reg", addr);
418 mem_info_cnt = 1;
419 } else {
420 /* Append the mem info to the reg prop for subsequent nodes. */
421 ret = fdt_appendprop_u32 (fdt, offset, "reg", addr);
422 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800423
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700424 if (ret) {
425 DEBUG (
426 (EFI_D_ERROR, "Failed to add the memory information addr: %d\n", ret));
427 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800428
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700429 ret = fdt_appendprop_u32 (fdt, offset, "reg", size);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800430
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700431 if (ret) {
432 DEBUG (
433 (EFI_D_ERROR, "Failed to add the memory information size: %d\n", ret));
434 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800435
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700436 return ret;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800437}
438
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700439INT32
440dev_tree_add_mem_infoV64 (VOID *fdt, UINT32 offset, UINT64 addr, UINT64 size)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800441{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700442 STATIC INT32 mem_info_cnt = 0;
443 INT32 ret = 0;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800444
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700445 if (!mem_info_cnt) {
446 /* Replace any other reg prop in the memory node. */
447 ret = fdt_setprop_u64 (fdt, offset, "reg", addr);
448 mem_info_cnt = 1;
449 } else {
450 /* Append the mem info to the reg prop for subsequent nodes. */
451 ret = fdt_appendprop_u64 (fdt, offset, "reg", addr);
452 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800453
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700454 if (ret) {
455 DEBUG (
456 (EFI_D_ERROR, "Failed to add the memory information addr: %d\n", ret));
457 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800458
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700459 ret = fdt_appendprop_u64 (fdt, offset, "reg", size);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800460
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700461 if (ret) {
462 DEBUG (
463 (EFI_D_ERROR, "Failed to add the memory information size: %d\n", ret));
464 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800465
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700466 return ret;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800467}
468
469/* Top level function that updates the device tree. */
Jeevan Shriram27c8faa2017-09-27 12:05:53 -0700470EFI_STATUS
471UpdateDeviceTree (VOID *fdt,
472 CONST CHAR8 *cmdline,
473 VOID *ramdisk,
474 UINT32 RamDiskSize,
475 BOOLEAN BootWith32Bit)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800476{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700477 INT32 ret = 0;
478 UINT32 offset;
479 UINT32 PaddSize = 0;
480 UINT64 KaslrSeed = 0;
Jeevan Shriramf8fa2912018-11-02 13:16:47 -0700481 UINT8 DdrDeviceType;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700482 EFI_STATUS Status;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800483
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700484 /* Check the device tree header */
485 ret = fdt_check_header (fdt) || fdt_check_header_ext (fdt);
486 if (ret) {
487 DEBUG ((EFI_D_ERROR, "ERROR: Invalid device tree header ...\n"));
488 return EFI_NOT_FOUND;
489 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800490
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700491 /* Add padding to make space for new nodes and properties. */
lijuangfd6fa912017-11-16 18:08:22 +0800492 PaddSize = ADD_OF (fdt_totalsize (fdt),
493 DTB_PAD_SIZE + AsciiStrLen (cmdline));
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700494 if (!PaddSize) {
Mukesh Ojha82911f82018-04-30 16:45:07 +0530495 DEBUG ((EFI_D_ERROR, "ERROR: Integer Overflow: fdt size = %u\n",
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700496 fdt_totalsize (fdt)));
497 return EFI_BAD_BUFFER_SIZE;
498 }
499 ret = fdt_open_into (fdt, fdt, PaddSize);
500 if (ret != 0) {
501 DEBUG ((EFI_D_ERROR, "ERROR: Failed to move/resize dtb buffer ...\n"));
502 return EFI_BAD_BUFFER_SIZE;
503 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800504
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700505 /* Get offset of the memory node */
506 ret = fdt_path_offset (fdt, "/memory");
507 if (ret < 0) {
508 DEBUG ((EFI_D_ERROR, "ERROR: Could not find memory node ...\n"));
509 return EFI_NOT_FOUND;
510 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800511
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700512 offset = ret;
513 Status = target_dev_tree_mem (fdt, offset, BootWith32Bit);
514 if (Status != EFI_SUCCESS) {
515 DEBUG ((EFI_D_ERROR, "ERROR: Cannot update memory node\n"));
516 return Status;
517 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800518
Jeevan Shriramf8fa2912018-11-02 13:16:47 -0700519 Status = GetDDRInfo (&DdrDeviceType);
520 if (Status == EFI_SUCCESS) {
521 ret = fdt_appendprop_u32 (fdt, offset, (CONST char *)"ddr_device_type",
522 (UINT32)DdrDeviceType);
523 if (ret) {
524 DEBUG ((EFI_D_ERROR,
525 "ERROR: Cannot update memory node [ddr_device_type] - 0x%x\n",
526 ret));
527 } else {
528 DEBUG ((EFI_D_VERBOSE, "ddr_device_type is added to memory node\n"));
529 }
Jeevan Shriramf8fa2912018-11-02 13:16:47 -0700530 }
531
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700532 UpdateSplashMemInfo (fdt);
Aravind Venkateswaranc0048032016-07-22 14:28:03 -0700533
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700534 /* Get offset of the chosen node */
535 ret = fdt_path_offset (fdt, "/chosen");
536 if (ret < 0) {
537 DEBUG ((EFI_D_ERROR, "ERROR: Could not find chosen node ...\n"));
538 return EFI_NOT_FOUND;
539 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800540
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700541 offset = ret;
542 if (cmdline) {
543 /* Adding the cmdline to the chosen node */
544 ret = fdt_appendprop_string (fdt, offset, (CONST char *)"bootargs",
545 (CONST VOID *)cmdline);
546 if (ret) {
547 DEBUG ((EFI_D_ERROR,
548 "ERROR: Cannot update chosen node [bootargs] - 0x%x\n", ret));
549 return EFI_LOAD_ERROR;
550 }
551 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800552
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700553 Status = GetKaslrSeed (&KaslrSeed);
554 if (Status == EFI_SUCCESS) {
555 /* Adding Kaslr Seed to the chosen node */
556 ret = fdt_appendprop_u64 (fdt, offset, (CONST char *)"kaslr-seed",
557 (UINT64)KaslrSeed);
558 if (ret) {
559 DEBUG ((EFI_D_INFO,
560 "ERROR: Cannot update chosen node [kaslr-seed] - 0x%x\n", ret));
561 } else {
562 DEBUG ((EFI_D_INFO, "kaslr-Seed is added to chosen node\n"));
563 }
564 } else {
565 DEBUG ((EFI_D_INFO, "ERROR: Cannot generate Kaslr Seed - %r\n", Status));
566 }
Runmin Wang8ecc3f62016-12-09 14:03:05 -0800567
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700568 if (RamDiskSize) {
569 /* Adding the initrd-start to the chosen node */
570 ret = fdt_setprop_u64 (fdt, offset, "linux,initrd-start", (UINT64)ramdisk);
571 if (ret) {
572 DEBUG ((EFI_D_ERROR,
573 "ERROR: Cannot update chosen node [linux,initrd-start] - 0x%x\n",
574 ret));
575 return EFI_NOT_FOUND;
576 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800577
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700578 /* Adding the initrd-end to the chosen node */
579 ret = fdt_setprop_u64 (fdt, offset, "linux,initrd-end",
580 ((UINT64)ramdisk + RamDiskSize));
581 if (ret) {
582 DEBUG ((EFI_D_ERROR,
583 "ERROR: Cannot update chosen node [linux,initrd-end] - 0x%x\n",
584 ret));
585 return EFI_NOT_FOUND;
586 }
587 }
Bhanuprakash Modem0450e9b2017-06-09 11:58:51 +0530588
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700589 /* Update fstab node */
590 DEBUG ((EFI_D_VERBOSE, "Start DT fstab node update: %lu ms\n",
591 GetTimerCountms ()));
592 UpdateFstabNode (fdt);
593 DEBUG ((EFI_D_VERBOSE, "End DT fstab node update: %lu ms\n",
594 GetTimerCountms ()));
Bhanuprakash Modem0450e9b2017-06-09 11:58:51 +0530595
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700596 fdt_pack (fdt);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800597
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700598 return ret;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800599}
Channagoud Kadabi9be68a72016-02-08 15:18:24 -0800600
Bhanuprakash Modem0450e9b2017-06-09 11:58:51 +0530601/* Update device tree for fstab node */
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700602EFI_STATUS
603UpdateFstabNode (VOID *fdt)
Bhanuprakash Modem0450e9b2017-06-09 11:58:51 +0530604{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700605 INT32 ParentOffset = 0;
606 INT32 SubNodeOffset = 0;
607 CONST struct fdt_property *Prop = NULL;
608 INT32 PropLen = 0;
609 char *NodeName = NULL;
610 EFI_STATUS Status = EFI_SUCCESS;
611 CHAR8 *BootDevBuf = NULL;
612 CHAR8 *ReplaceStr = NULL;
613 CHAR8 *NextStr = NULL;
Mayank Grover546377b2019-04-12 15:52:10 +0530614 struct FstabNode Table = IsDynamicPartitionSupport () ? DynamicFstabTable
615 : FstabTable;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700616 UINT32 DevNodeBootDevLen = 0;
617 UINT32 Index = 0;
618 UINT32 PaddingEnd = 0;
Bhanuprakash Modem0450e9b2017-06-09 11:58:51 +0530619
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700620 /* Find the parent node */
621 ParentOffset = fdt_path_offset (fdt, Table.ParentNode);
622 if (ParentOffset < 0) {
623 DEBUG ((EFI_D_VERBOSE, "Failed to Get parent node: fstab\terror: %d\n",
624 ParentOffset));
625 return EFI_NOT_FOUND;
626 }
627 DEBUG ((EFI_D_VERBOSE, "Node: %a found.\n",
628 fdt_get_name (fdt, ParentOffset, NULL)));
Bhanuprakash Modem0450e9b2017-06-09 11:58:51 +0530629
Mayank Grover546377b2019-04-12 15:52:10 +0530630 if (!IsDynamicPartitionSupport ()) {
631 /* Get boot device type */
632 BootDevBuf = AllocateZeroPool (sizeof (CHAR8) * BOOT_DEV_MAX_LEN);
633 if (BootDevBuf == NULL) {
634 DEBUG ((EFI_D_ERROR, "Boot device buffer: Out of resources\n"));
635 return EFI_OUT_OF_RESOURCES;
636 }
Bhanuprakash Modem0450e9b2017-06-09 11:58:51 +0530637
Mayank Grover546377b2019-04-12 15:52:10 +0530638 Status = GetBootDevice (BootDevBuf, BOOT_DEV_MAX_LEN);
639 if (Status != EFI_SUCCESS) {
640 DEBUG ((EFI_D_ERROR, "Failed to get Boot Device: %r\n", Status));
641 FreePool (BootDevBuf);
642 BootDevBuf = NULL;
643 return Status;
644 }
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700645 }
Bhanuprakash Modem0450e9b2017-06-09 11:58:51 +0530646
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700647 /* Get properties of all sub nodes */
648 for (SubNodeOffset = fdt_first_subnode (fdt, ParentOffset);
649 SubNodeOffset >= 0;
650 SubNodeOffset = fdt_next_subnode (fdt, SubNodeOffset)) {
651 Prop = fdt_get_property (fdt, SubNodeOffset, Table.Property, &PropLen);
652 NodeName = (char *)(uintptr_t)fdt_get_name (fdt, SubNodeOffset, NULL);
653 if (!Prop) {
654 DEBUG ((EFI_D_VERBOSE, "Property:%a is not found for sub-node:%a\n",
655 Table.Property, NodeName));
656 } else {
657 DEBUG ((EFI_D_VERBOSE, "Property:%a found for sub-node:%a\tProperty:%a\n",
658 Table.Property, NodeName, Prop->data));
Bhanuprakash Modem0450e9b2017-06-09 11:58:51 +0530659
Mayank Grover546377b2019-04-12 15:52:10 +0530660 /* For Dynamic partition support disable firmware fstab nodes. */
661 if (IsDynamicPartitionSupport ()) {
662 DEBUG ((EFI_D_VERBOSE, "Disabling node status :%a\n", NodeName));
663 Status = fdt_setprop (fdt, SubNodeOffset, Table.Property, "disabled",
664 (AsciiStrLen ("disabled") + 1));
665 if (Status) {
666 DEBUG ((EFI_D_ERROR, "ERROR: Failed to disable Node: %a\n", NodeName));
667 }
668 continue;
669 }
670
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700671 /* Pointer to fdt 'dev' property string that needs to update based on the
672 * 'androidboot.bootdevice' */
673 ReplaceStr = (CHAR8 *)Prop->data;
674 ReplaceStr = AsciiStrStr (ReplaceStr, Table.DevicePathId);
675 if (!ReplaceStr) {
676 DEBUG ((EFI_D_VERBOSE, "Update property:%a value is not proper to "
677 "update for sub-node:%a\n",
678 Table.Property, NodeName));
679 continue;
680 }
681 ReplaceStr += AsciiStrLen (Table.DevicePathId);
682 NextStr = AsciiStrStr ((ReplaceStr + 1), "/");
683 DevNodeBootDevLen = NextStr - ReplaceStr;
684 if (DevNodeBootDevLen >= AsciiStrLen (BootDevBuf)) {
Mukesh Ojha323c9402017-10-03 23:49:05 +0530685 gBS->CopyMem (ReplaceStr, BootDevBuf, AsciiStrLen (BootDevBuf));
686 PaddingEnd = DevNodeBootDevLen - AsciiStrLen (BootDevBuf);
687 /* Update the property with new value */
688 if (PaddingEnd) {
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700689 gBS->CopyMem (ReplaceStr + AsciiStrLen (BootDevBuf), NextStr,
690 AsciiStrLen (NextStr));
691 for (Index = 0; Index < PaddingEnd; Index++) {
692 ReplaceStr[AsciiStrLen (BootDevBuf) + AsciiStrLen (NextStr) +
693 Index] = ' ';
694 }
Mukesh Ojha323c9402017-10-03 23:49:05 +0530695 }
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700696 } else {
Mukesh Ojha323c9402017-10-03 23:49:05 +0530697 DEBUG ((EFI_D_ERROR, "String length mismatch b/w DT Bootdevice string"
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700698 " (%d) and expected Bootdevice strings (%d)\n",
699 DevNodeBootDevLen, AsciiStrLen (BootDevBuf)));
700 }
Mukesh Ojha323c9402017-10-03 23:49:05 +0530701 }
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700702 }
Bhanuprakash Modem0450e9b2017-06-09 11:58:51 +0530703
Mayank Grover546377b2019-04-12 15:52:10 +0530704 if (BootDevBuf) {
705 FreePool (BootDevBuf);
706 }
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700707 BootDevBuf = NULL;
708 return Status;
Bhanuprakash Modem0450e9b2017-06-09 11:58:51 +0530709}