blob: 654617c2d510ad596aeee424fb21ecf978cb6acf [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
50}
51
Jiyong Park65c49f52020-11-24 14:23:26 +090052// android_filesystem packages a set of modules and their transitive dependencies into a filesystem
53// image. The filesystem images are expected to be mounted in the target device, which means the
54// modules in the filesystem image are built for the target device (i.e. Android, not Linux host).
55// The modules are placed in the filesystem image just like they are installed to the ordinary
56// partitions like system.img. For example, cc_library modules are placed under ./lib[64] directory.
Jiyong Park6f0f6882020-11-12 13:14:30 +090057func filesystemFactory() android.Module {
58 module := &filesystem{}
Jiyong Park71baa762021-01-18 21:11:03 +090059 module.AddProperties(&module.properties)
Jiyong Park6f0f6882020-11-12 13:14:30 +090060 android.InitPackageModule(module)
61 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
62 return module
63}
64
Jiyong Park12a719c2021-01-07 15:31:24 +090065var dependencyTag = struct {
66 blueprint.BaseDependencyTag
67 android.InstallAlwaysNeededDependencyTag
68}{}
Jiyong Park65b62242020-11-25 12:44:59 +090069
Jiyong Park6f0f6882020-11-12 13:14:30 +090070func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park65b62242020-11-25 12:44:59 +090071 f.AddDeps(ctx, dependencyTag)
Jiyong Park6f0f6882020-11-12 13:14:30 +090072}
73
Jiyong Park65c49f52020-11-24 14:23:26 +090074func (f *filesystem) installFileName() string {
75 return f.BaseModuleName() + ".img"
76}
77
Jiyong Park6f0f6882020-11-12 13:14:30 +090078var pctx = android.NewPackageContext("android/soong/filesystem")
79
80func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) {
81 zipFile := android.PathForModuleOut(ctx, "temp.zip").OutputPath
82 f.CopyDepsToZip(ctx, zipFile)
83
84 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -080085 builder := android.NewRuleBuilder(pctx, ctx)
Jiyong Park6f0f6882020-11-12 13:14:30 +090086 builder.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -080087 BuiltTool("zipsync").
Jiyong Park6f0f6882020-11-12 13:14:30 +090088 FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear.
89 Input(zipFile)
90
Jiyong Park72678312021-01-18 17:29:49 +090091 propFile, toolDeps := f.buildPropFile(ctx)
Jiyong Park12a719c2021-01-07 15:31:24 +090092 f.output = android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -080093 builder.Command().BuiltTool("build_image").
Jiyong Park6f0f6882020-11-12 13:14:30 +090094 Text(rootDir.String()). // input directory
95 Input(propFile).
Jiyong Park72678312021-01-18 17:29:49 +090096 Implicits(toolDeps).
Jiyong Park65c49f52020-11-24 14:23:26 +090097 Output(f.output).
Jiyong Park6f0f6882020-11-12 13:14:30 +090098 Text(rootDir.String()) // directory where to find fs_config_files|dirs
99
100 // rootDir is not deleted. Might be useful for quick inspection.
Colin Crossf1a035e2020-11-16 17:32:30 -0800101 builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park65c49f52020-11-24 14:23:26 +0900102
103 f.installDir = android.PathForModuleInstall(ctx, "etc")
104 ctx.InstallFile(f.installDir, f.installFileName(), f.output)
105}
106
Jiyong Park72678312021-01-18 17:29:49 +0900107func (f *filesystem) buildPropFile(ctx android.ModuleContext) (propFile android.OutputPath, toolDeps android.Paths) {
108 type prop struct {
109 name string
110 value string
111 }
112
113 var props []prop
114 var deps android.Paths
115 addStr := func(name string, value string) {
116 props = append(props, prop{name, value})
117 }
118 addPath := func(name string, path android.Path) {
119 props = append(props, prop{name, path.String()})
120 deps = append(deps, path)
121 }
122
123 // TODO(jiyong): support more filesystem types other than ext4
124 addStr("fs_type", "ext4")
125 addStr("mount_point", "system")
126 addStr("use_dynamic_partition_size", "true")
127 addPath("ext_mkuserimg", ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs"))
128 // b/177813163 deps of the host tools have to be added. Remove this.
129 for _, t := range []string{"mke2fs", "e2fsdroid", "tune2fs"} {
130 deps = append(deps, ctx.Config().HostToolPath(ctx, t))
131 }
132
Jiyong Park71baa762021-01-18 21:11:03 +0900133 if proptools.Bool(f.properties.Use_avb) {
134 addStr("avb_hashtree_enable", "true")
135 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
136 algorithm := proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096")
137 addStr("avb_algorithm", algorithm)
138 key := android.PathForModuleSrc(ctx, proptools.String(f.properties.Avb_private_key))
139 addPath("avb_key_path", key)
140 addStr("avb_add_hashtree_footer_args", "--do_not_generate_fec")
141 addStr("partition_name", f.Name())
142 }
143
Jiyong Park72678312021-01-18 17:29:49 +0900144 propFile = android.PathForModuleOut(ctx, "prop").OutputPath
145 builder := android.NewRuleBuilder(pctx, ctx)
146 builder.Command().Text("rm").Flag("-rf").Output(propFile)
147 for _, p := range props {
148 builder.Command().
Jiyong Park3db465d2021-01-26 14:08:16 +0900149 Text("echo").
Jiyong Park72678312021-01-18 17:29:49 +0900150 Flag(`"` + p.name + "=" + p.value + `"`).
151 Text(">>").Output(propFile)
152 }
153 builder.Build("build_filesystem_prop", fmt.Sprintf("Creating filesystem props for %s", f.BaseModuleName()))
154 return propFile, deps
155}
156
Jiyong Park65c49f52020-11-24 14:23:26 +0900157var _ android.AndroidMkEntriesProvider = (*filesystem)(nil)
158
159// Implements android.AndroidMkEntriesProvider
160func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries {
161 return []android.AndroidMkEntries{android.AndroidMkEntries{
162 Class: "ETC",
163 OutputFile: android.OptionalPathForPath(f.output),
164 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
165 func(entries *android.AndroidMkEntries) {
166 entries.SetString("LOCAL_MODULE_PATH", f.installDir.ToMakePath().String())
167 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
168 },
169 },
170 }}
Jiyong Park6f0f6882020-11-12 13:14:30 +0900171}
Jiyong Park12a719c2021-01-07 15:31:24 +0900172
173// Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex
174// package to have access to the output file.
175type Filesystem interface {
176 android.Module
177 OutputPath() android.Path
178}
179
180var _ Filesystem = (*filesystem)(nil)
181
182func (f *filesystem) OutputPath() android.Path {
183 return f.output
184}