Skip to content

Commit ec95e06

Browse files
authored
Merge pull request #82 from fpistm/harden
Hardening
2 parents 338d65a + 166f11f commit ec95e06

File tree

8 files changed

+200
-205
lines changed

8 files changed

+200
-205
lines changed

keywords.txt

+6
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,9 @@ FAT_TYPE_FAT12 LITERAL1
4343
FAT_TYPE_FAT16 LITERAL1
4444
FAT_TYPE_FAT32 LITERAL1
4545
FAT_TYPE_UNK LITERAL1
46+
SD_CARD_TYPE_UNK LITERAL1
47+
SD_CARD_TYPE_UKN LITERAL1
48+
SD_CARD_TYPE_SD1 LITERAL1
49+
SD_CARD_TYPE_SD2 LITERAL1
50+
SD_CARD_TYPE_SDHC LITERAL1
51+
SD_CARD_TYPE_SECURED LITERAL1

src/SD.cpp

+57-70
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,12 @@ SDClass SD;
6565
*/
6666
bool SDClass::begin(uint32_t detect, uint32_t level)
6767
{
68+
bool status = false;
6869
/*##-1- Initializes SD IOs #############################################*/
6970
if (_card.init(detect, level)) {
70-
return _fatFs.init();
71+
status = _fatFs.init();
7172
}
72-
return false;
73+
return status;
7374
}
7475

7576
/**
@@ -79,11 +80,12 @@ bool SDClass::begin(uint32_t detect, uint32_t level)
7980
*/
8081
bool SDClass::end(void)
8182
{
83+
bool status = false;
8284
/*##-1- DeInitializes SD IOs ###########################################*/
8385
if (_fatFs.deinit()) {
84-
return _card.deinit();
86+
status = _card.deinit();
8587
}
86-
return false;
88+
return status;
8789
}
8890

8991
/**
@@ -95,11 +97,7 @@ bool SDClass::exists(const char *filepath)
9597
{
9698
FILINFO fno;
9799

98-
if (f_stat(filepath, &fno) != FR_OK) {
99-
return false;
100-
} else {
101-
return true;
102-
}
100+
return (f_stat(filepath, &fno) != FR_OK) ? false : true;
103101
}
104102

105103
/**
@@ -110,11 +108,7 @@ bool SDClass::exists(const char *filepath)
110108
bool SDClass::mkdir(const char *filepath)
111109
{
112110
FRESULT res = f_mkdir(filepath);
113-
if ((res != FR_OK) && (res != FR_EXIST)) {
114-
return false;
115-
} else {
116-
return true;
117-
}
111+
return ((res != FR_OK) && (res != FR_EXIST)) ? false : true;
118112
}
119113

120114
/**
@@ -124,11 +118,7 @@ bool SDClass::mkdir(const char *filepath)
124118
*/
125119
bool SDClass::rmdir(const char *filepath)
126120
{
127-
if (f_unlink(filepath) != FR_OK) {
128-
return false;
129-
} else {
130-
return true;
131-
}
121+
return (f_unlink(filepath) != FR_OK) ? false : true;
132122
}
133123

134124
/**
@@ -184,11 +174,7 @@ File SDClass::open(const char *filepath, uint8_t mode /* = FA_READ */)
184174
*/
185175
bool SDClass::remove(const char *filepath)
186176
{
187-
if (f_unlink(filepath) != FR_OK) {
188-
return false;
189-
} else {
190-
return true;
191-
}
177+
return (f_unlink(filepath) != FR_OK) ? false : true;
192178
}
193179

194180
File SDClass::openRoot(void)
@@ -344,10 +330,7 @@ int File::read()
344330
{
345331
UINT byteread;
346332
int8_t data;
347-
if (f_read(_fil, (void *)&data, 1, (UINT *)&byteread) == FR_OK) {
348-
return data;
349-
}
350-
return -1;
333+
return (f_read(_fil, (void *)&data, 1, (UINT *)&byteread) == FR_OK) ? data : -1;
351334
}
352335

