You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

digest_cmd.c 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <getopt.h>
  23. #include <ipxe/command.h>
  24. #include <ipxe/parseopt.h>
  25. #include <ipxe/image.h>
  26. #include <ipxe/crypto.h>
  27. #include <ipxe/md5.h>
  28. #include <ipxe/sha1.h>
  29. #include <usr/imgmgmt.h>
  30. /** @file
  31. *
  32. * Digest commands
  33. *
  34. */
  35. /** "digest" options */
  36. struct digest_options {};
  37. /** "digest" option list */
  38. static struct option_descriptor digest_opts[] = {};
  39. /** "digest" command descriptor */
  40. static struct command_descriptor digest_cmd =
  41. COMMAND_DESC ( struct digest_options, digest_opts, 1, MAX_ARGUMENTS,
  42. "<image> [<image>...]" );
  43. /**
  44. * The "digest" command
  45. *
  46. * @v argc Argument count
  47. * @v argv Argument list
  48. * @v digest Digest algorithm
  49. * @ret rc Return status code
  50. */
  51. static int digest_exec ( int argc, char **argv,
  52. struct digest_algorithm *digest ) {
  53. struct digest_options opts;
  54. struct image *image;
  55. uint8_t digest_ctx[digest->ctxsize];
  56. uint8_t digest_out[digest->digestsize];
  57. uint8_t buf[128];
  58. size_t offset;
  59. size_t len;
  60. size_t frag_len;
  61. int i;
  62. unsigned j;
  63. int rc;
  64. /* Parse options */
  65. if ( ( rc = parse_options ( argc, argv, &digest_cmd, &opts ) ) != 0 )
  66. return rc;
  67. for ( i = optind ; i < argc ; i++ ) {
  68. /* Acquire image */
  69. if ( ( rc = imgacquire ( argv[i], &image ) ) != 0 )
  70. continue;
  71. offset = 0;
  72. len = image->len;
  73. /* calculate digest */
  74. digest_init ( digest, digest_ctx );
  75. while ( len ) {
  76. frag_len = len;
  77. if ( frag_len > sizeof ( buf ) )
  78. frag_len = sizeof ( buf );
  79. copy_from_user ( buf, image->data, offset, frag_len );
  80. digest_update ( digest, digest_ctx, buf, frag_len );
  81. len -= frag_len;
  82. offset += frag_len;
  83. }
  84. digest_final ( digest, digest_ctx, digest_out );
  85. for ( j = 0 ; j < sizeof ( digest_out ) ; j++ )
  86. printf ( "%02x", digest_out[j] );
  87. printf ( " %s\n", image->name );
  88. }
  89. return 0;
  90. }
  91. static int md5sum_exec ( int argc, char **argv ) {
  92. return digest_exec ( argc, argv, &md5_algorithm );
  93. }
  94. static int sha1sum_exec ( int argc, char **argv ) {
  95. return digest_exec ( argc, argv, &sha1_algorithm );
  96. }
  97. struct command md5sum_command __command = {
  98. .name = "md5sum",
  99. .exec = md5sum_exec,
  100. };
  101. struct command sha1sum_command __command = {
  102. .name = "sha1sum",
  103. .exec = sha1sum_exec,
  104. };