读取PCR值

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <tss/tss_error.h>
#include <tss/platform.h>
#include <tss/tss_defines.h>
#include <tss/tss_typedef.h>
#include <tss/tss_structs.h>
#include <tss/tspi.h>
#include <trousers/trousers.h>

#define Debug(message, tResult) printf("%s : %s\n", message, (char *)Trspi_Error_String(result))  
void printMenu();

int main(int argc, char **argv)
{
    TSS_HCONTEXT          hContext;
    TSS_HTPM                   hTPM;
    TSS_HPCRS                 hPcrs;
    TSS_HENCDATA         hEncData;
    TSS_HENCDATA         hRetrieveData;
    TSS_RESULT               result;
    TSS_HKEY                   hSRK = 0;
    TSS_HPOLICY            hSRKPolicy = 0;
    TSS_UUID                  SRK_UUID = TSS_UUID_SRK;

    BYTE                           wks[20];
    BYTE                           *pubKey;
    UINT32                       pubKeySize;
    BYTE                           *rgbPcrValue;
    UINT32                      ulPcrLen;
    BYTE                           *encData;
    UINT32                      encDataSize;
    BYTE                           *outstring;
    UINT32                      outlength;
    FILE                            *fout, *fin;
    int                               i;

    UINT32                      j;
    BYTE                          valueToExtend[250];
    int                              count = 0;
    int                             pcrToExtend = 0;

    FILE                         *fpt;
    memset(wks, 0, 20);
    memset(valueToExtend, 0, 250);

    //Pick the TPM you are talking to.   
    //In this case, it is the system TPM(indicated with NULL)  
    result = Tspi_Context_Create(&hContext);
    Debug("Create Context", result);

    //result = Tspi_Context_Connect(hContext, sm-Junyi-M580);  
    result = Tspi_Context_Connect(hContext, NULL);
    Debug("Context Connect", result);

    //Get the TPM handle  
    result = Tspi_Context_GetTpmObject(hContext, &hTPM);
    Debug("Get TPM Handle", result);

    //Get the SRK handle  
    result = Tspi_Context_LoadKeyByUUID(hContext, TSS_PS_TYPE_SYSTEM, SRK_UUID, &hSRK);
    Debug("Get the SRK handle", result);

    //Get the SRK policy  
    result = Tspi_GetPolicyObject(hSRK, TSS_POLICY_USAGE, &hSRKPolicy);
    Debug("Get the SRK policy", result);

    //Then set the SRK policy to be the well known secret  
    result = Tspi_Policy_SetSecret(hSRKPolicy, TSS_SECRET_MODE_SHA1, 20, wks);

printf("ssss");
    //输出所有PCR寄存器内的值  
    /*********************/
    fpt= fopen("731pcr.txt","w");
    ftruncate(fpt,0);
    if(fpt==NULL)

         printf("文件读取失败\n");
    for (j = 0; j < 24; j++)
    {
        result = Tspi_TPM_PcrRead(hTPM, j, &ulPcrLen, &rgbPcrValue);
        printf("PCR %02d ", j);
        fprintf(fpt,"PCR %02d ",j);
        for (i = 0; i < 19; i++)
        {
            printf("%02x", *(rgbPcrValue + i));
            fprintf(fpt," %02x",  *(rgbPcrValue + i));
        }
        printf("\n");
        fprintf(fpt,"\n");
    }
    fclose(fpt);
    /*********************/

    //Display each command line argument.  
    printf("\n Command line arguments:\n");
    for (count = 0; count <argc; count++)
        printf("argv[%d] : %s\n", count, argv[count]);

    //Examine command line arguments.  
    if (argc >= 3)
    {
        if (strcmp(argv[1],"-p") == 0)
        {
            pcrToExtend = atoi(argv[2]);
            if (pcrToExtend < 0 || pcrToExtend > 23)
            {
                printMenu();
                return 0;
            }
        }

        if (argc == 5)
        {
            if (strcmp(argv[3], "-v") == 0)
                memcpy(valueToExtend, argv[4], strlen(argv[4]));

         }
        else    //Use default value.  
        {
            memcpy(valueToExtend, "abcdefghijklmnopqrst", 20);
        }
    }
    else
    {
        printMenu();
        return 0;
    }

    //Extend the value  
    result = Tspi_TPM_PcrExtend(hTPM, pcrToExtend, 20, (BYTE *)valueToExtend, NULL, &ulPcrLen, &rgbPcrValue);
    Debug("Extended the PCR", result);

    //输出所有PCR寄存器内的值  
    /*********************/
    for (j = 0; j < 24; j++)
    {
        result = Tspi_TPM_PcrRead(hTPM, j, &ulPcrLen, &rgbPcrValue);
        printf("PCR %02d ", j);
        for (i = 0; i < 19; i++)
            printf("%02x", *(rgbPcrValue + i));
        printf("\n");
    }
    /*********************/


    //Clean up  
    Tspi_Context_FreeMemory(hContext, NULL);
    Tspi_Context_Close(hContext);

    return 0;
}

void printMenu()
{
    printf("\nChangePCRn Help Menu:\n");
    printf("\t -p PCR regiter to extend(0-23)\n");
    printf("\t -v Value to be extended into PCR(abc...)\n");
    printf("\t Note: -v argument is optional and a default value will be used if no value is provided\n");
    printf("\t Example: ChangePCRn -p 10 -v abcdef\n");
}

编译:gcc  readpcr.c -o read -ltspi

           ./read

运行时需开启TPM模拟器和tcs:      tpmd    -f  -d

                                                             tcsd    -e   -f

 

 

