blob: e640c5dae9e2c699c029736dbb3e7e7a686300fb [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 Parkeaac8232024-03-31 21:27:45 +090039 ctx.RegisterModuleType("android_system_image_defaults", systemImageDefaultsFactory)
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 Park65c49f52020-11-24 14:23:26 +090049
Jiyong Park71baa762021-01-18 21:11:03 +090050 properties filesystemProperties
51
Jiyong Parkfa616132021-04-20 11:36:40 +090052 // Function that builds extra files under the root directory and returns the files
53 buildExtraFiles func(ctx android.ModuleContext, root android.OutputPath) android.OutputPaths
54
Jeongik Cha54bf8752024-02-08 10:44:37 +090055 // Function that filters PackagingSpec in PackagingBase.GatherPackagingSpecs()
56 filterPackagingSpec func(spec android.PackagingSpec) bool
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090057
Jiyong Park65c49f52020-11-24 14:23:26 +090058 output android.OutputPath
59 installDir android.InstallPath
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090060
Inseob Kim53391842024-03-29 17:44:07 +090061 // For testing. Keeps the result of CopySpecsToDir()
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090062 entries []string
Jiyong Park6f0f6882020-11-12 13:14:30 +090063}
64
Inseob Kim14199b02021-02-09 21:18:31 +090065type symlinkDefinition struct {
66 Target *string
67 Name *string
68}
69
Jiyong Park71baa762021-01-18 21:11:03 +090070type filesystemProperties struct {
71 // When set to true, sign the image with avbtool. Default is false.
72 Use_avb *bool
73
74 // Path to the private key that avbtool will use to sign this filesystem image.
75 // TODO(jiyong): allow apex_key to be specified here
76 Avb_private_key *string `android:"path"`
77
Shikha Panwar01403bb2022-12-22 12:22:57 +000078 // Signing algorithm for avbtool. Default is SHA256_RSA4096.
Jiyong Park71baa762021-01-18 21:11:03 +090079 Avb_algorithm *string
Jiyong Park11a65972021-02-01 21:09:38 +090080
Shikha Panwar01403bb2022-12-22 12:22:57 +000081 // Hash algorithm used for avbtool (for descriptors). This is passed as hash_algorithm to
82 // avbtool. Default used by avbtool is sha1.
Shikha Panware6f30632022-12-21 12:54:45 +000083 Avb_hash_algorithm *string
84
Jiyong Parkac4076d2021-03-15 23:21:30 +090085 // Name of the partition stored in vbmeta desc. Defaults to the name of this module.
86 Partition_name *string
87
Jiyong Park837cdb22021-02-05 00:17:14 +090088 // Type of the filesystem. Currently, ext4, cpio, and compressed_cpio are supported. Default
89 // is ext4.
Jiyong Park11a65972021-02-01 21:09:38 +090090 Type *string
Inseob Kimcc8e5362021-02-03 14:05:24 +090091
Cole Faust9a24d902024-03-18 15:38:12 -070092 // Identifies which partition this is for //visibility:any_system_image (and others) visibility
93 // checks, and will be used in the future for API surface checks.
94 Partition_type *string
95
Inseob Kimcc8e5362021-02-03 14:05:24 +090096 // file_contexts file to make image. Currently, only ext4 is supported.
97 File_contexts *string `android:"path"`
Inseob Kim2ce1b5d2021-02-15 17:01:04 +090098
99 // Base directory relative to root, to which deps are installed, e.g. "system". Default is "."
100 // (root).
101 Base_dir *string
Inseob Kim14199b02021-02-09 21:18:31 +0900102
103 // Directories to be created under root. e.g. /dev, /proc, etc.
104 Dirs []string
105
106 // Symbolic links to be created under root with "ln -sf <target> <name>".
107 Symlinks []symlinkDefinition
Jooyung Han65f402b2022-04-21 14:24:04 +0900108
109 // Seconds since unix epoch to override timestamps of file entries
110 Fake_timestamp *string
111
112 // When set, passed to mkuserimg_mke2fs --mke2fs_uuid & --mke2fs_hash_seed.
113 // Otherwise, they'll be set as random which might cause indeterministic build output.
114 Uuid *string
Inseob Kim376d72f2023-11-01 15:40:25 +0900115
116 // Mount point for this image. Default is "/"
117 Mount_point *string
Cole Faust4a2a7c92024-03-12 12:44:40 -0700118
119 // If set to the name of a partition ("system", "vendor", etc), this filesystem module
120 // will also include the contents of the make-built staging directories. If any soong
121 // modules would be installed to the same location as a make module, they will overwrite
122 // the make version.
123 Include_make_built_files string
Inseob Kim53391842024-03-29 17:44:07 +0900124
125 Fsverity fsverityProperties
Jiyong Park71baa762021-01-18 21:11:03 +0900126}
127
Jiyong Park65c49f52020-11-24 14:23:26 +0900128// android_filesystem packages a set of modules and their transitive dependencies into a filesystem
129// image. The filesystem images are expected to be mounted in the target device, which means the
130// modules in the filesystem image are built for the target device (i.e. Android, not Linux host).
131// The modules are placed in the filesystem image just like they are installed to the ordinary
132// partitions like system.img. For example, cc_library modules are placed under ./lib[64] directory.
Jiyong Park6f0f6882020-11-12 13:14:30 +0900133func filesystemFactory() android.Module {
134 module := &filesystem{}
Jiyong Parkfa616132021-04-20 11:36:40 +0900135 initFilesystemModule(module)
136 return module
137}
138
139func initFilesystemModule(module *filesystem) {
Jiyong Park71baa762021-01-18 21:11:03 +0900140 module.AddProperties(&module.properties)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900141 android.InitPackageModule(module)
142 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900143}
144
Jiyong Park12a719c2021-01-07 15:31:24 +0900145var dependencyTag = struct {
146 blueprint.BaseDependencyTag
Jooyung Han092ef812021-03-10 15:40:34 +0900147 android.PackagingItemAlwaysDepTag
Jiyong Park12a719c2021-01-07 15:31:24 +0900148}{}
Jiyong Park65b62242020-11-25 12:44:59 +0900149
Jiyong Park6f0f6882020-11-12 13:14:30 +0900150func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park65b62242020-11-25 12:44:59 +0900151 f.AddDeps(ctx, dependencyTag)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900152}
153
Jiyong Park11a65972021-02-01 21:09:38 +0900154type fsType int
155
156const (
157 ext4Type fsType = iota
158 compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900159 cpioType // uncompressed
Jiyong Park11a65972021-02-01 21:09:38 +0900160 unknown
161)
162
163func (f *filesystem) fsType(ctx android.ModuleContext) fsType {
164 typeStr := proptools.StringDefault(f.properties.Type, "ext4")
165 switch typeStr {
166 case "ext4":
167 return ext4Type
168 case "compressed_cpio":
169 return compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900170 case "cpio":
171 return cpioType
Jiyong Park11a65972021-02-01 21:09:38 +0900172 default:
173 ctx.PropertyErrorf("type", "%q not supported", typeStr)
174 return unknown
175 }
176}
177
Jiyong Park65c49f52020-11-24 14:23:26 +0900178func (f *filesystem) installFileName() string {
179 return f.BaseModuleName() + ".img"
180}
181
Inseob Kim53391842024-03-29 17:44:07 +0900182func (f *filesystem) partitionName() string {
183 return proptools.StringDefault(f.properties.Partition_name, f.Name())
184}
185
Jiyong Park6f0f6882020-11-12 13:14:30 +0900186var pctx = android.NewPackageContext("android/soong/filesystem")
187
188func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust9a24d902024-03-18 15:38:12 -0700189 if !android.InList(f.PartitionType(), validPartitions) {
190 ctx.PropertyErrorf("partition_type", "partition_type must be one of %s, found: %s", validPartitions, f.PartitionType())
191 }
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
Cole Faust3b806d32024-03-11 15:15:03 -0700207// Copy extra files/dirs that are not from the `deps` property to `rootDir`, checking for conflicts with files
208// already in `rootDir`.
209func (f *filesystem) buildNonDepsFiles(ctx android.ModuleContext, builder *android.RuleBuilder, rootDir android.OutputPath) {
Inseob Kim14199b02021-02-09 21:18:31 +0900210 // create dirs and symlinks
211 for _, dir := range f.properties.Dirs {
212 // OutputPath.Join verifies dir
213 builder.Command().Text("mkdir -p").Text(rootDir.Join(ctx, dir).String())
214 }
215
216 for _, symlink := range f.properties.Symlinks {
217 name := strings.TrimSpace(proptools.String(symlink.Name))
218 target := strings.TrimSpace(proptools.String(symlink.Target))
219
220 if name == "" {
221 ctx.PropertyErrorf("symlinks", "Name can't be empty")
222 continue
223 }
224
225 if target == "" {
226 ctx.PropertyErrorf("symlinks", "Target can't be empty")
227 continue
228 }
229
230 // OutputPath.Join verifies name. don't need to verify target.
231 dst := rootDir.Join(ctx, name)
Cole Faust3b806d32024-03-11 15:15:03 -0700232 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 +0900233 builder.Command().Text("mkdir -p").Text(filepath.Dir(dst.String()))
234 builder.Command().Text("ln -sf").Text(proptools.ShellEscape(target)).Text(dst.String())
235 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900236
Jiyong Parkfa616132021-04-20 11:36:40 +0900237 // create extra files if there's any
Jiyong Parkfa616132021-04-20 11:36:40 +0900238 if f.buildExtraFiles != nil {
Cole Faust4a2a7c92024-03-12 12:44:40 -0700239 rootForExtraFiles := android.PathForModuleGen(ctx, "root-extra").OutputPath
240 extraFiles := f.buildExtraFiles(ctx, rootForExtraFiles)
241 for _, f := range extraFiles {
242 rel, err := filepath.Rel(rootForExtraFiles.String(), f.String())
243 if err != nil || strings.HasPrefix(rel, "..") {
244 ctx.ModuleErrorf("can't make %q relative to %q", f, rootForExtraFiles)
245 }
Jiyong Parkfa616132021-04-20 11:36:40 +0900246 }
Cole Faust4a2a7c92024-03-12 12:44:40 -0700247 if len(extraFiles) > 0 {
248 builder.Command().BuiltTool("merge_directories").
249 Implicits(extraFiles.Paths()).
250 Text(rootDir.String()).
251 Text(rootForExtraFiles.String())
252 }
Jiyong Parkfa616132021-04-20 11:36:40 +0900253 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900254}
255
Jiyong Park11a65972021-02-01 21:09:38 +0900256func (f *filesystem) buildImageUsingBuildImage(ctx android.ModuleContext) android.OutputPath {
Jiyong Park6f0f6882020-11-12 13:14:30 +0900257 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Cole Faust3b806d32024-03-11 15:15:03 -0700258 rebasedDir := rootDir
259 if f.properties.Base_dir != nil {
260 rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir)
261 }
262 builder := android.NewRuleBuilder(pctx, ctx)
263 // Wipe the root dir to get rid of leftover files from prior builds
264 builder.Command().Textf("rm -rf %s && mkdir -p %s", rootDir, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900265 specs := f.gatherFilteredPackagingSpecs(ctx)
266 f.entries = f.CopySpecsToDir(ctx, builder, specs, rebasedDir)
Cole Faust3b806d32024-03-11 15:15:03 -0700267
268 f.buildNonDepsFiles(ctx, builder, rootDir)
Cole Faust4a2a7c92024-03-12 12:44:40 -0700269 f.addMakeBuiltFiles(ctx, builder, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900270 f.buildFsverityMetadataFiles(ctx, builder, specs, rootDir, rebasedDir)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900271
Nikita Ioffe519015f2022-12-23 15:36:29 +0000272 // run host_init_verifier
273 // Ideally we should have a concept of pluggable linters that verify the generated image.
274 // While such concept is not implement this will do.
275 // TODO(b/263574231): substitute with pluggable linter.
276 builder.Command().
277 BuiltTool("host_init_verifier").
278 FlagWithArg("--out_system=", rootDir.String()+"/system")
279
Jiyong Park72678312021-01-18 17:29:49 +0900280 propFile, toolDeps := f.buildPropFile(ctx)
Jiyong Park11a65972021-02-01 21:09:38 +0900281 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -0800282 builder.Command().BuiltTool("build_image").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900283 Text(rootDir.String()). // input directory
284 Input(propFile).
Jiyong Park72678312021-01-18 17:29:49 +0900285 Implicits(toolDeps).
Jiyong Park11a65972021-02-01 21:09:38 +0900286 Output(output).
Jiyong Park6f0f6882020-11-12 13:14:30 +0900287 Text(rootDir.String()) // directory where to find fs_config_files|dirs
288
289 // rootDir is not deleted. Might be useful for quick inspection.
Colin Crossf1a035e2020-11-16 17:32:30 -0800290 builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park65c49f52020-11-24 14:23:26 +0900291
Jiyong Park11a65972021-02-01 21:09:38 +0900292 return output
Jiyong Park65c49f52020-11-24 14:23:26 +0900293}
294
Inseob Kimcc8e5362021-02-03 14:05:24 +0900295func (f *filesystem) buildFileContexts(ctx android.ModuleContext) android.OutputPath {
296 builder := android.NewRuleBuilder(pctx, ctx)
297 fcBin := android.PathForModuleOut(ctx, "file_contexts.bin")
298 builder.Command().BuiltTool("sefcontext_compile").
299 FlagWithOutput("-o ", fcBin).
300 Input(android.PathForModuleSrc(ctx, proptools.String(f.properties.File_contexts)))
301 builder.Build("build_filesystem_file_contexts", fmt.Sprintf("Creating filesystem file contexts for %s", f.BaseModuleName()))
302 return fcBin.OutputPath
303}
304
Jooyung Han65f402b2022-04-21 14:24:04 +0900305// Calculates avb_salt from entry list (sorted) for deterministic output.
306func (f *filesystem) salt() string {
307 return sha1sum(f.entries)
308}
309
Jiyong Park72678312021-01-18 17:29:49 +0900310func (f *filesystem) buildPropFile(ctx android.ModuleContext) (propFile android.OutputPath, toolDeps android.Paths) {
Jiyong Park72678312021-01-18 17:29:49 +0900311 var deps android.Paths
Cole Faustcec230a2024-03-07 15:51:12 -0800312 var propFileString strings.Builder
Jiyong Park72678312021-01-18 17:29:49 +0900313 addStr := func(name string, value string) {
Cole Faustcec230a2024-03-07 15:51:12 -0800314 propFileString.WriteString(name)
315 propFileString.WriteRune('=')
316 propFileString.WriteString(value)
317 propFileString.WriteRune('\n')
Jiyong Park72678312021-01-18 17:29:49 +0900318 }
319 addPath := func(name string, path android.Path) {
Cole Faustcec230a2024-03-07 15:51:12 -0800320 addStr(name, path.String())
Jiyong Park72678312021-01-18 17:29:49 +0900321 deps = append(deps, path)
322 }
323
Jiyong Park11a65972021-02-01 21:09:38 +0900324 // Type string that build_image.py accepts.
325 fsTypeStr := func(t fsType) string {
326 switch t {
327 // TODO(jiyong): add more types like f2fs, erofs, etc.
328 case ext4Type:
329 return "ext4"
330 }
331 panic(fmt.Errorf("unsupported fs type %v", t))
332 }
333
334 addStr("fs_type", fsTypeStr(f.fsType(ctx)))
Inseob Kim376d72f2023-11-01 15:40:25 +0900335 addStr("mount_point", proptools.StringDefault(f.properties.Mount_point, "/"))
Jiyong Park72678312021-01-18 17:29:49 +0900336 addStr("use_dynamic_partition_size", "true")
337 addPath("ext_mkuserimg", ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs"))
338 // b/177813163 deps of the host tools have to be added. Remove this.
339 for _, t := range []string{"mke2fs", "e2fsdroid", "tune2fs"} {
340 deps = append(deps, ctx.Config().HostToolPath(ctx, t))
341 }
342
Jiyong Park71baa762021-01-18 21:11:03 +0900343 if proptools.Bool(f.properties.Use_avb) {
344 addStr("avb_hashtree_enable", "true")
345 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
346 algorithm := proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096")
347 addStr("avb_algorithm", algorithm)
348 key := android.PathForModuleSrc(ctx, proptools.String(f.properties.Avb_private_key))
349 addPath("avb_key_path", key)
Inseob Kim53391842024-03-29 17:44:07 +0900350 addStr("partition_name", f.partitionName())
Shikha Panware6f30632022-12-21 12:54:45 +0000351 avb_add_hashtree_footer_args := "--do_not_generate_fec"
352 if hashAlgorithm := proptools.String(f.properties.Avb_hash_algorithm); hashAlgorithm != "" {
353 avb_add_hashtree_footer_args += " --hash_algorithm " + hashAlgorithm
354 }
Inseob Kim53391842024-03-29 17:44:07 +0900355 securityPatchKey := "com.android.build." + f.partitionName() + ".security_patch"
Seungjae Yooa30e4502023-11-09 14:55:44 +0900356 securityPatchValue := ctx.Config().PlatformSecurityPatch()
357 avb_add_hashtree_footer_args += " --prop " + securityPatchKey + ":" + securityPatchValue
Shikha Panware6f30632022-12-21 12:54:45 +0000358 addStr("avb_add_hashtree_footer_args", avb_add_hashtree_footer_args)
Jooyung Han65f402b2022-04-21 14:24:04 +0900359 addStr("avb_salt", f.salt())
Jiyong Park71baa762021-01-18 21:11:03 +0900360 }
361
Inseob Kimcc8e5362021-02-03 14:05:24 +0900362 if proptools.String(f.properties.File_contexts) != "" {
363 addPath("selinux_fc", f.buildFileContexts(ctx))
364 }
Jooyung Han65f402b2022-04-21 14:24:04 +0900365 if timestamp := proptools.String(f.properties.Fake_timestamp); timestamp != "" {
366 addStr("timestamp", timestamp)
367 }
368 if uuid := proptools.String(f.properties.Uuid); uuid != "" {
369 addStr("uuid", uuid)
370 addStr("hash_seed", uuid)
371 }
Jiyong Park72678312021-01-18 17:29:49 +0900372 propFile = android.PathForModuleOut(ctx, "prop").OutputPath
Cole Faustcec230a2024-03-07 15:51:12 -0800373 android.WriteFileRuleVerbatim(ctx, propFile, propFileString.String())
Jiyong Park72678312021-01-18 17:29:49 +0900374 return propFile, deps
375}
376
Jiyong Park837cdb22021-02-05 00:17:14 +0900377func (f *filesystem) buildCpioImage(ctx android.ModuleContext, compressed bool) android.OutputPath {
Jiyong Park11a65972021-02-01 21:09:38 +0900378 if proptools.Bool(f.properties.Use_avb) {
379 ctx.PropertyErrorf("use_avb", "signing compresed cpio image using avbtool is not supported."+
380 "Consider adding this to bootimg module and signing the entire boot image.")
381 }
382
Inseob Kimcc8e5362021-02-03 14:05:24 +0900383 if proptools.String(f.properties.File_contexts) != "" {
384 ctx.PropertyErrorf("file_contexts", "file_contexts is not supported for compressed cpio image.")
385 }
386
Cole Faust4a2a7c92024-03-12 12:44:40 -0700387 if f.properties.Include_make_built_files != "" {
388 ctx.PropertyErrorf("include_make_built_files", "include_make_built_files is not supported for compressed cpio image.")
389 }
390
Jiyong Park11a65972021-02-01 21:09:38 +0900391 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Cole Faust3b806d32024-03-11 15:15:03 -0700392 rebasedDir := rootDir
393 if f.properties.Base_dir != nil {
394 rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir)
395 }
396 builder := android.NewRuleBuilder(pctx, ctx)
397 // Wipe the root dir to get rid of leftover files from prior builds
398 builder.Command().Textf("rm -rf %s && mkdir -p %s", rootDir, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900399 specs := f.gatherFilteredPackagingSpecs(ctx)
400 f.entries = f.CopySpecsToDir(ctx, builder, specs, rebasedDir)
Cole Faust3b806d32024-03-11 15:15:03 -0700401
402 f.buildNonDepsFiles(ctx, builder, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900403 f.buildFsverityMetadataFiles(ctx, builder, specs, rootDir, rebasedDir)
Jiyong Park11a65972021-02-01 21:09:38 +0900404
405 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Jiyong Park837cdb22021-02-05 00:17:14 +0900406 cmd := builder.Command().
Jiyong Park11a65972021-02-01 21:09:38 +0900407 BuiltTool("mkbootfs").
Jiyong Park837cdb22021-02-05 00:17:14 +0900408 Text(rootDir.String()) // input directory
409 if compressed {
410 cmd.Text("|").
411 BuiltTool("lz4").
412 Flag("--favor-decSpeed"). // for faster boot
413 Flag("-12"). // maximum compression level
414 Flag("-l"). // legacy format for kernel
415 Text(">").Output(output)
416 } else {
417 cmd.Text(">").Output(output)
418 }
Jiyong Park11a65972021-02-01 21:09:38 +0900419
420 // rootDir is not deleted. Might be useful for quick inspection.
Jiyong Park837cdb22021-02-05 00:17:14 +0900421 builder.Build("build_cpio_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park11a65972021-02-01 21:09:38 +0900422
423 return output
424}
425
Cole Faust4a2a7c92024-03-12 12:44:40 -0700426var validPartitions = []string{
427 "system",
428 "userdata",
429 "cache",
430 "system_other",
431 "vendor",
432 "product",
433 "system_ext",
434 "odm",
435 "vendor_dlkm",
436 "odm_dlkm",
437 "system_dlkm",
438}
439
440func (f *filesystem) addMakeBuiltFiles(ctx android.ModuleContext, builder *android.RuleBuilder, rootDir android.Path) {
441 partition := f.properties.Include_make_built_files
442 if partition == "" {
443 return
444 }
445 if !slices.Contains(validPartitions, partition) {
446 ctx.PropertyErrorf("include_make_built_files", "Expected one of %#v, found %q", validPartitions, partition)
447 return
448 }
449 stampFile := fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/staging_dir.stamp", ctx.Config().DeviceName(), partition)
450 fileListFile := fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partition)
451 stagingDir := fmt.Sprintf("target/product/%s/%s", ctx.Config().DeviceName(), partition)
452
453 builder.Command().BuiltTool("merge_directories").
454 Implicit(android.PathForArbitraryOutput(ctx, stampFile)).
455 Text("--ignore-duplicates").
456 FlagWithInput("--file-list", android.PathForArbitraryOutput(ctx, fileListFile)).
457 Text(rootDir.String()).
458 Text(android.PathForArbitraryOutput(ctx, stagingDir).String())
459}
460
Cole Faust9a24d902024-03-18 15:38:12 -0700461func (f *filesystem) PartitionType() string {
462 return proptools.StringDefault(f.properties.Partition_type, "system")
463}
464
Jiyong Park65c49f52020-11-24 14:23:26 +0900465var _ android.AndroidMkEntriesProvider = (*filesystem)(nil)
466
467// Implements android.AndroidMkEntriesProvider
468func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries {
469 return []android.AndroidMkEntries{android.AndroidMkEntries{
470 Class: "ETC",
471 OutputFile: android.OptionalPathForPath(f.output),
472 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700473 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800474 entries.SetString("LOCAL_MODULE_PATH", f.installDir.String())
Jiyong Park65c49f52020-11-24 14:23:26 +0900475 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
476 },
477 },
478 }}
Jiyong Park6f0f6882020-11-12 13:14:30 +0900479}
Jiyong Park12a719c2021-01-07 15:31:24 +0900480
Jiyong Park940dfd42021-02-04 15:37:34 +0900481var _ android.OutputFileProducer = (*filesystem)(nil)
482
483// Implements android.OutputFileProducer
484func (f *filesystem) OutputFiles(tag string) (android.Paths, error) {
485 if tag == "" {
486 return []android.Path{f.output}, nil
487 }
488 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
489}
490
Jiyong Park12a719c2021-01-07 15:31:24 +0900491// Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex
492// package to have access to the output file.
493type Filesystem interface {
494 android.Module
495 OutputPath() android.Path
Jiyong Park972e06c2021-03-15 23:32:49 +0900496
497 // Returns the output file that is signed by avbtool. If this module is not signed, returns
498 // nil.
499 SignedOutputPath() android.Path
Jiyong Park12a719c2021-01-07 15:31:24 +0900500}
501
502var _ Filesystem = (*filesystem)(nil)
503
504func (f *filesystem) OutputPath() android.Path {
505 return f.output
506}
Jiyong Park972e06c2021-03-15 23:32:49 +0900507
508func (f *filesystem) SignedOutputPath() android.Path {
509 if proptools.Bool(f.properties.Use_avb) {
510 return f.OutputPath()
511 }
512 return nil
513}
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900514
515// Filter the result of GatherPackagingSpecs to discard items targeting outside "system" partition.
516// Note that "apex" module installs its contents to "apex"(fake partition) as well
517// for symbol lookup by imitating "activated" paths.
518func (f *filesystem) gatherFilteredPackagingSpecs(ctx android.ModuleContext) map[string]android.PackagingSpec {
Jeongik Cha54bf8752024-02-08 10:44:37 +0900519 specs := f.PackagingBase.GatherPackagingSpecsWithFilter(ctx, f.filterPackagingSpec)
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900520 return specs
521}
Jooyung Han65f402b2022-04-21 14:24:04 +0900522
523func sha1sum(values []string) string {
524 h := sha256.New()
525 for _, value := range values {
526 io.WriteString(h, value)
527 }
528 return fmt.Sprintf("%x", h.Sum(nil))
529}
Jooyung Hane6067592023-03-16 13:11:17 +0900530
531// Base cc.UseCoverage
532
533var _ cc.UseCoverage = (*filesystem)(nil)
534
Colin Crossf5f4ad32024-01-19 15:41:48 -0800535func (*filesystem) IsNativeCoverageNeeded(ctx android.IncomingTransitionContext) bool {
Jooyung Hane6067592023-03-16 13:11:17 +0900536 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
537}