trunks: Fix generator bug in handle serialization

The trunks generator incorrectly serializes some handles in the
handles section rather than the parameter section of commands.
This CL special cases on those commands to ensure that their
handles are serialized in the parameter section

BUG=None
TEST=manual verification of code generated

Change-Id: Id139de517f0b28551a9e577e50387c2dded928bd
diff --git a/generator/generator.py b/generator/generator.py
index bd47d45..09b6cd2 100755
--- a/generator/generator.py
+++ b/generator/generator.py
@@ -81,11 +81,21 @@
 _OUTPUT_FILE_H = 'tpm_generated.h'
 _OUTPUT_FILE_CC = 'tpm_generated.cc'
 _COPYRIGHT_HEADER = (
-    '// Copyright 2014 The Chromium OS Authors. All rights reserved.\n'
-    '// Use of this source code is governed by a BSD-style license that can '
-    'be\n'
-    '// found in the LICENSE file.\n'
-    '\n'
+    '// \n'
+    '// Copyright (C) 2015 The Android Open Source Project \n'
+    '// \n'
+    '// Licensed under the Apache License, Version 2.0 (the "License"); \n'
+    '// you may not use this file except in compliance with the License. \n'
+    '// \n'
+    '//      http://www.apache.org/licenses/LICENSE-2.0 \n'
+    '// \n'
+    '// Unless required by applicable law or agreed to in writing, software \n'
+    '// distributed under the License is distributed on an "AS IS" BASIS, \n'
+    '// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or '
+    'implied. \n'
+    '// See the License for the specific language governing permissions and \n'
+    '// limitations under the License. \n'
+    '// \n'
     '// THIS CODE IS GENERATED - DO NOT MODIFY!\n')
 _HEADER_FILE_GUARD_HEADER = """
 #ifndef %(name)s
@@ -1737,11 +1747,20 @@
     """Splits a list of args into handles and parameters."""
     handles = []
     parameters = []
+    # These commands have handles that are serialized into the parameter
+    # section.
+    command_handle_parameters = {
+        'TPM_CC_FlushContext': 'TPMI_DH_CONTEXT',
+        'TPM_CC_Hash': 'TPMI_RH_HIERARCHY',
+        'TPM_CC_LoadExternal': 'TPMI_RH_HIERARCHY',
+        'TPM_CC_SequenceComplete': 'TPMI_RH_HIERARCHY',
+    }
+    # Handle type that appears in the handle section.
     always_handle = set(['TPM_HANDLE'])
-    # Handle types that appear as command parameters.
+    # Handle types that always appear as command parameters.
     always_parameter = set(['TPMI_RH_ENABLES', 'TPMI_DH_PERSISTENT'])
-    if self.command_code == 'TPM_CC_FlushContext':
-      always_parameter.add('TPMI_DH_CONTEXT')
+    if self.command_code in command_handle_parameters:
+      always_parameter.add(command_handle_parameters[self.command_code])
     for arg in args:
       if (arg['type'] in always_handle or
           (self._HANDLE_RE.search(arg['type']) and
diff --git a/tpm_generated.cc b/tpm_generated.cc
index d7d4246..657c880 100644
--- a/tpm_generated.cc
+++ b/tpm_generated.cc
@@ -1,5 +1,5 @@
 //
-// Copyright (C) 2014 The Android Open Source Project
+// Copyright (C) 2015 The Android Open Source Project
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -47,7 +47,7 @@
     case TPM_CC_PolicyRestart: return 1;
     case TPM_CC_Create: return 1;
     case TPM_CC_Load: return 1;
-    case TPM_CC_LoadExternal: return 1;
+    case TPM_CC_LoadExternal: return 0;
     case TPM_CC_ReadPublic: return 1;
     case TPM_CC_ActivateCredential: return 2;
     case TPM_CC_MakeCredential: return 1;
@@ -63,14 +63,14 @@
     case TPM_CC_ECC_Parameters: return 0;
     case TPM_CC_ZGen_2Phase: return 1;
     case TPM_CC_EncryptDecrypt: return 1;
-    case TPM_CC_Hash: return 1;
+    case TPM_CC_Hash: return 0;
     case TPM_CC_HMAC: return 1;
     case TPM_CC_GetRandom: return 0;
     case TPM_CC_StirRandom: return 0;
     case TPM_CC_HMAC_Start: return 1;
     case TPM_CC_HashSequenceStart: return 0;
     case TPM_CC_SequenceUpdate: return 1;
-    case TPM_CC_SequenceComplete: return 2;
+    case TPM_CC_SequenceComplete: return 1;
     case TPM_CC_EventSequenceComplete: return 2;
     case TPM_CC_Certify: return 2;
     case TPM_CC_CertifyCreation: return 2;
@@ -12004,10 +12004,9 @@
 }
 
 TPM_RC Tpm::SerializeCommand_LoadExternal(
-      const TPMI_RH_HIERARCHY& hierarchy,
-      const std::string& hierarchy_name,
       const TPM2B_SENSITIVE& in_private,
       const TPM2B_PUBLIC& in_public,
+      const TPMI_RH_HIERARCHY& hierarchy,
       std::string* serialized_command,
       AuthorizationDelegate* authorization_delegate) {
   VLOG(3) << __func__;
@@ -12059,10 +12058,6 @@
       crypto::SecureHash::SHA256));
   hash->Update(command_code_bytes.data(),
                command_code_bytes.size());
-  hash->Update(hierarchy_name.data(),
-               hierarchy_name.size());
-  handle_section_bytes += hierarchy_bytes;
-  command_size += hierarchy_bytes.size();
   hash->Update(in_private_bytes.data(),
                in_private_bytes.size());
   parameter_section_bytes += in_private_bytes;
@@ -12071,6 +12066,10 @@
                in_public_bytes.size());
   parameter_section_bytes += in_public_bytes;
   command_size += in_public_bytes.size();
+  hash->Update(hierarchy_bytes.data(),
+               hierarchy_bytes.size());
+  parameter_section_bytes += hierarchy_bytes;
+  command_size += hierarchy_bytes.size();
   std::string command_hash(32, 0);
   hash->Finish(string_as_array(&command_hash), command_hash.size());
   std::string authorization_section_bytes;
@@ -12273,10 +12272,9 @@
 }
 
 void Tpm::LoadExternal(
-      const TPMI_RH_HIERARCHY& hierarchy,
-      const std::string& hierarchy_name,
       const TPM2B_SENSITIVE& in_private,
       const TPM2B_PUBLIC& in_public,
+      const TPMI_RH_HIERARCHY& hierarchy,
       AuthorizationDelegate* authorization_delegate,
       const LoadExternalResponse& callback) {
   VLOG(1) << __func__;
@@ -12288,10 +12286,9 @@
                  authorization_delegate);
   std::string command;
   TPM_RC rc = SerializeCommand_LoadExternal(
-      hierarchy,
-      hierarchy_name,
       in_private,
       in_public,
+      hierarchy,
       &command,
       authorization_delegate);
   if (rc != TPM_RC_SUCCESS) {
@@ -12302,20 +12299,18 @@
 }
 
 TPM_RC Tpm::LoadExternalSync(
-      const TPMI_RH_HIERARCHY& hierarchy,
-      const std::string& hierarchy_name,
       const TPM2B_SENSITIVE& in_private,
       const TPM2B_PUBLIC& in_public,
+      const TPMI_RH_HIERARCHY& hierarchy,
       TPM_HANDLE* object_handle,
       TPM2B_NAME* name,
       AuthorizationDelegate* authorization_delegate) {
   VLOG(1) << __func__;
   std::string command;
   TPM_RC rc = SerializeCommand_LoadExternal(
-      hierarchy,
-      hierarchy_name,
       in_private,
       in_public,
+      hierarchy,
       &command,
       authorization_delegate);
   if (rc != TPM_RC_SUCCESS) {
@@ -17169,10 +17164,9 @@
 }
 
 TPM_RC Tpm::SerializeCommand_Hash(
-      const TPMI_RH_HIERARCHY& hierarchy,
-      const std::string& hierarchy_name,
       const TPM2B_MAX_BUFFER& data,
       const TPMI_ALG_HASH& hash_alg,
+      const TPMI_RH_HIERARCHY& hierarchy,
       std::string* serialized_command,
       AuthorizationDelegate* authorization_delegate) {
   VLOG(3) << __func__;
@@ -17224,10 +17218,6 @@
       crypto::SecureHash::SHA256));
   hash->Update(command_code_bytes.data(),
                command_code_bytes.size());
-  hash->Update(hierarchy_name.data(),
-               hierarchy_name.size());
-  handle_section_bytes += hierarchy_bytes;
-  command_size += hierarchy_bytes.size();
   hash->Update(data_bytes.data(),
                data_bytes.size());
   parameter_section_bytes += data_bytes;
@@ -17236,6 +17226,10 @@
                hash_alg_bytes.size());
   parameter_section_bytes += hash_alg_bytes;
   command_size += hash_alg_bytes.size();
+  hash->Update(hierarchy_bytes.data(),
+               hierarchy_bytes.size());
+  parameter_section_bytes += hierarchy_bytes;
+  command_size += hierarchy_bytes.size();
   std::string command_hash(32, 0);
   hash->Finish(string_as_array(&command_hash), command_hash.size());
   std::string authorization_section_bytes;
@@ -17438,10 +17432,9 @@
 }
 
 void Tpm::Hash(
-      const TPMI_RH_HIERARCHY& hierarchy,
-      const std::string& hierarchy_name,
       const TPM2B_MAX_BUFFER& data,
       const TPMI_ALG_HASH& hash_alg,
+      const TPMI_RH_HIERARCHY& hierarchy,
       AuthorizationDelegate* authorization_delegate,
       const HashResponse& callback) {
   VLOG(1) << __func__;
@@ -17453,10 +17446,9 @@
                  authorization_delegate);
   std::string command;
   TPM_RC rc = SerializeCommand_Hash(
-      hierarchy,
-      hierarchy_name,
       data,
       hash_alg,
+      hierarchy,
       &command,
       authorization_delegate);
   if (rc != TPM_RC_SUCCESS) {
@@ -17467,20 +17459,18 @@
 }
 
 TPM_RC Tpm::HashSync(
-      const TPMI_RH_HIERARCHY& hierarchy,
-      const std::string& hierarchy_name,
       const TPM2B_MAX_BUFFER& data,
       const TPMI_ALG_HASH& hash_alg,
+      const TPMI_RH_HIERARCHY& hierarchy,
       TPM2B_DIGEST* out_hash,
       TPMT_TK_HASHCHECK* validation,
       AuthorizationDelegate* authorization_delegate) {
   VLOG(1) << __func__;
   std::string command;
   TPM_RC rc = SerializeCommand_Hash(
-      hierarchy,
-      hierarchy_name,
       data,
       hash_alg,
+      hierarchy,
       &command,
       authorization_delegate);
   if (rc != TPM_RC_SUCCESS) {
@@ -19157,9 +19147,8 @@
 TPM_RC Tpm::SerializeCommand_SequenceComplete(
       const TPMI_DH_OBJECT& sequence_handle,
       const std::string& sequence_handle_name,
-      const TPMI_RH_HIERARCHY& hierarchy,
-      const std::string& hierarchy_name,
       const TPM2B_MAX_BUFFER& buffer,
+      const TPMI_RH_HIERARCHY& hierarchy,
       std::string* serialized_command,
       AuthorizationDelegate* authorization_delegate) {
   VLOG(3) << __func__;
@@ -19215,14 +19204,14 @@
                sequence_handle_name.size());
   handle_section_bytes += sequence_handle_bytes;
   command_size += sequence_handle_bytes.size();
-  hash->Update(hierarchy_name.data(),
-               hierarchy_name.size());
-  handle_section_bytes += hierarchy_bytes;
-  command_size += hierarchy_bytes.size();
   hash->Update(buffer_bytes.data(),
                buffer_bytes.size());
   parameter_section_bytes += buffer_bytes;
   command_size += buffer_bytes.size();
+  hash->Update(hierarchy_bytes.data(),
+               hierarchy_bytes.size());
+  parameter_section_bytes += hierarchy_bytes;
+  command_size += hierarchy_bytes.size();
   std::string command_hash(32, 0);
   hash->Finish(string_as_array(&command_hash), command_hash.size());
   std::string authorization_section_bytes;
@@ -19427,9 +19416,8 @@
 void Tpm::SequenceComplete(
       const TPMI_DH_OBJECT& sequence_handle,
       const std::string& sequence_handle_name,
-      const TPMI_RH_HIERARCHY& hierarchy,
-      const std::string& hierarchy_name,
       const TPM2B_MAX_BUFFER& buffer,
+      const TPMI_RH_HIERARCHY& hierarchy,
       AuthorizationDelegate* authorization_delegate,
       const SequenceCompleteResponse& callback) {
   VLOG(1) << __func__;
@@ -19443,9 +19431,8 @@
   TPM_RC rc = SerializeCommand_SequenceComplete(
       sequence_handle,
       sequence_handle_name,
-      hierarchy,
-      hierarchy_name,
       buffer,
+      hierarchy,
       &command,
       authorization_delegate);
   if (rc != TPM_RC_SUCCESS) {
@@ -19458,9 +19445,8 @@
 TPM_RC Tpm::SequenceCompleteSync(
       const TPMI_DH_OBJECT& sequence_handle,
       const std::string& sequence_handle_name,
-      const TPMI_RH_HIERARCHY& hierarchy,
-      const std::string& hierarchy_name,
       const TPM2B_MAX_BUFFER& buffer,
+      const TPMI_RH_HIERARCHY& hierarchy,
       TPM2B_DIGEST* result,
       TPMT_TK_HASHCHECK* validation,
       AuthorizationDelegate* authorization_delegate) {
@@ -19469,9 +19455,8 @@
   TPM_RC rc = SerializeCommand_SequenceComplete(
       sequence_handle,
       sequence_handle_name,
-      hierarchy,
-      hierarchy_name,
       buffer,
+      hierarchy,
       &command,
       authorization_delegate);
   if (rc != TPM_RC_SUCCESS) {
diff --git a/tpm_generated.h b/tpm_generated.h
index e60e83c..5a7c7e0 100644
--- a/tpm_generated.h
+++ b/tpm_generated.h
@@ -1,5 +1,5 @@
 //
-// Copyright (C) 2014 The Android Open Source Project
+// Copyright (C) 2015 The Android Open Source Project
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -3966,10 +3966,9 @@
       const TPM_HANDLE& object_handle,
       const TPM2B_NAME& name)> LoadExternalResponse;
   static TPM_RC SerializeCommand_LoadExternal(
-      const TPMI_RH_HIERARCHY& hierarchy,
-      const std::string& hierarchy_name,
       const TPM2B_SENSITIVE& in_private,
       const TPM2B_PUBLIC& in_public,
+      const TPMI_RH_HIERARCHY& hierarchy,
       std::string* serialized_command,
       AuthorizationDelegate* authorization_delegate);
   static TPM_RC ParseResponse_LoadExternal(
@@ -3978,17 +3977,15 @@
       TPM2B_NAME* name,
       AuthorizationDelegate* authorization_delegate);
   virtual void LoadExternal(
-      const TPMI_RH_HIERARCHY& hierarchy,
-      const std::string& hierarchy_name,
       const TPM2B_SENSITIVE& in_private,
       const TPM2B_PUBLIC& in_public,
+      const TPMI_RH_HIERARCHY& hierarchy,
       AuthorizationDelegate* authorization_delegate,
       const LoadExternalResponse& callback);
   virtual TPM_RC LoadExternalSync(
-      const TPMI_RH_HIERARCHY& hierarchy,
-      const std::string& hierarchy_name,
       const TPM2B_SENSITIVE& in_private,
       const TPM2B_PUBLIC& in_public,
+      const TPMI_RH_HIERARCHY& hierarchy,
       TPM_HANDLE* object_handle,
       TPM2B_NAME* name,
       AuthorizationDelegate* authorization_delegate);
@@ -4465,10 +4462,9 @@
       const TPM2B_DIGEST& out_hash,
       const TPMT_TK_HASHCHECK& validation)> HashResponse;
   static TPM_RC SerializeCommand_Hash(
-      const TPMI_RH_HIERARCHY& hierarchy,
-      const std::string& hierarchy_name,
       const TPM2B_MAX_BUFFER& data,
       const TPMI_ALG_HASH& hash_alg,
+      const TPMI_RH_HIERARCHY& hierarchy,
       std::string* serialized_command,
       AuthorizationDelegate* authorization_delegate);
   static TPM_RC ParseResponse_Hash(
@@ -4477,17 +4473,15 @@
       TPMT_TK_HASHCHECK* validation,
       AuthorizationDelegate* authorization_delegate);
   virtual void Hash(
-      const TPMI_RH_HIERARCHY& hierarchy,
-      const std::string& hierarchy_name,
       const TPM2B_MAX_BUFFER& data,
       const TPMI_ALG_HASH& hash_alg,
+      const TPMI_RH_HIERARCHY& hierarchy,
       AuthorizationDelegate* authorization_delegate,
       const HashResponse& callback);
   virtual TPM_RC HashSync(
-      const TPMI_RH_HIERARCHY& hierarchy,
-      const std::string& hierarchy_name,
       const TPM2B_MAX_BUFFER& data,
       const TPMI_ALG_HASH& hash_alg,
+      const TPMI_RH_HIERARCHY& hierarchy,
       TPM2B_DIGEST* out_hash,
       TPMT_TK_HASHCHECK* validation,
       AuthorizationDelegate* authorization_delegate);
@@ -4633,9 +4627,8 @@
   static TPM_RC SerializeCommand_SequenceComplete(
       const TPMI_DH_OBJECT& sequence_handle,
       const std::string& sequence_handle_name,
-      const TPMI_RH_HIERARCHY& hierarchy,
-      const std::string& hierarchy_name,
       const TPM2B_MAX_BUFFER& buffer,
+      const TPMI_RH_HIERARCHY& hierarchy,
       std::string* serialized_command,
       AuthorizationDelegate* authorization_delegate);
   static TPM_RC ParseResponse_SequenceComplete(
@@ -4646,17 +4639,15 @@
   virtual void SequenceComplete(
       const TPMI_DH_OBJECT& sequence_handle,
       const std::string& sequence_handle_name,
-      const TPMI_RH_HIERARCHY& hierarchy,
-      const std::string& hierarchy_name,
       const TPM2B_MAX_BUFFER& buffer,
+      const TPMI_RH_HIERARCHY& hierarchy,
       AuthorizationDelegate* authorization_delegate,
       const SequenceCompleteResponse& callback);
   virtual TPM_RC SequenceCompleteSync(
       const TPMI_DH_OBJECT& sequence_handle,
       const std::string& sequence_handle_name,
-      const TPMI_RH_HIERARCHY& hierarchy,
-      const std::string& hierarchy_name,
       const TPM2B_MAX_BUFFER& buffer,
+      const TPMI_RH_HIERARCHY& hierarchy,
       TPM2B_DIGEST* result,
       TPMT_TK_HASHCHECK* validation,
       AuthorizationDelegate* authorization_delegate);