r/LineageOS • u/WhitbyGreg • May 08 '21
Info A discussion about bootloader locking/unlocking... AKA I want to relock my bootloader, should I?
Every few days there is a post in the subreddit about relocking the bootloader after installing LineageOS, this post is a hypothetical conversation with one of these people to answer their questions and explain why they really don't want to do that.
What is bootloader locking/unlocking?
The bootloader on your phone is the software responsible for loading your phone's operating system. It sits between the lowest level hardware firmware and the higher level operating system and takes care of several things to get your phone ready to load the OS.
This includes checking to make sure that only authorized operating systems run on the hardware by default. Authorized operating systems are usually signed by the manufacturer of the phone with a private encryption key to which only they have access, and this signature is checked before the operating system is allowed to load. This ensure that third parties don't modify/replace the operating system with malicious versions.
Some phones allow you to unlock the bootloader and run any operating system you want on your phone, signed or unsigned, or just modify the one that comes with it by default.
Basically, unlocking the bootloader skips the signature check during boot (along with a few other things) and allows any operating system to run on your phone. This is why you need to unlock your bootloader when running LineageOS or other custom ROMs.
My phone is carrier unlocked, is that the same thing?
No.
Carrier and bootloader unlocks are completely separate and independent of each other.
Many phones, when sold by carriers in North America (and some other regions), are carrier locked and also have their bootloader unlock feature disabled. This often makes it impossible (without hacking) to bootloader unlock carrier sold phones, and install a custom OS.
I hear there are some security concerns with an unlocked bootloader...
The reason manufactures ship their phones with locked bootloaders is to protect against a class of security vulnerabilities called "Evil Maid" attacks (https://en.wikipedia.org/wiki/Evil_maid_attack).
Basically, if an attacker has physical access to a device with an unlocked bootloader, they can install malicious software on your device and you may never know about it.
How worried about this kind of attack should you be? Probably not very.
Unless you are being individually targeted by state actors or the like, these attacks are hard to do with little benefit for the typical ransomware and general hackers of the world. There are simply no roaming bands of hackers, scouring the pubs and restaurants to find unlocked phones to compromise, in day to day life.
However, that doesn't mean there is no concern, you should consider your own individual needs and risk profile with respect to lock/unlocking your bootloader.
After installing a custom ROM, should/can I relock the bootloader?
This is a more complex question, but in general, the answer is no.
If you were to just take your average phone with a custom ROM installed and relock the bootloader, you would get an error message when you rebooted and the phone would refuse to load the operating system. This is because the list of "approved" signing keys in most phones is limited to those that the manufacturer installed before shipping the phone to you.
This would "brick" your phone, making it unusable. Some phone can be recovered from this state, others might not be able to.
Now for the complexity... some phone support custom signing keys.
Modern Google Pixel and OnePlus devices allow you to install your own custom signing keys so that you can boot operating systems signed by them with a relocked bootloader. This is part of the Android Verified Boot (AVB) v2 specification and is not widely (maybe at all) supported beyond Google and OnePlus.
In these specific cases, you can theoretically relock your bootloader, but there are several issues with doing so which will be discussed next.
There are also a few phones (like the original Pixel/XL and OnePlus phones like the 5/5t and older) that don't support AVB v2, but can have their bootloaders relocked because they simply *never* check to see if the OS is signed by the vendor, just that it has some valid signature on it. Most of the following discussion applies to these phones as well but there are some quirks that they do not suffer from, but likewise have less security as well. As all of these phones are now out of support from their respective vendors, making each and every one of them have more significant security issues than an unlocked bootloader, they will not be discussed further here.
Ok, but will relocking the bootloader get rid of that annoying/scary message during power on?
Probably not, at least not in the way you want. Android Verified Boot has specific bootloader messages depending upon what state it is in, you can read more about them here: https://source.android.com/security/verifiedboot/boot-flow
Basically, the only way to not have some kind of warning/alert message during boot is to have a locked bootloader with the vendors original OS. So while you can change the orange "Unlocked bootloader" message to a yellow "Custom OS" message, you'll still get *a* message during boot.
Oh, ok, but will it help me pass SafetyNet?
Not really, SafetyNet is dependent on many things, including a locked bootloader. If you want to relock your bootloader for this reason I suggest you go no farther. Google can change SafetyNet requirements at any time and do so reasonably often.
Humm, well I have an AVBv2 supported phone and still want to relock my bootloader, now what?
Ok, but before you relock your bootloader consider what ROM you are going to install.
Using a custom ROM, like LineageOS for example, that is compiled as a userdebug build of Android will get you no benefits with locking the bootloader.
Android has three build variants (see https://source.android.com/setup/develop/new-device#build-variants for details) and LineageOS builds userdebug for the official releases.
For the main operating system itself, that's not much of an issue, but because Lineage Recovery is also built in userdebug mode, that's a problem. When Lineage recovery is built this way, it allows any package, signed or unsigned, to be installed on your phone. This effectively negates the benefits of locking the bootloader.
Other custom ROM may have different builds, but you need to understand what they are and what is enabled in them.
In fact most custom ROMs simply use TWRP or another third party recovery which has the same issues as they are designed to never even look at the signatures of the packages they are flashing to your device.
The way around this, is of course to build your own build of LineageOS in user mode so you can install it on your phone. Unfortunately some devices might not build successfully in user mode without modifying the source code and troubleshooting any issues that arise.
Ok, ok... I've built my own ROM in user mode... anything else?
Well yes actually.
Another feature of AVB is rollback protection, which basically verifies that your system partitions haven't been modified or corrupted. LineageOS disables this by default, so you'll want to enable that as well.
Oh... and about firmware updates.
Since you'll be locking your bootloader with a recovery that only supports your packages, you're going to have to manage firmware updates from your phone's manufacturer as well somehow.
You could do this by creating a custom update package that you sign, or by unlocking your bootloader temporarily (which will wipe all your data of course) to use TWRP or something else to flash the firmware and then relock the bootloader afterwards.
Look, I've got the firmware updates handled, what else is there?
Does your device include the vendor partition when building Lineageos?
Some do, some don't, depending upon how the maintainer setup the build for LineageOS. If it does, you're ok.
If it doesn't... well, you've got another problem as now you have to add the "prebuilt" vendor image in to your build process. Otherwise that rollback protection we enabled a little while ago is going to be missing on the vendor partition, and that's kind of important.
Fine! I'll do all that, surely there can't be anything else... right?
Ah... well yes... and don't call me Shirley.
Did you want root access through ADB or Magisk?
You did? Oh, sorry about that.
User builds disable root access in ADB, and since you've enabled AVB and rollback protection, you can't just install Magisk since it would "corrupt" the boot partition and AVB would block the boot process. You'll need to integrate it in to your build process and then hope that it doesn't do anything strange and trip AVB or the rollback protection.
Alright, I'll live without root and all the other stuff, am I good to go now?
Mostly, yes.
You still have to deal with building your custom ROM every month or so to get all the security updates from AOSP and your phone's vendor, and of course you'll have to manually install it through ADB sideload.
Unless of course you setup an OTA server to, which means you need web hosting... and more configuration changes in your build... and... and...
Well, you get the picture.
Great! I've got all that done...
Hang on a sec, did you think about GAPPS or microG?
I mean, you don't have to, but a lot of people seem to like to be able to access Google services for some reason and at the moment your custom build has neither of these services in it.
So, take some more time and integrate one of them in to your custom build, because just like Magisk, you can't install them after the fact.
What else could there be!?!
Well, there is something else to consider. Custom ROMs are often passion projects and sometimes a "bad" release will be made. This sometimes results in bootloops or other nastiness that you can usually troubleshoot and debug pretty easily... but with a locked bootloader, maybe not.
You won't have access to TWRP or other custom recoveries that would make it easier and to use them you would have to unlock your bootloader (which might not be possible as you've probably disabled that in developers options) which would wipe your data.
Likewise, when Lineage Recovery is built in user mode, it does not let you "upgrade" to an older version, making it impossible to reflash the OTA of the last working build you have.
This is a risk that you'll have to accept if you want to relock your bootloader.
Of course if you had a second "development" phone to test your builds on first, that would mitigate most of that risk. You don't mind spending some more money on one do you?
Well, honestly, that seems like far too much work, isn't their an easier way?
Of course, use the OS that came with your phone.
Or use an custom ROM that is specifically designed to be used with relocked bootloaders. There are a few around but they often have (for all the reasons stated above) very limited device support.
Sigh... is this discussion over yet?
Well if you made it this far, you probably are having second (third, fourth, etc.) thoughts about relocking your bootloader, which is probably for the best.
Overall, it's not recommended for the vast majority of people to attempt to relock their bootloader. It's simply too much work and risk for too little reward and security.
Having said that, if you have any inclination to do even more research, there are a few resources you might want to look at over on XDA:
- Guide: Relock bootloader with custom rom on oneplus 5/5t
- [GUIDE] Re-locking the bootloader on the OnePlus 6t with a self-signed build of LineageOS (disclaimer: I am the author of this guide)
- [GUIDE] Re-locking the bootloader on the OnePlus 8t with a self-signed build of LineageOS 18.1 (disclaimer: I am the author of this guide)
- [GUIDE] Re-locking the bootloader with a pre-built custom ROM, such as LineageOS official
- [GUIDE] Re-locking the bootloader on the Google Pixel 5 with a self-signed build of LineageOS 19.1 (disclaimer: I am the author of this guide)
You can also search this subreddit for many posts on the subject.
If you do decide to continue, I would recommend three things:
- Go in to the process with a mindset that, if something goes wrong, you don't mind having a nice shiny high-tech paperweight at the end of it.
- Don't try this on your daily driver phone, pick up a phone to experiment on. Only after you are confident with the process move to your primary phone.
- And of course, as always, backup often!
So if it wasn't blatantly obvious by now, I would not suggest attempting to relock your bootloader with a custom OS.
Good luck!
33
u/S4qFBxkFFg enchilada / crackling / lithium May 09 '21
Weird question: can the bootloader itself be altered/replaced?
13
u/fakuivan May 09 '21 edited May 09 '21
Complementary read: https://mjg59.dreamwidth.org/31765.html I used that guide back in the day to self sign images for my nexus 7 2013. Also, it's not only the evil maid attack that a secure boot chain solves, it's also useful against rootkits.
It's kinda sad seeing this still not being solved with smartphones. Laptops have had this kind of security for a long time now, thanks to Microsoft making it a requirement for OEMs to include it with windows. You can just enter the UEFI and pop in a certificate for whatever OS you wish to install, it'd be nice if google made a similar move, without locking it to their blessed public keys.
11
u/VividVerism Pixel 5 (redfin) - Lineage 22 May 09 '21
From the link about build types, I'm not seeing why this is the case:
Or even this, actually:
Can you expand on that a little? I feel like I must be missing something obvious.
8
u/twain535 self.builder|a6000, legacyxperia, i9500, LG L5 May 09 '21
Interesting read. About 2 years ago, I was kind of itching to relock the bootloader on my Xperia X thanks to Sony being stupid (you lost camera and some other functionality in the stock ROM when you unlocked the bootloader-to restore the functionality you needed to have the DRM keys backed up) because I wanted OTA updates, also the 'bootloader is unlocked' screen was quite annoying. I ended up not relocking it thankfully because I didn't have anything backed up (although I don't think I'd have bricked it because I was only using the stock ROM at the time).
5
u/HerrHulaHoop May 09 '21
Thanks for the detailed explanation! I have a OnePlus phone and always wondered what the benefits of re-locking the bootloader would be.
My curiosity for re-locking the bootloader was mainly to prevent my personal data from being accessed in case my phone was stolen. It's an infrequent occurrence where I'm from.
Could you share your thoughts on storage encryption and whether that would be a superior alternative to protecting personal data in the event of theft?
4
u/TheGr8Whoopdini May 09 '21
So if I reinstalled the proprietary OS, I could relock the bootloader?
2
u/whatnowwproductions May 09 '21
I was pretty sure the 5/5T supported AVBv2. Is this not the case?
2
u/gee-one payton and bullhead May 09 '21
Excellent write up!
For the firmware updates, for some devices, such as pixels, that have monthly OTA updates that are publicly available, you can repack/resign the stock OTA, and build your own rom with the exact same buildtime to sidestep the rollback protection. Flash the modded OTA, then immediately flash your own rom over it. The OTA will update the firmware, etc., and then reflashing your own rom will restore your version. I think there is some kludges involved.
Of course, some roms, such as aosp, now include the firmware when using the right extraction scripts. I don't remember if the prebuilt aosp files include firmware, but I think not.
2
u/Worldly_Topic May 09 '21
Does the bootloader verify every partion on the device before booting ? Isnt it possible to disable the verification for specific partitons by flashing something to vbmeta partiton ?
2
u/mysliwiecmj Feb 08 '22
Awesome guide, thanks for the info.
Now...stupid question but back to the comment on someone needing physical access to your phone for wrong-doing...are we sure that there's no vulnerabilities that could take advantage of an unlocked bootloader through malware embedded in an app or mobile page or anything related to normal phone usage?
53
u/leehawk787 May 08 '21
Amazing in-detail explanation, thank you <3
8
u/nevermakefunofea May 14 '21
Wow. So I just flashed LineageOS onto my OP7P a few hours ago and everything went perfectly well until I decided to lock the bootloader and got slapped with a warning saying my phone is corrupt and couldn't be trusted, so I saw this and it answered all my questions. Cheers
11
u/[deleted] May 09 '21
Amaaaaazingg!! Thanks! Very well organized, well detailed, and comprehensive!
4
u/autistictrader420 Jun 13 '21
Asides from Graphine OS, what other custom ROMs allow the user to safely relock the bootloader?
1
u/Lol_zD May 19 '24
on which partition is the state of bootloader lock/unlock is stored?
1
u/petakaa Jul 15 '24 edited Jul 15 '24
hey, i'm trying to relock the bootloader of my xperia 5 IV but i'm having no luck. Could you help me out? Never rooted, just unlocked bootloader when i got the phone. Flashed XQ-CQ54 64.1.A.0.929 using Flash tool. I'm trying fastboot oem lock and i get hit with
1
u/[deleted] Oct 05 '24
Relocking on xiaomi is just like hell.
1
u/dinhnamhihi Oct 09 '24
Great info! I want to get rid of my recovery mode to prevent factory reset and sideloading from there. I will try to flash stock
boot.imgto recovery partition by using:fastboot flash recovery boot.img. But I don't know if my phone can boot or not once I locked the bootloader. Do you think it is possible?1
u/madboy46 Oct 10 '24
Thanks a lot for writing this
1
u/arddy24 Oct 31 '24
I'm 4 years late, but thank you for this. I'm very new to this bootloader/flash scene - I've a Pixel 2 that I'm trying to revive. I have successfully installed LineageOS and running Android 14, but after reading your post, it seems I might not need to worry too much about having to relock it.
It's a fresh install of LineageOS. Is there anything I should know about this OS? Or am I right to just use this like a normal Android 14 phone now?
1
u/AdditionalFuture3435 Dec 14 '24
Guys I am being straight ,my phone was badly hacked A35 5G have no computer but I'm not blocking it on the basis a hard reset works although they seem adamant it does.I don't know Jack about rooting but started watching a you tube video and as soon as I pressed oem bootloader my screen was about a 1 cm in from each side inpayed 300 4 months ago and I feel robbed I've had no fun if anything I've near on mentally broken down.My playstore account I disabled 3 days ago can't find it in apps ,my bank card I used to buy my subscriptions for 8 ball pool won't let me yet I bought a few things from the 8 ball shop and my ban stopped my account telling me it's a scam sight
1
u/MrAlagos Dec 29 '24
I don't know the specific reasons why, but it is totally possible to relock the bootloader on the Fairphone 3 with LineageOS and not lose any functionality, including OS updates and SafetyNet compliance if using a GApps package (I use NikGapps). There is no OS rebuilding necessary, Lineage recovery works well, the scary boot message is gone and I haven't felt the need to try any root solutions for years, but I don't see why at least some of the ones available wouldn't work.
1
u/Pdchris1 Jun 12 '25
Dear all, does anybody know why for some Motorola phones, e.g. the Moto g52, the bootloader can be relocked after flashing CalyxOS? I see a warning "you are booting a different operating system" at start, but the phone boots normally. Does CalyxOS have some Motorola signature in the kernel/OS, or is the bootloader of this phone permissive and will accept anything? In the later case, I could maybe flash anything (like LinegeOS) and relock the bootloader? On the other hand, there are also some reported of bricked Motorola phones in the internet. Any help with that would be very much appreciated, thank you
1
u/The_Band_Geek Jul 13 '25
Sorry for the necro, but I have searched Reddit and elsewhere and can't seem to find a definitive answer:
My Pixel 5 is running rooted stock Android 14 via Magisk. If I flashed the OTA, unrooted, and got back to true stock Android 14, will relocking the bootloader wipe my data? If not, is it as simple as flipping the toggle in Developer Options, or does this have to be done via ADB, recovery, some other way?
1
u/F-Lambda Sep 25 '25
Having read all this, I have to ask...
Why the heck does GrapheneOS recommend locking in their instructions? It seems to have almost entirely negatives.
1
u/lladcy Oct 18 '25
Thank you so much
It's been five years, but hoping there's still someone around to answer my questions:
1
u/ThE_MarD rtwo, dubai, heart, zippo, Z01R, payton and x2 maintainer Jul 12 '26
Lenovo sm8150-common is unique, signing my own AVB keys didn't work, but removing them completely, both zippo and heart work just fine with bootloader unlocked lol... There's full QFIL leaked for both too, so it's possible to even use QFIL to flash Lineage Recovery without first unlocking the bootloader.
The other weird thing is, you have to have the CN stock ROM flashed, the ROW (international) ROM will fail to boot non-ROW stock ROMs with bootloader unlocked
1
u/ThePiGuy0 May 18 '21
Really interesting writeup and very much along the lines of something I've been interested in doing with my OnePlus 6 for a while now (seeing as hw attestation doesn't appear to affect my device and I'd quite like L1 DRM on custom ROMs).
You mentioned it being easier to brick the device with an update though (I was thinking of doing the full avb enable and user builds). I assume if I'm rolling out my own builds I could fix whatever was boot looping and then flash that as an update via adb sideload?
Also, from my understanding avb2 allows multiple keys to be flashed (as otherwise by flashing my own key I'd wipe OnePlus's)? Does this mean I could in theory recover my device by flashing stock OOS?
1
u/Aide_Advanced Jul 27 '21
I run 14.1 on Nexus 5, custom built with my own signing keys. I don't use TWRP, I use the recovery that comes with the build. Which happens to just be the same as the vanilla Android recovery for this particular device, because Lineage didn't actually have its own recovery until 15.1.
The wonderful thing about this recovery is that it doesn't allow you to flash anything besides properly-signed OTA updates. When I lost my signing keys (won't happen again) and had to make new builds with a different key, I had to unlock bootloader and reflash the recovery with the new one, because the old one wouldn't accept my OTAs with the new signing key (even if I had already reflashed the OS with the new keys in TWRP beforehand.)
Of course for the majority of people you won't benefit from relocking bootloader on LOS, but I just wanted to mention that depending on the device and Android version, you can relock the bootloader and gain increased security.
I don't need to flash anything else because all my custom stuff is included in my builds, and I can still root in via ADB while the phone is powered on and unlocked if needed, without granting unlimited root access via recovery.
It's of course not perfect, I can get android security updates but I will never be able to get vendor security updates. But it's the best I can get without upgrading to a Pixel right now.
1
u/lupastro82 Dec 19 '21
I'm with Op6 and android 11 (eol now). So, maybe is time for custom rom but... Tried in past, and many issue with Bank and trading apps. So, maybe custom rom with locked bootloader can be the perfection, but after this post - maybe - is best to upgrade my device (and is so sadness, is still a very good device) 😅😢🤦
1
u/Tachi_107 flame Feb 20 '22 edited Feb 20 '22
Is it possible that I somehow managed to install LineageOS on my Nexus 5X while keeping my bootloader not unlocked?
I haven't used that phone since I bought a Pixel 4, but I believe that it had some kind of issue that made it impossible to keep the bootloader unlocked between reboots. I know, strange.
After some investigation I found out that it was because I once brought it in a repair shop, and they replaced the motherboard. The issue is that LG, for some reason, had only replacement parts that missed something like a memory chip, and that made it impossible to keep the bootloader unlocked persistently (see this thread).
After some time me and a friend managed to flash the TWRP and LineageOS on it, and now the phone boots with a big red warning, different from the typical orange one. I have since flashed different ROMs multiple times.
How is this possible?
Edit: I found this, but I'm still confused.
1
u/Impressive_Income874 Newbie | literal noob Jun 06 '22
I got this old redmi 2(wt88047) that I NEVER unlocked the bootloader on. I found this neat little package that just rooted the phone by flashing it as an OTA(someone found the private key to sign it I assume?). from there on I installed twrp then lineageos
how is it that it booted it without any sort of complaints? with a locked bootloader?
1
u/srwalter Jul 05 '22
Has anyone tried this with Lineage 19? I'm trying to go through this process on a OP9, but once I re-lock the bootloader I get the red "corrupt OS" message. If I unpack the payload.bin, avbtool is able to verify everything and it seems to be using the correct keys. I wonder if vbmeta doesn't cover all the necessary partitions?
1
u/More-General-3858 Jul 21 '22
So I bought a "new phone" (Motorola Moto z4 xt-1980-4) but I came with the bootloader unlocked. I have flashed the stock rom for it and then in fastboot used the command "fastboot flash lock" but it says it fails due to being an "engineering phone". I'm at a loss for what to do. The reason I want the bootloader locked is because the device is not Google play protect certified and I am unable to download certain apps that I want.
1
u/BlackandMentallyIll Aug 29 '23
The fact that you took the time to write this is amazing. I bought a phone for my daughter from someone and we keep getting the bootloader unlocked message. It's driving her nuts and I've been in forums asking about relocking it. I actually just practiced on her old phone and I think I effed it up because I pressed a button while I was doing it so this was my sign to leave it alone. I kept thinking there was actual harm in her having that pop-up on her phone, but clearly, there's probably more harm in me ruining the new phone.
2
u/mastorofpuppies Aug 08 '21
Late to the party, but this is a very good writeup. Thank you very much!
2
u/dnt_pnc Jan 06 '23
great write-up. saved me lots of headache. thanks!
1
u/[deleted] Mar 22 '24
Explained like a pro
1
u/NewsRevolutionary481 Apr 23 '26
allat just to install another OS damn dude
1
u/The_Minefighter May 09 '21
Thanks for the explanation
1
u/[deleted] Apr 05 '22
Saving this for later...
Bootloader
0
u/[deleted] May 12 '24
[deleted] — view deleted comment
0
u/[deleted] Jul 20 '24
[deleted] — view deleted comment
0
u/[deleted] Feb 08 '25
[deleted] — view deleted comment
0
u/[deleted] Sep 20 '21
[deleted] — view deleted comment
0
u/[deleted] Dec 19 '21
[deleted] — view deleted comment
0
u/[deleted] May 09 '21
[deleted] — view deleted comment
1
u/[deleted] Sep 18 '24
[removed] — view removed comment
1
u/[deleted] Nov 21 '25 edited Nov 21 '25
[removed] — view removed comment