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 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 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 | |
| 25 | var recoverySnapshotSingleton = SnapshotSingleton{ |
| 26 | "recovery", // name |
| 27 | "SOONG_RECOVERY_SNAPSHOT_ZIP", // makeVar |
| 28 | android.OptionalPath{}, // snapshotZipFile |
| 29 | RecoverySnapshotImageSingleton, // Image |
| 30 | false, // Fake |
| 31 | } |
| 32 | |
| 33 | func RecoverySnapshotSingleton() android.Singleton { |
| 34 | return &recoverySnapshotSingleton |
| 35 | } |
| 36 | |
| 37 | // Determine if a dir under source tree is an SoC-owned proprietary directory based |
| 38 | // on recovery snapshot configuration |
| 39 | // Examples: device/, vendor/ |
| 40 | func isRecoveryProprietaryPath(dir string, deviceConfig android.DeviceConfig) bool { |
| 41 | return RecoverySnapshotSingleton().(*SnapshotSingleton).Image.IsProprietaryPath(dir, deviceConfig) |
| 42 | } |
| 43 | |
| 44 | func IsRecoveryProprietaryModule(ctx android.BaseModuleContext) bool { |
| 45 | |
| 46 | // Any module in a recovery proprietary path is a recovery proprietary |
| 47 | // module. |
| 48 | if isRecoveryProprietaryPath(ctx.ModuleDir(), ctx.DeviceConfig()) { |
| 49 | return true |
| 50 | } |
| 51 | |
| 52 | // However if the module is not in a recovery proprietary path, it may |
| 53 | // still be a recovery proprietary module. This happens for cc modules |
| 54 | // that are excluded from the recovery snapshot, and it means that the |
| 55 | // vendor has assumed control of the framework-provided module. |
| 56 | |
| 57 | if c, ok := ctx.Module().(RecoverySnapshotModuleInterface); ok { |
| 58 | if c.ExcludeFromRecoverySnapshot() { |
| 59 | return true |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return false |
| 64 | } |
| 65 | |
| 66 | var RecoverySnapshotImageName = "recovery" |
| 67 | |
| 68 | type RecoverySnapshotImage struct{} |
| 69 | |
| 70 | func (RecoverySnapshotImage) Init(ctx android.RegistrationContext) { |
| 71 | ctx.RegisterSingletonType("recovery-snapshot", RecoverySnapshotSingleton) |
| 72 | } |
| 73 | |
Jose Galmes | d7d99be | 2021-11-05 14:04:54 -0700 | [diff] [blame] | 74 | func (RecoverySnapshotImage) RegisterAdditionalModule(ctx android.RegistrationContext, name string, factory android.ModuleFactory) { |
| 75 | ctx.RegisterModuleType(name, factory) |
| 76 | } |
| 77 | |
Kiyoung Kim | 48f3778 | 2021-07-07 12:42:39 +0900 | [diff] [blame] | 78 | func (RecoverySnapshotImage) shouldGenerateSnapshot(ctx android.SingletonContext) bool { |
| 79 | // RECOVERY_SNAPSHOT_VERSION must be set to 'current' in order to generate a |
| 80 | // snapshot. |
| 81 | return ctx.DeviceConfig().RecoverySnapshotVersion() == "current" |
| 82 | } |
| 83 | |
| 84 | func (RecoverySnapshotImage) InImage(m SnapshotModuleInterfaceBase) func() bool { |
| 85 | r, ok := m.(RecoverySnapshotModuleInterface) |
| 86 | |
| 87 | if !ok { |
| 88 | // This module does not support recovery snapshot |
| 89 | return func() bool { return false } |
| 90 | } |
| 91 | return r.InRecovery |
| 92 | } |
| 93 | |
| 94 | func (RecoverySnapshotImage) IsProprietaryPath(dir string, deviceConfig android.DeviceConfig) bool { |
| 95 | return isDirectoryExcluded(dir, deviceConfig.RecoverySnapshotDirsExcludedMap(), deviceConfig.RecoverySnapshotDirsIncludedMap()) |
| 96 | } |
| 97 | |
| 98 | func (RecoverySnapshotImage) ExcludeFromSnapshot(m SnapshotModuleInterfaceBase) bool { |
| 99 | r, ok := m.(RecoverySnapshotModuleInterface) |
| 100 | |
| 101 | if !ok { |
| 102 | // This module does not support recovery snapshot |
| 103 | return true |
| 104 | } |
| 105 | return r.ExcludeFromRecoverySnapshot() |
| 106 | } |
| 107 | |
| 108 | func (RecoverySnapshotImage) IsUsingSnapshot(cfg android.DeviceConfig) bool { |
| 109 | recoverySnapshotVersion := cfg.RecoverySnapshotVersion() |
| 110 | return recoverySnapshotVersion != "current" && recoverySnapshotVersion != "" |
| 111 | } |
| 112 | |
| 113 | func (RecoverySnapshotImage) TargetSnapshotVersion(cfg android.DeviceConfig) string { |
| 114 | return cfg.RecoverySnapshotVersion() |
| 115 | } |
| 116 | |
| 117 | func (RecoverySnapshotImage) ExcludeFromDirectedSnapshot(cfg android.DeviceConfig, name string) bool { |
| 118 | // If we're using full snapshot, not directed snapshot, capture every module |
| 119 | if !cfg.DirectedRecoverySnapshot() { |
| 120 | return false |
| 121 | } |
| 122 | // Else, checks if name is in RECOVERY_SNAPSHOT_MODULES. |
| 123 | return !cfg.RecoverySnapshotModules()[name] |
| 124 | } |
| 125 | |
| 126 | func (RecoverySnapshotImage) ImageName() string { |
| 127 | return RecoverySnapshotImageName |
| 128 | } |
| 129 | |
| 130 | var RecoverySnapshotImageSingleton RecoverySnapshotImage |
| 131 | |
| 132 | func init() { |
| 133 | RecoverySnapshotImageSingleton.Init(android.InitRegistrationContext) |
| 134 | } |