Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Mike Frysinger | c3d2a17 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 2 | /* |
Joe Hershberger | 5ab177b | 2012-08-17 11:00:47 +0000 | [diff] [blame] | 3 | * (C) Copyright 2011 |
| 4 | * Joe Hershberger, National Instruments, joe.hershberger@ni.com |
| 5 | * |
Mike Frysinger | c3d2a17 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 6 | * (C) Copyright 2000 |
| 7 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Mike Frysinger | c3d2a17 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <command.h> |
Joe Hershberger | 0eb25b6 | 2015-03-22 17:08:59 -0500 | [diff] [blame] | 12 | #include <mapmem.h> |
Mike Frysinger | c3d2a17 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 13 | #include <u-boot/md5.h> |
Suriyan Ramasami | 7944495 | 2014-11-17 14:39:34 -0800 | [diff] [blame] | 14 | #include <asm/io.h> |
Mike Frysinger | c3d2a17 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 15 | |
Joe Hershberger | ecd7295 | 2012-08-17 11:00:48 +0000 | [diff] [blame] | 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 | |
| 23 | if (*dest == '*') { |
| 24 | u8 *ptr; |
| 25 | |
| 26 | ptr = (u8 *)simple_strtoul(dest + 1, NULL, 16); |
| 27 | for (i = 0; i < 16; i++) |
| 28 | *ptr++ = sum[i]; |
| 29 | } else { |
| 30 | char str_output[33]; |
| 31 | char *str_ptr = str_output; |
| 32 | |
| 33 | for (i = 0; i < 16; i++) { |
| 34 | sprintf(str_ptr, "%02x", sum[i]); |
| 35 | str_ptr += 2; |
| 36 | } |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 37 | env_set(dest, str_output); |
Joe Hershberger | ecd7295 | 2012-08-17 11:00:48 +0000 | [diff] [blame] | 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 { |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 56 | vsum_str = env_get(verify_str); |
Joe Hershberger | 5ab177b | 2012-08-17 11:00:47 +0000 | [diff] [blame] | 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; |
Suriyan Ramasami | 7944495 | 2014-11-17 14:39:34 -0800 | [diff] [blame] | 83 | void *buf; |
Joe Hershberger | 5ab177b | 2012-08-17 11:00:47 +0000 | [diff] [blame] | 84 | |
| 85 | if (argc < 3) |
| 86 | return CMD_RET_USAGE; |
| 87 | |
| 88 | av = argv + 1; |
| 89 | ac = argc - 1; |
| 90 | if (strcmp(*av, "-v") == 0) { |
| 91 | verify = 1; |
| 92 | av++; |
| 93 | ac--; |
| 94 | if (ac < 3) |
| 95 | return CMD_RET_USAGE; |
| 96 | } |
| 97 | |
| 98 | addr = simple_strtoul(*av++, NULL, 16); |
| 99 | len = simple_strtoul(*av++, NULL, 16); |
| 100 | |
Suriyan Ramasami | 7944495 | 2014-11-17 14:39:34 -0800 | [diff] [blame] | 101 | buf = map_sysmem(addr, len); |
| 102 | md5_wd(buf, len, output, CHUNKSZ_MD5); |
| 103 | unmap_sysmem(buf); |
Joe Hershberger | 5ab177b | 2012-08-17 11:00:47 +0000 | [diff] [blame] | 104 | |
| 105 | if (!verify) { |
| 106 | printf("md5 for %08lx ... %08lx ==> ", addr, addr + len - 1); |
| 107 | for (i = 0; i < 16; i++) |
| 108 | printf("%02x", output[i]); |
| 109 | printf("\n"); |
Joe Hershberger | ecd7295 | 2012-08-17 11:00:48 +0000 | [diff] [blame] | 110 | |
| 111 | if (ac > 2) |
| 112 | store_result(output, *av); |
Joe Hershberger | 5ab177b | 2012-08-17 11:00:47 +0000 | [diff] [blame] | 113 | } else { |
| 114 | char *verify_str = *av++; |
| 115 | |
| 116 | if (parse_verify_sum(verify_str, vsum)) { |
| 117 | printf("ERROR: %s does not contain a valid md5 sum\n", |
| 118 | verify_str); |
| 119 | return 1; |
| 120 | } |
| 121 | if (memcmp(output, vsum, 16) != 0) { |
| 122 | printf("md5 for %08lx ... %08lx ==> ", addr, |
| 123 | addr + len - 1); |
| 124 | for (i = 0; i < 16; i++) |
| 125 | printf("%02x", output[i]); |
| 126 | printf(" != "); |
| 127 | for (i = 0; i < 16; i++) |
| 128 | printf("%02x", vsum[i]); |
| 129 | printf(" ** ERROR **\n"); |
| 130 | return 1; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | #else |
Mike Frysinger | c3d2a17 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 137 | static int do_md5sum(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 138 | { |
| 139 | unsigned long addr, len; |
| 140 | unsigned int i; |
| 141 | u8 output[16]; |
Suriyan Ramasami | 7944495 | 2014-11-17 14:39:34 -0800 | [diff] [blame] | 142 | void *buf; |
Mike Frysinger | c3d2a17 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 143 | |
| 144 | if (argc < 3) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 145 | return CMD_RET_USAGE; |
Mike Frysinger | c3d2a17 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 146 | |
| 147 | addr = simple_strtoul(argv[1], NULL, 16); |
| 148 | len = simple_strtoul(argv[2], NULL, 16); |
| 149 | |
Suriyan Ramasami | 7944495 | 2014-11-17 14:39:34 -0800 | [diff] [blame] | 150 | buf = map_sysmem(addr, len); |
| 151 | md5_wd(buf, len, output, CHUNKSZ_MD5); |
| 152 | unmap_sysmem(buf); |
| 153 | |
Mike Frysinger | c3d2a17 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 154 | printf("md5 for %08lx ... %08lx ==> ", addr, addr + len - 1); |
| 155 | for (i = 0; i < 16; i++) |
| 156 | printf("%02x", output[i]); |
| 157 | printf("\n"); |
| 158 | |
Joe Hershberger | ecd7295 | 2012-08-17 11:00:48 +0000 | [diff] [blame] | 159 | if (argc > 3) |
| 160 | store_result(output, argv[3]); |
| 161 | |
Mike Frysinger | c3d2a17 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 162 | return 0; |
| 163 | } |
Joe Hershberger | 5ab177b | 2012-08-17 11:00:47 +0000 | [diff] [blame] | 164 | #endif |
Mike Frysinger | c3d2a17 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 165 | |
Joe Hershberger | 5ab177b | 2012-08-17 11:00:47 +0000 | [diff] [blame] | 166 | #ifdef CONFIG_MD5SUM_VERIFY |
| 167 | U_BOOT_CMD( |
| 168 | md5sum, 5, 1, do_md5sum, |
| 169 | "compute MD5 message digest", |
Joe Hershberger | ecd7295 | 2012-08-17 11:00:48 +0000 | [diff] [blame] | 170 | "address count [[*]sum]\n" |
| 171 | " - compute MD5 message digest [save to sum]\n" |
Joe Hershberger | 5ab177b | 2012-08-17 11:00:47 +0000 | [diff] [blame] | 172 | "md5sum -v address count [*]sum\n" |
| 173 | " - verify md5sum of memory area" |
| 174 | ); |
| 175 | #else |
Mike Frysinger | c3d2a17 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 176 | U_BOOT_CMD( |
Joe Hershberger | ecd7295 | 2012-08-17 11:00:48 +0000 | [diff] [blame] | 177 | md5sum, 4, 1, do_md5sum, |
Mike Frysinger | c3d2a17 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 178 | "compute MD5 message digest", |
Joe Hershberger | ecd7295 | 2012-08-17 11:00:48 +0000 | [diff] [blame] | 179 | "address count [[*]sum]\n" |
| 180 | " - compute MD5 message digest [save to sum]" |
Mike Frysinger | c3d2a17 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 181 | ); |
Joe Hershberger | 5ab177b | 2012-08-17 11:00:47 +0000 | [diff] [blame] | 182 | #endif |