[OnePlus X General] SebiderSushi:
If your persist partition gets damaged this can break your sensors. Sometimes that just happens during a faulty operation while flashing.

Some ROMs might even fail to boot and in some weird situations the screen may rotate to the opposite direction.

To fix persist you can try these methods. If none of them work flash the lastest OxygenOS (v3.1.4) and try again. This doesn't work with all ROMs.

Sensors can be checked using apps like CPU Info (Google Play), CPU Info (F-Droid) or SatStat (F-Droid).

Method A) Restore an older TWRP backup of persist

If this does not work or you never made a backup, keep reading for advanced options.​

Method B) Flash the lastest OxygenOS and reboot.

OxygenOS can recreate missing sensor calibration data. Some custom ROMs can but not all of them.​

If this does not work, keep reading for advanced options.​

Once everything works again take a TWRP backup of persist you can always return to.

While you're at it, back up EFS as well if you don't know where to find a backup or you never made one.

More about what every partition does, including EFS and persist.

Congrats, you're done!

If you need to move to the advanced methods, take a TWRP backup of your current persist and keep it no matter what.

If none of them fixed your issues, restore this backup of persist before trying any other approach.

Copy the backup to your PC now to make sure you don't lose it on accident!

To open a shell on TWRP go to Advanced > Terminal or run adb shell on your PC.

If you really can't use TWRP or another custom recovery for this you can also try the following methods at runtime if your ROM is rooted.

Do this at your own risk.

If you can't take a TWRP backup of persist, e.g. because it is unmountable:

cd /sdcard; backup=persist_$(date +%Y-%m-%d_%H-%M-%S)_broken.img

dd bs=64k if=/dev/block/platform/msm_sdcc.1/by-name/persist | gzip > "$backup".gz

sha256sum /dev/block/platform/msm_sdcc.1/by-name/persist "$backup".gz > "$backup".sha2

To restore:

(gzip -d | dd bs=64k of=/dev/block/platform/msm_sdcc.1/by-name/persist) < <backup>.gz

sha256sum -c <backup>.sha2

Copy the backup to your PC now to make sure you don't lose it on accident!

These methods were tested with TWRP v3.4.0-0

Method C) Repair the filesystem on persist

- This may or may not work. This can help if you get the TWRP error Failed to mount '/persist'​

- Make sure that Persist is unchecked in the Mount section in TWRP.​

e2fsck -f /dev/block/platform/msm_sdcc.1/by-name/persist​

If e2fsck asks questions you don't know how to answer cancel with Ctrl+C and ask someone for help. Otherwise just try whatever is available, restore your backup and try again if needed.​

mount /persist (this should give no errors)​

- Reboot​

Method D) Restore file permissions

- This may or may not work. This only works if persist is mountable.​

- It is possible that this method works better at runtime on a rooted ROM.​

- This should fix the File-system permissions and SELinux Contexts of sensor related files​

mount /persist (this should give no errors)​

restorecon -R /persist/sensors/ /persist/sns.reg​

chmod u+rwx /persist/sns.reg; chmod 775 /persist/sensors/; chmod u+rw /persist/sensors/*​

- Verify permissions​

ls -lZ /persist/sensors/ /persist/sns.reg​

If this says "unlabeled" anywhere run​

chcon -R u:object_r:sensors_persist_file:s0 /persist/sns.reg /persist/sensors/​

(This command may also help if the file contexts are different and sensors are still broken after the next reboot)​

- Reboot​

Method E) Manually recreate sns.reg

- This may or may not work. This only works if persist is mountable. This should work with a custom ROM installed.​

mount /persist (this should give no errors)​

rm /persist/sns.reg​

touch /persist/sns.reg​

chcon u:object_r:sensors_persist_file:s0 /persist/sns.reg​

chmod u+rwx /persist/sns.reg​

- Reboot​

It is recommended to try the following methods with OxygenOS 3.1.4 installed. Not all custom ROMs are able to regenerate sns.reg

Method F) Delete sensor related files from persist

- This may or may not work. This only works if persist is mountable.​

mount /persist (this should give no errors)​

rm -fr /persist/sensors/ /persist/sns.reg​

- Reboot​

Method G) Reformat persist, if all else fails, known to work

This method was tested to fix sensors without creating obvious issues but wiping persist could have unknown side-effects in the long run.

Only do it if you have to. Make sure you know what persist if used for so you can recognize possbile issues if they occur and keep your initial backup around.

- Tested with OxygenOS 3.1.4 and TWRP v3.4.0-0​

- This will get you a completely fresh start. This helps if you get the TWRP error Failed to mount '/persist'​

- Make sure that Persist is unchecked in the Mount section in TWRP.​

make_ext4fs /dev/block/platform/msm_sdcc.1/by-name/persist​

reboot​

- Note: Recent TWRP builds for the OnePlus X store settings in /persist/.twrps and /sdcard/TWRP/.twrps and will reset when wiping both at the same time.​

That's it!

Remember to take a backup of EFS and Persist now if everything is working or restore the backup you took at the start if you're still having issues and want to try other solutions.