Fix integration bugs (vboot side)

BUG=chromium-os:8621
TEST=See below

1. Build and run tests of vboot (including linktest)
$ make && make runtests

2. Check if *_stub.o are not in vboot_fw.a
$ nm /build/<board>/usr/lib/vboot_fw.a | grep _stub.o

3. Build and boot x86-generic image
$ ./build_packages --board=x86-generic && ./build_image --board=x86-generic
(Then successfully boot the image you just built)

See CL=4372001 for u-boot side changes

Review URL: http://codereview.chromium.org/4266002

Change-Id: Icc2bcc551c998f370e4b737fbe442ebf029cd81c
diff --git a/Makefile b/Makefile
index a9445e7..15ef638 100644
--- a/Makefile
+++ b/Makefile
@@ -21,7 +21,7 @@
 
 export BUILD = ${TOP}/build
 export FWLIB = ${BUILD}/vboot_fw.a
-export HOSTLIB= ${BUILD}/vboot_host.a
+export HOSTLIB = ${BUILD}/vboot_host.a
 
 SUBDIRS = firmware host utility cgpt tests tests/tpm_lite
 
diff --git a/cgpt/Makefile b/cgpt/Makefile
index 3984628..cd503ba 100644
--- a/cgpt/Makefile
+++ b/cgpt/Makefile
@@ -7,7 +7,7 @@
 BUILD_ROOT := ${BUILD}/cgpt
 
 INCLUDES = -I$(FWDIR)/lib/cgptlib/include -I$(FWDIR)/include
-LIBS = ${FWLIB}
+LIBS = ${HOSTLIB}
 
 DESTDIR ?= /usr/bin
 
diff --git a/firmware/Makefile b/firmware/Makefile
index 99f33f2..ea1f183 100644
--- a/firmware/Makefile
+++ b/firmware/Makefile
@@ -40,17 +40,21 @@
 	./lib/vboot_firmware.c \
 	./lib/vboot_kernel.c
 
+LIB_OBJS = $(LIB_SRCS:%.c=${BUILD_ROOT}/%.o)
+
 STUB_SRCS = \
 	./stub/boot_device_stub.c \
 	./stub/load_firmware_stub.c \
 	./stub/tpm_lite_stub.c \
 	./stub/utility_stub.c
 
+STUB_OBJS = $(STUB_SRCS:%.c=${BUILD_ROOT}/%.o)
+
 ALL_SRCS = ${LIB_SRCS} ${STUB_SRCS} version.c
 
-test : $(FWLIB) update-version
+test : $(STUB_OBJS) $(FWLIB) update-version
 	$(CC) $(CFLAGS) $(INCLUDES) -o $(BUILD_ROOT)/a.out \
-		$(TESTDIR)/main.c $(LIBS)
+		$(TESTDIR)/main.c $(STUB_OBJS) $(LIBS)
 
 # This is executed at every make, to see if anything has changed
 update-version :
@@ -65,6 +69,6 @@
 
 include ../common.mk
 
-$(FWLIB) : $(ALL_OBJS)
+$(FWLIB) : $(LIB_OBJS)
 	rm -f $@
 	ar qc $@ $^
diff --git a/firmware/include/tlcl_stub.h b/firmware/include/tlcl_stub.h
new file mode 100644
index 0000000..72582af
--- /dev/null
+++ b/firmware/include/tlcl_stub.h
@@ -0,0 +1,35 @@
+/* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+/* TPM Lightweight Command Library.
+ *
+ * A low-level library for interfacing to TPM hardware or an emulator.
+ */
+
+#ifndef VBOOT_REFERENCE_TLCL_STUB_H_
+#define VBOOT_REFERENCE_TLCL_STUB_H_
+
+#include "sysincludes.h"
+#include "tss_constants.h"
+
+/*****************************************************************************/
+/* Functions to be implemented by the stub library */
+
+/* Initialize the stub library */
+void TlclStubInit(void);
+
+/* Close and open the device.  This is needed for running more complex commands
+ * at user level, such as TPM_TakeOwnership, since the TPM device can be opened
+ * only by one process at a time.
+ */
+void TlclCloseDevice(void);
+void TlclOpenDevice(void);
+
+/* Send data to the TPM and receive a response.  Returns 0 if success,
+ * nonzero if error. */
+uint32_t TlclStubSendReceive(const uint8_t* request, int request_length,
+                             uint8_t* response, int max_length);
+
+#endif  /* VBOOT_REFERENCE_TLCL_STUB_H_ */
diff --git a/firmware/lib/tpm_lite/include/tss_constants.h b/firmware/include/tss_constants.h
similarity index 95%
rename from firmware/lib/tpm_lite/include/tss_constants.h
rename to firmware/include/tss_constants.h
index 5bbc092..00a7c03 100644
--- a/firmware/lib/tpm_lite/include/tss_constants.h
+++ b/firmware/include/tss_constants.h
@@ -6,8 +6,8 @@
  * the firmware
  */
 
-#ifndef TPM_LITE_TSS_CONSTANTS_H_
-#define TPM_LITE_TSS_CONSTANTS_H_
+#ifndef VBOOT_REFERENCE_TSS_CONSTANTS_H_
+#define VBOOT_REFERENCE_TSS_CONSTANTS_H_
 
 #include "sysincludes.h"
 
@@ -87,4 +87,4 @@
   TSS_BOOL bGlobalLock;
 } TPM_STCLEAR_FLAGS;
 
