Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 1 | // Copyright (C) 2024 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 | "android/soong/android" |
| 19 | |
| 20 | "github.com/google/blueprint" |
| 21 | "github.com/google/blueprint/proptools" |
| 22 | ) |
| 23 | |
| 24 | type PartitionNameProperties struct { |
Jihoon Kang | e7e3ec8 | 2025-01-02 21:29:14 +0000 | [diff] [blame] | 25 | // Name of the boot partition filesystem module |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 26 | Boot_partition_name *string |
Jihoon Kang | e7e3ec8 | 2025-01-02 21:29:14 +0000 | [diff] [blame] | 27 | // Name of the vendor boot partition filesystem module |
| 28 | Vendor_boot_partition_name *string |
| 29 | // Name of the init boot partition filesystem module |
| 30 | Init_boot_partition_name *string |
| 31 | // Name of the system partition filesystem module |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 32 | System_partition_name *string |
Jihoon Kang | e7e3ec8 | 2025-01-02 21:29:14 +0000 | [diff] [blame] | 33 | // Name of the system_ext partition filesystem module |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 34 | System_ext_partition_name *string |
Jihoon Kang | e7e3ec8 | 2025-01-02 21:29:14 +0000 | [diff] [blame] | 35 | // Name of the product partition filesystem module |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 36 | Product_partition_name *string |
Jihoon Kang | e7e3ec8 | 2025-01-02 21:29:14 +0000 | [diff] [blame] | 37 | // Name of the vendor partition filesystem module |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 38 | Vendor_partition_name *string |
Jihoon Kang | e7e3ec8 | 2025-01-02 21:29:14 +0000 | [diff] [blame] | 39 | // Name of the odm partition filesystem module |
Spandan Das | c571716 | 2024-11-01 18:33:57 +0000 | [diff] [blame] | 40 | Odm_partition_name *string |
Jihoon Kang | e7e3ec8 | 2025-01-02 21:29:14 +0000 | [diff] [blame] | 41 | // Name of the recovery partition filesystem module |
| 42 | Recovery_partition_name *string |
Cole Faust | 3552eb6 | 2024-11-06 18:07:26 -0800 | [diff] [blame] | 43 | // The vbmeta partition and its "chained" partitions |
| 44 | Vbmeta_partitions []string |
Jihoon Kang | e7e3ec8 | 2025-01-02 21:29:14 +0000 | [diff] [blame] | 45 | // Name of the userdata partition filesystem module |
mrziwang | 23ba876 | 2024-11-07 16:21:53 -0800 | [diff] [blame] | 46 | Userdata_partition_name *string |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | type androidDevice struct { |
| 50 | android.ModuleBase |
| 51 | |
| 52 | partitionProps PartitionNameProperties |
| 53 | } |
| 54 | |
| 55 | func AndroidDeviceFactory() android.Module { |
| 56 | module := &androidDevice{} |
| 57 | module.AddProperties(&module.partitionProps) |
| 58 | android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 59 | return module |
| 60 | } |
| 61 | |
| 62 | type partitionDepTagType struct { |
| 63 | blueprint.BaseDependencyTag |
| 64 | } |
| 65 | |
| 66 | var filesystemDepTag partitionDepTagType |
| 67 | |
| 68 | func (a *androidDevice) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 69 | addDependencyIfDefined := func(dep *string) { |
| 70 | if dep != nil { |
Jihoon Kang | e7e3ec8 | 2025-01-02 21:29:14 +0000 | [diff] [blame] | 71 | ctx.AddFarVariationDependencies(nil, filesystemDepTag, proptools.String(dep)) |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
| 75 | addDependencyIfDefined(a.partitionProps.Boot_partition_name) |
| 76 | addDependencyIfDefined(a.partitionProps.System_partition_name) |
| 77 | addDependencyIfDefined(a.partitionProps.System_ext_partition_name) |
| 78 | addDependencyIfDefined(a.partitionProps.Product_partition_name) |
| 79 | addDependencyIfDefined(a.partitionProps.Vendor_partition_name) |
Spandan Das | c571716 | 2024-11-01 18:33:57 +0000 | [diff] [blame] | 80 | addDependencyIfDefined(a.partitionProps.Odm_partition_name) |
mrziwang | 23ba876 | 2024-11-07 16:21:53 -0800 | [diff] [blame] | 81 | addDependencyIfDefined(a.partitionProps.Userdata_partition_name) |
Cole Faust | 3552eb6 | 2024-11-06 18:07:26 -0800 | [diff] [blame] | 82 | for _, vbmetaPartition := range a.partitionProps.Vbmeta_partitions { |
| 83 | ctx.AddDependency(ctx.Module(), filesystemDepTag, vbmetaPartition) |
| 84 | } |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | func (a *androidDevice) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Cole Faust | 4408041 | 2024-12-20 14:17:07 -0800 | [diff] [blame] | 88 | a.buildTargetFilesZip(ctx) |
mrziwang | 2fd33a7 | 2025-01-08 12:22:08 -0800 | [diff] [blame^] | 89 | var deps []android.Path |
| 90 | ctx.VisitDirectDepsWithTag(filesystemDepTag, func(m android.Module) { |
| 91 | imageOutput, ok := android.OtherModuleProvider(ctx, m, android.OutputFilesProvider) |
| 92 | if !ok { |
| 93 | ctx.ModuleErrorf("Partition module %s doesn't set OutputfilesProvider", m.Name()) |
| 94 | } |
| 95 | if len(imageOutput.DefaultOutputFiles) != 1 { |
| 96 | ctx.ModuleErrorf("Partition module %s should provide exact 1 output file", m.Name()) |
| 97 | } |
| 98 | deps = append(deps, imageOutput.DefaultOutputFiles[0]) |
| 99 | }) |
| 100 | out := android.PathForModuleOut(ctx, "out") |
| 101 | ctx.Build(pctx, android.BuildParams{ |
| 102 | Rule: android.Touch, |
| 103 | Output: out, |
| 104 | Implicits: deps, |
| 105 | }) |
| 106 | ctx.SetOutputFiles(android.Paths{out}, "") |
| 107 | ctx.CheckbuildFile(out) |
Cole Faust | 4408041 | 2024-12-20 14:17:07 -0800 | [diff] [blame] | 108 | } |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 109 | |
Cole Faust | 4408041 | 2024-12-20 14:17:07 -0800 | [diff] [blame] | 110 | func (a *androidDevice) buildTargetFilesZip(ctx android.ModuleContext) { |
| 111 | targetFilesDir := android.PathForModuleOut(ctx, "target_files_dir") |
| 112 | targetFilesZip := android.PathForModuleOut(ctx, "target_files.zip") |
| 113 | |
| 114 | builder := android.NewRuleBuilder(pctx, ctx) |
| 115 | builder.Command().Textf("rm -rf %s", targetFilesDir.String()) |
| 116 | builder.Command().Textf("mkdir -p %s", targetFilesDir.String()) |
| 117 | if a.partitionProps.Vendor_partition_name != nil { |
| 118 | fsInfo := a.getFilesystemInfo(ctx, *a.partitionProps.Vendor_partition_name) |
| 119 | builder.Command().Textf("mkdir -p %s/VENDOR", targetFilesDir.String()) |
| 120 | builder.Command(). |
| 121 | BuiltTool("acp"). |
| 122 | Textf("-rd %s/. %s/VENDOR", fsInfo.RootDir, targetFilesDir). |
| 123 | Implicit(fsInfo.Output) // so that the staging dir is built |
| 124 | } |
| 125 | builder.Command(). |
| 126 | BuiltTool("soong_zip"). |
| 127 | Text("-d"). |
| 128 | FlagWithOutput("-o ", targetFilesZip). |
| 129 | FlagWithArg("-C ", targetFilesDir.String()). |
| 130 | FlagWithArg("-D ", targetFilesDir.String()). |
| 131 | Text("-sha256") |
| 132 | builder.Build("target_files_"+ctx.ModuleName(), "Build target_files.zip") |
| 133 | } |
| 134 | |
| 135 | func (a *androidDevice) getFilesystemInfo(ctx android.ModuleContext, depName string) FilesystemInfo { |
| 136 | fsMod := ctx.GetDirectDepWithTag(depName, filesystemDepTag) |
| 137 | fsInfo, ok := android.OtherModuleProvider(ctx, fsMod, FilesystemProvider) |
| 138 | if !ok { |
| 139 | ctx.ModuleErrorf("Expected dependency %s to be a filesystem", depName) |
| 140 | } |
| 141 | return fsInfo |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 142 | } |