2 * Copyright (C) 2009 Daniel Verkamp <daniel@drv.nu>.
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.
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.
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
20 FILE_LICENCE ( GPL2_OR_LATER );
26 #include <ipxe/command.h>
27 #include <ipxe/parseopt.h>
28 #include <ipxe/image.h>
29 #include <ipxe/crypto.h>
31 #include <ipxe/sha1.h>
32 #include <usr/imgmgmt.h>
40 /** "digest" options */
41 struct digest_options {};
43 /** "digest" option list */
44 static struct option_descriptor digest_opts[] = {};
46 /** "digest" command descriptor */
47 static struct command_descriptor digest_cmd =
48 COMMAND_DESC ( struct digest_options, digest_opts, 1, MAX_ARGUMENTS,
49 "<image> [<image>...]" );
52 * The "digest" command
54 * @v argc Argument count
55 * @v argv Argument list
56 * @v digest Digest algorithm
57 * @ret rc Return status code
59 static int digest_exec ( int argc, char **argv,
60 struct digest_algorithm *digest ) {
61 struct digest_options opts;
63 uint8_t digest_ctx[digest->ctxsize];
64 uint8_t digest_out[digest->digestsize];
74 if ( ( rc = parse_options ( argc, argv, &digest_cmd, &opts ) ) != 0 )
77 for ( i = optind ; i < argc ; i++ ) {
80 if ( ( rc = imgacquire ( argv[i], 0, &image ) ) != 0 )
85 /* calculate digest */
86 digest_init ( digest, digest_ctx );
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 );
96 digest_final ( digest, digest_ctx, digest_out );
98 for ( j = 0 ; j < sizeof ( digest_out ) ; j++ )
99 printf ( "%02x", digest_out[j] );
101 printf ( " %s\n", image->name );
107 static int md5sum_exec ( int argc, char **argv ) {
108 return digest_exec ( argc, argv, &md5_algorithm );
111 static int sha1sum_exec ( int argc, char **argv ) {
112 return digest_exec ( argc, argv, &sha1_algorithm );
115 struct command md5sum_command __command = {
120 struct command sha1sum_command __command = {
122 .exec = sha1sum_exec,