blob: 67ef64f119de8eae4b59e23647529a9bd28e91aa [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"
Inseob Kim14199b02021-02-09 21:18:31 +090023 "strings"
Jiyong Park6f0f6882020-11-12 13:14:30 +090024
25 "android/soong/android"
Jooyung Hane6067592023-03-16 13:11:17 +090026 "android/soong/cc"
Jiyong Park65b62242020-11-25 12:44:59 +090027
28 "github.com/google/blueprint"
Jiyong Park71baa762021-01-18 21:11:03 +090029 "github.com/google/blueprint/proptools"
Jiyong Park6f0f6882020-11-12 13:14:30 +090030)
31
32func init() {
Jooyung Han9706cbc2021-04-15 22:43:48 +090033 registerBuildComponents(android.InitRegistrationContext)
34}
35
36func registerBuildComponents(ctx android.RegistrationContext) {
37 ctx.RegisterModuleType("android_filesystem", filesystemFactory)
Jiyong Parkf46b1af2024-04-05 18:13:33 +090038 ctx.RegisterModuleType("android_filesystem_defaults", filesystemDefaultsFactory)
Jiyong Parkfa616132021-04-20 11:36:40 +090039 ctx.RegisterModuleType("android_system_image", systemImageFactory)
Jiyong Parkbc485482022-11-15 22:31:49 +090040 ctx.RegisterModuleType("avb_add_hash_footer", avbAddHashFooterFactory)
Inseob Kim87230e62023-11-22 18:55:07 +090041 ctx.RegisterModuleType("avb_add_hash_footer_defaults", avbAddHashFooterDefaultsFactory)
Alice Wang000e3a32023-01-03 16:11:20 +000042 ctx.RegisterModuleType("avb_gen_vbmeta_image", avbGenVbmetaImageFactory)
Inseob Kim87230e62023-11-22 18:55:07 +090043 ctx.RegisterModuleType("avb_gen_vbmeta_image_defaults", avbGenVbmetaImageDefaultsFactory)
Jiyong Park6f0f6882020-11-12 13:14:30 +090044}
45
46type filesystem struct {
47 android.ModuleBase
48 android.PackagingBase
Jiyong Parkf46b1af2024-04-05 18:13:33 +090049 android.DefaultableModuleBase
Jiyong Park65c49f52020-11-24 14:23:26 +090050
Jiyong Park71baa762021-01-18 21:11:03 +090051 properties filesystemProperties
52
Jiyong Parkfa616132021-04-20 11:36:40 +090053 // Function that builds extra files under the root directory and returns the files
54 buildExtraFiles func(ctx android.ModuleContext, root android.OutputPath) android.OutputPaths
55
Jeongik Cha54bf8752024-02-08 10:44:37 +090056 // Function that filters PackagingSpec in PackagingBase.GatherPackagingSpecs()
57 filterPackagingSpec func(spec android.PackagingSpec) bool
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090058
Jiyong Park65c49f52020-11-24 14:23:26 +090059 output android.OutputPath
60 installDir android.InstallPath
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090061
Inseob Kim53391842024-03-29 17:44:07 +090062 // For testing. Keeps the result of CopySpecsToDir()
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090063 entries []string
Jiyong Park6f0f6882020-11-12 13:14:30 +090064}
65
Inseob Kim14199b02021-02-09 21:18:31 +090066type symlinkDefinition struct {
67 Target *string
68 Name *string
69}
70
Jiyong Park71baa762021-01-18 21:11:03 +090071type filesystemProperties struct {
72 // When set to true, sign the image with avbtool. Default is false.
73 Use_avb *bool
74
75 // Path to the private key that avbtool will use to sign this filesystem image.
76 // TODO(jiyong): allow apex_key to be specified here
77 Avb_private_key *string `android:"path"`
78
Shikha Panwar01403bb2022-12-22 12:22:57 +000079 // Signing algorithm for avbtool. Default is SHA256_RSA4096.
Jiyong Park71baa762021-01-18 21:11:03 +090080 Avb_algorithm *string
Jiyong Park11a65972021-02-01 21:09:38 +090081
Shikha Panwar01403bb2022-12-22 12:22:57 +000082 // Hash algorithm used for avbtool (for descriptors). This is passed as hash_algorithm to
83 // avbtool. Default used by avbtool is sha1.
Shikha Panware6f30632022-12-21 12:54:45 +000084 Avb_hash_algorithm *string
85
Jiyong Parkac4076d2021-03-15 23:21:30 +090086 // Name of the partition stored in vbmeta desc. Defaults to the name of this module.
87 Partition_name *string
88
Jiyong Park837cdb22021-02-05 00:17:14 +090089 // Type of the filesystem. Currently, ext4, cpio, and compressed_cpio are supported. Default
90 // is ext4.
Jiyong Park11a65972021-02-01 21:09:38 +090091 Type *string
Inseob Kimcc8e5362021-02-03 14:05:24 +090092
Cole Faust9a24d902024-03-18 15:38:12 -070093 // Identifies which partition this is for //visibility:any_system_image (and others) visibility
94 // checks, and will be used in the future for API surface checks.
95 Partition_type *string
96
Inseob Kimcc8e5362021-02-03 14:05:24 +090097 // file_contexts file to make image. Currently, only ext4 is supported.
98 File_contexts *string `android:"path"`
Inseob Kim2ce1b5d2021-02-15 17:01:04 +090099
100 // Base directory relative to root, to which deps are installed, e.g. "system". Default is "."
101 // (root).
102 Base_dir *string
Inseob Kim14199b02021-02-09 21:18:31 +0900103
104 // Directories to be created under root. e.g. /dev, /proc, etc.
105 Dirs []string
106
107 // Symbolic links to be created under root with "ln -sf <target> <name>".
108 Symlinks []symlinkDefinition
Jooyung Han65f402b2022-04-21 14:24:04 +0900109
110 // Seconds since unix epoch to override timestamps of file entries
111 Fake_timestamp *string
112
113 // When set, passed to mkuserimg_mke2fs --mke2fs_uuid & --mke2fs_hash_seed.
114 // Otherwise, they'll be set as random which might cause indeterministic build output.
115 Uuid *string
Inseob Kim376d72f2023-11-01 15:40:25 +0900116
117 // Mount point for this image. Default is "/"
118 Mount_point *string
Cole Faust4a2a7c92024-03-12 12:44:40 -0700119
120 // If set to the name of a partition ("system", "vendor", etc), this filesystem module
121 // will also include the contents of the make-built staging directories. If any soong
122 // modules would be installed to the same location as a make module, they will overwrite
123 // the make version.
124 Include_make_built_files string
Inseob Kim53391842024-03-29 17:44:07 +0900125
126 Fsverity fsverityProperties
Jiyong Park71baa762021-01-18 21:11:03 +0900127}
128
Jiyong Park65c49f52020-11-24 14:23:26 +0900129// android_filesystem packages a set of modules and their transitive dependencies into a filesystem
130// image. The filesystem images are expected to be mounted in the target device, which means the
131// modules in the filesystem image are built for the target device (i.e. Android, not Linux host).
132// The modules are placed in the filesystem image just like they are installed to the ordinary
133// partitions like system.img. For example, cc_library modules are placed under ./lib[64] directory.
Jiyong Park6f0f6882020-11-12 13:14:30 +0900134func filesystemFactory() android.Module {
135 module := &filesystem{}
Jiyong Parkfa616132021-04-20 11:36:40 +0900136 initFilesystemModule(module)
137 return module
138}
139
140func initFilesystemModule(module *filesystem) {
Jiyong Park71baa762021-01-18 21:11:03 +0900141 module.AddProperties(&module.properties)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900142 android.InitPackageModule(module)
143 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900144 android.InitDefaultableModule(module)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900145}
146
Jiyong Park12a719c2021-01-07 15:31:24 +0900147var dependencyTag = struct {
148 blueprint.BaseDependencyTag
Jooyung Han092ef812021-03-10 15:40:34 +0900149 android.PackagingItemAlwaysDepTag
Jiyong Park12a719c2021-01-07 15:31:24 +0900150}{}
Jiyong Park65b62242020-11-25 12:44:59 +0900151
Jiyong Park6f0f6882020-11-12 13:14:30 +0900152func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park65b62242020-11-25 12:44:59 +0900153 f.AddDeps(ctx, dependencyTag)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900154}
155
Jiyong Park11a65972021-02-01 21:09:38 +0900156type fsType int
157
158const (
159 ext4Type fsType = iota
160 compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900161 cpioType // uncompressed
Jiyong Park11a65972021-02-01 21:09:38 +0900162 unknown
163)
164
165func (f *filesystem) fsType(ctx android.ModuleContext) fsType {
166 typeStr := proptools.StringDefault(f.properties.Type, "ext4")
167 switch typeStr {
168 case "ext4":
169 return ext4Type
170 case "compressed_cpio":
171 return compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900172 case "cpio":
173 return cpioType
Jiyong Park11a65972021-02-01 21:09:38 +0900174 default:
175 ctx.PropertyErrorf("type", "%q not supported", typeStr)
176 return unknown
177 }
178}
179
Jiyong Park65c49f52020-11-24 14:23:26 +0900180func (f *filesystem) installFileName() string {
181 return f.BaseModuleName() + ".img"
182}
183
Inseob Kim53391842024-03-29 17:44:07 +0900184func (f *filesystem) partitionName() string {
185 return proptools.StringDefault(f.properties.Partition_name, f.Name())
186}
187
Jiyong Park6f0f6882020-11-12 13:14:30 +0900188var pctx = android.NewPackageContext("android/soong/filesystem")
189
190func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900191 validatePartitionType(ctx, f)
Jiyong Park11a65972021-02-01 21:09:38 +0900192 switch f.fsType(ctx) {
193 case ext4Type:
194 f.output = f.buildImageUsingBuildImage(ctx)
195 case compressedCpioType:
Jiyong Park837cdb22021-02-05 00:17:14 +0900196 f.output = f.buildCpioImage(ctx, true)
197 case cpioType:
198 f.output = f.buildCpioImage(ctx, false)
Jiyong Park11a65972021-02-01 21:09:38 +0900199 default:
200 return
201 }
202
203 f.installDir = android.PathForModuleInstall(ctx, "etc")
204 ctx.InstallFile(f.installDir, f.installFileName(), f.output)
205}
206
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900207func validatePartitionType(ctx android.ModuleContext, p partition) {
208 if !android.InList(p.PartitionType(), validPartitions) {
209 ctx.PropertyErrorf("partition_type", "partition_type must be one of %s, found: %s", validPartitions, p.PartitionType())
210 }
211
212 ctx.VisitDirectDepsWithTag(android.DefaultsDepTag, func(m android.Module) {
213 if fdm, ok := m.(*filesystemDefaults); ok {
214 if p.PartitionType() != fdm.PartitionType() {
215 ctx.PropertyErrorf("partition_type",
216 "%s doesn't match with the partition type %s of the filesystem default module %s",
217 p.PartitionType(), fdm.PartitionType(), m.Name())
218 }
219 }
220 })
221}
222
Cole Faust3b806d32024-03-11 15:15:03 -0700223// Copy extra files/dirs that are not from the `deps` property to `rootDir`, checking for conflicts with files
224// already in `rootDir`.
225func (f *filesystem) buildNonDepsFiles(ctx android.ModuleContext, builder *android.RuleBuilder, rootDir android.OutputPath) {
Inseob Kim14199b02021-02-09 21:18:31 +0900226 // create dirs and symlinks
227 for _, dir := range f.properties.Dirs {
228 // OutputPath.Join verifies dir
229 builder.Command().Text("mkdir -p").Text(rootDir.Join(ctx, dir).String())
230 }
231
232 for _, symlink := range f.properties.Symlinks {
233 name := strings.TrimSpace(proptools.String(symlink.Name))
234 target := strings.TrimSpace(proptools.String(symlink.Target))
235
236 if name == "" {
237 ctx.PropertyErrorf("symlinks", "Name can't be empty")
238 continue
239 }
240
241 if target == "" {
242 ctx.PropertyErrorf("symlinks", "Target can't be empty")
243 continue
244 }
245
246 // OutputPath.Join verifies name. don't need to verify target.
247 dst := rootDir.Join(ctx, name)
Cole Faust3b806d32024-03-11 15:15:03 -0700248 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 +0900249 builder.Command().Text("mkdir -p").Text(filepath.Dir(dst.String()))
250 builder.Command().Text("ln -sf").Text(proptools.ShellEscape(target)).Text(dst.String())
251 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900252
Jiyong Parkfa616132021-04-20 11:36:40 +0900253 // create extra files if there's any
Jiyong Parkfa616132021-04-20 11:36:40 +0900254 if f.buildExtraFiles != nil {
Cole Faust4a2a7c92024-03-12 12:44:40 -0700255 rootForExtraFiles := android.PathForModuleGen(ctx, "root-extra").OutputPath
256 extraFiles := f.buildExtraFiles(ctx, rootForExtraFiles)
257 for _, f := range extraFiles {
258 rel, err := filepath.Rel(rootForExtraFiles.String(), f.String())
259 if err != nil || strings.HasPrefix(rel, "..") {
260 ctx.ModuleErrorf("can't make %q relative to %q", f, rootForExtraFiles)
261 }
Jiyong Parkfa616132021-04-20 11:36:40 +0900262 }
Cole Faust4a2a7c92024-03-12 12:44:40 -0700263 if len(extraFiles) > 0 {
264 builder.Command().BuiltTool("merge_directories").
265 Implicits(extraFiles.Paths()).
266 Text(rootDir.String()).
267 Text(rootForExtraFiles.String())
268 }
Jiyong Parkfa616132021-04-20 11:36:40 +0900269 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900270}
271
Jiyong Park11a65972021-02-01 21:09:38 +0900272func (f *filesystem) buildImageUsingBuildImage(ctx android.ModuleContext) android.OutputPath {
Jiyong Park6f0f6882020-11-12 13:14:30 +0900273 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Cole Faust3b806d32024-03-11 15:15:03 -0700274 rebasedDir := rootDir
275 if f.properties.Base_dir != nil {
276 rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir)
277 }
278 builder := android.NewRuleBuilder(pctx, ctx)
279 // Wipe the root dir to get rid of leftover files from prior builds
280 builder.Command().Textf("rm -rf %s && mkdir -p %s", rootDir, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900281 specs := f.gatherFilteredPackagingSpecs(ctx)
282 f.entries = f.CopySpecsToDir(ctx, builder, specs, rebasedDir)
Cole Faust3b806d32024-03-11 15:15:03 -0700283
284 f.buildNonDepsFiles(ctx, builder, rootDir)
Cole Faust4a2a7c92024-03-12 12:44:40 -0700285 f.addMakeBuiltFiles(ctx, builder, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900286 f.buildFsverityMetadataFiles(ctx, builder, specs, rootDir, rebasedDir)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900287
Nikita Ioffe519015f2022-12-23 15:36:29 +0000288 // run host_init_verifier
289 // Ideally we should have a concept of pluggable linters that verify the generated image.
290 // While such concept is not implement this will do.
291 // TODO(b/263574231): substitute with pluggable linter.
292 builder.Command().
293 BuiltTool("host_init_verifier").
294 FlagWithArg("--out_system=", rootDir.String()+"/system")
295
Jiyong Park72678312021-01-18 17:29:49 +0900296 propFile, toolDeps := f.buildPropFile(ctx)
Jiyong Park11a65972021-02-01 21:09:38 +0900297 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -0800298 builder.Command().BuiltTool("build_image").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900299 Text(rootDir.String()). // input directory
300 Input(propFile).
Jiyong Park72678312021-01-18 17:29:49 +0900301 Implicits(toolDeps).
Jiyong Park11a65972021-02-01 21:09:38 +0900302 Output(output).
Jiyong Park6f0f6882020-11-12 13:14:30 +0900303 Text(rootDir.String()) // directory where to find fs_config_files|dirs
304
305 // rootDir is not deleted. Might be useful for quick inspection.
Colin Crossf1a035e2020-11-16 17:32:30 -0800306 builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park65c49f52020-11-24 14:23:26 +0900307
Jiyong Park11a65972021-02-01 21:09:38 +0900308 return output
Jiyong Park65c49f52020-11-24 14:23:26 +0900309}
310
Inseob Kimcc8e5362021-02-03 14:05:24 +0900311func (f *filesystem) buildFileContexts(ctx android.ModuleContext) android.OutputPath {
312 builder := android.NewRuleBuilder(pctx, ctx)
313 fcBin := android.PathForModuleOut(ctx, "file_contexts.bin")
314 builder.Command().BuiltTool("sefcontext_compile").
315 FlagWithOutput("-o ", fcBin).
316 Input(android.PathForModuleSrc(ctx, proptools.String(f.properties.File_contexts)))
317 builder.Build("build_filesystem_file_contexts", fmt.Sprintf("Creating filesystem file contexts for %s", f.BaseModuleName()))
318 return fcBin.OutputPath
319}
320
Jooyung Han65f402b2022-04-21 14:24:04 +0900321// Calculates avb_salt from entry list (sorted) for deterministic output.
322func (f *filesystem) salt() string {
323 return sha1sum(f.entries)
324}
325
Jiyong Park72678312021-01-18 17:29:49 +0900326func (f *filesystem) buildPropFile(ctx android.ModuleContext) (propFile android.OutputPath, toolDeps android.Paths) {
Jiyong Park72678312021-01-18 17:29:49 +0900327 var deps android.Paths
Cole Faustcec230a2024-03-07 15:51:12 -0800328 var propFileString strings.Builder
Jiyong Park72678312021-01-18 17:29:49 +0900329 addStr := func(name string, value string) {
Cole Faustcec230a2024-03-07 15:51:12 -0800330 propFileString.WriteString(name)
331 propFileString.WriteRune('=')
332 propFileString.WriteString(value)
333 propFileString.WriteRune('\n')
Jiyong Park72678312021-01-18 17:29:49 +0900334 }
335 addPath := func(name string, path android.Path) {
Cole Faustcec230a2024-03-07 15:51:12 -0800336 addStr(name, path.String())
Jiyong Park72678312021-01-18 17:29:49 +0900337 deps = append(deps, path)
338 }
339
Jiyong Park11a65972021-02-01 21:09:38 +0900340 // Type string that build_image.py accepts.
341 fsTypeStr := func(t fsType) string {
342 switch t {
343 // TODO(jiyong): add more types like f2fs, erofs, etc.
344 case ext4Type:
345 return "ext4"
346 }
347 panic(fmt.Errorf("unsupported fs type %v", t))
348 }
349
350 addStr("fs_type", fsTypeStr(f.fsType(ctx)))
Inseob Kim376d72f2023-11-01 15:40:25 +0900351 addStr("mount_point", proptools.StringDefault(f.properties.Mount_point, "/"))
Jiyong Park72678312021-01-18 17:29:49 +0900352 addStr("use_dynamic_partition_size", "true")
353 addPath("ext_mkuserimg", ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs"))
354 // b/177813163 deps of the host tools have to be added. Remove this.
355 for _, t := range []string{"mke2fs", "e2fsdroid", "tune2fs"} {
356 deps = append(deps, ctx.Config().HostToolPath(ctx, t))
357 }
358
Jiyong Park71baa762021-01-18 21:11:03 +0900359 if proptools.Bool(f.properties.Use_avb) {
360 addStr("avb_hashtree_enable", "true")
361 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
362 algorithm := proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096")
363 addStr("avb_algorithm", algorithm)
364 key := android.PathForModuleSrc(ctx, proptools.String(f.properties.Avb_private_key))
365 addPath("avb_key_path", key)
Inseob Kim53391842024-03-29 17:44:07 +0900366 addStr("partition_name", f.partitionName())
Shikha Panware6f30632022-12-21 12:54:45 +0000367 avb_add_hashtree_footer_args := "--do_not_generate_fec"
368 if hashAlgorithm := proptools.String(f.properties.Avb_hash_algorithm); hashAlgorithm != "" {
369 avb_add_hashtree_footer_args += " --hash_algorithm " + hashAlgorithm
370 }
Inseob Kim53391842024-03-29 17:44:07 +0900371 securityPatchKey := "com.android.build." + f.partitionName() + ".security_patch"
Seungjae Yooa30e4502023-11-09 14:55:44 +0900372 securityPatchValue := ctx.Config().PlatformSecurityPatch()
373 avb_add_hashtree_footer_args += " --prop " + securityPatchKey + ":" + securityPatchValue
Shikha Panware6f30632022-12-21 12:54:45 +0000374 addStr("avb_add_hashtree_footer_args", avb_add_hashtree_footer_args)
Jooyung Han65f402b2022-04-21 14:24:04 +0900375 addStr("avb_salt", f.salt())
Jiyong Park71baa762021-01-18 21:11:03 +0900376 }
377
Inseob Kimcc8e5362021-02-03 14:05:24 +0900378 if proptools.String(f.properties.File_contexts) != "" {
379 addPath("selinux_fc", f.buildFileContexts(ctx))
380 }
Jooyung Han65f402b2022-04-21 14:24:04 +0900381 if timestamp := proptools.String(f.properties.Fake_timestamp); timestamp != "" {
382 addStr("timestamp", timestamp)
383 }
384 if uuid := proptools.String(f.properties.Uuid); uuid != "" {
385 addStr("uuid", uuid)
386 addStr("hash_seed", uuid)
387 }
Jiyong Park72678312021-01-18 17:29:49 +0900388 propFile = android.PathForModuleOut(ctx, "prop").OutputPath
Cole Faustcec230a2024-03-07 15:51:12 -0800389 android.WriteFileRuleVerbatim(ctx, propFile, propFileString.String())
Jiyong Park72678312021-01-18 17:29:49 +0900390 return propFile, deps
391}
392
Jiyong Park837cdb22021-02-05 00:17:14 +0900393func (f *filesystem) buildCpioImage(ctx android.ModuleContext, compressed bool) android.OutputPath {
Jiyong Park11a65972021-02-01 21:09:38 +0900394 if proptools.Bool(f.properties.Use_avb) {
395 ctx.PropertyErrorf("use_avb", "signing compresed cpio image using avbtool is not supported."+
396 "Consider adding this to bootimg module and signing the entire boot image.")
397 }
398
Inseob Kimcc8e5362021-02-03 14:05:24 +0900399 if proptools.String(f.properties.File_contexts) != "" {
400 ctx.PropertyErrorf("file_contexts", "file_contexts is not supported for compressed cpio image.")
401 }
402
Cole Faust4a2a7c92024-03-12 12:44:40 -0700403 if f.properties.Include_make_built_files != "" {
404 ctx.PropertyErrorf("include_make_built_files", "include_make_built_files is not supported for compressed cpio image.")
405 }
406
Jiyong Park11a65972021-02-01 21:09:38 +0900407 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Cole Faust3b806d32024-03-11 15:15:03 -0700408 rebasedDir := rootDir
409 if f.properties.Base_dir != nil {
410 rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir)
411 }
412 builder := android.NewRuleBuilder(pctx, ctx)
413 // Wipe the root dir to get rid of leftover files from prior builds
414 builder.Command().Textf("rm -rf %s && mkdir -p %s", rootDir, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900415 specs := f.gatherFilteredPackagingSpecs(ctx)
416 f.entries = f.CopySpecsToDir(ctx, builder, specs, rebasedDir)
Cole Faust3b806d32024-03-11 15:15:03 -0700417
418 f.buildNonDepsFiles(ctx, builder, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900419 f.buildFsverityMetadataFiles(ctx, builder, specs, rootDir, rebasedDir)
Jiyong Park11a65972021-02-01 21:09:38 +0900420
421 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Jiyong Park837cdb22021-02-05 00:17:14 +0900422 cmd := builder.Command().
Jiyong Park11a65972021-02-01 21:09:38 +0900423 BuiltTool("mkbootfs").
Jiyong Park837cdb22021-02-05 00:17:14 +0900424 Text(rootDir.String()) // input directory
425 if compressed {
426 cmd.Text("|").
427 BuiltTool("lz4").
428 Flag("--favor-decSpeed"). // for faster boot
429 Flag("-12"). // maximum compression level
430 Flag("-l"). // legacy format for kernel
431 Text(">").Output(output)
432 } else {
433 cmd.Text(">").Output(output)
434 }
Jiyong Park11a65972021-02-01 21:09:38 +0900435
436 // rootDir is not deleted. Might be useful for quick inspection.
Jiyong Park837cdb22021-02-05 00:17:14 +0900437 builder.Build("build_cpio_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park11a65972021-02-01 21:09:38 +0900438
439 return output
440}
441
Cole Faust4a2a7c92024-03-12 12:44:40 -0700442var validPartitions = []string{
443 "system",
444 "userdata",
445 "cache",
446 "system_other",
447 "vendor",
448 "product",
449 "system_ext",
450 "odm",
451 "vendor_dlkm",
452 "odm_dlkm",
453 "system_dlkm",
454}
455
456func (f *filesystem) addMakeBuiltFiles(ctx android.ModuleContext, builder *android.RuleBuilder, rootDir android.Path) {
457 partition := f.properties.Include_make_built_files
458 if partition == "" {
459 return
460 }
461 if !slices.Contains(validPartitions, partition) {
462 ctx.PropertyErrorf("include_make_built_files", "Expected one of %#v, found %q", validPartitions, partition)
463 return
464 }
465 stampFile := fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/staging_dir.stamp", ctx.Config().DeviceName(), partition)
466 fileListFile := fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partition)
467 stagingDir := fmt.Sprintf("target/product/%s/%s", ctx.Config().DeviceName(), partition)
468
469 builder.Command().BuiltTool("merge_directories").
470 Implicit(android.PathForArbitraryOutput(ctx, stampFile)).
471 Text("--ignore-duplicates").
472 FlagWithInput("--file-list", android.PathForArbitraryOutput(ctx, fileListFile)).
473 Text(rootDir.String()).
474 Text(android.PathForArbitraryOutput(ctx, stagingDir).String())
475}
476
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900477type partition interface {
478 PartitionType() string
479}
480
Cole Faust9a24d902024-03-18 15:38:12 -0700481func (f *filesystem) PartitionType() string {
482 return proptools.StringDefault(f.properties.Partition_type, "system")
483}
484
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900485var _ partition = (*filesystem)(nil)
486
Jiyong Park65c49f52020-11-24 14:23:26 +0900487var _ android.AndroidMkEntriesProvider = (*filesystem)(nil)
488
489// Implements android.AndroidMkEntriesProvider
490func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries {
491 return []android.AndroidMkEntries{android.AndroidMkEntries{
492 Class: "ETC",
493 OutputFile: android.OptionalPathForPath(f.output),
494 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700495 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800496 entries.SetString("LOCAL_MODULE_PATH", f.installDir.String())
Jiyong Park65c49f52020-11-24 14:23:26 +0900497 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
498 },
499 },
500 }}
Jiyong Park6f0f6882020-11-12 13:14:30 +0900501}
Jiyong Park12a719c2021-01-07 15:31:24 +0900502
Jiyong Park940dfd42021-02-04 15:37:34 +0900503var _ android.OutputFileProducer = (*filesystem)(nil)
504
505// Implements android.OutputFileProducer
506func (f *filesystem) OutputFiles(tag string) (android.Paths, error) {
507 if tag == "" {
508 return []android.Path{f.output}, nil
509 }
510 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
511}
512
Jiyong Park12a719c2021-01-07 15:31:24 +0900513// Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex
514// package to have access to the output file.
515type Filesystem interface {
516 android.Module
517 OutputPath() android.Path
Jiyong Park972e06c2021-03-15 23:32:49 +0900518
519 // Returns the output file that is signed by avbtool. If this module is not signed, returns
520 // nil.
521 SignedOutputPath() android.Path
Jiyong Park12a719c2021-01-07 15:31:24 +0900522}
523
524var _ Filesystem = (*filesystem)(nil)
525
526func (f *filesystem) OutputPath() android.Path {
527 return f.output
528}
Jiyong Park972e06c2021-03-15 23:32:49 +0900529
530func (f *filesystem) SignedOutputPath() android.Path {
531 if proptools.Bool(f.properties.Use_avb) {
532 return f.OutputPath()
533 }
534 return nil
535}
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900536
537// Filter the result of GatherPackagingSpecs to discard items targeting outside "system" partition.
538// Note that "apex" module installs its contents to "apex"(fake partition) as well
539// for symbol lookup by imitating "activated" paths.
540func (f *filesystem) gatherFilteredPackagingSpecs(ctx android.ModuleContext) map[string]android.PackagingSpec {
Jeongik Cha54bf8752024-02-08 10:44:37 +0900541 specs := f.PackagingBase.GatherPackagingSpecsWithFilter(ctx, f.filterPackagingSpec)
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900542 return specs
543}
Jooyung Han65f402b2022-04-21 14:24:04 +0900544
545func sha1sum(values []string) string {
546 h := sha256.New()
547 for _, value := range values {
548 io.WriteString(h, value)
549 }
550 return fmt.Sprintf("%x", h.Sum(nil))
551}
Jooyung Hane6067592023-03-16 13:11:17 +0900552
553// Base cc.UseCoverage
554
555var _ cc.UseCoverage = (*filesystem)(nil)
556
Colin Crossf5f4ad32024-01-19 15:41:48 -0800557func (*filesystem) IsNativeCoverageNeeded(ctx android.IncomingTransitionContext) bool {
Jooyung Hane6067592023-03-16 13:11:17 +0900558 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
559}
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900560
561// android_filesystem_defaults
562
563type filesystemDefaults struct {
564 android.ModuleBase
565 android.DefaultsModuleBase
566
567 properties filesystemDefaultsProperties
568}
569
570type filesystemDefaultsProperties struct {
571 // Identifies which partition this is for //visibility:any_system_image (and others) visibility
572 // checks, and will be used in the future for API surface checks.
573 Partition_type *string
574}
575
576// android_filesystem_defaults is a default module for android_filesystem and android_system_image
577func filesystemDefaultsFactory() android.Module {
578 module := &filesystemDefaults{}
579 module.AddProperties(&module.properties)
580 module.AddProperties(&android.PackagingProperties{})
581 android.InitDefaultsModule(module)
582 return module
583}
584
585func (f *filesystemDefaults) PartitionType() string {
586 return proptools.StringDefault(f.properties.Partition_type, "system")
587}
588
589var _ partition = (*filesystemDefaults)(nil)
590
591func (f *filesystemDefaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
592 validatePartitionType(ctx, f)
593}