blob: 76581548ea6de217a5528f8ede37bf846dddb034 [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"`
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
Jiyong Park837cdb22021-02-05 00:17:14 +090086 cpioType // uncompressed
Jiyong Park11a65972021-02-01 21:09:38 +090087 unknown
88)
89
90func (f *filesystem) fsType(ctx android.ModuleContext) fsType {
91 typeStr := proptools.StringDefault(f.properties.Type, "ext4")
92 switch typeStr {
93 case "ext4":
94 return ext4Type
95 case "compressed_cpio":
96 return compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +090097 case "cpio":
98 return cpioType
Jiyong Park11a65972021-02-01 21:09:38 +090099 default:
100 ctx.PropertyErrorf("type", "%q not supported", typeStr)
101 return unknown
102 }
103}
104
Jiyong Park65c49f52020-11-24 14:23:26 +0900105func (f *filesystem) installFileName() string {
106 return f.BaseModuleName() + ".img"
107}
108
Jiyong Park6f0f6882020-11-12 13:14:30 +0900109var pctx = android.NewPackageContext("android/soong/filesystem")
110
111func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park11a65972021-02-01 21:09:38 +0900112 switch f.fsType(ctx) {
113 case ext4Type:
114 f.output = f.buildImageUsingBuildImage(ctx)
115 case compressedCpioType:
Jiyong Park837cdb22021-02-05 00:17:14 +0900116 f.output = f.buildCpioImage(ctx, true)
117 case cpioType:
118 f.output = f.buildCpioImage(ctx, false)
Jiyong Park11a65972021-02-01 21:09:38 +0900119 default:
120 return
121 }
122
123 f.installDir = android.PathForModuleInstall(ctx, "etc")
124 ctx.InstallFile(f.installDir, f.installFileName(), f.output)
125}
126
127func (f *filesystem) buildImageUsingBuildImage(ctx android.ModuleContext) android.OutputPath {
Jiyong Park6f0f6882020-11-12 13:14:30 +0900128 zipFile := android.PathForModuleOut(ctx, "temp.zip").OutputPath
129 f.CopyDepsToZip(ctx, zipFile)
130
131 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -0800132 builder := android.NewRuleBuilder(pctx, ctx)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900133 builder.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800134 BuiltTool("zipsync").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900135 FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear.
136 Input(zipFile)
137
Jiyong Park72678312021-01-18 17:29:49 +0900138 propFile, toolDeps := f.buildPropFile(ctx)
Jiyong Park11a65972021-02-01 21:09:38 +0900139 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -0800140 builder.Command().BuiltTool("build_image").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900141 Text(rootDir.String()). // input directory
142 Input(propFile).
Jiyong Park72678312021-01-18 17:29:49 +0900143 Implicits(toolDeps).
Jiyong Park11a65972021-02-01 21:09:38 +0900144 Output(output).
Jiyong Park6f0f6882020-11-12 13:14:30 +0900145 Text(rootDir.String()) // directory where to find fs_config_files|dirs
146
147 // rootDir is not deleted. Might be useful for quick inspection.
Colin Crossf1a035e2020-11-16 17:32:30 -0800148 builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park65c49f52020-11-24 14:23:26 +0900149
Jiyong Park11a65972021-02-01 21:09:38 +0900150 return output
Jiyong Park65c49f52020-11-24 14:23:26 +0900151}
152
Inseob Kimcc8e5362021-02-03 14:05:24 +0900153func (f *filesystem) buildFileContexts(ctx android.ModuleContext) android.OutputPath {
154 builder := android.NewRuleBuilder(pctx, ctx)
155 fcBin := android.PathForModuleOut(ctx, "file_contexts.bin")
156 builder.Command().BuiltTool("sefcontext_compile").
157 FlagWithOutput("-o ", fcBin).
158 Input(android.PathForModuleSrc(ctx, proptools.String(f.properties.File_contexts)))
159 builder.Build("build_filesystem_file_contexts", fmt.Sprintf("Creating filesystem file contexts for %s", f.BaseModuleName()))
160 return fcBin.OutputPath
161}
162
Jiyong Park72678312021-01-18 17:29:49 +0900163func (f *filesystem) buildPropFile(ctx android.ModuleContext) (propFile android.OutputPath, toolDeps android.Paths) {
164 type prop struct {
165 name string
166 value string
167 }
168
169 var props []prop
170 var deps android.Paths
171 addStr := func(name string, value string) {
172 props = append(props, prop{name, value})
173 }
174 addPath := func(name string, path android.Path) {
175 props = append(props, prop{name, path.String()})
176 deps = append(deps, path)
177 }
178
Jiyong Park11a65972021-02-01 21:09:38 +0900179 // Type string that build_image.py accepts.
180 fsTypeStr := func(t fsType) string {
181 switch t {
182 // TODO(jiyong): add more types like f2fs, erofs, etc.
183 case ext4Type:
184 return "ext4"
185 }
186 panic(fmt.Errorf("unsupported fs type %v", t))
187 }
188
189 addStr("fs_type", fsTypeStr(f.fsType(ctx)))
Jiyong Park72678312021-01-18 17:29:49 +0900190 addStr("mount_point", "system")
191 addStr("use_dynamic_partition_size", "true")
192 addPath("ext_mkuserimg", ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs"))
193 // b/177813163 deps of the host tools have to be added. Remove this.
194 for _, t := range []string{"mke2fs", "e2fsdroid", "tune2fs"} {
195 deps = append(deps, ctx.Config().HostToolPath(ctx, t))
196 }
197
Jiyong Park71baa762021-01-18 21:11:03 +0900198 if proptools.Bool(f.properties.Use_avb) {
199 addStr("avb_hashtree_enable", "true")
200 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
201 algorithm := proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096")
202 addStr("avb_algorithm", algorithm)
203 key := android.PathForModuleSrc(ctx, proptools.String(f.properties.Avb_private_key))
204 addPath("avb_key_path", key)
205 addStr("avb_add_hashtree_footer_args", "--do_not_generate_fec")
206 addStr("partition_name", f.Name())
207 }
208
Inseob Kimcc8e5362021-02-03 14:05:24 +0900209 if proptools.String(f.properties.File_contexts) != "" {
210 addPath("selinux_fc", f.buildFileContexts(ctx))
211 }
212
Jiyong Park72678312021-01-18 17:29:49 +0900213 propFile = android.PathForModuleOut(ctx, "prop").OutputPath
214 builder := android.NewRuleBuilder(pctx, ctx)
215 builder.Command().Text("rm").Flag("-rf").Output(propFile)
216 for _, p := range props {
217 builder.Command().
Jiyong Park3db465d2021-01-26 14:08:16 +0900218 Text("echo").
Jiyong Park72678312021-01-18 17:29:49 +0900219 Flag(`"` + p.name + "=" + p.value + `"`).
220 Text(">>").Output(propFile)
221 }
222 builder.Build("build_filesystem_prop", fmt.Sprintf("Creating filesystem props for %s", f.BaseModuleName()))
223 return propFile, deps
224}
225
Jiyong Park837cdb22021-02-05 00:17:14 +0900226func (f *filesystem) buildCpioImage(ctx android.ModuleContext, compressed bool) android.OutputPath {
Jiyong Park11a65972021-02-01 21:09:38 +0900227 if proptools.Bool(f.properties.Use_avb) {
228 ctx.PropertyErrorf("use_avb", "signing compresed cpio image using avbtool is not supported."+
229 "Consider adding this to bootimg module and signing the entire boot image.")
230 }
231
Inseob Kimcc8e5362021-02-03 14:05:24 +0900232 if proptools.String(f.properties.File_contexts) != "" {
233 ctx.PropertyErrorf("file_contexts", "file_contexts is not supported for compressed cpio image.")
234 }
235
Jiyong Park11a65972021-02-01 21:09:38 +0900236 zipFile := android.PathForModuleOut(ctx, "temp.zip").OutputPath
237 f.CopyDepsToZip(ctx, zipFile)
238
239 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
240 builder := android.NewRuleBuilder(pctx, ctx)
241 builder.Command().
242 BuiltTool("zipsync").
243 FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear.
244 Input(zipFile)
245
246 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Jiyong Park837cdb22021-02-05 00:17:14 +0900247 cmd := builder.Command().
Jiyong Park11a65972021-02-01 21:09:38 +0900248 BuiltTool("mkbootfs").
Jiyong Park837cdb22021-02-05 00:17:14 +0900249 Text(rootDir.String()) // input directory
250 if compressed {
251 cmd.Text("|").
252 BuiltTool("lz4").
253 Flag("--favor-decSpeed"). // for faster boot
254 Flag("-12"). // maximum compression level
255 Flag("-l"). // legacy format for kernel
256 Text(">").Output(output)
257 } else {
258 cmd.Text(">").Output(output)
259 }
Jiyong Park11a65972021-02-01 21:09:38 +0900260
261 // rootDir is not deleted. Might be useful for quick inspection.
Jiyong Park837cdb22021-02-05 00:17:14 +0900262 builder.Build("build_cpio_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park11a65972021-02-01 21:09:38 +0900263
264 return output
265}
266
Jiyong Park65c49f52020-11-24 14:23:26 +0900267var _ android.AndroidMkEntriesProvider = (*filesystem)(nil)
268
269// Implements android.AndroidMkEntriesProvider
270func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries {
271 return []android.AndroidMkEntries{android.AndroidMkEntries{
272 Class: "ETC",
273 OutputFile: android.OptionalPathForPath(f.output),
274 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
275 func(entries *android.AndroidMkEntries) {
276 entries.SetString("LOCAL_MODULE_PATH", f.installDir.ToMakePath().String())
277 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
278 },
279 },
280 }}
Jiyong Park6f0f6882020-11-12 13:14:30 +0900281}
Jiyong Park12a719c2021-01-07 15:31:24 +0900282
Jiyong Park940dfd42021-02-04 15:37:34 +0900283var _ android.OutputFileProducer = (*filesystem)(nil)
284
285// Implements android.OutputFileProducer
286func (f *filesystem) OutputFiles(tag string) (android.Paths, error) {
287 if tag == "" {
288 return []android.Path{f.output}, nil
289 }
290 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
291}
292
Jiyong Park12a719c2021-01-07 15:31:24 +0900293// Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex
294// package to have access to the output file.
295type Filesystem interface {
296 android.Module
297 OutputPath() android.Path
298}
299
300var _ Filesystem = (*filesystem)(nil)
301
302func (f *filesystem) OutputPath() android.Path {
303 return f.output
304}