blob: cf9871701c8329a78066d50152b994cc6d1556d1 [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 Park6f0f6882020-11-12 13:14:30 +090034}
35
36type filesystem struct {
37 android.ModuleBase
38 android.PackagingBase
Jiyong Park65c49f52020-11-24 14:23:26 +090039
Jiyong Park71baa762021-01-18 21:11:03 +090040 properties filesystemProperties
41
Jiyong Park65c49f52020-11-24 14:23:26 +090042 output android.OutputPath
43 installDir android.InstallPath
Jiyong Park6f0f6882020-11-12 13:14:30 +090044}
45
Inseob Kim14199b02021-02-09 21:18:31 +090046type symlinkDefinition struct {
47 Target *string
48 Name *string
49}
50
Jiyong Park71baa762021-01-18 21:11:03 +090051type filesystemProperties struct {
52 // When set to true, sign the image with avbtool. Default is false.
53 Use_avb *bool
54
55 // Path to the private key that avbtool will use to sign this filesystem image.
56 // TODO(jiyong): allow apex_key to be specified here
57 Avb_private_key *string `android:"path"`
58
59 // Hash and signing algorithm for avbtool. Default is SHA256_RSA4096.
60 Avb_algorithm *string
Jiyong Park11a65972021-02-01 21:09:38 +090061
Jiyong Parkac4076d2021-03-15 23:21:30 +090062 // Name of the partition stored in vbmeta desc. Defaults to the name of this module.
63 Partition_name *string
64
Jiyong Park837cdb22021-02-05 00:17:14 +090065 // Type of the filesystem. Currently, ext4, cpio, and compressed_cpio are supported. Default
66 // is ext4.
Jiyong Park11a65972021-02-01 21:09:38 +090067 Type *string
Inseob Kimcc8e5362021-02-03 14:05:24 +090068
69 // file_contexts file to make image. Currently, only ext4 is supported.
70 File_contexts *string `android:"path"`
Inseob Kim2ce1b5d2021-02-15 17:01:04 +090071
72 // Base directory relative to root, to which deps are installed, e.g. "system". Default is "."
73 // (root).
74 Base_dir *string
Inseob Kim14199b02021-02-09 21:18:31 +090075
76 // Directories to be created under root. e.g. /dev, /proc, etc.
77 Dirs []string
78
79 // Symbolic links to be created under root with "ln -sf <target> <name>".
80 Symlinks []symlinkDefinition
Jiyong Park71baa762021-01-18 21:11:03 +090081}
82
Jiyong Park65c49f52020-11-24 14:23:26 +090083// android_filesystem packages a set of modules and their transitive dependencies into a filesystem
84// image. The filesystem images are expected to be mounted in the target device, which means the
85// modules in the filesystem image are built for the target device (i.e. Android, not Linux host).
86// The modules are placed in the filesystem image just like they are installed to the ordinary
87// partitions like system.img. For example, cc_library modules are placed under ./lib[64] directory.
Jiyong Park6f0f6882020-11-12 13:14:30 +090088func filesystemFactory() android.Module {
89 module := &filesystem{}
Jiyong Park71baa762021-01-18 21:11:03 +090090 module.AddProperties(&module.properties)
Jiyong Park6f0f6882020-11-12 13:14:30 +090091 android.InitPackageModule(module)
92 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
93 return module
94}
95
Jiyong Park12a719c2021-01-07 15:31:24 +090096var dependencyTag = struct {
97 blueprint.BaseDependencyTag
Jooyung Han092ef812021-03-10 15:40:34 +090098 android.PackagingItemAlwaysDepTag
Jiyong Park12a719c2021-01-07 15:31:24 +090099}{}
Jiyong Park65b62242020-11-25 12:44:59 +0900100
Jiyong Park6f0f6882020-11-12 13:14:30 +0900101func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park65b62242020-11-25 12:44:59 +0900102 f.AddDeps(ctx, dependencyTag)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900103}
104
Jiyong Park11a65972021-02-01 21:09:38 +0900105type fsType int
106
107const (
108 ext4Type fsType = iota
109 compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900110 cpioType // uncompressed
Jiyong Park11a65972021-02-01 21:09:38 +0900111 unknown
112)
113
114func (f *filesystem) fsType(ctx android.ModuleContext) fsType {
115 typeStr := proptools.StringDefault(f.properties.Type, "ext4")
116 switch typeStr {
117 case "ext4":
118 return ext4Type
119 case "compressed_cpio":
120 return compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900121 case "cpio":
122 return cpioType
Jiyong Park11a65972021-02-01 21:09:38 +0900123 default:
124 ctx.PropertyErrorf("type", "%q not supported", typeStr)
125 return unknown
126 }
127}
128
Jiyong Park65c49f52020-11-24 14:23:26 +0900129func (f *filesystem) installFileName() string {
130 return f.BaseModuleName() + ".img"
131}
132
Jiyong Park6f0f6882020-11-12 13:14:30 +0900133var pctx = android.NewPackageContext("android/soong/filesystem")
134
135func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park11a65972021-02-01 21:09:38 +0900136 switch f.fsType(ctx) {
137 case ext4Type:
138 f.output = f.buildImageUsingBuildImage(ctx)
139 case compressedCpioType:
Jiyong Park837cdb22021-02-05 00:17:14 +0900140 f.output = f.buildCpioImage(ctx, true)
141 case cpioType:
142 f.output = f.buildCpioImage(ctx, false)
Jiyong Park11a65972021-02-01 21:09:38 +0900143 default:
144 return
145 }
146
147 f.installDir = android.PathForModuleInstall(ctx, "etc")
148 ctx.InstallFile(f.installDir, f.installFileName(), f.output)
149}
150
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900151// root zip will contain stuffs like dirs or symlinks.
152func (f *filesystem) buildRootZip(ctx android.ModuleContext) android.OutputPath {
153 rootDir := android.PathForModuleGen(ctx, "root").OutputPath
154 builder := android.NewRuleBuilder(pctx, ctx)
155 builder.Command().Text("rm -rf").Text(rootDir.String())
156 builder.Command().Text("mkdir -p").Text(rootDir.String())
157
Inseob Kim14199b02021-02-09 21:18:31 +0900158 // create dirs and symlinks
159 for _, dir := range f.properties.Dirs {
160 // OutputPath.Join verifies dir
161 builder.Command().Text("mkdir -p").Text(rootDir.Join(ctx, dir).String())
162 }
163
164 for _, symlink := range f.properties.Symlinks {
165 name := strings.TrimSpace(proptools.String(symlink.Name))
166 target := strings.TrimSpace(proptools.String(symlink.Target))
167
168 if name == "" {
169 ctx.PropertyErrorf("symlinks", "Name can't be empty")
170 continue
171 }
172
173 if target == "" {
174 ctx.PropertyErrorf("symlinks", "Target can't be empty")
175 continue
176 }
177
178 // OutputPath.Join verifies name. don't need to verify target.
179 dst := rootDir.Join(ctx, name)
180
181 builder.Command().Text("mkdir -p").Text(filepath.Dir(dst.String()))
182 builder.Command().Text("ln -sf").Text(proptools.ShellEscape(target)).Text(dst.String())
183 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900184
185 zipOut := android.PathForModuleGen(ctx, "root.zip").OutputPath
186
187 builder.Command().
188 BuiltTool("soong_zip").
189 FlagWithOutput("-o ", zipOut).
190 FlagWithArg("-C ", rootDir.String()).
191 Flag("-L 0"). // no compression because this will be unzipped soon
192 FlagWithArg("-D ", rootDir.String()).
193 Flag("-d") // include empty directories
194 builder.Command().Text("rm -rf").Text(rootDir.String())
195
196 builder.Build("zip_root", fmt.Sprintf("zipping root contents for %s", ctx.ModuleName()))
197 return zipOut
198}
199
Jiyong Park11a65972021-02-01 21:09:38 +0900200func (f *filesystem) buildImageUsingBuildImage(ctx android.ModuleContext) android.OutputPath {
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900201 depsZipFile := android.PathForModuleOut(ctx, "deps.zip").OutputPath
202 f.CopyDepsToZip(ctx, depsZipFile)
203
204 builder := android.NewRuleBuilder(pctx, ctx)
205 depsBase := proptools.StringDefault(f.properties.Base_dir, ".")
206 rebasedDepsZip := android.PathForModuleOut(ctx, "rebased_deps.zip").OutputPath
207 builder.Command().
208 BuiltTool("zip2zip").
209 FlagWithInput("-i ", depsZipFile).
210 FlagWithOutput("-o ", rebasedDepsZip).
211 Text("**/*:" + proptools.ShellEscape(depsBase)) // zip2zip verifies depsBase
Jiyong Park6f0f6882020-11-12 13:14:30 +0900212
213 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900214 rootZip := f.buildRootZip(ctx)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900215 builder.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800216 BuiltTool("zipsync").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900217 FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear.
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900218 Input(rootZip).
219 Input(rebasedDepsZip)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900220
Jiyong Park72678312021-01-18 17:29:49 +0900221 propFile, toolDeps := f.buildPropFile(ctx)
Jiyong Park11a65972021-02-01 21:09:38 +0900222 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -0800223 builder.Command().BuiltTool("build_image").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900224 Text(rootDir.String()). // input directory
225 Input(propFile).
Jiyong Park72678312021-01-18 17:29:49 +0900226 Implicits(toolDeps).
Jiyong Park11a65972021-02-01 21:09:38 +0900227 Output(output).
Jiyong Park6f0f6882020-11-12 13:14:30 +0900228 Text(rootDir.String()) // directory where to find fs_config_files|dirs
229
230 // rootDir is not deleted. Might be useful for quick inspection.
Colin Crossf1a035e2020-11-16 17:32:30 -0800231 builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park65c49f52020-11-24 14:23:26 +0900232
Jiyong Park11a65972021-02-01 21:09:38 +0900233 return output
Jiyong Park65c49f52020-11-24 14:23:26 +0900234}
235
Inseob Kimcc8e5362021-02-03 14:05:24 +0900236func (f *filesystem) buildFileContexts(ctx android.ModuleContext) android.OutputPath {
237 builder := android.NewRuleBuilder(pctx, ctx)
238 fcBin := android.PathForModuleOut(ctx, "file_contexts.bin")
239 builder.Command().BuiltTool("sefcontext_compile").
240 FlagWithOutput("-o ", fcBin).
241 Input(android.PathForModuleSrc(ctx, proptools.String(f.properties.File_contexts)))
242 builder.Build("build_filesystem_file_contexts", fmt.Sprintf("Creating filesystem file contexts for %s", f.BaseModuleName()))
243 return fcBin.OutputPath
244}
245
Jiyong Park72678312021-01-18 17:29:49 +0900246func (f *filesystem) buildPropFile(ctx android.ModuleContext) (propFile android.OutputPath, toolDeps android.Paths) {
247 type prop struct {
248 name string
249 value string
250 }
251
252 var props []prop
253 var deps android.Paths
254 addStr := func(name string, value string) {
255 props = append(props, prop{name, value})
256 }
257 addPath := func(name string, path android.Path) {
258 props = append(props, prop{name, path.String()})
259 deps = append(deps, path)
260 }
261
Jiyong Park11a65972021-02-01 21:09:38 +0900262 // Type string that build_image.py accepts.
263 fsTypeStr := func(t fsType) string {
264 switch t {
265 // TODO(jiyong): add more types like f2fs, erofs, etc.
266 case ext4Type:
267 return "ext4"
268 }
269 panic(fmt.Errorf("unsupported fs type %v", t))
270 }
271
272 addStr("fs_type", fsTypeStr(f.fsType(ctx)))
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900273 addStr("mount_point", "/")
Jiyong Park72678312021-01-18 17:29:49 +0900274 addStr("use_dynamic_partition_size", "true")
275 addPath("ext_mkuserimg", ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs"))
276 // b/177813163 deps of the host tools have to be added. Remove this.
277 for _, t := range []string{"mke2fs", "e2fsdroid", "tune2fs"} {
278 deps = append(deps, ctx.Config().HostToolPath(ctx, t))
279 }
280
Jiyong Park71baa762021-01-18 21:11:03 +0900281 if proptools.Bool(f.properties.Use_avb) {
282 addStr("avb_hashtree_enable", "true")
283 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
284 algorithm := proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096")
285 addStr("avb_algorithm", algorithm)
286 key := android.PathForModuleSrc(ctx, proptools.String(f.properties.Avb_private_key))
287 addPath("avb_key_path", key)
288 addStr("avb_add_hashtree_footer_args", "--do_not_generate_fec")
Jiyong Parkac4076d2021-03-15 23:21:30 +0900289 partitionName := proptools.StringDefault(f.properties.Partition_name, f.Name())
290 addStr("partition_name", partitionName)
Jiyong Park71baa762021-01-18 21:11:03 +0900291 }
292
Inseob Kimcc8e5362021-02-03 14:05:24 +0900293 if proptools.String(f.properties.File_contexts) != "" {
294 addPath("selinux_fc", f.buildFileContexts(ctx))
295 }
296
Jiyong Park72678312021-01-18 17:29:49 +0900297 propFile = android.PathForModuleOut(ctx, "prop").OutputPath
298 builder := android.NewRuleBuilder(pctx, ctx)
299 builder.Command().Text("rm").Flag("-rf").Output(propFile)
300 for _, p := range props {
301 builder.Command().
Jiyong Park3db465d2021-01-26 14:08:16 +0900302 Text("echo").
Jiyong Park72678312021-01-18 17:29:49 +0900303 Flag(`"` + p.name + "=" + p.value + `"`).
304 Text(">>").Output(propFile)
305 }
306 builder.Build("build_filesystem_prop", fmt.Sprintf("Creating filesystem props for %s", f.BaseModuleName()))
307 return propFile, deps
308}
309
Jiyong Park837cdb22021-02-05 00:17:14 +0900310func (f *filesystem) buildCpioImage(ctx android.ModuleContext, compressed bool) android.OutputPath {
Jiyong Park11a65972021-02-01 21:09:38 +0900311 if proptools.Bool(f.properties.Use_avb) {
312 ctx.PropertyErrorf("use_avb", "signing compresed cpio image using avbtool is not supported."+
313 "Consider adding this to bootimg module and signing the entire boot image.")
314 }
315
Inseob Kimcc8e5362021-02-03 14:05:24 +0900316 if proptools.String(f.properties.File_contexts) != "" {
317 ctx.PropertyErrorf("file_contexts", "file_contexts is not supported for compressed cpio image.")
318 }
319
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900320 depsZipFile := android.PathForModuleOut(ctx, "deps.zip").OutputPath
321 f.CopyDepsToZip(ctx, depsZipFile)
322
323 builder := android.NewRuleBuilder(pctx, ctx)
324 depsBase := proptools.StringDefault(f.properties.Base_dir, ".")
325 rebasedDepsZip := android.PathForModuleOut(ctx, "rebased_deps.zip").OutputPath
326 builder.Command().
327 BuiltTool("zip2zip").
328 FlagWithInput("-i ", depsZipFile).
329 FlagWithOutput("-o ", rebasedDepsZip).
330 Text("**/*:" + proptools.ShellEscape(depsBase)) // zip2zip verifies depsBase
Jiyong Park11a65972021-02-01 21:09:38 +0900331
332 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900333 rootZip := f.buildRootZip(ctx)
Jiyong Park11a65972021-02-01 21:09:38 +0900334 builder.Command().
335 BuiltTool("zipsync").
336 FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear.
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900337 Input(rootZip).
338 Input(rebasedDepsZip)
Jiyong Park11a65972021-02-01 21:09:38 +0900339
340 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Jiyong Park837cdb22021-02-05 00:17:14 +0900341 cmd := builder.Command().
Jiyong Park11a65972021-02-01 21:09:38 +0900342 BuiltTool("mkbootfs").
Jiyong Park837cdb22021-02-05 00:17:14 +0900343 Text(rootDir.String()) // input directory
344 if compressed {
345 cmd.Text("|").
346 BuiltTool("lz4").
347 Flag("--favor-decSpeed"). // for faster boot
348 Flag("-12"). // maximum compression level
349 Flag("-l"). // legacy format for kernel
350 Text(">").Output(output)
351 } else {
352 cmd.Text(">").Output(output)
353 }
Jiyong Park11a65972021-02-01 21:09:38 +0900354
355 // rootDir is not deleted. Might be useful for quick inspection.
Jiyong Park837cdb22021-02-05 00:17:14 +0900356 builder.Build("build_cpio_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park11a65972021-02-01 21:09:38 +0900357
358 return output
359}
360
Jiyong Park65c49f52020-11-24 14:23:26 +0900361var _ android.AndroidMkEntriesProvider = (*filesystem)(nil)
362
363// Implements android.AndroidMkEntriesProvider
364func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries {
365 return []android.AndroidMkEntries{android.AndroidMkEntries{
366 Class: "ETC",
367 OutputFile: android.OptionalPathForPath(f.output),
368 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700369 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Park65c49f52020-11-24 14:23:26 +0900370 entries.SetString("LOCAL_MODULE_PATH", f.installDir.ToMakePath().String())
371 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
372 },
373 },
374 }}
Jiyong Park6f0f6882020-11-12 13:14:30 +0900375}
Jiyong Park12a719c2021-01-07 15:31:24 +0900376
Jiyong Park940dfd42021-02-04 15:37:34 +0900377var _ android.OutputFileProducer = (*filesystem)(nil)
378
379// Implements android.OutputFileProducer
380func (f *filesystem) OutputFiles(tag string) (android.Paths, error) {
381 if tag == "" {
382 return []android.Path{f.output}, nil
383 }
384 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
385}
386
Jiyong Park12a719c2021-01-07 15:31:24 +0900387// Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex
388// package to have access to the output file.
389type Filesystem interface {
390 android.Module
391 OutputPath() android.Path
Jiyong Park972e06c2021-03-15 23:32:49 +0900392
393 // Returns the output file that is signed by avbtool. If this module is not signed, returns
394 // nil.
395 SignedOutputPath() android.Path
Jiyong Park12a719c2021-01-07 15:31:24 +0900396}
397
398var _ Filesystem = (*filesystem)(nil)
399
400func (f *filesystem) OutputPath() android.Path {
401 return f.output
402}
Jiyong Park972e06c2021-03-15 23:32:49 +0900403
404func (f *filesystem) SignedOutputPath() android.Path {
405 if proptools.Bool(f.properties.Use_avb) {
406 return f.OutputPath()
407 }
408 return nil
409}