1 // SPDX-License-Identifier: GPL-2.0+
9 #include <linux/ctype.h>
17 void write_to_env_var(char *varname, u8 *result, ulong len)
23 str_output = malloc(len * 2 + 1);
26 for (i = 0; i < len; i++) {
27 sprintf(str_ptr, "%02x", result[i]);
31 env_set(varname, str_output);
36 void read_from_env_var(char *varname, u8 *result)
40 str_value = env_get(varname);
42 hex2bin(result, str_value, strlen(str_value) / 2);
44 hex2bin(result, varname, strlen(varname) / 2);
47 void read_from_mem(ulong addr, u8 *result, ulong len)
51 src = map_sysmem(addr, len);
52 memcpy(result, src, len);
56 void write_to_mem(char *varname, u8 *result, ulong len)
61 addr = simple_strtoul(varname, NULL, 16);
62 buf = map_sysmem(addr, len);
63 memcpy(buf, result, len);
67 static int do_binop(struct cmd_tbl *cmdtp, int flag, int argc,
71 u8 *result, *src1, *src2;
72 char *oparg, *lenarg, *src1arg, *src2arg, *destarg;
83 if (!strcmp(oparg, "xor"))
85 else if (!strcmp(oparg, "or"))
87 else if (!strcmp(oparg, "and"))
92 len = simple_strtoul(lenarg, NULL, 10);
98 read_from_mem(simple_strtoul(src1arg + 1, NULL, 16), src1, len);
100 read_from_env_var(src1arg, src1);
103 read_from_mem(simple_strtoul(src2arg + 1, NULL, 16), src2, len);
105 read_from_env_var(src2arg, src2);
107 result = malloc(len);
111 for (i = 0; i < len; i++)
112 result[i] = src1[i] ^ src2[i];
115 for (i = 0; i < len; i++)
116 result[i] = src1[i] | src2[i];
119 for (i = 0; i < len; i++)
120 result[i] = src1[i] & src2[i];
125 for (i = 0; i < len; i++) {
126 printf("%02x ", result[i]);
138 write_to_mem(destarg + 1, result, len); /* Skip asterisk */
140 write_to_env_var(destarg, result, len);
150 binop, 6, 1, do_binop,
151 "compute binary operation",
152 "op count [*]src1 [*]src2 [[*]dest]\n"
153 " - 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."