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 |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame^] | 50 | |
| 51 | // Type of the filesystem. Currently, ext4 and compressed_cpio are supported. Default is |
| 52 | // ext4. |
| 53 | Type *string |
Jiyong Park | 71baa76 | 2021-01-18 21:11:03 +0900 | [diff] [blame] | 54 | } |
| 55 | |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 56 | // 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 Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 61 | func filesystemFactory() android.Module { |
| 62 | module := &filesystem{} |
Jiyong Park | 71baa76 | 2021-01-18 21:11:03 +0900 | [diff] [blame] | 63 | module.AddProperties(&module.properties) |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 64 | android.InitPackageModule(module) |
| 65 | android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) |
| 66 | return module |
| 67 | } |
| 68 | |
Jiyong Park | 12a719c | 2021-01-07 15:31:24 +0900 | [diff] [blame] | 69 | var dependencyTag = struct { |
| 70 | blueprint.BaseDependencyTag |
| 71 | android.InstallAlwaysNeededDependencyTag |
| 72 | }{} |
Jiyong Park | 65b6224 | 2020-11-25 12:44:59 +0900 | [diff] [blame] | 73 | |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 74 | func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) { |
Jiyong Park | 65b6224 | 2020-11-25 12:44:59 +0900 | [diff] [blame] | 75 | f.AddDeps(ctx, dependencyTag) |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 76 | } |
| 77 | |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame^] | 78 | type fsType int |
| 79 | |
| 80 | const ( |
| 81 | ext4Type fsType = iota |
| 82 | compressedCpioType |
| 83 | unknown |
| 84 | ) |
| 85 | |
| 86 | func (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 Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 99 | func (f *filesystem) installFileName() string { |
| 100 | return f.BaseModuleName() + ".img" |
| 101 | } |
| 102 | |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 103 | var pctx = android.NewPackageContext("android/soong/filesystem") |
| 104 | |
| 105 | func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame^] | 106 | 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 | |
| 119 | func (f *filesystem) buildImageUsingBuildImage(ctx android.ModuleContext) android.OutputPath { |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 120 | zipFile := android.PathForModuleOut(ctx, "temp.zip").OutputPath |
| 121 | f.CopyDepsToZip(ctx, zipFile) |
| 122 | |
| 123 | rootDir := android.PathForModuleOut(ctx, "root").OutputPath |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 124 | builder := android.NewRuleBuilder(pctx, ctx) |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 125 | builder.Command(). |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 126 | BuiltTool("zipsync"). |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 127 | FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear. |
| 128 | Input(zipFile) |
| 129 | |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 130 | propFile, toolDeps := f.buildPropFile(ctx) |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame^] | 131 | output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 132 | builder.Command().BuiltTool("build_image"). |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 133 | Text(rootDir.String()). // input directory |
| 134 | Input(propFile). |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 135 | Implicits(toolDeps). |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame^] | 136 | Output(output). |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 137 | Text(rootDir.String()) // directory where to find fs_config_files|dirs |
| 138 | |
| 139 | // rootDir is not deleted. Might be useful for quick inspection. |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 140 | builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName())) |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 141 | |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame^] | 142 | return output |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 143 | } |
| 144 | |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 145 | func (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 Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame^] | 161 | // 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 Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 172 | 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 Park | 71baa76 | 2021-01-18 21:11:03 +0900 | [diff] [blame] | 180 | 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 Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 191 | 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 Park | 3db465d | 2021-01-26 14:08:16 +0900 | [diff] [blame] | 196 | Text("echo"). |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 197 | 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 Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame^] | 204 | func (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 Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 237 | var _ android.AndroidMkEntriesProvider = (*filesystem)(nil) |
| 238 | |
| 239 | // Implements android.AndroidMkEntriesProvider |
| 240 | func (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 Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 251 | } |
Jiyong Park | 12a719c | 2021-01-07 15:31:24 +0900 | [diff] [blame] | 252 | |
| 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. |
| 255 | type Filesystem interface { |
| 256 | android.Module |
| 257 | OutputPath() android.Path |
| 258 | } |
| 259 | |
| 260 | var _ Filesystem = (*filesystem)(nil) |
| 261 | |
| 262 | func (f *filesystem) OutputPath() android.Path { |
| 263 | return f.output |
| 264 | } |