Kaushal Kumar | f6e9aa5 | 2019-11-19 18:52:38 +0530 | [diff] [blame] | 1 | /* Copyright (c) 2015-2020, 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> |
Parth Dixit | 3f4e4aa | 2019-05-14 17:08:46 +0530 | [diff] [blame] | 42 | #include <Library/PartialGoods.h> |
lijuang | c8803a6 | 2020-02-10 15:23:51 +0800 | [diff] [blame] | 43 | #include <Library/FdtRw.h> |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 44 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 45 | #define NUM_SPLASHMEM_PROP_ELEM 4 |
Mayank Grover | abfc353 | 2018-05-03 14:52:32 +0530 | [diff] [blame] | 46 | #define DEFAULT_CELL_SIZE 2 |
Elliot Berman | 78ecae4 | 2019-10-29 09:35:08 -0700 | [diff] [blame] | 47 | #define NUM_RNG_SEED_WORDS 512 |
Aravind Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 48 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 49 | STATIC struct FstabNode FstabTable = {"/firmware/android/fstab", "dev", |
| 50 | "/soc/"}; |
Mayank Grover | 546377b | 2019-04-12 15:52:10 +0530 | [diff] [blame] | 51 | STATIC struct FstabNode DynamicFstabTable = {"/firmware/android/fstab", |
| 52 | "status", |
| 53 | ""}; |
Aravind Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 54 | STATIC struct DisplaySplashBufferInfo splashBuf; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 55 | STATIC UINTN splashBufSize = sizeof (splashBuf); |
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 | STATIC VOID |
| 58 | PrintSplashMemInfo (CONST CHAR8 *data, INT32 datalen) |
Aravind Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 59 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 60 | UINT32 i, val[NUM_SPLASHMEM_PROP_ELEM] = {0}; |
Aravind Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 61 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 62 | for (i = 0; (i < NUM_SPLASHMEM_PROP_ELEM) && datalen; i++) { |
| 63 | memcpy (&val[i], data, sizeof (UINT32)); |
| 64 | val[i] = fdt32_to_cpu (val[i]); |
| 65 | data += sizeof (UINT32); |
| 66 | datalen -= sizeof (UINT32); |
| 67 | } |
Aravind Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 68 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 69 | DEBUG ((EFI_D_VERBOSE, "reg = <0x%08x 0x%08x 0x%08x 0x%08x>\n", val[0], |
| 70 | val[1], val[2], val[3])); |
Aravind Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 73 | STATIC EFI_STATUS |
Mukesh Ojha | 7495e91 | 2020-06-22 20:40:05 +0530 | [diff] [blame] | 74 | ValidateDdrRankChannel (struct ddr_details_entry_info *DdrInfo) |
| 75 | { |
| 76 | if (DdrInfo->num_channels > MAX_CHANNELS) { |
| 77 | DEBUG ((EFI_D_ERROR, "ERROR: Number of channels is over the limit\n")); |
| 78 | return EFI_INVALID_PARAMETER; |
| 79 | } |
| 80 | |
| 81 | for (UINT8 Chan = 0; Chan < DdrInfo->num_channels; Chan++) { |
| 82 | if (DdrInfo->num_ranks[Chan] > MAX_RANKS) { |
| 83 | DEBUG ((EFI_D_ERROR, "ERROR: Number of ranks is over the limit\n")); |
| 84 | return EFI_INVALID_PARAMETER; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return EFI_SUCCESS; |
| 89 | } |
| 90 | |
| 91 | STATIC EFI_STATUS |
Kaushal Kumar | f6e9aa5 | 2019-11-19 18:52:38 +0530 | [diff] [blame] | 92 | GetDDRInfo (struct ddr_details_entry_info *DdrInfo, |
| 93 | UINT64 *Revision) |
Jeevan Shriram | f8fa291 | 2018-11-02 13:16:47 -0700 | [diff] [blame] | 94 | { |
| 95 | EFI_DDRGETINFO_PROTOCOL *DdrInfoIf; |
Jeevan Shriram | f8fa291 | 2018-11-02 13:16:47 -0700 | [diff] [blame] | 96 | EFI_STATUS Status; |
| 97 | |
| 98 | Status = gBS->LocateProtocol (&gEfiDDRGetInfoProtocolGuid, NULL, |
| 99 | (VOID **)&DdrInfoIf); |
| 100 | if (Status != EFI_SUCCESS) { |
| 101 | DEBUG ((EFI_D_VERBOSE, |
Kaushal Kumar | f6e9aa5 | 2019-11-19 18:52:38 +0530 | [diff] [blame] | 102 | "INFO: Unable to get DDR Info protocol:%r\n", |
Jeevan Shriram | f8fa291 | 2018-11-02 13:16:47 -0700 | [diff] [blame] | 103 | Status)); |
| 104 | return Status; |
| 105 | } |
| 106 | |
Kaushal Kumar | f6e9aa5 | 2019-11-19 18:52:38 +0530 | [diff] [blame] | 107 | Status = DdrInfoIf->GetDDRDetails (DdrInfoIf, DdrInfo); |
Jeevan Shriram | f8fa291 | 2018-11-02 13:16:47 -0700 | [diff] [blame] | 108 | if (EFI_ERROR (Status)) { |
Parth Dixit | 489de78 | 2019-04-22 17:18:21 +0530 | [diff] [blame] | 109 | DEBUG ((EFI_D_ERROR, "INFO: GetDDR details failed\n")); |
| 110 | return Status; |
Jeevan Shriram | f8fa291 | 2018-11-02 13:16:47 -0700 | [diff] [blame] | 111 | } |
Komal Bajaj | dd943e0 | 2020-05-05 18:22:39 +0530 | [diff] [blame] | 112 | |
Kaushal Kumar | f6e9aa5 | 2019-11-19 18:52:38 +0530 | [diff] [blame] | 113 | *Revision = DdrInfoIf->Revision; |
| 114 | DEBUG ((EFI_D_VERBOSE, "DDR Header Revision =0x%x\n", *Revision)); |
Jeevan Shriram | f8fa291 | 2018-11-02 13:16:47 -0700 | [diff] [blame] | 115 | return Status; |
| 116 | } |
| 117 | |
| 118 | STATIC EFI_STATUS |
Elliot Berman | 78ecae4 | 2019-10-29 09:35:08 -0700 | [diff] [blame] | 119 | GetRandomSeed (UINT64 *RandomSeed) |
Runmin Wang | 8ecc3f6 | 2016-12-09 14:03:05 -0800 | [diff] [blame] | 120 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 121 | EFI_QCOM_RNG_PROTOCOL *RngIf; |
| 122 | EFI_STATUS Status; |
Runmin Wang | 8ecc3f6 | 2016-12-09 14:03:05 -0800 | [diff] [blame] | 123 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 124 | Status = gBS->LocateProtocol (&gQcomRngProtocolGuid, NULL, (VOID **)&RngIf); |
| 125 | if (Status != EFI_SUCCESS) { |
| 126 | DEBUG ((EFI_D_VERBOSE, |
Elliot Berman | 78ecae4 | 2019-10-29 09:35:08 -0700 | [diff] [blame] | 127 | "Error locating PRNG protocol. Fail to generate random seed:%r\n", |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 128 | Status)); |
| 129 | return Status; |
| 130 | } |
Runmin Wang | 8ecc3f6 | 2016-12-09 14:03:05 -0800 | [diff] [blame] | 131 | |
Jeevan Shriram | f039efb | 2018-08-24 12:42:23 -0700 | [diff] [blame] | 132 | Status = RngIf->GetRNG (RngIf, |
| 133 | &gEfiRNGAlgRawGuid, |
| 134 | sizeof (UINTN), |
Elliot Berman | 78ecae4 | 2019-10-29 09:35:08 -0700 | [diff] [blame] | 135 | (UINT8 *)RandomSeed); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 136 | if (Status != EFI_SUCCESS) { |
| 137 | DEBUG ((EFI_D_VERBOSE, |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 138 | "Error getting PRNG random number. Fail to generate Kaslr seed:%r\n", |
| 139 | Status)); |
Elliot Berman | 78ecae4 | 2019-10-29 09:35:08 -0700 | [diff] [blame] | 140 | *RandomSeed = 0; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 141 | return Status; |
| 142 | } |
Runmin Wang | 8ecc3f6 | 2016-12-09 14:03:05 -0800 | [diff] [blame] | 143 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 144 | return Status; |
Runmin Wang | 8ecc3f6 | 2016-12-09 14:03:05 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 147 | STATIC EFI_STATUS |
| 148 | UpdateSplashMemInfo (VOID *fdt) |
Aravind Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 149 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 150 | EFI_STATUS Status; |
| 151 | CONST struct fdt_property *Prop = NULL; |
| 152 | INT32 PropLen = 0; |
| 153 | INT32 ret = 0; |
| 154 | UINT32 offset; |
| 155 | CHAR8 *tmp = NULL; |
| 156 | UINT32 CONST SplashMemPropSize = NUM_SPLASHMEM_PROP_ELEM * sizeof (UINT32); |
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 | Status = |
| 159 | gRT->GetVariable ((CHAR16 *)L"DisplaySplashBufferInfo", |
| 160 | &gQcomTokenSpaceGuid, NULL, &splashBufSize, &splashBuf); |
| 161 | if (Status != EFI_SUCCESS) { |
| 162 | DEBUG ((EFI_D_ERROR, "Unable to get splash buffer info, %r\n", Status)); |
| 163 | goto error; |
| 164 | } |
Aravind Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 165 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 166 | DEBUG ((EFI_D_VERBOSE, "Version=%d\nAddr=0x%08x\nSize=0x%08x\n", |
| 167 | splashBuf.uVersion, splashBuf.uFrameAddr, splashBuf.uFrameSize)); |
Aravind Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 168 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 169 | /* Get offset of the splash memory reservation node */ |
lijuang | c8803a6 | 2020-02-10 15:23:51 +0800 | [diff] [blame] | 170 | ret = FdtPathOffset (fdt, "/reserved-memory/splash_region"); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 171 | if (ret < 0) { |
| 172 | DEBUG ((EFI_D_ERROR, "ERROR: Could not get splash memory region node\n")); |
| 173 | return EFI_NOT_FOUND; |
| 174 | } |
| 175 | offset = ret; |
| 176 | DEBUG ((EFI_D_VERBOSE, "FB mem node name: %a\n", |
| 177 | fdt_get_name (fdt, offset, NULL))); |
Aravind Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 178 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 179 | /* Get the property that specifies the splash memory details */ |
| 180 | Prop = fdt_get_property (fdt, offset, "reg", &PropLen); |
| 181 | if (!Prop) { |
| 182 | DEBUG ((EFI_D_ERROR, "ERROR: Could not find the splash reg property\n")); |
| 183 | return EFI_NOT_FOUND; |
| 184 | } |
Aravind Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 185 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 186 | /* |
| 187 | * The format of the "reg" field is as follows: |
| 188 | * <0x0 FBAddress 0x0 FBSize> |
| 189 | * The expected size of this property is 4 * sizeof(UINT32) |
| 190 | */ |
| 191 | if (PropLen != SplashMemPropSize) { |
| 192 | DEBUG ( |
| 193 | (EFI_D_ERROR, |
| 194 | "ERROR: splash mem reservation node size. Expected: %d, Actual: %d\n", |
| 195 | SplashMemPropSize, PropLen)); |
| 196 | return EFI_BAD_BUFFER_SIZE; |
| 197 | } |
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 | DEBUG ((EFI_D_VERBOSE, "Splash memory region before updating:\n")); |
| 200 | PrintSplashMemInfo (Prop->data, PropLen); |
Aravind Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 201 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 202 | /* First, update the FBAddress */ |
| 203 | if (CHECK_ADD64 ((UINT64)Prop->data, sizeof (UINT32))) { |
Mukesh Ojha | 82911f8 | 2018-04-30 16:45:07 +0530 | [diff] [blame] | 204 | DEBUG ((EFI_D_ERROR, "ERROR: integer Overflow while updating FBAddress")); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 205 | return EFI_BAD_BUFFER_SIZE; |
| 206 | } |
| 207 | tmp = (CHAR8 *)Prop->data + sizeof (UINT32); |
| 208 | splashBuf.uFrameAddr = cpu_to_fdt32 (splashBuf.uFrameAddr); |
| 209 | memcpy (tmp, &splashBuf.uFrameAddr, sizeof (UINT32)); |
Aravind Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 210 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 211 | /* Next, update the FBSize */ |
| 212 | if (CHECK_ADD64 ((UINT64)tmp, (2 * sizeof (UINT32)))) { |
Mukesh Ojha | 82911f8 | 2018-04-30 16:45:07 +0530 | [diff] [blame] | 213 | DEBUG ((EFI_D_ERROR, "ERROR: integer Overflow while updating FBSize")); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 214 | return EFI_BAD_BUFFER_SIZE; |
| 215 | } |
| 216 | tmp += (2 * sizeof (UINT32)); |
| 217 | splashBuf.uFrameSize = cpu_to_fdt32 (splashBuf.uFrameSize); |
| 218 | memcpy (tmp, &splashBuf.uFrameSize, sizeof (UINT32)); |
Aravind Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 219 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 220 | /* Update the property value in place */ |
| 221 | ret = fdt_setprop_inplace (fdt, offset, "reg", Prop->data, PropLen); |
| 222 | if (ret < 0) { |
| 223 | DEBUG ((EFI_D_ERROR, "ERROR: Could not update splash mem info\n")); |
| 224 | return EFI_NO_MAPPING; |
| 225 | } |
Aravind Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 226 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 227 | DEBUG ((EFI_D_VERBOSE, "Splash memory region after updating:\n")); |
| 228 | PrintSplashMemInfo (Prop->data, PropLen); |
Aravind Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 229 | error: |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 230 | return Status; |
Aravind Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 231 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 232 | |
Yahui Wang | 6e86ac5 | 2020-07-13 17:51:17 +0800 | [diff] [blame^] | 233 | STATIC EFI_STATUS |
| 234 | UpdateDemuraRegion (VOID *fdt, CONST CHAR8 *Path, |
| 235 | UINT32 HFCAddr, UINT32 HFCSize) |
| 236 | { |
| 237 | EFI_STATUS Status = EFI_SUCCESS; |
| 238 | UINT32 DemuraInfoSize = 4 * sizeof (UINT32); |
| 239 | CONST struct fdt_property *Prop = NULL; |
| 240 | INT32 PropLen = 0; |
| 241 | CHAR8 *tmp = NULL; |
| 242 | INT32 ret = 0; |
| 243 | UINT32 offset = 0; |
| 244 | |
| 245 | if (Path != NULL) |
| 246 | { |
| 247 | ret = FdtPathOffset (fdt, Path); |
| 248 | if (ret < 0) { |
| 249 | /* Just return success if demura node not exists */ |
| 250 | return EFI_SUCCESS; |
| 251 | } |
| 252 | |
| 253 | offset = (UINT32)ret; |
| 254 | Prop = fdt_get_property (fdt, offset, "reg", &PropLen); |
| 255 | |
| 256 | if (!Prop) { |
| 257 | DEBUG ((EFI_D_WARN, "Could not find the demura reg property\n")); |
| 258 | Status = EFI_NOT_FOUND; |
| 259 | } else if (PropLen < DemuraInfoSize) { |
| 260 | DEBUG ((EFI_D_WARN, "Invalid demura node size\n")); |
| 261 | Status = EFI_INVALID_PARAMETER; |
| 262 | } else { |
| 263 | /* First, update the demura HFC Address */ |
| 264 | tmp = (CHAR8 *)Prop->data + sizeof (UINT32); |
| 265 | HFCAddr = cpu_to_fdt32 (HFCAddr); |
| 266 | memcpy (tmp, &HFCAddr, sizeof (UINT32)); |
| 267 | |
| 268 | /* Next, update the demura HFC Size */ |
| 269 | tmp += (2 * sizeof (UINT32)); |
| 270 | HFCSize = cpu_to_fdt32 (HFCSize); |
| 271 | memcpy (tmp, &HFCSize, sizeof (UINT32)); |
| 272 | |
| 273 | /* Update the property value in place */ |
| 274 | ret = fdt_setprop_inplace (fdt, offset, "reg", Prop->data, PropLen); |
| 275 | if (ret < 0) { |
| 276 | DEBUG ((EFI_D_WARN, "Could not update demura info\n")); |
| 277 | Status = EFI_NO_MAPPING; |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | return Status; |
| 283 | } |
| 284 | |
| 285 | STATIC EFI_STATUS |
| 286 | UpdateDemuraPanelID (VOID *fdt, CONST CHAR8 *Path, UINT64 PanelID) |
| 287 | { |
| 288 | EFI_STATUS Status = EFI_SUCCESS; |
| 289 | UINT32 PanelIDSize = sizeof (UINT64); |
| 290 | CONST struct fdt_property *Prop = NULL; |
| 291 | INT32 PropLen = 0; |
| 292 | CHAR8 *tmp = NULL; |
| 293 | INT32 ret = 0; |
| 294 | UINT32 offset = 0; |
| 295 | |
| 296 | if (Path != NULL) |
| 297 | { |
| 298 | /* Get offset of the display node */ |
| 299 | ret = FdtPathOffset (fdt, Path); |
| 300 | if (ret < 0) { |
| 301 | /* Just return success if display node not exists */ |
| 302 | return EFI_SUCCESS; |
| 303 | } |
| 304 | |
| 305 | offset = (UINT32)ret; |
| 306 | Prop = fdt_get_property (fdt, offset, "qcom,demura-panel-id", &PropLen); |
| 307 | |
| 308 | if (!Prop) { |
| 309 | DEBUG ((EFI_D_WARN, "Could not find the panel id property\n")); |
| 310 | Status = EFI_NOT_FOUND; |
| 311 | } else if (PropLen < PanelIDSize) { |
| 312 | DEBUG ((EFI_D_WARN, "Invalid panel ID size\n")); |
| 313 | Status = EFI_INVALID_PARAMETER; |
| 314 | } else { |
| 315 | /* Update panel id */ |
| 316 | tmp = (CHAR8 *)Prop->data; |
| 317 | PanelID = fdt64_to_cpu (PanelID); |
| 318 | memcpy (tmp, &PanelID, sizeof (UINT64)); |
| 319 | |
| 320 | /* Update the property value in place */ |
| 321 | ret = fdt_setprop_inplace (fdt, |
| 322 | offset, |
| 323 | "qcom,demura-panel-id", |
| 324 | Prop->data, |
| 325 | PropLen); |
| 326 | if (ret < 0) { |
| 327 | DEBUG ((EFI_D_WARN, "Could not update demura panel id\n")); |
| 328 | Status = EFI_NO_MAPPING; |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | return Status; |
| 334 | } |
| 335 | |
| 336 | STATIC EFI_STATUS |
| 337 | UpdateDemuraInfo (VOID *fdt) |
| 338 | { |
| 339 | EFI_STATUS Status = EFI_SUCCESS; |
| 340 | struct DisplayDemuraInfoType DemuraInfo; |
| 341 | UINTN DemuraInfoSize = sizeof (DemuraInfo); |
| 342 | |
| 343 | memset (&DemuraInfo, 0, DemuraInfoSize); |
| 344 | |
| 345 | Status = gRT->GetVariable ((CHAR16 *)L"DisplayDemuraInfo", |
| 346 | &gQcomTokenSpaceGuid, |
| 347 | NULL, |
| 348 | &DemuraInfoSize, |
| 349 | &DemuraInfo); |
| 350 | if ((Status == EFI_SUCCESS) && |
| 351 | (DemuraInfo.Version > 0)) { |
| 352 | /* Update demura 0 region */ |
| 353 | if ((DemuraInfo.Demura0HFCAddr != 0) && |
| 354 | (DemuraInfo.Demura0HFCSize != 0)) { |
| 355 | UpdateDemuraRegion(fdt, |
| 356 | "/reserved-memory/demura_region_0", |
| 357 | DemuraInfo.Demura0HFCAddr, |
| 358 | DemuraInfo.Demura0HFCSize); |
| 359 | } |
| 360 | |
| 361 | /* Update demura 1 region */ |
| 362 | if ((DemuraInfo.Demura1HFCAddr != 0) && |
| 363 | (DemuraInfo.Demura1HFCSize != 0)) { |
| 364 | UpdateDemuraRegion(fdt, |
| 365 | "/reserved-memory/demura_region_1", |
| 366 | DemuraInfo.Demura1HFCAddr, |
| 367 | DemuraInfo.Demura1HFCSize); |
| 368 | } |
| 369 | |
| 370 | /* Update demura 0 panel id */ |
| 371 | if (DemuraInfo.Demura0PanelID != 0) { |
| 372 | UpdateDemuraPanelID(fdt, |
| 373 | "/soc/qcom,dsi-display-primary", |
| 374 | DemuraInfo.Demura0PanelID); |
| 375 | } |
| 376 | |
| 377 | /* Update demura 1 panel id */ |
| 378 | if (DemuraInfo.Demura1PanelID != 0) { |
| 379 | |
| 380 | UpdateDemuraPanelID(fdt, |
| 381 | "/soc/qcom,dsi-display-secondary", |
| 382 | DemuraInfo.Demura1PanelID); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | return Status; |
| 387 | } |
| 388 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 389 | UINT32 |
| 390 | fdt_check_header_ext (VOID *fdt) |
Runmin Wang | e74d804 | 2016-07-19 18:09:07 -0700 | [diff] [blame] | 391 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 392 | UINT64 fdt_start, fdt_end; |
| 393 | UINT32 sum; |
| 394 | fdt_start = (UINT64)fdt; |
Runmin Wang | e74d804 | 2016-07-19 18:09:07 -0700 | [diff] [blame] | 395 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 396 | if (fdt_start + fdt_totalsize (fdt) <= fdt_start) { |
| 397 | return FDT_ERR_BADOFFSET; |
| 398 | } |
| 399 | fdt_end = fdt_start + fdt_totalsize (fdt); |
Runmin Wang | e74d804 | 2016-07-19 18:09:07 -0700 | [diff] [blame] | 400 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 401 | if (!(sum = ADD_OF (fdt_off_dt_struct (fdt), fdt_size_dt_struct (fdt)))) { |
| 402 | return FDT_ERR_BADOFFSET; |
| 403 | } else { |
| 404 | if (CHECK_ADD64 (fdt_start, sum)) |
| 405 | return FDT_ERR_BADOFFSET; |
| 406 | else if (fdt_start + sum > fdt_end) |
| 407 | return FDT_ERR_BADOFFSET; |
| 408 | } |
| 409 | if (!(sum = ADD_OF (fdt_off_dt_strings (fdt), fdt_size_dt_strings (fdt)))) { |
| 410 | return FDT_ERR_BADOFFSET; |
| 411 | } else { |
| 412 | if (CHECK_ADD64 (fdt_start, sum)) |
| 413 | return FDT_ERR_BADOFFSET; |
| 414 | else if (fdt_start + sum > fdt_end) |
| 415 | return FDT_ERR_BADOFFSET; |
| 416 | } |
| 417 | if (fdt_start + fdt_off_mem_rsvmap (fdt) > fdt_end) |
| 418 | return FDT_ERR_BADOFFSET; |
| 419 | return 0; |
Runmin Wang | e74d804 | 2016-07-19 18:09:07 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Jeevan Shriram | 27c8faa | 2017-09-27 12:05:53 -0700 | [diff] [blame] | 422 | STATIC |
Jeevan Shriram | 067bbb5 | 2018-05-08 17:48:47 -0700 | [diff] [blame] | 423 | VOID |
| 424 | UpdateGranuleInfo (VOID *fdt) |
| 425 | { |
| 426 | EFI_STATUS Status = EFI_SUCCESS; |
Bhanuprakash Modem | 7ee0312 | 2018-06-12 19:47:36 +0530 | [diff] [blame] | 427 | INT32 GranuleNodeOffset; |
Jeevan Shriram | 067bbb5 | 2018-05-08 17:48:47 -0700 | [diff] [blame] | 428 | UINT32 GranuleSize; |
| 429 | INT32 Ret; |
| 430 | |
Parth Dixit | fc028d9 | 2019-04-22 15:55:46 +0530 | [diff] [blame] | 431 | Status = GetGranuleSize (&GranuleSize); |
| 432 | if (EFI_ERROR (Status)) { |
| 433 | DEBUG ((EFI_D_VERBOSE, |
| 434 | "Unable to get Granule Size, Status = %r\r\n", |
| 435 | Status)); |
Bhanuprakash Modem | 7ee0312 | 2018-06-12 19:47:36 +0530 | [diff] [blame] | 436 | return; |
| 437 | } |
| 438 | |
lijuang | c8803a6 | 2020-02-10 15:23:51 +0800 | [diff] [blame] | 439 | GranuleNodeOffset = FdtPathOffset (fdt, "/mem-offline"); |
Parth Dixit | fc028d9 | 2019-04-22 15:55:46 +0530 | [diff] [blame] | 440 | if (GranuleNodeOffset < 0) { |
| 441 | DEBUG ((EFI_D_VERBOSE, "INFO: Could not find mem-offline node.\n")); |
Jeevan Shriram | 067bbb5 | 2018-05-08 17:48:47 -0700 | [diff] [blame] | 442 | return; |
| 443 | } |
| 444 | |
lijuang | c8803a6 | 2020-02-10 15:23:51 +0800 | [diff] [blame] | 445 | FdtPropUpdateFunc (fdt, GranuleNodeOffset, "granule", |
| 446 | GranuleSize, fdt_setprop_u32, Ret); |
Jeevan Shriram | 067bbb5 | 2018-05-08 17:48:47 -0700 | [diff] [blame] | 447 | if (Ret) { |
Parth Dixit | fc028d9 | 2019-04-22 15:55:46 +0530 | [diff] [blame] | 448 | DEBUG ((EFI_D_ERROR, "INFO: Granule size update failed.\n")); |
Jeevan Shriram | 067bbb5 | 2018-05-08 17:48:47 -0700 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
| 452 | STATIC |
Jeevan Shriram | 27c8faa | 2017-09-27 12:05:53 -0700 | [diff] [blame] | 453 | EFI_STATUS |
Mayank Grover | abfc353 | 2018-05-03 14:52:32 +0530 | [diff] [blame] | 454 | QueryMemoryCellSize (IN VOID *Fdt, OUT UINT32 *MemoryCellLen) |
| 455 | { |
| 456 | INT32 RootOffset; |
| 457 | INT32 PropLen; |
| 458 | UINT32 AddrCellSize = 0; |
| 459 | UINT32 SizeCellSize = 0; |
| 460 | UINT32 *Prop = NULL; |
| 461 | |
| 462 | RootOffset = fdt_path_offset (Fdt, "/"); |
| 463 | if (RootOffset < 0) { |
| 464 | DEBUG ((EFI_D_ERROR, "Error finding root offset\n")); |
| 465 | return EFI_NOT_FOUND; |
| 466 | } |
| 467 | |
| 468 | /* Find address-cells size */ |
| 469 | Prop = (UINT32 *) fdt_getprop (Fdt, RootOffset, "#address-cells", &PropLen); |
| 470 | if (Prop && |
| 471 | PropLen > 0) { |
| 472 | AddrCellSize = fdt32_to_cpu (*Prop); |
| 473 | } else { |
| 474 | DEBUG ((EFI_D_ERROR, "Error finding #address-cells property\n")); |
| 475 | return EFI_NOT_FOUND; |
| 476 | } |
| 477 | |
| 478 | /* Find size-cells size */ |
| 479 | Prop =(UINT32 *) fdt_getprop (Fdt, RootOffset, "#size-cells", &PropLen); |
| 480 | if (Prop && |
| 481 | PropLen > 0) { |
| 482 | SizeCellSize = fdt32_to_cpu (*Prop); |
| 483 | } else { |
| 484 | DEBUG ((EFI_D_ERROR, "Error finding #size-cells property\n")); |
| 485 | return EFI_NOT_FOUND; |
| 486 | } |
| 487 | |
| 488 | if (AddrCellSize > DEFAULT_CELL_SIZE || |
| 489 | SizeCellSize > DEFAULT_CELL_SIZE || |
| 490 | SizeCellSize == 0 || |
| 491 | AddrCellSize == 0) { |
| 492 | DEBUG ((EFI_D_ERROR, "Error unsupported cell size value: #address-cell %d" \ |
| 493 | "#size-cell\n", AddrCellSize, SizeCellSize)); |
| 494 | return EFI_INVALID_PARAMETER; |
| 495 | } |
| 496 | |
| 497 | /* Make sure memory cell size and address cell size are same */ |
| 498 | if (AddrCellSize == SizeCellSize) { |
| 499 | *MemoryCellLen = AddrCellSize; |
| 500 | } else { |
| 501 | DEBUG ((EFI_D_ERROR, "Mismatch memory address cell and size cell size\n")); |
| 502 | return EFI_INVALID_PARAMETER; |
| 503 | } |
| 504 | |
| 505 | return EFI_SUCCESS; |
| 506 | } |
| 507 | |
| 508 | STATIC |
| 509 | EFI_STATUS |
| 510 | AddMemMap (VOID *Fdt, UINT32 MemNodeOffset, BOOLEAN BootWith32Bit) |
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 | EFI_STATUS Status = EFI_NOT_FOUND; |
| 513 | INT32 ret = 0; |
| 514 | RamPartitionEntry *RamPartitions = NULL; |
| 515 | UINT32 NumPartitions = 0; |
| 516 | UINT32 i = 0; |
Mayank Grover | abfc353 | 2018-05-03 14:52:32 +0530 | [diff] [blame] | 517 | UINT32 MemoryCellLen = 0; |
| 518 | |
| 519 | Status = QueryMemoryCellSize (Fdt, &MemoryCellLen); |
| 520 | if (EFI_ERROR (Status)) { |
| 521 | DEBUG ((EFI_D_ERROR, "ERROR: Not a valid memory node found!\n")); |
| 522 | return Status; |
| 523 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 524 | |
Chandra Sai Chidipudi | ecc6263 | 2020-01-24 15:20:50 +0530 | [diff] [blame] | 525 | Status = ReadRamPartitions (&RamPartitions, &NumPartitions); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 526 | if (EFI_ERROR (Status)) { |
Chandra Sai Chidipudi | ecc6263 | 2020-01-24 15:20:50 +0530 | [diff] [blame] | 527 | DEBUG ((EFI_D_ERROR, "Error returned from ReadRamPartitions %r\n", Status)); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 528 | return Status; |
| 529 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 530 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 531 | DEBUG ((EFI_D_INFO, "RAM Partitions\r\n")); |
| 532 | for (i = 0; i < NumPartitions; i++) { |
| 533 | DEBUG ((EFI_D_INFO, "Add Base: 0x%016lx Available Length: 0x%016lx \n", |
| 534 | RamPartitions[i].Base, RamPartitions[i].AvailableLength)); |
Jeevan Shriram | 27c8faa | 2017-09-27 12:05:53 -0700 | [diff] [blame] | 535 | |
Mayank Grover | abfc353 | 2018-05-03 14:52:32 +0530 | [diff] [blame] | 536 | if (MemoryCellLen == 1) { |
| 537 | ret = dev_tree_add_mem_info (Fdt, MemNodeOffset, RamPartitions[i].Base, |
| 538 | RamPartitions[i].AvailableLength); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 539 | } else { |
Mayank Grover | abfc353 | 2018-05-03 14:52:32 +0530 | [diff] [blame] | 540 | ret = dev_tree_add_mem_infoV64 (Fdt, MemNodeOffset, |
| 541 | RamPartitions[i].Base, |
| 542 | RamPartitions[i].AvailableLength); |
Jeevan Shriram | 27c8faa | 2017-09-27 12:05:53 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 545 | if (ret) { |
| 546 | DEBUG ((EFI_D_ERROR, "Add Base: 0x%016lx Length: 0x%016lx Fail\n", |
| 547 | RamPartitions[i].Base, RamPartitions[i].AvailableLength)); |
| 548 | } |
| 549 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 550 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 551 | FreePool (RamPartitions); |
| 552 | RamPartitions = NULL; |
Chandra Sai Chidipudi | ecc6263 | 2020-01-24 15:20:50 +0530 | [diff] [blame] | 553 | RamPartitionEntries = NULL; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 554 | |
| 555 | return EFI_SUCCESS; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | /* Supporting function of UpdateDeviceTree() |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 559 | * Function first gets the RAM partition table, then passes the pointer to |
| 560 | * AddMemMap() */ |
Jeevan Shriram | 27c8faa | 2017-09-27 12:05:53 -0700 | [diff] [blame] | 561 | STATIC |
| 562 | EFI_STATUS |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 563 | target_dev_tree_mem (VOID *fdt, UINT32 MemNodeOffset, BOOLEAN BootWith32Bit) |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 564 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 565 | EFI_STATUS Status; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 566 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 567 | /* Get Available memory from partition table */ |
| 568 | Status = AddMemMap (fdt, MemNodeOffset, BootWith32Bit); |
Jeevan Shriram | 067bbb5 | 2018-05-08 17:48:47 -0700 | [diff] [blame] | 569 | if (EFI_ERROR (Status)) { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 570 | DEBUG ((EFI_D_ERROR, |
| 571 | "Invalid memory configuration, check memory partition table: %r\n", |
| 572 | Status)); |
Jeevan Shriram | 067bbb5 | 2018-05-08 17:48:47 -0700 | [diff] [blame] | 573 | goto out; |
| 574 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 575 | |
Jeevan Shriram | 067bbb5 | 2018-05-08 17:48:47 -0700 | [diff] [blame] | 576 | UpdateGranuleInfo (fdt); |
| 577 | |
| 578 | out: |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 579 | return Status; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | /* Supporting function of target_dev_tree_mem() |
| 583 | * Function to add the subsequent RAM partition info to the device tree */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 584 | INT32 |
| 585 | 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] | 586 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 587 | STATIC INT32 mem_info_cnt = 0; |
| 588 | INT32 ret = 0; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 589 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 590 | if (!mem_info_cnt) { |
| 591 | /* Replace any other reg prop in the memory node. */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 592 | mem_info_cnt = 1; |
lijuang | c8803a6 | 2020-02-10 15:23:51 +0800 | [diff] [blame] | 593 | FdtPropUpdateFunc (fdt, offset, "reg", addr, fdt_setprop_u32, ret); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 594 | } else { |
| 595 | /* Append the mem info to the reg prop for subsequent nodes. */ |
lijuang | c8803a6 | 2020-02-10 15:23:51 +0800 | [diff] [blame] | 596 | FdtPropUpdateFunc (fdt, offset, "reg", addr, fdt_appendprop_u32, ret); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 597 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 598 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 599 | if (ret) { |
| 600 | DEBUG ( |
| 601 | (EFI_D_ERROR, "Failed to add the memory information addr: %d\n", ret)); |
| 602 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 603 | |
lijuang | c8803a6 | 2020-02-10 15:23:51 +0800 | [diff] [blame] | 604 | FdtPropUpdateFunc (fdt, offset, "reg", size, fdt_appendprop_u32, ret); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 605 | if (ret) { |
| 606 | DEBUG ( |
| 607 | (EFI_D_ERROR, "Failed to add the memory information size: %d\n", ret)); |
| 608 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 609 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 610 | return ret; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 611 | } |
| 612 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 613 | INT32 |
| 614 | 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] | 615 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 616 | STATIC INT32 mem_info_cnt = 0; |
| 617 | INT32 ret = 0; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 618 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 619 | if (!mem_info_cnt) { |
| 620 | /* Replace any other reg prop in the memory node. */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 621 | mem_info_cnt = 1; |
lijuang | c8803a6 | 2020-02-10 15:23:51 +0800 | [diff] [blame] | 622 | FdtPropUpdateFunc (fdt, offset, "reg", addr, fdt_setprop_u64, ret); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 623 | } else { |
| 624 | /* Append the mem info to the reg prop for subsequent nodes. */ |
lijuang | c8803a6 | 2020-02-10 15:23:51 +0800 | [diff] [blame] | 625 | FdtPropUpdateFunc (fdt, offset, "reg", addr, fdt_appendprop_u64, ret); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 626 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 627 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 628 | if (ret) { |
| 629 | DEBUG ( |
| 630 | (EFI_D_ERROR, "Failed to add the memory information addr: %d\n", ret)); |
| 631 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 632 | |
lijuang | c8803a6 | 2020-02-10 15:23:51 +0800 | [diff] [blame] | 633 | FdtPropUpdateFunc (fdt, offset, "reg", size, fdt_appendprop_u64, ret); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 634 | if (ret) { |
| 635 | DEBUG ( |
| 636 | (EFI_D_ERROR, "Failed to add the memory information size: %d\n", ret)); |
| 637 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 638 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 639 | return ret; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | /* Top level function that updates the device tree. */ |
Jeevan Shriram | 27c8faa | 2017-09-27 12:05:53 -0700 | [diff] [blame] | 643 | EFI_STATUS |
| 644 | UpdateDeviceTree (VOID *fdt, |
| 645 | CONST CHAR8 *cmdline, |
| 646 | VOID *ramdisk, |
| 647 | UINT32 RamDiskSize, |
| 648 | BOOLEAN BootWith32Bit) |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 649 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 650 | INT32 ret = 0; |
| 651 | UINT32 offset; |
| 652 | UINT32 PaddSize = 0; |
Elliot Berman | 78ecae4 | 2019-10-29 09:35:08 -0700 | [diff] [blame] | 653 | UINT64 RandomSeed = 0; |
Jeevan Shriram | f8fa291 | 2018-11-02 13:16:47 -0700 | [diff] [blame] | 654 | UINT8 DdrDeviceType; |
Kaushal Kumar | f6e9aa5 | 2019-11-19 18:52:38 +0530 | [diff] [blame] | 655 | /* Single space reserved for chan(0-9) */ |
| 656 | CHAR8 FdtRankProp[] = "ddr_device_rank_ch "; |
| 657 | /* Single spaces reserved for chan(0-9), rank(0-9) */ |
| 658 | CHAR8 FdtHbbProp[] = "ddr_device_hbb_ch _rank "; |
| 659 | struct ddr_details_entry_info *DdrInfo; |
| 660 | UINT64 Revision; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 661 | EFI_STATUS Status; |
lijuang | 768a445 | 2019-09-24 18:37:42 +0800 | [diff] [blame] | 662 | UINT64 UpdateDTStartTime = GetTimerCountms (); |
Elliot Berman | 78ecae4 | 2019-10-29 09:35:08 -0700 | [diff] [blame] | 663 | UINT32 Index; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 664 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 665 | /* Check the device tree header */ |
| 666 | ret = fdt_check_header (fdt) || fdt_check_header_ext (fdt); |
| 667 | if (ret) { |
| 668 | DEBUG ((EFI_D_ERROR, "ERROR: Invalid device tree header ...\n")); |
| 669 | return EFI_NOT_FOUND; |
| 670 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 671 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 672 | /* Add padding to make space for new nodes and properties. */ |
lijuang | fd6fa91 | 2017-11-16 18:08:22 +0800 | [diff] [blame] | 673 | PaddSize = ADD_OF (fdt_totalsize (fdt), |
| 674 | DTB_PAD_SIZE + AsciiStrLen (cmdline)); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 675 | if (!PaddSize) { |
Mukesh Ojha | 82911f8 | 2018-04-30 16:45:07 +0530 | [diff] [blame] | 676 | DEBUG ((EFI_D_ERROR, "ERROR: Integer Overflow: fdt size = %u\n", |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 677 | fdt_totalsize (fdt))); |
| 678 | return EFI_BAD_BUFFER_SIZE; |
| 679 | } |
| 680 | ret = fdt_open_into (fdt, fdt, PaddSize); |
| 681 | if (ret != 0) { |
| 682 | DEBUG ((EFI_D_ERROR, "ERROR: Failed to move/resize dtb buffer ...\n")); |
| 683 | return EFI_BAD_BUFFER_SIZE; |
| 684 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 685 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 686 | /* Get offset of the memory node */ |
lijuang | c8803a6 | 2020-02-10 15:23:51 +0800 | [diff] [blame] | 687 | ret = FdtPathOffset (fdt, "/memory"); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 688 | if (ret < 0) { |
| 689 | DEBUG ((EFI_D_ERROR, "ERROR: Could not find memory node ...\n")); |
| 690 | return EFI_NOT_FOUND; |
| 691 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 692 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 693 | offset = ret; |
| 694 | Status = target_dev_tree_mem (fdt, offset, BootWith32Bit); |
| 695 | if (Status != EFI_SUCCESS) { |
| 696 | DEBUG ((EFI_D_ERROR, "ERROR: Cannot update memory node\n")); |
| 697 | return Status; |
| 698 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 699 | |
Kaushal Kumar | f6e9aa5 | 2019-11-19 18:52:38 +0530 | [diff] [blame] | 700 | DdrInfo = AllocateZeroPool (sizeof (struct ddr_details_entry_info)); |
| 701 | if (DdrInfo == NULL) { |
| 702 | DEBUG ((EFI_D_ERROR, "DDR Info Buffer: Out of resources\n")); |
| 703 | return EFI_OUT_OF_RESOURCES; |
| 704 | } |
| 705 | Status = GetDDRInfo (DdrInfo, &Revision); |
Jeevan Shriram | f8fa291 | 2018-11-02 13:16:47 -0700 | [diff] [blame] | 706 | if (Status == EFI_SUCCESS) { |
Kaushal Kumar | f6e9aa5 | 2019-11-19 18:52:38 +0530 | [diff] [blame] | 707 | DdrDeviceType = DdrInfo->device_type; |
| 708 | DEBUG ((EFI_D_VERBOSE, "DDR deviceType:%d\n", DdrDeviceType)); |
| 709 | |
lijuang | c8803a6 | 2020-02-10 15:23:51 +0800 | [diff] [blame] | 710 | FdtPropUpdateFunc (fdt, offset, (CONST char *)"ddr_device_type", |
| 711 | (UINT32)DdrDeviceType, fdt_appendprop_u32, ret); |
Jeevan Shriram | f8fa291 | 2018-11-02 13:16:47 -0700 | [diff] [blame] | 712 | if (ret) { |
| 713 | DEBUG ((EFI_D_ERROR, |
Kaushal Kumar | f6e9aa5 | 2019-11-19 18:52:38 +0530 | [diff] [blame] | 714 | "ERROR: Cannot update memory node [ddr_device_type]:0x%x\n", |
Jeevan Shriram | f8fa291 | 2018-11-02 13:16:47 -0700 | [diff] [blame] | 715 | ret)); |
| 716 | } else { |
| 717 | DEBUG ((EFI_D_VERBOSE, "ddr_device_type is added to memory node\n")); |
| 718 | } |
Kaushal Kumar | f6e9aa5 | 2019-11-19 18:52:38 +0530 | [diff] [blame] | 719 | |
| 720 | if (Revision < EFI_DDRGETINFO_PROTOCOL_REVISION) { |
| 721 | DEBUG ((EFI_D_VERBOSE, |
| 722 | "ddr_device_rank, HBB not supported in Revision=0x%x\n", |
| 723 | Revision)); |
| 724 | } else { |
Mukesh Ojha | 7495e91 | 2020-06-22 20:40:05 +0530 | [diff] [blame] | 725 | Status = ValidateDdrRankChannel (DdrInfo); |
| 726 | if (Status != EFI_SUCCESS) { |
| 727 | goto OutofUpdateRankChannel; |
| 728 | } |
| 729 | |
Kaushal Kumar | f6e9aa5 | 2019-11-19 18:52:38 +0530 | [diff] [blame] | 730 | DEBUG ((EFI_D_VERBOSE, "DdrInfo->num_channels:%d\n", |
| 731 | DdrInfo->num_channels)); |
| 732 | for (UINT8 Chan = 0; Chan < DdrInfo->num_channels; Chan++) { |
| 733 | DEBUG ((EFI_D_VERBOSE, "ddr_device_rank_ch%d:%d\n", |
| 734 | Chan, DdrInfo->num_ranks[Chan])); |
| 735 | AsciiSPrint (FdtRankProp, sizeof (FdtRankProp), |
| 736 | "ddr_device_rank_ch%d", Chan); |
lijuang | c8803a6 | 2020-02-10 15:23:51 +0800 | [diff] [blame] | 737 | FdtPropUpdateFunc (fdt, offset, (CONST char *)FdtRankProp, |
| 738 | (UINT32)DdrInfo->num_ranks[Chan], |
| 739 | fdt_appendprop_u32, ret); |
Kaushal Kumar | f6e9aa5 | 2019-11-19 18:52:38 +0530 | [diff] [blame] | 740 | if (ret) { |
| 741 | DEBUG ((EFI_D_ERROR, |
| 742 | "ERROR: Cannot update memory node ddr_device_rank_ch%d:0x%x\n", |
| 743 | Chan, ret)); |
| 744 | } else { |
| 745 | DEBUG ((EFI_D_VERBOSE, "ddr_device_rank_ch%d added to memory node\n", |
| 746 | Chan)); |
| 747 | } |
| 748 | for (UINT8 Rank = 0; Rank < DdrInfo->num_ranks[Chan]; Rank++) { |
| 749 | DEBUG ((EFI_D_VERBOSE, "ddr_device_hbb_ch%d_rank%d:%d\n", |
| 750 | Chan, Rank, DdrInfo->hbb[Chan][Rank])); |
| 751 | AsciiSPrint (FdtHbbProp, sizeof (FdtHbbProp), |
| 752 | "ddr_device_hbb_ch%d_rank%d", Chan, Rank); |
lijuang | c8803a6 | 2020-02-10 15:23:51 +0800 | [diff] [blame] | 753 | FdtPropUpdateFunc (fdt, offset, (CONST char *)FdtHbbProp, |
| 754 | (UINT32)DdrInfo->hbb[Chan][Rank], |
| 755 | fdt_appendprop_u32, ret); |
Kaushal Kumar | f6e9aa5 | 2019-11-19 18:52:38 +0530 | [diff] [blame] | 756 | if (ret) { |
| 757 | DEBUG ((EFI_D_ERROR, |
| 758 | "ERROR: Cannot update memory node ddr_device_hbb_ch%d_rank%d:0x%x\n", |
| 759 | Chan, Rank, ret)); |
| 760 | } else { |
| 761 | DEBUG ((EFI_D_VERBOSE, |
| 762 | "ddr_device_hbb_ch%d_rank%d added to memory node\n", |
| 763 | Chan, Rank)); |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | } |
Jeevan Shriram | f8fa291 | 2018-11-02 13:16:47 -0700 | [diff] [blame] | 768 | } |
| 769 | |
Mukesh Ojha | 7495e91 | 2020-06-22 20:40:05 +0530 | [diff] [blame] | 770 | OutofUpdateRankChannel: |
| 771 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 772 | UpdateSplashMemInfo (fdt); |
Yahui Wang | 6e86ac5 | 2020-07-13 17:51:17 +0800 | [diff] [blame^] | 773 | UpdateDemuraInfo (fdt); |
Aravind Venkateswaran | c004803 | 2016-07-22 14:28:03 -0700 | [diff] [blame] | 774 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 775 | /* Get offset of the chosen node */ |
lijuang | c8803a6 | 2020-02-10 15:23:51 +0800 | [diff] [blame] | 776 | ret = FdtPathOffset (fdt, "/chosen"); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 777 | if (ret < 0) { |
| 778 | DEBUG ((EFI_D_ERROR, "ERROR: Could not find chosen node ...\n")); |
| 779 | return EFI_NOT_FOUND; |
| 780 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 781 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 782 | offset = ret; |
| 783 | if (cmdline) { |
| 784 | /* Adding the cmdline to the chosen node */ |
lijuang | c8803a6 | 2020-02-10 15:23:51 +0800 | [diff] [blame] | 785 | FdtPropUpdateFunc (fdt, offset, (CONST char *)"bootargs", |
| 786 | (CONST VOID *)cmdline, fdt_appendprop_string, ret); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 787 | if (ret) { |
| 788 | DEBUG ((EFI_D_ERROR, |
| 789 | "ERROR: Cannot update chosen node [bootargs] - 0x%x\n", ret)); |
| 790 | return EFI_LOAD_ERROR; |
| 791 | } |
| 792 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 793 | |
Elliot Berman | 78ecae4 | 2019-10-29 09:35:08 -0700 | [diff] [blame] | 794 | for (Index = 0; Index < NUM_RNG_SEED_WORDS / sizeof (UINT64); Index++) { |
| 795 | Status = GetRandomSeed (&RandomSeed); |
| 796 | if (Status == EFI_SUCCESS) { |
| 797 | |
| 798 | /* Adding the RNG seed to the chosen node */ |
lijuang | c8803a6 | 2020-02-10 15:23:51 +0800 | [diff] [blame] | 799 | FdtPropUpdateFunc (fdt, offset, (CONST CHAR8 *)"rng-seed", |
| 800 | (UINT64)RandomSeed, fdt_appendprop_u64, ret); |
Elliot Berman | 78ecae4 | 2019-10-29 09:35:08 -0700 | [diff] [blame] | 801 | if (ret) { |
| 802 | DEBUG ((EFI_D_ERROR, |
| 803 | "ERROR: Cannot update chosen node [rng-seed] - 0x%x\n", ret)); |
| 804 | break; |
| 805 | } |
| 806 | } else { |
| 807 | DEBUG ((EFI_D_INFO, "ERROR: Cannot generate Random Seed - %r\n", Status)); |
| 808 | break; |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | Status = GetRandomSeed (&RandomSeed); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 813 | if (Status == EFI_SUCCESS) { |
| 814 | /* Adding Kaslr Seed to the chosen node */ |
lijuang | c8803a6 | 2020-02-10 15:23:51 +0800 | [diff] [blame] | 815 | FdtPropUpdateFunc (fdt, offset, (CONST CHAR8 *)"kaslr-seed", |
| 816 | (UINT64)RandomSeed, fdt_appendprop_u64, ret); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 817 | if (ret) { |
| 818 | DEBUG ((EFI_D_INFO, |
| 819 | "ERROR: Cannot update chosen node [kaslr-seed] - 0x%x\n", ret)); |
| 820 | } else { |
lijuang | f2eb1fa | 2019-09-23 14:10:20 +0800 | [diff] [blame] | 821 | DEBUG ((EFI_D_VERBOSE, "kaslr-Seed is added to chosen node\n")); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 822 | } |
| 823 | } else { |
| 824 | DEBUG ((EFI_D_INFO, "ERROR: Cannot generate Kaslr Seed - %r\n", Status)); |
| 825 | } |
Runmin Wang | 8ecc3f6 | 2016-12-09 14:03:05 -0800 | [diff] [blame] | 826 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 827 | if (RamDiskSize) { |
| 828 | /* Adding the initrd-start to the chosen node */ |
lijuang | c8803a6 | 2020-02-10 15:23:51 +0800 | [diff] [blame] | 829 | FdtPropUpdateFunc (fdt, offset, (CONST CHAR8 *)"linux,initrd-start", |
| 830 | (UINT64)ramdisk, fdt_setprop_u64, ret); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 831 | if (ret) { |
| 832 | DEBUG ((EFI_D_ERROR, |
| 833 | "ERROR: Cannot update chosen node [linux,initrd-start] - 0x%x\n", |
| 834 | ret)); |
| 835 | return EFI_NOT_FOUND; |
| 836 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 837 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 838 | /* Adding the initrd-end to the chosen node */ |
lijuang | c8803a6 | 2020-02-10 15:23:51 +0800 | [diff] [blame] | 839 | FdtPropUpdateFunc (fdt, offset, (CONST CHAR8 *)"linux,initrd-end", |
| 840 | (UINT64)ramdisk + RamDiskSize, fdt_setprop_u64, ret); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 841 | if (ret) { |
| 842 | DEBUG ((EFI_D_ERROR, |
| 843 | "ERROR: Cannot update chosen node [linux,initrd-end] - 0x%x\n", |
| 844 | ret)); |
| 845 | return EFI_NOT_FOUND; |
| 846 | } |
| 847 | } |
Bhanuprakash Modem | 0450e9b | 2017-06-09 11:58:51 +0530 | [diff] [blame] | 848 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 849 | /* Update fstab node */ |
| 850 | DEBUG ((EFI_D_VERBOSE, "Start DT fstab node update: %lu ms\n", |
| 851 | GetTimerCountms ())); |
| 852 | UpdateFstabNode (fdt); |
| 853 | DEBUG ((EFI_D_VERBOSE, "End DT fstab node update: %lu ms\n", |
| 854 | GetTimerCountms ())); |
Bhanuprakash Modem | 0450e9b | 2017-06-09 11:58:51 +0530 | [diff] [blame] | 855 | |
Parth Dixit | 3f4e4aa | 2019-05-14 17:08:46 +0530 | [diff] [blame] | 856 | /* Check partial goods*/ |
| 857 | if (FixedPcdGetBool (EnablePartialGoods)) { |
| 858 | ret = UpdatePartialGoodsNode (fdt); |
| 859 | if (ret != EFI_SUCCESS) { |
| 860 | DEBUG ((EFI_D_ERROR, |
| 861 | "Failed to update device tree for partial goods, Status=%r\n", |
| 862 | ret)); |
| 863 | return ret; |
| 864 | } |
| 865 | } |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 866 | fdt_pack (fdt); |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 867 | |
lijuang | 768a445 | 2019-09-24 18:37:42 +0800 | [diff] [blame] | 868 | DEBUG ((EFI_D_INFO, "Update Device Tree total time: %lu ms \n", |
| 869 | GetTimerCountms () - UpdateDTStartTime)); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 870 | return ret; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 871 | } |
Channagoud Kadabi | 9be68a7 | 2016-02-08 15:18:24 -0800 | [diff] [blame] | 872 | |
Bhanuprakash Modem | 0450e9b | 2017-06-09 11:58:51 +0530 | [diff] [blame] | 873 | /* Update device tree for fstab node */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 874 | EFI_STATUS |
| 875 | UpdateFstabNode (VOID *fdt) |
Bhanuprakash Modem | 0450e9b | 2017-06-09 11:58:51 +0530 | [diff] [blame] | 876 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 877 | INT32 ParentOffset = 0; |
| 878 | INT32 SubNodeOffset = 0; |
| 879 | CONST struct fdt_property *Prop = NULL; |
| 880 | INT32 PropLen = 0; |
| 881 | char *NodeName = NULL; |
| 882 | EFI_STATUS Status = EFI_SUCCESS; |
| 883 | CHAR8 *BootDevBuf = NULL; |
| 884 | CHAR8 *ReplaceStr = NULL; |
| 885 | CHAR8 *NextStr = NULL; |
Mayank Grover | 546377b | 2019-04-12 15:52:10 +0530 | [diff] [blame] | 886 | struct FstabNode Table = IsDynamicPartitionSupport () ? DynamicFstabTable |
| 887 | : FstabTable; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 888 | UINT32 DevNodeBootDevLen = 0; |
| 889 | UINT32 Index = 0; |
| 890 | UINT32 PaddingEnd = 0; |
Bhanuprakash Modem | 0450e9b | 2017-06-09 11:58:51 +0530 | [diff] [blame] | 891 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 892 | /* Find the parent node */ |
lijuang | c8803a6 | 2020-02-10 15:23:51 +0800 | [diff] [blame] | 893 | ParentOffset = FdtPathOffset (fdt, Table.ParentNode); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 894 | if (ParentOffset < 0) { |
| 895 | DEBUG ((EFI_D_VERBOSE, "Failed to Get parent node: fstab\terror: %d\n", |
| 896 | ParentOffset)); |
| 897 | return EFI_NOT_FOUND; |
| 898 | } |
| 899 | DEBUG ((EFI_D_VERBOSE, "Node: %a found.\n", |
| 900 | fdt_get_name (fdt, ParentOffset, NULL))); |
Bhanuprakash Modem | 0450e9b | 2017-06-09 11:58:51 +0530 | [diff] [blame] | 901 | |
Mayank Grover | 546377b | 2019-04-12 15:52:10 +0530 | [diff] [blame] | 902 | if (!IsDynamicPartitionSupport ()) { |
| 903 | /* Get boot device type */ |
| 904 | BootDevBuf = AllocateZeroPool (sizeof (CHAR8) * BOOT_DEV_MAX_LEN); |
| 905 | if (BootDevBuf == NULL) { |
| 906 | DEBUG ((EFI_D_ERROR, "Boot device buffer: Out of resources\n")); |
| 907 | return EFI_OUT_OF_RESOURCES; |
| 908 | } |
Bhanuprakash Modem | 0450e9b | 2017-06-09 11:58:51 +0530 | [diff] [blame] | 909 | |
Mayank Grover | 546377b | 2019-04-12 15:52:10 +0530 | [diff] [blame] | 910 | Status = GetBootDevice (BootDevBuf, BOOT_DEV_MAX_LEN); |
| 911 | if (Status != EFI_SUCCESS) { |
| 912 | DEBUG ((EFI_D_ERROR, "Failed to get Boot Device: %r\n", Status)); |
| 913 | FreePool (BootDevBuf); |
| 914 | BootDevBuf = NULL; |
| 915 | return Status; |
| 916 | } |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 917 | } |
Bhanuprakash Modem | 0450e9b | 2017-06-09 11:58:51 +0530 | [diff] [blame] | 918 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 919 | /* Get properties of all sub nodes */ |
| 920 | for (SubNodeOffset = fdt_first_subnode (fdt, ParentOffset); |
| 921 | SubNodeOffset >= 0; |
| 922 | SubNodeOffset = fdt_next_subnode (fdt, SubNodeOffset)) { |
| 923 | Prop = fdt_get_property (fdt, SubNodeOffset, Table.Property, &PropLen); |
| 924 | NodeName = (char *)(uintptr_t)fdt_get_name (fdt, SubNodeOffset, NULL); |
| 925 | if (!Prop) { |
| 926 | DEBUG ((EFI_D_VERBOSE, "Property:%a is not found for sub-node:%a\n", |
| 927 | Table.Property, NodeName)); |
| 928 | } else { |
| 929 | DEBUG ((EFI_D_VERBOSE, "Property:%a found for sub-node:%a\tProperty:%a\n", |
| 930 | Table.Property, NodeName, Prop->data)); |
Bhanuprakash Modem | 0450e9b | 2017-06-09 11:58:51 +0530 | [diff] [blame] | 931 | |
Mayank Grover | 546377b | 2019-04-12 15:52:10 +0530 | [diff] [blame] | 932 | /* For Dynamic partition support disable firmware fstab nodes. */ |
| 933 | if (IsDynamicPartitionSupport ()) { |
| 934 | DEBUG ((EFI_D_VERBOSE, "Disabling node status :%a\n", NodeName)); |
lijuang | c8803a6 | 2020-02-10 15:23:51 +0800 | [diff] [blame] | 935 | Status = FdtSetProp (fdt, SubNodeOffset, Table.Property, |
| 936 | (CONST VOID *)"disabled", |
| 937 | (AsciiStrLen ("disabled") + 1)); |
Mayank Grover | 546377b | 2019-04-12 15:52:10 +0530 | [diff] [blame] | 938 | if (Status) { |
| 939 | DEBUG ((EFI_D_ERROR, "ERROR: Failed to disable Node: %a\n", NodeName)); |
| 940 | } |
| 941 | continue; |
| 942 | } |
| 943 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 944 | /* Pointer to fdt 'dev' property string that needs to update based on the |
| 945 | * 'androidboot.bootdevice' */ |
| 946 | ReplaceStr = (CHAR8 *)Prop->data; |
| 947 | ReplaceStr = AsciiStrStr (ReplaceStr, Table.DevicePathId); |
| 948 | if (!ReplaceStr) { |
| 949 | DEBUG ((EFI_D_VERBOSE, "Update property:%a value is not proper to " |
| 950 | "update for sub-node:%a\n", |
| 951 | Table.Property, NodeName)); |
| 952 | continue; |
| 953 | } |
| 954 | ReplaceStr += AsciiStrLen (Table.DevicePathId); |
| 955 | NextStr = AsciiStrStr ((ReplaceStr + 1), "/"); |
| 956 | DevNodeBootDevLen = NextStr - ReplaceStr; |
| 957 | if (DevNodeBootDevLen >= AsciiStrLen (BootDevBuf)) { |
Mukesh Ojha | 323c940 | 2017-10-03 23:49:05 +0530 | [diff] [blame] | 958 | gBS->CopyMem (ReplaceStr, BootDevBuf, AsciiStrLen (BootDevBuf)); |
| 959 | PaddingEnd = DevNodeBootDevLen - AsciiStrLen (BootDevBuf); |
| 960 | /* Update the property with new value */ |
| 961 | if (PaddingEnd) { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 962 | gBS->CopyMem (ReplaceStr + AsciiStrLen (BootDevBuf), NextStr, |
| 963 | AsciiStrLen (NextStr)); |
| 964 | for (Index = 0; Index < PaddingEnd; Index++) { |
| 965 | ReplaceStr[AsciiStrLen (BootDevBuf) + AsciiStrLen (NextStr) + |
| 966 | Index] = ' '; |
| 967 | } |
Mukesh Ojha | 323c940 | 2017-10-03 23:49:05 +0530 | [diff] [blame] | 968 | } |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 969 | } else { |
Mukesh Ojha | 323c940 | 2017-10-03 23:49:05 +0530 | [diff] [blame] | 970 | DEBUG ((EFI_D_ERROR, "String length mismatch b/w DT Bootdevice string" |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 971 | " (%d) and expected Bootdevice strings (%d)\n", |
| 972 | DevNodeBootDevLen, AsciiStrLen (BootDevBuf))); |
| 973 | } |
Mukesh Ojha | 323c940 | 2017-10-03 23:49:05 +0530 | [diff] [blame] | 974 | } |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 975 | } |
Bhanuprakash Modem | 0450e9b | 2017-06-09 11:58:51 +0530 | [diff] [blame] | 976 | |
Mayank Grover | 546377b | 2019-04-12 15:52:10 +0530 | [diff] [blame] | 977 | if (BootDevBuf) { |
| 978 | FreePool (BootDevBuf); |
| 979 | } |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 980 | BootDevBuf = NULL; |
| 981 | return Status; |
Bhanuprakash Modem | 0450e9b | 2017-06-09 11:58:51 +0530 | [diff] [blame] | 982 | } |