0

I am trying to write data to an SD card on an ESP32-WROOM. My function for file creation is:

bool create_file(const char *file_path, const char *data)
{
    if (!file_path || !data) {
        printf("Error: Invalid parameters for file creation\n");
        return false;
    }
    
    char full_path[256];
    int ret = snprintf(full_path, sizeof(full_path), "%s/%s", MOUNT_POINT, file_path);
    if (ret >= sizeof(full_path)) {
        printf("Error: File path too long: %s/%s\n", MOUNT_POINT, file_path);
        return false;
    }

    FILE *file = fopen(full_path, "w");
    if (!file) {
        printf("Failed to create file: %s (errno: %d, %s)\n", 
               full_path, errno, strerror(errno));
        return false;
    }
    
    size_t data_len = strlen(data);
    size_t written = fwrite(data, 1, data_len, file);
    fclose(file);
    
    if (written != data_len) {
        printf("Error: Failed to write complete data to file: %s\n", full_path);
        return false;
    }
    
    printf("File created successfully: %s (%zu bytes)\n", full_path, written);
    return true;
}

The error I get:

Failed to create file: /sdcard/tel_13092025114100.json (errno: 22, Invalid argument)

Observations:

  1. The full path length is less than 100 characters, so not too long.

  2. The same SD card works fine when tested with another project (so card itself is OK).

  3. The same ESP32 device is used (so hardware slot is fine).

  4. My code is working when in internal flash encrypted project ( another project folder ). same code extra only secure boot added then why this error comes?

Environment:

  • Board: ESP32-WROOM

  • Card: SanDisk 16GB

  • SDK: ESP-IDF (please mention your exact version)

  • Config: One project uses external flash encryption (works fine), another uses secure boot + flash encryption (fails).

Question:

Why does SD card file creation (fopen) fail with errno 22 (Invalid argument) only when secure boot + flash encryption are enabled? Is there some extra configuration required in ESP-IDF for SD card use under secure boot + flash encryption?

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.