blob: 69496c99865c67dcce3a06a4bd170a6c1b9d7616 [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"
19
20 "android/soong/android"
Jiyong Park65b62242020-11-25 12:44:59 +090021
22 "github.com/google/blueprint"
Jiyong Park71baa762021-01-18 21:11:03 +090023 "github.com/google/blueprint/proptools"
Jiyong Park6f0f6882020-11-12 13:14:30 +090024)
25
26func init() {
27 android.RegisterModuleType("android_filesystem", filesystemFactory)
28}
29
30type filesystem struct {
31 android.ModuleBase
32 android.PackagingBase
Jiyong Park65c49f52020-11-24 14:23:26 +090033
Jiyong Park71baa762021-01-18 21:11:03 +090034 properties filesystemProperties
35
Jiyong Park65c49f52020-11-24 14:23:26 +090036 output android.OutputPath
37 installDir android.InstallPath
Jiyong Park6f0f6882020-11-12 13:14:30 +090038}
39
Jiyong Park71baa762021-01-18 21:11:03 +090040type filesystemProperties struct {
41 // When set to true, sign the image with avbtool. Default is false.
42 Use_avb *bool
43
44 // Path to the private key that avbtool will use to sign this filesystem image.
45 // TODO(jiyong): allow apex_key to be specified here
46 Avb_private_key *string `android:"path"`
47
48 // Hash and signing algorithm for avbtool. Default is SHA256_RSA4096.
49 Avb_algorithm *string
Jiyong Park11a65972021-02-01 21:09:38 +090050
Jiyong Park837cdb22021-02-05 00:17:14 +090051 // Type of the filesystem. Currently, ext4, cpio, and compressed_cpio are supported. Default
52 // is ext4.
Jiyong Park11a65972021-02-01 21:09:38 +090053 Type *string
Inseob Kimcc8e5362021-02-03 14:05:24 +090054
55 // file_contexts file to make image. Currently, only ext4 is supported.
56 File_contexts *string `android:"path"`
Inseob Kim2ce1b5d2021-02-15 17:01:04 +090057
58 // Base directory relative to root, to which deps are installed, e.g. "system". Default is "."
59 // (root).
60 Base_dir *string
Jiyong Park71baa762021-01-18 21:11:03 +090061}
62
Jiyong Park65c49f52020-11-24 14:23:26 +090063// android_filesystem packages a set of modules and their transitive dependencies into a filesystem
64// image. The filesystem images are expected to be mounted in the target device, which means the
65// modules in the filesystem image are built for the target device (i.e. Android, not Linux host).
66// The modules are placed in the filesystem image just like they are installed to the ordinary
67// partitions like system.img. For example, cc_library modules are placed under ./lib[64] directory.
Jiyong Park6f0f6882020-11-12 13:14:30 +090068func filesystemFactory() android.Module {
69 module := &filesystem{}
Jiyong Park71baa762021-01-18 21:11:03 +090070 module.AddProperties(&module.properties)
Jiyong Park6f0f6882020-11-12 13:14:30 +090071 android.InitPackageModule(module)
72 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
73 return module
74}
75
Jiyong Park12a719c2021-01-07 15:31:24 +090076var dependencyTag = struct {
77 blueprint.BaseDependencyTag
78 android.InstallAlwaysNeededDependencyTag
79}{}
Jiyong Park65b62242020-11-25 12:44:59 +090080
Jiyong Park6f0f6882020-11-12 13:14:30 +090081func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park65b62242020-11-25 12:44:59 +090082 f.AddDeps(ctx, dependencyTag)
Jiyong Park6f0f6882020-11-12 13:14:30 +090083}
84
Jiyong Park11a65972021-02-01 21:09:38 +090085type fsType int
86
87const (
88 ext4Type fsType = iota
89 compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +090090 cpioType // uncompressed
Jiyong Park11a65972021-02-01 21:09:38 +090091 unknown
92)
93
94func (f *filesystem) fsType(ctx android.ModuleContext) fsType {
95 typeStr := proptools.StringDefault(f.properties.Type, "ext4")
96 switch typeStr {
97 case "ext4":
98 return ext4Type
99 case "compressed_cpio":
100 return compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900101 case "cpio":
102 return cpioType
Jiyong Park11a65972021-02-01 21:09:38 +0900103 default:
104 ctx.PropertyErrorf("type", "%q not supported", typeStr)
105 return unknown
106 }
107}
108
Jiyong Park65c49f52020-11-24 14:23:26 +0900109func (f *filesystem) installFileName() string {
110 return f.BaseModuleName() + ".img"
111}
112
Jiyong Park6f0f6882020-11-12 13:14:30 +0900113var pctx = android.NewPackageContext("android/soong/filesystem")
114
115func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park11a65972021-02-01 21:09:38 +0900116 switch f.fsType(ctx) {
117 case ext4Type:
118 f.output = f.buildImageUsingBuildImage(ctx)
119 case compressedCpioType:
Jiyong Park837cdb22021-02-05 00:17:14 +0900120 f.output = f.buildCpioImage(ctx, true)
121 case cpioType:
122 f.output = f.buildCpioImage(ctx, false)
Jiyong Park11a65972021-02-01 21:09:38 +0900123 default:
124 return
125 }
126
127 f.installDir = android.PathForModuleInstall(ctx, "etc")
128 ctx.InstallFile(f.installDir, f.installFileName(), f.output)
129}
130
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900131// root zip will contain stuffs like dirs or symlinks.
132func (f *filesystem) buildRootZip(ctx android.ModuleContext) android.OutputPath {
133 rootDir := android.PathForModuleGen(ctx, "root").OutputPath
134 builder := android.NewRuleBuilder(pctx, ctx)
135 builder.Command().Text("rm -rf").Text(rootDir.String())
136 builder.Command().Text("mkdir -p").Text(rootDir.String())
137
138 // Currently root.zip is empty, and just a placeholder now. Dirs and symlinks will be added.
139
140 zipOut := android.PathForModuleGen(ctx, "root.zip").OutputPath
141
142 builder.Command().
143 BuiltTool("soong_zip").
144 FlagWithOutput("-o ", zipOut).
145 FlagWithArg("-C ", rootDir.String()).
146 Flag("-L 0"). // no compression because this will be unzipped soon
147 FlagWithArg("-D ", rootDir.String()).
148 Flag("-d") // include empty directories
149 builder.Command().Text("rm -rf").Text(rootDir.String())
150
151 builder.Build("zip_root", fmt.Sprintf("zipping root contents for %s", ctx.ModuleName()))
152 return zipOut
153}
154
Jiyong Park11a65972021-02-01 21:09:38 +0900155func (f *filesystem) buildImageUsingBuildImage(ctx android.ModuleContext) android.OutputPath {
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900156 depsZipFile := android.PathForModuleOut(ctx, "deps.zip").OutputPath
157 f.CopyDepsToZip(ctx, depsZipFile)
158
159 builder := android.NewRuleBuilder(pctx, ctx)
160 depsBase := proptools.StringDefault(f.properties.Base_dir, ".")
161 rebasedDepsZip := android.PathForModuleOut(ctx, "rebased_deps.zip").OutputPath
162 builder.Command().
163 BuiltTool("zip2zip").
164 FlagWithInput("-i ", depsZipFile).
165 FlagWithOutput("-o ", rebasedDepsZip).
166 Text("**/*:" + proptools.ShellEscape(depsBase)) // zip2zip verifies depsBase
Jiyong Park6f0f6882020-11-12 13:14:30 +0900167
168 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900169 rootZip := f.buildRootZip(ctx)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900170 builder.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800171 BuiltTool("zipsync").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900172 FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear.
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900173 Input(rootZip).
174 Input(rebasedDepsZip)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900175
Jiyong Park72678312021-01-18 17:29:49 +0900176 propFile, toolDeps := f.buildPropFile(ctx)
Jiyong Park11a65972021-02-01 21:09:38 +0900177 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -0800178 builder.Command().BuiltTool("build_image").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900179 Text(rootDir.String()). // input directory
180 Input(propFile).
Jiyong Park72678312021-01-18 17:29:49 +0900181 Implicits(toolDeps).
Jiyong Park11a65972021-02-01 21:09:38 +0900182 Output(output).
Jiyong Park6f0f6882020-11-12 13:14:30 +0900183 Text(rootDir.String()) // directory where to find fs_config_files|dirs
184
185 // rootDir is not deleted. Might be useful for quick inspection.
Colin Crossf1a035e2020-11-16 17:32:30 -0800186 builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park65c49f52020-11-24 14:23:26 +0900187
Jiyong Park11a65972021-02-01 21:09:38 +0900188 return output
Jiyong Park65c49f52020-11-24 14:23:26 +0900189}
190
Inseob Kimcc8e5362021-02-03 14:05:24 +0900191func (f *filesystem) buildFileContexts(ctx android.ModuleContext) android.OutputPath {
192 builder := android.NewRuleBuilder(pctx, ctx)
193 fcBin := android.PathForModuleOut(ctx, "file_contexts.bin")
194 builder.Command().BuiltTool("sefcontext_compile").
195 FlagWithOutput("-o ", fcBin).
196 Input(android.PathForModuleSrc(ctx, proptools.String(f.properties.File_contexts)))
197 builder.Build("build_filesystem_file_contexts", fmt.Sprintf("Creating filesystem file contexts for %s", f.BaseModuleName()))
198 return fcBin.OutputPath
199}
200
Jiyong Park72678312021-01-18 17:29:49 +0900201func (f *filesystem) buildPropFile(ctx android.ModuleContext) (propFile android.OutputPath, toolDeps android.Paths) {
202 type prop struct {
203 name string
204 value string
205 }
206
207 var props []prop
208 var deps android.Paths
209 addStr := func(name string, value string) {
210 props = append(props, prop{name, value})
211 }
212 addPath := func(name string, path android.Path) {
213 props = append(props, prop{name, path.String()})
214 deps = append(deps, path)
215 }
216
Jiyong Park11a65972021-02-01 21:09:38 +0900217 // Type string that build_image.py accepts.
218 fsTypeStr := func(t fsType) string {
219 switch t {
220 // TODO(jiyong): add more types like f2fs, erofs, etc.
221 case ext4Type:
222 return "ext4"
223 }
224 panic(fmt.Errorf("unsupported fs type %v", t))
225 }
226
227 addStr("fs_type", fsTypeStr(f.fsType(ctx)))
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900228 addStr("mount_point", "/")
Jiyong Park72678312021-01-18 17:29:49 +0900229 addStr("use_dynamic_partition_size", "true")
230 addPath("ext_mkuserimg", ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs"))
231 // b/177813163 deps of the host tools have to be added. Remove this.
232 for _, t := range []string{"mke2fs", "e2fsdroid", "tune2fs"} {
233 deps = append(deps, ctx.Config().HostToolPath(ctx, t))
234 }
235
Jiyong Park71baa762021-01-18 21:11:03 +0900236 if proptools.Bool(f.properties.Use_avb) {
237 addStr("avb_hashtree_enable", "true")
238 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
239 algorithm := proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096")
240 addStr("avb_algorithm", algorithm)
241 key := android.PathForModuleSrc(ctx, proptools.String(f.properties.Avb_private_key))
242 addPath("avb_key_path", key)
243 addStr("avb_add_hashtree_footer_args", "--do_not_generate_fec")
244 addStr("partition_name", f.Name())
245 }
246
Inseob Kimcc8e5362021-02-03 14:05:24 +0900247 if proptools.String(f.properties.File_contexts) != "" {
248 addPath("selinux_fc", f.buildFileContexts(ctx))
249 }
250
Jiyong Park72678312021-01-18 17:29:49 +0900251 propFile = android.PathForModuleOut(ctx, "prop").OutputPath
252 builder := android.NewRuleBuilder(pctx, ctx)
253 builder.Command().Text("rm").Flag("-rf").Output(propFile)
254 for _, p := range props {
255 builder.Command().
Jiyong Park3db465d2021-01-26 14:08:16 +0900256 Text("echo").
Jiyong Park72678312021-01-18 17:29:49 +0900257 Flag(`"` + p.name + "=" + p.value + `"`).
258 Text(">>").Output(propFile)
259 }
260 builder.Build("build_filesystem_prop", fmt.Sprintf("Creating filesystem props for %s", f.BaseModuleName()))
261 return propFile, deps
262}
263
Jiyong Park837cdb22021-02-05 00:17:14 +0900264func (f *filesystem) buildCpioImage(ctx android.ModuleContext, compressed bool) android.OutputPath {
Jiyong Park11a65972021-02-01 21:09:38 +0900265 if proptools.Bool(f.properties.Use_avb) {
266 ctx.PropertyErrorf("use_avb", "signing compresed cpio image using avbtool is not supported."+
267 "Consider adding this to bootimg module and signing the entire boot image.")
268 }
269
Inseob Kimcc8e5362021-02-03 14:05:24 +0900270 if proptools.String(f.properties.File_contexts) != "" {
271 ctx.PropertyErrorf("file_contexts", "file_contexts is not supported for compressed cpio image.")
272 }
273
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900274 depsZipFile := android.PathForModuleOut(ctx, "deps.zip").OutputPath
275 f.CopyDepsToZip(ctx, depsZipFile)
276
277 builder := android.NewRuleBuilder(pctx, ctx)
278 depsBase := proptools.StringDefault(f.properties.Base_dir, ".")
279 rebasedDepsZip := android.PathForModuleOut(ctx, "rebased_deps.zip").OutputPath
280 builder.Command().
281 BuiltTool("zip2zip").
282 FlagWithInput("-i ", depsZipFile).
283 FlagWithOutput("-o ", rebasedDepsZip).
284 Text("**/*:" + proptools.ShellEscape(depsBase)) // zip2zip verifies depsBase
Jiyong Park11a65972021-02-01 21:09:38 +0900285
286 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900287 rootZip := f.buildRootZip(ctx)
Jiyong Park11a65972021-02-01 21:09:38 +0900288 builder.Command().
289 BuiltTool("zipsync").
290 FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear.
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900291 Input(rootZip).
292 Input(rebasedDepsZip)
Jiyong Park11a65972021-02-01 21:09:38 +0900293
294 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Jiyong Park837cdb22021-02-05 00:17:14 +0900295 cmd := builder.Command().
Jiyong Park11a65972021-02-01 21:09:38 +0900296 BuiltTool("mkbootfs").
Jiyong Park837cdb22021-02-05 00:17:14 +0900297 Text(rootDir.String()) // input directory
298 if compressed {
299 cmd.Text("|").
300 BuiltTool("lz4").
301 Flag("--favor-decSpeed"). // for faster boot
302 Flag("-12"). // maximum compression level
303 Flag("-l"). // legacy format for kernel
304 Text(">").Output(output)
305 } else {
306 cmd.Text(">").Output(output)
307 }
Jiyong Park11a65972021-02-01 21:09:38 +0900308
309 // rootDir is not deleted. Might be useful for quick inspection.
Jiyong Park837cdb22021-02-05 00:17:14 +0900310 builder.Build("build_cpio_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park11a65972021-02-01 21:09:38 +0900311
312 return output
313}
314
Jiyong Park65c49f52020-11-24 14:23:26 +0900315var _ android.AndroidMkEntriesProvider = (*filesystem)(nil)
316
317// Implements android.AndroidMkEntriesProvider
318func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries {
319 return []android.AndroidMkEntries{android.AndroidMkEntries{
320 Class: "ETC",
321 OutputFile: android.OptionalPathForPath(f.output),
322 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
323 func(entries *android.AndroidMkEntries) {
324 entries.SetString("LOCAL_MODULE_PATH", f.installDir.ToMakePath().String())
325 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
326 },
327 },
328 }}
Jiyong Park6f0f6882020-11-12 13:14:30 +0900329}
Jiyong Park12a719c2021-01-07 15:31:24 +0900330
Jiyong Park940dfd42021-02-04 15:37:34 +0900331var _ android.OutputFileProducer = (*filesystem)(nil)
332
333// Implements android.OutputFileProducer
334func (f *filesystem) OutputFiles(tag string) (android.Paths, error) {
335 if tag == "" {
336 return []android.Path{f.output}, nil
337 }
338 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
339}
340
Jiyong Park12a719c2021-01-07 15:31:24 +0900341// Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex
342// package to have access to the output file.
343type Filesystem interface {
344 android.Module
345 OutputPath() android.Path
346}
347
348var _ Filesystem = (*filesystem)(nil)
349
350func (f *filesystem) OutputPath() android.Path {
351 return f.output
352}