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