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., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <gpxe/command.h>
23 #include <gpxe/image.h>
24 #include <gpxe/crypto.h>
27 #include <gpxe/sha1.h>
30 * "digest" command syntax message
32 * @v argv Argument list
34 static void digest_syntax ( char **argv
) {
38 "Calculate the %s of an image\n",
43 * The "digest" command
45 * @v argc Argument count
46 * @v argv Argument list
47 * @v digest Digest algorithm
50 static int digest_exec ( int argc
, char **argv
,
51 struct digest_algorithm
*digest
) {
52 const char *image_name
;
54 uint8_t digest_ctx
[digest
->ctxsize
];
55 uint8_t digest_out
[digest
->digestsize
];
64 !strcmp ( argv
[1], "--help" ) ||
65 !strcmp ( argv
[1], "-h" ) ) {
66 digest_syntax ( argv
);
70 for ( i
= 1 ; i
< argc
; i
++ ) {
74 image
= find_image ( image_name
);
76 printf ( "No such image: %s\n", image_name
);
82 /* calculate digest */
83 digest_init ( digest
, digest_ctx
);
86 if ( frag_len
> sizeof ( buf
) )
87 frag_len
= sizeof ( buf
);
88 copy_from_user ( buf
, image
->data
, offset
, frag_len
);
89 digest_update ( digest
, digest_ctx
, buf
, frag_len
);
93 digest_final ( digest
, digest_ctx
, digest_out
);
95 for ( j
= 0 ; j
< sizeof ( digest_out
) ; j
++ )
96 printf ( "%02x", digest_out
[j
] );
98 printf ( " %s\n", image
->name
);
104 static int md5sum_exec ( int argc
, char **argv
) {
105 return digest_exec ( argc
, argv
, &md5_algorithm
);
108 static int sha1sum_exec ( int argc
, char **argv
) {
109 return digest_exec ( argc
, argv
, &sha1_algorithm
);
112 struct command md5sum_command __command
= {
117 struct command sha1sum_command __command
= {
119 .exec
= sha1sum_exec
,