blob: 10d91b5dbf2ee9dc8f946becbb054c2a4b71f03a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Mario Six8a4f6642018-03-28 14:39:18 +02002
Mario Six8a4f6642018-03-28 14:39:18 +02003#include <command.h>
Simon Glass5e6201b2019-08-01 09:46:51 -06004#include <env.h>
Mario Six0c2b3c72019-01-28 09:43:43 +01005#include <hexdump.h>
Mario Six8a4f6642018-03-28 14:39:18 +02006#include <malloc.h>
7#include <mapmem.h>
Tom Rini5c1444f2024-04-27 08:10:59 -06008#include <vsprintf.h>
Mario Six8a4f6642018-03-28 14:39:18 +02009#include <linux/ctype.h>
10
11enum {
12 OP_ID_XOR,
13 OP_ID_AND,
14 OP_ID_OR,
15};
16
17void 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 Six3d829722019-01-28 09:43:42 +010031 env_set(varname, str_output);
Mario Six8a4f6642018-03-28 14:39:18 +020032
33 free(str_output);
34}
35
Mario Six8a4f6642018-03-28 14:39:18 +020036void read_from_env_var(char *varname, u8 *result)
37{
38 char *str_value;
39
Mario Six3d829722019-01-28 09:43:42 +010040 str_value = env_get(varname);
Mario Six8a4f6642018-03-28 14:39:18 +020041 if (str_value)
Mario Six0c2b3c72019-01-28 09:43:43 +010042 hex2bin(result, str_value, strlen(str_value) / 2);
Mario Six8a4f6642018-03-28 14:39:18 +020043 else
Mario Six0c2b3c72019-01-28 09:43:43 +010044 hex2bin(result, varname, strlen(varname) / 2);
Mario Six8a4f6642018-03-28 14:39:18 +020045}
46
47void 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
56void write_to_mem(char *varname, u8 *result, ulong len)
57{
58 ulong addr;
59 u8 *buf;
60
Simon Glass3ff49ec2021-07-24 09:03:29 -060061 addr = hextoul(varname, NULL);
Mario Six8a4f6642018-03-28 14:39:18 +020062 buf = map_sysmem(addr, len);
63 memcpy(buf, result, len);
64 unmap_sysmem(buf);
65}
66
Simon Glassed38aef2020-05-10 11:40:03 -060067static int do_binop(struct cmd_tbl *cmdtp, int flag, int argc,
68 char *const argv[])
Mario Six8a4f6642018-03-28 14:39:18 +020069{
70 ulong len;
71 u8 *result, *src1, *src2;
72 char *oparg, *lenarg, *src1arg, *src2arg, *destarg;
73 int i, op;
74
75 if (argc < 5)
76 return CMD_RET_USAGE;
77
78 oparg = argv[1];
79 lenarg = argv[2];
80 src1arg = argv[3];
81 src2arg = argv[4];
82
83 if (!strcmp(oparg, "xor"))
84 op = OP_ID_XOR;
85 else if (!strcmp(oparg, "or"))
86 op = OP_ID_OR;
87 else if (!strcmp(oparg, "and"))
88 op = OP_ID_AND;
89 else
90 return CMD_RET_USAGE;
91
Simon Glassff9b9032021-07-24 09:03:30 -060092 len = dectoul(lenarg, NULL);
Mario Six8a4f6642018-03-28 14:39:18 +020093
94 src1 = malloc(len);
95 src2 = malloc(len);
96
97 if (*src1arg == '*')
Simon Glass3ff49ec2021-07-24 09:03:29 -060098 read_from_mem(hextoul(src1arg + 1, NULL), src1, len);
Mario Six8a4f6642018-03-28 14:39:18 +020099 else
100 read_from_env_var(src1arg, src1);
101
102 if (*src2arg == '*')
Simon Glass3ff49ec2021-07-24 09:03:29 -0600103 read_from_mem(hextoul(src2arg + 1, NULL), src2, len);
Mario Six8a4f6642018-03-28 14:39:18 +0200104 else
105 read_from_env_var(src2arg, src2);
106
107 result = malloc(len);
108
109 switch (op) {
110 case OP_ID_XOR:
111 for (i = 0; i < len; i++)
112 result[i] = src1[i] ^ src2[i];
113 break;
114 case OP_ID_OR:
115 for (i = 0; i < len; i++)
116 result[i] = src1[i] | src2[i];
117 break;
118 case OP_ID_AND:
119 for (i = 0; i < len; i++)
120 result[i] = src1[i] & src2[i];
121 break;
122 }
123
124 if (argc == 5) {
125 for (i = 0; i < len; i++) {
126 printf("%02x ", result[i]);
127 if (i % 16 == 15)
128 puts("\n");
129 }
130 puts("\n");
131
132 goto exit;
133 }
134
135 destarg = argv[5];
136
137 if (*destarg == '*')
138 write_to_mem(destarg + 1, result, len); /* Skip asterisk */
139 else
140 write_to_env_var(destarg, result, len);
141exit:
142 free(result);
143 free(src2);
144 free(src1);
145
146 return 0;
147}
148
149U_BOOT_CMD(
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."
154);