summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBill Richardson <wfrichar@chromium.org>2014-09-23 12:49:26 -0700
committerchrome-internal-fetch <chrome-internal-fetch@google.com>2014-09-25 20:26:53 +0000
commite192e7f6cea5f2c4556b729bf57ef1f552e0242a (patch)
tree1cfcde6d0b2a6a5490f4953080849f3ec9e3ab21
parentc6fa98d2ed1816d88e8517cd988de186fd6477b8 (diff)
downloadvboot-e192e7f6cea5f2c4556b729bf57ef1f552e0242a.tar.gz
futility: clean up a few shared functions
Move the Debug() function into a common place instead of several copies in different files, rename shared functions to start with "futil_" BUG=none BRANCH=ToT TEST=make runtests Signed-off-by: Bill Richardson <wfrichar@chromium.org> Change-Id: I6b844553dff95c24894dae611102716a8da5312d Reviewed-on: https://chromium-review.googlesource.com/219645 Reviewed-by: Randall Spangler <rspangler@chromium.org>
-rw-r--r--futility/cmd_dev_sign_file.c17
-rw-r--r--futility/cmd_load_fmap.c8
-rw-r--r--futility/cmd_show.c3
-rw-r--r--futility/cmd_sign.c5
-rw-r--r--futility/cmd_vbutil_kernel.c27
-rw-r--r--futility/cmd_verify_kernel.c2
-rw-r--r--futility/futility.h20
-rw-r--r--futility/misc.c38
-rw-r--r--futility/traversal.c6
9 files changed, 61 insertions, 65 deletions
diff --git a/futility/cmd_dev_sign_file.c b/futility/cmd_dev_sign_file.c
index bcfb3d62..d107bea0 100644
--- a/futility/cmd_dev_sign_file.c
+++ b/futility/cmd_dev_sign_file.c
@@ -23,9 +23,6 @@
#include "kernel_blob.h"
#include "vboot_common.h"
-/* Global opt */
-static int opt_debug;
-
/* Command line options */
enum {
OPT_MODE_SIGN = 1000,
@@ -41,7 +38,7 @@ static const struct option long_opts[] = {
{"keyblock", 1, 0, OPT_KEYBLOCK},
{"signprivate", 1, 0, OPT_SIGNPRIVATE},
{"vblock", 1, 0, OPT_VBLOCK},
- {"debug", 0, &opt_debug, 1},
+ {"debug", 0, &debugging_enabled, 1},
{NULL, 0, 0, 0}
};
@@ -72,18 +69,6 @@ static void PrintHelp(const char *progname)
"\n", progname);
}
-static void Debug(const char *format, ...)
-{
- if (!opt_debug)
- return;
-
- va_list ap;
- va_start(ap, format);
- fprintf(stderr, "DEBUG: ");
- vfprintf(stderr, format, ap);
- va_end(ap);
-}
-
/* Sign a file. We'll reuse the same structs used to sign kernels, to avoid
having to declare yet another one for just this purpose. */
static int Sign(const char *filename, const char *keyblock_file,
diff --git a/futility/cmd_load_fmap.c b/futility/cmd_load_fmap.c
index f7b978aa..7922ad62 100644
--- a/futility/cmd_load_fmap.c
+++ b/futility/cmd_load_fmap.c
@@ -92,7 +92,6 @@ static int do_load_fmap(int argc, char *argv[])
{
char *infile = 0;
char *outfile = 0;
- void *mmap_ptr = 0;
uint8_t *buf;
uint32_t len;
FmapHeader *fmap;
@@ -140,7 +139,7 @@ static int do_load_fmap(int argc, char *argv[])
/* okay, let's do it ... */
if (outfile)
- copy_file_or_die(infile, outfile);
+ futil_copy_file_or_die(infile, outfile);
else
outfile = infile;
@@ -151,10 +150,9 @@ static int do_load_fmap(int argc, char *argv[])
return 1;
}
- errorcnt |= map_it(fd, 1, &mmap_ptr, &len);
+ errorcnt |= futil_map_file(fd, MAP_RW, &buf, &len);
if (errorcnt)
goto done_file;
- buf = (uint8_t *)mmap_ptr;
fmap = fmap_find(buf, len);
if (!fmap) {
@@ -187,7 +185,7 @@ static int do_load_fmap(int argc, char *argv[])
}
done_map:
- errorcnt |= unmap_it(fd, 1, mmap_ptr, len);
+ errorcnt |= futil_unmap_file(fd, 1, buf, len);
done_file:
diff --git a/futility/cmd_show.c b/futility/cmd_show.c
index c6be07a3..8f8ebb5c 100644
--- a/futility/cmd_show.c
+++ b/futility/cmd_show.c
@@ -406,6 +406,7 @@ static const struct option long_opts[] = {
/* name hasarg *flag val */
{"publickey", 1, 0, 'k'},
{"fv", 1, 0, 'f'},
+ {"debug", 0, &debugging_enabled, 1},
{NULL, 0, NULL, 0},
};
static char *short_opts = ":f:k:";
@@ -479,7 +480,7 @@ static int do_show(int argc, char *argv[])
state.in_filename = infile ? infile : "<none>";
state.op = FUTIL_OP_SHOW;
- errorcnt += futil_traverse(ifd, &state, 0);
+ errorcnt += futil_traverse(ifd, &state, MAP_RO);
if (close(ifd)) {
errorcnt++;
diff --git a/futility/cmd_sign.c b/futility/cmd_sign.c
index 49ea5f00..a983a222 100644
--- a/futility/cmd_sign.c
+++ b/futility/cmd_sign.c
@@ -317,6 +317,7 @@ static const struct option long_opts[] = {
{"flags", 1, NULL, 'f'},
{"loemdir", 1, NULL, 'd'},
{"loemid", 1, NULL, 'l'},
+ {"debug", 0, &debugging_enabled, 1},
{NULL, 0, NULL, 0},
};
static char *short_opts = ":s:b:k:S:B:v:f:d:l:";
@@ -437,7 +438,7 @@ static int do_sign(int argc, char *argv[])
case 2:
infile = argv[optind++];
outfile = argv[optind++];
- copy_file_or_die(infile, outfile);
+ futil_copy_file_or_die(infile, outfile);
break;
case 1:
/* Stomping right on it. Errors will leave it garbled. */
@@ -468,7 +469,7 @@ static int do_sign(int argc, char *argv[])
state.in_filename = outfile ? outfile : "<none>";
state.op = FUTIL_OP_SIGN;
- errorcnt += futil_traverse(fd, &state, 1);
+ errorcnt += futil_traverse(fd, &state, MAP_RW);
if (close(fd)) {
errorcnt++;
diff --git a/futility/cmd_vbutil_kernel.c b/futility/cmd_vbutil_kernel.c
index 740c3af4..855c7a53 100644
--- a/futility/cmd_vbutil_kernel.c
+++ b/futility/cmd_vbutil_kernel.c
@@ -28,7 +28,6 @@
#include "vboot_common.h"
/* Global opts */
-static int opt_debug;
static int opt_verbose;
static int opt_vblockonly;
static uint64_t opt_pad = 65536;
@@ -54,12 +53,6 @@ enum {
OPT_MINVERSION,
};
-typedef enum {
- ARCH_ARM,
- ARCH_X86, /* default */
- ARCH_MIPS
-} arch_t;
-
static const struct option long_opts[] = {
{"pack", 1, 0, OPT_MODE_PACK},
{"repack", 1, 0, OPT_MODE_REPACK},
@@ -78,7 +71,7 @@ static const struct option long_opts[] = {
{"vblockonly", 0, 0, OPT_VBLOCKONLY},
{"pad", 1, 0, OPT_PAD},
{"verbose", 0, &opt_verbose, 1},
- {"debug", 0, &opt_debug, 1},
+ {"debug", 0, &debugging_enabled, 1},
{NULL, 0, 0, 0}
};
@@ -140,18 +133,6 @@ static void print_help(const char *progname)
printf(usage, progname, progname, progname);
}
-static void Debug(const char *format, ...)
-{
- if (!opt_debug)
- return;
-
- va_list ap;
- va_start(ap, format);
- fprintf(stderr, "DEBUG: ");
- vfprintf(stderr, format, ap);
- va_end(ap);
-}
-
static void Fatal(const char *format, ...)
{
va_list ap;
@@ -266,7 +247,7 @@ static uint64_t CmdLineOffset(VbKernelPreambleHeader *preamble)
/* This initializes g_vmlinuz and g_param from a standard vmlinuz file.
* It returns 0 on error. */
-static int ImportVmlinuzFile(const char *vmlinuz_file, arch_t arch,
+static int ImportVmlinuzFile(const char *vmlinuz_file, enum arch_t arch,
uint64_t kernel_body_load_address)
{
uint8_t *kernel_buf;
@@ -495,7 +476,7 @@ static void UnpackKernelBlob(uint8_t *kernel_blob_data,
/****************************************************************************/
static uint8_t *CreateKernBlob(uint64_t kernel_body_load_address,
- arch_t arch, uint64_t *size_ptr)
+ enum arch_t arch, uint64_t *size_ptr)
{
uint8_t *kern_blob;
uint64_t kern_blob_size;
@@ -721,7 +702,7 @@ static int do_vbutil_kernel(int argc, char *argv[])
char *vmlinuz_file = NULL;
char *bootloader_file = NULL;
char *config_file = NULL;
- arch_t arch = ARCH_X86;
+ enum arch_t arch = ARCH_X86;
char *address_str = NULL;
uint64_t kernel_body_load_address = CROS_32BIT_ENTRY_ADDR;
int mode = 0;
diff --git a/futility/cmd_verify_kernel.c b/futility/cmd_verify_kernel.c
index 28d24536..f5ee2e00 100644
--- a/futility/cmd_verify_kernel.c
+++ b/futility/cmd_verify_kernel.c
@@ -71,7 +71,7 @@ static int do_verify_kernel(int argc, char *argv[])
}
/* Load disk file */
- /* TODO: better to nmap() in the long run */
+ /* TODO: is it better to mmap() in the long run? */
diskbuf = ReadFile(argv[1], &disk_bytes);
if (!diskbuf) {
fprintf(stderr, "Can't read disk file %s\n", argv[1]);
diff --git a/futility/futility.h b/futility/futility.h
index 18261710..7892c5ad 100644
--- a/futility/futility.h
+++ b/futility/futility.h
@@ -9,6 +9,7 @@
#include "vboot_common.h"
#include "gbb_header.h"
+#include "host_key.h"
/* This program */
#define MYNAME "futility"
@@ -56,6 +57,9 @@ extern const struct futil_cmd_t *const futil_cmds[];
} while (0)
#endif
+/* Debug output (off by default) */
+extern int debugging_enabled;
+void Debug(const char *format, ...);
/* Returns true if this looks enough like a GBB header to proceed. */
int futil_looks_like_gbb(GoogleBinaryBlockHeader *gbb, uint32_t len);
@@ -68,10 +72,20 @@ int futil_valid_gbb_header(GoogleBinaryBlockHeader *gbb, uint32_t len,
uint32_t *maxlen);
/* Copies a file or dies with an error message */
-void copy_file_or_die(const char *infile, const char *outfile);
+void futil_copy_file_or_die(const char *infile, const char *outfile);
/* Wrapper for mmap/munmap. Returns 0 on success. Skips stupidly large files. */
-int map_it(int fd, int writeable, void **buf, uint32_t *len);
-int unmap_it(int fd, int writeable, void *buf, uint32_t len);
+#define MAP_RO 0
+#define MAP_RW 1
+int futil_map_file(int fd, int writeable, uint8_t **buf, uint32_t *len);
+int futil_unmap_file(int fd, int writeable, uint8_t *buf, uint32_t len);
+
+/* The CPU architecture is occasionally important */
+enum arch_t {
+ ARCH_UNSPECIFIED,
+ ARCH_X86,
+ ARCH_ARM,
+ ARCH_MIPS
+};
#endif /* VBOOT_REFERENCE_FUTILITY_H_ */
diff --git a/futility/misc.c b/futility/misc.c
index 41458ed7..513fc8b6 100644
--- a/futility/misc.c
+++ b/futility/misc.c
@@ -5,18 +5,35 @@
*/
#include <errno.h>
+#include <linux/fs.h> /* For BLKGETSIZE64 */
+#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
+#include "futility.h"
#include "gbb_header.h"
+int debugging_enabled;
+void Debug(const char *format, ...)
+{
+ if (!debugging_enabled)
+ return;
+
+ va_list ap;
+ va_start(ap, format);
+ fprintf(stderr, "DEBUG: ");
+ vfprintf(stderr, format, ap);
+ va_end(ap);
+}
+
static int is_null_terminated(const char *s, int len)
{
len--;
@@ -102,7 +119,7 @@ int futil_valid_gbb_header(GoogleBinaryBlockHeader *gbb, uint32_t len,
* TODO: All sorts of race conditions likely here, and everywhere this is used.
* Do we care? If so, fix it.
*/
-void copy_file_or_die(const char *infile, const char *outfile)
+void futil_copy_file_or_die(const char *infile, const char *outfile)
{
pid_t pid;
int status;
@@ -151,7 +168,7 @@ void copy_file_or_die(const char *infile, const char *outfile)
}
-int map_it(int fd, int writeable, void **buf, uint32_t *len)
+int futil_map_file(int fd, int writeable, uint8_t **buf, uint32_t *len)
{
struct stat sb;
void *mmap_ptr;
@@ -163,10 +180,8 @@ int map_it(int fd, int writeable, void **buf, uint32_t *len)
return 1;
}
- if (!S_ISREG(sb.st_mode)) {
- fprintf(stderr, "Block devices are not yet supported\n");
- return 1;
- }
+ if (S_ISBLK(sb.st_mode))
+ ioctl(fd, BLKGETSIZE64, &sb.st_size);
/* If the image is larger than 2^32 bytes, it's wrong. */
if (sb.st_size < 0 || sb.st_size > UINT32_MAX) {
@@ -180,7 +195,7 @@ int map_it(int fd, int writeable, void **buf, uint32_t *len)
PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
else
mmap_ptr = mmap(0, sb.st_size,
- PROT_READ, MAP_PRIVATE, fd, 0);
+ PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
if (mmap_ptr == (void *)-1) {
fprintf(stderr, "Can't mmap %s file: %s\n",
@@ -189,22 +204,23 @@ int map_it(int fd, int writeable, void **buf, uint32_t *len)
return 1;
}
- *buf = mmap_ptr;
+ *buf = (uint8_t *)mmap_ptr;
*len = reasonable_len;
return 0;
}
-int unmap_it(int fd, int writeable, void *buf, uint32_t len)
+int futil_unmap_file(int fd, int writeable, uint8_t *buf, uint32_t len)
{
+ void *mmap_ptr = buf;
int errorcnt = 0;
if (writeable &&
- (0 != msync(buf, len, MS_SYNC|MS_INVALIDATE))) {
+ (0 != msync(mmap_ptr, len, MS_SYNC|MS_INVALIDATE))) {
fprintf(stderr, "msync failed: %s\n", strerror(errno));
errorcnt++;
}
- if (0 != munmap(buf, len)) {
+ if (0 != munmap(mmap_ptr, len)) {
fprintf(stderr, "Can't munmap pointer: %s\n",
strerror(errno));
errorcnt++;
diff --git a/futility/traversal.c b/futility/traversal.c
index 7f58b74f..acd74397 100644
--- a/futility/traversal.c
+++ b/futility/traversal.c
@@ -266,7 +266,7 @@ static int traverse_buffer(uint8_t *buf, uint32_t len,
int futil_traverse(int ifd, struct futil_traverse_state_s *state,
int writeable)
{
- void *mmap_ptr = 0;
+ uint8_t *mmap_ptr = 0;
uint32_t len;
int errorcnt = 0;
@@ -275,12 +275,12 @@ int futil_traverse(int ifd, struct futil_traverse_state_s *state,
return 1;
}
- if (0 != map_it(ifd, writeable, &mmap_ptr, &len))
+ if (0 != futil_map_file(ifd, writeable, &mmap_ptr, &len))
return 1;
errorcnt |= traverse_buffer(mmap_ptr, len, state);
- errorcnt |= unmap_it(ifd, writeable, mmap_ptr, len);
+ errorcnt |= futil_unmap_file(ifd, writeable, mmap_ptr, len);
return errorcnt;
}