1 // SPDX-License-Identifier: GPL-2.0+
8 #include <linux/ctype.h>
16 void write_to_env_var(char *varname, u8 *result, ulong len)
22 str_output = malloc(len * 2 + 1);
25 for (i = 0; i < len; i++) {
26 sprintf(str_ptr, "%02x", result[i]);
30 env_set(varname, str_output);
35 void read_from_env_var(char *varname, u8 *result)
39 str_value = env_get(varname);
41 hex2bin(result, str_value, strlen(str_value) / 2);
43 hex2bin(result, varname, strlen(varname) / 2);
46 void read_from_mem(ulong addr, u8 *result, ulong len)
50 src = map_sysmem(addr, len);
51 memcpy(result, src, len);
55 void write_to_mem(char *varname, u8 *result, ulong len)
60 addr = simple_strtoul(varname, NULL, 16);
61 buf = map_sysmem(addr, len);
62 memcpy(buf, result, len);
66 static int do_binop(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
69 u8 *result, *src1, *src2;
70 char *oparg, *lenarg, *src1arg, *src2arg, *destarg;
81 if (!strcmp(oparg, "xor"))
83 else if (!strcmp(oparg, "or"))
85 else if (!strcmp(oparg, "and"))
90 len = simple_strtoul(lenarg, NULL, 10);
96 read_from_mem(simple_strtoul(src1arg + 1, NULL, 16), src1, len);
98 read_from_env_var(src1arg, src1);
101 read_from_mem(simple_strtoul(src2arg + 1, NULL, 16), src2, len);
103 read_from_env_var(src2arg, src2);
105 result = malloc(len);
109 for (i = 0; i < len; i++)
110 result[i] = src1[i] ^ src2[i];
113 for (i = 0; i < len; i++)
114 result[i] = src1[i] | src2[i];
117 for (i = 0; i < len; i++)
118 result[i] = src1[i] & src2[i];
123 for (i = 0; i < len; i++) {
124 printf("%02x ", result[i]);
136 write_to_mem(destarg + 1, result, len); /* Skip asterisk */
138 write_to_env_var(destarg, result, len);
148 binop, 6, 1, do_binop,
149 "compute binary operation",
150 "op count [*]src1 [*]src2 [[*]dest]\n"
151 " - 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."