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