Kiyoung Kim | 48f3778 | 2021-07-07 12:42:39 +0900 | [diff] [blame] | 1 | // Copyright 2021 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
Kiyoung Kim | 48f3778 | 2021-07-07 12:42:39 +0900 | [diff] [blame] | 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package snapshot |
| 15 | |
| 16 | import "android/soong/android" |
| 17 | |
| 18 | // Interface for modules which can be captured in the recovery snapshot. |
| 19 | type RecoverySnapshotModuleInterface interface { |
| 20 | SnapshotModuleInterfaceBase |
| 21 | InRecovery() bool |
| 22 | ExcludeFromRecoverySnapshot() bool |
| 23 | } |
| 24 | |
Kiyoung Kim | 48f3778 | 2021-07-07 12:42:39 +0900 | [diff] [blame] | 25 | func RecoverySnapshotSingleton() android.Singleton { |
Colin Cross | 6c7e8ff | 2024-01-17 11:09:16 -0800 | [diff] [blame] | 26 | return &SnapshotSingleton{ |
| 27 | "recovery", // name |
| 28 | "SOONG_RECOVERY_SNAPSHOT_ZIP", // makeVar |
| 29 | android.OptionalPath{}, // snapshotZipFile |
| 30 | RecoverySnapshotImageSingleton, // Image |
| 31 | false, // Fake |
| 32 | } |
Kiyoung Kim | 48f3778 | 2021-07-07 12:42:39 +0900 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | // Determine if a dir under source tree is an SoC-owned proprietary directory based |
| 36 | // on recovery snapshot configuration |
| 37 | // Examples: device/, vendor/ |
| 38 | func isRecoveryProprietaryPath(dir string, deviceConfig android.DeviceConfig) bool { |
| 39 | return RecoverySnapshotSingleton().(*SnapshotSingleton).Image.IsProprietaryPath(dir, deviceConfig) |
| 40 | } |
| 41 | |
| 42 | func IsRecoveryProprietaryModule(ctx android.BaseModuleContext) bool { |
| 43 | |
| 44 | // Any module in a recovery proprietary path is a recovery proprietary |
| 45 | // module. |
| 46 | if isRecoveryProprietaryPath(ctx.ModuleDir(), ctx.DeviceConfig()) { |
| 47 | return true |
| 48 | } |
| 49 | |
| 50 | // However if the module is not in a recovery proprietary path, it may |
| 51 | // still be a recovery proprietary module. This happens for cc modules |
| 52 | // that are excluded from the recovery snapshot, and it means that the |
| 53 | // vendor has assumed control of the framework-provided module. |
| 54 | |
| 55 | if c, ok := ctx.Module().(RecoverySnapshotModuleInterface); ok { |
| 56 | if c.ExcludeFromRecoverySnapshot() { |
| 57 | return true |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return false |
| 62 | } |
| 63 | |
| 64 | var RecoverySnapshotImageName = "recovery" |
| 65 | |
| 66 | type RecoverySnapshotImage struct{} |
| 67 | |
| 68 | func (RecoverySnapshotImage) Init(ctx android.RegistrationContext) { |
LaMont Jones | 0c10e4d | 2023-05-16 00:58:37 +0000 | [diff] [blame] | 69 | ctx.RegisterParallelSingletonType("recovery-snapshot", RecoverySnapshotSingleton) |
Kiyoung Kim | 48f3778 | 2021-07-07 12:42:39 +0900 | [diff] [blame] | 70 | } |
| 71 | |
Jose Galmes | d7d99be | 2021-11-05 14:04:54 -0700 | [diff] [blame] | 72 | func (RecoverySnapshotImage) RegisterAdditionalModule(ctx android.RegistrationContext, name string, factory android.ModuleFactory) { |
| 73 | ctx.RegisterModuleType(name, factory) |
| 74 | } |
| 75 | |
Kiyoung Kim | 48f3778 | 2021-07-07 12:42:39 +0900 | [diff] [blame] | 76 | func (RecoverySnapshotImage) shouldGenerateSnapshot(ctx android.SingletonContext) bool { |
| 77 | // RECOVERY_SNAPSHOT_VERSION must be set to 'current' in order to generate a |
| 78 | // snapshot. |
| 79 | return ctx.DeviceConfig().RecoverySnapshotVersion() == "current" |
| 80 | } |
| 81 | |
| 82 | func (RecoverySnapshotImage) InImage(m SnapshotModuleInterfaceBase) func() bool { |
| 83 | r, ok := m.(RecoverySnapshotModuleInterface) |
| 84 | |
| 85 | if !ok { |
| 86 | // This module does not support recovery snapshot |
| 87 | return func() bool { return false } |
| 88 | } |
| 89 | return r.InRecovery |
| 90 | } |
| 91 | |
| 92 | func (RecoverySnapshotImage) IsProprietaryPath(dir string, deviceConfig android.DeviceConfig) bool { |
| 93 | return isDirectoryExcluded(dir, deviceConfig.RecoverySnapshotDirsExcludedMap(), deviceConfig.RecoverySnapshotDirsIncludedMap()) |
| 94 | } |
| 95 | |
| 96 | func (RecoverySnapshotImage) ExcludeFromSnapshot(m SnapshotModuleInterfaceBase) bool { |
| 97 | r, ok := m.(RecoverySnapshotModuleInterface) |
| 98 | |
| 99 | if !ok { |
| 100 | // This module does not support recovery snapshot |
| 101 | return true |
| 102 | } |
| 103 | return r.ExcludeFromRecoverySnapshot() |
| 104 | } |
| 105 | |
| 106 | func (RecoverySnapshotImage) IsUsingSnapshot(cfg android.DeviceConfig) bool { |
| 107 | recoverySnapshotVersion := cfg.RecoverySnapshotVersion() |
| 108 | return recoverySnapshotVersion != "current" && recoverySnapshotVersion != "" |
| 109 | } |
| 110 | |
| 111 | func (RecoverySnapshotImage) TargetSnapshotVersion(cfg android.DeviceConfig) string { |
| 112 | return cfg.RecoverySnapshotVersion() |
| 113 | } |
| 114 | |
| 115 | func (RecoverySnapshotImage) ExcludeFromDirectedSnapshot(cfg android.DeviceConfig, name string) bool { |
| 116 | // If we're using full snapshot, not directed snapshot, capture every module |
| 117 | if !cfg.DirectedRecoverySnapshot() { |
| 118 | return false |
| 119 | } |
| 120 | // Else, checks if name is in RECOVERY_SNAPSHOT_MODULES. |
| 121 | return !cfg.RecoverySnapshotModules()[name] |
| 122 | } |
| 123 | |
| 124 | func (RecoverySnapshotImage) ImageName() string { |
| 125 | return RecoverySnapshotImageName |
| 126 | } |
| 127 | |
| 128 | var RecoverySnapshotImageSingleton RecoverySnapshotImage |
| 129 | |
| 130 | func init() { |
| 131 | RecoverySnapshotImageSingleton.Init(android.InitRegistrationContext) |
| 132 | } |