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