blob: 177296cb20cda6e65502b2616c8dfe6d581d5c34 [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
51 // Type of the filesystem. Currently, ext4 and compressed_cpio are supported. Default is
52 // ext4.
53 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"`
Jiyong Park71baa762021-01-18 21:11:03 +090057}
58
Jiyong Park65c49f52020-11-24 14:23:26 +090059// android_filesystem packages a set of modules and their transitive dependencies into a filesystem
60// image. The filesystem images are expected to be mounted in the target device, which means the
61// modules in the filesystem image are built for the target device (i.e. Android, not Linux host).
62// The modules are placed in the filesystem image just like they are installed to the ordinary
63// partitions like system.img. For example, cc_library modules are placed under ./lib[64] directory.
Jiyong Park6f0f6882020-11-12 13:14:30 +090064func filesystemFactory() android.Module {
65 module := &filesystem{}
Jiyong Park71baa762021-01-18 21:11:03 +090066 module.AddProperties(&module.properties)
Jiyong Park6f0f6882020-11-12 13:14:30 +090067 android.InitPackageModule(module)
68 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
69 return module
70}
71
Jiyong Park12a719c2021-01-07 15:31:24 +090072var dependencyTag = struct {
73 blueprint.BaseDependencyTag
74 android.InstallAlwaysNeededDependencyTag
75}{}
Jiyong Park65b62242020-11-25 12:44:59 +090076
Jiyong Park6f0f6882020-11-12 13:14:30 +090077func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park65b62242020-11-25 12:44:59 +090078 f.AddDeps(ctx, dependencyTag)
Jiyong Park6f0f6882020-11-12 13:14:30 +090079}
80
Jiyong Park11a65972021-02-01 21:09:38 +090081type fsType int
82
83const (
84 ext4Type fsType = iota
85 compressedCpioType
86 unknown
87)
88
89func (f *filesystem) fsType(ctx android.ModuleContext) fsType {
90 typeStr := proptools.StringDefault(f.properties.Type, "ext4")
91 switch typeStr {
92 case "ext4":
93 return ext4Type
94 case "compressed_cpio":
95 return compressedCpioType
96 default:
97 ctx.PropertyErrorf("type", "%q not supported", typeStr)
98 return unknown
99 }
100}
101
Jiyong Park65c49f52020-11-24 14:23:26 +0900102func (f *filesystem) installFileName() string {
103 return f.BaseModuleName() + ".img"
104}
105
Jiyong Park6f0f6882020-11-12 13:14:30 +0900106var pctx = android.NewPackageContext("android/soong/filesystem")
107
108func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park11a65972021-02-01 21:09:38 +0900109 switch f.fsType(ctx) {
110 case ext4Type:
111 f.output = f.buildImageUsingBuildImage(ctx)
112 case compressedCpioType:
113 f.output = f.buildCompressedCpioImage(ctx)
114 default:
115 return
116 }
117
118 f.installDir = android.PathForModuleInstall(ctx, "etc")
119 ctx.InstallFile(f.installDir, f.installFileName(), f.output)
120}
121
122func (f *filesystem) buildImageUsingBuildImage(ctx android.ModuleContext) android.OutputPath {
Jiyong Park6f0f6882020-11-12 13:14:30 +0900123 zipFile := android.PathForModuleOut(ctx, "temp.zip").OutputPath
124 f.CopyDepsToZip(ctx, zipFile)
125
126 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -0800127 builder := android.NewRuleBuilder(pctx, ctx)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900128 builder.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800129 BuiltTool("zipsync").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900130 FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear.
131 Input(zipFile)
132
Jiyong Park72678312021-01-18 17:29:49 +0900133 propFile, toolDeps := f.buildPropFile(ctx)
Jiyong Park11a65972021-02-01 21:09:38 +0900134 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -0800135 builder.Command().BuiltTool("build_image").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900136 Text(rootDir.String()). // input directory
137 Input(propFile).
Jiyong Park72678312021-01-18 17:29:49 +0900138 Implicits(toolDeps).
Jiyong Park11a65972021-02-01 21:09:38 +0900139 Output(output).
Jiyong Park6f0f6882020-11-12 13:14:30 +0900140 Text(rootDir.String()) // directory where to find fs_config_files|dirs
141
142 // rootDir is not deleted. Might be useful for quick inspection.
Colin Crossf1a035e2020-11-16 17:32:30 -0800143 builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park65c49f52020-11-24 14:23:26 +0900144
Jiyong Park11a65972021-02-01 21:09:38 +0900145 return output
Jiyong Park65c49f52020-11-24 14:23:26 +0900146}
147
Inseob Kimcc8e5362021-02-03 14:05:24 +0900148func (f *filesystem) buildFileContexts(ctx android.ModuleContext) android.OutputPath {
149 builder := android.NewRuleBuilder(pctx, ctx)
150 fcBin := android.PathForModuleOut(ctx, "file_contexts.bin")
151 builder.Command().BuiltTool("sefcontext_compile").
152 FlagWithOutput("-o ", fcBin).
153 Input(android.PathForModuleSrc(ctx, proptools.String(f.properties.File_contexts)))
154 builder.Build("build_filesystem_file_contexts", fmt.Sprintf("Creating filesystem file contexts for %s", f.BaseModuleName()))
155 return fcBin.OutputPath
156}
157
Jiyong Park72678312021-01-18 17:29:49 +0900158func (f *filesystem) buildPropFile(ctx android.ModuleContext) (propFile android.OutputPath, toolDeps android.Paths) {
159 type prop struct {
160 name string
161 value string
162 }
163
164 var props []prop
165 var deps android.Paths
166 addStr := func(name string, value string) {
167 props = append(props, prop{name, value})
168 }
169 addPath := func(name string, path android.Path) {
170 props = append(props, prop{name, path.String()})
171 deps = append(deps, path)
172 }
173
Jiyong Park11a65972021-02-01 21:09:38 +0900174 // Type string that build_image.py accepts.
175 fsTypeStr := func(t fsType) string {
176 switch t {
177 // TODO(jiyong): add more types like f2fs, erofs, etc.
178 case ext4Type:
179 return "ext4"
180 }
181 panic(fmt.Errorf("unsupported fs type %v", t))
182 }
183
184 addStr("fs_type", fsTypeStr(f.fsType(ctx)))
Jiyong Park72678312021-01-18 17:29:49 +0900185 addStr("mount_point", "system")
186 addStr("use_dynamic_partition_size", "true")
187 addPath("ext_mkuserimg", ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs"))
188 // b/177813163 deps of the host tools have to be added. Remove this.
189 for _, t := range []string{"mke2fs", "e2fsdroid", "tune2fs"} {
190 deps = append(deps, ctx.Config().HostToolPath(ctx, t))
191 }
192
Jiyong Park71baa762021-01-18 21:11:03 +0900193 if proptools.Bool(f.properties.Use_avb) {
194 addStr("avb_hashtree_enable", "true")
195 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
196 algorithm := proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096")
197 addStr("avb_algorithm", algorithm)
198 key := android.PathForModuleSrc(ctx, proptools.String(f.properties.Avb_private_key))
199 addPath("avb_key_path", key)
200 addStr("avb_add_hashtree_footer_args", "--do_not_generate_fec")
201 addStr("partition_name", f.Name())
202 }
203
Inseob Kimcc8e5362021-02-03 14:05:24 +0900204 if proptools.String(f.properties.File_contexts) != "" {
205 addPath("selinux_fc", f.buildFileContexts(ctx))
206 }
207
Jiyong Park72678312021-01-18 17:29:49 +0900208 propFile = android.PathForModuleOut(ctx, "prop").OutputPath
209 builder := android.NewRuleBuilder(pctx, ctx)
210 builder.Command().Text("rm").Flag("-rf").Output(propFile)
211 for _, p := range props {
212 builder.Command().
Jiyong Park3db465d2021-01-26 14:08:16 +0900213 Text("echo").
Jiyong Park72678312021-01-18 17:29:49 +0900214 Flag(`"` + p.name + "=" + p.value + `"`).
215 Text(">>").Output(propFile)
216 }
217 builder.Build("build_filesystem_prop", fmt.Sprintf("Creating filesystem props for %s", f.BaseModuleName()))
218 return propFile, deps
219}
220
Jiyong Park11a65972021-02-01 21:09:38 +0900221func (f *filesystem) buildCompressedCpioImage(ctx android.ModuleContext) android.OutputPath {
222 if proptools.Bool(f.properties.Use_avb) {
223 ctx.PropertyErrorf("use_avb", "signing compresed cpio image using avbtool is not supported."+
224 "Consider adding this to bootimg module and signing the entire boot image.")
225 }
226
Inseob Kimcc8e5362021-02-03 14:05:24 +0900227 if proptools.String(f.properties.File_contexts) != "" {
228 ctx.PropertyErrorf("file_contexts", "file_contexts is not supported for compressed cpio image.")
229 }
230
Jiyong Park11a65972021-02-01 21:09:38 +0900231 zipFile := android.PathForModuleOut(ctx, "temp.zip").OutputPath
232 f.CopyDepsToZip(ctx, zipFile)
233
234 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
235 builder := android.NewRuleBuilder(pctx, ctx)
236 builder.Command().
237 BuiltTool("zipsync").
238 FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear.
239 Input(zipFile)
240
241 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
242 builder.Command().
243 BuiltTool("mkbootfs").
244 Text(rootDir.String()). // input directory
245 Text("|").
246 BuiltTool("lz4").
247 Flag("--favor-decSpeed"). // for faster boot
248 Flag("-12"). // maximum compression level
249 Flag("-l"). // legacy format for kernel
250 Text(">").Output(output)
251
252 // rootDir is not deleted. Might be useful for quick inspection.
253 builder.Build("build_compressed_cpio_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
254
255 return output
256}
257
Jiyong Park65c49f52020-11-24 14:23:26 +0900258var _ android.AndroidMkEntriesProvider = (*filesystem)(nil)
259
260// Implements android.AndroidMkEntriesProvider
261func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries {
262 return []android.AndroidMkEntries{android.AndroidMkEntries{
263 Class: "ETC",
264 OutputFile: android.OptionalPathForPath(f.output),
265 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
266 func(entries *android.AndroidMkEntries) {
267 entries.SetString("LOCAL_MODULE_PATH", f.installDir.ToMakePath().String())
268 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
269 },
270 },
271 }}
Jiyong Park6f0f6882020-11-12 13:14:30 +0900272}
Jiyong Park12a719c2021-01-07 15:31:24 +0900273
274// Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex
275// package to have access to the output file.
276type Filesystem interface {
277 android.Module
278 OutputPath() android.Path
279}
280
281var _ Filesystem = (*filesystem)(nil)
282
283func (f *filesystem) OutputPath() android.Path {
284 return f.output
285}