1 /* 2 * The Initial Developer of the Original Code is International 3 * Business Machines Corporation. Portions created by IBM 4 * Corporation are Copyright (C) 2005 International Business 5 * Machines Corporation. All Rights Reserved. 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the Common Public License as published by 9 * IBM Corporation; either version 1 of the License, or (at your option) 10 * any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * Common Public License for more details. 16 * 17 * You should have received a copy of the Common Public License 18 * along with this program; if not, a copy can be viewed at 19 * http://www.opensource.org/licenses/cpl1.0.php. 20 */ 21 22 #include <limits.h> 23 #include <arpa/inet.h> 24 25 #include "tpm_tspi.h" 26 #include "tpm_utils.h" 27 #include "tpm_nvcommon.h" 28 29 30 static BOOL nvindex_set; 31 static unsigned int nvindex; 32 static BOOL list_only; 33 TSS_HCONTEXT hContext = 0; 34 35 static int parse(const int aOpt, const char *aArg) 36 { 37 38 switch (aOpt) { 39 case 'i': 40 if (parseHexOrDecimal(aArg, &nvindex, 0, UINT_MAX, 41 "NVRAM index") != 0) 42 return -1; 43 44 nvindex_set = TRUE; 45 list_only = FALSE; 46 47 break; 48 49 case 'n': 50 list_only = TRUE; 51 nvindex_set = FALSE; 52 break; 53 54 default: 55 return -1; 56 } 57 return 0; 58 } 59 60 61 static void help(const char* aCmd) 62 { 63 logCmdHelp(aCmd); 64 logNVIndexCmdOption(); 65 logCmdOption("-n, --list-only", 66 _("Only list the defined NVRAM areas' indices.")); 67 } 68 69 70 static void pcrInfoShortDisplay(TPM_PCR_INFO_SHORT *tpis, const char *type) 71 { 72 UINT16 i, c; 73 74 c = 0; 75 76 logMsg("PCR %sselection:\n", type); 77 78 for (i = 0; i < tpis->pcrSelection.sizeOfSelect * 8; i++) { 79 if (tpis->pcrSelection.pcrSelect[(i / 8)] & (1 << (i & 0x7))) { 80 if (!c) 81 logMsg(" PCRs : "); 82 if (c) 83 logMsg(", "); 84 printf("%d", i); 85 c++; 86 } 87 } 88 89 if (c) 90 logMsg("\n"); 91 92 if (tpis->localityAtRelease) { 93 if (tpis->localityAtRelease == 0x1f) { 94 logMsg(" Localities : ALL\n"); 95 } else { 96 logMsg(" Localities : 0x%01x\n", tpis->localityAtRelease); 97 } 98 } 99 100 if (c) { 101 logMsg(" Hash : "); 102 for (i = 0; i < 20; i++) 103 logMsg("%02x", tpis->digestAtRelease.digest[i]); 104 logMsg("\n"); 105 } 106 } 107 108 109 static void nvindexDisplay(TSS_HTPM hTpm, UINT32 nvindex) 110 { 111 TSS_RESULT res; 112 char *buffer; 113 TPM_NV_DATA_PUBLIC *nvpub = NULL; 114 115 logMsg("NVRAM index : 0x%08x (%u)\n", nvindex, nvindex); 116 117 res = getNVDataPublic(hTpm, nvindex, &nvpub); 118 119 if (res != TSS_SUCCESS) 120 goto out; 121 122 pcrInfoShortDisplay(&nvpub->pcrInfoRead , "read "); 123 pcrInfoShortDisplay(&nvpub->pcrInfoWrite, "write "); 124 125 buffer = printValueAsStrings((unsigned int)nvpub->permission.attributes, 126 permvalues); 127 128 logMsg("Permissions : 0x%08x (%s)\n", nvpub->permission.attributes, buffer); 129 free(buffer); 130 buffer = NULL; 131 132 logMsg("bReadSTClear : %s\n", nvpub->bReadSTClear ? "TRUE" : "FALSE"); 133 logMsg("bWriteSTClear : %s\n", nvpub->bWriteSTClear ? "TRUE" : "FALSE"); 134 logMsg("bWriteDefine : %s\n", nvpub->bWriteDefine ? "TRUE" : "FALSE"); 135 136 logMsg("Size : %d (0x%x)\n", nvpub->dataSize, nvpub->dataSize); 137 138 139 out: 140 freeNVDataPublic(nvpub); 141 142 return; 143 } 144 145 146 int main(int argc, char **argv) 147 { 148 TSS_HTPM hTpm; 149 UINT32 ulResultLen; 150 BYTE *pResult = NULL; 151 int iRc = -1; 152 unsigned int i; 153 struct option hOpts[] = { 154 {"index" , required_argument, NULL, 'i'}, 155 {"list-only", no_argument, NULL, 'n'}, 156 {NULL , no_argument, NULL, 0}, 157 }; 158 159 initIntlSys(); 160 161 if (genericOptHandler 162 (argc, argv, "i:o:n", hOpts, 163 sizeof(hOpts) / sizeof(struct option), parse, help) != 0) 164 goto out; 165 166 if (contextCreate(&hContext) != TSS_SUCCESS) 167 goto out; 168 169 if (contextConnect(hContext) != TSS_SUCCESS) 170 goto out_close; 171 172 if (contextGetTpm(hContext, &hTpm) != TSS_SUCCESS) 173 goto out_close; 174 175 176 if (getCapability(hTpm, TSS_TPMCAP_NV_LIST, 0, NULL, 177 &ulResultLen, &pResult) != TSS_SUCCESS) { 178 goto out_close; 179 } 180 181 if (list_only) { 182 logMsg(_("The following NVRAM areas have been defined:\n")); 183 } 184 185 for (i = 0; i < ulResultLen/sizeof(UINT32); i++) { 186 UINT32 nvi; 187 nvi = Decode_UINT32(pResult + i * sizeof(UINT32)); 188 189 if (list_only) { 190 logMsg("0x%08x (%d)\n", nvi, nvi); 191 } else { 192 if ((nvindex_set && nvi == (UINT32)nvindex) || 193 !nvindex_set) { 194 nvindexDisplay(hTpm, nvi); 195 logMsg("\n"); 196 } 197 } 198 } 199 200 iRc = 0; 201 202 out_close: 203 contextClose(hContext); 204 205 out: 206 207 return iRc; 208 } 209