iPXE
digest_cmd.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2009 Daniel Verkamp <daniel@drv.nu>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 */
19
20FILE_LICENCE ( GPL2_OR_LATER );
21FILE_SECBOOT ( PERMITTED );
22
23#include <stdio.h>
24#include <string.h>
25#include <unistd.h>
26#include <getopt.h>
27#include <ipxe/command.h>
28#include <ipxe/parseopt.h>
29#include <ipxe/image.h>
30#include <ipxe/settings.h>
31#include <ipxe/crypto.h>
32#include <ipxe/md5.h>
33#include <ipxe/sha1.h>
34#include <usr/imgmgmt.h>
35#include <hci/digest_cmd.h>
36
37/** @file
38 *
39 * Digest commands
40 *
41 */
42
43/** "digest" options */
45 /** Setting */
47};
48
49/** "digest" option list */
54
55/** "digest" command descriptor */
58 "<image> [<image>...]" );
59
60/**
61 * The "digest" command
62 *
63 * @v argc Argument count
64 * @v argv Argument list
65 * @v digest Digest algorithm
66 * @ret rc Return status code
67 */
68int digest_exec ( int argc, char **argv, struct digest_algorithm *digest ) {
69 struct digest_options opts;
70 struct image *image;
71 uint8_t ctx[digest->ctxsize];
72 uint8_t out[digest->digestsize];
73 unsigned int j;
74 int i;
75 int rc;
76
77 /* Parse options */
78 if ( ( rc = parse_options ( argc, argv, &digest_cmd, &opts ) ) != 0 )
79 return rc;
80
81 /* Use default setting type, if not specified */
82 if ( ! opts.setting.setting.type )
83 opts.setting.setting.type = &setting_type_hexraw;
84
85 /* Calculate digests for each image */
86 for ( i = optind ; i < argc ; i++ ) {
87
88 /* Acquire image */
89 if ( ( rc = imgacquire ( argv[i], 0, &image ) ) != 0 )
90 return rc;
91
92 /* Calculate digest */
93 digest_init ( digest, ctx );
94 digest_update ( digest, ctx, image->data, image->len );
95 digest_final ( digest, ctx, out );
96
97 /* Display or store digest as directed */
98 if ( opts.setting.settings ) {
99
100 /* Store digest */
101 if ( ( rc = store_setting ( opts.setting.settings,
102 &opts.setting.setting, out,
103 sizeof ( out ) ) ) != 0 ) {
104 printf ( "Could not store \"%s\": %s\n",
105 opts.setting.setting.name,
106 strerror ( rc ) );
107 return rc;
108 }
109
110 } else {
111
112 /* Print digest */
113 for ( j = 0 ; j < sizeof ( out ) ; j++ )
114 printf ( "%02x", out[j] );
115 printf ( " %s\n", image->name );
116 }
117 }
118
119 return 0;
120}
121
122/* Include "md5sum" and "sha1sum" commands unconditionally */
123
124static int md5sum_exec ( int argc, char **argv ) {
125 return digest_exec ( argc, argv, &md5_algorithm );
126}
127
128static int sha1sum_exec ( int argc, char **argv ) {
129 return digest_exec ( argc, argv, &sha1_algorithm );
130}
131
134
135/* Drag in commands for any other enabled algorithms */
137REQUIRE_OBJECT ( config_digest_cmd );
struct golan_eq_context ctx
Definition CIB_PRM.h:0
__be32 out[4]
Definition CIB_PRM.h:8
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
unsigned char uint8_t
Definition stdint.h:10
static union @024010030001061367220137227263210031030210157031 opts
"cert<xxx>" option list
#define COMMAND(name, exec)
Definition command.h:27
static struct option_descriptor digest_opts[]
"digest" option list
Definition digest_cmd.c:50
static int sha1sum_exec(int argc, char **argv)
Definition digest_cmd.c:128
static int md5sum_exec(int argc, char **argv)
Definition digest_cmd.c:124
static struct command_descriptor digest_cmd
"digest" command descriptor
Definition digest_cmd.c:56
int digest_exec(int argc, char **argv, struct digest_algorithm *digest)
The "digest" command.
Definition digest_cmd.c:68
int optind
Current option index.
Definition getopt.c:52
Parse command-line options.
@ required_argument
Option requires an argument.
Definition getopt.h:19
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define REQUIRE_OBJECT(object)
Require an object.
Definition compiler.h:202
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
#define REQUIRING_SYMBOL(symbol)
Specify the file's requiring symbol.
Definition compiler.h:140
Executable images.
int imgacquire(const char *name_uri, unsigned long timeout, struct image **image)
Acquire an image.
Definition imgmgmt.c:143
Image management.
Cryptographic API.
static void digest_init(struct digest_algorithm *digest, void *ctx)
Definition crypto.h:219
static void digest_final(struct digest_algorithm *digest, void *ctx, void *out)
Definition crypto.h:230
static void digest_update(struct digest_algorithm *digest, void *ctx, const void *data, size_t len)
Definition crypto.h:224
Configuration settings.
String functions.
struct digest_algorithm md5_algorithm
MD5 algorithm.
Definition md5.c:287
MD5 algorithm.
int parse_autovivified_setting(char *text, struct named_setting *setting)
Parse and autovivify setting name.
Definition parseopt.c:337
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition parseopt.c:485
Command line option parsing.
#define MAX_ARGUMENTS
No maximum number of arguments.
Definition parseopt.h:98
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition parseopt.h:109
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition parseopt.h:68
int store_setting(struct settings *settings, const struct setting *setting, const void *data, size_t len)
Store value of setting.
Definition settings.c:616
struct digest_algorithm sha1_algorithm
SHA-1 algorithm.
Definition sha1.c:258
SHA-1 algorithm.
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
A command descriptor.
Definition parseopt.h:78
A message digest algorithm.
Definition crypto.h:19
size_t digestsize
Digest size.
Definition crypto.h:27
size_t ctxsize
Context size.
Definition crypto.h:23
"digest" options
Definition digest_cmd.c:44
struct named_setting setting
Setting.
Definition digest_cmd.c:46
An executable image.
Definition image.h:24
const void * data
Read-only data.
Definition image.h:51
char * name
Name.
Definition image.h:38
size_t len
Length of raw file image.
Definition image.h:56
A parsed named setting.
Definition parseopt.h:123
A command-line option descriptor.
Definition parseopt.h:24
A setting.
Definition settings.h:24
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition vsprintf.c:465