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