PPI编程
1 #include <Uefi.h> 2 #include <Library/DebugLib.h> 3 #include <Library/PcdLib.h> 4 #include <Library/PeiServicesLib.h> 5 #include <Pi/PiPeiCis.h> 6 #include <Pi/PiHob.h> 7 #include <Token.h> 8 #include <Ppi/ReadOnlyVariable2.h> 9 #include <Ppi/MyFirstPeiLib.h> 10 #include <Setup.h> 11 #include <Library/homeworkppi.h> 12 #include <Library/PeimEntryPoint.h> 13 #include <Library/BaseLib.h> 14 #include <Guid/homeworkitem.h> 15 #include <Uefi/UefiBaseType.h> 16 17 18 19 20 21 22 23 #define BscDefinedValue (10)//()表示赋值[]表示数组大小 24 25 EFI_GUID gHwPpiGuid = {0x37241739, 0x260e, 0x47ef, {0xbd, 0xa8, 0x6b, 0x9c, 0x90, 0xd0, 0xad, 0x1e}}; 26 // {0x5908a557,0x27f6,0x4ce6,{0x9d,0x06,0x10,0x04,0x76,0x1f,0x5b,0x67}} 27 EFI_STATUS HwPpiInterfaceInc (UINT8* TempValue) 28 { 29 BSC_DEBUG (DEBUG_INFO, "[homework]%a: In!!!\r\n", __func__); 30 BSC_DEBUG (DEBUG_INFO, "[homework]TempValue: %d\r\n", *TempValue);//这些是输出 31 (*TempValue)++;//要传递指针的 因为return只会是EFI_STATUS 32 BSC_DEBUG (DEBUG_INFO, "[homework]TempValue: %d\r\n", *TempValue); 33 BSC_DEBUG (DEBUG_INFO, "[homework]%a: Exit!!!\r\n", __func__); 34 35 return EFI_SUCCESS; 36 } 37 38 static HwPpi_s HwPpi = { 39 BscDefinedValue, 40 HwPpiInterfaceInc 41 };//新建的PPI 42 43 static EFI_PEI_PPI_DESCRIPTOR HwPpiList = { 44 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST), 45 &gHwPpiGuid,//GUID类型 46 &HwPpi 47 }; 48 49 VOID HomeWorkPpiInstall (VOID) 50 { 51 BSC_DEBUG (DEBUG_INFO, "[homework]%a: In!!!\r\n", __func__); 52 PeiServicesInstallPpi (&HwPpiList); 53 BSC_DEBUG (DEBUG_INFO, "[homework]%a: Exit!!!\r\n", __func__); 54 } 55 56 VOID HomeWorkPpiLocate (VOID) 57 { 58 EFI_STATUS Status = EFI_SUCCESS; 59 HwPpi_s *pHwPpi = NULL;//指针形式 60 UINT8 Data = 0; 61 62 BSC_DEBUG (DEBUG_INFO, "[homework]%a: In!!!\r\n", __func__); 63 Status = PeiServicesLocatePpi ( 64 &gHwPpiGuid, 0, NULL, (VOID **) &pHwPpi 65 ); 66 if (EFI_ERROR(Status)) { 67 BSC_DEBUG (DEBUG_INFO, "[homework]Locate pHwPpi Fail..%r\r\n", Status); 68 } 69 else 70 { 71 Data = pHwPpi->Value; 72 pHwPpi->Interface (&Data); 73 BSC_DEBUG (DEBUG_INFO, "[homework]PCD Value: %d\r\n", PcdGet8 (PcdBscDefinedValue)); 74 PcdSet8 (PcdBscDefinedValue, Data); 75 BSC_DEBUG (DEBUG_INFO, "[homework]Set PCD Value\r\n"); 76 BSC_DEBUG (DEBUG_INFO, "[homework]PCD Value: %d\r\n", PcdGet8 (PcdBscDefinedValue)); 77 } 78 BSC_DEBUG (DEBUG_INFO, "[homework]%a: Exit!!!\r\n", __func__); 79 } 80 81 static VOID PrintGuid2(UINT8 *pGuid) 82 { 83 EFI_GUID *RomGuid = (EFI_GUID*) pGuid; 84 85 if (RomGuid == NULL) return; 86 87 BSC_DEBUG (DEBUG_INFO, "%08lx-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x\n", 88 RomGuid->Data1, 89 RomGuid->Data2,RomGuid->Data3, 90 RomGuid->Data4[0],RomGuid->Data4[1],RomGuid->Data4[2],RomGuid->Data4[3], 91 RomGuid->Data4[4],RomGuid->Data4[5],RomGuid->Data4[6],RomGuid->Data4[7] 92 ); 93 } 94 95 VOID HomeWorkVariableHob (VOID) 96 { 97 EFI_STATUS Status = EFI_SUCCESS; 98 EFI_GUID gBscHwItemGuid = HOMEWORKITEM_GUID; 99 HomeworkHob_s *homeworkhob = NULL; 100 EFI_GUID homeworkHobGuid = HOMEWORK_HOB_GUID; 101 EFI_PEI_READ_ONLY_VARIABLE2_PPI *VariablePpi = NULL; 102 UINTN Size = 0; 103 UINT32 VariableAttr = 0; 104 HwItem_s HwItemData = {0}; 105 106 BSC_DEBUG (DEBUG_INFO, "[homework]%a: In!!!\r\n", __func__); 107 Size = sizeof (HwItem_s); 108 Status = PeiServicesLocatePpi ( 109 &gEfiPeiReadOnlyVariable2PpiGuid, 0, NULL, (VOID **)&VariablePpi 110 ); 111 if (EFI_ERROR(Status)) { 112 BSC_DEBUG (DEBUG_INFO, "[homework]Locate VariablePpi Fail..%r\r\n", Status); 113 } 114 else 115 { 116 Status = VariablePpi->GetVariable ( 117 VariablePpi, 118 L"homeworkitemvar", 119 &gBscHwItemGuid, 120 &VariableAttr, 121 &Size, 122 &HwItemData 123 ); 124 if (EFI_ERROR(Status)) { 125 BSC_DEBUG (DEBUG_INFO, "[homework]GetVariable HwItemData Fail..%r\r\n", Status); 126 } 127 else 128 { 129 PrintGuid2((UINT8*)(&gBscHwItemGuid)); 130 BSC_DEBUG (DEBUG_INFO, "[homework]HwItemData->HomeworkItemData Value: %d\r\n", HwItemData.HomeworkItemData); 131 } 132 133 Status = PeiServicesCreateHob (//创建HOB 134 EFI_HOB_TYPE_GUID_EXTENSION, 135 sizeof(HomeworkHob_s), 136 &homeworkhob 137 ); 138 if (EFI_ERROR(Status)) { 139 BSC_DEBUG (DEBUG_INFO, "[homework]CreateHob homeworkhob Fail..%r\r\n", Status); 140 } 141 else { 142 homeworkhob->HwHobGuidType.Name = homeworkHobGuid; 143 homeworkhob->homeworkdata = HwItemData.HomeworkItemData; 144 BSC_DEBUG (DEBUG_INFO, "[homework]homeworkhob->homeworkdata Value: %d\r\n", homeworkhob->homeworkdata); 145 } 146 } 147 BSC_DEBUG (DEBUG_INFO, "[homework]%a: Exit!!!\r\n", __func__); 148 }
D:\self\第二周\代码作业\homeworkppi.c
homeworkppi.h
1 #ifndef _HOMEWORKPPI_H_ 2 #define _HOMEWORKPPI_H_ 3 4 #define HOMEWORK_HOB_GUID \ 5 {0x60d01e51, 0xe37e, 0x4543, 0xb9, 0xb1, 0x88, 0xda, 0xc9, 0xaf, 0x5, 0xfb} 6 7 typedef EFI_STATUS (*INC_INTERFACE) (UINT8* TempValue); 8 typedef struct tagHwPpi { 9 UINT8 Value; 10 INC_INTERFACE Interface; 11 }HwPpi_s; 12 13 typedef struct tagHomeworkHob{ 14 EFI_HOB_GUID_TYPE HwHobGuidType; 15 UINT8 homeworkdata; 16 } HomeworkHob_s; 17 18 VOID HomeWorkPpiInstall (VOID); 19 VOID HomeWorkPpiLocate (VOID); 20 VOID HomeWorkVariableHob (VOID); 21 22 #endif
homeworkdxehob.c
1 #include <Guid/HobList.h> 2 #include <AmiDxeLib.h> 3 #include <Library/homeworkppi.h> 4 #include <Pi/PiHob.h> 5 #include <AmiLib.h> 6 #include <Library/PcdLib.h> 7 #include <Library/UefiBootServicesTableLib.h> 8 #include <IndustryStandard/SmBios.h> 9 #include <Library/MemoryAllocationLib.h> 10 #include <Protocol/Smbios.h> 11 #include <SmbiosDynamicUpdateDxe.h> 12 13 static UINT8 SmbiosData = 0; 14 15 VOID HomeWorkGetHob (IN EFI_SYSTEM_TABLE *SystemTable) 16 { 17 EFI_STATUS Status = EFI_SUCCESS; 18 EFI_GUID GuidHob = HOB_LIST_GUID; 19 EFI_GUID HomeworkHobGuid = HOMEWORK_HOB_GUID; 20 HomeworkHob_s *Homeworkhob = NULL; 21 VOID *pHobList = NULL; 22 23 BSC_DEBUG (DEBUG_INFO, "[homework]%a: In!!!\r\n", __func__); 24 pHobList = GetEfiConfigurationTable (SystemTable, &GuidHob); 25 if (!pHobList) { 26 BSC_DEBUG (DEBUG_INFO, "[homework]pHobList get Fail..%r\r\n"); 27 } 28 29 Homeworkhob = (HomeworkHob_s *) pHobList; 30 while (!EFI_ERROR(Status = FindNextHobByType (EFI_HOB_TYPE_GUID_EXTENSION, &Homeworkhob))) 31 { 32 if (guidcmp (&(*Homeworkhob).HwHobGuidType.Name, &HomeworkHobGuid) == 0) 33 break; 34 } 35 if (EFI_ERROR(Status)) 36 { 37 BSC_DEBUG (DEBUG_INFO, "[homework]Homeworkhob found Fail..%r\r\n"); 38 }else{ 39 BSC_DEBUG (DEBUG_INFO, "[homework]Homeworkhob->homeworkdata value: %d\r\n", Homeworkhob->homeworkdata); 40 SmbiosData = PcdGet8 (PcdBscDefinedValue) + Homeworkhob->homeworkdata; 41 BSC_DEBUG (DEBUG_INFO, "[homework]PcdBscDefinedValue value: %d\r\n", PcdGet8 (PcdBscDefinedValue)); 42 BSC_DEBUG (DEBUG_INFO, "[homework]33 SmbiosData value: %d\r\n", SmbiosData); 43 } 44 BSC_DEBUG (DEBUG_INFO, "[homework]%a: Exit!!!\r\n", __func__); 45 } 46 47 static VOID HomeWorkUpdateSmbiosType0 48 ( 49 IN EFI_EVENT Event, 50 IN VOID *Cotext 51 ) 52 { 53 EFI_STATUS Status = EFI_SUCCESS; 54 EFI_SMBIOS_PROTOCOL *SmbiosProtocol; 55 SMBIOS_TABLE_TYPE0 *Type0 = NULL; 56 EFI_SMBIOS_TYPE SmbiosType; 57 EFI_SMBIOS_HANDLE SmbiosHandle; 58 EFI_SMBIOS_TABLE_HEADER *SmbiosRecord = NULL; 59 UINT8 EntryNum = 0; 60 UINT8 Index = 0; 61 UINT8 *MemoryIndex = NULL; 62 UINTN SmbiosType0Size; 63 SMBIOS_TABLE_TYPE0_WITH_STRING TempType0Data; 64 CHAR16 *HwBiosVersionStr = NULL; 65 UINTN StringBufferSize = 0; 66 CHAR8 *AsciiStringBuffer = NULL; 67 68 BSC_DEBUG (DEBUG_INFO, "[homework]%a: In!!!\r\n", __func__); 69 Status = gBS->LocateProtocol ( 70 &gEfiSmbiosProtocolGuid, 71 NULL, 72 (VOID **) &SmbiosProtocol 73 ); 74 if (EFI_ERROR(Status)) { 75 BSC_DEBUG (DEBUG_INFO, "[homework]LocateProtocol SmbiosProtocol Fail..%r\r\n", Status); 76 } 77 else { 78 SmbiosType = EFI_SMBIOS_TYPE_BIOS_INFORMATION; 79 SmbiosHandle = SMBIOS_HANDLE_PI_RESERVED; 80 81 while (!(Status == EFI_NOT_FOUND && SmbiosHandle == SMBIOS_HANDLE_PI_RESERVED)) { 82 Status = SmbiosProtocol->GetNext (SmbiosProtocol, &SmbiosHandle, &SmbiosType, &SmbiosRecord, NULL); 83 if (!EFI_ERROR(Status)) { 84 EntryNum++; 85 } 86 } 87 BSC_DEBUG (DEBUG_INFO, "[homework]Type0 Recored Number: %d\r\n", EntryNum); 88 89 for (Index = 0; Index < EntryNum; Index++) 90 { 91 SmbiosType = EFI_SMBIOS_TYPE_BIOS_INFORMATION; 92 SmbiosHandle = SMBIOS_HANDLE_PI_RESERVED; 93 94 Status = SmbiosProtocol->GetNext (SmbiosProtocol, &SmbiosHandle, &SmbiosType, &SmbiosRecord, NULL); 95 if (EFI_ERROR(Status)) { 96 BSC_DEBUG (DEBUG_INFO, "[homework]SmbiosProtocol GetNext Fail..%r\r\n", Status); 97 } 98 99 Type0 = (SMBIOS_TABLE_TYPE0*) SmbiosRecord; 100 101 MemoryIndex = (UINT8*)(VOID*) Type0; 102 SmbiosType0Size = sizeof(SMBIOS_TABLE_TYPE0) + CalculateTheStringsSizeOfTheType ((MemoryIndex + sizeof(SMBIOS_TABLE_TYPE0))) - 1; 103 104 gBS->SetMem (&TempType0Data, sizeof(TempType0Data), 0); 105 gBS->CopyMem (&TempType0Data, Type0, SmbiosType0Size); 106 Type0 = (SMBIOS_TABLE_TYPE0*)&TempType0Data; 107 108 StringBufferSize = (sizeof (CHAR8)) * SMBIOS_STRING_MAX_LENGTH; 109 AsciiStringBuffer = AllocateZeroPool(StringBufferSize); 110 BSC_DEBUG (DEBUG_INFO, "[homework]33 SmbiosData value: %d\r\n", SmbiosData); 111 BSC_DEBUG (DEBUG_INFO, "[homework]Itoa AsciiStringBuffer: %s\r\n", Itoa (SmbiosData, AsciiStringBuffer, 10)); 112 BSC_DEBUG (DEBUG_INFO, "[homework]AsciiStringBuffer: %s\r\n", AsciiStringBuffer); 113 114 Status = SmbiosProtocol->UpdateString ( 115 SmbiosProtocol, 116 &SmbiosHandle, 117 (UINTN*)&(Type0-> BiosVersion), 118 AsciiStringBuffer 119 ); 120 gBS->FreePool (AsciiStringBuffer); 121 } 122 } 123 gBS->CloseEvent (Event); 124 BSC_DEBUG (DEBUG_INFO, "[homework]%a: Exit!!!\r\n", __func__); 125 } 126 127 VOID DynamicUpdateSmbiosType0(VOID) 128 { 129 EFI_STATUS Status = EFI_SUCCESS; 130 VOID *Registration = NULL; 131 EFI_EVENT Event = NULL; 132 133 BSC_DEBUG (DEBUG_INFO, "[homework]%a: In!!!\r\n", __func__); 134 Status = gBS->CreateEvent (EVT_NOTIFY_SIGNAL, TPL_CALLBACK, &HomeWorkUpdateSmbiosType0, 0, &Event); 135 if (!EFI_ERROR(Status)) { 136 (VOID)gBS->RegisterProtocolNotify (&gBdsAllDriversConnectedProtocolGuid, Event, &Registration); 137 } 138 BSC_DEBUG (DEBUG_INFO, "[homework]%a: Exit!!!\r\n", __func__); 139 }
1 #include <Uefi.h> 2 #include <Library/DebugLib.h> 3 #include <Library/PcdLib.h> 4 #include <Library/PeiServicesLib.h> 5 #include <Pi/PiPeiCis.h> 6 #include <Pi/PiHob.h> 7 #include <Token.h> 8 #include <Ppi/ReadOnlyVariable2.h> 9 #include <Ppi/MyFirstPeiLib.h> 10 #include <Setup.h> 11 #include <Library/homeworkppi.h> 12 #include <Library/PeimEntryPoint.h> 13 #include <Library/BaseLib.h> 14 #include <Guid/homeworkitem.h> 15 #include <Uefi/UefiBaseType.h> 16 17 #define BscDefinedValue 5 //PPI的数值 18 19 CONST EFI_GUID HPpiGuid = {0x5908a557,0x27f6,0x4ce6,{0x9d,0x06,0x10,0x04,0x76,0x1f,0x5b,0x67}}; //由在线GUID生成器生成的 20 21 EFI_STATUS 22 EFIAPI 23 BscAddFunction ( 24 IN UINT* Value 25 ) 26 { 27 (*Value)++; 28 return EFI_SUCCESS; 29 } 30 31 Static HwPpi_s HPpi = {//HwPpi_s 32 BscDefinedValue, 33 BscAddFunction 34 };//自行定义一个PPI,里面包含一个数值BscDefinedValue和一个函数 35 36 //自行定义PpiList 37 CONST EFI_PEI_PPI_DESCRIPTOR HPpiList = { 38 EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST, //标记描述特征 39 &HPpiGuid, 40 &HPpi //定义里要求这俩都是指针 所以这里得是地址& 41 };//要带; 42 43 44 45 EFI_STATUS 46 EFIAPI 47 HPpiInstall ( 48 VOID 49 ) 50 { 51 EFI_STATUS Status; 52 EFI_PEI_PPI_DESCRIPTOR HPpiList2; //局部变量? 53 54 Status = PeiServicesInstallPpi (&HPpiList2);//传PpiList的地址 55 if(EFI_ERROR(Status)) { 56 ASSERT_EFI_ERROR (Status); 57 return Status; 58 } 59 return EFI_SUCCESS; 60 } 61 62 63 64 65 EFI_STATUS 66 EFIAPI 67 HPpiLocate ( 68 VOID 69 ) 70 { 71 EFI_STATUS Status; 72 HwPpi_s HPpi2; 73 UINT Data; 74 75 DEBUG ((EFI_D_INFO, "Reset Required. \n")); 76 Status = PeiServicesLocatePpi ( 77 &HPpiGuid, 78 0, //Instance 79 NULL, //Descriptor 80 (VOID **) &HPpi2); //IN OUT VOID **Ppi 二级指针 81 //就只是把响应的ppi能拿上干活了 并不是返回保存信息的数据state只是是否可以使用的状态 82 83 if(EFI_ERROR (Status)) { 84 BSC_DEBUG (DEBUG_INFO, "[homework]Locate pHwPpi Fail..%r\r\n", Status); 85 DEBUG((DEBUG_ERROR | DEBUG_WARN, "Failed to register SwSmi 0x52\n")); 86 } else{ 87 //通过BscAddFunction把BscDefinedValue传入获取结果 88 Data = HPpi2.BscDefinedValue; 89 Status = HPpi2.BscAddFunction(&Data); 90 if(EFI_ERROR(Status)) { 91 ASSERT_EFI_ERROR (Status); 92 return Status; 93 } 94 //更新到自定义的数值类型PCD(PcdBscDefinedValue)内,并Debug Log打印该数值是否符合预期; 95 BSC_DEBUG (DEBUG_INFO, "[homework]PCD Value: %d\r\n", PcdGet8 (PcdBscDefinedValue)); 96 PcdSet8 (PcdBscDefinedValue, Data); 97 BSC_DEBUG (DEBUG_INFO, "[homework]Set PCD Value\r\n"); 98 BSC_DEBUG (DEBUG_INFO, "[homework]PCD Value: %d\r\n", PcdGet8 (PcdBscDefinedValue)); 99 //所以先 get再set再get 100 } 101 } 102 103 104 105 EFI_STATUS 106 EFIAPI 107 HPpiLocate ( 108 VOID 109 ) 110 { 111 EFI_STATUS Status; 112 UINT Size = 0; 113 EFI_PEI_READ_ONLY_VARIABLE2_PPI *VariablePpi = NULL; //设置开机启动项/开机密码等功能:使用Variable机制实现。 114 HwItem_s HwItemData = {0};//??? 115 116 117 Status = PeiServicesLocatePpi ( 118 &gEfiPeiReadOnlyVariable2PpiGuid, 119 0, 120 NULL, 121 (VOID **) &VariablePpi);//貌似参数就是这个形式 122 123 Size = sizeof (HwItem_s);//??? 124 125 if (!EFI_ERROR (Status)){ 126 Status = ReadOnlyVariable->GetVariable( 127 &gEfiSetupVariableGuid, 128 NULL, 129 &VariableSize, 130 &SetupData 131 ); 132 if (!EFI_ERROR (Status)) { 133 BootMode = BOOT_ON_FLASH_UPDATE; 134 PeiServicesSetBootMode(BootMode); 135 DEBUG ((EFI_D_INFO, " Change boot mode to BOOT_ON_FLASH_UPDATE\n")); 136 }else{ 137 DEBUG ((EFI_D_INFO, " No Capsule data found!\n")); 138 return Status; 139 } 140 }else { 141 DEBUG ((EFI_D_INFO, " Cannot locate CapsulePpi!\n")); 142 return Status; 143 } 144 145 146 147 148 149 150 151 152 153 154 155 156 DEBUG ((EFI_D_INFO, "Reset Required. \n")); 157 Status = 158 159 if(EFI_ERROR (Status)) { 160 BSC_DEBUG (DEBUG_INFO, "[homework]Locate pHwPpi Fail..%r\r\n", Status); 161 DEBUG((DEBUG_ERROR | DEBUG_WARN, "Failed to register SwSmi 0x52\n")); 162 } else{ 163 164 } 165 }