[efi] Include installed protocol list in unknown handle names
[ipxe.git] / src / hci / commands / digest_cmd.c
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 */
47 static struct command_descriptor digest_cmd =
48 COMMAND_DESC ( struct digest_options, digest_opts, 1, MAX_ARGUMENTS,
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 };