blob: c354f453bbc7866cbb520fbf3a1d1921a85d190b [file] [log] [blame]
Mayank Groverf0fb3792021-05-17 15:21:36 +05301/* Copyright (c) 2015-2018, 2020-2021, The Linux Foundation. All rights reserved.
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08002 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
12 * * Neither the name of The Linux Foundation nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28
Jeevan Shriram17f173d2017-10-24 22:11:07 -070029#include "AutoGen.h"
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080030#include <Board.h>
Jeevan Shriram17f173d2017-10-24 22:11:07 -070031#include <Library/BootImage.h>
32#include <Library/UpdateDeviceTree.h>
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080033#include <Protocol/EFICardInfo.h>
lijuang834ebd22016-09-07 18:27:29 +080034#include <Protocol/EFIPlatformInfoTypes.h>
lijuang6a97bc52016-10-18 17:32:54 +080035
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080036#include <LinuxLoaderLib.h>
37
Channagoud Kadabi539d3072016-02-11 21:34:48 -080038STATIC struct BoardInfo platform_board_info;
39
lijuang24de5342016-09-07 18:09:10 +080040STATIC CONST CHAR8 *DeviceType[] = {
Jeevan Shriram17f173d2017-10-24 22:11:07 -070041 [EMMC] = "EMMC", [UFS] = "UFS", [NAND] = "NAND", [UNKNOWN] = "Unknown",
lijuang24de5342016-09-07 18:09:10 +080042};
43
Chandra Sai Chidipudiecc62632020-01-24 15:20:50 +053044RamPartitionEntry *RamPartitionEntries = NULL;
45
46STATIC EFI_STATUS
Jeevan Shriram17f173d2017-10-24 22:11:07 -070047GetRamPartitions (RamPartitionEntry **RamPartitions, UINT32 *NumPartitions)
48{
Vijay Kumar Pendotic52e80b2016-10-12 16:47:50 +053049
Jeevan Shriram17f173d2017-10-24 22:11:07 -070050 EFI_STATUS Status = EFI_NOT_FOUND;
51 EFI_RAMPARTITION_PROTOCOL *pRamPartProtocol = NULL;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080052
Jeevan Shriram17f173d2017-10-24 22:11:07 -070053 Status = gBS->LocateProtocol (&gEfiRamPartitionProtocolGuid, NULL,
54 (VOID **)&pRamPartProtocol);
55 if (EFI_ERROR (Status) || (pRamPartProtocol == NULL)) {
56 DEBUG ((EFI_D_ERROR,
57 "Locate EFI_RAMPARTITION_Protocol failed, Status = (0x%x)\r\n",
58 Status));
59 return EFI_NOT_FOUND;
60 }
61 Status = pRamPartProtocol->GetRamPartitions (pRamPartProtocol, NULL,
62 NumPartitions);
63 if (Status == EFI_BUFFER_TOO_SMALL) {
Bhanuprakash Modem763cdd52018-11-01 14:49:55 +053064 *RamPartitions = AllocateZeroPool (
65 *NumPartitions * sizeof (RamPartitionEntry));
Jeevan Shriram17f173d2017-10-24 22:11:07 -070066 if (*RamPartitions == NULL)
67 return EFI_OUT_OF_RESOURCES;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080068
Jeevan Shriram17f173d2017-10-24 22:11:07 -070069 Status = pRamPartProtocol->GetRamPartitions (pRamPartProtocol,
70 *RamPartitions, NumPartitions);
71 if (EFI_ERROR (Status) || (*NumPartitions < 1)) {
72 DEBUG ((EFI_D_ERROR, "Failed to get RAM partitions"));
73 FreePool (*RamPartitions);
74 *RamPartitions = NULL;
75 return EFI_NOT_FOUND;
76 }
77 } else {
78 DEBUG ((EFI_D_ERROR, "Error Occured while populating RamPartitions\n"));
79 return EFI_PROTOCOL_ERROR;
80 }
81 return Status;
Vijay Kumar Pendotic52e80b2016-10-12 16:47:50 +053082}
83
Jeevan Shriram17f173d2017-10-24 22:11:07 -070084EFI_STATUS
Chandra Sai Chidipudiecc62632020-01-24 15:20:50 +053085ReadRamPartitions (RamPartitionEntry **RamPartitions, UINT32 *NumPartitions)
86{
87 STATIC UINT32 NumPartitionEntries = 0;
88 EFI_STATUS Status = EFI_SUCCESS;
89
90 if (RamPartitionEntries == NULL) {
Chandra Sai Chidipudi2b350922020-03-10 16:04:54 +053091 NumPartitionEntries = 0;
Chandra Sai Chidipudiecc62632020-01-24 15:20:50 +053092 Status = GetRamPartitions (&RamPartitionEntries, &NumPartitionEntries);
93 if (EFI_ERROR (Status)) {
94 DEBUG ((EFI_D_ERROR, "Error returned from GetRamPartitions %r\n",
95 Status));
96 return Status;
97 }
98 if (!RamPartitionEntries) {
99 DEBUG ((EFI_D_ERROR, "RamPartitions is NULL\n"));
100 return EFI_NOT_FOUND;
101 }
102 }
103
104 *RamPartitions = RamPartitionEntries;
105 *NumPartitions = NumPartitionEntries;
106
107 return Status;
108}
109
110EFI_STATUS
Jeevan Shriram067bbb52018-05-08 17:48:47 -0700111GetGranuleSize (UINT32 *MinPasrGranuleSize)
112{
113 EFI_STATUS Status = EFI_NOT_FOUND;
114 EFI_RAMPARTITION_PROTOCOL *pRamPartProtocol = NULL;
115
116 Status = gBS->LocateProtocol (&gEfiRamPartitionProtocolGuid, NULL,
117 (VOID **)&pRamPartProtocol);
118 if (EFI_ERROR (Status) ||
119 (pRamPartProtocol == NULL)) {
120 DEBUG ((EFI_D_ERROR,
121 "Locate EFI_RAMPARTITION_Protocol failed, Status = %r \n",
122 Status));
123 return Status;
124 }
125
Jeevan Shriram97ec78e2018-06-12 20:31:12 -0700126 if (pRamPartProtocol->Revision < EFI_RAMPARTITION_PROTOCOL_REVISION) {
127 DEBUG ((EFI_D_ERROR, "WARNING: Unsupported EFI_RAMPARTITION_PROTOCOL\n"));
128 return EFI_UNSUPPORTED;
129 }
130
Jeevan Shriram067bbb52018-05-08 17:48:47 -0700131 Status = pRamPartProtocol->GetMinPasrSize (pRamPartProtocol,
132 MinPasrGranuleSize);
133 if (EFI_ERROR (Status)) {
134 DEBUG ((EFI_D_ERROR,
135 "Failed to get MinPasrSize, Status = %r\n",
136 Status));
137 return Status;
138 }
139 return Status;
140}
141
142EFI_STATUS
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700143BaseMem (UINT64 *BaseMemory)
Vijay Kumar Pendotic52e80b2016-10-12 16:47:50 +0530144{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700145 EFI_STATUS Status = EFI_NOT_FOUND;
146 RamPartitionEntry *RamPartitions = NULL;
147 UINT32 NumPartitions = 0;
148 UINT64 SmallestBase;
149 UINT32 i = 0;
Vijay Kumar Pendotic52e80b2016-10-12 16:47:50 +0530150
Chandra Sai Chidipudiecc62632020-01-24 15:20:50 +0530151 Status = ReadRamPartitions (&RamPartitions, &NumPartitions);
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700152 if (EFI_ERROR (Status)) {
Chandra Sai Chidipudiecc62632020-01-24 15:20:50 +0530153 DEBUG ((EFI_D_ERROR, "Error returned from ReadRamPartitions %r\n", Status));
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700154 return Status;
155 }
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700156 SmallestBase = RamPartitions[0].Base;
157 for (i = 0; i < NumPartitions; i++) {
158 if (SmallestBase > RamPartitions[i].Base)
159 SmallestBase = RamPartitions[i].Base;
160 }
161 *BaseMemory = SmallestBase;
162 DEBUG ((EFI_D_INFO, "Memory Base Address: 0x%x\n", *BaseMemory));
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800163
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700164 return Status;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800165}
166
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700167STATIC EFI_STATUS
Jeevan Shriram62997572018-09-20 16:56:21 -0700168GetChipInfo (struct BoardInfo *platform_board_info,
169 EFIChipInfoModemType *ModemType)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800170{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700171 EFI_STATUS Status;
172 EFI_CHIPINFO_PROTOCOL *pChipInfoProtocol;
Saranya Chidura6f047e22018-09-05 10:09:45 +0530173
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700174 Status = gBS->LocateProtocol (&gEfiChipInfoProtocolGuid, NULL,
175 (VOID **)&pChipInfoProtocol);
176 if (EFI_ERROR (Status))
177 return Status;
178 Status = pChipInfoProtocol->GetChipId (pChipInfoProtocol,
179 &platform_board_info->RawChipId);
180 if (EFI_ERROR (Status))
181 return Status;
182 Status = pChipInfoProtocol->GetChipVersion (
183 pChipInfoProtocol, &platform_board_info->ChipVersion);
184 if (EFI_ERROR (Status))
185 return Status;
186 Status = pChipInfoProtocol->GetFoundryId (pChipInfoProtocol,
187 &platform_board_info->FoundryId);
188 if (EFI_ERROR (Status))
189 return Status;
Saranya Chidura6f047e22018-09-05 10:09:45 +0530190 Status = pChipInfoProtocol->GetModemSupport (
Jeevan Shriram62997572018-09-20 16:56:21 -0700191 pChipInfoProtocol, ModemType);
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700192 if (EFI_ERROR (Status))
193 return Status;
Saranya Chidura6f047e22018-09-05 10:09:45 +0530194
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700195 return Status;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800196}
197
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700198STATIC EFI_STATUS
199GetPlatformInfo (struct BoardInfo *platform_board_info)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800200{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700201 EFI_STATUS eResult;
202 EFI_PLATFORMINFO_PROTOCOL *hPlatformInfoProtocol;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800203
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700204 eResult = gBS->LocateProtocol (&gEfiPlatformInfoProtocolGuid, NULL,
205 (VOID **)&hPlatformInfoProtocol);
206 if (eResult != EFI_SUCCESS) {
207 AsciiPrint ("Error: Failed to locate PlatformInfo protocol.\n");
208 goto endtest;
209 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800210
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700211 eResult = hPlatformInfoProtocol->GetPlatformInfo (
212 hPlatformInfoProtocol, &platform_board_info->PlatformInfo);
213 if (eResult != EFI_SUCCESS) {
214 AsciiPrint ("Error: GetPlatformInfo failed.\n");
215 goto endtest;
216 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800217
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700218 if (platform_board_info->PlatformInfo.platform >=
219 EFI_PLATFORMINFO_NUM_TYPES) {
220 AsciiPrint ("Error: Unknown platform type (%d).\n",
221 platform_board_info->PlatformInfo.platform);
222 eResult = EFI_PROTOCOL_ERROR;
223 goto endtest;
224 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800225
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700226 DEBUG ((EFI_D_VERBOSE, "Platform Info : 0x%x\n",
227 platform_board_info->PlatformInfo.platform));
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800228endtest:
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700229 return eResult;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800230}
231
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700232STATIC EFI_STATUS
233GetPmicInfoExt (UINT32 PmicDeviceIndex,
234 EFI_PM_DEVICE_INFO_EXT_TYPE *pmic_info_ext)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800235{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700236 EFI_STATUS Status;
237 EFI_QCOM_PMIC_VERSION_PROTOCOL *pPmicVersionProtocol;
Vijay Kumar Pendoti0be25612016-11-18 18:52:46 +0530238
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700239 Status = gBS->LocateProtocol (&gQcomPmicVersionProtocolGuid, NULL,
240 (VOID **)&pPmicVersionProtocol);
241 if (EFI_ERROR (Status)) {
242 DEBUG ((EFI_D_ERROR, "Error locating pmic protocol: %r\n", Status));
243 return Status;
244 }
Vijay Kumar Pendoti0be25612016-11-18 18:52:46 +0530245
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700246 Status =
247 pPmicVersionProtocol->GetPmicInfoExt (PmicDeviceIndex, pmic_info_ext);
248 if (EFI_ERROR (Status)) {
249 DEBUG ((EFI_D_VERBOSE, "Error getting pmic info ext: %r\n", Status));
250 return Status;
251 }
252 return Status;
Vijay Kumar Pendoti0be25612016-11-18 18:52:46 +0530253}
254
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700255STATIC EFI_STATUS
256GetPmicInfo (UINT32 PmicDeviceIndex,
257 EFI_PM_DEVICE_INFO_TYPE *pmic_info,
258 UINT64 *Revision)
Vijay Kumar Pendoti0be25612016-11-18 18:52:46 +0530259{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700260 EFI_STATUS Status;
261 EFI_QCOM_PMIC_VERSION_PROTOCOL *pPmicVersionProtocol;
Vijay Kumar Pendoti0be25612016-11-18 18:52:46 +0530262
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700263 Status = gBS->LocateProtocol (&gQcomPmicVersionProtocolGuid, NULL,
264 (VOID **)&pPmicVersionProtocol);
265 if (EFI_ERROR (Status)) {
266 DEBUG ((EFI_D_ERROR, "Error locating pmic protocol: %r\n", Status));
267 return Status;
268 }
Vijay Kumar Pendoti0be25612016-11-18 18:52:46 +0530269
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700270 *Revision = pPmicVersionProtocol->Revision;
Vijay Kumar Pendoti0be25612016-11-18 18:52:46 +0530271
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700272 Status = pPmicVersionProtocol->GetPmicInfo (PmicDeviceIndex, pmic_info);
273 if (EFI_ERROR (Status)) {
274 DEBUG ((EFI_D_ERROR, "Error getting pmic info: %r\n", Status));
275 return Status;
276 }
277 return Status;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800278}
279
lijuang24de5342016-09-07 18:09:10 +0800280/**
Jeevan Shriramcf9ea6d2017-09-11 19:04:40 -0700281 Device Handler Info
282
Mukesh Ojha82c0fa32018-02-01 17:19:44 +0530283 @param[out] HndlInfo : Pointer to array of HandleInfo structures
lijuang24de5342016-09-07 18:09:10 +0800284 in which the output is returned.
285 @param[in, out] MaxHandles : On input, max number of handle structures
286 the buffer can hold, On output, the number
287 of handle structures returned.
Jeevan Shriramcf9ea6d2017-09-11 19:04:40 -0700288 @param[in] Type : Device Type : UNKNOWN, UFS, EMMC, NAND
289 @retval EFI_STATUS : Return Success on getting Handler Info
290 **/
291
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700292STATIC EFI_STATUS
Mukesh Ojha82c0fa32018-02-01 17:19:44 +0530293GetDeviceHandleInfo (VOID *HndlInfo, UINT32 MaxHandles, MemCardType Type)
Jeevan Shriramcf9ea6d2017-09-11 19:04:40 -0700294{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700295 EFI_STATUS Status = EFI_INVALID_PARAMETER;
296 UINT32 Attribs = 0;
297 PartiSelectFilter HandleFilter;
Mukesh Ojha82c0fa32018-02-01 17:19:44 +0530298 HandleInfo *HandleInfoList = HndlInfo;
Jeevan Shriramcf9ea6d2017-09-11 19:04:40 -0700299
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700300 Attribs |= BLK_IO_SEL_MATCH_ROOT_DEVICE;
301 HandleFilter.PartitionType = NULL;
302 HandleFilter.VolumeName = NULL;
Jeevan Shriramcf9ea6d2017-09-11 19:04:40 -0700303
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700304 switch (Type) {
305 case UFS:
306 HandleFilter.RootDeviceType = &gEfiUfsLU0Guid;
307 break;
308 case EMMC:
309 HandleFilter.RootDeviceType = &gEfiEmmcUserPartitionGuid;
310 break;
311 case NAND:
312 HandleFilter.RootDeviceType = &gEfiNandUserPartitionGuid;
313 break;
314 case UNKNOWN:
315 DEBUG ((EFI_D_ERROR, "Device type unknown\n"));
Jeevan Shriramcf9ea6d2017-09-11 19:04:40 -0700316 return Status;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700317 }
318
319 Status =
Mukesh Ojha82c0fa32018-02-01 17:19:44 +0530320 GetBlkIOHandles (Attribs, &HandleFilter, HandleInfoList, &MaxHandles);
321 if (EFI_ERROR (Status) ||
322 MaxHandles == 0) {
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700323 DEBUG ((EFI_D_ERROR, "Get BlkIohandles failed\n"));
324 return Status;
325 }
326 return Status;
Jeevan Shriramcf9ea6d2017-09-11 19:04:40 -0700327}
328
329/**
330 Return a device type
Mukesh Ojha82c0fa32018-02-01 17:19:44 +0530331 @retval Device type : UNKNOWN | UFS | EMMC | NAND
332 **/
333STATIC UINT32
334GetCompatibleRootDeviceType (VOID)
335{
336 EFI_STATUS Status = EFI_INVALID_PARAMETER;
337 HandleInfo HandleInfoList[HANDLE_MAX_INFO_LIST];
338 UINT32 MaxHandles = ARRAY_SIZE (HandleInfoList);
339 UINT32 Index;
340
341 for (Index = 0; Index < UNKNOWN; Index++) {
342 Status = GetDeviceHandleInfo (HandleInfoList, MaxHandles, Index);
343 if (Status == EFI_SUCCESS) {
344 return Index;
345 }
346 }
347
348 return Index;
349}
350
351/**
352 Return a device type
353 @retval Device type : UNKNOWN | UFS | EMMC | NAND, default is UNKNOWN
lijuang24de5342016-09-07 18:09:10 +0800354 **/
Jeevan Shriramcf9ea6d2017-09-11 19:04:40 -0700355
Jeevan Shriram9f8489e2018-03-12 16:45:59 -0700356MemCardType
357CheckRootDeviceType (VOID)
lijuang24de5342016-09-07 18:09:10 +0800358{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700359 EFI_STATUS Status = EFI_INVALID_PARAMETER;
360 STATIC MemCardType Type = UNKNOWN;
361 MEM_CARD_INFO CardInfoData;
362 EFI_MEM_CARDINFO_PROTOCOL *CardInfo;
lijuang24de5342016-09-07 18:09:10 +0800363
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700364 if (Type == UNKNOWN) {
365 Status = gBS->LocateProtocol (&gEfiMemCardInfoProtocolGuid, NULL,
366 (VOID **)&CardInfo);
367 if (!EFI_ERROR (Status)) {
lijuang24de5342016-09-07 18:09:10 +0800368
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700369 Status = CardInfo->GetCardInfo (CardInfo, &CardInfoData);
lijuang24de5342016-09-07 18:09:10 +0800370
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700371 if (!EFI_ERROR (Status)) {
lijuang24de5342016-09-07 18:09:10 +0800372
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700373 if (!AsciiStrnCmp ((CHAR8 *)CardInfoData.card_type, "UFS",
374 AsciiStrLen ("UFS"))) {
375 Type = UFS;
376 } else if (!AsciiStrnCmp ((CHAR8 *)CardInfoData.card_type, "EMMC",
377 AsciiStrLen ("EMMC"))) {
378 Type = EMMC;
379 } else if (!AsciiStrnCmp ((CHAR8 *)CardInfoData.card_type, "NAND",
380 AsciiStrLen ("NAND"))) {
381 Type = NAND;
382 } else {
Mukesh Ojha82c0fa32018-02-01 17:19:44 +0530383 Type = GetCompatibleRootDeviceType ();
Jeevan Shriramcf9ea6d2017-09-11 19:04:40 -0700384 }
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700385 }
Jeevan Shriramcf9ea6d2017-09-11 19:04:40 -0700386 }
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700387 }
388 return Type;
lijuang24de5342016-09-07 18:09:10 +0800389}
390
391/**
392 Get device type
393 @param[out] StrDeviceType : Pointer to array of device type string.
394 @param[in] Len : The size of the device type string
395 **/
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700396VOID
397GetRootDeviceType (CHAR8 *StrDeviceType, UINT32 Len)
lijuang24de5342016-09-07 18:09:10 +0800398{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700399 UINT32 Type;
lijuang24de5342016-09-07 18:09:10 +0800400
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700401 Type = CheckRootDeviceType ();
402 AsciiSPrint (StrDeviceType, Len, "%a", DeviceType[Type]);
lijuang24de5342016-09-07 18:09:10 +0800403}
404
lijuang6a97bc52016-10-18 17:32:54 +0800405/**
406 Get device page size
407 @param[out] PageSize : Pointer to the page size.
408 **/
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700409VOID
410GetPageSize (UINT32 *PageSize)
lijuang6a97bc52016-10-18 17:32:54 +0800411{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700412 EFI_BLOCK_IO_PROTOCOL *BlkIo = NULL;
413 HandleInfo HandleInfoList[HANDLE_MAX_INFO_LIST];
414 UINT32 MaxHandles = ARRAY_SIZE (HandleInfoList);
415 UINT32 Type;
416 EFI_STATUS Status = EFI_INVALID_PARAMETER;
lijuang6a97bc52016-10-18 17:32:54 +0800417
Mukesh Ojhac6d501a2018-04-25 13:45:41 +0530418 *PageSize = BOOT_IMG_MAX_PAGE_SIZE;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700419 Type = CheckRootDeviceType ();
Mukesh Ojhac6d501a2018-04-25 13:45:41 +0530420 Status = GetDeviceHandleInfo (HandleInfoList, MaxHandles, Type);
421 if (Status == EFI_SUCCESS) {
422 BlkIo = HandleInfoList[0].BlkIo;
423 *PageSize = BlkIo->Media->BlockSize;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700424 }
lijuang6a97bc52016-10-18 17:32:54 +0800425}
426
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700427UINT32
428BoardPmicModel (UINT32 PmicDeviceIndex)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800429{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700430 EFI_STATUS Status;
431 EFI_PM_DEVICE_INFO_TYPE pmic_info;
432 UINT64 Revision;
Vijay Kumar Pendoti0be25612016-11-18 18:52:46 +0530433
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700434 Status = GetPmicInfo (PmicDeviceIndex, &pmic_info, &Revision);
435 if (Status != EFI_SUCCESS) {
436 DEBUG ((EFI_D_ERROR, "Error getting pmic model info: %r\n", Status));
437 ASSERT (0);
438 }
439 DEBUG ((EFI_D_VERBOSE, "PMIC Model 0x%x: 0x%x\n", PmicDeviceIndex,
440 pmic_info.PmicModel));
441 return pmic_info.PmicModel;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800442}
443
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700444UINT32
445BoardPmicTarget (UINT32 PmicDeviceIndex)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800446{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700447 UINT32 target;
448 EFI_STATUS Status;
449 UINT64 Revision;
450 EFI_PM_DEVICE_INFO_TYPE pmic_info;
451 EFI_PM_DEVICE_INFO_EXT_TYPE pmic_info_ext;
Vijay Kumar Pendoti0be25612016-11-18 18:52:46 +0530452
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700453 Status = GetPmicInfo (PmicDeviceIndex, &pmic_info, &Revision);
454 if (Status != EFI_SUCCESS) {
455 DEBUG ((EFI_D_ERROR, "Error finding board pmic info: %r\n", Status));
456 ASSERT (0);
457 }
Vijay Kumar Pendoti0be25612016-11-18 18:52:46 +0530458
Mayank Groverf0fb3792021-05-17 15:21:36 +0530459 /* GetPmicInfoExt API is only supported for Protocol Revsion v3 */
460 if (Revision == PMIC_VERSION_REVISION) {
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700461 Status = GetPmicInfoExt (PmicDeviceIndex, &pmic_info_ext);
462 if (Status != EFI_SUCCESS) {
463 DEBUG ((EFI_D_VERBOSE, "Error finding board pmic info: %r\n", Status));
464 return 0;
465 }
Vijay Kumar Pendoti0be25612016-11-18 18:52:46 +0530466
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700467 target = (pmic_info_ext.PmicVariantRevision << 24) |
468 (pmic_info_ext.PmicAllLayerRevision << 16) |
469 (pmic_info_ext.PmicMetalRevision << 8) | pmic_info_ext.PmicModel;
470 } else {
471 target = (pmic_info.PmicAllLayerRevision << 16) |
472 (pmic_info.PmicMetalRevision << 8) | pmic_info.PmicModel;
473 }
Vijay Kumar Pendoti0be25612016-11-18 18:52:46 +0530474
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700475 DEBUG ((EFI_D_VERBOSE, "PMIC Target 0x%x: 0x%x\n", PmicDeviceIndex, target));
476 return target;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800477}
478
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700479EFI_STATUS BoardInit (VOID)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800480{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700481 EFI_STATUS Status;
Jeevan Shriram62997572018-09-20 16:56:21 -0700482 EFIChipInfoModemType ModemType;
Chandra Sai Chidipudi64ced632020-01-22 12:25:28 +0530483 UINT32 DdrType;
Jeevan Shriram62997572018-09-20 16:56:21 -0700484
485 Status = GetChipInfo (&platform_board_info, &ModemType);
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700486 if (EFI_ERROR (Status))
487 return Status;
Jeevan Shriram62997572018-09-20 16:56:21 -0700488
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700489 Status = GetPlatformInfo (&platform_board_info);
490 if (EFI_ERROR (Status))
491 return Status;
Channagoud Kadabi539d3072016-02-11 21:34:48 -0800492
Chandra Sai Chidipudi64ced632020-01-22 12:25:28 +0530493 Status = BoardDdrType (&DdrType);
494 if (EFI_ERROR (Status))
495 return Status;
496
497 platform_board_info.HlosSubType = (DdrType << DDR_SHIFT);
498
Jeevan Shriram62997572018-09-20 16:56:21 -0700499 if (BoardPlatformFusion ()) {
500 AsciiSPrint ((CHAR8 *)platform_board_info.ChipBaseBand,
501 CHIP_BASE_BAND_LEN, "%a", CHIP_BASE_BAND_MDM);
502 } else if (ModemType == 0) {
503 AsciiSPrint ((CHAR8 *)platform_board_info.ChipBaseBand,
504 CHIP_BASE_BAND_LEN, "%a", CHIP_BASE_BAND_APQ);
505 } else {
506 AsciiSPrint ((CHAR8 *)platform_board_info.ChipBaseBand,
507 CHIP_BASE_BAND_LEN, "%a", CHIP_BASE_BAND_MSM);
508 }
509
510 DEBUG ((EFI_D_VERBOSE, "Raw Chip Id : 0x%x\n",
511 platform_board_info.RawChipId));
512 DEBUG ((EFI_D_VERBOSE, "Chip Version : 0x%x\n",
513 platform_board_info.ChipVersion));
514 DEBUG ((EFI_D_VERBOSE, "Foundry Id : 0x%x\n",
515 platform_board_info.FoundryId));
516 DEBUG ((EFI_D_VERBOSE, "Chip BaseBand : %a\n",
517 platform_board_info.ChipBaseBand));
518 DEBUG ((EFI_D_VERBOSE, "Fusion Value : %d\n",
519 platform_board_info.PlatformInfo.fusion));
Chandra Sai Chidipudi64ced632020-01-22 12:25:28 +0530520 DEBUG ((EFI_D_VERBOSE, "HLOS SubType : 0x%x\n",
521 platform_board_info.HlosSubType));
Jeevan Shriram62997572018-09-20 16:56:21 -0700522
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700523 return Status;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800524}
525
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700526EFI_STATUS
527UfsGetSetBootLun (UINT32 *UfsBootlun, BOOLEAN IsGet)
Vijay Kumar Pendoti09d572e2016-08-27 03:22:36 +0530528{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700529 EFI_STATUS Status = EFI_INVALID_PARAMETER;
530 EFI_MEM_CARDINFO_PROTOCOL *CardInfo;
531 HandleInfo HandleInfoList[MAX_HANDLE_INFO_LIST];
532 UINT32 Attribs = 0;
533 UINT32 MaxHandles;
534 PartiSelectFilter HandleFilter;
Vijay Kumar Pendoti09d572e2016-08-27 03:22:36 +0530535
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700536 Attribs |= BLK_IO_SEL_MATCH_ROOT_DEVICE;
537 MaxHandles = ARRAY_SIZE (HandleInfoList);
538 HandleFilter.PartitionType = NULL;
539 HandleFilter.VolumeName = NULL;
540 HandleFilter.RootDeviceType = &gEfiUfsLU0Guid;
Vijay Kumar Pendoti09d572e2016-08-27 03:22:36 +0530541
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700542 Status =
543 GetBlkIOHandles (Attribs, &HandleFilter, HandleInfoList, &MaxHandles);
544 if (EFI_ERROR (Status))
545 return EFI_NOT_FOUND;
Vijay Kumar Pendoti09d572e2016-08-27 03:22:36 +0530546
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700547 Status =
548 gBS->HandleProtocol (HandleInfoList[0].Handle,
549 &gEfiMemCardInfoProtocolGuid, (VOID **)&CardInfo);
Vijay Kumar Pendoti09d572e2016-08-27 03:22:36 +0530550
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700551 if (Status != EFI_SUCCESS) {
552 DEBUG ((EFI_D_ERROR, "Error locating MemCardInfoProtocol:%x\n", Status));
553 return Status;
554 }
Vijay Kumar Pendoti09d572e2016-08-27 03:22:36 +0530555
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700556 if (CardInfo->Revision < EFI_MEM_CARD_INFO_PROTOCOL_REVISION) {
557 DEBUG ((EFI_D_ERROR, "This API not supported in Revision =%u\n",
558 CardInfo->Revision));
559 return EFI_NOT_FOUND;
560 }
Vijay Kumar Pendoti09d572e2016-08-27 03:22:36 +0530561
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700562 if (IsGet == TRUE) {
563 if (CardInfo->GetBootLU (CardInfo, UfsBootlun) == EFI_SUCCESS)
564 DEBUG ((EFI_D_VERBOSE, "Get BootLun =%u\n", *UfsBootlun));
565 } else {
566 if (CardInfo->SetBootLU (CardInfo, *UfsBootlun) == EFI_SUCCESS)
567 DEBUG ((EFI_D_VERBOSE, "SetBootLun =%u\n", *UfsBootlun));
568 }
569 return Status;
Vijay Kumar Pendoti09d572e2016-08-27 03:22:36 +0530570}
571
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700572EFI_STATUS
573BoardSerialNum (CHAR8 *StrSerialNum, UINT32 Len)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800574{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700575 EFI_STATUS Status = EFI_INVALID_PARAMETER;
576 MEM_CARD_INFO CardInfoData;
577 EFI_MEM_CARDINFO_PROTOCOL *CardInfo;
578 UINT32 SerialNo;
579 HandleInfo HandleInfoList[HANDLE_MAX_INFO_LIST];
580 UINT32 MaxHandles = ARRAY_SIZE (HandleInfoList);
581 MemCardType Type = EMMC;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800582
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700583 Type = CheckRootDeviceType ();
584 if (Type == UNKNOWN)
585 return EFI_NOT_FOUND;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800586
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700587 Status = GetDeviceHandleInfo (HandleInfoList, MaxHandles, Type);
588 if (EFI_ERROR (Status)) {
589 return Status;
590 }
Jeevan Shriramcf9ea6d2017-09-11 19:04:40 -0700591
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700592 Status =
593 gBS->HandleProtocol (HandleInfoList[0].Handle,
594 &gEfiMemCardInfoProtocolGuid, (VOID **)&CardInfo);
595 if (Status != EFI_SUCCESS) {
596 DEBUG ((EFI_D_ERROR, "Error locating MemCardInfoProtocol:%x\n", Status));
597 return Status;
598 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800599
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700600 if (CardInfo->GetCardInfo (CardInfo, &CardInfoData) == EFI_SUCCESS) {
601 if (Type == UFS) {
602 Status = gBS->CalculateCrc32 (CardInfoData.product_serial_num,
603 CardInfoData.serial_num_len, &SerialNo);
604 if (Status != EFI_SUCCESS) {
605 DEBUG ((EFI_D_ERROR,
606 "Error calculating Crc of the unicode serial number: %x\n",
607 Status));
608 return Status;
609 }
610 AsciiSPrint (StrSerialNum, Len, "%x", SerialNo);
611 } else {
612 AsciiSPrint (StrSerialNum, Len, "%x",
613 *(UINT32 *)CardInfoData.product_serial_num);
614 }
Vijay Kumar Pendoti78b1a962017-01-12 22:48:41 +0530615
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700616 /* adb is case sensitive, convert the serial number to lower case
617 * to maintain uniformity across the system. */
618 ToLower (StrSerialNum);
619 }
620 return Status;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800621}
Channagoud Kadabi539d3072016-02-11 21:34:48 -0800622
623/* Helper APIs for device tree selection */
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700624UINT32 BoardPlatformRawChipId (VOID)
Channagoud Kadabi539d3072016-02-11 21:34:48 -0800625{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700626 return platform_board_info.RawChipId;
Channagoud Kadabi539d3072016-02-11 21:34:48 -0800627}
628
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700629EFIChipInfoVersionType BoardPlatformChipVersion (VOID)
Channagoud Kadabi539d3072016-02-11 21:34:48 -0800630{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700631 return platform_board_info.ChipVersion;
Channagoud Kadabi539d3072016-02-11 21:34:48 -0800632}
633
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700634EFIChipInfoFoundryIdType BoardPlatformFoundryId (VOID)
Channagoud Kadabi539d3072016-02-11 21:34:48 -0800635{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700636 return platform_board_info.FoundryId;
Channagoud Kadabi539d3072016-02-11 21:34:48 -0800637}
638
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700639CHAR8 *BoardPlatformChipBaseBand (VOID)
Vijay Kumar Pendoti582158c2016-09-29 23:47:37 +0530640{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700641 return platform_board_info.ChipBaseBand;
Vijay Kumar Pendoti582158c2016-09-29 23:47:37 +0530642}
643
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700644EFI_PLATFORMINFO_PLATFORM_TYPE BoardPlatformType (VOID)
Channagoud Kadabi539d3072016-02-11 21:34:48 -0800645{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700646 return platform_board_info.PlatformInfo.platform;
Channagoud Kadabi539d3072016-02-11 21:34:48 -0800647}
648
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700649UINT32 BoardPlatformVersion (VOID)
Channagoud Kadabi539d3072016-02-11 21:34:48 -0800650{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700651 return platform_board_info.PlatformInfo.version;
Channagoud Kadabi539d3072016-02-11 21:34:48 -0800652}
653
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700654UINT32 BoardPlatformSubType (VOID)
Channagoud Kadabi539d3072016-02-11 21:34:48 -0800655{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700656 return platform_board_info.PlatformInfo.subtype;
Channagoud Kadabi539d3072016-02-11 21:34:48 -0800657}
Channagoud Kadabi5bd40992016-03-04 15:05:20 -0800658
Bhanuprakash Modem027aae52017-11-07 14:58:02 -0800659BOOLEAN BoardPlatformFusion (VOID)
660{
661 return platform_board_info.PlatformInfo.fusion;
662}
663
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700664UINT32 BoardTargetId (VOID)
Channagoud Kadabi5bd40992016-03-04 15:05:20 -0800665{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700666 UINT32 Target;
Channagoud Kadabi5bd40992016-03-04 15:05:20 -0800667
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700668 Target = (((platform_board_info.PlatformInfo.subtype & 0xff) << 24) |
669 (((platform_board_info.PlatformInfo.version >> 16) & 0xff) << 16) |
670 ((platform_board_info.PlatformInfo.version & 0xff) << 8) |
671 (platform_board_info.PlatformInfo.platform & 0xff));
Channagoud Kadabi5bd40992016-03-04 15:05:20 -0800672
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700673 return Target;
Channagoud Kadabi5bd40992016-03-04 15:05:20 -0800674}
lijuang834ebd22016-09-07 18:27:29 +0800675
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700676VOID
677BoardHwPlatformName (CHAR8 *StrHwPlatform, UINT32 Len)
lijuang834ebd22016-09-07 18:27:29 +0800678{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700679 EFI_STATUS Status;
680 EFI_CHIPINFO_PROTOCOL *pChipInfoProtocol;
681 UINT32 ChipIdValidLen = 4;
lijuang834ebd22016-09-07 18:27:29 +0800682
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700683 if (StrHwPlatform == NULL) {
684 DEBUG ((EFI_D_ERROR, "Error: HW Platform string is NULL\n"));
685 return;
686 }
lijuang834ebd22016-09-07 18:27:29 +0800687
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700688 Status = gBS->LocateProtocol (&gEfiChipInfoProtocolGuid, NULL,
689 (VOID **)&pChipInfoProtocol);
690 if (EFI_ERROR (Status)) {
691 DEBUG (
692 (EFI_D_ERROR, "Locate Protocol failed for gEfiChipInfoProtocolGuid\n"));
693 return;
694 }
lijuang834ebd22016-09-07 18:27:29 +0800695
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700696 Status = pChipInfoProtocol->GetChipIdString (
697 pChipInfoProtocol, StrHwPlatform,
698 EFICHIPINFO_MAX_ID_LENGTH > Len ? Len : EFICHIPINFO_MAX_ID_LENGTH);
699 if (EFI_ERROR (Status)) {
700 DEBUG ((EFI_D_ERROR, "Failed to Get the ChipIdString\n"));
701 return;
702 }
lijuanga0136d32017-09-07 15:22:51 +0800703
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700704 if (Len < ChipIdValidLen)
705 StrHwPlatform[Len - 1] = '\0';
706 else
707 StrHwPlatform[ChipIdValidLen - 1] = '\0';
lijuang834ebd22016-09-07 18:27:29 +0800708}
Chandra Sai Chidipudi64ced632020-01-22 12:25:28 +0530709
710EFI_STATUS BoardDdrType (UINT32 *Type)
711{
712 EFI_STATUS Status;
713 RamPartitionEntry *RamPartitions = NULL;
714 UINT32 i = 0;
715 UINT32 NumPartitions = 0;
716 UINT64 DdrSize = 0;
717
718 Status = ReadRamPartitions (&RamPartitions, &NumPartitions);
719 if (EFI_ERROR (Status)) {
720 DEBUG ((EFI_D_ERROR, "Error returned from GetRamPartitions %r\n", Status));
721 return Status;
722 }
723
724 for (i = 0; i < NumPartitions; i++) {
725 DdrSize += RamPartitions[i].AvailableLength;
726 }
727 DEBUG ((EFI_D_INFO, "Total DDR Size: 0x%016lx \n", DdrSize));
728
729 *Type = 0;
730 if (DdrSize <= DDR_256MB) {
731 *Type = DDRTYPE_256MB;
732 } else if (DdrSize <= DDR_512MB) {
733 *Type = DDRTYPE_512MB;
734 } else if (DdrSize <= DDR_1024MB) {
735 *Type = DDRTYPE_1024MB;
736 } else if (DdrSize <= DDR_2048MB) {
737 *Type = DDRTYPE_2048MB;
738 }
739
740 return Status;
741}
742
743UINT32 BoardPlatformHlosSubType (VOID)
744{
745 return platform_board_info.HlosSubType;
746}