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