blob: c889dd61c2a1e10490bb5d2b0296f98140f4a7ae [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"
Jiyong Park65b62242020-11-25 12:44:59 +090028
29 "github.com/google/blueprint"
Jiyong Park71baa762021-01-18 21:11:03 +090030 "github.com/google/blueprint/proptools"
Jiyong Park6f0f6882020-11-12 13:14:30 +090031)
32
33func init() {
Jooyung Han9706cbc2021-04-15 22:43:48 +090034 registerBuildComponents(android.InitRegistrationContext)
35}
36
37func registerBuildComponents(ctx android.RegistrationContext) {
38 ctx.RegisterModuleType("android_filesystem", filesystemFactory)
Jiyong Parkf46b1af2024-04-05 18:13:33 +090039 ctx.RegisterModuleType("android_filesystem_defaults", filesystemDefaultsFactory)
Jiyong Parkfa616132021-04-20 11:36:40 +090040 ctx.RegisterModuleType("android_system_image", systemImageFactory)
Jiyong Parkbc485482022-11-15 22:31:49 +090041 ctx.RegisterModuleType("avb_add_hash_footer", avbAddHashFooterFactory)
Inseob Kim87230e62023-11-22 18:55:07 +090042 ctx.RegisterModuleType("avb_add_hash_footer_defaults", avbAddHashFooterDefaultsFactory)
Alice Wang000e3a32023-01-03 16:11:20 +000043 ctx.RegisterModuleType("avb_gen_vbmeta_image", avbGenVbmetaImageFactory)
Inseob Kim87230e62023-11-22 18:55:07 +090044 ctx.RegisterModuleType("avb_gen_vbmeta_image_defaults", avbGenVbmetaImageDefaultsFactory)
Jiyong Park6f0f6882020-11-12 13:14:30 +090045}
46
47type filesystem struct {
48 android.ModuleBase
49 android.PackagingBase
Jiyong Parkf46b1af2024-04-05 18:13:33 +090050 android.DefaultableModuleBase
Jiyong Park65c49f52020-11-24 14:23:26 +090051
Jiyong Park71baa762021-01-18 21:11:03 +090052 properties filesystemProperties
53
Jiyong Parkfa616132021-04-20 11:36:40 +090054 // Function that builds extra files under the root directory and returns the files
55 buildExtraFiles func(ctx android.ModuleContext, root android.OutputPath) android.OutputPaths
56
Jeongik Cha54bf8752024-02-08 10:44:37 +090057 // Function that filters PackagingSpec in PackagingBase.GatherPackagingSpecs()
58 filterPackagingSpec func(spec android.PackagingSpec) bool
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090059
Jiyong Park65c49f52020-11-24 14:23:26 +090060 output android.OutputPath
61 installDir android.InstallPath
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090062
Inseob Kim53391842024-03-29 17:44:07 +090063 // For testing. Keeps the result of CopySpecsToDir()
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090064 entries []string
Jiyong Park6f0f6882020-11-12 13:14:30 +090065}
66
Inseob Kim14199b02021-02-09 21:18:31 +090067type symlinkDefinition struct {
68 Target *string
69 Name *string
70}
71
Jiyong Park71baa762021-01-18 21:11:03 +090072type filesystemProperties struct {
73 // When set to true, sign the image with avbtool. Default is false.
74 Use_avb *bool
75
76 // Path to the private key that avbtool will use to sign this filesystem image.
77 // TODO(jiyong): allow apex_key to be specified here
78 Avb_private_key *string `android:"path"`
79
Shikha Panwar01403bb2022-12-22 12:22:57 +000080 // Signing algorithm for avbtool. Default is SHA256_RSA4096.
Jiyong Park71baa762021-01-18 21:11:03 +090081 Avb_algorithm *string
Jiyong Park11a65972021-02-01 21:09:38 +090082
Shikha Panwar01403bb2022-12-22 12:22:57 +000083 // Hash algorithm used for avbtool (for descriptors). This is passed as hash_algorithm to
84 // avbtool. Default used by avbtool is sha1.
Shikha Panware6f30632022-12-21 12:54:45 +000085 Avb_hash_algorithm *string
86
Nikita Ioffe2c8cdc62024-03-27 22:19:30 +000087 // The index used to prevent rollback of the image. Only used if use_avb is true.
88 Rollback_index *int64
89
Jiyong Parkac4076d2021-03-15 23:21:30 +090090 // Name of the partition stored in vbmeta desc. Defaults to the name of this module.
91 Partition_name *string
92
Jiyong Park837cdb22021-02-05 00:17:14 +090093 // Type of the filesystem. Currently, ext4, cpio, and compressed_cpio are supported. Default
94 // is ext4.
Jiyong Park11a65972021-02-01 21:09:38 +090095 Type *string
Inseob Kimcc8e5362021-02-03 14:05:24 +090096
Cole Faust9a24d902024-03-18 15:38:12 -070097 // Identifies which partition this is for //visibility:any_system_image (and others) visibility
98 // checks, and will be used in the future for API surface checks.
99 Partition_type *string
100
Inseob Kimcc8e5362021-02-03 14:05:24 +0900101 // file_contexts file to make image. Currently, only ext4 is supported.
102 File_contexts *string `android:"path"`
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900103
104 // Base directory relative to root, to which deps are installed, e.g. "system". Default is "."
105 // (root).
106 Base_dir *string
Inseob Kim14199b02021-02-09 21:18:31 +0900107
108 // Directories to be created under root. e.g. /dev, /proc, etc.
Cole Faustd9c6a5b2024-05-21 14:54:00 -0700109 Dirs proptools.Configurable[[]string]
Inseob Kim14199b02021-02-09 21:18:31 +0900110
111 // Symbolic links to be created under root with "ln -sf <target> <name>".
112 Symlinks []symlinkDefinition
Jooyung Han65f402b2022-04-21 14:24:04 +0900113
114 // Seconds since unix epoch to override timestamps of file entries
115 Fake_timestamp *string
116
117 // When set, passed to mkuserimg_mke2fs --mke2fs_uuid & --mke2fs_hash_seed.
118 // Otherwise, they'll be set as random which might cause indeterministic build output.
119 Uuid *string
Inseob Kim376d72f2023-11-01 15:40:25 +0900120
121 // Mount point for this image. Default is "/"
122 Mount_point *string
Cole Faust4a2a7c92024-03-12 12:44:40 -0700123
124 // If set to the name of a partition ("system", "vendor", etc), this filesystem module
125 // will also include the contents of the make-built staging directories. If any soong
126 // modules would be installed to the same location as a make module, they will overwrite
127 // the make version.
128 Include_make_built_files string
Inseob Kim53391842024-03-29 17:44:07 +0900129
Inseob Kimb7b84572024-04-30 10:51:47 +0900130 // When set, builds etc/event-log-tags file by merging logtags from all dependencies.
131 // Default is false
132 Build_logtags *bool
133
Justin Yun74f3f302024-05-07 14:32:14 +0900134 // Install aconfig_flags.pb file for the modules installed in this partition.
135 Gen_aconfig_flags_pb *bool
136
Inseob Kim53391842024-03-29 17:44:07 +0900137 Fsverity fsverityProperties
Jiyong Park71baa762021-01-18 21:11:03 +0900138}
139
Jiyong Park65c49f52020-11-24 14:23:26 +0900140// android_filesystem packages a set of modules and their transitive dependencies into a filesystem
141// image. The filesystem images are expected to be mounted in the target device, which means the
142// modules in the filesystem image are built for the target device (i.e. Android, not Linux host).
143// The modules are placed in the filesystem image just like they are installed to the ordinary
144// partitions like system.img. For example, cc_library modules are placed under ./lib[64] directory.
Jiyong Park6f0f6882020-11-12 13:14:30 +0900145func filesystemFactory() android.Module {
146 module := &filesystem{}
Jiyong Park7e7d4af2024-05-01 12:36:10 +0000147 module.filterPackagingSpec = module.filterInstallablePackagingSpec
Jiyong Parkfa616132021-04-20 11:36:40 +0900148 initFilesystemModule(module)
149 return module
150}
151
152func initFilesystemModule(module *filesystem) {
Jiyong Park71baa762021-01-18 21:11:03 +0900153 module.AddProperties(&module.properties)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900154 android.InitPackageModule(module)
Jiyong Park3ea9b652024-05-15 23:01:54 +0900155 module.PackagingBase.DepsCollectFirstTargetOnly = true
Jiyong Park6f0f6882020-11-12 13:14:30 +0900156 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900157 android.InitDefaultableModule(module)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900158}
159
Jiyong Park12a719c2021-01-07 15:31:24 +0900160var dependencyTag = struct {
161 blueprint.BaseDependencyTag
Jooyung Han092ef812021-03-10 15:40:34 +0900162 android.PackagingItemAlwaysDepTag
Jiyong Park12a719c2021-01-07 15:31:24 +0900163}{}
Jiyong Park65b62242020-11-25 12:44:59 +0900164
Jiyong Park6f0f6882020-11-12 13:14:30 +0900165func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park65b62242020-11-25 12:44:59 +0900166 f.AddDeps(ctx, dependencyTag)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900167}
168
Jiyong Park11a65972021-02-01 21:09:38 +0900169type fsType int
170
171const (
172 ext4Type fsType = iota
173 compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900174 cpioType // uncompressed
Jiyong Park11a65972021-02-01 21:09:38 +0900175 unknown
176)
177
178func (f *filesystem) fsType(ctx android.ModuleContext) fsType {
179 typeStr := proptools.StringDefault(f.properties.Type, "ext4")
180 switch typeStr {
181 case "ext4":
182 return ext4Type
183 case "compressed_cpio":
184 return compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900185 case "cpio":
186 return cpioType
Jiyong Park11a65972021-02-01 21:09:38 +0900187 default:
188 ctx.PropertyErrorf("type", "%q not supported", typeStr)
189 return unknown
190 }
191}
192
Jiyong Park65c49f52020-11-24 14:23:26 +0900193func (f *filesystem) installFileName() string {
194 return f.BaseModuleName() + ".img"
195}
196
Inseob Kim53391842024-03-29 17:44:07 +0900197func (f *filesystem) partitionName() string {
198 return proptools.StringDefault(f.properties.Partition_name, f.Name())
199}
200
Jiyong Park7e7d4af2024-05-01 12:36:10 +0000201func (f *filesystem) filterInstallablePackagingSpec(ps android.PackagingSpec) bool {
202 // Filesystem module respects the installation semantic. A PackagingSpec from a module with
203 // IsSkipInstall() is skipped.
204 return !ps.SkipInstall()
205}
206
Jiyong Park6f0f6882020-11-12 13:14:30 +0900207var pctx = android.NewPackageContext("android/soong/filesystem")
208
209func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900210 validatePartitionType(ctx, f)
Jiyong Park11a65972021-02-01 21:09:38 +0900211 switch f.fsType(ctx) {
212 case ext4Type:
213 f.output = f.buildImageUsingBuildImage(ctx)
214 case compressedCpioType:
Jiyong Park837cdb22021-02-05 00:17:14 +0900215 f.output = f.buildCpioImage(ctx, true)
216 case cpioType:
217 f.output = f.buildCpioImage(ctx, false)
Jiyong Park11a65972021-02-01 21:09:38 +0900218 default:
219 return
220 }
221
222 f.installDir = android.PathForModuleInstall(ctx, "etc")
223 ctx.InstallFile(f.installDir, f.installFileName(), f.output)
mrziwang555d1332024-06-07 11:15:33 -0700224
225 ctx.SetOutputFiles([]android.Path{f.output}, "")
Jiyong Park11a65972021-02-01 21:09:38 +0900226}
227
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900228func validatePartitionType(ctx android.ModuleContext, p partition) {
229 if !android.InList(p.PartitionType(), validPartitions) {
230 ctx.PropertyErrorf("partition_type", "partition_type must be one of %s, found: %s", validPartitions, p.PartitionType())
231 }
232
233 ctx.VisitDirectDepsWithTag(android.DefaultsDepTag, func(m android.Module) {
234 if fdm, ok := m.(*filesystemDefaults); ok {
235 if p.PartitionType() != fdm.PartitionType() {
236 ctx.PropertyErrorf("partition_type",
237 "%s doesn't match with the partition type %s of the filesystem default module %s",
238 p.PartitionType(), fdm.PartitionType(), m.Name())
239 }
240 }
241 })
242}
243
Cole Faust3b806d32024-03-11 15:15:03 -0700244// Copy extra files/dirs that are not from the `deps` property to `rootDir`, checking for conflicts with files
245// already in `rootDir`.
246func (f *filesystem) buildNonDepsFiles(ctx android.ModuleContext, builder *android.RuleBuilder, rootDir android.OutputPath) {
Inseob Kim14199b02021-02-09 21:18:31 +0900247 // create dirs and symlinks
Cole Faustd9c6a5b2024-05-21 14:54:00 -0700248 for _, dir := range f.properties.Dirs.GetOrDefault(ctx, nil) {
Inseob Kim14199b02021-02-09 21:18:31 +0900249 // OutputPath.Join verifies dir
250 builder.Command().Text("mkdir -p").Text(rootDir.Join(ctx, dir).String())
251 }
252
253 for _, symlink := range f.properties.Symlinks {
254 name := strings.TrimSpace(proptools.String(symlink.Name))
255 target := strings.TrimSpace(proptools.String(symlink.Target))
256
257 if name == "" {
258 ctx.PropertyErrorf("symlinks", "Name can't be empty")
259 continue
260 }
261
262 if target == "" {
263 ctx.PropertyErrorf("symlinks", "Target can't be empty")
264 continue
265 }
266
267 // OutputPath.Join verifies name. don't need to verify target.
268 dst := rootDir.Join(ctx, name)
Cole Faust3b806d32024-03-11 15:15:03 -0700269 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 +0900270 builder.Command().Text("mkdir -p").Text(filepath.Dir(dst.String()))
271 builder.Command().Text("ln -sf").Text(proptools.ShellEscape(target)).Text(dst.String())
272 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900273
Jiyong Parkfa616132021-04-20 11:36:40 +0900274 // create extra files if there's any
Jiyong Parkfa616132021-04-20 11:36:40 +0900275 if f.buildExtraFiles != nil {
Cole Faust4a2a7c92024-03-12 12:44:40 -0700276 rootForExtraFiles := android.PathForModuleGen(ctx, "root-extra").OutputPath
277 extraFiles := f.buildExtraFiles(ctx, rootForExtraFiles)
278 for _, f := range extraFiles {
279 rel, err := filepath.Rel(rootForExtraFiles.String(), f.String())
280 if err != nil || strings.HasPrefix(rel, "..") {
281 ctx.ModuleErrorf("can't make %q relative to %q", f, rootForExtraFiles)
282 }
Jiyong Parkfa616132021-04-20 11:36:40 +0900283 }
Cole Faust4a2a7c92024-03-12 12:44:40 -0700284 if len(extraFiles) > 0 {
285 builder.Command().BuiltTool("merge_directories").
286 Implicits(extraFiles.Paths()).
287 Text(rootDir.String()).
288 Text(rootForExtraFiles.String())
289 }
Jiyong Parkfa616132021-04-20 11:36:40 +0900290 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900291}
292
Jiyong Park11a65972021-02-01 21:09:38 +0900293func (f *filesystem) buildImageUsingBuildImage(ctx android.ModuleContext) android.OutputPath {
Jiyong Park6f0f6882020-11-12 13:14:30 +0900294 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Cole Faust3b806d32024-03-11 15:15:03 -0700295 rebasedDir := rootDir
296 if f.properties.Base_dir != nil {
297 rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir)
298 }
299 builder := android.NewRuleBuilder(pctx, ctx)
300 // Wipe the root dir to get rid of leftover files from prior builds
301 builder.Command().Textf("rm -rf %s && mkdir -p %s", rootDir, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900302 specs := f.gatherFilteredPackagingSpecs(ctx)
303 f.entries = f.CopySpecsToDir(ctx, builder, specs, rebasedDir)
Cole Faust3b806d32024-03-11 15:15:03 -0700304
305 f.buildNonDepsFiles(ctx, builder, rootDir)
Cole Faust4a2a7c92024-03-12 12:44:40 -0700306 f.addMakeBuiltFiles(ctx, builder, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900307 f.buildFsverityMetadataFiles(ctx, builder, specs, rootDir, rebasedDir)
Inseob Kimb7b84572024-04-30 10:51:47 +0900308 f.buildEventLogtagsFile(ctx, builder, rebasedDir)
Justin Yun74f3f302024-05-07 14:32:14 +0900309 f.buildAconfigFlagsFiles(ctx, builder, specs, rebasedDir)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900310
Nikita Ioffe519015f2022-12-23 15:36:29 +0000311 // run host_init_verifier
312 // Ideally we should have a concept of pluggable linters that verify the generated image.
313 // While such concept is not implement this will do.
314 // TODO(b/263574231): substitute with pluggable linter.
315 builder.Command().
316 BuiltTool("host_init_verifier").
317 FlagWithArg("--out_system=", rootDir.String()+"/system")
318
Jiyong Park72678312021-01-18 17:29:49 +0900319 propFile, toolDeps := f.buildPropFile(ctx)
Jiyong Park11a65972021-02-01 21:09:38 +0900320 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -0800321 builder.Command().BuiltTool("build_image").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900322 Text(rootDir.String()). // input directory
323 Input(propFile).
Jiyong Park72678312021-01-18 17:29:49 +0900324 Implicits(toolDeps).
Jiyong Park11a65972021-02-01 21:09:38 +0900325 Output(output).
Jiyong Park6f0f6882020-11-12 13:14:30 +0900326 Text(rootDir.String()) // directory where to find fs_config_files|dirs
327
328 // rootDir is not deleted. Might be useful for quick inspection.
Colin Crossf1a035e2020-11-16 17:32:30 -0800329 builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park65c49f52020-11-24 14:23:26 +0900330
Jiyong Park11a65972021-02-01 21:09:38 +0900331 return output
Jiyong Park65c49f52020-11-24 14:23:26 +0900332}
333
Inseob Kimcc8e5362021-02-03 14:05:24 +0900334func (f *filesystem) buildFileContexts(ctx android.ModuleContext) android.OutputPath {
335 builder := android.NewRuleBuilder(pctx, ctx)
336 fcBin := android.PathForModuleOut(ctx, "file_contexts.bin")
337 builder.Command().BuiltTool("sefcontext_compile").
338 FlagWithOutput("-o ", fcBin).
339 Input(android.PathForModuleSrc(ctx, proptools.String(f.properties.File_contexts)))
340 builder.Build("build_filesystem_file_contexts", fmt.Sprintf("Creating filesystem file contexts for %s", f.BaseModuleName()))
341 return fcBin.OutputPath
342}
343
Jooyung Han65f402b2022-04-21 14:24:04 +0900344// Calculates avb_salt from entry list (sorted) for deterministic output.
345func (f *filesystem) salt() string {
346 return sha1sum(f.entries)
347}
348
Jiyong Park72678312021-01-18 17:29:49 +0900349func (f *filesystem) buildPropFile(ctx android.ModuleContext) (propFile android.OutputPath, toolDeps android.Paths) {
Jiyong Park72678312021-01-18 17:29:49 +0900350 var deps android.Paths
Cole Faustcec230a2024-03-07 15:51:12 -0800351 var propFileString strings.Builder
Jiyong Park72678312021-01-18 17:29:49 +0900352 addStr := func(name string, value string) {
Cole Faustcec230a2024-03-07 15:51:12 -0800353 propFileString.WriteString(name)
354 propFileString.WriteRune('=')
355 propFileString.WriteString(value)
356 propFileString.WriteRune('\n')
Jiyong Park72678312021-01-18 17:29:49 +0900357 }
358 addPath := func(name string, path android.Path) {
Cole Faustcec230a2024-03-07 15:51:12 -0800359 addStr(name, path.String())
Jiyong Park72678312021-01-18 17:29:49 +0900360 deps = append(deps, path)
361 }
362
Jiyong Park11a65972021-02-01 21:09:38 +0900363 // Type string that build_image.py accepts.
364 fsTypeStr := func(t fsType) string {
365 switch t {
366 // TODO(jiyong): add more types like f2fs, erofs, etc.
367 case ext4Type:
368 return "ext4"
369 }
370 panic(fmt.Errorf("unsupported fs type %v", t))
371 }
372
373 addStr("fs_type", fsTypeStr(f.fsType(ctx)))
Inseob Kim376d72f2023-11-01 15:40:25 +0900374 addStr("mount_point", proptools.StringDefault(f.properties.Mount_point, "/"))
Jiyong Park72678312021-01-18 17:29:49 +0900375 addStr("use_dynamic_partition_size", "true")
376 addPath("ext_mkuserimg", ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs"))
377 // b/177813163 deps of the host tools have to be added. Remove this.
378 for _, t := range []string{"mke2fs", "e2fsdroid", "tune2fs"} {
379 deps = append(deps, ctx.Config().HostToolPath(ctx, t))
380 }
381
Jiyong Park71baa762021-01-18 21:11:03 +0900382 if proptools.Bool(f.properties.Use_avb) {
383 addStr("avb_hashtree_enable", "true")
384 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
385 algorithm := proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096")
386 addStr("avb_algorithm", algorithm)
387 key := android.PathForModuleSrc(ctx, proptools.String(f.properties.Avb_private_key))
388 addPath("avb_key_path", key)
Inseob Kim53391842024-03-29 17:44:07 +0900389 addStr("partition_name", f.partitionName())
Shikha Panware6f30632022-12-21 12:54:45 +0000390 avb_add_hashtree_footer_args := "--do_not_generate_fec"
391 if hashAlgorithm := proptools.String(f.properties.Avb_hash_algorithm); hashAlgorithm != "" {
392 avb_add_hashtree_footer_args += " --hash_algorithm " + hashAlgorithm
393 }
Nikita Ioffe2c8cdc62024-03-27 22:19:30 +0000394 if f.properties.Rollback_index != nil {
395 rollbackIndex := proptools.Int(f.properties.Rollback_index)
396 if rollbackIndex < 0 {
397 ctx.PropertyErrorf("rollback_index", "Rollback index must be non-negative")
398 }
399 avb_add_hashtree_footer_args += " --rollback_index " + strconv.Itoa(rollbackIndex)
400 }
Inseob Kim53391842024-03-29 17:44:07 +0900401 securityPatchKey := "com.android.build." + f.partitionName() + ".security_patch"
Seungjae Yooa30e4502023-11-09 14:55:44 +0900402 securityPatchValue := ctx.Config().PlatformSecurityPatch()
403 avb_add_hashtree_footer_args += " --prop " + securityPatchKey + ":" + securityPatchValue
Shikha Panware6f30632022-12-21 12:54:45 +0000404 addStr("avb_add_hashtree_footer_args", avb_add_hashtree_footer_args)
Jooyung Han65f402b2022-04-21 14:24:04 +0900405 addStr("avb_salt", f.salt())
Jiyong Park71baa762021-01-18 21:11:03 +0900406 }
407
Inseob Kimcc8e5362021-02-03 14:05:24 +0900408 if proptools.String(f.properties.File_contexts) != "" {
409 addPath("selinux_fc", f.buildFileContexts(ctx))
410 }
Jooyung Han65f402b2022-04-21 14:24:04 +0900411 if timestamp := proptools.String(f.properties.Fake_timestamp); timestamp != "" {
412 addStr("timestamp", timestamp)
413 }
414 if uuid := proptools.String(f.properties.Uuid); uuid != "" {
415 addStr("uuid", uuid)
416 addStr("hash_seed", uuid)
417 }
Jiyong Park72678312021-01-18 17:29:49 +0900418 propFile = android.PathForModuleOut(ctx, "prop").OutputPath
Cole Faustcec230a2024-03-07 15:51:12 -0800419 android.WriteFileRuleVerbatim(ctx, propFile, propFileString.String())
Jiyong Park72678312021-01-18 17:29:49 +0900420 return propFile, deps
421}
422
Jiyong Park837cdb22021-02-05 00:17:14 +0900423func (f *filesystem) buildCpioImage(ctx android.ModuleContext, compressed bool) android.OutputPath {
Jiyong Park11a65972021-02-01 21:09:38 +0900424 if proptools.Bool(f.properties.Use_avb) {
425 ctx.PropertyErrorf("use_avb", "signing compresed cpio image using avbtool is not supported."+
426 "Consider adding this to bootimg module and signing the entire boot image.")
427 }
428
Inseob Kimcc8e5362021-02-03 14:05:24 +0900429 if proptools.String(f.properties.File_contexts) != "" {
430 ctx.PropertyErrorf("file_contexts", "file_contexts is not supported for compressed cpio image.")
431 }
432
Cole Faust4a2a7c92024-03-12 12:44:40 -0700433 if f.properties.Include_make_built_files != "" {
434 ctx.PropertyErrorf("include_make_built_files", "include_make_built_files is not supported for compressed cpio image.")
435 }
436
Jiyong Park11a65972021-02-01 21:09:38 +0900437 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Cole Faust3b806d32024-03-11 15:15:03 -0700438 rebasedDir := rootDir
439 if f.properties.Base_dir != nil {
440 rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir)
441 }
442 builder := android.NewRuleBuilder(pctx, ctx)
443 // Wipe the root dir to get rid of leftover files from prior builds
444 builder.Command().Textf("rm -rf %s && mkdir -p %s", rootDir, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900445 specs := f.gatherFilteredPackagingSpecs(ctx)
446 f.entries = f.CopySpecsToDir(ctx, builder, specs, rebasedDir)
Cole Faust3b806d32024-03-11 15:15:03 -0700447
448 f.buildNonDepsFiles(ctx, builder, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900449 f.buildFsverityMetadataFiles(ctx, builder, specs, rootDir, rebasedDir)
Inseob Kimb7b84572024-04-30 10:51:47 +0900450 f.buildEventLogtagsFile(ctx, builder, rebasedDir)
Justin Yun74f3f302024-05-07 14:32:14 +0900451 f.buildAconfigFlagsFiles(ctx, builder, specs, rebasedDir)
Jiyong Park11a65972021-02-01 21:09:38 +0900452
453 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Jiyong Park837cdb22021-02-05 00:17:14 +0900454 cmd := builder.Command().
Jiyong Park11a65972021-02-01 21:09:38 +0900455 BuiltTool("mkbootfs").
Jiyong Park837cdb22021-02-05 00:17:14 +0900456 Text(rootDir.String()) // input directory
457 if compressed {
458 cmd.Text("|").
459 BuiltTool("lz4").
460 Flag("--favor-decSpeed"). // for faster boot
461 Flag("-12"). // maximum compression level
462 Flag("-l"). // legacy format for kernel
463 Text(">").Output(output)
464 } else {
465 cmd.Text(">").Output(output)
466 }
Jiyong Park11a65972021-02-01 21:09:38 +0900467
468 // rootDir is not deleted. Might be useful for quick inspection.
Jiyong Park837cdb22021-02-05 00:17:14 +0900469 builder.Build("build_cpio_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park11a65972021-02-01 21:09:38 +0900470
471 return output
472}
473
Cole Faust4a2a7c92024-03-12 12:44:40 -0700474var validPartitions = []string{
475 "system",
476 "userdata",
477 "cache",
478 "system_other",
479 "vendor",
480 "product",
481 "system_ext",
482 "odm",
483 "vendor_dlkm",
484 "odm_dlkm",
485 "system_dlkm",
486}
487
488func (f *filesystem) addMakeBuiltFiles(ctx android.ModuleContext, builder *android.RuleBuilder, rootDir android.Path) {
489 partition := f.properties.Include_make_built_files
490 if partition == "" {
491 return
492 }
493 if !slices.Contains(validPartitions, partition) {
494 ctx.PropertyErrorf("include_make_built_files", "Expected one of %#v, found %q", validPartitions, partition)
495 return
496 }
497 stampFile := fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/staging_dir.stamp", ctx.Config().DeviceName(), partition)
498 fileListFile := fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partition)
499 stagingDir := fmt.Sprintf("target/product/%s/%s", ctx.Config().DeviceName(), partition)
500
501 builder.Command().BuiltTool("merge_directories").
502 Implicit(android.PathForArbitraryOutput(ctx, stampFile)).
503 Text("--ignore-duplicates").
504 FlagWithInput("--file-list", android.PathForArbitraryOutput(ctx, fileListFile)).
505 Text(rootDir.String()).
506 Text(android.PathForArbitraryOutput(ctx, stagingDir).String())
507}
508
Inseob Kimb7b84572024-04-30 10:51:47 +0900509func (f *filesystem) buildEventLogtagsFile(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath) {
510 if !proptools.Bool(f.properties.Build_logtags) {
511 return
512 }
513
514 logtagsFilePaths := make(map[string]bool)
515 ctx.WalkDeps(func(child, parent android.Module) bool {
516 if logtagsInfo, ok := android.OtherModuleProvider(ctx, child, android.LogtagsProviderKey); ok {
517 for _, path := range logtagsInfo.Logtags {
518 logtagsFilePaths[path.String()] = true
519 }
520 }
521 return true
522 })
523
524 if len(logtagsFilePaths) == 0 {
525 return
526 }
527
528 etcPath := rebasedDir.Join(ctx, "etc")
529 eventLogtagsPath := etcPath.Join(ctx, "event-log-tags")
530 builder.Command().Text("mkdir").Flag("-p").Text(etcPath.String())
531 cmd := builder.Command().BuiltTool("merge-event-log-tags").
532 FlagWithArg("-o ", eventLogtagsPath.String()).
533 FlagWithInput("-m ", android.MergedLogtagsPath(ctx))
534
535 for _, path := range android.SortedKeys(logtagsFilePaths) {
536 cmd.Text(path)
537 }
538}
539
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900540type partition interface {
541 PartitionType() string
542}
543
Cole Faust9a24d902024-03-18 15:38:12 -0700544func (f *filesystem) PartitionType() string {
545 return proptools.StringDefault(f.properties.Partition_type, "system")
546}
547
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900548var _ partition = (*filesystem)(nil)
549
Jiyong Park65c49f52020-11-24 14:23:26 +0900550var _ android.AndroidMkEntriesProvider = (*filesystem)(nil)
551
552// Implements android.AndroidMkEntriesProvider
553func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries {
554 return []android.AndroidMkEntries{android.AndroidMkEntries{
555 Class: "ETC",
556 OutputFile: android.OptionalPathForPath(f.output),
557 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700558 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800559 entries.SetString("LOCAL_MODULE_PATH", f.installDir.String())
Jiyong Park65c49f52020-11-24 14:23:26 +0900560 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
561 },
562 },
563 }}
Jiyong Park6f0f6882020-11-12 13:14:30 +0900564}
Jiyong Park12a719c2021-01-07 15:31:24 +0900565
566// Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex
567// package to have access to the output file.
568type Filesystem interface {
569 android.Module
570 OutputPath() android.Path
Jiyong Park972e06c2021-03-15 23:32:49 +0900571
572 // Returns the output file that is signed by avbtool. If this module is not signed, returns
573 // nil.
574 SignedOutputPath() android.Path
Jiyong Park12a719c2021-01-07 15:31:24 +0900575}
576
577var _ Filesystem = (*filesystem)(nil)
578
579func (f *filesystem) OutputPath() android.Path {
580 return f.output
581}
Jiyong Park972e06c2021-03-15 23:32:49 +0900582
583func (f *filesystem) SignedOutputPath() android.Path {
584 if proptools.Bool(f.properties.Use_avb) {
585 return f.OutputPath()
586 }
587 return nil
588}
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900589
590// Filter the result of GatherPackagingSpecs to discard items targeting outside "system" partition.
591// Note that "apex" module installs its contents to "apex"(fake partition) as well
592// for symbol lookup by imitating "activated" paths.
593func (f *filesystem) gatherFilteredPackagingSpecs(ctx android.ModuleContext) map[string]android.PackagingSpec {
Jeongik Cha54bf8752024-02-08 10:44:37 +0900594 specs := f.PackagingBase.GatherPackagingSpecsWithFilter(ctx, f.filterPackagingSpec)
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900595 return specs
596}
Jooyung Han65f402b2022-04-21 14:24:04 +0900597
598func sha1sum(values []string) string {
599 h := sha256.New()
600 for _, value := range values {
601 io.WriteString(h, value)
602 }
603 return fmt.Sprintf("%x", h.Sum(nil))
604}
Jooyung Hane6067592023-03-16 13:11:17 +0900605
606// Base cc.UseCoverage
607
608var _ cc.UseCoverage = (*filesystem)(nil)
609
Colin Crossf5f4ad32024-01-19 15:41:48 -0800610func (*filesystem) IsNativeCoverageNeeded(ctx android.IncomingTransitionContext) bool {
Jooyung Hane6067592023-03-16 13:11:17 +0900611 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
612}
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900613
614// android_filesystem_defaults
615
616type filesystemDefaults struct {
617 android.ModuleBase
618 android.DefaultsModuleBase
619
620 properties filesystemDefaultsProperties
621}
622
623type filesystemDefaultsProperties struct {
624 // Identifies which partition this is for //visibility:any_system_image (and others) visibility
625 // checks, and will be used in the future for API surface checks.
626 Partition_type *string
627}
628
629// android_filesystem_defaults is a default module for android_filesystem and android_system_image
630func filesystemDefaultsFactory() android.Module {
631 module := &filesystemDefaults{}
632 module.AddProperties(&module.properties)
633 module.AddProperties(&android.PackagingProperties{})
634 android.InitDefaultsModule(module)
635 return module
636}
637
638func (f *filesystemDefaults) PartitionType() string {
639 return proptools.StringDefault(f.properties.Partition_type, "system")
640}
641
642var _ partition = (*filesystemDefaults)(nil)
643
644func (f *filesystemDefaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
645 validatePartitionType(ctx, f)
646}