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 ( |
Spandan Das | 258c08f | 2025-01-08 23:30:45 +0000 | [diff] [blame] | 18 | "strings" |
| 19 | |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 20 | "android/soong/android" |
| 21 | |
| 22 | "github.com/google/blueprint" |
| 23 | "github.com/google/blueprint/proptools" |
| 24 | ) |
| 25 | |
| 26 | type PartitionNameProperties struct { |
Jihoon Kang | e7e3ec8 | 2025-01-02 21:29:14 +0000 | [diff] [blame] | 27 | // Name of the boot partition filesystem module |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 28 | Boot_partition_name *string |
Jihoon Kang | e7e3ec8 | 2025-01-02 21:29:14 +0000 | [diff] [blame] | 29 | // Name of the vendor boot partition filesystem module |
| 30 | Vendor_boot_partition_name *string |
| 31 | // Name of the init boot partition filesystem module |
| 32 | Init_boot_partition_name *string |
| 33 | // Name of the system partition filesystem module |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 34 | System_partition_name *string |
Jihoon Kang | e7e3ec8 | 2025-01-02 21:29:14 +0000 | [diff] [blame] | 35 | // Name of the system_ext partition filesystem module |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 36 | System_ext_partition_name *string |
Jihoon Kang | e7e3ec8 | 2025-01-02 21:29:14 +0000 | [diff] [blame] | 37 | // Name of the product partition filesystem module |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 38 | Product_partition_name *string |
Jihoon Kang | e7e3ec8 | 2025-01-02 21:29:14 +0000 | [diff] [blame] | 39 | // Name of the vendor partition filesystem module |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 40 | Vendor_partition_name *string |
Jihoon Kang | e7e3ec8 | 2025-01-02 21:29:14 +0000 | [diff] [blame] | 41 | // Name of the odm partition filesystem module |
Spandan Das | c571716 | 2024-11-01 18:33:57 +0000 | [diff] [blame] | 42 | Odm_partition_name *string |
Jihoon Kang | e7e3ec8 | 2025-01-02 21:29:14 +0000 | [diff] [blame] | 43 | // Name of the recovery partition filesystem module |
| 44 | Recovery_partition_name *string |
Cole Faust | 3552eb6 | 2024-11-06 18:07:26 -0800 | [diff] [blame] | 45 | // The vbmeta partition and its "chained" partitions |
| 46 | Vbmeta_partitions []string |
Jihoon Kang | e7e3ec8 | 2025-01-02 21:29:14 +0000 | [diff] [blame] | 47 | // Name of the userdata partition filesystem module |
mrziwang | 23ba876 | 2024-11-07 16:21:53 -0800 | [diff] [blame] | 48 | Userdata_partition_name *string |
Spandan Das | a039400 | 2025-01-07 18:38:34 +0000 | [diff] [blame] | 49 | // Name of the system_dlkm partition filesystem module |
| 50 | System_dlkm_partition_name *string |
| 51 | // Name of the vendor_dlkm partition filesystem module |
| 52 | Vendor_dlkm_partition_name *string |
| 53 | // Name of the odm_dlkm partition filesystem module |
| 54 | Odm_dlkm_partition_name *string |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | type androidDevice struct { |
| 58 | android.ModuleBase |
| 59 | |
| 60 | partitionProps PartitionNameProperties |
| 61 | } |
| 62 | |
| 63 | func AndroidDeviceFactory() android.Module { |
| 64 | module := &androidDevice{} |
| 65 | module.AddProperties(&module.partitionProps) |
Cole Faust | 341d5f1 | 2025-01-07 15:32:38 -0800 | [diff] [blame] | 66 | android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibFirst) |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 67 | return module |
| 68 | } |
| 69 | |
| 70 | type partitionDepTagType struct { |
| 71 | blueprint.BaseDependencyTag |
| 72 | } |
| 73 | |
| 74 | var filesystemDepTag partitionDepTagType |
| 75 | |
| 76 | func (a *androidDevice) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 77 | addDependencyIfDefined := func(dep *string) { |
| 78 | if dep != nil { |
Cole Faust | 341d5f1 | 2025-01-07 15:32:38 -0800 | [diff] [blame] | 79 | ctx.AddDependency(ctx.Module(), filesystemDepTag, proptools.String(dep)) |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | addDependencyIfDefined(a.partitionProps.Boot_partition_name) |
Jihoon Kang | 9e08700 | 2025-01-08 19:12:23 +0000 | [diff] [blame] | 84 | addDependencyIfDefined(a.partitionProps.Init_boot_partition_name) |
Spandan Das | ef200ac | 2025-01-08 01:42:45 +0000 | [diff] [blame] | 85 | addDependencyIfDefined(a.partitionProps.Vendor_boot_partition_name) |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 86 | addDependencyIfDefined(a.partitionProps.System_partition_name) |
| 87 | addDependencyIfDefined(a.partitionProps.System_ext_partition_name) |
| 88 | addDependencyIfDefined(a.partitionProps.Product_partition_name) |
| 89 | addDependencyIfDefined(a.partitionProps.Vendor_partition_name) |
Spandan Das | c571716 | 2024-11-01 18:33:57 +0000 | [diff] [blame] | 90 | addDependencyIfDefined(a.partitionProps.Odm_partition_name) |
mrziwang | 23ba876 | 2024-11-07 16:21:53 -0800 | [diff] [blame] | 91 | addDependencyIfDefined(a.partitionProps.Userdata_partition_name) |
Spandan Das | a039400 | 2025-01-07 18:38:34 +0000 | [diff] [blame] | 92 | addDependencyIfDefined(a.partitionProps.System_dlkm_partition_name) |
| 93 | addDependencyIfDefined(a.partitionProps.Vendor_dlkm_partition_name) |
| 94 | addDependencyIfDefined(a.partitionProps.Odm_dlkm_partition_name) |
Spandan Das | ef200ac | 2025-01-08 01:42:45 +0000 | [diff] [blame] | 95 | addDependencyIfDefined(a.partitionProps.Recovery_partition_name) |
Cole Faust | 3552eb6 | 2024-11-06 18:07:26 -0800 | [diff] [blame] | 96 | for _, vbmetaPartition := range a.partitionProps.Vbmeta_partitions { |
| 97 | ctx.AddDependency(ctx.Module(), filesystemDepTag, vbmetaPartition) |
| 98 | } |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | func (a *androidDevice) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Cole Faust | 4408041 | 2024-12-20 14:17:07 -0800 | [diff] [blame] | 102 | a.buildTargetFilesZip(ctx) |
mrziwang | 2fd33a7 | 2025-01-08 12:22:08 -0800 | [diff] [blame] | 103 | var deps []android.Path |
| 104 | ctx.VisitDirectDepsWithTag(filesystemDepTag, func(m android.Module) { |
| 105 | imageOutput, ok := android.OtherModuleProvider(ctx, m, android.OutputFilesProvider) |
| 106 | if !ok { |
| 107 | ctx.ModuleErrorf("Partition module %s doesn't set OutputfilesProvider", m.Name()) |
| 108 | } |
| 109 | if len(imageOutput.DefaultOutputFiles) != 1 { |
| 110 | ctx.ModuleErrorf("Partition module %s should provide exact 1 output file", m.Name()) |
| 111 | } |
| 112 | deps = append(deps, imageOutput.DefaultOutputFiles[0]) |
| 113 | }) |
| 114 | out := android.PathForModuleOut(ctx, "out") |
| 115 | ctx.Build(pctx, android.BuildParams{ |
| 116 | Rule: android.Touch, |
| 117 | Output: out, |
| 118 | Implicits: deps, |
| 119 | }) |
| 120 | ctx.SetOutputFiles(android.Paths{out}, "") |
| 121 | ctx.CheckbuildFile(out) |
Cole Faust | 4408041 | 2024-12-20 14:17:07 -0800 | [diff] [blame] | 122 | } |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 123 | |
Spandan Das | ef200ac | 2025-01-08 01:42:45 +0000 | [diff] [blame] | 124 | type targetFilesZipCopy struct { |
| 125 | srcModule *string |
| 126 | destSubdir string |
| 127 | } |
| 128 | |
Cole Faust | 4408041 | 2024-12-20 14:17:07 -0800 | [diff] [blame] | 129 | func (a *androidDevice) buildTargetFilesZip(ctx android.ModuleContext) { |
| 130 | targetFilesDir := android.PathForModuleOut(ctx, "target_files_dir") |
| 131 | targetFilesZip := android.PathForModuleOut(ctx, "target_files.zip") |
| 132 | |
| 133 | builder := android.NewRuleBuilder(pctx, ctx) |
| 134 | builder.Command().Textf("rm -rf %s", targetFilesDir.String()) |
| 135 | builder.Command().Textf("mkdir -p %s", targetFilesDir.String()) |
Spandan Das | ef200ac | 2025-01-08 01:42:45 +0000 | [diff] [blame] | 136 | toCopy := []targetFilesZipCopy{ |
| 137 | targetFilesZipCopy{a.partitionProps.System_partition_name, "SYSTEM"}, |
| 138 | targetFilesZipCopy{a.partitionProps.System_ext_partition_name, "SYSTEM_EXT"}, |
| 139 | targetFilesZipCopy{a.partitionProps.Product_partition_name, "PRODUCT"}, |
| 140 | targetFilesZipCopy{a.partitionProps.Vendor_partition_name, "VENDOR"}, |
| 141 | targetFilesZipCopy{a.partitionProps.Odm_partition_name, "ODM"}, |
| 142 | targetFilesZipCopy{a.partitionProps.System_dlkm_partition_name, "SYSTEM_DLKM"}, |
| 143 | targetFilesZipCopy{a.partitionProps.Vendor_dlkm_partition_name, "VENDOR_DLKM"}, |
| 144 | targetFilesZipCopy{a.partitionProps.Odm_dlkm_partition_name, "ODM_DLKM"}, |
| 145 | targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "BOOT/RAMDISK"}, |
| 146 | targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "INIT_BOOT/RAMDISK"}, |
| 147 | targetFilesZipCopy{a.partitionProps.Vendor_boot_partition_name, "VENDOR_BOOT/RAMDISK"}, |
Spandan Das | 25649f5 | 2025-01-07 18:09:22 +0000 | [diff] [blame] | 148 | } |
Spandan Das | ef200ac | 2025-01-08 01:42:45 +0000 | [diff] [blame] | 149 | // TODO: Handle cases where recovery files are copied to BOOT/ or RECOVERY/ |
| 150 | // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=6211-6219?q=core%2FMakefile&ss=android%2Fplatform%2Fsuperproject%2Fmain |
| 151 | if ctx.DeviceConfig().BoardMoveRecoveryResourcesToVendorBoot() { |
| 152 | toCopy = append(toCopy, targetFilesZipCopy{a.partitionProps.Recovery_partition_name, "VENDOR_BOOT/RAMDISK"}) |
| 153 | } |
| 154 | |
| 155 | for _, zipCopy := range toCopy { |
| 156 | if zipCopy.srcModule == nil { |
Spandan Das | 25649f5 | 2025-01-07 18:09:22 +0000 | [diff] [blame] | 157 | continue |
| 158 | } |
Spandan Das | ef200ac | 2025-01-08 01:42:45 +0000 | [diff] [blame] | 159 | fsInfo := a.getFilesystemInfo(ctx, *zipCopy.srcModule) |
| 160 | subdir := zipCopy.destSubdir |
| 161 | rootDirString := fsInfo.RootDir.String() |
| 162 | if subdir == "SYSTEM" { |
| 163 | rootDirString = rootDirString + "/system" |
| 164 | } |
Spandan Das | 25649f5 | 2025-01-07 18:09:22 +0000 | [diff] [blame] | 165 | builder.Command().Textf("mkdir -p %s/%s", targetFilesDir.String(), subdir) |
Cole Faust | 4408041 | 2024-12-20 14:17:07 -0800 | [diff] [blame] | 166 | builder.Command(). |
| 167 | BuiltTool("acp"). |
Spandan Das | ef200ac | 2025-01-08 01:42:45 +0000 | [diff] [blame] | 168 | Textf("-rd %s/. %s/%s", rootDirString, targetFilesDir, subdir). |
Cole Faust | 4408041 | 2024-12-20 14:17:07 -0800 | [diff] [blame] | 169 | Implicit(fsInfo.Output) // so that the staging dir is built |
Spandan Das | ef200ac | 2025-01-08 01:42:45 +0000 | [diff] [blame] | 170 | |
Cole Faust | 4408041 | 2024-12-20 14:17:07 -0800 | [diff] [blame] | 171 | } |
Spandan Das | 9b17df2 | 2025-01-08 23:30:45 +0000 | [diff] [blame^] | 172 | // Copy cmdline, kernel etc. files of boot images |
Spandan Das | 258c08f | 2025-01-08 23:30:45 +0000 | [diff] [blame] | 173 | if a.partitionProps.Vendor_boot_partition_name != nil { |
| 174 | bootImg := ctx.GetDirectDepWithTag(proptools.String(a.partitionProps.Vendor_boot_partition_name), filesystemDepTag) |
| 175 | bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider) |
Spandan Das | 9b17df2 | 2025-01-08 23:30:45 +0000 | [diff] [blame^] | 176 | builder.Command().Textf("echo %s > %s/VENDOR_BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir) |
| 177 | builder.Command().Textf("echo %s > %s/VENDOR_BOOT/vendor_cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir) |
| 178 | if bootImgInfo.Dtb != nil { |
| 179 | builder.Command().Textf("cp %s %s/VENDOR_BOOT/dtb", bootImgInfo.Dtb, targetFilesDir) |
| 180 | } |
Spandan Das | 258c08f | 2025-01-08 23:30:45 +0000 | [diff] [blame] | 181 | } |
| 182 | if a.partitionProps.Boot_partition_name != nil { |
| 183 | bootImg := ctx.GetDirectDepWithTag(proptools.String(a.partitionProps.Boot_partition_name), filesystemDepTag) |
| 184 | bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider) |
Spandan Das | 9b17df2 | 2025-01-08 23:30:45 +0000 | [diff] [blame^] | 185 | builder.Command().Textf("echo %s > %s/BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir) |
| 186 | if bootImgInfo.Dtb != nil { |
| 187 | builder.Command().Textf("cp %s %s/BOOT/dtb", bootImgInfo.Dtb, targetFilesDir) |
| 188 | } |
| 189 | if bootImgInfo.Kernel != nil { |
| 190 | builder.Command().Textf("cp %s %s/BOOT/kernel", bootImgInfo.Kernel, targetFilesDir) |
| 191 | // Even though kernel is not used to build vendor_boot, copy the kernel to VENDOR_BOOT to match the behavior of make packaging. |
| 192 | builder.Command().Textf("cp %s %s/VENDOR_BOOT/kernel", bootImgInfo.Kernel, targetFilesDir) |
| 193 | } |
Spandan Das | 258c08f | 2025-01-08 23:30:45 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Cole Faust | 4408041 | 2024-12-20 14:17:07 -0800 | [diff] [blame] | 196 | builder.Command(). |
| 197 | BuiltTool("soong_zip"). |
| 198 | Text("-d"). |
| 199 | FlagWithOutput("-o ", targetFilesZip). |
| 200 | FlagWithArg("-C ", targetFilesDir.String()). |
| 201 | FlagWithArg("-D ", targetFilesDir.String()). |
| 202 | Text("-sha256") |
| 203 | builder.Build("target_files_"+ctx.ModuleName(), "Build target_files.zip") |
| 204 | } |
| 205 | |
| 206 | func (a *androidDevice) getFilesystemInfo(ctx android.ModuleContext, depName string) FilesystemInfo { |
| 207 | fsMod := ctx.GetDirectDepWithTag(depName, filesystemDepTag) |
| 208 | fsInfo, ok := android.OtherModuleProvider(ctx, fsMod, FilesystemProvider) |
| 209 | if !ok { |
| 210 | ctx.ModuleErrorf("Expected dependency %s to be a filesystem", depName) |
| 211 | } |
| 212 | return fsInfo |
Jihoon Kang | f2c5398 | 2024-10-09 17:32:52 +0000 | [diff] [blame] | 213 | } |