Andrew Scull | 5d7027d | 2017-04-12 11:46:27 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.oemlock; |
| 18 | |
| 19 | import android.annotation.Nullable; |
| 20 | import android.content.Context; |
| 21 | import android.os.UserHandle; |
| 22 | import android.os.UserManager; |
| 23 | import android.service.persistentdata.PersistentDataBlockManager; |
| 24 | import android.util.Slog; |
| 25 | |
| 26 | /** |
| 27 | * Implementation of the OEM lock using the persistent data block to communicate with the |
| 28 | * bootloader. |
| 29 | * |
| 30 | * The carrier flag is stored as a user restriction on the system user. The user flag is set in the |
| 31 | * presistent data block but depends on the carrier flag. |
| 32 | */ |
| 33 | class PersistentDataBlockLock extends OemLock { |
| 34 | private static final String TAG = "OemLock"; |
| 35 | |
| 36 | private Context mContext; |
| 37 | |
| 38 | PersistentDataBlockLock(Context context) { |
| 39 | mContext = context; |
| 40 | } |
| 41 | |
| 42 | @Override |
Andrew Scull | 23a1a5f | 2018-11-27 16:45:58 +0000 | [diff] [blame] | 43 | @Nullable |
| 44 | String getLockName() { |
| 45 | return ""; |
| 46 | } |
| 47 | |
| 48 | @Override |
Andrew Scull | 5d7027d | 2017-04-12 11:46:27 +0100 | [diff] [blame] | 49 | void setOemUnlockAllowedByCarrier(boolean allowed, @Nullable byte[] signature) { |
| 50 | // Note: this implementation does not require a signature |
| 51 | if (signature != null) { |
| 52 | Slog.w(TAG, "Signature provided but is not being used"); |
| 53 | } |
| 54 | |
| 55 | // Continue using user restriction for backwards compatibility |
| 56 | UserManager.get(mContext).setUserRestriction( |
| 57 | UserManager.DISALLOW_OEM_UNLOCK, !allowed, UserHandle.SYSTEM); |
| 58 | |
| 59 | if (!allowed) { |
| 60 | disallowUnlockIfNotUnlocked(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | boolean isOemUnlockAllowedByCarrier() { |
| 66 | return !UserManager.get(mContext) |
| 67 | .hasUserRestriction(UserManager.DISALLOW_OEM_UNLOCK, UserHandle.SYSTEM); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | void setOemUnlockAllowedByDevice(boolean allowedByDevice) { |
| 72 | // The method name is misleading as it really just means whether or not the device can be |
| 73 | // unlocked but doesn't actually do any unlocking. |
| 74 | final PersistentDataBlockManager pdbm = (PersistentDataBlockManager) |
| 75 | mContext.getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE); |
Howard Chen | f1c7648 | 2019-06-06 11:36:48 +0800 | [diff] [blame] | 76 | if (pdbm == null) { |
| 77 | Slog.w(TAG, "PersistentDataBlock is not supported on this device"); |
| 78 | return; |
| 79 | } |
Andrew Scull | 5d7027d | 2017-04-12 11:46:27 +0100 | [diff] [blame] | 80 | pdbm.setOemUnlockEnabled(allowedByDevice); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | boolean isOemUnlockAllowedByDevice() { |
| 85 | final PersistentDataBlockManager pdbm = (PersistentDataBlockManager) |
| 86 | mContext.getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE); |
Howard Chen | f1c7648 | 2019-06-06 11:36:48 +0800 | [diff] [blame] | 87 | if (pdbm == null) { |
| 88 | Slog.w(TAG, "PersistentDataBlock is not supported on this device"); |
| 89 | return false; |
| 90 | } |
Andrew Scull | 5d7027d | 2017-04-12 11:46:27 +0100 | [diff] [blame] | 91 | return pdbm.getOemUnlockEnabled(); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Update state to prevent the bootloader from being able to unlock the device unless the device |
| 96 | * has already been unlocked by the bootloader in which case it is too late as it would remain |
| 97 | * unlocked. |
| 98 | */ |
| 99 | private void disallowUnlockIfNotUnlocked() { |
| 100 | final PersistentDataBlockManager pdbm = (PersistentDataBlockManager) |
| 101 | mContext.getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE); |
Howard Chen | f1c7648 | 2019-06-06 11:36:48 +0800 | [diff] [blame] | 102 | if (pdbm == null) { |
| 103 | Slog.w(TAG, "PersistentDataBlock is not supported on this device"); |
| 104 | return; |
| 105 | } |
Andrew Scull | 5d7027d | 2017-04-12 11:46:27 +0100 | [diff] [blame] | 106 | if (pdbm.getFlashLockState() != PersistentDataBlockManager.FLASH_LOCK_UNLOCKED) { |
| 107 | pdbm.setOemUnlockEnabled(false); |
| 108 | } |
| 109 | } |
| 110 | } |