Skip to content

Instantly share code, notes, and snippets.

@Manawyrm
Created April 12, 2022 23:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Manawyrm/ec0a8eb1c0dd0d806e29d169df25e70e to your computer and use it in GitHub Desktop.
Save Manawyrm/ec0a8eb1c0dd0d806e29d169df25e70e to your computer and use it in GitHub Desktop.
Hacky script to use an ESP32 as an emergency BIOS/UEFI recovery flasher
// 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);
}
@maj113
Copy link

maj113 commented Apr 24, 2022

looks promising, might test it out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment