blob: 5092ad06e3a4fec430e6ac114f67b429bc0fca7d [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
Jiyong Park71baa762021-01-18 21:11:03 +090054}
55
Jiyong Park65c49f52020-11-24 14:23:26 +090056// android_filesystem packages a set of modules and their transitive dependencies into a filesystem
57// image. The filesystem images are expected to be mounted in the target device, which means the
58// modules in the filesystem image are built for the target device (i.e. Android, not Linux host).
59// The modules are placed in the filesystem image just like they are installed to the ordinary
60// partitions like system.img. For example, cc_library modules are placed under ./lib[64] directory.
Jiyong Park6f0f6882020-11-12 13:14:30 +090061func filesystemFactory() android.Module {
62 module := &filesystem{}
Jiyong Park71baa762021-01-18 21:11:03 +090063 module.AddProperties(&module.properties)
Jiyong Park6f0f6882020-11-12 13:14:30 +090064 android.InitPackageModule(module)
65 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
66 return module
67}
68
Jiyong Park12a719c2021-01-07 15:31:24 +090069var dependencyTag = struct {
70 blueprint.BaseDependencyTag
71 android.InstallAlwaysNeededDependencyTag
72}{}
Jiyong Park65b62242020-11-25 12:44:59 +090073
Jiyong Park6f0f6882020-11-12 13:14:30 +090074func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park65b62242020-11-25 12:44:59 +090075 f.AddDeps(ctx, dependencyTag)
Jiyong Park6f0f6882020-11-12 13:14:30 +090076}
77
Jiyong Park11a65972021-02-01 21:09:38 +090078type fsType int
79
80const (
81 ext4Type fsType = iota
82 compressedCpioType
83 unknown
84)
85
86func (f *filesystem) fsType(ctx android.ModuleContext) fsType {
87 typeStr := proptools.StringDefault(f.properties.Type, "ext4")
88 switch typeStr {
89 case "ext4":
90 return ext4Type
91 case "compressed_cpio":
92 return compressedCpioType
93 default:
94 ctx.PropertyErrorf("type", "%q not supported", typeStr)
95 return unknown
96 }
97}
98
Jiyong Park65c49f52020-11-24 14:23:26 +090099func (f *filesystem) installFileName() string {
100 return f.BaseModuleName() + ".img"
101}
102
Jiyong Park6f0f6882020-11-12 13:14:30 +0900103var pctx = android.NewPackageContext("android/soong/filesystem")
104
105func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park11a65972021-02-01 21:09:38 +0900106 switch f.fsType(ctx) {
107 case ext4Type:
108 f.output = f.buildImageUsingBuildImage(ctx)
109 case compressedCpioType:
110 f.output = f.buildCompressedCpioImage(ctx)
111 default:
112 return
113 }
114
115 f.installDir = android.PathForModuleInstall(ctx, "etc")
116 ctx.InstallFile(f.installDir, f.installFileName(), f.output)
117}
118
119func (f *filesystem) buildImageUsingBuildImage(ctx android.ModuleContext) android.OutputPath {
Jiyong Park6f0f6882020-11-12 13:14:30 +0900120 zipFile := android.PathForModuleOut(ctx, "temp.zip").OutputPath
121 f.CopyDepsToZip(ctx, zipFile)
122
123 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -0800124 builder := android.NewRuleBuilder(pctx, ctx)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900125 builder.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800126 BuiltTool("zipsync").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900127 FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear.
128 Input(zipFile)
129
Jiyong Park72678312021-01-18 17:29:49 +0900130 propFile, toolDeps := f.buildPropFile(ctx)
Jiyong Park11a65972021-02-01 21:09:38 +0900131 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -0800132 builder.Command().BuiltTool("build_image").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900133 Text(rootDir.String()). // input directory
134 Input(propFile).
Jiyong Park72678312021-01-18 17:29:49 +0900135 Implicits(toolDeps).
Jiyong Park11a65972021-02-01 21:09:38 +0900136 Output(output).
Jiyong Park6f0f6882020-11-12 13:14:30 +0900137 Text(rootDir.String()) // directory where to find fs_config_files|dirs
138
139 // rootDir is not deleted. Might be useful for quick inspection.
Colin Crossf1a035e2020-11-16 17:32:30 -0800140 builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park65c49f52020-11-24 14:23:26 +0900141
Jiyong Park11a65972021-02-01 21:09:38 +0900142 return output
Jiyong Park65c49f52020-11-24 14:23:26 +0900143}
144
Jiyong Park72678312021-01-18 17:29:49 +0900145func (f *filesystem) buildPropFile(ctx android.ModuleContext) (propFile android.OutputPath, toolDeps android.Paths) {
146 type prop struct {
147 name string
148 value string
149 }
150
151 var props []prop
152 var deps android.Paths
153 addStr := func(name string, value string) {
154 props = append(props, prop{name, value})
155 }
156 addPath := func(name string, path android.Path) {
157 props = append(props, prop{name, path.String()})
158 deps = append(deps, path)
159 }
160
Jiyong Park11a65972021-02-01 21:09:38 +0900161 // Type string that build_image.py accepts.
162 fsTypeStr := func(t fsType) string {
163 switch t {
164 // TODO(jiyong): add more types like f2fs, erofs, etc.
165 case ext4Type:
166 return "ext4"
167 }
168 panic(fmt.Errorf("unsupported fs type %v", t))
169 }
170
171 addStr("fs_type", fsTypeStr(f.fsType(ctx)))
Jiyong Park72678312021-01-18 17:29:49 +0900172 addStr("mount_point", "system")
173 addStr("use_dynamic_partition_size", "true")
174 addPath("ext_mkuserimg", ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs"))
175 // b/177813163 deps of the host tools have to be added. Remove this.
176 for _, t := range []string{"mke2fs", "e2fsdroid", "tune2fs"} {
177 deps = append(deps, ctx.Config().HostToolPath(ctx, t))
178 }
179
Jiyong Park71baa762021-01-18 21:11:03 +0900180 if proptools.Bool(f.properties.Use_avb) {
181 addStr("avb_hashtree_enable", "true")
182 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
183 algorithm := proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096")
184 addStr("avb_algorithm", algorithm)
185 key := android.PathForModuleSrc(ctx, proptools.String(f.properties.Avb_private_key))
186 addPath("avb_key_path", key)
187 addStr("avb_add_hashtree_footer_args", "--do_not_generate_fec")
188 addStr("partition_name", f.Name())
189 }
190
Jiyong Park72678312021-01-18 17:29:49 +0900191 propFile = android.PathForModuleOut(ctx, "prop").OutputPath
192 builder := android.NewRuleBuilder(pctx, ctx)
193 builder.Command().Text("rm").Flag("-rf").Output(propFile)
194 for _, p := range props {
195 builder.Command().
Jiyong Park3db465d2021-01-26 14:08:16 +0900196 Text("echo").
Jiyong Park72678312021-01-18 17:29:49 +0900197 Flag(`"` + p.name + "=" + p.value + `"`).
198 Text(">>").Output(propFile)
199 }
200 builder.Build("build_filesystem_prop", fmt.Sprintf("Creating filesystem props for %s", f.BaseModuleName()))
201 return propFile, deps
202}
203
Jiyong Park11a65972021-02-01 21:09:38 +0900204func (f *filesystem) buildCompressedCpioImage(ctx android.ModuleContext) android.OutputPath {
205 if proptools.Bool(f.properties.Use_avb) {
206 ctx.PropertyErrorf("use_avb", "signing compresed cpio image using avbtool is not supported."+
207 "Consider adding this to bootimg module and signing the entire boot image.")
208 }
209
210 zipFile := android.PathForModuleOut(ctx, "temp.zip").OutputPath
211 f.CopyDepsToZip(ctx, zipFile)
212
213 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
214 builder := android.NewRuleBuilder(pctx, ctx)
215 builder.Command().
216 BuiltTool("zipsync").
217 FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear.
218 Input(zipFile)
219
220 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
221 builder.Command().
222 BuiltTool("mkbootfs").
223 Text(rootDir.String()). // input directory
224 Text("|").
225 BuiltTool("lz4").
226 Flag("--favor-decSpeed"). // for faster boot
227 Flag("-12"). // maximum compression level
228 Flag("-l"). // legacy format for kernel
229 Text(">").Output(output)
230
231 // rootDir is not deleted. Might be useful for quick inspection.
232 builder.Build("build_compressed_cpio_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
233
234 return output
235}
236
Jiyong Park65c49f52020-11-24 14:23:26 +0900237var _ android.AndroidMkEntriesProvider = (*filesystem)(nil)
238
239// Implements android.AndroidMkEntriesProvider
240func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries {
241 return []android.AndroidMkEntries{android.AndroidMkEntries{
242 Class: "ETC",
243 OutputFile: android.OptionalPathForPath(f.output),
244 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
245 func(entries *android.AndroidMkEntries) {
246 entries.SetString("LOCAL_MODULE_PATH", f.installDir.ToMakePath().String())
247 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
248 },
249 },
250 }}
Jiyong Park6f0f6882020-11-12 13:14:30 +0900251}
Jiyong Park12a719c2021-01-07 15:31:24 +0900252
253// Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex
254// package to have access to the output file.
255type Filesystem interface {
256 android.Module
257 OutputPath() android.Path
258}
259
260var _ Filesystem = (*filesystem)(nil)
261
262func (f *filesystem) OutputPath() android.Path {
263 return f.output
264}