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