1 /*
2  * Copyright (c) 2022 Meta
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <errno.h>
8 #include <stdbool.h>
9 #include <stdint.h>
10 #include <stdlib.h>
11 #ifdef CONFIG_ARCH_POSIX
12 #include <unistd.h>
13 #else
14 #include <zephyr/posix/unistd.h>
15 #endif
16 
17 #include <zephyr/kernel.h>
18 #include <zephyr/shell/shell.h>
19 #include <zephyr/sys/crc.h>
20 
21 static const char *const crc_types[] = {
22 	[CRC4] = "4",
23 	[CRC4_TI] = "4_ti",
24 	[CRC7_BE] = "7_be",
25 	[CRC8] = "8",
26 	[CRC8_CCITT] = "8_ccitt",
27 	[CRC16] = "16",
28 	[CRC16_ANSI] = "16_ansi",
29 	[CRC16_CCITT] = "16_ccitt",
30 	[CRC16_ITU_T] = "16_itu_t",
31 	[CRC32_C] = "32_c",
32 	[CRC32_IEEE] = "32_ieee",
33 };
34 
string_to_crc_type(const char * s)35 static int string_to_crc_type(const char *s)
36 {
37 	int i;
38 
39 	for (i = 0; i < ARRAY_SIZE(crc_types); ++i) {
40 		if (strcmp(s, crc_types[i]) == 0) {
41 			return i;
42 		}
43 	}
44 
45 	return -1;
46 }
47 
usage(const struct shell * sh)48 static void usage(const struct shell *sh)
49 {
50 	size_t i;
51 
52 	shell_print(sh, "crc [options..] <address> <size>");
53 	shell_print(sh, "options:");
54 	shell_print(sh, "-f         This is the first packet");
55 	shell_print(sh, "-l         This is the last packet");
56 	shell_print(sh, "-p <poly>  Use polynomial 'poly'");
57 	shell_print(sh, "-r         Reflect");
58 	shell_print(sh, "-s <seed>  Use 'seed' as the initial value");
59 	shell_print(sh, "-t <type>  Compute the CRC described by 'type'");
60 	shell_print(sh, "Note: some options are only useful for certain CRCs");
61 	shell_print(sh, "CRC Types:");
62 	for (i = 0; i < ARRAY_SIZE(crc_types); ++i) {
63 		shell_print(sh, "%s", crc_types[i]);
64 	}
65 }
66 
cmd_crc(const struct shell * sh,size_t argc,char ** argv)67 static int cmd_crc(const struct shell *sh, size_t argc, char **argv)
68 {
69 	int rv;
70 	size_t size = -1;
71 	bool last = false;
72 	uint32_t poly = 0;
73 	bool first = false;
74 	uint32_t seed = 0;
75 	bool reflect = false;
76 	void *addr = (void *)-1;
77 	enum crc_type type = CRC32_IEEE;
78 
79 	optind = 1;
80 
81 	while ((rv = getopt(argc, argv, "fhlp:rs:t:")) != -1) {
82 		switch (rv) {
83 		case 'f':
84 			first = true;
85 			break;
86 		case 'h':
87 			usage(sh);
88 			return 0;
89 		case 'l':
90 			last = true;
91 			break;
92 		case 'p':
93 			poly = (size_t)strtoul(optarg, NULL, 16);
94 			if (poly == 0 && errno == EINVAL) {
95 				shell_error(sh, "invalid seed '%s'", optarg);
96 				return -EINVAL;
97 			}
98 			break;
99 		case 'r':
100 			reflect = true;
101 			break;
102 		case 's':
103 			seed = (size_t)strtoul(optarg, NULL, 16);
104 			if (seed == 0 && errno == EINVAL) {
105 				shell_error(sh, "invalid seed '%s'", optarg);
106 				return -EINVAL;
107 			}
108 			break;
109 		case 't':
110 			type = string_to_crc_type(optarg);
111 			if (type == -1) {
112 				shell_error(sh, "invalid type '%s'", optarg);
113 				return -EINVAL;
114 			}
115 			break;
116 		case '?':
117 		default:
118 			usage(sh);
119 			return -EINVAL;
120 		}
121 	}
122 
123 	if (optind + 2 > argc) {
124 		shell_error(sh, "'address' and 'size' arguments are mandatory");
125 		usage(sh);
126 		return -EINVAL;
127 	}
128 
129 	addr = (void *)strtoul(argv[optind], NULL, 16);
130 	if (addr == 0 && errno == EINVAL) {
131 		shell_error(sh, "invalid address '%s'", argv[optind]);
132 		return -EINVAL;
133 	}
134 
135 	size = (size_t)strtoul(argv[optind + 1], NULL, 0);
136 	if (size == 0 && errno == EINVAL) {
137 		shell_error(sh, "invalid size '%s'", argv[optind + 1]);
138 		return -EINVAL;
139 	}
140 
141 	shell_print(sh, "0x%x", crc_by_type(type, addr, size, seed, poly, reflect, first, last));
142 
143 	return 0;
144 }
145 
146 SHELL_CMD_ARG_REGISTER(crc, NULL, NULL, cmd_crc, 0, 12);
147