blob: efc889ccb2d1b47c92bb355785fc35ed1ded9867 [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 Parkfa616132021-04-20 11:36:40 +090038 ctx.RegisterModuleType("android_system_image", systemImageFactory)
Jiyong Parkbc485482022-11-15 22:31:49 +090039 ctx.RegisterModuleType("avb_add_hash_footer", avbAddHashFooterFactory)
Inseob Kim87230e62023-11-22 18:55:07 +090040 ctx.RegisterModuleType("avb_add_hash_footer_defaults", avbAddHashFooterDefaultsFactory)
Alice Wang000e3a32023-01-03 16:11:20 +000041 ctx.RegisterModuleType("avb_gen_vbmeta_image", avbGenVbmetaImageFactory)
Inseob Kim87230e62023-11-22 18:55:07 +090042 ctx.RegisterModuleType("avb_gen_vbmeta_image_defaults", avbGenVbmetaImageDefaultsFactory)
Jiyong Park6f0f6882020-11-12 13:14:30 +090043}
44
45type filesystem struct {
46 android.ModuleBase
47 android.PackagingBase
Jiyong Park65c49f52020-11-24 14:23:26 +090048
Jiyong Park71baa762021-01-18 21:11:03 +090049 properties filesystemProperties
50
Jiyong Parkfa616132021-04-20 11:36:40 +090051 // Function that builds extra files under the root directory and returns the files
52 buildExtraFiles func(ctx android.ModuleContext, root android.OutputPath) android.OutputPaths
53
Jeongik Cha54bf8752024-02-08 10:44:37 +090054 // Function that filters PackagingSpec in PackagingBase.GatherPackagingSpecs()
55 filterPackagingSpec func(spec android.PackagingSpec) bool
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090056
Jiyong Park65c49f52020-11-24 14:23:26 +090057 output android.OutputPath
58 installDir android.InstallPath
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090059
60 // For testing. Keeps the result of CopyDepsToZip()
61 entries []string
Jiyong Park6f0f6882020-11-12 13:14:30 +090062}
63
Inseob Kim14199b02021-02-09 21:18:31 +090064type symlinkDefinition struct {
65 Target *string
66 Name *string
67}
68
Jiyong Park71baa762021-01-18 21:11:03 +090069type filesystemProperties struct {
70 // When set to true, sign the image with avbtool. Default is false.
71 Use_avb *bool
72
73 // Path to the private key that avbtool will use to sign this filesystem image.
74 // TODO(jiyong): allow apex_key to be specified here
75 Avb_private_key *string `android:"path"`
76
Shikha Panwar01403bb2022-12-22 12:22:57 +000077 // Signing algorithm for avbtool. Default is SHA256_RSA4096.
Jiyong Park71baa762021-01-18 21:11:03 +090078 Avb_algorithm *string
Jiyong Park11a65972021-02-01 21:09:38 +090079
Shikha Panwar01403bb2022-12-22 12:22:57 +000080 // Hash algorithm used for avbtool (for descriptors). This is passed as hash_algorithm to
81 // avbtool. Default used by avbtool is sha1.
Shikha Panware6f30632022-12-21 12:54:45 +000082 Avb_hash_algorithm *string
83
Jiyong Parkac4076d2021-03-15 23:21:30 +090084 // Name of the partition stored in vbmeta desc. Defaults to the name of this module.
85 Partition_name *string
86
Jiyong Park837cdb22021-02-05 00:17:14 +090087 // Type of the filesystem. Currently, ext4, cpio, and compressed_cpio are supported. Default
88 // is ext4.
Jiyong Park11a65972021-02-01 21:09:38 +090089 Type *string
Inseob Kimcc8e5362021-02-03 14:05:24 +090090
Cole Faust9a24d902024-03-18 15:38:12 -070091 // Identifies which partition this is for //visibility:any_system_image (and others) visibility
92 // checks, and will be used in the future for API surface checks.
93 Partition_type *string
94
Inseob Kimcc8e5362021-02-03 14:05:24 +090095 // file_contexts file to make image. Currently, only ext4 is supported.
96 File_contexts *string `android:"path"`
Inseob Kim2ce1b5d2021-02-15 17:01:04 +090097
98 // Base directory relative to root, to which deps are installed, e.g. "system". Default is "."
99 // (root).
100 Base_dir *string
Inseob Kim14199b02021-02-09 21:18:31 +0900101
102 // Directories to be created under root. e.g. /dev, /proc, etc.
103 Dirs []string
104
105 // Symbolic links to be created under root with "ln -sf <target> <name>".
106 Symlinks []symlinkDefinition
Jooyung Han65f402b2022-04-21 14:24:04 +0900107
108 // Seconds since unix epoch to override timestamps of file entries
109 Fake_timestamp *string
110
111 // When set, passed to mkuserimg_mke2fs --mke2fs_uuid & --mke2fs_hash_seed.
112 // Otherwise, they'll be set as random which might cause indeterministic build output.
113 Uuid *string
Inseob Kim376d72f2023-11-01 15:40:25 +0900114
115 // Mount point for this image. Default is "/"
116 Mount_point *string
Cole Faust4a2a7c92024-03-12 12:44:40 -0700117
118 // If set to the name of a partition ("system", "vendor", etc), this filesystem module
119 // will also include the contents of the make-built staging directories. If any soong
120 // modules would be installed to the same location as a make module, they will overwrite
121 // the make version.
122 Include_make_built_files string
Jiyong Park71baa762021-01-18 21:11:03 +0900123}
124
Jiyong Park65c49f52020-11-24 14:23:26 +0900125// android_filesystem packages a set of modules and their transitive dependencies into a filesystem
126// image. The filesystem images are expected to be mounted in the target device, which means the
127// modules in the filesystem image are built for the target device (i.e. Android, not Linux host).
128// The modules are placed in the filesystem image just like they are installed to the ordinary
129// partitions like system.img. For example, cc_library modules are placed under ./lib[64] directory.
Jiyong Park6f0f6882020-11-12 13:14:30 +0900130func filesystemFactory() android.Module {
131 module := &filesystem{}
Jiyong Parkfa616132021-04-20 11:36:40 +0900132 initFilesystemModule(module)
133 return module
134}
135
136func initFilesystemModule(module *filesystem) {
Jiyong Park71baa762021-01-18 21:11:03 +0900137 module.AddProperties(&module.properties)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900138 android.InitPackageModule(module)
139 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900140}
141
Jiyong Park12a719c2021-01-07 15:31:24 +0900142var dependencyTag = struct {
143 blueprint.BaseDependencyTag
Jooyung Han092ef812021-03-10 15:40:34 +0900144 android.PackagingItemAlwaysDepTag
Jiyong Park12a719c2021-01-07 15:31:24 +0900145}{}
Jiyong Park65b62242020-11-25 12:44:59 +0900146
Jiyong Park6f0f6882020-11-12 13:14:30 +0900147func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park65b62242020-11-25 12:44:59 +0900148 f.AddDeps(ctx, dependencyTag)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900149}
150
Jiyong Park11a65972021-02-01 21:09:38 +0900151type fsType int
152
153const (
154 ext4Type fsType = iota
155 compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900156 cpioType // uncompressed
Jiyong Park11a65972021-02-01 21:09:38 +0900157 unknown
158)
159
160func (f *filesystem) fsType(ctx android.ModuleContext) fsType {
161 typeStr := proptools.StringDefault(f.properties.Type, "ext4")
162 switch typeStr {
163 case "ext4":
164 return ext4Type
165 case "compressed_cpio":
166 return compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900167 case "cpio":
168 return cpioType
Jiyong Park11a65972021-02-01 21:09:38 +0900169 default:
170 ctx.PropertyErrorf("type", "%q not supported", typeStr)
171 return unknown
172 }
173}
174
Jiyong Park65c49f52020-11-24 14:23:26 +0900175func (f *filesystem) installFileName() string {
176 return f.BaseModuleName() + ".img"
177}
178
Jiyong Park6f0f6882020-11-12 13:14:30 +0900179var pctx = android.NewPackageContext("android/soong/filesystem")
180
181func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust9a24d902024-03-18 15:38:12 -0700182 if !android.InList(f.PartitionType(), validPartitions) {
183 ctx.PropertyErrorf("partition_type", "partition_type must be one of %s, found: %s", validPartitions, f.PartitionType())
184 }
Jiyong Park11a65972021-02-01 21:09:38 +0900185 switch f.fsType(ctx) {
186 case ext4Type:
187 f.output = f.buildImageUsingBuildImage(ctx)
188 case compressedCpioType:
Jiyong Park837cdb22021-02-05 00:17:14 +0900189 f.output = f.buildCpioImage(ctx, true)
190 case cpioType:
191 f.output = f.buildCpioImage(ctx, false)
Jiyong Park11a65972021-02-01 21:09:38 +0900192 default:
193 return
194 }
195
196 f.installDir = android.PathForModuleInstall(ctx, "etc")
197 ctx.InstallFile(f.installDir, f.installFileName(), f.output)
198}
199
Cole Faust3b806d32024-03-11 15:15:03 -0700200// Copy extra files/dirs that are not from the `deps` property to `rootDir`, checking for conflicts with files
201// already in `rootDir`.
202func (f *filesystem) buildNonDepsFiles(ctx android.ModuleContext, builder *android.RuleBuilder, rootDir android.OutputPath) {
Inseob Kim14199b02021-02-09 21:18:31 +0900203 // create dirs and symlinks
204 for _, dir := range f.properties.Dirs {
205 // OutputPath.Join verifies dir
206 builder.Command().Text("mkdir -p").Text(rootDir.Join(ctx, dir).String())
207 }
208
209 for _, symlink := range f.properties.Symlinks {
210 name := strings.TrimSpace(proptools.String(symlink.Name))
211 target := strings.TrimSpace(proptools.String(symlink.Target))
212
213 if name == "" {
214 ctx.PropertyErrorf("symlinks", "Name can't be empty")
215 continue
216 }
217
218 if target == "" {
219 ctx.PropertyErrorf("symlinks", "Target can't be empty")
220 continue
221 }
222
223 // OutputPath.Join verifies name. don't need to verify target.
224 dst := rootDir.Join(ctx, name)
Cole Faust3b806d32024-03-11 15:15:03 -0700225 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 +0900226 builder.Command().Text("mkdir -p").Text(filepath.Dir(dst.String()))
227 builder.Command().Text("ln -sf").Text(proptools.ShellEscape(target)).Text(dst.String())
228 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900229
Jiyong Parkfa616132021-04-20 11:36:40 +0900230 // create extra files if there's any
Jiyong Parkfa616132021-04-20 11:36:40 +0900231 if f.buildExtraFiles != nil {
Cole Faust4a2a7c92024-03-12 12:44:40 -0700232 rootForExtraFiles := android.PathForModuleGen(ctx, "root-extra").OutputPath
233 extraFiles := f.buildExtraFiles(ctx, rootForExtraFiles)
234 for _, f := range extraFiles {
235 rel, err := filepath.Rel(rootForExtraFiles.String(), f.String())
236 if err != nil || strings.HasPrefix(rel, "..") {
237 ctx.ModuleErrorf("can't make %q relative to %q", f, rootForExtraFiles)
238 }
Jiyong Parkfa616132021-04-20 11:36:40 +0900239 }
Cole Faust4a2a7c92024-03-12 12:44:40 -0700240 if len(extraFiles) > 0 {
241 builder.Command().BuiltTool("merge_directories").
242 Implicits(extraFiles.Paths()).
243 Text(rootDir.String()).
244 Text(rootForExtraFiles.String())
245 }
Jiyong Parkfa616132021-04-20 11:36:40 +0900246 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900247}
248
Jiyong Park11a65972021-02-01 21:09:38 +0900249func (f *filesystem) buildImageUsingBuildImage(ctx android.ModuleContext) android.OutputPath {
Jiyong Park6f0f6882020-11-12 13:14:30 +0900250 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Cole Faust3b806d32024-03-11 15:15:03 -0700251 rebasedDir := rootDir
252 if f.properties.Base_dir != nil {
253 rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir)
254 }
255 builder := android.NewRuleBuilder(pctx, ctx)
256 // Wipe the root dir to get rid of leftover files from prior builds
257 builder.Command().Textf("rm -rf %s && mkdir -p %s", rootDir, rootDir)
258 f.entries = f.CopySpecsToDir(ctx, builder, f.gatherFilteredPackagingSpecs(ctx), rebasedDir)
259
260 f.buildNonDepsFiles(ctx, builder, rootDir)
Cole Faust4a2a7c92024-03-12 12:44:40 -0700261 f.addMakeBuiltFiles(ctx, builder, rootDir)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900262
Nikita Ioffe519015f2022-12-23 15:36:29 +0000263 // run host_init_verifier
264 // Ideally we should have a concept of pluggable linters that verify the generated image.
265 // While such concept is not implement this will do.
266 // TODO(b/263574231): substitute with pluggable linter.
267 builder.Command().
268 BuiltTool("host_init_verifier").
269 FlagWithArg("--out_system=", rootDir.String()+"/system")
270
Jiyong Park72678312021-01-18 17:29:49 +0900271 propFile, toolDeps := f.buildPropFile(ctx)
Jiyong Park11a65972021-02-01 21:09:38 +0900272 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -0800273 builder.Command().BuiltTool("build_image").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900274 Text(rootDir.String()). // input directory
275 Input(propFile).
Jiyong Park72678312021-01-18 17:29:49 +0900276 Implicits(toolDeps).
Jiyong Park11a65972021-02-01 21:09:38 +0900277 Output(output).
Jiyong Park6f0f6882020-11-12 13:14:30 +0900278 Text(rootDir.String()) // directory where to find fs_config_files|dirs
279
280 // rootDir is not deleted. Might be useful for quick inspection.
Colin Crossf1a035e2020-11-16 17:32:30 -0800281 builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park65c49f52020-11-24 14:23:26 +0900282
Jiyong Park11a65972021-02-01 21:09:38 +0900283 return output
Jiyong Park65c49f52020-11-24 14:23:26 +0900284}
285
Inseob Kimcc8e5362021-02-03 14:05:24 +0900286func (f *filesystem) buildFileContexts(ctx android.ModuleContext) android.OutputPath {
287 builder := android.NewRuleBuilder(pctx, ctx)
288 fcBin := android.PathForModuleOut(ctx, "file_contexts.bin")
289 builder.Command().BuiltTool("sefcontext_compile").
290 FlagWithOutput("-o ", fcBin).
291 Input(android.PathForModuleSrc(ctx, proptools.String(f.properties.File_contexts)))
292 builder.Build("build_filesystem_file_contexts", fmt.Sprintf("Creating filesystem file contexts for %s", f.BaseModuleName()))
293 return fcBin.OutputPath
294}
295
Jooyung Han65f402b2022-04-21 14:24:04 +0900296// Calculates avb_salt from entry list (sorted) for deterministic output.
297func (f *filesystem) salt() string {
298 return sha1sum(f.entries)
299}
300
Jiyong Park72678312021-01-18 17:29:49 +0900301func (f *filesystem) buildPropFile(ctx android.ModuleContext) (propFile android.OutputPath, toolDeps android.Paths) {
Jiyong Park72678312021-01-18 17:29:49 +0900302 var deps android.Paths
Cole Faustcec230a2024-03-07 15:51:12 -0800303 var propFileString strings.Builder
Jiyong Park72678312021-01-18 17:29:49 +0900304 addStr := func(name string, value string) {
Cole Faustcec230a2024-03-07 15:51:12 -0800305 propFileString.WriteString(name)
306 propFileString.WriteRune('=')
307 propFileString.WriteString(value)
308 propFileString.WriteRune('\n')
Jiyong Park72678312021-01-18 17:29:49 +0900309 }
310 addPath := func(name string, path android.Path) {
Cole Faustcec230a2024-03-07 15:51:12 -0800311 addStr(name, path.String())
Jiyong Park72678312021-01-18 17:29:49 +0900312 deps = append(deps, path)
313 }
314
Jiyong Park11a65972021-02-01 21:09:38 +0900315 // Type string that build_image.py accepts.
316 fsTypeStr := func(t fsType) string {
317 switch t {
318 // TODO(jiyong): add more types like f2fs, erofs, etc.
319 case ext4Type:
320 return "ext4"
321 }
322 panic(fmt.Errorf("unsupported fs type %v", t))
323 }
324
325 addStr("fs_type", fsTypeStr(f.fsType(ctx)))
Inseob Kim376d72f2023-11-01 15:40:25 +0900326 addStr("mount_point", proptools.StringDefault(f.properties.Mount_point, "/"))
Jiyong Park72678312021-01-18 17:29:49 +0900327 addStr("use_dynamic_partition_size", "true")
328 addPath("ext_mkuserimg", ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs"))
329 // b/177813163 deps of the host tools have to be added. Remove this.
330 for _, t := range []string{"mke2fs", "e2fsdroid", "tune2fs"} {
331 deps = append(deps, ctx.Config().HostToolPath(ctx, t))
332 }
333
Jiyong Park71baa762021-01-18 21:11:03 +0900334 if proptools.Bool(f.properties.Use_avb) {
335 addStr("avb_hashtree_enable", "true")
336 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
337 algorithm := proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096")
338 addStr("avb_algorithm", algorithm)
339 key := android.PathForModuleSrc(ctx, proptools.String(f.properties.Avb_private_key))
340 addPath("avb_key_path", key)
Seungjae Yooa30e4502023-11-09 14:55:44 +0900341 partitionName := proptools.StringDefault(f.properties.Partition_name, f.Name())
342 addStr("partition_name", partitionName)
Shikha Panware6f30632022-12-21 12:54:45 +0000343 avb_add_hashtree_footer_args := "--do_not_generate_fec"
344 if hashAlgorithm := proptools.String(f.properties.Avb_hash_algorithm); hashAlgorithm != "" {
345 avb_add_hashtree_footer_args += " --hash_algorithm " + hashAlgorithm
346 }
Seungjae Yooa30e4502023-11-09 14:55:44 +0900347 securityPatchKey := "com.android.build." + partitionName + ".security_patch"
348 securityPatchValue := ctx.Config().PlatformSecurityPatch()
349 avb_add_hashtree_footer_args += " --prop " + securityPatchKey + ":" + securityPatchValue
Shikha Panware6f30632022-12-21 12:54:45 +0000350 addStr("avb_add_hashtree_footer_args", avb_add_hashtree_footer_args)
Jooyung Han65f402b2022-04-21 14:24:04 +0900351 addStr("avb_salt", f.salt())
Jiyong Park71baa762021-01-18 21:11:03 +0900352 }
353
Inseob Kimcc8e5362021-02-03 14:05:24 +0900354 if proptools.String(f.properties.File_contexts) != "" {
355 addPath("selinux_fc", f.buildFileContexts(ctx))
356 }
Jooyung Han65f402b2022-04-21 14:24:04 +0900357 if timestamp := proptools.String(f.properties.Fake_timestamp); timestamp != "" {
358 addStr("timestamp", timestamp)
359 }
360 if uuid := proptools.String(f.properties.Uuid); uuid != "" {
361 addStr("uuid", uuid)
362 addStr("hash_seed", uuid)
363 }
Jiyong Park72678312021-01-18 17:29:49 +0900364 propFile = android.PathForModuleOut(ctx, "prop").OutputPath
Cole Faustcec230a2024-03-07 15:51:12 -0800365 android.WriteFileRuleVerbatim(ctx, propFile, propFileString.String())
Jiyong Park72678312021-01-18 17:29:49 +0900366 return propFile, deps
367}
368
Jiyong Park837cdb22021-02-05 00:17:14 +0900369func (f *filesystem) buildCpioImage(ctx android.ModuleContext, compressed bool) android.OutputPath {
Jiyong Park11a65972021-02-01 21:09:38 +0900370 if proptools.Bool(f.properties.Use_avb) {
371 ctx.PropertyErrorf("use_avb", "signing compresed cpio image using avbtool is not supported."+
372 "Consider adding this to bootimg module and signing the entire boot image.")
373 }
374
Inseob Kimcc8e5362021-02-03 14:05:24 +0900375 if proptools.String(f.properties.File_contexts) != "" {
376 ctx.PropertyErrorf("file_contexts", "file_contexts is not supported for compressed cpio image.")
377 }
378
Cole Faust4a2a7c92024-03-12 12:44:40 -0700379 if f.properties.Include_make_built_files != "" {
380 ctx.PropertyErrorf("include_make_built_files", "include_make_built_files is not supported for compressed cpio image.")
381 }
382
Jiyong Park11a65972021-02-01 21:09:38 +0900383 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Cole Faust3b806d32024-03-11 15:15:03 -0700384 rebasedDir := rootDir
385 if f.properties.Base_dir != nil {
386 rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir)
387 }
388 builder := android.NewRuleBuilder(pctx, ctx)
389 // Wipe the root dir to get rid of leftover files from prior builds
390 builder.Command().Textf("rm -rf %s && mkdir -p %s", rootDir, rootDir)
391 f.entries = f.CopySpecsToDir(ctx, builder, f.gatherFilteredPackagingSpecs(ctx), rebasedDir)
392
393 f.buildNonDepsFiles(ctx, builder, rootDir)
Jiyong Park11a65972021-02-01 21:09:38 +0900394
395 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Jiyong Park837cdb22021-02-05 00:17:14 +0900396 cmd := builder.Command().
Jiyong Park11a65972021-02-01 21:09:38 +0900397 BuiltTool("mkbootfs").
Jiyong Park837cdb22021-02-05 00:17:14 +0900398 Text(rootDir.String()) // input directory
399 if compressed {
400 cmd.Text("|").
401 BuiltTool("lz4").
402 Flag("--favor-decSpeed"). // for faster boot
403 Flag("-12"). // maximum compression level
404 Flag("-l"). // legacy format for kernel
405 Text(">").Output(output)
406 } else {
407 cmd.Text(">").Output(output)
408 }
Jiyong Park11a65972021-02-01 21:09:38 +0900409
410 // rootDir is not deleted. Might be useful for quick inspection.
Jiyong Park837cdb22021-02-05 00:17:14 +0900411 builder.Build("build_cpio_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park11a65972021-02-01 21:09:38 +0900412
413 return output
414}
415
Cole Faust4a2a7c92024-03-12 12:44:40 -0700416var validPartitions = []string{
417 "system",
418 "userdata",
419 "cache",
420 "system_other",
421 "vendor",
422 "product",
423 "system_ext",
424 "odm",
425 "vendor_dlkm",
426 "odm_dlkm",
427 "system_dlkm",
428}
429
430func (f *filesystem) addMakeBuiltFiles(ctx android.ModuleContext, builder *android.RuleBuilder, rootDir android.Path) {
431 partition := f.properties.Include_make_built_files
432 if partition == "" {
433 return
434 }
435 if !slices.Contains(validPartitions, partition) {
436 ctx.PropertyErrorf("include_make_built_files", "Expected one of %#v, found %q", validPartitions, partition)
437 return
438 }
439 stampFile := fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/staging_dir.stamp", ctx.Config().DeviceName(), partition)
440 fileListFile := fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partition)
441 stagingDir := fmt.Sprintf("target/product/%s/%s", ctx.Config().DeviceName(), partition)
442
443 builder.Command().BuiltTool("merge_directories").
444 Implicit(android.PathForArbitraryOutput(ctx, stampFile)).
445 Text("--ignore-duplicates").
446 FlagWithInput("--file-list", android.PathForArbitraryOutput(ctx, fileListFile)).
447 Text(rootDir.String()).
448 Text(android.PathForArbitraryOutput(ctx, stagingDir).String())
449}
450
Cole Faust9a24d902024-03-18 15:38:12 -0700451func (f *filesystem) PartitionType() string {
452 return proptools.StringDefault(f.properties.Partition_type, "system")
453}
454
Jiyong Park65c49f52020-11-24 14:23:26 +0900455var _ android.AndroidMkEntriesProvider = (*filesystem)(nil)
456
457// Implements android.AndroidMkEntriesProvider
458func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries {
459 return []android.AndroidMkEntries{android.AndroidMkEntries{
460 Class: "ETC",
461 OutputFile: android.OptionalPathForPath(f.output),
462 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700463 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800464 entries.SetString("LOCAL_MODULE_PATH", f.installDir.String())
Jiyong Park65c49f52020-11-24 14:23:26 +0900465 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
466 },
467 },
468 }}
Jiyong Park6f0f6882020-11-12 13:14:30 +0900469}
Jiyong Park12a719c2021-01-07 15:31:24 +0900470
Jiyong Park940dfd42021-02-04 15:37:34 +0900471var _ android.OutputFileProducer = (*filesystem)(nil)
472
473// Implements android.OutputFileProducer
474func (f *filesystem) OutputFiles(tag string) (android.Paths, error) {
475 if tag == "" {
476 return []android.Path{f.output}, nil
477 }
478 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
479}
480
Jiyong Park12a719c2021-01-07 15:31:24 +0900481// Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex
482// package to have access to the output file.
483type Filesystem interface {
484 android.Module
485 OutputPath() android.Path
Jiyong Park972e06c2021-03-15 23:32:49 +0900486
487 // Returns the output file that is signed by avbtool. If this module is not signed, returns
488 // nil.
489 SignedOutputPath() android.Path
Jiyong Park12a719c2021-01-07 15:31:24 +0900490}
491
492var _ Filesystem = (*filesystem)(nil)
493
494func (f *filesystem) OutputPath() android.Path {
495 return f.output
496}
Jiyong Park972e06c2021-03-15 23:32:49 +0900497
498func (f *filesystem) SignedOutputPath() android.Path {
499 if proptools.Bool(f.properties.Use_avb) {
500 return f.OutputPath()
501 }
502 return nil
503}
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900504
505// Filter the result of GatherPackagingSpecs to discard items targeting outside "system" partition.
506// Note that "apex" module installs its contents to "apex"(fake partition) as well
507// for symbol lookup by imitating "activated" paths.
508func (f *filesystem) gatherFilteredPackagingSpecs(ctx android.ModuleContext) map[string]android.PackagingSpec {
Jeongik Cha54bf8752024-02-08 10:44:37 +0900509 specs := f.PackagingBase.GatherPackagingSpecsWithFilter(ctx, f.filterPackagingSpec)
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900510 return specs
511}
Jooyung Han65f402b2022-04-21 14:24:04 +0900512
513func sha1sum(values []string) string {
514 h := sha256.New()
515 for _, value := range values {
516 io.WriteString(h, value)
517 }
518 return fmt.Sprintf("%x", h.Sum(nil))
519}
Jooyung Hane6067592023-03-16 13:11:17 +0900520
521// Base cc.UseCoverage
522
523var _ cc.UseCoverage = (*filesystem)(nil)
524
Colin Crossf5f4ad32024-01-19 15:41:48 -0800525func (*filesystem) IsNativeCoverageNeeded(ctx android.IncomingTransitionContext) bool {
Jooyung Hane6067592023-03-16 13:11:17 +0900526 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
527}