Skip to content

Commit

Permalink
add esp32 p4 stub and target code (#147)
Browse files Browse the repository at this point in the history
* add esp32 p4 stub and target code

* update esp32p4 magic value

* add all listed esp32 p4 magic values
  • Loading branch information
brianignacio5 authored Jul 18, 2024
1 parent d071e7d commit 71a6a29
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/esploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ async function magic2Chip(magic: number): Promise<ROM | null> {
const { ESP8266ROM } = await import("./targets/esp8266.js");
return new ESP8266ROM();
}
case 0x0:
case 0x0addbad0:
case 0x7039ad9: {
const { ESP32P4ROM } = await import("./targets/esp32p4.js");
return new ESP32P4ROM();
}
default:
return null;
}
Expand Down Expand Up @@ -672,6 +678,7 @@ export class ESPLoader {
if (!detecting) {
const chipMagicValue = (await this.readReg(0x40001000)) >>> 0;
this.debug("Chip Magic " + chipMagicValue.toString(16));
this.info("Chip Magic " + chipMagicValue.toString(16));
const chip = await magic2Chip(chipMagicValue);
if (this.chip === null) {
throw new ESPError(`Unexpected CHIP magic value ${chipMagicValue}. Failed to autodetect chip type.`);
Expand Down
222 changes: 222 additions & 0 deletions src/targets/esp32p4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
import { ESPLoader } from "../esploader.js";
import { ESP32ROM } from "./esp32.js";
import ESP32P4_STUB from "./stub_flasher/stub_flasher_32p4.json";

export class ESP32P4ROM extends ESP32ROM {
public CHIP_NAME = "ESP32-P4";
public IMAGE_CHIP_ID = 18;

public IROM_MAP_START = 0x40000000;
public IROM_MAP_END = 0x4c000000;
public DROM_MAP_START = 0x40000000;
public DROM_MAP_END = 0x4c000000;

public BOOTLOADER_FLASH_OFFSET = 0x2000; // First 2 sectors are reserved for FE purposes

public CHIP_DETECT_MAGIC_VALUE = [0x0, 0x0addbad0];

public UART_DATE_REG_ADDR = 0x500ca000 + 0x8c;

public EFUSE_BASE = 0x5012d000;
public EFUSE_BLOCK1_ADDR = this.EFUSE_BASE + 0x044;
public MAC_EFUSE_REG = this.EFUSE_BASE + 0x044;

public SPI_REG_BASE = 0x5008d000; // SPIMEM1
public SPI_USR_OFFS = 0x18;
public SPI_USR1_OFFS = 0x1c;
public SPI_USR2_OFFS = 0x20;
public SPI_MOSI_DLEN_OFFS = 0x24;
public SPI_MISO_DLEN_OFFS = 0x28;
public SPI_W0_OFFS = 0x58;

public EFUSE_RD_REG_BASE = this.EFUSE_BASE + 0x030; // BLOCK0 read base address

public EFUSE_PURPOSE_KEY0_REG = this.EFUSE_BASE + 0x34;
public EFUSE_PURPOSE_KEY0_SHIFT = 24;
public EFUSE_PURPOSE_KEY1_REG = this.EFUSE_BASE + 0x34;
public EFUSE_PURPOSE_KEY1_SHIFT = 28;
public EFUSE_PURPOSE_KEY2_REG = this.EFUSE_BASE + 0x38;
public EFUSE_PURPOSE_KEY2_SHIFT = 0;
public EFUSE_PURPOSE_KEY3_REG = this.EFUSE_BASE + 0x38;
public EFUSE_PURPOSE_KEY3_SHIFT = 4;
public EFUSE_PURPOSE_KEY4_REG = this.EFUSE_BASE + 0x38;
public EFUSE_PURPOSE_KEY4_SHIFT = 8;
public EFUSE_PURPOSE_KEY5_REG = this.EFUSE_BASE + 0x38;
public EFUSE_PURPOSE_KEY5_SHIFT = 12;

public EFUSE_DIS_DOWNLOAD_MANUAL_ENCRYPT_REG = this.EFUSE_RD_REG_BASE;
public EFUSE_DIS_DOWNLOAD_MANUAL_ENCRYPT = 1 << 20;

public EFUSE_SPI_BOOT_CRYPT_CNT_REG = this.EFUSE_BASE + 0x034;
public EFUSE_SPI_BOOT_CRYPT_CNT_MASK = 0x7 << 18;

public EFUSE_SECURE_BOOT_EN_REG = this.EFUSE_BASE + 0x038;
public EFUSE_SECURE_BOOT_EN_MASK = 1 << 20;

public PURPOSE_VAL_XTS_AES256_KEY_1 = 2;
public PURPOSE_VAL_XTS_AES256_KEY_2 = 3;
public PURPOSE_VAL_XTS_AES128_KEY = 4;

public SUPPORTS_ENCRYPTED_FLASH = true;

public FLASH_ENCRYPTED_WRITE_ALIGN = 16;

public MEMORY_MAP = [
[0x00000000, 0x00010000, "PADDING"],
[0x40000000, 0x4c000000, "DROM"],
[0x4ff00000, 0x4ffa0000, "DRAM"],
[0x4ff00000, 0x4ffa0000, "BYTE_ACCESSIBLE"],
[0x4fc00000, 0x4fc20000, "DROM_MASK"],
[0x4fc00000, 0x4fc20000, "IROM_MASK"],
[0x40000000, 0x4c000000, "IROM"],
[0x4ff00000, 0x4ffa0000, "IRAM"],
[0x50108000, 0x50110000, "RTC_IRAM"],
[0x50108000, 0x50110000, "RTC_DRAM"],
[0x600fe000, 0x60100000, "MEM_INTERNAL2"],
];

public UF2_FAMILY_ID = 0x3d308e94;

public EFUSE_MAX_KEY = 5;
public KEY_PURPOSES = {
0: "USER/EMPTY",
1: "ECDSA_KEY",
2: "XTS_AES_256_KEY_1",
3: "XTS_AES_256_KEY_2",
4: "XTS_AES_128_KEY",
5: "HMAC_DOWN_ALL",
6: "HMAC_DOWN_JTAG",
7: "HMAC_DOWN_DIGITAL_SIGNATURE",
8: "HMAC_UP",
9: "SECURE_BOOT_DIGEST0",
10: "SECURE_BOOT_DIGEST1",
11: "SECURE_BOOT_DIGEST2",
12: "KM_INIT_KEY",
};

public TEXT_START = ESP32P4_STUB.text_start;
public ENTRY = ESP32P4_STUB.entry;
public DATA_START = ESP32P4_STUB.data_start;
public ROM_DATA = ESP32P4_STUB.data;
public ROM_TEXT = ESP32P4_STUB.text;

public async getPkgVersion(loader: ESPLoader): Promise<number> {
const numWord = 2;
const addr = this.EFUSE_BLOCK1_ADDR + 4 * numWord;
const registerValue = await loader.readReg(addr);
return (registerValue >> 27) & 0x07;
}

public async getMinorChipVersion(loader: ESPLoader): Promise<number> {
const numWord = 2;
const addr = this.EFUSE_BLOCK1_ADDR + 4 * numWord;
const registerValue = await loader.readReg(addr);
return (registerValue >> 0) & 0x0f;
}

public async getMajorChipVersion(loader: ESPLoader): Promise<number> {
const numWord = 2;
const addr = this.EFUSE_BLOCK1_ADDR + 4 * numWord;
const registerValue = await loader.readReg(addr);
return (registerValue >> 4) & 0x03;
}

public async getChipDescription(loader: ESPLoader): Promise<string> {
const pkgVersion = await this.getPkgVersion(loader);
const chipName = pkgVersion === 0 ? "ESP32-P4" : "unknown ESP32-P4";
const majorRev = await this.getMajorChipVersion(loader);
const minorRev = await this.getMinorChipVersion(loader);
return `${chipName} (revision v${majorRev}.${minorRev})`;
}

public async getChipFeatures(loader: ESPLoader): Promise<string[]> {
return ["High-Performance MCU"];
}

public async getCrystalFreq(loader: ESPLoader): Promise<number> {
return 40; // ESP32P4 XTAL is fixed to 40MHz
}

public async getFlashVoltage(loader: ESPLoader) {
return;
}

public async overrideVddsdio(loader: ESPLoader) {
loader.debug("VDD_SDIO overrides are not supported for ESP32-P4");
}

public async readMac(loader: ESPLoader) {
let mac0 = await loader.readReg(this.MAC_EFUSE_REG);
mac0 = mac0 >>> 0;
let mac1 = await loader.readReg(this.MAC_EFUSE_REG + 4);
mac1 = (mac1 >>> 0) & 0x0000ffff;
const mac = new Uint8Array(6);
mac[0] = (mac1 >> 8) & 0xff;
mac[1] = mac1 & 0xff;
mac[2] = (mac0 >> 24) & 0xff;
mac[3] = (mac0 >> 16) & 0xff;
mac[4] = (mac0 >> 8) & 0xff;
mac[5] = mac0 & 0xff;

return (
this._d2h(mac[0]) +
":" +
this._d2h(mac[1]) +
":" +
this._d2h(mac[2]) +
":" +
this._d2h(mac[3]) +
":" +
this._d2h(mac[4]) +
":" +
this._d2h(mac[5])
);
}

public async getFlashCryptConfig(loader: ESPLoader) {
return; // doesn't exist on ESP32-P4
}

public async getSecureBootEnabled(laoder: ESPLoader) {
const registerValue = await laoder.readReg(this.EFUSE_SECURE_BOOT_EN_REG);
return registerValue & this.EFUSE_SECURE_BOOT_EN_MASK;
}

public async getKeyBlockPurpose(loader: ESPLoader, keyBlock: number) {
if (keyBlock < 0 || keyBlock > this.EFUSE_MAX_KEY) {
loader.debug(`Valid key block numbers must be in range 0-${this.EFUSE_MAX_KEY}`);
return;
}

const regShiftDictionary = [
[this.EFUSE_PURPOSE_KEY0_REG, this.EFUSE_PURPOSE_KEY0_SHIFT],
[this.EFUSE_PURPOSE_KEY1_REG, this.EFUSE_PURPOSE_KEY1_SHIFT],
[this.EFUSE_PURPOSE_KEY2_REG, this.EFUSE_PURPOSE_KEY2_SHIFT],
[this.EFUSE_PURPOSE_KEY3_REG, this.EFUSE_PURPOSE_KEY3_SHIFT],
[this.EFUSE_PURPOSE_KEY4_REG, this.EFUSE_PURPOSE_KEY4_SHIFT],
[this.EFUSE_PURPOSE_KEY5_REG, this.EFUSE_PURPOSE_KEY5_SHIFT],
];
const [reg, shift] = regShiftDictionary[keyBlock];

const registerValue = await loader.readReg(reg);
return (registerValue >> shift) & 0xf;
}

public async isFlashEncryptionKeyValid(loader: ESPLoader) {
const purposes = [];
for (let i = 0; i <= this.EFUSE_MAX_KEY; i++) {
const purpose = await this.getKeyBlockPurpose(loader, i);
purposes.push(purpose);
}
const isXtsAes128Key = purposes.find((p) => p === this.PURPOSE_VAL_XTS_AES128_KEY);
if (typeof isXtsAes128Key !== undefined) {
return true;
}
const isXtsAes256Key1 = purposes.find((p) => p === this.PURPOSE_VAL_XTS_AES256_KEY_1);
const isXtsAes256Key2 = purposes.find((p) => p === this.PURPOSE_VAL_XTS_AES256_KEY_2);
if (typeof isXtsAes256Key1 !== undefined && typeof isXtsAes256Key2 !== undefined) {
return true;
}
return false;
}
}
8 changes: 8 additions & 0 deletions src/targets/stub_flasher/stub_flasher_32p4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"entry": 1341195918,
"text": "QREixCbCBsa3Jw1QEUc3BPVP2Mu3JA1QEwQEANxAkYuR57JAIkSSREEBgoCIQBxAE3X1D4KX3bcBEbenDFBOxoOphwBKyDcJ9U8mylLEBs4izLekDFB9WhMJCQDATBN09D8N4PJAYkQjqDQBQknSRLJJIkoFYYKAi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Q7AIPHKwATjIsBIgRdjNxEQRTN45FHhUtj/4cIkweQDNzIrbwDpw0AItAFSLOH7EA+1oMnirBjc/QADUhCxjrE7/CvxiJHMkg3hfVP4oV8EJOGCgEQEBMFhQKX8M7/54BgNze39U+TCAcBglcDp4iwg6UNAB2MHY8+nLJXI6TosKqLvpUjoL0Ak4cKAZ2NAcWhZ2OX9QBahe/wb9EjoG0BCcTcRJnD409w92PfCwCTB3AMvbeFS7c99k+3jPVPk43NupOMDAHpv+OaC5zcROOHB5yTB4AMqbeDp4sA45AHnO/wD9YJZRMFBXGX8M7/54CgIpfwzv/ngKAnTbIDpMsA4w4EmO/wz9MTBYA+l/DO/+eAgCAClFmy9lBmVNZURlm2WSZalloGW/ZLZkzWTEZNtk0JYYKAAAA=",
"text_start": 1341194240,
"data": "EAD1TwYK8U9WCvFPrgrxT4QL8U/wC/FPngvxT9QI8U9AC/FPgAvxT8IK8U+ECPFP9grxT4QI8U/gCfFPJgrxT1YK8U+uCvFP8gnxTzgJ8U9oCfFP7gnxT0AO8U9WCvFPCA3xTwAO8U/EB/FPJA7xT8QH8U/EB/FPxAfxT8QH8U/EB/FPxAfxT8QH8U/EB/FPpAzxT8QH8U8mDfFPAA7xTw==",
"data_start": 1341533100,
"bss_start": 1341456384
}

0 comments on commit 71a6a29

Please sign in to comment.