353336
/**
@@ -359,11 +342,7 @@ int File::read()
359342
int File::read(void *buf, size_t len)
360343
{
361344
UINT bytesread;
362-
363-
if (f_read(_fil, buf, len, (UINT *)&bytesread) == FR_OK) {
364-
return bytesread;
365-
}
366-
return -1;
345+
return (f_read(_fil, buf, len, (UINT *)&bytesread) == FR_OK) ? bytesread : -1;
367346
}
368347

369348
/**
@@ -447,15 +426,11 @@ uint32_t File::position()
447426
*/
448427
bool File::seek(uint32_t pos)
449428
{
450-
if (pos > size()) {
451-
return false;
452-
} else {
453-
if (f_lseek(_fil, pos) != FR_OK) {
454-
return false;
455-
} else {
456-
return true;
457-
}
429+
bool status = false;
430+
if (pos <= size()) {
431+
status = (f_lseek(_fil, pos) != FR_OK) ? false : true;
458432
}
433+
return status;
459434
}
460435

461436
/**
@@ -529,10 +504,12 @@ char *File::name()
529504

530505
/**
531506
* @brief Check if the file is directory or normal file
532-
* @retval TRUE if directory else FALSE
507+
* @retval true if directory else false
533508
*/
534509
bool File::isDirectory()
535510
{
511+
// Assume not a directory
512+
bool status = false;
536513
FILINFO fno;
537514
if (_name == NULL) {
538515
Error_Handler();
@@ -542,21 +519,25 @@ bool File::isDirectory()
542519
#else
543520
if (_dir.fs != 0)
544521
#endif
545-
return true;
522+
{
523+
status = true;
524+
}
546525
#if (_FATFS == 68300) || (_FATFS == 80286)
547526
else if (_fil->obj.fs != 0)
548527
#else
549528
else if (_fil->fs != 0)
550529
#endif
551-
return false;
552-
// if not init get info
553-
if (f_stat(_name, &fno) == FR_OK) {
554-
if (fno.fattrib & AM_DIR) {
555-
return true;
530+
{
531+
status = false;
532+
} else {
533+
// if not init get info
534+
if (f_stat(_name, &fno) == FR_OK) {
535+
if (fno.fattrib & AM_DIR) {
536+
status = true;
537+
}
556538
}
557539
}
558-
// Assume not a directory
559-
return false;
540+
return status;
560541
}
561542

562543
File File::openNextFile(uint8_t mode)
@@ -569,35 +550,41 @@ File File::openNextFile(uint8_t mode)
569550
fno.lfname = lfn;
570551
fno.lfsize = sizeof(lfn);
571552
#endif
572-
while (1) {
553+
bool found = false;
554+
File filtmp = File();
555+
while (!found) {
573556
res = f_readdir(&_dir, &fno);
574557
if (res != FR_OK || fno.fname[0] == 0) {
575-
return File(res);
576-
}
577-
if (fno.fname[0] == '.') {
578-
continue;
579-
}
558+
filtmp._res = res;
559+
found = true;
560+
} else {
561+
if (fno.fname[0] == '.') {
562+
continue;
563+
}
580564
#if _USE_LFN && (_FATFS != 68300 && _FATFS != 80286)
581-
fn = *fno.lfname ? fno.lfname : fno.fname;
565+
fn = *fno.lfname ? fno.lfname : fno.fname;
582566
#else
583-
fn = fno.fname;
567+
fn = fno.fname;
584568
#endif
585-
size_t name_len = strlen(_name);
586-
char *fullPath = (char *)malloc(name_len + strlen(fn) + 2);
587-
if (fullPath != NULL) {
588-
// Avoid twice '/'
589-
if ((name_len > 0) && (_name[name_len - 1] == '/')) {
590-
sprintf(fullPath, "%s%s", _name, fn);
569+
size_t name_len = strlen(_name);
570+
char *fullPath = (char *)malloc(name_len + strlen(fn) + 2);
571+
if (fullPath != NULL) {
572+
// Avoid twice '/'
573+
if ((name_len > 0) && (_name[name_len - 1] == '/')) {
574+
sprintf(fullPath, "%s%s", _name, fn);
575+
} else {
576+
sprintf(fullPath, "%s/%s", _name, fn);
577+
}
578+
filtmp = SD.open(fullPath, mode);
579+
free(fullPath);
580+
found = true;
591581
} else {
592-
sprintf(fullPath, "%s/%s", _name, fn);
582+
filtmp._res = FR_NOT_ENOUGH_CORE;
583+
found = true;
593584
}
594-
File filtmp = SD.open(fullPath, mode);
595-
free(fullPath);
596-
return filtmp;
597-
} else {
598-
return File(FR_NOT_ENOUGH_CORE);
599585
}
600586
}
587+
return filtmp;
601588
}
602589

603590
void File::rewindDirectory(void)

src/STM32SD.h

+10-2
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,19 @@ class SDClass {
157157
{
158158
return _fatFs.fatType();
159159
}
160-
160+
/** \return Pointer to SD card object. */
161+
Sd2Card *card()
162+
{
163+
return &_card;
164+
}
165+
/** \return Pointer to FatFs object. */
166+
SdFatFs *fatFs()
167+
{
168+
return &_fatFs;
169+
}
161170
private:
162171
Sd2Card _card;
163172
SdFatFs _fatFs;
164-
165173
};
166174

167175
extern SDClass SD;

src/Sd2Card.cpp

+18-20
Original file line numberDiff line numberDiff line change
@@ -54,41 +54,40 @@ Sd2Card::Sd2Card()
5454

5555
bool Sd2Card::init(uint32_t detect, uint32_t level)
5656
{
57+
bool status = true;
5758
if (detect != SD_DETECT_NONE) {
5859
PinName p = digitalPinToPinName(detect);
5960
if ((p == NC) || \
6061
BSP_SD_DetectPin(p, level) != MSD_OK) {
61-
return false;
62+
status = false;
6263
}
6364
}
6465
#if defined(USE_SD_TRANSCEIVER) && (USE_SD_TRANSCEIVER != 0U)
65-
PinName sd_en = digitalPinToPinName(SD_TRANSCEIVER_EN);
66-
PinName sd_sel = digitalPinToPinName(SD_TRANSCEIVER_SEL);
67-
if (BSP_SD_TransceiverPin(set_GPIO_Port_Clock(STM_PORT(sd_en)),
68-
STM_LL_GPIO_PIN(sd_en),
69-
set_GPIO_Port_Clock(STM_PORT(sd_sel)),
70-
STM_LL_GPIO_PIN(sd_sel)) == MSD_ERROR) {
71-
return false;
66+
if (status == true) {
67+
PinName sd_en = digitalPinToPinName(SD_TRANSCEIVER_EN);
68+
PinName sd_sel = digitalPinToPinName(SD_TRANSCEIVER_SEL);
69+
if (BSP_SD_TransceiverPin(set_GPIO_Port_Clock(STM_PORT(sd_en)),
70+
STM_LL_GPIO_PIN(sd_en),
71+
set_GPIO_Port_Clock(STM_PORT(sd_sel)),
72+
STM_LL_GPIO_PIN(sd_sel)) == MSD_ERROR) {
73+
status = false;
74+
}
7275
}
7376
#endif
74-
if (BSP_SD_Init() == MSD_OK) {
75-
BSP_SD_GetCardInfo(&_SdCardInfo);
76-
return true;
77+
if ((status == true) && (BSP_SD_Init() == MSD_OK)) {
78+
status = BSP_SD_GetCardInfo(&_SdCardInfo);
7779
}
78-
return false;
80+
return status;
7981
}
8082

8183
bool Sd2Card::deinit(void)
8284
{
83-
if (BSP_SD_DeInit() == MSD_OK) {
84-
return true;
85-
}
86-
return false;
85+
return (BSP_SD_DeInit() == MSD_OK) ? true : false;
8786
}
8887

8988
uint8_t Sd2Card::type(void) const
9089
{
91-
uint8_t cardType = SD_CARD_TYPE_UKN;
90+
uint8_t cardType = SD_CARD_TYPE_UNK;
9291
switch (_SdCardInfo.CardType) {
9392
case CARD_SDSC:
9493
switch (_SdCardInfo.CardVersion) {
@@ -99,7 +98,7 @@ uint8_t Sd2Card::type(void) const
9998
cardType = SD_CARD_TYPE_SD2;
10099
break;
101100
default:
102-
cardType = SD_CARD_TYPE_UKN;
101+
cardType = SD_CARD_TYPE_UNK;
103102
}
104103
break;
105104
case CARD_SDHC_SDXC:
@@ -109,8 +108,7 @@ uint8_t Sd2Card::type(void) const
109108
cardType = SD_CARD_TYPE_SECURED;
110109
break;
111110
default:
112-
cardType = SD_CARD_TYPE_UKN;
111+
cardType = SD_CARD_TYPE_UNK;
113112
}
114113
return cardType;
115114
}
116-

src/Sd2Card.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
#include "bsp_sd.h"
4141

4242
// card types to match Arduino definition
43-
#define SD_CARD_TYPE_UKN 0
43+
#define SD_CARD_TYPE_UNK 0
44+
// back compatibility
45+
#define SD_CARD_TYPE_UKN SD_CARD_TYPE_UNK
4446
/** Standard capacity V1 SD card */
4547
#define SD_CARD_TYPE_SD1 1
4648
/** Standard capacity V2 SD card */

src/SdFatFs.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,30 @@
3939

4040
bool SdFatFs::init(void)
4141
{
42-
42+
bool status = false;
4343
/*##-1- Link the SD disk I/O driver ########################################*/
4444
if (FATFS_LinkDriver(&SD_Driver, _SDPath) == 0) {
4545
/*##-2- Register the file system object to the FatFs module ##############*/
4646
if (f_mount(&_SDFatFs, (TCHAR const *)_SDPath, 1) == FR_OK) {
4747
/* FatFs Initialization done */
48-
return true;
48+
status = true;
4949
}
5050
}
51-
return false;
51+
return status;
5252
}
5353

5454
bool SdFatFs::deinit(void)
5555
{
56+
bool status = false;
5657
/*##-1- Unregister the file system object to the FatFs module ##############*/
5758
if (f_unmount((TCHAR const *)_SDPath) == FR_OK) {
5859
/*##-2- Unlink the SD disk I/O driver ####################################*/
5960
if (FATFS_UnLinkDriver(_SDPath) == 0) {
6061
/* FatFs deInitialization done */
61-
return true;
62+
status = true;
6263
}
6364
}
64-
return false;
65+
return status;
6566
}
6667

6768
uint8_t SdFatFs::fatType(void)

0 commit comments

Comments
 (0)