This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
fastboot.js
243 lines (202 loc) · 6.68 KB
/
fastboot.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// Wrapper around the fastboot utility.
"use strict";
// Whether or not this script is being loaded as a CommonJS module
// (from an add-on built using the Add-on SDK). If it isn't a CommonJS Module,
// then it's a JavaScript Module.
const { Cu } = require("chrome");
const { Subprocess } = Cu.import("resource://gre/modules/Subprocess.jsm", {});
const { setInterval, clearInterval } = Cu.import("resource://gre/modules/Timer.jsm", {});
const { PromiseUtils } = Cu.import("resource://gre/modules/PromiseUtils.jsm", {});
const { getFileForBinary } = require("./binary-manager");
const { Devices } =
require("./devtools-import")("resource://devtools/shared/apps/Devices.jsm");
let fastbootTimer = null;
let fastbootDevices = [];
const Fastboot = {
get fastbootDevices() {
return fastbootDevices;
},
set fastbootDevices(newVal) {
fastbootDevices = newVal;
},
get fastbootFilePromise() {
if (this._fastbootFilePromise) {
return this._fastbootFilePromise;
}
this._fastbootFilePromise = getFileForBinary("fastboot");
return this._fastbootFilePromise;
},
async do(args, serial) {
let deferred = PromiseUtils.defer();
let out_buffer = [];
let err_buffer = [];
if (serial && typeof(serial) === "string") {
args.unshift("-s", serial);
}
let binary = await this.fastbootFilePromise;
let callPayload = {
command: binary,
arguments: args,
stdout(data) {
out_buffer.push(data);
},
stderr(data) {
err_buffer.push(data);
},
done() {
deferred.resolve({ stdout: out_buffer, stderr: err_buffer });
}
};
Subprocess.call(callPayload);
return deferred.promise;
},
startPolling: function fastboot_startPolling() {
console.debug("IN fastboot_startPolling");
if (fastbootTimer !== null) {
console.warn("Fastboot poll already running.");
return;
}
let doPoll = (function() {
this.devices().then((devices) => {
let added = [];
let removed = [];
if (devices.sort() === this.fastbootDevices.sort()) {
console.debug("No change.");
return;
}
console.debug("Read devices from fastboot output", devices);
for (let dev of devices) {
if (!this.fastbootDevices.includes(dev)) {
added.push(dev);
}
}
console.debug("Fastboot devices added", added);
for (let dev of this.fastbootDevices) {
// listed in previous devices and not in the current one
if (!devices.includes(dev)) {
removed.push(dev);
}
}
console.debug("Fastboot devices removed", removed);
this.fastbootDevices = devices;
for (let dev of added) {
let fbdevice = new FastbootDevice(dev);
Devices.register(dev, fbdevice);
}
for (let dev of removed) {
Devices.unregister(dev);
}
});
}).bind(this);
console.log("fastboot_polling starting");
fastbootTimer = setInterval(doPoll, 2000);
},
stopPolling: function fastboot_stopPolling() {
console.debug("IN fastboot_stopPolling");
console.log("fastboot_polling stopping");
clearInterval(fastbootTimer);
fastbootTimer = null;
},
// Sends back an array of device names.
devices: function fastboot_devices() {
return this.do(["devices"]).then(
function onSuccess(data) {
let devices = [];
for (let line of data.stdout) {
let [ sn, mode ] = line.trim().split("\t");
if (mode === "fastboot") {
devices.push(sn);
}
}
return devices;
}, function onError(error) {
return [];
});
},
getvar: function fastboot_getvar(varname, serial) {
return this.do(["getvar", varname], serial).then(
function onSuccess(data) {
console.debug("getvar", data);
// product: D6603finished. total time: 0.003s
for (let line of data.stderr.join("\n").split("\n")) {
if (line.includes(":")) {
return line.split(":")[1].trim();
}
}
return "";
}, function onError(error) {
console.debug("error getvar", error);
return "";
}
);
},
flash: function fastboot_flash(partition, image, serial) {
return this.do(["flash", partition, image], serial).then(
function onSuccess(data) {
console.debug("flash", partition, image);
// sending 'recovery' (5334 KB)...
// OKAY [ 0.545s]
// writing 'recovery'...
// OKAY [ 0.891s]
// finished. total time: 1.436s
let flashProgress = {
sending: false,
sendingOk: false,
writing: false,
writingOk: false,
finished: false
};
console.debug("fastboot flash reported:", data);
let fullOutput = data.stderr.join("\n").split("\n");
console.debug("Will look into", fullOutput);
for (let line of fullOutput) {
console.debug("Read:", line);
if (!line || !line.includes(" ")) {
console.debug("No space ...");
continue;
}
let first = line.split(" ")[0];
console.debug("Checking with:", first);
switch (first) {
case "sending": flashProgress.sending = true; break;
case "writing": flashProgress.writing = true; break;
case "finished.": flashProgress.finished = true; break;
case "OKAY":
if (flashProgress.sending && !flashProgress.writing) {
flashProgress.sendingOk = true;
} else if (flashProgress.sending && flashProgress.writing) {
flashProgress.writingOk = true;
}
break;
default:
console.debug("Unknown state: ", first);
}
}
return flashProgress;
}, function onError(error) {
console.debug("error flash", error);
return "";
}
);
},
reboot: function fastboot_reboot(serial) {
return this.do(["reboot"], serial);
}
};
// Fastboot object
function FastbootDevice(id) {
this.id = id;
}
FastbootDevice.prototype = {
type: "fastboot",
devices: Fastboot.devices.bind(Fastboot),
flash: Fastboot.flash.bind(Fastboot),
getvar: Fastboot.getvar.bind(Fastboot),
reboot: Fastboot.reboot.bind(Fastboot),
startPolling: Fastboot.startPolling.bind(Fastboot),
stopPolling: Fastboot.stopPolling.bind(Fastboot)
};
module.exports = Fastboot;