blob: 023c69adfa1679bdb21e462b4d7d7547e6744c07 [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)
Alice Wang000e3a32023-01-03 16:11:20 +000039 ctx.RegisterModuleType("avb_gen_vbmeta_image", avbGenVbmetaImageFactory)
Jiyong Park6f0f6882020-11-12 13:14:30 +090040}
41
42type filesystem struct {
43 android.ModuleBase
44 android.PackagingBase
Jiyong Park65c49f52020-11-24 14:23:26 +090045
Jiyong Park71baa762021-01-18 21:11:03 +090046 properties filesystemProperties
47
Jiyong Parkfa616132021-04-20 11:36:40 +090048 // Function that builds extra files under the root directory and returns the files
49 buildExtraFiles func(ctx android.ModuleContext, root android.OutputPath) android.OutputPaths
50
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090051 // Function that filters PackagingSpecs returned by PackagingBase.GatherPackagingSpecs()
52 filterPackagingSpecs func(specs map[string]android.PackagingSpec)
53
Jiyong Park65c49f52020-11-24 14:23:26 +090054 output android.OutputPath
55 installDir android.InstallPath
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090056
57 // For testing. Keeps the result of CopyDepsToZip()
58 entries []string
Jiyong Park6f0f6882020-11-12 13:14:30 +090059}
60
Inseob Kim14199b02021-02-09 21:18:31 +090061type symlinkDefinition struct {
62 Target *string
63 Name *string
64}
65
Jiyong Park71baa762021-01-18 21:11:03 +090066type filesystemProperties struct {
67 // When set to true, sign the image with avbtool. Default is false.
68 Use_avb *bool
69
70 // Path to the private key that avbtool will use to sign this filesystem image.
71 // TODO(jiyong): allow apex_key to be specified here
72 Avb_private_key *string `android:"path"`
73
Shikha Panwar01403bb2022-12-22 12:22:57 +000074 // Signing algorithm for avbtool. Default is SHA256_RSA4096.
Jiyong Park71baa762021-01-18 21:11:03 +090075 Avb_algorithm *string
Jiyong Park11a65972021-02-01 21:09:38 +090076
Shikha Panwar01403bb2022-12-22 12:22:57 +000077 // Hash algorithm used for avbtool (for descriptors). This is passed as hash_algorithm to
78 // avbtool. Default used by avbtool is sha1.
Shikha Panware6f30632022-12-21 12:54:45 +000079 Avb_hash_algorithm *string
80
Jiyong Parkac4076d2021-03-15 23:21:30 +090081 // Name of the partition stored in vbmeta desc. Defaults to the name of this module.
82 Partition_name *string
83
Jiyong Park837cdb22021-02-05 00:17:14 +090084 // Type of the filesystem. Currently, ext4, cpio, and compressed_cpio are supported. Default
85 // is ext4.
Jiyong Park11a65972021-02-01 21:09:38 +090086 Type *string
Inseob Kimcc8e5362021-02-03 14:05:24 +090087
88 // file_contexts file to make image. Currently, only ext4 is supported.
89 File_contexts *string `android:"path"`
Inseob Kim2ce1b5d2021-02-15 17:01:04 +090090
91 // Base directory relative to root, to which deps are installed, e.g. "system". Default is "."
92 // (root).
93 Base_dir *string
Inseob Kim14199b02021-02-09 21:18:31 +090094
95 // Directories to be created under root. e.g. /dev, /proc, etc.
96 Dirs []string
97
98 // Symbolic links to be created under root with "ln -sf <target> <name>".
99 Symlinks []symlinkDefinition
Jooyung Han65f402b2022-04-21 14:24:04 +0900100
101 // Seconds since unix epoch to override timestamps of file entries
102 Fake_timestamp *string
103
104 // When set, passed to mkuserimg_mke2fs --mke2fs_uuid & --mke2fs_hash_seed.
105 // Otherwise, they'll be set as random which might cause indeterministic build output.
106 Uuid *string
Jiyong Park71baa762021-01-18 21:11:03 +0900107}
108
Jiyong Park65c49f52020-11-24 14:23:26 +0900109// android_filesystem packages a set of modules and their transitive dependencies into a filesystem
110// image. The filesystem images are expected to be mounted in the target device, which means the
111// modules in the filesystem image are built for the target device (i.e. Android, not Linux host).
112// The modules are placed in the filesystem image just like they are installed to the ordinary
113// partitions like system.img. For example, cc_library modules are placed under ./lib[64] directory.
Jiyong Park6f0f6882020-11-12 13:14:30 +0900114func filesystemFactory() android.Module {
115 module := &filesystem{}
Jiyong Parkfa616132021-04-20 11:36:40 +0900116 initFilesystemModule(module)
117 return module
118}
119
120func initFilesystemModule(module *filesystem) {
Jiyong Park71baa762021-01-18 21:11:03 +0900121 module.AddProperties(&module.properties)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900122 android.InitPackageModule(module)
123 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900124}
125
Jiyong Park12a719c2021-01-07 15:31:24 +0900126var dependencyTag = struct {
127 blueprint.BaseDependencyTag
Jooyung Han092ef812021-03-10 15:40:34 +0900128 android.PackagingItemAlwaysDepTag
Jiyong Park12a719c2021-01-07 15:31:24 +0900129}{}
Jiyong Park65b62242020-11-25 12:44:59 +0900130
Jiyong Park6f0f6882020-11-12 13:14:30 +0900131func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park65b62242020-11-25 12:44:59 +0900132 f.AddDeps(ctx, dependencyTag)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900133}
134
Jiyong Park11a65972021-02-01 21:09:38 +0900135type fsType int
136
137const (
138 ext4Type fsType = iota
139 compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900140 cpioType // uncompressed
Jiyong Park11a65972021-02-01 21:09:38 +0900141 unknown
142)
143
144func (f *filesystem) fsType(ctx android.ModuleContext) fsType {
145 typeStr := proptools.StringDefault(f.properties.Type, "ext4")
146 switch typeStr {
147 case "ext4":
148 return ext4Type
149 case "compressed_cpio":
150 return compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900151 case "cpio":
152 return cpioType
Jiyong Park11a65972021-02-01 21:09:38 +0900153 default:
154 ctx.PropertyErrorf("type", "%q not supported", typeStr)
155 return unknown
156 }
157}
158
Jiyong Park65c49f52020-11-24 14:23:26 +0900159func (f *filesystem) installFileName() string {
160 return f.BaseModuleName() + ".img"
161}
162
Jiyong Park6f0f6882020-11-12 13:14:30 +0900163var pctx = android.NewPackageContext("android/soong/filesystem")
164
165func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park11a65972021-02-01 21:09:38 +0900166 switch f.fsType(ctx) {
167 case ext4Type:
168 f.output = f.buildImageUsingBuildImage(ctx)
169 case compressedCpioType:
Jiyong Park837cdb22021-02-05 00:17:14 +0900170 f.output = f.buildCpioImage(ctx, true)
171 case cpioType:
172 f.output = f.buildCpioImage(ctx, false)
Jiyong Park11a65972021-02-01 21:09:38 +0900173 default:
174 return
175 }
176
177 f.installDir = android.PathForModuleInstall(ctx, "etc")
178 ctx.InstallFile(f.installDir, f.installFileName(), f.output)
179}
180
Jiyong Parkfa616132021-04-20 11:36:40 +0900181// root zip will contain extra files/dirs that are not from the `deps` property.
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900182func (f *filesystem) buildRootZip(ctx android.ModuleContext) android.OutputPath {
183 rootDir := android.PathForModuleGen(ctx, "root").OutputPath
184 builder := android.NewRuleBuilder(pctx, ctx)
185 builder.Command().Text("rm -rf").Text(rootDir.String())
186 builder.Command().Text("mkdir -p").Text(rootDir.String())
187
Inseob Kim14199b02021-02-09 21:18:31 +0900188 // create dirs and symlinks
189 for _, dir := range f.properties.Dirs {
190 // OutputPath.Join verifies dir
191 builder.Command().Text("mkdir -p").Text(rootDir.Join(ctx, dir).String())
192 }
193
194 for _, symlink := range f.properties.Symlinks {
195 name := strings.TrimSpace(proptools.String(symlink.Name))
196 target := strings.TrimSpace(proptools.String(symlink.Target))
197
198 if name == "" {
199 ctx.PropertyErrorf("symlinks", "Name can't be empty")
200 continue
201 }
202
203 if target == "" {
204 ctx.PropertyErrorf("symlinks", "Target can't be empty")
205 continue
206 }
207
208 // OutputPath.Join verifies name. don't need to verify target.
209 dst := rootDir.Join(ctx, name)
210
211 builder.Command().Text("mkdir -p").Text(filepath.Dir(dst.String()))
212 builder.Command().Text("ln -sf").Text(proptools.ShellEscape(target)).Text(dst.String())
213 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900214
Jiyong Parkfa616132021-04-20 11:36:40 +0900215 // create extra files if there's any
216 rootForExtraFiles := android.PathForModuleGen(ctx, "root-extra").OutputPath
217 var extraFiles android.OutputPaths
218 if f.buildExtraFiles != nil {
219 extraFiles = f.buildExtraFiles(ctx, rootForExtraFiles)
220 for _, f := range extraFiles {
221 rel, _ := filepath.Rel(rootForExtraFiles.String(), f.String())
222 if strings.HasPrefix(rel, "..") {
223 panic(fmt.Errorf("%q is not under %q\n", f, rootForExtraFiles))
224 }
225 }
226 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900227
Jiyong Parkfa616132021-04-20 11:36:40 +0900228 // Zip them all
229 zipOut := android.PathForModuleGen(ctx, "root.zip").OutputPath
230 zipCommand := builder.Command().BuiltTool("soong_zip")
231 zipCommand.FlagWithOutput("-o ", zipOut).
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900232 FlagWithArg("-C ", rootDir.String()).
233 Flag("-L 0"). // no compression because this will be unzipped soon
234 FlagWithArg("-D ", rootDir.String()).
235 Flag("-d") // include empty directories
Jiyong Parkfa616132021-04-20 11:36:40 +0900236 if len(extraFiles) > 0 {
237 zipCommand.FlagWithArg("-C ", rootForExtraFiles.String())
238 for _, f := range extraFiles {
239 zipCommand.FlagWithInput("-f ", f)
240 }
241 }
242
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900243 builder.Command().Text("rm -rf").Text(rootDir.String())
244
245 builder.Build("zip_root", fmt.Sprintf("zipping root contents for %s", ctx.ModuleName()))
246 return zipOut
247}
248
Jiyong Park11a65972021-02-01 21:09:38 +0900249func (f *filesystem) buildImageUsingBuildImage(ctx android.ModuleContext) android.OutputPath {
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900250 depsZipFile := android.PathForModuleOut(ctx, "deps.zip").OutputPath
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900251 f.entries = f.CopyDepsToZip(ctx, f.gatherFilteredPackagingSpecs(ctx), depsZipFile)
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900252
253 builder := android.NewRuleBuilder(pctx, ctx)
254 depsBase := proptools.StringDefault(f.properties.Base_dir, ".")
255 rebasedDepsZip := android.PathForModuleOut(ctx, "rebased_deps.zip").OutputPath
256 builder.Command().
257 BuiltTool("zip2zip").
258 FlagWithInput("-i ", depsZipFile).
259 FlagWithOutput("-o ", rebasedDepsZip).
260 Text("**/*:" + proptools.ShellEscape(depsBase)) // zip2zip verifies depsBase
Jiyong Park6f0f6882020-11-12 13:14:30 +0900261
262 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900263 rootZip := f.buildRootZip(ctx)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900264 builder.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800265 BuiltTool("zipsync").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900266 FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear.
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900267 Input(rootZip).
268 Input(rebasedDepsZip)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900269
Nikita Ioffe519015f2022-12-23 15:36:29 +0000270 // run host_init_verifier
271 // Ideally we should have a concept of pluggable linters that verify the generated image.
272 // While such concept is not implement this will do.
273 // TODO(b/263574231): substitute with pluggable linter.
274 builder.Command().
275 BuiltTool("host_init_verifier").
276 FlagWithArg("--out_system=", rootDir.String()+"/system")
277
Jiyong Park72678312021-01-18 17:29:49 +0900278 propFile, toolDeps := f.buildPropFile(ctx)
Jiyong Park11a65972021-02-01 21:09:38 +0900279 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -0800280 builder.Command().BuiltTool("build_image").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900281 Text(rootDir.String()). // input directory
282 Input(propFile).
Jiyong Park72678312021-01-18 17:29:49 +0900283 Implicits(toolDeps).
Jiyong Park11a65972021-02-01 21:09:38 +0900284 Output(output).
Jiyong Park6f0f6882020-11-12 13:14:30 +0900285 Text(rootDir.String()) // directory where to find fs_config_files|dirs
286
287 // rootDir is not deleted. Might be useful for quick inspection.
Colin Crossf1a035e2020-11-16 17:32:30 -0800288 builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park65c49f52020-11-24 14:23:26 +0900289
Jiyong Park11a65972021-02-01 21:09:38 +0900290 return output
Jiyong Park65c49f52020-11-24 14:23:26 +0900291}
292
Inseob Kimcc8e5362021-02-03 14:05:24 +0900293func (f *filesystem) buildFileContexts(ctx android.ModuleContext) android.OutputPath {
294 builder := android.NewRuleBuilder(pctx, ctx)
295 fcBin := android.PathForModuleOut(ctx, "file_contexts.bin")
296 builder.Command().BuiltTool("sefcontext_compile").
297 FlagWithOutput("-o ", fcBin).
298 Input(android.PathForModuleSrc(ctx, proptools.String(f.properties.File_contexts)))
299 builder.Build("build_filesystem_file_contexts", fmt.Sprintf("Creating filesystem file contexts for %s", f.BaseModuleName()))
300 return fcBin.OutputPath
301}
302
Jooyung Han65f402b2022-04-21 14:24:04 +0900303// Calculates avb_salt from entry list (sorted) for deterministic output.
304func (f *filesystem) salt() string {
305 return sha1sum(f.entries)
306}
307
Jiyong Park72678312021-01-18 17:29:49 +0900308func (f *filesystem) buildPropFile(ctx android.ModuleContext) (propFile android.OutputPath, toolDeps android.Paths) {
309 type prop struct {
310 name string
311 value string
312 }
313
314 var props []prop
315 var deps android.Paths
316 addStr := func(name string, value string) {
317 props = append(props, prop{name, value})
318 }
319 addPath := func(name string, path android.Path) {
320 props = append(props, prop{name, path.String()})
321 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 Kim2ce1b5d2021-02-15 17:01:04 +0900335 addStr("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)
Shikha Panware6f30632022-12-21 12:54:45 +0000350 avb_add_hashtree_footer_args := "--do_not_generate_fec"
351 if hashAlgorithm := proptools.String(f.properties.Avb_hash_algorithm); hashAlgorithm != "" {
352 avb_add_hashtree_footer_args += " --hash_algorithm " + hashAlgorithm
353 }
354 addStr("avb_add_hashtree_footer_args", avb_add_hashtree_footer_args)
Jiyong Parkac4076d2021-03-15 23:21:30 +0900355 partitionName := proptools.StringDefault(f.properties.Partition_name, f.Name())
356 addStr("partition_name", partitionName)
Jooyung Han65f402b2022-04-21 14:24:04 +0900357 addStr("avb_salt", f.salt())
Jiyong Park71baa762021-01-18 21:11:03 +0900358 }
359
Inseob Kimcc8e5362021-02-03 14:05:24 +0900360 if proptools.String(f.properties.File_contexts) != "" {
361 addPath("selinux_fc", f.buildFileContexts(ctx))
362 }
Jooyung Han65f402b2022-04-21 14:24:04 +0900363 if timestamp := proptools.String(f.properties.Fake_timestamp); timestamp != "" {
364 addStr("timestamp", timestamp)
365 }
366 if uuid := proptools.String(f.properties.Uuid); uuid != "" {
367 addStr("uuid", uuid)
368 addStr("hash_seed", uuid)
369 }
Jiyong Park72678312021-01-18 17:29:49 +0900370 propFile = android.PathForModuleOut(ctx, "prop").OutputPath
371 builder := android.NewRuleBuilder(pctx, ctx)
372 builder.Command().Text("rm").Flag("-rf").Output(propFile)
373 for _, p := range props {
374 builder.Command().
Jiyong Park3db465d2021-01-26 14:08:16 +0900375 Text("echo").
Jiyong Park72678312021-01-18 17:29:49 +0900376 Flag(`"` + p.name + "=" + p.value + `"`).
377 Text(">>").Output(propFile)
378 }
379 builder.Build("build_filesystem_prop", fmt.Sprintf("Creating filesystem props for %s", f.BaseModuleName()))
380 return propFile, deps
381}
382
Jiyong Park837cdb22021-02-05 00:17:14 +0900383func (f *filesystem) buildCpioImage(ctx android.ModuleContext, compressed bool) android.OutputPath {
Jiyong Park11a65972021-02-01 21:09:38 +0900384 if proptools.Bool(f.properties.Use_avb) {
385 ctx.PropertyErrorf("use_avb", "signing compresed cpio image using avbtool is not supported."+
386 "Consider adding this to bootimg module and signing the entire boot image.")
387 }
388
Inseob Kimcc8e5362021-02-03 14:05:24 +0900389 if proptools.String(f.properties.File_contexts) != "" {
390 ctx.PropertyErrorf("file_contexts", "file_contexts is not supported for compressed cpio image.")
391 }
392
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900393 depsZipFile := android.PathForModuleOut(ctx, "deps.zip").OutputPath
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900394 f.entries = f.CopyDepsToZip(ctx, f.gatherFilteredPackagingSpecs(ctx), depsZipFile)
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900395
396 builder := android.NewRuleBuilder(pctx, ctx)
397 depsBase := proptools.StringDefault(f.properties.Base_dir, ".")
398 rebasedDepsZip := android.PathForModuleOut(ctx, "rebased_deps.zip").OutputPath
399 builder.Command().
400 BuiltTool("zip2zip").
401 FlagWithInput("-i ", depsZipFile).
402 FlagWithOutput("-o ", rebasedDepsZip).
403 Text("**/*:" + proptools.ShellEscape(depsBase)) // zip2zip verifies depsBase
Jiyong Park11a65972021-02-01 21:09:38 +0900404
405 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900406 rootZip := f.buildRootZip(ctx)
Jiyong Park11a65972021-02-01 21:09:38 +0900407 builder.Command().
408 BuiltTool("zipsync").
409 FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear.
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900410 Input(rootZip).
411 Input(rebasedDepsZip)
Jiyong Park11a65972021-02-01 21:09:38 +0900412
413 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Jiyong Park837cdb22021-02-05 00:17:14 +0900414 cmd := builder.Command().
Jiyong Park11a65972021-02-01 21:09:38 +0900415 BuiltTool("mkbootfs").
Jiyong Park837cdb22021-02-05 00:17:14 +0900416 Text(rootDir.String()) // input directory
417 if compressed {
418 cmd.Text("|").
419 BuiltTool("lz4").
420 Flag("--favor-decSpeed"). // for faster boot
421 Flag("-12"). // maximum compression level
422 Flag("-l"). // legacy format for kernel
423 Text(">").Output(output)
424 } else {
425 cmd.Text(">").Output(output)
426 }
Jiyong Park11a65972021-02-01 21:09:38 +0900427
428 // rootDir is not deleted. Might be useful for quick inspection.
Jiyong Park837cdb22021-02-05 00:17:14 +0900429 builder.Build("build_cpio_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park11a65972021-02-01 21:09:38 +0900430
431 return output
432}
433
Jiyong Park65c49f52020-11-24 14:23:26 +0900434var _ android.AndroidMkEntriesProvider = (*filesystem)(nil)
435
436// Implements android.AndroidMkEntriesProvider
437func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries {
438 return []android.AndroidMkEntries{android.AndroidMkEntries{
439 Class: "ETC",
440 OutputFile: android.OptionalPathForPath(f.output),
441 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700442 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800443 entries.SetString("LOCAL_MODULE_PATH", f.installDir.String())
Jiyong Park65c49f52020-11-24 14:23:26 +0900444 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
445 },
446 },
447 }}
Jiyong Park6f0f6882020-11-12 13:14:30 +0900448}
Jiyong Park12a719c2021-01-07 15:31:24 +0900449
Jiyong Park940dfd42021-02-04 15:37:34 +0900450var _ android.OutputFileProducer = (*filesystem)(nil)
451
452// Implements android.OutputFileProducer
453func (f *filesystem) OutputFiles(tag string) (android.Paths, error) {
454 if tag == "" {
455 return []android.Path{f.output}, nil
456 }
457 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
458}
459
Jiyong Park12a719c2021-01-07 15:31:24 +0900460// Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex
461// package to have access to the output file.
462type Filesystem interface {
463 android.Module
464 OutputPath() android.Path
Jiyong Park972e06c2021-03-15 23:32:49 +0900465
466 // Returns the output file that is signed by avbtool. If this module is not signed, returns
467 // nil.
468 SignedOutputPath() android.Path
Jiyong Park12a719c2021-01-07 15:31:24 +0900469}
470
471var _ Filesystem = (*filesystem)(nil)
472
473func (f *filesystem) OutputPath() android.Path {
474 return f.output
475}
Jiyong Park972e06c2021-03-15 23:32:49 +0900476
477func (f *filesystem) SignedOutputPath() android.Path {
478 if proptools.Bool(f.properties.Use_avb) {
479 return f.OutputPath()
480 }
481 return nil
482}
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900483
484// Filter the result of GatherPackagingSpecs to discard items targeting outside "system" partition.
485// Note that "apex" module installs its contents to "apex"(fake partition) as well
486// for symbol lookup by imitating "activated" paths.
487func (f *filesystem) gatherFilteredPackagingSpecs(ctx android.ModuleContext) map[string]android.PackagingSpec {
488 specs := f.PackagingBase.GatherPackagingSpecs(ctx)
489 if f.filterPackagingSpecs != nil {
490 f.filterPackagingSpecs(specs)
491 }
492 return specs
493}
Jooyung Han65f402b2022-04-21 14:24:04 +0900494
495func sha1sum(values []string) string {
496 h := sha256.New()
497 for _, value := range values {
498 io.WriteString(h, value)
499 }
500 return fmt.Sprintf("%x", h.Sum(nil))
501}
Jooyung Hane6067592023-03-16 13:11:17 +0900502
503// Base cc.UseCoverage
504
505var _ cc.UseCoverage = (*filesystem)(nil)
506
507func (*filesystem) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
508 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
509}