Build dump_fmap into futility.

This stops creating dump_fmap as a standalone utility and builds it into
futility. Since it was already invoked as a symlink, no user-visible changes
should be observed.

BUG=chromium:224734
BRANCH=none
TEST=manual, trybots

  sudo FEATURES=test emerge vboot_reference
  FEATURES=test emerge-$BOARD vboot_reference

Change-Id: I68d1bea0c1867043b2633e15509b95c2717009a7
Signed-off-by: Bill Richardson <wfrichar@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/47672
Reviewed-by: Randall Spangler <rspangler@chromium.org>
diff --git a/Makefile b/Makefile
index 7ad926d..48b16c4 100644
--- a/Makefile
+++ b/Makefile
@@ -402,7 +402,6 @@
 # These utilities should be linked statically.
 UTIL_NAMES_STATIC = \
 	utility/crossystem \
-	utility/dump_fmap \
 	utility/gbb_utility
 
 UTIL_NAMES = ${UTIL_NAMES_STATIC} \
@@ -455,11 +454,18 @@
 FUTIL_STATIC_BIN = ${FUTIL_BIN}_s
 
 # These are the others it will replace.
-FUTIL_OLD = $(notdir ${CGPT} ${UTIL_BINS} ${UTIL_SCRIPTS} \
-		${SIGNING_SCRIPTS} ${SIGNING_SCRIPTS_DEV})
+FUTIL_OLD = bmpblk_font bmpblk_utility cgpt chromeos-tpm-recovery crossystem \
+	dev_debug_vboot dev_make_keypair dev_sign_file dumpRSAPublicKey \
+	dump_fmap dump_kernel_config eficompress efidecompress \
+	enable_dev_usb_boot gbb_utility load_kernel_test \
+	make_dev_firmware.sh make_dev_ssd.sh pad_digest_utility \
+	resign_firmwarefd.sh set_gbb_flags.sh signature_digest_utility \
+	tpm-nvsize tpm_init_temp_fix tpmc vbutil_firmware vbutil_kernel \
+	vbutil_key vbutil_keyblock vbutil_what_keys verify_data
 
 FUTIL_STATIC_SRCS = \
 	futility/futility.c \
+	futility/cmd_dump_fmap.c \
 	futility/cmd_foo.c
 
 FUTIL_SRCS = \
@@ -779,6 +785,14 @@
 	${Q}for prog in ${FUTIL_OLD}; do \
 		ln -sf futility "${F_DIR}/$$prog"; done
 
+# TODO(wfrichar): This will need some refactoring (crbug.com/228932)
+${BUILD}/futility/% ${HOSTLIB}: INCLUDES += \
+	-Ihost/include \
+	-Ihost/arch/${ARCH}/include \
+	-Ihost/lib/include
+${FUTIL_STATIC_BIN} ${FUTIL_BIN}: ${HOSTLIB}
+${FUTIL_STATIC_BIN} ${FUTIL_BIN}: LIBS = ${HOSTLIB}
+
 # ----------------------------------------------------------------------------
 # Utility to generate TLCL structure definition header file.
 
diff --git a/utility/dump_fmap.c b/futility/cmd_dump_fmap.c
similarity index 97%
rename from utility/dump_fmap.c
rename to futility/cmd_dump_fmap.c
index 28fd2f2..4faf221 100644
--- a/utility/dump_fmap.c
+++ b/futility/cmd_dump_fmap.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
+ * Copyright (c) 2013 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.
  */
@@ -16,6 +16,7 @@
 #include <unistd.h>
 
 #include "fmap.h"
+#include "futility.h"
 
 enum { FMT_NORMAL, FMT_PRETTY, FMT_FLASHROM, FMT_HUMAN };
 
@@ -350,7 +351,7 @@
 /* End of human-reable stuff */
 /****************************************************************************/
 
-int main(int argc, char *argv[])
+static int do_dump_fmap(int argc, char *argv[])
 {
   int c;
   int errorcnt = 0;
@@ -470,3 +471,6 @@
 
   return retval;
 }
