Merge "Merge branch 'upstream' into tizen" into tizen
[platform/upstream/cryptsetup.git] / src / veritysetup.c
index 7de039b..fc32cca 100644 (file)
@@ -1,11 +1,13 @@
 /*
  * veritysetup - setup cryptographic volumes for dm-verity
  *
- * Copyright (C) 2012, Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2012-2021 Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2012-2021 Milan Broz
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
- * version 2 as published by the Free Software Foundation.
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdarg.h>
-#include <errno.h>
-#include <string.h>
-#include <ctype.h>
-#include <inttypes.h>
-#include <popt.h>
-#include <limits.h>
-#include <sys/stat.h>
-
 #include "cryptsetup.h"
 
 #define PACKAGE_VERITY "veritysetup"
 
-static int use_superblock = 1;
-
-static const char *hash_algorithm = NULL;
-static int hash_type = 1;
-static int data_block_size = DEFAULT_VERITY_DATA_BLOCK;
-static int hash_block_size = DEFAULT_VERITY_HASH_BLOCK;
+static char *opt_fec_device = NULL;
+static char *opt_hash_algorithm = NULL;
+static char *opt_salt = NULL;
+static char *opt_uuid = NULL;
+static char *opt_root_hash_signature = NULL;
+
+static int opt_use_superblock = 1;
+static int opt_fec_roots = DEFAULT_VERITY_FEC_ROOTS;
+static int opt_hash_type = 1;
+static int opt_data_block_size = DEFAULT_VERITY_DATA_BLOCK;
+static int opt_hash_block_size = DEFAULT_VERITY_HASH_BLOCK;
 static uint64_t data_blocks = 0;
-static const char *salt_string = NULL;
 static uint64_t hash_offset = 0;
-static const char *opt_uuid = NULL;
-
-static int opt_verbose = 0;
-static int opt_debug = 0;
-static int opt_version_mode = 0;
+static uint64_t fec_offset = 0;
+static int opt_restart_on_corruption = 0;
+static int opt_panic_on_corruption = 0;
+static int opt_ignore_corruption = 0;
+static int opt_ignore_zero_blocks = 0;
+static int opt_check_at_most_once = 0;
 
 static const char **action_argv;
 static int action_argc;
 
-static size_t hex_to_bytes(const char *hex, char **result)
+void tools_cleanup(void)
 {
-       char buf[3] = "xx\0", *endp, *bytes;
-       size_t i, len;
-
-       len = strlen(hex);
-       if (len % 2)
-               return -EINVAL;
-       len /= 2;
-
-       if (!(bytes = malloc(len)))
-               return -ENOMEM;
-
-       for (i = 0; i < len; i++) {
-               memcpy(buf, &hex[i * 2], 2);
-               bytes[i] = strtoul(buf, &endp, 16);
-               if (endp != &buf[2]) {
-                       free(bytes);
-                       return -EINVAL;
-               }
-       }
-       *result = bytes;
-       return i;
-}
-
-__attribute__((format(printf, 5, 6)))
-static void clogger(struct crypt_device *cd, int level, const char *file,
-                  int line, const char *format, ...)
-{
-       va_list argp;
-       char *target = NULL;
-
-       va_start(argp, format);
-
-       if (vasprintf(&target, format, argp) > 0) {
-               if (level >= 0) {
-                       crypt_log(cd, level, target);
-#ifdef CRYPT_DEBUG
-               } else if (opt_debug)
-                       printf("# %s:%d %s\n", file ?: "?", line, target);
-#else
-               } else if (opt_debug)
-                       printf("# %s\n", target);
-#endif
-       }
-
-       va_end(argp);
-       free(target);
-}
-
-static void _log(int level, const char *msg, void *usrptr __attribute__((unused)))
-{
-       switch(level) {
-
-       case CRYPT_LOG_NORMAL:
-               fputs(msg, stdout);
-               break;
-       case CRYPT_LOG_VERBOSE:
-               if (opt_verbose)
-                       fputs(msg, stdout);
-               break;
-       case CRYPT_LOG_ERROR:
-               fputs(msg, stderr);
-               break;
-       case CRYPT_LOG_DEBUG:
-               if (opt_debug)
-                       printf("# %s\n", msg);
-               break;
-       default:
-               fprintf(stderr, "Internal error on logging class for msg: %s", msg);
-               break;
-       }
+       FREE_AND_NULL(opt_fec_device);
+       FREE_AND_NULL(opt_hash_algorithm);
+       FREE_AND_NULL(opt_salt);
+       FREE_AND_NULL(opt_uuid);
+       FREE_AND_NULL(opt_root_hash_signature);
 }
 
 static int _prepare_format(struct crypt_params_verity *params,
@@ -131,16 +62,18 @@ static int _prepare_format(struct crypt_params_verity *params,
        char *salt = NULL;
        int len;
 
-       params->hash_name = hash_algorithm ?: DEFAULT_VERITY_HASH;
+       params->hash_name = opt_hash_algorithm ?: DEFAULT_VERITY_HASH;
        params->data_device = data_device;
+       params->fec_device = opt_fec_device;
+       params->fec_roots = opt_fec_roots;
 
-       if (salt_string && !strcmp(salt_string, "-")) {
+       if (opt_salt && !strcmp(opt_salt, "-")) {
                params->salt_size = 0;
                params->salt = NULL;
-       } else if (salt_string) {
-               len = hex_to_bytes(salt_string, &salt);
+       } else if (opt_salt) {
+               len = crypt_hex_to_bytes(opt_salt, &salt, 0);
                if (len < 0) {
-                       log_err(_("Invalid salt string specified.\n"));
+                       log_err(_("Invalid salt string specified."));
                        return -EINVAL;
                }
                params->salt_size = len;
@@ -150,11 +83,12 @@ static int _prepare_format(struct crypt_params_verity *params,
                params->salt = NULL;
        }
 
-       params->data_block_size = data_block_size;
-       params->hash_block_size = hash_block_size;
+       params->data_block_size = opt_data_block_size;
+       params->hash_block_size = opt_hash_block_size;
        params->data_size = data_blocks;
        params->hash_area_offset = hash_offset;
-       params->hash_type = hash_type;
+       params->fec_area_offset = fec_offset;
+       params->hash_type = opt_hash_type;
        params->flags = flags;
 
        return 0;
@@ -167,10 +101,31 @@ static int action_format(int arg)
        uint32_t flags = CRYPT_VERITY_CREATE_HASH;
        int r;
 
+       /* Try to create hash image if doesn't exist */
+       r = open(action_argv[1], O_WRONLY | O_EXCL | O_CREAT, S_IRUSR | S_IWUSR);
+       if (r < 0 && errno != EEXIST) {
+               log_err(_("Cannot create hash image %s for writing."), action_argv[1]);
+               return -EINVAL;
+       } else if (r >= 0) {
+               log_dbg("Created hash image %s.", action_argv[1]);
+               close(r);
+       }
+       /* Try to create FEC image if doesn't exist */
+       if (opt_fec_device) {
+               r = open(opt_fec_device, O_WRONLY | O_EXCL | O_CREAT, S_IRUSR | S_IWUSR);
+               if (r < 0 && errno != EEXIST) {
+                       log_err(_("Cannot create FEC image %s for writing."), opt_fec_device);
+                       return -EINVAL;
+               } else if (r >= 0) {
+                       log_dbg("Created FEC image %s.", opt_fec_device);
+                       close(r);
+               }
+       }
+
        if ((r = crypt_init(&cd, action_argv[1])))
                goto out;
 
