89 lines
2.1 KiB
C
89 lines
2.1 KiB
C
//
|
|
// Created by cfif on 13.06.2024.
|
|
//
|
|
#include "lfs_file_utils.h"
|
|
|
|
#define CHECK_LFS_ERR if (lfs_err != LFS_ERR_OK) return lfs_err;
|
|
|
|
int dumpEbuFile(lfs_t *env, char *filename, void *buf, uint16_t size) {
|
|
lfs_file_t file;
|
|
|
|
int lfs_err = lfs_file_open(env, &file, filename, LFS_O_WRONLY | LFS_O_CREAT);
|
|
CHECK_LFS_ERR
|
|
|
|
lfs_err = lfs_file_rewind(env, &file);
|
|
CHECK_LFS_ERR
|
|
|
|
int writes = lfs_file_write(env, &file, buf, size);
|
|
if (writes != size)
|
|
return -100;
|
|
|
|
lfs_err = lfs_file_close(env, &file);
|
|
CHECK_LFS_ERR
|
|
|
|
return lfs_err;
|
|
}
|
|
|
|
int loadEbuFile(lfs_t *env, char *filename, void *buf, uint16_t size) {
|
|
lfs_file_t file;
|
|
|
|
int lfs_err = lfs_file_open(env, &file, filename, LFS_O_RDONLY);
|
|
CHECK_LFS_ERR
|
|
|
|
int reading = lfs_file_read(env, &file, buf, size);
|
|
if (reading != size)
|
|
return -100;
|
|
|
|
lfs_err = lfs_file_close(env, &file);
|
|
CHECK_LFS_ERR
|
|
|
|
return lfs_err;
|
|
}
|
|
|
|
int32_t getFreeSize(lfs_t *env) {
|
|
lfs_ssize_t size = lfs_fs_size(env);
|
|
lfs_ssize_t countFree = (lfs_ssize_t)env->block_count - size;
|
|
return countFree;
|
|
}
|
|
|
|
uint32_t getBufCrc(uint8_t *buf, uint32_t size) {
|
|
|
|
uint32_t crc = 0;
|
|
|
|
for (uint32_t i = 0; i < size; ++i) {
|
|
crc += buf[i];
|
|
}
|
|
|
|
return crc;
|
|
}
|
|
|
|
uint32_t getFileCrc(lfs_t *env, char *filename, uint32_t size) {
|
|
uint8_t buf[256];
|
|
uint32_t crc = 0;
|
|
|
|
|
|
uint32_t countPartFull = size / sizeof(buf);
|
|
uint32_t countPartTail = size % sizeof(buf);
|
|
|
|
int lfs_err;
|
|
lfs_file_t file;
|
|
|
|
lfs_err = lfs_file_open(env, &file, filename, LFS_O_RDONLY);
|
|
lfs_err = lfs_file_rewind(env, &file);
|
|
|
|
uint32_t pCountPart = 0;
|
|
|
|
for (uint32_t i = 0; i < countPartFull; ++i) {
|
|
int reading = lfs_file_read(env, &file, &buf[pCountPart], countPartFull);
|
|
crc += getBufCrc(buf, countPartFull);
|
|
pCountPart += countPartFull;
|
|
}
|
|
|
|
if (countPartTail) {
|
|
int reading = lfs_file_read(env, &file, &buf[pCountPart], countPartTail);
|
|
crc += getBufCrc(buf, countPartFull);
|
|
}
|
|
lfs_err = lfs_file_close(env, &file);
|
|
|
|
return crc;
|
|
} |