iPXE
digest_cmd.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009 Daniel Verkamp <daniel@drv.nu>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  */
00019 
00020 FILE_LICENCE ( GPL2_OR_LATER );
00021 
00022 #include <stdio.h>
00023 #include <string.h>
00024 #include <unistd.h>
00025 #include <getopt.h>
00026 #include <ipxe/command.h>
00027 #include <ipxe/parseopt.h>
00028 #include <ipxe/image.h>
00029 #include <ipxe/crypto.h>
00030 #include <ipxe/md5.h>
00031 #include <ipxe/sha1.h>
00032 #include <usr/imgmgmt.h>
00033 
00034 /** @file
00035  *
00036  * Digest commands
00037  *
00038  */
00039 
00040 /** "digest" options */
00041 struct digest_options {};
00042 
00043 /** "digest" option list */
00044 static struct option_descriptor digest_opts[] = {};
00045 
00046 /** "digest" command descriptor */
00047 static struct command_descriptor digest_cmd =
00048         COMMAND_DESC ( struct digest_options, digest_opts, 1, MAX_ARGUMENTS,
00049                        "<image> [<image>...]" );
00050 
00051 /**
00052  * The "digest" command
00053  *
00054  * @v argc              Argument count
00055  * @v argv              Argument list
00056  * @v digest            Digest algorithm
00057  * @ret rc              Return status code
00058  */
00059 static int digest_exec ( int argc, char **argv,
00060                          struct digest_algorithm *digest ) {
00061         struct digest_options opts;
00062         struct image *image;
00063         uint8_t digest_ctx[digest->ctxsize];
00064         uint8_t digest_out[digest->digestsize];
00065         uint8_t buf[128];
00066         size_t offset;
00067         size_t len;
00068         size_t frag_len;
00069         int i;
00070         unsigned j;
00071         int rc;
00072 
00073         /* Parse options */
00074         if ( ( rc = parse_options ( argc, argv, &digest_cmd, &opts ) ) != 0 )
00075                 return rc;
00076 
00077         for ( i = optind ; i < argc ; i++ ) {
00078 
00079                 /* Acquire image */
00080                 if ( ( rc = imgacquire ( argv[i], 0, &image ) ) != 0 )
00081                         continue;
00082                 offset = 0;
00083                 len = image->len;
00084 
00085                 /* calculate digest */
00086                 digest_init ( digest, digest_ctx );
00087                 while ( len ) {
00088                         frag_len = len;
00089                         if ( frag_len > sizeof ( buf ) )
00090                                 frag_len = sizeof ( buf );
00091                         copy_from_user ( buf, image->data, offset, frag_len );
00092                         digest_update ( digest, digest_ctx, buf, frag_len );
00093                         len -= frag_len;
00094                         offset += frag_len;
00095                 }
00096                 digest_final ( digest, digest_ctx, digest_out );
00097 
00098                 for ( j = 0 ; j < sizeof ( digest_out ) ; j++ )
00099                         printf ( "%02x", digest_out[j] );
00100 
00101                 printf ( "  %s\n", image->name );
00102         }
00103 
00104         return 0;
00105 }
00106 
00107 static int md5sum_exec ( int argc, char **argv ) {
00108         return digest_exec ( argc, argv, &md5_algorithm );
00109 }
00110 
00111 static int sha1sum_exec ( int argc, char **argv ) {
00112         return digest_exec ( argc, argv, &sha1_algorithm );
00113 }
00114 
00115 struct command md5sum_command __command = {
00116         .name = "md5sum",
00117         .exec = md5sum_exec,
00118 };
00119 
00120 struct command sha1sum_command __command = {
00121         .name = "sha1sum",
00122         .exec = sha1sum_exec,
00123 };