Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
bongbui321 committed Mar 27, 2024
1 parent 92619dd commit 5c9b220
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 45 deletions.
2 changes: 0 additions & 2 deletions src/QDL/firehose.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ class cfg {
this.MaxPayloadSizeFromTargetInBytes = 4096;
this.MaxXMLSizeInBytes = 4096;
this.bit64 = true;
this.total_blocks = 0;
this.block_size = 0;
this.SECTOR_SIZE_IN_BYTES = 4096;
this.MemoryName = "UFS";
this.maxlun = 6;
Expand Down
60 changes: 30 additions & 30 deletions src/QDL/gpt.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { containsBytes } = require("./utils");
const { containsBytes, fromUint8ArrayToNumber } = require("./utils");
var CRC32 = require("crc-32");

export const AB_FLAG_OFFSET = 6;
Expand Down Expand Up @@ -53,18 +53,18 @@ class gptHeader {
let sh = new structHelper(data);
this.signature = sh.bytes(8);
this.revision = sh.dword();
this.header_size = sh.dword();
this.headerSize = sh.dword();
this.crc32 = sh.dword();
this.reserved = sh.dword();
this.current_lba = sh.qword();
this.backup_lba = sh.qword();
this.first_usable_lba = sh.qword();
this.last_usable_lba = sh.qword();
this.disk_guid = sh.bytes(16);
this.part_entry_start_lba = sh.qword();
this.num_part_entries = sh.dword();
this.part_entry_size = sh.dword();
this.crc32_part_entries = sh.dword();
this.currentLba = sh.qword();
this.backupLba = sh.qword();
this.firstUsableLba = sh.qword();
this.lastUsableLba = sh.qword();
this.diskGuid = sh.bytes(16);
this.partEntryStartLba = sh.qword();
this.numPartEntries = sh.dword();
this.partEntrySize = sh.dword();
this.crc32PartEntries = sh.dword();
}
}

