This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathimaging_tools.js
299 lines (258 loc) · 9.22 KB
/
imaging_tools.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* 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/. */
"use strict";
const { Cc, Ci, Cu, Cr } = require("chrome");
const { XPCOMABI } = require("sdk/system/runtime");
const subprocess = require("./subprocess");
const { OS } = Cu.import("resource://gre/modules/osfile.jsm", {});
Cu.import("resource://gre/modules/FileUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyGetter(this, "ppmm", function() {
return Cc["@mozilla.org/parentprocessmessagemanager;1"]
.getService(Ci.nsIMessageListenerManager);
});
function getUint8Array(str) {
let uint = new Uint8Array(str.length);
for (let i = 0, j = str.length; i < j; ++i) {
uint[i] = str.charCodeAt(i);
}
return uint;
}
function gzipCompressString(string, obs) {
let scs = Cc["@mozilla.org/streamConverters;1"]
.getService(Ci.nsIStreamConverterService);
let listener = Cc["@mozilla.org/network/stream-loader;1"]
.createInstance(Ci.nsIStreamLoader);
listener.init(obs);
let converter = scs.asyncConvertData("uncompressed", "gzip",
listener, null);
let stringStream = Cc["@mozilla.org/io/string-input-stream;1"]
.createInstance(Ci.nsIStringInputStream);
stringStream.data = string;
converter.onStartRequest(null, null);
converter.onDataAvailable(null, null, stringStream, 0, string.length);
converter.onStopRequest(null, null, null);
}
function ImagingTools() {
this._tools = [ "mkbootfs", "mkbootimg", "make_ext4fs" ];
this._baseURI = "resource://b2g-installeratmozilla.org/chrome/b2g-installer/content/";
this._paths = {};
this._platform = Services.appinfo.OS;
}
ImagingTools.prototype = {
init: function() {
console.debug("Doing ImagingTools.init()");
this.detectBinaries();
ppmm.addMessageListener("B2GInstaller:MainProcess:Cleanup", this);
ppmm.addMessageListener("B2GInstaller:MainProcess:BuildRamdisk", this);
ppmm.addMessageListener("B2GInstaller:MainProcess:BuildBootable", this);
ppmm.addMessageListener("B2GInstaller:MainProcess:BuildExt4FS", this);
},
uninit: function() {
console.debug("Doing ImagingTools.uninit()");
ppmm.removeMessageListener("B2GInstaller:MainProcess:Cleanup", this);
ppmm.removeMessageListener("B2GInstaller:MainProcess:BuildRamdisk", this);
ppmm.removeMessageListener("B2GInstaller:MainProcess:BuildBootable", this);
ppmm.removeMessageListener("B2GInstaller:MainProcess:BuildExt4FS", this);
},
detectBinaries: function() {
let uri = this._baseURI;
let platform = this._platform;
console.log("Checking existence of", this._tools, "for", platform, "within", uri);
this._tools.forEach(tool => {
console.debug("Checking for tool", tool);
let binary, system;
switch (platform) {
case "Linux":
system = XPCOMABI.indexOf("x86_64") == 0 ? "linux64" : "linux";
binary = tool;
break;
case "Darwin":
system = "mac64";
binary = tool;
break;
case "WINNT":
system = "win32";
binary = tool + ".exe";
break;
case "XPCSHELL":
system = ".";
binary = tool;
break;
default:
console.error("Unsupported platform", platform);
return;
}
console.debug("Looking into", uri, system, "for", binary);
let bin = uri + system + "/" + binary;
let url = Services.io.newURI(bin, null, null)
.QueryInterface(Ci.nsIFileURL);
if (!url.file.exists()) {
console.error("Unable to find", url.file);
return;
}
// Finally, save it.
console.debug("Tool", tool, "at", url.file.path);
this._paths[tool] = url.file;
});
console.log("All tools have been picked up.");
},
receiveMessage: function(msg) {
console.debug("Received:", msg);
// msg.target.sendAsyncMessage("B2GInstaller:About", { "fastboot": true, "adb": false});
let options = msg.data;
switch (msg.name) {
case "B2GInstaller:MainProcess:Cleanup":
this.uninit();
break;
case "B2GInstaller:MainProcess:BuildRamdisk":
this.executeTool("mkbootfs", options).then(res => {
msg.target.sendAsyncMessage("B2GInstaller:MainProcess:BuildRamdisk:Return", { res: res, req: options });
});
break;
case "B2GInstaller:MainProcess:BuildBootable":
this.executeTool("mkbootimg", options).then(res => {
msg.target.sendAsyncMessage("B2GInstaller:MainProcess:BuildBootable:Return", { res: res, req: options });
});
break;
case "B2GInstaller:MainProcess:BuildExt4FS":
this.executeTool("make_ext4fs", options).then(res => {
msg.target.sendAsyncMessage("B2GInstaller:MainProcess:BuildExt4FS:Return", { res: res, req: options });
});
break;
default:
console.error("Unsupported message:", msg.name);
break;
}
},
getTool: function(name) {
if (Object.keys(this._paths).indexOf(name) === -1) {
console.error("Trying to use inexistent tool", name);
return;
}
return this._paths[name];
},
mkbootfs: function(options) {
return new Promise((resolve, reject) => {
// Build a cpio archive ramdisk and gzip it
let cpioContent = "";
subprocess.call({
command: this.getTool("mkbootfs"),
charset: null, // make sure we get a binary stream
arguments: [ options.from ],
stdout: function(cpio) {
cpioContent += cpio;
},
done: function() {
let observer = {
onStreamComplete: function(loader, context, status, length, result) {
let payload = new Uint8Array(length);
payload.set(result, 0);
console.debug("About to write", payload.byteLength, "bytes ...");
OS.File.writeAtomic(options.to, payload, { }).then(
function onSuccess(bytes) {
let expected = new FileUtils.File(options.to);
if (expected.exists()) {
console.debug("Written gzip'd cpio", bytes," bytes to", options.to);
resolve(true);
} else {
console.debug("Error checking gzip'd cpio", bytes," bytes to", options.to);
reject(true);
}
},
function onFailure() {
console.debug("Unable to write gzip'd cpio to", options.to);
resolve(false);
}
);
}
};
gzipCompressString(cpioContent, observer);
}
});
});
},
mkbootimg: function(options) {
/**
pushd "${IMAGE_DIR}/${src}";
../../mkbootimg \
--kernel "kernel" \
--ramdisk "initrd.img" \
--cmdline "`cat cmdline`" \
--pagesize "`cat pagesize`" \
--base "`cat base`" \
--dt "../../dt.img" \
--output "../../${img}"
**/
let args = [];
args.push("--kernel", options.kernel);
args.push("--ramdisk", options.ramdisk);
args.push("--cmdline", options.cmdline);
args.push("--pagesize", options.pagesize);
args.push("--base", options.base);
if (options.dt) {
args.push("--dt", options.dt);
}
if (options.extraArguments) {
let extraArguments = options.extraArguments.trim().split(/ +/);
args = args.concat(extraArguments);
}
args.push("--output", options.output);
return new Promise((resolve, reject) => {
// Build an Android boot image
subprocess.call({
command: this.getTool("mkbootimg"),
arguments: args,
stdout: function(data) {
console.debug("STDOUT:", data);
},
done: function() {
let expected = new FileUtils.File(options.output);
if (expected.exists()) {
resolve(true);
} else {
reject(true);
}
}
});
});
},
make_ext4fs: function(options) {
/**
./make_ext4fs `cat "${DEVICE}-cmdline-fs.txt" | grep ^system|cut -d':' -f2` "system.img" "${IMAGE_DIR}/SYSTEM/"
**/
// cmdline_fs may include multiple spaces between arguments
let args = options.cmdline_fs.split(/ +/);
args.push(options.image);
args.push(options.source);
return new Promise((resolve, reject) => {
// Build an Android system partition image
subprocess.call({
command: this.getTool("make_ext4fs"),
arguments: args,
stdout: function(data) {
console.debug("STDOUT:", data);
},
done: function() {
let expected = new FileUtils.File(options.image);
if (expected.exists()) {
resolve(true);
} else {
reject(true);
}
}
});
});
},
executeTool: function(name, options) {
if (!this.getTool(name)) {
return;
}
console.debug("Calling", name, "with", options);
return this[name](options);
}
};
module.exports = ImagingTools;
/* vim: set et ts=2 sw=2 : */