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 | |
Jeongik Cha | 54bf875 | 2024-02-08 10:44:37 +0900 | [diff] [blame] | 53 | // Function that filters PackagingSpec in PackagingBase.GatherPackagingSpecs() |
| 54 | filterPackagingSpec func(spec android.PackagingSpec) bool |
Jooyung Han | 0fbbc2b | 2022-03-25 12:35:46 +0900 | [diff] [blame] | 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 | |
Cole Faust | 3b806d3 | 2024-03-11 15:15:03 -0700 | [diff] [blame^] | 186 | // Copy extra files/dirs that are not from the `deps` property to `rootDir`, checking for conflicts with files |
| 187 | // already in `rootDir`. |
| 188 | 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] | 189 | // create dirs and symlinks |
| 190 | for _, dir := range f.properties.Dirs { |
| 191 | // OutputPath.Join verifies dir |
| 192 | builder.Command().Text("mkdir -p").Text(rootDir.Join(ctx, dir).String()) |
| 193 | } |
| 194 | |
| 195 | for _, symlink := range f.properties.Symlinks { |
| 196 | name := strings.TrimSpace(proptools.String(symlink.Name)) |
| 197 | target := strings.TrimSpace(proptools.String(symlink.Target)) |
| 198 | |
| 199 | if name == "" { |
| 200 | ctx.PropertyErrorf("symlinks", "Name can't be empty") |
| 201 | continue |
| 202 | } |
| 203 | |
| 204 | if target == "" { |
| 205 | ctx.PropertyErrorf("symlinks", "Target can't be empty") |
| 206 | continue |
| 207 | } |
| 208 | |
| 209 | // OutputPath.Join verifies name. don't need to verify target. |
| 210 | dst := rootDir.Join(ctx, name) |
Cole Faust | 3b806d3 | 2024-03-11 15:15:03 -0700 | [diff] [blame^] | 211 | 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] | 212 | builder.Command().Text("mkdir -p").Text(filepath.Dir(dst.String())) |
| 213 | builder.Command().Text("ln -sf").Text(proptools.ShellEscape(target)).Text(dst.String()) |
| 214 | } |
Inseob Kim | 2ce1b5d | 2021-02-15 17:01:04 +0900 | [diff] [blame] | 215 | |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 216 | // create extra files if there's any |
| 217 | rootForExtraFiles := android.PathForModuleGen(ctx, "root-extra").OutputPath |
| 218 | var extraFiles android.OutputPaths |
| 219 | if f.buildExtraFiles != nil { |
| 220 | extraFiles = f.buildExtraFiles(ctx, rootForExtraFiles) |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 221 | } |
Inseob Kim | 2ce1b5d | 2021-02-15 17:01:04 +0900 | [diff] [blame] | 222 | |
Cole Faust | 3b806d3 | 2024-03-11 15:15:03 -0700 | [diff] [blame^] | 223 | for _, f := range extraFiles { |
| 224 | rel, err := filepath.Rel(rootForExtraFiles.String(), f.String()) |
| 225 | if err != nil || strings.HasPrefix(rel, "..") { |
| 226 | ctx.ModuleErrorf("can't make %q relative to %q", f, rootForExtraFiles) |
| 227 | continue |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 228 | } |
Cole Faust | 3b806d3 | 2024-03-11 15:15:03 -0700 | [diff] [blame^] | 229 | dst := rootDir.Join(ctx, rel) |
| 230 | builder.Command().Textf("(! [ -e %s -o -L %s ] || (echo \"%s already exists from an earlier stage of the build\" && exit 1))", dst, dst, dst) |
| 231 | builder.Command().Text("mkdir -p").Text(filepath.Dir(dst.String())) |
| 232 | builder.Command().Text("cp -P").Input(f).Output(dst) |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 233 | } |
Inseob Kim | 2ce1b5d | 2021-02-15 17:01:04 +0900 | [diff] [blame] | 234 | } |
| 235 | |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 236 | func (f *filesystem) buildImageUsingBuildImage(ctx android.ModuleContext) android.OutputPath { |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 237 | rootDir := android.PathForModuleOut(ctx, "root").OutputPath |
Cole Faust | 3b806d3 | 2024-03-11 15:15:03 -0700 | [diff] [blame^] | 238 | rebasedDir := rootDir |
| 239 | if f.properties.Base_dir != nil { |
| 240 | rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir) |
| 241 | } |
| 242 | builder := android.NewRuleBuilder(pctx, ctx) |
| 243 | // Wipe the root dir to get rid of leftover files from prior builds |
| 244 | builder.Command().Textf("rm -rf %s && mkdir -p %s", rootDir, rootDir) |
| 245 | f.entries = f.CopySpecsToDir(ctx, builder, f.gatherFilteredPackagingSpecs(ctx), rebasedDir) |
| 246 | |
| 247 | f.buildNonDepsFiles(ctx, builder, rootDir) |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 248 | |
Nikita Ioffe | 519015f | 2022-12-23 15:36:29 +0000 | [diff] [blame] | 249 | // run host_init_verifier |
| 250 | // Ideally we should have a concept of pluggable linters that verify the generated image. |
| 251 | // While such concept is not implement this will do. |
| 252 | // TODO(b/263574231): substitute with pluggable linter. |
| 253 | builder.Command(). |
| 254 | BuiltTool("host_init_verifier"). |
| 255 | FlagWithArg("--out_system=", rootDir.String()+"/system") |
| 256 | |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 257 | propFile, toolDeps := f.buildPropFile(ctx) |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 258 | output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 259 | builder.Command().BuiltTool("build_image"). |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 260 | Text(rootDir.String()). // input directory |
| 261 | Input(propFile). |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 262 | Implicits(toolDeps). |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 263 | Output(output). |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 264 | Text(rootDir.String()) // directory where to find fs_config_files|dirs |
| 265 | |
| 266 | // rootDir is not deleted. Might be useful for quick inspection. |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 267 | builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName())) |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 268 | |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 269 | return output |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 270 | } |
| 271 | |
Inseob Kim | cc8e536 | 2021-02-03 14:05:24 +0900 | [diff] [blame] | 272 | func (f *filesystem) buildFileContexts(ctx android.ModuleContext) android.OutputPath { |
| 273 | builder := android.NewRuleBuilder(pctx, ctx) |
| 274 | fcBin := android.PathForModuleOut(ctx, "file_contexts.bin") |
| 275 | builder.Command().BuiltTool("sefcontext_compile"). |
| 276 | FlagWithOutput("-o ", fcBin). |
| 277 | Input(android.PathForModuleSrc(ctx, proptools.String(f.properties.File_contexts))) |
| 278 | builder.Build("build_filesystem_file_contexts", fmt.Sprintf("Creating filesystem file contexts for %s", f.BaseModuleName())) |
| 279 | return fcBin.OutputPath |
| 280 | } |
| 281 | |
Jooyung Han | 65f402b | 2022-04-21 14:24:04 +0900 | [diff] [blame] | 282 | // Calculates avb_salt from entry list (sorted) for deterministic output. |
| 283 | func (f *filesystem) salt() string { |
| 284 | return sha1sum(f.entries) |
| 285 | } |
| 286 | |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 287 | 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] | 288 | var deps android.Paths |
Cole Faust | cec230a | 2024-03-07 15:51:12 -0800 | [diff] [blame] | 289 | var propFileString strings.Builder |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 290 | addStr := func(name string, value string) { |
Cole Faust | cec230a | 2024-03-07 15:51:12 -0800 | [diff] [blame] | 291 | propFileString.WriteString(name) |
| 292 | propFileString.WriteRune('=') |
| 293 | propFileString.WriteString(value) |
| 294 | propFileString.WriteRune('\n') |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 295 | } |
| 296 | addPath := func(name string, path android.Path) { |
Cole Faust | cec230a | 2024-03-07 15:51:12 -0800 | [diff] [blame] | 297 | addStr(name, path.String()) |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 298 | deps = append(deps, path) |
| 299 | } |
| 300 | |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 301 | // Type string that build_image.py accepts. |
| 302 | fsTypeStr := func(t fsType) string { |
| 303 | switch t { |
| 304 | // TODO(jiyong): add more types like f2fs, erofs, etc. |
| 305 | case ext4Type: |
| 306 | return "ext4" |
| 307 | } |
| 308 | panic(fmt.Errorf("unsupported fs type %v", t)) |
| 309 | } |
| 310 | |
| 311 | addStr("fs_type", fsTypeStr(f.fsType(ctx))) |
Inseob Kim | 376d72f | 2023-11-01 15:40:25 +0900 | [diff] [blame] | 312 | addStr("mount_point", proptools.StringDefault(f.properties.Mount_point, "/")) |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 313 | addStr("use_dynamic_partition_size", "true") |
| 314 | addPath("ext_mkuserimg", ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs")) |
| 315 | // b/177813163 deps of the host tools have to be added. Remove this. |
| 316 | for _, t := range []string{"mke2fs", "e2fsdroid", "tune2fs"} { |
| 317 | deps = append(deps, ctx.Config().HostToolPath(ctx, t)) |
| 318 | } |
| 319 | |
Jiyong Park | 71baa76 | 2021-01-18 21:11:03 +0900 | [diff] [blame] | 320 | if proptools.Bool(f.properties.Use_avb) { |
| 321 | addStr("avb_hashtree_enable", "true") |
| 322 | addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool")) |
| 323 | algorithm := proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096") |
| 324 | addStr("avb_algorithm", algorithm) |
| 325 | key := android.PathForModuleSrc(ctx, proptools.String(f.properties.Avb_private_key)) |
| 326 | addPath("avb_key_path", key) |
Seungjae Yoo | a30e450 | 2023-11-09 14:55:44 +0900 | [diff] [blame] | 327 | partitionName := proptools.StringDefault(f.properties.Partition_name, f.Name()) |
| 328 | addStr("partition_name", partitionName) |
Shikha Panwar | e6f3063 | 2022-12-21 12:54:45 +0000 | [diff] [blame] | 329 | avb_add_hashtree_footer_args := "--do_not_generate_fec" |
| 330 | if hashAlgorithm := proptools.String(f.properties.Avb_hash_algorithm); hashAlgorithm != "" { |
| 331 | avb_add_hashtree_footer_args += " --hash_algorithm " + hashAlgorithm |
| 332 | } |
Seungjae Yoo | a30e450 | 2023-11-09 14:55:44 +0900 | [diff] [blame] | 333 | securityPatchKey := "com.android.build." + partitionName + ".security_patch" |
| 334 | securityPatchValue := ctx.Config().PlatformSecurityPatch() |
| 335 | avb_add_hashtree_footer_args += " --prop " + securityPatchKey + ":" + securityPatchValue |
Shikha Panwar | e6f3063 | 2022-12-21 12:54:45 +0000 | [diff] [blame] | 336 | addStr("avb_add_hashtree_footer_args", avb_add_hashtree_footer_args) |
Jooyung Han | 65f402b | 2022-04-21 14:24:04 +0900 | [diff] [blame] | 337 | addStr("avb_salt", f.salt()) |
Jiyong Park | 71baa76 | 2021-01-18 21:11:03 +0900 | [diff] [blame] | 338 | } |
| 339 | |
Inseob Kim | cc8e536 | 2021-02-03 14:05:24 +0900 | [diff] [blame] | 340 | if proptools.String(f.properties.File_contexts) != "" { |
| 341 | addPath("selinux_fc", f.buildFileContexts(ctx)) |
| 342 | } |
Jooyung Han | 65f402b | 2022-04-21 14:24:04 +0900 | [diff] [blame] | 343 | if timestamp := proptools.String(f.properties.Fake_timestamp); timestamp != "" { |
| 344 | addStr("timestamp", timestamp) |
| 345 | } |
| 346 | if uuid := proptools.String(f.properties.Uuid); uuid != "" { |
| 347 | addStr("uuid", uuid) |
| 348 | addStr("hash_seed", uuid) |
| 349 | } |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 350 | propFile = android.PathForModuleOut(ctx, "prop").OutputPath |
Cole Faust | cec230a | 2024-03-07 15:51:12 -0800 | [diff] [blame] | 351 | android.WriteFileRuleVerbatim(ctx, propFile, propFileString.String()) |
Jiyong Park | 7267831 | 2021-01-18 17:29:49 +0900 | [diff] [blame] | 352 | return propFile, deps |
| 353 | } |
| 354 | |
Jiyong Park | 837cdb2 | 2021-02-05 00:17:14 +0900 | [diff] [blame] | 355 | func (f *filesystem) buildCpioImage(ctx android.ModuleContext, compressed bool) android.OutputPath { |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 356 | if proptools.Bool(f.properties.Use_avb) { |
| 357 | ctx.PropertyErrorf("use_avb", "signing compresed cpio image using avbtool is not supported."+ |
| 358 | "Consider adding this to bootimg module and signing the entire boot image.") |
| 359 | } |
| 360 | |
Inseob Kim | cc8e536 | 2021-02-03 14:05:24 +0900 | [diff] [blame] | 361 | if proptools.String(f.properties.File_contexts) != "" { |
| 362 | ctx.PropertyErrorf("file_contexts", "file_contexts is not supported for compressed cpio image.") |
| 363 | } |
| 364 | |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 365 | rootDir := android.PathForModuleOut(ctx, "root").OutputPath |
Cole Faust | 3b806d3 | 2024-03-11 15:15:03 -0700 | [diff] [blame^] | 366 | rebasedDir := rootDir |
| 367 | if f.properties.Base_dir != nil { |
| 368 | rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir) |
| 369 | } |
| 370 | builder := android.NewRuleBuilder(pctx, ctx) |
| 371 | // Wipe the root dir to get rid of leftover files from prior builds |
| 372 | builder.Command().Textf("rm -rf %s && mkdir -p %s", rootDir, rootDir) |
| 373 | f.entries = f.CopySpecsToDir(ctx, builder, f.gatherFilteredPackagingSpecs(ctx), rebasedDir) |
| 374 | |
| 375 | f.buildNonDepsFiles(ctx, builder, rootDir) |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 376 | |
| 377 | output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath |
Jiyong Park | 837cdb2 | 2021-02-05 00:17:14 +0900 | [diff] [blame] | 378 | cmd := builder.Command(). |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 379 | BuiltTool("mkbootfs"). |
Jiyong Park | 837cdb2 | 2021-02-05 00:17:14 +0900 | [diff] [blame] | 380 | Text(rootDir.String()) // input directory |
| 381 | if compressed { |
| 382 | cmd.Text("|"). |
| 383 | BuiltTool("lz4"). |
| 384 | Flag("--favor-decSpeed"). // for faster boot |
| 385 | Flag("-12"). // maximum compression level |
| 386 | Flag("-l"). // legacy format for kernel |
| 387 | Text(">").Output(output) |
| 388 | } else { |
| 389 | cmd.Text(">").Output(output) |
| 390 | } |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 391 | |
| 392 | // rootDir is not deleted. Might be useful for quick inspection. |
Jiyong Park | 837cdb2 | 2021-02-05 00:17:14 +0900 | [diff] [blame] | 393 | builder.Build("build_cpio_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName())) |
Jiyong Park | 11a6597 | 2021-02-01 21:09:38 +0900 | [diff] [blame] | 394 | |
| 395 | return output |
| 396 | } |
| 397 | |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 398 | var _ android.AndroidMkEntriesProvider = (*filesystem)(nil) |
| 399 | |
| 400 | // Implements android.AndroidMkEntriesProvider |
| 401 | func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries { |
| 402 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
| 403 | Class: "ETC", |
| 404 | OutputFile: android.OptionalPathForPath(f.output), |
| 405 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 406 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
Colin Cross | c68db4b | 2021-11-11 18:59:15 -0800 | [diff] [blame] | 407 | entries.SetString("LOCAL_MODULE_PATH", f.installDir.String()) |
Jiyong Park | 65c49f5 | 2020-11-24 14:23:26 +0900 | [diff] [blame] | 408 | entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName()) |
| 409 | }, |
| 410 | }, |
| 411 | }} |
Jiyong Park | 6f0f688 | 2020-11-12 13:14:30 +0900 | [diff] [blame] | 412 | } |
Jiyong Park | 12a719c | 2021-01-07 15:31:24 +0900 | [diff] [blame] | 413 | |
Jiyong Park | 940dfd4 | 2021-02-04 15:37:34 +0900 | [diff] [blame] | 414 | var _ android.OutputFileProducer = (*filesystem)(nil) |
| 415 | |
| 416 | // Implements android.OutputFileProducer |
| 417 | func (f *filesystem) OutputFiles(tag string) (android.Paths, error) { |
| 418 | if tag == "" { |
| 419 | return []android.Path{f.output}, nil |
| 420 | } |
| 421 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) |
| 422 | } |
| 423 | |
Jiyong Park | 12a719c | 2021-01-07 15:31:24 +0900 | [diff] [blame] | 424 | // Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex |
| 425 | // package to have access to the output file. |
| 426 | type Filesystem interface { |
| 427 | android.Module |
| 428 | OutputPath() android.Path |
Jiyong Park | 972e06c | 2021-03-15 23:32:49 +0900 | [diff] [blame] | 429 | |
| 430 | // Returns the output file that is signed by avbtool. If this module is not signed, returns |
| 431 | // nil. |
| 432 | SignedOutputPath() android.Path |
Jiyong Park | 12a719c | 2021-01-07 15:31:24 +0900 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | var _ Filesystem = (*filesystem)(nil) |
| 436 | |
| 437 | func (f *filesystem) OutputPath() android.Path { |
| 438 | return f.output |
| 439 | } |
Jiyong Park | 972e06c | 2021-03-15 23:32:49 +0900 | [diff] [blame] | 440 | |
| 441 | func (f *filesystem) SignedOutputPath() android.Path { |
| 442 | if proptools.Bool(f.properties.Use_avb) { |
| 443 | return f.OutputPath() |
| 444 | } |
| 445 | return nil |
| 446 | } |
Jooyung Han | 0fbbc2b | 2022-03-25 12:35:46 +0900 | [diff] [blame] | 447 | |
| 448 | // Filter the result of GatherPackagingSpecs to discard items targeting outside "system" partition. |
| 449 | // Note that "apex" module installs its contents to "apex"(fake partition) as well |
| 450 | // for symbol lookup by imitating "activated" paths. |
| 451 | func (f *filesystem) gatherFilteredPackagingSpecs(ctx android.ModuleContext) map[string]android.PackagingSpec { |
Jeongik Cha | 54bf875 | 2024-02-08 10:44:37 +0900 | [diff] [blame] | 452 | specs := f.PackagingBase.GatherPackagingSpecsWithFilter(ctx, f.filterPackagingSpec) |
Jooyung Han | 0fbbc2b | 2022-03-25 12:35:46 +0900 | [diff] [blame] | 453 | return specs |
| 454 | } |
Jooyung Han | 65f402b | 2022-04-21 14:24:04 +0900 | [diff] [blame] | 455 | |
| 456 | func sha1sum(values []string) string { |
| 457 | h := sha256.New() |
| 458 | for _, value := range values { |
| 459 | io.WriteString(h, value) |
| 460 | } |
| 461 | return fmt.Sprintf("%x", h.Sum(nil)) |
| 462 | } |
Jooyung Han | e606759 | 2023-03-16 13:11:17 +0900 | [diff] [blame] | 463 | |
| 464 | // Base cc.UseCoverage |
| 465 | |
| 466 | var _ cc.UseCoverage = (*filesystem)(nil) |
| 467 | |
Colin Cross | f5f4ad3 | 2024-01-19 15:41:48 -0800 | [diff] [blame] | 468 | func (*filesystem) IsNativeCoverageNeeded(ctx android.IncomingTransitionContext) bool { |
Jooyung Han | e606759 | 2023-03-16 13:11:17 +0900 | [diff] [blame] | 469 | return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled() |
| 470 | } |