mirror of
https://github.com/siemens-mobile-hacks/node-sie-serial.git
synced 2026-11-25 17:17:17 +00:00
small fixes + more examples
This commit is contained in:
parent
46cf03ae70
commit
87665a3f61
5 changed files with 332 additions and 26 deletions
171
examples/dwd-apoxi-patch-mmu.ts
Normal file
171
examples/dwd-apoxi-patch-mmu.ts
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
import { DWD } from "../src/index.js";
|
||||
import fs from "fs";
|
||||
import { openPort } from "./utils.js";
|
||||
import { parseArgs } from "node:util";
|
||||
import { sprintf } from "sprintf-js";
|
||||
import { retryAsyncOnError } from "../src/utils.js";
|
||||
import { loadELF } from "@sie-js/creampie";
|
||||
|
||||
const TCM_START = 0xFFFF0000;
|
||||
const PRAM_IRQ_HANDLER = TCM_START + 0x38;
|
||||
const BOOT_MODE = 0xA000000C;
|
||||
|
||||
const EBU_ADDRSEL1 = 0xF0000088;
|
||||
|
||||
enum PatchResponseCode {
|
||||
SUCCESS = 0,
|
||||
BOOT_ALREADY_OPEN = -1,
|
||||
UNKNOWN_FLASH = -2,
|
||||
FLASH_BUSY = -3,
|
||||
ERASE_ERROR = -4,
|
||||
PROGRAM_ERROR = -5,
|
||||
ADDR_NOT_ALIGNED = -6,
|
||||
FLASH_REGION_NOT_FOUND = -7,
|
||||
FLASH_REGION_TOO_BIG = -8,
|
||||
INVALID_FLASH_REGIONS = -9,
|
||||
INVALID_FLASH_REGION_COUNT = -10,
|
||||
UNSUPPORTED_FLASH = -11,
|
||||
FLASH_NOT_FOUND = -12,
|
||||
UNKNOWN = -13
|
||||
}
|
||||
|
||||
const { values: argv } = parseArgs({
|
||||
options: {
|
||||
port: {
|
||||
type: "string",
|
||||
default: "/dev/ttyACM0"
|
||||
},
|
||||
key: {
|
||||
type: "string",
|
||||
default: "panasonic"
|
||||
},
|
||||
help: {
|
||||
type: "boolean",
|
||||
short: "h",
|
||||
default: false
|
||||
},
|
||||
usage: {
|
||||
type: "boolean",
|
||||
default: false
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (argv.help || argv.usage) {
|
||||
console.log(`USAGE: dwd-apoxi-unlock-boot.js --port /dev/ttyACM0`);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
const port = await openPort(argv.port, 115200);
|
||||
await port.open();
|
||||
|
||||
const dwd = new DWD(port);
|
||||
|
||||
dwd.setKeys(argv.key);
|
||||
await dwd.connect();
|
||||
await loadPatch(dwd, fs.readFileSync("/home/azq2/dev/sie/pmb887x-emu/bsp/examples/apoxi_patch_mmu/build/app.elf"));
|
||||
await port.close();
|
||||
|
||||
async function loadPatch(dwd: DWD, patchElf: Buffer, minRamBlockSize: number = 256 * 1024) {
|
||||
const addrsel = (await dwd.readMemory(EBU_ADDRSEL1, 4)).buffer.readUInt32LE(0);
|
||||
const ramSize = (1 << (27 - ((addrsel & 0xF0) >> 4)));
|
||||
const ramAddr = Number((BigInt(addrsel) & 0xFFFF0000n));
|
||||
|
||||
const { loadELF } = await import("@sie-js/creampie");
|
||||
|
||||
const bootMode = (await dwd.readMemory(BOOT_MODE, 4)).buffer.readUInt32LE(0);
|
||||
console.log(sprintf("Boot mode: %08X", bootMode));
|
||||
|
||||
console.log(sprintf("RAM: %08X, %d MB", ramAddr, ramSize / 1024 / 1024));
|
||||
console.log("Searching for an empty RAM block... (this may take a while)");
|
||||
|
||||
let emptyRamBlock = 0;
|
||||
for (let i = ramAddr; i < ramAddr + ramSize; i += minRamBlockSize) {
|
||||
if ((i % (1024 * 1024)) == 0)
|
||||
console.log(sprintf("RAM scan progress: %d MB / %d MB", (i - ramAddr) / 1024 / 1024, ramSize / 1024 / 1024));
|
||||
|
||||
const blockStart = await dwd.readMemory(i, 230);
|
||||
if (blockStart.buffer.every((v) => v == 0)) {
|
||||
const fullBlock = await dwd.readMemory(i, minRamBlockSize);
|
||||
if (fullBlock.buffer.every((v) => v == 0)) {
|
||||
emptyRamBlock = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!emptyRamBlock)
|
||||
throw new Error("Empty RAM block not found!");
|
||||
|
||||
console.log(sprintf("Found empty RAM block: %08X", emptyRamBlock));
|
||||
|
||||
const elf = loadELF(emptyRamBlock, patchElf);
|
||||
for (let i = 0; i < 30; i++) {
|
||||
await dwd.writeMemory(emptyRamBlock, elf.image);
|
||||
const check = await dwd.readMemory(emptyRamBlock, elf.image.length);
|
||||
if (check.buffer.toString("hex") != elf.image.toString("hex")) {
|
||||
console.log(check.buffer.toString("hex"));
|
||||
console.log(elf.image.toString("hex"));
|
||||
throw new Error("Payload is corrupted.");
|
||||
}
|
||||
|
||||
console.log(sprintf("Patcher entry: %08X", elf.entry));
|
||||
|
||||
const PATCHER_ADDR = elf.entry;
|
||||
const PARAM_OLD_IRQ_HANDLER = PATCHER_ADDR + 4;
|
||||
const PARAM_RESPONSE_CODE = PATCHER_ADDR + 8;
|
||||
const PARAM_RESPONSE_FLASH_ID = PATCHER_ADDR + 12;
|
||||
|
||||
const oldIrqHandler = (await dwd.readMemory(PRAM_IRQ_HANDLER, 4)).buffer.readUInt32LE(0);
|
||||
console.log(sprintf("Old SWI handler: %08X", oldIrqHandler))
|
||||
|
||||
await dwd.writeMemory(PARAM_OLD_IRQ_HANDLER, uint32(oldIrqHandler));
|
||||
|
||||
console.log("Running patcher...");
|
||||
try {
|
||||
await dwd.writeMemory(PRAM_IRQ_HANDLER, uint32(PATCHER_ADDR));
|
||||
} catch (e) {
|
||||
// fail is ok
|
||||
}
|
||||
|
||||
console.log("Waiting 5 seconds to complete...");
|
||||
await new Promise((resolve) => setTimeout(resolve, 5000));
|
||||
|
||||
// Flush
|
||||
await dwd.getSerialPort()?.read(1024, 100);
|
||||
|
||||
try {
|
||||
const responseCode = (await dwd.readMemory(PARAM_RESPONSE_CODE, 4)).buffer.readInt32LE(0);
|
||||
const responseFlashId = (await dwd.readMemory(PARAM_RESPONSE_FLASH_ID, 4)).buffer.readUInt32LE(0);
|
||||
|
||||
console.log(sprintf("Code: %d (%s)", responseCode, PatchResponseCode[responseCode]));
|
||||
console.log(sprintf("Flash ID: %08X", responseFlashId));
|
||||
|
||||
if (responseCode == PatchResponseCode.SUCCESS) {
|
||||
console.log("Success. Boot mode patched. Please reboot the phone.");
|
||||
} else if (responseCode == PatchResponseCode.BOOT_ALREADY_OPEN) {
|
||||
console.log("Boot is already open. Unlock is not needed.");
|
||||
} else {
|
||||
console.log("Unlocking failed!");
|
||||
}
|
||||
|
||||
console.log("Clearing RAM block...");
|
||||
await dwd.writeMemory(emptyRamBlock, Buffer.alloc(minRamBlockSize).fill(0));
|
||||
|
||||
if (!(responseCode == PatchResponseCode.FLASH_NOT_FOUND || responseCode == PatchResponseCode.FLASH_BUSY))
|
||||
break;
|
||||
|
||||
console.log("Retrying...");
|
||||
} catch (e) {
|
||||
console.log(String(e));
|
||||
console.log("An error occurred while waiting for a response from the unlocker. Please remove and reinstall the battery, then try again.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function uint32(value: number) {
|
||||
const buffer = Buffer.alloc(4);
|
||||
buffer.writeUInt32LE(value);
|
||||
return buffer;
|
||||
}
|
||||
157
examples/dwd.ts
157
examples/dwd.ts
|
|
@ -51,6 +51,18 @@ console.log(foundKeys);
|
|||
dwd.setKeys("auto");
|
||||
await dwd.connect();
|
||||
|
||||
console.log(sprintf("SCU_ID0=%08X", (await dwd.readMemory(0xF4400000 + 0x06C, 4)).buffer.readUInt32LE(0)));
|
||||
console.log(sprintf("SCU_ID1=%08X", (await dwd.readMemory(0xF4400000 + 0x070, 4)).buffer.readUInt32LE(0)));
|
||||
console.log(sprintf("SCU_BOOT_CFG=%08X", (await dwd.readMemory(0xF4400000 + 0x074, 4)).buffer.readUInt32LE(0)));
|
||||
console.log(sprintf("SCU_MANID=%08X", (await dwd.readMemory(0xF4400000 + 0x05C, 4)).buffer.readUInt32LE(0)));
|
||||
console.log(sprintf("SCU_CHIPID=%08X", (await dwd.readMemory(0xF4400000 + 0x060, 4)).buffer.readUInt32LE(0)));
|
||||
|
||||
|
||||
// decodeMMU(await readMmuTable(dwd));
|
||||
|
||||
// await dwd.readMemory(0x00400000, 4);
|
||||
|
||||
/*
|
||||
console.log(await dwd.getSWVersion());
|
||||
|
||||
for (let i = 0; i < 64; i++) {
|
||||
|
|
@ -59,7 +71,7 @@ for (let i = 0; i < 64; i++) {
|
|||
console.log(sprintf("%08X %08X", 0xF0000000 + i * 4, r.buffer.readUInt32LE(0)));
|
||||
}
|
||||
console.log(await dwd.getMemoryRegions());
|
||||
|
||||
*/
|
||||
/*
|
||||
const abortController = new AbortController();
|
||||
|
||||
|
|
@ -82,3 +94,146 @@ fs.writeFileSync("/tmp/ff.bin", result.buffer);
|
|||
await dwd.disconnect();
|
||||
|
||||
await port.close();
|
||||
|
||||
type MMUTableRow = {
|
||||
type: number;
|
||||
addr: number;
|
||||
size: number;
|
||||
phys: number;
|
||||
domain?: number;
|
||||
AP?: number;
|
||||
C?: boolean;
|
||||
B?: boolean;
|
||||
table?: MMUTableRow[];
|
||||
};
|
||||
|
||||
async function readMmuTable(dwd: DWD) {
|
||||
// Read 1st level descriptors
|
||||
const response = await dwd.readMemory(16 * 1024 * 0, 16 * 1024);
|
||||
|
||||
const mmuTable: MMUTableRow[] = [];
|
||||
for (let i = 0; i < 4096; i++) {
|
||||
const value = response.buffer.readUInt32LE(i * 4);
|
||||
const addr = i * 1024 * 1024;
|
||||
const size = 1024 * 1024;
|
||||
const type = value & 0b11;
|
||||
|
||||
switch (type) {
|
||||
case 1: // Coarse
|
||||
{
|
||||
const phys = (value & 0xFFFFFC00) >>> 0;
|
||||
const domain = (value >>> 5) & 0b1111;
|
||||
const AP = (value >> 10) & 0b11;
|
||||
mmuTable[i] = { type, addr, size, phys, domain, AP };
|
||||
}
|
||||
break;
|
||||
|
||||
case 2: // Section
|
||||
{
|
||||
const phys = (value & 0xFFF00000) >>> 0;
|
||||
const domain = (value >>> 5) & 0b1111;
|
||||
const C = (value & (1 << 3)) != 0;
|
||||
const B = (value & (1 << 2)) != 0;
|
||||
const AP = (value >> 10) & 0b11;
|
||||
mmuTable[i] = { type, addr, size, phys, domain, AP, C, B };
|
||||
}
|
||||
break;
|
||||
|
||||
case 3: // Fine
|
||||
{
|
||||
const phys = (value & 0xFFFFF000) >>> 0;
|
||||
const domain = (value >>> 5) & 0b1111;
|
||||
mmuTable[i] = { type, addr, size, phys, domain };
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Read 2nd level descriptors
|
||||
for (const row of mmuTable) {
|
||||
if (row == null)
|
||||
continue;
|
||||
|
||||
if (row.type == 1 || row.type == 3) {
|
||||
const subTableSize = (row.type == 3 ? 1024 * 4 : 256 * 4);
|
||||
const subTableCnt = subTableSize / 4;
|
||||
const subTableEntrySize = (1024 * 1024) / subTableCnt;
|
||||
|
||||
const response = await dwd.readMemory(row.phys, subTableSize);
|
||||
|
||||
const subTable: MMUTableRow[] = [];
|
||||
for (let i = 0; i < subTableCnt; i++) {
|
||||
const value = response.buffer.readUInt32LE(i * 4);
|
||||
const type = value & 0b11;
|
||||
const addr = row.addr + i * subTableEntrySize;
|
||||
|
||||
switch (type) {
|
||||
case 1:
|
||||
{
|
||||
const phys = (value & 0xFFFF0000) >>> 0;
|
||||
const C = (value & (1 << 3)) != 0;
|
||||
const B = (value & (1 << 2)) != 0;
|
||||
const AP = (value >> 4) & 0xFF;
|
||||
subTable[i] = { addr, type, phys, C, B, AP, size: 64 * 1024 };
|
||||
}
|
||||
break;
|
||||
|
||||
case 2:
|
||||
{
|
||||
const phys = (value & 0xFFFFF000) >>> 0;
|
||||
const C = (value & (1 << 3)) != 0;
|
||||
const B = (value & (1 << 2)) != 0;
|
||||
const AP = (value >> 4) & 0xFF;
|
||||
subTable[i] = { addr, type, phys, C, B, AP, size: 4 * 1024 };
|
||||
}
|
||||
break;
|
||||
|
||||
case 3:
|
||||
{
|
||||
const phys = (value & 0xFFFFFC00) >>> 0;
|
||||
const C = (value & (1 << 3)) != 0;
|
||||
const B = (value & (1 << 2)) != 0;
|
||||
const AP = (value >> 4) & 0b11;
|
||||
subTable[i] = { addr, type, phys, C, B, AP, size: 4 * 1024 };
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
row.table = subTable;
|
||||
}
|
||||
}
|
||||
|
||||
return mmuTable;
|
||||
}
|
||||
|
||||
function decodeMMU(table: MMUTableRow[]) {
|
||||
const types: string[] = [
|
||||
'[Fault] ',
|
||||
'[Coarse] ',
|
||||
'[Section]',
|
||||
'[Fine] ',
|
||||
];
|
||||
for (const row of table) {
|
||||
if (row == null)
|
||||
continue;
|
||||
|
||||
switch (row.type) {
|
||||
case 1:
|
||||
console.log(sprintf("%s %08X -> %08X D[%X] AP[%X]", types[row.type], row.addr, row.phys, row.domain ?? 0, row.AP));
|
||||
break;
|
||||
|
||||
case 2:
|
||||
console.log(sprintf("%s %08X -> %08X D[%X] AP[%X] %s%s",
|
||||
types[row.type], row.addr, row.phys, row.domain ?? "", row.AP, (row.C ? "C" : "-"), (row.B ? "B" : "-")));
|
||||
break;
|
||||
|
||||
case 3:
|
||||
console.log(sprintf("%s %08X -> %08X D[%X] AP[%X]", types[row.type], row.addr, row.phys, row.domain ?? 0, row.AP));
|
||||
break;
|
||||
}
|
||||
|
||||
if (row.table) {
|
||||
decodeMMU(row.table);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,31 +1,13 @@
|
|||
import { AsyncSerialPort } from "./AsyncSerialPort.js";
|
||||
|
||||
export class BaseSerialProtocol {
|
||||
#port?: AsyncSerialPort;
|
||||
protected readonly port: AsyncSerialPort;
|
||||
|
||||
protected get port(): AsyncSerialPort {
|
||||
if (!this.#port)
|
||||
throw new Error("Serial port not attached!");
|
||||
return this.#port;
|
||||
}
|
||||
|
||||
protected set port(port: AsyncSerialPort | undefined) {
|
||||
this.#port = port;
|
||||
}
|
||||
|
||||
constructor(port?: AsyncSerialPort) {
|
||||
constructor(port: AsyncSerialPort) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
attachSerialPort(port: AsyncSerialPort) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
detachSerialPort() {
|
||||
this.port = undefined;
|
||||
}
|
||||
|
||||
getSerialPort() {
|
||||
return this.#port;
|
||||
return this.port;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -68,7 +68,7 @@ export type CgsnGetMemoryRegionsResponse = CgsnBaseResponse<{
|
|||
}>;
|
||||
|
||||
export class CGSN extends BaseSerialProtocol {
|
||||
private readonly atc: AtChannel = new AtChannel();
|
||||
private readonly atc: AtChannel = new AtChannel(this.port);
|
||||
private connectionType: string = "";
|
||||
private isConnected = false;
|
||||
|
||||
|
|
@ -76,7 +76,6 @@ export class CGSN extends BaseSerialProtocol {
|
|||
if (this.isConnected)
|
||||
await this.disconnect();
|
||||
|
||||
this.atc.attachSerialPort(this.port);
|
||||
this.atc.start();
|
||||
testBaudRates ||= SERIAL_BAUD_RATES;
|
||||
for (const baudRate of testBaudRates) {
|
||||
|
|
@ -426,7 +425,6 @@ export class CGSN extends BaseSerialProtocol {
|
|||
if (this.isConnected && this.port.isOpen)
|
||||
await this.setBaudRate(115200);
|
||||
this.atc.stop();
|
||||
this.atc.detachSerialPort();
|
||||
this.isConnected = false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -453,7 +453,7 @@ export class DWD extends BaseSerialProtocol {
|
|||
request.writeUInt16LE(RAND4, 2);
|
||||
request.writeUInt16LE((keys.key1[0xF - keyRotate] ^ keys.key3[keyRotate] << 4 ^ 0x4d33) & 0xFFFF, 4);
|
||||
request.writeUInt16LE(RAND4, 6);
|
||||
console.log(await this.execCommand(request, FrameType.CONNECT2_RESP, timeout));
|
||||
await this.execCommand(request, FrameType.CONNECT2_RESP, timeout);
|
||||
}
|
||||
|
||||
newRequest(frameId: FrameType, payloadLength: number = 0): Buffer {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue