Project

General

Profile

RestoreEfsNvDataBackupFile » History » Version 2

Denis 'GNUtoo' Carikli, 07/04/2020 05:44 AM
continue improving the tutorial

1 1 Denis 'GNUtoo' Carikli
h1. RestoreEfsNvDataBackupFile
2
3
{{TOC}}
4
5
h2. Introduction
6
7
Many devices supported by Replicant have a partition with the modem data (IMEI, etc) which is called EFS.
8
9
That partition is used by Replicant to enable the modem to store data, by making files inside that partition available to the modem through a network protocol between Replicant and the modem.
10
11
The advantage of that architecture is that Replicant has more control, and the modem (which runs nonfree software) has less control over the data it uses.
12
13
However as with every other computer system, files can get corrupted, especially if the power goes away suddenly, while data is being written in some of the files.
14
15
That can happen with batteries that are in very bad conditions: very old batteries can still last several hours, but if the smartphones tries to consume too much power (mA / miliampers) at once, the battery can be unable to provide that much power at the same time, which results in the voltage dropping and the smartphone or tablets being turned off suddenly, without having the time to properly save any data it may be writing.
16
17
In addition to the issue described above, the modem data partition contains data that is crucial (like the IMEI) and we don't know (yet) how to reconstruct the files that are in that partition. And without them, the modem will not work. This is why doing a backup of the data that is in this partition is crucial.
18
19
So if you didn't do that backup yet, you can follow the instructions in the [[BackupTheEFS]] wiki page.
20
21
The [[BackupTheEFS]] page has information on which devices have a modem data partition (some devices supported by Replicant don't have a modem for instance).
22
23
h2. Warning
24
25
If you don't want your device to break, really make sure you have a backup of the modem data partition. The backup can be done with the instruction that are in the [[BackupTheEFS]] wiki page.
26
27
If all the files in the modem data partition are destroyed for a reason or another, you won't be able to receive SMS, make calls, get 3G data connections, etc.
28
29
We don't know how to repair that (yet).
30
31
h2. Restoring nv_data.bin
32
33
If only the nv_data.bin file is corrupted , you can easily restore it, without having to restore a full backup.
34
35
h3. Making a new backup
36
37
Before starting, it's a good idea to make a new backup of the modem data partition, and name it in a way that is self-explanatory. For instance you could put it in a directory named 'backup-with-corrupted-nvdata-bin' not to confuse it with other backups that do not contain any corrupted files. As repeated before, you can use the [[BackupTheEFS]] instructions to do a backup.
38
39
Making a new backup has several advantages:
40
* If you lost your old backups you have a new one
41
* If you have your old backups somewhere else (like on another computer), you don't risk erasing them by mistake
42
43
Make sure not to erase the old backups while making a new one.
44
45
At the time of writing, the [[BackupTheEFS]] contain the following command:
46
<pre>
47
REPLICANT_EFS_BACKUP_DIR=~/replicant_devices/0123456789abcdef/backup_efs
48
mkdir -p $REPLICANT_EFS_BACKUP_DIR && cd $REPLICANT_EFS_BACKUP_DIR
49
</pre>
50
51
This creates a directory named replicant_devices in your home, with inside a directory named 0123456789abcdef, with inside a directory named backup_efs.
52
Then it makes you go in that directory from the command line. This way the next commands will save the backups in that directory.
53
54
We can easily substitute that command with another one:
55
<pre>
56
REPLICANT_EFS_BACKUP_DIR=~/replicant_devices/backup-with-corrupted-nvdata-bin-of-fourth-jully-2020/backup_efs
57
mkdir -p $REPLICANT_EFS_BACKUP_DIR && cd $REPLICANT_EFS_BACKUP_DIR
58
</pre>
59
60
Here instead of creating the 0123456789abcdef directory, it will create one named backup-with-corrupted-nvdata-bin-of-fourth-jully-2020. We can change that as we need to make sure that the directory has a name that is self explanatory, not to confuse the half-corrupted backups with ones that are not corrupted at all.
61
62
Alternatively if the replicant_devices, 0123456789abcdef or backup_efs directories don't exist, you can also follow the [[BackupTheEFS]] and rename the directories as you wish once the backup is done. 
63
64
Or if you have an old backup, you could rename its directories to make sure you don't accidentally overwrite it.
65
66
h3. Setup ADB
67
68
The [[BackupTheEFS]] page has instructions to setup ADB. So if you just did a backup you should be good.
69
70
However if you already had an old backup somewhere and didn't do a new one, you will need to make sure that ADB is setup.
71
72
To do that, you can follow the [[ADB|setting up ADB on your computer]] instructions, so that you can access a root shell on your device.
73
74
*NOTE*: when prompted on your Replicant device, make sure that you check the box that says *Always allow from this computer* when you grant your computer USB debugging permissions. Otherwise, you will be unable to obtain root shell access on your Replicant device when you reboot it into the recovery OS to actually perform the EFS backup.
75
76
*NOTE*: for security reasons, you may want to [[BackupTheEFS#Revoke-USB-debugging-permissions|revoke these non-expiring permissions]] once the EFS backup is complete.
77
78
h3. Reboot into the recovery
79
80
You can reboot your device into the recovery by one of the following methods:
81
82
h4. Using ADB
83
84
Run the following on your computer:
85
86
<pre>
87
adb reboot recovery
88
</pre>
89
90
h4. Using your Replicant device's user interface
91
92
Do the following:
93
94
# Make sure that your device is booted under Replicant.
95
# Press the power button until a menu appears.
96
# Press *Reboot*.
97
# Select *Recovery* and then press *ok*.
98
99
h3. Ensure that your device's system partition is mounted
100
101
In order to get a root shell in the recovery, your devices system partition must be mounted.
102
103
On certain devices, "the system partition is already mounted":https://redmine.replicant.us/issues/1934, so you might already be able to get a root shell without mounting the system partition again.
104
105
In any case, it's still best to do the following to make sure that the system partition is mounted:
106
107
# Select *Advanced*.
108
# Select *Mount /system*.
109
# Press the back key to get back to the general menu.
110
111
h3. Copy and pasting commands
112
113
The next sections will have commands that you can copy and paste. To ensure that they work correctly it's best to:
114
* Type them in the same terminal window
115
* Copy/paste one line at the same time. For instance if you copy 10 lines and just paste then, in some cases you could have some issues.
116
117
Also to make sure that they are really executed, you can press the 'enter' key after having pasted them. 
118
119
Pressing 'enter' twice will not hurt as the command will still be executed only once.
120
121
h3. Restore the hidden nv_data.bin
122
123
The nv_data.bin file seem to be the file that is corrupted the most, probably because it is more often written to than the other files.
124
125
On most devices it can be found at several locations:
126
* /efs/nv_data.bin
127
* /efs/.nv_data.bak
128
129
The second one is a backup file. Note the '.' in front of the nv_data.bak. That dot ('.') is part of the file name. So that file name is '.nv_data.bak'.
130
131
When a file name starts with a dot, many tools (like file managers, command line utilities like 'ls', etc) will not list that file by default.
132
133
This convention enables to 'hide' some files. In GNU/Linux, this is typically used to 'hide' the configuration files used by graphical programs. Without that, it wound't be very convenient as you would see hundreds of extra files in your home. And in GNU/Linux the graphical programs still need to store their configuration somewhere.
134
135
For instance if you want to list all the files in your home in GNU/Linux, including the hidden ones you can use the following command:
136
<pre>
137
ls -a ~
138
</pre>
139
140
The '~' is a shortcut/alias for your home, and the '-a' switch tells ls to list all files (including the hidden ones).
141
142
So here we need to replace 'nv_data.bin' with '.nv_data.bak'. And as '.nv_data.bak' is a hidden file, we will use the concept of hidden files later on.
143
144
To do that we first need to mount the modem data partition (named efs) to a directory (here we will use /efs).
145
146
Your device should already be in the recovery, ready to accept commands through adb.
147
148 2 Denis 'GNUtoo' Carikli
Not all the devices have their modem data partition at the same place. Because of that, the mount instructions are device specific.
149 1 Denis 'GNUtoo' Carikli
150 2 Denis 'GNUtoo' Carikli
You can use the following instructions to mount the modem data partition, but make sure that you use the ones that corresponds to your device.
151 1 Denis 'GNUtoo' Carikli
152
h4. Galaxy S 2 (I9100), Galaxy S 3 (I9300), Galaxy S 3 4G (I9305), Galaxy Note (N7000), or Galaxy Note 2 (N7100)
153
154
<pre>
155
adb shell "mkdir /efs"
156
adb shell "mount /dev/block/platform/*/by-name/EFS /efs"
157
</pre>
158
159
h4. Galaxy Nexus (I9250)
160
161
<pre>
162
adb shell "mkdir /efs"
163
adb shell "mount /dev/block/platform/*/*/by-name/efs /efs"
164
</pre>
165
166
h4. Galaxy Tab 2 7.0 (P3100)
167
<pre>
168
adb shell "mkdir /efs"
169
adb shell "mount /dev/block/platform/*/*/by-name/EFS /efs"
170
</pre>
171
172 2 Denis 'GNUtoo' Carikli
h4. Actually restore  the nv_data.bin
173 1 Denis 'GNUtoo' Carikli
174 2 Denis 'GNUtoo' Carikli
Now that the modem data partition is mounted to /efs, we can proceed to replace nv_data.bin.
175 1 Denis 'GNUtoo' Carikli
176
TODO
177
178
<pre>
179
ls -a /efs/
180
</pre>
181
182
TODO
183
184
We will then proceed to replace /efs/nv_data.bin by /efs/.nv_data.bak.
185
186
187
TODO: /efs/.nv_data.bak.md5 and /efs/nv_data.bin.md5
188
189
TODO:
190
<pre>
191
adb shell "umount /efs" 
192
adb shell "rmdir /efs" 
193
</pre>
194
195
h2. Reboot your device
196
197
Once the backup is done, you can reboot your device into Replicant by one of two ways.
198
199
You can reboot by running the following command on your computer:
200
<pre>
201
adb reboot
202
</pre>
203
204
Or alternatively you can use the Replicant recovery graphical user interface to reboot by selecting *Reboot system now*.
205
206
h2. Revoke USB debugging permissions
207
208
If you are concerned about security, you may want to revoke the non-expiring USB debugging permissions that you granted to your computer earlier (which you may of course grant again at any time, as desired). This control is located at *Settings* > *Developer options* > *Revoke USB debugging authorizations*.
209
210
That's it! Your device's EFS partition is now backed up. Your device should be running Replicant normally again.