blob: fbc8089366d2e454a2619615627a6c0767b12df0 [file] [log] [blame]
Jiyong Park6f0f6882020-11-12 13:14:30 +09001// 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
15package filesystem
16
17import (
Jooyung Han65f402b2022-04-21 14:24:04 +090018 "crypto/sha256"
Jiyong Park6f0f6882020-11-12 13:14:30 +090019 "fmt"
Jooyung Han65f402b2022-04-21 14:24:04 +090020 "io"
Inseob Kim14199b02021-02-09 21:18:31 +090021 "path/filepath"
Cole Faust4a2a7c92024-03-12 12:44:40 -070022 "slices"
Nikita Ioffe2c8cdc62024-03-27 22:19:30 +000023 "strconv"
Inseob Kim14199b02021-02-09 21:18:31 +090024 "strings"
Jiyong Park6f0f6882020-11-12 13:14:30 +090025
26 "android/soong/android"
Jooyung Hane6067592023-03-16 13:11:17 +090027 "android/soong/cc"
Spandan Das71be42d2024-11-20 18:34:16 +000028 "android/soong/java"
Spandan Das92631882024-10-28 22:49:38 +000029 "android/soong/linkerconfig"
Jiyong Park65b62242020-11-25 12:44:59 +090030
31 "github.com/google/blueprint"
Jiyong Park71baa762021-01-18 21:11:03 +090032 "github.com/google/blueprint/proptools"
Jiyong Park6f0f6882020-11-12 13:14:30 +090033)
34
35func init() {
Jooyung Han9706cbc2021-04-15 22:43:48 +090036 registerBuildComponents(android.InitRegistrationContext)
Spandan Das71be42d2024-11-20 18:34:16 +000037 registerMutators(android.InitRegistrationContext)
Jooyung Han9706cbc2021-04-15 22:43:48 +090038}
39
40func registerBuildComponents(ctx android.RegistrationContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -070041 ctx.RegisterModuleType("android_filesystem", FilesystemFactory)
Jiyong Parkf46b1af2024-04-05 18:13:33 +090042 ctx.RegisterModuleType("android_filesystem_defaults", filesystemDefaultsFactory)
Jihoon Kang98047cf2024-10-02 17:13:54 +000043 ctx.RegisterModuleType("android_system_image", SystemImageFactory)
Jiyong Parkbc485482022-11-15 22:31:49 +090044 ctx.RegisterModuleType("avb_add_hash_footer", avbAddHashFooterFactory)
Inseob Kim87230e62023-11-22 18:55:07 +090045 ctx.RegisterModuleType("avb_add_hash_footer_defaults", avbAddHashFooterDefaultsFactory)
Alice Wang000e3a32023-01-03 16:11:20 +000046 ctx.RegisterModuleType("avb_gen_vbmeta_image", avbGenVbmetaImageFactory)
Inseob Kim87230e62023-11-22 18:55:07 +090047 ctx.RegisterModuleType("avb_gen_vbmeta_image_defaults", avbGenVbmetaImageDefaultsFactory)
Jiyong Park6f0f6882020-11-12 13:14:30 +090048}
49
Spandan Das71be42d2024-11-20 18:34:16 +000050func registerMutators(ctx android.RegistrationContext) {
51 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
52 ctx.BottomUp("add_autogenerated_rro_deps", addAutogeneratedRroDeps)
53 })
54}
55
Jiyong Park6f0f6882020-11-12 13:14:30 +090056type filesystem struct {
57 android.ModuleBase
58 android.PackagingBase
Jiyong Parkf46b1af2024-04-05 18:13:33 +090059 android.DefaultableModuleBase
Jiyong Park65c49f52020-11-24 14:23:26 +090060
Jihoon Kang98047cf2024-10-02 17:13:54 +000061 properties FilesystemProperties
Jiyong Park71baa762021-01-18 21:11:03 +090062
Cole Faust4e9f5922024-11-13 16:09:23 -080063 output android.Path
Jiyong Park65c49f52020-11-24 14:23:26 +090064 installDir android.InstallPath
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090065
Cole Faust4e9f5922024-11-13 16:09:23 -080066 fileListFile android.Path
Kiyoung Kim99a954d2024-06-21 14:22:20 +090067
68 // Keeps the entries installed from this filesystem
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090069 entries []string
Kiyoung Kim67118212024-11-07 13:23:44 +090070
71 filesystemBuilder filesystemBuilder
Jiyong Park6f0f6882020-11-12 13:14:30 +090072}
73
Kiyoung Kim67118212024-11-07 13:23:44 +090074type filesystemBuilder interface {
75 BuildLinkerConfigFile(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath)
76 // Function that filters PackagingSpec in PackagingBase.GatherPackagingSpecs()
77 FilterPackagingSpec(spec android.PackagingSpec) bool
Inseob Kim3c0a0422024-11-05 17:21:37 +090078 // Function that modifies PackagingSpec in PackagingBase.GatherPackagingSpecs() to customize.
79 // For example, GSI system.img contains system_ext and product artifacts and their
80 // relPathInPackage need to be rebased to system/system_ext and system/system_product.
81 ModifyPackagingSpec(spec *android.PackagingSpec)
Kiyoung Kim23be5bb2024-11-27 00:50:30 +000082
83 // Function to check if the filesystem should not use `vintf_fragments` property,
84 // but use `vintf_fragment` module type instead
85 ShouldUseVintfFragmentModuleOnly() bool
Kiyoung Kim67118212024-11-07 13:23:44 +090086}
87
88var _ filesystemBuilder = (*filesystem)(nil)
89
Spandan Das69464c32024-10-25 20:08:06 +000090type SymlinkDefinition struct {
Inseob Kim14199b02021-02-09 21:18:31 +090091 Target *string
92 Name *string
93}
94
Jihoon Kang98047cf2024-10-02 17:13:54 +000095type FilesystemProperties struct {
Jiyong Park71baa762021-01-18 21:11:03 +090096 // When set to true, sign the image with avbtool. Default is false.
97 Use_avb *bool
98
99 // Path to the private key that avbtool will use to sign this filesystem image.
100 // TODO(jiyong): allow apex_key to be specified here
101 Avb_private_key *string `android:"path"`
102
Shikha Panwar01403bb2022-12-22 12:22:57 +0000103 // Signing algorithm for avbtool. Default is SHA256_RSA4096.
Jiyong Park71baa762021-01-18 21:11:03 +0900104 Avb_algorithm *string
Jiyong Park11a65972021-02-01 21:09:38 +0900105
Shikha Panwar01403bb2022-12-22 12:22:57 +0000106 // Hash algorithm used for avbtool (for descriptors). This is passed as hash_algorithm to
107 // avbtool. Default used by avbtool is sha1.
Shikha Panware6f30632022-12-21 12:54:45 +0000108 Avb_hash_algorithm *string
109
Nikita Ioffe2c8cdc62024-03-27 22:19:30 +0000110 // The index used to prevent rollback of the image. Only used if use_avb is true.
111 Rollback_index *int64
112
Jiyong Parkac4076d2021-03-15 23:21:30 +0900113 // Name of the partition stored in vbmeta desc. Defaults to the name of this module.
114 Partition_name *string
115
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000116 // Type of the filesystem. Currently, ext4, erofs, cpio, and compressed_cpio are supported. Default
Jiyong Park837cdb22021-02-05 00:17:14 +0900117 // is ext4.
Jiyong Park11a65972021-02-01 21:09:38 +0900118 Type *string
Inseob Kimcc8e5362021-02-03 14:05:24 +0900119
Cole Faust9a24d902024-03-18 15:38:12 -0700120 // Identifies which partition this is for //visibility:any_system_image (and others) visibility
121 // checks, and will be used in the future for API surface checks.
122 Partition_type *string
123
Inseob Kimcc8e5362021-02-03 14:05:24 +0900124 // file_contexts file to make image. Currently, only ext4 is supported.
125 File_contexts *string `android:"path"`
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900126
127 // Base directory relative to root, to which deps are installed, e.g. "system". Default is "."
128 // (root).
129 Base_dir *string
Inseob Kim14199b02021-02-09 21:18:31 +0900130
131 // Directories to be created under root. e.g. /dev, /proc, etc.
Cole Faustd9c6a5b2024-05-21 14:54:00 -0700132 Dirs proptools.Configurable[[]string]
Inseob Kim14199b02021-02-09 21:18:31 +0900133
134 // Symbolic links to be created under root with "ln -sf <target> <name>".
Spandan Das69464c32024-10-25 20:08:06 +0000135 Symlinks []SymlinkDefinition
Jooyung Han65f402b2022-04-21 14:24:04 +0900136
137 // Seconds since unix epoch to override timestamps of file entries
138 Fake_timestamp *string
139
140 // When set, passed to mkuserimg_mke2fs --mke2fs_uuid & --mke2fs_hash_seed.
141 // Otherwise, they'll be set as random which might cause indeterministic build output.
142 Uuid *string
Inseob Kim376d72f2023-11-01 15:40:25 +0900143
144 // Mount point for this image. Default is "/"
145 Mount_point *string
Cole Faust4a2a7c92024-03-12 12:44:40 -0700146
147 // If set to the name of a partition ("system", "vendor", etc), this filesystem module
148 // will also include the contents of the make-built staging directories. If any soong
149 // modules would be installed to the same location as a make module, they will overwrite
150 // the make version.
151 Include_make_built_files string
Inseob Kim53391842024-03-29 17:44:07 +0900152
Inseob Kimb7b84572024-04-30 10:51:47 +0900153 // When set, builds etc/event-log-tags file by merging logtags from all dependencies.
154 // Default is false
155 Build_logtags *bool
156
Justin Yun74f3f302024-05-07 14:32:14 +0900157 // Install aconfig_flags.pb file for the modules installed in this partition.
158 Gen_aconfig_flags_pb *bool
159
Inseob Kim53391842024-03-29 17:44:07 +0900160 Fsverity fsverityProperties
Cole Faust92ccbe22024-10-03 14:38:37 -0700161
162 // If this property is set to true, the filesystem will call ctx.UncheckedModule(), causing
163 // it to not be built on checkbuilds. Used for the automatic migration from make to soong
164 // build modules, where we want to emit some not-yet-working filesystems and we don't want them
165 // to be built.
166 Unchecked_module *bool `blueprint:"mutated"`
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000167
168 Erofs ErofsProperties
Jihoon Kang0d545b82024-10-11 00:21:57 +0000169
mrziwang1a6291f2024-11-07 14:29:25 -0800170 F2fs F2fsProperties
171
Spandan Das2047a4c2024-11-11 21:24:58 +0000172 Linker_config LinkerConfigProperties
Spandan Das92631882024-10-28 22:49:38 +0000173
Jihoon Kang0d545b82024-10-11 00:21:57 +0000174 // Determines if the module is auto-generated from Soong or not. If the module is
175 // auto-generated, its deps are exempted from visibility enforcement.
176 Is_auto_generated *bool
Jihoon Kang6c03c8e2024-11-18 21:30:22 +0000177
178 // Path to the dev nodes description file. This is only needed for building the ramdisk
179 // partition and should not be explicitly specified.
180 Dev_nodes_description_file *string `android:"path" blueprint:"mutated"`
Spandan Das71be42d2024-11-20 18:34:16 +0000181
182 // Additional dependencies used for building android products
183 Android_filesystem_deps AndroidFilesystemDeps
184}
185
186type AndroidFilesystemDeps struct {
187 System *string
188 System_ext *string
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000189}
190
191// Additional properties required to generate erofs FS partitions.
192type ErofsProperties struct {
193 // Compressor and Compression level passed to mkfs.erofs. e.g. (lz4hc,9)
194 // Please see external/erofs-utils/README for complete documentation.
195 Compressor *string
196
197 // Used as --compress-hints for mkfs.erofs
198 Compress_hints *string `android:"path"`
199
200 Sparse *bool
Jiyong Park71baa762021-01-18 21:11:03 +0900201}
202
mrziwang1a6291f2024-11-07 14:29:25 -0800203// Additional properties required to generate f2fs FS partitions.
204type F2fsProperties struct {
205 Sparse *bool
206}
207
Spandan Das173256b2024-10-31 19:59:30 +0000208type LinkerConfigProperties struct {
209
210 // Build a linker.config.pb file
211 Gen_linker_config *bool
212
213 // List of files (in .json format) that will be converted to a linker config file (in .pb format).
214 // The linker config file be installed in the filesystem at /etc/linker.config.pb
215 Linker_config_srcs []string `android:"path"`
216}
217
Jiyong Park65c49f52020-11-24 14:23:26 +0900218// android_filesystem packages a set of modules and their transitive dependencies into a filesystem
219// image. The filesystem images are expected to be mounted in the target device, which means the
220// modules in the filesystem image are built for the target device (i.e. Android, not Linux host).
221// The modules are placed in the filesystem image just like they are installed to the ordinary
222// partitions like system.img. For example, cc_library modules are placed under ./lib[64] directory.
Cole Faust92ccbe22024-10-03 14:38:37 -0700223func FilesystemFactory() android.Module {
Jiyong Park6f0f6882020-11-12 13:14:30 +0900224 module := &filesystem{}
Kiyoung Kim67118212024-11-07 13:23:44 +0900225 module.filesystemBuilder = module
Cole Faust2cfe6962024-09-17 11:31:14 -0700226 initFilesystemModule(module, module)
Jiyong Parkfa616132021-04-20 11:36:40 +0900227 return module
228}
229
Cole Faust2cfe6962024-09-17 11:31:14 -0700230func initFilesystemModule(module android.DefaultableModule, filesystemModule *filesystem) {
231 module.AddProperties(&filesystemModule.properties)
232 android.InitPackageModule(filesystemModule)
233 filesystemModule.PackagingBase.DepsCollectFirstTargetOnly = true
Jihoon Kang79196c52024-10-30 18:49:47 +0000234 filesystemModule.PackagingBase.AllowHighPriorityDeps = true
Jiyong Park6f0f6882020-11-12 13:14:30 +0900235 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900236 android.InitDefaultableModule(module)
Jihoon Kang6c03c8e2024-11-18 21:30:22 +0000237
238 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
239 filesystemModule.setDevNodesDescriptionProp()
240 })
Jiyong Park6f0f6882020-11-12 13:14:30 +0900241}
242
Jihoon Kang0d545b82024-10-11 00:21:57 +0000243type depTag struct {
Jiyong Park12a719c2021-01-07 15:31:24 +0900244 blueprint.BaseDependencyTag
Jooyung Han092ef812021-03-10 15:40:34 +0900245 android.PackagingItemAlwaysDepTag
Jihoon Kang0d545b82024-10-11 00:21:57 +0000246}
247
248var dependencyTag = depTag{}
249
250type depTagWithVisibilityEnforcementBypass struct {
251 depTag
252}
253
Spandan Das71be42d2024-11-20 18:34:16 +0000254type interPartitionDepTag struct {
255 blueprint.BaseDependencyTag
256}
257
258var interPartitionDependencyTag = interPartitionDepTag{}
259
Jihoon Kang0d545b82024-10-11 00:21:57 +0000260var _ android.ExcludeFromVisibilityEnforcementTag = (*depTagWithVisibilityEnforcementBypass)(nil)
261
262func (t depTagWithVisibilityEnforcementBypass) ExcludeFromVisibilityEnforcement() {}
263
264var dependencyTagWithVisibilityEnforcementBypass = depTagWithVisibilityEnforcementBypass{}
Jiyong Park65b62242020-11-25 12:44:59 +0900265
Jihoon Kang6c03c8e2024-11-18 21:30:22 +0000266// ramdiskDevNodesDescription is the name of the filegroup module that provides the file that
267// contains the description of dev nodes added to the CPIO archive for the ramdisk partition.
268const ramdiskDevNodesDescription = "ramdisk_node_list"
269
270func (f *filesystem) setDevNodesDescriptionProp() {
271 if proptools.String(f.properties.Partition_name) == "ramdisk" {
272 f.properties.Dev_nodes_description_file = proptools.StringPtr(":" + ramdiskDevNodesDescription)
273 }
274}
275
Jiyong Park6f0f6882020-11-12 13:14:30 +0900276func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000277 if proptools.Bool(f.properties.Is_auto_generated) {
278 f.AddDeps(ctx, dependencyTagWithVisibilityEnforcementBypass)
279 } else {
280 f.AddDeps(ctx, dependencyTag)
281 }
Spandan Das71be42d2024-11-20 18:34:16 +0000282 if f.properties.Android_filesystem_deps.System != nil {
283 ctx.AddDependency(ctx.Module(), interPartitionDependencyTag, proptools.String(f.properties.Android_filesystem_deps.System))
284 }
285 if f.properties.Android_filesystem_deps.System_ext != nil {
286 ctx.AddDependency(ctx.Module(), interPartitionDependencyTag, proptools.String(f.properties.Android_filesystem_deps.System_ext))
287 }
Jiyong Park6f0f6882020-11-12 13:14:30 +0900288}
289
Jiyong Park11a65972021-02-01 21:09:38 +0900290type fsType int
291
292const (
293 ext4Type fsType = iota
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000294 erofsType
mrziwang1a6291f2024-11-07 14:29:25 -0800295 f2fsType
Jiyong Park11a65972021-02-01 21:09:38 +0900296 compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900297 cpioType // uncompressed
Jiyong Park11a65972021-02-01 21:09:38 +0900298 unknown
299)
300
Spandan Das7a46f6c2024-10-14 18:41:18 +0000301func (fs fsType) IsUnknown() bool {
302 return fs == unknown
303}
304
Cole Faust92ccbe22024-10-03 14:38:37 -0700305type FilesystemInfo struct {
306 // A text file containing the list of paths installed on the partition.
307 FileListFile android.Path
308}
309
310var FilesystemProvider = blueprint.NewProvider[FilesystemInfo]()
311
Spandan Das7a46f6c2024-10-14 18:41:18 +0000312func GetFsTypeFromString(ctx android.EarlyModuleContext, typeStr string) fsType {
Jiyong Park11a65972021-02-01 21:09:38 +0900313 switch typeStr {
314 case "ext4":
315 return ext4Type
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000316 case "erofs":
317 return erofsType
mrziwang1a6291f2024-11-07 14:29:25 -0800318 case "f2fs":
319 return f2fsType
Jiyong Park11a65972021-02-01 21:09:38 +0900320 case "compressed_cpio":
321 return compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900322 case "cpio":
323 return cpioType
Jiyong Park11a65972021-02-01 21:09:38 +0900324 default:
Jiyong Park11a65972021-02-01 21:09:38 +0900325 return unknown
326 }
327}
328
Spandan Das7a46f6c2024-10-14 18:41:18 +0000329func (f *filesystem) fsType(ctx android.ModuleContext) fsType {
330 typeStr := proptools.StringDefault(f.properties.Type, "ext4")
331 fsType := GetFsTypeFromString(ctx, typeStr)
332 if fsType == unknown {
333 ctx.PropertyErrorf("type", "%q not supported", typeStr)
334 }
335 return fsType
336}
337
Jiyong Park65c49f52020-11-24 14:23:26 +0900338func (f *filesystem) installFileName() string {
339 return f.BaseModuleName() + ".img"
340}
341
Inseob Kim53391842024-03-29 17:44:07 +0900342func (f *filesystem) partitionName() string {
343 return proptools.StringDefault(f.properties.Partition_name, f.Name())
344}
345
Kiyoung Kim67118212024-11-07 13:23:44 +0900346func (f *filesystem) FilterPackagingSpec(ps android.PackagingSpec) bool {
Jiyong Park7e7d4af2024-05-01 12:36:10 +0000347 // Filesystem module respects the installation semantic. A PackagingSpec from a module with
348 // IsSkipInstall() is skipped.
Cole Faust76a6e952024-11-07 16:56:45 -0800349 if ps.SkipInstall() {
350 return false
Spandan Das6d056502024-10-21 15:40:32 +0000351 }
Cole Faust76a6e952024-11-07 16:56:45 -0800352 if proptools.Bool(f.properties.Is_auto_generated) { // TODO (spandandas): Remove this.
353 pt := f.PartitionType()
Cole Faustc88cff12024-11-12 13:24:05 -0800354 return ps.Partition() == pt || strings.HasPrefix(ps.Partition(), pt+"/")
Cole Faust76a6e952024-11-07 16:56:45 -0800355 }
356 return true
Jiyong Park7e7d4af2024-05-01 12:36:10 +0000357}
358
Inseob Kim3c0a0422024-11-05 17:21:37 +0900359func (f *filesystem) ModifyPackagingSpec(ps *android.PackagingSpec) {
Cole Faustc88cff12024-11-12 13:24:05 -0800360 // Sometimes, android.modulePartition() returns a path with >1 path components.
361 // This makes the partition field of packagingSpecs have multiple components, like
362 // "system/product". Right now, the filesystem module doesn't look at the partition field
363 // when deciding what path to install the file under, only the RelPathInPackage field, so
364 // we move the later path components from partition to relPathInPackage. This should probably
365 // be revisited in the future.
366 prefix := f.PartitionType() + "/"
367 if strings.HasPrefix(ps.Partition(), prefix) {
368 subPartition := strings.TrimPrefix(ps.Partition(), prefix)
369 ps.SetPartition(f.PartitionType())
370 ps.SetRelPathInPackage(filepath.Join(subPartition, ps.RelPathInPackage()))
371 }
Inseob Kim3c0a0422024-11-05 17:21:37 +0900372}
373
Jiyong Park6f0f6882020-11-12 13:14:30 +0900374var pctx = android.NewPackageContext("android/soong/filesystem")
375
376func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900377 validatePartitionType(ctx, f)
Kiyoung Kim23be5bb2024-11-27 00:50:30 +0000378 if f.filesystemBuilder.ShouldUseVintfFragmentModuleOnly() {
379 f.validateVintfFragments(ctx)
380 }
Jiyong Park11a65972021-02-01 21:09:38 +0900381 switch f.fsType(ctx) {
mrziwang1a6291f2024-11-07 14:29:25 -0800382 case ext4Type, erofsType, f2fsType:
Jiyong Park11a65972021-02-01 21:09:38 +0900383 f.output = f.buildImageUsingBuildImage(ctx)
384 case compressedCpioType:
Jiyong Park837cdb22021-02-05 00:17:14 +0900385 f.output = f.buildCpioImage(ctx, true)
386 case cpioType:
387 f.output = f.buildCpioImage(ctx, false)
Jiyong Park11a65972021-02-01 21:09:38 +0900388 default:
389 return
390 }
391
392 f.installDir = android.PathForModuleInstall(ctx, "etc")
393 ctx.InstallFile(f.installDir, f.installFileName(), f.output)
mrziwang555d1332024-06-07 11:15:33 -0700394 ctx.SetOutputFiles([]android.Path{f.output}, "")
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900395
Cole Faust4e9f5922024-11-13 16:09:23 -0800396 fileListFile := android.PathForModuleOut(ctx, "fileList")
397 android.WriteFileRule(ctx, fileListFile, f.installedFilesList())
Cole Faust92ccbe22024-10-03 14:38:37 -0700398
399 android.SetProvider(ctx, FilesystemProvider, FilesystemInfo{
Cole Faust4e9f5922024-11-13 16:09:23 -0800400 FileListFile: fileListFile,
Cole Faust92ccbe22024-10-03 14:38:37 -0700401 })
Cole Faust4e9f5922024-11-13 16:09:23 -0800402 f.fileListFile = fileListFile
Cole Faust92ccbe22024-10-03 14:38:37 -0700403
404 if proptools.Bool(f.properties.Unchecked_module) {
405 ctx.UncheckedModule()
406 }
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900407}
408
Kiyoung Kim23be5bb2024-11-27 00:50:30 +0000409func (f *filesystem) validateVintfFragments(ctx android.ModuleContext) {
410 visitedModule := map[string]bool{}
411 packagingSpecs := f.gatherFilteredPackagingSpecs(ctx)
412
413 moduleInFileSystem := func(mod android.Module) bool {
414 for _, ps := range android.OtherModuleProviderOrDefault(
415 ctx, mod, android.InstallFilesProvider).PackagingSpecs {
416 if _, ok := packagingSpecs[ps.RelPathInPackage()]; ok {
417 return true
418 }
419 }
420 return false
421 }
422
423 ctx.WalkDeps(func(child, parent android.Module) bool {
424 if visitedModule[child.Name()] {
425 return false
426 }
427 if !moduleInFileSystem(child) {
428 visitedModule[child.Name()] = true
429 return true
430 }
431 if vintfFragments := child.VintfFragments(ctx); vintfFragments != nil {
432 ctx.PropertyErrorf(
433 "vintf_fragments",
434 "Module %s is referenced by soong-defined filesystem %s with property vintf_fragments(%s) in use."+
435 " Use vintf_fragment_modules property instead.",
436 child.Name(),
437 f.BaseModuleName(),
438 strings.Join(vintfFragments, ", "),
439 )
440 }
441 visitedModule[child.Name()] = true
442 return true
443 })
444}
445
Cole Faust4e9f5922024-11-13 16:09:23 -0800446func (f *filesystem) appendToEntry(ctx android.ModuleContext, installedFile android.Path) {
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900447 partitionBaseDir := android.PathForModuleOut(ctx, "root", f.partitionName()).String() + "/"
448
449 relPath, inTargetPartition := strings.CutPrefix(installedFile.String(), partitionBaseDir)
450 if inTargetPartition {
451 f.entries = append(f.entries, relPath)
452 }
453}
454
455func (f *filesystem) installedFilesList() string {
456 installedFilePaths := android.FirstUniqueStrings(f.entries)
457 slices.Sort(installedFilePaths)
458
459 return strings.Join(installedFilePaths, "\n")
Jiyong Park11a65972021-02-01 21:09:38 +0900460}
461
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900462func validatePartitionType(ctx android.ModuleContext, p partition) {
463 if !android.InList(p.PartitionType(), validPartitions) {
464 ctx.PropertyErrorf("partition_type", "partition_type must be one of %s, found: %s", validPartitions, p.PartitionType())
465 }
466
467 ctx.VisitDirectDepsWithTag(android.DefaultsDepTag, func(m android.Module) {
468 if fdm, ok := m.(*filesystemDefaults); ok {
469 if p.PartitionType() != fdm.PartitionType() {
470 ctx.PropertyErrorf("partition_type",
471 "%s doesn't match with the partition type %s of the filesystem default module %s",
472 p.PartitionType(), fdm.PartitionType(), m.Name())
473 }
474 }
475 })
476}
477
Cole Faust3b806d32024-03-11 15:15:03 -0700478// Copy extra files/dirs that are not from the `deps` property to `rootDir`, checking for conflicts with files
479// already in `rootDir`.
480func (f *filesystem) buildNonDepsFiles(ctx android.ModuleContext, builder *android.RuleBuilder, rootDir android.OutputPath) {
Inseob Kim14199b02021-02-09 21:18:31 +0900481 // create dirs and symlinks
Cole Faustd9c6a5b2024-05-21 14:54:00 -0700482 for _, dir := range f.properties.Dirs.GetOrDefault(ctx, nil) {
Inseob Kim14199b02021-02-09 21:18:31 +0900483 // OutputPath.Join verifies dir
484 builder.Command().Text("mkdir -p").Text(rootDir.Join(ctx, dir).String())
485 }
486
487 for _, symlink := range f.properties.Symlinks {
488 name := strings.TrimSpace(proptools.String(symlink.Name))
489 target := strings.TrimSpace(proptools.String(symlink.Target))
490
491 if name == "" {
492 ctx.PropertyErrorf("symlinks", "Name can't be empty")
493 continue
494 }
495
496 if target == "" {
497 ctx.PropertyErrorf("symlinks", "Target can't be empty")
498 continue
499 }
500
501 // OutputPath.Join verifies name. don't need to verify target.
502 dst := rootDir.Join(ctx, name)
Cole Faust3b806d32024-03-11 15:15:03 -0700503 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 Kim14199b02021-02-09 21:18:31 +0900504 builder.Command().Text("mkdir -p").Text(filepath.Dir(dst.String()))
505 builder.Command().Text("ln -sf").Text(proptools.ShellEscape(target)).Text(dst.String())
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900506 f.appendToEntry(ctx, dst)
Inseob Kim14199b02021-02-09 21:18:31 +0900507 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900508}
509
Inseob Kim33f95a92024-07-11 15:44:49 +0900510func (f *filesystem) copyPackagingSpecs(ctx android.ModuleContext, builder *android.RuleBuilder, specs map[string]android.PackagingSpec, rootDir, rebasedDir android.WritablePath) []string {
511 rootDirSpecs := make(map[string]android.PackagingSpec)
512 rebasedDirSpecs := make(map[string]android.PackagingSpec)
513
514 for rel, spec := range specs {
515 if spec.Partition() == "root" {
516 rootDirSpecs[rel] = spec
517 } else {
518 rebasedDirSpecs[rel] = spec
519 }
520 }
521
522 dirsToSpecs := make(map[android.WritablePath]map[string]android.PackagingSpec)
523 dirsToSpecs[rootDir] = rootDirSpecs
524 dirsToSpecs[rebasedDir] = rebasedDirSpecs
525
526 return f.CopySpecsToDirs(ctx, builder, dirsToSpecs)
527}
528
Justin Yun34baa2e2024-08-30 21:11:33 +0900529func (f *filesystem) copyFilesToProductOut(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath) {
Justin Yun2cc42502024-09-11 14:10:20 +0900530 if f.Name() != ctx.Config().SoongDefinedSystemImage() {
Justin Yun34baa2e2024-08-30 21:11:33 +0900531 return
532 }
533 installPath := android.PathForModuleInPartitionInstall(ctx, f.partitionName())
534 builder.Command().Textf("cp -prf %s/* %s", rebasedDir, installPath)
535}
536
Cole Faust4e9f5922024-11-13 16:09:23 -0800537func (f *filesystem) buildImageUsingBuildImage(ctx android.ModuleContext) android.Path {
Jiyong Park6f0f6882020-11-12 13:14:30 +0900538 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Cole Faust3b806d32024-03-11 15:15:03 -0700539 rebasedDir := rootDir
540 if f.properties.Base_dir != nil {
541 rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir)
542 }
543 builder := android.NewRuleBuilder(pctx, ctx)
544 // Wipe the root dir to get rid of leftover files from prior builds
545 builder.Command().Textf("rm -rf %s && mkdir -p %s", rootDir, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900546 specs := f.gatherFilteredPackagingSpecs(ctx)
Inseob Kim33f95a92024-07-11 15:44:49 +0900547 f.entries = f.copyPackagingSpecs(ctx, builder, specs, rootDir, rebasedDir)
Cole Faust3b806d32024-03-11 15:15:03 -0700548
549 f.buildNonDepsFiles(ctx, builder, rootDir)
Cole Faust4a2a7c92024-03-12 12:44:40 -0700550 f.addMakeBuiltFiles(ctx, builder, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900551 f.buildFsverityMetadataFiles(ctx, builder, specs, rootDir, rebasedDir)
Inseob Kimb7b84572024-04-30 10:51:47 +0900552 f.buildEventLogtagsFile(ctx, builder, rebasedDir)
Justin Yun74f3f302024-05-07 14:32:14 +0900553 f.buildAconfigFlagsFiles(ctx, builder, specs, rebasedDir)
Kiyoung Kim67118212024-11-07 13:23:44 +0900554 f.filesystemBuilder.BuildLinkerConfigFile(ctx, builder, rebasedDir)
Justin Yun34baa2e2024-08-30 21:11:33 +0900555 f.copyFilesToProductOut(ctx, builder, rebasedDir)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900556
Nikita Ioffe519015f2022-12-23 15:36:29 +0000557 // run host_init_verifier
558 // Ideally we should have a concept of pluggable linters that verify the generated image.
559 // While such concept is not implement this will do.
560 // TODO(b/263574231): substitute with pluggable linter.
561 builder.Command().
562 BuiltTool("host_init_verifier").
563 FlagWithArg("--out_system=", rootDir.String()+"/system")
564
Jiyong Park72678312021-01-18 17:29:49 +0900565 propFile, toolDeps := f.buildPropFile(ctx)
Cole Faust4e9f5922024-11-13 16:09:23 -0800566 output := android.PathForModuleOut(ctx, f.installFileName())
Colin Crossf1a035e2020-11-16 17:32:30 -0800567 builder.Command().BuiltTool("build_image").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900568 Text(rootDir.String()). // input directory
569 Input(propFile).
Jiyong Park72678312021-01-18 17:29:49 +0900570 Implicits(toolDeps).
Jiyong Park11a65972021-02-01 21:09:38 +0900571 Output(output).
Jiyong Park6f0f6882020-11-12 13:14:30 +0900572 Text(rootDir.String()) // directory where to find fs_config_files|dirs
573
574 // rootDir is not deleted. Might be useful for quick inspection.
Colin Crossf1a035e2020-11-16 17:32:30 -0800575 builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park65c49f52020-11-24 14:23:26 +0900576
Jiyong Park11a65972021-02-01 21:09:38 +0900577 return output
Jiyong Park65c49f52020-11-24 14:23:26 +0900578}
579
Cole Faust4e9f5922024-11-13 16:09:23 -0800580func (f *filesystem) buildFileContexts(ctx android.ModuleContext) android.Path {
Inseob Kimcc8e5362021-02-03 14:05:24 +0900581 builder := android.NewRuleBuilder(pctx, ctx)
582 fcBin := android.PathForModuleOut(ctx, "file_contexts.bin")
583 builder.Command().BuiltTool("sefcontext_compile").
584 FlagWithOutput("-o ", fcBin).
585 Input(android.PathForModuleSrc(ctx, proptools.String(f.properties.File_contexts)))
586 builder.Build("build_filesystem_file_contexts", fmt.Sprintf("Creating filesystem file contexts for %s", f.BaseModuleName()))
Cole Faust4e9f5922024-11-13 16:09:23 -0800587 return fcBin
Inseob Kimcc8e5362021-02-03 14:05:24 +0900588}
589
Jooyung Han65f402b2022-04-21 14:24:04 +0900590// Calculates avb_salt from entry list (sorted) for deterministic output.
591func (f *filesystem) salt() string {
592 return sha1sum(f.entries)
593}
594
Cole Faust4e9f5922024-11-13 16:09:23 -0800595func (f *filesystem) buildPropFile(ctx android.ModuleContext) (android.Path, android.Paths) {
Jiyong Park72678312021-01-18 17:29:49 +0900596 var deps android.Paths
Cole Faustcec230a2024-03-07 15:51:12 -0800597 var propFileString strings.Builder
Jiyong Park72678312021-01-18 17:29:49 +0900598 addStr := func(name string, value string) {
Cole Faustcec230a2024-03-07 15:51:12 -0800599 propFileString.WriteString(name)
600 propFileString.WriteRune('=')
601 propFileString.WriteString(value)
602 propFileString.WriteRune('\n')
Jiyong Park72678312021-01-18 17:29:49 +0900603 }
604 addPath := func(name string, path android.Path) {
Cole Faustcec230a2024-03-07 15:51:12 -0800605 addStr(name, path.String())
Jiyong Park72678312021-01-18 17:29:49 +0900606 deps = append(deps, path)
607 }
608
Jiyong Park11a65972021-02-01 21:09:38 +0900609 // Type string that build_image.py accepts.
610 fsTypeStr := func(t fsType) string {
611 switch t {
Spandan Das94668822024-10-09 20:51:33 +0000612 // TODO(372522486): add more types like f2fs, erofs, etc.
Jiyong Park11a65972021-02-01 21:09:38 +0900613 case ext4Type:
614 return "ext4"
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000615 case erofsType:
616 return "erofs"
mrziwang1a6291f2024-11-07 14:29:25 -0800617 case f2fsType:
618 return "f2fs"
Jiyong Park11a65972021-02-01 21:09:38 +0900619 }
620 panic(fmt.Errorf("unsupported fs type %v", t))
621 }
622
623 addStr("fs_type", fsTypeStr(f.fsType(ctx)))
Inseob Kim376d72f2023-11-01 15:40:25 +0900624 addStr("mount_point", proptools.StringDefault(f.properties.Mount_point, "/"))
Jiyong Park72678312021-01-18 17:29:49 +0900625 addStr("use_dynamic_partition_size", "true")
626 addPath("ext_mkuserimg", ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs"))
627 // b/177813163 deps of the host tools have to be added. Remove this.
628 for _, t := range []string{"mke2fs", "e2fsdroid", "tune2fs"} {
629 deps = append(deps, ctx.Config().HostToolPath(ctx, t))
630 }
631
Jiyong Park71baa762021-01-18 21:11:03 +0900632 if proptools.Bool(f.properties.Use_avb) {
633 addStr("avb_hashtree_enable", "true")
634 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
635 algorithm := proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096")
636 addStr("avb_algorithm", algorithm)
637 key := android.PathForModuleSrc(ctx, proptools.String(f.properties.Avb_private_key))
638 addPath("avb_key_path", key)
Inseob Kim53391842024-03-29 17:44:07 +0900639 addStr("partition_name", f.partitionName())
Shikha Panware6f30632022-12-21 12:54:45 +0000640 avb_add_hashtree_footer_args := "--do_not_generate_fec"
641 if hashAlgorithm := proptools.String(f.properties.Avb_hash_algorithm); hashAlgorithm != "" {
642 avb_add_hashtree_footer_args += " --hash_algorithm " + hashAlgorithm
643 }
Nikita Ioffe2c8cdc62024-03-27 22:19:30 +0000644 if f.properties.Rollback_index != nil {
645 rollbackIndex := proptools.Int(f.properties.Rollback_index)
646 if rollbackIndex < 0 {
647 ctx.PropertyErrorf("rollback_index", "Rollback index must be non-negative")
648 }
649 avb_add_hashtree_footer_args += " --rollback_index " + strconv.Itoa(rollbackIndex)
650 }
Inseob Kim53391842024-03-29 17:44:07 +0900651 securityPatchKey := "com.android.build." + f.partitionName() + ".security_patch"
Seungjae Yooa30e4502023-11-09 14:55:44 +0900652 securityPatchValue := ctx.Config().PlatformSecurityPatch()
653 avb_add_hashtree_footer_args += " --prop " + securityPatchKey + ":" + securityPatchValue
Shikha Panware6f30632022-12-21 12:54:45 +0000654 addStr("avb_add_hashtree_footer_args", avb_add_hashtree_footer_args)
Jooyung Han65f402b2022-04-21 14:24:04 +0900655 addStr("avb_salt", f.salt())
Jiyong Park71baa762021-01-18 21:11:03 +0900656 }
657
Inseob Kimcc8e5362021-02-03 14:05:24 +0900658 if proptools.String(f.properties.File_contexts) != "" {
659 addPath("selinux_fc", f.buildFileContexts(ctx))
660 }
Jooyung Han65f402b2022-04-21 14:24:04 +0900661 if timestamp := proptools.String(f.properties.Fake_timestamp); timestamp != "" {
662 addStr("timestamp", timestamp)
663 }
664 if uuid := proptools.String(f.properties.Uuid); uuid != "" {
665 addStr("uuid", uuid)
666 addStr("hash_seed", uuid)
667 }
mrziwang1a6291f2024-11-07 14:29:25 -0800668
Cole Faust43a52c72024-11-26 12:46:08 -0800669 // TODO(b/381120092): This should only be added if none of the size-related properties are set,
670 // but currently soong built partitions don't have size properties. Make code:
671 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=2262;drc=39cd33701c9278db0e7e481a090605f428d5b12d
672 // Make uses system_disable_sparse but disable_sparse has the same effect, and we shouldn't need
673 // to qualify it because each partition gets its own property file built.
674 addStr("disable_sparse", "true")
675
mrziwang1a6291f2024-11-07 14:29:25 -0800676 fst := f.fsType(ctx)
677 switch fst {
678 case erofsType:
679 // Add erofs properties
Cole Faust3e730972024-12-03 13:12:08 -0800680 addStr("erofs_default_compressor", proptools.StringDefault(f.properties.Erofs.Compressor, "lz4hc,9"))
681 if f.properties.Erofs.Compress_hints != nil {
682 src := android.PathForModuleSrc(ctx, *f.properties.Erofs.Compress_hints)
683 addPath("erofs_default_compress_hints", src)
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000684 }
685 if proptools.BoolDefault(f.properties.Erofs.Sparse, true) {
686 // https://source.corp.google.com/h/googleplex-android/platform/build/+/88b1c67239ca545b11580237242774b411f2fed9:core/Makefile;l=2292;bpv=1;bpt=0;drc=ea8f34bc1d6e63656b4ec32f2391e9d54b3ebb6b
687 addStr("erofs_sparse_flag", "-s")
688 }
mrziwang1a6291f2024-11-07 14:29:25 -0800689 case f2fsType:
690 if proptools.BoolDefault(f.properties.F2fs.Sparse, true) {
691 // https://source.corp.google.com/h/googleplex-android/platform/build/+/88b1c67239ca545b11580237242774b411f2fed9:core/Makefile;l=2294;drc=ea8f34bc1d6e63656b4ec32f2391e9d54b3ebb6b;bpv=1;bpt=0
692 addStr("f2fs_sparse_flag", "-S")
693 }
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000694 }
mrziwang1a6291f2024-11-07 14:29:25 -0800695 f.checkFsTypePropertyError(ctx, fst, fsTypeStr(fst))
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000696
Cole Faust4e9f5922024-11-13 16:09:23 -0800697 propFile := android.PathForModuleOut(ctx, "prop")
Cole Faustcec230a2024-03-07 15:51:12 -0800698 android.WriteFileRuleVerbatim(ctx, propFile, propFileString.String())
Jiyong Park72678312021-01-18 17:29:49 +0900699 return propFile, deps
700}
701
mrziwang1a6291f2024-11-07 14:29:25 -0800702// This method checks if there is any property set for the fstype(s) other than
703// the current fstype.
704func (f *filesystem) checkFsTypePropertyError(ctx android.ModuleContext, t fsType, fs string) {
705 raiseError := func(otherFsType, currentFsType string) {
706 errMsg := fmt.Sprintf("%s is non-empty, but FS type is %s\n. Please delete %s properties if this partition should use %s\n", otherFsType, currentFsType, otherFsType, currentFsType)
707 ctx.PropertyErrorf(otherFsType, errMsg)
708 }
709
710 if t != erofsType {
711 if f.properties.Erofs.Compressor != nil || f.properties.Erofs.Compress_hints != nil || f.properties.Erofs.Sparse != nil {
712 raiseError("erofs", fs)
713 }
714 }
715 if t != f2fsType {
716 if f.properties.F2fs.Sparse != nil {
717 raiseError("f2fs", fs)
718 }
719 }
720}
721
Cole Faust4e9f5922024-11-13 16:09:23 -0800722func (f *filesystem) buildCpioImage(ctx android.ModuleContext, compressed bool) android.Path {
Jiyong Park11a65972021-02-01 21:09:38 +0900723 if proptools.Bool(f.properties.Use_avb) {
724 ctx.PropertyErrorf("use_avb", "signing compresed cpio image using avbtool is not supported."+
725 "Consider adding this to bootimg module and signing the entire boot image.")
726 }
727
Inseob Kimcc8e5362021-02-03 14:05:24 +0900728 if proptools.String(f.properties.File_contexts) != "" {
729 ctx.PropertyErrorf("file_contexts", "file_contexts is not supported for compressed cpio image.")
730 }
731
Cole Faust4a2a7c92024-03-12 12:44:40 -0700732 if f.properties.Include_make_built_files != "" {
733 ctx.PropertyErrorf("include_make_built_files", "include_make_built_files is not supported for compressed cpio image.")
734 }
735
Jiyong Park11a65972021-02-01 21:09:38 +0900736 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Cole Faust3b806d32024-03-11 15:15:03 -0700737 rebasedDir := rootDir
738 if f.properties.Base_dir != nil {
739 rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir)
740 }
741 builder := android.NewRuleBuilder(pctx, ctx)
742 // Wipe the root dir to get rid of leftover files from prior builds
743 builder.Command().Textf("rm -rf %s && mkdir -p %s", rootDir, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900744 specs := f.gatherFilteredPackagingSpecs(ctx)
Inseob Kim33f95a92024-07-11 15:44:49 +0900745 f.entries = f.copyPackagingSpecs(ctx, builder, specs, rootDir, rebasedDir)
Cole Faust3b806d32024-03-11 15:15:03 -0700746
747 f.buildNonDepsFiles(ctx, builder, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900748 f.buildFsverityMetadataFiles(ctx, builder, specs, rootDir, rebasedDir)
Inseob Kimb7b84572024-04-30 10:51:47 +0900749 f.buildEventLogtagsFile(ctx, builder, rebasedDir)
Justin Yun74f3f302024-05-07 14:32:14 +0900750 f.buildAconfigFlagsFiles(ctx, builder, specs, rebasedDir)
Kiyoung Kim67118212024-11-07 13:23:44 +0900751 f.filesystemBuilder.BuildLinkerConfigFile(ctx, builder, rebasedDir)
Justin Yun34baa2e2024-08-30 21:11:33 +0900752 f.copyFilesToProductOut(ctx, builder, rebasedDir)
Jiyong Park11a65972021-02-01 21:09:38 +0900753
Cole Faust4e9f5922024-11-13 16:09:23 -0800754 output := android.PathForModuleOut(ctx, f.installFileName())
Jiyong Park837cdb22021-02-05 00:17:14 +0900755 cmd := builder.Command().
Jiyong Park11a65972021-02-01 21:09:38 +0900756 BuiltTool("mkbootfs").
Jiyong Park837cdb22021-02-05 00:17:14 +0900757 Text(rootDir.String()) // input directory
Jihoon Kang6c03c8e2024-11-18 21:30:22 +0000758 if nodeList := f.properties.Dev_nodes_description_file; nodeList != nil {
759 cmd.FlagWithInput("-n ", android.PathForModuleSrc(ctx, proptools.String(nodeList)))
760 }
Jiyong Park837cdb22021-02-05 00:17:14 +0900761 if compressed {
762 cmd.Text("|").
763 BuiltTool("lz4").
764 Flag("--favor-decSpeed"). // for faster boot
765 Flag("-12"). // maximum compression level
766 Flag("-l"). // legacy format for kernel
767 Text(">").Output(output)
768 } else {
769 cmd.Text(">").Output(output)
770 }
Jiyong Park11a65972021-02-01 21:09:38 +0900771
772 // rootDir is not deleted. Might be useful for quick inspection.
Jiyong Park837cdb22021-02-05 00:17:14 +0900773 builder.Build("build_cpio_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park11a65972021-02-01 21:09:38 +0900774
775 return output
776}
777
Cole Faust4a2a7c92024-03-12 12:44:40 -0700778var validPartitions = []string{
779 "system",
780 "userdata",
781 "cache",
782 "system_other",
783 "vendor",
784 "product",
785 "system_ext",
786 "odm",
787 "vendor_dlkm",
788 "odm_dlkm",
789 "system_dlkm",
Cole Faust76a6e952024-11-07 16:56:45 -0800790 "ramdisk",
Cole Faust24938e22024-11-18 14:01:58 -0800791 "vendor_ramdisk",
Jihoon Kang3216c982024-12-02 19:42:20 +0000792 "recovery",
Cole Faust4a2a7c92024-03-12 12:44:40 -0700793}
794
795func (f *filesystem) addMakeBuiltFiles(ctx android.ModuleContext, builder *android.RuleBuilder, rootDir android.Path) {
796 partition := f.properties.Include_make_built_files
797 if partition == "" {
798 return
799 }
800 if !slices.Contains(validPartitions, partition) {
801 ctx.PropertyErrorf("include_make_built_files", "Expected one of %#v, found %q", validPartitions, partition)
802 return
803 }
804 stampFile := fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/staging_dir.stamp", ctx.Config().DeviceName(), partition)
805 fileListFile := fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partition)
806 stagingDir := fmt.Sprintf("target/product/%s/%s", ctx.Config().DeviceName(), partition)
807
808 builder.Command().BuiltTool("merge_directories").
809 Implicit(android.PathForArbitraryOutput(ctx, stampFile)).
810 Text("--ignore-duplicates").
811 FlagWithInput("--file-list", android.PathForArbitraryOutput(ctx, fileListFile)).
812 Text(rootDir.String()).
813 Text(android.PathForArbitraryOutput(ctx, stagingDir).String())
814}
815
Inseob Kimb7b84572024-04-30 10:51:47 +0900816func (f *filesystem) buildEventLogtagsFile(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath) {
817 if !proptools.Bool(f.properties.Build_logtags) {
818 return
819 }
820
821 logtagsFilePaths := make(map[string]bool)
822 ctx.WalkDeps(func(child, parent android.Module) bool {
823 if logtagsInfo, ok := android.OtherModuleProvider(ctx, child, android.LogtagsProviderKey); ok {
824 for _, path := range logtagsInfo.Logtags {
825 logtagsFilePaths[path.String()] = true
826 }
827 }
828 return true
829 })
830
831 if len(logtagsFilePaths) == 0 {
832 return
833 }
834
835 etcPath := rebasedDir.Join(ctx, "etc")
836 eventLogtagsPath := etcPath.Join(ctx, "event-log-tags")
837 builder.Command().Text("mkdir").Flag("-p").Text(etcPath.String())
838 cmd := builder.Command().BuiltTool("merge-event-log-tags").
839 FlagWithArg("-o ", eventLogtagsPath.String()).
840 FlagWithInput("-m ", android.MergedLogtagsPath(ctx))
841
842 for _, path := range android.SortedKeys(logtagsFilePaths) {
843 cmd.Text(path)
844 }
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900845
846 f.appendToEntry(ctx, eventLogtagsPath)
Inseob Kimb7b84572024-04-30 10:51:47 +0900847}
848
Kiyoung Kim67118212024-11-07 13:23:44 +0900849func (f *filesystem) BuildLinkerConfigFile(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath) {
Spandan Das2047a4c2024-11-11 21:24:58 +0000850 if !proptools.Bool(f.properties.Linker_config.Gen_linker_config) {
Spandan Das92631882024-10-28 22:49:38 +0000851 return
852 }
853
Spandan Das918191e2024-10-31 18:27:23 +0000854 provideModules, _ := f.getLibsForLinkerConfig(ctx)
Spandan Das92631882024-10-28 22:49:38 +0000855 output := rebasedDir.Join(ctx, "etc", "linker.config.pb")
Spandan Das2047a4c2024-11-11 21:24:58 +0000856 linkerconfig.BuildLinkerConfig(ctx, builder, android.PathsForModuleSrc(ctx, f.properties.Linker_config.Linker_config_srcs), provideModules, nil, output)
Spandan Das92631882024-10-28 22:49:38 +0000857
858 f.appendToEntry(ctx, output)
859}
860
Kiyoung Kim23be5bb2024-11-27 00:50:30 +0000861func (f *filesystem) ShouldUseVintfFragmentModuleOnly() bool {
862 return false
863}
864
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900865type partition interface {
866 PartitionType() string
867}
868
Cole Faust9a24d902024-03-18 15:38:12 -0700869func (f *filesystem) PartitionType() string {
870 return proptools.StringDefault(f.properties.Partition_type, "system")
871}
872
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900873var _ partition = (*filesystem)(nil)
874
Jiyong Park65c49f52020-11-24 14:23:26 +0900875var _ android.AndroidMkEntriesProvider = (*filesystem)(nil)
876
877// Implements android.AndroidMkEntriesProvider
878func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries {
879 return []android.AndroidMkEntries{android.AndroidMkEntries{
880 Class: "ETC",
881 OutputFile: android.OptionalPathForPath(f.output),
882 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700883 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800884 entries.SetString("LOCAL_MODULE_PATH", f.installDir.String())
Jiyong Park65c49f52020-11-24 14:23:26 +0900885 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900886 entries.SetString("LOCAL_FILESYSTEM_FILELIST", f.fileListFile.String())
Jiyong Park65c49f52020-11-24 14:23:26 +0900887 },
888 },
889 }}
Jiyong Park6f0f6882020-11-12 13:14:30 +0900890}
Jiyong Park12a719c2021-01-07 15:31:24 +0900891
892// Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex
893// package to have access to the output file.
894type Filesystem interface {
895 android.Module
896 OutputPath() android.Path
Jiyong Park972e06c2021-03-15 23:32:49 +0900897
898 // Returns the output file that is signed by avbtool. If this module is not signed, returns
899 // nil.
900 SignedOutputPath() android.Path
Jiyong Park12a719c2021-01-07 15:31:24 +0900901}
902
903var _ Filesystem = (*filesystem)(nil)
904
905func (f *filesystem) OutputPath() android.Path {
906 return f.output
907}
Jiyong Park972e06c2021-03-15 23:32:49 +0900908
909func (f *filesystem) SignedOutputPath() android.Path {
910 if proptools.Bool(f.properties.Use_avb) {
911 return f.OutputPath()
912 }
913 return nil
914}
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900915
916// Filter the result of GatherPackagingSpecs to discard items targeting outside "system" partition.
917// Note that "apex" module installs its contents to "apex"(fake partition) as well
918// for symbol lookup by imitating "activated" paths.
919func (f *filesystem) gatherFilteredPackagingSpecs(ctx android.ModuleContext) map[string]android.PackagingSpec {
Inseob Kim3c0a0422024-11-05 17:21:37 +0900920 specs := f.PackagingBase.GatherPackagingSpecsWithFilterAndModifier(ctx, f.filesystemBuilder.FilterPackagingSpec, f.filesystemBuilder.ModifyPackagingSpec)
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900921 return specs
922}
Jooyung Han65f402b2022-04-21 14:24:04 +0900923
924func sha1sum(values []string) string {
925 h := sha256.New()
926 for _, value := range values {
927 io.WriteString(h, value)
928 }
929 return fmt.Sprintf("%x", h.Sum(nil))
930}
Jooyung Hane6067592023-03-16 13:11:17 +0900931
932// Base cc.UseCoverage
933
934var _ cc.UseCoverage = (*filesystem)(nil)
935
Colin Crosse1a85552024-06-14 12:17:37 -0700936func (*filesystem) IsNativeCoverageNeeded(ctx cc.IsNativeCoverageNeededContext) bool {
Jooyung Hane6067592023-03-16 13:11:17 +0900937 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
938}
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900939
940// android_filesystem_defaults
941
942type filesystemDefaults struct {
943 android.ModuleBase
944 android.DefaultsModuleBase
945
Inseob Kim3c0a0422024-11-05 17:21:37 +0900946 properties FilesystemProperties
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900947}
948
949// android_filesystem_defaults is a default module for android_filesystem and android_system_image
950func filesystemDefaultsFactory() android.Module {
951 module := &filesystemDefaults{}
952 module.AddProperties(&module.properties)
953 module.AddProperties(&android.PackagingProperties{})
954 android.InitDefaultsModule(module)
955 return module
956}
957
958func (f *filesystemDefaults) PartitionType() string {
959 return proptools.StringDefault(f.properties.Partition_type, "system")
960}
961
962var _ partition = (*filesystemDefaults)(nil)
963
964func (f *filesystemDefaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
965 validatePartitionType(ctx, f)
966}
Spandan Das918191e2024-10-31 18:27:23 +0000967
968// getLibsForLinkerConfig returns
969// 1. A list of libraries installed in this filesystem
970// 2. A list of dep libraries _not_ installed in this filesystem
971//
972// `linkerconfig.BuildLinkerConfig` will convert these two to a linker.config.pb for the filesystem
973// (1) will be added to --provideLibs if they are C libraries with a stable interface (has stubs)
974// (2) will be added to --requireLibs if they are C libraries with a stable interface (has stubs)
975func (f *filesystem) getLibsForLinkerConfig(ctx android.ModuleContext) ([]android.Module, []android.Module) {
976 // we need "Module"s for packaging items
977 modulesInPackageByModule := make(map[android.Module]bool)
978 modulesInPackageByName := make(map[string]bool)
979
980 deps := f.gatherFilteredPackagingSpecs(ctx)
981 ctx.WalkDeps(func(child, parent android.Module) bool {
982 for _, ps := range android.OtherModuleProviderOrDefault(
983 ctx, child, android.InstallFilesProvider).PackagingSpecs {
984 if _, ok := deps[ps.RelPathInPackage()]; ok {
985 modulesInPackageByModule[child] = true
986 modulesInPackageByName[child.Name()] = true
987 return true
988 }
989 }
990 return true
991 })
992
993 provideModules := make([]android.Module, 0, len(modulesInPackageByModule))
994 for mod := range modulesInPackageByModule {
995 provideModules = append(provideModules, mod)
996 }
997
998 var requireModules []android.Module
999 ctx.WalkDeps(func(child, parent android.Module) bool {
1000 _, parentInPackage := modulesInPackageByModule[parent]
1001 _, childInPackageName := modulesInPackageByName[child.Name()]
1002
1003 // When parent is in the package, and child (or its variant) is not, this can be from an interface.
1004 if parentInPackage && !childInPackageName {
1005 requireModules = append(requireModules, child)
1006 }
1007 return true
1008 })
1009
1010 return provideModules, requireModules
1011}
Cole Faust26bdac52024-11-19 13:37:53 -08001012
1013// Checks that the given file doesn't exceed the given size, and will also print a warning
1014// if it's nearing the maximum size. Equivalent to assert-max-image-size in make:
1015// https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/definitions.mk;l=3455;drc=993c4de29a02a6accd60ceaaee153307e1a18d10
1016func assertMaxImageSize(builder *android.RuleBuilder, image android.Path, maxSize int64, addAvbLater bool) {
1017 if addAvbLater {
1018 // The value 69632 is derived from MAX_VBMETA_SIZE + MAX_FOOTER_SIZE in avbtool.
1019 // Logic copied from make:
1020 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=228;drc=a6a0007ef24e16c0b79f439beac4a118416717e6
1021 maxSize -= 69632
1022 }
1023 cmd := builder.Command()
1024 cmd.Textf(`file="%s"; maxsize="%d";`+
1025 `total=$(stat -c "%%s" "$file" | tr -d '\n');`+
1026 `if [ "$total" -gt "$maxsize" ]; then `+
1027 ` echo "error: $file too large ($total > $maxsize)";`+
1028 ` false;`+
1029 `elif [ "$total" -gt $((maxsize - 32768)) ]; then `+
1030 ` echo "WARNING: $file approaching size limit ($total now; limit $maxsize)";`+
1031 `fi`,
1032 image, maxSize)
1033 cmd.Implicit(image)
1034}
Spandan Das71be42d2024-11-20 18:34:16 +00001035
1036// addAutogeneratedRroDeps walks the transitive closure of vendor and product partitions.
1037// It visits apps installed in system and system_ext partitions, and adds the autogenerated
1038// RRO modules to its own deps.
1039func addAutogeneratedRroDeps(ctx android.BottomUpMutatorContext) {
1040 f, ok := ctx.Module().(*filesystem)
1041 if !ok {
1042 return
1043 }
1044 thisPartition := f.PartitionType()
1045 if thisPartition != "vendor" && thisPartition != "product" {
1046 return
1047 }
1048 ctx.WalkDeps(func(child, parent android.Module) bool {
1049 depTag := ctx.OtherModuleDependencyTag(child)
1050 if parent.Name() == f.Name() && depTag != interPartitionDependencyTag {
1051 return false // This is a module listed in deps of vendor/product filesystem
1052 }
1053 if vendorOverlay := java.AutogeneratedRroModuleName(ctx, child.Name(), "vendor"); ctx.OtherModuleExists(vendorOverlay) && thisPartition == "vendor" {
1054 ctx.AddFarVariationDependencies(nil, dependencyTagWithVisibilityEnforcementBypass, vendorOverlay)
1055 }
1056 if productOverlay := java.AutogeneratedRroModuleName(ctx, child.Name(), "product"); ctx.OtherModuleExists(productOverlay) && thisPartition == "product" {
1057 ctx.AddFarVariationDependencies(nil, dependencyTagWithVisibilityEnforcementBypass, productOverlay)
1058 }
1059 return true
1060 })
1061}