Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Mike Frysinger | a227490 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 2 | /* |
Joe Hershberger | cd39f64 | 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 | a227490 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 6 | * (C) Copyright 2000 |
| 7 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Mike Frysinger | a227490 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <command.h> |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 12 | #include <env.h> |
Simon Glass | 2dc9c34 | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 13 | #include <image.h> |
Joe Hershberger | 65b905b | 2015-03-22 17:08:59 -0500 | [diff] [blame] | 14 | #include <mapmem.h> |
Mike Frysinger | a227490 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 15 | #include <u-boot/md5.h> |
Suriyan Ramasami | 95bfacb | 2014-11-17 14:39:34 -0800 | [diff] [blame] | 16 | #include <asm/io.h> |
Mike Frysinger | a227490 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 17 | |
Joe Hershberger | 47685cf | 2012-08-17 11:00:48 +0000 | [diff] [blame] | 18 | /* |
| 19 | * Store the resulting sum to an address or variable |
| 20 | */ |
| 21 | static void store_result(const u8 *sum, const char *dest) |
| 22 | { |
| 23 | unsigned int i; |
| 24 | |
| 25 | if (*dest == '*') { |
| 26 | u8 *ptr; |
| 27 | |
| 28 | ptr = (u8 *)simple_strtoul(dest + 1, NULL, 16); |
| 29 | for (i = 0; i < 16; i++) |
| 30 | *ptr++ = sum[i]; |
| 31 | } else { |
| 32 | char str_output[33]; |
| 33 | char *str_ptr = str_output; |
| 34 | |
| 35 | for (i = 0; i < 16; i++) { |
| 36 | sprintf(str_ptr, "%02x", sum[i]); |
| 37 | str_ptr += 2; |
| 38 | } |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 39 | env_set(dest, str_output); |
Joe Hershberger | 47685cf | 2012-08-17 11:00:48 +0000 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
Joe Hershberger | cd39f64 | 2012-08-17 11:00:47 +0000 | [diff] [blame] | 43 | #ifdef CONFIG_MD5SUM_VERIFY |
| 44 | static int parse_verify_sum(char *verify_str, u8 *vsum) |
| 45 | { |
| 46 | if (*verify_str == '*') { |
| 47 | u8 *ptr; |
| 48 | |
| 49 | ptr = (u8 *)simple_strtoul(verify_str + 1, NULL, 16); |
| 50 | memcpy(vsum, ptr, 16); |
| 51 | } else { |
| 52 | unsigned int i; |
| 53 | char *vsum_str; |
| 54 | |
| 55 | if (strlen(verify_str) == 32) |
| 56 | vsum_str = verify_str; |
| 57 | else { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 58 | vsum_str = env_get(verify_str); |
Joe Hershberger | cd39f64 | 2012-08-17 11:00:47 +0000 | [diff] [blame] | 59 | if (vsum_str == NULL || strlen(vsum_str) != 32) |
| 60 | return 1; |
| 61 | } |
| 62 | |
| 63 | for (i = 0; i < 16; i++) { |
| 64 | char *nullp = vsum_str + (i + 1) * 2; |
| 65 | char end = *nullp; |
| 66 | |
| 67 | *nullp = '\0'; |
| 68 | *(u8 *)(vsum + i) = |
| 69 | simple_strtoul(vsum_str + (i * 2), NULL, 16); |
| 70 | *nullp = end; |
| 71 | } |
| 72 | } |
| 73 | return 0; |
| 74 | } |
| 75 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 76 | int do_md5sum(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Joe Hershberger | cd39f64 | 2012-08-17 11:00:47 +0000 | [diff] [blame] | 77 | { |
| 78 | ulong addr, len; |
| 79 | unsigned int i; |
| 80 | u8 output[16]; |
| 81 | u8 vsum[16]; |
| 82 | int verify = 0; |
| 83 | int ac; |
| 84 | char * const *av; |
Suriyan Ramasami | 95bfacb | 2014-11-17 14:39:34 -0800 | [diff] [blame] | 85 | void *buf; |
Joe Hershberger | cd39f64 | 2012-08-17 11:00:47 +0000 | [diff] [blame] | 86 | |
| 87 | if (argc < 3) |
| 88 | return CMD_RET_USAGE; |
| 89 | |
| 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 | |
| 100 | addr = simple_strtoul(*av++, NULL, 16); |
| 101 | len = simple_strtoul(*av++, NULL, 16); |
| 102 | |
Suriyan Ramasami | 95bfacb | 2014-11-17 14:39:34 -0800 | [diff] [blame] | 103 | buf = map_sysmem(addr, len); |
| 104 | md5_wd(buf, len, output, CHUNKSZ_MD5); |
| 105 | unmap_sysmem(buf); |
Joe Hershberger | cd39f64 | 2012-08-17 11:00:47 +0000 | [diff] [blame] | 106 | |
| 107 | if (!verify) { |
| 108 | printf("md5 for %08lx ... %08lx ==> ", addr, addr + len - 1); |
| 109 | for (i = 0; i < 16; i++) |
| 110 | printf("%02x", output[i]); |
| 111 | printf("\n"); |
Joe Hershberger | 47685cf | 2012-08-17 11:00:48 +0000 | [diff] [blame] | 112 | |
| 113 | if (ac > 2) |
| 114 | store_result(output, *av); |
Joe Hershberger | cd39f64 | 2012-08-17 11:00:47 +0000 | [diff] [blame] | 115 | } else { |
| 116 | char *verify_str = *av++; |
| 117 | |
| 118 | if (parse_verify_sum(verify_str, vsum)) { |
| 119 | printf("ERROR: %s does not contain a valid md5 sum\n", |
| 120 | verify_str); |
| 121 | return 1; |
| 122 | } |
| 123 | if (memcmp(output, vsum, 16) != 0) { |
| 124 | printf("md5 for %08lx ... %08lx ==> ", addr, |
| 125 | addr + len - 1); |
| 126 | for (i = 0; i < 16; i++) |
| 127 | printf("%02x", output[i]); |
| 128 | printf(" != "); |
| 129 | for (i = 0; i < 16; i++) |
| 130 | printf("%02x", vsum[i]); |
| 131 | printf(" ** ERROR **\n"); |
| 132 | return 1; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | #else |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 139 | static int do_md5sum(struct cmd_tbl *cmdtp, int flag, int argc, |
| 140 | char *const argv[]) |
Mike Frysinger | a227490 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 141 | { |
| 142 | unsigned long addr, len; |
| 143 | unsigned int i; |
| 144 | u8 output[16]; |
Suriyan Ramasami | 95bfacb | 2014-11-17 14:39:34 -0800 | [diff] [blame] | 145 | void *buf; |
Mike Frysinger | a227490 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 146 | |
| 147 | if (argc < 3) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 148 | return CMD_RET_USAGE; |
Mike Frysinger | a227490 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 149 | |
| 150 | addr = simple_strtoul(argv[1], NULL, 16); |
| 151 | len = simple_strtoul(argv[2], NULL, 16); |
| 152 | |
Suriyan Ramasami | 95bfacb | 2014-11-17 14:39:34 -0800 | [diff] [blame] | 153 | buf = map_sysmem(addr, len); |
| 154 | md5_wd(buf, len, output, CHUNKSZ_MD5); |
| 155 | unmap_sysmem(buf); |
| 156 | |
Mike Frysinger | a227490 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 157 | printf("md5 for %08lx ... %08lx ==> ", addr, addr + len - 1); |
| 158 | for (i = 0; i < 16; i++) |
| 159 | printf("%02x", output[i]); |
| 160 | printf("\n"); |
| 161 | |
Joe Hershberger | 47685cf | 2012-08-17 11:00:48 +0000 | [diff] [blame] | 162 | if (argc > 3) |
| 163 | store_result(output, argv[3]); |
| 164 | |
Mike Frysinger | a227490 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 165 | return 0; |
| 166 | } |
Joe Hershberger | cd39f64 | 2012-08-17 11:00:47 +0000 | [diff] [blame] | 167 | #endif |
Mike Frysinger | a227490 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 168 | |
Joe Hershberger | cd39f64 | 2012-08-17 11:00:47 +0000 | [diff] [blame] | 169 | #ifdef CONFIG_MD5SUM_VERIFY |
| 170 | U_BOOT_CMD( |
| 171 | md5sum, 5, 1, do_md5sum, |
| 172 | "compute MD5 message digest", |
Joe Hershberger | 47685cf | 2012-08-17 11:00:48 +0000 | [diff] [blame] | 173 | "address count [[*]sum]\n" |
| 174 | " - compute MD5 message digest [save to sum]\n" |
Joe Hershberger | cd39f64 | 2012-08-17 11:00:47 +0000 | [diff] [blame] | 175 | "md5sum -v address count [*]sum\n" |
| 176 | " - verify md5sum of memory area" |
| 177 | ); |
| 178 | #else |
Mike Frysinger | a227490 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 179 | U_BOOT_CMD( |
Joe Hershberger | 47685cf | 2012-08-17 11:00:48 +0000 | [diff] [blame] | 180 | md5sum, 4, 1, do_md5sum, |
Mike Frysinger | a227490 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 181 | "compute MD5 message digest", |
Joe Hershberger | 47685cf | 2012-08-17 11:00:48 +0000 | [diff] [blame] | 182 | "address count [[*]sum]\n" |
| 183 | " - compute MD5 message digest [save to sum]" |
Mike Frysinger | a227490 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 184 | ); |
Joe Hershberger | cd39f64 | 2012-08-17 11:00:47 +0000 | [diff] [blame] | 185 | #endif |