blob: 795a0aa0212acf7b696e9beb0337e1f0c94dd124 [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"
22 "strings"
Jiyong Park6f0f6882020-11-12 13:14:30 +090023
24 "android/soong/android"
Jooyung Hane6067592023-03-16 13:11:17 +090025 "android/soong/cc"
Jiyong Park65b62242020-11-25 12:44:59 +090026
27 "github.com/google/blueprint"
Jiyong Park71baa762021-01-18 21:11:03 +090028 "github.com/google/blueprint/proptools"
Jiyong Park6f0f6882020-11-12 13:14:30 +090029)
30
31func init() {
Jooyung Han9706cbc2021-04-15 22:43:48 +090032 registerBuildComponents(android.InitRegistrationContext)
33}
34
35func registerBuildComponents(ctx android.RegistrationContext) {
36 ctx.RegisterModuleType("android_filesystem", filesystemFactory)
Jiyong Parkfa616132021-04-20 11:36:40 +090037 ctx.RegisterModuleType("android_system_image", systemImageFactory)
Jiyong Parkbc485482022-11-15 22:31:49 +090038 ctx.RegisterModuleType("avb_add_hash_footer", avbAddHashFooterFactory)
Inseob Kim87230e62023-11-22 18:55:07 +090039 ctx.RegisterModuleType("avb_add_hash_footer_defaults", avbAddHashFooterDefaultsFactory)
Alice Wang000e3a32023-01-03 16:11:20 +000040 ctx.RegisterModuleType("avb_gen_vbmeta_image", avbGenVbmetaImageFactory)
Inseob Kim87230e62023-11-22 18:55:07 +090041 ctx.RegisterModuleType("avb_gen_vbmeta_image_defaults", avbGenVbmetaImageDefaultsFactory)
Jiyong Park6f0f6882020-11-12 13:14:30 +090042}
43
44type filesystem struct {
45 android.ModuleBase
46 android.PackagingBase
Jiyong Park65c49f52020-11-24 14:23:26 +090047
Jiyong Park71baa762021-01-18 21:11:03 +090048 properties filesystemProperties
49
Jiyong Parkfa616132021-04-20 11:36:40 +090050 // Function that builds extra files under the root directory and returns the files
51 buildExtraFiles func(ctx android.ModuleContext, root android.OutputPath) android.OutputPaths
52
Jeongik Cha54bf8752024-02-08 10:44:37 +090053 // Function that filters PackagingSpec in PackagingBase.GatherPackagingSpecs()
54 filterPackagingSpec func(spec android.PackagingSpec) bool
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090055
Jiyong Park65c49f52020-11-24 14:23:26 +090056 output android.OutputPath
57 installDir android.InstallPath
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090058
59 // For testing. Keeps the result of CopyDepsToZip()
60 entries []string
Jiyong Park6f0f6882020-11-12 13:14:30 +090061}
62
Inseob Kim14199b02021-02-09 21:18:31 +090063type symlinkDefinition struct {
64 Target *string
65 Name *string
66}
67
Jiyong Park71baa762021-01-18 21:11:03 +090068type filesystemProperties struct {
69 // When set to true, sign the image with avbtool. Default is false.
70 Use_avb *bool
71
72 // Path to the private key that avbtool will use to sign this filesystem image.
73 // TODO(jiyong): allow apex_key to be specified here
74 Avb_private_key *string `android:"path"`
75
Shikha Panwar01403bb2022-12-22 12:22:57 +000076 // Signing algorithm for avbtool. Default is SHA256_RSA4096.
Jiyong Park71baa762021-01-18 21:11:03 +090077 Avb_algorithm *string
Jiyong Park11a65972021-02-01 21:09:38 +090078
Shikha Panwar01403bb2022-12-22 12:22:57 +000079 // Hash algorithm used for avbtool (for descriptors). This is passed as hash_algorithm to
80 // avbtool. Default used by avbtool is sha1.
Shikha Panware6f30632022-12-21 12:54:45 +000081 Avb_hash_algorithm *string
82
Jiyong Parkac4076d2021-03-15 23:21:30 +090083 // Name of the partition stored in vbmeta desc. Defaults to the name of this module.
84 Partition_name *string
85
Jiyong Park837cdb22021-02-05 00:17:14 +090086 // Type of the filesystem. Currently, ext4, cpio, and compressed_cpio are supported. Default
87 // is ext4.
Jiyong Park11a65972021-02-01 21:09:38 +090088 Type *string
Inseob Kimcc8e5362021-02-03 14:05:24 +090089
90 // file_contexts file to make image. Currently, only ext4 is supported.
91 File_contexts *string `android:"path"`
Inseob Kim2ce1b5d2021-02-15 17:01:04 +090092
93 // Base directory relative to root, to which deps are installed, e.g. "system". Default is "."
94 // (root).
95 Base_dir *string
Inseob Kim14199b02021-02-09 21:18:31 +090096
97 // Directories to be created under root. e.g. /dev, /proc, etc.
98 Dirs []string
99
100 // Symbolic links to be created under root with "ln -sf <target> <name>".
101 Symlinks []symlinkDefinition
Jooyung Han65f402b2022-04-21 14:24:04 +0900102
103 // Seconds since unix epoch to override timestamps of file entries
104 Fake_timestamp *string
105
106 // When set, passed to mkuserimg_mke2fs --mke2fs_uuid & --mke2fs_hash_seed.
107 // Otherwise, they'll be set as random which might cause indeterministic build output.
108 Uuid *string
Inseob Kim376d72f2023-11-01 15:40:25 +0900109
110 // Mount point for this image. Default is "/"
111 Mount_point *string
Jiyong Park71baa762021-01-18 21:11:03 +0900112}
113
Jiyong Park65c49f52020-11-24 14:23:26 +0900114// android_filesystem packages a set of modules and their transitive dependencies into a filesystem
115// image. The filesystem images are expected to be mounted in the target device, which means the
116// modules in the filesystem image are built for the target device (i.e. Android, not Linux host).
117// The modules are placed in the filesystem image just like they are installed to the ordinary
118// partitions like system.img. For example, cc_library modules are placed under ./lib[64] directory.
Jiyong Park6f0f6882020-11-12 13:14:30 +0900119func filesystemFactory() android.Module {
120 module := &filesystem{}
Jiyong Parkfa616132021-04-20 11:36:40 +0900121 initFilesystemModule(module)
122 return module
123}
124
125func initFilesystemModule(module *filesystem) {
Jiyong Park71baa762021-01-18 21:11:03 +0900126 module.AddProperties(&module.properties)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900127 android.InitPackageModule(module)
128 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900129}
130
Jiyong Park12a719c2021-01-07 15:31:24 +0900131var dependencyTag = struct {
132 blueprint.BaseDependencyTag
Jooyung Han092ef812021-03-10 15:40:34 +0900133 android.PackagingItemAlwaysDepTag
Jiyong Park12a719c2021-01-07 15:31:24 +0900134}{}
Jiyong Park65b62242020-11-25 12:44:59 +0900135
Jiyong Park6f0f6882020-11-12 13:14:30 +0900136func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park65b62242020-11-25 12:44:59 +0900137 f.AddDeps(ctx, dependencyTag)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900138}
139
Jiyong Park11a65972021-02-01 21:09:38 +0900140type fsType int
141
142const (
143 ext4Type fsType = iota
144 compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900145 cpioType // uncompressed
Jiyong Park11a65972021-02-01 21:09:38 +0900146 unknown
147)
148
149func (f *filesystem) fsType(ctx android.ModuleContext) fsType {
150 typeStr := proptools.StringDefault(f.properties.Type, "ext4")
151 switch typeStr {
152 case "ext4":
153 return ext4Type
154 case "compressed_cpio":
155 return compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900156 case "cpio":
157 return cpioType
Jiyong Park11a65972021-02-01 21:09:38 +0900158 default:
159 ctx.PropertyErrorf("type", "%q not supported", typeStr)
160 return unknown
161 }
162}
163
Jiyong Park65c49f52020-11-24 14:23:26 +0900164func (f *filesystem) installFileName() string {
165 return f.BaseModuleName() + ".img"
166}
167
Jiyong Park6f0f6882020-11-12 13:14:30 +0900168var pctx = android.NewPackageContext("android/soong/filesystem")
169
170func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park11a65972021-02-01 21:09:38 +0900171 switch f.fsType(ctx) {
172 case ext4Type:
173 f.output = f.buildImageUsingBuildImage(ctx)
174 case compressedCpioType:
Jiyong Park837cdb22021-02-05 00:17:14 +0900175 f.output = f.buildCpioImage(ctx, true)
176 case cpioType:
177 f.output = f.buildCpioImage(ctx, false)
Jiyong Park11a65972021-02-01 21:09:38 +0900178 default:
179 return
180 }
181
182 f.installDir = android.PathForModuleInstall(ctx, "etc")
183 ctx.InstallFile(f.installDir, f.installFileName(), f.output)
184}
185
Jiyong Parkfa616132021-04-20 11:36:40 +0900186// root zip will contain extra files/dirs that are not from the `deps` property.
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900187func (f *filesystem) buildRootZip(ctx android.ModuleContext) android.OutputPath {
188 rootDir := android.PathForModuleGen(ctx, "root").OutputPath
189 builder := android.NewRuleBuilder(pctx, ctx)
190 builder.Command().Text("rm -rf").Text(rootDir.String())
191 builder.Command().Text("mkdir -p").Text(rootDir.String())
192
Inseob Kim14199b02021-02-09 21:18:31 +0900193 // create dirs and symlinks
194 for _, dir := range f.properties.Dirs {
195 // OutputPath.Join verifies dir
196 builder.Command().Text("mkdir -p").Text(rootDir.Join(ctx, dir).String())
197 }
198
199 for _, symlink := range f.properties.Symlinks {
200 name := strings.TrimSpace(proptools.String(symlink.Name))
201 target := strings.TrimSpace(proptools.String(symlink.Target))
202
203 if name == "" {
204 ctx.PropertyErrorf("symlinks", "Name can't be empty")
205 continue
206 }
207
208 if target == "" {
209 ctx.PropertyErrorf("symlinks", "Target can't be empty")
210 continue
211 }
212
213 // OutputPath.Join verifies name. don't need to verify target.
214 dst := rootDir.Join(ctx, name)
215
216 builder.Command().Text("mkdir -p").Text(filepath.Dir(dst.String()))
217 builder.Command().Text("ln -sf").Text(proptools.ShellEscape(target)).Text(dst.String())
218 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900219
Jiyong Parkfa616132021-04-20 11:36:40 +0900220 // create extra files if there's any
221 rootForExtraFiles := android.PathForModuleGen(ctx, "root-extra").OutputPath
222 var extraFiles android.OutputPaths
223 if f.buildExtraFiles != nil {
224 extraFiles = f.buildExtraFiles(ctx, rootForExtraFiles)
225 for _, f := range extraFiles {
226 rel, _ := filepath.Rel(rootForExtraFiles.String(), f.String())
227 if strings.HasPrefix(rel, "..") {
228 panic(fmt.Errorf("%q is not under %q\n", f, rootForExtraFiles))
229 }
230 }
231 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900232
Jiyong Parkfa616132021-04-20 11:36:40 +0900233 // Zip them all
234 zipOut := android.PathForModuleGen(ctx, "root.zip").OutputPath
235 zipCommand := builder.Command().BuiltTool("soong_zip")
236 zipCommand.FlagWithOutput("-o ", zipOut).
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900237 FlagWithArg("-C ", rootDir.String()).
238 Flag("-L 0"). // no compression because this will be unzipped soon
239 FlagWithArg("-D ", rootDir.String()).
240 Flag("-d") // include empty directories
Jiyong Parkfa616132021-04-20 11:36:40 +0900241 if len(extraFiles) > 0 {
242 zipCommand.FlagWithArg("-C ", rootForExtraFiles.String())
243 for _, f := range extraFiles {
244 zipCommand.FlagWithInput("-f ", f)
245 }
246 }
247
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900248 builder.Command().Text("rm -rf").Text(rootDir.String())
249
250 builder.Build("zip_root", fmt.Sprintf("zipping root contents for %s", ctx.ModuleName()))
251 return zipOut
252}
253
Jiyong Park11a65972021-02-01 21:09:38 +0900254func (f *filesystem) buildImageUsingBuildImage(ctx android.ModuleContext) android.OutputPath {
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900255 depsZipFile := android.PathForModuleOut(ctx, "deps.zip").OutputPath
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900256 f.entries = f.CopyDepsToZip(ctx, f.gatherFilteredPackagingSpecs(ctx), depsZipFile)
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900257
258 builder := android.NewRuleBuilder(pctx, ctx)
259 depsBase := proptools.StringDefault(f.properties.Base_dir, ".")
260 rebasedDepsZip := android.PathForModuleOut(ctx, "rebased_deps.zip").OutputPath
261 builder.Command().
262 BuiltTool("zip2zip").
263 FlagWithInput("-i ", depsZipFile).
264 FlagWithOutput("-o ", rebasedDepsZip).
265 Text("**/*:" + proptools.ShellEscape(depsBase)) // zip2zip verifies depsBase
Jiyong Park6f0f6882020-11-12 13:14:30 +0900266
267 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900268 rootZip := f.buildRootZip(ctx)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900269 builder.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800270 BuiltTool("zipsync").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900271 FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear.
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900272 Input(rootZip).
273 Input(rebasedDepsZip)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900274
Nikita Ioffe519015f2022-12-23 15:36:29 +0000275 // run host_init_verifier
276 // Ideally we should have a concept of pluggable linters that verify the generated image.
277 // While such concept is not implement this will do.
278 // TODO(b/263574231): substitute with pluggable linter.
279 builder.Command().
280 BuiltTool("host_init_verifier").
281 FlagWithArg("--out_system=", rootDir.String()+"/system")
282
Jiyong Park72678312021-01-18 17:29:49 +0900283 propFile, toolDeps := f.buildPropFile(ctx)
Jiyong Park11a65972021-02-01 21:09:38 +0900284 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -0800285 builder.Command().BuiltTool("build_image").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900286 Text(rootDir.String()). // input directory
287 Input(propFile).
Jiyong Park72678312021-01-18 17:29:49 +0900288 Implicits(toolDeps).
Jiyong Park11a65972021-02-01 21:09:38 +0900289 Output(output).
Jiyong Park6f0f6882020-11-12 13:14:30 +0900290 Text(rootDir.String()) // directory where to find fs_config_files|dirs
291
292 // rootDir is not deleted. Might be useful for quick inspection.
Colin Crossf1a035e2020-11-16 17:32:30 -0800293 builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park65c49f52020-11-24 14:23:26 +0900294
Jiyong Park11a65972021-02-01 21:09:38 +0900295 return output
Jiyong Park65c49f52020-11-24 14:23:26 +0900296}
297
Inseob Kimcc8e5362021-02-03 14:05:24 +0900298func (f *filesystem) buildFileContexts(ctx android.ModuleContext) android.OutputPath {
299 builder := android.NewRuleBuilder(pctx, ctx)
300 fcBin := android.PathForModuleOut(ctx, "file_contexts.bin")
301 builder.Command().BuiltTool("sefcontext_compile").
302 FlagWithOutput("-o ", fcBin).
303 Input(android.PathForModuleSrc(ctx, proptools.String(f.properties.File_contexts)))
304 builder.Build("build_filesystem_file_contexts", fmt.Sprintf("Creating filesystem file contexts for %s", f.BaseModuleName()))
305 return fcBin.OutputPath
306}
307
Jooyung Han65f402b2022-04-21 14:24:04 +0900308// Calculates avb_salt from entry list (sorted) for deterministic output.
309func (f *filesystem) salt() string {
310 return sha1sum(f.entries)
311}
312
Jiyong Park72678312021-01-18 17:29:49 +0900313func (f *filesystem) buildPropFile(ctx android.ModuleContext) (propFile android.OutputPath, toolDeps android.Paths) {
Jiyong Park72678312021-01-18 17:29:49 +0900314 var deps android.Paths
Cole Faustcec230a2024-03-07 15:51:12 -0800315 var propFileString strings.Builder
Jiyong Park72678312021-01-18 17:29:49 +0900316 addStr := func(name string, value string) {
Cole Faustcec230a2024-03-07 15:51:12 -0800317 propFileString.WriteString(name)
318 propFileString.WriteRune('=')
319 propFileString.WriteString(value)
320 propFileString.WriteRune('\n')
Jiyong Park72678312021-01-18 17:29:49 +0900321 }
322 addPath := func(name string, path android.Path) {
Cole Faustcec230a2024-03-07 15:51:12 -0800323 addStr(name, path.String())
Jiyong Park72678312021-01-18 17:29:49 +0900324 deps = append(deps, path)
325 }
326
Jiyong Park11a65972021-02-01 21:09:38 +0900327 // Type string that build_image.py accepts.
328 fsTypeStr := func(t fsType) string {
329 switch t {
330 // TODO(jiyong): add more types like f2fs, erofs, etc.
331 case ext4Type:
332 return "ext4"
333 }
334 panic(fmt.Errorf("unsupported fs type %v", t))
335 }
336
337 addStr("fs_type", fsTypeStr(f.fsType(ctx)))
Inseob Kim376d72f2023-11-01 15:40:25 +0900338 addStr("mount_point", proptools.StringDefault(f.properties.Mount_point, "/"))
Jiyong Park72678312021-01-18 17:29:49 +0900339 addStr("use_dynamic_partition_size", "true")
340 addPath("ext_mkuserimg", ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs"))
341 // b/177813163 deps of the host tools have to be added. Remove this.
342 for _, t := range []string{"mke2fs", "e2fsdroid", "tune2fs"} {
343 deps = append(deps, ctx.Config().HostToolPath(ctx, t))
344 }
345
Jiyong Park71baa762021-01-18 21:11:03 +0900346 if proptools.Bool(f.properties.Use_avb) {
347 addStr("avb_hashtree_enable", "true")
348 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
349 algorithm := proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096")
350 addStr("avb_algorithm", algorithm)
351 key := android.PathForModuleSrc(ctx, proptools.String(f.properties.Avb_private_key))
352 addPath("avb_key_path", key)
Seungjae Yooa30e4502023-11-09 14:55:44 +0900353 partitionName := proptools.StringDefault(f.properties.Partition_name, f.Name())
354 addStr("partition_name", 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 }
Seungjae Yooa30e4502023-11-09 14:55:44 +0900359 securityPatchKey := "com.android.build." + partitionName + ".security_patch"
360 securityPatchValue := ctx.Config().PlatformSecurityPatch()
361 avb_add_hashtree_footer_args += " --prop " + securityPatchKey + ":" + securityPatchValue
Shikha Panware6f30632022-12-21 12:54:45 +0000362 addStr("avb_add_hashtree_footer_args", avb_add_hashtree_footer_args)
Jooyung Han65f402b2022-04-21 14:24:04 +0900363 addStr("avb_salt", f.salt())
Jiyong Park71baa762021-01-18 21:11:03 +0900364 }
365
Inseob Kimcc8e5362021-02-03 14:05:24 +0900366 if proptools.String(f.properties.File_contexts) != "" {
367 addPath("selinux_fc", f.buildFileContexts(ctx))
368 }
Jooyung Han65f402b2022-04-21 14:24:04 +0900369 if timestamp := proptools.String(f.properties.Fake_timestamp); timestamp != "" {
370 addStr("timestamp", timestamp)
371 }
372 if uuid := proptools.String(f.properties.Uuid); uuid != "" {
373 addStr("uuid", uuid)
374 addStr("hash_seed", uuid)
375 }
Jiyong Park72678312021-01-18 17:29:49 +0900376 propFile = android.PathForModuleOut(ctx, "prop").OutputPath
Cole Faustcec230a2024-03-07 15:51:12 -0800377 android.WriteFileRuleVerbatim(ctx, propFile, propFileString.String())
Jiyong Park72678312021-01-18 17:29:49 +0900378 return propFile, deps
379}
380
Jiyong Park837cdb22021-02-05 00:17:14 +0900381func (f *filesystem) buildCpioImage(ctx android.ModuleContext, compressed bool) android.OutputPath {
Jiyong Park11a65972021-02-01 21:09:38 +0900382 if proptools.Bool(f.properties.Use_avb) {
383 ctx.PropertyErrorf("use_avb", "signing compresed cpio image using avbtool is not supported."+
384 "Consider adding this to bootimg module and signing the entire boot image.")
385 }
386
Inseob Kimcc8e5362021-02-03 14:05:24 +0900387 if proptools.String(f.properties.File_contexts) != "" {
388 ctx.PropertyErrorf("file_contexts", "file_contexts is not supported for compressed cpio image.")
389 }
390
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900391 depsZipFile := android.PathForModuleOut(ctx, "deps.zip").OutputPath
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900392 f.entries = f.CopyDepsToZip(ctx, f.gatherFilteredPackagingSpecs(ctx), depsZipFile)
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900393
394 builder := android.NewRuleBuilder(pctx, ctx)
395 depsBase := proptools.StringDefault(f.properties.Base_dir, ".")
396 rebasedDepsZip := android.PathForModuleOut(ctx, "rebased_deps.zip").OutputPath
397 builder.Command().
398 BuiltTool("zip2zip").
399 FlagWithInput("-i ", depsZipFile).
400 FlagWithOutput("-o ", rebasedDepsZip).
401 Text("**/*:" + proptools.ShellEscape(depsBase)) // zip2zip verifies depsBase
Jiyong Park11a65972021-02-01 21:09:38 +0900402
403 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900404 rootZip := f.buildRootZip(ctx)
Jiyong Park11a65972021-02-01 21:09:38 +0900405 builder.Command().
406 BuiltTool("zipsync").
407 FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear.
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900408 Input(rootZip).
409 Input(rebasedDepsZip)
Jiyong Park11a65972021-02-01 21:09:38 +0900410
411 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Jiyong Park837cdb22021-02-05 00:17:14 +0900412 cmd := builder.Command().
Jiyong Park11a65972021-02-01 21:09:38 +0900413 BuiltTool("mkbootfs").
Jiyong Park837cdb22021-02-05 00:17:14 +0900414 Text(rootDir.String()) // input directory
415 if compressed {
416 cmd.Text("|").
417 BuiltTool("lz4").
418 Flag("--favor-decSpeed"). // for faster boot
419 Flag("-12"). // maximum compression level
420 Flag("-l"). // legacy format for kernel
421 Text(">").Output(output)
422 } else {
423 cmd.Text(">").Output(output)
424 }
Jiyong Park11a65972021-02-01 21:09:38 +0900425
426 // rootDir is not deleted. Might be useful for quick inspection.
Jiyong Park837cdb22021-02-05 00:17:14 +0900427 builder.Build("build_cpio_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park11a65972021-02-01 21:09:38 +0900428
429 return output
430}
431
Jiyong Park65c49f52020-11-24 14:23:26 +0900432var _ android.AndroidMkEntriesProvider = (*filesystem)(nil)
433
434// Implements android.AndroidMkEntriesProvider
435func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries {
436 return []android.AndroidMkEntries{android.AndroidMkEntries{
437 Class: "ETC",
438 OutputFile: android.OptionalPathForPath(f.output),
439 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700440 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800441 entries.SetString("LOCAL_MODULE_PATH", f.installDir.String())
Jiyong Park65c49f52020-11-24 14:23:26 +0900442 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
443 },
444 },
445 }}
Jiyong Park6f0f6882020-11-12 13:14:30 +0900446}
Jiyong Park12a719c2021-01-07 15:31:24 +0900447
Jiyong Park940dfd42021-02-04 15:37:34 +0900448var _ android.OutputFileProducer = (*filesystem)(nil)
449
450// Implements android.OutputFileProducer
451func (f *filesystem) OutputFiles(tag string) (android.Paths, error) {
452 if tag == "" {
453 return []android.Path{f.output}, nil
454 }
455 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
456}
457
Jiyong Park12a719c2021-01-07 15:31:24 +0900458// Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex
459// package to have access to the output file.
460type Filesystem interface {
461 android.Module
462 OutputPath() android.Path
Jiyong Park972e06c2021-03-15 23:32:49 +0900463
464 // Returns the output file that is signed by avbtool. If this module is not signed, returns
465 // nil.
466 SignedOutputPath() android.Path
Jiyong Park12a719c2021-01-07 15:31:24 +0900467}
468
469var _ Filesystem = (*filesystem)(nil)
470
471func (f *filesystem) OutputPath() android.Path {
472 return f.output
473}
Jiyong Park972e06c2021-03-15 23:32:49 +0900474
475func (f *filesystem) SignedOutputPath() android.Path {
476 if proptools.Bool(f.properties.Use_avb) {
477 return f.OutputPath()
478 }
479 return nil
480}
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900481
482// Filter the result of GatherPackagingSpecs to discard items targeting outside "system" partition.
483// Note that "apex" module installs its contents to "apex"(fake partition) as well
484// for symbol lookup by imitating "activated" paths.
485func (f *filesystem) gatherFilteredPackagingSpecs(ctx android.ModuleContext) map[string]android.PackagingSpec {
Jeongik Cha54bf8752024-02-08 10:44:37 +0900486 specs := f.PackagingBase.GatherPackagingSpecsWithFilter(ctx, f.filterPackagingSpec)
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900487 return specs
488}
Jooyung Han65f402b2022-04-21 14:24:04 +0900489
490func sha1sum(values []string) string {
491 h := sha256.New()
492 for _, value := range values {
493 io.WriteString(h, value)
494 }
495 return fmt.Sprintf("%x", h.Sum(nil))
496}
Jooyung Hane6067592023-03-16 13:11:17 +0900497
498// Base cc.UseCoverage
499
500var _ cc.UseCoverage = (*filesystem)(nil)
501
Colin Crossf5f4ad32024-01-19 15:41:48 -0800502func (*filesystem) IsNativeCoverageNeeded(ctx android.IncomingTransitionContext) bool {
Jooyung Hane6067592023-03-16 13:11:17 +0900503 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
504}