Issue Integrating QSPI Flash Memory with WiFi on Arduino: Failed to Mount WiFi Firmware Filesystem

I'm encountering an issue with my Arduino setup where I successfully read SSID and password from a QSPI Flash memory upon power-up, and then use this information to connect to WiFi using WiFi.begin(). However, after reading the data, when the SetUpWiFi method starts, I receive the following error message in the serial monitor:

"Failed to mount the filesystem containing the WiFi firmware.
Usually that means that the WiFi firmware has not been installed yet or was overwritten with another firmware."

This is my code:

void storeAndReadCred(){
    // Deinizializzazione del dispositivo
    uint8_t myCred[] = {69, 77, 95, 71, 85, 69, 83, 84, 32, 101, 109, 95, 103, 117, 101, 115, 116};
    bd_size_t program_size = bd.get_program_size();
    
    size_t buffer_size = 17;
     
    writeBlockDevice(myCred,buffer_size,0);
    
    uint8_t* buffer = new uint8_t[buffer_size];
    
    readBlockDevice(buffer,buffer_size,0);

    printStoredData(reinterpret_cast<char*>(buffer), buffer_size);    
    bd.deinit();

    delete[] buffer;  
}
void writeBlockDevice(uint8_t* data, size_t data_size, size_t offset) {
    bd_size_t erase_size = bd.get_erase_size();
    size_t buffer_size =data_size + bd.get_program_size() - 1;
    buffer_size = buffer_size - (buffer_size % bd.get_program_size());

    uint8_t* buffer = new uint8_t[buffer_size];
    memset(buffer, 0, buffer_size);
    memcpy(buffer, data, data_size);
    
    for(int i=0; i<16; i++){
      Serial.print(buffer[i]);
      Serial.print("  ");
    }
    Serial.println("---");
    int err=bd.erase(0,erase_size);
    Serial.println(err);

    Serial.print("bd.program(buffer, ");
    Serial.print(", ");
    Serial.print(buffer_size);
    Serial.println(")");
    err = bd.program(buffer, 0, buffer_size);
    Serial.print("bd.program -> ");
    if (err == 0){
        Serial.println("Scrittura eseguita con successo");
    }
    delete[] buffer;
}

void readBlockDevice(uint8_t* buffer, size_t buffer_size, size_t offset) {
    int err = bd.read(buffer, offset, buffer_size);
    Serial.print("bd.read -> ");
    if(err==0){
      Serial.println("lettura esguita con successo");
    }
    for (int i=0;i<buffer_size;i++){
      Serial.print(buffer[i]);
      Serial.print(", ");
    }
    //non so per quale motivo ma ogni volta scrive il penultimo carattere errato al posto di s scrive w
    Serial.println(" ");
}
void printStoredData(const char* buffer, size_t buffer_size) {
    Serial.println("--- Stored data ---");
    for(int i=0; i<buffer_size; i++){
      Serial.print(buffer[i]);
    }
    Serial.println();
    for(int i=0; i<buffer_size; i++){
      Serial.print(buffer[i], HEX);
      Serial.print(", ");
    }
    
    for(int i=0; i<8; i++){
      ssid[i]=buffer[i];
    }
    for(int i=0; i<8; i++){
      pass[i] = buffer[buffer_size - 8 + i ];
    }
    Serial.println("\n---");
    Serial.print("Ssid:");
    Serial.println(ssid);
    Serial.print("Password:");
    Serial.println(pass);
    
    SetUpWiFi();
}

I have already tried to load WiFiFirmwareUpdater from the STM32H747_System library, but without success. Additionally, I attempted to use WiFiFirmwareUpdate from the GigaWifiSolver library, as suggested in another thread, but it also did not work.

Could anyone help me figure out how to successfully integrate QSPI Flash memory and WiFi together?

If you need more information, please let me know.

I have found the solution to this problem. The issue was that both the firmware and the data were being saved on the QSPI flash memory, and when I saved the data, it would overwrite the firmware. The solution is to simply partition the memory area to prevent the data from overwriting the firmware.

Here is the corrected code:

#define QSPI_START_ADDR 0x90000000
#define FIRMWARE_PARTITION_SIZE 0x1E00000
#define DATA_PARTITION_SIZE 0x1000
QSPIFBlockDevice bd(QSPI_FLASH1_IO0, QSPI_FLASH1_IO1, QSPI_FLASH1_IO2, QSPI_FLASH1_IO3, QSPI_FLASH1_SCK, QSPI_FLASH1_CSN, QSPIF_POLARITY_MODE_0, MBED_CONF_QSPIF_QSPI_FREQ);
void initializaBlockDevice(){
  int err=bd.init();
  Serial.print("bd->");
  Serial.println(err);
}

