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 ( |
Jooyung Han | 65f402b | 2022-04-21 14:24:04 +0900 | [diff] [blame] | 18 | "crypto/sha256" |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 19 | "fmt" |
Jooyung Han | 65f402b | 2022-04-21 14:24:04 +0900 | [diff] [blame] | 20 | "io" |
Inseob Kim | 14199b0 | 2021-02-09 21:18:31 +0900 | [diff] [blame] | 21 | "path/filepath" |
| 22 | "strings" |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 23 | |
| 24 | "android/soong/android" |
Jooyung Han | e606759 | 2023-03-16 13:11:17 +0900 | [diff] [blame] | 25 | "android/soong/cc" |
Jiyong Park | 65b6224 | 2020-11-25 12:44:59 +0900 | [diff] [blame] | 26 | |
| 27 | "github.com/google/blueprint" |
Jiyong Park | 71baa76 | 2021-01-18 21:11:03 +0900 | [diff] [blame] | 28 | "github.com/google/blueprint/proptools" |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | func init() { |
Jooyung Han | 9706cbc | 2021-04-15 22:43:48 +0900 | [diff] [blame] | 32 | registerBuildComponents(android.InitRegistrationContext) |
| 33 | } |
| 34 | |
| 35 | func registerBuildComponents(ctx android.RegistrationContext) { |
| 36 | ctx.RegisterModuleType("android_filesystem", filesystemFactory) |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 37 | ctx.RegisterModuleType("android_system_image", systemImageFactory) |
Jiyong Park | bc48548 | 2022-11-15 22:31:49 +0900 | [diff] [blame] | 38 | ctx.RegisterModuleType("avb_add_hash_footer", avbAddHashFooterFactory) |
Inseob Kim | 87230e6 | 2023-11-22 18:55:07 +0900 | [diff] [blame] | 39 | ctx.RegisterModuleType("avb_add_hash_footer_defaults", avbAddHashFooterDefaultsFactory) |
Alice Wang | 000e3a3 | 2023-01-03 16:11:20 +0000 | [diff] [blame] | 40 | ctx.RegisterModuleType("avb_gen_vbmeta_image", avbGenVbmetaImageFactory) |
Inseob Kim | 87230e6 | 2023-11-22 18:55:07 +0900 | [diff] [blame] | 41 | ctx.RegisterModuleType("avb_gen_vbmeta_image_defaults", avbGenVbmetaImageDefaultsFactory) |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | type filesystem struct { |
| 45 | android.ModuleBase |
| 46 | android.PackagingBase |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 47 | |
Jiyong Park | 71baa76 | 2021-01-18 21:11:03 +0900 | [diff] [blame] | 48 | properties filesystemProperties |
| 49 | |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 50 | // Function that builds extra files under the root directory and returns the files |
| 51 | buildExtraFiles func(ctx android.ModuleContext, root android.OutputPath) android.OutputPaths |
| 52 | |
Jooyung Han | 0fbbc2b | 2022-03-25 12:35:46 +0900 | [diff] [blame] | 53 | // Function that filters PackagingSpecs returned by PackagingBase.GatherPackagingSpecs() |
| 54 | filterPackagingSpecs func(specs map[string]android.PackagingSpec) |
| 55 | |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 56 | output android.OutputPath |
| 57 | installDir android.InstallPath |
Jooyung Han | 0fbbc2b | 2022-03-25 12:35:46 +0900 | [diff] [blame] | 58 | |
| 59 | // For testing. Keeps the result of CopyDepsToZip() |
| 60 | entries []string |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 61 | } |
| 62 | |
Inseob Kim | 14199b0 | 2021-02-09 21:18:31 +0900 | [diff] [blame] | 63 | type symlinkDefinition struct { |
| 64 | Target *string |
| 65 | Name *string |
| 66 | } |
| 67 | |
Jiyong Park | 71baa76 | 2021-01-18 21:11:03 +0900 | [diff] [blame] | 68 | type filesystemProperties struct { |
| 69 | // When set to true, sign the image with avbtool. Default is false. |
| 70 | Use_avb *bool |
| 71 | |
| 72 | // Path to the private key that avbtool will use to sign this filesystem image. |
| 73 | // TODO(jiyong): allow apex_key to be specified here |
| 74 | Avb_private_key *string `android:"path"` |
| 75 | |
Shikha Panwar | 01403bb | 2022-12-22 12:22:57 +0000 | [diff] [blame] | 76 | // Signing algorithm for avbtool. Default is SHA256_RSA4096. |
Jiyong Park | 71baa76 | 2021-01-18 21:11:03 +0900 | [diff] [blame] | 77 | Avb_algorithm *string |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 78 | |
Shikha Panwar | 01403bb | 2022-12-22 12:22:57 +0000 | [diff] [blame] | 79 | // Hash algorithm used for avbtool (for descriptors). This is passed as hash_algorithm to |
| 80 | // avbtool. Default used by avbtool is sha1. |
Shikha Panwar | e6f3063 | 2022-12-21 12:54:45 +0000 | [diff] [blame] | 81 | Avb_hash_algorithm *string |
| 82 | |
Jiyong Park | ac4076d | 2021-03-15 23:21:30 +0900 | [diff] [blame] | 83 | // Name of the partition stored in vbmeta desc. Defaults to the name of this module. |
| 84 | Partition_name *string |
| 85 | |
Jiyong Park | 837cdb2 | 2021-02-05 00:17:14 +0900 | [diff] [blame] | 86 | // Type of the filesystem. Currently, ext4, cpio, and compressed_cpio are supported. Default |
| 87 | // is ext4. |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 88 | Type *string |
Inseob Kim | cc8e536 | 2021-02-03 14:05:24 +0900 | [diff] [blame] | 89 | |
| 90 | // file_contexts file to make image. Currently, only ext4 is supported. |
| 91 | File_contexts *string `android:"path"` |
Inseob Kim | 2ce1b5d | 2021-02-15 17:01:04 +0900 | [diff] [blame] | 92 | |
| 93 | // Base directory relative to root, to which deps are installed, e.g. "system". Default is "." |
| 94 | // (root). |
| 95 | Base_dir *string |
Inseob Kim | 14199b0 | 2021-02-09 21:18:31 +0900 | [diff] [blame] | 96 | |
| 97 | // Directories to be created under root. e.g. /dev, /proc, etc. |
| 98 | Dirs []string |
| 99 | |
| 100 | // Symbolic links to be created under root with "ln -sf <target> <name>". |
| 101 | Symlinks []symlinkDefinition |
Jooyung Han | 65f402b | 2022-04-21 14:24:04 +0900 | [diff] [blame] | 102 | |
| 103 | // Seconds since unix epoch to override timestamps of file entries |
| 104 | Fake_timestamp *string |
| 105 | |
| 106 | // When set, passed to mkuserimg_mke2fs --mke2fs_uuid & --mke2fs_hash_seed. |
| 107 | // Otherwise, they'll be set as random which might cause indeterministic build output. |
| 108 | Uuid *string |
Inseob Kim | 376d72f | 2023-11-01 15:40:25 +0900 | [diff] [blame] | 109 | |
| 110 | // Mount point for this image. Default is "/" |
| 111 | Mount_point *string |
Jiyong Park | 71baa76 | 2021-01-18 21:11:03 +0900 | [diff] [blame] | 112 | } |
| 113 | |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 114 | // android_filesystem packages a set of modules and their transitive dependencies into a filesystem |
| 115 | // image. The filesystem images are expected to be mounted in the target device, which means the |
| 116 | // modules in the filesystem image are built for the target device (i.e. Android, not Linux host). |
| 117 | // The modules are placed in the filesystem image just like they are installed to the ordinary |
| 118 | // 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] | 119 | func filesystemFactory() android.Module { |
| 120 | module := &filesystem{} |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 121 | initFilesystemModule(module) |
| 122 | return module |
| 123 | } |
| 124 | |
| 125 | func initFilesystemModule(module *filesystem) { |
Jiyong Park | 71baa76 | 2021-01-18 21:11:03 +0900 | [diff] [blame] | 126 | module.AddProperties(&module.properties) |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 127 | android.InitPackageModule(module) |
| 128 | android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 129 | } |
| 130 | |
Jiyong Park | 12a719c | 2021-01-07 15:31:24 +0900 | [diff] [blame] | 131 | var dependencyTag = struct { |
| 132 | blueprint.BaseDependencyTag |
Jooyung Han | 092ef81 | 2021-03-10 15:40:34 +0900 | [diff] [blame] | 133 | android.PackagingItemAlwaysDepTag |
Jiyong Park | 12a719c | 2021-01-07 15:31:24 +0900 | [diff] [blame] | 134 | }{} |
Jiyong Park | 65b6224 | 2020-11-25 12:44:59 +0900 | [diff] [blame] | 135 | |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 136 | func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) { |
Jiyong Park | 65b6224 | 2020-11-25 12:44:59 +0900 | [diff] [blame] | 137 | f.AddDeps(ctx, dependencyTag) |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 138 | } |
| 139 | |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 140 | type fsType int |
| 141 | |
| 142 | const ( |
| 143 | ext4Type fsType = iota |
| 144 | compressedCpioType |
Jiyong Park | 837cdb2 | 2021-02-05 00:17:14 +0900 | [diff] [blame] | 145 | cpioType // uncompressed |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 146 | unknown |
| 147 | ) |
| 148 | |
| 149 | func (f *filesystem) fsType(ctx android.ModuleContext) fsType { |
| 150 | typeStr := proptools.StringDefault(f.properties.Type, "ext4") |
| 151 | switch typeStr { |
| 152 | case "ext4": |
| 153 | return ext4Type |
| 154 | case "compressed_cpio": |
| 155 | return compressedCpioType |
Jiyong Park | 837cdb2 | 2021-02-05 00:17:14 +0900 | [diff] [blame] | 156 | case "cpio": |
| 157 | return cpioType |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 158 | default: |
| 159 | ctx.PropertyErrorf("type", "%q not supported", typeStr) |
| 160 | return unknown |
| 161 | } |
| 162 | } |
| 163 | |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 164 | func (f *filesystem) installFileName() string { |
| 165 | return f.BaseModuleName() + ".img" |
| 166 | } |
| 167 | |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 168 | var pctx = android.NewPackageContext("android/soong/filesystem") |
| 169 | |
| 170 | func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 171 | switch f.fsType(ctx) { |
| 172 | case ext4Type: |
| 173 | f.output = f.buildImageUsingBuildImage(ctx) |
| 174 | case compressedCpioType: |
Jiyong Park | 837cdb2 | 2021-02-05 00:17:14 +0900 | [diff] [blame] | 175 | f.output = f.buildCpioImage(ctx, true) |
| 176 | case cpioType: |
| 177 | f.output = f.buildCpioImage(ctx, false) |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 178 | default: |
| 179 | return |
| 180 | } |
| 181 | |
| 182 | f.installDir = android.PathForModuleInstall(ctx, "etc") |
| 183 | ctx.InstallFile(f.installDir, f.installFileName(), f.output) |
| 184 | } |
| 185 | |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 186 | // root zip will contain extra files/dirs that are not from the `deps` property. |
Inseob Kim | 2ce1b5d | 2021-02-15 17:01:04 +0900 | [diff] [blame] | 187 | func (f *filesystem) buildRootZip(ctx android.ModuleContext) android.OutputPath { |
| 188 | rootDir := android.PathForModuleGen(ctx, "root").OutputPath |
| 189 | builder := android.NewRuleBuilder(pctx, ctx) |
| 190 | builder.Command().Text("rm -rf").Text(rootDir.String()) |
| 191 | builder.Command().Text("mkdir -p").Text(rootDir.String()) |
| 192 | |
Inseob Kim | 14199b0 | 2021-02-09 21:18:31 +0900 | [diff] [blame] | 193 | // create dirs and symlinks |
| 194 | for _, dir := range f.properties.Dirs { |
| 195 | // OutputPath.Join verifies dir |
| 196 | builder.Command().Text("mkdir -p").Text(rootDir.Join(ctx, dir).String()) |
| 197 | } |
| 198 | |
| 199 | for _, symlink := range f.properties.Symlinks { |
| 200 | name := strings.TrimSpace(proptools.String(symlink.Name)) |
| 201 | target := strings.TrimSpace(proptools.String(symlink.Target)) |
| 202 | |
| 203 | if name == "" { |
| 204 | ctx.PropertyErrorf("symlinks", "Name can't be empty") |
| 205 | continue |
| 206 | } |
| 207 | |
| 208 | if target == "" { |
| 209 | ctx.PropertyErrorf("symlinks", "Target can't be empty") |
| 210 | continue |
| 211 | } |
| 212 | |
| 213 | // OutputPath.Join verifies name. don't need to verify target. |
| 214 | dst := rootDir.Join(ctx, name) |
| 215 | |
| 216 | builder.Command().Text("mkdir -p").Text(filepath.Dir(dst.String())) |
| 217 | builder.Command().Text("ln -sf").Text(proptools.ShellEscape(target)).Text(dst.String()) |
| 218 | } |
Inseob Kim | 2ce1b5d | 2021-02-15 17:01:04 +0900 | [diff] [blame] | 219 | |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 220 | // create extra files if there's any |
| 221 | rootForExtraFiles := android.PathForModuleGen(ctx, "root-extra").OutputPath |
| 222 | var extraFiles android.OutputPaths |
| 223 | if f.buildExtraFiles != nil { |
| 224 | extraFiles = f.buildExtraFiles(ctx, rootForExtraFiles) |
| 225 | for _, f := range extraFiles { |
| 226 | rel, _ := filepath.Rel(rootForExtraFiles.String(), f.String()) |
| 227 | if strings.HasPrefix(rel, "..") { |
| 228 | panic(fmt.Errorf("%q is not under %q\n", f, rootForExtraFiles)) |
| 229 | } |
| 230 | } |
| 231 | } |
Inseob Kim | 2ce1b5d | 2021-02-15 17:01:04 +0900 | [diff] [blame] | 232 | |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 233 | // Zip them all |
| 234 | zipOut := android.PathForModuleGen(ctx, "root.zip").OutputPath |
| 235 | zipCommand := builder.Command().BuiltTool("soong_zip") |
| 236 | zipCommand.FlagWithOutput("-o ", zipOut). |
Inseob Kim | 2ce1b5d | 2021-02-15 17:01:04 +0900 | [diff] [blame] | 237 | FlagWithArg("-C ", rootDir.String()). |
| 238 | Flag("-L 0"). // no compression because this will be unzipped soon |
| 239 | FlagWithArg("-D ", rootDir.String()). |
| 240 | Flag("-d") // include empty directories |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 241 | if len(extraFiles) > 0 { |
| 242 | zipCommand.FlagWithArg("-C ", rootForExtraFiles.String()) |
| 243 | for _, f := range extraFiles { |
| 244 | zipCommand.FlagWithInput("-f ", f) |
| 245 | } |
| 246 | } |
| 247 | |
Inseob Kim | 2ce1b5d | 2021-02-15 17:01:04 +0900 | [diff] [blame] | 248 | builder.Command().Text("rm -rf").Text(rootDir.String()) |
| 249 | |
| 250 | builder.Build("zip_root", fmt.Sprintf("zipping root contents for %s", ctx.ModuleName())) |
| 251 | return zipOut |
| 252 | } |
| 253 | |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 254 | func (f *filesystem) buildImageUsingBuildImage(ctx android.ModuleContext) android.OutputPath { |
Inseob Kim | 2ce1b5d | 2021-02-15 17:01:04 +0900 | [diff] [blame] | 255 | depsZipFile := android.PathForModuleOut(ctx, "deps.zip").OutputPath |
Jooyung Han | 0fbbc2b | 2022-03-25 12:35:46 +0900 | [diff] [blame] | 256 | f.entries = f.CopyDepsToZip(ctx, f.gatherFilteredPackagingSpecs(ctx), depsZipFile) |
Inseob Kim | 2ce1b5d | 2021-02-15 17:01:04 +0900 | [diff] [blame] | 257 | |
| 258 | builder := android.NewRuleBuilder(pctx, ctx) |
| 259 | depsBase := proptools.StringDefault(f.properties.Base_dir, ".") |
| 260 | rebasedDepsZip := android.PathForModuleOut(ctx, "rebased_deps.zip").OutputPath |
| 261 | builder.Command(). |
| 262 | BuiltTool("zip2zip"). |
| 263 | FlagWithInput("-i ", depsZipFile). |
| 264 | FlagWithOutput("-o ", rebasedDepsZip). |
| 265 | Text("**/*:" + proptools.ShellEscape(depsBase)) // zip2zip verifies depsBase |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 266 | |
| 267 | rootDir := android.PathForModuleOut(ctx, "root").OutputPath |
Inseob Kim | 2ce1b5d | 2021-02-15 17:01:04 +0900 | [diff] [blame] | 268 | rootZip := f.buildRootZip(ctx) |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 269 | builder.Command(). |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 270 | BuiltTool("zipsync"). |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 271 | FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear. |
Inseob Kim | 2ce1b5d | 2021-02-15 17:01:04 +0900 | [diff] [blame] | 272 | Input(rootZip). |
| 273 | Input(rebasedDepsZip) |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 274 | |
Nikita Ioffe | 519015f | 2022-12-23 15:36:29 +0000 | [diff] [blame] | 275 | // run host_init_verifier |
| 276 | // Ideally we should have a concept of pluggable linters that verify the generated image. |
| 277 | // While such concept is not implement this will do. |
| 278 | // TODO(b/263574231): substitute with pluggable linter. |
| 279 | builder.Command(). |
| 280 | BuiltTool("host_init_verifier"). |
| 281 | FlagWithArg("--out_system=", rootDir.String()+"/system") |
| 282 | |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 283 | propFile, toolDeps := f.buildPropFile(ctx) |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 284 | output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 285 | builder.Command().BuiltTool("build_image"). |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 286 | Text(rootDir.String()). // input directory |
| 287 | Input(propFile). |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 288 | Implicits(toolDeps). |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 289 | Output(output). |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 290 | Text(rootDir.String()) // directory where to find fs_config_files|dirs |
| 291 | |
| 292 | // rootDir is not deleted. Might be useful for quick inspection. |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 293 | builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName())) |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 294 | |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 295 | return output |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 296 | } |
| 297 | |
Inseob Kim | cc8e536 | 2021-02-03 14:05:24 +0900 | [diff] [blame] | 298 | func (f *filesystem) buildFileContexts(ctx android.ModuleContext) android.OutputPath { |
| 299 | builder := android.NewRuleBuilder(pctx, ctx) |
| 300 | fcBin := android.PathForModuleOut(ctx, "file_contexts.bin") |
| 301 | builder.Command().BuiltTool("sefcontext_compile"). |
| 302 | FlagWithOutput("-o ", fcBin). |
| 303 | Input(android.PathForModuleSrc(ctx, proptools.String(f.properties.File_contexts))) |
| 304 | builder.Build("build_filesystem_file_contexts", fmt.Sprintf("Creating filesystem file contexts for %s", f.BaseModuleName())) |
| 305 | return fcBin.OutputPath |
| 306 | } |
| 307 | |
Jooyung Han | 65f402b | 2022-04-21 14:24:04 +0900 | [diff] [blame] | 308 | // Calculates avb_salt from entry list (sorted) for deterministic output. |
| 309 | func (f *filesystem) salt() string { |
| 310 | return sha1sum(f.entries) |
| 311 | } |
| 312 | |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 313 | func (f *filesystem) buildPropFile(ctx android.ModuleContext) (propFile android.OutputPath, toolDeps android.Paths) { |
| 314 | type prop struct { |
| 315 | name string |
| 316 | value string |
| 317 | } |
| 318 | |
| 319 | var props []prop |
| 320 | var deps android.Paths |
| 321 | addStr := func(name string, value string) { |
| 322 | props = append(props, prop{name, value}) |
| 323 | } |
| 324 | addPath := func(name string, path android.Path) { |
| 325 | props = append(props, prop{name, path.String()}) |
| 326 | deps = append(deps, path) |
| 327 | } |
| 328 | |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 329 | // Type string that build_image.py accepts. |
| 330 | fsTypeStr := func(t fsType) string { |
| 331 | switch t { |
| 332 | // TODO(jiyong): add more types like f2fs, erofs, etc. |
| 333 | case ext4Type: |
| 334 | return "ext4" |
| 335 | } |
| 336 | panic(fmt.Errorf("unsupported fs type %v", t)) |
| 337 | } |
| 338 | |
| 339 | addStr("fs_type", fsTypeStr(f.fsType(ctx))) |
Inseob Kim | 376d72f | 2023-11-01 15:40:25 +0900 | [diff] [blame] | 340 | addStr("mount_point", proptools.StringDefault(f.properties.Mount_point, "/")) |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 341 | addStr("use_dynamic_partition_size", "true") |
| 342 | addPath("ext_mkuserimg", ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs")) |
| 343 | // b/177813163 deps of the host tools have to be added. Remove this. |
| 344 | for _, t := range []string{"mke2fs", "e2fsdroid", "tune2fs"} { |
| 345 | deps = append(deps, ctx.Config().HostToolPath(ctx, t)) |
| 346 | } |
| 347 | |
Jiyong Park | 71baa76 | 2021-01-18 21:11:03 +0900 | [diff] [blame] | 348 | if proptools.Bool(f.properties.Use_avb) { |
| 349 | addStr("avb_hashtree_enable", "true") |
| 350 | addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool")) |
| 351 | algorithm := proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096") |
| 352 | addStr("avb_algorithm", algorithm) |
| 353 | key := android.PathForModuleSrc(ctx, proptools.String(f.properties.Avb_private_key)) |
| 354 | addPath("avb_key_path", key) |
Seungjae Yoo | a30e450 | 2023-11-09 14:55:44 +0900 | [diff] [blame] | 355 | partitionName := proptools.StringDefault(f.properties.Partition_name, f.Name()) |
| 356 | addStr("partition_name", partitionName) |
Shikha Panwar | e6f3063 | 2022-12-21 12:54:45 +0000 | [diff] [blame] | 357 | avb_add_hashtree_footer_args := "--do_not_generate_fec" |
| 358 | if hashAlgorithm := proptools.String(f.properties.Avb_hash_algorithm); hashAlgorithm != "" { |
| 359 | avb_add_hashtree_footer_args += " --hash_algorithm " + hashAlgorithm |
| 360 | } |
Seungjae Yoo | a30e450 | 2023-11-09 14:55:44 +0900 | [diff] [blame] | 361 | securityPatchKey := "com.android.build." + partitionName + ".security_patch" |
| 362 | securityPatchValue := ctx.Config().PlatformSecurityPatch() |
| 363 | avb_add_hashtree_footer_args += " --prop " + securityPatchKey + ":" + securityPatchValue |
Shikha Panwar | e6f3063 | 2022-12-21 12:54:45 +0000 | [diff] [blame] | 364 | addStr("avb_add_hashtree_footer_args", avb_add_hashtree_footer_args) |
Jooyung Han | 65f402b | 2022-04-21 14:24:04 +0900 | [diff] [blame] | 365 | addStr("avb_salt", f.salt()) |
Jiyong Park | 71baa76 | 2021-01-18 21:11:03 +0900 | [diff] [blame] | 366 | } |
| 367 | |
Inseob Kim | cc8e536 | 2021-02-03 14:05:24 +0900 | [diff] [blame] | 368 | if proptools.String(f.properties.File_contexts) != "" { |
| 369 | addPath("selinux_fc", f.buildFileContexts(ctx)) |
| 370 | } |
Jooyung Han | 65f402b | 2022-04-21 14:24:04 +0900 | [diff] [blame] | 371 | if timestamp := proptools.String(f.properties.Fake_timestamp); timestamp != "" { |
| 372 | addStr("timestamp", timestamp) |
| 373 | } |
| 374 | if uuid := proptools.String(f.properties.Uuid); uuid != "" { |
| 375 | addStr("uuid", uuid) |
| 376 | addStr("hash_seed", uuid) |
| 377 | } |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 378 | propFile = android.PathForModuleOut(ctx, "prop").OutputPath |
| 379 | builder := android.NewRuleBuilder(pctx, ctx) |
| 380 | builder.Command().Text("rm").Flag("-rf").Output(propFile) |
| 381 | for _, p := range props { |
| 382 | builder.Command(). |
Jiyong Park | 3db465d | 2021-01-26 14:08:16 +0900 | [diff] [blame] | 383 | Text("echo"). |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 384 | Flag(`"` + p.name + "=" + p.value + `"`). |
| 385 | Text(">>").Output(propFile) |
| 386 | } |
| 387 | builder.Build("build_filesystem_prop", fmt.Sprintf("Creating filesystem props for %s", f.BaseModuleName())) |
| 388 | return propFile, deps |
| 389 | } |
| 390 | |
Jiyong Park | 837cdb2 | 2021-02-05 00:17:14 +0900 | [diff] [blame] | 391 | func (f *filesystem) buildCpioImage(ctx android.ModuleContext, compressed bool) android.OutputPath { |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 392 | if proptools.Bool(f.properties.Use_avb) { |
| 393 | ctx.PropertyErrorf("use_avb", "signing compresed cpio image using avbtool is not supported."+ |
| 394 | "Consider adding this to bootimg module and signing the entire boot image.") |
| 395 | } |
| 396 | |
Inseob Kim | cc8e536 | 2021-02-03 14:05:24 +0900 | [diff] [blame] | 397 | if proptools.String(f.properties.File_contexts) != "" { |
| 398 | ctx.PropertyErrorf("file_contexts", "file_contexts is not supported for compressed cpio image.") |
| 399 | } |
| 400 | |
Inseob Kim | 2ce1b5d | 2021-02-15 17:01:04 +0900 | [diff] [blame] | 401 | depsZipFile := android.PathForModuleOut(ctx, "deps.zip").OutputPath |
Jooyung Han | 0fbbc2b | 2022-03-25 12:35:46 +0900 | [diff] [blame] | 402 | f.entries = f.CopyDepsToZip(ctx, f.gatherFilteredPackagingSpecs(ctx), depsZipFile) |
Inseob Kim | 2ce1b5d | 2021-02-15 17:01:04 +0900 | [diff] [blame] | 403 | |
| 404 | builder := android.NewRuleBuilder(pctx, ctx) |
| 405 | depsBase := proptools.StringDefault(f.properties.Base_dir, ".") |
| 406 | rebasedDepsZip := android.PathForModuleOut(ctx, "rebased_deps.zip").OutputPath |
| 407 | builder.Command(). |
| 408 | BuiltTool("zip2zip"). |
| 409 | FlagWithInput("-i ", depsZipFile). |
| 410 | FlagWithOutput("-o ", rebasedDepsZip). |
| 411 | Text("**/*:" + proptools.ShellEscape(depsBase)) // zip2zip verifies depsBase |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 412 | |
| 413 | rootDir := android.PathForModuleOut(ctx, "root").OutputPath |
Inseob Kim | 2ce1b5d | 2021-02-15 17:01:04 +0900 | [diff] [blame] | 414 | rootZip := f.buildRootZip(ctx) |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 415 | builder.Command(). |
| 416 | BuiltTool("zipsync"). |
| 417 | FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear. |
Inseob Kim | 2ce1b5d | 2021-02-15 17:01:04 +0900 | [diff] [blame] | 418 | Input(rootZip). |
| 419 | Input(rebasedDepsZip) |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 420 | |
| 421 | output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath |
Jiyong Park | 837cdb2 | 2021-02-05 00:17:14 +0900 | [diff] [blame] | 422 | cmd := builder.Command(). |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 423 | BuiltTool("mkbootfs"). |
Jiyong Park | 837cdb2 | 2021-02-05 00:17:14 +0900 | [diff] [blame] | 424 | Text(rootDir.String()) // input directory |
| 425 | if compressed { |
| 426 | cmd.Text("|"). |
| 427 | BuiltTool("lz4"). |
| 428 | Flag("--favor-decSpeed"). // for faster boot |
| 429 | Flag("-12"). // maximum compression level |
| 430 | Flag("-l"). // legacy format for kernel |
| 431 | Text(">").Output(output) |
| 432 | } else { |
| 433 | cmd.Text(">").Output(output) |
| 434 | } |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 435 | |
| 436 | // rootDir is not deleted. Might be useful for quick inspection. |
Jiyong Park | 837cdb2 | 2021-02-05 00:17:14 +0900 | [diff] [blame] | 437 | builder.Build("build_cpio_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName())) |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 438 | |
| 439 | return output |
| 440 | } |
| 441 | |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 442 | var _ android.AndroidMkEntriesProvider = (*filesystem)(nil) |
| 443 | |
| 444 | // Implements android.AndroidMkEntriesProvider |
| 445 | func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries { |
| 446 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
| 447 | Class: "ETC", |
| 448 | OutputFile: android.OptionalPathForPath(f.output), |
| 449 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 450 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
Colin Cross | c68db4b | 2021-11-11 18:59:15 -0800 | [diff] [blame] | 451 | entries.SetString("LOCAL_MODULE_PATH", f.installDir.String()) |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 452 | entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName()) |
| 453 | }, |
| 454 | }, |
| 455 | }} |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 456 | } |
Jiyong Park | 12a719c | 2021-01-07 15:31:24 +0900 | [diff] [blame] | 457 | |
Jiyong Park | 940dfd4 | 2021-02-04 15:37:34 +0900 | [diff] [blame] | 458 | var _ android.OutputFileProducer = (*filesystem)(nil) |
| 459 | |
| 460 | // Implements android.OutputFileProducer |
| 461 | func (f *filesystem) OutputFiles(tag string) (android.Paths, error) { |
| 462 | if tag == "" { |
| 463 | return []android.Path{f.output}, nil |
| 464 | } |
| 465 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) |
| 466 | } |
| 467 | |
Jiyong Park | 12a719c | 2021-01-07 15:31:24 +0900 | [diff] [blame] | 468 | // Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex |
| 469 | // package to have access to the output file. |
| 470 | type Filesystem interface { |
| 471 | android.Module |
| 472 | OutputPath() android.Path |
Jiyong Park | 972e06c | 2021-03-15 23:32:49 +0900 | [diff] [blame] | 473 | |
| 474 | // Returns the output file that is signed by avbtool. If this module is not signed, returns |
| 475 | // nil. |
| 476 | SignedOutputPath() android.Path |
Jiyong Park | 12a719c | 2021-01-07 15:31:24 +0900 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | var _ Filesystem = (*filesystem)(nil) |
| 480 | |
| 481 | func (f *filesystem) OutputPath() android.Path { |
| 482 | return f.output |
| 483 | } |
Jiyong Park | 972e06c | 2021-03-15 23:32:49 +0900 | [diff] [blame] | 484 | |
| 485 | func (f *filesystem) SignedOutputPath() android.Path { |
| 486 | if proptools.Bool(f.properties.Use_avb) { |
| 487 | return f.OutputPath() |
| 488 | } |
| 489 | return nil |
| 490 | } |
Jooyung Han | 0fbbc2b | 2022-03-25 12:35:46 +0900 | [diff] [blame] | 491 | |
| 492 | // Filter the result of GatherPackagingSpecs to discard items targeting outside "system" partition. |
| 493 | // Note that "apex" module installs its contents to "apex"(fake partition) as well |
| 494 | // for symbol lookup by imitating "activated" paths. |
| 495 | func (f *filesystem) gatherFilteredPackagingSpecs(ctx android.ModuleContext) map[string]android.PackagingSpec { |
| 496 | specs := f.PackagingBase.GatherPackagingSpecs(ctx) |
| 497 | if f.filterPackagingSpecs != nil { |
| 498 | f.filterPackagingSpecs(specs) |
| 499 | } |
| 500 | return specs |
| 501 | } |
Jooyung Han | 65f402b | 2022-04-21 14:24:04 +0900 | [diff] [blame] | 502 | |
| 503 | func sha1sum(values []string) string { |
| 504 | h := sha256.New() |
| 505 | for _, value := range values { |
| 506 | io.WriteString(h, value) |
| 507 | } |
| 508 | return fmt.Sprintf("%x", h.Sum(nil)) |
| 509 | } |
Jooyung Han | e606759 | 2023-03-16 13:11:17 +0900 | [diff] [blame] | 510 | |
| 511 | // Base cc.UseCoverage |
| 512 | |
| 513 | var _ cc.UseCoverage = (*filesystem)(nil) |
| 514 | |
Colin Cross | f5f4ad3 | 2024-01-19 15:41:48 -0800 | [diff] [blame] | 515 | func (*filesystem) IsNativeCoverageNeeded(ctx android.IncomingTransitionContext) bool { |
Jooyung Han | e606759 | 2023-03-16 13:11:17 +0900 | [diff] [blame] | 516 | return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled() |
| 517 | } |