Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 1 | // Copyright (C) 2020 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 | |
| 15 | package filesystem |
| 16 | |
| 17 | import ( |
Jooyung Han | 65f402b | 2022-04-21 14:24:04 +0900 | [diff] [blame] | 18 | "crypto/sha256" |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 19 | "fmt" |
Jooyung Han | 65f402b | 2022-04-21 14:24:04 +0900 | [diff] [blame] | 20 | "io" |
Inseob Kim | 14199b0 | 2021-02-09 21:18:31 +0900 | [diff] [blame] | 21 | "path/filepath" |
Cole Faust | 4a2a7c9 | 2024-03-12 12:44:40 -0700 | [diff] [blame] | 22 | "slices" |
Nikita Ioffe | 2c8cdc6 | 2024-03-27 22:19:30 +0000 | [diff] [blame] | 23 | "strconv" |
Inseob Kim | 14199b0 | 2021-02-09 21:18:31 +0900 | [diff] [blame] | 24 | "strings" |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 25 | |
| 26 | "android/soong/android" |
Jooyung Han | e606759 | 2023-03-16 13:11:17 +0900 | [diff] [blame] | 27 | "android/soong/cc" |
Spandan Das | 9263188 | 2024-10-28 22:49:38 +0000 | [diff] [blame] | 28 | "android/soong/linkerconfig" |
Jiyong Park | 65b6224 | 2020-11-25 12:44:59 +0900 | [diff] [blame] | 29 | |
| 30 | "github.com/google/blueprint" |
Jiyong Park | 71baa76 | 2021-01-18 21:11:03 +0900 | [diff] [blame] | 31 | "github.com/google/blueprint/proptools" |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 32 | ) |
| 33 | |
| 34 | func init() { |
Jooyung Han | 9706cbc | 2021-04-15 22:43:48 +0900 | [diff] [blame] | 35 | registerBuildComponents(android.InitRegistrationContext) |
| 36 | } |
| 37 | |
| 38 | func registerBuildComponents(ctx android.RegistrationContext) { |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 39 | ctx.RegisterModuleType("android_filesystem", FilesystemFactory) |
Jiyong Park | f46b1af | 2024-04-05 18:13:33 +0900 | [diff] [blame] | 40 | ctx.RegisterModuleType("android_filesystem_defaults", filesystemDefaultsFactory) |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 41 | ctx.RegisterModuleType("android_system_image", SystemImageFactory) |
Jiyong Park | bc48548 | 2022-11-15 22:31:49 +0900 | [diff] [blame] | 42 | ctx.RegisterModuleType("avb_add_hash_footer", avbAddHashFooterFactory) |
Inseob Kim | 87230e6 | 2023-11-22 18:55:07 +0900 | [diff] [blame] | 43 | ctx.RegisterModuleType("avb_add_hash_footer_defaults", avbAddHashFooterDefaultsFactory) |
Alice Wang | 000e3a3 | 2023-01-03 16:11:20 +0000 | [diff] [blame] | 44 | ctx.RegisterModuleType("avb_gen_vbmeta_image", avbGenVbmetaImageFactory) |
Inseob Kim | 87230e6 | 2023-11-22 18:55:07 +0900 | [diff] [blame] | 45 | ctx.RegisterModuleType("avb_gen_vbmeta_image_defaults", avbGenVbmetaImageDefaultsFactory) |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | type filesystem struct { |
| 49 | android.ModuleBase |
| 50 | android.PackagingBase |
Jiyong Park | f46b1af | 2024-04-05 18:13:33 +0900 | [diff] [blame] | 51 | android.DefaultableModuleBase |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 52 | |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 53 | properties FilesystemProperties |
Jiyong Park | 71baa76 | 2021-01-18 21:11:03 +0900 | [diff] [blame] | 54 | |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 55 | // Function that builds extra files under the root directory and returns the files |
| 56 | buildExtraFiles func(ctx android.ModuleContext, root android.OutputPath) android.OutputPaths |
| 57 | |
Jeongik Cha | 54bf875 | 2024-02-08 10:44:37 +0900 | [diff] [blame] | 58 | // Function that filters PackagingSpec in PackagingBase.GatherPackagingSpecs() |
| 59 | filterPackagingSpec func(spec android.PackagingSpec) bool |
Jooyung Han | 0fbbc2b | 2022-03-25 12:35:46 +0900 | [diff] [blame] | 60 | |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 61 | output android.OutputPath |
| 62 | installDir android.InstallPath |
Jooyung Han | 0fbbc2b | 2022-03-25 12:35:46 +0900 | [diff] [blame] | 63 | |
Kiyoung Kim | 99a954d | 2024-06-21 14:22:20 +0900 | [diff] [blame] | 64 | fileListFile android.OutputPath |
| 65 | |
| 66 | // Keeps the entries installed from this filesystem |
Jooyung Han | 0fbbc2b | 2022-03-25 12:35:46 +0900 | [diff] [blame] | 67 | entries []string |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 68 | } |
| 69 | |
Spandan Das | 69464c3 | 2024-10-25 20:08:06 +0000 | [diff] [blame] | 70 | type SymlinkDefinition struct { |
Inseob Kim | 14199b0 | 2021-02-09 21:18:31 +0900 | [diff] [blame] | 71 | Target *string |
| 72 | Name *string |
| 73 | } |
| 74 | |
Jihoon Kang | 98047cf | 2024-10-02 17:13:54 +0000 | [diff] [blame] | 75 | type FilesystemProperties struct { |
Jiyong Park | 71baa76 | 2021-01-18 21:11:03 +0900 | [diff] [blame] | 76 | // When set to true, sign the image with avbtool. Default is false. |
| 77 | Use_avb *bool |
| 78 | |
| 79 | // Path to the private key that avbtool will use to sign this filesystem image. |
| 80 | // TODO(jiyong): allow apex_key to be specified here |
| 81 | Avb_private_key *string `android:"path"` |
| 82 | |
Shikha Panwar | 01403bb | 2022-12-22 12:22:57 +0000 | [diff] [blame] | 83 | // Signing algorithm for avbtool. Default is SHA256_RSA4096. |
Jiyong Park | 71baa76 | 2021-01-18 21:11:03 +0900 | [diff] [blame] | 84 | Avb_algorithm *string |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 85 | |
Shikha Panwar | 01403bb | 2022-12-22 12:22:57 +0000 | [diff] [blame] | 86 | // Hash algorithm used for avbtool (for descriptors). This is passed as hash_algorithm to |
| 87 | // avbtool. Default used by avbtool is sha1. |
Shikha Panwar | e6f3063 | 2022-12-21 12:54:45 +0000 | [diff] [blame] | 88 | Avb_hash_algorithm *string |
| 89 | |
Nikita Ioffe | 2c8cdc6 | 2024-03-27 22:19:30 +0000 | [diff] [blame] | 90 | // The index used to prevent rollback of the image. Only used if use_avb is true. |
| 91 | Rollback_index *int64 |
| 92 | |
Jiyong Park | ac4076d | 2021-03-15 23:21:30 +0900 | [diff] [blame] | 93 | // Name of the partition stored in vbmeta desc. Defaults to the name of this module. |
| 94 | Partition_name *string |
| 95 | |
Spandan Das | c35d6fb | 2024-10-10 17:51:14 +0000 | [diff] [blame] | 96 | // Type of the filesystem. Currently, ext4, erofs, cpio, and compressed_cpio are supported. Default |
Jiyong Park | 837cdb2 | 2021-02-05 00:17:14 +0900 | [diff] [blame] | 97 | // is ext4. |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 98 | Type *string |
Inseob Kim | cc8e536 | 2021-02-03 14:05:24 +0900 | [diff] [blame] | 99 | |
Cole Faust | 9a24d90 | 2024-03-18 15:38:12 -0700 | [diff] [blame] | 100 | // Identifies which partition this is for //visibility:any_system_image (and others) visibility |
| 101 | // checks, and will be used in the future for API surface checks. |
| 102 | Partition_type *string |
| 103 | |
Inseob Kim | cc8e536 | 2021-02-03 14:05:24 +0900 | [diff] [blame] | 104 | // file_contexts file to make image. Currently, only ext4 is supported. |
| 105 | File_contexts *string `android:"path"` |
Inseob Kim | 2ce1b5d | 2021-02-15 17:01:04 +0900 | [diff] [blame] | 106 | |
| 107 | // Base directory relative to root, to which deps are installed, e.g. "system". Default is "." |
| 108 | // (root). |
| 109 | Base_dir *string |
Inseob Kim | 14199b0 | 2021-02-09 21:18:31 +0900 | [diff] [blame] | 110 | |
| 111 | // Directories to be created under root. e.g. /dev, /proc, etc. |
Cole Faust | d9c6a5b | 2024-05-21 14:54:00 -0700 | [diff] [blame] | 112 | Dirs proptools.Configurable[[]string] |
Inseob Kim | 14199b0 | 2021-02-09 21:18:31 +0900 | [diff] [blame] | 113 | |
| 114 | // Symbolic links to be created under root with "ln -sf <target> <name>". |
Spandan Das | 69464c3 | 2024-10-25 20:08:06 +0000 | [diff] [blame] | 115 | Symlinks []SymlinkDefinition |
Jooyung Han | 65f402b | 2022-04-21 14:24:04 +0900 | [diff] [blame] | 116 | |
| 117 | // Seconds since unix epoch to override timestamps of file entries |
| 118 | Fake_timestamp *string |
| 119 | |
| 120 | // When set, passed to mkuserimg_mke2fs --mke2fs_uuid & --mke2fs_hash_seed. |
| 121 | // Otherwise, they'll be set as random which might cause indeterministic build output. |
| 122 | Uuid *string |
Inseob Kim | 376d72f | 2023-11-01 15:40:25 +0900 | [diff] [blame] | 123 | |
| 124 | // Mount point for this image. Default is "/" |
| 125 | Mount_point *string |
Cole Faust | 4a2a7c9 | 2024-03-12 12:44:40 -0700 | [diff] [blame] | 126 | |
| 127 | // If set to the name of a partition ("system", "vendor", etc), this filesystem module |
| 128 | // will also include the contents of the make-built staging directories. If any soong |
| 129 | // modules would be installed to the same location as a make module, they will overwrite |
| 130 | // the make version. |
| 131 | Include_make_built_files string |
Inseob Kim | 5339184 | 2024-03-29 17:44:07 +0900 | [diff] [blame] | 132 | |
Inseob Kim | b7b8457 | 2024-04-30 10:51:47 +0900 | [diff] [blame] | 133 | // When set, builds etc/event-log-tags file by merging logtags from all dependencies. |
| 134 | // Default is false |
| 135 | Build_logtags *bool |
| 136 | |
Justin Yun | 74f3f30 | 2024-05-07 14:32:14 +0900 | [diff] [blame] | 137 | // Install aconfig_flags.pb file for the modules installed in this partition. |
| 138 | Gen_aconfig_flags_pb *bool |
| 139 | |
Inseob Kim | 5339184 | 2024-03-29 17:44:07 +0900 | [diff] [blame] | 140 | Fsverity fsverityProperties |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 141 | |
| 142 | // If this property is set to true, the filesystem will call ctx.UncheckedModule(), causing |
| 143 | // it to not be built on checkbuilds. Used for the automatic migration from make to soong |
| 144 | // build modules, where we want to emit some not-yet-working filesystems and we don't want them |
| 145 | // to be built. |
| 146 | Unchecked_module *bool `blueprint:"mutated"` |
Spandan Das | c35d6fb | 2024-10-10 17:51:14 +0000 | [diff] [blame] | 147 | |
| 148 | Erofs ErofsProperties |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 149 | |
Spandan Das | 173256b | 2024-10-31 19:59:30 +0000 | [diff] [blame] | 150 | Linkerconfig LinkerConfigProperties |
Spandan Das | 9263188 | 2024-10-28 22:49:38 +0000 | [diff] [blame] | 151 | |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 152 | // Determines if the module is auto-generated from Soong or not. If the module is |
| 153 | // auto-generated, its deps are exempted from visibility enforcement. |
| 154 | Is_auto_generated *bool |
Spandan Das | c35d6fb | 2024-10-10 17:51:14 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | // Additional properties required to generate erofs FS partitions. |
| 158 | type ErofsProperties struct { |
| 159 | // Compressor and Compression level passed to mkfs.erofs. e.g. (lz4hc,9) |
| 160 | // Please see external/erofs-utils/README for complete documentation. |
| 161 | Compressor *string |
| 162 | |
| 163 | // Used as --compress-hints for mkfs.erofs |
| 164 | Compress_hints *string `android:"path"` |
| 165 | |
| 166 | Sparse *bool |
Jiyong Park | 71baa76 | 2021-01-18 21:11:03 +0900 | [diff] [blame] | 167 | } |
| 168 | |
Spandan Das | 173256b | 2024-10-31 19:59:30 +0000 | [diff] [blame] | 169 | type LinkerConfigProperties struct { |
| 170 | |
| 171 | // Build a linker.config.pb file |
| 172 | Gen_linker_config *bool |
| 173 | |
| 174 | // List of files (in .json format) that will be converted to a linker config file (in .pb format). |
| 175 | // The linker config file be installed in the filesystem at /etc/linker.config.pb |
| 176 | Linker_config_srcs []string `android:"path"` |
| 177 | } |
| 178 | |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 179 | // android_filesystem packages a set of modules and their transitive dependencies into a filesystem |
| 180 | // image. The filesystem images are expected to be mounted in the target device, which means the |
| 181 | // modules in the filesystem image are built for the target device (i.e. Android, not Linux host). |
| 182 | // The modules are placed in the filesystem image just like they are installed to the ordinary |
| 183 | // partitions like system.img. For example, cc_library modules are placed under ./lib[64] directory. |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 184 | func FilesystemFactory() android.Module { |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 185 | module := &filesystem{} |
Jiyong Park | 7e7d4af | 2024-05-01 12:36:10 +0000 | [diff] [blame] | 186 | module.filterPackagingSpec = module.filterInstallablePackagingSpec |
Cole Faust | 2cfe696 | 2024-09-17 11:31:14 -0700 | [diff] [blame] | 187 | initFilesystemModule(module, module) |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 188 | return module |
| 189 | } |
| 190 | |
Cole Faust | 2cfe696 | 2024-09-17 11:31:14 -0700 | [diff] [blame] | 191 | func initFilesystemModule(module android.DefaultableModule, filesystemModule *filesystem) { |
| 192 | module.AddProperties(&filesystemModule.properties) |
| 193 | android.InitPackageModule(filesystemModule) |
| 194 | filesystemModule.PackagingBase.DepsCollectFirstTargetOnly = true |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame] | 195 | filesystemModule.PackagingBase.AllowHighPriorityDeps = true |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 196 | android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) |
Jiyong Park | f46b1af | 2024-04-05 18:13:33 +0900 | [diff] [blame] | 197 | android.InitDefaultableModule(module) |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 198 | } |
| 199 | |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 200 | type depTag struct { |
Jiyong Park | 12a719c | 2021-01-07 15:31:24 +0900 | [diff] [blame] | 201 | blueprint.BaseDependencyTag |
Jooyung Han | 092ef81 | 2021-03-10 15:40:34 +0900 | [diff] [blame] | 202 | android.PackagingItemAlwaysDepTag |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | var dependencyTag = depTag{} |
| 206 | |
| 207 | type depTagWithVisibilityEnforcementBypass struct { |
| 208 | depTag |
| 209 | } |
| 210 | |
| 211 | var _ android.ExcludeFromVisibilityEnforcementTag = (*depTagWithVisibilityEnforcementBypass)(nil) |
| 212 | |
| 213 | func (t depTagWithVisibilityEnforcementBypass) ExcludeFromVisibilityEnforcement() {} |
| 214 | |
| 215 | var dependencyTagWithVisibilityEnforcementBypass = depTagWithVisibilityEnforcementBypass{} |
Jiyong Park | 65b6224 | 2020-11-25 12:44:59 +0900 | [diff] [blame] | 216 | |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 217 | func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) { |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 218 | if proptools.Bool(f.properties.Is_auto_generated) { |
| 219 | f.AddDeps(ctx, dependencyTagWithVisibilityEnforcementBypass) |
| 220 | } else { |
| 221 | f.AddDeps(ctx, dependencyTag) |
| 222 | } |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 223 | } |
| 224 | |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 225 | type fsType int |
| 226 | |
| 227 | const ( |
| 228 | ext4Type fsType = iota |
Spandan Das | c35d6fb | 2024-10-10 17:51:14 +0000 | [diff] [blame] | 229 | erofsType |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 230 | compressedCpioType |
Jiyong Park | 837cdb2 | 2021-02-05 00:17:14 +0900 | [diff] [blame] | 231 | cpioType // uncompressed |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 232 | unknown |
| 233 | ) |
| 234 | |
Spandan Das | 7a46f6c | 2024-10-14 18:41:18 +0000 | [diff] [blame] | 235 | func (fs fsType) IsUnknown() bool { |
| 236 | return fs == unknown |
| 237 | } |
| 238 | |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 239 | type FilesystemInfo struct { |
| 240 | // A text file containing the list of paths installed on the partition. |
| 241 | FileListFile android.Path |
| 242 | } |
| 243 | |
| 244 | var FilesystemProvider = blueprint.NewProvider[FilesystemInfo]() |
| 245 | |
Spandan Das | 7a46f6c | 2024-10-14 18:41:18 +0000 | [diff] [blame] | 246 | func GetFsTypeFromString(ctx android.EarlyModuleContext, typeStr string) fsType { |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 247 | switch typeStr { |
| 248 | case "ext4": |
| 249 | return ext4Type |
Spandan Das | c35d6fb | 2024-10-10 17:51:14 +0000 | [diff] [blame] | 250 | case "erofs": |
| 251 | return erofsType |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 252 | case "compressed_cpio": |
| 253 | return compressedCpioType |
Jiyong Park | 837cdb2 | 2021-02-05 00:17:14 +0900 | [diff] [blame] | 254 | case "cpio": |
| 255 | return cpioType |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 256 | default: |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 257 | return unknown |
| 258 | } |
| 259 | } |
| 260 | |
Spandan Das | 7a46f6c | 2024-10-14 18:41:18 +0000 | [diff] [blame] | 261 | func (f *filesystem) fsType(ctx android.ModuleContext) fsType { |
| 262 | typeStr := proptools.StringDefault(f.properties.Type, "ext4") |
| 263 | fsType := GetFsTypeFromString(ctx, typeStr) |
| 264 | if fsType == unknown { |
| 265 | ctx.PropertyErrorf("type", "%q not supported", typeStr) |
| 266 | } |
| 267 | return fsType |
| 268 | } |
| 269 | |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 270 | func (f *filesystem) installFileName() string { |
| 271 | return f.BaseModuleName() + ".img" |
| 272 | } |
| 273 | |
Inseob Kim | 5339184 | 2024-03-29 17:44:07 +0900 | [diff] [blame] | 274 | func (f *filesystem) partitionName() string { |
| 275 | return proptools.StringDefault(f.properties.Partition_name, f.Name()) |
| 276 | } |
| 277 | |
Jiyong Park | 7e7d4af | 2024-05-01 12:36:10 +0000 | [diff] [blame] | 278 | func (f *filesystem) filterInstallablePackagingSpec(ps android.PackagingSpec) bool { |
| 279 | // Filesystem module respects the installation semantic. A PackagingSpec from a module with |
| 280 | // IsSkipInstall() is skipped. |
Spandan Das | 6d05650 | 2024-10-21 15:40:32 +0000 | [diff] [blame] | 281 | if proptools.Bool(f.properties.Is_auto_generated) { // TODO (spandandas): Remove this. |
| 282 | return !ps.SkipInstall() && (ps.Partition() == f.PartitionType()) |
| 283 | } |
| 284 | return !ps.SkipInstall() |
Jiyong Park | 7e7d4af | 2024-05-01 12:36:10 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 287 | var pctx = android.NewPackageContext("android/soong/filesystem") |
| 288 | |
| 289 | func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Jiyong Park | f46b1af | 2024-04-05 18:13:33 +0900 | [diff] [blame] | 290 | validatePartitionType(ctx, f) |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 291 | switch f.fsType(ctx) { |
Spandan Das | c35d6fb | 2024-10-10 17:51:14 +0000 | [diff] [blame] | 292 | case ext4Type, erofsType: |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 293 | f.output = f.buildImageUsingBuildImage(ctx) |
| 294 | case compressedCpioType: |
Jiyong Park | 837cdb2 | 2021-02-05 00:17:14 +0900 | [diff] [blame] | 295 | f.output = f.buildCpioImage(ctx, true) |
| 296 | case cpioType: |
| 297 | f.output = f.buildCpioImage(ctx, false) |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 298 | default: |
| 299 | return |
| 300 | } |
| 301 | |
| 302 | f.installDir = android.PathForModuleInstall(ctx, "etc") |
| 303 | ctx.InstallFile(f.installDir, f.installFileName(), f.output) |
mrziwang | 555d133 | 2024-06-07 11:15:33 -0700 | [diff] [blame] | 304 | ctx.SetOutputFiles([]android.Path{f.output}, "") |
Kiyoung Kim | 99a954d | 2024-06-21 14:22:20 +0900 | [diff] [blame] | 305 | |
| 306 | f.fileListFile = android.PathForModuleOut(ctx, "fileList").OutputPath |
| 307 | android.WriteFileRule(ctx, f.fileListFile, f.installedFilesList()) |
Cole Faust | 92ccbe2 | 2024-10-03 14:38:37 -0700 | [diff] [blame] | 308 | |
| 309 | android.SetProvider(ctx, FilesystemProvider, FilesystemInfo{ |
| 310 | FileListFile: f.fileListFile, |
| 311 | }) |
| 312 | |
| 313 | if proptools.Bool(f.properties.Unchecked_module) { |
| 314 | ctx.UncheckedModule() |
| 315 | } |
Kiyoung Kim | 99a954d | 2024-06-21 14:22:20 +0900 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | func (f *filesystem) appendToEntry(ctx android.ModuleContext, installedFile android.OutputPath) { |
| 319 | partitionBaseDir := android.PathForModuleOut(ctx, "root", f.partitionName()).String() + "/" |
| 320 | |
| 321 | relPath, inTargetPartition := strings.CutPrefix(installedFile.String(), partitionBaseDir) |
| 322 | if inTargetPartition { |
| 323 | f.entries = append(f.entries, relPath) |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | func (f *filesystem) installedFilesList() string { |
| 328 | installedFilePaths := android.FirstUniqueStrings(f.entries) |
| 329 | slices.Sort(installedFilePaths) |
| 330 | |
| 331 | return strings.Join(installedFilePaths, "\n") |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 332 | } |
| 333 | |
Jiyong Park | f46b1af | 2024-04-05 18:13:33 +0900 | [diff] [blame] | 334 | func validatePartitionType(ctx android.ModuleContext, p partition) { |
| 335 | if !android.InList(p.PartitionType(), validPartitions) { |
| 336 | ctx.PropertyErrorf("partition_type", "partition_type must be one of %s, found: %s", validPartitions, p.PartitionType()) |
| 337 | } |
| 338 | |
| 339 | ctx.VisitDirectDepsWithTag(android.DefaultsDepTag, func(m android.Module) { |
| 340 | if fdm, ok := m.(*filesystemDefaults); ok { |
| 341 | if p.PartitionType() != fdm.PartitionType() { |
| 342 | ctx.PropertyErrorf("partition_type", |
| 343 | "%s doesn't match with the partition type %s of the filesystem default module %s", |
| 344 | p.PartitionType(), fdm.PartitionType(), m.Name()) |
| 345 | } |
| 346 | } |
| 347 | }) |
| 348 | } |
| 349 | |
Cole Faust | 3b806d3 | 2024-03-11 15:15:03 -0700 | [diff] [blame] | 350 | // Copy extra files/dirs that are not from the `deps` property to `rootDir`, checking for conflicts with files |
| 351 | // already in `rootDir`. |
| 352 | func (f *filesystem) buildNonDepsFiles(ctx android.ModuleContext, builder *android.RuleBuilder, rootDir android.OutputPath) { |
Inseob Kim | 14199b0 | 2021-02-09 21:18:31 +0900 | [diff] [blame] | 353 | // create dirs and symlinks |
Cole Faust | d9c6a5b | 2024-05-21 14:54:00 -0700 | [diff] [blame] | 354 | for _, dir := range f.properties.Dirs.GetOrDefault(ctx, nil) { |
Inseob Kim | 14199b0 | 2021-02-09 21:18:31 +0900 | [diff] [blame] | 355 | // OutputPath.Join verifies dir |
| 356 | builder.Command().Text("mkdir -p").Text(rootDir.Join(ctx, dir).String()) |
| 357 | } |
| 358 | |
| 359 | for _, symlink := range f.properties.Symlinks { |
| 360 | name := strings.TrimSpace(proptools.String(symlink.Name)) |
| 361 | target := strings.TrimSpace(proptools.String(symlink.Target)) |
| 362 | |
| 363 | if name == "" { |
| 364 | ctx.PropertyErrorf("symlinks", "Name can't be empty") |
| 365 | continue |
| 366 | } |
| 367 | |
| 368 | if target == "" { |
| 369 | ctx.PropertyErrorf("symlinks", "Target can't be empty") |
| 370 | continue |
| 371 | } |
| 372 | |
| 373 | // OutputPath.Join verifies name. don't need to verify target. |
| 374 | dst := rootDir.Join(ctx, name) |
Cole Faust | 3b806d3 | 2024-03-11 15:15:03 -0700 | [diff] [blame] | 375 | builder.Command().Textf("(! [ -e %s -o -L %s ] || (echo \"%s already exists from an earlier stage of the build\" && exit 1))", dst, dst, dst) |
Inseob Kim | 14199b0 | 2021-02-09 21:18:31 +0900 | [diff] [blame] | 376 | builder.Command().Text("mkdir -p").Text(filepath.Dir(dst.String())) |
| 377 | builder.Command().Text("ln -sf").Text(proptools.ShellEscape(target)).Text(dst.String()) |
Kiyoung Kim | 99a954d | 2024-06-21 14:22:20 +0900 | [diff] [blame] | 378 | f.appendToEntry(ctx, dst) |
Inseob Kim | 14199b0 | 2021-02-09 21:18:31 +0900 | [diff] [blame] | 379 | } |
Inseob Kim | 2ce1b5d | 2021-02-15 17:01:04 +0900 | [diff] [blame] | 380 | |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 381 | // create extra files if there's any |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 382 | if f.buildExtraFiles != nil { |
Cole Faust | 4a2a7c9 | 2024-03-12 12:44:40 -0700 | [diff] [blame] | 383 | rootForExtraFiles := android.PathForModuleGen(ctx, "root-extra").OutputPath |
| 384 | extraFiles := f.buildExtraFiles(ctx, rootForExtraFiles) |
Kiyoung Kim | 99a954d | 2024-06-21 14:22:20 +0900 | [diff] [blame] | 385 | for _, extraFile := range extraFiles { |
| 386 | rel, err := filepath.Rel(rootForExtraFiles.String(), extraFile.String()) |
Cole Faust | 4a2a7c9 | 2024-03-12 12:44:40 -0700 | [diff] [blame] | 387 | if err != nil || strings.HasPrefix(rel, "..") { |
Kiyoung Kim | 99a954d | 2024-06-21 14:22:20 +0900 | [diff] [blame] | 388 | ctx.ModuleErrorf("can't make %q relative to %q", extraFile, rootForExtraFiles) |
Cole Faust | 4a2a7c9 | 2024-03-12 12:44:40 -0700 | [diff] [blame] | 389 | } |
Kiyoung Kim | 99a954d | 2024-06-21 14:22:20 +0900 | [diff] [blame] | 390 | f.appendToEntry(ctx, rootDir.Join(ctx, rel)) |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 391 | } |
Cole Faust | 4a2a7c9 | 2024-03-12 12:44:40 -0700 | [diff] [blame] | 392 | if len(extraFiles) > 0 { |
| 393 | builder.Command().BuiltTool("merge_directories"). |
| 394 | Implicits(extraFiles.Paths()). |
| 395 | Text(rootDir.String()). |
| 396 | Text(rootForExtraFiles.String()) |
| 397 | } |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 398 | } |
Inseob Kim | 2ce1b5d | 2021-02-15 17:01:04 +0900 | [diff] [blame] | 399 | } |
| 400 | |
Inseob Kim | 33f95a9 | 2024-07-11 15:44:49 +0900 | [diff] [blame] | 401 | func (f *filesystem) copyPackagingSpecs(ctx android.ModuleContext, builder *android.RuleBuilder, specs map[string]android.PackagingSpec, rootDir, rebasedDir android.WritablePath) []string { |
| 402 | rootDirSpecs := make(map[string]android.PackagingSpec) |
| 403 | rebasedDirSpecs := make(map[string]android.PackagingSpec) |
| 404 | |
| 405 | for rel, spec := range specs { |
| 406 | if spec.Partition() == "root" { |
| 407 | rootDirSpecs[rel] = spec |
| 408 | } else { |
| 409 | rebasedDirSpecs[rel] = spec |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | dirsToSpecs := make(map[android.WritablePath]map[string]android.PackagingSpec) |
| 414 | dirsToSpecs[rootDir] = rootDirSpecs |
| 415 | dirsToSpecs[rebasedDir] = rebasedDirSpecs |
| 416 | |
| 417 | return f.CopySpecsToDirs(ctx, builder, dirsToSpecs) |
| 418 | } |
| 419 | |
Justin Yun | 34baa2e | 2024-08-30 21:11:33 +0900 | [diff] [blame] | 420 | func (f *filesystem) copyFilesToProductOut(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath) { |
Justin Yun | 2cc4250 | 2024-09-11 14:10:20 +0900 | [diff] [blame] | 421 | if f.Name() != ctx.Config().SoongDefinedSystemImage() { |
Justin Yun | 34baa2e | 2024-08-30 21:11:33 +0900 | [diff] [blame] | 422 | return |
| 423 | } |
| 424 | installPath := android.PathForModuleInPartitionInstall(ctx, f.partitionName()) |
| 425 | builder.Command().Textf("cp -prf %s/* %s", rebasedDir, installPath) |
| 426 | } |
| 427 | |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 428 | func (f *filesystem) buildImageUsingBuildImage(ctx android.ModuleContext) android.OutputPath { |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 429 | rootDir := android.PathForModuleOut(ctx, "root").OutputPath |
Cole Faust | 3b806d3 | 2024-03-11 15:15:03 -0700 | [diff] [blame] | 430 | rebasedDir := rootDir |
| 431 | if f.properties.Base_dir != nil { |
| 432 | rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir) |
| 433 | } |
| 434 | builder := android.NewRuleBuilder(pctx, ctx) |
| 435 | // Wipe the root dir to get rid of leftover files from prior builds |
| 436 | builder.Command().Textf("rm -rf %s && mkdir -p %s", rootDir, rootDir) |
Inseob Kim | 5339184 | 2024-03-29 17:44:07 +0900 | [diff] [blame] | 437 | specs := f.gatherFilteredPackagingSpecs(ctx) |
Inseob Kim | 33f95a9 | 2024-07-11 15:44:49 +0900 | [diff] [blame] | 438 | f.entries = f.copyPackagingSpecs(ctx, builder, specs, rootDir, rebasedDir) |
Cole Faust | 3b806d3 | 2024-03-11 15:15:03 -0700 | [diff] [blame] | 439 | |
| 440 | f.buildNonDepsFiles(ctx, builder, rootDir) |
Cole Faust | 4a2a7c9 | 2024-03-12 12:44:40 -0700 | [diff] [blame] | 441 | f.addMakeBuiltFiles(ctx, builder, rootDir) |
Inseob Kim | 5339184 | 2024-03-29 17:44:07 +0900 | [diff] [blame] | 442 | f.buildFsverityMetadataFiles(ctx, builder, specs, rootDir, rebasedDir) |
Inseob Kim | b7b8457 | 2024-04-30 10:51:47 +0900 | [diff] [blame] | 443 | f.buildEventLogtagsFile(ctx, builder, rebasedDir) |
Justin Yun | 74f3f30 | 2024-05-07 14:32:14 +0900 | [diff] [blame] | 444 | f.buildAconfigFlagsFiles(ctx, builder, specs, rebasedDir) |
Spandan Das | 9263188 | 2024-10-28 22:49:38 +0000 | [diff] [blame] | 445 | f.buildLinkerConfigFile(ctx, builder, rebasedDir) |
Justin Yun | 34baa2e | 2024-08-30 21:11:33 +0900 | [diff] [blame] | 446 | f.copyFilesToProductOut(ctx, builder, rebasedDir) |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 447 | |
Nikita Ioffe | 519015f | 2022-12-23 15:36:29 +0000 | [diff] [blame] | 448 | // run host_init_verifier |
| 449 | // Ideally we should have a concept of pluggable linters that verify the generated image. |
| 450 | // While such concept is not implement this will do. |
| 451 | // TODO(b/263574231): substitute with pluggable linter. |
| 452 | builder.Command(). |
| 453 | BuiltTool("host_init_verifier"). |
| 454 | FlagWithArg("--out_system=", rootDir.String()+"/system") |
| 455 | |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 456 | propFile, toolDeps := f.buildPropFile(ctx) |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 457 | output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 458 | builder.Command().BuiltTool("build_image"). |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 459 | Text(rootDir.String()). // input directory |
| 460 | Input(propFile). |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 461 | Implicits(toolDeps). |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 462 | Output(output). |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 463 | Text(rootDir.String()) // directory where to find fs_config_files|dirs |
| 464 | |
| 465 | // rootDir is not deleted. Might be useful for quick inspection. |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 466 | builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName())) |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 467 | |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 468 | return output |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 469 | } |
| 470 | |
Inseob Kim | cc8e536 | 2021-02-03 14:05:24 +0900 | [diff] [blame] | 471 | func (f *filesystem) buildFileContexts(ctx android.ModuleContext) android.OutputPath { |
| 472 | builder := android.NewRuleBuilder(pctx, ctx) |
| 473 | fcBin := android.PathForModuleOut(ctx, "file_contexts.bin") |
| 474 | builder.Command().BuiltTool("sefcontext_compile"). |
| 475 | FlagWithOutput("-o ", fcBin). |
| 476 | Input(android.PathForModuleSrc(ctx, proptools.String(f.properties.File_contexts))) |
| 477 | builder.Build("build_filesystem_file_contexts", fmt.Sprintf("Creating filesystem file contexts for %s", f.BaseModuleName())) |
| 478 | return fcBin.OutputPath |
| 479 | } |
| 480 | |
Jooyung Han | 65f402b | 2022-04-21 14:24:04 +0900 | [diff] [blame] | 481 | // Calculates avb_salt from entry list (sorted) for deterministic output. |
| 482 | func (f *filesystem) salt() string { |
| 483 | return sha1sum(f.entries) |
| 484 | } |
| 485 | |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 486 | func (f *filesystem) buildPropFile(ctx android.ModuleContext) (propFile android.OutputPath, toolDeps android.Paths) { |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 487 | var deps android.Paths |
Cole Faust | cec230a | 2024-03-07 15:51:12 -0800 | [diff] [blame] | 488 | var propFileString strings.Builder |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 489 | addStr := func(name string, value string) { |
Cole Faust | cec230a | 2024-03-07 15:51:12 -0800 | [diff] [blame] | 490 | propFileString.WriteString(name) |
| 491 | propFileString.WriteRune('=') |
| 492 | propFileString.WriteString(value) |
| 493 | propFileString.WriteRune('\n') |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 494 | } |
| 495 | addPath := func(name string, path android.Path) { |
Cole Faust | cec230a | 2024-03-07 15:51:12 -0800 | [diff] [blame] | 496 | addStr(name, path.String()) |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 497 | deps = append(deps, path) |
| 498 | } |
| 499 | |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 500 | // Type string that build_image.py accepts. |
| 501 | fsTypeStr := func(t fsType) string { |
| 502 | switch t { |
Spandan Das | 9466882 | 2024-10-09 20:51:33 +0000 | [diff] [blame] | 503 | // TODO(372522486): add more types like f2fs, erofs, etc. |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 504 | case ext4Type: |
| 505 | return "ext4" |
Spandan Das | c35d6fb | 2024-10-10 17:51:14 +0000 | [diff] [blame] | 506 | case erofsType: |
| 507 | return "erofs" |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 508 | } |
| 509 | panic(fmt.Errorf("unsupported fs type %v", t)) |
| 510 | } |
| 511 | |
| 512 | addStr("fs_type", fsTypeStr(f.fsType(ctx))) |
Inseob Kim | 376d72f | 2023-11-01 15:40:25 +0900 | [diff] [blame] | 513 | addStr("mount_point", proptools.StringDefault(f.properties.Mount_point, "/")) |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 514 | addStr("use_dynamic_partition_size", "true") |
| 515 | addPath("ext_mkuserimg", ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs")) |
| 516 | // b/177813163 deps of the host tools have to be added. Remove this. |
| 517 | for _, t := range []string{"mke2fs", "e2fsdroid", "tune2fs"} { |
| 518 | deps = append(deps, ctx.Config().HostToolPath(ctx, t)) |
| 519 | } |
| 520 | |
Jiyong Park | 71baa76 | 2021-01-18 21:11:03 +0900 | [diff] [blame] | 521 | if proptools.Bool(f.properties.Use_avb) { |
| 522 | addStr("avb_hashtree_enable", "true") |
| 523 | addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool")) |
| 524 | algorithm := proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096") |
| 525 | addStr("avb_algorithm", algorithm) |
| 526 | key := android.PathForModuleSrc(ctx, proptools.String(f.properties.Avb_private_key)) |
| 527 | addPath("avb_key_path", key) |
Inseob Kim | 5339184 | 2024-03-29 17:44:07 +0900 | [diff] [blame] | 528 | addStr("partition_name", f.partitionName()) |
Shikha Panwar | e6f3063 | 2022-12-21 12:54:45 +0000 | [diff] [blame] | 529 | avb_add_hashtree_footer_args := "--do_not_generate_fec" |
| 530 | if hashAlgorithm := proptools.String(f.properties.Avb_hash_algorithm); hashAlgorithm != "" { |
| 531 | avb_add_hashtree_footer_args += " --hash_algorithm " + hashAlgorithm |
| 532 | } |
Nikita Ioffe | 2c8cdc6 | 2024-03-27 22:19:30 +0000 | [diff] [blame] | 533 | if f.properties.Rollback_index != nil { |
| 534 | rollbackIndex := proptools.Int(f.properties.Rollback_index) |
| 535 | if rollbackIndex < 0 { |
| 536 | ctx.PropertyErrorf("rollback_index", "Rollback index must be non-negative") |
| 537 | } |
| 538 | avb_add_hashtree_footer_args += " --rollback_index " + strconv.Itoa(rollbackIndex) |
| 539 | } |
Inseob Kim | 5339184 | 2024-03-29 17:44:07 +0900 | [diff] [blame] | 540 | securityPatchKey := "com.android.build." + f.partitionName() + ".security_patch" |
Seungjae Yoo | a30e450 | 2023-11-09 14:55:44 +0900 | [diff] [blame] | 541 | securityPatchValue := ctx.Config().PlatformSecurityPatch() |
| 542 | avb_add_hashtree_footer_args += " --prop " + securityPatchKey + ":" + securityPatchValue |
Shikha Panwar | e6f3063 | 2022-12-21 12:54:45 +0000 | [diff] [blame] | 543 | addStr("avb_add_hashtree_footer_args", avb_add_hashtree_footer_args) |
Jooyung Han | 65f402b | 2022-04-21 14:24:04 +0900 | [diff] [blame] | 544 | addStr("avb_salt", f.salt()) |
Jiyong Park | 71baa76 | 2021-01-18 21:11:03 +0900 | [diff] [blame] | 545 | } |
| 546 | |
Inseob Kim | cc8e536 | 2021-02-03 14:05:24 +0900 | [diff] [blame] | 547 | if proptools.String(f.properties.File_contexts) != "" { |
| 548 | addPath("selinux_fc", f.buildFileContexts(ctx)) |
| 549 | } |
Jooyung Han | 65f402b | 2022-04-21 14:24:04 +0900 | [diff] [blame] | 550 | if timestamp := proptools.String(f.properties.Fake_timestamp); timestamp != "" { |
| 551 | addStr("timestamp", timestamp) |
| 552 | } |
| 553 | if uuid := proptools.String(f.properties.Uuid); uuid != "" { |
| 554 | addStr("uuid", uuid) |
| 555 | addStr("hash_seed", uuid) |
| 556 | } |
Spandan Das | c35d6fb | 2024-10-10 17:51:14 +0000 | [diff] [blame] | 557 | // Add erofs properties |
| 558 | if f.fsType(ctx) == erofsType { |
| 559 | if compressor := f.properties.Erofs.Compressor; compressor != nil { |
| 560 | addStr("erofs_default_compressor", proptools.String(compressor)) |
| 561 | } |
| 562 | if compressHints := f.properties.Erofs.Compress_hints; compressHints != nil { |
Cole Faust | 7cd4bad | 2024-10-11 10:31:12 -0700 | [diff] [blame] | 563 | addPath("erofs_default_compress_hints", android.PathForModuleSrc(ctx, *compressHints)) |
Spandan Das | c35d6fb | 2024-10-10 17:51:14 +0000 | [diff] [blame] | 564 | } |
| 565 | if proptools.BoolDefault(f.properties.Erofs.Sparse, true) { |
| 566 | // https://source.corp.google.com/h/googleplex-android/platform/build/+/88b1c67239ca545b11580237242774b411f2fed9:core/Makefile;l=2292;bpv=1;bpt=0;drc=ea8f34bc1d6e63656b4ec32f2391e9d54b3ebb6b |
| 567 | addStr("erofs_sparse_flag", "-s") |
| 568 | } |
Cole Faust | 7cd4bad | 2024-10-11 10:31:12 -0700 | [diff] [blame] | 569 | } else if f.properties.Erofs.Compressor != nil || f.properties.Erofs.Compress_hints != nil || f.properties.Erofs.Sparse != nil { |
| 570 | // Raise an exception if the propfile contains erofs properties, but the fstype is not erofs |
| 571 | fs := fsTypeStr(f.fsType(ctx)) |
Spandan Das | c35d6fb | 2024-10-10 17:51:14 +0000 | [diff] [blame] | 572 | ctx.PropertyErrorf("erofs", "erofs is non-empty, but FS type is %s\n. Please delete erofs properties if this partition should use %s\n", fs, fs) |
| 573 | } |
| 574 | |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 575 | propFile = android.PathForModuleOut(ctx, "prop").OutputPath |
Cole Faust | cec230a | 2024-03-07 15:51:12 -0800 | [diff] [blame] | 576 | android.WriteFileRuleVerbatim(ctx, propFile, propFileString.String()) |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 577 | return propFile, deps |
| 578 | } |
| 579 | |
Jiyong Park | 837cdb2 | 2021-02-05 00:17:14 +0900 | [diff] [blame] | 580 | func (f *filesystem) buildCpioImage(ctx android.ModuleContext, compressed bool) android.OutputPath { |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 581 | if proptools.Bool(f.properties.Use_avb) { |
| 582 | ctx.PropertyErrorf("use_avb", "signing compresed cpio image using avbtool is not supported."+ |
| 583 | "Consider adding this to bootimg module and signing the entire boot image.") |
| 584 | } |
| 585 | |
Inseob Kim | cc8e536 | 2021-02-03 14:05:24 +0900 | [diff] [blame] | 586 | if proptools.String(f.properties.File_contexts) != "" { |
| 587 | ctx.PropertyErrorf("file_contexts", "file_contexts is not supported for compressed cpio image.") |
| 588 | } |
| 589 | |
Cole Faust | 4a2a7c9 | 2024-03-12 12:44:40 -0700 | [diff] [blame] | 590 | if f.properties.Include_make_built_files != "" { |
| 591 | ctx.PropertyErrorf("include_make_built_files", "include_make_built_files is not supported for compressed cpio image.") |
| 592 | } |
| 593 | |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 594 | rootDir := android.PathForModuleOut(ctx, "root").OutputPath |
Cole Faust | 3b806d3 | 2024-03-11 15:15:03 -0700 | [diff] [blame] | 595 | rebasedDir := rootDir |
| 596 | if f.properties.Base_dir != nil { |
| 597 | rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir) |
| 598 | } |
| 599 | builder := android.NewRuleBuilder(pctx, ctx) |
| 600 | // Wipe the root dir to get rid of leftover files from prior builds |
| 601 | builder.Command().Textf("rm -rf %s && mkdir -p %s", rootDir, rootDir) |
Inseob Kim | 5339184 | 2024-03-29 17:44:07 +0900 | [diff] [blame] | 602 | specs := f.gatherFilteredPackagingSpecs(ctx) |
Inseob Kim | 33f95a9 | 2024-07-11 15:44:49 +0900 | [diff] [blame] | 603 | f.entries = f.copyPackagingSpecs(ctx, builder, specs, rootDir, rebasedDir) |
Cole Faust | 3b806d3 | 2024-03-11 15:15:03 -0700 | [diff] [blame] | 604 | |
| 605 | f.buildNonDepsFiles(ctx, builder, rootDir) |
Inseob Kim | 5339184 | 2024-03-29 17:44:07 +0900 | [diff] [blame] | 606 | f.buildFsverityMetadataFiles(ctx, builder, specs, rootDir, rebasedDir) |
Inseob Kim | b7b8457 | 2024-04-30 10:51:47 +0900 | [diff] [blame] | 607 | f.buildEventLogtagsFile(ctx, builder, rebasedDir) |
Justin Yun | 74f3f30 | 2024-05-07 14:32:14 +0900 | [diff] [blame] | 608 | f.buildAconfigFlagsFiles(ctx, builder, specs, rebasedDir) |
Spandan Das | 9263188 | 2024-10-28 22:49:38 +0000 | [diff] [blame] | 609 | f.buildLinkerConfigFile(ctx, builder, rebasedDir) |
Justin Yun | 34baa2e | 2024-08-30 21:11:33 +0900 | [diff] [blame] | 610 | f.copyFilesToProductOut(ctx, builder, rebasedDir) |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 611 | |
| 612 | output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath |
Jiyong Park | 837cdb2 | 2021-02-05 00:17:14 +0900 | [diff] [blame] | 613 | cmd := builder.Command(). |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 614 | BuiltTool("mkbootfs"). |
Jiyong Park | 837cdb2 | 2021-02-05 00:17:14 +0900 | [diff] [blame] | 615 | Text(rootDir.String()) // input directory |
| 616 | if compressed { |
| 617 | cmd.Text("|"). |
| 618 | BuiltTool("lz4"). |
| 619 | Flag("--favor-decSpeed"). // for faster boot |
| 620 | Flag("-12"). // maximum compression level |
| 621 | Flag("-l"). // legacy format for kernel |
| 622 | Text(">").Output(output) |
| 623 | } else { |
| 624 | cmd.Text(">").Output(output) |
| 625 | } |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 626 | |
| 627 | // rootDir is not deleted. Might be useful for quick inspection. |
Jiyong Park | 837cdb2 | 2021-02-05 00:17:14 +0900 | [diff] [blame] | 628 | builder.Build("build_cpio_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName())) |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 629 | |
| 630 | return output |
| 631 | } |
| 632 | |
Cole Faust | 4a2a7c9 | 2024-03-12 12:44:40 -0700 | [diff] [blame] | 633 | var validPartitions = []string{ |
| 634 | "system", |
| 635 | "userdata", |
| 636 | "cache", |
| 637 | "system_other", |
| 638 | "vendor", |
| 639 | "product", |
| 640 | "system_ext", |
| 641 | "odm", |
| 642 | "vendor_dlkm", |
| 643 | "odm_dlkm", |
| 644 | "system_dlkm", |
| 645 | } |
| 646 | |
| 647 | func (f *filesystem) addMakeBuiltFiles(ctx android.ModuleContext, builder *android.RuleBuilder, rootDir android.Path) { |
| 648 | partition := f.properties.Include_make_built_files |
| 649 | if partition == "" { |
| 650 | return |
| 651 | } |
| 652 | if !slices.Contains(validPartitions, partition) { |
| 653 | ctx.PropertyErrorf("include_make_built_files", "Expected one of %#v, found %q", validPartitions, partition) |
| 654 | return |
| 655 | } |
| 656 | stampFile := fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/staging_dir.stamp", ctx.Config().DeviceName(), partition) |
| 657 | fileListFile := fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partition) |
| 658 | stagingDir := fmt.Sprintf("target/product/%s/%s", ctx.Config().DeviceName(), partition) |
| 659 | |
| 660 | builder.Command().BuiltTool("merge_directories"). |
| 661 | Implicit(android.PathForArbitraryOutput(ctx, stampFile)). |
| 662 | Text("--ignore-duplicates"). |
| 663 | FlagWithInput("--file-list", android.PathForArbitraryOutput(ctx, fileListFile)). |
| 664 | Text(rootDir.String()). |
| 665 | Text(android.PathForArbitraryOutput(ctx, stagingDir).String()) |
| 666 | } |
| 667 | |
Inseob Kim | b7b8457 | 2024-04-30 10:51:47 +0900 | [diff] [blame] | 668 | func (f *filesystem) buildEventLogtagsFile(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath) { |
| 669 | if !proptools.Bool(f.properties.Build_logtags) { |
| 670 | return |
| 671 | } |
| 672 | |
| 673 | logtagsFilePaths := make(map[string]bool) |
| 674 | ctx.WalkDeps(func(child, parent android.Module) bool { |
| 675 | if logtagsInfo, ok := android.OtherModuleProvider(ctx, child, android.LogtagsProviderKey); ok { |
| 676 | for _, path := range logtagsInfo.Logtags { |
| 677 | logtagsFilePaths[path.String()] = true |
| 678 | } |
| 679 | } |
| 680 | return true |
| 681 | }) |
| 682 | |
| 683 | if len(logtagsFilePaths) == 0 { |
| 684 | return |
| 685 | } |
| 686 | |
| 687 | etcPath := rebasedDir.Join(ctx, "etc") |
| 688 | eventLogtagsPath := etcPath.Join(ctx, "event-log-tags") |
| 689 | builder.Command().Text("mkdir").Flag("-p").Text(etcPath.String()) |
| 690 | cmd := builder.Command().BuiltTool("merge-event-log-tags"). |
| 691 | FlagWithArg("-o ", eventLogtagsPath.String()). |
| 692 | FlagWithInput("-m ", android.MergedLogtagsPath(ctx)) |
| 693 | |
| 694 | for _, path := range android.SortedKeys(logtagsFilePaths) { |
| 695 | cmd.Text(path) |
| 696 | } |
Kiyoung Kim | 99a954d | 2024-06-21 14:22:20 +0900 | [diff] [blame] | 697 | |
| 698 | f.appendToEntry(ctx, eventLogtagsPath) |
Inseob Kim | b7b8457 | 2024-04-30 10:51:47 +0900 | [diff] [blame] | 699 | } |
| 700 | |
Spandan Das | 9263188 | 2024-10-28 22:49:38 +0000 | [diff] [blame] | 701 | func (f *filesystem) buildLinkerConfigFile(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath) { |
Spandan Das | 173256b | 2024-10-31 19:59:30 +0000 | [diff] [blame] | 702 | if !proptools.Bool(f.properties.Linkerconfig.Gen_linker_config) { |
Spandan Das | 9263188 | 2024-10-28 22:49:38 +0000 | [diff] [blame] | 703 | return |
| 704 | } |
| 705 | |
Spandan Das | 918191e | 2024-10-31 18:27:23 +0000 | [diff] [blame] | 706 | provideModules, _ := f.getLibsForLinkerConfig(ctx) |
Spandan Das | 9263188 | 2024-10-28 22:49:38 +0000 | [diff] [blame] | 707 | output := rebasedDir.Join(ctx, "etc", "linker.config.pb") |
Spandan Das | 173256b | 2024-10-31 19:59:30 +0000 | [diff] [blame] | 708 | linkerconfig.BuildLinkerConfig(ctx, builder, android.PathsForModuleSrc(ctx, f.properties.Linkerconfig.Linker_config_srcs), provideModules, nil, output) |
Spandan Das | 9263188 | 2024-10-28 22:49:38 +0000 | [diff] [blame] | 709 | |
| 710 | f.appendToEntry(ctx, output) |
| 711 | } |
| 712 | |
Jiyong Park | f46b1af | 2024-04-05 18:13:33 +0900 | [diff] [blame] | 713 | type partition interface { |
| 714 | PartitionType() string |
| 715 | } |
| 716 | |
Cole Faust | 9a24d90 | 2024-03-18 15:38:12 -0700 | [diff] [blame] | 717 | func (f *filesystem) PartitionType() string { |
| 718 | return proptools.StringDefault(f.properties.Partition_type, "system") |
| 719 | } |
| 720 | |
Jiyong Park | f46b1af | 2024-04-05 18:13:33 +0900 | [diff] [blame] | 721 | var _ partition = (*filesystem)(nil) |
| 722 | |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 723 | var _ android.AndroidMkEntriesProvider = (*filesystem)(nil) |
| 724 | |
| 725 | // Implements android.AndroidMkEntriesProvider |
| 726 | func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries { |
| 727 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
| 728 | Class: "ETC", |
| 729 | OutputFile: android.OptionalPathForPath(f.output), |
| 730 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 731 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
Colin Cross | c68db4b | 2021-11-11 18:59:15 -0800 | [diff] [blame] | 732 | entries.SetString("LOCAL_MODULE_PATH", f.installDir.String()) |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 733 | entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName()) |
Kiyoung Kim | 99a954d | 2024-06-21 14:22:20 +0900 | [diff] [blame] | 734 | entries.SetString("LOCAL_FILESYSTEM_FILELIST", f.fileListFile.String()) |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 735 | }, |
| 736 | }, |
| 737 | }} |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 738 | } |
Jiyong Park | 12a719c | 2021-01-07 15:31:24 +0900 | [diff] [blame] | 739 | |
| 740 | // Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex |
| 741 | // package to have access to the output file. |
| 742 | type Filesystem interface { |
| 743 | android.Module |
| 744 | OutputPath() android.Path |
Jiyong Park | 972e06c | 2021-03-15 23:32:49 +0900 | [diff] [blame] | 745 | |
| 746 | // Returns the output file that is signed by avbtool. If this module is not signed, returns |
| 747 | // nil. |
| 748 | SignedOutputPath() android.Path |
Jiyong Park | 12a719c | 2021-01-07 15:31:24 +0900 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | var _ Filesystem = (*filesystem)(nil) |
| 752 | |
| 753 | func (f *filesystem) OutputPath() android.Path { |
| 754 | return f.output |
| 755 | } |
Jiyong Park | 972e06c | 2021-03-15 23:32:49 +0900 | [diff] [blame] | 756 | |
| 757 | func (f *filesystem) SignedOutputPath() android.Path { |
| 758 | if proptools.Bool(f.properties.Use_avb) { |
| 759 | return f.OutputPath() |
| 760 | } |
| 761 | return nil |
| 762 | } |
Jooyung Han | 0fbbc2b | 2022-03-25 12:35:46 +0900 | [diff] [blame] | 763 | |
| 764 | // Filter the result of GatherPackagingSpecs to discard items targeting outside "system" partition. |
| 765 | // Note that "apex" module installs its contents to "apex"(fake partition) as well |
| 766 | // for symbol lookup by imitating "activated" paths. |
| 767 | func (f *filesystem) gatherFilteredPackagingSpecs(ctx android.ModuleContext) map[string]android.PackagingSpec { |
Jeongik Cha | 54bf875 | 2024-02-08 10:44:37 +0900 | [diff] [blame] | 768 | specs := f.PackagingBase.GatherPackagingSpecsWithFilter(ctx, f.filterPackagingSpec) |
Jooyung Han | 0fbbc2b | 2022-03-25 12:35:46 +0900 | [diff] [blame] | 769 | return specs |
| 770 | } |
Jooyung Han | 65f402b | 2022-04-21 14:24:04 +0900 | [diff] [blame] | 771 | |
| 772 | func sha1sum(values []string) string { |
| 773 | h := sha256.New() |
| 774 | for _, value := range values { |
| 775 | io.WriteString(h, value) |
| 776 | } |
| 777 | return fmt.Sprintf("%x", h.Sum(nil)) |
| 778 | } |
Jooyung Han | e606759 | 2023-03-16 13:11:17 +0900 | [diff] [blame] | 779 | |
| 780 | // Base cc.UseCoverage |
| 781 | |
| 782 | var _ cc.UseCoverage = (*filesystem)(nil) |
| 783 | |
Colin Cross | e1a8555 | 2024-06-14 12:17:37 -0700 | [diff] [blame] | 784 | func (*filesystem) IsNativeCoverageNeeded(ctx cc.IsNativeCoverageNeededContext) bool { |
Jooyung Han | e606759 | 2023-03-16 13:11:17 +0900 | [diff] [blame] | 785 | return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled() |
| 786 | } |
Jiyong Park | f46b1af | 2024-04-05 18:13:33 +0900 | [diff] [blame] | 787 | |
| 788 | // android_filesystem_defaults |
| 789 | |
| 790 | type filesystemDefaults struct { |
| 791 | android.ModuleBase |
| 792 | android.DefaultsModuleBase |
| 793 | |
| 794 | properties filesystemDefaultsProperties |
| 795 | } |
| 796 | |
| 797 | type filesystemDefaultsProperties struct { |
| 798 | // Identifies which partition this is for //visibility:any_system_image (and others) visibility |
| 799 | // checks, and will be used in the future for API surface checks. |
| 800 | Partition_type *string |
| 801 | } |
| 802 | |
| 803 | // android_filesystem_defaults is a default module for android_filesystem and android_system_image |
| 804 | func filesystemDefaultsFactory() android.Module { |
| 805 | module := &filesystemDefaults{} |
| 806 | module.AddProperties(&module.properties) |
| 807 | module.AddProperties(&android.PackagingProperties{}) |
| 808 | android.InitDefaultsModule(module) |
| 809 | return module |
| 810 | } |
| 811 | |
| 812 | func (f *filesystemDefaults) PartitionType() string { |
| 813 | return proptools.StringDefault(f.properties.Partition_type, "system") |
| 814 | } |
| 815 | |
| 816 | var _ partition = (*filesystemDefaults)(nil) |
| 817 | |
| 818 | func (f *filesystemDefaults) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 819 | validatePartitionType(ctx, f) |
| 820 | } |
Spandan Das | 918191e | 2024-10-31 18:27:23 +0000 | [diff] [blame] | 821 | |
| 822 | // getLibsForLinkerConfig returns |
| 823 | // 1. A list of libraries installed in this filesystem |
| 824 | // 2. A list of dep libraries _not_ installed in this filesystem |
| 825 | // |
| 826 | // `linkerconfig.BuildLinkerConfig` will convert these two to a linker.config.pb for the filesystem |
| 827 | // (1) will be added to --provideLibs if they are C libraries with a stable interface (has stubs) |
| 828 | // (2) will be added to --requireLibs if they are C libraries with a stable interface (has stubs) |
| 829 | func (f *filesystem) getLibsForLinkerConfig(ctx android.ModuleContext) ([]android.Module, []android.Module) { |
| 830 | // we need "Module"s for packaging items |
| 831 | modulesInPackageByModule := make(map[android.Module]bool) |
| 832 | modulesInPackageByName := make(map[string]bool) |
| 833 | |
| 834 | deps := f.gatherFilteredPackagingSpecs(ctx) |
| 835 | ctx.WalkDeps(func(child, parent android.Module) bool { |
| 836 | for _, ps := range android.OtherModuleProviderOrDefault( |
| 837 | ctx, child, android.InstallFilesProvider).PackagingSpecs { |
| 838 | if _, ok := deps[ps.RelPathInPackage()]; ok { |
| 839 | modulesInPackageByModule[child] = true |
| 840 | modulesInPackageByName[child.Name()] = true |
| 841 | return true |
| 842 | } |
| 843 | } |
| 844 | return true |
| 845 | }) |
| 846 | |
| 847 | provideModules := make([]android.Module, 0, len(modulesInPackageByModule)) |
| 848 | for mod := range modulesInPackageByModule { |
| 849 | provideModules = append(provideModules, mod) |
| 850 | } |
| 851 | |
| 852 | var requireModules []android.Module |
| 853 | ctx.WalkDeps(func(child, parent android.Module) bool { |
| 854 | _, parentInPackage := modulesInPackageByModule[parent] |
| 855 | _, childInPackageName := modulesInPackageByName[child.Name()] |
| 856 | |
| 857 | // When parent is in the package, and child (or its variant) is not, this can be from an interface. |
| 858 | if parentInPackage && !childInPackageName { |
| 859 | requireModules = append(requireModules, child) |
| 860 | } |
| 861 | return true |
| 862 | }) |
| 863 | |
| 864 | return provideModules, requireModules |
| 865 | } |