-#endif  /* TPM_LITE_TSS_CONSTANTS_H_ */
+#endif  /* VBOOT_REFERENCE_TSS_CONSTANTS_H_ */
diff --git a/firmware/lib/tpm_lite/include/tlcl.h b/firmware/lib/tpm_lite/include/tlcl.h
index 5361540..401a3ae 100644
--- a/firmware/lib/tpm_lite/include/tlcl.h
+++ b/firmware/lib/tpm_lite/include/tlcl.h
@@ -13,24 +13,7 @@
 
 #include "sysincludes.h"
 #include "tss_constants.h"
-
-/*****************************************************************************/
-/* Functions to be implemented by the stub library */
-
-/* Initialize the stub library */
-void TlclStubInit(void);
-
-/* Close and open the device.  This is needed for running more complex commands
- * at user level, such as TPM_TakeOwnership, since the TPM device can be opened
- * only by one process at a time.
- */
-void TlclCloseDevice(void);
-void TlclOpenDevice(void);
-
-/* Send data to the TPM and receive a response.  Returns 0 if success,
- * nonzero if error. */
-uint32_t TlclStubSendReceive(const uint8_t* request, int request_length,
-                             uint8_t* response, int max_length);
+#include "tlcl_stub.h"
 
 /*****************************************************************************/
 /* Functions implemented in tlcl.c */
diff --git a/host/Makefile b/host/Makefile
index 29b8231..763eb37 100644
--- a/host/Makefile
+++ b/host/Makefile
@@ -13,7 +13,7 @@
 	-I$(FWDIR)/lib/cryptolib/include
 
 # find ./lib -iname '*.c' | sort
-ALL_SRCS = \
+LIB_SRCS = \
 	./lib/file_keys.c \
 	./lib/host_common.c \
 	./lib/host_key.c \
@@ -22,12 +22,22 @@
 	./lib/host_signature.c \
 	./lib/signature_digest.c
 
+STUB_SRCS = \
+	../firmware/stub/boot_device_stub.c \
+	../firmware/stub/load_firmware_stub.c \
+	../firmware/stub/tpm_lite_stub.c \
+	../firmware/stub/utility_stub.c
+
+ALL_SRCS = ${LIB_SRCS} ${STUB_SRCS}
+
 test : $(HOSTLIB)
 	$(CC) $(CFLAGS) $(INCLUDES) -o $(BUILD_ROOT)/a.out $(TESTDIR)/main.c \
-	$(HOSTLIB) $(FWLIB) -lcrypto
+	$(HOSTLIB) -lcrypto
 
 include ../common.mk
 
-$(HOSTLIB) : $(ALL_OBJS)
-	rm -f $@
-	ar qc $@ $^
+$(HOSTLIB) : $(ALL_OBJS) $(FWLIB)
+	rm -rf $@ $(BUILD_ROOT)/.tmp
+	mkdir -p $(BUILD_ROOT)/.tmp
+	cd $(BUILD_ROOT)/.tmp ; ar x $(FWLIB)
+	ar qc $@ $^ $(BUILD_ROOT)/.tmp/*.o
diff --git a/tests/Makefile b/tests/Makefile
index 1286210..4994bdb 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -27,7 +27,7 @@
 ALL_DEPS = $(addsuffix .d,${TEST_BINS} ${TEST_LIB_OBJS})
 CFLAGS +=  -MMD -MF $@.d
 
-LIBS := ${TEST_LIB} $(HOSTLIB) $(FWLIB)
+LIBS := ${TEST_LIB} $(HOSTLIB)
 
 ifneq (${RUNTESTS},)
 EXTRA_TARGET = runtests
@@ -44,9 +44,9 @@
 ${BUILD_ROOT}/rollback_index_test.o : rollback_index_test.c
 	$(CC) $(CFLAGS) -I/usr/include $(INCLUDES) -MMD -MF $@.d -c -o $@ $<
 
-${BUILD_ROOT}/rollback_index_test: rollback_index_test.c ${HOSTLIB} ${FWLIB}
+${BUILD_ROOT}/rollback_index_test: rollback_index_test.c ${HOSTLIB}
 	$(CC) $(CFLAGS) -I/usr/include $(INCLUDES) $< -o $@ \
-        -ltlcl ${HOSTLIB} ${FWLIB} -lcrypto -lrt
+        -ltlcl ${HOSTLIB} -lcrypto -lrt
 
 ${BUILD_ROOT}/%.o : %.c
 	$(CC) $(CFLAGS) $(INCLUDES) -MMD -MF $@.d -c -o $@ $<
diff --git a/tests/tpm_lite/Makefile b/tests/tpm_lite/Makefile
index 6c4250f..69e67f3 100644
--- a/tests/tpm_lite/Makefile
+++ b/tests/tpm_lite/Makefile
@@ -31,7 +31,7 @@
 ALL_DEPS = $(addsuffix .d,${TEST_BINS})
 CFLAGS +=  -MMD -MF $@.d
 
-LIBS := ${TEST_LIB} $(HOSTLIB) $(FWLIB)
+LIBS := ${TEST_LIB} $(HOSTLIB)
 
 all: $(TEST_BINS)
 
diff --git a/utility/Makefile b/utility/Makefile
index af25ee2..6897584 100644
--- a/utility/Makefile
+++ b/utility/Makefile
@@ -10,7 +10,7 @@
 		-I$(HOSTDIR)/include
 CFLAGS += $(INCLUDES)
 CFLAGS += -MMD -MF $@.d
-LIBS = $(HOSTLIB) $(FWLIB)
+LIBS = $(HOSTLIB)
 HOSTCC = cc
 
 BUILD_ROOT = ${BUILD}/utility