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.

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