Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #include <stdio.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <gpxe/command.h>
  22. #include <gpxe/image.h>
  23. #include <gpxe/crypto.h>
  24. #include <gpxe/md5.h>
  25. #include <gpxe/sha1.h>
  26. /**
  27. * "digest" command syntax message
  28. *
  29. * @v argv Argument list
  30. */
  31. static void digest_syntax ( char **argv ) {
  32. printf ( "Usage:\n"
  33. " %s <image name>\n"
  34. "\n"
  35. "Calculate the %s of an image\n",
  36. argv[0], argv[0] );
  37. }
  38. /**
  39. * The "digest" command
  40. *
  41. * @v argc Argument count
  42. * @v argv Argument list
  43. * @v digest Digest algorithm
  44. * @ret rc Exit code
  45. */
  46. static int digest_exec ( int argc, char **argv,
  47. struct digest_algorithm *digest ) {
  48. const char *image_name;
  49. struct image *image;
  50. uint8_t digest_ctx[digest->ctxsize];
  51. uint8_t digest_out[digest->digestsize];
  52. uint8_t buf[128];
  53. size_t offset;
  54. size_t len;
  55. size_t frag_len;
  56. int i;
  57. unsigned j;
  58. if ( argc < 2 ||
  59. !strcmp ( argv[1], "--help" ) ||
  60. !strcmp ( argv[1], "-h" ) ) {
  61. digest_syntax ( argv );
  62. return 1;
  63. }
  64. for ( i = 1 ; i < argc ; i++ ) {
  65. image_name = argv[i];
  66. /* find image */
  67. image = find_image ( image_name );
  68. if ( ! image ) {
  69. printf ( "No such image: %s\n", image_name );
  70. continue;
  71. }
  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. };