blob: 6d2df5f3dd4fafc9a865f504038e38a36b9b9545 [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
3#include <common.h>
4#include <command.h>
Simon Glass5e6201b2019-08-01 09:46:51 -06005#include <env.h>
Mario Six0c2b3c72019-01-28 09:43:43 +01006#include <hexdump.h>
Mario Six8a4f6642018-03-28 14:39:18 +02007#include <malloc.h>
8#include <mapmem.h>
9#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
61 addr = simple_strtoul(varname, NULL, 16);
62 buf = map_sysmem(addr, len);
63 memcpy(buf, result, len);
64 unmap_sysmem(buf);
65}
66
67static 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);
140exit:
141 free(result);
142 free(src2);
143 free(src1);
144
145 return 0;
146}
147
148U_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);