2009-03-26 23:04:31 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 Daniel Verkamp <daniel@drv.nu>.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2012-07-20 20:55:45 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301, USA.
|
2009-03-26 23:04:31 +01:00
|
|
|
*/
|
|
|
|
|
2010-11-22 00:35:50 +01:00
|
|
|
FILE_LICENCE ( GPL2_OR_LATER );
|
|
|
|
|
2009-03-26 23:04:31 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2010-11-21 19:37:54 +01:00
|
|
|
#include <getopt.h>
|
2010-04-19 21:16:01 +02:00
|
|
|
#include <ipxe/command.h>
|
2010-11-21 19:37:54 +01:00
|
|
|
#include <ipxe/parseopt.h>
|
2010-04-19 21:16:01 +02:00
|
|
|
#include <ipxe/image.h>
|
|
|
|
#include <ipxe/crypto.h>
|
|
|
|
#include <ipxe/md5.h>
|
|
|
|
#include <ipxe/sha1.h>
|
2012-03-24 02:16:37 +01:00
|
|
|
#include <usr/imgmgmt.h>
|
2009-03-26 23:04:31 +01:00
|
|
|
|
2010-11-21 19:37:54 +01:00
|
|
|
/** @file
|
|
|
|
*
|
|
|
|
* Digest commands
|
2009-03-26 23:04:31 +01:00
|
|
|
*
|
|
|
|
*/
|
2010-11-21 19:37:54 +01:00
|
|
|
|
|
|
|
/** "digest" options */
|
|
|
|
struct digest_options {};
|
|
|
|
|
|
|
|
/** "digest" option list */
|
|
|
|
static struct option_descriptor digest_opts[] = {};
|
|
|
|
|
|
|
|
/** "digest" command descriptor */
|
|
|
|
static struct command_descriptor digest_cmd =
|
|
|
|
COMMAND_DESC ( struct digest_options, digest_opts, 1, MAX_ARGUMENTS,
|
2011-03-04 13:14:51 +01:00
|
|
|
"<image> [<image>...]" );
|
2009-03-26 23:04:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The "digest" command
|
|
|
|
*
|
|
|
|
* @v argc Argument count
|
|
|
|
* @v argv Argument list
|
|
|
|
* @v digest Digest algorithm
|
2010-11-21 19:37:54 +01:00
|
|
|
* @ret rc Return status code
|
2009-03-26 23:04:31 +01:00
|
|
|
*/
|
|
|
|
static int digest_exec ( int argc, char **argv,
|
|
|
|
struct digest_algorithm *digest ) {
|
2010-11-21 19:37:54 +01:00
|
|
|
struct digest_options opts;
|
2009-03-26 23:04:31 +01:00
|
|
|
struct image *image;
|
|
|
|
uint8_t digest_ctx[digest->ctxsize];
|
|
|
|
uint8_t digest_out[digest->digestsize];
|
|
|
|
uint8_t buf[128];
|
|
|
|
size_t offset;
|
|
|
|
size_t len;
|
|
|
|
size_t frag_len;
|
|
|
|
int i;
|
|
|
|
unsigned j;
|
2010-11-21 19:37:54 +01:00
|
|
|
int rc;
|
2009-03-26 23:04:31 +01:00
|
|
|
|
2010-11-21 19:37:54 +01:00
|
|
|
/* Parse options */
|
|
|
|
if ( ( rc = parse_options ( argc, argv, &digest_cmd, &opts ) ) != 0 )
|
|
|
|
return rc;
|
2009-03-26 23:04:31 +01:00
|
|
|
|
2010-11-21 19:37:54 +01:00
|
|
|
for ( i = optind ; i < argc ; i++ ) {
|
2009-03-26 23:04:31 +01:00
|
|
|
|
2012-03-24 02:16:37 +01:00
|
|
|
/* Acquire image */
|
|
|
|
if ( ( rc = imgacquire ( argv[i], &image ) ) != 0 )
|
2009-03-26 23:04:31 +01:00
|
|
|
continue;
|
|
|
|
offset = 0;
|
|
|
|
len = image->len;
|
|
|
|
|
|
|
|
/* calculate digest */
|
|
|
|
digest_init ( digest, digest_ctx );
|
|
|
|
while ( len ) {
|
|
|
|
frag_len = len;
|
|
|
|
if ( frag_len > sizeof ( buf ) )
|
|
|
|
frag_len = sizeof ( buf );
|
|
|
|
copy_from_user ( buf, image->data, offset, frag_len );
|
|
|
|
digest_update ( digest, digest_ctx, buf, frag_len );
|
|
|
|
len -= frag_len;
|
|
|
|
offset += frag_len;
|
|
|
|
}
|
|
|
|
digest_final ( digest, digest_ctx, digest_out );
|
|
|
|
|
|
|
|
for ( j = 0 ; j < sizeof ( digest_out ) ; j++ )
|
|
|
|
printf ( "%02x", digest_out[j] );
|
|
|
|
|
|
|
|
printf ( " %s\n", image->name );
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int md5sum_exec ( int argc, char **argv ) {
|
|
|
|
return digest_exec ( argc, argv, &md5_algorithm );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sha1sum_exec ( int argc, char **argv ) {
|
|
|
|
return digest_exec ( argc, argv, &sha1_algorithm );
|
|
|
|
}
|
|
|
|
|
|
|
|
struct command md5sum_command __command = {
|
|
|
|
.name = "md5sum",
|
|
|
|
.exec = md5sum_exec,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct command sha1sum_command __command = {
|
|
|
|
.name = "sha1sum",
|
|
|
|
.exec = sha1sum_exec,
|
|
|
|
};
|