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 |
Inseob Kim | cc8e536 | 2021-02-03 14:05:24 +0900 | [diff] [blame^] | 54 | |
| 55 | // file_contexts file to make image. Currently, only ext4 is supported. |
| 56 | File_contexts *string `android:"path"` |
Jiyong Park | 71baa76 | 2021-01-18 21:11:03 +0900 | [diff] [blame] | 57 | } |
| 58 | |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 59 | // 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 Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 64 | func filesystemFactory() android.Module { |
| 65 | module := &filesystem{} |
Jiyong Park | 71baa76 | 2021-01-18 21:11:03 +0900 | [diff] [blame] | 66 | module.AddProperties(&module.properties) |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 67 | android.InitPackageModule(module) |
| 68 | android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) |
| 69 | return module |
| 70 | } |
| 71 | |
Jiyong Park | 12a719c | 2021-01-07 15:31:24 +0900 | [diff] [blame] | 72 | var dependencyTag = struct { |
| 73 | blueprint.BaseDependencyTag |
| 74 | android.InstallAlwaysNeededDependencyTag |
| 75 | }{} |
Jiyong Park | 65b6224 | 2020-11-25 12:44:59 +0900 | [diff] [blame] | 76 | |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 77 | func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) { |
Jiyong Park | 65b6224 | 2020-11-25 12:44:59 +0900 | [diff] [blame] | 78 | f.AddDeps(ctx, dependencyTag) |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 79 | } |
| 80 | |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 81 | type fsType int |
| 82 | |
| 83 | const ( |
| 84 | ext4Type fsType = iota |
| 85 | compressedCpioType |
| 86 | unknown |
| 87 | ) |
| 88 | |
| 89 | func (f *filesystem) fsType(ctx android.ModuleContext) fsType { |
| 90 | typeStr := proptools.StringDefault(f.properties.Type, "ext4") |
| 91 | switch typeStr { |
| 92 | case "ext4": |
| 93 | return ext4Type |
| 94 | case "compressed_cpio": |
| 95 | return compressedCpioType |
| 96 | default: |
| 97 | ctx.PropertyErrorf("type", "%q not supported", typeStr) |
| 98 | return unknown |
| 99 | } |
| 100 | } |
| 101 | |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 102 | func (f *filesystem) installFileName() string { |
| 103 | return f.BaseModuleName() + ".img" |
| 104 | } |
| 105 | |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 106 | var pctx = android.NewPackageContext("android/soong/filesystem") |
| 107 | |
| 108 | func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 109 | switch f.fsType(ctx) { |
| 110 | case ext4Type: |
| 111 | f.output = f.buildImageUsingBuildImage(ctx) |
| 112 | case compressedCpioType: |
| 113 | f.output = f.buildCompressedCpioImage(ctx) |
| 114 | default: |
| 115 | return |
| 116 | } |
| 117 | |
| 118 | f.installDir = android.PathForModuleInstall(ctx, "etc") |
| 119 | ctx.InstallFile(f.installDir, f.installFileName(), f.output) |
| 120 | } |
| 121 | |
| 122 | func (f *filesystem) buildImageUsingBuildImage(ctx android.ModuleContext) android.OutputPath { |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 123 | zipFile := android.PathForModuleOut(ctx, "temp.zip").OutputPath |
| 124 | f.CopyDepsToZip(ctx, zipFile) |
| 125 | |
| 126 | rootDir := android.PathForModuleOut(ctx, "root").OutputPath |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 127 | builder := android.NewRuleBuilder(pctx, ctx) |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 128 | builder.Command(). |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 129 | BuiltTool("zipsync"). |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 130 | FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear. |
| 131 | Input(zipFile) |
| 132 | |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 133 | propFile, toolDeps := f.buildPropFile(ctx) |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 134 | output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 135 | builder.Command().BuiltTool("build_image"). |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 136 | Text(rootDir.String()). // input directory |
| 137 | Input(propFile). |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 138 | Implicits(toolDeps). |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 139 | Output(output). |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 140 | Text(rootDir.String()) // directory where to find fs_config_files|dirs |
| 141 | |
| 142 | // rootDir is not deleted. Might be useful for quick inspection. |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 143 | builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName())) |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 144 | |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 145 | return output |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 146 | } |
| 147 | |
Inseob Kim | cc8e536 | 2021-02-03 14:05:24 +0900 | [diff] [blame^] | 148 | func (f *filesystem) buildFileContexts(ctx android.ModuleContext) android.OutputPath { |
| 149 | builder := android.NewRuleBuilder(pctx, ctx) |
| 150 | fcBin := android.PathForModuleOut(ctx, "file_contexts.bin") |
| 151 | builder.Command().BuiltTool("sefcontext_compile"). |
| 152 | FlagWithOutput("-o ", fcBin). |
| 153 | Input(android.PathForModuleSrc(ctx, proptools.String(f.properties.File_contexts))) |
| 154 | builder.Build("build_filesystem_file_contexts", fmt.Sprintf("Creating filesystem file contexts for %s", f.BaseModuleName())) |
| 155 | return fcBin.OutputPath |
| 156 | } |
| 157 | |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 158 | func (f *filesystem) buildPropFile(ctx android.ModuleContext) (propFile android.OutputPath, toolDeps android.Paths) { |
| 159 | type prop struct { |
| 160 | name string |
| 161 | value string |
| 162 | } |
| 163 | |
| 164 | var props []prop |
| 165 | var deps android.Paths |
| 166 | addStr := func(name string, value string) { |
| 167 | props = append(props, prop{name, value}) |
| 168 | } |
| 169 | addPath := func(name string, path android.Path) { |
| 170 | props = append(props, prop{name, path.String()}) |
| 171 | deps = append(deps, path) |
| 172 | } |
| 173 | |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 174 | // Type string that build_image.py accepts. |
| 175 | fsTypeStr := func(t fsType) string { |
| 176 | switch t { |
| 177 | // TODO(jiyong): add more types like f2fs, erofs, etc. |
| 178 | case ext4Type: |
| 179 | return "ext4" |
| 180 | } |
| 181 | panic(fmt.Errorf("unsupported fs type %v", t)) |
| 182 | } |
| 183 | |
| 184 | addStr("fs_type", fsTypeStr(f.fsType(ctx))) |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 185 | addStr("mount_point", "system") |
| 186 | addStr("use_dynamic_partition_size", "true") |
| 187 | addPath("ext_mkuserimg", ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs")) |
| 188 | // b/177813163 deps of the host tools have to be added. Remove this. |
| 189 | for _, t := range []string{"mke2fs", "e2fsdroid", "tune2fs"} { |
| 190 | deps = append(deps, ctx.Config().HostToolPath(ctx, t)) |
| 191 | } |
| 192 | |
Jiyong Park | 71baa76 | 2021-01-18 21:11:03 +0900 | [diff] [blame] | 193 | if proptools.Bool(f.properties.Use_avb) { |
| 194 | addStr("avb_hashtree_enable", "true") |
| 195 | addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool")) |
| 196 | algorithm := proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096") |
| 197 | addStr("avb_algorithm", algorithm) |
| 198 | key := android.PathForModuleSrc(ctx, proptools.String(f.properties.Avb_private_key)) |
| 199 | addPath("avb_key_path", key) |
| 200 | addStr("avb_add_hashtree_footer_args", "--do_not_generate_fec") |
| 201 | addStr("partition_name", f.Name()) |
| 202 | } |
| 203 | |
Inseob Kim | cc8e536 | 2021-02-03 14:05:24 +0900 | [diff] [blame^] | 204 | if proptools.String(f.properties.File_contexts) != "" { |
| 205 | addPath("selinux_fc", f.buildFileContexts(ctx)) |
| 206 | } |
| 207 | |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 208 | propFile = android.PathForModuleOut(ctx, "prop").OutputPath |
| 209 | builder := android.NewRuleBuilder(pctx, ctx) |
| 210 | builder.Command().Text("rm").Flag("-rf").Output(propFile) |
| 211 | for _, p := range props { |
| 212 | builder.Command(). |
Jiyong Park | 3db465d | 2021-01-26 14:08:16 +0900 | [diff] [blame] | 213 | Text("echo"). |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 214 | Flag(`"` + p.name + "=" + p.value + `"`). |
| 215 | Text(">>").Output(propFile) |
| 216 | } |
| 217 | builder.Build("build_filesystem_prop", fmt.Sprintf("Creating filesystem props for %s", f.BaseModuleName())) |
| 218 | return propFile, deps |
| 219 | } |
| 220 | |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 221 | func (f *filesystem) buildCompressedCpioImage(ctx android.ModuleContext) android.OutputPath { |
| 222 | if proptools.Bool(f.properties.Use_avb) { |
| 223 | ctx.PropertyErrorf("use_avb", "signing compresed cpio image using avbtool is not supported."+ |
| 224 | "Consider adding this to bootimg module and signing the entire boot image.") |
| 225 | } |
| 226 | |
Inseob Kim | cc8e536 | 2021-02-03 14:05:24 +0900 | [diff] [blame^] | 227 | if proptools.String(f.properties.File_contexts) != "" { |
| 228 | ctx.PropertyErrorf("file_contexts", "file_contexts is not supported for compressed cpio image.") |
| 229 | } |
| 230 | |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 231 | zipFile := android.PathForModuleOut(ctx, "temp.zip").OutputPath |
| 232 | f.CopyDepsToZip(ctx, zipFile) |
| 233 | |
| 234 | rootDir := android.PathForModuleOut(ctx, "root").OutputPath |
| 235 | builder := android.NewRuleBuilder(pctx, ctx) |
| 236 | builder.Command(). |
| 237 | BuiltTool("zipsync"). |
| 238 | FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear. |
| 239 | Input(zipFile) |
| 240 | |
| 241 | output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath |
| 242 | builder.Command(). |
| 243 | BuiltTool("mkbootfs"). |
| 244 | Text(rootDir.String()). // input directory |
| 245 | Text("|"). |
| 246 | BuiltTool("lz4"). |
| 247 | Flag("--favor-decSpeed"). // for faster boot |
| 248 | Flag("-12"). // maximum compression level |
| 249 | Flag("-l"). // legacy format for kernel |
| 250 | Text(">").Output(output) |
| 251 | |
| 252 | // rootDir is not deleted. Might be useful for quick inspection. |
| 253 | builder.Build("build_compressed_cpio_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName())) |
| 254 | |
| 255 | return output |
| 256 | } |
| 257 | |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 258 | var _ android.AndroidMkEntriesProvider = (*filesystem)(nil) |
| 259 | |
| 260 | // Implements android.AndroidMkEntriesProvider |
| 261 | func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries { |
| 262 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
| 263 | Class: "ETC", |
| 264 | OutputFile: android.OptionalPathForPath(f.output), |
| 265 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
| 266 | func(entries *android.AndroidMkEntries) { |
| 267 | entries.SetString("LOCAL_MODULE_PATH", f.installDir.ToMakePath().String()) |
| 268 | entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName()) |
| 269 | }, |
| 270 | }, |
| 271 | }} |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 272 | } |
Jiyong Park | 12a719c | 2021-01-07 15:31:24 +0900 | [diff] [blame] | 273 | |
| 274 | // Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex |
| 275 | // package to have access to the output file. |
| 276 | type Filesystem interface { |
| 277 | android.Module |
| 278 | OutputPath() android.Path |
| 279 | } |
| 280 | |
| 281 | var _ Filesystem = (*filesystem)(nil) |
| 282 | |
| 283 | func (f *filesystem) OutputPath() android.Path { |
| 284 | return f.output |
| 285 | } |