1 // SPDX-License-Identifier: GPL-2.0+
7 #include <linux/ctype.h>
15 void write_to_env_var(char *varname, u8 *result, ulong len)
21 str_output = malloc(len * 2 + 1);
24 for (i = 0; i < len; i++) {
25 sprintf(str_ptr, "%02x", result[i]);
29 env_set(varname, str_output);
34 void decode_hexstring(char *hexstr, u8 *result)
39 for (i = 0; i < strlen(hexstr); ++i) {
46 value = (islower(d) ? toupper(d) : d) - 'A' + 10;
51 result[i / 2] = acc + value;
57 void read_from_env_var(char *varname, u8 *result)
61 str_value = env_get(varname);
63 decode_hexstring(str_value, result);
65 decode_hexstring(varname, result);
68 void read_from_mem(ulong addr, u8 *result, ulong len)
72 src = map_sysmem(addr, len);
73 memcpy(result, src, len);
77 void write_to_mem(char *varname, u8 *result, ulong len)
82 addr = simple_strtoul(varname, NULL, 16);
83 buf = map_sysmem(addr, len);
84 memcpy(buf, result, len);
88 static int do_binop(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
91 u8 *result, *src1, *src2;
92 char *oparg, *lenarg, *src1arg, *src2arg, *destarg;
103 if (!strcmp(oparg, "xor"))
105 else if (!strcmp(oparg, "or"))
107 else if (!strcmp(oparg, "and"))
110 return CMD_RET_USAGE;
112 len = simple_strtoul(lenarg, NULL, 10);
118 read_from_mem(simple_strtoul(src1arg + 1, NULL, 16), src1, len);
120 read_from_env_var(src1arg, src1);
123 read_from_mem(simple_strtoul(src2arg + 1, NULL, 16), src2, len);
125 read_from_env_var(src2arg, src2);
127 result = malloc(len);
131 for (i = 0; i < len; i++)
132 result[i] = src1[i] ^ src2[i];
135 for (i = 0; i < len; i++)
136 result[i] = src1[i] | src2[i];
139 for (i = 0; i < len; i++)
140 result[i] = src1[i] & src2[i];
145 for (i = 0; i < len; i++) {
146 printf("%02x ", result[i]);
158 write_to_mem(destarg + 1, result, len); /* Skip asterisk */
160 write_to_env_var(destarg, result, len);
170 binop, 6, 1, do_binop,
171 "compute binary operation",
172 "op count [*]src1 [*]src2 [[*]dest]\n"
173 " - 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."