Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame^] | 1 | /* Copyright (c) 2015-2016, The Linux Foundation. All rights reserved. |
| 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 | #include <Board.h> |
| 30 | #include <Protocol/EFICardInfo.h> |
| 31 | #include <LinuxLoaderLib.h> |
| 32 | |
| 33 | EFI_STATUS BaseMem(UINTN *BaseMemory) |
| 34 | { |
| 35 | EFI_STATUS Status = EFI_NOT_FOUND; |
| 36 | EFI_RAMPARTITION_PROTOCOL *pRamPartProtocol = NULL; |
| 37 | RamPartitionEntry *RamPartitions = NULL; |
| 38 | UINT32 NumPartitions = 0; |
| 39 | UINTN SmallestBase; |
| 40 | UINT32 i = 0; |
| 41 | |
| 42 | Status = gBS->LocateProtocol(&gEfiRamPartitionProtocolGuid, NULL, (VOID**)&pRamPartProtocol); |
| 43 | if (EFI_ERROR(Status) || (&pRamPartProtocol == NULL)) |
| 44 | { |
| 45 | DEBUG((EFI_D_ERROR, "Locate EFI_RAMPARTITION_Protocol failed, Status = (0x%x)\r\n", Status)); |
| 46 | return EFI_NOT_FOUND; |
| 47 | } |
| 48 | |
| 49 | Status = pRamPartProtocol->GetRamPartitions (pRamPartProtocol, NULL, &NumPartitions); |
| 50 | if (Status == EFI_BUFFER_TOO_SMALL) |
| 51 | { |
| 52 | RamPartitions = AllocatePool (NumPartitions * sizeof (RamPartitionEntry)); |
| 53 | if (RamPartitions == NULL) |
| 54 | return EFI_OUT_OF_RESOURCES; |
| 55 | |
| 56 | Status = pRamPartProtocol->GetRamPartitions (pRamPartProtocol, RamPartitions, &NumPartitions); |
| 57 | if (EFI_ERROR (Status) || (NumPartitions < 1) ) |
| 58 | { |
| 59 | DEBUG((EFI_D_ERROR, "Failed to get RAM partitions")); |
| 60 | return EFI_NOT_FOUND; |
| 61 | } |
| 62 | } |
| 63 | SmallestBase = RamPartitions[0].Base; |
| 64 | for (i = 0; i < NumPartitions; i++) |
| 65 | { |
| 66 | if (SmallestBase > RamPartitions[i].Base) |
| 67 | SmallestBase = RamPartitions[i].Base; |
| 68 | } |
| 69 | *BaseMemory = SmallestBase; |
| 70 | DEBUG((EFI_D_ERROR, "Memory Base Address: 0x%x\n", *BaseMemory)); |
| 71 | |
| 72 | return EFI_SUCCESS; |
| 73 | } |
| 74 | |
| 75 | STATIC EFI_STATUS GetChipInfo(struct BoardInfo *platform_board_info) |
| 76 | { |
| 77 | EFI_STATUS Status; |
| 78 | EFI_CHIPINFO_PROTOCOL *pChipInfoProtocol; |
| 79 | Status = gBS->LocateProtocol (&gEfiChipInfoProtocolGuid, NULL,(VOID **) &pChipInfoProtocol); |
| 80 | if (EFI_ERROR(Status)) |
| 81 | return Status; |
| 82 | Status = pChipInfoProtocol->GetChipId(pChipInfoProtocol, &platform_board_info->RawChipId); |
| 83 | if (EFI_ERROR(Status)) |
| 84 | return Status; |
| 85 | Status = pChipInfoProtocol->GetChipVersion(pChipInfoProtocol, &platform_board_info->ChipVersion); |
| 86 | if (EFI_ERROR(Status)) |
| 87 | return Status; |
| 88 | Status = pChipInfoProtocol->GetFoundryId(pChipInfoProtocol, &platform_board_info->FoundryId); |
| 89 | if (EFI_ERROR(Status)) |
| 90 | return Status; |
| 91 | DEBUG((EFI_D_WARN, "Platform Info : 0x%x\n", platform_board_info->PlatformInfo.platform)); |
| 92 | DEBUG((EFI_D_WARN, "Raw Chip Id : 0x%x\n", platform_board_info->RawChipId)); |
| 93 | DEBUG((EFI_D_WARN, "Chip Version : 0x%x\n", platform_board_info->ChipVersion)); |
| 94 | DEBUG((EFI_D_WARN, "Foundry Id : 0x%x\n", platform_board_info->FoundryId)); |
| 95 | return Status; |
| 96 | } |
| 97 | |
| 98 | STATIC EFI_STATUS GetPlatformInfo(struct BoardInfo *platform_board_info) |
| 99 | { |
| 100 | EFI_STATUS eResult; |
| 101 | EFI_PLATFORMINFO_PROTOCOL *hPlatformInfoProtocol; |
| 102 | |
| 103 | eResult = gBS->LocateProtocol(&gEfiPlatformInfoProtocolGuid, NULL, (VOID **) &hPlatformInfoProtocol); |
| 104 | if (eResult != EFI_SUCCESS) |
| 105 | { |
| 106 | AsciiPrint("Error: Failed to locate PlatformInfo protocol.\n"); |
| 107 | goto endtest; |
| 108 | } |
| 109 | |
| 110 | eResult = hPlatformInfoProtocol->GetPlatformInfo(hPlatformInfoProtocol, &platform_board_info->PlatformInfo); |
| 111 | if (eResult != EFI_SUCCESS) |
| 112 | { |
| 113 | AsciiPrint("Error: GetPlatformInfo failed.\n"); |
| 114 | goto endtest; |
| 115 | } |
| 116 | |
| 117 | if (platform_board_info->PlatformInfo.platform >= EFI_PLATFORMINFO_NUM_TYPES) |
| 118 | { |
| 119 | AsciiPrint("Error: Unknown platform type (%d).\n", platform_board_info->PlatformInfo.platform); |
| 120 | eResult = EFI_PROTOCOL_ERROR; |
| 121 | goto endtest; |
| 122 | } |
| 123 | |
| 124 | //AsciiPrint ("Version: %d.%d\n", PlatformInfo.version >> 16, PlatformInfo.version & 0xFFFF); |
| 125 | |
| 126 | endtest: |
| 127 | return eResult; |
| 128 | } |
| 129 | |
| 130 | STATIC EFI_STATUS GetPmicInfo(UINT32 PmicDeviceIndex, EFI_PM_DEVICE_INFO_TYPE *pmic_info) |
| 131 | { |
| 132 | EFI_STATUS Status; |
| 133 | EFI_QCOM_PMIC_VERSION_PROTOCOL *pPmicVersionProtocol; |
| 134 | Status = gBS->LocateProtocol (&gQcomPmicVersionProtocolGuid, NULL, |
| 135 | (VOID **) &pPmicVersionProtocol); |
| 136 | if (EFI_ERROR(Status)) |
| 137 | return Status; |
| 138 | Status = pPmicVersionProtocol->GetPmicInfo(PmicDeviceIndex, pmic_info); |
| 139 | if (EFI_ERROR(Status)) |
| 140 | return Status; |
| 141 | return Status; |
| 142 | } |
| 143 | |
| 144 | UINT32 BoardPmicModel(UINT32 PmicDeviceIndex) |
| 145 | { |
| 146 | EFI_PM_DEVICE_INFO_TYPE pmic_info; |
| 147 | GetPmicInfo(PmicDeviceIndex, &pmic_info); |
| 148 | DEBUG((EFI_D_WARN, "PMIC Model 0x%x: 0x%x\n", PmicDeviceIndex, pmic_info.PmicModel)); |
| 149 | return pmic_info.PmicModel; |
| 150 | } |
| 151 | |
| 152 | UINT32 BoardPmicTarget(UINT32 PmicDeviceIndex) |
| 153 | { |
| 154 | UINT32 target; |
| 155 | EFI_PM_DEVICE_INFO_TYPE pmic_info; |
| 156 | GetPmicInfo(PmicDeviceIndex, &pmic_info); |
| 157 | target = (pmic_info.PmicAllLayerRevision << 16) | pmic_info.PmicModel; |
| 158 | DEBUG((EFI_D_WARN, "PMIC Target 0x%x: 0x%x\n", PmicDeviceIndex, target)); |
| 159 | return target; |
| 160 | } |
| 161 | |
| 162 | EFI_STATUS BoardInit(struct BoardInfo *platform_board_info) |
| 163 | { |
| 164 | EFI_STATUS Status; |
| 165 | Status = GetChipInfo(platform_board_info); |
| 166 | if (EFI_ERROR(Status)) |
| 167 | return Status; |
| 168 | Status = GetPlatformInfo(platform_board_info); |
| 169 | if (EFI_ERROR(Status)) |
| 170 | return Status; |
| 171 | return Status; |
| 172 | } |
| 173 | |
| 174 | EFI_STATUS BoardSerialNum(CHAR8 *StrSerialNum, UINT32 Len) |
| 175 | { |
| 176 | EFI_STATUS Status = EFI_INVALID_PARAMETER; |
| 177 | MEM_CARD_INFO CardInfoData; |
| 178 | EFI_MEM_CARDINFO_PROTOCOL *CardInfo; |
| 179 | extern EFI_GUID gEfiEmmcUserPartitionGuid; |
| 180 | extern EFI_GUID gEfiUfsLU0Guid; |
| 181 | UINT32 SerialNo; |
| 182 | HandleInfo HandleInfoList[128]; |
| 183 | UINT32 Attribs = 0; |
| 184 | UINT32 MaxHandles; |
| 185 | PartiSelectFilter HandleFilter; |
| 186 | MemCardType Type = EMMC; |
| 187 | |
| 188 | Attribs |= BLK_IO_SEL_MATCH_ROOT_DEVICE; |
| 189 | |
| 190 | MaxHandles = sizeof(HandleInfoList) / sizeof(*HandleInfoList); |
| 191 | HandleFilter.PartitionType = 0; |
| 192 | HandleFilter.VolumeName = 0; |
| 193 | HandleFilter.RootDeviceType = &gEfiEmmcUserPartitionGuid; |
| 194 | |
| 195 | Status = GetBlkIOHandles(Attribs, &HandleFilter, HandleInfoList, &MaxHandles); |
| 196 | if (EFI_ERROR (Status) || MaxHandles == 0) |
| 197 | { |
| 198 | MaxHandles = sizeof(HandleInfoList) / sizeof(*HandleInfoList); |
| 199 | HandleFilter.PartitionType = 0; |
| 200 | HandleFilter.VolumeName = 0; |
| 201 | HandleFilter.RootDeviceType = &gEfiUfsLU0Guid; |
| 202 | |
| 203 | Status = GetBlkIOHandles(Attribs, &HandleFilter, HandleInfoList, &MaxHandles); |
| 204 | if (EFI_ERROR (Status)) |
| 205 | return EFI_NOT_FOUND; |
| 206 | Type = UFS; |
| 207 | } |
| 208 | |
| 209 | Status = gBS->HandleProtocol(HandleInfoList[0].Handle, |
| 210 | &gEfiMemCardInfoProtocolGuid, |
| 211 | (VOID**)&CardInfo); |
| 212 | |
| 213 | if (Status != EFI_SUCCESS) |
| 214 | { |
| 215 | DEBUG((EFI_D_ERROR,"Error locating MemCardInfoProtocol:%x\n",Status)); |
| 216 | return Status; |
| 217 | } |
| 218 | |
| 219 | if (CardInfo->GetCardInfo (CardInfo, &CardInfoData) == EFI_SUCCESS) |
| 220 | { |
| 221 | if (Type == UFS) |
| 222 | { |
| 223 | Status = gBS->CalculateCrc32(CardInfoData.product_serial_num, CardInfoData.serial_num_len, &SerialNo); |
| 224 | if (Status != EFI_SUCCESS) |
| 225 | { |
| 226 | DEBUG((EFI_D_ERROR, "Error calculating Crc of the unicode serial number: %x\n", Status)); |
| 227 | return Status; |
| 228 | } |
| 229 | AsciiSPrint(StrSerialNum, Len, "%x", SerialNo); |
| 230 | } |
| 231 | else |
| 232 | AsciiSPrint(StrSerialNum, Len, "%x", CardInfoData.product_serial_num); |
| 233 | } |
| 234 | return Status; |
| 235 | } |