1 /*
2 * Copyright (c) 2012 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23 #include <ctype.h>
24 #include <lk/debug.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <kernel/thread.h>
28 #include <platform.h>
29 #include <lib/cksum.h>
30
31 #include <lk/console_cmd.h>
32
33 static int cmd_crc16(int argc, const console_cmd_args *argv);
34 static int cmd_crc32(int argc, const console_cmd_args *argv);
35 static int cmd_adler32(int argc, const console_cmd_args *argv);
36 static int cmd_cksum_bench(int argc, const console_cmd_args *argv);
37
38 STATIC_COMMAND_START
39 #if LK_DEBUGLEVEL > 0
40 STATIC_COMMAND("crc16", "crc16", &cmd_crc16)
41 STATIC_COMMAND("crc32", "crc32", &cmd_crc32)
42 STATIC_COMMAND("adler32", "adler32", &cmd_adler32)
43 #endif
44 #if LK_DEBUGLEVEL > 1
45 STATIC_COMMAND("bench_cksum", "benchmark the checksum routines", &cmd_cksum_bench)
46 #endif
47 STATIC_COMMAND_END(crc);
48
cmd_crc16(int argc,const console_cmd_args * argv)49 static int cmd_crc16(int argc, const console_cmd_args *argv)
50 {
51 if (argc < 3) {
52 printf("not enough arguments\n");
53 printf("usage: %s <address> <size>\n", argv[0].str);
54 return -1;
55 }
56
57 uint16_t crc = crc16(argv[1].p, argv[2].u);
58
59 printf("0x%hx\n", crc);
60
61 return 0;
62 }
63
cmd_crc32(int argc,const console_cmd_args * argv)64 static int cmd_crc32(int argc, const console_cmd_args *argv)
65 {
66 if (argc < 3) {
67 printf("not enough arguments\n");
68 printf("usage: %s <address> <size>\n", argv[0].str);
69 return -1;
70 }
71
72 uint32_t crc = crc32(0, argv[1].p, argv[2].u);
73
74 printf("0x%x\n", crc);
75
76 return 0;
77 }
78
cmd_adler32(int argc,const console_cmd_args * argv)79 static int cmd_adler32(int argc, const console_cmd_args *argv)
80 {
81 if (argc < 3) {
82 printf("not enough arguments\n");
83 printf("usage: %s <address> <size>\n", argv[0].str);
84 return -1;
85 }
86
87 uint32_t crc = adler32(0, argv[1].p, argv[2].u);
88
89 printf("0x%x\n", crc);
90
91 return 0;
92 }
93
cmd_cksum_bench(int argc,const console_cmd_args * argv)94 static int cmd_cksum_bench(int argc, const console_cmd_args *argv)
95 {
96 #define BUFSIZE 0x1000
97 #define ITER 16384
98 void *buf;
99 bool freebuf;
100
101 if (argc > 1) {
102 buf = argv[1].p;
103 freebuf = false;
104 } else {
105 buf = malloc(BUFSIZE);
106 freebuf = true;
107 }
108
109 if (!buf)
110 return -1;
111
112 lk_bigtime_t t;
113 uint32_t crc;
114
115 printf("buffer at %p, size %u\n", buf, BUFSIZE);
116
117 t = current_time_hires();
118 crc = 0;
119 for (int i = 0; i < ITER; i++) {
120 crc = crc32(crc, buf, BUFSIZE);
121 }
122 t = current_time_hires() - t;
123
124 printf("took %llu usecs to crc32 %d bytes (%lld bytes/sec)\n", t, BUFSIZE * ITER, (BUFSIZE * ITER) * 1000000ULL / t);
125 thread_sleep(500);
126
127 t = current_time_hires();
128 crc = 0;
129 for (int i = 0; i < ITER; i++) {
130 crc = adler32(crc, buf, BUFSIZE);
131 }
132 t = current_time_hires() - t;
133
134 printf("took %llu usecs to adler32 %d bytes (%lld bytes/sec)\n", t, BUFSIZE * ITER, (BUFSIZE * ITER) * 1000000ULL / t);
135
136 if (freebuf)
137 free(buf);
138 return 0;
139 }
140