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