+
+DECLARE_FUTIL_COMMAND(dump_fmap, do_dump_fmap,
+                      "Display FMAP contents from a firmware image");
diff --git a/tests/futility/data_fmap.bin b/tests/futility/data_fmap.bin
new file mode 100644
index 0000000..67e57a0
--- /dev/null
+++ b/tests/futility/data_fmap.bin
Binary files differ
diff --git a/tests/futility/data_fmap_expect_f.txt b/tests/futility/data_fmap_expect_f.txt
new file mode 100644
index 0000000..c6658db
--- /dev/null
+++ b/tests/futility/data_fmap_expect_f.txt
@@ -0,0 +1,30 @@
+0x00000000:0x001fffff SI_ALL
+0x00000000:0x00000fff SI_DESC
+0x00001000:0x001fffff SI_ME
+0x00200000:0x007fffff SI_BIOS
+0x00200000:0x002effff RW_SECTION_A
+0x00200000:0x0020ffff VBLOCK_A
+0x00210000:0x002effbf FW_MAIN_A
+0x002effc0:0x002effff RW_FWID_A
+0x002f0000:0x003dffff RW_SECTION_B
+0x002f0000:0x002fffff VBLOCK_B
+0x00300000:0x003dffbf FW_MAIN_B
+0x003dffc0:0x003dffff RW_FWID_B
+0x003e0000:0x003effff RW_MRC_CACHE
+0x003f0000:0x003f3fff RW_ELOG
+0x003f4000:0x003f7fff RW_SHARED
+0x003f4000:0x003f5fff SHARED_DATA
+0x003f6000:0x003f7fff VBLOCK_DEV
+0x003f8000:0x003fbfff RW_ENVIRONMENT
+0x003fc000:0x003fdfff RW_VPD
+0x003fe000:0x003fffff RW_UNUSED
+0x00400000:0x005fffff RW_LEGACY
+0x00600000:0x007fffff WP_RO
+0x00600000:0x00603fff RO_VPD
+0x00604000:0x0060ffff RO_UNUSED
+0x00610000:0x007fffff RO_SECTION
+0x00610000:0x006107ff FMAP
+0x00610800:0x0061083f RO_FRID
+0x00610840:0x00610fff RO_FRID_PAD
+0x00611000:0x006fffff GBB
+0x00700000:0x007fffff BOOT_STUB
diff --git a/tests/futility/data_fmap_expect_h.txt b/tests/futility/data_fmap_expect_h.txt
new file mode 100644
index 0000000..fe31c1e
--- /dev/null
+++ b/tests/futility/data_fmap_expect_h.txt
@@ -0,0 +1,31 @@
+# name                     start       end         size
+SI_BIOS                    00200000    00800000    00600000
+  WP_RO                      00600000    00800000    00200000
+    RO_SECTION                 00610000    00800000    001f0000
+      BOOT_STUB                  00700000    00800000    00100000
+      GBB                        00611000    00700000    000ef000
+      RO_FRID_PAD                00610840    00611000    000007c0
+      RO_FRID                    00610800    00610840    00000040
+      FMAP                       00610000    00610800    00000800
+    RO_UNUSED                  00604000    00610000    0000c000
+    RO_VPD                     00600000    00604000    00004000
+  RW_LEGACY                  00400000    00600000    00200000
+  RW_UNUSED                  003fe000    00400000    00002000
+  RW_VPD                     003fc000    003fe000    00002000
+  RW_ENVIRONMENT             003f8000    003fc000    00004000
+  RW_SHARED                  003f4000    003f8000    00004000
+    VBLOCK_DEV                 003f6000    003f8000    00002000
+    SHARED_DATA                003f4000    003f6000    00002000
+  RW_ELOG                    003f0000    003f4000    00004000
+  RW_MRC_CACHE               003e0000    003f0000    00010000
+  RW_SECTION_B               002f0000    003e0000    000f0000
+    RW_FWID_B                  003dffc0    003e0000    00000040
+    FW_MAIN_B                  00300000    003dffc0    000dffc0
+    VBLOCK_B                   002f0000    00300000    00010000
+  RW_SECTION_A               00200000    002f0000    000f0000
+    RW_FWID_A                  002effc0    002f0000    00000040
+    FW_MAIN_A                  00210000    002effc0    000dffc0
+    VBLOCK_A                   00200000    00210000    00010000
+SI_ALL                     00000000    00200000    00200000
+  SI_ME                      00001000    00200000    001ff000
+  SI_DESC                    00000000    00001000    00001000
diff --git a/tests/futility/data_fmap_expect_p.txt b/tests/futility/data_fmap_expect_p.txt
new file mode 100644
index 0000000..c0cf17b
--- /dev/null
+++ b/tests/futility/data_fmap_expect_p.txt
@@ -0,0 +1,30 @@
+SI_ALL 0 2097152
+SI_DESC 0 4096
+SI_ME 4096 2093056
+SI_BIOS 2097152 6291456
+RW_SECTION_A 2097152 983040
+VBLOCK_A 2097152 65536
+FW_MAIN_A 2162688 917440
+RW_FWID_A 3080128 64
+RW_SECTION_B 3080192 983040
+VBLOCK_B 3080192 65536
+FW_MAIN_B 3145728 917440
+RW_FWID_B 4063168 64
+RW_MRC_CACHE 4063232 65536
+RW_ELOG 4128768 16384
+RW_SHARED 4145152 16384
+SHARED_DATA 4145152 8192
+VBLOCK_DEV 4153344 8192
+RW_ENVIRONMENT 4161536 16384
+RW_VPD 4177920 8192
+RW_UNUSED 4186112 8192
+RW_LEGACY 4194304 2097152
+WP_RO 6291456 2097152
+RO_VPD 6291456 16384
+RO_UNUSED 6307840 49152
+RO_SECTION 6356992 2031616
+FMAP 6356992 2048
+RO_FRID 6359040 64
+RO_FRID_PAD 6359104 1984
+GBB 6361088 978944
+BOOT_STUB 7340032 1048576
diff --git a/tests/futility/run_test_scripts.sh b/tests/futility/run_test_scripts.sh
index d886ea9..97e9d84 100755
--- a/tests/futility/run_test_scripts.sh
+++ b/tests/futility/run_test_scripts.sh
@@ -28,11 +28,12 @@
 
 # Let each test know where to find things...
 export FUTILITY
