It’s perfectly legal for you to live in the property rent free.
It’s then perfectly legal for him to lock you out because he doesn’t want you in the property any more.
If you are a lodger and your friend lives in the property he would only have to give you one months notice even if you paid rent. There is no way around that if he lives in the property. You need a notification in that case of paying a nominal rent, 1 pound, every month and the rent needs to be paid. But it only gives you one month security and then he could lock you out after giving you one month notice.
Otherwise if he doesn’t live in the property you need a tenancy agreement, Assured Short hold Tenancy, at a monthly rental of 1 pound.
You would need to speak to someone else about your immigration status and whether you were entitled to rent property in the UK because that’s a completely different issue.
Thank you for letting me assist you with your legal question. I am glad that I was able to help.
I am not certain whether that answers the question for you or not, but I am happy to answer any specific points arising from this.
It will be my pleasure to help you again either further with this or any future questions you have