Skip to content

Commit

Permalink
Merge pull request #82 from fpistm/harden
Browse files Browse the repository at this point in the history
Hardening
  • Loading branch information
fpistm authored Feb 5, 2025
2 parents 338d65a + 166f11f commit ec95e06
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 205 deletions.
6 changes: 6 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,9 @@ FAT_TYPE_FAT12 LITERAL1
FAT_TYPE_FAT16 LITERAL1
FAT_TYPE_FAT32 LITERAL1
FAT_TYPE_UNK LITERAL1
SD_CARD_TYPE_UNK LITERAL1
SD_CARD_TYPE_UKN LITERAL1
SD_CARD_TYPE_SD1 LITERAL1
SD_CARD_TYPE_SD2 LITERAL1
SD_CARD_TYPE_SDHC LITERAL1
SD_CARD_TYPE_SECURED LITERAL1
127 changes: 57 additions & 70 deletions src/SD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ SDClass SD;
*/
bool SDClass::begin(uint32_t detect, uint32_t level)
{
bool status = false;
/*##-1- Initializes SD IOs #############################################*/
if (_card.init(detect, level)) {
return _fatFs.init();
status = _fatFs.init();
}
return false;
return status;
}

/**
Expand All @@ -79,11 +80,12 @@ bool SDClass::begin(uint32_t detect, uint32_t level)
*/
bool SDClass::end(void)
{
bool status = false;
/*##-1- DeInitializes SD IOs ###########################################*/
if (_fatFs.deinit()) {
return _card.deinit();
status = _card.deinit();
}
return false;
return status;
}

/**
Expand All @@ -95,11 +97,7 @@ bool SDClass::exists(const char *filepath)
{
FILINFO fno;

if (f_stat(filepath, &fno) != FR_OK) {
return false;
} else {
return true;
}
return (f_stat(filepath, &fno) != FR_OK) ? false : true;
}

/**
Expand All @@ -110,11 +108,7 @@ bool SDClass::exists(const char *filepath)
bool SDClass::mkdir(const char *filepath)
{
FRESULT res = f_mkdir(filepath);
if ((res != FR_OK) && (res != FR_EXIST)) {
return false;
} else {
return true;
}
return ((res != FR_OK) && (res != FR_EXIST)) ? false : true;
}

/**
Expand All @@ -124,11 +118,7 @@ bool SDClass::mkdir(const char *filepath)
*/
bool SDClass::rmdir(const char *filepath)
{
if (f_unlink(filepath) != FR_OK) {
return false;
} else {
return true;
}
return (f_unlink(filepath) != FR_OK) ? false : true;
}

/**
Expand Down Expand Up @@ -184,11 +174,7 @@ File SDClass::open(const char *filepath, uint8_t mode /* = FA_READ */)
*/
bool SDClass::remove(const char *filepath)
{
if (f_unlink(filepath) != FR_OK) {
return false;
} else {
return true;
}
return (f_unlink(filepath) != FR_OK) ? false : true;
}

File SDClass::openRoot(void)
Expand Down Expand Up @@ -344,10 +330,7 @@ int File::read()
{
UINT byteread;
int8_t data;
if (f_read(_fil, (void *)&data, 1, (UINT *)&byteread) == FR_OK) {
return data;
}
return -1;
return (f_read(_fil, (void *)&data, 1, (UINT *)&byteread) == FR_OK) ? data : -1;
}

