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 
20 FILE_LICENCE ( GPL2_OR_LATER );
21 
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <getopt.h>
26 #include <ipxe/command.h>
27 #include <ipxe/parseopt.h>
28 #include <ipxe/image.h>
29 #include <ipxe/settings.h>
30 #include <ipxe/crypto.h>
31 #include <ipxe/md5.h>
32 #include <ipxe/sha1.h>
33 #include <usr/imgmgmt.h>
34 #include <hci/digest_cmd.h>
35 
36 /** @file
37  *
38  * Digest commands
39  *
40  */
41 
42 /** "digest" options */
44  /** Setting */
46 };
47 
48 /** "digest" option list */
49 static struct option_descriptor digest_opts[] = {
50  OPTION_DESC ( "set", 's', required_argument, struct digest_options,
52 };
53 
54 /** "digest" command descriptor */
57  "<image> [<image>...]" );
58 
59 /**
60  * The "digest" command
61  *
62  * @v argc Argument count
63  * @v argv Argument list
64  * @v digest Digest algorithm
65  * @ret rc Return status code
66  */
67 int digest_exec ( int argc, char **argv, struct digest_algorithm *digest ) {
68  struct digest_options opts;
69  struct image *image;
70  uint8_t ctx[digest->ctxsize];
71  uint8_t out[digest->digestsize];
72  unsigned int j;
73  int i;
74  int rc;
75 
76  /* Parse options */
77  if ( ( rc = parse_options ( argc, argv, &digest_cmd, &opts ) ) != 0 )
78  return rc;
79 
80  /* Use default setting type, if not specified */
81  if ( ! opts.setting.setting.type )
82  opts.setting.setting.type = &setting_type_hexraw;
83 
84  /* Calculate digests for each image */
85  for ( i = optind ; i < argc ; i++ ) {
86 
87  /* Acquire image */
88  if ( ( rc = imgacquire ( argv[i], 0, &image ) ) != 0 )
89  return rc;
90 
91  /* Calculate digest */
92  digest_init ( digest, ctx );
93  digest_update ( digest, ctx, image->data, image->len );
94  digest_final ( digest, ctx, out );
95 
96  /* Display or store digest as directed */
97  if ( opts.setting.settings ) {
98 
99  /* Store digest */
100  if ( ( rc = store_setting ( opts.setting.settings,
101  &opts.setting.setting, out,
102  sizeof ( out ) ) ) != 0 ) {
103  printf ( "Could not store \"%s\": %s\n",
104  opts.setting.setting.name,
105  strerror ( rc ) );
106  return rc;
107  }
108 
109  } else {
110 
111  /* Print digest */
112  for ( j = 0 ; j < sizeof ( out ) ; j++ )
113  printf ( "%02x", out[j] );
114  printf ( " %s\n", image->name );
115  }
116  }
117 
118  return 0;
119 }
120 
121 /* Include "md5sum" and "sha1sum" commands unconditionally */
122 
123 static int md5sum_exec ( int argc, char **argv ) {
124  return digest_exec ( argc, argv, &md5_algorithm );
125 }
126 
127 static int sha1sum_exec ( int argc, char **argv ) {
128  return digest_exec ( argc, argv, &sha1_algorithm );
129 }
130 
131 COMMAND ( md5sum, md5sum_exec );
132 COMMAND ( sha1sum, sha1sum_exec );
133 
134 /* Drag in commands for any other enabled algorithms */
136 REQUIRE_OBJECT ( config_digest_cmd );
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static void digest_update(struct digest_algorithm *digest, void *ctx, const void *data, size_t len)
Definition: crypto.h:201
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
A parsed named setting.
Definition: parseopt.h:122
static struct option_descriptor digest_opts[]
"digest" option list
Definition: digest_cmd.c:49
int optind
Current option index.
Definition: getopt.c:51
FILE_LICENCE(GPL2_OR_LATER)
const void * data
Read-only data.
Definition: image.h:50
static void digest_final(struct digest_algorithm *digest, void *ctx, void *out)
Definition: crypto.h:207
int parse_autovivified_setting(char *text, struct named_setting *setting)
Parse and autovivify setting name.
Definition: parseopt.c:336
int store_setting(struct settings *settings, const struct setting *setting, const void *data, size_t len)
Store value of setting.
Definition: settings.c:615
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:484
Cryptographic API.
REQUIRE_OBJECT(config_digest_cmd)
An executable image.
Definition: image.h:23
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
A command descriptor.
Definition: parseopt.h:77
static struct command_descriptor digest_cmd
"digest" command descriptor
Definition: digest_cmd.c:55
static int md5sum_exec(int argc, char **argv)
Definition: digest_cmd.c:123
__be32 out[4]
Definition: CIB_PRM.h:36
COMMAND(md5sum, md5sum_exec)
"digest" options
Definition: digest_cmd.c:43
int digest_exec(int argc, char **argv, struct digest_algorithm *digest)
The "digest" command.
Definition: digest_cmd.c:67
Parse command-line options.
Executable images.
REQUIRING_SYMBOL(digest_exec)
#define MAX_ARGUMENTS
No maximum number of arguments.
Definition: parseopt.h:97
Configuration settings.
static void digest_init(struct digest_algorithm *digest, void *ctx)
Definition: crypto.h:196
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
size_t len
Length of raw file image.
Definition: image.h:55
Command line option parsing.
unsigned char uint8_t
Definition: stdint.h:10
A setting.
Definition: settings.h:23
size_t ctxsize
Context size.
Definition: crypto.h:22
Image management.
size_t digestsize
Digest size.
Definition: crypto.h:26
SHA-1 algorithm.
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition: parseopt.h:67
Option requires an argument.
Definition: getopt.h:18
A command-line option descriptor.
Definition: parseopt.h:23
A message digest algorithm.
Definition: crypto.h:18
static int sha1sum_exec(int argc, char **argv)
Definition: digest_cmd.c:127
static union @447 opts
"cert<xxx>" option list
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:108
MD5 algorithm.
char * name
Name.
Definition: image.h:37
String functions.
int imgacquire(const char *name_uri, unsigned long timeout, struct image **image)
Acquire an image.
Definition: imgmgmt.c:142
struct digest_algorithm md5_algorithm
MD5 algorithm.
Definition: md5.c:286
struct digest_algorithm sha1_algorithm
SHA-1 algorithm.
Definition: sha1.c:257