blob: b2caa51d29ea7f0049d70105e5632c21d0200379 [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 (
18 "fmt"
Inseob Kim14199b02021-02-09 21:18:31 +090019 "path/filepath"
20 "strings"
Jiyong Park6f0f6882020-11-12 13:14:30 +090021
22 "android/soong/android"
Jiyong Park65b62242020-11-25 12:44:59 +090023
24 "github.com/google/blueprint"
Jiyong Park71baa762021-01-18 21:11:03 +090025 "github.com/google/blueprint/proptools"
Jiyong Park6f0f6882020-11-12 13:14:30 +090026)
27
28func init() {
Jooyung Han9706cbc2021-04-15 22:43:48 +090029 registerBuildComponents(android.InitRegistrationContext)
30}
31
32func registerBuildComponents(ctx android.RegistrationContext) {
33 ctx.RegisterModuleType("android_filesystem", filesystemFactory)
Jiyong Parkfa616132021-04-20 11:36:40 +090034 ctx.RegisterModuleType("android_system_image", systemImageFactory)
Jiyong Park6f0f6882020-11-12 13:14:30 +090035}
36
37type filesystem struct {
38 android.ModuleBase
39 android.PackagingBase
Jiyong Park65c49f52020-11-24 14:23:26 +090040
Jiyong Park71baa762021-01-18 21:11:03 +090041 properties filesystemProperties
42
Jiyong Parkfa616132021-04-20 11:36:40 +090043 // Function that builds extra files under the root directory and returns the files
44 buildExtraFiles func(ctx android.ModuleContext, root android.OutputPath) android.OutputPaths
45
Jiyong Park65c49f52020-11-24 14:23:26 +090046 output android.OutputPath
47 installDir android.InstallPath
Jiyong Park6f0f6882020-11-12 13:14:30 +090048}
49
Inseob Kim14199b02021-02-09 21:18:31 +090050type symlinkDefinition struct {
51 Target *string
52 Name *string
53}
54
Jiyong Park71baa762021-01-18 21:11:03 +090055type filesystemProperties struct {
56 // When set to true, sign the image with avbtool. Default is false.
57 Use_avb *bool
58
59 // Path to the private key that avbtool will use to sign this filesystem image.
60 // TODO(jiyong): allow apex_key to be specified here
61 Avb_private_key *string `android:"path"`
62
63 // Hash and signing algorithm for avbtool. Default is SHA256_RSA4096.
64 Avb_algorithm *string
Jiyong Park11a65972021-02-01 21:09:38 +090065
Jiyong Parkac4076d2021-03-15 23:21:30 +090066 // Name of the partition stored in vbmeta desc. Defaults to the name of this module.
67 Partition_name *string
68
Jiyong Park837cdb22021-02-05 00:17:14 +090069 // Type of the filesystem. Currently, ext4, cpio, and compressed_cpio are supported. Default
70 // is ext4.
Jiyong Park11a65972021-02-01 21:09:38 +090071 Type *string
Inseob Kimcc8e5362021-02-03 14:05:24 +090072
73 // file_contexts file to make image. Currently, only ext4 is supported.
74 File_contexts *string `android:"path"`
Inseob Kim2ce1b5d2021-02-15 17:01:04 +090075
76 // Base directory relative to root, to which deps are installed, e.g. "system". Default is "."
77 // (root).
78 Base_dir *string
Inseob Kim14199b02021-02-09 21:18:31 +090079
80 // Directories to be created under root. e.g. /dev, /proc, etc.
81 Dirs []string
82
83 // Symbolic links to be created under root with "ln -sf <target> <name>".
84 Symlinks []symlinkDefinition
Jiyong Park71baa762021-01-18 21:11:03 +090085}
86
Jiyong Park65c49f52020-11-24 14:23:26 +090087// android_filesystem packages a set of modules and their transitive dependencies into a filesystem
88// image. The filesystem images are expected to be mounted in the target device, which means the
89// modules in the filesystem image are built for the target device (i.e. Android, not Linux host).
90// The modules are placed in the filesystem image just like they are installed to the ordinary
91// partitions like system.img. For example, cc_library modules are placed under ./lib[64] directory.
Jiyong Park6f0f6882020-11-12 13:14:30 +090092func filesystemFactory() android.Module {
93 module := &filesystem{}
Jiyong Parkfa616132021-04-20 11:36:40 +090094 initFilesystemModule(module)
95 return module
96}
97
98func initFilesystemModule(module *filesystem) {
Jiyong Park71baa762021-01-18 21:11:03 +090099 module.AddProperties(&module.properties)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900100 android.InitPackageModule(module)
101 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900102}
103
Jiyong Park12a719c2021-01-07 15:31:24 +0900104var dependencyTag = struct {
105 blueprint.BaseDependencyTag
Jooyung Han092ef812021-03-10 15:40:34 +0900106 android.PackagingItemAlwaysDepTag
Jiyong Park12a719c2021-01-07 15:31:24 +0900107}{}
Jiyong Park65b62242020-11-25 12:44:59 +0900108
Jiyong Park6f0f6882020-11-12 13:14:30 +0900109func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park65b62242020-11-25 12:44:59 +0900110 f.AddDeps(ctx, dependencyTag)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900111}
112
Jiyong Park11a65972021-02-01 21:09:38 +0900113type fsType int
114
115const (
116 ext4Type fsType = iota
117 compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900118 cpioType // uncompressed
Jiyong Park11a65972021-02-01 21:09:38 +0900119 unknown
120)
121
122func (f *filesystem) fsType(ctx android.ModuleContext) fsType {
123 typeStr := proptools.StringDefault(f.properties.Type, "ext4")
124 switch typeStr {
125 case "ext4":
126 return ext4Type
127 case "compressed_cpio":
128 return compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900129 case "cpio":
130 return cpioType
Jiyong Park11a65972021-02-01 21:09:38 +0900131 default:
132 ctx.PropertyErrorf("type", "%q not supported", typeStr)
133 return unknown
134 }
135}
136
Jiyong Park65c49f52020-11-24 14:23:26 +0900137func (f *filesystem) installFileName() string {
138 return f.BaseModuleName() + ".img"
139}
140
Jiyong Park6f0f6882020-11-12 13:14:30 +0900141var pctx = android.NewPackageContext("android/soong/filesystem")
142
143func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park11a65972021-02-01 21:09:38 +0900144 switch f.fsType(ctx) {
145 case ext4Type:
146 f.output = f.buildImageUsingBuildImage(ctx)
147 case compressedCpioType:
Jiyong Park837cdb22021-02-05 00:17:14 +0900148 f.output = f.buildCpioImage(ctx, true)
149 case cpioType:
150 f.output = f.buildCpioImage(ctx, false)
Jiyong Park11a65972021-02-01 21:09:38 +0900151 default:
152 return
153 }
154
155 f.installDir = android.PathForModuleInstall(ctx, "etc")
156 ctx.InstallFile(f.installDir, f.installFileName(), f.output)
157}
158
Jiyong Parkfa616132021-04-20 11:36:40 +0900159// root zip will contain extra files/dirs that are not from the `deps` property.
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900160func (f *filesystem) buildRootZip(ctx android.ModuleContext) android.OutputPath {
161 rootDir := android.PathForModuleGen(ctx, "root").OutputPath
162 builder := android.NewRuleBuilder(pctx, ctx)
163 builder.Command().Text("rm -rf").Text(rootDir.String())
164 builder.Command().Text("mkdir -p").Text(rootDir.String())
165
Inseob Kim14199b02021-02-09 21:18:31 +0900166 // create dirs and symlinks
167 for _, dir := range f.properties.Dirs {
168 // OutputPath.Join verifies dir
169 builder.Command().Text("mkdir -p").Text(rootDir.Join(ctx, dir).String())
170 }
171
172 for _, symlink := range f.properties.Symlinks {
173 name := strings.TrimSpace(proptools.String(symlink.Name))
174 target := strings.TrimSpace(proptools.String(symlink.Target))
175
176 if name == "" {
177 ctx.PropertyErrorf("symlinks", "Name can't be empty")
178 continue
179 }
180
181 if target == "" {
182 ctx.PropertyErrorf("symlinks", "Target can't be empty")
183 continue
184 }
185
186 // OutputPath.Join verifies name. don't need to verify target.
187 dst := rootDir.Join(ctx, name)
188
189 builder.Command().Text("mkdir -p").Text(filepath.Dir(dst.String()))
190 builder.Command().Text("ln -sf").Text(proptools.ShellEscape(target)).Text(dst.String())
191 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900192
Jiyong Parkfa616132021-04-20 11:36:40 +0900193 // create extra files if there's any
194 rootForExtraFiles := android.PathForModuleGen(ctx, "root-extra").OutputPath
195 var extraFiles android.OutputPaths
196 if f.buildExtraFiles != nil {
197 extraFiles = f.buildExtraFiles(ctx, rootForExtraFiles)
198 for _, f := range extraFiles {
199 rel, _ := filepath.Rel(rootForExtraFiles.String(), f.String())
200 if strings.HasPrefix(rel, "..") {
201 panic(fmt.Errorf("%q is not under %q\n", f, rootForExtraFiles))
202 }
203 }
204 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900205
Jiyong Parkfa616132021-04-20 11:36:40 +0900206 // Zip them all
207 zipOut := android.PathForModuleGen(ctx, "root.zip").OutputPath
208 zipCommand := builder.Command().BuiltTool("soong_zip")
209 zipCommand.FlagWithOutput("-o ", zipOut).
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900210 FlagWithArg("-C ", rootDir.String()).
211 Flag("-L 0"). // no compression because this will be unzipped soon
212 FlagWithArg("-D ", rootDir.String()).
213 Flag("-d") // include empty directories
Jiyong Parkfa616132021-04-20 11:36:40 +0900214 if len(extraFiles) > 0 {
215 zipCommand.FlagWithArg("-C ", rootForExtraFiles.String())
216 for _, f := range extraFiles {
217 zipCommand.FlagWithInput("-f ", f)
218 }
219 }
220
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900221 builder.Command().Text("rm -rf").Text(rootDir.String())
222
223 builder.Build("zip_root", fmt.Sprintf("zipping root contents for %s", ctx.ModuleName()))
224 return zipOut
225}
226
Jiyong Park11a65972021-02-01 21:09:38 +0900227func (f *filesystem) buildImageUsingBuildImage(ctx android.ModuleContext) android.OutputPath {
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900228 depsZipFile := android.PathForModuleOut(ctx, "deps.zip").OutputPath
229 f.CopyDepsToZip(ctx, depsZipFile)
230
231 builder := android.NewRuleBuilder(pctx, ctx)
232 depsBase := proptools.StringDefault(f.properties.Base_dir, ".")
233 rebasedDepsZip := android.PathForModuleOut(ctx, "rebased_deps.zip").OutputPath
234 builder.Command().
235 BuiltTool("zip2zip").
236 FlagWithInput("-i ", depsZipFile).
237 FlagWithOutput("-o ", rebasedDepsZip).
238 Text("**/*:" + proptools.ShellEscape(depsBase)) // zip2zip verifies depsBase
Jiyong Park6f0f6882020-11-12 13:14:30 +0900239
240 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900241 rootZip := f.buildRootZip(ctx)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900242 builder.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800243 BuiltTool("zipsync").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900244 FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear.
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900245 Input(rootZip).
246 Input(rebasedDepsZip)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900247
Jiyong Park72678312021-01-18 17:29:49 +0900248 propFile, toolDeps := f.buildPropFile(ctx)
Jiyong Park11a65972021-02-01 21:09:38 +0900249 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -0800250 builder.Command().BuiltTool("build_image").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900251 Text(rootDir.String()). // input directory
252 Input(propFile).
Jiyong Park72678312021-01-18 17:29:49 +0900253 Implicits(toolDeps).
Jiyong Park11a65972021-02-01 21:09:38 +0900254 Output(output).
Jiyong Park6f0f6882020-11-12 13:14:30 +0900255 Text(rootDir.String()) // directory where to find fs_config_files|dirs
256
257 // rootDir is not deleted. Might be useful for quick inspection.
Colin Crossf1a035e2020-11-16 17:32:30 -0800258 builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park65c49f52020-11-24 14:23:26 +0900259
Jiyong Park11a65972021-02-01 21:09:38 +0900260 return output
Jiyong Park65c49f52020-11-24 14:23:26 +0900261}
262
Inseob Kimcc8e5362021-02-03 14:05:24 +0900263func (f *filesystem) buildFileContexts(ctx android.ModuleContext) android.OutputPath {
264 builder := android.NewRuleBuilder(pctx, ctx)
265 fcBin := android.PathForModuleOut(ctx, "file_contexts.bin")
266 builder.Command().BuiltTool("sefcontext_compile").
267 FlagWithOutput("-o ", fcBin).
268 Input(android.PathForModuleSrc(ctx, proptools.String(f.properties.File_contexts)))
269 builder.Build("build_filesystem_file_contexts", fmt.Sprintf("Creating filesystem file contexts for %s", f.BaseModuleName()))
270 return fcBin.OutputPath
271}
272
Jiyong Park72678312021-01-18 17:29:49 +0900273func (f *filesystem) buildPropFile(ctx android.ModuleContext) (propFile android.OutputPath, toolDeps android.Paths) {
274 type prop struct {
275 name string
276 value string
277 }
278
279 var props []prop
280 var deps android.Paths
281 addStr := func(name string, value string) {
282 props = append(props, prop{name, value})
283 }
284 addPath := func(name string, path android.Path) {
285 props = append(props, prop{name, path.String()})
286 deps = append(deps, path)
287 }
288
Jiyong Park11a65972021-02-01 21:09:38 +0900289 // Type string that build_image.py accepts.
290 fsTypeStr := func(t fsType) string {
291 switch t {
292 // TODO(jiyong): add more types like f2fs, erofs, etc.
293 case ext4Type:
294 return "ext4"
295 }
296 panic(fmt.Errorf("unsupported fs type %v", t))
297 }
298
299 addStr("fs_type", fsTypeStr(f.fsType(ctx)))
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900300 addStr("mount_point", "/")
Jiyong Park72678312021-01-18 17:29:49 +0900301 addStr("use_dynamic_partition_size", "true")
302 addPath("ext_mkuserimg", ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs"))
303 // b/177813163 deps of the host tools have to be added. Remove this.
304 for _, t := range []string{"mke2fs", "e2fsdroid", "tune2fs"} {
305 deps = append(deps, ctx.Config().HostToolPath(ctx, t))
306 }
307
Jiyong Park71baa762021-01-18 21:11:03 +0900308 if proptools.Bool(f.properties.Use_avb) {
309 addStr("avb_hashtree_enable", "true")
310 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
311 algorithm := proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096")
312 addStr("avb_algorithm", algorithm)
313 key := android.PathForModuleSrc(ctx, proptools.String(f.properties.Avb_private_key))
314 addPath("avb_key_path", key)
315 addStr("avb_add_hashtree_footer_args", "--do_not_generate_fec")
Jiyong Parkac4076d2021-03-15 23:21:30 +0900316 partitionName := proptools.StringDefault(f.properties.Partition_name, f.Name())
317 addStr("partition_name", partitionName)
Jiyong Park71baa762021-01-18 21:11:03 +0900318 }
319
Inseob Kimcc8e5362021-02-03 14:05:24 +0900320 if proptools.String(f.properties.File_contexts) != "" {
321 addPath("selinux_fc", f.buildFileContexts(ctx))
322 }
323
Jiyong Park72678312021-01-18 17:29:49 +0900324 propFile = android.PathForModuleOut(ctx, "prop").OutputPath
325 builder := android.NewRuleBuilder(pctx, ctx)
326 builder.Command().Text("rm").Flag("-rf").Output(propFile)
327 for _, p := range props {
328 builder.Command().
Jiyong Park3db465d2021-01-26 14:08:16 +0900329 Text("echo").
Jiyong Park72678312021-01-18 17:29:49 +0900330 Flag(`"` + p.name + "=" + p.value + `"`).
331 Text(">>").Output(propFile)
332 }
333 builder.Build("build_filesystem_prop", fmt.Sprintf("Creating filesystem props for %s", f.BaseModuleName()))
334 return propFile, deps
335}
336
Jiyong Park837cdb22021-02-05 00:17:14 +0900337func (f *filesystem) buildCpioImage(ctx android.ModuleContext, compressed bool) android.OutputPath {
Jiyong Park11a65972021-02-01 21:09:38 +0900338 if proptools.Bool(f.properties.Use_avb) {
339 ctx.PropertyErrorf("use_avb", "signing compresed cpio image using avbtool is not supported."+
340 "Consider adding this to bootimg module and signing the entire boot image.")
341 }
342
Inseob Kimcc8e5362021-02-03 14:05:24 +0900343 if proptools.String(f.properties.File_contexts) != "" {
344 ctx.PropertyErrorf("file_contexts", "file_contexts is not supported for compressed cpio image.")
345 }
346
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900347 depsZipFile := android.PathForModuleOut(ctx, "deps.zip").OutputPath
348 f.CopyDepsToZip(ctx, depsZipFile)
349
350 builder := android.NewRuleBuilder(pctx, ctx)
351 depsBase := proptools.StringDefault(f.properties.Base_dir, ".")
352 rebasedDepsZip := android.PathForModuleOut(ctx, "rebased_deps.zip").OutputPath
353 builder.Command().
354 BuiltTool("zip2zip").
355 FlagWithInput("-i ", depsZipFile).
356 FlagWithOutput("-o ", rebasedDepsZip).
357 Text("**/*:" + proptools.ShellEscape(depsBase)) // zip2zip verifies depsBase
Jiyong Park11a65972021-02-01 21:09:38 +0900358
359 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900360 rootZip := f.buildRootZip(ctx)
Jiyong Park11a65972021-02-01 21:09:38 +0900361 builder.Command().
362 BuiltTool("zipsync").
363 FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear.
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900364 Input(rootZip).
365 Input(rebasedDepsZip)
Jiyong Park11a65972021-02-01 21:09:38 +0900366
367 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Jiyong Park837cdb22021-02-05 00:17:14 +0900368 cmd := builder.Command().
Jiyong Park11a65972021-02-01 21:09:38 +0900369 BuiltTool("mkbootfs").
Jiyong Park837cdb22021-02-05 00:17:14 +0900370 Text(rootDir.String()) // input directory
371 if compressed {
372 cmd.Text("|").
373 BuiltTool("lz4").
374 Flag("--favor-decSpeed"). // for faster boot
375 Flag("-12"). // maximum compression level
376 Flag("-l"). // legacy format for kernel
377 Text(">").Output(output)
378 } else {
379 cmd.Text(">").Output(output)
380 }
Jiyong Park11a65972021-02-01 21:09:38 +0900381
382 // rootDir is not deleted. Might be useful for quick inspection.
Jiyong Park837cdb22021-02-05 00:17:14 +0900383 builder.Build("build_cpio_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park11a65972021-02-01 21:09:38 +0900384
385 return output
386}
387
Jiyong Park65c49f52020-11-24 14:23:26 +0900388var _ android.AndroidMkEntriesProvider = (*filesystem)(nil)
389
390// Implements android.AndroidMkEntriesProvider
391func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries {
392 return []android.AndroidMkEntries{android.AndroidMkEntries{
393 Class: "ETC",
394 OutputFile: android.OptionalPathForPath(f.output),
395 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700396 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Park65c49f52020-11-24 14:23:26 +0900397 entries.SetString("LOCAL_MODULE_PATH", f.installDir.ToMakePath().String())
398 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
399 },
400 },
401 }}
Jiyong Park6f0f6882020-11-12 13:14:30 +0900402}
Jiyong Park12a719c2021-01-07 15:31:24 +0900403
Jiyong Park940dfd42021-02-04 15:37:34 +0900404var _ android.OutputFileProducer = (*filesystem)(nil)
405
406// Implements android.OutputFileProducer
407func (f *filesystem) OutputFiles(tag string) (android.Paths, error) {
408 if tag == "" {
409 return []android.Path{f.output}, nil
410 }
411 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
412}
413
Jiyong Park12a719c2021-01-07 15:31:24 +0900414// Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex
415// package to have access to the output file.
416type Filesystem interface {
417 android.Module
418 OutputPath() android.Path
Jiyong Park972e06c2021-03-15 23:32:49 +0900419
420 // Returns the output file that is signed by avbtool. If this module is not signed, returns
421 // nil.
422 SignedOutputPath() android.Path
Jiyong Park12a719c2021-01-07 15:31:24 +0900423}
424
425var _ Filesystem = (*filesystem)(nil)
426
427func (f *filesystem) OutputPath() android.Path {
428 return f.output
429}
Jiyong Park972e06c2021-03-15 23:32:49 +0900430
431func (f *filesystem) SignedOutputPath() android.Path {
432 if proptools.Bool(f.properties.Use_avb) {
433 return f.OutputPath()
434 }
435 return nil
436}