Expand All @@ -74,8 +74,8 @@ export class gptPartition {
let sh = new structHelper(data)
this.type = sh.bytes(16);
this.unique = sh.bytes(16);
this.first_lba = sh.qword();
this.last_lba = sh.qword();
this.firstLba = sh.qword();
this.lastLba = sh.qword();
this.flags = sh.qword();
this.name = sh.toString(72);
}
Expand All @@ -91,7 +91,7 @@ export class gptPartition {
for (let i = 0; i < this.unique.length; i++) {
view.setUint8(offset++, this.unique[i], true);
}
let tmp = [BigInt(this.first_lba), BigInt(this.last_lba), BigInt(this.flags)];
let tmp = [BigInt(this.firstLba), BigInt(this.lastLba), BigInt(this.flags)];
for (let i = 0; i < 3; i++) {
view.setBigUint64(offset, tmp[i], true);
offset += 8;
Expand Down Expand Up @@ -143,9 +143,9 @@ export class gpt {
// mbr (even for backup gpt header to ensure offset consistency) + gpt header + part_table
let start = 2 * sectorSize;

let entrySize = this.header.part_entry_size;
let entrySize = this.header.partEntrySize;
this.partentries = {};
let numPartEntries = this.header.num_part_entries;
let numPartEntries = this.header.numPartEntries;
for (let idx = 0; idx < numPartEntries; idx++) {
const data = gptData.slice(start + (idx * entrySize), start + (idx * entrySize) + entrySize);
if (new DataView(data.slice(16,32).buffer, 0).getUint32(0, true) == 0)
Expand All @@ -165,8 +165,8 @@ export class gpt {
${guid3.toString(16).padStart(4, '0')}-
${guid4.toString(16).padStart(4, '0')}-
${guid5}`;
pa.sector = partentry.first_lba;
pa.sectors = partentry.last_lba - partentry.first_lba + 1;
pa.sector = partentry.firstLba;
pa.sectors = partentry.lastLba - partentry.firstLba + 1;
pa.flags = partentry.flags;
pa.entryOffset = start + (idx * entrySize);
const typeOfPartentry = new DataView(partentry.type.slice(0, 0x4).buffer, 0).getUint32(0, true);
Expand All @@ -192,9 +192,9 @@ export class gpt {
fixGptCrc(data) {
const headerOffset = this.sectorSize;
const partentryOffset = 2 * this.sectorSize;
const partentrySize = this.header.num_part_entries * this.header.part_entry_size;
const partentrySize = this.header.numPartEntries * this.header.partEntrySize;
const partdata = Uint8Array.from(data.slice(partentryOffset, partentryOffset + partentrySize));
let headerdata = Uint8Array.from(data.slice(headerOffset, headerOffset + this.header.header_size));
let headerdata = Uint8Array.from(data.slice(headerOffset, headerOffset + this.header.headerSize));

let view = new DataView(new ArrayBuffer(4));
view.setInt32(0, CRC32.buf(Buffer.from(partdata)), true);
Expand All @@ -210,7 +210,7 @@ export class gpt {
}


// 0x003a as inactive and 0x006f for active boot partitions. This follows fastboot standard
// 0x003a for inactive and 0x006f for active boot partitions. This follows fastboot standard
export function setPartitionFlags(flags, active, isBoot) {
let newFlags = BigInt(flags);
if (active) {
Expand All @@ -232,15 +232,15 @@ export function setPartitionFlags(flags, active, isBoot) {

function checkHeaderCrc(gptData, guidGpt) {
const headerOffset = guidGpt.sectorSize;
const headerSize = guidGpt.header.header_size;

const header = gptData.slice(headerOffset, headerOffset + headerSize);
const testHeader = new Uint8Array(guidGpt.fixGptCrc(gptData).buffer).slice(headerOffset, headerOffset + headerSize);
const headerCrc = header.slice(0x10, 0x10 + 4), testHeaderCrc = testHeader.slice(0x10, 0x10 + 4);
const partTableCrc = header.slice(0x58, 0x58 + 4), testPartTableCrc = testHeader.slice(0x58, 0x58 + 4);
return [Buffer.from(headerCrc).toString('hex') !== Buffer.from(testHeaderCrc).toString('hex') ||
Buffer.from(partTableCrc).toString('hex') !== Buffer.from(testPartTableCrc).toString('hex'),
Buffer.from(partTableCrc).toString('hex')]
const headerSize = guidGpt.header.headerSize;
const testGptData = guidGpt.fixGptCrc(gptData).buffer;
const testHeader = new Uint8Array(testGptData.slice(headerOffset, headerOffset + headerSize));

const headerCrc = guidGpt.header.crc32
const testHeaderCrc = fromUint8ArrayToNumber(testHeader.slice(0x10, 0x10 + 4));
const partTableCrc = guidGpt.header.crc32PartEntries
const testPartTableCrc = fromUint8ArrayToNumber(testHeader.slice(0x58, 0x58 + 4));
return [headerCrc !== testHeaderCrc || partTableCrc !== testPartTableCrc, partTableCrc];
}


Expand Down
26 changes: 13 additions & 13 deletions src/QDL/qdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ export class qdlDevice {
let guidGpt = new gpt.gpt();
const header = guidGpt.parseHeader(data, this.firehose.cfg.SECTOR_SIZE_IN_BYTES);
if (containsBytes("EFI PART", header.signature)) {
const partTableSize = header.num_part_entries * header.part_entry_size;
const partTableSize = header.numPartEntries * header.partEntrySize;
const sectors = Math.floor(partTableSize / this.firehose.cfg.SECTOR_SIZE_IN_BYTES);
data = concatUint8Array([data, (await this.firehose.cmdReadBuffer(lun, header.part_entry_start_lba, sectors)).data]);
data = concatUint8Array([data, (await this.firehose.cmdReadBuffer(lun, header.partEntryStartLba, sectors)).data]);
guidGpt.parse(data, this.firehose.cfg.SECTOR_SIZE_IN_BYTES);
return [data, guidGpt];
} else {
Expand Down Expand Up @@ -168,8 +168,8 @@ export class qdlDevice {
throw "Cannot get active slot."
for (const partitionName in guidGpt.partentries) {
const slot = partitionName.slice(-2);
// backup gpt header is more reliable
const [ backupGptData, backupGuidGpt ] = await this.getGpt(lun, guidGpt.header.backup_lba);
// backup gpt header is more reliable, since it would always has the non-corrupted gpt header
const [ backupGptData, backupGuidGpt ] = await this.getGpt(lun, guidGpt.header.backupLba);
const partition = backupGuidGpt.partentries[partitionName];
const active = (((BigInt(partition.flags) >> (BigInt(gpt.AB_FLAG_OFFSET) * BigInt(8))))
& BigInt(gpt.AB_PARTITION_ATTR_SLOT_ACTIVE)) === BigInt(gpt.AB_PARTITION_ATTR_SLOT_ACTIVE);
Expand Down Expand Up @@ -198,7 +198,7 @@ export class qdlDevice {


patchSetActiveHelper(gptDataA, gptDataB, guidGpt, partA, partB, slot_a_status, slot_b_status, isBoot) {
const partEntrySize = guidGpt.header.part_entry_size;
const partEntrySize = guidGpt.header.partEntrySize;

const sdataA = gptDataA.slice(partA.entryOffset, partA.entryOffset+partEntrySize);
const sdataB = gptDataB.slice(partB.entryOffset, partB.entryOffset+partEntrySize);
Expand Down Expand Up @@ -232,7 +232,7 @@ export class qdlDevice {
for (const lunA of luns) {
let checkGptHeader = false;
let [ gptDataA, guidGptA ] = await this.getGpt(lunA);
let [ backupGptDataA, backupGuidGptA ] = await this.getGpt(lunA, guidGptA.header.backup_lba);
let [ backupGptDataA, backupGuidGptA ] = await this.getGpt(lunA, guidGptA.header.backupLba);
if (guidGptA === null)
throw "Firehose - Error while getting gpt header data";
for (const partitionNameA in guidGptA.partentries) {
Expand All @@ -253,7 +253,7 @@ export class qdlDevice {
if (!sts)
throw `Firehose - Cannot find partition ${partitionNameB}`;
[ sts, lunB, gptDataB, guidGptB ] = resp;
[ backupGptDataB, backupGuidGptB ] = await this.getGpt(lunB, guidGptB.header.backup_lba);
[ backupGptDataB, backupGuidGptB ] = await this.getGpt(lunB, guidGptB.header.backupLba);
}

if (!checkGptHeader) {
Expand Down Expand Up @@ -288,9 +288,9 @@ export class qdlDevice {

// gpt header updated by slot B if in the same lun
if (lunA !== lunB) {
const headerOffsetA = guidGptA.header.current_lba * guidGptA.sectorSize;
const startSectorHdrA = guidGptA.header.current_lba;
const pHeaderA = newGptDataA.slice(headerOffsetA, headerOffsetA + guidGptA.header.header_size);
const headerOffsetA = guidGptA.sectorSize;
const startSectorHdrA = guidGptA.header.currentLba;
const pHeaderA = newGptDataA.slice(headerOffsetA, headerOffsetA + guidGptA.header.headerSize);
await this.cmdPatchMultiple(lunA, startSectorHdrA, 0, pHeaderA);
}
}
Expand All @@ -299,9 +299,9 @@ export class qdlDevice {
const byteOffsetPatchB = pOffsetB % this.firehose.cfg.SECTOR_SIZE_IN_BYTES;
await this.cmdPatchMultiple(lunB, startSectorPatchB, byteOffsetPatchB, pDataB);

const headerOffsetB = guidGptB.header.current_lba * guidGptB.sectorSize;
const startSectorHdrB = guidGptB.header.current_lba;
const pHeaderB = newGptDataB.slice(headerOffsetB, headerOffsetB + guidGptB.header.header_size);
const headerOffsetB = guidGptB.sectorSize;
const startSectorHdrB = guidGptB.header.currentLba;
const pHeaderB = newGptDataB.slice(headerOffsetB, headerOffsetB + guidGptB.header.headerSize);
await this.cmdPatchMultiple(lunB, startSectorHdrB, 0, pHeaderB);
}
}
Expand Down

0 comments on commit 5c9b220

Please sign in to comment.