portability fixes: support building vboot on FreeBSD

Built on FreeBSD 12.1-RELEASE, 13-CURRENT, using gcc9 installed from
packages.

Change-Id: Ifa8bb343c7e916c1b545cf6c1e4bd0a18ea391cd
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/2382790
Reviewed-by: Julius Werner <jwerner@chromium.org>
Tested-by: Julius Werner <jwerner@chromium.org>
Commit-Queue: Julius Werner <jwerner@chromium.org>
diff --git a/Makefile b/Makefile
index f77e79e..637ca87 100644
--- a/Makefile
+++ b/Makefile
@@ -325,7 +325,7 @@
 .PHONY: all
 all: fwlib futil utillib hostlib cgpt tlcl \
 	$(if ${SDK_BUILD},utils_sdk,utils_board) \
-	$(if $(filter x86_64,${ARCH}),fuzzers) \
+	$(if $(filter x86_64,${ARCH}),$(if $(filter clang,${CC}),fuzzers)) \
 	$(if ${COV},coverage)
 
 ##############################################################################
@@ -944,6 +944,8 @@
 .PHONY: cgpt
 cgpt: ${CGPT} ${CGPT_WRAPPER}
 
+# on FreeBSD: install misc/e2fsprogs-libuuid from ports,
+# or e2fsprogs-libuuid from its binary package system.
 ${CGPT}: LDLIBS += -luuid
 
 ${CGPT}: ${CGPT_OBJS} ${UTILLIB}
@@ -1116,6 +1118,9 @@
 
 # Some utilities need external crypto functions
 CRYPTO_LIBS := $(shell ${PKG_CONFIG} --libs libcrypto)
+ifeq ($(shell uname -s), FreeBSD)
+CRYPTO_LIBS += -lcrypto
+endif
 
 ${BUILD}/utility/dumpRSAPublicKey: LDLIBS += ${CRYPTO_LIBS}
 ${BUILD}/utility/pad_digest_utility: LDLIBS += ${CRYPTO_LIBS}
diff --git a/cgpt/cgpt.h b/cgpt/cgpt.h
index 23f2f90..0747b5c 100644
--- a/cgpt/cgpt.h
+++ b/cgpt/cgpt.h
@@ -7,7 +7,7 @@
 #define VBOOT_REFERENCE_CGPT_H_
 
 #include <fcntl.h>
-#ifndef HAVE_MACOS
+#if !defined(HAVE_MACOS) && !defined(__FreeBSD__)
 #include <features.h>
 #endif
 #include <stdint.h>
diff --git a/cgpt/cgpt_common.c b/cgpt/cgpt_common.c
index 2ace900..426be3b 100644
--- a/cgpt/cgpt_common.c
+++ b/cgpt/cgpt_common.c
@@ -9,7 +9,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <getopt.h>
-#ifndef HAVE_MACOS
+#if !defined(HAVE_MACOS) && !defined(__FreeBSD__)
 #include <linux/major.h>
 #include <mtd/mtd-user.h>
 #endif
@@ -295,7 +295,7 @@
   if (fstat(fd, &stat) == -1) {
     return -1;
   }
-#ifndef HAVE_MACOS
+#if !defined(HAVE_MACOS) && !defined(__FreeBSD__)
   if ((stat.st_mode & S_IFMT) != S_IFREG) {
     if (ioctl(fd, BLKGETSIZE64, size) < 0) {
       return -1;
@@ -325,7 +325,7 @@
   memset(drive, 0, sizeof(struct drive));
 
   drive->fd = open(drive_path, mode |
-#ifndef HAVE_MACOS
+#if !defined(HAVE_MACOS) && !defined(__FreeBSD__)
 		               O_LARGEFILE |
 #endif
 			       O_NOFOLLOW);
diff --git a/cgpt/cgpt_endian.h b/cgpt/cgpt_endian.h
index a40b2c1..f59ab1b 100644
--- a/cgpt/cgpt_endian.h
+++ b/cgpt/cgpt_endian.h
@@ -7,8 +7,10 @@
 #define VBOOT_REFERENCE_CGPT_ENDIAN_H_
 
 // Newer distros already have this. For those that don't, we add it here.
-#ifndef HAVE_MACOS
+#if !defined(HAVE_MACOS) && !defined(__FreeBSD__)
 #include <endian.h>
+#elif defined(__FreeBSD__)
+#include <sys/endian.h>
 #endif
 
 #ifndef le16toh
diff --git a/cgpt/cgpt_nor.c b/cgpt/cgpt_nor.c
index f8b361f..fd18446 100644
--- a/cgpt/cgpt_nor.c
+++ b/cgpt/cgpt_nor.c
@@ -8,7 +8,9 @@
 #include <fcntl.h>
 #include <ftw.h>
 #include <inttypes.h>
+#if !defined(__FreeBSD__)
 #include <linux/major.h>
+#endif
 #include <stdbool.h>
 #include <stdarg.h>
 #include <stdlib.h>
diff --git a/cgpt/cgpt_wrapper.c b/cgpt/cgpt_wrapper.c
index 2b47383..d26682d 100644
--- a/cgpt/cgpt_wrapper.c
+++ b/cgpt/cgpt_wrapper.c
@@ -11,14 +11,18 @@
 #include <fcntl.h>
 #include <inttypes.h>
 #include <limits.h>
+#if !defined(__FreeBSD__)
 #include <linux/major.h>
+#endif
 #include <stdbool.h>
 #include <stdlib.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <string.h>
 #include <sys/stat.h>
+#if !defined(__FreeBSD__)
 #include <sys/sysmacros.h>
+#endif
 #include <sys/types.h>
 #include <unistd.h>
 
@@ -49,11 +53,12 @@
     return false;
   }
 
-  if (major(stat.st_rdev) != MTD_CHAR_MAJOR) {
-    return false;
+#if !defined(__FreeBSD__)
+  if (major(stat.st_rdev) == MTD_CHAR_MAJOR) {
+    return true;
   }
-
-  return true;
+#endif
+  return false;
 }
 
 // Return the element in |argv| that is an MTD device.
diff --git a/futility/cmd_vbutil_kernel.c b/futility/cmd_vbutil_kernel.c
index 1684c18..83a76f8 100644
--- a/futility/cmd_vbutil_kernel.c
+++ b/futility/cmd_vbutil_kernel.c
@@ -9,7 +9,7 @@
 #include <fcntl.h>
 #include <getopt.h>
 #include <inttypes.h>		/* For PRIu64 */
-#ifndef HAVE_MACOS
+#if !defined(HAVE_MACOS) && !defined(__FreeBSD__)
 #include <linux/fs.h>		/* For BLKGETSIZE64 */
 #endif
 #include <stdarg.h>
@@ -173,7 +173,7 @@
 		FATAL("Unable to stat %s: %s\n", filename, strerror(errno));
 
 	if (S_ISBLK(statbuf.st_mode)) {
-#ifndef HAVE_MACOS
+#if !defined(HAVE_MACOS) && !defined(__FreeBSD__)
 		int fd = open(filename, O_RDONLY);
 		if (fd >= 0) {
 			ioctl(fd, BLKGETSIZE64, &file_size);
diff --git a/futility/dump_kernel_config_lib.c b/futility/dump_kernel_config_lib.c
index 5f7aa20..4240e5e 100644
--- a/futility/dump_kernel_config_lib.c
+++ b/futility/dump_kernel_config_lib.c
@@ -10,7 +10,9 @@
 #include <string.h>
 #include <sys/mman.h>
 #include <sys/stat.h>
+#if !defined (__FreeBSD__)
 #include <sys/sysmacros.h>
+#endif
 #include <sys/types.h>
 #include <unistd.h>
 
@@ -122,7 +124,11 @@
 {
 	char *newstr = NULL;
 
-	int fd = open(infile, O_RDONLY | O_CLOEXEC | O_LARGEFILE);
+	int fd = open(infile, O_RDONLY | O_CLOEXEC
+#if !defined(__FreeBSD__)
+			| O_LARGEFILE
+#endif
+			);
 	if (fd < 0) {
 		FATAL("Cannot open %s\n", infile);
 		return NULL;
diff --git a/futility/misc.c b/futility/misc.c
index de7db51..0c8a0e7 100644
--- a/futility/misc.c
+++ b/futility/misc.c
@@ -5,7 +5,7 @@
 
 #include <assert.h>
 #include <errno.h>
-#ifndef HAVE_MACOS
+#if !defined(HAVE_MACOS) && !defined(__FreeBSD__)
 #include <linux/fs.h>		/* For BLKGETSIZE64 */
 #endif
 #include <stdarg.h>
@@ -272,7 +272,7 @@
 		return FILE_ERR_STAT;
 	}
 
-#ifndef HAVE_MACOS
+#if !defined(HAVE_MACOS) && !defined(__FreeBSD__)
 	if (S_ISBLK(sb.st_mode))
 		ioctl(fd, BLKGETSIZE64, &sb.st_size);
 #endif
diff --git a/futility/updater_utils.c b/futility/updater_utils.c
index e87094b..5a2a5e6 100644
--- a/futility/updater_utils.c
+++ b/futility/updater_utils.c
@@ -10,6 +10,9 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
+#if defined (__FreeBSD__)
+#include <sys/wait.h>
+#endif
 
 #include "2common.h"
 #include "crossystem.h"
diff --git a/host/arch/x86/lib/crossystem_arch.c b/host/arch/x86/lib/crossystem_arch.c
index 3d1c818..6a8f5a2 100644
--- a/host/arch/x86/lib/crossystem_arch.c
+++ b/host/arch/x86/lib/crossystem_arch.c
@@ -7,8 +7,10 @@
 #include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
+#if !defined(__FreeBSD__)
 #include <linux/nvram.h>
 #include <linux/version.h>
+#endif
 #include <stddef.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -98,8 +100,10 @@
 
 static void VbFixCmosChecksum(FILE* file)
 {
+#if !defined(__FreeBSD__)
 	int fd = fileno(file);
 	ioctl(fd, NVRAM_SETCKS);
+#endif
 }
 
 
@@ -662,9 +666,11 @@
 
 	if (uname(&host) == 0) {
 		if (sscanf(host.release, "%u.%u.", &maj, &min) == 2) {
+#if !defined(__FreeBSD__)
 			if (KERNEL_VERSION(maj, min, 0) >= KERNEL_VERSION(4, 16, 0) &&
 			    *offset > 11)
 				*offset += 3;
+#endif
 		} else {
 			printf("Couldn't retrieve kernel version!\n");
 			ret = 0;
diff --git a/host/lib/flashrom.c b/host/lib/flashrom.c
index b1647ae..62ab1bc 100644
--- a/host/lib/flashrom.c
+++ b/host/lib/flashrom.c
@@ -47,6 +47,10 @@
 	char *path;
 	mode_t umask_save;
 
+#if defined(__FreeBSD__)
+#define P_tmpdir "/tmp"
+#endif
+
 	*path_out = NULL;
 	path = strdup(P_tmpdir "/vb2_flashrom.XXXXXX");
 
diff --git a/scripts/getversion.sh b/scripts/getversion.sh
index d505579..a563007 100755
--- a/scripts/getversion.sh
+++ b/scripts/getversion.sh
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/usr/bin/env bash
 #
 # Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
 # Use of this source code is governed by a BSD-style license that can be