md5sum.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2011
  4. * Joe Hershberger, National Instruments, joe.hershberger@ni.com
  5. *
  6. * (C) Copyright 2000
  7. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  8. */
  9. #include <common.h>
  10. #include <command.h>
  11. #include <env.h>
  12. #include <image.h>
  13. #include <mapmem.h>
  14. #include <u-boot/md5.h>
  15. #include <asm/io.h>
  16. /*
  17. * Store the resulting sum to an address or variable
  18. */
  19. static void store_result(const u8 *sum, const char *dest)
  20. {
  21. unsigned int i;
  22. if (*dest == '*') {
  23. u8 *ptr;
  24. ptr = (u8 *)hextoul(dest + 1, NULL);
  25. for (i = 0; i < 16; i++)
  26. *ptr++ = sum[i];
  27. } else {
  28. char str_output[33];
  29. char *str_ptr = str_output;
  30. for (i = 0; i < 16; i++) {
  31. sprintf(str_ptr, "%02x", sum[i]);
  32. str_ptr += 2;
  33. }
  34. env_set(dest, str_output);
  35. }
  36. }
  37. #ifdef CONFIG_MD5SUM_VERIFY
  38. static int parse_verify_sum(char *verify_str, u8 *vsum)
  39. {
  40. if (*verify_str == '*') {
  41. u8 *ptr;
  42. ptr = (u8 *)hextoul(verify_str + 1, NULL);
  43. memcpy(vsum, ptr, 16);
  44. } else {
  45. unsigned int i;
  46. char *vsum_str;
  47. if (strlen(verify_str) == 32)
  48. vsum_str = verify_str;
  49. else {
  50. vsum_str = env_get(verify_str);
  51. if (vsum_str == NULL || strlen(vsum_str) != 32)
  52. return 1;
  53. }
  54. for (i = 0; i < 16; i++) {
  55. char *nullp = vsum_str + (i + 1) * 2;
  56. char end = *nullp;
  57. *nullp = '\0';
  58. *(u8 *)(vsum + i) =
  59. hextoul(vsum_str + (i * 2), NULL);
  60. *nullp = end;
  61. }
  62. }
  63. return 0;
  64. }
  65. int do_md5sum(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  66. {
  67. ulong addr, len;
  68. unsigned int i;
  69. u8 output[16];
  70. u8 vsum[16];
  71. int verify = 0;
  72. int ac;
  73. char * const *av;
  74. void *buf;
  75. if (argc < 3)
  76. return CMD_RET_USAGE;
  77. av = argv + 1;
  78. ac = argc - 1;
  79. if (strcmp(*av, "-v") == 0) {
  80. verify = 1;
  81. av++;
  82. ac--;
  83. if (ac < 3)
  84. return CMD_RET_USAGE;
  85. }
  86. addr = hextoul(*av++, NULL);
  87. len = hextoul(*av++, NULL);
  88. buf = map_sysmem(addr, len);
  89. md5_wd(buf, len, output, CHUNKSZ_MD5);
  90. unmap_sysmem(buf);
  91. if (!verify) {
  92. printf("md5 for %08lx ... %08lx ==> ", addr, addr + len - 1);
  93. for (i = 0; i < 16; i++)
  94. printf("%02x", output[i]);
  95. printf("\n");
  96. if (ac > 2)
  97. store_result(output, *av);
  98. } else {
  99. char *verify_str = *av++;
  100. if (parse_verify_sum(verify_str, vsum)) {
  101. printf("ERROR: %s does not contain a valid md5 sum\n",
  102. verify_str);
  103. return 1;
  104. }
  105. if (memcmp(output, vsum, 16) != 0) {
  106. printf("md5 for %08lx ... %08lx ==> ", addr,
  107. addr + len - 1);
  108. for (i = 0; i < 16; i++)
  109. printf("%02x", output[i]);
  110. printf(" != ");
  111. for (i = 0; i < 16; i++)
  112. printf("%02x", vsum[i]);
  113. printf(" ** ERROR **\n");
  114. return 1;
  115. }
  116. }
  117. return 0;
  118. }
  119. #else
  120. static int do_md5sum(struct cmd_tbl *cmdtp, int flag, int argc,
  121. char *const argv[])
  122. {
  123. unsigned long addr, len;
  124. unsigned int i;
  125. u8 output[16];
  126. void *buf;
  127. if (argc < 3)
  128. return CMD_RET_USAGE;
  129. addr = hextoul(argv[1], NULL);
  130. len = hextoul(argv[2], NULL);
  131. buf = map_sysmem(addr, len);
  132. md5_wd(buf, len, output, CHUNKSZ_MD5);
  133. unmap_sysmem(buf);
  134. printf("md5 for %08lx ... %08lx ==> ", addr, addr + len - 1);
  135. for (i = 0; i < 16; i++)
  136. printf("%02x", output[i]);
  137. printf("\n");
  138. if (argc > 3)
  139. store_result(output, argv[3]);
  140. return 0;
  141. }
  142. #endif
  143. #ifdef CONFIG_MD5SUM_VERIFY
  144. U_BOOT_CMD(
  145. md5sum, 5, 1, do_md5sum,
  146. "compute MD5 message digest",
  147. "address count [[*]sum]\n"
  148. " - compute MD5 message digest [save to sum]\n"
  149. "md5sum -v address count [*]sum\n"
  150. " - verify md5sum of memory area"
  151. );
  152. #else
  153. U_BOOT_CMD(
  154. md5sum, 4, 1, do_md5sum,
  155. "compute MD5 message digest",
  156. "address count [[*]sum]\n"
  157. " - compute MD5 message digest [save to sum]"
  158. );
  159. #endif