/**
Expand All @@ -359,11 +342,7 @@ int File::read()
int File::read(void *buf, size_t len)
{
UINT bytesread;

if (f_read(_fil, buf, len, (UINT *)&bytesread) == FR_OK) {
return bytesread;
}
return -1;
return (f_read(_fil, buf, len, (UINT *)&bytesread) == FR_OK) ? bytesread : -1;
}

/**
Expand Down Expand Up @@ -447,15 +426,11 @@ uint32_t File::position()
*/
bool File::seek(uint32_t pos)
{
if (pos > size()) {
return false;
} else {
if (f_lseek(_fil, pos) != FR_OK) {
return false;
} else {
return true;
}
bool status = false;
if (pos <= size()) {
status = (f_lseek(_fil, pos) != FR_OK) ? false : true;
}
return status;
}

/**
Expand Down Expand Up @@ -529,10 +504,12 @@ char *File::name()

/**
* @brief Check if the file is directory or normal file
* @retval TRUE if directory else FALSE
* @retval true if directory else false
*/
bool File::isDirectory()
{
// Assume not a directory
bool status = false;
FILINFO fno;
if (_name == NULL) {
Error_Handler();
Expand All @@ -542,21 +519,25 @@ bool File::isDirectory()
#else
if (_dir.fs != 0)
#endif
return true;
{
status = true;
}
#if (_FATFS == 68300) || (_FATFS == 80286)
else if (_fil->obj.fs != 0)
#else
else if (_fil->fs != 0)
#endif
return false;
// if not init get info
if (f_stat(_name, &fno) == FR_OK) {
if (fno.fattrib & AM_DIR) {
return true;
{
status = false;
} else {
// if not init get info
if (f_stat(_name, &fno) == FR_OK) {
if (fno.fattrib & AM_DIR) {
status = true;
}
}
}
// Assume not a directory
return false;
return status;
}

File File::openNextFile(uint8_t mode)
Expand All @@ -569,35 +550,41 @@ File File::openNextFile(uint8_t mode)
fno.lfname = lfn;
fno.lfsize = sizeof(lfn);
#endif
while (1) {
bool found = false;
File filtmp = File();
while (!found) {
res = f_readdir(&_dir, &fno);
if (res != FR_OK || fno.fname[0] == 0) {
return File(res);
}
if (fno.fname[0] == '.') {
continue;
}
filtmp._res = res;
found = true;
} else {
if (fno.fname[0] == '.') {
continue;
}
#if _USE_LFN && (_FATFS != 68300 && _FATFS != 80286)
fn = *fno.lfname ? fno.lfname : fno.fname;
fn = *fno.lfname ? fno.lfname : fno.fname;
#else
fn = fno.fname;
fn = fno.fname;
#endif
size_t name_len = strlen(_name);
char *fullPath = (char *)malloc(name_len + strlen(fn) + 2);
if (fullPath != NULL) {
// Avoid twice '/'
if ((name_len > 0) && (_name[name_len - 1] == '/')) {
sprintf(fullPath, "%s%s", _name, fn);
size_t name_len = strlen(_name);
char *fullPath = (char *)malloc(name_len + strlen(fn) + 2);
if (fullPath != NULL) {
// Avoid twice '/'
if ((name_len > 0) && (_name[name_len - 1] == '/')) {
sprintf(fullPath, "%s%s", _name, fn);
} else {
sprintf(fullPath, "%s/%s", _name, fn);
}
filtmp = SD.open(fullPath, mode);
free(fullPath);
found = true;
} else {
sprintf(fullPath, "%s/%s", _name, fn);
filtmp._res = FR_NOT_ENOUGH_CORE;
found = true;
}
File filtmp = SD.open(fullPath, mode);
free(fullPath);
return filtmp;
} else {
return File(FR_NOT_ENOUGH_CORE);
}
}
return filtmp;
}

void File::rewindDirectory(void)
Expand Down
12 changes: 10 additions & 2 deletions src/STM32SD.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,19 @@ class SDClass {
{
return _fatFs.fatType();
}

/** \return Pointer to SD card object. */
Sd2Card *card()
{
return &_card;
}
/** \return Pointer to FatFs object. */
SdFatFs *fatFs()
{
return &_fatFs;
}
private:
Sd2Card _card;
SdFatFs _fatFs;

};

extern SDClass SD;
Expand Down
38 changes: 18 additions & 20 deletions src/Sd2Card.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,41 +54,40 @@ Sd2Card::Sd2Card()

bool Sd2Card::init(uint32_t detect, uint32_t level)
{
bool status = true;
if (detect != SD_DETECT_NONE) {
PinName p = digitalPinToPinName(detect);
if ((p == NC) || \
BSP_SD_DetectPin(p, level) != MSD_OK) {
return false;
status = false;
}
}
#if defined(USE_SD_TRANSCEIVER) && (USE_SD_TRANSCEIVER != 0U)
PinName sd_en = digitalPinToPinName(SD_TRANSCEIVER_EN);
PinName sd_sel = digitalPinToPinName(SD_TRANSCEIVER_SEL);
if (BSP_SD_TransceiverPin(set_GPIO_Port_Clock(STM_PORT(sd_en)),
STM_LL_GPIO_PIN(sd_en),
set_GPIO_Port_Clock(STM_PORT(sd_sel)),
STM_LL_GPIO_PIN(sd_sel)) == MSD_ERROR) {
return false;
if (status == true) {
PinName sd_en = digitalPinToPinName(SD_TRANSCEIVER_EN);
PinName sd_sel = digitalPinToPinName(SD_TRANSCEIVER_SEL);
if (BSP_SD_TransceiverPin(set_GPIO_Port_Clock(STM_PORT(sd_en)),
STM_LL_GPIO_PIN(sd_en),
set_GPIO_Port_Clock(STM_PORT(sd_sel)),
STM_LL_GPIO_PIN(sd_sel)) == MSD_ERROR) {
status = false;
}
}
#endif
if (BSP_SD_Init() == MSD_OK) {
BSP_SD_GetCardInfo(&_SdCardInfo);
return true;
if ((status == true) && (BSP_SD_Init() == MSD_OK)) {
status = BSP_SD_GetCardInfo(&_SdCardInfo);
}
return false;
return status;
}

bool Sd2Card::deinit(void)
{
if (BSP_SD_DeInit() == MSD_OK) {
return true;
}
return false;
return (BSP_SD_DeInit() == MSD_OK) ? true : false;
}

uint8_t Sd2Card::type(void) const
{
uint8_t cardType = SD_CARD_TYPE_UKN;
uint8_t cardType = SD_CARD_TYPE_UNK;
switch (_SdCardInfo.CardType) {
case CARD_SDSC:
switch (_SdCardInfo.CardVersion) {
Expand All @@ -99,7 +98,7 @@ uint8_t Sd2Card::type(void) const
cardType = SD_CARD_TYPE_SD2;
break;
default:
cardType = SD_CARD_TYPE_UKN;
cardType = SD_CARD_TYPE_UNK;
}
break;
case CARD_SDHC_SDXC:
Expand All @@ -109,8 +108,7 @@ uint8_t Sd2Card::type(void) const
cardType = SD_CARD_TYPE_SECURED;
break;
default:
cardType = SD_CARD_TYPE_UKN;
cardType = SD_CARD_TYPE_UNK;
}
return cardType;
}

4 changes: 3 additions & 1 deletion src/Sd2Card.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
#include "bsp_sd.h"

// card types to match Arduino definition
#define SD_CARD_TYPE_UKN 0
#define SD_CARD_TYPE_UNK 0
// back compatibility
#define SD_CARD_TYPE_UKN SD_CARD_TYPE_UNK
/** Standard capacity V1 SD card */
#define SD_CARD_TYPE_SD1 1
/** Standard capacity V2 SD card */
Expand Down
11 changes: 6 additions & 5 deletions src/SdFatFs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,30 @@

bool SdFatFs::init(void)
{

bool status = false;
/*##-1- Link the SD disk I/O driver ########################################*/
if (FATFS_LinkDriver(&SD_Driver, _SDPath) == 0) {
/*##-2- Register the file system object to the FatFs module ##############*/
if (f_mount(&_SDFatFs, (TCHAR const *)_SDPath, 1) == FR_OK) {
/* FatFs Initialization done */
return true;
status = true;
}
}
return false;
return status;
}

bool SdFatFs::deinit(void)
{
bool status = false;
/*##-1- Unregister the file system object to the FatFs module ##############*/
if (f_unmount((TCHAR const *)_SDPath) == FR_OK) {
/*##-2- Unlink the SD disk I/O driver ####################################*/
if (FATFS_UnLinkDriver(_SDPath) == 0) {
/* FatFs deInitialization done */
return true;
status = true;
}
}
return false;
return status;
}

uint8_t SdFatFs::fatType(void)
Expand Down
Loading

0 comments on commit ec95e06

Please sign in to comment.