Mayank Grover | 2676fa4 | 2019-04-16 18:43:03 +0530 | [diff] [blame] | 1 | /* Copyright (c) 2015-2019, The Linux Foundation. All rights reserved. |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 2 | * |
| 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 Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 31 | * dev_tree_add_mem_info() is called at every time when memory type matches |
| 32 | * conditions */ |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 33 | |
| 34 | #include "UpdateDeviceTree.h" |
lijuang | f0bbcad | 2017-08-16 16:59:18 +0800 | [diff] [blame] | 35 | #include "AutoGen.h" |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 36 | #include <Library/UpdateDeviceTree.h> |
Mayank Grover | 2676fa4 | 2019-04-16 18:43:03 +0530 | [diff] [blame] | 37 | #include <Library/LocateDeviceTree.h> |
Mayank Grover | 546377b | 2019-04-12 15:52:10 +0530 | [diff] [blame] | 38 | #include <Library/BootLinux.h> |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 39 | #include <Protocol/EFIChipInfoTypes.h> |
Jeevan Shriram | f8fa291 | 2018-11-02 13:16:47 -0700 | [diff] [blame] | 40 | #include <Protocol/EFIDDRGetConfig.h> |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 41 | #include <Protocol/EFIRng.h> |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 42 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 43 | #define NUM_SPLASHMEM_PROP_ELEM 4 |
Mayank Grover | abfc353 | 2018-05-03 14:52:32 +0530 | [diff] [blame] | 44 | #define DEFAULT_CELL_SIZE 2 |
Aravind Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 45 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 46 | STATIC struct FstabNode FstabTable = {"/firmware/android/fstab", "dev", |
| 47 | "/soc/"}; |
Mayank Grover | 546377b | 2019-04-12 15:52:10 +0530 | [diff] [blame] | 48 | STATIC struct FstabNode DynamicFstabTable = {"/firmware/android/fstab", |
| 49 | "status", |
| 50 | ""}; |
Aravind Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 51 | STATIC struct DisplaySplashBufferInfo splashBuf; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 52 | STATIC UINTN splashBufSize = sizeof (splashBuf); |
Aravind Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 53 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 54 | STATIC VOID |
| 55 | PrintSplashMemInfo (CONST CHAR8 *data, INT32 datalen) |
Aravind Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 56 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 57 | UINT32 i, val[NUM_SPLASHMEM_PROP_ELEM] = {0}; |
Aravind Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 58 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 59 | 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 Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 65 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 66 | DEBUG ((EFI_D_VERBOSE, "reg = <0x%08x 0x%08x 0x%08x 0x%08x>\n", val[0], |
| 67 | val[1], val[2], val[3])); |
Aravind Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 70 | STATIC EFI_STATUS |
Jeevan Shriram | f8fa291 | 2018-11-02 13:16:47 -0700 | [diff] [blame] | 71 | GetDDRInfo (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 Dixit | 489de78 | 2019-04-22 17:18:21 +0530 | [diff] [blame^] | 81 | "INFO: Unable to get DDR Info protocol. DDR type not updated:%r\n", |
Jeevan Shriram | f8fa291 | 2018-11-02 13:16:47 -0700 | [diff] [blame] | 82 | Status)); |
| 83 | return Status; |
| 84 | } |
| 85 | |
| 86 | Status = DdrInfoIf->GetDDRDetails (DdrInfoIf, &DdrInfo); |
| 87 | if (EFI_ERROR (Status)) { |
Parth Dixit | 489de78 | 2019-04-22 17:18:21 +0530 | [diff] [blame^] | 88 | DEBUG ((EFI_D_ERROR, "INFO: GetDDR details failed\n")); |
| 89 | return Status; |
Jeevan Shriram | f8fa291 | 2018-11-02 13:16:47 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | *DdrDeviceType = DdrInfo.device_type; |
| 93 | DEBUG ((EFI_D_VERBOSE, "DDR deviceType:%d", *DdrDeviceType)); |
| 94 | return Status; |
| 95 | } |
| 96 | |
| 97 | STATIC EFI_STATUS |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 98 | GetKaslrSeed (UINT64 *KaslrSeed) |
Runmin Wang | 8ecc3f6 | 2016-12-09 14:03:05 -0800 | [diff] [blame] | 99 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 100 | EFI_QCOM_RNG_PROTOCOL *RngIf; |
| 101 | EFI_STATUS Status; |
Runmin Wang | 8ecc3f6 | 2016-12-09 14:03:05 -0800 | [diff] [blame] | 102 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 103 | 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 Wang | 8ecc3f6 | 2016-12-09 14:03:05 -0800 | [diff] [blame] | 110 | |
Jeevan Shriram | f039efb | 2018-08-24 12:42:23 -0700 | [diff] [blame] | 111 | Status = RngIf->GetRNG (RngIf, |
| 112 | &gEfiRNGAlgRawGuid, |
| 113 | sizeof (UINTN), |
| 114 | (UINT8 *)KaslrSeed); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 115 | if (Status != EFI_SUCCESS) { |
| 116 | DEBUG ((EFI_D_VERBOSE, |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 117 | "Error getting PRNG random number. Fail to generate Kaslr seed:%r\n", |
| 118 | Status)); |
| 119 | *KaslrSeed = 0; |
| 120 | return Status; |
| 121 | } |
Runmin Wang | 8ecc3f6 | 2016-12-09 14:03:05 -0800 | [diff] [blame] | 122 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 123 | return Status; |
Runmin Wang | 8ecc3f6 | 2016-12-09 14:03:05 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 126 | STATIC EFI_STATUS |
| 127 | UpdateSplashMemInfo (VOID *fdt) |
Aravind Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 128 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 129 | 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 Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 136 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 137 | 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 Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 144 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 145 | DEBUG ((EFI_D_VERBOSE, "Version=%d\nAddr=0x%08x\nSize=0x%08x\n", |
| 146 | splashBuf.uVersion, splashBuf.uFrameAddr, splashBuf.uFrameSize)); |
Aravind Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 147 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 148 | /* 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 Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 157 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 158 | /* 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 Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 164 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 165 | /* |
| 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 Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 177 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 178 | DEBUG ((EFI_D_VERBOSE, "Splash memory region before updating:\n")); |
| 179 | PrintSplashMemInfo (Prop->data, PropLen); |
Aravind Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 180 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 181 | /* First, update the FBAddress */ |
| 182 | if (CHECK_ADD64 ((UINT64)Prop->data, sizeof (UINT32))) { |
Mukesh Ojha | 82911f8 | 2018-04-30 16:45:07 +0530 | [diff] [blame] | 183 | DEBUG ((EFI_D_ERROR, "ERROR: integer Overflow while updating FBAddress")); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 184 | 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 Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 189 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 190 | /* Next, update the FBSize */ |
| 191 | if (CHECK_ADD64 ((UINT64)tmp, (2 * sizeof (UINT32)))) { |
Mukesh Ojha | 82911f8 | 2018-04-30 16:45:07 +0530 | [diff] [blame] | 192 | DEBUG ((EFI_D_ERROR, "ERROR: integer Overflow while updating FBSize")); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 193 | 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 Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 198 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 199 | /* 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 Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 205 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 206 | DEBUG ((EFI_D_VERBOSE, "Splash memory region after updating:\n")); |
| 207 | PrintSplashMemInfo (Prop->data, PropLen); |
Aravind Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 208 | error: |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 209 | return Status; |
Aravind Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 210 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 211 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 212 | UINT32 |
| 213 | fdt_check_header_ext (VOID *fdt) |
Runmin Wang | e74d804 | 2016-07-19 18:09:07 -0700 | [diff] [blame] | 214 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 215 | UINT64 fdt_start, fdt_end; |
| 216 | UINT32 sum; |
| 217 | fdt_start = (UINT64)fdt; |
Runmin Wang | e74d804 | 2016-07-19 18:09:07 -0700 | [diff] [blame] | 218 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 219 | if (fdt_start + fdt_totalsize (fdt) <= fdt_start) { |
| 220 | return FDT_ERR_BADOFFSET; |
| 221 | } |
| 222 | fdt_end = fdt_start + fdt_totalsize (fdt); |
Runmin Wang | e74d804 | 2016-07-19 18:09:07 -0700 | [diff] [blame] | 223 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 224 | 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 Wang | e74d804 | 2016-07-19 18:09:07 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Jeevan Shriram | 27c8faa | 2017-09-27 12:05:53 -0700 | [diff] [blame] | 245 | STATIC |
Jeevan Shriram | 067bbb5 | 2018-05-08 17:48:47 -0700 | [diff] [blame] | 246 | VOID |
| 247 | UpdateGranuleInfo (VOID *fdt) |
| 248 | { |
| 249 | EFI_STATUS Status = EFI_SUCCESS; |
Bhanuprakash Modem | 7ee0312 | 2018-06-12 19:47:36 +0530 | [diff] [blame] | 250 | INT32 GranuleNodeOffset; |
Jeevan Shriram | 067bbb5 | 2018-05-08 17:48:47 -0700 | [diff] [blame] | 251 | UINT32 GranuleSize; |
| 252 | INT32 Ret; |
| 253 | |
Parth Dixit | fc028d9 | 2019-04-22 15:55:46 +0530 | [diff] [blame] | 254 | 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 Modem | 7ee0312 | 2018-06-12 19:47:36 +0530 | [diff] [blame] | 259 | return; |
| 260 | } |
| 261 | |
Parth Dixit | fc028d9 | 2019-04-22 15:55:46 +0530 | [diff] [blame] | 262 | 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 Shriram | 067bbb5 | 2018-05-08 17:48:47 -0700 | [diff] [blame] | 265 | return; |
| 266 | } |
| 267 | |
Jeevan Shriram | 067bbb5 | 2018-05-08 17:48:47 -0700 | [diff] [blame] | 268 | Ret = fdt_setprop_u32 (fdt, GranuleNodeOffset, "granule", GranuleSize); |
| 269 | if (Ret) { |
Parth Dixit | fc028d9 | 2019-04-22 15:55:46 +0530 | [diff] [blame] | 270 | DEBUG ((EFI_D_ERROR, "INFO: Granule size update failed.\n")); |
Jeevan Shriram | 067bbb5 | 2018-05-08 17:48:47 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
| 274 | STATIC |
Jeevan Shriram | 27c8faa | 2017-09-27 12:05:53 -0700 | [diff] [blame] | 275 | EFI_STATUS |
Mayank Grover | abfc353 | 2018-05-03 14:52:32 +0530 | [diff] [blame] | 276 | QueryMemoryCellSize (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 | |
| 330 | STATIC |
| 331 | EFI_STATUS |
| 332 | AddMemMap (VOID *Fdt, UINT32 MemNodeOffset, BOOLEAN BootWith32Bit) |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 333 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 334 | EFI_STATUS Status = EFI_NOT_FOUND; |
| 335 | INT32 ret = 0; |
| 336 | RamPartitionEntry *RamPartitions = NULL; |
| 337 | UINT32 NumPartitions = 0; |
| 338 | UINT32 i = 0; |
Mayank Grover | abfc353 | 2018-05-03 14:52:32 +0530 | [diff] [blame] | 339 | 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 Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 346 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 347 | 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 Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 356 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 357 | 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 Shriram | 27c8faa | 2017-09-27 12:05:53 -0700 | [diff] [blame] | 361 | |
Mayank Grover | abfc353 | 2018-05-03 14:52:32 +0530 | [diff] [blame] | 362 | if (MemoryCellLen == 1) { |
| 363 | ret = dev_tree_add_mem_info (Fdt, MemNodeOffset, RamPartitions[i].Base, |
| 364 | RamPartitions[i].AvailableLength); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 365 | } else { |
Mayank Grover | abfc353 | 2018-05-03 14:52:32 +0530 | [diff] [blame] | 366 | ret = dev_tree_add_mem_infoV64 (Fdt, MemNodeOffset, |
| 367 | RamPartitions[i].Base, |
| 368 | RamPartitions[i].AvailableLength); |
Jeevan Shriram | 27c8faa | 2017-09-27 12:05:53 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 371 | 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 Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 376 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 377 | FreePool (RamPartitions); |
| 378 | RamPartitions = NULL; |
| 379 | |
| 380 | return EFI_SUCCESS; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | /* Supporting function of UpdateDeviceTree() |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 384 | * Function first gets the RAM partition table, then passes the pointer to |
| 385 | * AddMemMap() */ |
Jeevan Shriram | 27c8faa | 2017-09-27 12:05:53 -0700 | [diff] [blame] | 386 | STATIC |
| 387 | EFI_STATUS |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 388 | target_dev_tree_mem (VOID *fdt, UINT32 MemNodeOffset, BOOLEAN BootWith32Bit) |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 389 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 390 | EFI_STATUS Status; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 391 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 392 | /* Get Available memory from partition table */ |
| 393 | Status = AddMemMap (fdt, MemNodeOffset, BootWith32Bit); |
Jeevan Shriram | 067bbb5 | 2018-05-08 17:48:47 -0700 | [diff] [blame] | 394 | if (EFI_ERROR (Status)) { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 395 | DEBUG ((EFI_D_ERROR, |
| 396 | "Invalid memory configuration, check memory partition table: %r\n", |
| 397 | Status)); |
Jeevan Shriram | 067bbb5 | 2018-05-08 17:48:47 -0700 | [diff] [blame] | 398 | goto out; |
| 399 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 400 | |
Jeevan Shriram | 067bbb5 | 2018-05-08 17:48:47 -0700 | [diff] [blame] | 401 | UpdateGranuleInfo (fdt); |
| 402 | |
| 403 | out: |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 404 | return Status; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | /* Supporting function of target_dev_tree_mem() |
| 408 | * Function to add the subsequent RAM partition info to the device tree */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 409 | INT32 |
| 410 | dev_tree_add_mem_info (VOID *fdt, UINT32 offset, UINT32 addr, UINT32 size) |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 411 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 412 | STATIC INT32 mem_info_cnt = 0; |
| 413 | INT32 ret = 0; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 414 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 415 | 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 Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 423 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 424 | if (ret) { |
| 425 | DEBUG ( |
| 426 | (EFI_D_ERROR, "Failed to add the memory information addr: %d\n", ret)); |
| 427 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 428 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 429 | ret = fdt_appendprop_u32 (fdt, offset, "reg", size); |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 430 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 431 | if (ret) { |
| 432 | DEBUG ( |
| 433 | (EFI_D_ERROR, "Failed to add the memory information size: %d\n", ret)); |
| 434 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 435 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 436 | return ret; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 437 | } |
| 438 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 439 | INT32 |
| 440 | dev_tree_add_mem_infoV64 (VOID *fdt, UINT32 offset, UINT64 addr, UINT64 size) |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 441 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 442 | STATIC INT32 mem_info_cnt = 0; |
| 443 | INT32 ret = 0; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 444 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 445 | 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 Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 453 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 454 | if (ret) { |
| 455 | DEBUG ( |
| 456 | (EFI_D_ERROR, "Failed to add the memory information addr: %d\n", ret)); |
| 457 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 458 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 459 | ret = fdt_appendprop_u64 (fdt, offset, "reg", size); |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 460 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 461 | if (ret) { |
| 462 | DEBUG ( |
| 463 | (EFI_D_ERROR, "Failed to add the memory information size: %d\n", ret)); |
| 464 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 465 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 466 | return ret; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /* Top level function that updates the device tree. */ |
Jeevan Shriram | 27c8faa | 2017-09-27 12:05:53 -0700 | [diff] [blame] | 470 | EFI_STATUS |
| 471 | UpdateDeviceTree (VOID *fdt, |
| 472 | CONST CHAR8 *cmdline, |
| 473 | VOID *ramdisk, |
| 474 | UINT32 RamDiskSize, |
| 475 | BOOLEAN BootWith32Bit) |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 476 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 477 | INT32 ret = 0; |
| 478 | UINT32 offset; |
| 479 | UINT32 PaddSize = 0; |
| 480 | UINT64 KaslrSeed = 0; |
Jeevan Shriram | f8fa291 | 2018-11-02 13:16:47 -0700 | [diff] [blame] | 481 | UINT8 DdrDeviceType; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 482 | EFI_STATUS Status; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 483 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 484 | /* 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 Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 490 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 491 | /* Add padding to make space for new nodes and properties. */ |
lijuang | fd6fa91 | 2017-11-16 18:08:22 +0800 | [diff] [blame] | 492 | PaddSize = ADD_OF (fdt_totalsize (fdt), |
| 493 | DTB_PAD_SIZE + AsciiStrLen (cmdline)); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 494 | if (!PaddSize) { |
Mukesh Ojha | 82911f8 | 2018-04-30 16:45:07 +0530 | [diff] [blame] | 495 | DEBUG ((EFI_D_ERROR, "ERROR: Integer Overflow: fdt size = %u\n", |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 496 | 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 Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 504 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 505 | /* 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 Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 511 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 512 | 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 Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 518 | |
Jeevan Shriram | f8fa291 | 2018-11-02 13:16:47 -0700 | [diff] [blame] | 519 | 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 Shriram | f8fa291 | 2018-11-02 13:16:47 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 532 | UpdateSplashMemInfo (fdt); |
Aravind Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 533 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 534 | /* 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 Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 540 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 541 | 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 Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 552 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 553 | 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 Wang | 8ecc3f6 | 2016-12-09 14:03:05 -0800 | [diff] [blame] | 567 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 568 | 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 Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 577 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 578 | /* 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 Modem | 0450e9b | 2017-06-09 11:58:51 +0530 | [diff] [blame] | 588 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 589 | /* 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 Modem | 0450e9b | 2017-06-09 11:58:51 +0530 | [diff] [blame] | 595 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 596 | fdt_pack (fdt); |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 597 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 598 | return ret; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 599 | } |
Channagoud Kadabi | 9be68a7 | 2016-02-08 15:18:24 -0800 | [diff] [blame] | 600 | |
Bhanuprakash Modem | 0450e9b | 2017-06-09 11:58:51 +0530 | [diff] [blame] | 601 | /* Update device tree for fstab node */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 602 | EFI_STATUS |
| 603 | UpdateFstabNode (VOID *fdt) |
Bhanuprakash Modem | 0450e9b | 2017-06-09 11:58:51 +0530 | [diff] [blame] | 604 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 605 | 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 Grover | 546377b | 2019-04-12 15:52:10 +0530 | [diff] [blame] | 614 | struct FstabNode Table = IsDynamicPartitionSupport () ? DynamicFstabTable |
| 615 | : FstabTable; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 616 | UINT32 DevNodeBootDevLen = 0; |
| 617 | UINT32 Index = 0; |
| 618 | UINT32 PaddingEnd = 0; |
Bhanuprakash Modem | 0450e9b | 2017-06-09 11:58:51 +0530 | [diff] [blame] | 619 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 620 | /* 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 Modem | 0450e9b | 2017-06-09 11:58:51 +0530 | [diff] [blame] | 629 | |
Mayank Grover | 546377b | 2019-04-12 15:52:10 +0530 | [diff] [blame] | 630 | 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 Modem | 0450e9b | 2017-06-09 11:58:51 +0530 | [diff] [blame] | 637 | |
Mayank Grover | 546377b | 2019-04-12 15:52:10 +0530 | [diff] [blame] | 638 | 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 Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 645 | } |
Bhanuprakash Modem | 0450e9b | 2017-06-09 11:58:51 +0530 | [diff] [blame] | 646 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 647 | /* 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 Modem | 0450e9b | 2017-06-09 11:58:51 +0530 | [diff] [blame] | 659 | |
Mayank Grover | 546377b | 2019-04-12 15:52:10 +0530 | [diff] [blame] | 660 | /* 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 Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 671 | /* 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 Ojha | 323c940 | 2017-10-03 23:49:05 +0530 | [diff] [blame] | 685 | gBS->CopyMem (ReplaceStr, BootDevBuf, AsciiStrLen (BootDevBuf)); |
| 686 | PaddingEnd = DevNodeBootDevLen - AsciiStrLen (BootDevBuf); |
| 687 | /* Update the property with new value */ |
| 688 | if (PaddingEnd) { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 689 | 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 Ojha | 323c940 | 2017-10-03 23:49:05 +0530 | [diff] [blame] | 695 | } |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 696 | } else { |
Mukesh Ojha | 323c940 | 2017-10-03 23:49:05 +0530 | [diff] [blame] | 697 | DEBUG ((EFI_D_ERROR, "String length mismatch b/w DT Bootdevice string" |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 698 | " (%d) and expected Bootdevice strings (%d)\n", |
| 699 | DevNodeBootDevLen, AsciiStrLen (BootDevBuf))); |
| 700 | } |
Mukesh Ojha | 323c940 | 2017-10-03 23:49:05 +0530 | [diff] [blame] | 701 | } |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 702 | } |
Bhanuprakash Modem | 0450e9b | 2017-06-09 11:58:51 +0530 | [diff] [blame] | 703 | |
Mayank Grover | 546377b | 2019-04-12 15:52:10 +0530 | [diff] [blame] | 704 | if (BootDevBuf) { |
| 705 | FreePool (BootDevBuf); |
| 706 | } |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 707 | BootDevBuf = NULL; |
| 708 | return Status; |
Bhanuprakash Modem | 0450e9b | 2017-06-09 11:58:51 +0530 | [diff] [blame] | 709 | } |