Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Mario Six | 55b2556 | 2018-03-28 14:39:18 +0200 | [diff] [blame] | 2 | |
| 3 | #include <common.h> |
| 4 | #include <command.h> |
Simon Glass | 9fb625c | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 5 | #include <env.h> |
Mario Six | 32dbe8c | 2019-01-28 09:43:43 +0100 | [diff] [blame] | 6 | #include <hexdump.h> |
Mario Six | 55b2556 | 2018-03-28 14:39:18 +0200 | [diff] [blame] | 7 | #include <malloc.h> |
| 8 | #include <mapmem.h> |
| 9 | #include <linux/ctype.h> |
| 10 | |
| 11 | enum { |
| 12 | OP_ID_XOR, |
| 13 | OP_ID_AND, |
| 14 | OP_ID_OR, |
| 15 | }; |
| 16 | |
| 17 | void write_to_env_var(char *varname, u8 *result, ulong len) |
| 18 | { |
| 19 | char *str_output; |
| 20 | char *str_ptr; |
| 21 | int i; |
| 22 | |
| 23 | str_output = malloc(len * 2 + 1); |
| 24 | str_ptr = str_output; |
| 25 | |
| 26 | for (i = 0; i < len; i++) { |
| 27 | sprintf(str_ptr, "%02x", result[i]); |
| 28 | str_ptr += 2; |
| 29 | } |
| 30 | *str_ptr = '\0'; |
Mario Six | 29dada9 | 2019-01-28 09:43:42 +0100 | [diff] [blame] | 31 | env_set(varname, str_output); |
Mario Six | 55b2556 | 2018-03-28 14:39:18 +0200 | [diff] [blame] | 32 | |
| 33 | free(str_output); |
| 34 | } |
| 35 | |
Mario Six | 55b2556 | 2018-03-28 14:39:18 +0200 | [diff] [blame] | 36 | void read_from_env_var(char *varname, u8 *result) |
| 37 | { |
| 38 | char *str_value; |
| 39 | |
Mario Six | 29dada9 | 2019-01-28 09:43:42 +0100 | [diff] [blame] | 40 | str_value = env_get(varname); |
Mario Six | 55b2556 | 2018-03-28 14:39:18 +0200 | [diff] [blame] | 41 | if (str_value) |
Mario Six | 32dbe8c | 2019-01-28 09:43:43 +0100 | [diff] [blame] | 42 | hex2bin(result, str_value, strlen(str_value) / 2); |
Mario Six | 55b2556 | 2018-03-28 14:39:18 +0200 | [diff] [blame] | 43 | else |
Mario Six | 32dbe8c | 2019-01-28 09:43:43 +0100 | [diff] [blame] | 44 | hex2bin(result, varname, strlen(varname) / 2); |
Mario Six | 55b2556 | 2018-03-28 14:39:18 +0200 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void read_from_mem(ulong addr, u8 *result, ulong len) |
| 48 | { |
| 49 | u8 *src; |
| 50 | |
| 51 | src = map_sysmem(addr, len); |
| 52 | memcpy(result, src, len); |
| 53 | unmap_sysmem(src); |
| 54 | } |
| 55 | |
| 56 | void write_to_mem(char *varname, u8 *result, ulong len) |
| 57 | { |
| 58 | ulong addr; |
| 59 | u8 *buf; |
| 60 | |
| 61 | addr = simple_strtoul(varname, NULL, 16); |
| 62 | buf = map_sysmem(addr, len); |
| 63 | memcpy(buf, result, len); |
| 64 | unmap_sysmem(buf); |
| 65 | } |
| 66 | |
| 67 | static int do_binop(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 68 | { |
| 69 | ulong len; |
| 70 | u8 *result, *src1, *src2; |
| 71 | char *oparg, *lenarg, *src1arg, *src2arg, *destarg; |
| 72 | int i, op; |
| 73 | |
| 74 | if (argc < 5) |
| 75 | return CMD_RET_USAGE; |
| 76 | |
| 77 | oparg = argv[1]; |
| 78 | lenarg = argv[2]; |
| 79 | src1arg = argv[3]; |
| 80 | src2arg = argv[4]; |
| 81 | |
| 82 | if (!strcmp(oparg, "xor")) |
| 83 | op = OP_ID_XOR; |
| 84 | else if (!strcmp(oparg, "or")) |
| 85 | op = OP_ID_OR; |
| 86 | else if (!strcmp(oparg, "and")) |
| 87 | op = OP_ID_AND; |
| 88 | else |
| 89 | return CMD_RET_USAGE; |
| 90 | |
| 91 | len = simple_strtoul(lenarg, NULL, 10); |
| 92 | |
| 93 | src1 = malloc(len); |
| 94 | src2 = malloc(len); |
| 95 | |
| 96 | if (*src1arg == '*') |
| 97 | read_from_mem(simple_strtoul(src1arg + 1, NULL, 16), src1, len); |
| 98 | else |
| 99 | read_from_env_var(src1arg, src1); |
| 100 | |
| 101 | if (*src2arg == '*') |
| 102 | read_from_mem(simple_strtoul(src2arg + 1, NULL, 16), src2, len); |
| 103 | else |
| 104 | read_from_env_var(src2arg, src2); |
| 105 | |
| 106 | result = malloc(len); |
| 107 | |
| 108 | switch (op) { |
| 109 | case OP_ID_XOR: |
| 110 | for (i = 0; i < len; i++) |
| 111 | result[i] = src1[i] ^ src2[i]; |
| 112 | break; |
| 113 | case OP_ID_OR: |
| 114 | for (i = 0; i < len; i++) |
| 115 | result[i] = src1[i] | src2[i]; |
| 116 | break; |
| 117 | case OP_ID_AND: |
| 118 | for (i = 0; i < len; i++) |
| 119 | result[i] = src1[i] & src2[i]; |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | if (argc == 5) { |
| 124 | for (i = 0; i < len; i++) { |
| 125 | printf("%02x ", result[i]); |
| 126 | if (i % 16 == 15) |
| 127 | puts("\n"); |
| 128 | } |
| 129 | puts("\n"); |
| 130 | |
| 131 | goto exit; |
| 132 | } |
| 133 | |
| 134 | destarg = argv[5]; |
| 135 | |
| 136 | if (*destarg == '*') |
| 137 | write_to_mem(destarg + 1, result, len); /* Skip asterisk */ |
| 138 | else |
| 139 | write_to_env_var(destarg, result, len); |
| 140 | exit: |
| 141 | free(result); |
| 142 | free(src2); |
| 143 | free(src1); |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | U_BOOT_CMD( |
| 149 | binop, 6, 1, do_binop, |
| 150 | "compute binary operation", |
| 151 | "op count [*]src1 [*]src2 [[*]dest]\n" |
| 152 | " - compute binary operation of data at/in src1 and\n src2 (either *memaddr, env var name or hex string)\n and store result in/at dest, where op is one of\n xor, or, and." |
| 153 | ); |