/esp32_spirecovery.ino Secret
Created
April 12, 2022 23:43
Star
You must be signed in to star a gist
Hacky script to use an ESP32 as an emergency BIOS/UEFI recovery flasher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// WARNING! | |
// THIS SCRIPT IMMEDIATELY ERASES ANY CONNECTED SPI FLASH ROMS AFTER STARTUP! | |
// WARNING! | |
// Wiring: | |
// ESP32 CS0 (GPIO 5) - Flash /CS (Pin 1) | |
// ESP32 MISO (GPIO 19) - Flash DO (Pin 2) | |
// ESP32 GPIO 2 - Flash /WP (Pin 3) | |
// ESP32 GND - Flash GND (Pin 4) | |
// ESP32 MOSI (GPIO 23) - Flash DI (Pin 5) | |
// ESP32 V_SPI_CLK (GPIO 35) - Flash CLK (Pin 6) | |
// ESP32 V_SPI_HD (GPIO 21) - Flash /HOLD (Pin 7) | |
// ESP32 3V3 - Flash VCC (Pin 8) | |
#include <Arduino.h> | |
#include <SPIMemory.h> | |
int8_t SPIPins[4] = {-1, -1, -1, 33}; | |
SPIFlash flash; | |
bool getID() { | |
uint32_t JEDEC = flash.getJEDECID(); | |
if (!JEDEC) { | |
Serial.println("No comms. Check wiring. Is chip supported? If unable to fix, raise an issue on Github"); | |
return false; | |
} | |
else { | |
Serial.print("JEDEC ID: 0x"); | |
Serial.println(JEDEC, HEX); | |
Serial.print("Man ID: 0x"); | |
Serial.println(uint8_t(JEDEC >> 16), HEX); | |
Serial.print("Memory ID: 0x"); | |
Serial.println(uint8_t(JEDEC >> 8), HEX); | |
Serial.print("Capacity: "); | |
Serial.println(flash.getCapacity()); | |
Serial.print("Max Pages: "); | |
Serial.println(flash.getMaxPage()); | |
} | |
return true; | |
} | |
uint8_t buffer[4099]; | |
void setup() { | |
Serial.begin(115200); | |
while (!Serial) ; // Wait for Serial monitor to open | |
delay(50); //Time to terminal get connected | |
Serial.print(F("Initialising")); | |
for (uint8_t i = 0; i < 10; ++i) | |
{ | |
Serial.print(F(".")); | |
} | |
Serial.println(); | |
if (flash.error()) { | |
Serial.println(flash.error(VERBOSE)); | |
} | |
// H_SPI_WP (write protect) pin | |
pinMode(2, OUTPUT); | |
digitalWrite(2, HIGH); | |
// V_SPI_HD (hold) pin | |
pinMode(21, OUTPUT); | |
digitalWrite(21, HIGH); | |
flash.begin(); | |
getID(); | |
flash.eraseChip(); | |
Serial.println("E"); | |
uint32_t address = 0; | |
// 8 MiB, change this to your flash size | |
while (address < (8 * 1024 * 1024)) | |
{ | |
Serial.println("W"); | |
for (uint32_t i = 0; i < 4096; i++) | |
{ | |
while (!Serial.available()); | |
buffer[i] = Serial.read(); | |
} | |
flash.writeByteArray(address, buffer, 4096); | |
address += 4096; | |
} | |
} | |
void loop() { | |
Serial.println("done!"); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
looks promising, might test it out