Skip to content

Commit

Permalink
add FLASH_ENCRYPTED_WRITE_ALIGN check
Browse files Browse the repository at this point in the history
  • Loading branch information
brianignacio5 committed May 31, 2024
1 parent fd04829 commit 2953127
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 3 deletions.
69 changes: 66 additions & 3 deletions src/esploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ export class ESPLoader {

if (!detecting) {
try {
const chipMagicValue = (await this.readReg(0x40001000)) >>> 0;
const chipMagicValue = (await this.readReg(this.CHIP_DETECT_MAGIC_REG_ADDR)) >>> 0;
this.debug("Chip Magic " + chipMagicValue.toString(16));
const chip = await magic2Chip(chipMagicValue);
if (this.chip === null) {
Expand Down Expand Up @@ -1418,6 +1418,46 @@ export class ESPLoader {
}
}

const encryptedFiles = options.fileArray.filter((f) => f.encrypted);

if (encryptedFiles && encryptedFiles.length > 0) {
let doWrite = true;
for (let f of encryptedFiles) {
if (f.encrypted && this.chip.SUPPORTS_ENCRYPTED_FLASH && f.address % this.chip.FLASH_ENCRYPTED_WRITE_ALIGN) {
doWrite = false;
this.info(`File at address ${f.address} is not %d byte aligned, can't flash encrypted`);
}
}
if (!doWrite) {
let errMsg =
"Can't perform encrypted flash write,\n" + "consult Flash Encryption documentation for more information";
this.debug(errMsg);
throw new ESPError(errMsg);
}
} else {
if (this.chip.CHIP_NAME !== "ESP32" && this.secureDownloadMode && this) {
throw new ESPError(
"WARNING: Detected flash encryption and " +
"secure download mode enabled.\n" +
"Flashing plaintext binary may brick your device! ",
);
}

// TO DO
// Add esp.get_security_info
// esp.get_encrypted_download_disabled()
// and esp.get_flash_encryption_enabled()

if (!this.secureDownloadMode) {
throw new ESPError(
"WARNING: Detected flash encryption enabled and " +
"download manual encrypt disabled.\n" +
"Flashing plaintext binary may brick your device! " +
"Use --force to override the warning.",
);
}
}

if (this.IS_STUB === true && options.eraseAll === true) {
await this.eraseFlash();
}
Expand Down Expand Up @@ -1563,11 +1603,34 @@ export class ESPLoader {
this.info("Detected flash size: " + this.DETECTED_FLASH_SIZES[flidLowbyte]);
}

async getFlashSize() {
async detectFlashSize(options?: FlashOptions) {
this.debug("flash_size");
if (this.secureDownloadMode) {
if (options && options.flashSize === "detect") {
const errMsg =
"Detecting flash size is not supported in secure download mode. Need to manually specify flash size.";
this.debug(errMsg);
throw new ESPError(errMsg);
} else {
return;
}
}
const flashid = await this.readFlashId();
const flidLowbyte = (flashid >> 16) & 0xff;
return this.DETECTED_FLASH_SIZES_NUM[flidLowbyte];
let flashSize = this.DETECTED_FLASH_SIZES[flidLowbyte];

if (options && options.flashSize === "detect") {
if (!flashSize) {
this.info(
`WARNING: Could not auto-detect Flash size. FlashID: ${flashid}, SizeId: ${flashSize}. Defaulting to 4MB.`,
);
flashSize = "4MB";
} else {
this.info(`Auto-detected Flash size: ${flashSize}`);
}
options.flashSize = flashSize;
}
return flashSize;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/targets/esp32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class ESP32ROM extends ROM {
public ROM_TEXT = ESP32_STUB.text;

public SUPPORTS_ENCRYPTED_FLASH = false;
public FLASH_ENCRYPTED_WRITE_ALIGN = 32;

public async readEfuse(loader: ESPLoader, offset: number): Promise<number> {
const addr = this.EFUSE_RD_REG_BASE + 4 * offset;
Expand Down
1 change: 1 addition & 0 deletions src/targets/esp32c3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class ESP32C3ROM extends ROM {
public ROM_TEXT = ESP32C3_STUB.text;

public SUPPORTS_ENCRYPTED_FLASH = true;
public FLASH_ENCRYPTED_WRITE_ALIGN = 16;

public async getPkgVersion(loader: ESPLoader): Promise<number> {
const numWord = 3;
Expand Down
1 change: 1 addition & 0 deletions src/targets/esp32c6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class ESP32C6ROM extends ROM {
public ROM_TEXT = ESP32C6_STUB.text;

public SUPPORTS_ENCRYPTED_FLASH = true;
public FLASH_ENCRYPTED_WRITE_ALIGN = 16;

public async getPkgVersion(loader: ESPLoader) {
const numWord = 3;
Expand Down
1 change: 1 addition & 0 deletions src/targets/esp32h2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class ESP32H2ROM extends ROM {
public ROM_TEXT = ESP32H2_STUB.text;

public SUPPORTS_ENCRYPTED_FLASH = true;
public FLASH_ENCRYPTED_WRITE_ALIGN = 16;

public async getChipDescription(loader: ESPLoader) {
return this.CHIP_NAME;
Expand Down
1 change: 1 addition & 0 deletions src/targets/esp32s2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class ESP32S2ROM extends ROM {
public ROM_TEXT = ESP32S2_STUB.text;

public SUPPORTS_ENCRYPTED_FLASH = true;
public FLASH_ENCRYPTED_WRITE_ALIGN = 16;

public async getPkgVersion(loader: ESPLoader): Promise<number> {
const numWord = 3;
Expand Down
1 change: 1 addition & 0 deletions src/targets/esp32s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class ESP32S3ROM extends ROM {
public ROM_TEXT = ESP32S3_STUB.text;

public SUPPORTS_ENCRYPTED_FLASH = true;
public FLASH_ENCRYPTED_WRITE_ALIGN = 16;

public async getChipDescription(loader: ESPLoader) {
return "ESP32-S3";
Expand Down
1 change: 1 addition & 0 deletions src/targets/esp8266.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export class ESP8266ROM extends ROM {
public ROM_TEXT = ESP8266_STUB.text;

public SUPPORTS_ENCRYPTED_FLASH = false;
public FLASH_ENCRYPTED_WRITE_ALIGN = 16;

public async readEfuse(loader: ESPLoader, offset: number): Promise<number> {
const addr = this.EFUSE_RD_REG_BASE + 4 * offset;
Expand Down
1 change: 1 addition & 0 deletions src/targets/rom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,5 @@ export abstract class ROM {
// abstract XTAL_CLK_DIVIDER: number; //esp32

abstract SUPPORTS_ENCRYPTED_FLASH: boolean;
abstract FLASH_ENCRYPTED_WRITE_ALIGN: number;
}

0 comments on commit 2953127

Please sign in to comment.