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