1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
2 /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
8 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
10 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
11 Cu.import("resource://gre/modules/ctypes.jsm");
13 const RECOVERYSERVICE_CID = Components.ID("{b3caca5d-0bb0-48c6-912b-6be6cbf08832}");
14 const RECOVERYSERVICE_CONTRACTID = "@mozilla.org/recovery-service;1";
17 dump("-*- RecoveryService: " + msg + "\n");
20 #ifdef MOZ_WIDGET_GONK
21 let librecovery = (function() {
24 library = ctypes.open("librecovery.so");
26 log("Unable to open librecovery.so");
27 throw Cr.NS_ERROR_FAILURE;
29 let FotaUpdateStatus = new ctypes.StructType("FotaUpdateStatus", [
30 { result: ctypes.int },
31 { updatePath: ctypes.char.ptr }
35 factoryReset: library.declare("factoryReset",
38 installFotaUpdate: library.declare("installFotaUpdate",
44 FotaUpdateStatus: FotaUpdateStatus,
45 getFotaUpdateStatus: library.declare("getFotaUpdateStatus",
52 const gFactoryResetFile = "__post_reset_cmd__";
56 function RecoveryService() {}
58 RecoveryService.prototype = {
59 classID: RECOVERYSERVICE_CID,
60 QueryInterface: XPCOMUtils.generateQI([Ci.nsIRecoveryService]),
61 classInfo: XPCOMUtils.generateCI({
62 classID: RECOVERYSERVICE_CID,
63 contractID: RECOVERYSERVICE_CONTRACTID,
64 interfaces: [Ci.nsIRecoveryService],
65 classDescription: "B2G Recovery Service"
68 factoryReset: function RS_factoryReset(reason) {
69 #ifdef MOZ_WIDGET_GONK
71 // If this succeeds, then the device reboots and this never returns
72 if (librecovery.factoryReset() != 0) {
73 log("Error: Factory reset failed. Trying again after clearing cache.");
75 let cache = Cc["@mozilla.org/netwerk/cache-storage-service;1"]
76 .getService(Ci.nsICacheStorageService);
78 if (librecovery.factoryReset() != 0) {
79 log("Error: Factory reset failed again");
83 log("factoryReset " + reason);
84 if (reason == "wipe") {
85 let volumeService = Cc["@mozilla.org/telephony/volume-service;1"]
86 .getService(Ci.nsIVolumeService);
87 let volNames = volumeService.getVolumeNames();
88 log("Found " + volNames.length + " volumes");
90 for (let i = 0; i < volNames.length; i++) {
91 let name = volNames.queryElementAt(i, Ci.nsISupportsString);
92 let volume = volumeService.getVolumeByName(name.data);
93 log("Got volume: " + name.data + " at " + volume.mountPoint);
94 text += "wipe " + volume.mountPoint + "\n";
97 Cu.import("resource://gre/modules/osfile.jsm");
98 let dir = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
99 dir.initWithPath("/persist");
100 var postResetFile = dir.exists() ?
101 OS.Path.join("/persist", gFactoryResetFile):
102 OS.Path.join("/cache", gFactoryResetFile);
103 let encoder = new TextEncoder();
104 let array = encoder.encode(text);
105 let promise = OS.File.writeAtomic(postResetFile, array,
106 { tmpPath: postResetFile + ".tmp" });
108 promise.then(doReset, function onError(error) {
109 log("Error: " + error);
115 throw Cr.NS_ERROR_FAILURE;
118 installFotaUpdate: function RS_installFotaUpdate(updatePath) {
119 #ifdef MOZ_WIDGET_GONK
120 // If this succeeds, then the device reboots and this never returns
121 if (librecovery.installFotaUpdate(updatePath, updatePath.length) != 0) {
122 log("Error: FOTA install failed. Trying again after clearing cache.");
124 var cache = Cc["@mozilla.org/netwerk/cache-storage-service;1"].getService(Ci.nsICacheStorageService);
126 if (librecovery.installFotaUpdate(updatePath, updatePath.length) != 0) {
127 log("Error: FOTA install failed again");
130 throw Cr.NS_ERROR_FAILURE;
133 getFotaUpdateStatus: function RS_getFotaUpdateStatus() {
134 let status = Ci.nsIRecoveryService.FOTA_UPDATE_UNKNOWN;
135 #ifdef MOZ_WIDGET_GONK
136 let cStatus = librecovery.FotaUpdateStatus();
138 if (librecovery.getFotaUpdateStatus(cStatus.address()) == 0) {
139 status = cStatus.result;
147 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([RecoveryService]);