blob: 6d07d92ede3b946df1d0348b17ebf7c9646244f5 [file] [log] [blame]
Mukesh Ojha82c0fa32018-02-01 17:19:44 +05301/* Copyright (c) 2015-2018, 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
Jeevan Shriram17f173d2017-10-24 22:11:07 -070044EFI_STATUS
45GetRamPartitions (RamPartitionEntry **RamPartitions, UINT32 *NumPartitions)
46{
Vijay Kumar Pendotic52e80b2016-10-12 16:47:50 +053047
Jeevan Shriram17f173d2017-10-24 22:11:07 -070048 EFI_STATUS Status = EFI_NOT_FOUND;
49 EFI_RAMPARTITION_PROTOCOL *pRamPartProtocol = NULL;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080050
Jeevan Shriram17f173d2017-10-24 22:11:07 -070051 Status = gBS->LocateProtocol (&gEfiRamPartitionProtocolGuid, NULL,
52 (VOID **)&pRamPartProtocol);
53 if (EFI_ERROR (Status) || (pRamPartProtocol == NULL)) {
54 DEBUG ((EFI_D_ERROR,
55 "Locate EFI_RAMPARTITION_Protocol failed, Status = (0x%x)\r\n",
56 Status));
57 return EFI_NOT_FOUND;
58 }
59 Status = pRamPartProtocol->GetRamPartitions (pRamPartProtocol, NULL,
60 NumPartitions);
61 if (Status == EFI_BUFFER_TOO_SMALL) {
62 *RamPartitions = AllocatePool (*NumPartitions * sizeof (RamPartitionEntry));
63 if (*RamPartitions == NULL)
64 return EFI_OUT_OF_RESOURCES;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080065
Jeevan Shriram17f173d2017-10-24 22:11:07 -070066 Status = pRamPartProtocol->GetRamPartitions (pRamPartProtocol,
67 *RamPartitions, NumPartitions);
68 if (EFI_ERROR (Status) || (*NumPartitions < 1)) {
69 DEBUG ((EFI_D_ERROR, "Failed to get RAM partitions"));
70 FreePool (*RamPartitions);
71 *RamPartitions = NULL;
72 return EFI_NOT_FOUND;
73 }
74 } else {
75 DEBUG ((EFI_D_ERROR, "Error Occured while populating RamPartitions\n"));
76 return EFI_PROTOCOL_ERROR;
77 }
78 return Status;
Vijay Kumar Pendotic52e80b2016-10-12 16:47:50 +053079}
80
Jeevan Shriram17f173d2017-10-24 22:11:07 -070081EFI_STATUS
Jeevan Shriram067bbb52018-05-08 17:48:47 -070082GetGranuleSize (UINT32 *MinPasrGranuleSize)
83{
84 EFI_STATUS Status = EFI_NOT_FOUND;
85 EFI_RAMPARTITION_PROTOCOL *pRamPartProtocol = NULL;
86
87 Status = gBS->LocateProtocol (&gEfiRamPartitionProtocolGuid, NULL,
88 (VOID **)&pRamPartProtocol);
89 if (EFI_ERROR (Status) ||
90 (pRamPartProtocol == NULL)) {
91 DEBUG ((EFI_D_ERROR,
92 "Locate EFI_RAMPARTITION_Protocol failed, Status = %r \n",
93 Status));
94 return Status;
95 }
96
Jeevan Shriram97ec78e2018-06-12 20:31:12 -070097 if (pRamPartProtocol->Revision < EFI_RAMPARTITION_PROTOCOL_REVISION) {
98 DEBUG ((EFI_D_ERROR, "WARNING: Unsupported EFI_RAMPARTITION_PROTOCOL\n"));
99 return EFI_UNSUPPORTED;
100 }
101
Jeevan Shriram067bbb52018-05-08 17:48:47 -0700102 Status = pRamPartProtocol->GetMinPasrSize (pRamPartProtocol,
103 MinPasrGranuleSize);
104 if (EFI_ERROR (Status)) {
105 DEBUG ((EFI_D_ERROR,
106 "Failed to get MinPasrSize, Status = %r\n",
107 Status));
108 return Status;
109 }
110 return Status;
111}
112
113EFI_STATUS
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700114BaseMem (UINT64 *BaseMemory)
Vijay Kumar Pendotic52e80b2016-10-12 16:47:50 +0530115{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700116 EFI_STATUS Status = EFI_NOT_FOUND;
117 RamPartitionEntry *RamPartitions = NULL;
118 UINT32 NumPartitions = 0;
119 UINT64 SmallestBase;
120 UINT32 i = 0;
Vijay Kumar Pendotic52e80b2016-10-12 16:47:50 +0530121
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700122 Status = GetRamPartitions (&RamPartitions, &NumPartitions);
123 if (EFI_ERROR (Status)) {
124 DEBUG ((EFI_D_ERROR, "Error returned from GetRamPartitions %r\n", Status));
125 return Status;
126 }
127 if (!RamPartitions) {
128 DEBUG ((EFI_D_ERROR, "RamPartitions is NULL\n"));
129 return EFI_NOT_FOUND;
130 }
131 SmallestBase = RamPartitions[0].Base;
132 for (i = 0; i < NumPartitions; i++) {
133 if (SmallestBase > RamPartitions[i].Base)
134 SmallestBase = RamPartitions[i].Base;
135 }
136 *BaseMemory = SmallestBase;
137 DEBUG ((EFI_D_INFO, "Memory Base Address: 0x%x\n", *BaseMemory));
138 FreePool (RamPartitions);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800139
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700140 return Status;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800141}
142
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700143STATIC EFI_STATUS
144GetChipInfo (struct BoardInfo *platform_board_info)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800145{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700146 EFI_STATUS Status;
147 EFI_CHIPINFO_PROTOCOL *pChipInfoProtocol;
Saranya Chidura6f047e22018-09-05 10:09:45 +0530148 EFIChipInfoModemType ModemType;
149
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700150 Status = gBS->LocateProtocol (&gEfiChipInfoProtocolGuid, NULL,
151 (VOID **)&pChipInfoProtocol);
152 if (EFI_ERROR (Status))
153 return Status;
154 Status = pChipInfoProtocol->GetChipId (pChipInfoProtocol,
155 &platform_board_info->RawChipId);
156 if (EFI_ERROR (Status))
157 return Status;
158 Status = pChipInfoProtocol->GetChipVersion (
159 pChipInfoProtocol, &platform_board_info->ChipVersion);
160 if (EFI_ERROR (Status))
161 return Status;
162 Status = pChipInfoProtocol->GetFoundryId (pChipInfoProtocol,
163 &platform_board_info->FoundryId);
164 if (EFI_ERROR (Status))
165 return Status;
Saranya Chidura6f047e22018-09-05 10:09:45 +0530166 Status = pChipInfoProtocol->GetModemSupport (
167 pChipInfoProtocol, &ModemType);
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700168 if (EFI_ERROR (Status))
169 return Status;
Saranya Chidura6f047e22018-09-05 10:09:45 +0530170
171 if (BoardPlatformFusion ()) {
172 AsciiSPrint ((CHAR8 *)platform_board_info->ChipBaseBand,
173 CHIP_BASE_BAND_LEN, "%a", CHIP_BASE_BAND_MDM);
174 } else if (ModemType == 0) {
175 AsciiSPrint ((CHAR8 *)platform_board_info->ChipBaseBand,
176 CHIP_BASE_BAND_LEN, "%a", CHIP_BASE_BAND_APQ);
177 } else {
178 AsciiSPrint ((CHAR8 *)platform_board_info->ChipBaseBand,
179 CHIP_BASE_BAND_LEN, "%a", CHIP_BASE_BAND_MSM);
180 }
181
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700182 DEBUG ((EFI_D_VERBOSE, "Raw Chip Id : 0x%x\n",
183 platform_board_info->RawChipId));
184 DEBUG ((EFI_D_VERBOSE, "Chip Version : 0x%x\n",
185 platform_board_info->ChipVersion));
186 DEBUG ((EFI_D_VERBOSE, "Foundry Id : 0x%x\n",
187 platform_board_info->FoundryId));
188 DEBUG ((EFI_D_VERBOSE, "Chip BaseBand : %a\n",
189 platform_board_info->ChipBaseBand));
Bhanuprakash Modem027aae52017-11-07 14:58:02 -0800190 DEBUG ((EFI_D_VERBOSE, "Fusion Value : %d\n",
191 platform_board_info->PlatformInfo.fusion));
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700192 return Status;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800193}
194
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700195STATIC EFI_STATUS
196GetPlatformInfo (struct BoardInfo *platform_board_info)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800197{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700198 EFI_STATUS eResult;
199 EFI_PLATFORMINFO_PROTOCOL *hPlatformInfoProtocol;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800200
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700201 eResult = gBS->LocateProtocol (&gEfiPlatformInfoProtocolGuid, NULL,
202 (VOID **)&hPlatformInfoProtocol);
203 if (eResult != EFI_SUCCESS) {
204 AsciiPrint ("Error: Failed to locate PlatformInfo protocol.\n");
205 goto endtest;
206 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800207
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700208 eResult = hPlatformInfoProtocol->GetPlatformInfo (
209 hPlatformInfoProtocol, &platform_board_info->PlatformInfo);
210 if (eResult != EFI_SUCCESS) {
211 AsciiPrint ("Error: GetPlatformInfo failed.\n");
212 goto endtest;
213 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800214
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700215 if (platform_board_info->PlatformInfo.platform >=
216 EFI_PLATFORMINFO_NUM_TYPES) {
217 AsciiPrint ("Error: Unknown platform type (%d).\n",
218 platform_board_info->PlatformInfo.platform);
219 eResult = EFI_PROTOCOL_ERROR;
220 goto endtest;
221 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800222
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700223 DEBUG ((EFI_D_VERBOSE, "Platform Info : 0x%x\n",
224 platform_board_info->PlatformInfo.platform));
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800225endtest:
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700226 return eResult;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800227}
228
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700229STATIC EFI_STATUS
230GetPmicInfoExt (UINT32 PmicDeviceIndex,
231 EFI_PM_DEVICE_INFO_EXT_TYPE *pmic_info_ext)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800232{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700233 EFI_STATUS Status;
234 EFI_QCOM_PMIC_VERSION_PROTOCOL *pPmicVersionProtocol;
Vijay Kumar Pendoti0be25612016-11-18 18:52:46 +0530235
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700236 Status = gBS->LocateProtocol (&gQcomPmicVersionProtocolGuid, NULL,
237 (VOID **)&pPmicVersionProtocol);
238 if (EFI_ERROR (Status)) {
239 DEBUG ((EFI_D_ERROR, "Error locating pmic protocol: %r\n", Status));
240 return Status;
241 }
Vijay Kumar Pendoti0be25612016-11-18 18:52:46 +0530242
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700243 Status =
244 pPmicVersionProtocol->GetPmicInfoExt (PmicDeviceIndex, pmic_info_ext);
245 if (EFI_ERROR (Status)) {
246 DEBUG ((EFI_D_VERBOSE, "Error getting pmic info ext: %r\n", Status));
247 return Status;
248 }
249 return Status;
Vijay Kumar Pendoti0be25612016-11-18 18:52:46 +0530250}
251
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700252STATIC EFI_STATUS
253GetPmicInfo (UINT32 PmicDeviceIndex,
254 EFI_PM_DEVICE_INFO_TYPE *pmic_info,
255 UINT64 *Revision)
Vijay Kumar Pendoti0be25612016-11-18 18:52:46 +0530256{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700257 EFI_STATUS Status;
258 EFI_QCOM_PMIC_VERSION_PROTOCOL *pPmicVersionProtocol;
Vijay Kumar Pendoti0be25612016-11-18 18:52:46 +0530259
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700260 Status = gBS->LocateProtocol (&gQcomPmicVersionProtocolGuid, NULL,
261 (VOID **)&pPmicVersionProtocol);
262 if (EFI_ERROR (Status)) {
263 DEBUG ((EFI_D_ERROR, "Error locating pmic protocol: %r\n", Status));
264 return Status;
265 }
Vijay Kumar Pendoti0be25612016-11-18 18:52:46 +0530266
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700267 *Revision = pPmicVersionProtocol->Revision;
Vijay Kumar Pendoti0be25612016-11-18 18:52:46 +0530268
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700269 Status = pPmicVersionProtocol->GetPmicInfo (PmicDeviceIndex, pmic_info);
270 if (EFI_ERROR (Status)) {
271 DEBUG ((EFI_D_ERROR, "Error getting pmic info: %r\n", Status));
272 return Status;
273 }
274 return Status;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800275}
276
lijuang24de5342016-09-07 18:09:10 +0800277/**
Jeevan Shriramcf9ea6d2017-09-11 19:04:40 -0700278 Device Handler Info
279
Mukesh Ojha82c0fa32018-02-01 17:19:44 +0530280 @param[out] HndlInfo : Pointer to array of HandleInfo structures
lijuang24de5342016-09-07 18:09:10 +0800281 in which the output is returned.
282 @param[in, out] MaxHandles : On input, max number of handle structures
283 the buffer can hold, On output, the number
284 of handle structures returned.
Jeevan Shriramcf9ea6d2017-09-11 19:04:40 -0700285 @param[in] Type : Device Type : UNKNOWN, UFS, EMMC, NAND
286 @retval EFI_STATUS : Return Success on getting Handler Info
287 **/
288
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700289STATIC EFI_STATUS
Mukesh Ojha82c0fa32018-02-01 17:19:44 +0530290GetDeviceHandleInfo (VOID *HndlInfo, UINT32 MaxHandles, MemCardType Type)
Jeevan Shriramcf9ea6d2017-09-11 19:04:40 -0700291{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700292 EFI_STATUS Status = EFI_INVALID_PARAMETER;
293 UINT32 Attribs = 0;
294 PartiSelectFilter HandleFilter;
Mukesh Ojha82c0fa32018-02-01 17:19:44 +0530295 HandleInfo *HandleInfoList = HndlInfo;
Jeevan Shriramcf9ea6d2017-09-11 19:04:40 -0700296
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700297 Attribs |= BLK_IO_SEL_MATCH_ROOT_DEVICE;
298 HandleFilter.PartitionType = NULL;
299 HandleFilter.VolumeName = NULL;
Jeevan Shriramcf9ea6d2017-09-11 19:04:40 -0700300
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700301 switch (Type) {
302 case UFS:
303 HandleFilter.RootDeviceType = &gEfiUfsLU0Guid;
304 break;
305 case EMMC:
306 HandleFilter.RootDeviceType = &gEfiEmmcUserPartitionGuid;
307 break;
308 case NAND:
309 HandleFilter.RootDeviceType = &gEfiNandUserPartitionGuid;
310 break;
311 case UNKNOWN:
312 DEBUG ((EFI_D_ERROR, "Device type unknown\n"));
Jeevan Shriramcf9ea6d2017-09-11 19:04:40 -0700313 return Status;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700314 }
315
316 Status =
Mukesh Ojha82c0fa32018-02-01 17:19:44 +0530317 GetBlkIOHandles (Attribs, &HandleFilter, HandleInfoList, &MaxHandles);
318 if (EFI_ERROR (Status) ||
319 MaxHandles == 0) {
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700320 DEBUG ((EFI_D_ERROR, "Get BlkIohandles failed\n"));
321 return Status;
322 }
323 return Status;
Jeevan Shriramcf9ea6d2017-09-11 19:04:40 -0700324}
325
326/**
327 Return a device type
Mukesh Ojha82c0fa32018-02-01 17:19:44 +0530328 @retval Device type : UNKNOWN | UFS | EMMC | NAND
329 **/
330STATIC UINT32
331GetCompatibleRootDeviceType (VOID)
332{
333 EFI_STATUS Status = EFI_INVALID_PARAMETER;
334 HandleInfo HandleInfoList[HANDLE_MAX_INFO_LIST];
335 UINT32 MaxHandles = ARRAY_SIZE (HandleInfoList);
336 UINT32 Index;
337
338 for (Index = 0; Index < UNKNOWN; Index++) {
339 Status = GetDeviceHandleInfo (HandleInfoList, MaxHandles, Index);
340 if (Status == EFI_SUCCESS) {
341 return Index;
342 }
343 }
344
345 return Index;
346}
347
348/**
349 Return a device type
350 @retval Device type : UNKNOWN | UFS | EMMC | NAND, default is UNKNOWN
lijuang24de5342016-09-07 18:09:10 +0800351 **/
Jeevan Shriramcf9ea6d2017-09-11 19:04:40 -0700352
Jeevan Shriram9f8489e2018-03-12 16:45:59 -0700353MemCardType
354CheckRootDeviceType (VOID)
lijuang24de5342016-09-07 18:09:10 +0800355{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700356 EFI_STATUS Status = EFI_INVALID_PARAMETER;
357 STATIC MemCardType Type = UNKNOWN;
358 MEM_CARD_INFO CardInfoData;
359 EFI_MEM_CARDINFO_PROTOCOL *CardInfo;
lijuang24de5342016-09-07 18:09:10 +0800360
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700361 if (Type == UNKNOWN) {
362 Status = gBS->LocateProtocol (&gEfiMemCardInfoProtocolGuid, NULL,
363 (VOID **)&CardInfo);
364 if (!EFI_ERROR (Status)) {
lijuang24de5342016-09-07 18:09:10 +0800365
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700366 Status = CardInfo->GetCardInfo (CardInfo, &CardInfoData);
lijuang24de5342016-09-07 18:09:10 +0800367
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700368 if (!EFI_ERROR (Status)) {
lijuang24de5342016-09-07 18:09:10 +0800369
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700370 if (!AsciiStrnCmp ((CHAR8 *)CardInfoData.card_type, "UFS",
371 AsciiStrLen ("UFS"))) {
372 Type = UFS;
373 } else if (!AsciiStrnCmp ((CHAR8 *)CardInfoData.card_type, "EMMC",
374 AsciiStrLen ("EMMC"))) {
375 Type = EMMC;
376 } else if (!AsciiStrnCmp ((CHAR8 *)CardInfoData.card_type, "NAND",
377 AsciiStrLen ("NAND"))) {
378 Type = NAND;
379 } else {
Mukesh Ojha82c0fa32018-02-01 17:19:44 +0530380 Type = GetCompatibleRootDeviceType ();
Jeevan Shriramcf9ea6d2017-09-11 19:04:40 -0700381 }
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700382 }
Jeevan Shriramcf9ea6d2017-09-11 19:04:40 -0700383 }
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700384 }
385 return Type;
lijuang24de5342016-09-07 18:09:10 +0800386}
387
388/**
389 Get device type
390 @param[out] StrDeviceType : Pointer to array of device type string.
391 @param[in] Len : The size of the device type string
392 **/
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700393VOID
394GetRootDeviceType (CHAR8 *StrDeviceType, UINT32 Len)
lijuang24de5342016-09-07 18:09:10 +0800395{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700396 UINT32 Type;
lijuang24de5342016-09-07 18:09:10 +0800397
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700398 Type = CheckRootDeviceType ();
399 AsciiSPrint (StrDeviceType, Len, "%a", DeviceType[Type]);
lijuang24de5342016-09-07 18:09:10 +0800400}
401
lijuang6a97bc52016-10-18 17:32:54 +0800402/**
403 Get device page size
404 @param[out] PageSize : Pointer to the page size.
405 **/
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700406VOID
407GetPageSize (UINT32 *PageSize)
lijuang6a97bc52016-10-18 17:32:54 +0800408{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700409 EFI_BLOCK_IO_PROTOCOL *BlkIo = NULL;
410 HandleInfo HandleInfoList[HANDLE_MAX_INFO_LIST];
411 UINT32 MaxHandles = ARRAY_SIZE (HandleInfoList);
412 UINT32 Type;
413 EFI_STATUS Status = EFI_INVALID_PARAMETER;
lijuang6a97bc52016-10-18 17:32:54 +0800414
Mukesh Ojhac6d501a2018-04-25 13:45:41 +0530415 *PageSize = BOOT_IMG_MAX_PAGE_SIZE;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700416 Type = CheckRootDeviceType ();
Mukesh Ojhac6d501a2018-04-25 13:45:41 +0530417 Status = GetDeviceHandleInfo (HandleInfoList, MaxHandles, Type);
418 if (Status == EFI_SUCCESS) {
419 BlkIo = HandleInfoList[0].BlkIo;
420 *PageSize = BlkIo->Media->BlockSize;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700421 }
lijuang6a97bc52016-10-18 17:32:54 +0800422}
423
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700424UINT32
425BoardPmicModel (UINT32 PmicDeviceIndex)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800426{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700427 EFI_STATUS Status;
428 EFI_PM_DEVICE_INFO_TYPE pmic_info;
429 UINT64 Revision;
Vijay Kumar Pendoti0be25612016-11-18 18:52:46 +0530430
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700431 Status = GetPmicInfo (PmicDeviceIndex, &pmic_info, &Revision);
432 if (Status != EFI_SUCCESS) {
433 DEBUG ((EFI_D_ERROR, "Error getting pmic model info: %r\n", Status));
434 ASSERT (0);
435 }
436 DEBUG ((EFI_D_VERBOSE, "PMIC Model 0x%x: 0x%x\n", PmicDeviceIndex,
437 pmic_info.PmicModel));
438 return pmic_info.PmicModel;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800439}
440
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700441UINT32
442BoardPmicTarget (UINT32 PmicDeviceIndex)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800443{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700444 UINT32 target;
445 EFI_STATUS Status;
446 UINT64 Revision;
447 EFI_PM_DEVICE_INFO_TYPE pmic_info;
448 EFI_PM_DEVICE_INFO_EXT_TYPE pmic_info_ext;
Vijay Kumar Pendoti0be25612016-11-18 18:52:46 +0530449
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700450 Status = GetPmicInfo (PmicDeviceIndex, &pmic_info, &Revision);
451 if (Status != EFI_SUCCESS) {
452 DEBUG ((EFI_D_ERROR, "Error finding board pmic info: %r\n", Status));
453 ASSERT (0);
454 }
Vijay Kumar Pendoti0be25612016-11-18 18:52:46 +0530455
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700456 if (Revision >= PMIC_VERSION_REVISION) {
457 Status = GetPmicInfoExt (PmicDeviceIndex, &pmic_info_ext);
458 if (Status != EFI_SUCCESS) {
459 DEBUG ((EFI_D_VERBOSE, "Error finding board pmic info: %r\n", Status));
460 return 0;
461 }
Vijay Kumar Pendoti0be25612016-11-18 18:52:46 +0530462
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700463 target = (pmic_info_ext.PmicVariantRevision << 24) |
464 (pmic_info_ext.PmicAllLayerRevision << 16) |
465 (pmic_info_ext.PmicMetalRevision << 8) | pmic_info_ext.PmicModel;
466 } else {
467 target = (pmic_info.PmicAllLayerRevision << 16) |
468 (pmic_info.PmicMetalRevision << 8) | pmic_info.PmicModel;
469 }
Vijay Kumar Pendoti0be25612016-11-18 18:52:46 +0530470
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700471 DEBUG ((EFI_D_VERBOSE, "PMIC Target 0x%x: 0x%x\n", PmicDeviceIndex, target));
472 return target;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800473}
474
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700475EFI_STATUS BoardInit (VOID)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800476{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700477 EFI_STATUS Status;
478 Status = GetChipInfo (&platform_board_info);
479 if (EFI_ERROR (Status))
480 return Status;
481 Status = GetPlatformInfo (&platform_board_info);
482 if (EFI_ERROR (Status))
483 return Status;
Channagoud Kadabi539d3072016-02-11 21:34:48 -0800484
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700485 return Status;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800486}
487
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700488EFI_STATUS
489UfsGetSetBootLun (UINT32 *UfsBootlun, BOOLEAN IsGet)
Vijay Kumar Pendoti09d572e2016-08-27 03:22:36 +0530490{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700491 EFI_STATUS Status = EFI_INVALID_PARAMETER;
492 EFI_MEM_CARDINFO_PROTOCOL *CardInfo;
493 HandleInfo HandleInfoList[MAX_HANDLE_INFO_LIST];
494 UINT32 Attribs = 0;
495 UINT32 MaxHandles;
496 PartiSelectFilter HandleFilter;
Vijay Kumar Pendoti09d572e2016-08-27 03:22:36 +0530497
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700498 Attribs |= BLK_IO_SEL_MATCH_ROOT_DEVICE;
499 MaxHandles = ARRAY_SIZE (HandleInfoList);
500 HandleFilter.PartitionType = NULL;
501 HandleFilter.VolumeName = NULL;
502 HandleFilter.RootDeviceType = &gEfiUfsLU0Guid;
Vijay Kumar Pendoti09d572e2016-08-27 03:22:36 +0530503
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700504 Status =
505 GetBlkIOHandles (Attribs, &HandleFilter, HandleInfoList, &MaxHandles);
506 if (EFI_ERROR (Status))
507 return EFI_NOT_FOUND;
Vijay Kumar Pendoti09d572e2016-08-27 03:22:36 +0530508
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700509 Status =
510 gBS->HandleProtocol (HandleInfoList[0].Handle,
511 &gEfiMemCardInfoProtocolGuid, (VOID **)&CardInfo);
Vijay Kumar Pendoti09d572e2016-08-27 03:22:36 +0530512
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700513 if (Status != EFI_SUCCESS) {
514 DEBUG ((EFI_D_ERROR, "Error locating MemCardInfoProtocol:%x\n", Status));
515 return Status;
516 }
Vijay Kumar Pendoti09d572e2016-08-27 03:22:36 +0530517
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700518 if (CardInfo->Revision < EFI_MEM_CARD_INFO_PROTOCOL_REVISION) {
519 DEBUG ((EFI_D_ERROR, "This API not supported in Revision =%u\n",
520 CardInfo->Revision));
521 return EFI_NOT_FOUND;
522 }
Vijay Kumar Pendoti09d572e2016-08-27 03:22:36 +0530523
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700524 if (IsGet == TRUE) {
525 if (CardInfo->GetBootLU (CardInfo, UfsBootlun) == EFI_SUCCESS)
526 DEBUG ((EFI_D_VERBOSE, "Get BootLun =%u\n", *UfsBootlun));
527 } else {
528 if (CardInfo->SetBootLU (CardInfo, *UfsBootlun) == EFI_SUCCESS)
529 DEBUG ((EFI_D_VERBOSE, "SetBootLun =%u\n", *UfsBootlun));
530 }
531 return Status;
Vijay Kumar Pendoti09d572e2016-08-27 03:22:36 +0530532}
533
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700534EFI_STATUS
535BoardSerialNum (CHAR8 *StrSerialNum, UINT32 Len)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800536{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700537 EFI_STATUS Status = EFI_INVALID_PARAMETER;
538 MEM_CARD_INFO CardInfoData;
539 EFI_MEM_CARDINFO_PROTOCOL *CardInfo;
540 UINT32 SerialNo;
541 HandleInfo HandleInfoList[HANDLE_MAX_INFO_LIST];
542 UINT32 MaxHandles = ARRAY_SIZE (HandleInfoList);
543 MemCardType Type = EMMC;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800544
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700545 Type = CheckRootDeviceType ();
546 if (Type == UNKNOWN)
547 return EFI_NOT_FOUND;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800548
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700549 Status = GetDeviceHandleInfo (HandleInfoList, MaxHandles, Type);
550 if (EFI_ERROR (Status)) {
551 return Status;
552 }
Jeevan Shriramcf9ea6d2017-09-11 19:04:40 -0700553
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700554 Status =
555 gBS->HandleProtocol (HandleInfoList[0].Handle,
556 &gEfiMemCardInfoProtocolGuid, (VOID **)&CardInfo);
557 if (Status != EFI_SUCCESS) {
558 DEBUG ((EFI_D_ERROR, "Error locating MemCardInfoProtocol:%x\n", Status));
559 return Status;
560 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800561
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700562 if (CardInfo->GetCardInfo (CardInfo, &CardInfoData) == EFI_SUCCESS) {
563 if (Type == UFS) {
564 Status = gBS->CalculateCrc32 (CardInfoData.product_serial_num,
565 CardInfoData.serial_num_len, &SerialNo);
566 if (Status != EFI_SUCCESS) {
567 DEBUG ((EFI_D_ERROR,
568 "Error calculating Crc of the unicode serial number: %x\n",
569 Status));
570 return Status;
571 }
572 AsciiSPrint (StrSerialNum, Len, "%x", SerialNo);
573 } else {
574 AsciiSPrint (StrSerialNum, Len, "%x",
575 *(UINT32 *)CardInfoData.product_serial_num);
576 }
Vijay Kumar Pendoti78b1a962017-01-12 22:48:41 +0530577
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700578 /* adb is case sensitive, convert the serial number to lower case
579 * to maintain uniformity across the system. */
580 ToLower (StrSerialNum);
581 }
582 return Status;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800583}
Channagoud Kadabi539d3072016-02-11 21:34:48 -0800584
585/* Helper APIs for device tree selection */
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700586UINT32 BoardPlatformRawChipId (VOID)
Channagoud Kadabi539d3072016-02-11 21:34:48 -0800587{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700588 return platform_board_info.RawChipId;
Channagoud Kadabi539d3072016-02-11 21:34:48 -0800589}
590
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700591EFIChipInfoVersionType BoardPlatformChipVersion (VOID)
Channagoud Kadabi539d3072016-02-11 21:34:48 -0800592{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700593 return platform_board_info.ChipVersion;
Channagoud Kadabi539d3072016-02-11 21:34:48 -0800594}
595
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700596EFIChipInfoFoundryIdType BoardPlatformFoundryId (VOID)
Channagoud Kadabi539d3072016-02-11 21:34:48 -0800597{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700598 return platform_board_info.FoundryId;
Channagoud Kadabi539d3072016-02-11 21:34:48 -0800599}
600
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700601CHAR8 *BoardPlatformChipBaseBand (VOID)
Vijay Kumar Pendoti582158c2016-09-29 23:47:37 +0530602{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700603 return platform_board_info.ChipBaseBand;
Vijay Kumar Pendoti582158c2016-09-29 23:47:37 +0530604}
605
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700606EFI_PLATFORMINFO_PLATFORM_TYPE BoardPlatformType (VOID)
Channagoud Kadabi539d3072016-02-11 21:34:48 -0800607{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700608 return platform_board_info.PlatformInfo.platform;
Channagoud Kadabi539d3072016-02-11 21:34:48 -0800609}
610
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700611UINT32 BoardPlatformVersion (VOID)
Channagoud Kadabi539d3072016-02-11 21:34:48 -0800612{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700613 return platform_board_info.PlatformInfo.version;
Channagoud Kadabi539d3072016-02-11 21:34:48 -0800614}
615
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700616UINT32 BoardPlatformSubType (VOID)
Channagoud Kadabi539d3072016-02-11 21:34:48 -0800617{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700618 return platform_board_info.PlatformInfo.subtype;
Channagoud Kadabi539d3072016-02-11 21:34:48 -0800619}
Channagoud Kadabi5bd40992016-03-04 15:05:20 -0800620
Bhanuprakash Modem027aae52017-11-07 14:58:02 -0800621BOOLEAN BoardPlatformFusion (VOID)
622{
623 return platform_board_info.PlatformInfo.fusion;
624}
625
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700626UINT32 BoardTargetId (VOID)
Channagoud Kadabi5bd40992016-03-04 15:05:20 -0800627{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700628 UINT32 Target;
Channagoud Kadabi5bd40992016-03-04 15:05:20 -0800629
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700630 Target = (((platform_board_info.PlatformInfo.subtype & 0xff) << 24) |
631 (((platform_board_info.PlatformInfo.version >> 16) & 0xff) << 16) |
632 ((platform_board_info.PlatformInfo.version & 0xff) << 8) |
633 (platform_board_info.PlatformInfo.platform & 0xff));
Channagoud Kadabi5bd40992016-03-04 15:05:20 -0800634
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700635 return Target;
Channagoud Kadabi5bd40992016-03-04 15:05:20 -0800636}
lijuang834ebd22016-09-07 18:27:29 +0800637
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700638VOID
639BoardHwPlatformName (CHAR8 *StrHwPlatform, UINT32 Len)
lijuang834ebd22016-09-07 18:27:29 +0800640{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700641 EFI_STATUS Status;
642 EFI_CHIPINFO_PROTOCOL *pChipInfoProtocol;
643 UINT32 ChipIdValidLen = 4;
lijuang834ebd22016-09-07 18:27:29 +0800644
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700645 if (StrHwPlatform == NULL) {
646 DEBUG ((EFI_D_ERROR, "Error: HW Platform string is NULL\n"));
647 return;
648 }
lijuang834ebd22016-09-07 18:27:29 +0800649
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700650 Status = gBS->LocateProtocol (&gEfiChipInfoProtocolGuid, NULL,
651 (VOID **)&pChipInfoProtocol);
652 if (EFI_ERROR (Status)) {
653 DEBUG (
654 (EFI_D_ERROR, "Locate Protocol failed for gEfiChipInfoProtocolGuid\n"));
655 return;
656 }
lijuang834ebd22016-09-07 18:27:29 +0800657
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700658 Status = pChipInfoProtocol->GetChipIdString (
659 pChipInfoProtocol, StrHwPlatform,
660 EFICHIPINFO_MAX_ID_LENGTH > Len ? Len : EFICHIPINFO_MAX_ID_LENGTH);
661 if (EFI_ERROR (Status)) {
662 DEBUG ((EFI_D_ERROR, "Failed to Get the ChipIdString\n"));
663 return;
664 }
lijuanga0136d32017-09-07 15:22:51 +0800665
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700666 if (Len < ChipIdValidLen)
667 StrHwPlatform[Len - 1] = '\0';
668 else
669 StrHwPlatform[ChipIdValidLen - 1] = '\0';
lijuang834ebd22016-09-07 18:27:29 +0800670}