blob: 28cec27158cce3e53dff6679329e07bcbb7df59f [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 Das92631882024-10-28 22:49:38 +000028 "android/soong/linkerconfig"
Jiyong Park65b62242020-11-25 12:44:59 +090029
30 "github.com/google/blueprint"
Jiyong Park71baa762021-01-18 21:11:03 +090031 "github.com/google/blueprint/proptools"
Jiyong Park6f0f6882020-11-12 13:14:30 +090032)
33
34func init() {
Jooyung Han9706cbc2021-04-15 22:43:48 +090035 registerBuildComponents(android.InitRegistrationContext)
36}
37
38func registerBuildComponents(ctx android.RegistrationContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -070039 ctx.RegisterModuleType("android_filesystem", FilesystemFactory)
Jiyong Parkf46b1af2024-04-05 18:13:33 +090040 ctx.RegisterModuleType("android_filesystem_defaults", filesystemDefaultsFactory)
Jihoon Kang98047cf2024-10-02 17:13:54 +000041 ctx.RegisterModuleType("android_system_image", SystemImageFactory)
Jiyong Parkbc485482022-11-15 22:31:49 +090042 ctx.RegisterModuleType("avb_add_hash_footer", avbAddHashFooterFactory)
Inseob Kim87230e62023-11-22 18:55:07 +090043 ctx.RegisterModuleType("avb_add_hash_footer_defaults", avbAddHashFooterDefaultsFactory)
Alice Wang000e3a32023-01-03 16:11:20 +000044 ctx.RegisterModuleType("avb_gen_vbmeta_image", avbGenVbmetaImageFactory)
Inseob Kim87230e62023-11-22 18:55:07 +090045 ctx.RegisterModuleType("avb_gen_vbmeta_image_defaults", avbGenVbmetaImageDefaultsFactory)
Jiyong Park6f0f6882020-11-12 13:14:30 +090046}
47
48type filesystem struct {
49 android.ModuleBase
50 android.PackagingBase
Jiyong Parkf46b1af2024-04-05 18:13:33 +090051 android.DefaultableModuleBase
Jiyong Park65c49f52020-11-24 14:23:26 +090052
Jihoon Kang98047cf2024-10-02 17:13:54 +000053 properties FilesystemProperties
Jiyong Park71baa762021-01-18 21:11:03 +090054
Cole Faust4e9f5922024-11-13 16:09:23 -080055 output android.Path
Jiyong Park65c49f52020-11-24 14:23:26 +090056 installDir android.InstallPath
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090057
Cole Faust4e9f5922024-11-13 16:09:23 -080058 fileListFile android.Path
Kiyoung Kim99a954d2024-06-21 14:22:20 +090059
60 // Keeps the entries installed from this filesystem
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090061 entries []string
Kiyoung Kim67118212024-11-07 13:23:44 +090062
63 filesystemBuilder filesystemBuilder
Jiyong Park6f0f6882020-11-12 13:14:30 +090064}
65
Kiyoung Kim67118212024-11-07 13:23:44 +090066type filesystemBuilder interface {
67 BuildLinkerConfigFile(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath)
68 // Function that filters PackagingSpec in PackagingBase.GatherPackagingSpecs()
69 FilterPackagingSpec(spec android.PackagingSpec) bool
Inseob Kim3c0a0422024-11-05 17:21:37 +090070 // Function that modifies PackagingSpec in PackagingBase.GatherPackagingSpecs() to customize.
71 // For example, GSI system.img contains system_ext and product artifacts and their
72 // relPathInPackage need to be rebased to system/system_ext and system/system_product.
73 ModifyPackagingSpec(spec *android.PackagingSpec)
Kiyoung Kim67118212024-11-07 13:23:44 +090074}
75
76var _ filesystemBuilder = (*filesystem)(nil)
77
Spandan Das69464c32024-10-25 20:08:06 +000078type SymlinkDefinition struct {
Inseob Kim14199b02021-02-09 21:18:31 +090079 Target *string
80 Name *string
81}
82
Jihoon Kang98047cf2024-10-02 17:13:54 +000083type FilesystemProperties struct {
Jiyong Park71baa762021-01-18 21:11:03 +090084 // When set to true, sign the image with avbtool. Default is false.
85 Use_avb *bool
86
87 // Path to the private key that avbtool will use to sign this filesystem image.
88 // TODO(jiyong): allow apex_key to be specified here
89 Avb_private_key *string `android:"path"`
90
Shikha Panwar01403bb2022-12-22 12:22:57 +000091 // Signing algorithm for avbtool. Default is SHA256_RSA4096.
Jiyong Park71baa762021-01-18 21:11:03 +090092 Avb_algorithm *string
Jiyong Park11a65972021-02-01 21:09:38 +090093
Shikha Panwar01403bb2022-12-22 12:22:57 +000094 // Hash algorithm used for avbtool (for descriptors). This is passed as hash_algorithm to
95 // avbtool. Default used by avbtool is sha1.
Shikha Panware6f30632022-12-21 12:54:45 +000096 Avb_hash_algorithm *string
97
Nikita Ioffe2c8cdc62024-03-27 22:19:30 +000098 // The index used to prevent rollback of the image. Only used if use_avb is true.
99 Rollback_index *int64
100
Jiyong Parkac4076d2021-03-15 23:21:30 +0900101 // Name of the partition stored in vbmeta desc. Defaults to the name of this module.
102 Partition_name *string
103
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000104 // Type of the filesystem. Currently, ext4, erofs, cpio, and compressed_cpio are supported. Default
Jiyong Park837cdb22021-02-05 00:17:14 +0900105 // is ext4.
Jiyong Park11a65972021-02-01 21:09:38 +0900106 Type *string
Inseob Kimcc8e5362021-02-03 14:05:24 +0900107
Cole Faust9a24d902024-03-18 15:38:12 -0700108 // Identifies which partition this is for //visibility:any_system_image (and others) visibility
109 // checks, and will be used in the future for API surface checks.
110 Partition_type *string
111
Inseob Kimcc8e5362021-02-03 14:05:24 +0900112 // file_contexts file to make image. Currently, only ext4 is supported.
113 File_contexts *string `android:"path"`
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900114
115 // Base directory relative to root, to which deps are installed, e.g. "system". Default is "."
116 // (root).
117 Base_dir *string
Inseob Kim14199b02021-02-09 21:18:31 +0900118
119 // Directories to be created under root. e.g. /dev, /proc, etc.
Cole Faustd9c6a5b2024-05-21 14:54:00 -0700120 Dirs proptools.Configurable[[]string]
Inseob Kim14199b02021-02-09 21:18:31 +0900121
122 // Symbolic links to be created under root with "ln -sf <target> <name>".
Spandan Das69464c32024-10-25 20:08:06 +0000123 Symlinks []SymlinkDefinition
Jooyung Han65f402b2022-04-21 14:24:04 +0900124
125 // Seconds since unix epoch to override timestamps of file entries
126 Fake_timestamp *string
127
128 // When set, passed to mkuserimg_mke2fs --mke2fs_uuid & --mke2fs_hash_seed.
129 // Otherwise, they'll be set as random which might cause indeterministic build output.
130 Uuid *string
Inseob Kim376d72f2023-11-01 15:40:25 +0900131
132 // Mount point for this image. Default is "/"
133 Mount_point *string
Cole Faust4a2a7c92024-03-12 12:44:40 -0700134
135 // If set to the name of a partition ("system", "vendor", etc), this filesystem module
136 // will also include the contents of the make-built staging directories. If any soong
137 // modules would be installed to the same location as a make module, they will overwrite
138 // the make version.
139 Include_make_built_files string
Inseob Kim53391842024-03-29 17:44:07 +0900140
Inseob Kimb7b84572024-04-30 10:51:47 +0900141 // When set, builds etc/event-log-tags file by merging logtags from all dependencies.
142 // Default is false
143 Build_logtags *bool
144
Justin Yun74f3f302024-05-07 14:32:14 +0900145 // Install aconfig_flags.pb file for the modules installed in this partition.
146 Gen_aconfig_flags_pb *bool
147
Inseob Kim53391842024-03-29 17:44:07 +0900148 Fsverity fsverityProperties
Cole Faust92ccbe22024-10-03 14:38:37 -0700149
150 // If this property is set to true, the filesystem will call ctx.UncheckedModule(), causing
151 // it to not be built on checkbuilds. Used for the automatic migration from make to soong
152 // build modules, where we want to emit some not-yet-working filesystems and we don't want them
153 // to be built.
154 Unchecked_module *bool `blueprint:"mutated"`
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000155
156 Erofs ErofsProperties
Jihoon Kang0d545b82024-10-11 00:21:57 +0000157
mrziwang1a6291f2024-11-07 14:29:25 -0800158 F2fs F2fsProperties
159
Spandan Das2047a4c2024-11-11 21:24:58 +0000160 Linker_config LinkerConfigProperties
Spandan Das92631882024-10-28 22:49:38 +0000161
Jihoon Kang0d545b82024-10-11 00:21:57 +0000162 // Determines if the module is auto-generated from Soong or not. If the module is
163 // auto-generated, its deps are exempted from visibility enforcement.
164 Is_auto_generated *bool
Jihoon Kang6c03c8e2024-11-18 21:30:22 +0000165
166 // Path to the dev nodes description file. This is only needed for building the ramdisk
167 // partition and should not be explicitly specified.
168 Dev_nodes_description_file *string `android:"path" blueprint:"mutated"`
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000169}
170
171// Additional properties required to generate erofs FS partitions.
172type ErofsProperties struct {
173 // Compressor and Compression level passed to mkfs.erofs. e.g. (lz4hc,9)
174 // Please see external/erofs-utils/README for complete documentation.
175 Compressor *string
176
177 // Used as --compress-hints for mkfs.erofs
178 Compress_hints *string `android:"path"`
179
180 Sparse *bool
Jiyong Park71baa762021-01-18 21:11:03 +0900181}
182
mrziwang1a6291f2024-11-07 14:29:25 -0800183// Additional properties required to generate f2fs FS partitions.
184type F2fsProperties struct {
185 Sparse *bool
186}
187
Spandan Das173256b2024-10-31 19:59:30 +0000188type LinkerConfigProperties struct {
189
190 // Build a linker.config.pb file
191 Gen_linker_config *bool
192
193 // List of files (in .json format) that will be converted to a linker config file (in .pb format).
194 // The linker config file be installed in the filesystem at /etc/linker.config.pb
195 Linker_config_srcs []string `android:"path"`
196}
197
Jiyong Park65c49f52020-11-24 14:23:26 +0900198// android_filesystem packages a set of modules and their transitive dependencies into a filesystem
199// image. The filesystem images are expected to be mounted in the target device, which means the
200// modules in the filesystem image are built for the target device (i.e. Android, not Linux host).
201// The modules are placed in the filesystem image just like they are installed to the ordinary
202// partitions like system.img. For example, cc_library modules are placed under ./lib[64] directory.
Cole Faust92ccbe22024-10-03 14:38:37 -0700203func FilesystemFactory() android.Module {
Jiyong Park6f0f6882020-11-12 13:14:30 +0900204 module := &filesystem{}
Kiyoung Kim67118212024-11-07 13:23:44 +0900205 module.filesystemBuilder = module
Cole Faust2cfe6962024-09-17 11:31:14 -0700206 initFilesystemModule(module, module)
Jiyong Parkfa616132021-04-20 11:36:40 +0900207 return module
208}
209
Cole Faust2cfe6962024-09-17 11:31:14 -0700210func initFilesystemModule(module android.DefaultableModule, filesystemModule *filesystem) {
211 module.AddProperties(&filesystemModule.properties)
212 android.InitPackageModule(filesystemModule)
213 filesystemModule.PackagingBase.DepsCollectFirstTargetOnly = true
Jihoon Kang79196c52024-10-30 18:49:47 +0000214 filesystemModule.PackagingBase.AllowHighPriorityDeps = true
Jiyong Park6f0f6882020-11-12 13:14:30 +0900215 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900216 android.InitDefaultableModule(module)
Jihoon Kang6c03c8e2024-11-18 21:30:22 +0000217
218 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
219 filesystemModule.setDevNodesDescriptionProp()
220 })
Jiyong Park6f0f6882020-11-12 13:14:30 +0900221}
222
Jihoon Kang0d545b82024-10-11 00:21:57 +0000223type depTag struct {
Jiyong Park12a719c2021-01-07 15:31:24 +0900224 blueprint.BaseDependencyTag
Jooyung Han092ef812021-03-10 15:40:34 +0900225 android.PackagingItemAlwaysDepTag
Jihoon Kang0d545b82024-10-11 00:21:57 +0000226}
227
228var dependencyTag = depTag{}
229
230type depTagWithVisibilityEnforcementBypass struct {
231 depTag
232}
233
234var _ android.ExcludeFromVisibilityEnforcementTag = (*depTagWithVisibilityEnforcementBypass)(nil)
235
236func (t depTagWithVisibilityEnforcementBypass) ExcludeFromVisibilityEnforcement() {}
237
238var dependencyTagWithVisibilityEnforcementBypass = depTagWithVisibilityEnforcementBypass{}
Jiyong Park65b62242020-11-25 12:44:59 +0900239
Jihoon Kang6c03c8e2024-11-18 21:30:22 +0000240// ramdiskDevNodesDescription is the name of the filegroup module that provides the file that
241// contains the description of dev nodes added to the CPIO archive for the ramdisk partition.
242const ramdiskDevNodesDescription = "ramdisk_node_list"
243
244func (f *filesystem) setDevNodesDescriptionProp() {
245 if proptools.String(f.properties.Partition_name) == "ramdisk" {
246 f.properties.Dev_nodes_description_file = proptools.StringPtr(":" + ramdiskDevNodesDescription)
247 }
248}
249
Jiyong Park6f0f6882020-11-12 13:14:30 +0900250func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000251 if proptools.Bool(f.properties.Is_auto_generated) {
252 f.AddDeps(ctx, dependencyTagWithVisibilityEnforcementBypass)
253 } else {
254 f.AddDeps(ctx, dependencyTag)
255 }
Jiyong Park6f0f6882020-11-12 13:14:30 +0900256}
257
Jiyong Park11a65972021-02-01 21:09:38 +0900258type fsType int
259
260const (
261 ext4Type fsType = iota
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000262 erofsType
mrziwang1a6291f2024-11-07 14:29:25 -0800263 f2fsType
Jiyong Park11a65972021-02-01 21:09:38 +0900264 compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900265 cpioType // uncompressed
Jiyong Park11a65972021-02-01 21:09:38 +0900266 unknown
267)
268
Spandan Das7a46f6c2024-10-14 18:41:18 +0000269func (fs fsType) IsUnknown() bool {
270 return fs == unknown
271}
272
Cole Faust92ccbe22024-10-03 14:38:37 -0700273type FilesystemInfo struct {
274 // A text file containing the list of paths installed on the partition.
275 FileListFile android.Path
276}
277
278var FilesystemProvider = blueprint.NewProvider[FilesystemInfo]()
279
Spandan Das7a46f6c2024-10-14 18:41:18 +0000280func GetFsTypeFromString(ctx android.EarlyModuleContext, typeStr string) fsType {
Jiyong Park11a65972021-02-01 21:09:38 +0900281 switch typeStr {
282 case "ext4":
283 return ext4Type
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000284 case "erofs":
285 return erofsType
mrziwang1a6291f2024-11-07 14:29:25 -0800286 case "f2fs":
287 return f2fsType
Jiyong Park11a65972021-02-01 21:09:38 +0900288 case "compressed_cpio":
289 return compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900290 case "cpio":
291 return cpioType
Jiyong Park11a65972021-02-01 21:09:38 +0900292 default:
Jiyong Park11a65972021-02-01 21:09:38 +0900293 return unknown
294 }
295}
296
Spandan Das7a46f6c2024-10-14 18:41:18 +0000297func (f *filesystem) fsType(ctx android.ModuleContext) fsType {
298 typeStr := proptools.StringDefault(f.properties.Type, "ext4")
299 fsType := GetFsTypeFromString(ctx, typeStr)
300 if fsType == unknown {
301 ctx.PropertyErrorf("type", "%q not supported", typeStr)
302 }
303 return fsType
304}
305
Jiyong Park65c49f52020-11-24 14:23:26 +0900306func (f *filesystem) installFileName() string {
307 return f.BaseModuleName() + ".img"
308}
309
Inseob Kim53391842024-03-29 17:44:07 +0900310func (f *filesystem) partitionName() string {
311 return proptools.StringDefault(f.properties.Partition_name, f.Name())
312}
313
Kiyoung Kim67118212024-11-07 13:23:44 +0900314func (f *filesystem) FilterPackagingSpec(ps android.PackagingSpec) bool {
Jiyong Park7e7d4af2024-05-01 12:36:10 +0000315 // Filesystem module respects the installation semantic. A PackagingSpec from a module with
316 // IsSkipInstall() is skipped.
Cole Faust76a6e952024-11-07 16:56:45 -0800317 if ps.SkipInstall() {
318 return false
Spandan Das6d056502024-10-21 15:40:32 +0000319 }
Cole Faust76a6e952024-11-07 16:56:45 -0800320 if proptools.Bool(f.properties.Is_auto_generated) { // TODO (spandandas): Remove this.
321 pt := f.PartitionType()
Cole Faustc88cff12024-11-12 13:24:05 -0800322 return ps.Partition() == pt || strings.HasPrefix(ps.Partition(), pt+"/")
Cole Faust76a6e952024-11-07 16:56:45 -0800323 }
324 return true
Jiyong Park7e7d4af2024-05-01 12:36:10 +0000325}
326
Inseob Kim3c0a0422024-11-05 17:21:37 +0900327func (f *filesystem) ModifyPackagingSpec(ps *android.PackagingSpec) {
Cole Faustc88cff12024-11-12 13:24:05 -0800328 // Sometimes, android.modulePartition() returns a path with >1 path components.
329 // This makes the partition field of packagingSpecs have multiple components, like
330 // "system/product". Right now, the filesystem module doesn't look at the partition field
331 // when deciding what path to install the file under, only the RelPathInPackage field, so
332 // we move the later path components from partition to relPathInPackage. This should probably
333 // be revisited in the future.
334 prefix := f.PartitionType() + "/"
335 if strings.HasPrefix(ps.Partition(), prefix) {
336 subPartition := strings.TrimPrefix(ps.Partition(), prefix)
337 ps.SetPartition(f.PartitionType())
338 ps.SetRelPathInPackage(filepath.Join(subPartition, ps.RelPathInPackage()))
339 }
Inseob Kim3c0a0422024-11-05 17:21:37 +0900340}
341
Jiyong Park6f0f6882020-11-12 13:14:30 +0900342var pctx = android.NewPackageContext("android/soong/filesystem")
343
344func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900345 validatePartitionType(ctx, f)
Jiyong Park11a65972021-02-01 21:09:38 +0900346 switch f.fsType(ctx) {
mrziwang1a6291f2024-11-07 14:29:25 -0800347 case ext4Type, erofsType, f2fsType:
Jiyong Park11a65972021-02-01 21:09:38 +0900348 f.output = f.buildImageUsingBuildImage(ctx)
349 case compressedCpioType:
Jiyong Park837cdb22021-02-05 00:17:14 +0900350 f.output = f.buildCpioImage(ctx, true)
351 case cpioType:
352 f.output = f.buildCpioImage(ctx, false)
Jiyong Park11a65972021-02-01 21:09:38 +0900353 default:
354 return
355 }
356
357 f.installDir = android.PathForModuleInstall(ctx, "etc")
358 ctx.InstallFile(f.installDir, f.installFileName(), f.output)
mrziwang555d1332024-06-07 11:15:33 -0700359 ctx.SetOutputFiles([]android.Path{f.output}, "")
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900360
Cole Faust4e9f5922024-11-13 16:09:23 -0800361 fileListFile := android.PathForModuleOut(ctx, "fileList")
362 android.WriteFileRule(ctx, fileListFile, f.installedFilesList())
Cole Faust92ccbe22024-10-03 14:38:37 -0700363
364 android.SetProvider(ctx, FilesystemProvider, FilesystemInfo{
Cole Faust4e9f5922024-11-13 16:09:23 -0800365 FileListFile: fileListFile,
Cole Faust92ccbe22024-10-03 14:38:37 -0700366 })
Cole Faust4e9f5922024-11-13 16:09:23 -0800367 f.fileListFile = fileListFile
Cole Faust92ccbe22024-10-03 14:38:37 -0700368
369 if proptools.Bool(f.properties.Unchecked_module) {
370 ctx.UncheckedModule()
371 }
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900372}
373
Cole Faust4e9f5922024-11-13 16:09:23 -0800374func (f *filesystem) appendToEntry(ctx android.ModuleContext, installedFile android.Path) {
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900375 partitionBaseDir := android.PathForModuleOut(ctx, "root", f.partitionName()).String() + "/"
376
377 relPath, inTargetPartition := strings.CutPrefix(installedFile.String(), partitionBaseDir)
378 if inTargetPartition {
379 f.entries = append(f.entries, relPath)
380 }
381}
382
383func (f *filesystem) installedFilesList() string {
384 installedFilePaths := android.FirstUniqueStrings(f.entries)
385 slices.Sort(installedFilePaths)
386
387 return strings.Join(installedFilePaths, "\n")
Jiyong Park11a65972021-02-01 21:09:38 +0900388}
389
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900390func validatePartitionType(ctx android.ModuleContext, p partition) {
391 if !android.InList(p.PartitionType(), validPartitions) {
392 ctx.PropertyErrorf("partition_type", "partition_type must be one of %s, found: %s", validPartitions, p.PartitionType())
393 }
394
395 ctx.VisitDirectDepsWithTag(android.DefaultsDepTag, func(m android.Module) {
396 if fdm, ok := m.(*filesystemDefaults); ok {
397 if p.PartitionType() != fdm.PartitionType() {
398 ctx.PropertyErrorf("partition_type",
399 "%s doesn't match with the partition type %s of the filesystem default module %s",
400 p.PartitionType(), fdm.PartitionType(), m.Name())
401 }
402 }
403 })
404}
405
Cole Faust3b806d32024-03-11 15:15:03 -0700406// Copy extra files/dirs that are not from the `deps` property to `rootDir`, checking for conflicts with files
407// already in `rootDir`.
408func (f *filesystem) buildNonDepsFiles(ctx android.ModuleContext, builder *android.RuleBuilder, rootDir android.OutputPath) {
Inseob Kim14199b02021-02-09 21:18:31 +0900409 // create dirs and symlinks
Cole Faustd9c6a5b2024-05-21 14:54:00 -0700410 for _, dir := range f.properties.Dirs.GetOrDefault(ctx, nil) {
Inseob Kim14199b02021-02-09 21:18:31 +0900411 // OutputPath.Join verifies dir
412 builder.Command().Text("mkdir -p").Text(rootDir.Join(ctx, dir).String())
413 }
414
415 for _, symlink := range f.properties.Symlinks {
416 name := strings.TrimSpace(proptools.String(symlink.Name))
417 target := strings.TrimSpace(proptools.String(symlink.Target))
418
419 if name == "" {
420 ctx.PropertyErrorf("symlinks", "Name can't be empty")
421 continue
422 }
423
424 if target == "" {
425 ctx.PropertyErrorf("symlinks", "Target can't be empty")
426 continue
427 }
428
429 // OutputPath.Join verifies name. don't need to verify target.
430 dst := rootDir.Join(ctx, name)
Cole Faust3b806d32024-03-11 15:15:03 -0700431 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 +0900432 builder.Command().Text("mkdir -p").Text(filepath.Dir(dst.String()))
433 builder.Command().Text("ln -sf").Text(proptools.ShellEscape(target)).Text(dst.String())
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900434 f.appendToEntry(ctx, dst)
Inseob Kim14199b02021-02-09 21:18:31 +0900435 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900436}
437
Inseob Kim33f95a92024-07-11 15:44:49 +0900438func (f *filesystem) copyPackagingSpecs(ctx android.ModuleContext, builder *android.RuleBuilder, specs map[string]android.PackagingSpec, rootDir, rebasedDir android.WritablePath) []string {
439 rootDirSpecs := make(map[string]android.PackagingSpec)
440 rebasedDirSpecs := make(map[string]android.PackagingSpec)
441
442 for rel, spec := range specs {
443 if spec.Partition() == "root" {
444 rootDirSpecs[rel] = spec
445 } else {
446 rebasedDirSpecs[rel] = spec
447 }
448 }
449
450 dirsToSpecs := make(map[android.WritablePath]map[string]android.PackagingSpec)
451 dirsToSpecs[rootDir] = rootDirSpecs
452 dirsToSpecs[rebasedDir] = rebasedDirSpecs
453
454 return f.CopySpecsToDirs(ctx, builder, dirsToSpecs)
455}
456
Justin Yun34baa2e2024-08-30 21:11:33 +0900457func (f *filesystem) copyFilesToProductOut(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath) {
Justin Yun2cc42502024-09-11 14:10:20 +0900458 if f.Name() != ctx.Config().SoongDefinedSystemImage() {
Justin Yun34baa2e2024-08-30 21:11:33 +0900459 return
460 }
461 installPath := android.PathForModuleInPartitionInstall(ctx, f.partitionName())
462 builder.Command().Textf("cp -prf %s/* %s", rebasedDir, installPath)
463}
464
Cole Faust4e9f5922024-11-13 16:09:23 -0800465func (f *filesystem) buildImageUsingBuildImage(ctx android.ModuleContext) android.Path {
Jiyong Park6f0f6882020-11-12 13:14:30 +0900466 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Cole Faust3b806d32024-03-11 15:15:03 -0700467 rebasedDir := rootDir
468 if f.properties.Base_dir != nil {
469 rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir)
470 }
471 builder := android.NewRuleBuilder(pctx, ctx)
472 // Wipe the root dir to get rid of leftover files from prior builds
473 builder.Command().Textf("rm -rf %s && mkdir -p %s", rootDir, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900474 specs := f.gatherFilteredPackagingSpecs(ctx)
Inseob Kim33f95a92024-07-11 15:44:49 +0900475 f.entries = f.copyPackagingSpecs(ctx, builder, specs, rootDir, rebasedDir)
Cole Faust3b806d32024-03-11 15:15:03 -0700476
477 f.buildNonDepsFiles(ctx, builder, rootDir)
Cole Faust4a2a7c92024-03-12 12:44:40 -0700478 f.addMakeBuiltFiles(ctx, builder, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900479 f.buildFsverityMetadataFiles(ctx, builder, specs, rootDir, rebasedDir)
Inseob Kimb7b84572024-04-30 10:51:47 +0900480 f.buildEventLogtagsFile(ctx, builder, rebasedDir)
Justin Yun74f3f302024-05-07 14:32:14 +0900481 f.buildAconfigFlagsFiles(ctx, builder, specs, rebasedDir)
Kiyoung Kim67118212024-11-07 13:23:44 +0900482 f.filesystemBuilder.BuildLinkerConfigFile(ctx, builder, rebasedDir)
Justin Yun34baa2e2024-08-30 21:11:33 +0900483 f.copyFilesToProductOut(ctx, builder, rebasedDir)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900484
Nikita Ioffe519015f2022-12-23 15:36:29 +0000485 // run host_init_verifier
486 // Ideally we should have a concept of pluggable linters that verify the generated image.
487 // While such concept is not implement this will do.
488 // TODO(b/263574231): substitute with pluggable linter.
489 builder.Command().
490 BuiltTool("host_init_verifier").
491 FlagWithArg("--out_system=", rootDir.String()+"/system")
492
Jiyong Park72678312021-01-18 17:29:49 +0900493 propFile, toolDeps := f.buildPropFile(ctx)
Cole Faust4e9f5922024-11-13 16:09:23 -0800494 output := android.PathForModuleOut(ctx, f.installFileName())
Colin Crossf1a035e2020-11-16 17:32:30 -0800495 builder.Command().BuiltTool("build_image").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900496 Text(rootDir.String()). // input directory
497 Input(propFile).
Jiyong Park72678312021-01-18 17:29:49 +0900498 Implicits(toolDeps).
Jiyong Park11a65972021-02-01 21:09:38 +0900499 Output(output).
Jiyong Park6f0f6882020-11-12 13:14:30 +0900500 Text(rootDir.String()) // directory where to find fs_config_files|dirs
501
502 // rootDir is not deleted. Might be useful for quick inspection.
Colin Crossf1a035e2020-11-16 17:32:30 -0800503 builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park65c49f52020-11-24 14:23:26 +0900504
Jiyong Park11a65972021-02-01 21:09:38 +0900505 return output
Jiyong Park65c49f52020-11-24 14:23:26 +0900506}
507
Cole Faust4e9f5922024-11-13 16:09:23 -0800508func (f *filesystem) buildFileContexts(ctx android.ModuleContext) android.Path {
Inseob Kimcc8e5362021-02-03 14:05:24 +0900509 builder := android.NewRuleBuilder(pctx, ctx)
510 fcBin := android.PathForModuleOut(ctx, "file_contexts.bin")
511 builder.Command().BuiltTool("sefcontext_compile").
512 FlagWithOutput("-o ", fcBin).
513 Input(android.PathForModuleSrc(ctx, proptools.String(f.properties.File_contexts)))
514 builder.Build("build_filesystem_file_contexts", fmt.Sprintf("Creating filesystem file contexts for %s", f.BaseModuleName()))
Cole Faust4e9f5922024-11-13 16:09:23 -0800515 return fcBin
Inseob Kimcc8e5362021-02-03 14:05:24 +0900516}
517
Jooyung Han65f402b2022-04-21 14:24:04 +0900518// Calculates avb_salt from entry list (sorted) for deterministic output.
519func (f *filesystem) salt() string {
520 return sha1sum(f.entries)
521}
522
Cole Faust4e9f5922024-11-13 16:09:23 -0800523func (f *filesystem) buildPropFile(ctx android.ModuleContext) (android.Path, android.Paths) {
Jiyong Park72678312021-01-18 17:29:49 +0900524 var deps android.Paths
Cole Faustcec230a2024-03-07 15:51:12 -0800525 var propFileString strings.Builder
Jiyong Park72678312021-01-18 17:29:49 +0900526 addStr := func(name string, value string) {
Cole Faustcec230a2024-03-07 15:51:12 -0800527 propFileString.WriteString(name)
528 propFileString.WriteRune('=')
529 propFileString.WriteString(value)
530 propFileString.WriteRune('\n')
Jiyong Park72678312021-01-18 17:29:49 +0900531 }
532 addPath := func(name string, path android.Path) {
Cole Faustcec230a2024-03-07 15:51:12 -0800533 addStr(name, path.String())
Jiyong Park72678312021-01-18 17:29:49 +0900534 deps = append(deps, path)
535 }
536
Jiyong Park11a65972021-02-01 21:09:38 +0900537 // Type string that build_image.py accepts.
538 fsTypeStr := func(t fsType) string {
539 switch t {
Spandan Das94668822024-10-09 20:51:33 +0000540 // TODO(372522486): add more types like f2fs, erofs, etc.
Jiyong Park11a65972021-02-01 21:09:38 +0900541 case ext4Type:
542 return "ext4"
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000543 case erofsType:
544 return "erofs"
mrziwang1a6291f2024-11-07 14:29:25 -0800545 case f2fsType:
546 return "f2fs"
Jiyong Park11a65972021-02-01 21:09:38 +0900547 }
548 panic(fmt.Errorf("unsupported fs type %v", t))
549 }
550
551 addStr("fs_type", fsTypeStr(f.fsType(ctx)))
Inseob Kim376d72f2023-11-01 15:40:25 +0900552 addStr("mount_point", proptools.StringDefault(f.properties.Mount_point, "/"))
Jiyong Park72678312021-01-18 17:29:49 +0900553 addStr("use_dynamic_partition_size", "true")
554 addPath("ext_mkuserimg", ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs"))
555 // b/177813163 deps of the host tools have to be added. Remove this.
556 for _, t := range []string{"mke2fs", "e2fsdroid", "tune2fs"} {
557 deps = append(deps, ctx.Config().HostToolPath(ctx, t))
558 }
559
Jiyong Park71baa762021-01-18 21:11:03 +0900560 if proptools.Bool(f.properties.Use_avb) {
561 addStr("avb_hashtree_enable", "true")
562 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
563 algorithm := proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096")
564 addStr("avb_algorithm", algorithm)
565 key := android.PathForModuleSrc(ctx, proptools.String(f.properties.Avb_private_key))
566 addPath("avb_key_path", key)
Inseob Kim53391842024-03-29 17:44:07 +0900567 addStr("partition_name", f.partitionName())
Shikha Panware6f30632022-12-21 12:54:45 +0000568 avb_add_hashtree_footer_args := "--do_not_generate_fec"
569 if hashAlgorithm := proptools.String(f.properties.Avb_hash_algorithm); hashAlgorithm != "" {
570 avb_add_hashtree_footer_args += " --hash_algorithm " + hashAlgorithm
571 }
Nikita Ioffe2c8cdc62024-03-27 22:19:30 +0000572 if f.properties.Rollback_index != nil {
573 rollbackIndex := proptools.Int(f.properties.Rollback_index)
574 if rollbackIndex < 0 {
575 ctx.PropertyErrorf("rollback_index", "Rollback index must be non-negative")
576 }
577 avb_add_hashtree_footer_args += " --rollback_index " + strconv.Itoa(rollbackIndex)
578 }
Inseob Kim53391842024-03-29 17:44:07 +0900579 securityPatchKey := "com.android.build." + f.partitionName() + ".security_patch"
Seungjae Yooa30e4502023-11-09 14:55:44 +0900580 securityPatchValue := ctx.Config().PlatformSecurityPatch()
581 avb_add_hashtree_footer_args += " --prop " + securityPatchKey + ":" + securityPatchValue
Shikha Panware6f30632022-12-21 12:54:45 +0000582 addStr("avb_add_hashtree_footer_args", avb_add_hashtree_footer_args)
Jooyung Han65f402b2022-04-21 14:24:04 +0900583 addStr("avb_salt", f.salt())
Jiyong Park71baa762021-01-18 21:11:03 +0900584 }
585
Inseob Kimcc8e5362021-02-03 14:05:24 +0900586 if proptools.String(f.properties.File_contexts) != "" {
587 addPath("selinux_fc", f.buildFileContexts(ctx))
588 }
Jooyung Han65f402b2022-04-21 14:24:04 +0900589 if timestamp := proptools.String(f.properties.Fake_timestamp); timestamp != "" {
590 addStr("timestamp", timestamp)
591 }
592 if uuid := proptools.String(f.properties.Uuid); uuid != "" {
593 addStr("uuid", uuid)
594 addStr("hash_seed", uuid)
595 }
mrziwang1a6291f2024-11-07 14:29:25 -0800596
597 fst := f.fsType(ctx)
598 switch fst {
599 case erofsType:
600 // Add erofs properties
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000601 if compressor := f.properties.Erofs.Compressor; compressor != nil {
602 addStr("erofs_default_compressor", proptools.String(compressor))
603 }
604 if compressHints := f.properties.Erofs.Compress_hints; compressHints != nil {
Cole Faust7cd4bad2024-10-11 10:31:12 -0700605 addPath("erofs_default_compress_hints", android.PathForModuleSrc(ctx, *compressHints))
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000606 }
607 if proptools.BoolDefault(f.properties.Erofs.Sparse, true) {
608 // https://source.corp.google.com/h/googleplex-android/platform/build/+/88b1c67239ca545b11580237242774b411f2fed9:core/Makefile;l=2292;bpv=1;bpt=0;drc=ea8f34bc1d6e63656b4ec32f2391e9d54b3ebb6b
609 addStr("erofs_sparse_flag", "-s")
610 }
mrziwang1a6291f2024-11-07 14:29:25 -0800611 case f2fsType:
612 if proptools.BoolDefault(f.properties.F2fs.Sparse, true) {
613 // https://source.corp.google.com/h/googleplex-android/platform/build/+/88b1c67239ca545b11580237242774b411f2fed9:core/Makefile;l=2294;drc=ea8f34bc1d6e63656b4ec32f2391e9d54b3ebb6b;bpv=1;bpt=0
614 addStr("f2fs_sparse_flag", "-S")
615 }
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000616 }
mrziwang1a6291f2024-11-07 14:29:25 -0800617 f.checkFsTypePropertyError(ctx, fst, fsTypeStr(fst))
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000618
Cole Faust4e9f5922024-11-13 16:09:23 -0800619 propFile := android.PathForModuleOut(ctx, "prop")
Cole Faustcec230a2024-03-07 15:51:12 -0800620 android.WriteFileRuleVerbatim(ctx, propFile, propFileString.String())
Jiyong Park72678312021-01-18 17:29:49 +0900621 return propFile, deps
622}
623
mrziwang1a6291f2024-11-07 14:29:25 -0800624// This method checks if there is any property set for the fstype(s) other than
625// the current fstype.
626func (f *filesystem) checkFsTypePropertyError(ctx android.ModuleContext, t fsType, fs string) {
627 raiseError := func(otherFsType, currentFsType string) {
628 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)
629 ctx.PropertyErrorf(otherFsType, errMsg)
630 }
631
632 if t != erofsType {
633 if f.properties.Erofs.Compressor != nil || f.properties.Erofs.Compress_hints != nil || f.properties.Erofs.Sparse != nil {
634 raiseError("erofs", fs)
635 }
636 }
637 if t != f2fsType {
638 if f.properties.F2fs.Sparse != nil {
639 raiseError("f2fs", fs)
640 }
641 }
642}
643
Cole Faust4e9f5922024-11-13 16:09:23 -0800644func (f *filesystem) buildCpioImage(ctx android.ModuleContext, compressed bool) android.Path {
Jiyong Park11a65972021-02-01 21:09:38 +0900645 if proptools.Bool(f.properties.Use_avb) {
646 ctx.PropertyErrorf("use_avb", "signing compresed cpio image using avbtool is not supported."+
647 "Consider adding this to bootimg module and signing the entire boot image.")
648 }
649
Inseob Kimcc8e5362021-02-03 14:05:24 +0900650 if proptools.String(f.properties.File_contexts) != "" {
651 ctx.PropertyErrorf("file_contexts", "file_contexts is not supported for compressed cpio image.")
652 }
653
Cole Faust4a2a7c92024-03-12 12:44:40 -0700654 if f.properties.Include_make_built_files != "" {
655 ctx.PropertyErrorf("include_make_built_files", "include_make_built_files is not supported for compressed cpio image.")
656 }
657
Jiyong Park11a65972021-02-01 21:09:38 +0900658 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Cole Faust3b806d32024-03-11 15:15:03 -0700659 rebasedDir := rootDir
660 if f.properties.Base_dir != nil {
661 rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir)
662 }
663 builder := android.NewRuleBuilder(pctx, ctx)
664 // Wipe the root dir to get rid of leftover files from prior builds
665 builder.Command().Textf("rm -rf %s && mkdir -p %s", rootDir, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900666 specs := f.gatherFilteredPackagingSpecs(ctx)
Inseob Kim33f95a92024-07-11 15:44:49 +0900667 f.entries = f.copyPackagingSpecs(ctx, builder, specs, rootDir, rebasedDir)
Cole Faust3b806d32024-03-11 15:15:03 -0700668
669 f.buildNonDepsFiles(ctx, builder, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900670 f.buildFsverityMetadataFiles(ctx, builder, specs, rootDir, rebasedDir)
Inseob Kimb7b84572024-04-30 10:51:47 +0900671 f.buildEventLogtagsFile(ctx, builder, rebasedDir)
Justin Yun74f3f302024-05-07 14:32:14 +0900672 f.buildAconfigFlagsFiles(ctx, builder, specs, rebasedDir)
Kiyoung Kim67118212024-11-07 13:23:44 +0900673 f.filesystemBuilder.BuildLinkerConfigFile(ctx, builder, rebasedDir)
Justin Yun34baa2e2024-08-30 21:11:33 +0900674 f.copyFilesToProductOut(ctx, builder, rebasedDir)
Jiyong Park11a65972021-02-01 21:09:38 +0900675
Cole Faust4e9f5922024-11-13 16:09:23 -0800676 output := android.PathForModuleOut(ctx, f.installFileName())
Jiyong Park837cdb22021-02-05 00:17:14 +0900677 cmd := builder.Command().
Jiyong Park11a65972021-02-01 21:09:38 +0900678 BuiltTool("mkbootfs").
Jiyong Park837cdb22021-02-05 00:17:14 +0900679 Text(rootDir.String()) // input directory
Jihoon Kang6c03c8e2024-11-18 21:30:22 +0000680 if nodeList := f.properties.Dev_nodes_description_file; nodeList != nil {
681 cmd.FlagWithInput("-n ", android.PathForModuleSrc(ctx, proptools.String(nodeList)))
682 }
Jiyong Park837cdb22021-02-05 00:17:14 +0900683 if compressed {
684 cmd.Text("|").
685 BuiltTool("lz4").
686 Flag("--favor-decSpeed"). // for faster boot
687 Flag("-12"). // maximum compression level
688 Flag("-l"). // legacy format for kernel
689 Text(">").Output(output)
690 } else {
691 cmd.Text(">").Output(output)
692 }
Jiyong Park11a65972021-02-01 21:09:38 +0900693
694 // rootDir is not deleted. Might be useful for quick inspection.
Jiyong Park837cdb22021-02-05 00:17:14 +0900695 builder.Build("build_cpio_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park11a65972021-02-01 21:09:38 +0900696
697 return output
698}
699
Cole Faust4a2a7c92024-03-12 12:44:40 -0700700var validPartitions = []string{
701 "system",
702 "userdata",
703 "cache",
704 "system_other",
705 "vendor",
706 "product",
707 "system_ext",
708 "odm",
709 "vendor_dlkm",
710 "odm_dlkm",
711 "system_dlkm",
Cole Faust76a6e952024-11-07 16:56:45 -0800712 "ramdisk",
Cole Faust24938e22024-11-18 14:01:58 -0800713 "vendor_ramdisk",
Cole Faust4a2a7c92024-03-12 12:44:40 -0700714}
715
716func (f *filesystem) addMakeBuiltFiles(ctx android.ModuleContext, builder *android.RuleBuilder, rootDir android.Path) {
717 partition := f.properties.Include_make_built_files
718 if partition == "" {
719 return
720 }
721 if !slices.Contains(validPartitions, partition) {
722 ctx.PropertyErrorf("include_make_built_files", "Expected one of %#v, found %q", validPartitions, partition)
723 return
724 }
725 stampFile := fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/staging_dir.stamp", ctx.Config().DeviceName(), partition)
726 fileListFile := fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partition)
727 stagingDir := fmt.Sprintf("target/product/%s/%s", ctx.Config().DeviceName(), partition)
728
729 builder.Command().BuiltTool("merge_directories").
730 Implicit(android.PathForArbitraryOutput(ctx, stampFile)).
731 Text("--ignore-duplicates").
732 FlagWithInput("--file-list", android.PathForArbitraryOutput(ctx, fileListFile)).
733 Text(rootDir.String()).
734 Text(android.PathForArbitraryOutput(ctx, stagingDir).String())
735}
736
Inseob Kimb7b84572024-04-30 10:51:47 +0900737func (f *filesystem) buildEventLogtagsFile(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath) {
738 if !proptools.Bool(f.properties.Build_logtags) {
739 return
740 }
741
742 logtagsFilePaths := make(map[string]bool)
743 ctx.WalkDeps(func(child, parent android.Module) bool {
744 if logtagsInfo, ok := android.OtherModuleProvider(ctx, child, android.LogtagsProviderKey); ok {
745 for _, path := range logtagsInfo.Logtags {
746 logtagsFilePaths[path.String()] = true
747 }
748 }
749 return true
750 })
751
752 if len(logtagsFilePaths) == 0 {
753 return
754 }
755
756 etcPath := rebasedDir.Join(ctx, "etc")
757 eventLogtagsPath := etcPath.Join(ctx, "event-log-tags")
758 builder.Command().Text("mkdir").Flag("-p").Text(etcPath.String())
759 cmd := builder.Command().BuiltTool("merge-event-log-tags").
760 FlagWithArg("-o ", eventLogtagsPath.String()).
761 FlagWithInput("-m ", android.MergedLogtagsPath(ctx))
762
763 for _, path := range android.SortedKeys(logtagsFilePaths) {
764 cmd.Text(path)
765 }
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900766
767 f.appendToEntry(ctx, eventLogtagsPath)
Inseob Kimb7b84572024-04-30 10:51:47 +0900768}
769
Kiyoung Kim67118212024-11-07 13:23:44 +0900770func (f *filesystem) BuildLinkerConfigFile(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath) {
Spandan Das2047a4c2024-11-11 21:24:58 +0000771 if !proptools.Bool(f.properties.Linker_config.Gen_linker_config) {
Spandan Das92631882024-10-28 22:49:38 +0000772 return
773 }
774
Spandan Das918191e2024-10-31 18:27:23 +0000775 provideModules, _ := f.getLibsForLinkerConfig(ctx)
Spandan Das92631882024-10-28 22:49:38 +0000776 output := rebasedDir.Join(ctx, "etc", "linker.config.pb")
Spandan Das2047a4c2024-11-11 21:24:58 +0000777 linkerconfig.BuildLinkerConfig(ctx, builder, android.PathsForModuleSrc(ctx, f.properties.Linker_config.Linker_config_srcs), provideModules, nil, output)
Spandan Das92631882024-10-28 22:49:38 +0000778
779 f.appendToEntry(ctx, output)
780}
781
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900782type partition interface {
783 PartitionType() string
784}
785
Cole Faust9a24d902024-03-18 15:38:12 -0700786func (f *filesystem) PartitionType() string {
787 return proptools.StringDefault(f.properties.Partition_type, "system")
788}
789
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900790var _ partition = (*filesystem)(nil)
791
Jiyong Park65c49f52020-11-24 14:23:26 +0900792var _ android.AndroidMkEntriesProvider = (*filesystem)(nil)
793
794// Implements android.AndroidMkEntriesProvider
795func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries {
796 return []android.AndroidMkEntries{android.AndroidMkEntries{
797 Class: "ETC",
798 OutputFile: android.OptionalPathForPath(f.output),
799 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700800 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800801 entries.SetString("LOCAL_MODULE_PATH", f.installDir.String())
Jiyong Park65c49f52020-11-24 14:23:26 +0900802 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900803 entries.SetString("LOCAL_FILESYSTEM_FILELIST", f.fileListFile.String())
Jiyong Park65c49f52020-11-24 14:23:26 +0900804 },
805 },
806 }}
Jiyong Park6f0f6882020-11-12 13:14:30 +0900807}
Jiyong Park12a719c2021-01-07 15:31:24 +0900808
809// Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex
810// package to have access to the output file.
811type Filesystem interface {
812 android.Module
813 OutputPath() android.Path
Jiyong Park972e06c2021-03-15 23:32:49 +0900814
815 // Returns the output file that is signed by avbtool. If this module is not signed, returns
816 // nil.
817 SignedOutputPath() android.Path
Jiyong Park12a719c2021-01-07 15:31:24 +0900818}
819
820var _ Filesystem = (*filesystem)(nil)
821
822func (f *filesystem) OutputPath() android.Path {
823 return f.output
824}
Jiyong Park972e06c2021-03-15 23:32:49 +0900825
826func (f *filesystem) SignedOutputPath() android.Path {
827 if proptools.Bool(f.properties.Use_avb) {
828 return f.OutputPath()
829 }
830 return nil
831}
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900832
833// Filter the result of GatherPackagingSpecs to discard items targeting outside "system" partition.
834// Note that "apex" module installs its contents to "apex"(fake partition) as well
835// for symbol lookup by imitating "activated" paths.
836func (f *filesystem) gatherFilteredPackagingSpecs(ctx android.ModuleContext) map[string]android.PackagingSpec {
Inseob Kim3c0a0422024-11-05 17:21:37 +0900837 specs := f.PackagingBase.GatherPackagingSpecsWithFilterAndModifier(ctx, f.filesystemBuilder.FilterPackagingSpec, f.filesystemBuilder.ModifyPackagingSpec)
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900838 return specs
839}
Jooyung Han65f402b2022-04-21 14:24:04 +0900840
841func sha1sum(values []string) string {
842 h := sha256.New()
843 for _, value := range values {
844 io.WriteString(h, value)
845 }
846 return fmt.Sprintf("%x", h.Sum(nil))
847}
Jooyung Hane6067592023-03-16 13:11:17 +0900848
849// Base cc.UseCoverage
850
851var _ cc.UseCoverage = (*filesystem)(nil)
852
Colin Crosse1a85552024-06-14 12:17:37 -0700853func (*filesystem) IsNativeCoverageNeeded(ctx cc.IsNativeCoverageNeededContext) bool {
Jooyung Hane6067592023-03-16 13:11:17 +0900854 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
855}
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900856
857// android_filesystem_defaults
858
859type filesystemDefaults struct {
860 android.ModuleBase
861 android.DefaultsModuleBase
862
Inseob Kim3c0a0422024-11-05 17:21:37 +0900863 properties FilesystemProperties
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900864}
865
866// android_filesystem_defaults is a default module for android_filesystem and android_system_image
867func filesystemDefaultsFactory() android.Module {
868 module := &filesystemDefaults{}
869 module.AddProperties(&module.properties)
870 module.AddProperties(&android.PackagingProperties{})
871 android.InitDefaultsModule(module)
872 return module
873}
874
875func (f *filesystemDefaults) PartitionType() string {
876 return proptools.StringDefault(f.properties.Partition_type, "system")
877}
878
879var _ partition = (*filesystemDefaults)(nil)
880
881func (f *filesystemDefaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
882 validatePartitionType(ctx, f)
883}
Spandan Das918191e2024-10-31 18:27:23 +0000884
885// getLibsForLinkerConfig returns
886// 1. A list of libraries installed in this filesystem
887// 2. A list of dep libraries _not_ installed in this filesystem
888//
889// `linkerconfig.BuildLinkerConfig` will convert these two to a linker.config.pb for the filesystem
890// (1) will be added to --provideLibs if they are C libraries with a stable interface (has stubs)
891// (2) will be added to --requireLibs if they are C libraries with a stable interface (has stubs)
892func (f *filesystem) getLibsForLinkerConfig(ctx android.ModuleContext) ([]android.Module, []android.Module) {
893 // we need "Module"s for packaging items
894 modulesInPackageByModule := make(map[android.Module]bool)
895 modulesInPackageByName := make(map[string]bool)
896
897 deps := f.gatherFilteredPackagingSpecs(ctx)
898 ctx.WalkDeps(func(child, parent android.Module) bool {
899 for _, ps := range android.OtherModuleProviderOrDefault(
900 ctx, child, android.InstallFilesProvider).PackagingSpecs {
901 if _, ok := deps[ps.RelPathInPackage()]; ok {
902 modulesInPackageByModule[child] = true
903 modulesInPackageByName[child.Name()] = true
904 return true
905 }
906 }
907 return true
908 })
909
910 provideModules := make([]android.Module, 0, len(modulesInPackageByModule))
911 for mod := range modulesInPackageByModule {
912 provideModules = append(provideModules, mod)
913 }
914
915 var requireModules []android.Module
916 ctx.WalkDeps(func(child, parent android.Module) bool {
917 _, parentInPackage := modulesInPackageByModule[parent]
918 _, childInPackageName := modulesInPackageByName[child.Name()]
919
920 // When parent is in the package, and child (or its variant) is not, this can be from an interface.
921 if parentInPackage && !childInPackageName {
922 requireModules = append(requireModules, child)
923 }
924 return true
925 })
926
927 return provideModules, requireModules
928}