+export OLDDIR
 export SCRIPTDIR
 export OUTDIR
 
 # These are the scripts to run. Binaries are invoked directly by the Makefile.
-TESTS="${SCRIPTDIR}/test_not_really.sh"
+TESTS="${SCRIPTDIR}/test_dump_fmap.sh"
 
 
 # Get ready...
@@ -46,7 +47,7 @@
 # Here are the old programs to be wrapped
 # FIXME: dev_debug_vboot isn't tested right now.
 PROGS=${*:-cgpt crossystem dev_sign_file dumpRSAPublicKey
-           dump_fmap dump_kernel_config enable_dev_usb_boot gbb_utility
+           dump_kernel_config enable_dev_usb_boot gbb_utility
            tpm_init_temp_fix tpmc vbutil_firmware vbutil_kernel vbutil_key
            vbutil_keyblock vbutil_what_keys}
 
diff --git a/tests/futility/test_dump_fmap.sh b/tests/futility/test_dump_fmap.sh
new file mode 100755
index 0000000..e4d35d0
--- /dev/null
+++ b/tests/futility/test_dump_fmap.sh
@@ -0,0 +1,30 @@
+#!/bin/bash -eu
+# Copyright (c) 2013 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.
+
+me=${0##*/}
+
+if [ -e "$OLDDIR/dump_fmap" ] ; then
+  echo old dump_fmap binary still exists 1>&2
+  exit 1
+fi
+
+TMP="$OUTDIR/$me.tmp"
+
+"$FUTILITY" dump_fmap -f "${SCRIPTDIR}/data_fmap.bin"  > "$TMP"
+cmp "${SCRIPTDIR}/data_fmap_expect_f.txt" "$TMP"
+
+"$FUTILITY" dump_fmap -p "${SCRIPTDIR}/data_fmap.bin"  > "$TMP"
+cmp "${SCRIPTDIR}/data_fmap_expect_p.txt" "$TMP"
+
+"$FUTILITY" dump_fmap -h "${SCRIPTDIR}/data_fmap.bin"  > "$TMP"
+cmp "${SCRIPTDIR}/data_fmap_expect_h.txt" "$TMP"
+
+# This should fail because the input file is truncated and doesn't really
+# contain the stuff that the FMAP claims it does.
+cd "$OUTDIR"  # TODO: we really need a directory argument for dump_fmap.
+if "$FUTILITY" dump_fmap -x "${SCRIPTDIR}/data_fmap.bin" FMAP ; then
+  echo Wait, that was supposed to fail. 1>&2
+  exit 1
+fi
diff --git a/tests/futility/test_not_really.sh b/tests/futility/test_not_really.sh
deleted file mode 100755
index 8ea0c9a..0000000
--- a/tests/futility/test_not_really.sh
+++ /dev/null
@@ -1,13 +0,0 @@
-#!/bin/bash -eu
-# Copyright (c) 2013 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.
-
-me=${0##*/}
-
-TMP="$OUTDIR/$me.tmp"
-
-echo "FUTILITY=$FUTILITY" > "$TMP"
-echo "SCRIPTDIR=$SCRIPTDIR" >> "$TMP"
-
-exit 0