-       if (!use_superblock)
+       if (!opt_use_superblock)
                flags |= CRYPT_VERITY_NO_HEADER;
 
        r = _prepare_format(&params, action_argv[0], flags);
@@ -182,7 +137,7 @@ static int action_format(int arg)
                crypt_dump(cd);
 out:
        crypt_free(cd);
-       free((char*)params.salt);
+       free(CONST_CAST(char*)params.salt);
        return r;
 }
 
@@ -196,15 +151,31 @@ static int _activate(const char *dm_device,
        struct crypt_params_verity params = {};
        uint32_t activate_flags = CRYPT_ACTIVATE_READONLY;
        char *root_hash_bytes = NULL;
-       size_t hash_size;
-       int r;
+       ssize_t hash_size;
+       struct stat st;
+       char *signature = NULL;
+       int signature_size = 0, r;
 
-       if ((r = crypt_init(&cd, hash_device)))
+       if ((r = crypt_init_data_device(&cd, hash_device, data_device)))
                goto out;
 
-       if (use_superblock) {
+       if (opt_ignore_corruption)
+               activate_flags |= CRYPT_ACTIVATE_IGNORE_CORRUPTION;
+       if (opt_restart_on_corruption)
+               activate_flags |= CRYPT_ACTIVATE_RESTART_ON_CORRUPTION;
+       if (opt_panic_on_corruption)
+               activate_flags |= CRYPT_ACTIVATE_PANIC_ON_CORRUPTION;
+       if (opt_ignore_zero_blocks)
+               activate_flags |= CRYPT_ACTIVATE_IGNORE_ZERO_BLOCKS;
+       if (opt_check_at_most_once)
+               activate_flags |= CRYPT_ACTIVATE_CHECK_AT_MOST_ONCE;
+
+       if (opt_use_superblock) {
                params.flags = flags;
                params.hash_area_offset = hash_offset;
+               params.fec_area_offset = fec_offset;
+               params.fec_device = opt_fec_device;
+               params.fec_roots = opt_fec_roots;
                r = crypt_load(cd, CRYPT_VERITY, &params);
        } else {
                r = _prepare_format(&params, data_device, flags | CRYPT_VERITY_NO_HEADER);
@@ -214,33 +185,48 @@ static int _activate(const char *dm_device,
        }
        if (r < 0)
                goto out;
-       r = crypt_set_data_device(cd, data_device);
-       if (r < 0)
-               goto out;
 
        hash_size = crypt_get_volume_key_size(cd);
-       if (hex_to_bytes(root_hash, &root_hash_bytes) != hash_size) {
-               log_err(_("Invalid root hash string specified.\n"));
+       if (crypt_hex_to_bytes(root_hash, &root_hash_bytes, 0) != hash_size) {
+               log_err(_("Invalid root hash string specified."));
                r = -EINVAL;
                goto out;
        }
-       r = crypt_activate_by_volume_key(cd, dm_device,
+
+       if (opt_root_hash_signature) {
+               // FIXME: check max file size
+               if (stat(opt_root_hash_signature, &st) || !S_ISREG(st.st_mode) || !st.st_size) {
+                       log_err(_("Invalid signature file %s."), opt_root_hash_signature);
+                       r = -EINVAL;
+                       goto out;
+               }
+               signature_size = st.st_size;
+               r = tools_read_mk(opt_root_hash_signature, &signature, signature_size);
+               if (r < 0) {
+                       log_err(_("Cannot read signature file %s."), opt_root_hash_signature);
+                       goto out;
+               }
+       }
+       r = crypt_activate_by_signed_key(cd, dm_device,
                                         root_hash_bytes,
                                         hash_size,
+                                        signature, signature_size,
                                         activate_flags);
 out:
+       crypt_safe_free(signature);
        crypt_free(cd);
        free(root_hash_bytes);
-       free((char*)params.salt);
+       free(CONST_CAST(char*)params.salt);
        return r;
 }
 
-static int action_create(int arg)
+static int action_open(int arg)
 {
-       return _activate(action_argv[0],
-                        action_argv[1],
+       return _activate(action_argv[1],
+                        action_argv[0],
                         action_argv[2],
-                        action_argv[3], 0);
+                        action_argv[3],
+                        opt_root_hash_signature ? CRYPT_VERITY_ROOT_HASH_SIGNATURE : 0);
 }
 
 static int action_verify(int arg)
@@ -252,7 +238,7 @@ static int action_verify(int arg)
                         CRYPT_VERITY_CHECK_HASH);
 }
 
-static int action_remove(int arg)
+static int action_close(int arg)
 {
        struct crypt_device *cd = NULL;
        int r;
@@ -272,7 +258,8 @@ static int action_status(int arg)
        struct crypt_params_verity vp = {};
        struct crypt_device *cd = NULL;
        struct stat st;
-       char *backing_file;
+       char *backing_file, *root_hash;
+       size_t root_hash_size;
        unsigned i, path = 0;
        int r = 0;
 
@@ -302,22 +289,24 @@ static int action_status(int arg)
                                ci == CRYPT_BUSY ? " and is in use" : "");
 
                r = crypt_init_by_name_and_header(&cd, action_argv[0], NULL);
-               if (r < 0 || !crypt_get_type(cd))
+               if (r < 0)
                        goto out;
 
-               log_std("  type:        %s\n", crypt_get_type(cd));
+               log_std("  type:        %s\n", crypt_get_type(cd) ?: "n/a");
 
                r = crypt_get_active_device(cd, action_argv[0], &cad);
                if (r < 0)
                        goto out;
 
-               log_std("  status:      %s\n",
-                       cad.flags & CRYPT_ACTIVATE_CORRUPTED ? "corrupted" : "verified");
-
+               /* Print only VERITY type devices */
                r = crypt_get_verity_info(cd, &vp);
                if (r < 0)
                        goto out;
 
+               log_std("  status:      %s%s\n",
+                       cad.flags & CRYPT_ACTIVATE_CORRUPTED ? "corrupted" : "verified",
+                       vp.flags & CRYPT_VERITY_ROOT_HASH_SIGNATURE ? " (with signature)" : "");
+
                log_std("  hash type:   %u\n", vp.hash_type);
                log_std("  data block:  %u\n", vp.data_block_size);
                log_std("  hash block:  %u\n", vp.hash_block_size);
@@ -331,8 +320,7 @@ static int action_status(int arg)
                log_std("\n");
 
                log_std("  data device: %s\n", vp.data_device);
-               if (crypt_loop_device(vp.data_device)) {
-                       backing_file = crypt_loop_backing_file(vp.data_device);
+               if ((backing_file = crypt_loop_backing_file(vp.data_device))) {
                        log_std("  data loop:   %s\n", backing_file);
                        free(backing_file);
                }
@@ -341,13 +329,47 @@ static int action_status(int arg)
                                           "readonly" : "read/write");
 
                log_std("  hash device: %s\n", vp.hash_device);
-               if (crypt_loop_device(vp.hash_device)) {
-                       backing_file = crypt_loop_backing_file(vp.hash_device);
+               if ((backing_file = crypt_loop_backing_file(vp.hash_device))) {
                        log_std("  hash loop:   %s\n", backing_file);
                        free(backing_file);
                }
                log_std("  hash offset: %" PRIu64 " sectors\n",
                        vp.hash_area_offset * vp.hash_block_size / 512);
+
+               if (vp.fec_device) {
+                       log_std("  FEC device:  %s\n", vp.fec_device);
+                       if ((backing_file = crypt_loop_backing_file(opt_fec_device))) {
+                               log_std("  FEC loop:    %s\n", backing_file);
+                               free(backing_file);
+                       }
+                       log_std("  FEC offset:  %" PRIu64 " sectors\n",
+                               vp.fec_area_offset * vp.hash_block_size / 512);
+                       log_std("  FEC roots:   %u\n", vp.fec_roots);
+               }
+
+               root_hash_size = crypt_get_volume_key_size(cd);
+               if (root_hash_size > 0 && (root_hash = malloc(root_hash_size))) {
+                       r = crypt_volume_key_get(cd, CRYPT_ANY_SLOT, root_hash, &root_hash_size, NULL, 0);
+                       if (!r) {
+                               log_std("  root hash:   ");
+                               for (i = 0; i < root_hash_size; i++)
+                                       log_std("%02hhx", (const char)root_hash[i]);
+                               log_std("\n");
+                       }
+                       free(root_hash);
+               }
+
+               if (cad.flags & (CRYPT_ACTIVATE_IGNORE_CORRUPTION|
+                                CRYPT_ACTIVATE_RESTART_ON_CORRUPTION|
+                                CRYPT_ACTIVATE_PANIC_ON_CORRUPTION|
+                                CRYPT_ACTIVATE_IGNORE_ZERO_BLOCKS|
+                                CRYPT_ACTIVATE_CHECK_AT_MOST_ONCE))
+                       log_std("  flags:       %s%s%s%s%s\n",
+                               (cad.flags & CRYPT_ACTIVATE_IGNORE_CORRUPTION) ? "ignore_corruption " : "",
+                               (cad.flags & CRYPT_ACTIVATE_RESTART_ON_CORRUPTION) ? "restart_on_corruption " : "",
+                               (cad.flags & CRYPT_ACTIVATE_PANIC_ON_CORRUPTION) ? "panic_on_corruption " : "",
+                               (cad.flags & CRYPT_ACTIVATE_IGNORE_ZERO_BLOCKS) ? "ignore_zero_blocks " : "",
+                               (cad.flags & CRYPT_ACTIVATE_CHECK_AT_MOST_ONCE) ? "check_at_most_once" : "");
        }
 out:
        crypt_free(cd);
@@ -366,6 +388,7 @@ static int action_dump(int arg)
                return r;
 
        params.hash_area_offset = hash_offset;
+       params.fec_area_offset = fec_offset;
        r = crypt_load(cd, CRYPT_VERITY, &params);
        if (!r)
                crypt_dump(cd);
@@ -373,30 +396,6 @@ static int action_dump(int arg)
        return r;
 }
 
-static __attribute__ ((noreturn)) void usage(poptContext popt_context,
-                                            int exitcode, const char *error,
-                                            const char *more)
-{
-       poptPrintUsage(popt_context, stderr, 0);
-       if (error)
-               log_err("%s: %s\n", more, error);
-       poptFreeContext(popt_context);
-       exit(exitcode);
-}
-
-static void _dbg_version_and_cmd(int argc, const char **argv)
-{
-       int i;
-
-       log_std("# %s %s processing \"", PACKAGE_VERITY, PACKAGE_VERSION);
-       for (i = 0; i < argc; i++) {
-               if (i)
-                       log_std(" ");
-               log_std("%s", argv[i]);
-       }
-       log_std("\"\n");
-}
-
 static struct action_type {
        const char *type;
        int (*handler)(int);
@@ -406,8 +405,8 @@ static struct action_type {
 } action_types[] = {
        { "format",     action_format, 2, N_("<data_device> <hash_device>"),N_("format device") },
        { "verify",     action_verify, 3, N_("<data_device> <hash_device> <root_hash>"),N_("verify device") },
-       { "create",     action_create, 4, N_("<name> <data_device> <hash_device> <root_hash>"),N_("create active device") },
-       { "remove",     action_remove, 1, N_("<name>"),N_("remove (deactivate) device") },
+       { "open",       action_open,   4, N_("<data_device> <name> <hash_device> <root_hash>"),N_("open device as <name>") },
+       { "close",      action_close,  1, N_("<name>"),N_("close device (remove mapping)") },
        { "status",     action_status, 1, N_("<name>"),N_("show active device status") },
        { "dump",       action_dump,   1, N_("<hash_device>"),N_("show on-disk information") },
        { NULL, NULL, 0, NULL, NULL }
@@ -441,40 +440,18 @@ static void help(poptContext popt_context,
                        DEFAULT_VERITY_HASH, DEFAULT_VERITY_DATA_BLOCK,
                        DEFAULT_VERITY_HASH_BLOCK, DEFAULT_VERITY_SALT_SIZE,
                        1);
+               tools_cleanup();
+               poptFreeContext(popt_context);
+               exit(EXIT_SUCCESS);
+       } else if (key->shortName == 'V') {
+               log_std("%s %s\n", PACKAGE_VERITY, PACKAGE_VERSION);
+               tools_cleanup();
+               poptFreeContext(popt_context);
                exit(EXIT_SUCCESS);
        } else
                usage(popt_context, EXIT_SUCCESS, NULL, NULL);
 }
 
-static void show_status(int errcode)
-{
-       char error[256], *error_;
-
-       if(!opt_verbose)
-               return;
-
-       if(!errcode) {
-               log_std(_("Command successful.\n"));
-               return;
-       }
-
-       crypt_get_error(error, sizeof(error));
-
-       if (!error[0]) {
-               error_ = strerror_r(-errcode, error, sizeof(error));
-               if (error_ != error) {
-                       strncpy(error, error_, sizeof(error));
-                       error[sizeof(error) - 1] = '\0';
-               }
-       }
-
-       log_err(_("Command failed with code %i"), -errcode);
-       if (*error)
-               log_err(": %s\n", error);
-       else
-               log_err(".\n");
-}
-
 static int run_action(struct action_type *action)
 {
        int r;
@@ -484,48 +461,41 @@ static int run_action(struct action_type *action)
        r = action->handler(0);
 
        show_status(r);
-
-       /* Translate exit code to simple codes */
-       switch (r) {
-       case 0:         r = EXIT_SUCCESS; break;
-       case -EEXIST:
-       case -EBUSY:    r = 5; break;
-       case -ENOTBLK:
-       case -ENODEV:   r = 4; break;
-       case -ENOMEM:   r = 3; break;
-       case -EPERM:    r = 2; break;
-       case -EINVAL:
-       case -ENOENT:
-       case -ENOSYS:
-       default:        r = EXIT_FAILURE;
-       }
-       return r;
+       return translate_errno(r);
 }
 
 int main(int argc, const char **argv)
 {
-       static char *popt_tmp;
        static const char *null_action_argv[] = {NULL};
        static struct poptOption popt_help_options[] = {
                { NULL,    '\0', POPT_ARG_CALLBACK, help, 0, NULL,                         NULL },
                { "help",  '?',  POPT_ARG_NONE,     NULL, 0, N_("Show this help message"), NULL },
                { "usage", '\0', POPT_ARG_NONE,     NULL, 0, N_("Display brief usage"),    NULL },
+               { "version",'V', POPT_ARG_NONE,     NULL, 0, N_("Print package version"),  NULL },
                POPT_TABLEEND
        };
        static struct poptOption popt_options[] = {
                { NULL,              '\0', POPT_ARG_INCLUDE_TABLE, popt_help_options, 0, N_("Help options:"), NULL },
-               { "version",         '\0', POPT_ARG_NONE, &opt_version_mode, 0, N_("Print package version"), NULL },
                { "verbose",         'v',  POPT_ARG_NONE, &opt_verbose,      0, N_("Shows more detailed error messages"), NULL },
                { "debug",           '\0', POPT_ARG_NONE, &opt_debug,        0, N_("Show debug messages"), NULL },
-               { "no-superblock",   0,    POPT_ARG_VAL,  &use_superblock,   0, N_("Do not use verity superblock"), NULL },
-               { "format",          0,    POPT_ARG_INT,  &hash_type,        0, N_("Format type (1 - normal, 0 - original Chrome OS)"), N_("number") },
-               { "data-block-size", 0,    POPT_ARG_INT,  &data_block_size,  0, N_("Block size on the data device"), N_("bytes") },
-               { "hash-block-size", 0,    POPT_ARG_INT,  &hash_block_size,  0, N_("Block size on the hash device"), N_("bytes") },
-               { "data-blocks",     0,    POPT_ARG_STRING, &popt_tmp,       1, N_("The number of blocks in the data file"), N_("blocks") },
-               { "hash-offset",     0,    POPT_ARG_STRING, &popt_tmp,       2, N_("Starting offset on the hash device"), N_("bytes") },
-               { "hash",            'h',  POPT_ARG_STRING, &hash_algorithm, 0, N_("Hash algorithm"), N_("string") },
-               { "salt",            's',  POPT_ARG_STRING, &salt_string,    0, N_("Salt"), N_("hex string") },
-               { "uuid",            '\0', POPT_ARG_STRING, &opt_uuid,       0, N_("UUID for device to use."), NULL },
+               { "no-superblock",   0,    POPT_ARG_VAL,  &opt_use_superblock,   0, N_("Do not use verity superblock"), NULL },
+               { "format",          0,    POPT_ARG_INT,  &opt_hash_type,        0, N_("Format type (1 - normal, 0 - original Chrome OS)"), N_("number") },
+               { "data-block-size", 0,    POPT_ARG_INT,  &opt_data_block_size,  0, N_("Block size on the data device"), N_("bytes") },
+               { "hash-block-size", 0,    POPT_ARG_INT,  &opt_hash_block_size,  0, N_("Block size on the hash device"), N_("bytes") },
+               { "fec-roots",       0,    POPT_ARG_INT,  &opt_fec_roots,        0, N_("FEC parity bytes"), N_("bytes") },
+               { "data-blocks",     0,    POPT_ARG_STRING, NULL,       1, N_("The number of blocks in the data file"), N_("blocks") },
+               { "fec-device",      0,    POPT_ARG_STRING, &opt_fec_device,     0, N_("Path to device with error correction data"), N_("path") },
+               { "hash-offset",     0,    POPT_ARG_STRING, NULL,       2, N_("Starting offset on the hash device"), N_("bytes") },
+               { "fec-offset",      0,    POPT_ARG_STRING, NULL,       3, N_("Starting offset on the FEC device"), N_("bytes") },
+               { "hash",            'h',  POPT_ARG_STRING, &opt_hash_algorithm, 0, N_("Hash algorithm"), N_("string") },
+               { "salt",            's',  POPT_ARG_STRING, &opt_salt,    0, N_("Salt"), N_("hex string") },
+               { "uuid",            '\0', POPT_ARG_STRING, &opt_uuid,       0, N_("UUID for device to use"), NULL },
+               { "root-hash-signature",'\0', POPT_ARG_STRING, &opt_root_hash_signature,  0, N_("Path to root hash signature file"), NULL },
+               { "restart-on-corruption", 0,POPT_ARG_NONE,&opt_restart_on_corruption, 0, N_("Restart kernel if corruption is detected"), NULL },
+               { "panic-on-corruption", 0,POPT_ARG_NONE, &opt_panic_on_corruption, 0, N_("Panic kernel if corruption is detected"), NULL },
+               { "ignore-corruption", 0,  POPT_ARG_NONE, &opt_ignore_corruption,  0, N_("Ignore corruption, log it only"), NULL },
+               { "ignore-zero-blocks", 0, POPT_ARG_NONE, &opt_ignore_zero_blocks, 0, N_("Do not verify zeroed blocks"), NULL },
+               { "check-at-most-once", 0, POPT_ARG_NONE, &opt_check_at_most_once, 0, N_("Verify data block only the first time it is read"), NULL },
                POPT_TABLEEND
        };
 
@@ -534,7 +504,7 @@ int main(int argc, const char **argv)
        const char *aname;
        int r;
 
-       crypt_set_log_callback(NULL, _log, NULL);
+       crypt_set_log_callback(NULL, tool_log, NULL);
 
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
@@ -542,19 +512,21 @@ int main(int argc, const char **argv)
 
        popt_context = poptGetContext("verity", argc, argv, popt_options, 0);
        poptSetOtherOptionHelp(popt_context,
-                              N_("[OPTION...] <action> <action-specific>"));
+                              _("[OPTION...] <action> <action-specific>"));
 
        while((r = poptGetNextOpt(popt_context)) > 0) {
                unsigned long long ull_value;
-               char *endp;
+               char *endp, *str = poptGetOptArg(popt_context);
 
                errno = 0;
-               ull_value = strtoull(popt_tmp, &endp, 10);
-               if (*endp || !*popt_tmp || !isdigit(*popt_tmp) ||
+               ull_value = strtoull(str, &endp, 10);
+               if (*endp || !*str || !isdigit(*str) ||
                    (errno == ERANGE && ull_value == ULLONG_MAX) ||
                    (errno != 0 && ull_value == 0))
                        r = POPT_ERROR_BADNUMBER;
 
+               free(str);
+
                switch(r) {
                        case 1:
                                data_blocks = ull_value;
@@ -562,6 +534,9 @@ int main(int argc, const char **argv)
                        case 2:
                                hash_offset = ull_value;
                                break;
+                       case 3:
+                               fec_offset = ull_value;
+                               break;
                }
 
                if (r < 0)
@@ -572,21 +547,9 @@ int main(int argc, const char **argv)
                usage(popt_context, EXIT_FAILURE, poptStrerror(r),
                      poptBadOption(popt_context, POPT_BADOPTION_NOALIAS));
 
-       if (opt_version_mode) {
-               log_std("%s %s\n", PACKAGE_VERITY, PACKAGE_VERSION);
-               poptFreeContext(popt_context);
-               exit(EXIT_SUCCESS);
-       }
-
        if (!(aname = poptGetArg(popt_context)))
                usage(popt_context, EXIT_FAILURE, _("Argument <action> missing."),
                      poptGetInvocationName(popt_context));
-       for(action = action_types; action->type; action++)
-               if (strcmp(action->type, aname) == 0)
-                       break;
-       if (!action->type)
-               usage(popt_context, EXIT_FAILURE, _("Unknown action."),
-                     poptGetInvocationName(popt_context));
 
        action_argc = 0;
        action_argv = poptGetArgs(popt_context);
@@ -598,26 +561,68 @@ int main(int argc, const char **argv)
        while(action_argv[action_argc] != NULL)
                action_argc++;
 
-       if(action_argc < action->required_action_argc) {
+       /* Handle aliases */
+       if (!strcmp(aname, "create") && action_argc > 1) {
+               /* create command had historically switched arguments */
+               if (action_argv[0] && action_argv[1]) {
+                       const char *tmp = action_argv[0];
+                       action_argv[0] = action_argv[1];
+                       action_argv[1] = tmp;
+               }
+               aname = "open";
+       } else if (!strcmp(aname, "remove")) {
+               aname = "close";
+       }
+
+       for (action = action_types; action->type; action++)
+               if (strcmp(action->type, aname) == 0)
+                       break;
+
+       if (!action->type)
+               usage(popt_context, EXIT_FAILURE, _("Unknown action."),
+                     poptGetInvocationName(popt_context));
+
+       if (action_argc < action->required_action_argc) {
                char buf[128];
                snprintf(buf, 128,_("%s: requires %s as arguments"), action->type, action->arg_desc);
                usage(popt_context, EXIT_FAILURE, buf,
                      poptGetInvocationName(popt_context));
        }
 
-       if (data_block_size < 0 || hash_block_size < 0 || hash_type < 0) {
+       if (opt_data_block_size < 0 || opt_hash_block_size < 0 || opt_hash_type < 0) {
                usage(popt_context, EXIT_FAILURE,
                      _("Negative number for option not permitted."),
                      poptGetInvocationName(popt_context));
        }
 
+       if ((opt_ignore_corruption || opt_restart_on_corruption || opt_ignore_zero_blocks) && strcmp(aname, "open"))
+               usage(popt_context, EXIT_FAILURE,
+               _("Option --ignore-corruption, --restart-on-corruption or --ignore-zero-blocks is allowed only for open operation."),
+               poptGetInvocationName(popt_context));
+
+       if (opt_root_hash_signature && strcmp(aname, "open"))
+               usage(popt_context, EXIT_FAILURE,
+               _("Option --root-hash-signature can be used only for open operation."),
+               poptGetInvocationName(popt_context));
+
+       if (opt_ignore_corruption && opt_restart_on_corruption)
+               usage(popt_context, EXIT_FAILURE,
+               _("Option --ignore-corruption and --restart-on-corruption cannot be used together."),
+               poptGetInvocationName(popt_context));
+
+       if (opt_panic_on_corruption && opt_restart_on_corruption)
+               usage(popt_context, EXIT_FAILURE,
+               _("Option --panic-on-corruption and --restart-on-corruption cannot be used together."),
+               poptGetInvocationName(popt_context));
+
        if (opt_debug) {
                opt_verbose = 1;
                crypt_set_debug_level(-1);
-               _dbg_version_and_cmd(argc, argv);
+               dbg_version_and_cmd(argc, argv);
        }
 
        r = run_action(action);
+       tools_cleanup();
        poptFreeContext(popt_context);
        return r;
 }