Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Gl1tchub authored Nov 5, 2023
1 parent 98529cd commit a2aba63
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 46 deletions.
2 changes: 1 addition & 1 deletion sd_spi/application.fam
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ App(
stack_size=2 * 1024,
fap_category="GPIO",
# Optional values
# fap_version=(0, 1), # (major, minor)
# fap_version=(0, 2), # (major, minor)
fap_icon="sd_spi_app_10px.png", # 10x10 1-bit PNG
fap_description="SD SPI Lock Management",
# fap_weburl="https://github.com/Gl1tchub/Flipperzero-SD-SPI",
Expand Down
106 changes: 63 additions & 43 deletions sd_spi/sd_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
#define sd_spi_debug(...)
#endif

#define LOCK_UNLOCK_ATTEMPS 3
#define SD_CMD_LENGTH 6
#define SD_DUMMY_BYTE 0xFF
#define SD_ANSWER_RETRY_COUNT 8
#define SD_IDLE_RETRY_COUNT 100

#define FLAG_SET(x, y) (((x) & (y)) == (y))
FuriHalSpiBusHandle* furi_hal_sd_spi_handle = NULL;

static bool sd_high_capacity = false;
SdSpiCmdAnswer cmd_answer = {
Expand Down Expand Up @@ -705,12 +707,17 @@ SdSpiStatus sd_init(bool power_reset) {
// disable power and set low on all bus pins
furi_hal_power_disable_external_3_3v();
sd_spi_bus_to_ground();
hal_sd_detect_set_low();
// hal_sd_detect_set_low();

furi_hal_gpio_init_simple(furi_hal_sd_spi_handle->cs, GpioModeOutputOpenDrain);
furi_hal_gpio_write(furi_hal_sd_spi_handle->cs, 0);

furi_delay_ms(250);

// reinit bus and enable power
sd_spi_bus_rise_up();
hal_sd_detect_init();
// hal_sd_detect_init();
furi_hal_sd_presence_init();
furi_hal_power_enable_external_3_3v();
furi_delay_ms(100);
}
Expand All @@ -735,9 +742,10 @@ SdSpiStatus sd_init(bool power_reset) {

status = sd_get_card_state();


furi_hal_sd_spi_handle = NULL;
// furi_hal_spi_release(&furi_hal_spi_bus_handle_sd_slow);
furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
// furi_hal_spi_release(&furi_hal_spi_bus_handle_sd_slow);


// Init sector cache
Expand Down Expand Up @@ -805,34 +813,40 @@ SdSpiStatus sd_set_pwd(char* pwd) {
furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
furi_hal_sd_spi_handle = &furi_hal_spi_bus_handle_external;

response = sd_spi_send_cmd(SD_CMD42_LOCK_UNLOCK, 0, 0xFF, SdSpiCmdAnswerTypeR1);
for(uint8_t i = 0; i < LOCK_UNLOCK_ATTEMPS; i++) {

if(response.r1 == SdSpi_R1_NO_ERROR) {
sd_spi_debug("SD_CMD42_LOCK_UNLOCK R1_NO_ERROR");
uint8_t data[512] = {0xFF};
data[0] = 0x05;
data[1] = strlen(pwd);
for(int i = 0; i < (int)strlen(pwd); i++){
data[i+2] = pwd[i];
}
response = sd_spi_send_cmd(SD_CMD42_LOCK_UNLOCK, 0, 0xFF, SdSpiCmdAnswerTypeR1);
if(response.r1 == SdSpi_R1_NO_ERROR) {
sd_spi_debug("SD_CMD42_LOCK_UNLOCK R1_NO_ERROR");
uint8_t data[512] = {0xFF};
data[0] = 0x05;
data[1] = strlen(pwd);
for(int i = 0; i < (int)strlen(pwd); i++){
data[i+2] = pwd[i];
}

sd_spi_write_byte(SD_TOKEN_START_DATA_SINGLE_BLOCK_WRITE);
sd_spi_write_bytes_dma(data, SD_BLOCK_SIZE);
sd_spi_purge_crc();
sd_spi_write_byte(SD_TOKEN_START_DATA_SINGLE_BLOCK_WRITE);
sd_spi_write_bytes_dma(data, SD_BLOCK_SIZE);
sd_spi_purge_crc();

SdSpiDataResponce data_responce = sd_spi_get_data_response(SD_TIMEOUT_MS);
sd_spi_deselect_card_and_purge();
SdSpiDataResponce data_responce = sd_spi_get_data_response(SD_TIMEOUT_MS);
sd_spi_deselect_card_and_purge();

if(data_responce == SdSpiDataResponceOK) {
sd_spi_debug("SD_CMD42_LOCK_UNLOCK SdSpiDataResponceOK");
if(data_responce == SdSpiDataResponceOK) {
sd_spi_debug("SD_CMD42_LOCK_UNLOCK SdSpiDataResponceOK");

if(sd_get_card_state()==SdSpiStatusOK) {
if(cmd_answer.r2==SdSpi_R2_CARD_LOCKED) { status = SdSpiStatusOK; }
if(sd_get_card_state()==SdSpiStatusOK) {
if(cmd_answer.r2==SdSpi_R2_CARD_LOCKED) { status = SdSpiStatusOK; break; }
else { sd_spi_debug("SD_CMD42_LOCK_UNLOCK CARD UNLOCKED"); }
}
}
else { sd_spi_debug("SD_CMD42_LOCK_UNLOCK SdSpiDataResponceError"); }
}
else { sd_spi_debug("SD_CMD42_LOCK_UNLOCK SdSpiDataResponceError"); }
else { sd_spi_debug("SD_CMD42_LOCK_UNLOCK R1_ERROR"); }

}
else { sd_spi_debug("SD_CMD42_LOCK_UNLOCK R1_ERROR"); }


furi_hal_sd_spi_handle = NULL;
furi_hal_spi_release(&furi_hal_spi_bus_handle_external);

Expand All @@ -848,32 +862,37 @@ SdSpiStatus sd_clr_pwd(char* pwd) {
furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
furi_hal_sd_spi_handle = &furi_hal_spi_bus_handle_external;

response = sd_spi_send_cmd(SD_CMD42_LOCK_UNLOCK, 0, 0xFF, SdSpiCmdAnswerTypeR1);
if(response.r1 == SdSpi_R1_NO_ERROR) {
sd_spi_debug("SD_CMD42_LOCK_UNLOCK R1_NO_ERROR");
uint8_t data[512] = {0xFF};
data[0] = 0x02;
data[1] = strlen(pwd);
for(int i = 0; i < (int)strlen(pwd); i++){
data[i+2] = pwd[i];
}
sd_spi_write_byte(SD_TOKEN_START_DATA_SINGLE_BLOCK_WRITE);
sd_spi_write_bytes_dma(data, SD_BLOCK_SIZE);
sd_spi_purge_crc();
for(uint8_t i = 0; i < LOCK_UNLOCK_ATTEMPS; i++) {

SdSpiDataResponce data_responce = sd_spi_get_data_response(SD_TIMEOUT_MS);
sd_spi_deselect_card_and_purge();
response = sd_spi_send_cmd(SD_CMD42_LOCK_UNLOCK, 0, 0xFF, SdSpiCmdAnswerTypeR1);
if(response.r1 == SdSpi_R1_NO_ERROR) {
sd_spi_debug("SD_CMD42_LOCK_UNLOCK R1_NO_ERROR");
uint8_t data[512] = {0xFF};
data[0] = 0x02;
data[1] = strlen(pwd);
for(int i = 0; i < (int)strlen(pwd); i++){
data[i+2] = pwd[i];
}

if(data_responce == SdSpiDataResponceOK) {
sd_spi_debug("SD_CMD42_LOCK_UNLOCK SdSpiDataResponceOK");
sd_spi_write_byte(SD_TOKEN_START_DATA_SINGLE_BLOCK_WRITE);
sd_spi_write_bytes_dma(data, SD_BLOCK_SIZE);
sd_spi_purge_crc();

if(sd_get_card_state()==SdSpiStatusOK) {
if(cmd_answer.r2==SdSpi_R2_NO_ERROR) { status = SdSpiStatusOK; }
SdSpiDataResponce data_responce = sd_spi_get_data_response(SD_TIMEOUT_MS);
sd_spi_deselect_card_and_purge();

if(data_responce == SdSpiDataResponceOK) {
sd_spi_debug("SD_CMD42_LOCK_UNLOCK SdSpiDataResponceOK");

if(sd_get_card_state()==SdSpiStatusOK) {
if(cmd_answer.r2==SdSpi_R2_NO_ERROR) { status = SdSpiStatusOK; break; }
}
}
else { sd_spi_debug("SD_CMD42_LOCK_UNLOCK SdSpiDataResponceError"); }
}
else { sd_spi_debug("SD_CMD42_LOCK_UNLOCK SdSpiDataResponceError"); }
else { sd_spi_debug("SD_CMD42_LOCK_UNLOCK R1_ERROR"); }

}
else { sd_spi_debug("SD_CMD42_LOCK_UNLOCK R1_ERROR"); }

furi_hal_sd_spi_handle = NULL;
furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
Expand All @@ -887,6 +906,7 @@ SdSpiStatus sd_force_erase(void) {
furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
furi_hal_sd_spi_handle = &furi_hal_spi_bus_handle_external;


sd_spi_debug("SD_CMD16_SET_BLOCKLEN 1");
sd_spi_send_cmd(SD_CMD16_SET_BLOCKLEN, 1, 0xFF, SdSpiCmdAnswerTypeR1);
sd_spi_deselect_card_and_purge();
Expand Down
2 changes: 1 addition & 1 deletion sd_spi/sd_spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#define SD_TIMEOUT_MS 500//(1000)
#define SD_BLOCK_SIZE 512

#define VERSION_APP "0.1"
#define VERSION_APP "0.2"
#define DEVELOPED " "
#define GITHUB "github.com/Gl1tchub/Flipperzero-SD-SPI"

Expand Down
9 changes: 8 additions & 1 deletion sd_spi/sd_spi_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include <gui/modules/widget.h>




#include "sd_spi.h"

#define TAG "sd-spi-app"
Expand Down Expand Up @@ -235,13 +237,15 @@ void text_input_done_callback(void* context){
storage_file_close(file);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);

scene_manager_previous_scene(app->scene_manager);
}

/* App Scene Select Password */
void app_scene_on_enter_password(void* context) {
FURI_LOG_T(TAG, "app_scene_on_enter_password");
SDSPIApp* app = context;
// TextInput* text_input = app->text_input;
text_input_set_header_text(app->text_input,"Enter password");
text_input_set_validator(app->text_input, text_input_validator, context);
text_input_set_result_callback(
Expand All @@ -262,7 +266,9 @@ bool app_scene_on_event_password(void* context, SceneManagerEvent event) {
void app_scene_on_exit_password(void* context) {
FURI_LOG_T(TAG, "app_scene_on_exit_password");
SDSPIApp* app = context;
text_input_reset(app->text_input);
UNUSED(app);
// text_input_reset(app->text_input);
// text_input_set_validator(app->text_input, NULL, NULL);
}

/* App Scene Confirm SD Force Erase */
Expand Down Expand Up @@ -485,6 +491,7 @@ void app_view_dispatcher_init(SDSPIApp* app) {
else{
FURI_LOG_E(TAG, "File pwd not found");
}
FURI_LOG_E(TAG, "storage_file_free");
storage_file_free(file);
}
furi_string_free(path);
Expand Down

0 comments on commit a2aba63

Please sign in to comment.