From 5f8942c484fd2f6c046de3aab18b618e2c5f2ff3 Mon Sep 17 00:00:00 2001 From: HoLo85 Date: Mon, 8 Jun 2020 23:32:25 +0200 Subject: [PATCH 1/4] CMD42 and force erase added --- src/SD.cpp | 12 ++++++ src/SD.h | 6 +++ src/utility/Sd2Card.cpp | 81 +++++++++++++++++++++++++++++++++++++++++ src/utility/Sd2Card.h | 3 ++ src/utility/SdInfo.h | 10 +++++ 5 files changed, 112 insertions(+) diff --git a/src/SD.cpp b/src/SD.cpp index 24fcb2e..45c59eb 100644 --- a/src/SD.cpp +++ b/src/SD.cpp @@ -369,6 +369,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 completly 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..e5896ca 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 neccessary 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..00fa72c 100644 --- a/src/utility/Sd2Card.cpp +++ b/src/utility/Sd2Card.cpp @@ -775,3 +775,84 @@ 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 Date: Wed, 30 Jul 2025 20:49:46 +0200 Subject: [PATCH 2/4] refactor code --- src/SD.h | 2 +- src/utility/Sd2Card.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/SD.h b/src/SD.h index e5896ca..92a5e79 100644 --- a/src/SD.h +++ b/src/SD.h @@ -110,7 +110,7 @@ namespace SDLib { 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 - bool forceEraseCard(); + bool forceEraseCard(); private: diff --git a/src/utility/Sd2Card.h b/src/utility/Sd2Card.h index 63fcb8a..f5df3e2 100644 --- a/src/utility/Sd2Card.h +++ b/src/utility/Sd2Card.h @@ -242,8 +242,8 @@ class Sd2Card { uint8_t writeStop(void); uint8_t isBusy(void); bool lockCard(bool setLock, const char *pwd); - bool forceEraseCard(void); - bool getCardStatus(void); + bool forceEraseCard(void); + bool getCardStatus(void); private: uint32_t block_; uint8_t chipSelectPin_; From 056eb385c287982c5afea2bd4c9438a5976f1d04 Mon Sep 17 00:00:00 2001 From: HoLo85 Date: Wed, 30 Jul 2025 21:19:55 +0200 Subject: [PATCH 3/4] add missing CMD16, refactor code --- src/SD.cpp | 4 +- src/utility/Sd2Card.cpp | 125 ++++++++++++++++++++-------------------- src/utility/SdInfo.h | 2 + 3 files changed, 66 insertions(+), 65 deletions(-) diff --git a/src/SD.cpp b/src/SD.cpp index 960e125..5dbeeab 100644 --- a/src/SD.cpp +++ b/src/SD.cpp @@ -373,14 +373,14 @@ namespace SDLib { // protect a card with a password or remove it bool SDClass::lockCard(bool setLock, const char pwd) { - return card.lockCard(setLock, pwd); + return card.lockCard(setLock, pwd); } // !!CAUTION!! call this to completly 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(); + return card.forceEraseCard(); } // this little helper is used to traverse paths diff --git a/src/utility/Sd2Card.cpp b/src/utility/Sd2Card.cpp index 00fa72c..8f7e021 100644 --- a/src/utility/Sd2Card.cpp +++ b/src/utility/Sd2Card.cpp @@ -780,79 +780,78 @@ uint8_t Sd2Card::isBusy(void) { \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 + \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 Date: Wed, 30 Jul 2025 21:23:39 +0200 Subject: [PATCH 4/4] fix typos --- src/SD.cpp | 2 +- src/SD.h | 2 +- src/utility/Sd2Card.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/SD.cpp b/src/SD.cpp index 5dbeeab..a952a63 100644 --- a/src/SD.cpp +++ b/src/SD.cpp @@ -376,7 +376,7 @@ namespace SDLib { return card.lockCard(setLock, pwd); } - // !!CAUTION!! call this to completly erase a card, + // !!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(){ diff --git a/src/SD.h b/src/SD.h index 92a5e79..b1011a5 100644 --- a/src/SD.h +++ b/src/SD.h @@ -109,7 +109,7 @@ namespace SDLib { // 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 + // this will remove the files and filesystem, a reformatting of the card is necessary after this bool forceEraseCard(); private: diff --git a/src/utility/Sd2Card.cpp b/src/utility/Sd2Card.cpp index 8f7e021..6ed26cd 100644 --- a/src/utility/Sd2Card.cpp +++ b/src/utility/Sd2Card.cpp @@ -822,7 +822,7 @@ bool Sd2Card::lockCard(bool setLock, const char *pwd){ } } -/** Completly erase a card +/** Completely erase a card !! 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.