blob: 3b0a7ae5a5d6371c85c752603d3fcbbb3e0270b7 [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() {
29 android.RegisterModuleType("android_filesystem", filesystemFactory)
30}
31
32type filesystem struct {
33 android.ModuleBase
34 android.PackagingBase
Jiyong Park65c49f52020-11-24 14:23:26 +090035
Jiyong Park71baa762021-01-18 21:11:03 +090036 properties filesystemProperties
37
Jiyong Park65c49f52020-11-24 14:23:26 +090038 output android.OutputPath
39 installDir android.InstallPath
Jiyong Park6f0f6882020-11-12 13:14:30 +090040}
41
Inseob Kim14199b02021-02-09 21:18:31 +090042type symlinkDefinition struct {
43 Target *string
44 Name *string
45}
46
Jiyong Park71baa762021-01-18 21:11:03 +090047type filesystemProperties struct {
48 // When set to true, sign the image with avbtool. Default is false.
49 Use_avb *bool
50
51 // Path to the private key that avbtool will use to sign this filesystem image.
52 // TODO(jiyong): allow apex_key to be specified here
53 Avb_private_key *string `android:"path"`
54
55 // Hash and signing algorithm for avbtool. Default is SHA256_RSA4096.
56 Avb_algorithm *string
Jiyong Park11a65972021-02-01 21:09:38 +090057
Jiyong Park837cdb22021-02-05 00:17:14 +090058 // Type of the filesystem. Currently, ext4, cpio, and compressed_cpio are supported. Default
59 // is ext4.
Jiyong Park11a65972021-02-01 21:09:38 +090060 Type *string
Inseob Kimcc8e5362021-02-03 14:05:24 +090061
62 // file_contexts file to make image. Currently, only ext4 is supported.
63 File_contexts *string `android:"path"`
Inseob Kim2ce1b5d2021-02-15 17:01:04 +090064
65 // Base directory relative to root, to which deps are installed, e.g. "system". Default is "."
66 // (root).
67 Base_dir *string
Inseob Kim14199b02021-02-09 21:18:31 +090068
69 // Directories to be created under root. e.g. /dev, /proc, etc.
70 Dirs []string
71
72 // Symbolic links to be created under root with "ln -sf <target> <name>".
73 Symlinks []symlinkDefinition
Jiyong Park71baa762021-01-18 21:11:03 +090074}
75
Jiyong Park65c49f52020-11-24 14:23:26 +090076// android_filesystem packages a set of modules and their transitive dependencies into a filesystem
77// image. The filesystem images are expected to be mounted in the target device, which means the
78// modules in the filesystem image are built for the target device (i.e. Android, not Linux host).
79// The modules are placed in the filesystem image just like they are installed to the ordinary
80// partitions like system.img. For example, cc_library modules are placed under ./lib[64] directory.
Jiyong Park6f0f6882020-11-12 13:14:30 +090081func filesystemFactory() android.Module {
82 module := &filesystem{}
Jiyong Park71baa762021-01-18 21:11:03 +090083 module.AddProperties(&module.properties)
Jiyong Park6f0f6882020-11-12 13:14:30 +090084 android.InitPackageModule(module)
85 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
86 return module
87}
88
Jiyong Park12a719c2021-01-07 15:31:24 +090089var dependencyTag = struct {
90 blueprint.BaseDependencyTag
91 android.InstallAlwaysNeededDependencyTag
92}{}
Jiyong Park65b62242020-11-25 12:44:59 +090093
Jiyong Park6f0f6882020-11-12 13:14:30 +090094func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park65b62242020-11-25 12:44:59 +090095 f.AddDeps(ctx, dependencyTag)
Jiyong Park6f0f6882020-11-12 13:14:30 +090096}
97
Jiyong Park11a65972021-02-01 21:09:38 +090098type fsType int
99
100const (
101 ext4Type fsType = iota
102 compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900103 cpioType // uncompressed
Jiyong Park11a65972021-02-01 21:09:38 +0900104 unknown
105)
106
107func (f *filesystem) fsType(ctx android.ModuleContext) fsType {
108 typeStr := proptools.StringDefault(f.properties.Type, "ext4")
109 switch typeStr {
110 case "ext4":
111 return ext4Type
112 case "compressed_cpio":
113 return compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900114 case "cpio":
115 return cpioType
Jiyong Park11a65972021-02-01 21:09:38 +0900116 default:
117 ctx.PropertyErrorf("type", "%q not supported", typeStr)
118 return unknown
119 }
120}
121
Jiyong Park65c49f52020-11-24 14:23:26 +0900122func (f *filesystem) installFileName() string {
123 return f.BaseModuleName() + ".img"
124}
125
Jiyong Park6f0f6882020-11-12 13:14:30 +0900126var pctx = android.NewPackageContext("android/soong/filesystem")
127
128func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park11a65972021-02-01 21:09:38 +0900129 switch f.fsType(ctx) {
130 case ext4Type:
131 f.output = f.buildImageUsingBuildImage(ctx)
132 case compressedCpioType:
Jiyong Park837cdb22021-02-05 00:17:14 +0900133 f.output = f.buildCpioImage(ctx, true)
134 case cpioType:
135 f.output = f.buildCpioImage(ctx, false)
Jiyong Park11a65972021-02-01 21:09:38 +0900136 default:
137 return
138 }
139
140 f.installDir = android.PathForModuleInstall(ctx, "etc")
141 ctx.InstallFile(f.installDir, f.installFileName(), f.output)
142}
143
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900144// root zip will contain stuffs like dirs or symlinks.
145func (f *filesystem) buildRootZip(ctx android.ModuleContext) android.OutputPath {
146 rootDir := android.PathForModuleGen(ctx, "root").OutputPath
147 builder := android.NewRuleBuilder(pctx, ctx)
148 builder.Command().Text("rm -rf").Text(rootDir.String())
149 builder.Command().Text("mkdir -p").Text(rootDir.String())
150
Inseob Kim14199b02021-02-09 21:18:31 +0900151 // create dirs and symlinks
152 for _, dir := range f.properties.Dirs {
153 // OutputPath.Join verifies dir
154 builder.Command().Text("mkdir -p").Text(rootDir.Join(ctx, dir).String())
155 }
156
157 for _, symlink := range f.properties.Symlinks {
158 name := strings.TrimSpace(proptools.String(symlink.Name))
159 target := strings.TrimSpace(proptools.String(symlink.Target))
160
161 if name == "" {
162 ctx.PropertyErrorf("symlinks", "Name can't be empty")
163 continue
164 }
165
166 if target == "" {
167 ctx.PropertyErrorf("symlinks", "Target can't be empty")
168 continue
169 }
170
171 // OutputPath.Join verifies name. don't need to verify target.
172 dst := rootDir.Join(ctx, name)
173
174 builder.Command().Text("mkdir -p").Text(filepath.Dir(dst.String()))
175 builder.Command().Text("ln -sf").Text(proptools.ShellEscape(target)).Text(dst.String())
176 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900177
178 zipOut := android.PathForModuleGen(ctx, "root.zip").OutputPath
179
180 builder.Command().
181 BuiltTool("soong_zip").
182 FlagWithOutput("-o ", zipOut).
183 FlagWithArg("-C ", rootDir.String()).
184 Flag("-L 0"). // no compression because this will be unzipped soon
185 FlagWithArg("-D ", rootDir.String()).
186 Flag("-d") // include empty directories
187 builder.Command().Text("rm -rf").Text(rootDir.String())
188
189 builder.Build("zip_root", fmt.Sprintf("zipping root contents for %s", ctx.ModuleName()))
190 return zipOut
191}
192
Jiyong Park11a65972021-02-01 21:09:38 +0900193func (f *filesystem) buildImageUsingBuildImage(ctx android.ModuleContext) android.OutputPath {
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900194 depsZipFile := android.PathForModuleOut(ctx, "deps.zip").OutputPath
195 f.CopyDepsToZip(ctx, depsZipFile)
196
197 builder := android.NewRuleBuilder(pctx, ctx)
198 depsBase := proptools.StringDefault(f.properties.Base_dir, ".")
199 rebasedDepsZip := android.PathForModuleOut(ctx, "rebased_deps.zip").OutputPath
200 builder.Command().
201 BuiltTool("zip2zip").
202 FlagWithInput("-i ", depsZipFile).
203 FlagWithOutput("-o ", rebasedDepsZip).
204 Text("**/*:" + proptools.ShellEscape(depsBase)) // zip2zip verifies depsBase
Jiyong Park6f0f6882020-11-12 13:14:30 +0900205
206 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900207 rootZip := f.buildRootZip(ctx)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900208 builder.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800209 BuiltTool("zipsync").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900210 FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear.
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900211 Input(rootZip).
212 Input(rebasedDepsZip)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900213
Jiyong Park72678312021-01-18 17:29:49 +0900214 propFile, toolDeps := f.buildPropFile(ctx)
Jiyong Park11a65972021-02-01 21:09:38 +0900215 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -0800216 builder.Command().BuiltTool("build_image").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900217 Text(rootDir.String()). // input directory
218 Input(propFile).
Jiyong Park72678312021-01-18 17:29:49 +0900219 Implicits(toolDeps).
Jiyong Park11a65972021-02-01 21:09:38 +0900220 Output(output).
Jiyong Park6f0f6882020-11-12 13:14:30 +0900221 Text(rootDir.String()) // directory where to find fs_config_files|dirs
222
223 // rootDir is not deleted. Might be useful for quick inspection.
Colin Crossf1a035e2020-11-16 17:32:30 -0800224 builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park65c49f52020-11-24 14:23:26 +0900225
Jiyong Park11a65972021-02-01 21:09:38 +0900226 return output
Jiyong Park65c49f52020-11-24 14:23:26 +0900227}
228
Inseob Kimcc8e5362021-02-03 14:05:24 +0900229func (f *filesystem) buildFileContexts(ctx android.ModuleContext) android.OutputPath {
230 builder := android.NewRuleBuilder(pctx, ctx)
231 fcBin := android.PathForModuleOut(ctx, "file_contexts.bin")
232 builder.Command().BuiltTool("sefcontext_compile").
233 FlagWithOutput("-o ", fcBin).
234 Input(android.PathForModuleSrc(ctx, proptools.String(f.properties.File_contexts)))
235 builder.Build("build_filesystem_file_contexts", fmt.Sprintf("Creating filesystem file contexts for %s", f.BaseModuleName()))
236 return fcBin.OutputPath
237}
238
Jiyong Park72678312021-01-18 17:29:49 +0900239func (f *filesystem) buildPropFile(ctx android.ModuleContext) (propFile android.OutputPath, toolDeps android.Paths) {
240 type prop struct {
241 name string
242 value string
243 }
244
245 var props []prop
246 var deps android.Paths
247 addStr := func(name string, value string) {
248 props = append(props, prop{name, value})
249 }
250 addPath := func(name string, path android.Path) {
251 props = append(props, prop{name, path.String()})
252 deps = append(deps, path)
253 }
254
Jiyong Park11a65972021-02-01 21:09:38 +0900255 // Type string that build_image.py accepts.
256 fsTypeStr := func(t fsType) string {
257 switch t {
258 // TODO(jiyong): add more types like f2fs, erofs, etc.
259 case ext4Type:
260 return "ext4"
261 }
262 panic(fmt.Errorf("unsupported fs type %v", t))
263 }
264
265 addStr("fs_type", fsTypeStr(f.fsType(ctx)))
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900266 addStr("mount_point", "/")
Jiyong Park72678312021-01-18 17:29:49 +0900267 addStr("use_dynamic_partition_size", "true")
268 addPath("ext_mkuserimg", ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs"))
269 // b/177813163 deps of the host tools have to be added. Remove this.
270 for _, t := range []string{"mke2fs", "e2fsdroid", "tune2fs"} {
271 deps = append(deps, ctx.Config().HostToolPath(ctx, t))
272 }
273
Jiyong Park71baa762021-01-18 21:11:03 +0900274 if proptools.Bool(f.properties.Use_avb) {
275 addStr("avb_hashtree_enable", "true")
276 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
277 algorithm := proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096")
278 addStr("avb_algorithm", algorithm)
279 key := android.PathForModuleSrc(ctx, proptools.String(f.properties.Avb_private_key))
280 addPath("avb_key_path", key)
281 addStr("avb_add_hashtree_footer_args", "--do_not_generate_fec")
282 addStr("partition_name", f.Name())
283 }
284
Inseob Kimcc8e5362021-02-03 14:05:24 +0900285 if proptools.String(f.properties.File_contexts) != "" {
286 addPath("selinux_fc", f.buildFileContexts(ctx))
287 }
288
Jiyong Park72678312021-01-18 17:29:49 +0900289 propFile = android.PathForModuleOut(ctx, "prop").OutputPath
290 builder := android.NewRuleBuilder(pctx, ctx)
291 builder.Command().Text("rm").Flag("-rf").Output(propFile)
292 for _, p := range props {
293 builder.Command().
Jiyong Park3db465d2021-01-26 14:08:16 +0900294 Text("echo").
Jiyong Park72678312021-01-18 17:29:49 +0900295 Flag(`"` + p.name + "=" + p.value + `"`).
296 Text(">>").Output(propFile)
297 }
298 builder.Build("build_filesystem_prop", fmt.Sprintf("Creating filesystem props for %s", f.BaseModuleName()))
299 return propFile, deps
300}
301
Jiyong Park837cdb22021-02-05 00:17:14 +0900302func (f *filesystem) buildCpioImage(ctx android.ModuleContext, compressed bool) android.OutputPath {
Jiyong Park11a65972021-02-01 21:09:38 +0900303 if proptools.Bool(f.properties.Use_avb) {
304 ctx.PropertyErrorf("use_avb", "signing compresed cpio image using avbtool is not supported."+
305 "Consider adding this to bootimg module and signing the entire boot image.")
306 }
307
Inseob Kimcc8e5362021-02-03 14:05:24 +0900308 if proptools.String(f.properties.File_contexts) != "" {
309 ctx.PropertyErrorf("file_contexts", "file_contexts is not supported for compressed cpio image.")
310 }
311
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900312 depsZipFile := android.PathForModuleOut(ctx, "deps.zip").OutputPath
313 f.CopyDepsToZip(ctx, depsZipFile)
314
315 builder := android.NewRuleBuilder(pctx, ctx)
316 depsBase := proptools.StringDefault(f.properties.Base_dir, ".")
317 rebasedDepsZip := android.PathForModuleOut(ctx, "rebased_deps.zip").OutputPath
318 builder.Command().
319 BuiltTool("zip2zip").
320 FlagWithInput("-i ", depsZipFile).
321 FlagWithOutput("-o ", rebasedDepsZip).
322 Text("**/*:" + proptools.ShellEscape(depsBase)) // zip2zip verifies depsBase
Jiyong Park11a65972021-02-01 21:09:38 +0900323
324 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900325 rootZip := f.buildRootZip(ctx)
Jiyong Park11a65972021-02-01 21:09:38 +0900326 builder.Command().
327 BuiltTool("zipsync").
328 FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear.
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900329 Input(rootZip).
330 Input(rebasedDepsZip)
Jiyong Park11a65972021-02-01 21:09:38 +0900331
332 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Jiyong Park837cdb22021-02-05 00:17:14 +0900333 cmd := builder.Command().
Jiyong Park11a65972021-02-01 21:09:38 +0900334 BuiltTool("mkbootfs").
Jiyong Park837cdb22021-02-05 00:17:14 +0900335 Text(rootDir.String()) // input directory
336 if compressed {
337 cmd.Text("|").
338 BuiltTool("lz4").
339 Flag("--favor-decSpeed"). // for faster boot
340 Flag("-12"). // maximum compression level
341 Flag("-l"). // legacy format for kernel
342 Text(">").Output(output)
343 } else {
344 cmd.Text(">").Output(output)
345 }
Jiyong Park11a65972021-02-01 21:09:38 +0900346
347 // rootDir is not deleted. Might be useful for quick inspection.
Jiyong Park837cdb22021-02-05 00:17:14 +0900348 builder.Build("build_cpio_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park11a65972021-02-01 21:09:38 +0900349
350 return output
351}
352
Jiyong Park65c49f52020-11-24 14:23:26 +0900353var _ android.AndroidMkEntriesProvider = (*filesystem)(nil)
354
355// Implements android.AndroidMkEntriesProvider
356func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries {
357 return []android.AndroidMkEntries{android.AndroidMkEntries{
358 Class: "ETC",
359 OutputFile: android.OptionalPathForPath(f.output),
360 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700361 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Park65c49f52020-11-24 14:23:26 +0900362 entries.SetString("LOCAL_MODULE_PATH", f.installDir.ToMakePath().String())
363 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
364 },
365 },
366 }}
Jiyong Park6f0f6882020-11-12 13:14:30 +0900367}
Jiyong Park12a719c2021-01-07 15:31:24 +0900368
Jiyong Park940dfd42021-02-04 15:37:34 +0900369var _ android.OutputFileProducer = (*filesystem)(nil)
370
371// Implements android.OutputFileProducer
372func (f *filesystem) OutputFiles(tag string) (android.Paths, error) {
373 if tag == "" {
374 return []android.Path{f.output}, nil
375 }
376 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
377}
378
Jiyong Park12a719c2021-01-07 15:31:24 +0900379// Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex
380// package to have access to the output file.
381type Filesystem interface {
382 android.Module
383 OutputPath() android.Path
384}
385
386var _ Filesystem = (*filesystem)(nil)
387
388func (f *filesystem) OutputPath() android.Path {
389 return f.output
390}