1 #!/bin/sh 2 # Copyright 2014 The Chromium OS Authors. All rights reserved. 3 # Use of this source code is governed by a BSD-style license that can be 4 # found in the LICENSE file. 5 6 cat $1 | 7 # Mark descriptions and actions in the body. 8 sed 's/^[0-9. ]*Command and Response$/_COMMAND_SECTION/' | 9 sed 's/^[0-9. ]*Detailed Actions$/_ACTIONS_SECTION/' | 10 # Keep only command and response sections. 11 awk '/^_COMMAND_SECTION$/,/^_ACTIONS_SECTION$/ { print $0; }' | 12 sed 's/^_COMMAND_SECTION$//' | 13 sed 's/^_ACTIONS_SECTION$//' | 14 # Remove headers and footers. 15 sed 's/^.*Trusted Platform Module Library.*$//' | 16 sed 's/^.*Part 3: Commands.*$//' | 17 sed 's/^.*Family .2.0..*$//' | 18 sed 's/^.*Level 00 Revision.*$//' | 19 sed 's/^.*Published.*$//' | 20 sed 's/^.*Copyright.*$//' | 21 sed 's/^.*Page [0-9].*$//' | 22 sed 's/^.*October 31, 2013.*$//' | 23 # Remove table headers. 24 sed 's/^Type$//' | sed 's/^Name$//' | sed 's/^Description$//' | 25 # Remove leading spaces. 26 sed 's/^[ ][ ]*//' | 27 # Remove empty lines. 28 sed '/^$/d' | 29 # Mark begin and end. 30 awk ' 31 BEGIN { print "_BEGIN"; } 32 { print $0; } 33 END { print "_END"; } 34 ' | 35 # Mark command / response tables. 36 sed 's/^Table [0-9]* . \(.*\) Command$/_INPUT_START \1/' | 37 sed 's/^Table [0-9]* . \(.*\) Response$/_OUTPUT_START \1/' | 38 # Mark argument types. 39 sed 's/^\(TPM[_A-Z0-9+]*\)$/_TYPE \1/' | 40 sed 's/^\(U*INT[0-9]*\)$/_TYPE \1/' | 41 # Filter out a few special cases that look like types but are not. 42 sed 's/^_TYPE TPM_ST_NO_SESSIONS$/TPM_ST_NO_SESSIONS/' | 43 sed 's/^_TYPE TPM_ALG_NULL$/TPM_ALG_NULL/' | 44 sed 's/^_TYPE TPM_CC_HMAC$/TPM_CC_HMAC/' | 45 sed 's/^_TYPE TPM_GENERATED_VALUE$/TPM_GENERATED_VALUE/' | 46 sed 's/^_TYPE \(TPM_RH_[A-Z+]*\)$/\1/' | 47 # Mark argument names. 48 awk ' 49 BEGIN { last_line_was_type = 0; } 50 /^_.*$/ { print $0; } 51 /^_TYPE .*$/ { last_line_was_type = 1; } 52 /^[^_].*$/ { if (last_line_was_type) { 53 last_line_was_type = 0; 54 print "_NAME " $0; 55 if ($0 !~ /^[@a-zA-Z0-9]*$/) { 56 print "_ERROR: Invalid name."; } } 57 else { print $0; } } 58 ' | 59 # Consolidate comments to a single line and mark. 60 awk ' 61 BEGIN { comment = ""; } 62 /^_.*$/ { if (comment != "") { print "_COMMENT " comment; comment = ""; } 63 print $0; } 64 /^[^_].*$/ { if (comment != "") { comment = comment " " $0; } 65 else { comment = $0 } } 66 ' | 67 # Strip off @name modifier, keep TYPE+ modifier for for conditional unmarshaling. 68 sed 's/^_NAME @\(.*\)$/_NAME \1/' | 69 # Sanity check. The format should now follow this grammar: 70 # Format:Begin||CommandBlock*||End 71 # Begin:_BEGIN\n 72 # End:_END\n 73 # CommandBlock:InputTag||Argument*||OutputTag||Argument* 74 # InputTag:_INPUT_START <command>\n 75 # OutputTag:_OUTPUT_START <command>\n 76 # Argument:Type||Name[||Comment] 77 # Type:_TYPE <type>\n 78 # Name:_NAME <name>\n 79 # Comment:_COMMENT <comment>\n 80 awk ' 81 BEGIN { RS = ""; } 82 $0 !~ /_BEGIN\n(_INPUT_START[^\n]*\n(_TYPE[^\n]*\n_NAME[^\n]*\n(_COMMENT[^\n]*\n)*)*_OUTPUT_START[^\n]*\n(_TYPE[^\n]*\n_NAME[^\n]*\n(_COMMENT[^\n]*\n)*)*)*_END/ { 83 print "_ERROR: Format check failed."; } 84 { print $0; } 85 ' 86 exit 0 87