blob: 61127bf16a4e1b70fd59bb9930cf981862cb1f39 [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 Parkfa616132021-04-20 11:36:40 +090039 ctx.RegisterModuleType("android_system_image", systemImageFactory)
Jiyong Parkeaac8232024-03-31 21:27:45 +090040 ctx.RegisterModuleType("android_system_image_defaults", systemImageDefaultsFactory)
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 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
Nikita Ioffe2c8cdc62024-03-27 22:19:30 +000086 // The index used to prevent rollback of the image. Only used if use_avb is true.
87 Rollback_index *int64
88
Jiyong Parkac4076d2021-03-15 23:21:30 +090089 // Name of the partition stored in vbmeta desc. Defaults to the name of this module.
90 Partition_name *string
91
Jiyong Park837cdb22021-02-05 00:17:14 +090092 // Type of the filesystem. Currently, ext4, cpio, and compressed_cpio are supported. Default
93 // is ext4.
Jiyong Park11a65972021-02-01 21:09:38 +090094 Type *string
Inseob Kimcc8e5362021-02-03 14:05:24 +090095
Cole Faust9a24d902024-03-18 15:38:12 -070096 // Identifies which partition this is for //visibility:any_system_image (and others) visibility
97 // checks, and will be used in the future for API surface checks.
98 Partition_type *string
99
Inseob Kimcc8e5362021-02-03 14:05:24 +0900100 // file_contexts file to make image. Currently, only ext4 is supported.
101 File_contexts *string `android:"path"`
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900102
103 // Base directory relative to root, to which deps are installed, e.g. "system". Default is "."
104 // (root).
105 Base_dir *string
Inseob Kim14199b02021-02-09 21:18:31 +0900106
107 // Directories to be created under root. e.g. /dev, /proc, etc.
108 Dirs []string
109
110 // Symbolic links to be created under root with "ln -sf <target> <name>".
111 Symlinks []symlinkDefinition
Jooyung Han65f402b2022-04-21 14:24:04 +0900112
113 // Seconds since unix epoch to override timestamps of file entries
114 Fake_timestamp *string
115
116 // When set, passed to mkuserimg_mke2fs --mke2fs_uuid & --mke2fs_hash_seed.
117 // Otherwise, they'll be set as random which might cause indeterministic build output.
118 Uuid *string
Inseob Kim376d72f2023-11-01 15:40:25 +0900119
120 // Mount point for this image. Default is "/"
121 Mount_point *string
Cole Faust4a2a7c92024-03-12 12:44:40 -0700122
123 // If set to the name of a partition ("system", "vendor", etc), this filesystem module
124 // will also include the contents of the make-built staging directories. If any soong
125 // modules would be installed to the same location as a make module, they will overwrite
126 // the make version.
127 Include_make_built_files string
Inseob Kim53391842024-03-29 17:44:07 +0900128
129 Fsverity fsverityProperties
Jiyong Park71baa762021-01-18 21:11:03 +0900130}
131
Jiyong Park65c49f52020-11-24 14:23:26 +0900132// android_filesystem packages a set of modules and their transitive dependencies into a filesystem
133// image. The filesystem images are expected to be mounted in the target device, which means the
134// modules in the filesystem image are built for the target device (i.e. Android, not Linux host).
135// The modules are placed in the filesystem image just like they are installed to the ordinary
136// partitions like system.img. For example, cc_library modules are placed under ./lib[64] directory.
Jiyong Park6f0f6882020-11-12 13:14:30 +0900137func filesystemFactory() android.Module {
138 module := &filesystem{}
Jiyong Parkfa616132021-04-20 11:36:40 +0900139 initFilesystemModule(module)
140 return module
141}
142
143func initFilesystemModule(module *filesystem) {
Jiyong Park71baa762021-01-18 21:11:03 +0900144 module.AddProperties(&module.properties)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900145 android.InitPackageModule(module)
146 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900147}
148
Jiyong Park12a719c2021-01-07 15:31:24 +0900149var dependencyTag = struct {
150 blueprint.BaseDependencyTag
Jooyung Han092ef812021-03-10 15:40:34 +0900151 android.PackagingItemAlwaysDepTag
Jiyong Park12a719c2021-01-07 15:31:24 +0900152}{}
Jiyong Park65b62242020-11-25 12:44:59 +0900153
Jiyong Park6f0f6882020-11-12 13:14:30 +0900154func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park65b62242020-11-25 12:44:59 +0900155 f.AddDeps(ctx, dependencyTag)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900156}
157
Jiyong Park11a65972021-02-01 21:09:38 +0900158type fsType int
159
160const (
161 ext4Type fsType = iota
162 compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900163 cpioType // uncompressed
Jiyong Park11a65972021-02-01 21:09:38 +0900164 unknown
165)
166
167func (f *filesystem) fsType(ctx android.ModuleContext) fsType {
168 typeStr := proptools.StringDefault(f.properties.Type, "ext4")
169 switch typeStr {
170 case "ext4":
171 return ext4Type
172 case "compressed_cpio":
173 return compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900174 case "cpio":
175 return cpioType
Jiyong Park11a65972021-02-01 21:09:38 +0900176 default:
177 ctx.PropertyErrorf("type", "%q not supported", typeStr)
178 return unknown
179 }
180}
181
Jiyong Park65c49f52020-11-24 14:23:26 +0900182func (f *filesystem) installFileName() string {
183 return f.BaseModuleName() + ".img"
184}
185
Inseob Kim53391842024-03-29 17:44:07 +0900186func (f *filesystem) partitionName() string {
187 return proptools.StringDefault(f.properties.Partition_name, f.Name())
188}
189
Jiyong Park6f0f6882020-11-12 13:14:30 +0900190var pctx = android.NewPackageContext("android/soong/filesystem")
191
192func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust9a24d902024-03-18 15:38:12 -0700193 if !android.InList(f.PartitionType(), validPartitions) {
194 ctx.PropertyErrorf("partition_type", "partition_type must be one of %s, found: %s", validPartitions, f.PartitionType())
195 }
Jiyong Park11a65972021-02-01 21:09:38 +0900196 switch f.fsType(ctx) {
197 case ext4Type:
198 f.output = f.buildImageUsingBuildImage(ctx)
199 case compressedCpioType:
Jiyong Park837cdb22021-02-05 00:17:14 +0900200 f.output = f.buildCpioImage(ctx, true)
201 case cpioType:
202 f.output = f.buildCpioImage(ctx, false)
Jiyong Park11a65972021-02-01 21:09:38 +0900203 default:
204 return
205 }
206
207 f.installDir = android.PathForModuleInstall(ctx, "etc")
208 ctx.InstallFile(f.installDir, f.installFileName(), f.output)
209}
210
Cole Faust3b806d32024-03-11 15:15:03 -0700211// Copy extra files/dirs that are not from the `deps` property to `rootDir`, checking for conflicts with files
212// already in `rootDir`.
213func (f *filesystem) buildNonDepsFiles(ctx android.ModuleContext, builder *android.RuleBuilder, rootDir android.OutputPath) {
Inseob Kim14199b02021-02-09 21:18:31 +0900214 // create dirs and symlinks
215 for _, dir := range f.properties.Dirs {
216 // OutputPath.Join verifies dir
217 builder.Command().Text("mkdir -p").Text(rootDir.Join(ctx, dir).String())
218 }
219
220 for _, symlink := range f.properties.Symlinks {
221 name := strings.TrimSpace(proptools.String(symlink.Name))
222 target := strings.TrimSpace(proptools.String(symlink.Target))
223
224 if name == "" {
225 ctx.PropertyErrorf("symlinks", "Name can't be empty")
226 continue
227 }
228
229 if target == "" {
230 ctx.PropertyErrorf("symlinks", "Target can't be empty")
231 continue
232 }
233
234 // OutputPath.Join verifies name. don't need to verify target.
235 dst := rootDir.Join(ctx, name)
Cole Faust3b806d32024-03-11 15:15:03 -0700236 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 +0900237 builder.Command().Text("mkdir -p").Text(filepath.Dir(dst.String()))
238 builder.Command().Text("ln -sf").Text(proptools.ShellEscape(target)).Text(dst.String())
239 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900240
Jiyong Parkfa616132021-04-20 11:36:40 +0900241 // create extra files if there's any
Jiyong Parkfa616132021-04-20 11:36:40 +0900242 if f.buildExtraFiles != nil {
Cole Faust4a2a7c92024-03-12 12:44:40 -0700243 rootForExtraFiles := android.PathForModuleGen(ctx, "root-extra").OutputPath
244 extraFiles := f.buildExtraFiles(ctx, rootForExtraFiles)
245 for _, f := range extraFiles {
246 rel, err := filepath.Rel(rootForExtraFiles.String(), f.String())
247 if err != nil || strings.HasPrefix(rel, "..") {
248 ctx.ModuleErrorf("can't make %q relative to %q", f, rootForExtraFiles)
249 }
Jiyong Parkfa616132021-04-20 11:36:40 +0900250 }
Cole Faust4a2a7c92024-03-12 12:44:40 -0700251 if len(extraFiles) > 0 {
252 builder.Command().BuiltTool("merge_directories").
253 Implicits(extraFiles.Paths()).
254 Text(rootDir.String()).
255 Text(rootForExtraFiles.String())
256 }
Jiyong Parkfa616132021-04-20 11:36:40 +0900257 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900258}
259
Jiyong Park11a65972021-02-01 21:09:38 +0900260func (f *filesystem) buildImageUsingBuildImage(ctx android.ModuleContext) android.OutputPath {
Jiyong Park6f0f6882020-11-12 13:14:30 +0900261 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Cole Faust3b806d32024-03-11 15:15:03 -0700262 rebasedDir := rootDir
263 if f.properties.Base_dir != nil {
264 rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir)
265 }
266 builder := android.NewRuleBuilder(pctx, ctx)
267 // Wipe the root dir to get rid of leftover files from prior builds
268 builder.Command().Textf("rm -rf %s && mkdir -p %s", rootDir, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900269 specs := f.gatherFilteredPackagingSpecs(ctx)
270 f.entries = f.CopySpecsToDir(ctx, builder, specs, rebasedDir)
Cole Faust3b806d32024-03-11 15:15:03 -0700271
272 f.buildNonDepsFiles(ctx, builder, rootDir)
Cole Faust4a2a7c92024-03-12 12:44:40 -0700273 f.addMakeBuiltFiles(ctx, builder, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900274 f.buildFsverityMetadataFiles(ctx, builder, specs, rootDir, rebasedDir)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900275
Nikita Ioffe519015f2022-12-23 15:36:29 +0000276 // run host_init_verifier
277 // Ideally we should have a concept of pluggable linters that verify the generated image.
278 // While such concept is not implement this will do.
279 // TODO(b/263574231): substitute with pluggable linter.
280 builder.Command().
281 BuiltTool("host_init_verifier").
282 FlagWithArg("--out_system=", rootDir.String()+"/system")
283
Jiyong Park72678312021-01-18 17:29:49 +0900284 propFile, toolDeps := f.buildPropFile(ctx)
Jiyong Park11a65972021-02-01 21:09:38 +0900285 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -0800286 builder.Command().BuiltTool("build_image").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900287 Text(rootDir.String()). // input directory
288 Input(propFile).
Jiyong Park72678312021-01-18 17:29:49 +0900289 Implicits(toolDeps).
Jiyong Park11a65972021-02-01 21:09:38 +0900290 Output(output).
Jiyong Park6f0f6882020-11-12 13:14:30 +0900291 Text(rootDir.String()) // directory where to find fs_config_files|dirs
292
293 // rootDir is not deleted. Might be useful for quick inspection.
Colin Crossf1a035e2020-11-16 17:32:30 -0800294 builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park65c49f52020-11-24 14:23:26 +0900295
Jiyong Park11a65972021-02-01 21:09:38 +0900296 return output
Jiyong Park65c49f52020-11-24 14:23:26 +0900297}
298
Inseob Kimcc8e5362021-02-03 14:05:24 +0900299func (f *filesystem) buildFileContexts(ctx android.ModuleContext) android.OutputPath {
300 builder := android.NewRuleBuilder(pctx, ctx)
301 fcBin := android.PathForModuleOut(ctx, "file_contexts.bin")
302 builder.Command().BuiltTool("sefcontext_compile").
303 FlagWithOutput("-o ", fcBin).
304 Input(android.PathForModuleSrc(ctx, proptools.String(f.properties.File_contexts)))
305 builder.Build("build_filesystem_file_contexts", fmt.Sprintf("Creating filesystem file contexts for %s", f.BaseModuleName()))
306 return fcBin.OutputPath
307}
308
Jooyung Han65f402b2022-04-21 14:24:04 +0900309// Calculates avb_salt from entry list (sorted) for deterministic output.
310func (f *filesystem) salt() string {
311 return sha1sum(f.entries)
312}
313
Jiyong Park72678312021-01-18 17:29:49 +0900314func (f *filesystem) buildPropFile(ctx android.ModuleContext) (propFile android.OutputPath, toolDeps android.Paths) {
Jiyong Park72678312021-01-18 17:29:49 +0900315 var deps android.Paths
Cole Faustcec230a2024-03-07 15:51:12 -0800316 var propFileString strings.Builder
Jiyong Park72678312021-01-18 17:29:49 +0900317 addStr := func(name string, value string) {
Cole Faustcec230a2024-03-07 15:51:12 -0800318 propFileString.WriteString(name)
319 propFileString.WriteRune('=')
320 propFileString.WriteString(value)
321 propFileString.WriteRune('\n')
Jiyong Park72678312021-01-18 17:29:49 +0900322 }
323 addPath := func(name string, path android.Path) {
Cole Faustcec230a2024-03-07 15:51:12 -0800324 addStr(name, path.String())
Jiyong Park72678312021-01-18 17:29:49 +0900325 deps = append(deps, path)
326 }
327
Jiyong Park11a65972021-02-01 21:09:38 +0900328 // Type string that build_image.py accepts.
329 fsTypeStr := func(t fsType) string {
330 switch t {
331 // TODO(jiyong): add more types like f2fs, erofs, etc.
332 case ext4Type:
333 return "ext4"
334 }
335 panic(fmt.Errorf("unsupported fs type %v", t))
336 }
337
338 addStr("fs_type", fsTypeStr(f.fsType(ctx)))
Inseob Kim376d72f2023-11-01 15:40:25 +0900339 addStr("mount_point", proptools.StringDefault(f.properties.Mount_point, "/"))
Jiyong Park72678312021-01-18 17:29:49 +0900340 addStr("use_dynamic_partition_size", "true")
341 addPath("ext_mkuserimg", ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs"))
342 // b/177813163 deps of the host tools have to be added. Remove this.
343 for _, t := range []string{"mke2fs", "e2fsdroid", "tune2fs"} {
344 deps = append(deps, ctx.Config().HostToolPath(ctx, t))
345 }
346
Jiyong Park71baa762021-01-18 21:11:03 +0900347 if proptools.Bool(f.properties.Use_avb) {
348 addStr("avb_hashtree_enable", "true")
349 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
350 algorithm := proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096")
351 addStr("avb_algorithm", algorithm)
352 key := android.PathForModuleSrc(ctx, proptools.String(f.properties.Avb_private_key))
353 addPath("avb_key_path", key)
Inseob Kim53391842024-03-29 17:44:07 +0900354 addStr("partition_name", f.partitionName())
Shikha Panware6f30632022-12-21 12:54:45 +0000355 avb_add_hashtree_footer_args := "--do_not_generate_fec"
356 if hashAlgorithm := proptools.String(f.properties.Avb_hash_algorithm); hashAlgorithm != "" {
357 avb_add_hashtree_footer_args += " --hash_algorithm " + hashAlgorithm
358 }
Nikita Ioffe2c8cdc62024-03-27 22:19:30 +0000359 if f.properties.Rollback_index != nil {
360 rollbackIndex := proptools.Int(f.properties.Rollback_index)
361 if rollbackIndex < 0 {
362 ctx.PropertyErrorf("rollback_index", "Rollback index must be non-negative")
363 }
364 avb_add_hashtree_footer_args += " --rollback_index " + strconv.Itoa(rollbackIndex)
365 }
Inseob Kim53391842024-03-29 17:44:07 +0900366 securityPatchKey := "com.android.build." + f.partitionName() + ".security_patch"
Seungjae Yooa30e4502023-11-09 14:55:44 +0900367 securityPatchValue := ctx.Config().PlatformSecurityPatch()
368 avb_add_hashtree_footer_args += " --prop " + securityPatchKey + ":" + securityPatchValue
Shikha Panware6f30632022-12-21 12:54:45 +0000369 addStr("avb_add_hashtree_footer_args", avb_add_hashtree_footer_args)
Jooyung Han65f402b2022-04-21 14:24:04 +0900370 addStr("avb_salt", f.salt())
Jiyong Park71baa762021-01-18 21:11:03 +0900371 }
372
Inseob Kimcc8e5362021-02-03 14:05:24 +0900373 if proptools.String(f.properties.File_contexts) != "" {
374 addPath("selinux_fc", f.buildFileContexts(ctx))
375 }
Jooyung Han65f402b2022-04-21 14:24:04 +0900376 if timestamp := proptools.String(f.properties.Fake_timestamp); timestamp != "" {
377 addStr("timestamp", timestamp)
378 }
379 if uuid := proptools.String(f.properties.Uuid); uuid != "" {
380 addStr("uuid", uuid)
381 addStr("hash_seed", uuid)
382 }
Jiyong Park72678312021-01-18 17:29:49 +0900383 propFile = android.PathForModuleOut(ctx, "prop").OutputPath
Cole Faustcec230a2024-03-07 15:51:12 -0800384 android.WriteFileRuleVerbatim(ctx, propFile, propFileString.String())
Jiyong Park72678312021-01-18 17:29:49 +0900385 return propFile, deps
386}
387
Jiyong Park837cdb22021-02-05 00:17:14 +0900388func (f *filesystem) buildCpioImage(ctx android.ModuleContext, compressed bool) android.OutputPath {
Jiyong Park11a65972021-02-01 21:09:38 +0900389 if proptools.Bool(f.properties.Use_avb) {
390 ctx.PropertyErrorf("use_avb", "signing compresed cpio image using avbtool is not supported."+
391 "Consider adding this to bootimg module and signing the entire boot image.")
392 }
393
Inseob Kimcc8e5362021-02-03 14:05:24 +0900394 if proptools.String(f.properties.File_contexts) != "" {
395 ctx.PropertyErrorf("file_contexts", "file_contexts is not supported for compressed cpio image.")
396 }
397
Cole Faust4a2a7c92024-03-12 12:44:40 -0700398 if f.properties.Include_make_built_files != "" {
399 ctx.PropertyErrorf("include_make_built_files", "include_make_built_files is not supported for compressed cpio image.")
400 }
401
Jiyong Park11a65972021-02-01 21:09:38 +0900402 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Cole Faust3b806d32024-03-11 15:15:03 -0700403 rebasedDir := rootDir
404 if f.properties.Base_dir != nil {
405 rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir)
406 }
407 builder := android.NewRuleBuilder(pctx, ctx)
408 // Wipe the root dir to get rid of leftover files from prior builds
409 builder.Command().Textf("rm -rf %s && mkdir -p %s", rootDir, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900410 specs := f.gatherFilteredPackagingSpecs(ctx)
411 f.entries = f.CopySpecsToDir(ctx, builder, specs, rebasedDir)
Cole Faust3b806d32024-03-11 15:15:03 -0700412
413 f.buildNonDepsFiles(ctx, builder, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900414 f.buildFsverityMetadataFiles(ctx, builder, specs, rootDir, rebasedDir)
Jiyong Park11a65972021-02-01 21:09:38 +0900415
416 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Jiyong Park837cdb22021-02-05 00:17:14 +0900417 cmd := builder.Command().
Jiyong Park11a65972021-02-01 21:09:38 +0900418 BuiltTool("mkbootfs").
Jiyong Park837cdb22021-02-05 00:17:14 +0900419 Text(rootDir.String()) // input directory
420 if compressed {
421 cmd.Text("|").
422 BuiltTool("lz4").
423 Flag("--favor-decSpeed"). // for faster boot
424 Flag("-12"). // maximum compression level
425 Flag("-l"). // legacy format for kernel
426 Text(">").Output(output)
427 } else {
428 cmd.Text(">").Output(output)
429 }
Jiyong Park11a65972021-02-01 21:09:38 +0900430
431 // rootDir is not deleted. Might be useful for quick inspection.
Jiyong Park837cdb22021-02-05 00:17:14 +0900432 builder.Build("build_cpio_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park11a65972021-02-01 21:09:38 +0900433
434 return output
435}
436
Cole Faust4a2a7c92024-03-12 12:44:40 -0700437var validPartitions = []string{
438 "system",
439 "userdata",
440 "cache",
441 "system_other",
442 "vendor",
443 "product",
444 "system_ext",
445 "odm",
446 "vendor_dlkm",
447 "odm_dlkm",
448 "system_dlkm",
449}
450
451func (f *filesystem) addMakeBuiltFiles(ctx android.ModuleContext, builder *android.RuleBuilder, rootDir android.Path) {
452 partition := f.properties.Include_make_built_files
453 if partition == "" {
454 return
455 }
456 if !slices.Contains(validPartitions, partition) {
457 ctx.PropertyErrorf("include_make_built_files", "Expected one of %#v, found %q", validPartitions, partition)
458 return
459 }
460 stampFile := fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/staging_dir.stamp", ctx.Config().DeviceName(), partition)
461 fileListFile := fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partition)
462 stagingDir := fmt.Sprintf("target/product/%s/%s", ctx.Config().DeviceName(), partition)
463
464 builder.Command().BuiltTool("merge_directories").
465 Implicit(android.PathForArbitraryOutput(ctx, stampFile)).
466 Text("--ignore-duplicates").
467 FlagWithInput("--file-list", android.PathForArbitraryOutput(ctx, fileListFile)).
468 Text(rootDir.String()).
469 Text(android.PathForArbitraryOutput(ctx, stagingDir).String())
470}
471
Cole Faust9a24d902024-03-18 15:38:12 -0700472func (f *filesystem) PartitionType() string {
473 return proptools.StringDefault(f.properties.Partition_type, "system")
474}
475
Jiyong Park65c49f52020-11-24 14:23:26 +0900476var _ android.AndroidMkEntriesProvider = (*filesystem)(nil)
477
478// Implements android.AndroidMkEntriesProvider
479func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries {
480 return []android.AndroidMkEntries{android.AndroidMkEntries{
481 Class: "ETC",
482 OutputFile: android.OptionalPathForPath(f.output),
483 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700484 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800485 entries.SetString("LOCAL_MODULE_PATH", f.installDir.String())
Jiyong Park65c49f52020-11-24 14:23:26 +0900486 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
487 },
488 },
489 }}
Jiyong Park6f0f6882020-11-12 13:14:30 +0900490}
Jiyong Park12a719c2021-01-07 15:31:24 +0900491
Jiyong Park940dfd42021-02-04 15:37:34 +0900492var _ android.OutputFileProducer = (*filesystem)(nil)
493
494// Implements android.OutputFileProducer
495func (f *filesystem) OutputFiles(tag string) (android.Paths, error) {
496 if tag == "" {
497 return []android.Path{f.output}, nil
498 }
499 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
500}
501
Jiyong Park12a719c2021-01-07 15:31:24 +0900502// Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex
503// package to have access to the output file.
504type Filesystem interface {
505 android.Module
506 OutputPath() android.Path
Jiyong Park972e06c2021-03-15 23:32:49 +0900507
508 // Returns the output file that is signed by avbtool. If this module is not signed, returns
509 // nil.
510 SignedOutputPath() android.Path
Jiyong Park12a719c2021-01-07 15:31:24 +0900511}
512
513var _ Filesystem = (*filesystem)(nil)
514
515func (f *filesystem) OutputPath() android.Path {
516 return f.output
517}
Jiyong Park972e06c2021-03-15 23:32:49 +0900518
519func (f *filesystem) SignedOutputPath() android.Path {
520 if proptools.Bool(f.properties.Use_avb) {
521 return f.OutputPath()
522 }
523 return nil
524}
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900525
526// Filter the result of GatherPackagingSpecs to discard items targeting outside "system" partition.
527// Note that "apex" module installs its contents to "apex"(fake partition) as well
528// for symbol lookup by imitating "activated" paths.
529func (f *filesystem) gatherFilteredPackagingSpecs(ctx android.ModuleContext) map[string]android.PackagingSpec {
Jeongik Cha54bf8752024-02-08 10:44:37 +0900530 specs := f.PackagingBase.GatherPackagingSpecsWithFilter(ctx, f.filterPackagingSpec)
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900531 return specs
532}
Jooyung Han65f402b2022-04-21 14:24:04 +0900533
534func sha1sum(values []string) string {
535 h := sha256.New()
536 for _, value := range values {
537 io.WriteString(h, value)
538 }
539 return fmt.Sprintf("%x", h.Sum(nil))
540}
Jooyung Hane6067592023-03-16 13:11:17 +0900541
542// Base cc.UseCoverage
543
544var _ cc.UseCoverage = (*filesystem)(nil)
545
Colin Crossf5f4ad32024-01-19 15:41:48 -0800546func (*filesystem) IsNativeCoverageNeeded(ctx android.IncomingTransitionContext) bool {
Jooyung Hane6067592023-03-16 13:11:17 +0900547 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
548}