1 /* 2 * Copyright (C) 2009 Daniel Verkamp <daniel (at) 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., 675 Mass Ave, Cambridge, MA 02139, USA. 17 */ 18 19 #include <stdio.h> 20 #include <string.h> 21 #include <unistd.h> 22 #include <gpxe/command.h> 23 #include <gpxe/image.h> 24 #include <gpxe/crypto.h> 25 26 #include <gpxe/md5.h> 27 #include <gpxe/sha1.h> 28 29 /** 30 * "digest" command syntax message 31 * 32 * @v argv Argument list 33 */ 34 static void digest_syntax ( char **argv ) { 35 printf ( "Usage:\n" 36 " %s <image name>\n" 37 "\n" 38 "Calculate the %s of an image\n", 39 argv[0], argv[0] ); 40 } 41 42 /** 43 * The "digest" command 44 * 45 * @v argc Argument count 46 * @v argv Argument list 47 * @v digest Digest algorithm 48 * @ret rc Exit code 49 */ 50 static int digest_exec ( int argc, char **argv, 51 struct digest_algorithm *digest ) { 52 const char *image_name; 53 struct image *image; 54 uint8_t digest_ctx[digest->ctxsize]; 55 uint8_t digest_out[digest->digestsize]; 56 uint8_t buf[128]; 57 size_t offset; 58 size_t len; 59 size_t frag_len; 60 int i; 61 unsigned j; 62 63 if ( argc < 2 || 64 !strcmp ( argv[1], "--help" ) || 65 !strcmp ( argv[1], "-h" ) ) { 66 digest_syntax ( argv ); 67 return 1; 68 } 69 70 for ( i = 1 ; i < argc ; i++ ) { 71 image_name = argv[i]; 72 73 /* find image */ 74 image = find_image ( image_name ); 75 if ( ! image ) { 76 printf ( "No such image: %s\n", image_name ); 77 continue; 78 } 79 offset = 0; 80 len = image->len; 81 82 /* calculate digest */ 83 digest_init ( digest, digest_ctx ); 84 while ( len ) { 85 frag_len = len; 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 ); 90 len -= frag_len; 91 offset += frag_len; 92 } 93 digest_final ( digest, digest_ctx, digest_out ); 94 95 for ( j = 0 ; j < sizeof ( digest_out ) ; j++ ) 96 printf ( "%02x", digest_out[j] ); 97 98 printf ( " %s\n", image->name ); 99 } 100 101 return 0; 102 } 103 104 static int md5sum_exec ( int argc, char **argv ) { 105 return digest_exec ( argc, argv, &md5_algorithm ); 106 } 107 108 static int sha1sum_exec ( int argc, char **argv ) { 109 return digest_exec ( argc, argv, &sha1_algorithm ); 110 } 111 112 struct command md5sum_command __command = { 113 .name = "md5sum", 114 .exec = md5sum_exec, 115 }; 116 117 struct command sha1sum_command __command = { 118 .name = "sha1sum", 119 .exec = sha1sum_exec, 120 }; 121