Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/SD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,18 @@
root.close();
}

// protect a card with a password or remove it
bool SDClass::lockCard(bool setLock, const char pwd) {
return card.lockCard(setLock, pwd);
}

// !!CAUTION!! call this to completly erase a card,

Check failure on line 379 in src/SD.cpp

View workflow job for this annotation

GitHub Actions / spellcheck

completly ==> completely
// this will unlock a card but also delete the complete file system,
// reformatting will be required!
bool SDClass::forceEraseCard(){
return card.forceEraseCard();
}

// this little helper is used to traverse paths
SdFile SDClass::getParentDir(const char *filepath, int *index) {
// get parent directory
Expand Down
6 changes: 6 additions & 0 deletions src/SD.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
SdFile root;

// my quick&dirty iterator, should be replaced
SdFile getParentDir(const char *filepath, int *indx);

Check failure on line 67 in src/SD.h

View workflow job for this annotation

GitHub Actions / spellcheck

indx ==> index
public:
// This needs to be called to set up the connection to the SD card
// before other methods are used.
Expand Down Expand Up @@ -106,6 +106,12 @@
return rmdir(filepath.c_str());
}

// lock card with a password
bool lockCard(bool setLock, const char pwd);
// !!CAUTION!! this can be used as last resort if the password of a locked card is unknown,
// this will remove the files and filesystem, a reformatting of the card is neccessary after this

Check failure on line 112 in src/SD.h

View workflow job for this annotation

GitHub Actions / spellcheck

neccessary ==> necessary
bool forceEraseCard();

private:

// This is used to determine the mode used to open a file
Expand Down
81 changes: 81 additions & 0 deletions src/utility/Sd2Card.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -775,3 +775,84 @@

return (b != 0XFF);
}

/** Locks a card with a password
\param[in] setLock true if the card should be locked, false if it should be unlocked
\param[in] pwd password to lock/unlock the card

\return true if the locking was successful
*/
bool Sd2Card::lockCard(bool setLock, const char *pwd){
if(!isBusy()){
int16_t i;
uint8_t pwd_len = sizeof(pwd);
bool locked = false;

if(cardCommand(CMD42, 0) != 0){
return false;
}

spiSend(DATA_START_BLOCK);
if(setLock) {
spiSend(SET_CARD_PWD & 0x07);
}
else{
spiSend(CLEAR_CARD_PWD & 0x07);
}

spiSend(pwd_len);
for(i=0; i<512; i++){
if(i<pwd_len){
spiSend(pwd[i]);
}
else{
spiSend(0xFF);
}
}

locked = getCardStatus();

spiSend(0xFF);
spiSend(0xFF);

i = 0xFFFF;
while(isBusy() && (--i));

return locked;
}
}

/** Completly erase a card

Check failure on line 825 in src/utility/Sd2Card.cpp

View workflow job for this annotation

GitHub Actions / spellcheck

Completly ==> Completely
!! Use with caution !! this removes everything from a card, even the file system.
Use this to remove a lock with an unknown password from a card.

\return true if the erase was successful
*/
bool Sd2Card::forceEraseCard(void) {
// set block size to 512 bytes
cardCommand(CMD16, 1);

// enable
if(cardCommand(CMD42, 0) != 0){
return false;
}

spiSend(DATA_START_BLOCK);
spiSend(FORCE_ERASE);

return true;
}

/** Check status of card lock

\return true if card is locked
*/
bool Sd2Card::getCardStatus(void) {
bool cardOK = true;

if(cardCommand(CMD13, 0) || spiRec()) {
cardOK = false;
}

return cardOK;
}
4 changes: 4 additions & 0 deletions src/utility/Sd2Card.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ class Sd2Card {
uint8_t writeStart(uint32_t blockNumber, uint32_t eraseCount);
uint8_t writeStop(void);
uint8_t isBusy(void);
/** un-/lock card with given password */
bool lockCard(bool setLock, const char *pwd);
bool forceEraseCard(void);
bool getCardStatus(void);
private:
uint32_t block_;
uint8_t chipSelectPin_;
Expand Down
10 changes: 10 additions & 0 deletions src/utility/SdInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ uint8_t const CMD32 = 0X20;
uint8_t const CMD33 = 0X21;
/** ERASE - erase all previously selected blocks */
uint8_t const CMD38 = 0X26;
/** LOCK_CMD - lock card with password */
uint8_t const CMD42 = 0x2A;
/** APP_CMD - escape for application specific command */
uint8_t const CMD55 = 0X37;
/** READ_OCR - read the OCR register of a card */
Expand Down Expand Up @@ -82,6 +84,14 @@ uint8_t const WRITE_MULTIPLE_TOKEN = 0XFC;
uint8_t const DATA_RES_MASK = 0X1F;
/** write data accepted token */
uint8_t const DATA_RES_ACCEPTED = 0X05;
/** mask for setting a card password*/
uint8_t const SET_CARD_PWD = 0x01;
/** mask for removing a card password*/
uint8_t const CLEAR_CARD_PWD = 0x02;
/** mask for locking or unlocking a card*/
uint8_t const ACTIVATE_CARD_LOCK = 0x04;
/** mask to erase the entire card*/
uint8_t const FORCE_ERASE = 0x08;
//------------------------------------------------------------------------------
typedef struct CID {
// byte 0
Expand Down
Loading