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 |
Spandan Das | a039400 | 2025-01-07 18:38:34 +0000 | [diff] [blame] | 47 | // Name of the system_dlkm partition filesystem module |
| 48 | System_dlkm_partition_name *string |
| 49 | // Name of the vendor_dlkm partition filesystem module |
| 50 | Vendor_dlkm_partition_name *string |
| 51 | // Name of the odm_dlkm partition filesystem module |
| 52 | Odm_dlkm_partition_name *string |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | type androidDevice struct { |
| 56 | android.ModuleBase |
| 57 | |
| 58 | partitionProps PartitionNameProperties |
| 59 | } |
| 60 | |
| 61 | func AndroidDeviceFactory() android.Module { |
| 62 | module := &androidDevice{} |
| 63 | module.AddProperties(&module.partitionProps) |
Cole Faust | 341d5f1 | 2025-01-07 15:32:38 -0800 | [diff] [blame] | 64 | android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibFirst) |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 65 | return module |
| 66 | } |
| 67 | |
| 68 | type partitionDepTagType struct { |
| 69 | blueprint.BaseDependencyTag |
| 70 | } |
| 71 | |
| 72 | var filesystemDepTag partitionDepTagType |
| 73 | |
| 74 | func (a *androidDevice) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 75 | addDependencyIfDefined := func(dep *string) { |
| 76 | if dep != nil { |
Cole Faust | 341d5f1 | 2025-01-07 15:32:38 -0800 | [diff] [blame] | 77 | ctx.AddDependency(ctx.Module(), filesystemDepTag, proptools.String(dep)) |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | addDependencyIfDefined(a.partitionProps.Boot_partition_name) |
Jihoon Kang | 9e08700 | 2025-01-08 19:12:23 +0000 | [diff] [blame] | 82 | addDependencyIfDefined(a.partitionProps.Init_boot_partition_name) |
Spandan Das | ef200ac | 2025-01-08 01:42:45 +0000 | [diff] [blame^] | 83 | addDependencyIfDefined(a.partitionProps.Vendor_boot_partition_name) |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 84 | addDependencyIfDefined(a.partitionProps.System_partition_name) |
| 85 | addDependencyIfDefined(a.partitionProps.System_ext_partition_name) |
| 86 | addDependencyIfDefined(a.partitionProps.Product_partition_name) |
| 87 | addDependencyIfDefined(a.partitionProps.Vendor_partition_name) |
Spandan Das | c571716 | 2024-11-01 18:33:57 +0000 | [diff] [blame] | 88 | addDependencyIfDefined(a.partitionProps.Odm_partition_name) |
mrziwang | 23ba876 | 2024-11-07 16:21:53 -0800 | [diff] [blame] | 89 | addDependencyIfDefined(a.partitionProps.Userdata_partition_name) |
Spandan Das | a039400 | 2025-01-07 18:38:34 +0000 | [diff] [blame] | 90 | addDependencyIfDefined(a.partitionProps.System_dlkm_partition_name) |
| 91 | addDependencyIfDefined(a.partitionProps.Vendor_dlkm_partition_name) |
| 92 | addDependencyIfDefined(a.partitionProps.Odm_dlkm_partition_name) |
Spandan Das | ef200ac | 2025-01-08 01:42:45 +0000 | [diff] [blame^] | 93 | addDependencyIfDefined(a.partitionProps.Recovery_partition_name) |
Cole Faust | 3552eb6 | 2024-11-06 18:07:26 -0800 | [diff] [blame] | 94 | for _, vbmetaPartition := range a.partitionProps.Vbmeta_partitions { |
| 95 | ctx.AddDependency(ctx.Module(), filesystemDepTag, vbmetaPartition) |
| 96 | } |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | func (a *androidDevice) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Cole Faust | 4408041 | 2024-12-20 14:17:07 -0800 | [diff] [blame] | 100 | a.buildTargetFilesZip(ctx) |
| 101 | } |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 102 | |
Spandan Das | ef200ac | 2025-01-08 01:42:45 +0000 | [diff] [blame^] | 103 | type targetFilesZipCopy struct { |
| 104 | srcModule *string |
| 105 | destSubdir string |
| 106 | } |
| 107 | |
Cole Faust | 4408041 | 2024-12-20 14:17:07 -0800 | [diff] [blame] | 108 | func (a *androidDevice) buildTargetFilesZip(ctx android.ModuleContext) { |
| 109 | targetFilesDir := android.PathForModuleOut(ctx, "target_files_dir") |
| 110 | targetFilesZip := android.PathForModuleOut(ctx, "target_files.zip") |
| 111 | |
| 112 | builder := android.NewRuleBuilder(pctx, ctx) |
| 113 | builder.Command().Textf("rm -rf %s", targetFilesDir.String()) |
| 114 | builder.Command().Textf("mkdir -p %s", targetFilesDir.String()) |
Spandan Das | ef200ac | 2025-01-08 01:42:45 +0000 | [diff] [blame^] | 115 | toCopy := []targetFilesZipCopy{ |
| 116 | targetFilesZipCopy{a.partitionProps.System_partition_name, "SYSTEM"}, |
| 117 | targetFilesZipCopy{a.partitionProps.System_ext_partition_name, "SYSTEM_EXT"}, |
| 118 | targetFilesZipCopy{a.partitionProps.Product_partition_name, "PRODUCT"}, |
| 119 | targetFilesZipCopy{a.partitionProps.Vendor_partition_name, "VENDOR"}, |
| 120 | targetFilesZipCopy{a.partitionProps.Odm_partition_name, "ODM"}, |
| 121 | targetFilesZipCopy{a.partitionProps.System_dlkm_partition_name, "SYSTEM_DLKM"}, |
| 122 | targetFilesZipCopy{a.partitionProps.Vendor_dlkm_partition_name, "VENDOR_DLKM"}, |
| 123 | targetFilesZipCopy{a.partitionProps.Odm_dlkm_partition_name, "ODM_DLKM"}, |
| 124 | targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "BOOT/RAMDISK"}, |
| 125 | targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "INIT_BOOT/RAMDISK"}, |
| 126 | targetFilesZipCopy{a.partitionProps.Vendor_boot_partition_name, "VENDOR_BOOT/RAMDISK"}, |
Spandan Das | 25649f5 | 2025-01-07 18:09:22 +0000 | [diff] [blame] | 127 | } |
Spandan Das | ef200ac | 2025-01-08 01:42:45 +0000 | [diff] [blame^] | 128 | // TODO: Handle cases where recovery files are copied to BOOT/ or RECOVERY/ |
| 129 | // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=6211-6219?q=core%2FMakefile&ss=android%2Fplatform%2Fsuperproject%2Fmain |
| 130 | if ctx.DeviceConfig().BoardMoveRecoveryResourcesToVendorBoot() { |
| 131 | toCopy = append(toCopy, targetFilesZipCopy{a.partitionProps.Recovery_partition_name, "VENDOR_BOOT/RAMDISK"}) |
| 132 | } |
| 133 | |
| 134 | for _, zipCopy := range toCopy { |
| 135 | if zipCopy.srcModule == nil { |
Spandan Das | 25649f5 | 2025-01-07 18:09:22 +0000 | [diff] [blame] | 136 | continue |
| 137 | } |
Spandan Das | ef200ac | 2025-01-08 01:42:45 +0000 | [diff] [blame^] | 138 | fsInfo := a.getFilesystemInfo(ctx, *zipCopy.srcModule) |
| 139 | subdir := zipCopy.destSubdir |
| 140 | rootDirString := fsInfo.RootDir.String() |
| 141 | if subdir == "SYSTEM" { |
| 142 | rootDirString = rootDirString + "/system" |
| 143 | } |
Spandan Das | 25649f5 | 2025-01-07 18:09:22 +0000 | [diff] [blame] | 144 | builder.Command().Textf("mkdir -p %s/%s", targetFilesDir.String(), subdir) |
Cole Faust | 4408041 | 2024-12-20 14:17:07 -0800 | [diff] [blame] | 145 | builder.Command(). |
| 146 | BuiltTool("acp"). |
Spandan Das | ef200ac | 2025-01-08 01:42:45 +0000 | [diff] [blame^] | 147 | Textf("-rd %s/. %s/%s", rootDirString, targetFilesDir, subdir). |
Cole Faust | 4408041 | 2024-12-20 14:17:07 -0800 | [diff] [blame] | 148 | Implicit(fsInfo.Output) // so that the staging dir is built |
Spandan Das | ef200ac | 2025-01-08 01:42:45 +0000 | [diff] [blame^] | 149 | |
Cole Faust | 4408041 | 2024-12-20 14:17:07 -0800 | [diff] [blame] | 150 | } |
| 151 | builder.Command(). |
| 152 | BuiltTool("soong_zip"). |
| 153 | Text("-d"). |
| 154 | FlagWithOutput("-o ", targetFilesZip). |
| 155 | FlagWithArg("-C ", targetFilesDir.String()). |
| 156 | FlagWithArg("-D ", targetFilesDir.String()). |
| 157 | Text("-sha256") |
| 158 | builder.Build("target_files_"+ctx.ModuleName(), "Build target_files.zip") |
| 159 | } |
| 160 | |
| 161 | func (a *androidDevice) getFilesystemInfo(ctx android.ModuleContext, depName string) FilesystemInfo { |
| 162 | fsMod := ctx.GetDirectDepWithTag(depName, filesystemDepTag) |
| 163 | fsInfo, ok := android.OtherModuleProvider(ctx, fsMod, FilesystemProvider) |
| 164 | if !ok { |
| 165 | ctx.ModuleErrorf("Expected dependency %s to be a filesystem", depName) |
| 166 | } |
| 167 | return fsInfo |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 168 | } |