blob: 8974eba4f66b171683852b1e6c19660a1da2edf9 [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 Parkac4076d2021-03-15 23:21:30 +090058 // Name of the partition stored in vbmeta desc. Defaults to the name of this module.
59 Partition_name *string
60
Jiyong Park837cdb22021-02-05 00:17:14 +090061 // Type of the filesystem. Currently, ext4, cpio, and compressed_cpio are supported. Default
62 // is ext4.
Jiyong Park11a65972021-02-01 21:09:38 +090063 Type *string
Inseob Kimcc8e5362021-02-03 14:05:24 +090064
65 // file_contexts file to make image. Currently, only ext4 is supported.
66 File_contexts *string `android:"path"`
Inseob Kim2ce1b5d2021-02-15 17:01:04 +090067
68 // Base directory relative to root, to which deps are installed, e.g. "system". Default is "."
69 // (root).
70 Base_dir *string
Inseob Kim14199b02021-02-09 21:18:31 +090071
72 // Directories to be created under root. e.g. /dev, /proc, etc.
73 Dirs []string
74
75 // Symbolic links to be created under root with "ln -sf <target> <name>".
76 Symlinks []symlinkDefinition
Jiyong Park71baa762021-01-18 21:11:03 +090077}
78
Jiyong Park65c49f52020-11-24 14:23:26 +090079// android_filesystem packages a set of modules and their transitive dependencies into a filesystem
80// image. The filesystem images are expected to be mounted in the target device, which means the
81// modules in the filesystem image are built for the target device (i.e. Android, not Linux host).
82// The modules are placed in the filesystem image just like they are installed to the ordinary
83// partitions like system.img. For example, cc_library modules are placed under ./lib[64] directory.
Jiyong Park6f0f6882020-11-12 13:14:30 +090084func filesystemFactory() android.Module {
85 module := &filesystem{}
Jiyong Park71baa762021-01-18 21:11:03 +090086 module.AddProperties(&module.properties)
Jiyong Park6f0f6882020-11-12 13:14:30 +090087 android.InitPackageModule(module)
88 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
89 return module
90}
91
Jiyong Park12a719c2021-01-07 15:31:24 +090092var dependencyTag = struct {
93 blueprint.BaseDependencyTag
94 android.InstallAlwaysNeededDependencyTag
95}{}
Jiyong Park65b62242020-11-25 12:44:59 +090096
Jiyong Park6f0f6882020-11-12 13:14:30 +090097func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park65b62242020-11-25 12:44:59 +090098 f.AddDeps(ctx, dependencyTag)
Jiyong Park6f0f6882020-11-12 13:14:30 +090099}
100
Jiyong Park11a65972021-02-01 21:09:38 +0900101type fsType int
102
103const (
104 ext4Type fsType = iota
105 compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900106 cpioType // uncompressed
Jiyong Park11a65972021-02-01 21:09:38 +0900107 unknown
108)
109
110func (f *filesystem) fsType(ctx android.ModuleContext) fsType {
111 typeStr := proptools.StringDefault(f.properties.Type, "ext4")
112 switch typeStr {
113 case "ext4":
114 return ext4Type
115 case "compressed_cpio":
116 return compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900117 case "cpio":
118 return cpioType
Jiyong Park11a65972021-02-01 21:09:38 +0900119 default:
120 ctx.PropertyErrorf("type", "%q not supported", typeStr)
121 return unknown
122 }
123}
124
Jiyong Park65c49f52020-11-24 14:23:26 +0900125func (f *filesystem) installFileName() string {
126 return f.BaseModuleName() + ".img"
127}
128
Jiyong Park6f0f6882020-11-12 13:14:30 +0900129var pctx = android.NewPackageContext("android/soong/filesystem")
130
131func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park11a65972021-02-01 21:09:38 +0900132 switch f.fsType(ctx) {
133 case ext4Type:
134 f.output = f.buildImageUsingBuildImage(ctx)
135 case compressedCpioType:
Jiyong Park837cdb22021-02-05 00:17:14 +0900136 f.output = f.buildCpioImage(ctx, true)
137 case cpioType:
138 f.output = f.buildCpioImage(ctx, false)
Jiyong Park11a65972021-02-01 21:09:38 +0900139 default:
140 return
141 }
142
143 f.installDir = android.PathForModuleInstall(ctx, "etc")
144 ctx.InstallFile(f.installDir, f.installFileName(), f.output)
145}
146
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900147// root zip will contain stuffs like dirs or symlinks.
148func (f *filesystem) buildRootZip(ctx android.ModuleContext) android.OutputPath {
149 rootDir := android.PathForModuleGen(ctx, "root").OutputPath
150 builder := android.NewRuleBuilder(pctx, ctx)
151 builder.Command().Text("rm -rf").Text(rootDir.String())
152 builder.Command().Text("mkdir -p").Text(rootDir.String())
153
Inseob Kim14199b02021-02-09 21:18:31 +0900154 // create dirs and symlinks
155 for _, dir := range f.properties.Dirs {
156 // OutputPath.Join verifies dir
157 builder.Command().Text("mkdir -p").Text(rootDir.Join(ctx, dir).String())
158 }
159
160 for _, symlink := range f.properties.Symlinks {
161 name := strings.TrimSpace(proptools.String(symlink.Name))
162 target := strings.TrimSpace(proptools.String(symlink.Target))
163
164 if name == "" {
165 ctx.PropertyErrorf("symlinks", "Name can't be empty")
166 continue
167 }
168
169 if target == "" {
170 ctx.PropertyErrorf("symlinks", "Target can't be empty")
171 continue
172 }
173
174 // OutputPath.Join verifies name. don't need to verify target.
175 dst := rootDir.Join(ctx, name)
176
177 builder.Command().Text("mkdir -p").Text(filepath.Dir(dst.String()))
178 builder.Command().Text("ln -sf").Text(proptools.ShellEscape(target)).Text(dst.String())
179 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900180
181 zipOut := android.PathForModuleGen(ctx, "root.zip").OutputPath
182
183 builder.Command().
184 BuiltTool("soong_zip").
185 FlagWithOutput("-o ", zipOut).
186 FlagWithArg("-C ", rootDir.String()).
187 Flag("-L 0"). // no compression because this will be unzipped soon
188 FlagWithArg("-D ", rootDir.String()).
189 Flag("-d") // include empty directories
190 builder.Command().Text("rm -rf").Text(rootDir.String())
191
192 builder.Build("zip_root", fmt.Sprintf("zipping root contents for %s", ctx.ModuleName()))
193 return zipOut
194}
195
Jiyong Park11a65972021-02-01 21:09:38 +0900196func (f *filesystem) buildImageUsingBuildImage(ctx android.ModuleContext) android.OutputPath {
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900197 depsZipFile := android.PathForModuleOut(ctx, "deps.zip").OutputPath
198 f.CopyDepsToZip(ctx, depsZipFile)
199
200 builder := android.NewRuleBuilder(pctx, ctx)
201 depsBase := proptools.StringDefault(f.properties.Base_dir, ".")
202 rebasedDepsZip := android.PathForModuleOut(ctx, "rebased_deps.zip").OutputPath
203 builder.Command().
204 BuiltTool("zip2zip").
205 FlagWithInput("-i ", depsZipFile).
206 FlagWithOutput("-o ", rebasedDepsZip).
207 Text("**/*:" + proptools.ShellEscape(depsBase)) // zip2zip verifies depsBase
Jiyong Park6f0f6882020-11-12 13:14:30 +0900208
209 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900210 rootZip := f.buildRootZip(ctx)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900211 builder.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800212 BuiltTool("zipsync").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900213 FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear.
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900214 Input(rootZip).
215 Input(rebasedDepsZip)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900216
Jiyong Park72678312021-01-18 17:29:49 +0900217 propFile, toolDeps := f.buildPropFile(ctx)
Jiyong Park11a65972021-02-01 21:09:38 +0900218 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -0800219 builder.Command().BuiltTool("build_image").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900220 Text(rootDir.String()). // input directory
221 Input(propFile).
Jiyong Park72678312021-01-18 17:29:49 +0900222 Implicits(toolDeps).
Jiyong Park11a65972021-02-01 21:09:38 +0900223 Output(output).
Jiyong Park6f0f6882020-11-12 13:14:30 +0900224 Text(rootDir.String()) // directory where to find fs_config_files|dirs
225
226 // rootDir is not deleted. Might be useful for quick inspection.
Colin Crossf1a035e2020-11-16 17:32:30 -0800227 builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park65c49f52020-11-24 14:23:26 +0900228
Jiyong Park11a65972021-02-01 21:09:38 +0900229 return output
Jiyong Park65c49f52020-11-24 14:23:26 +0900230}
231
Inseob Kimcc8e5362021-02-03 14:05:24 +0900232func (f *filesystem) buildFileContexts(ctx android.ModuleContext) android.OutputPath {
233 builder := android.NewRuleBuilder(pctx, ctx)
234 fcBin := android.PathForModuleOut(ctx, "file_contexts.bin")
235 builder.Command().BuiltTool("sefcontext_compile").
236 FlagWithOutput("-o ", fcBin).
237 Input(android.PathForModuleSrc(ctx, proptools.String(f.properties.File_contexts)))
238 builder.Build("build_filesystem_file_contexts", fmt.Sprintf("Creating filesystem file contexts for %s", f.BaseModuleName()))
239 return fcBin.OutputPath
240}
241
Jiyong Park72678312021-01-18 17:29:49 +0900242func (f *filesystem) buildPropFile(ctx android.ModuleContext) (propFile android.OutputPath, toolDeps android.Paths) {
243 type prop struct {
244 name string
245 value string
246 }
247
248 var props []prop
249 var deps android.Paths
250 addStr := func(name string, value string) {
251 props = append(props, prop{name, value})
252 }
253 addPath := func(name string, path android.Path) {
254 props = append(props, prop{name, path.String()})
255 deps = append(deps, path)
256 }
257
Jiyong Park11a65972021-02-01 21:09:38 +0900258 // Type string that build_image.py accepts.
259 fsTypeStr := func(t fsType) string {
260 switch t {
261 // TODO(jiyong): add more types like f2fs, erofs, etc.
262 case ext4Type:
263 return "ext4"
264 }
265 panic(fmt.Errorf("unsupported fs type %v", t))
266 }
267
268 addStr("fs_type", fsTypeStr(f.fsType(ctx)))
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900269 addStr("mount_point", "/")
Jiyong Park72678312021-01-18 17:29:49 +0900270 addStr("use_dynamic_partition_size", "true")
271 addPath("ext_mkuserimg", ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs"))
272 // b/177813163 deps of the host tools have to be added. Remove this.
273 for _, t := range []string{"mke2fs", "e2fsdroid", "tune2fs"} {
274 deps = append(deps, ctx.Config().HostToolPath(ctx, t))
275 }
276
Jiyong Park71baa762021-01-18 21:11:03 +0900277 if proptools.Bool(f.properties.Use_avb) {
278 addStr("avb_hashtree_enable", "true")
279 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
280 algorithm := proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096")
281 addStr("avb_algorithm", algorithm)
282 key := android.PathForModuleSrc(ctx, proptools.String(f.properties.Avb_private_key))
283 addPath("avb_key_path", key)
284 addStr("avb_add_hashtree_footer_args", "--do_not_generate_fec")
Jiyong Parkac4076d2021-03-15 23:21:30 +0900285 partitionName := proptools.StringDefault(f.properties.Partition_name, f.Name())
286 addStr("partition_name", partitionName)
Jiyong Park71baa762021-01-18 21:11:03 +0900287 }
288
Inseob Kimcc8e5362021-02-03 14:05:24 +0900289 if proptools.String(f.properties.File_contexts) != "" {
290 addPath("selinux_fc", f.buildFileContexts(ctx))
291 }
292
Jiyong Park72678312021-01-18 17:29:49 +0900293 propFile = android.PathForModuleOut(ctx, "prop").OutputPath
294 builder := android.NewRuleBuilder(pctx, ctx)
295 builder.Command().Text("rm").Flag("-rf").Output(propFile)
296 for _, p := range props {
297 builder.Command().
Jiyong Park3db465d2021-01-26 14:08:16 +0900298 Text("echo").
Jiyong Park72678312021-01-18 17:29:49 +0900299 Flag(`"` + p.name + "=" + p.value + `"`).
300 Text(">>").Output(propFile)
301 }
302 builder.Build("build_filesystem_prop", fmt.Sprintf("Creating filesystem props for %s", f.BaseModuleName()))
303 return propFile, deps
304}
305
Jiyong Park837cdb22021-02-05 00:17:14 +0900306func (f *filesystem) buildCpioImage(ctx android.ModuleContext, compressed bool) android.OutputPath {
Jiyong Park11a65972021-02-01 21:09:38 +0900307 if proptools.Bool(f.properties.Use_avb) {
308 ctx.PropertyErrorf("use_avb", "signing compresed cpio image using avbtool is not supported."+
309 "Consider adding this to bootimg module and signing the entire boot image.")
310 }
311
Inseob Kimcc8e5362021-02-03 14:05:24 +0900312 if proptools.String(f.properties.File_contexts) != "" {
313 ctx.PropertyErrorf("file_contexts", "file_contexts is not supported for compressed cpio image.")
314 }
315
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900316 depsZipFile := android.PathForModuleOut(ctx, "deps.zip").OutputPath
317 f.CopyDepsToZip(ctx, depsZipFile)
318
319 builder := android.NewRuleBuilder(pctx, ctx)
320 depsBase := proptools.StringDefault(f.properties.Base_dir, ".")
321 rebasedDepsZip := android.PathForModuleOut(ctx, "rebased_deps.zip").OutputPath
322 builder.Command().
323 BuiltTool("zip2zip").
324 FlagWithInput("-i ", depsZipFile).
325 FlagWithOutput("-o ", rebasedDepsZip).
326 Text("**/*:" + proptools.ShellEscape(depsBase)) // zip2zip verifies depsBase
Jiyong Park11a65972021-02-01 21:09:38 +0900327
328 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900329 rootZip := f.buildRootZip(ctx)
Jiyong Park11a65972021-02-01 21:09:38 +0900330 builder.Command().
331 BuiltTool("zipsync").
332 FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear.
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900333 Input(rootZip).
334 Input(rebasedDepsZip)
Jiyong Park11a65972021-02-01 21:09:38 +0900335
336 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Jiyong Park837cdb22021-02-05 00:17:14 +0900337 cmd := builder.Command().
Jiyong Park11a65972021-02-01 21:09:38 +0900338 BuiltTool("mkbootfs").
Jiyong Park837cdb22021-02-05 00:17:14 +0900339 Text(rootDir.String()) // input directory
340 if compressed {
341 cmd.Text("|").
342 BuiltTool("lz4").
343 Flag("--favor-decSpeed"). // for faster boot
344 Flag("-12"). // maximum compression level
345 Flag("-l"). // legacy format for kernel
346 Text(">").Output(output)
347 } else {
348 cmd.Text(">").Output(output)
349 }
Jiyong Park11a65972021-02-01 21:09:38 +0900350
351 // rootDir is not deleted. Might be useful for quick inspection.
Jiyong Park837cdb22021-02-05 00:17:14 +0900352 builder.Build("build_cpio_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park11a65972021-02-01 21:09:38 +0900353
354 return output
355}
356
Jiyong Park65c49f52020-11-24 14:23:26 +0900357var _ android.AndroidMkEntriesProvider = (*filesystem)(nil)
358
359// Implements android.AndroidMkEntriesProvider
360func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries {
361 return []android.AndroidMkEntries{android.AndroidMkEntries{
362 Class: "ETC",
363 OutputFile: android.OptionalPathForPath(f.output),
364 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700365 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Park65c49f52020-11-24 14:23:26 +0900366 entries.SetString("LOCAL_MODULE_PATH", f.installDir.ToMakePath().String())
367 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
368 },
369 },
370 }}
Jiyong Park6f0f6882020-11-12 13:14:30 +0900371}
Jiyong Park12a719c2021-01-07 15:31:24 +0900372
Jiyong Park940dfd42021-02-04 15:37:34 +0900373var _ android.OutputFileProducer = (*filesystem)(nil)
374
375// Implements android.OutputFileProducer
376func (f *filesystem) OutputFiles(tag string) (android.Paths, error) {
377 if tag == "" {
378 return []android.Path{f.output}, nil
379 }
380 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
381}
382
Jiyong Park12a719c2021-01-07 15:31:24 +0900383// Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex
384// package to have access to the output file.
385type Filesystem interface {
386 android.Module
387 OutputPath() android.Path
Jiyong Park972e06c2021-03-15 23:32:49 +0900388
389 // Returns the output file that is signed by avbtool. If this module is not signed, returns
390 // nil.
391 SignedOutputPath() android.Path
Jiyong Park12a719c2021-01-07 15:31:24 +0900392}
393
394var _ Filesystem = (*filesystem)(nil)
395
396func (f *filesystem) OutputPath() android.Path {
397 return f.output
398}
Jiyong Park972e06c2021-03-15 23:32:49 +0900399
400func (f *filesystem) SignedOutputPath() android.Path {
401 if proptools.Bool(f.properties.Use_avb) {
402 return f.OutputPath()
403 }
404 return nil
405}