转载于:https://my.oschina.net/u/3548719/blog/1545597

好的,我明白了。为了配置PORTB0,PORTB1,PORTB2和PORTB3为16位ADC,您可以使用MK22FN512VLH12的官方库函数,具体步骤如下: 1. 首先需要打开ADC模块。可以使用ADC_Init()函数实现。 2. 接下来需要配置ADC通道。可以使用ADC_Configuration()函数实现。 3. 然后需要设置ADC采样时钟。可以使用ADC_Clock_Set()函数实现。 4. 最后,需要启动ADC转换。可以使用ADC_Start()函数实现。 下面是一份示例代码,您可以参考一下: ```c #include "MK22F51212.h" void ADC_Init(void) { SIM->SCGC6 |= SIM_SCGC6_ADC0_MASK; // Enable ADC0 clock gate ADC0->CFG1 |= ADC_CFG1_ADICLK(0); // Clock source: Bus clock / 2 ADC0->CFG1 |= ADC_CFG1_ADIV(0); // Clock divide: 1 ADC0->CFG1 |= ADC_CFG1_MODE(3); // 16-bit mode ADC0->SC2 |= ADC_SC2_ADTRG_MASK; // Enable hardware trigger } void ADC_Configuration(void) { PORTB->PCR[0] |= PORT_PCR_MUX(0); // Set PORTB0 to ADC0_SE8 PORTB->PCR[1] |= PORT_PCR_MUX(0); // Set PORTB1 to ADC0_SE9 PORTB->PCR[2] |= PORT_PCR_MUX(0); // Set PORTB2 to ADC0_SE10 PORTB->PCR[3] |= PORT_PCR_MUX(0); // Set PORTB3 to ADC0_SE11 ADC0->SC1[0] |= ADC_SC1_ADCH(8); // Select ADC0_SE8 as input channel ADC0->SC1[1] |= ADC_SC1_ADCH(9); // Select ADC0_SE9 as input channel ADC0->SC1[2] |= ADC_SC1_ADCH(10); // Select ADC0_SE10 as input channel ADC0->SC1[3] |= ADC_SC1_ADCH(11); // Select ADC0_SE11 as input channel } void ADC_Clock_Set(void) { SIM->SCGC6 |= SIM_SCGC6_ADC0_MASK; // Enable ADC0 clock gate ADC0->CFG2 |= ADC_CFG2_ADHSC_MASK; // High speed conversion mode ADC0->CFG1 |= ADC_CFG1_ADLSMP_MASK; // Long sample time ADC0->CFG1 |= ADC_CFG1_ADICLK(0); // Clock source: Bus clock / 2 ADC0->CFG1 |= ADC_CFG1_ADIV(0); // Clock divide: 1 } void ADC_Start(void) { ADC0->SC1[0] |= ADC_SC1_ADCH(8); // Start conversion on ADC0_SE8 ADC0->SC1[1] |= ADC_SC1_ADCH(9); // Start conversion on ADC0_SE9 ADC0->SC1[2] |= ADC_SC1_ADCH(10); // Start conversion on ADC0_SE10 ADC0->SC1[3] |= ADC_SC1_ADCH(11); // Start conversion on ADC0_SE11 } int ADC_Read(int channel) { int result = 0; switch (channel) { case 0: result = ADC0->R[0]; break; case 1: result = ADC0->R[1]; break; case 2: result = ADC0->R[2]; break; case 3: result = ADC0->R[3]; break; default: break; } return result; } void ADC_Set_Channel(int channel) { switch (channel) { case 0: PORTB->PCR[0] |= PORT_PCR_MUX(0); PORTB->PCR[1] &= ~PORT_PCR_MUX(0); PORTB->PCR[2] &= ~PORT_PCR_MUX(0); PORTB->PCR[3] &= ~PORT_PCR_MUX(0); ADC0->SC1[0] |= ADC_SC1_ADCH(8); break; case 1: PORTB->PCR[0] &= ~PORT_PCR_MUX(0); PORTB->PCR[1] |= PORT_PCR_MUX(0); PORTB->PCR[2] &= ~PORT_PCR_MUX(0); PORTB->PCR[3] &= ~PORT_PCR_MUX(0); ADC0->SC1[1] |= ADC_SC1_ADCH(9); break; case 2: PORTB->PCR[0] &= ~PORT_PCR_MUX(0); PORTB->PCR[1] &= ~PORT_PCR_MUX(0); PORTB->PCR[2] |= PORT_PCR_MUX(0); PORTB->PCR[3] &= ~PORT_PCR_MUX(0); ADC0->SC1[2] |= ADC_SC1_ADCH(10); break; case 3: PORTB->PCR[0] &= ~PORT_PCR_MUX(0); PORTB->PCR[1] &= ~PORT_PCR_MUX(0); PORTB->PCR[2] &= ~PORT_PCR_MUX(0); PORTB->PCR[3] |= PORT_PCR_MUX(0); ADC0->SC1[3] |= ADC_SC1_ADCH(11); break; default: break; } } int ADC_Get_Value(int channel) { ADC_Set_Channel(channel); while (!(ADC0->SC1[channel] & ADC_SC1_COCO_MASK)); return ADC_Read(channel); } ``` 上面的代码实现了配置PORTB0,PORTB1,PORTB2和PORTB3为16位ADC,并且写了一个读取采样函数设置PORTB0为0,PORTB1为1,PORTB2为2和PORTB3为3,输入函数(0)就输出ADC通道PORTB0采样的。您可以根据需要进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值