Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 1 | // 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 | |
| 15 | package filesystem |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | |
| 20 | "android/soong/android" |
Jiyong Park | 65b6224 | 2020-11-25 12:44:59 +0900 | [diff] [blame] | 21 | |
| 22 | "github.com/google/blueprint" |
Jiyong Park | 71baa76 | 2021-01-18 21:11:03 +0900 | [diff] [blame] | 23 | "github.com/google/blueprint/proptools" |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | func init() { |
| 27 | android.RegisterModuleType("android_filesystem", filesystemFactory) |
| 28 | } |
| 29 | |
| 30 | type filesystem struct { |
| 31 | android.ModuleBase |
| 32 | android.PackagingBase |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 33 | |
Jiyong Park | 71baa76 | 2021-01-18 21:11:03 +0900 | [diff] [blame] | 34 | properties filesystemProperties |
| 35 | |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 36 | output android.OutputPath |
| 37 | installDir android.InstallPath |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 38 | } |
| 39 | |
Jiyong Park | 71baa76 | 2021-01-18 21:11:03 +0900 | [diff] [blame] | 40 | type 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 Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 52 | // 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 Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 57 | func filesystemFactory() android.Module { |
| 58 | module := &filesystem{} |
Jiyong Park | 71baa76 | 2021-01-18 21:11:03 +0900 | [diff] [blame] | 59 | module.AddProperties(&module.properties) |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 60 | android.InitPackageModule(module) |
| 61 | android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) |
| 62 | return module |
| 63 | } |
| 64 | |
Jiyong Park | 12a719c | 2021-01-07 15:31:24 +0900 | [diff] [blame] | 65 | var dependencyTag = struct { |
| 66 | blueprint.BaseDependencyTag |
| 67 | android.InstallAlwaysNeededDependencyTag |
| 68 | }{} |
Jiyong Park | 65b6224 | 2020-11-25 12:44:59 +0900 | [diff] [blame] | 69 | |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 70 | func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) { |
Jiyong Park | 65b6224 | 2020-11-25 12:44:59 +0900 | [diff] [blame] | 71 | f.AddDeps(ctx, dependencyTag) |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 72 | } |
| 73 | |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 74 | func (f *filesystem) installFileName() string { |
| 75 | return f.BaseModuleName() + ".img" |
| 76 | } |
| 77 | |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 78 | var pctx = android.NewPackageContext("android/soong/filesystem") |
| 79 | |
| 80 | func (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 Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 85 | builder := android.NewRuleBuilder(pctx, ctx) |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 86 | builder.Command(). |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 87 | BuiltTool("zipsync"). |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 88 | FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear. |
| 89 | Input(zipFile) |
| 90 | |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 91 | propFile, toolDeps := f.buildPropFile(ctx) |
Jiyong Park | 12a719c | 2021-01-07 15:31:24 +0900 | [diff] [blame] | 92 | f.output = android.PathForModuleOut(ctx, f.installFileName()).OutputPath |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 93 | builder.Command().BuiltTool("build_image"). |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 94 | Text(rootDir.String()). // input directory |
| 95 | Input(propFile). |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 96 | Implicits(toolDeps). |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 97 | Output(f.output). |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 98 | Text(rootDir.String()) // directory where to find fs_config_files|dirs |
| 99 | |
| 100 | // rootDir is not deleted. Might be useful for quick inspection. |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 101 | builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName())) |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 102 | |
| 103 | f.installDir = android.PathForModuleInstall(ctx, "etc") |
| 104 | ctx.InstallFile(f.installDir, f.installFileName(), f.output) |
| 105 | } |
| 106 | |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 107 | func (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 Park | 71baa76 | 2021-01-18 21:11:03 +0900 | [diff] [blame] | 133 | 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 Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 144 | 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(). |
| 149 | Text("echo").Flag("-e"). |
| 150 | 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 Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 157 | var _ android.AndroidMkEntriesProvider = (*filesystem)(nil) |
| 158 | |
| 159 | // Implements android.AndroidMkEntriesProvider |
| 160 | func (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 Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 171 | } |
Jiyong Park | 12a719c | 2021-01-07 15:31:24 +0900 | [diff] [blame] | 172 | |
| 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. |
| 175 | type Filesystem interface { |
| 176 | android.Module |
| 177 | OutputPath() android.Path |
| 178 | } |
| 179 | |
| 180 | var _ Filesystem = (*filesystem)(nil) |
| 181 | |
| 182 | func (f *filesystem) OutputPath() android.Path { |
| 183 | return f.output |
| 184 | } |