added crc32 command to usb shell (#1911)

This commit is contained in:
Bernd Herzog 2024-02-17 10:38:09 +01:00 committed by GitHub
parent ed834e3553
commit 27dc37713b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 2 deletions

View File

@ -26,6 +26,8 @@
#include "string_format.hpp" #include "string_format.hpp"
#include <cstring> #include <cstring>
#include "crc.hpp"
static File* shell_file = nullptr; static File* shell_file = nullptr;
static bool report_on_error(BaseSequentialStream* chp, File::Error& error) { static bool report_on_error(BaseSequentialStream* chp, File::Error& error) {
@ -371,3 +373,34 @@ void cmd_sd_write_binary(BaseSequentialStream* chp, int argc, char* argv[]) {
chprintf(chp, "ok\r\n"); chprintf(chp, "ok\r\n");
} }
void cmd_sd_crc32(BaseSequentialStream* chp, int argc, char* argv[]) {
if (argc != 1) {
chprintf(chp, "usage: crc32 <path>\r\n");
return;
}
auto path = path_from_string8(argv[0]);
File* crc_file = new File();
auto error = crc_file->open(path, true, false);
if (report_on_error(chp, error)) return;
uint8_t buffer[64];
CRC<32> crc{0x04c11db7, 0xffffffff, 0xffffffff};
while (true) {
auto bytes_read = crc_file->read(buffer, 64);
if (report_on_error(chp, bytes_read)) return;
if (bytes_read.value() > 0) {
crc.process_bytes((void*)buffer, bytes_read.value());
}
if (64 != bytes_read.value()) {
chprintf(chp, "CRC32: 0x%08X\r\n", crc.checksum());
return;
}
}
delete crc_file;
}

View File

@ -42,6 +42,7 @@ void cmd_sd_read(BaseSequentialStream* chp, int argc, char* argv[]);
void cmd_sd_read_binary(BaseSequentialStream* chp, int argc, char* argv[]); void cmd_sd_read_binary(BaseSequentialStream* chp, int argc, char* argv[]);
void cmd_sd_write(BaseSequentialStream* chp, int argc, char* argv[]); void cmd_sd_write(BaseSequentialStream* chp, int argc, char* argv[]);
void cmd_sd_write_binary(BaseSequentialStream* chp, int argc, char* argv[]); void cmd_sd_write_binary(BaseSequentialStream* chp, int argc, char* argv[]);
void cmd_sd_crc32(BaseSequentialStream* chp, int argc, char* argv[]);
static std::filesystem::path path_from_string8(char* path) { static std::filesystem::path path_from_string8(char* path) {
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv; std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
@ -63,5 +64,6 @@ static std::filesystem::path path_from_string8(char* path) {
{"fread", cmd_sd_read}, \ {"fread", cmd_sd_read}, \
{"frb", cmd_sd_read_binary}, \ {"frb", cmd_sd_read_binary}, \
{"fwrite", cmd_sd_write}, \ {"fwrite", cmd_sd_write}, \
{"fwb", cmd_sd_write_binary} {"fwb", cmd_sd_write_binary}, \
{"crc32", cmd_sd_crc32}
// clang-format on // clang-format on