blob: a91457848453a073a2b7eea23964ccd2a3f0a547 [file] [log] [blame]
Jihoon Kang98047cf2024-10-02 17:13:54 +00001// 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
15package fsgen
16
17import (
Cole Faust92ccbe22024-10-03 14:38:37 -070018 "crypto/sha256"
Jihoon Kang98047cf2024-10-02 17:13:54 +000019 "fmt"
Spandan Das312cc412024-10-29 18:20:11 +000020 "path/filepath"
Cole Fauste1676122024-12-03 17:32:25 -080021 "slices"
Jihoon Kang98047cf2024-10-02 17:13:54 +000022 "strconv"
mrziwang8f86c882024-10-03 12:34:33 -070023 "strings"
mrziwang8f86c882024-10-03 12:34:33 -070024
25 "android/soong/android"
26 "android/soong/filesystem"
Spandan Das5e336422024-11-01 22:31:20 +000027 "android/soong/kernel"
Jihoon Kang98047cf2024-10-02 17:13:54 +000028
Cole Faust92ccbe22024-10-03 14:38:37 -070029 "github.com/google/blueprint"
mrziwang8f86c882024-10-03 12:34:33 -070030 "github.com/google/blueprint/parser"
Jihoon Kang98047cf2024-10-02 17:13:54 +000031 "github.com/google/blueprint/proptools"
32)
33
Cole Faust92ccbe22024-10-03 14:38:37 -070034var pctx = android.NewPackageContext("android/soong/fsgen")
35
Jihoon Kang98047cf2024-10-02 17:13:54 +000036func init() {
37 registerBuildComponents(android.InitRegistrationContext)
38}
39
40func registerBuildComponents(ctx android.RegistrationContext) {
41 ctx.RegisterModuleType("soong_filesystem_creator", filesystemCreatorFactory)
mrziwang8f86c882024-10-03 12:34:33 -070042 ctx.PreDepsMutators(RegisterCollectFileSystemDepsMutators)
43}
44
Cole Faust92ccbe22024-10-03 14:38:37 -070045type filesystemCreatorProps struct {
46 Generated_partition_types []string `blueprint:"mutated"`
47 Unsupported_partition_types []string `blueprint:"mutated"`
Cole Faust3552eb62024-11-06 18:07:26 -080048
49 Vbmeta_module_names []string `blueprint:"mutated"`
50 Vbmeta_partition_names []string `blueprint:"mutated"`
Cole Faustf2a6e8b2024-11-14 10:54:48 -080051
Cole Faust24938e22024-11-18 14:01:58 -080052 Boot_image string `blueprint:"mutated" android:"path_device_first"`
53 Vendor_boot_image string `blueprint:"mutated" android:"path_device_first"`
Jihoon Kang95eb1da2024-11-19 20:55:20 +000054 Init_boot_image string `blueprint:"mutated" android:"path_device_first"`
Cole Faust92ccbe22024-10-03 14:38:37 -070055}
56
Jihoon Kang98047cf2024-10-02 17:13:54 +000057type filesystemCreator struct {
58 android.ModuleBase
Cole Faust92ccbe22024-10-03 14:38:37 -070059
60 properties filesystemCreatorProps
Jihoon Kang98047cf2024-10-02 17:13:54 +000061}
62
63func filesystemCreatorFactory() android.Module {
64 module := &filesystemCreator{}
65
Cole Faust69788792024-10-10 11:00:36 -070066 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
Cole Faust92ccbe22024-10-03 14:38:37 -070067 module.AddProperties(&module.properties)
Jihoon Kang98047cf2024-10-02 17:13:54 +000068 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Jihoon Kang675d4682024-10-24 23:45:11 +000069 generatedPrebuiltEtcModuleNames := createPrebuiltEtcModules(ctx)
Jihoon Kang04f12c92024-11-12 23:03:08 +000070 avbpubkeyGenerated := createAvbpubkeyModule(ctx)
71 createFsGenState(ctx, generatedPrebuiltEtcModuleNames, avbpubkeyGenerated)
Cole Faust953476f2024-11-14 14:11:29 -080072 module.createAvbKeyFilegroups(ctx)
Cole Faust3e730972024-12-03 13:12:08 -080073 module.createMiscFilegroups(ctx)
Jihoon Kang98047cf2024-10-02 17:13:54 +000074 module.createInternalModules(ctx)
75 })
76
77 return module
78}
79
Cole Faustf2a6e8b2024-11-14 10:54:48 -080080func generatedPartitions(ctx android.LoadHookContext) []string {
Cole Faust24938e22024-11-18 14:01:58 -080081 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Cole Faustf2a6e8b2024-11-14 10:54:48 -080082 generatedPartitions := []string{"system"}
83 if ctx.DeviceConfig().SystemExtPath() == "system_ext" {
84 generatedPartitions = append(generatedPartitions, "system_ext")
85 }
86 if ctx.DeviceConfig().BuildingVendorImage() && ctx.DeviceConfig().VendorPath() == "vendor" {
87 generatedPartitions = append(generatedPartitions, "vendor")
88 }
89 if ctx.DeviceConfig().BuildingProductImage() && ctx.DeviceConfig().ProductPath() == "product" {
90 generatedPartitions = append(generatedPartitions, "product")
91 }
92 if ctx.DeviceConfig().BuildingOdmImage() && ctx.DeviceConfig().OdmPath() == "odm" {
93 generatedPartitions = append(generatedPartitions, "odm")
94 }
95 if ctx.DeviceConfig().BuildingUserdataImage() && ctx.DeviceConfig().UserdataPath() == "data" {
96 generatedPartitions = append(generatedPartitions, "userdata")
97 }
Cole Faust24938e22024-11-18 14:01:58 -080098 if partitionVars.BuildingSystemDlkmImage {
Cole Faustf2a6e8b2024-11-14 10:54:48 -080099 generatedPartitions = append(generatedPartitions, "system_dlkm")
100 }
Cole Faust24938e22024-11-18 14:01:58 -0800101 if partitionVars.BuildingVendorDlkmImage {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800102 generatedPartitions = append(generatedPartitions, "vendor_dlkm")
103 }
Cole Faust24938e22024-11-18 14:01:58 -0800104 if partitionVars.BuildingOdmDlkmImage {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800105 generatedPartitions = append(generatedPartitions, "odm_dlkm")
106 }
Cole Faust24938e22024-11-18 14:01:58 -0800107 if partitionVars.BuildingRamdiskImage {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800108 generatedPartitions = append(generatedPartitions, "ramdisk")
109 }
Cole Faust24938e22024-11-18 14:01:58 -0800110 if buildingVendorBootImage(partitionVars) {
111 generatedPartitions = append(generatedPartitions, "vendor_ramdisk")
112 }
Jihoon Kang3216c982024-12-02 19:42:20 +0000113 if ctx.DeviceConfig().BuildingRecoveryImage() && ctx.DeviceConfig().RecoveryPath() == "recovery" {
114 generatedPartitions = append(generatedPartitions, "recovery")
115 }
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800116 return generatedPartitions
117}
118
Jihoon Kang98047cf2024-10-02 17:13:54 +0000119func (f *filesystemCreator) createInternalModules(ctx android.LoadHookContext) {
Cole Faust3552eb62024-11-06 18:07:26 -0800120 soongGeneratedPartitions := generatedPartitions(ctx)
121 finalSoongGeneratedPartitions := make([]string, 0, len(soongGeneratedPartitions))
122 for _, partitionType := range soongGeneratedPartitions {
Cole Faust92ccbe22024-10-03 14:38:37 -0700123 if f.createPartition(ctx, partitionType) {
124 f.properties.Generated_partition_types = append(f.properties.Generated_partition_types, partitionType)
Cole Faust3552eb62024-11-06 18:07:26 -0800125 finalSoongGeneratedPartitions = append(finalSoongGeneratedPartitions, partitionType)
Cole Faust92ccbe22024-10-03 14:38:37 -0700126 } else {
127 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, partitionType)
128 }
129 }
Cole Faust3552eb62024-11-06 18:07:26 -0800130
Cole Faust24938e22024-11-18 14:01:58 -0800131 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Jihoon Kang70c1c682024-11-20 23:58:38 +0000132 dtbImg := createDtbImgFilegroup(ctx)
133
Cole Faust24938e22024-11-18 14:01:58 -0800134 if buildingBootImage(partitionVars) {
Jihoon Kang70c1c682024-11-20 23:58:38 +0000135 if createBootImage(ctx, dtbImg) {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800136 f.properties.Boot_image = ":" + generatedModuleNameForPartition(ctx.Config(), "boot")
137 } else {
138 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, "boot")
139 }
140 }
Cole Faust24938e22024-11-18 14:01:58 -0800141 if buildingVendorBootImage(partitionVars) {
Jihoon Kang70c1c682024-11-20 23:58:38 +0000142 if createVendorBootImage(ctx, dtbImg) {
Cole Faust24938e22024-11-18 14:01:58 -0800143 f.properties.Vendor_boot_image = ":" + generatedModuleNameForPartition(ctx.Config(), "vendor_boot")
144 } else {
145 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, "vendor_boot")
146 }
147 }
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000148 if buildingInitBootImage(partitionVars) {
149 if createInitBootImage(ctx) {
150 f.properties.Init_boot_image = ":" + generatedModuleNameForPartition(ctx.Config(), "init_boot")
151 } else {
152 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, "init_boot")
153 }
154 }
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800155
Cole Faust3552eb62024-11-06 18:07:26 -0800156 for _, x := range createVbmetaPartitions(ctx, finalSoongGeneratedPartitions) {
157 f.properties.Vbmeta_module_names = append(f.properties.Vbmeta_module_names, x.moduleName)
158 f.properties.Vbmeta_partition_names = append(f.properties.Vbmeta_partition_names, x.partitionName)
159 }
160
161 ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions = finalSoongGeneratedPartitions
162 f.createDeviceModule(ctx, finalSoongGeneratedPartitions, f.properties.Vbmeta_module_names)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000163}
164
Jihoon Kang0d545b82024-10-11 00:21:57 +0000165func generatedModuleName(cfg android.Config, suffix string) string {
Cole Faust92ccbe22024-10-03 14:38:37 -0700166 prefix := "soong"
167 if cfg.HasDeviceProduct() {
168 prefix = cfg.DeviceProduct()
169 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000170 return fmt.Sprintf("%s_generated_%s", prefix, suffix)
171}
172
Jihoon Kang0d545b82024-10-11 00:21:57 +0000173func generatedModuleNameForPartition(cfg android.Config, partitionType string) string {
174 return generatedModuleName(cfg, fmt.Sprintf("%s_image", partitionType))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000175}
176
Cole Faust3552eb62024-11-06 18:07:26 -0800177func (f *filesystemCreator) createDeviceModule(
178 ctx android.LoadHookContext,
179 generatedPartitionTypes []string,
180 vbmetaPartitions []string,
181) {
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000182 baseProps := &struct {
183 Name *string
184 }{
Jihoon Kang0d545b82024-10-11 00:21:57 +0000185 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "device")),
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000186 }
187
Priyanka Advani (xWF)dafaa7f2024-10-21 22:55:13 +0000188 // Currently, only the system and system_ext partition module is created.
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000189 partitionProps := &filesystem.PartitionNameProperties{}
Cole Faust3552eb62024-11-06 18:07:26 -0800190 if android.InList("system", generatedPartitionTypes) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000191 partitionProps.System_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system"))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000192 }
Cole Faust3552eb62024-11-06 18:07:26 -0800193 if android.InList("system_ext", generatedPartitionTypes) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000194 partitionProps.System_ext_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_ext"))
Spandan Das7a46f6c2024-10-14 18:41:18 +0000195 }
Cole Faust3552eb62024-11-06 18:07:26 -0800196 if android.InList("vendor", generatedPartitionTypes) {
Spandan Dase3b65312024-10-22 00:27:27 +0000197 partitionProps.Vendor_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "vendor"))
198 }
Cole Faust3552eb62024-11-06 18:07:26 -0800199 if android.InList("product", generatedPartitionTypes) {
Jihoon Kang6dd13b62024-10-22 23:21:02 +0000200 partitionProps.Product_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "product"))
201 }
Cole Faust3552eb62024-11-06 18:07:26 -0800202 if android.InList("odm", generatedPartitionTypes) {
Spandan Dasc5717162024-11-01 18:33:57 +0000203 partitionProps.Odm_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "odm"))
204 }
mrziwang23ba8762024-11-07 16:21:53 -0800205 if android.InList("userdata", f.properties.Generated_partition_types) {
206 partitionProps.Userdata_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "userdata"))
207 }
Cole Faust3552eb62024-11-06 18:07:26 -0800208 partitionProps.Vbmeta_partitions = vbmetaPartitions
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000209
210 ctx.CreateModule(filesystem.AndroidDeviceFactory, baseProps, partitionProps)
Cole Faust92ccbe22024-10-03 14:38:37 -0700211}
212
Spandan Das71be42d2024-11-20 18:34:16 +0000213func partitionSpecificFsProps(ctx android.EarlyModuleContext, fsProps *filesystem.FilesystemProperties, partitionVars android.PartitionVariables, partitionType string) {
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000214 switch partitionType {
215 case "system":
216 fsProps.Build_logtags = proptools.BoolPtr(true)
217 // https://source.corp.google.com/h/googleplex-android/platform/build//639d79f5012a6542ab1f733b0697db45761ab0f3:core/packaging/flags.mk;l=21;drc=5ba8a8b77507f93aa48cc61c5ba3f31a4d0cbf37;bpv=1;bpt=0
218 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
Justin Yuned3dbce2024-11-15 11:57:24 +0900219 // Identical to that of the aosp_shared_system_image
Spandan Das2b4bf4c2024-12-02 19:41:04 +0000220 if partitionVars.ProductFsverityGenerateMetadata {
221 fsProps.Fsverity.Inputs = []string{
222 "etc/boot-image.prof",
223 "etc/dirty-image-objects",
224 "etc/preloaded-classes",
225 "etc/classpaths/*.pb",
226 "framework/*",
227 "framework/*/*", // framework/{arch}
228 "framework/oat/*/*", // framework/oat/{arch}
229 }
230 fsProps.Fsverity.Libs = []string{":framework-res{.export-package.apk}"}
Spandan Dasa8fa6b42024-10-23 00:45:29 +0000231 }
Cole Faust1d4e76c2024-11-26 14:15:29 -0800232 // Most of the symlinks and directories listed here originate from create_root_structure.mk,
233 // but the handwritten generic system image also recreates them:
234 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/target/product/generic/Android.bp;l=33;drc=db08311f1b6ef6cb0a4fbcc6263b89849360ce04
mrziwang9afc2982024-11-05 14:29:48 -0800235 // TODO(b/377734331): only generate the symlinks if the relevant partitions exist
236 fsProps.Symlinks = []filesystem.SymlinkDefinition{
237 filesystem.SymlinkDefinition{
Cole Faust1d4e76c2024-11-26 14:15:29 -0800238 Target: proptools.StringPtr("/system/bin/init"),
239 Name: proptools.StringPtr("init"),
240 },
241 filesystem.SymlinkDefinition{
242 Target: proptools.StringPtr("/system/etc"),
243 Name: proptools.StringPtr("etc"),
244 },
245 filesystem.SymlinkDefinition{
246 Target: proptools.StringPtr("/system/bin"),
247 Name: proptools.StringPtr("bin"),
248 },
249 filesystem.SymlinkDefinition{
250 Target: proptools.StringPtr("/data/user_de/0/com.android.shell/files/bugreports"),
251 Name: proptools.StringPtr("bugreports"),
252 },
253 filesystem.SymlinkDefinition{
254 Target: proptools.StringPtr("/sys/kernel/debug"),
255 Name: proptools.StringPtr("d"),
256 },
257 filesystem.SymlinkDefinition{
258 Target: proptools.StringPtr("/storage/self/primary"),
259 Name: proptools.StringPtr("sdcard"),
260 },
261 filesystem.SymlinkDefinition{
262 Target: proptools.StringPtr("/product/etc/security/adb_keys"),
263 Name: proptools.StringPtr("adb_keys"),
264 },
265 filesystem.SymlinkDefinition{
266 Target: proptools.StringPtr("/vendor/odm/app"),
267 Name: proptools.StringPtr("odm/app"),
268 },
269 filesystem.SymlinkDefinition{
270 Target: proptools.StringPtr("/vendor/odm/bin"),
271 Name: proptools.StringPtr("odm/bin"),
272 },
273 filesystem.SymlinkDefinition{
274 Target: proptools.StringPtr("/vendor/odm/etc"),
275 Name: proptools.StringPtr("odm/etc"),
276 },
277 filesystem.SymlinkDefinition{
278 Target: proptools.StringPtr("/vendor/odm/firmware"),
279 Name: proptools.StringPtr("odm/firmware"),
280 },
281 filesystem.SymlinkDefinition{
282 Target: proptools.StringPtr("/vendor/odm/framework"),
283 Name: proptools.StringPtr("odm/framework"),
284 },
285 filesystem.SymlinkDefinition{
286 Target: proptools.StringPtr("/vendor/odm/lib"),
287 Name: proptools.StringPtr("odm/lib"),
288 },
289 filesystem.SymlinkDefinition{
290 Target: proptools.StringPtr("/vendor/odm/lib64"),
291 Name: proptools.StringPtr("odm/lib64"),
292 },
293 filesystem.SymlinkDefinition{
294 Target: proptools.StringPtr("/vendor/odm/overlay"),
295 Name: proptools.StringPtr("odm/overlay"),
296 },
297 filesystem.SymlinkDefinition{
298 Target: proptools.StringPtr("/vendor/odm/priv-app"),
299 Name: proptools.StringPtr("odm/priv-app"),
300 },
301 filesystem.SymlinkDefinition{
302 Target: proptools.StringPtr("/vendor/odm/usr"),
303 Name: proptools.StringPtr("odm/usr"),
304 },
305 filesystem.SymlinkDefinition{
mrziwang9afc2982024-11-05 14:29:48 -0800306 Target: proptools.StringPtr("/product"),
307 Name: proptools.StringPtr("system/product"),
308 },
309 filesystem.SymlinkDefinition{
310 Target: proptools.StringPtr("/system_ext"),
311 Name: proptools.StringPtr("system/system_ext"),
312 },
313 filesystem.SymlinkDefinition{
314 Target: proptools.StringPtr("/vendor"),
315 Name: proptools.StringPtr("system/vendor"),
316 },
317 filesystem.SymlinkDefinition{
318 Target: proptools.StringPtr("/system_dlkm/lib/modules"),
319 Name: proptools.StringPtr("system/lib/modules"),
320 },
Cole Faust1d4e76c2024-11-26 14:15:29 -0800321 filesystem.SymlinkDefinition{
322 Target: proptools.StringPtr("/data/cache"),
323 Name: proptools.StringPtr("cache"),
324 },
Cole Faust0d467052024-12-04 17:19:19 -0800325 // For Treble Generic System Image (GSI), system-as-root GSI needs to work on
326 // both devices with and without /odm_dlkm partition. Those symlinks are for
327 // devices without /odm_dlkm partition. For devices with /odm_dlkm
328 // partition, mount odm_dlkm.img under /odm_dlkm will hide those symlinks.
329 // Note that /odm_dlkm/lib is omitted because odm DLKMs should be accessed
330 // via /odm/lib/modules directly. All of this also applies to the vendor_dlkm symlink
331 filesystem.SymlinkDefinition{
332 Target: proptools.StringPtr("/odm/odm_dlkm/etc"),
333 Name: proptools.StringPtr("odm_dlkm/etc"),
334 },
335 filesystem.SymlinkDefinition{
336 Target: proptools.StringPtr("/vendor/vendor_dlkm/etc"),
337 Name: proptools.StringPtr("vendor_dlkm/etc"),
338 },
mrziwang9afc2982024-11-05 14:29:48 -0800339 }
Cole Faust1d4e76c2024-11-26 14:15:29 -0800340 fsProps.Dirs = proptools.NewSimpleConfigurable([]string{
341 // From generic_rootdirs in build/make/target/product/generic/Android.bp
342 "acct",
343 "apex",
344 "bootstrap-apex",
345 "config",
346 "data",
347 "data_mirror",
348 "debug_ramdisk",
349 "dev",
350 "linkerconfig",
351 "metadata",
352 "mnt",
353 "odm",
354 "odm_dlkm",
355 "oem",
356 "postinstall",
357 "proc",
358 "second_stage_resources",
359 "storage",
360 "sys",
361 "system",
362 "system_dlkm",
363 "tmp",
364 "vendor",
365 "vendor_dlkm",
366
367 // from android_rootdirs in build/make/target/product/generic/Android.bp
368 "system_ext",
369 "product",
370 })
Spandan Dasa8fa6b42024-10-23 00:45:29 +0000371 case "system_ext":
Spandan Das2b4bf4c2024-12-02 19:41:04 +0000372 if partitionVars.ProductFsverityGenerateMetadata {
373 fsProps.Fsverity.Inputs = []string{
374 "framework/*",
375 "framework/*/*", // framework/{arch}
376 "framework/oat/*/*", // framework/oat/{arch}
377 }
378 fsProps.Fsverity.Libs = []string{":framework-res{.export-package.apk}"}
Spandan Dasa8fa6b42024-10-23 00:45:29 +0000379 }
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000380 case "product":
381 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
Spandan Das71be42d2024-11-20 18:34:16 +0000382 fsProps.Android_filesystem_deps.System = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system"))
383 if ctx.DeviceConfig().SystemExtPath() == "system_ext" {
384 fsProps.Android_filesystem_deps.System_ext = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_ext"))
385 }
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000386 case "vendor":
387 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
Spandan Das69464c32024-10-25 20:08:06 +0000388 fsProps.Symlinks = []filesystem.SymlinkDefinition{
389 filesystem.SymlinkDefinition{
390 Target: proptools.StringPtr("/odm"),
391 Name: proptools.StringPtr("vendor/odm"),
392 },
393 filesystem.SymlinkDefinition{
394 Target: proptools.StringPtr("/vendor_dlkm/lib/modules"),
395 Name: proptools.StringPtr("vendor/lib/modules"),
396 },
397 }
Spandan Das71be42d2024-11-20 18:34:16 +0000398 fsProps.Android_filesystem_deps.System = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system"))
399 if ctx.DeviceConfig().SystemExtPath() == "system_ext" {
400 fsProps.Android_filesystem_deps.System_ext = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_ext"))
401 }
Spandan Dasc5717162024-11-01 18:33:57 +0000402 case "odm":
403 fsProps.Symlinks = []filesystem.SymlinkDefinition{
404 filesystem.SymlinkDefinition{
405 Target: proptools.StringPtr("/odm_dlkm/lib/modules"),
406 Name: proptools.StringPtr("odm/lib/modules"),
407 },
408 }
mrziwang23ba8762024-11-07 16:21:53 -0800409 case "userdata":
410 fsProps.Base_dir = proptools.StringPtr("data")
Jihoon Kangd098d442024-11-19 00:03:22 +0000411 case "ramdisk":
412 // Following the logic in https://cs.android.com/android/platform/superproject/main/+/c3c5063df32748a8806ce5da5dd0db158eab9ad9:build/make/core/Makefile;l=1307
413 fsProps.Dirs = android.NewSimpleConfigurable([]string{
414 "debug_ramdisk",
415 "dev",
416 "metadata",
417 "mnt",
418 "proc",
419 "second_stage_resources",
420 "sys",
421 })
422 if partitionVars.BoardUsesGenericKernelImage {
423 fsProps.Dirs.AppendSimpleValue([]string{
424 "first_stage_ramdisk/debug_ramdisk",
425 "first_stage_ramdisk/dev",
426 "first_stage_ramdisk/metadata",
427 "first_stage_ramdisk/mnt",
428 "first_stage_ramdisk/proc",
429 "first_stage_ramdisk/second_stage_resources",
430 "first_stage_ramdisk/sys",
431 })
432 }
Jihoon Kang9007f382024-12-04 00:43:52 +0000433 case "recovery":
434 // Following https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=2826;drc=ad7cfb56010cb22c3aa0e70cf71c804352553526
435 fsProps.Dirs = android.NewSimpleConfigurable([]string{
436 "sdcard",
437 "tmp",
438 })
439 fsProps.Symlinks = []filesystem.SymlinkDefinition{
440 {
441 Target: proptools.StringPtr("/system/bin/init"),
442 Name: proptools.StringPtr("init"),
443 },
444 {
445 Target: proptools.StringPtr("prop.default"),
446 Name: proptools.StringPtr("default.prop"),
447 },
448 }
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000449 }
450}
Spandan Dascbe641a2024-10-14 21:07:34 +0000451
Spandan Das5b493cd2024-11-07 20:55:56 +0000452var (
453 dlkmPartitions = []string{
454 "system_dlkm",
455 "vendor_dlkm",
456 "odm_dlkm",
457 }
458)
459
Cole Faust92ccbe22024-10-03 14:38:37 -0700460// Creates a soong module to build the given partition. Returns false if we can't support building
461// it.
462func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partitionType string) bool {
mrziwang4b0ca972024-10-17 14:56:19 -0700463 baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)))
464
465 fsProps, supported := generateFsProps(ctx, partitionType)
466 if !supported {
467 return false
mrziwanga077b942024-10-16 16:00:06 -0700468 }
mrziwanga077b942024-10-16 16:00:06 -0700469
Cole Faust7db05752024-11-21 13:30:41 -0800470 if partitionType == "vendor" || partitionType == "product" || partitionType == "system" {
Spandan Das2047a4c2024-11-11 21:24:58 +0000471 fsProps.Linker_config.Gen_linker_config = proptools.BoolPtr(true)
Cole Faust7db05752024-11-21 13:30:41 -0800472 if partitionType != "system" {
473 fsProps.Linker_config.Linker_config_srcs = f.createLinkerConfigSourceFilegroups(ctx, partitionType)
474 }
Spandan Das312cc412024-10-29 18:20:11 +0000475 }
476
Jihoon Kanga8fa0712024-11-26 23:11:07 +0000477 if android.InList(partitionType, append(dlkmPartitions, "vendor_ramdisk")) {
Spandan Das5b493cd2024-11-07 20:55:56 +0000478 f.createPrebuiltKernelModules(ctx, partitionType)
Spandan Das5e336422024-11-01 22:31:20 +0000479 }
480
mrziwang4b0ca972024-10-17 14:56:19 -0700481 var module android.Module
482 if partitionType == "system" {
483 module = ctx.CreateModule(filesystem.SystemImageFactory, baseProps, fsProps)
484 } else {
485 // Explicitly set the partition.
486 fsProps.Partition_type = proptools.StringPtr(partitionType)
487 module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps)
488 }
489 module.HideFromMake()
Spandan Das168098c2024-10-28 19:44:34 +0000490 if partitionType == "vendor" {
Spandan Das4cd93b52024-11-05 23:27:03 +0000491 f.createVendorBuildProp(ctx)
Spandan Das168098c2024-10-28 19:44:34 +0000492 }
mrziwang4b0ca972024-10-17 14:56:19 -0700493 return true
494}
495
Cole Faust953476f2024-11-14 14:11:29 -0800496// Creates filegroups for the files specified in BOARD_(partition_)AVB_KEY_PATH
497func (f *filesystemCreator) createAvbKeyFilegroups(ctx android.LoadHookContext) {
498 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
499 var files []string
500
501 if len(partitionVars.BoardAvbKeyPath) > 0 {
502 files = append(files, partitionVars.BoardAvbKeyPath)
503 }
504 for _, partition := range android.SortedKeys(partitionVars.PartitionQualifiedVariables) {
505 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partition]
506 if len(specificPartitionVars.BoardAvbKeyPath) > 0 {
507 files = append(files, specificPartitionVars.BoardAvbKeyPath)
508 }
509 }
510
511 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
512 for _, file := range files {
513 if _, ok := fsGenState.avbKeyFilegroups[file]; ok {
514 continue
515 }
516 if file == "external/avb/test/data/testkey_rsa4096.pem" {
517 // There already exists a checked-in filegroup for this commonly-used key, just use that
518 fsGenState.avbKeyFilegroups[file] = "avb_testkey_rsa4096"
519 continue
520 }
521 dir := filepath.Dir(file)
522 base := filepath.Base(file)
523 name := fmt.Sprintf("avb_key_%x", strings.ReplaceAll(file, "/", "_"))
524 ctx.CreateModuleInDirectory(
525 android.FileGroupFactory,
526 dir,
527 &struct {
528 Name *string
529 Srcs []string
530 Visibility []string
531 }{
532 Name: proptools.StringPtr(name),
533 Srcs: []string{base},
534 Visibility: []string{"//visibility:public"},
535 },
536 )
537 fsGenState.avbKeyFilegroups[file] = name
538 }
539}
540
Cole Faust3e730972024-12-03 13:12:08 -0800541// Creates filegroups for miscellaneous other files
542func (f *filesystemCreator) createMiscFilegroups(ctx android.LoadHookContext) {
543 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
544
545 if partitionVars.BoardErofsCompressorHints != "" {
546 dir := filepath.Dir(partitionVars.BoardErofsCompressorHints)
547 base := filepath.Base(partitionVars.BoardErofsCompressorHints)
548 ctx.CreateModuleInDirectory(
549 android.FileGroupFactory,
550 dir,
551 &struct {
552 Name *string
553 Srcs []string
554 Visibility []string
555 }{
556 Name: proptools.StringPtr("soong_generated_board_erofs_compress_hints_filegroup"),
557 Srcs: []string{base},
558 Visibility: []string{"//visibility:public"},
559 },
560 )
561 }
562}
563
Spandan Das5e336422024-11-01 22:31:20 +0000564// createPrebuiltKernelModules creates `prebuilt_kernel_modules`. These modules will be added to deps of the
Spandan Das7b25a512024-11-06 20:41:26 +0000565// autogenerated *_dlkm filsystem modules. Each _dlkm partition should have a single prebuilt_kernel_modules dependency.
566// This ensures that the depmod artifacts (modules.* installed in /lib/modules/) are generated with a complete view.
Spandan Das5b493cd2024-11-07 20:55:56 +0000567func (f *filesystemCreator) createPrebuiltKernelModules(ctx android.LoadHookContext, partitionType string) {
Spandan Das5e336422024-11-01 22:31:20 +0000568 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
Spandan Das7b25a512024-11-06 20:41:26 +0000569 name := generatedModuleName(ctx.Config(), fmt.Sprintf("%s-kernel-modules", partitionType))
570 props := &struct {
Spandan Das912d26b2024-11-06 19:35:17 +0000571 Name *string
572 Srcs []string
Spandan Das5b493cd2024-11-07 20:55:56 +0000573 System_deps []string
Spandan Das912d26b2024-11-06 19:35:17 +0000574 System_dlkm_specific *bool
Spandan Das5b493cd2024-11-07 20:55:56 +0000575 Vendor_dlkm_specific *bool
576 Odm_dlkm_specific *bool
Jihoon Kanga8fa0712024-11-26 23:11:07 +0000577 Vendor_ramdisk *bool
Spandan Das5b493cd2024-11-07 20:55:56 +0000578 Load_by_default *bool
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000579 Blocklist_file *string
Jihoon Kang72dd6fc2024-11-27 01:16:39 +0000580 Options_file *string
Spandan Das7b25a512024-11-06 20:41:26 +0000581 }{
582 Name: proptools.StringPtr(name),
Spandan Das5e336422024-11-01 22:31:20 +0000583 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000584 switch partitionType {
585 case "system_dlkm":
Spandan Das59ee5d72024-11-18 19:36:32 +0000586 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelModules).Strings()
Spandan Das912d26b2024-11-06 19:35:17 +0000587 props.System_dlkm_specific = proptools.BoolPtr(true)
Spandan Das5b493cd2024-11-07 20:55:56 +0000588 if len(ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelLoadModules) == 0 {
589 // Create empty modules.load file for system
590 // https://source.corp.google.com/h/googleplex-android/platform/build/+/ef55daac9954896161b26db4f3ef1781b5a5694c:core/Makefile;l=695-700;drc=549fe2a5162548bd8b47867d35f907eb22332023;bpv=1;bpt=0
591 props.Load_by_default = proptools.BoolPtr(false)
592 }
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000593 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelBlocklistFile; blocklistFile != "" {
594 props.Blocklist_file = proptools.StringPtr(blocklistFile)
595 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000596 case "vendor_dlkm":
Spandan Das59ee5d72024-11-18 19:36:32 +0000597 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorKernelModules).Strings()
Spandan Das5b493cd2024-11-07 20:55:56 +0000598 if len(ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelModules) > 0 {
599 props.System_deps = []string{":" + generatedModuleName(ctx.Config(), "system_dlkm-kernel-modules") + "{.modules}"}
600 }
601 props.Vendor_dlkm_specific = proptools.BoolPtr(true)
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000602 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorKernelBlocklistFile; blocklistFile != "" {
603 props.Blocklist_file = proptools.StringPtr(blocklistFile)
604 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000605 case "odm_dlkm":
Spandan Das59ee5d72024-11-18 19:36:32 +0000606 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.OdmKernelModules).Strings()
Spandan Das5b493cd2024-11-07 20:55:56 +0000607 props.Odm_dlkm_specific = proptools.BoolPtr(true)
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000608 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.OdmKernelBlocklistFile; blocklistFile != "" {
609 props.Blocklist_file = proptools.StringPtr(blocklistFile)
610 }
Jihoon Kanga8fa0712024-11-26 23:11:07 +0000611 case "vendor_ramdisk":
612 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorRamdiskKernelModules).Strings()
613 props.Vendor_ramdisk = proptools.BoolPtr(true)
614 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorRamdiskKernelBlocklistFile; blocklistFile != "" {
615 props.Blocklist_file = proptools.StringPtr(blocklistFile)
616 }
Jihoon Kang72dd6fc2024-11-27 01:16:39 +0000617 if optionsFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorRamdiskKernelOptionsFile; optionsFile != "" {
618 props.Options_file = proptools.StringPtr(optionsFile)
619 }
620
Spandan Das5b493cd2024-11-07 20:55:56 +0000621 default:
622 ctx.ModuleErrorf("DLKM is not supported for %s\n", partitionType)
Spandan Das912d26b2024-11-06 19:35:17 +0000623 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000624
625 if len(props.Srcs) == 0 {
626 return // do not generate `prebuilt_kernel_modules` if there are no sources
627 }
628
Spandan Das7b25a512024-11-06 20:41:26 +0000629 kernelModule := ctx.CreateModuleInDirectory(
630 kernel.PrebuiltKernelModulesFactory,
631 ".", // create in root directory for now
632 props,
633 )
634 kernelModule.HideFromMake()
635 // Add to deps
636 (*fsGenState.fsDeps[partitionType])[name] = defaultDepCandidateProps(ctx.Config())
Spandan Das5e336422024-11-01 22:31:20 +0000637}
638
Spandan Das4cd93b52024-11-05 23:27:03 +0000639// Create a build_prop and android_info module. This will be used to create /vendor/build.prop
640func (f *filesystemCreator) createVendorBuildProp(ctx android.LoadHookContext) {
641 // Create a android_info for vendor
642 // The board info files might be in a directory outside the root soong namespace, so create
643 // the module in "."
644 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
645 androidInfoProps := &struct {
646 Name *string
647 Board_info_files []string
648 Bootloader_board_name *string
649 }{
650 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "android-info.prop")),
651 Board_info_files: partitionVars.BoardInfoFiles,
652 }
653 if len(androidInfoProps.Board_info_files) == 0 {
654 androidInfoProps.Bootloader_board_name = proptools.StringPtr(partitionVars.BootLoaderBoardName)
655 }
656 androidInfoProp := ctx.CreateModuleInDirectory(
657 android.AndroidInfoFactory,
658 ".",
659 androidInfoProps,
660 )
661 androidInfoProp.HideFromMake()
662 // Create a build prop for vendor
663 vendorBuildProps := &struct {
664 Name *string
665 Vendor *bool
666 Stem *string
667 Product_config *string
668 Android_info *string
669 }{
670 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "vendor-build.prop")),
671 Vendor: proptools.BoolPtr(true),
672 Stem: proptools.StringPtr("build.prop"),
673 Product_config: proptools.StringPtr(":product_config"),
674 Android_info: proptools.StringPtr(":" + androidInfoProp.Name()),
675 }
676 vendorBuildProp := ctx.CreateModule(
677 android.BuildPropFactory,
678 vendorBuildProps,
679 )
680 vendorBuildProp.HideFromMake()
681}
682
Spandan Das8fe68dc2024-10-29 18:20:11 +0000683// createLinkerConfigSourceFilegroups creates filegroup modules to generate linker.config.pb for the following partitions
684// 1. vendor: Using PRODUCT_VENDOR_LINKER_CONFIG_FRAGMENTS (space separated file list)
685// 1. product: Using PRODUCT_PRODUCT_LINKER_CONFIG_FRAGMENTS (space separated file list)
686// It creates a filegroup for each file in the fragment list
Spandan Das312cc412024-10-29 18:20:11 +0000687// The filegroup modules are then added to `linker_config_srcs` of the autogenerated vendor `android_filesystem`.
Spandan Das8fe68dc2024-10-29 18:20:11 +0000688func (f *filesystemCreator) createLinkerConfigSourceFilegroups(ctx android.LoadHookContext, partitionType string) []string {
Spandan Das312cc412024-10-29 18:20:11 +0000689 ret := []string{}
690 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Spandan Das8fe68dc2024-10-29 18:20:11 +0000691 var linkerConfigSrcs []string
692 if partitionType == "vendor" {
693 linkerConfigSrcs = android.FirstUniqueStrings(partitionVars.VendorLinkerConfigSrcs)
694 } else if partitionType == "product" {
695 linkerConfigSrcs = android.FirstUniqueStrings(partitionVars.ProductLinkerConfigSrcs)
696 } else {
697 ctx.ModuleErrorf("linker.config.pb is only supported for vendor and product partitions. For system partition, use `android_system_image`")
698 }
699
700 if len(linkerConfigSrcs) > 0 {
Spandan Das312cc412024-10-29 18:20:11 +0000701 // Create a filegroup, and add `:<filegroup_name>` to ret.
702 for index, linkerConfigSrc := range linkerConfigSrcs {
703 dir := filepath.Dir(linkerConfigSrc)
704 base := filepath.Base(linkerConfigSrc)
Spandan Das8fe68dc2024-10-29 18:20:11 +0000705 fgName := generatedModuleName(ctx.Config(), fmt.Sprintf("%s-linker-config-src%s", partitionType, strconv.Itoa(index)))
Spandan Das312cc412024-10-29 18:20:11 +0000706 srcs := []string{base}
707 fgProps := &struct {
708 Name *string
709 Srcs proptools.Configurable[[]string]
710 }{
711 Name: proptools.StringPtr(fgName),
712 Srcs: proptools.NewSimpleConfigurable(srcs),
713 }
714 ctx.CreateModuleInDirectory(
715 android.FileGroupFactory,
716 dir,
717 fgProps,
718 )
719 ret = append(ret, ":"+fgName)
720 }
721 }
722 return ret
723}
724
mrziwang4b0ca972024-10-17 14:56:19 -0700725type filesystemBaseProperty struct {
726 Name *string
727 Compile_multilib *string
Cole Faust3552eb62024-11-06 18:07:26 -0800728 Visibility []string
mrziwang4b0ca972024-10-17 14:56:19 -0700729}
730
731func generateBaseProps(namePtr *string) *filesystemBaseProperty {
732 return &filesystemBaseProperty{
733 Name: namePtr,
734 Compile_multilib: proptools.StringPtr("both"),
Cole Faust3552eb62024-11-06 18:07:26 -0800735 // The vbmeta modules are currently in the root directory and depend on the partitions
736 Visibility: []string{"//.", "//build/soong:__subpackages__"},
mrziwang4b0ca972024-10-17 14:56:19 -0700737 }
738}
739
740func generateFsProps(ctx android.EarlyModuleContext, partitionType string) (*filesystem.FilesystemProperties, bool) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700741 fsProps := &filesystem.FilesystemProperties{}
742
mrziwang4b0ca972024-10-17 14:56:19 -0700743 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Cole Faust0c4b4152024-11-20 16:42:53 -0800744 var avbInfo avbInfo
Cole Faust76a6e952024-11-07 16:56:45 -0800745 var fsType string
746 if strings.Contains(partitionType, "ramdisk") {
747 fsType = "compressed_cpio"
748 } else {
Cole Faust953476f2024-11-14 14:11:29 -0800749 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
Cole Faust76a6e952024-11-07 16:56:45 -0800750 fsType = specificPartitionVars.BoardFileSystemType
Cole Faust0c4b4152024-11-20 16:42:53 -0800751 avbInfo = getAvbInfo(ctx.Config(), partitionType)
Cole Faust953476f2024-11-14 14:11:29 -0800752 if fsType == "" {
753 fsType = "ext4" //default
754 }
Cole Faust76a6e952024-11-07 16:56:45 -0800755 }
Cole Faust76a6e952024-11-07 16:56:45 -0800756
mrziwang4b0ca972024-10-17 14:56:19 -0700757 fsProps.Type = proptools.StringPtr(fsType)
758 if filesystem.GetFsTypeFromString(ctx, *fsProps.Type).IsUnknown() {
759 // Currently the android_filesystem module type only supports a handful of FS types like ext4, erofs
760 return nil, false
761 }
762
Cole Faust3e730972024-12-03 13:12:08 -0800763 if *fsProps.Type == "erofs" {
764 if partitionVars.BoardErofsCompressor != "" {
765 fsProps.Erofs.Compressor = proptools.StringPtr(partitionVars.BoardErofsCompressor)
766 }
767 if partitionVars.BoardErofsCompressorHints != "" {
768 fsProps.Erofs.Compress_hints = proptools.StringPtr(":soong_generated_board_erofs_compress_hints_filegroup")
769 }
770 }
771
Cole Faust92ccbe22024-10-03 14:38:37 -0700772 // Don't build this module on checkbuilds, the soong-built partitions are still in-progress
773 // and sometimes don't build.
774 fsProps.Unchecked_module = proptools.BoolPtr(true)
775
Jihoon Kang98047cf2024-10-02 17:13:54 +0000776 // BOARD_AVB_ENABLE
Cole Faust0c4b4152024-11-20 16:42:53 -0800777 fsProps.Use_avb = avbInfo.avbEnable
Jihoon Kang98047cf2024-10-02 17:13:54 +0000778 // BOARD_AVB_KEY_PATH
Cole Faust0c4b4152024-11-20 16:42:53 -0800779 fsProps.Avb_private_key = avbInfo.avbkeyFilegroup
Jihoon Kang98047cf2024-10-02 17:13:54 +0000780 // BOARD_AVB_ALGORITHM
Cole Faust0c4b4152024-11-20 16:42:53 -0800781 fsProps.Avb_algorithm = avbInfo.avbAlgorithm
Jihoon Kang98047cf2024-10-02 17:13:54 +0000782 // BOARD_AVB_SYSTEM_ROLLBACK_INDEX
Cole Faust0c4b4152024-11-20 16:42:53 -0800783 fsProps.Rollback_index = avbInfo.avbRollbackIndex
Cole Fauste1676122024-12-03 17:32:25 -0800784 fsProps.Avb_hash_algorithm = avbInfo.avbHashAlgorithm
Jihoon Kang98047cf2024-10-02 17:13:54 +0000785
Cole Faust92ccbe22024-10-03 14:38:37 -0700786 fsProps.Partition_name = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000787
Cole Faust0d467052024-12-04 17:19:19 -0800788 switch partitionType {
789 // The partitions that support file_contexts came from here:
790 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=2270;drc=ad7cfb56010cb22c3aa0e70cf71c804352553526
791 case "system", "userdata", "cache", "vendor", "product", "system_ext", "odm", "vendor_dlkm", "odm_dlkm", "system_dlkm", "oem":
792 fsProps.Precompiled_file_contexts = proptools.StringPtr(":file_contexts_bin_gen")
793 }
794
Cole Faust68382192024-11-19 10:36:03 -0800795 if !strings.Contains(partitionType, "ramdisk") {
796 fsProps.Base_dir = proptools.StringPtr(partitionType)
797 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000798
Jihoon Kang0d545b82024-10-11 00:21:57 +0000799 fsProps.Is_auto_generated = proptools.BoolPtr(true)
800
Spandan Das71be42d2024-11-20 18:34:16 +0000801 partitionSpecificFsProps(ctx, fsProps, partitionVars, partitionType)
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000802
mrziwang4b0ca972024-10-17 14:56:19 -0700803 return fsProps, true
Cole Faust92ccbe22024-10-03 14:38:37 -0700804}
805
Cole Faust0c4b4152024-11-20 16:42:53 -0800806type avbInfo struct {
807 avbEnable *bool
808 avbKeyPath *string
809 avbkeyFilegroup *string
810 avbAlgorithm *string
811 avbRollbackIndex *int64
812 avbMode *string
Cole Fauste1676122024-12-03 17:32:25 -0800813 avbHashAlgorithm *string
Cole Faust0c4b4152024-11-20 16:42:53 -0800814}
815
816func getAvbInfo(config android.Config, partitionType string) avbInfo {
817 partitionVars := config.ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
818 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
819 var result avbInfo
820 boardAvbEnable := partitionVars.BoardAvbEnable
821 if boardAvbEnable {
822 result.avbEnable = proptools.BoolPtr(true)
Cole Fauste1676122024-12-03 17:32:25 -0800823 // There are "global" and "specific" copies of a lot of these variables. Sometimes they
824 // choose the specific and then fall back to the global one if it's not set, other times
825 // the global one actually only applies to the vbmeta partition.
826 if partitionType == "vbmeta" {
827 if partitionVars.BoardAvbKeyPath != "" {
828 result.avbKeyPath = proptools.StringPtr(partitionVars.BoardAvbKeyPath)
829 }
830 if partitionVars.BoardAvbRollbackIndex != "" {
831 parsed, err := strconv.ParseInt(partitionVars.BoardAvbRollbackIndex, 10, 64)
832 if err != nil {
833 panic(fmt.Sprintf("Rollback index must be an int, got %s", partitionVars.BoardAvbRollbackIndex))
834 }
835 result.avbRollbackIndex = &parsed
836 }
837 }
Cole Faust0c4b4152024-11-20 16:42:53 -0800838 if specificPartitionVars.BoardAvbKeyPath != "" {
839 result.avbKeyPath = proptools.StringPtr(specificPartitionVars.BoardAvbKeyPath)
Cole Faust0c4b4152024-11-20 16:42:53 -0800840 }
841 if specificPartitionVars.BoardAvbAlgorithm != "" {
842 result.avbAlgorithm = proptools.StringPtr(specificPartitionVars.BoardAvbAlgorithm)
843 } else if partitionVars.BoardAvbAlgorithm != "" {
844 result.avbAlgorithm = proptools.StringPtr(partitionVars.BoardAvbAlgorithm)
845 }
846 if specificPartitionVars.BoardAvbRollbackIndex != "" {
847 parsed, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64)
848 if err != nil {
849 panic(fmt.Sprintf("Rollback index must be an int, got %s", specificPartitionVars.BoardAvbRollbackIndex))
850 }
851 result.avbRollbackIndex = &parsed
Cole Fauste1676122024-12-03 17:32:25 -0800852 }
853 if specificPartitionVars.BoardAvbRollbackIndex != "" {
854 parsed, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64)
Cole Faust0c4b4152024-11-20 16:42:53 -0800855 if err != nil {
Cole Fauste1676122024-12-03 17:32:25 -0800856 panic(fmt.Sprintf("Rollback index must be an int, got %s", specificPartitionVars.BoardAvbRollbackIndex))
Cole Faust0c4b4152024-11-20 16:42:53 -0800857 }
858 result.avbRollbackIndex = &parsed
859 }
Cole Fauste1676122024-12-03 17:32:25 -0800860
861 // Make allows you to pass arbitrary arguments to avbtool via this variable, but in practice
862 // it's only used for --hash_algorithm. The soong module has a dedicated property for the
863 // hashtree algorithm, and doesn't allow custom arguments, so just extract the hashtree
864 // algorithm out of the arbitrary arguments.
865 addHashtreeFooterArgs := strings.Split(specificPartitionVars.BoardAvbAddHashtreeFooterArgs, " ")
866 if i := slices.Index(addHashtreeFooterArgs, "--hash_algorithm"); i >= 0 {
867 result.avbHashAlgorithm = &addHashtreeFooterArgs[i+1]
868 }
869
Cole Faust0c4b4152024-11-20 16:42:53 -0800870 result.avbMode = proptools.StringPtr("make_legacy")
871 }
872 if result.avbKeyPath != nil {
873 fsGenState := config.Get(fsGenStateOnceKey).(*FsGenState)
874 filegroup := fsGenState.avbKeyFilegroups[*result.avbKeyPath]
875 result.avbkeyFilegroup = proptools.StringPtr(":" + filegroup)
876 }
877 return result
878}
879
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800880func (f *filesystemCreator) createFileListDiffTest(ctx android.ModuleContext, partitionType string) android.Path {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000881 partitionModuleName := generatedModuleNameForPartition(ctx.Config(), partitionType)
Cole Faust92ccbe22024-10-03 14:38:37 -0700882 systemImage := ctx.GetDirectDepWithTag(partitionModuleName, generatedFilesystemDepTag)
883 filesystemInfo, ok := android.OtherModuleProvider(ctx, systemImage, filesystem.FilesystemProvider)
884 if !ok {
885 ctx.ModuleErrorf("Expected module %s to provide FileysystemInfo", partitionModuleName)
886 }
887 makeFileList := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partitionType))
Jihoon Kang9e866c82024-10-07 22:39:18 +0000888 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700889
890 builder := android.NewRuleBuilder(pctx, ctx)
891 builder.Command().BuiltTool("file_list_diff").
892 Input(makeFileList).
893 Input(filesystemInfo.FileListFile).
Cole Faust56301572024-11-07 15:22:42 -0800894 Text(partitionModuleName)
Cole Faust92ccbe22024-10-03 14:38:37 -0700895 builder.Command().Text("touch").Output(diffTestResultFile)
896 builder.Build(partitionModuleName+" diff test", partitionModuleName+" diff test")
897 return diffTestResultFile
898}
899
900func createFailingCommand(ctx android.ModuleContext, message string) android.Path {
901 hasher := sha256.New()
902 hasher.Write([]byte(message))
903 filename := fmt.Sprintf("failing_command_%x.txt", hasher.Sum(nil))
904 file := android.PathForModuleOut(ctx, filename)
905 builder := android.NewRuleBuilder(pctx, ctx)
906 builder.Command().Textf("echo %s", proptools.NinjaAndShellEscape(message))
907 builder.Command().Text("exit 1 #").Output(file)
908 builder.Build("failing command "+filename, "failing command "+filename)
909 return file
910}
911
Cole Faust3552eb62024-11-06 18:07:26 -0800912func createVbmetaDiff(ctx android.ModuleContext, vbmetaModuleName string, vbmetaPartitionName string) android.Path {
913 vbmetaModule := ctx.GetDirectDepWithTag(vbmetaModuleName, generatedVbmetaPartitionDepTag)
914 outputFilesProvider, ok := android.OtherModuleProvider(ctx, vbmetaModule, android.OutputFilesProvider)
915 if !ok {
916 ctx.ModuleErrorf("Expected module %s to provide OutputFiles", vbmetaModule)
917 }
918 if len(outputFilesProvider.DefaultOutputFiles) != 1 {
919 ctx.ModuleErrorf("Expected 1 output file from module %s", vbmetaModule)
920 }
921 soongVbMetaFile := outputFilesProvider.DefaultOutputFiles[0]
922 makeVbmetaFile := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/%s.img", ctx.Config().DeviceName(), vbmetaPartitionName))
923
924 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", vbmetaModuleName))
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800925 createDiffTest(ctx, diffTestResultFile, soongVbMetaFile, makeVbmetaFile)
926 return diffTestResultFile
927}
928
929func createDiffTest(ctx android.ModuleContext, diffTestResultFile android.WritablePath, file1 android.Path, file2 android.Path) {
Cole Faust3552eb62024-11-06 18:07:26 -0800930 builder := android.NewRuleBuilder(pctx, ctx)
931 builder.Command().Text("diff").
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800932 Input(file1).
933 Input(file2)
Cole Faust3552eb62024-11-06 18:07:26 -0800934 builder.Command().Text("touch").Output(diffTestResultFile)
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800935 builder.Build("diff test "+diffTestResultFile.String(), "diff test")
Cole Faust3552eb62024-11-06 18:07:26 -0800936}
937
Cole Faust92ccbe22024-10-03 14:38:37 -0700938type systemImageDepTagType struct {
939 blueprint.BaseDependencyTag
940}
941
942var generatedFilesystemDepTag systemImageDepTagType
Cole Faust3552eb62024-11-06 18:07:26 -0800943var generatedVbmetaPartitionDepTag systemImageDepTagType
Cole Faust92ccbe22024-10-03 14:38:37 -0700944
945func (f *filesystemCreator) DepsMutator(ctx android.BottomUpMutatorContext) {
946 for _, partitionType := range f.properties.Generated_partition_types {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000947 ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, generatedModuleNameForPartition(ctx.Config(), partitionType))
Cole Faust92ccbe22024-10-03 14:38:37 -0700948 }
Cole Faust3552eb62024-11-06 18:07:26 -0800949 for _, vbmetaModule := range f.properties.Vbmeta_module_names {
950 ctx.AddDependency(ctx.Module(), generatedVbmetaPartitionDepTag, vbmetaModule)
951 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000952}
953
954func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700955 if ctx.ModuleDir() != "build/soong/fsgen" {
956 ctx.ModuleErrorf("There can only be one soong_filesystem_creator in build/soong/fsgen")
957 }
958 f.HideFromMake()
Jihoon Kang98047cf2024-10-02 17:13:54 +0000959
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000960 var content strings.Builder
961 generatedBp := android.PathForModuleOut(ctx, "soong_generated_product_config.bp")
962 for _, partition := range ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions {
963 content.WriteString(generateBpContent(ctx, partition))
964 content.WriteString("\n")
965 }
966 android.WriteFileRule(ctx, generatedBp, content.String())
967
mrziwang8f86c882024-10-03 12:34:33 -0700968 ctx.Phony("product_config_to_bp", generatedBp)
969
Cole Faust92ccbe22024-10-03 14:38:37 -0700970 var diffTestFiles []android.Path
971 for _, partitionType := range f.properties.Generated_partition_types {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800972 diffTestFile := f.createFileListDiffTest(ctx, partitionType)
Jihoon Kang72f812f2024-10-17 18:46:24 +0000973 diffTestFiles = append(diffTestFiles, diffTestFile)
974 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700975 }
976 for _, partitionType := range f.properties.Unsupported_partition_types {
Jihoon Kang72f812f2024-10-17 18:46:24 +0000977 diffTestFile := createFailingCommand(ctx, fmt.Sprintf("Couldn't build %s partition", partitionType))
978 diffTestFiles = append(diffTestFiles, diffTestFile)
979 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700980 }
Cole Faust3552eb62024-11-06 18:07:26 -0800981 for i, vbmetaModule := range f.properties.Vbmeta_module_names {
982 diffTestFile := createVbmetaDiff(ctx, vbmetaModule, f.properties.Vbmeta_partition_names[i])
983 diffTestFiles = append(diffTestFiles, diffTestFile)
984 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", f.properties.Vbmeta_partition_names[i]), diffTestFile)
985 }
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800986 if f.properties.Boot_image != "" {
987 diffTestFile := android.PathForModuleOut(ctx, "boot_diff_test.txt")
988 soongBootImg := android.PathForModuleSrc(ctx, f.properties.Boot_image)
989 makeBootImage := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/boot.img", ctx.Config().DeviceName()))
990 createDiffTest(ctx, diffTestFile, soongBootImg, makeBootImage)
991 diffTestFiles = append(diffTestFiles, diffTestFile)
992 ctx.Phony("soong_generated_boot_filesystem_test", diffTestFile)
993 }
Cole Faust24938e22024-11-18 14:01:58 -0800994 if f.properties.Vendor_boot_image != "" {
995 diffTestFile := android.PathForModuleOut(ctx, "vendor_boot_diff_test.txt")
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000996 soongBootImg := android.PathForModuleSrc(ctx, f.properties.Vendor_boot_image)
Cole Faust24938e22024-11-18 14:01:58 -0800997 makeBootImage := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/vendor_boot.img", ctx.Config().DeviceName()))
998 createDiffTest(ctx, diffTestFile, soongBootImg, makeBootImage)
999 diffTestFiles = append(diffTestFiles, diffTestFile)
1000 ctx.Phony("soong_generated_vendor_boot_filesystem_test", diffTestFile)
1001 }
Jihoon Kang95eb1da2024-11-19 20:55:20 +00001002 if f.properties.Init_boot_image != "" {
1003 diffTestFile := android.PathForModuleOut(ctx, "init_boot_diff_test.txt")
1004 soongBootImg := android.PathForModuleSrc(ctx, f.properties.Init_boot_image)
1005 makeBootImage := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/init_boot.img", ctx.Config().DeviceName()))
1006 createDiffTest(ctx, diffTestFile, soongBootImg, makeBootImage)
1007 diffTestFiles = append(diffTestFiles, diffTestFile)
1008 ctx.Phony("soong_generated_init_boot_filesystem_test", diffTestFile)
1009 }
Cole Faust92ccbe22024-10-03 14:38:37 -07001010 ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...)
Jihoon Kang98047cf2024-10-02 17:13:54 +00001011}
mrziwang8f86c882024-10-03 12:34:33 -07001012
mrziwang8f86c882024-10-03 12:34:33 -07001013func generateBpContent(ctx android.EarlyModuleContext, partitionType string) string {
mrziwang4b0ca972024-10-17 14:56:19 -07001014 fsProps, fsTypeSupported := generateFsProps(ctx, partitionType)
1015 if !fsTypeSupported {
1016 return ""
mrziwang8f86c882024-10-03 12:34:33 -07001017 }
1018
mrziwang4b0ca972024-10-17 14:56:19 -07001019 baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)))
Jihoon Kang0d7b0112024-11-13 20:44:05 +00001020 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
1021 deps := fsGenState.fsDeps[partitionType]
1022 highPriorityDeps := fsGenState.generatedPrebuiltEtcModuleNames
1023 depProps := generateDepStruct(*deps, highPriorityDeps)
mrziwang8f86c882024-10-03 12:34:33 -07001024
mrziwang4b0ca972024-10-17 14:56:19 -07001025 result, err := proptools.RepackProperties([]interface{}{baseProps, fsProps, depProps})
mrziwang8f86c882024-10-03 12:34:33 -07001026 if err != nil {
Cole Faustae3e1d32024-11-05 13:22:50 -08001027 ctx.ModuleErrorf("%s", err.Error())
1028 return ""
mrziwang8f86c882024-10-03 12:34:33 -07001029 }
1030
Jihoon Kang4e5d8de2024-10-19 01:59:58 +00001031 moduleType := "android_filesystem"
1032 if partitionType == "system" {
1033 moduleType = "android_system_image"
1034 }
1035
mrziwang8f86c882024-10-03 12:34:33 -07001036 file := &parser.File{
1037 Defs: []parser.Definition{
1038 &parser.Module{
Jihoon Kang4e5d8de2024-10-19 01:59:58 +00001039 Type: moduleType,
mrziwang8f86c882024-10-03 12:34:33 -07001040 Map: *result,
1041 },
1042 },
1043 }
1044 bytes, err := parser.Print(file)
1045 if err != nil {
1046 ctx.ModuleErrorf(err.Error())
1047 }
1048 return strings.TrimSpace(string(bytes))
1049}