cmd_md5sum.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * (C) Copyright 2011
  3. * Joe Hershberger, National Instruments, joe.hershberger@ni.com
  4. *
  5. * (C) Copyright 2000
  6. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #include <command.h>
  28. #include <u-boot/md5.h>
  29. /*
  30. * Store the resulting sum to an address or variable
  31. */
  32. static void store_result(const u8 *sum, const char *dest)
  33. {
  34. unsigned int i;
  35. if (*dest == '*') {
  36. u8 *ptr;
  37. ptr = (u8 *)simple_strtoul(dest + 1, NULL, 16);
  38. for (i = 0; i < 16; i++)
  39. *ptr++ = sum[i];
  40. } else {
  41. char str_output[33];
  42. char *str_ptr = str_output;
  43. for (i = 0; i < 16; i++) {
  44. sprintf(str_ptr, "%02x", sum[i]);
  45. str_ptr += 2;
  46. }
  47. str_ptr = '\0';
  48. setenv(dest, str_output);
  49. }
  50. }
  51. #ifdef CONFIG_MD5SUM_VERIFY
  52. static int parse_verify_sum(char *verify_str, u8 *vsum)
  53. {
  54. if (*verify_str == '*') {
  55. u8 *ptr;
  56. ptr = (u8 *)simple_strtoul(verify_str + 1, NULL, 16);
  57. memcpy(vsum, ptr, 16);
  58. } else {
  59. unsigned int i;
  60. char *vsum_str;
  61. if (strlen(verify_str) == 32)
  62. vsum_str = verify_str;
  63. else {
  64. vsum_str = getenv(verify_str);
  65. if (vsum_str == NULL || strlen(vsum_str) != 32)
  66. return 1;
  67. }
  68. for (i = 0; i < 16; i++) {
  69. char *nullp = vsum_str + (i + 1) * 2;
  70. char end = *nullp;
  71. *nullp = '\0';
  72. *(u8 *)(vsum + i) =
  73. simple_strtoul(vsum_str + (i * 2), NULL, 16);
  74. *nullp = end;
  75. }
  76. }
  77. return 0;
  78. }
  79. int do_md5sum(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  80. {
  81. ulong addr, len;
  82. unsigned int i;
  83. u8 output[16];
  84. u8 vsum[16];
  85. int verify = 0;
  86. int ac;
  87. char * const *av;
  88. if (argc < 3)
  89. return CMD_RET_USAGE;
  90. av = argv + 1;
  91. ac = argc - 1;
  92. if (strcmp(*av, "-v") == 0) {
  93. verify = 1;
  94. av++;
  95. ac--;
  96. if (ac < 3)
  97. return CMD_RET_USAGE;
  98. }
  99. addr = simple_strtoul(*av++, NULL, 16);
  100. len = simple_strtoul(*av++, NULL, 16);
  101. md5_wd((unsigned char *) addr, len, output, CHUNKSZ_MD5);
  102. if (!verify) {
  103. printf("md5 for %08lx ... %08lx ==> ", addr, addr + len - 1);
  104. for (i = 0; i < 16; i++)
  105. printf("%02x", output[i]);
  106. printf("\n");
  107. if (ac > 2)
  108. store_result(output, *av);
  109. } else {
  110. char *verify_str = *av++;
  111. if (parse_verify_sum(verify_str, vsum)) {
  112. printf("ERROR: %s does not contain a valid md5 sum\n",
  113. verify_str);
  114. return 1;
  115. }
  116. if (memcmp(output, vsum, 16) != 0) {
  117. printf("md5 for %08lx ... %08lx ==> ", addr,
  118. addr + len - 1);
  119. for (i = 0; i < 16; i++)
  120. printf("%02x", output[i]);
  121. printf(" != ");
  122. for (i = 0; i < 16; i++)
  123. printf("%02x", vsum[i]);
  124. printf(" ** ERROR **\n");
  125. return 1;
  126. }
  127. }
  128. return 0;
  129. }
  130. #else
  131. static int do_md5sum(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  132. {
  133. unsigned long addr, len;
  134. unsigned int i;
  135. u8 output[16];
  136. if (argc < 3)
  137. return CMD_RET_USAGE;
  138. addr = simple_strtoul(argv[1], NULL, 16);
  139. len = simple_strtoul(argv[2], NULL, 16);
  140. md5_wd((unsigned char *) addr, len, output, CHUNKSZ_MD5);
  141. printf("md5 for %08lx ... %08lx ==> ", addr, addr + len - 1);
  142. for (i = 0; i < 16; i++)
  143. printf("%02x", output[i]);
  144. printf("\n");
  145. if (argc > 3)
  146. store_result(output, argv[3]);
  147. return 0;
  148. }
  149. #endif
  150. #ifdef CONFIG_MD5SUM_VERIFY
  151. U_BOOT_CMD(
  152. md5sum, 5, 1, do_md5sum,
  153. "compute MD5 message digest",
  154. "address count [[*]sum]\n"
  155. " - compute MD5 message digest [save to sum]\n"
  156. "md5sum -v address count [*]sum\n"
  157. " - verify md5sum of memory area"
  158. );
  159. #else
  160. U_BOOT_CMD(
  161. md5sum, 4, 1, do_md5sum,
  162. "compute MD5 message digest",
  163. "address count [[*]sum]\n"
  164. " - compute MD5 message digest [save to sum]"
  165. );
  166. #endif