13 changes: 11 additions & 2 deletions lib/options.c
Expand Up @@ -196,7 +196,7 @@ append_arg_to_vector (int* argc,
if (new_argv != NULL) {
new_argv[*argc - 1] = arg_string;
} else {
LOG_ERR("Failed to realloc new_argv to append string %s: %s\n",
LOG_ERR("Failed to realloc new_argv to append string %s: %s",
arg_string,
strerror (errno));
}
Expand Down Expand Up @@ -226,7 +226,7 @@ get_common_opts (int *argc_param,
char **argv = *argv_param;

int c = 0, option_index = 0;
char *arg_str = "-T:d:R:p:hvV";
char *arg_str = "-T:d:R:p:hvVQ";
struct option long_options [] = {
{
.name = "tcti",
Expand Down Expand Up @@ -271,6 +271,12 @@ get_common_opts (int *argc_param,
.flag = NULL,
.val = 'V',
},
{
.name = "quiet",
.has_arg = no_argument,
.flag = NULL,
.val = 'Q',
},
{
.name = "version",
.has_arg = no_argument,
Expand Down Expand Up @@ -351,6 +357,9 @@ get_common_opts (int *argc_param,
case 'V':
common_opts->verbose = true;
break;
case 'Q':
common_opts->quiet = true;
break;
case 'v':
common_opts->version = true;
break;
Expand Down
2 changes: 2 additions & 0 deletions lib/options.h
Expand Up @@ -76,6 +76,7 @@
.socket_port = TCTI_SOCKET_DEFAULT_PORT, \
.help = false, \
.verbose = false, \
.quiet = false, \
.version = false, \
}

Expand All @@ -100,6 +101,7 @@ typedef struct {
uint16_t socket_port;
int help;
int verbose;
int quiet;
int version;
} common_opts_t;

Expand Down
12 changes: 6 additions & 6 deletions lib/tpm2_policy.c
Expand Up @@ -56,7 +56,7 @@ static bool evaluate_populate_pcr_digests(TPML_PCR_SELECTION pcr_selections,
unsigned long filesize = 0;
bool result = files_get_file_size(raw_pcrs_file, &filesize);
if (!result) {
LOG_ERR("Could not retrieve raw_pcrs_file size\n");
LOG_ERR("Could not retrieve raw_pcrs_file size");
return false;
}
if (filesize != expected_pcr_input_file_size) {
Expand Down Expand Up @@ -154,7 +154,7 @@ static TPM_RC start_policy_session (TSS2_SYS_CONTEXT *sapi_context,
&symmetric,
policy_digest_hash_alg);
if (rval != TPM_RC_SUCCESS) {
LOG_ERR("Failed tpm session start auth with params\n");
LOG_ERR("Failed tpm session start auth with params");
}
return rval;
}
Expand All @@ -176,22 +176,22 @@ TPM_RC tpm2_policy_build(TSS2_SYS_CONTEXT *sapi_context,
policy_session_type,
policy_digest_hash_alg);
if (rval != TPM_RC_SUCCESS) {
LOG_ERR("Error starting the policy session.\n");
LOG_ERR("Error starting the policy session.");
return rval;
}
// Issue policy command.
rval = (*build_policy_function)(sapi_context, *policy_session,
pcr_selections, raw_pcrs_file);
if (rval != TPM_RC_SUCCESS) {
LOG_ERR("Failed parse_policy_type_and_send_command\n");
LOG_ERR("Failed parse_policy_type_and_send_command");
return rval;
}
// Get Policy Hash
rval = Tss2_Sys_PolicyGetDigest(sapi_context,
(*policy_session)->sessionHandle,
0, policy_digest, 0);
if (rval != TPM_RC_SUCCESS) {
LOG_ERR("Failed Policy Get Digest\n");
LOG_ERR("Failed Policy Get Digest");
return rval;
}

Expand All @@ -200,7 +200,7 @@ TPM_RC tpm2_policy_build(TSS2_SYS_CONTEXT *sapi_context,
rval = Tss2_Sys_FlushContext(sapi_context,
(*policy_session)->sessionHandle);
if (rval != TPM_RC_SUCCESS) {
LOG_ERR("Failed Flush Context\n");
LOG_ERR("Failed Flush Context");
return rval;
}

Expand Down
7 changes: 6 additions & 1 deletion man/common-options.troff
Expand Up @@ -11,4 +11,9 @@ Display version information for this tool.
.TP
\fB\-V,\ \-\-verbose\fR
Increase the information that the tool prints to the console during its
execution.
execution. When using this option the file and line number are printed.
.TP
.TP
\fB\-Q,\ \-\-quiet\fR
Decrease the information that the tool prints to the console during its
execution. With this option the tools output messages are not printed.
8 changes: 7 additions & 1 deletion tools/main.c
Expand Up @@ -35,6 +35,8 @@
#include "main.h"
#include "options.h"

bool output_enabled = true;

/*
* This program is a template for TPM2 tools that use the SAPI. It does
* nothing more than parsing command line options that allow the caller to
Expand All @@ -59,7 +61,7 @@ main (int argc,
switch (sanity_check_common (&opts)) {
case 1:
execute_man (argv[0], envp);
LOG_ERR ("failed to load manpage, check your environment / PATH\n");
LOG_ERR ("failed to load manpage, check your environment / PATH");
exit (1);
case 2:
exit (1);
Expand All @@ -72,6 +74,10 @@ main (int argc,
if (opts.verbose)
log_set_level(log_level_verbose);

if (opts.quiet) {
disable_output();
}

sapi_context = sapi_init_from_options (&opts);
if (sapi_context == NULL)
exit (1);
Expand Down
24 changes: 24 additions & 0 deletions tools/main.h
Expand Up @@ -32,13 +32,37 @@
#define MAIN_H

#include <sapi/tpm20.h>
#include <stdbool.h>
#include "options.h"

extern bool output_enabled;

int
execute_tool (int argc,
char *argv[],
char *envp[],
common_opts_t *opts,
TSS2_SYS_CONTEXT *sapi_context);

/*
* Prints output messages if not disabled. Output messages are what tools prints
* to the standard output during normal execution.
*/
#define TOOL_OUTPUT(fmt, ...) \
do { \
if (output_enabled) { \
printf(fmt, ##__VA_ARGS__); \
} \
} while (0)

static inline void enable_output(void)
{
output_enabled = true;
}

static inline void disable_output(void)
{
output_enabled = false;
}

#endif /* MAIN_H */
8 changes: 4 additions & 4 deletions tools/tpm2_activatecredential.c
Expand Up @@ -290,13 +290,13 @@ static bool init(int argc, char *argv[], tpm_activatecred_ctx *ctx) {
o_flag = 1;
break;
case ':':
LOG_ERR("Argument %c needs a value!\n", optopt);
LOG_ERR("Argument %c needs a value!", optopt);
return false;
case '?':
LOG_ERR("Unknown Argument: %c\n", optopt);
LOG_ERR("Unknown Argument: %c", optopt);
return false;
default:
LOG_ERR("?? getopt returned character code 0%o ??\n", opt);
LOG_ERR("?? getopt returned character code 0%o ??", opt);
return false;
}
};
Expand Down Expand Up @@ -327,7 +327,7 @@ int execute_tool(int argc, char *argv[], char *envp[], common_opts_t *opts,

bool result = init(argc, argv, &ctx);
if (!result) {
LOG_ERR("Initialization failed\n");
LOG_ERR("Initialization failed");
return 1;
}

Expand Down
6 changes: 3 additions & 3 deletions tools/tpm2_akparse.c
Expand Up @@ -201,13 +201,13 @@ static bool init(int argc, char *argv[], tpm_akparse_ctx *ctx) {
ctx->ak_key_file_path = optarg;
break;
case ':':
LOG_ERR("Argument %c needs a value!\n", optopt);
LOG_ERR("Argument %c needs a value!", optopt);
return false;
case '?':
LOG_ERR("Unknown Argument: %c\n", optopt);
LOG_ERR("Unknown Argument: %c", optopt);
return false;
default:
LOG_ERR("?? getopt returned character code 0%o ??\n", opt);
LOG_ERR("?? getopt returned character code 0%o ??", opt);
return false;
}
}
Expand Down
8 changes: 4 additions & 4 deletions tools/tpm2_certify.c
Expand Up @@ -156,7 +156,7 @@ static bool certify_and_save_data(tpm_certify_ctx *ctx) {
TPMT_SIG_SCHEME scheme;
bool result = set_scheme(ctx->sapi_context, ctx->handle.key, ctx->halg, &scheme);
if (!result) {
LOG_ERR("No suitable signing scheme!\n");
LOG_ERR("No suitable signing scheme!");
return false;
}

Expand Down Expand Up @@ -310,15 +310,15 @@ static bool init(int argc, char *argv[], tpm_certify_ctx *ctx) {
flags.C = 1;
break;
case ':':
LOG_ERR("Argument %c needs a value!\n", optopt);
LOG_ERR("Argument %c needs a value!", optopt);
result = false;
return false;
case '?':
LOG_ERR("Unknown Argument: %c\n", optopt);
LOG_ERR("Unknown Argument: %c", optopt);
result = false;
return false;
default:
LOG_ERR("?? getopt returned character code 0%o ??\n", opt);
LOG_ERR("?? getopt returned character code 0%o ??", opt);
result = false;
return false;
}
Expand Down
26 changes: 13 additions & 13 deletions tools/tpm2_create.c
Expand Up @@ -70,7 +70,7 @@ int setAlg(TPMI_ALG_PUBLIC type,TPMI_ALG_HASH nameAlg,TPM2B_PUBLIC *inPublic, in
inPublic->t.publicArea.nameAlg = nameAlg;
break;
default:
printf("nameAlg algrithm: 0x%0x not support !\n", nameAlg);
LOG_ERR("nameAlg algrithm: 0x%0x not support !", nameAlg);
return -1;
}

Expand Down Expand Up @@ -132,7 +132,7 @@ int setAlg(TPMI_ALG_PUBLIC type,TPMI_ALG_HASH nameAlg,TPM2B_PUBLIC *inPublic, in
break;

default:
printf("type algrithm: 0x%0x not support !\n",type);
LOG_ERR("type algrithm: 0x%0x not support !",type);
return -2;
}
return 0;
Expand Down Expand Up @@ -174,7 +174,7 @@ int create(TPMI_DH_OBJECT parentHandle, TPM2B_PUBLIC *inPublic, TPM2B_SENSITIVE_

if(A_flag == 1)
inPublic->t.publicArea.objectAttributes.val = objectAttributes;
printf("ObjectAttribute: 0x%08X\n",inPublic->t.publicArea.objectAttributes.val);
TOOL_OUTPUT("ObjectAttribute: 0x%08X\n",inPublic->t.publicArea.objectAttributes.val);

creationPCR.count = 0;

Expand All @@ -183,10 +183,10 @@ int create(TPMI_DH_OBJECT parentHandle, TPM2B_PUBLIC *inPublic, TPM2B_SENSITIVE_
&creationTicket, &sessionsDataOut);
if(rval != TPM_RC_SUCCESS)
{
printf("\nCreate Object Failed ! ErrorCode: 0x%0x\n\n",rval);
LOG_ERR("\nCreate Object Failed ! ErrorCode: 0x%0x\n",rval);
return -2;
}
printf("\nCreate Object Succeed !\n");
TOOL_OUTPUT("\nCreate Object Succeed !\n");

/*
* TODO These public and private serializations are not safe since its outputting size as well.
Expand Down Expand Up @@ -302,7 +302,7 @@ execute_tool (int argc,
showArgError(optarg, argv[0]);
return 1;
}
printf("nameAlg = 0x%4.4x\n", nameAlg);
TOOL_OUTPUT("nameAlg = 0x%4.4x\n", nameAlg);
g_flag = 1;
break;
case 'G':
Expand All @@ -312,7 +312,7 @@ execute_tool (int argc,
showArgError(optarg, argv[0]);
return 1;
}
printf("type = 0x%4.4x\n", type);
TOOL_OUTPUT("type = 0x%4.4x\n", type);
G_flag = 1;
break;
case 'A':
Expand All @@ -335,7 +335,7 @@ execute_tool (int argc,
return 1;
}
I_flag = 1;
printf("inSensitive.t.sensitive.data.t.size = %d\n",inSensitive.t.sensitive.data.t.size);
TOOL_OUTPUT("inSensitive.t.sensitive.data.t.size = %d\n",inSensitive.t.sensitive.data.t.size);
break;
case 'L':
inPublic.t.publicArea.authPolicy.t.size = sizeof(inPublic.t.publicArea.authPolicy) - 2;
Expand Down Expand Up @@ -377,17 +377,17 @@ execute_tool (int argc,
{
return 1;
}
printf("contextParentFile = %s\n", contextParentFilePath);
TOOL_OUTPUT("contextParentFile = %s\n", contextParentFilePath);
c_flag = 1;
break;
case ':':
LOG_ERR("Argument %c needs a value!\n", optopt);
LOG_ERR("Argument %c needs a value!", optopt);
return 1;
case '?':
LOG_ERR("Unknown Argument: %c\n", optopt);
LOG_ERR("Unknown Argument: %c", optopt);
return 1;
default:
LOG_ERR("?? getopt returned character code 0%o ??\n", opt);
LOG_ERR("?? getopt returned character code 0%o ??", opt);
return 1;
}
};
Expand All @@ -398,7 +398,7 @@ execute_tool (int argc,
if(I_flag == 0) {
inSensitive.t.sensitive.data.t.size = 0;
} else if (type != TPM_ALG_KEYEDHASH) {
LOG_ERR("Only TPM_ALG_KEYEDHASH algorithm is allowed when sealing data\n");
LOG_ERR("Only TPM_ALG_KEYEDHASH algorithm is allowed when sealing data");
return 1;
}

Expand Down