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/crypto.h>
30 #include <ipxe/md5.h>
31 #include <ipxe/sha1.h>
32 #include <usr/imgmgmt.h>
33 
34 /** @file
35  *
36  * Digest commands
37  *
38  */
39 
40 /** "digest" options */
41 struct digest_options {};
42 
43 /** "digest" option list */
44 static struct option_descriptor digest_opts[] = {};
45 
46 /** "digest" command descriptor */
49  "<image> [<image>...]" );
50 
51 /**
52  * The "digest" command
53  *
54  * @v argc Argument count
55  * @v argv Argument list
56  * @v digest Digest algorithm
57  * @ret rc Return status code
58  */
59 static int digest_exec ( int argc, char **argv,
60  struct digest_algorithm *digest ) {
61  struct digest_options opts;
62  struct image *image;
63  uint8_t digest_ctx[digest->ctxsize];
64  uint8_t digest_out[digest->digestsize];
65  uint8_t buf[128];
66  size_t offset;
67  size_t len;
68  size_t frag_len;
69  int i;
70  unsigned j;
71  int rc;
72 
73  /* Parse options */
74  if ( ( rc = parse_options ( argc, argv, &digest_cmd, &opts ) ) != 0 )
75  return rc;
76 
77  for ( i = optind ; i < argc ; i++ ) {
78 
79  /* Acquire image */
80  if ( ( rc = imgacquire ( argv[i], 0, &image ) ) != 0 )
81  continue;
82  offset = 0;
83  len = image->len;
84 
85  /* calculate digest */
86  digest_init ( digest, digest_ctx );
87  while ( len ) {
88  frag_len = len;
89  if ( frag_len > sizeof ( buf ) )
90  frag_len = sizeof ( buf );
91  copy_from_user ( buf, image->data, offset, frag_len );
92  digest_update ( digest, digest_ctx, buf, frag_len );
93  len -= frag_len;
94  offset += frag_len;
95  }
96  digest_final ( digest, digest_ctx, digest_out );
97 
98  for ( j = 0 ; j < sizeof ( digest_out ) ; j++ )
99  printf ( "%02x", digest_out[j] );
100 
101  printf ( " %s\n", image->name );
102  }
103 
104  return 0;
105 }
106 
107 static int md5sum_exec ( int argc, char **argv ) {
108  return digest_exec ( argc, argv, &md5_algorithm );
109 }
110 
111 static int sha1sum_exec ( int argc, char **argv ) {
112  return digest_exec ( argc, argv, &sha1_algorithm );
113 }
114 
115 struct command md5sum_command __command = {
116  .name = "md5sum",
117  .exec = md5sum_exec,
118 };
119 
120 struct command sha1sum_command __command = {
121  .name = "sha1sum",
122  .exec = sha1sum_exec,
123 };
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
userptr_t data
Raw file image.
Definition: image.h:41
static struct option_descriptor digest_opts[]
"digest" option list
Definition: digest_cmd.c:44
int optind
Current option index.
Definition: getopt.c:51
FILE_LICENCE(GPL2_OR_LATER)
A command-line command.
Definition: command.h:9
static __always_inline void copy_from_user(void *dest, userptr_t src, off_t src_off, size_t len)
Copy data from user buffer.
Definition: uaccess.h:337
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:484
Cryptographic API.
An executable image.
Definition: image.h:24
A command descriptor.
Definition: parseopt.h:77
struct command md5sum_command __command
Definition: digest_cmd.c:115
static struct command_descriptor digest_cmd
"digest" command descriptor
Definition: digest_cmd.c:47
static int md5sum_exec(int argc, char **argv)
Definition: digest_cmd.c:107
"digest" options
Definition: digest_cmd.c:41
Parse command-line options.
Executable images.
static void struct digest_algorithm * digest
HMAC-MD5 digest.
Definition: crypto.h:308
#define MAX_ARGUMENTS
No maximum number of arguments.
Definition: parseopt.h:97
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
size_t len
Length of raw file image.
Definition: image.h:43
Command line option parsing.
unsigned char uint8_t
Definition: stdint.h:10
static union @437 opts
"cert<xxx>" option list
const char * name
Name of the command.
Definition: command.h:11
uint32_t len
Length.
Definition: ena.h:14
static int digest_exec(int argc, char **argv, struct digest_algorithm *digest)
The "digest" command.
Definition: digest_cmd.c:59
size_t ctxsize
Context size.
Definition: crypto.h:21
Image management.
size_t digestsize
Digest size.
Definition: crypto.h:25
SHA-1 algorithm.
A command-line option descriptor.
Definition: parseopt.h:23
A message digest algorithm.
Definition: crypto.h:17
static int sha1sum_exec(int argc, char **argv)
Definition: digest_cmd.c:111
#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:34
String functions.
int imgacquire(const char *name_uri, unsigned long timeout, struct image **image)
Acquire an image.
Definition: imgmgmt.c:141
struct digest_algorithm md5_algorithm
MD5 algorithm.
Definition: md5.c:286
struct digest_algorithm sha1_algorithm
SHA-1 algorithm.
Definition: sha1.c:257