TcgStorageUtil.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. /** @file
  2. Provide functions to provide tcg storage core spec related functions.
  3. Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <Library/TcgStorageCoreLib.h>
  7. #include <Library/BaseLib.h>
  8. #include <Library/BaseMemoryLib.h>
  9. #include <Library/DebugLib.h>
  10. typedef struct {
  11. UINT16 FeatureCode;
  12. TCG_LEVEL0_FEATURE_DESCRIPTOR_HEADER *Feature;
  13. UINTN FeatureSize;
  14. } TCG_FIND_FEATURE_CTX;
  15. /**
  16. Returns a human-readable string representing a method status return code.
  17. @param[in] MethodStatus Method status to translate to a string
  18. @retval return the string info.
  19. **/
  20. CHAR8 *
  21. EFIAPI
  22. TcgMethodStatusString (
  23. UINT8 MethodStatus
  24. )
  25. {
  26. switch (MethodStatus) {
  27. #define C(status) case TCG_METHOD_STATUS_CODE_ ## status: return #status
  28. C (SUCCESS);
  29. C (NOT_AUTHORIZED);
  30. C (OBSOLETE);
  31. C (SP_BUSY);
  32. C (SP_FAILED);
  33. C (SP_DISABLED);
  34. C (SP_FROZEN);
  35. C (NO_SESSIONS_AVAILABLE);
  36. C (UNIQUENESS_CONFLICT);
  37. C (INSUFFICIENT_SPACE);
  38. C (INSUFFICIENT_ROWS);
  39. C (INVALID_PARAMETER);
  40. C (OBSOLETE2);
  41. C (OBSOLETE3);
  42. C (TPER_MALFUNCTION);
  43. C (TRANSACTION_FAILURE);
  44. C (RESPONSE_OVERFLOW);
  45. C (AUTHORITY_LOCKED_OUT);
  46. C (FAIL);
  47. #undef C
  48. }
  49. return "unknown";
  50. }
  51. /**
  52. adds call token and method Header (invoking id, and method id).
  53. @param CreateStruct The input create structure.
  54. @param InvokingId Invoking id.
  55. @param MethodId Method id.
  56. **/
  57. TCG_RESULT
  58. EFIAPI
  59. TcgStartMethodCall (
  60. TCG_CREATE_STRUCT *CreateStruct,
  61. TCG_UID InvokingId,
  62. TCG_UID MethodId
  63. )
  64. {
  65. NULL_CHECK (CreateStruct);
  66. if ((CreateStruct->ComPacket == NULL) ||
  67. (CreateStruct->CurPacket == NULL) ||
  68. (CreateStruct->CurSubPacket == NULL)
  69. )
  70. {
  71. DEBUG ((DEBUG_INFO, "unexpected state: ComPacket=%p CurPacket=%p CurSubPacket=%p\n", CreateStruct->ComPacket, CreateStruct->CurPacket, CreateStruct->CurSubPacket));
  72. return (TcgResultFailureInvalidAction);
  73. }
  74. ERROR_CHECK (TcgAddCall (CreateStruct));
  75. ERROR_CHECK (TcgAddTcgUid (CreateStruct, InvokingId));
  76. ERROR_CHECK (TcgAddTcgUid (CreateStruct, MethodId));
  77. return TcgResultSuccess;
  78. }
  79. /**
  80. Adds START LIST token.
  81. @param CreateStruct The input create structure.
  82. **/
  83. TCG_RESULT
  84. EFIAPI
  85. TcgStartParameters (
  86. TCG_CREATE_STRUCT *CreateStruct
  87. )
  88. {
  89. NULL_CHECK (CreateStruct);
  90. if ((CreateStruct->ComPacket == NULL) ||
  91. (CreateStruct->CurPacket == NULL) ||
  92. (CreateStruct->CurSubPacket == NULL)
  93. )
  94. {
  95. DEBUG ((DEBUG_INFO, "unexpected state: ComPacket=%p CurPacket=%p CurSubPacket=%p\n", CreateStruct->ComPacket, CreateStruct->CurPacket, CreateStruct->CurSubPacket));
  96. return (TcgResultFailureInvalidAction);
  97. }
  98. return TcgAddStartList (CreateStruct);
  99. }
  100. /**
  101. Adds END LIST token.
  102. @param CreateStruct The input create structure.
  103. **/
  104. TCG_RESULT
  105. EFIAPI
  106. TcgEndParameters (
  107. TCG_CREATE_STRUCT *CreateStruct
  108. )
  109. {
  110. NULL_CHECK (CreateStruct);
  111. if ((CreateStruct->ComPacket == NULL) ||
  112. (CreateStruct->CurPacket == NULL) ||
  113. (CreateStruct->CurSubPacket == NULL)
  114. )
  115. {
  116. DEBUG ((DEBUG_INFO, "unexpected state: ComPacket=%p CurPacket=%p CurSubPacket=%p\n", CreateStruct->ComPacket, CreateStruct->CurPacket, CreateStruct->CurSubPacket));
  117. return (TcgResultFailureInvalidAction);
  118. }
  119. return TcgAddEndList (CreateStruct);
  120. }
  121. /**
  122. Adds END Data token and method list.
  123. @param CreateStruct The input create structure.
  124. **/
  125. TCG_RESULT
  126. EFIAPI
  127. TcgEndMethodCall (
  128. TCG_CREATE_STRUCT *CreateStruct
  129. )
  130. {
  131. NULL_CHECK (CreateStruct);
  132. if ((CreateStruct->ComPacket == NULL) ||
  133. (CreateStruct->CurPacket == NULL) ||
  134. (CreateStruct->CurSubPacket == NULL)
  135. )
  136. {
  137. DEBUG ((DEBUG_INFO, "unexpected state: ComPacket=%p CurPacket=%p CurSubPacket=%p\n", CreateStruct->ComPacket, CreateStruct->CurPacket, CreateStruct->CurSubPacket));
  138. return (TcgResultFailureInvalidAction);
  139. }
  140. ERROR_CHECK (TcgAddEndOfData (CreateStruct));
  141. ERROR_CHECK (TcgAddStartList (CreateStruct));
  142. ERROR_CHECK (TcgAddUINT8 (CreateStruct, 0x00)); // expected to complete properly
  143. ERROR_CHECK (TcgAddUINT8 (CreateStruct, 0x00)); // reserved
  144. ERROR_CHECK (TcgAddUINT8 (CreateStruct, 0x00)); // reserved
  145. ERROR_CHECK (TcgAddEndList (CreateStruct));
  146. return TcgResultSuccess;
  147. }
  148. /**
  149. Retrieves the comID and Extended comID of the ComPacket in the Tcg response.
  150. It is intended to be used to confirm the received Tcg response is intended for user that received it.
  151. @param [in] ParseStruct Structure used to parse received TCG response.
  152. @param [in/out] ComId comID retrieved from received ComPacket.
  153. @param [in/out] ComIdExtension Extended comID retrieved from received ComPacket
  154. **/
  155. TCG_RESULT
  156. EFIAPI
  157. TcgGetComIds (
  158. const TCG_PARSE_STRUCT *ParseStruct,
  159. UINT16 *ComId,
  160. UINT16 *ComIdExtension
  161. )
  162. {
  163. NULL_CHECK (ParseStruct);
  164. NULL_CHECK (ComId);
  165. NULL_CHECK (ComIdExtension);
  166. if (ParseStruct->ComPacket == NULL) {
  167. DEBUG ((DEBUG_INFO, "unexpected state: ComPacket=%p\n", ParseStruct->ComPacket));
  168. return TcgResultFailureInvalidAction;
  169. }
  170. *ComId = SwapBytes16 (ParseStruct->ComPacket->ComIDBE);
  171. *ComIdExtension = SwapBytes16 (ParseStruct->ComPacket->ComIDExtensionBE);
  172. return TcgResultSuccess;
  173. }
  174. /**
  175. Checks if the ComIDs of the response match the expected values.
  176. @param[in] ParseStruct Structure used to parse received TCG response
  177. @param[in] ExpectedComId Expected comID
  178. @param[in] ExpectedComIdExtension Expected extended comID
  179. **/
  180. TCG_RESULT
  181. EFIAPI
  182. TcgCheckComIds (
  183. const TCG_PARSE_STRUCT *ParseStruct,
  184. UINT16 ExpectedComId,
  185. UINT16 ExpectedComIdExtension
  186. )
  187. {
  188. UINT16 ParseComId;
  189. UINT16 ParseComIdExtension;
  190. ERROR_CHECK (TcgGetComIds (ParseStruct, &ParseComId, &ParseComIdExtension));
  191. if ((ParseComId != ExpectedComId) || (ParseComIdExtension != ExpectedComIdExtension)) {
  192. DEBUG ((DEBUG_INFO, "Com ID: Actual 0x%02X Expected 0x%02X\n", ParseComId, ExpectedComId));
  193. DEBUG ((DEBUG_INFO, "Extended Com ID: 0x%02X Expected 0x%02X\n", ParseComIdExtension, ExpectedComIdExtension));
  194. return TcgResultFailure;
  195. }
  196. return TcgResultSuccess;
  197. }
  198. /**
  199. Returns the method status of the current subpacket. Does not affect the current position
  200. in the ComPacket. In other words, it can be called whenever you have a valid SubPacket.
  201. @param [in/out] ParseStruct Structure used to parse received TCG response
  202. @param [in/out] MethodStatus Method status retrieved of the current SubPacket
  203. **/
  204. TCG_RESULT
  205. EFIAPI
  206. TcgGetMethodStatus (
  207. const TCG_PARSE_STRUCT *ParseStruct,
  208. UINT8 *MethodStatus
  209. )
  210. {
  211. TCG_PARSE_STRUCT TmpParseStruct;
  212. TCG_TOKEN TcgToken;
  213. UINT8 Reserved1, Reserved2;
  214. NULL_CHECK (ParseStruct);
  215. NULL_CHECK (MethodStatus);
  216. if ((ParseStruct->ComPacket == NULL) ||
  217. (ParseStruct->CurPacket == NULL) ||
  218. (ParseStruct->CurSubPacket == NULL)
  219. )
  220. {
  221. DEBUG ((DEBUG_INFO, "unexpected state: ComPacket=%p CurPacket=%p CurSubPacket=%p\n", ParseStruct->ComPacket, ParseStruct->CurPacket, ParseStruct->CurSubPacket));
  222. return TcgResultFailureInvalidAction;
  223. }
  224. // duplicate ParseStruct, then don't need to "reset" location cur ptr
  225. CopyMem (&TmpParseStruct, ParseStruct, sizeof (TCG_PARSE_STRUCT));
  226. // method status list exists after the end method call in the subpacket
  227. // skip tokens until ENDDATA is found
  228. do {
  229. ERROR_CHECK (TcgGetNextToken (&TmpParseStruct, &TcgToken));
  230. } while (TcgToken.Type != TcgTokenTypeEndOfData);
  231. // only reach here if enddata is found
  232. // at this point, the curptr is pointing at method status list beginning
  233. ERROR_CHECK (TcgGetNextStartList (&TmpParseStruct));
  234. ERROR_CHECK (TcgGetNextUINT8 (&TmpParseStruct, MethodStatus));
  235. ERROR_CHECK (TcgGetNextUINT8 (&TmpParseStruct, &Reserved1));
  236. ERROR_CHECK (TcgGetNextUINT8 (&TmpParseStruct, &Reserved2));
  237. ERROR_CHECK (TcgGetNextEndList (&TmpParseStruct));
  238. if (Reserved1 != 0) {
  239. DEBUG ((DEBUG_INFO, "Method status reserved1 = 0x%02X (expected 0)\n", Reserved1));
  240. return TcgResultFailure;
  241. }
  242. if (Reserved2 != 0) {
  243. DEBUG ((DEBUG_INFO, "Method status reserved2 = 0x%02X (expected 0)\n", Reserved1));
  244. return TcgResultFailure;
  245. }
  246. return TcgResultSuccess;
  247. }
  248. /**
  249. Return the toke type string info.
  250. @param Type Input the type info.
  251. @retval Return the string for this type.
  252. **/
  253. CHAR8 *
  254. EFIAPI
  255. TcgTokenTypeString (
  256. TCG_TOKEN_TYPE Type
  257. )
  258. {
  259. switch (Type) {
  260. case TcgTokenTypeReserved: return "Reserved";
  261. case TcgTokenTypeTinyAtom: return "Tiny Atom";
  262. case TcgTokenTypeShortAtom: return "Short Atom";
  263. case TcgTokenTypeMediumAtom: return "Medium Atom";
  264. case TcgTokenTypeLongAtom: return "Long Atom";
  265. case TcgTokenTypeStartList: return "Start List";
  266. case TcgTokenTypeEndList: return "End List";
  267. case TcgTokenTypeStartName: return "Start Name";
  268. case TcgTokenTypeEndName: return "End Name";
  269. case TcgTokenTypeCall: return "Call";
  270. case TcgTokenTypeEndOfData: return "End of Data";
  271. case TcgTokenTypeEndOfSession: return "End of Session";
  272. case TcgTokenTypeStartTransaction: return "Start Transaction";
  273. case TcgTokenTypeEndTransaction: return "End Transaction";
  274. case TcgTokenTypeEmptyAtom: return "Empty atom";
  275. }
  276. return "Unknown";
  277. }
  278. /**
  279. Adds Start Session call to the data structure. This creates the entire ComPacket structure and
  280. returns the size of the entire compacket in the size parameter.
  281. @param [in/out] CreateStruct Structure used to add the start session call
  282. @param [in/out] Size Describes the size of the entire ComPacket (header and payload). Filled out by function.
  283. @param [in] ComId ComID for the ComPacket
  284. @param [in] ComIdExtension Extended ComID for the ComPacket
  285. @param [in] HostSessionId Host Session ID
  286. @param [in] SpId Security Provider to start session with
  287. @param [in] Write Write option for start session. TRUE = start session requests write access
  288. @param [in] HostChallengeLength Length of the host challenge. Length should be 0 if hostChallenge is NULL
  289. @param [in] HostChallenge Host challenge for Host Signing Authority. If NULL, then no Host Challenge shall be sent.
  290. @param [in] HostSigningAuthority Host Signing Authority used for start session. If NULL, then no Host Signing Authority shall be sent.
  291. **/
  292. TCG_RESULT
  293. EFIAPI
  294. TcgCreateStartSession (
  295. TCG_CREATE_STRUCT *CreateStruct,
  296. UINT32 *Size,
  297. UINT16 ComId,
  298. UINT16 ComIdExtension,
  299. UINT32 HostSessionId,
  300. TCG_UID SpId,
  301. BOOLEAN Write,
  302. UINT32 HostChallengeLength,
  303. const VOID *HostChallenge,
  304. TCG_UID HostSigningAuthority
  305. )
  306. {
  307. ERROR_CHECK (TcgStartComPacket (CreateStruct, ComId, ComIdExtension));
  308. ERROR_CHECK (TcgStartPacket (CreateStruct, 0x0, 0x0, 0x0, 0x0, 0x0));
  309. ERROR_CHECK (TcgStartSubPacket (CreateStruct, 0x0));
  310. ERROR_CHECK (TcgStartMethodCall (CreateStruct, TCG_UID_SMUID, TCG_UID_SM_START_SESSION));
  311. ERROR_CHECK (TcgStartParameters (CreateStruct));
  312. ERROR_CHECK (TcgAddUINT32 (CreateStruct, HostSessionId));
  313. ERROR_CHECK (TcgAddTcgUid (CreateStruct, SpId));
  314. ERROR_CHECK (TcgAddBOOLEAN (CreateStruct, Write));
  315. // optional parameters
  316. if ((HostChallenge != NULL) && (HostChallengeLength != 0)) {
  317. ERROR_CHECK (TcgAddStartName (CreateStruct));
  318. ERROR_CHECK (TcgAddUINT8 (CreateStruct, 0x00)); // TODO Create Enum for Method Optional Parameters?
  319. ERROR_CHECK (TcgAddByteSequence (CreateStruct, HostChallenge, HostChallengeLength, FALSE));
  320. ERROR_CHECK (TcgAddEndName (CreateStruct));
  321. }
  322. // optional parameters
  323. if (HostSigningAuthority != 0) {
  324. ERROR_CHECK (TcgAddStartName (CreateStruct));
  325. ERROR_CHECK (TcgAddUINT8 (CreateStruct, 0x03)); // TODO Create Enum for Method Optional Parameters?
  326. ERROR_CHECK (TcgAddTcgUid (CreateStruct, HostSigningAuthority));
  327. ERROR_CHECK (TcgAddEndName (CreateStruct));
  328. }
  329. ERROR_CHECK (TcgEndParameters (CreateStruct));
  330. ERROR_CHECK (TcgEndMethodCall (CreateStruct));
  331. ERROR_CHECK (TcgEndSubPacket (CreateStruct));
  332. ERROR_CHECK (TcgEndPacket (CreateStruct));
  333. ERROR_CHECK (TcgEndComPacket (CreateStruct, Size));
  334. return TcgResultSuccess;
  335. }
  336. /**
  337. Parses the Sync Session response contained in the parseStruct to retrieve Tper session ID. If the Sync Session response
  338. parameters do not match the comID, extended ComID and host session ID then a failure is returned.
  339. @param[in/out] ParseStruct Structure used to parse received TCG response, contains Sync Session response.
  340. @param[in] ComId Expected ComID that is compared to actual ComID of response
  341. @param[in] ComIdExtension Expected Extended ComID that is compared to actual Extended ComID of response
  342. @param[in] HostSessionId Expected Host Session ID that is compared to actual Host Session ID of response
  343. @param[in/out] TperSessionId Tper Session ID retrieved from the Sync Session response.
  344. **/
  345. TCG_RESULT
  346. EFIAPI
  347. TcgParseSyncSession (
  348. const TCG_PARSE_STRUCT *ParseStruct,
  349. UINT16 ComId,
  350. UINT16 ComIdExtension,
  351. UINT32 HostSessionId,
  352. UINT32 *TperSessionId
  353. )
  354. {
  355. UINT8 MethodStatus;
  356. TCG_PARSE_STRUCT TmpParseStruct;
  357. UINT16 ParseComId;
  358. UINT16 ParseExtComId;
  359. TCG_UID InvokingUID;
  360. TCG_UID MethodUID;
  361. UINT32 RecvHostSessionId;
  362. NULL_CHECK (ParseStruct);
  363. NULL_CHECK (TperSessionId);
  364. CopyMem (&TmpParseStruct, ParseStruct, sizeof (TCG_PARSE_STRUCT));
  365. // verify method status is good
  366. ERROR_CHECK (TcgGetMethodStatus (&TmpParseStruct, &MethodStatus));
  367. METHOD_STATUS_ERROR_CHECK (MethodStatus, TcgResultFailure);
  368. // verify comids
  369. ERROR_CHECK (TcgGetComIds (&TmpParseStruct, &ParseComId, &ParseExtComId));
  370. if ((ComId != ParseComId) || (ComIdExtension != ParseExtComId)) {
  371. DEBUG ((DEBUG_INFO, "unmatched comid (exp: 0x%X recv: 0x%X) or comid extension (exp: 0x%X recv: 0x%X)\n", ComId, ParseComId, ComIdExtension, ParseExtComId));
  372. return TcgResultFailure;
  373. }
  374. ERROR_CHECK (TcgGetNextCall (&TmpParseStruct));
  375. ERROR_CHECK (TcgGetNextTcgUid (&TmpParseStruct, &InvokingUID));
  376. ERROR_CHECK (TcgGetNextTcgUid (&TmpParseStruct, &MethodUID));
  377. ERROR_CHECK (TcgGetNextStartList (&TmpParseStruct));
  378. ERROR_CHECK (TcgGetNextUINT32 (&TmpParseStruct, &RecvHostSessionId));
  379. ERROR_CHECK (TcgGetNextUINT32 (&TmpParseStruct, TperSessionId));
  380. ERROR_CHECK (TcgGetNextEndList (&TmpParseStruct));
  381. ERROR_CHECK (TcgGetNextEndOfData (&TmpParseStruct));
  382. if (InvokingUID != TCG_UID_SMUID) {
  383. DEBUG ((DEBUG_INFO, "Invoking UID did not match UID_SMUID\n"));
  384. return TcgResultFailure;
  385. }
  386. if (MethodUID != TCG_UID_SM_SYNC_SESSION) {
  387. DEBUG ((DEBUG_INFO, "Method UID did not match UID_SM_SYNC_SESSION\n"));
  388. return TcgResultFailure;
  389. }
  390. if (HostSessionId != RecvHostSessionId) {
  391. DEBUG ((DEBUG_INFO, "unmatched HostSessionId (exp: 0x%X recv: 0x%X)\n", HostSessionId, RecvHostSessionId));
  392. return TcgResultFailure;
  393. }
  394. return TcgResultSuccess;
  395. }
  396. /**
  397. Creates ComPacket with EndSession.
  398. This assumes a start session has already been opened.
  399. @param [in/out] CreateStruct Structure used to add Endsession
  400. @param [in/out] Size Describes the size of the entire ComPacket (header and payload). Filled out by function.
  401. @param [in] ComId ComID for the ComPacket
  402. @param [in] ComIdExtension Extended ComID for the ComPacket
  403. @param [in] HostSessionId Host Session ID for the Packet
  404. @param [in] TpSessionId Tper Session ID for the Packet
  405. **/
  406. TCG_RESULT
  407. EFIAPI
  408. TcgCreateEndSession (
  409. TCG_CREATE_STRUCT *CreateStruct,
  410. UINT32 *Size,
  411. UINT16 ComId,
  412. UINT16 ComIdExtension,
  413. UINT32 HostSessionId,
  414. UINT32 TpSessionId
  415. )
  416. {
  417. ERROR_CHECK (TcgStartComPacket (CreateStruct, ComId, ComIdExtension));
  418. ERROR_CHECK (TcgStartPacket (CreateStruct, TpSessionId, HostSessionId, 0x0, 0x0, 0x0));
  419. ERROR_CHECK (TcgStartSubPacket (CreateStruct, 0x0));
  420. ERROR_CHECK (TcgAddEndOfSession (CreateStruct));
  421. ERROR_CHECK (TcgEndSubPacket (CreateStruct));
  422. ERROR_CHECK (TcgEndPacket (CreateStruct));
  423. ERROR_CHECK (TcgEndComPacket (CreateStruct, Size));
  424. return TcgResultSuccess;
  425. }
  426. /**
  427. Set start method.
  428. @param CreateStruct Input create structure.
  429. @param Row Input the row info.
  430. @param ColumnNumber the column info.
  431. **/
  432. TCG_RESULT
  433. EFIAPI
  434. TcgStartMethodSet (
  435. TCG_CREATE_STRUCT *CreateStruct,
  436. TCG_UID Row,
  437. UINT32 ColumnNumber
  438. )
  439. {
  440. ERROR_CHECK (TcgStartMethodCall (CreateStruct, Row, TCG_UID_METHOD_SET));
  441. ERROR_CHECK (TcgStartParameters (CreateStruct));
  442. ERROR_CHECK (TcgAddStartName (CreateStruct));
  443. ERROR_CHECK (TcgAddUINT8 (CreateStruct, 0x01)); // "Values"
  444. ERROR_CHECK (TcgAddStartList (CreateStruct));
  445. ERROR_CHECK (TcgAddStartName (CreateStruct));
  446. ERROR_CHECK (TcgAddUINT32 (CreateStruct, ColumnNumber));
  447. return TcgResultSuccess;
  448. }
  449. /**
  450. Set end method.
  451. @param CreateStruct Input create structure.
  452. **/
  453. TCG_RESULT
  454. EFIAPI
  455. TcgEndMethodSet (
  456. TCG_CREATE_STRUCT *CreateStruct
  457. )
  458. {
  459. ERROR_CHECK (TcgAddEndName (CreateStruct));
  460. ERROR_CHECK (TcgAddEndList (CreateStruct));
  461. ERROR_CHECK (TcgAddEndName (CreateStruct));
  462. ERROR_CHECK (TcgEndParameters (CreateStruct));
  463. ERROR_CHECK (TcgEndMethodCall (CreateStruct));
  464. return TcgResultSuccess;
  465. }
  466. /**
  467. Creates ComPacket with a Method call that sets the PIN column for the row specified.
  468. This assumes a start session has already been opened with the desired SP.
  469. @param [in/out] CreateStruct Structure used to add method call.
  470. @param [in/out] Size Describes the size of the entire ComPacket (header and payload). Filled out by function.
  471. @param [in] ComId ComID for the ComPacket
  472. @param [in] ComIdExtension Extended ComID for the ComPacket
  473. @param [in] TperSession Tper Session ID for the Packet
  474. @param [in] HostSession Host Session ID for the Packet
  475. @param [in] SidRow UID of row of current SP to set PIN column
  476. @param [in] Password value of PIN to set
  477. @param [in] PasswordSize Size of PIN
  478. **/
  479. TCG_RESULT
  480. EFIAPI
  481. TcgCreateSetCPin (
  482. TCG_CREATE_STRUCT *CreateStruct,
  483. UINT32 *Size,
  484. UINT16 ComId,
  485. UINT16 ComIdExtension,
  486. UINT32 TperSession,
  487. UINT32 HostSession,
  488. TCG_UID SidRow,
  489. const VOID *Password,
  490. UINT32 PasswordSize
  491. )
  492. {
  493. // set new SID Password
  494. ERROR_CHECK (TcgStartComPacket (CreateStruct, ComId, ComIdExtension));
  495. ERROR_CHECK (TcgStartPacket (CreateStruct, TperSession, HostSession, 0x0, 0x0, 0x0));
  496. ERROR_CHECK (TcgStartSubPacket (CreateStruct, 0x0));
  497. ERROR_CHECK (TcgStartMethodSet (CreateStruct, SidRow, 0x03)); // "PIN"
  498. ERROR_CHECK (TcgAddByteSequence (CreateStruct, Password, PasswordSize, FALSE));
  499. ERROR_CHECK (TcgEndMethodSet (CreateStruct));
  500. ERROR_CHECK (TcgEndSubPacket (CreateStruct));
  501. ERROR_CHECK (TcgEndPacket (CreateStruct));
  502. ERROR_CHECK (TcgEndComPacket (CreateStruct, Size));
  503. return TcgResultSuccess;
  504. }
  505. /**
  506. Creates ComPacket with a Method call that sets the "Enabled" column for the row specified using the value specified.
  507. This assumes a start session has already been opened with the desired SP.
  508. @param [in/out] CreateStruct Structure used to add method call
  509. @param [in/out] Size Describes the size of the entire ComPacket (header and payload). Filled out by function.
  510. @param [in] ComId ComID for the ComPacket
  511. @param [in] ComIdExtension Extended ComID for the ComPacket
  512. @param [in] TperSession Tper Session ID for the Packet
  513. @param [in] HostSession Host Session ID for the Packet
  514. @param [in] AuthorityUid Authority UID to modify the "Enabled" column for
  515. @param [in] Enabled Value to set the "Enabled" column to
  516. **/
  517. TCG_RESULT
  518. EFIAPI
  519. TcgSetAuthorityEnabled (
  520. TCG_CREATE_STRUCT *CreateStruct,
  521. UINT32 *Size,
  522. UINT16 ComId,
  523. UINT16 ComIdExtension,
  524. UINT32 TperSession,
  525. UINT32 HostSession,
  526. TCG_UID AuthorityUid,
  527. BOOLEAN Enabled
  528. )
  529. {
  530. ERROR_CHECK (TcgStartComPacket (CreateStruct, ComId, ComIdExtension));
  531. ERROR_CHECK (TcgStartPacket (CreateStruct, TperSession, HostSession, 0x0, 0x0, 0x0));
  532. ERROR_CHECK (TcgStartSubPacket (CreateStruct, 0x0));
  533. ERROR_CHECK (TcgStartMethodSet (CreateStruct, AuthorityUid, 0x05)); // "Enabled"
  534. ERROR_CHECK (TcgAddBOOLEAN (CreateStruct, Enabled));
  535. ERROR_CHECK (TcgEndMethodSet (CreateStruct));
  536. ERROR_CHECK (TcgEndSubPacket (CreateStruct));
  537. ERROR_CHECK (TcgEndPacket (CreateStruct));
  538. ERROR_CHECK (TcgEndComPacket (CreateStruct, Size));
  539. return TcgResultSuccess;
  540. }
  541. /**
  542. Create set ace.
  543. @param CreateStruct Input create structure.
  544. @param Size size info.
  545. @param ComId ComId info.
  546. @param ComIdExtension ComId extension info.
  547. @param TperSession Tper session data.
  548. @param HostSession Host session data.
  549. @param AceRow Ace row info.
  550. @param Authority1 Authority 1 info.
  551. @param LogicalOperator Logical operator info.
  552. @param Authority2 Authority 2 info.
  553. @retval Return the action result.
  554. **/
  555. TCG_RESULT
  556. EFIAPI
  557. TcgCreateSetAce (
  558. TCG_CREATE_STRUCT *CreateStruct,
  559. UINT32 *Size,
  560. UINT16 ComId,
  561. UINT16 ComIdExtension,
  562. UINT32 TperSession,
  563. UINT32 HostSession,
  564. TCG_UID AceRow,
  565. TCG_UID Authority1,
  566. BOOLEAN LogicalOperator,
  567. TCG_UID Authority2
  568. )
  569. {
  570. UINT8 HalfUidAuthorityObjectRef[4];
  571. UINT8 HalfUidBooleanAce[4];
  572. HalfUidAuthorityObjectRef[0] = 0x0;
  573. HalfUidAuthorityObjectRef[1] = 0x0;
  574. HalfUidAuthorityObjectRef[2] = 0xC;
  575. HalfUidAuthorityObjectRef[3] = 0x5;
  576. HalfUidBooleanAce[0] = 0x0;
  577. HalfUidBooleanAce[1] = 0x0;
  578. HalfUidBooleanAce[2] = 0x4;
  579. HalfUidBooleanAce[3] = 0xE;
  580. ERROR_CHECK (TcgStartComPacket (CreateStruct, ComId, ComIdExtension));
  581. ERROR_CHECK (TcgStartPacket (CreateStruct, TperSession, HostSession, 0x0, 0x0, 0x0));
  582. ERROR_CHECK (TcgStartSubPacket (CreateStruct, 0x0));
  583. ERROR_CHECK (TcgStartMethodSet (CreateStruct, AceRow, 0x03)); // "BooleanExpr"
  584. ERROR_CHECK (TcgAddStartList (CreateStruct));
  585. ERROR_CHECK (TcgAddStartName (CreateStruct));
  586. ERROR_CHECK (TcgAddByteSequence (CreateStruct, HalfUidAuthorityObjectRef, sizeof (HalfUidAuthorityObjectRef), FALSE));
  587. ERROR_CHECK (TcgAddTcgUid (CreateStruct, Authority1));
  588. ERROR_CHECK (TcgAddEndName (CreateStruct));
  589. ERROR_CHECK (TcgAddStartName (CreateStruct));
  590. ERROR_CHECK (TcgAddByteSequence (CreateStruct, HalfUidAuthorityObjectRef, sizeof (HalfUidAuthorityObjectRef), FALSE));
  591. ERROR_CHECK (TcgAddTcgUid (CreateStruct, Authority2));
  592. ERROR_CHECK (TcgAddEndName (CreateStruct));
  593. ERROR_CHECK (TcgAddStartName (CreateStruct));
  594. ERROR_CHECK (TcgAddByteSequence (CreateStruct, HalfUidBooleanAce, sizeof (HalfUidBooleanAce), FALSE));
  595. ERROR_CHECK (TcgAddBOOLEAN (CreateStruct, LogicalOperator));
  596. ERROR_CHECK (TcgAddEndName (CreateStruct));
  597. ERROR_CHECK (TcgAddEndList (CreateStruct));
  598. ERROR_CHECK (TcgEndMethodSet (CreateStruct));
  599. ERROR_CHECK (TcgEndSubPacket (CreateStruct));
  600. ERROR_CHECK (TcgEndPacket (CreateStruct));
  601. ERROR_CHECK (TcgEndComPacket (CreateStruct, Size));
  602. return TcgResultSuccess;
  603. }
  604. /**
  605. Enum level 0 discovery.
  606. @param DiscoveryHeader Discovery header.
  607. @param Callback Callback function.
  608. @param Context The context for the function.
  609. @retval return true if the callback return TRUE, else return FALSE.
  610. **/
  611. BOOLEAN
  612. EFIAPI
  613. TcgEnumLevel0Discovery (
  614. const TCG_LEVEL0_DISCOVERY_HEADER *DiscoveryHeader,
  615. TCG_LEVEL0_ENUM_CALLBACK Callback,
  616. VOID *Context
  617. )
  618. {
  619. UINT32 BytesLeft;
  620. const UINT8 *DiscoveryBufferPtr;
  621. UINT32 FeatLength;
  622. TCG_LEVEL0_FEATURE_DESCRIPTOR_HEADER *Feat;
  623. //
  624. // Total bytes including descriptors but not including the Length field
  625. //
  626. BytesLeft = SwapBytes32 (DiscoveryHeader->LengthBE);
  627. //
  628. // If discovery Header is not valid, exit
  629. //
  630. if (BytesLeft == 0) {
  631. return FALSE;
  632. }
  633. //
  634. // Subtract the Length of the Header, except the Length field, which is not included
  635. //
  636. BytesLeft -= (sizeof (TCG_LEVEL0_DISCOVERY_HEADER) - sizeof (DiscoveryHeader->LengthBE));
  637. //
  638. // Move ptr to first descriptor
  639. //
  640. DiscoveryBufferPtr = (const UINT8 *)DiscoveryHeader + sizeof (TCG_LEVEL0_DISCOVERY_HEADER);
  641. while (BytesLeft > sizeof (TCG_LEVEL0_FEATURE_DESCRIPTOR_HEADER)) {
  642. //
  643. // Pointer to beginning of descriptor (including common Header)
  644. //
  645. Feat = (TCG_LEVEL0_FEATURE_DESCRIPTOR_HEADER *)DiscoveryBufferPtr;
  646. FeatLength = Feat->Length + sizeof (TCG_LEVEL0_FEATURE_DESCRIPTOR_HEADER);
  647. //
  648. // Not enough bytes left for Feature descriptor
  649. //
  650. if (BytesLeft < FeatLength) {
  651. break;
  652. }
  653. //
  654. // Report the Feature to the callback
  655. //
  656. if (Callback (DiscoveryHeader, Feat, FeatLength, Context)) {
  657. return TRUE;
  658. }
  659. //
  660. // Descriptor Length only describes Data after common Header
  661. //
  662. BytesLeft -= FeatLength;
  663. DiscoveryBufferPtr += FeatLength;
  664. }
  665. return FALSE;
  666. }
  667. /**
  668. The callback function for Get Feature function.
  669. @param DiscoveryHeader Input discovery header.
  670. @param Feature Input Feature.
  671. @param FeatureSize Input Feature size.
  672. @param Context The context.
  673. **/
  674. BOOLEAN
  675. EFIAPI
  676. TcgFindFeatureCallback (
  677. const TCG_LEVEL0_DISCOVERY_HEADER *DiscoveryHeader,
  678. TCG_LEVEL0_FEATURE_DESCRIPTOR_HEADER *Feature,
  679. UINTN FeatureSize,
  680. VOID *Context
  681. )
  682. {
  683. TCG_FIND_FEATURE_CTX *FindCtx;
  684. FindCtx = (TCG_FIND_FEATURE_CTX *)Context;
  685. if ( SwapBytes16 (Feature->FeatureCode_BE) == FindCtx->FeatureCode ) {
  686. FindCtx->Feature = Feature;
  687. FindCtx->FeatureSize = FeatureSize;
  688. return TRUE; // done enumerating features
  689. }
  690. return FALSE; // continue enumerating
  691. }
  692. /**
  693. Get Feature code from the header.
  694. @param DiscoveryHeader The discovery header.
  695. @param FeatureCode return the Feature code.
  696. @param FeatureSize return the Feature size.
  697. @retval return the Feature code data.
  698. **/
  699. TCG_LEVEL0_FEATURE_DESCRIPTOR_HEADER *
  700. EFIAPI
  701. TcgGetFeature (
  702. const TCG_LEVEL0_DISCOVERY_HEADER *DiscoveryHeader,
  703. UINT16 FeatureCode,
  704. UINTN *FeatureSize
  705. )
  706. {
  707. TCG_FIND_FEATURE_CTX FindCtx;
  708. FindCtx.FeatureCode = FeatureCode;
  709. FindCtx.Feature = NULL;
  710. FindCtx.FeatureSize = 0;
  711. TcgEnumLevel0Discovery (DiscoveryHeader, TcgFindFeatureCallback, &FindCtx);
  712. if (FeatureSize != NULL) {
  713. *FeatureSize = FindCtx.FeatureSize;
  714. }
  715. return FindCtx.Feature;
  716. }
  717. /**
  718. Determines if the protocol provided is part of the provided supported protocol list.
  719. @param[in] ProtocolList Supported protocol list to investigate
  720. @param[in] Protocol Protocol value to determine if supported
  721. @return TRUE = protocol is supported, FALSE = protocol is not supported
  722. **/
  723. BOOLEAN
  724. EFIAPI
  725. TcgIsProtocolSupported (
  726. const TCG_SUPPORTED_SECURITY_PROTOCOLS *ProtocolList,
  727. UINT16 Protocol
  728. )
  729. {
  730. UINT16 Index;
  731. UINT16 ListLength;
  732. ListLength = SwapBytes16 (ProtocolList->ListLength_BE);
  733. if (ListLength > sizeof (ProtocolList->List)) {
  734. DEBUG ((DEBUG_INFO, "WARNING: list Length is larger than max allowed Value; truncating\n"));
  735. ListLength = sizeof (ProtocolList->List);
  736. }
  737. for (Index = 0; Index < ListLength; Index++) {
  738. if (ProtocolList->List[Index] == Protocol) {
  739. return TRUE;
  740. }
  741. }
  742. return FALSE;
  743. }
  744. /**
  745. Check whether lock or not.
  746. @param Discovery
  747. @retval TRUE if lock, FALSE if not lock.
  748. **/
  749. BOOLEAN
  750. EFIAPI
  751. TcgIsLocked (
  752. const TCG_LEVEL0_DISCOVERY_HEADER *Discovery
  753. )
  754. {
  755. UINTN Size;
  756. TCG_LOCKING_FEATURE_DESCRIPTOR *LockDescriptor;
  757. Size = 0;
  758. LockDescriptor = (TCG_LOCKING_FEATURE_DESCRIPTOR *)TcgGetFeature (Discovery, TCG_FEATURE_LOCKING, &Size);
  759. if ((LockDescriptor != NULL) && (Size >= sizeof (*LockDescriptor))) {
  760. DEBUG ((DEBUG_INFO, "locked: %d\n", LockDescriptor->Locked));
  761. return LockDescriptor->Locked;
  762. }
  763. //
  764. // Descriptor was not found
  765. //
  766. return FALSE;
  767. }