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 | * |
| 8 | * See file CREDITS for list of people who contributed to this |
| 9 | * project. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of |
| 14 | * the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 24 | * MA 02111-1307 USA |
| 25 | */ |
| 26 | |
| 27 | #include <common.h> |
| 28 | #include <command.h> |
| 29 | #include <u-boot/md5.h> |
| 30 | |
Joe Hershberger | ecd7295 | 2012-08-17 11:00:48 +0000 | [diff] [blame] | 31 | /* |
| 32 | * Store the resulting sum to an address or variable |
| 33 | */ |
| 34 | static void store_result(const u8 *sum, const char *dest) |
| 35 | { |
| 36 | unsigned int i; |
| 37 | |
| 38 | if (*dest == '*') { |
| 39 | u8 *ptr; |
| 40 | |
| 41 | ptr = (u8 *)simple_strtoul(dest + 1, NULL, 16); |
| 42 | for (i = 0; i < 16; i++) |
| 43 | *ptr++ = sum[i]; |
| 44 | } else { |
| 45 | char str_output[33]; |
| 46 | char *str_ptr = str_output; |
| 47 | |
| 48 | for (i = 0; i < 16; i++) { |
| 49 | sprintf(str_ptr, "%02x", sum[i]); |
| 50 | str_ptr += 2; |
| 51 | } |
| 52 | str_ptr = '\0'; |
| 53 | setenv(dest, str_output); |
| 54 | } |
| 55 | } |
| 56 | |
Joe Hershberger | 5ab177b | 2012-08-17 11:00:47 +0000 | [diff] [blame] | 57 | #ifdef CONFIG_MD5SUM_VERIFY |
| 58 | static int parse_verify_sum(char *verify_str, u8 *vsum) |
| 59 | { |
| 60 | if (*verify_str == '*') { |
| 61 | u8 *ptr; |
| 62 | |
| 63 | ptr = (u8 *)simple_strtoul(verify_str + 1, NULL, 16); |
| 64 | memcpy(vsum, ptr, 16); |
| 65 | } else { |
| 66 | unsigned int i; |
| 67 | char *vsum_str; |
| 68 | |
| 69 | if (strlen(verify_str) == 32) |
| 70 | vsum_str = verify_str; |
| 71 | else { |
| 72 | vsum_str = getenv(verify_str); |
| 73 | if (vsum_str == NULL || strlen(vsum_str) != 32) |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | for (i = 0; i < 16; i++) { |
| 78 | char *nullp = vsum_str + (i + 1) * 2; |
| 79 | char end = *nullp; |
| 80 | |
| 81 | *nullp = '\0'; |
| 82 | *(u8 *)(vsum + i) = |
| 83 | simple_strtoul(vsum_str + (i * 2), NULL, 16); |
| 84 | *nullp = end; |
| 85 | } |
| 86 | } |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | int do_md5sum(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 91 | { |
| 92 | ulong addr, len; |
| 93 | unsigned int i; |
| 94 | u8 output[16]; |
| 95 | u8 vsum[16]; |
| 96 | int verify = 0; |
| 97 | int ac; |
| 98 | char * const *av; |
| 99 | |
| 100 | if (argc < 3) |
| 101 | return CMD_RET_USAGE; |
| 102 | |
| 103 | av = argv + 1; |
| 104 | ac = argc - 1; |
| 105 | if (strcmp(*av, "-v") == 0) { |
| 106 | verify = 1; |
| 107 | av++; |
| 108 | ac--; |
| 109 | if (ac < 3) |
| 110 | return CMD_RET_USAGE; |
| 111 | } |
| 112 | |
| 113 | addr = simple_strtoul(*av++, NULL, 16); |
| 114 | len = simple_strtoul(*av++, NULL, 16); |
| 115 | |
| 116 | md5_wd((unsigned char *) addr, len, output, CHUNKSZ_MD5); |
| 117 | |
| 118 | if (!verify) { |
| 119 | printf("md5 for %08lx ... %08lx ==> ", addr, addr + len - 1); |
| 120 | for (i = 0; i < 16; i++) |
| 121 | printf("%02x", output[i]); |
| 122 | printf("\n"); |
Joe Hershberger | ecd7295 | 2012-08-17 11:00:48 +0000 | [diff] [blame] | 123 | |
| 124 | if (ac > 2) |
| 125 | store_result(output, *av); |
Joe Hershberger | 5ab177b | 2012-08-17 11:00:47 +0000 | [diff] [blame] | 126 | } else { |
| 127 | char *verify_str = *av++; |
| 128 | |
| 129 | if (parse_verify_sum(verify_str, vsum)) { |
| 130 | printf("ERROR: %s does not contain a valid md5 sum\n", |
| 131 | verify_str); |
| 132 | return 1; |
| 133 | } |
| 134 | if (memcmp(output, vsum, 16) != 0) { |
| 135 | printf("md5 for %08lx ... %08lx ==> ", addr, |
| 136 | addr + len - 1); |
| 137 | for (i = 0; i < 16; i++) |
| 138 | printf("%02x", output[i]); |
| 139 | printf(" != "); |
| 140 | for (i = 0; i < 16; i++) |
| 141 | printf("%02x", vsum[i]); |
| 142 | printf(" ** ERROR **\n"); |
| 143 | return 1; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | #else |
Mike Frysinger | c3d2a17 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 150 | static int do_md5sum(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 151 | { |
| 152 | unsigned long addr, len; |
| 153 | unsigned int i; |
| 154 | u8 output[16]; |
| 155 | |
| 156 | if (argc < 3) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 157 | return CMD_RET_USAGE; |
Mike Frysinger | c3d2a17 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 158 | |
| 159 | addr = simple_strtoul(argv[1], NULL, 16); |
| 160 | len = simple_strtoul(argv[2], NULL, 16); |
| 161 | |
Jens Scharsig | a16a84b | 2011-07-18 14:39:11 +0200 | [diff] [blame] | 162 | md5_wd((unsigned char *) addr, len, output, CHUNKSZ_MD5); |
Mike Frysinger | c3d2a17 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 163 | printf("md5 for %08lx ... %08lx ==> ", addr, addr + len - 1); |
| 164 | for (i = 0; i < 16; i++) |
| 165 | printf("%02x", output[i]); |
| 166 | printf("\n"); |
| 167 | |
Joe Hershberger | ecd7295 | 2012-08-17 11:00:48 +0000 | [diff] [blame] | 168 | if (argc > 3) |
| 169 | store_result(output, argv[3]); |
| 170 | |
Mike Frysinger | c3d2a17 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 171 | return 0; |
| 172 | } |
Joe Hershberger | 5ab177b | 2012-08-17 11:00:47 +0000 | [diff] [blame] | 173 | #endif |
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 | #ifdef CONFIG_MD5SUM_VERIFY |
| 176 | U_BOOT_CMD( |
| 177 | md5sum, 5, 1, do_md5sum, |
| 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]\n" |
Joe Hershberger | 5ab177b | 2012-08-17 11:00:47 +0000 | [diff] [blame] | 181 | "md5sum -v address count [*]sum\n" |
| 182 | " - verify md5sum of memory area" |
| 183 | ); |
| 184 | #else |
Mike Frysinger | c3d2a17 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 185 | U_BOOT_CMD( |
Joe Hershberger | ecd7295 | 2012-08-17 11:00:48 +0000 | [diff] [blame] | 186 | md5sum, 4, 1, do_md5sum, |
Mike Frysinger | c3d2a17 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 187 | "compute MD5 message digest", |
Joe Hershberger | ecd7295 | 2012-08-17 11:00:48 +0000 | [diff] [blame] | 188 | "address count [[*]sum]\n" |
| 189 | " - compute MD5 message digest [save to sum]" |
Mike Frysinger | c3d2a17 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 190 | ); |
Joe Hershberger | 5ab177b | 2012-08-17 11:00:47 +0000 | [diff] [blame] | 191 | #endif |