-
Notifications
You must be signed in to change notification settings - Fork 4
/
super.js
121 lines (107 loc) · 3.1 KB
/
super.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* eslint-disable no-sync */
/* eslint-disable import/no-commonjs */
const fs = require("fs");
const lpunpack = require("./lpunpack");
const simg2img = require("./simg2img");
const { fastbootExec, isUserspace } = require("./fastboot");
async function extractSuper(firmwareFolder) {
if (!fs.existsSync(firmwareFolder + "/vendor_a.img")) {
console.log("\nExtracting super image");
if (!fs.existsSync(firmwareFolder + "/super.img")) {
if (process.platform === "linux") {
await simg2img({
cmd:
firmwareFolder +
"/super.img_sparsechunk.* " +
firmwareFolder +
"/super.img",
});
} else if (process.platform === "win32") {
await simg2img({
cmd:
"/decompress " +
firmwareFolder +
"/super.img_sparsechunk.0 " +
firmwareFolder +
"/super.img",
});
}
}
await lpunpack({
cmd: firmwareFolder + "/super.img " + firmwareFolder + "/",
});
console.log("\nExtracted");
}
}
async function flashSuperBPartitions(firmwareFolder) {
if (!await isUserspace()) {
console.log("\nReboot to fastbootd");
await fastbootExec({ cmd: "reboot fastboot" });
while (true) {
const fbDevice = await fastbootExec({ cmd: "devices" });
if (!fbDevice) continue;
break;
}
}
try {
await fastbootExec({ cmd: "delete-logical-partition product_b" });
} catch (e) {
// ignore, partition doesn't exists
}
try {
await fastbootExec({ cmd: "delete-logical-partition vendor_b" });
} catch (e) {
// ignore, partition doesn't exists
}
let partitionSize = 0x0;
try {
let partitionSize = await fastbootExec({
cmd: "getvar partition-size:vendor_a",
});
partitionSize = partitionSize.split(":")[2].trim().split("\n")[0];
await fastbootExec({
cmd: "create-logical-partition vendor_b " + partitionSize,
});
} catch (e) {
console.log("\nUnable to create vendor_b");
}
try {
partitionSize = await fastbootExec({
cmd: "getvar partition-size:product_a",
});
partitionSize = partitionSize.split(":")[2].trim().split("\n")[0];
await fastbootExec({
cmd: "create-logical-partition product_b " + partitionSize,
});
} catch (e) {
console.log("\nUnable to create product_b");
}
console.log("\nFlashing system/product/vendor to slot b");
let result = "";
try {
result = await fastbootExec({
cmd: "flash product_b " + firmwareFolder + "/product_a.img",
});
console.log(result);
} catch (e) {
console.log("\nUnable to flash product_b");
}
try {
result = await fastbootExec({
cmd: "flash vendor_b " + firmwareFolder + "/vendor_a.img",
});
console.log(result);
} catch (e) {
console.log("\nUnable to flash vendor_b");
}
try {
result = await fastbootExec({
cmd: "flash system_b " + firmwareFolder + "/system_a.img",
});
console.log(result);
} catch (e) {
console.log("\nUnable to flash system_b");
}
console.log("Done");
}
module.exports = { extractSuper, flashSuperBPartitions };