Add cgpt_wrapper binary to transparently support NAND

This CL adds a "cgpt_wrapper" binary that will forward to "cgpt" binary
as needed to transparently support NAND.

The idea is that if we are working with an MTD device, this binary will
exec out to "flashrom" to read in the GPT section, then exec out to the
actual "cgpt" binary to work on that file, and finally write it back
with "flashrom".

This CL introduces a make target "install_mtd" to install this wrapper.
The corresponding ebuild should use this make target when "mtd" USE flag
is on.

BUG=chromium:442518
BRANCH=none
CQ-DEPEND=CL:239573
TEST=unittest and some trial runs with mtdram
TEST=test on a real device with NOR flash and NAND

Change-Id: I54534afa9a970ec858f313f698c0eb210c827477
Reviewed-on: https://chromium-review.googlesource.com/239580
Tested-by: Nam Nguyen <namnguyen@chromium.org>
Reviewed-by: Daniel Ehrenberg <dehrenberg@chromium.org>
Reviewed-by: Bill Richardson <wfrichar@chromium.org>
Commit-Queue: Nam Nguyen <namnguyen@chromium.org>
diff --git a/Makefile b/Makefile
index e595d2c..058b47a 100644
--- a/Makefile
+++ b/Makefile
@@ -476,6 +476,7 @@
 	cgpt/cgpt_create.c \
 	cgpt/cgpt_find.c \
 	cgpt/cgpt_legacy.c \
+	cgpt/cgpt_nor.c \
 	cgpt/cgpt_prioritize.c \
 	cgpt/cgpt_repair.c \
 	cgpt/cgpt_show.c \
@@ -489,8 +490,19 @@
 	cgpt/cmd_show.c
 
 CGPT_OBJS = ${CGPT_SRCS:%.c=${BUILD}/%.o}
+
 ALL_OBJS += ${CGPT_OBJS}
 
+CGPT_WRAPPER = ${BUILD}/cgpt/cgpt_wrapper
+
+CGPT_WRAPPER_SRCS = \
+	cgpt/cgpt_nor.c \
+	cgpt/cgpt_wrapper.c
+
+CGPT_WRAPPER_OBJS = ${CGPT_WRAPPER_SRCS:%.c=${BUILD}/%.o}
+
+ALL_OBJS += ${CGPT_WRAPPER_OBJS}
+
 # Utility defaults
 UTIL_DEFAULTS = ${BUILD}/default/vboot_reference
 
@@ -747,6 +759,9 @@
 .PHONY: install
 install: cgpt_install utils_install signing_install futil_install
 
+.PHONY: install_mtd
+install_mtd: install cgpt_wrapper_install
+
 .PHONY: install_for_test
 install_for_test: override DESTDIR = ${TEST_INSTALL_DIR}
 install_for_test: install
@@ -923,8 +938,15 @@
 # ----------------------------------------------------------------------------
 # CGPT library and utility
 
+.PHONY: cgpt_wrapper
+cgpt_wrapper: ${CGPT_WRAPPER}
+
+${CGPT_WRAPPER}: ${CGPT_WRAPPER_OBJS} ${UTILLIB}
+	@$(PRINTF) "    LD            $(subst ${BUILD}/,,$@)\n"
+	${Q}${LD} -o ${CGPT_WRAPPER} ${CFLAGS} $^
+
 .PHONY: cgpt
-cgpt: ${CGPT}
+cgpt: ${CGPT} ${CGPT_WRAPPER}
 
 ${CGPT}: LDFLAGS += -static
 ${CGPT}: LDLIBS += -luuid
@@ -939,6 +961,15 @@
 	${Q}mkdir -p ${UB_DIR}
 	${Q}${INSTALL} -t ${UB_DIR} $^
 
+.PHONY: cgpt_wrapper_install
+cgpt_wrapper_install: cgpt_install ${CGPT_WRAPPER}
+	@$(PRINTF) "    INSTALL       cgpt_wrapper\n"
+	${Q}${INSTALL} -t ${UB_DIR} ${CGPT_WRAPPER}
+	${Q}mv ${UB_DIR}/$(notdir ${CGPT}) \
+		${UB_DIR}/$(notdir ${CGPT}).bin
+	${Q}mv ${UB_DIR}/$(notdir ${CGPT_WRAPPER}) \
+		${UB_DIR}/$(notdir ${CGPT})
+
 # ----------------------------------------------------------------------------
 # Utilities