diff --git a/src/SD.cpp b/src/SD.cpp index 423db45..a952a63 100644 --- a/src/SD.cpp +++ b/src/SD.cpp @@ -371,6 +371,18 @@ namespace SDLib { 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 completely erase a card, + // 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 diff --git a/src/SD.h b/src/SD.h index c81a7d3..b1011a5 100644 --- a/src/SD.h +++ b/src/SD.h @@ -106,6 +106,12 @@ namespace SDLib { 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 necessary after this + bool forceEraseCard(); + private: // This is used to determine the mode used to open a file diff --git a/src/utility/Sd2Card.cpp b/src/utility/Sd2Card.cpp index 7cfbe73..6ed26cd 100644 --- a/src/utility/Sd2Card.cpp +++ b/src/utility/Sd2Card.cpp @@ -775,3 +775,83 @@ uint8_t Sd2Card::isBusy(void) { 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