void getBlockDeviceGeometry(){
    bd_size_t read_size    = bd.get_read_size();
    bd_size_t program_size = bd.get_program_size();
    bd_size_t erase_size   = bd.get_erase_size();
    bd_size_t size         = bd.size();

    Serial.println("--- Block device geometry ---");
    Serial.print("read_size:    ");
    Serial.print(read_size);
    Serial.println(" B");
    Serial.print("program_size: ");
    Serial.print(program_size);
    Serial.println(" B");
    Serial.print("erase_size:   ");
    Serial.print(erase_size);
    Serial.println(" B");
    Serial.print("size:         ");
    Serial.print(size);
    Serial.println(" B");
    Serial.println("---");
}

void readBlockDevice(uint8_t* buffer, size_t buffer_size, size_t offset) {
    int err = bd.read(buffer, offset, buffer_size);
    Serial.print("bd.read -> ");
    if(err==0){
      Serial.println("lettura esguita con successo");
    }
    for (int i=0;i<buffer_size;i++){
      Serial.print(buffer[i]);
      Serial.print(", ");
    }
    //non so per quale motivo ma ogni volta scrive il penultimo carattere errato al posto di s scrive w
    Serial.println(" ");
}

void writeBlockDevice(uint8_t* data, size_t data_size, uint32_t offset) {
    bd_size_t erase_size = bd.get_erase_size();
    size_t buffer_size =data_size + bd.get_program_size() - 1;
    buffer_size = buffer_size - (buffer_size % bd.get_program_size());

    uint8_t* buffer = new uint8_t[buffer_size];
    memset(buffer, offset, buffer_size);
    memcpy(buffer, data, data_size);
    
    for(int i=0; i<16; i++){
      Serial.print(buffer[i]);
      Serial.print("  ");
    }
    Serial.println("---");
    int err=bd.erase(offset,erase_size);
    Serial.println(err);

    Serial.print("bd.program(buffer, ");
    Serial.print(", ");
    Serial.print(buffer_size);
    Serial.println(")");
    err = bd.program(buffer, offset, buffer_size);
    Serial.print("bd.program -> ");
    if (err == 0){
        Serial.println("Scrittura eseguita con successo");
    }
    delete[] buffer;
}

void printStoredData(const char* buffer, size_t buffer_size) {
    Serial.println("--- Stored data ---");
    for(int i=0; i<buffer_size; i++){
      Serial.print(buffer[i]);
    }
    Serial.println();
    for(int i=0; i<buffer_size; i++){
      Serial.print(buffer[i], HEX);
      Serial.print(", ");
    }
    
    for(int i=0; i<8; i++){
      ssid[i] = buffer[i];
    }
    for(int i=0; i<8; i++){
      pass[i] = buffer[buffer_size - 8 + i ];
    }
    Serial.println("\n---");
    Serial.println("---Risultato Lettura---");
    Serial.print("Ssid:");
    Serial.println(ssid);
    Serial.print("Password:");
    Serial.println(pass);
    bd.deinit();
    SetUpWiFi();
}
uint32_t partitioningFlash(){
  uint32_t firm_start = QSPI_START_ADDR;
  uint32_t firm_end = firm_start + FIRMWARE_PARTITION_SIZE;

  uint32_t app_data_start = firm_end + 1;
  uint32_t app_data_end = app_data_start + DATA_PARTITION_SIZE;

  if(bd.get_erase_size()>(app_data_end - app_data_start)){
    Serial.print("ERRORE nella partizione");
  } 

  return app_data_end;
}
void storeAndReadCred(uint32_t data_start){
    // ho scritto le credenziali con numeri decimali perché scrivendole come stringhe alcune volte può causare errore, infatti il buffer le converte automaticamente in int.
    uint8_t myCred[] = {69, 77, 95, 71, 85, 69, 83, 84, 32, 101, 109, 95, 103, 117, 101, 115, 116};
    bd_size_t program_size = bd.get_program_size();
    
    size_t buffer_size = 17;
     
    writeBlockDevice(myCred,buffer_size,data_start);
    
    uint8_t* buffer = new uint8_t[buffer_size];
    
    readBlockDevice(buffer,buffer_size,data_start);

    printStoredData(reinterpret_cast<char*>(buffer), buffer_size);    

    delete[] buffer;  
}