blob: 25b8fe89c05ec959701ce49cf21d593300d34088 [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"
Jiyong Park65b62242020-11-25 12:44:59 +090025
26 "github.com/google/blueprint"
Jiyong Park71baa762021-01-18 21:11:03 +090027 "github.com/google/blueprint/proptools"
Jiyong Park6f0f6882020-11-12 13:14:30 +090028)
29
30func init() {
Jooyung Han9706cbc2021-04-15 22:43:48 +090031 registerBuildComponents(android.InitRegistrationContext)
32}
33
34func registerBuildComponents(ctx android.RegistrationContext) {
35 ctx.RegisterModuleType("android_filesystem", filesystemFactory)
Jiyong Parkfa616132021-04-20 11:36:40 +090036 ctx.RegisterModuleType("android_system_image", systemImageFactory)
Jiyong Parkbc485482022-11-15 22:31:49 +090037 ctx.RegisterModuleType("avb_add_hash_footer", avbAddHashFooterFactory)
Alice Wang000e3a32023-01-03 16:11:20 +000038 ctx.RegisterModuleType("avb_gen_vbmeta_image", avbGenVbmetaImageFactory)
Jiyong Park6f0f6882020-11-12 13:14:30 +090039}
40
41type filesystem struct {
42 android.ModuleBase
43 android.PackagingBase
Jiyong Park65c49f52020-11-24 14:23:26 +090044
Jiyong Park71baa762021-01-18 21:11:03 +090045 properties filesystemProperties
46
Jiyong Parkfa616132021-04-20 11:36:40 +090047 // Function that builds extra files under the root directory and returns the files
48 buildExtraFiles func(ctx android.ModuleContext, root android.OutputPath) android.OutputPaths
49
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090050 // Function that filters PackagingSpecs returned by PackagingBase.GatherPackagingSpecs()
51 filterPackagingSpecs func(specs map[string]android.PackagingSpec)
52
Jiyong Park65c49f52020-11-24 14:23:26 +090053 output android.OutputPath
54 installDir android.InstallPath
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090055
56 // For testing. Keeps the result of CopyDepsToZip()
57 entries []string
Jiyong Park6f0f6882020-11-12 13:14:30 +090058}
59
Inseob Kim14199b02021-02-09 21:18:31 +090060type symlinkDefinition struct {
61 Target *string
62 Name *string
63}
64
Jiyong Park71baa762021-01-18 21:11:03 +090065type filesystemProperties struct {
66 // When set to true, sign the image with avbtool. Default is false.
67 Use_avb *bool
68
69 // Path to the private key that avbtool will use to sign this filesystem image.
70 // TODO(jiyong): allow apex_key to be specified here
71 Avb_private_key *string `android:"path"`
72
Shikha Panwar01403bb2022-12-22 12:22:57 +000073 // Signing algorithm for avbtool. Default is SHA256_RSA4096.
Jiyong Park71baa762021-01-18 21:11:03 +090074 Avb_algorithm *string
Jiyong Park11a65972021-02-01 21:09:38 +090075
Shikha Panwar01403bb2022-12-22 12:22:57 +000076 // Hash algorithm used for avbtool (for descriptors). This is passed as hash_algorithm to
77 // avbtool. Default used by avbtool is sha1.
Shikha Panware6f30632022-12-21 12:54:45 +000078 Avb_hash_algorithm *string
79
Jiyong Parkac4076d2021-03-15 23:21:30 +090080 // Name of the partition stored in vbmeta desc. Defaults to the name of this module.
81 Partition_name *string
82
Jiyong Park837cdb22021-02-05 00:17:14 +090083 // Type of the filesystem. Currently, ext4, cpio, and compressed_cpio are supported. Default
84 // is ext4.
Jiyong Park11a65972021-02-01 21:09:38 +090085 Type *string
Inseob Kimcc8e5362021-02-03 14:05:24 +090086
87 // file_contexts file to make image. Currently, only ext4 is supported.
88 File_contexts *string `android:"path"`
Inseob Kim2ce1b5d2021-02-15 17:01:04 +090089
90 // Base directory relative to root, to which deps are installed, e.g. "system". Default is "."
91 // (root).
92 Base_dir *string
Inseob Kim14199b02021-02-09 21:18:31 +090093
94 // Directories to be created under root. e.g. /dev, /proc, etc.
95 Dirs []string
96
97 // Symbolic links to be created under root with "ln -sf <target> <name>".
98 Symlinks []symlinkDefinition
Jooyung Han65f402b2022-04-21 14:24:04 +090099
100 // Seconds since unix epoch to override timestamps of file entries
101 Fake_timestamp *string
102
103 // When set, passed to mkuserimg_mke2fs --mke2fs_uuid & --mke2fs_hash_seed.
104 // Otherwise, they'll be set as random which might cause indeterministic build output.
105 Uuid *string
Jiyong Park71baa762021-01-18 21:11:03 +0900106}
107
Jiyong Park65c49f52020-11-24 14:23:26 +0900108// android_filesystem packages a set of modules and their transitive dependencies into a filesystem
109// image. The filesystem images are expected to be mounted in the target device, which means the
110// modules in the filesystem image are built for the target device (i.e. Android, not Linux host).
111// The modules are placed in the filesystem image just like they are installed to the ordinary
112// partitions like system.img. For example, cc_library modules are placed under ./lib[64] directory.
Jiyong Park6f0f6882020-11-12 13:14:30 +0900113func filesystemFactory() android.Module {
114 module := &filesystem{}
Jiyong Parkfa616132021-04-20 11:36:40 +0900115 initFilesystemModule(module)
116 return module
117}
118
119func initFilesystemModule(module *filesystem) {
Jiyong Park71baa762021-01-18 21:11:03 +0900120 module.AddProperties(&module.properties)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900121 android.InitPackageModule(module)
122 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900123}
124
Jiyong Park12a719c2021-01-07 15:31:24 +0900125var dependencyTag = struct {
126 blueprint.BaseDependencyTag
Jooyung Han092ef812021-03-10 15:40:34 +0900127 android.PackagingItemAlwaysDepTag
Jiyong Park12a719c2021-01-07 15:31:24 +0900128}{}
Jiyong Park65b62242020-11-25 12:44:59 +0900129
Jiyong Park6f0f6882020-11-12 13:14:30 +0900130func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park65b62242020-11-25 12:44:59 +0900131 f.AddDeps(ctx, dependencyTag)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900132}
133
Jiyong Park11a65972021-02-01 21:09:38 +0900134type fsType int
135
136const (
137 ext4Type fsType = iota
138 compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900139 cpioType // uncompressed
Jiyong Park11a65972021-02-01 21:09:38 +0900140 unknown
141)
142
143func (f *filesystem) fsType(ctx android.ModuleContext) fsType {
144 typeStr := proptools.StringDefault(f.properties.Type, "ext4")
145 switch typeStr {
146 case "ext4":
147 return ext4Type
148 case "compressed_cpio":
149 return compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900150 case "cpio":
151 return cpioType
Jiyong Park11a65972021-02-01 21:09:38 +0900152 default:
153 ctx.PropertyErrorf("type", "%q not supported", typeStr)
154 return unknown
155 }
156}
157
Jiyong Park65c49f52020-11-24 14:23:26 +0900158func (f *filesystem) installFileName() string {
159 return f.BaseModuleName() + ".img"
160}
161
Jiyong Park6f0f6882020-11-12 13:14:30 +0900162var pctx = android.NewPackageContext("android/soong/filesystem")
163
164func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park11a65972021-02-01 21:09:38 +0900165 switch f.fsType(ctx) {
166 case ext4Type:
167 f.output = f.buildImageUsingBuildImage(ctx)
168 case compressedCpioType:
Jiyong Park837cdb22021-02-05 00:17:14 +0900169 f.output = f.buildCpioImage(ctx, true)
170 case cpioType:
171 f.output = f.buildCpioImage(ctx, false)
Jiyong Park11a65972021-02-01 21:09:38 +0900172 default:
173 return
174 }
175
176 f.installDir = android.PathForModuleInstall(ctx, "etc")
177 ctx.InstallFile(f.installDir, f.installFileName(), f.output)
178}
179
Jiyong Parkfa616132021-04-20 11:36:40 +0900180// root zip will contain extra files/dirs that are not from the `deps` property.
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900181func (f *filesystem) buildRootZip(ctx android.ModuleContext) android.OutputPath {
182 rootDir := android.PathForModuleGen(ctx, "root").OutputPath
183 builder := android.NewRuleBuilder(pctx, ctx)
184 builder.Command().Text("rm -rf").Text(rootDir.String())
185 builder.Command().Text("mkdir -p").Text(rootDir.String())
186
Inseob Kim14199b02021-02-09 21:18:31 +0900187 // create dirs and symlinks
188 for _, dir := range f.properties.Dirs {
189 // OutputPath.Join verifies dir
190 builder.Command().Text("mkdir -p").Text(rootDir.Join(ctx, dir).String())
191 }
192
193 for _, symlink := range f.properties.Symlinks {
194 name := strings.TrimSpace(proptools.String(symlink.Name))
195 target := strings.TrimSpace(proptools.String(symlink.Target))
196
197 if name == "" {
198 ctx.PropertyErrorf("symlinks", "Name can't be empty")
199 continue
200 }
201
202 if target == "" {
203 ctx.PropertyErrorf("symlinks", "Target can't be empty")
204 continue
205 }
206
207 // OutputPath.Join verifies name. don't need to verify target.
208 dst := rootDir.Join(ctx, name)
209
210 builder.Command().Text("mkdir -p").Text(filepath.Dir(dst.String()))
211 builder.Command().Text("ln -sf").Text(proptools.ShellEscape(target)).Text(dst.String())
212 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900213
Jiyong Parkfa616132021-04-20 11:36:40 +0900214 // create extra files if there's any
215 rootForExtraFiles := android.PathForModuleGen(ctx, "root-extra").OutputPath
216 var extraFiles android.OutputPaths
217 if f.buildExtraFiles != nil {
218 extraFiles = f.buildExtraFiles(ctx, rootForExtraFiles)
219 for _, f := range extraFiles {
220 rel, _ := filepath.Rel(rootForExtraFiles.String(), f.String())
221 if strings.HasPrefix(rel, "..") {
222 panic(fmt.Errorf("%q is not under %q\n", f, rootForExtraFiles))
223 }
224 }
225 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900226
Jiyong Parkfa616132021-04-20 11:36:40 +0900227 // Zip them all
228 zipOut := android.PathForModuleGen(ctx, "root.zip").OutputPath
229 zipCommand := builder.Command().BuiltTool("soong_zip")
230 zipCommand.FlagWithOutput("-o ", zipOut).
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900231 FlagWithArg("-C ", rootDir.String()).
232 Flag("-L 0"). // no compression because this will be unzipped soon
233 FlagWithArg("-D ", rootDir.String()).
234 Flag("-d") // include empty directories
Jiyong Parkfa616132021-04-20 11:36:40 +0900235 if len(extraFiles) > 0 {
236 zipCommand.FlagWithArg("-C ", rootForExtraFiles.String())
237 for _, f := range extraFiles {
238 zipCommand.FlagWithInput("-f ", f)
239 }
240 }
241
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900242 builder.Command().Text("rm -rf").Text(rootDir.String())
243
244 builder.Build("zip_root", fmt.Sprintf("zipping root contents for %s", ctx.ModuleName()))
245 return zipOut
246}
247
Jiyong Park11a65972021-02-01 21:09:38 +0900248func (f *filesystem) buildImageUsingBuildImage(ctx android.ModuleContext) android.OutputPath {
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900249 depsZipFile := android.PathForModuleOut(ctx, "deps.zip").OutputPath
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900250 f.entries = f.CopyDepsToZip(ctx, f.gatherFilteredPackagingSpecs(ctx), depsZipFile)
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900251
252 builder := android.NewRuleBuilder(pctx, ctx)
253 depsBase := proptools.StringDefault(f.properties.Base_dir, ".")
254 rebasedDepsZip := android.PathForModuleOut(ctx, "rebased_deps.zip").OutputPath
255 builder.Command().
256 BuiltTool("zip2zip").
257 FlagWithInput("-i ", depsZipFile).
258 FlagWithOutput("-o ", rebasedDepsZip).
259 Text("**/*:" + proptools.ShellEscape(depsBase)) // zip2zip verifies depsBase
Jiyong Park6f0f6882020-11-12 13:14:30 +0900260
261 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900262 rootZip := f.buildRootZip(ctx)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900263 builder.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800264 BuiltTool("zipsync").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900265 FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear.
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900266 Input(rootZip).
267 Input(rebasedDepsZip)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900268
Nikita Ioffe519015f2022-12-23 15:36:29 +0000269 // run host_init_verifier
270 // Ideally we should have a concept of pluggable linters that verify the generated image.
271 // While such concept is not implement this will do.
272 // TODO(b/263574231): substitute with pluggable linter.
273 builder.Command().
274 BuiltTool("host_init_verifier").
275 FlagWithArg("--out_system=", rootDir.String()+"/system")
276
Jiyong Park72678312021-01-18 17:29:49 +0900277 propFile, toolDeps := f.buildPropFile(ctx)
Jiyong Park11a65972021-02-01 21:09:38 +0900278 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -0800279 builder.Command().BuiltTool("build_image").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900280 Text(rootDir.String()). // input directory
281 Input(propFile).
Jiyong Park72678312021-01-18 17:29:49 +0900282 Implicits(toolDeps).
Jiyong Park11a65972021-02-01 21:09:38 +0900283 Output(output).
Jiyong Park6f0f6882020-11-12 13:14:30 +0900284 Text(rootDir.String()) // directory where to find fs_config_files|dirs
285
286 // rootDir is not deleted. Might be useful for quick inspection.
Colin Crossf1a035e2020-11-16 17:32:30 -0800287 builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park65c49f52020-11-24 14:23:26 +0900288
Jiyong Park11a65972021-02-01 21:09:38 +0900289 return output
Jiyong Park65c49f52020-11-24 14:23:26 +0900290}
291
Inseob Kimcc8e5362021-02-03 14:05:24 +0900292func (f *filesystem) buildFileContexts(ctx android.ModuleContext) android.OutputPath {
293 builder := android.NewRuleBuilder(pctx, ctx)
294 fcBin := android.PathForModuleOut(ctx, "file_contexts.bin")
295 builder.Command().BuiltTool("sefcontext_compile").
296 FlagWithOutput("-o ", fcBin).
297 Input(android.PathForModuleSrc(ctx, proptools.String(f.properties.File_contexts)))
298 builder.Build("build_filesystem_file_contexts", fmt.Sprintf("Creating filesystem file contexts for %s", f.BaseModuleName()))
299 return fcBin.OutputPath
300}
301
Jooyung Han65f402b2022-04-21 14:24:04 +0900302// Calculates avb_salt from entry list (sorted) for deterministic output.
303func (f *filesystem) salt() string {
304 return sha1sum(f.entries)
305}
306
Jiyong Park72678312021-01-18 17:29:49 +0900307func (f *filesystem) buildPropFile(ctx android.ModuleContext) (propFile android.OutputPath, toolDeps android.Paths) {
308 type prop struct {
309 name string
310 value string
311 }
312
313 var props []prop
314 var deps android.Paths
315 addStr := func(name string, value string) {
316 props = append(props, prop{name, value})
317 }
318 addPath := func(name string, path android.Path) {
319 props = append(props, prop{name, path.String()})
320 deps = append(deps, path)
321 }
322
Jiyong Park11a65972021-02-01 21:09:38 +0900323 // Type string that build_image.py accepts.
324 fsTypeStr := func(t fsType) string {
325 switch t {
326 // TODO(jiyong): add more types like f2fs, erofs, etc.
327 case ext4Type:
328 return "ext4"
329 }
330 panic(fmt.Errorf("unsupported fs type %v", t))
331 }
332
333 addStr("fs_type", fsTypeStr(f.fsType(ctx)))
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900334 addStr("mount_point", "/")
Jiyong Park72678312021-01-18 17:29:49 +0900335 addStr("use_dynamic_partition_size", "true")
336 addPath("ext_mkuserimg", ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs"))
337 // b/177813163 deps of the host tools have to be added. Remove this.
338 for _, t := range []string{"mke2fs", "e2fsdroid", "tune2fs"} {
339 deps = append(deps, ctx.Config().HostToolPath(ctx, t))
340 }
341
Jiyong Park71baa762021-01-18 21:11:03 +0900342 if proptools.Bool(f.properties.Use_avb) {
343 addStr("avb_hashtree_enable", "true")
344 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
345 algorithm := proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096")
346 addStr("avb_algorithm", algorithm)
347 key := android.PathForModuleSrc(ctx, proptools.String(f.properties.Avb_private_key))
348 addPath("avb_key_path", key)
Shikha Panware6f30632022-12-21 12:54:45 +0000349 avb_add_hashtree_footer_args := "--do_not_generate_fec"
350 if hashAlgorithm := proptools.String(f.properties.Avb_hash_algorithm); hashAlgorithm != "" {
351 avb_add_hashtree_footer_args += " --hash_algorithm " + hashAlgorithm
352 }
353 addStr("avb_add_hashtree_footer_args", avb_add_hashtree_footer_args)
Jiyong Parkac4076d2021-03-15 23:21:30 +0900354 partitionName := proptools.StringDefault(f.properties.Partition_name, f.Name())
355 addStr("partition_name", partitionName)
Jooyung Han65f402b2022-04-21 14:24:04 +0900356 addStr("avb_salt", f.salt())
Jiyong Park71baa762021-01-18 21:11:03 +0900357 }
358
Inseob Kimcc8e5362021-02-03 14:05:24 +0900359 if proptools.String(f.properties.File_contexts) != "" {
360 addPath("selinux_fc", f.buildFileContexts(ctx))
361 }
Jooyung Han65f402b2022-04-21 14:24:04 +0900362 if timestamp := proptools.String(f.properties.Fake_timestamp); timestamp != "" {
363 addStr("timestamp", timestamp)
364 }
365 if uuid := proptools.String(f.properties.Uuid); uuid != "" {
366 addStr("uuid", uuid)
367 addStr("hash_seed", uuid)
368 }
Jiyong Park72678312021-01-18 17:29:49 +0900369 propFile = android.PathForModuleOut(ctx, "prop").OutputPath
370 builder := android.NewRuleBuilder(pctx, ctx)
371 builder.Command().Text("rm").Flag("-rf").Output(propFile)
372 for _, p := range props {
373 builder.Command().
Jiyong Park3db465d2021-01-26 14:08:16 +0900374 Text("echo").
Jiyong Park72678312021-01-18 17:29:49 +0900375 Flag(`"` + p.name + "=" + p.value + `"`).
376 Text(">>").Output(propFile)
377 }
378 builder.Build("build_filesystem_prop", fmt.Sprintf("Creating filesystem props for %s", f.BaseModuleName()))
379 return propFile, deps
380}
381
Jiyong Park837cdb22021-02-05 00:17:14 +0900382func (f *filesystem) buildCpioImage(ctx android.ModuleContext, compressed bool) android.OutputPath {
Jiyong Park11a65972021-02-01 21:09:38 +0900383 if proptools.Bool(f.properties.Use_avb) {
384 ctx.PropertyErrorf("use_avb", "signing compresed cpio image using avbtool is not supported."+
385 "Consider adding this to bootimg module and signing the entire boot image.")
386 }
387
Inseob Kimcc8e5362021-02-03 14:05:24 +0900388 if proptools.String(f.properties.File_contexts) != "" {
389 ctx.PropertyErrorf("file_contexts", "file_contexts is not supported for compressed cpio image.")
390 }
391
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900392 depsZipFile := android.PathForModuleOut(ctx, "deps.zip").OutputPath
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900393 f.entries = f.CopyDepsToZip(ctx, f.gatherFilteredPackagingSpecs(ctx), depsZipFile)
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900394
395 builder := android.NewRuleBuilder(pctx, ctx)
396 depsBase := proptools.StringDefault(f.properties.Base_dir, ".")
397 rebasedDepsZip := android.PathForModuleOut(ctx, "rebased_deps.zip").OutputPath
398 builder.Command().
399 BuiltTool("zip2zip").
400 FlagWithInput("-i ", depsZipFile).
401 FlagWithOutput("-o ", rebasedDepsZip).
402 Text("**/*:" + proptools.ShellEscape(depsBase)) // zip2zip verifies depsBase
Jiyong Park11a65972021-02-01 21:09:38 +0900403
404 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900405 rootZip := f.buildRootZip(ctx)
Jiyong Park11a65972021-02-01 21:09:38 +0900406 builder.Command().
407 BuiltTool("zipsync").
408 FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear.
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900409 Input(rootZip).
410 Input(rebasedDepsZip)
Jiyong Park11a65972021-02-01 21:09:38 +0900411
412 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Jiyong Park837cdb22021-02-05 00:17:14 +0900413 cmd := builder.Command().
Jiyong Park11a65972021-02-01 21:09:38 +0900414 BuiltTool("mkbootfs").
Jiyong Park837cdb22021-02-05 00:17:14 +0900415 Text(rootDir.String()) // input directory
416 if compressed {
417 cmd.Text("|").
418 BuiltTool("lz4").
419 Flag("--favor-decSpeed"). // for faster boot
420 Flag("-12"). // maximum compression level
421 Flag("-l"). // legacy format for kernel
422 Text(">").Output(output)
423 } else {
424 cmd.Text(">").Output(output)
425 }
Jiyong Park11a65972021-02-01 21:09:38 +0900426
427 // rootDir is not deleted. Might be useful for quick inspection.
Jiyong Park837cdb22021-02-05 00:17:14 +0900428 builder.Build("build_cpio_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park11a65972021-02-01 21:09:38 +0900429
430 return output
431}
432
Jiyong Park65c49f52020-11-24 14:23:26 +0900433var _ android.AndroidMkEntriesProvider = (*filesystem)(nil)
434
435// Implements android.AndroidMkEntriesProvider
436func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries {
437 return []android.AndroidMkEntries{android.AndroidMkEntries{
438 Class: "ETC",
439 OutputFile: android.OptionalPathForPath(f.output),
440 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700441 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800442 entries.SetString("LOCAL_MODULE_PATH", f.installDir.String())
Jiyong Park65c49f52020-11-24 14:23:26 +0900443 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
444 },
445 },
446 }}
Jiyong Park6f0f6882020-11-12 13:14:30 +0900447}
Jiyong Park12a719c2021-01-07 15:31:24 +0900448
Jiyong Park940dfd42021-02-04 15:37:34 +0900449var _ android.OutputFileProducer = (*filesystem)(nil)
450
451// Implements android.OutputFileProducer
452func (f *filesystem) OutputFiles(tag string) (android.Paths, error) {
453 if tag == "" {
454 return []android.Path{f.output}, nil
455 }
456 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
457}
458
Jiyong Park12a719c2021-01-07 15:31:24 +0900459// Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex
460// package to have access to the output file.
461type Filesystem interface {
462 android.Module
463 OutputPath() android.Path
Jiyong Park972e06c2021-03-15 23:32:49 +0900464
465 // Returns the output file that is signed by avbtool. If this module is not signed, returns
466 // nil.
467 SignedOutputPath() android.Path
Jiyong Park12a719c2021-01-07 15:31:24 +0900468}
469
470var _ Filesystem = (*filesystem)(nil)
471
472func (f *filesystem) OutputPath() android.Path {
473 return f.output
474}
Jiyong Park972e06c2021-03-15 23:32:49 +0900475
476func (f *filesystem) SignedOutputPath() android.Path {
477 if proptools.Bool(f.properties.Use_avb) {
478 return f.OutputPath()
479 }
480 return nil
481}
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900482
483// Filter the result of GatherPackagingSpecs to discard items targeting outside "system" partition.
484// Note that "apex" module installs its contents to "apex"(fake partition) as well
485// for symbol lookup by imitating "activated" paths.
486func (f *filesystem) gatherFilteredPackagingSpecs(ctx android.ModuleContext) map[string]android.PackagingSpec {
487 specs := f.PackagingBase.GatherPackagingSpecs(ctx)
488 if f.filterPackagingSpecs != nil {
489 f.filterPackagingSpecs(specs)
490 }
491 return specs
492}
Jooyung Han65f402b2022-04-21 14:24:04 +0900493
494func sha1sum(values []string) string {
495 h := sha256.New()
496 for _, value := range values {
497 io.WriteString(h, value)
498 }
499 return fmt.Sprintf("%x", h.Sum(nil))
500}