blob: ec52f61dc843b9b623cea1a339dd6f4e06f4de70 [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"
Jihoon Kang98047cf2024-10-02 17:13:54 +000021 "strconv"
mrziwang8f86c882024-10-03 12:34:33 -070022 "strings"
mrziwang8f86c882024-10-03 12:34:33 -070023
24 "android/soong/android"
25 "android/soong/filesystem"
Spandan Das5e336422024-11-01 22:31:20 +000026 "android/soong/kernel"
Jihoon Kang98047cf2024-10-02 17:13:54 +000027
Cole Faust92ccbe22024-10-03 14:38:37 -070028 "github.com/google/blueprint"
mrziwang8f86c882024-10-03 12:34:33 -070029 "github.com/google/blueprint/parser"
Jihoon Kang98047cf2024-10-02 17:13:54 +000030 "github.com/google/blueprint/proptools"
31)
32
Cole Faust92ccbe22024-10-03 14:38:37 -070033var pctx = android.NewPackageContext("android/soong/fsgen")
34
Jihoon Kang98047cf2024-10-02 17:13:54 +000035func init() {
36 registerBuildComponents(android.InitRegistrationContext)
37}
38
39func registerBuildComponents(ctx android.RegistrationContext) {
40 ctx.RegisterModuleType("soong_filesystem_creator", filesystemCreatorFactory)
mrziwang8f86c882024-10-03 12:34:33 -070041 ctx.PreDepsMutators(RegisterCollectFileSystemDepsMutators)
42}
43
Cole Faust92ccbe22024-10-03 14:38:37 -070044type filesystemCreatorProps struct {
45 Generated_partition_types []string `blueprint:"mutated"`
46 Unsupported_partition_types []string `blueprint:"mutated"`
Cole Faust3552eb62024-11-06 18:07:26 -080047
48 Vbmeta_module_names []string `blueprint:"mutated"`
49 Vbmeta_partition_names []string `blueprint:"mutated"`
Cole Faustf2a6e8b2024-11-14 10:54:48 -080050
Cole Faust24938e22024-11-18 14:01:58 -080051 Boot_image string `blueprint:"mutated" android:"path_device_first"`
52 Vendor_boot_image string `blueprint:"mutated" android:"path_device_first"`
Jihoon Kang95eb1da2024-11-19 20:55:20 +000053 Init_boot_image string `blueprint:"mutated" android:"path_device_first"`
Cole Faust92ccbe22024-10-03 14:38:37 -070054}
55
Jihoon Kang98047cf2024-10-02 17:13:54 +000056type filesystemCreator struct {
57 android.ModuleBase
Cole Faust92ccbe22024-10-03 14:38:37 -070058
59 properties filesystemCreatorProps
Jihoon Kang98047cf2024-10-02 17:13:54 +000060}
61
62func filesystemCreatorFactory() android.Module {
63 module := &filesystemCreator{}
64
Cole Faust69788792024-10-10 11:00:36 -070065 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
Cole Faust92ccbe22024-10-03 14:38:37 -070066 module.AddProperties(&module.properties)
Jihoon Kang98047cf2024-10-02 17:13:54 +000067 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Jihoon Kang675d4682024-10-24 23:45:11 +000068 generatedPrebuiltEtcModuleNames := createPrebuiltEtcModules(ctx)
Jihoon Kang04f12c92024-11-12 23:03:08 +000069 avbpubkeyGenerated := createAvbpubkeyModule(ctx)
70 createFsGenState(ctx, generatedPrebuiltEtcModuleNames, avbpubkeyGenerated)
Cole Faust953476f2024-11-14 14:11:29 -080071 module.createAvbKeyFilegroups(ctx)
Jihoon Kang98047cf2024-10-02 17:13:54 +000072 module.createInternalModules(ctx)
73 })
74
75 return module
76}
77
Cole Faustf2a6e8b2024-11-14 10:54:48 -080078func generatedPartitions(ctx android.LoadHookContext) []string {
Cole Faust24938e22024-11-18 14:01:58 -080079 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Cole Faustf2a6e8b2024-11-14 10:54:48 -080080 generatedPartitions := []string{"system"}
81 if ctx.DeviceConfig().SystemExtPath() == "system_ext" {
82 generatedPartitions = append(generatedPartitions, "system_ext")
83 }
84 if ctx.DeviceConfig().BuildingVendorImage() && ctx.DeviceConfig().VendorPath() == "vendor" {
85 generatedPartitions = append(generatedPartitions, "vendor")
86 }
87 if ctx.DeviceConfig().BuildingProductImage() && ctx.DeviceConfig().ProductPath() == "product" {
88 generatedPartitions = append(generatedPartitions, "product")
89 }
90 if ctx.DeviceConfig().BuildingOdmImage() && ctx.DeviceConfig().OdmPath() == "odm" {
91 generatedPartitions = append(generatedPartitions, "odm")
92 }
93 if ctx.DeviceConfig().BuildingUserdataImage() && ctx.DeviceConfig().UserdataPath() == "data" {
94 generatedPartitions = append(generatedPartitions, "userdata")
95 }
Cole Faust24938e22024-11-18 14:01:58 -080096 if partitionVars.BuildingSystemDlkmImage {
Cole Faustf2a6e8b2024-11-14 10:54:48 -080097 generatedPartitions = append(generatedPartitions, "system_dlkm")
98 }
Cole Faust24938e22024-11-18 14:01:58 -080099 if partitionVars.BuildingVendorDlkmImage {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800100 generatedPartitions = append(generatedPartitions, "vendor_dlkm")
101 }
Cole Faust24938e22024-11-18 14:01:58 -0800102 if partitionVars.BuildingOdmDlkmImage {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800103 generatedPartitions = append(generatedPartitions, "odm_dlkm")
104 }
Cole Faust24938e22024-11-18 14:01:58 -0800105 if partitionVars.BuildingRamdiskImage {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800106 generatedPartitions = append(generatedPartitions, "ramdisk")
107 }
Cole Faust24938e22024-11-18 14:01:58 -0800108 if buildingVendorBootImage(partitionVars) {
109 generatedPartitions = append(generatedPartitions, "vendor_ramdisk")
110 }
Jihoon Kang3216c982024-12-02 19:42:20 +0000111 if ctx.DeviceConfig().BuildingRecoveryImage() && ctx.DeviceConfig().RecoveryPath() == "recovery" {
112 generatedPartitions = append(generatedPartitions, "recovery")
113 }
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800114 return generatedPartitions
115}
116
Jihoon Kang98047cf2024-10-02 17:13:54 +0000117func (f *filesystemCreator) createInternalModules(ctx android.LoadHookContext) {
Cole Faust3552eb62024-11-06 18:07:26 -0800118 soongGeneratedPartitions := generatedPartitions(ctx)
119 finalSoongGeneratedPartitions := make([]string, 0, len(soongGeneratedPartitions))
120 for _, partitionType := range soongGeneratedPartitions {
Cole Faust92ccbe22024-10-03 14:38:37 -0700121 if f.createPartition(ctx, partitionType) {
122 f.properties.Generated_partition_types = append(f.properties.Generated_partition_types, partitionType)
Cole Faust3552eb62024-11-06 18:07:26 -0800123 finalSoongGeneratedPartitions = append(finalSoongGeneratedPartitions, partitionType)
Cole Faust92ccbe22024-10-03 14:38:37 -0700124 } else {
125 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, partitionType)
126 }
127 }
Cole Faust3552eb62024-11-06 18:07:26 -0800128
Cole Faust24938e22024-11-18 14:01:58 -0800129 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Jihoon Kang70c1c682024-11-20 23:58:38 +0000130 dtbImg := createDtbImgFilegroup(ctx)
131
Cole Faust24938e22024-11-18 14:01:58 -0800132 if buildingBootImage(partitionVars) {
Jihoon Kang70c1c682024-11-20 23:58:38 +0000133 if createBootImage(ctx, dtbImg) {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800134 f.properties.Boot_image = ":" + generatedModuleNameForPartition(ctx.Config(), "boot")
135 } else {
136 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, "boot")
137 }
138 }
Cole Faust24938e22024-11-18 14:01:58 -0800139 if buildingVendorBootImage(partitionVars) {
Jihoon Kang70c1c682024-11-20 23:58:38 +0000140 if createVendorBootImage(ctx, dtbImg) {
Cole Faust24938e22024-11-18 14:01:58 -0800141 f.properties.Vendor_boot_image = ":" + generatedModuleNameForPartition(ctx.Config(), "vendor_boot")
142 } else {
143 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, "vendor_boot")
144 }
145 }
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000146 if buildingInitBootImage(partitionVars) {
147 if createInitBootImage(ctx) {
148 f.properties.Init_boot_image = ":" + generatedModuleNameForPartition(ctx.Config(), "init_boot")
149 } else {
150 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, "init_boot")
151 }
152 }
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800153
Cole Faust3552eb62024-11-06 18:07:26 -0800154 for _, x := range createVbmetaPartitions(ctx, finalSoongGeneratedPartitions) {
155 f.properties.Vbmeta_module_names = append(f.properties.Vbmeta_module_names, x.moduleName)
156 f.properties.Vbmeta_partition_names = append(f.properties.Vbmeta_partition_names, x.partitionName)
157 }
158
159 ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions = finalSoongGeneratedPartitions
160 f.createDeviceModule(ctx, finalSoongGeneratedPartitions, f.properties.Vbmeta_module_names)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000161}
162
Jihoon Kang0d545b82024-10-11 00:21:57 +0000163func generatedModuleName(cfg android.Config, suffix string) string {
Cole Faust92ccbe22024-10-03 14:38:37 -0700164 prefix := "soong"
165 if cfg.HasDeviceProduct() {
166 prefix = cfg.DeviceProduct()
167 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000168 return fmt.Sprintf("%s_generated_%s", prefix, suffix)
169}
170
Jihoon Kang0d545b82024-10-11 00:21:57 +0000171func generatedModuleNameForPartition(cfg android.Config, partitionType string) string {
172 return generatedModuleName(cfg, fmt.Sprintf("%s_image", partitionType))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000173}
174
Cole Faust3552eb62024-11-06 18:07:26 -0800175func (f *filesystemCreator) createDeviceModule(
176 ctx android.LoadHookContext,
177 generatedPartitionTypes []string,
178 vbmetaPartitions []string,
179) {
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000180 baseProps := &struct {
181 Name *string
182 }{
Jihoon Kang0d545b82024-10-11 00:21:57 +0000183 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "device")),
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000184 }
185
Priyanka Advani (xWF)dafaa7f2024-10-21 22:55:13 +0000186 // Currently, only the system and system_ext partition module is created.
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000187 partitionProps := &filesystem.PartitionNameProperties{}
Cole Faust3552eb62024-11-06 18:07:26 -0800188 if android.InList("system", generatedPartitionTypes) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000189 partitionProps.System_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system"))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000190 }
Cole Faust3552eb62024-11-06 18:07:26 -0800191 if android.InList("system_ext", generatedPartitionTypes) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000192 partitionProps.System_ext_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_ext"))
Spandan Das7a46f6c2024-10-14 18:41:18 +0000193 }
Cole Faust3552eb62024-11-06 18:07:26 -0800194 if android.InList("vendor", generatedPartitionTypes) {
Spandan Dase3b65312024-10-22 00:27:27 +0000195 partitionProps.Vendor_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "vendor"))
196 }
Cole Faust3552eb62024-11-06 18:07:26 -0800197 if android.InList("product", generatedPartitionTypes) {
Jihoon Kang6dd13b62024-10-22 23:21:02 +0000198 partitionProps.Product_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "product"))
199 }
Cole Faust3552eb62024-11-06 18:07:26 -0800200 if android.InList("odm", generatedPartitionTypes) {
Spandan Dasc5717162024-11-01 18:33:57 +0000201 partitionProps.Odm_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "odm"))
202 }
mrziwang23ba8762024-11-07 16:21:53 -0800203 if android.InList("userdata", f.properties.Generated_partition_types) {
204 partitionProps.Userdata_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "userdata"))
205 }
Cole Faust3552eb62024-11-06 18:07:26 -0800206 partitionProps.Vbmeta_partitions = vbmetaPartitions
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000207
208 ctx.CreateModule(filesystem.AndroidDeviceFactory, baseProps, partitionProps)
Cole Faust92ccbe22024-10-03 14:38:37 -0700209}
210
Spandan Das71be42d2024-11-20 18:34:16 +0000211func partitionSpecificFsProps(ctx android.EarlyModuleContext, fsProps *filesystem.FilesystemProperties, partitionVars android.PartitionVariables, partitionType string) {
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000212 switch partitionType {
213 case "system":
214 fsProps.Build_logtags = proptools.BoolPtr(true)
215 // https://source.corp.google.com/h/googleplex-android/platform/build//639d79f5012a6542ab1f733b0697db45761ab0f3:core/packaging/flags.mk;l=21;drc=5ba8a8b77507f93aa48cc61c5ba3f31a4d0cbf37;bpv=1;bpt=0
216 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
Justin Yuned3dbce2024-11-15 11:57:24 +0900217 // Identical to that of the aosp_shared_system_image
Spandan Das2b4bf4c2024-12-02 19:41:04 +0000218 if partitionVars.ProductFsverityGenerateMetadata {
219 fsProps.Fsverity.Inputs = []string{
220 "etc/boot-image.prof",
221 "etc/dirty-image-objects",
222 "etc/preloaded-classes",
223 "etc/classpaths/*.pb",
224 "framework/*",
225 "framework/*/*", // framework/{arch}
226 "framework/oat/*/*", // framework/oat/{arch}
227 }
228 fsProps.Fsverity.Libs = []string{":framework-res{.export-package.apk}"}
Spandan Dasa8fa6b42024-10-23 00:45:29 +0000229 }
Cole Faust1d4e76c2024-11-26 14:15:29 -0800230 // Most of the symlinks and directories listed here originate from create_root_structure.mk,
231 // but the handwritten generic system image also recreates them:
232 // 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 -0800233 // TODO(b/377734331): only generate the symlinks if the relevant partitions exist
234 fsProps.Symlinks = []filesystem.SymlinkDefinition{
235 filesystem.SymlinkDefinition{
Cole Faust1d4e76c2024-11-26 14:15:29 -0800236 Target: proptools.StringPtr("/system/bin/init"),
237 Name: proptools.StringPtr("init"),
238 },
239 filesystem.SymlinkDefinition{
240 Target: proptools.StringPtr("/system/etc"),
241 Name: proptools.StringPtr("etc"),
242 },
243 filesystem.SymlinkDefinition{
244 Target: proptools.StringPtr("/system/bin"),
245 Name: proptools.StringPtr("bin"),
246 },
247 filesystem.SymlinkDefinition{
248 Target: proptools.StringPtr("/data/user_de/0/com.android.shell/files/bugreports"),
249 Name: proptools.StringPtr("bugreports"),
250 },
251 filesystem.SymlinkDefinition{
252 Target: proptools.StringPtr("/sys/kernel/debug"),
253 Name: proptools.StringPtr("d"),
254 },
255 filesystem.SymlinkDefinition{
256 Target: proptools.StringPtr("/storage/self/primary"),
257 Name: proptools.StringPtr("sdcard"),
258 },
259 filesystem.SymlinkDefinition{
260 Target: proptools.StringPtr("/product/etc/security/adb_keys"),
261 Name: proptools.StringPtr("adb_keys"),
262 },
263 filesystem.SymlinkDefinition{
264 Target: proptools.StringPtr("/vendor/odm/app"),
265 Name: proptools.StringPtr("odm/app"),
266 },
267 filesystem.SymlinkDefinition{
268 Target: proptools.StringPtr("/vendor/odm/bin"),
269 Name: proptools.StringPtr("odm/bin"),
270 },
271 filesystem.SymlinkDefinition{
272 Target: proptools.StringPtr("/vendor/odm/etc"),
273 Name: proptools.StringPtr("odm/etc"),
274 },
275 filesystem.SymlinkDefinition{
276 Target: proptools.StringPtr("/vendor/odm/firmware"),
277 Name: proptools.StringPtr("odm/firmware"),
278 },
279 filesystem.SymlinkDefinition{
280 Target: proptools.StringPtr("/vendor/odm/framework"),
281 Name: proptools.StringPtr("odm/framework"),
282 },
283 filesystem.SymlinkDefinition{
284 Target: proptools.StringPtr("/vendor/odm/lib"),
285 Name: proptools.StringPtr("odm/lib"),
286 },
287 filesystem.SymlinkDefinition{
288 Target: proptools.StringPtr("/vendor/odm/lib64"),
289 Name: proptools.StringPtr("odm/lib64"),
290 },
291 filesystem.SymlinkDefinition{
292 Target: proptools.StringPtr("/vendor/odm/overlay"),
293 Name: proptools.StringPtr("odm/overlay"),
294 },
295 filesystem.SymlinkDefinition{
296 Target: proptools.StringPtr("/vendor/odm/priv-app"),
297 Name: proptools.StringPtr("odm/priv-app"),
298 },
299 filesystem.SymlinkDefinition{
300 Target: proptools.StringPtr("/vendor/odm/usr"),
301 Name: proptools.StringPtr("odm/usr"),
302 },
303 filesystem.SymlinkDefinition{
mrziwang9afc2982024-11-05 14:29:48 -0800304 Target: proptools.StringPtr("/product"),
305 Name: proptools.StringPtr("system/product"),
306 },
307 filesystem.SymlinkDefinition{
308 Target: proptools.StringPtr("/system_ext"),
309 Name: proptools.StringPtr("system/system_ext"),
310 },
311 filesystem.SymlinkDefinition{
312 Target: proptools.StringPtr("/vendor"),
313 Name: proptools.StringPtr("system/vendor"),
314 },
315 filesystem.SymlinkDefinition{
316 Target: proptools.StringPtr("/system_dlkm/lib/modules"),
317 Name: proptools.StringPtr("system/lib/modules"),
318 },
Cole Faust1d4e76c2024-11-26 14:15:29 -0800319 filesystem.SymlinkDefinition{
320 Target: proptools.StringPtr("/data/cache"),
321 Name: proptools.StringPtr("cache"),
322 },
mrziwang9afc2982024-11-05 14:29:48 -0800323 }
Cole Faust1d4e76c2024-11-26 14:15:29 -0800324 fsProps.Dirs = proptools.NewSimpleConfigurable([]string{
325 // From generic_rootdirs in build/make/target/product/generic/Android.bp
326 "acct",
327 "apex",
328 "bootstrap-apex",
329 "config",
330 "data",
331 "data_mirror",
332 "debug_ramdisk",
333 "dev",
334 "linkerconfig",
335 "metadata",
336 "mnt",
337 "odm",
338 "odm_dlkm",
339 "oem",
340 "postinstall",
341 "proc",
342 "second_stage_resources",
343 "storage",
344 "sys",
345 "system",
346 "system_dlkm",
347 "tmp",
348 "vendor",
349 "vendor_dlkm",
350
351 // from android_rootdirs in build/make/target/product/generic/Android.bp
352 "system_ext",
353 "product",
354 })
Spandan Dasa8fa6b42024-10-23 00:45:29 +0000355 case "system_ext":
Spandan Das2b4bf4c2024-12-02 19:41:04 +0000356 if partitionVars.ProductFsverityGenerateMetadata {
357 fsProps.Fsverity.Inputs = []string{
358 "framework/*",
359 "framework/*/*", // framework/{arch}
360 "framework/oat/*/*", // framework/oat/{arch}
361 }
362 fsProps.Fsverity.Libs = []string{":framework-res{.export-package.apk}"}
Spandan Dasa8fa6b42024-10-23 00:45:29 +0000363 }
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000364 case "product":
365 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
Spandan Das71be42d2024-11-20 18:34:16 +0000366 fsProps.Android_filesystem_deps.System = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system"))
367 if ctx.DeviceConfig().SystemExtPath() == "system_ext" {
368 fsProps.Android_filesystem_deps.System_ext = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_ext"))
369 }
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000370 case "vendor":
371 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
Spandan Das69464c32024-10-25 20:08:06 +0000372 fsProps.Symlinks = []filesystem.SymlinkDefinition{
373 filesystem.SymlinkDefinition{
374 Target: proptools.StringPtr("/odm"),
375 Name: proptools.StringPtr("vendor/odm"),
376 },
377 filesystem.SymlinkDefinition{
378 Target: proptools.StringPtr("/vendor_dlkm/lib/modules"),
379 Name: proptools.StringPtr("vendor/lib/modules"),
380 },
381 }
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 }
Spandan Dasc5717162024-11-01 18:33:57 +0000386 case "odm":
387 fsProps.Symlinks = []filesystem.SymlinkDefinition{
388 filesystem.SymlinkDefinition{
389 Target: proptools.StringPtr("/odm_dlkm/lib/modules"),
390 Name: proptools.StringPtr("odm/lib/modules"),
391 },
392 }
mrziwang23ba8762024-11-07 16:21:53 -0800393 case "userdata":
394 fsProps.Base_dir = proptools.StringPtr("data")
Jihoon Kangd098d442024-11-19 00:03:22 +0000395 case "ramdisk":
396 // Following the logic in https://cs.android.com/android/platform/superproject/main/+/c3c5063df32748a8806ce5da5dd0db158eab9ad9:build/make/core/Makefile;l=1307
397 fsProps.Dirs = android.NewSimpleConfigurable([]string{
398 "debug_ramdisk",
399 "dev",
400 "metadata",
401 "mnt",
402 "proc",
403 "second_stage_resources",
404 "sys",
405 })
406 if partitionVars.BoardUsesGenericKernelImage {
407 fsProps.Dirs.AppendSimpleValue([]string{
408 "first_stage_ramdisk/debug_ramdisk",
409 "first_stage_ramdisk/dev",
410 "first_stage_ramdisk/metadata",
411 "first_stage_ramdisk/mnt",
412 "first_stage_ramdisk/proc",
413 "first_stage_ramdisk/second_stage_resources",
414 "first_stage_ramdisk/sys",
415 })
416 }
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000417 }
418}
Spandan Dascbe641a2024-10-14 21:07:34 +0000419
Spandan Das5b493cd2024-11-07 20:55:56 +0000420var (
421 dlkmPartitions = []string{
422 "system_dlkm",
423 "vendor_dlkm",
424 "odm_dlkm",
425 }
426)
427
Cole Faust92ccbe22024-10-03 14:38:37 -0700428// Creates a soong module to build the given partition. Returns false if we can't support building
429// it.
430func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partitionType string) bool {
mrziwang4b0ca972024-10-17 14:56:19 -0700431 baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)))
432
433 fsProps, supported := generateFsProps(ctx, partitionType)
434 if !supported {
435 return false
mrziwanga077b942024-10-16 16:00:06 -0700436 }
mrziwanga077b942024-10-16 16:00:06 -0700437
Cole Faust7db05752024-11-21 13:30:41 -0800438 if partitionType == "vendor" || partitionType == "product" || partitionType == "system" {
Spandan Das2047a4c2024-11-11 21:24:58 +0000439 fsProps.Linker_config.Gen_linker_config = proptools.BoolPtr(true)
Cole Faust7db05752024-11-21 13:30:41 -0800440 if partitionType != "system" {
441 fsProps.Linker_config.Linker_config_srcs = f.createLinkerConfigSourceFilegroups(ctx, partitionType)
442 }
Spandan Das312cc412024-10-29 18:20:11 +0000443 }
444
Jihoon Kanga8fa0712024-11-26 23:11:07 +0000445 if android.InList(partitionType, append(dlkmPartitions, "vendor_ramdisk")) {
Spandan Das5b493cd2024-11-07 20:55:56 +0000446 f.createPrebuiltKernelModules(ctx, partitionType)
Spandan Das5e336422024-11-01 22:31:20 +0000447 }
448
mrziwang4b0ca972024-10-17 14:56:19 -0700449 var module android.Module
450 if partitionType == "system" {
451 module = ctx.CreateModule(filesystem.SystemImageFactory, baseProps, fsProps)
452 } else {
453 // Explicitly set the partition.
454 fsProps.Partition_type = proptools.StringPtr(partitionType)
455 module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps)
456 }
457 module.HideFromMake()
Spandan Das168098c2024-10-28 19:44:34 +0000458 if partitionType == "vendor" {
Spandan Das4cd93b52024-11-05 23:27:03 +0000459 f.createVendorBuildProp(ctx)
Spandan Das168098c2024-10-28 19:44:34 +0000460 }
mrziwang4b0ca972024-10-17 14:56:19 -0700461 return true
462}
463
Cole Faust953476f2024-11-14 14:11:29 -0800464// Creates filegroups for the files specified in BOARD_(partition_)AVB_KEY_PATH
465func (f *filesystemCreator) createAvbKeyFilegroups(ctx android.LoadHookContext) {
466 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
467 var files []string
468
469 if len(partitionVars.BoardAvbKeyPath) > 0 {
470 files = append(files, partitionVars.BoardAvbKeyPath)
471 }
472 for _, partition := range android.SortedKeys(partitionVars.PartitionQualifiedVariables) {
473 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partition]
474 if len(specificPartitionVars.BoardAvbKeyPath) > 0 {
475 files = append(files, specificPartitionVars.BoardAvbKeyPath)
476 }
477 }
478
479 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
480 for _, file := range files {
481 if _, ok := fsGenState.avbKeyFilegroups[file]; ok {
482 continue
483 }
484 if file == "external/avb/test/data/testkey_rsa4096.pem" {
485 // There already exists a checked-in filegroup for this commonly-used key, just use that
486 fsGenState.avbKeyFilegroups[file] = "avb_testkey_rsa4096"
487 continue
488 }
489 dir := filepath.Dir(file)
490 base := filepath.Base(file)
491 name := fmt.Sprintf("avb_key_%x", strings.ReplaceAll(file, "/", "_"))
492 ctx.CreateModuleInDirectory(
493 android.FileGroupFactory,
494 dir,
495 &struct {
496 Name *string
497 Srcs []string
498 Visibility []string
499 }{
500 Name: proptools.StringPtr(name),
501 Srcs: []string{base},
502 Visibility: []string{"//visibility:public"},
503 },
504 )
505 fsGenState.avbKeyFilegroups[file] = name
506 }
507}
508
Spandan Das5e336422024-11-01 22:31:20 +0000509// createPrebuiltKernelModules creates `prebuilt_kernel_modules`. These modules will be added to deps of the
Spandan Das7b25a512024-11-06 20:41:26 +0000510// autogenerated *_dlkm filsystem modules. Each _dlkm partition should have a single prebuilt_kernel_modules dependency.
511// This ensures that the depmod artifacts (modules.* installed in /lib/modules/) are generated with a complete view.
Spandan Das5b493cd2024-11-07 20:55:56 +0000512func (f *filesystemCreator) createPrebuiltKernelModules(ctx android.LoadHookContext, partitionType string) {
Spandan Das5e336422024-11-01 22:31:20 +0000513 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
Spandan Das7b25a512024-11-06 20:41:26 +0000514 name := generatedModuleName(ctx.Config(), fmt.Sprintf("%s-kernel-modules", partitionType))
515 props := &struct {
Spandan Das912d26b2024-11-06 19:35:17 +0000516 Name *string
517 Srcs []string
Spandan Das5b493cd2024-11-07 20:55:56 +0000518 System_deps []string
Spandan Das912d26b2024-11-06 19:35:17 +0000519 System_dlkm_specific *bool
Spandan Das5b493cd2024-11-07 20:55:56 +0000520 Vendor_dlkm_specific *bool
521 Odm_dlkm_specific *bool
Jihoon Kanga8fa0712024-11-26 23:11:07 +0000522 Vendor_ramdisk *bool
Spandan Das5b493cd2024-11-07 20:55:56 +0000523 Load_by_default *bool
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000524 Blocklist_file *string
Jihoon Kang72dd6fc2024-11-27 01:16:39 +0000525 Options_file *string
Spandan Das7b25a512024-11-06 20:41:26 +0000526 }{
527 Name: proptools.StringPtr(name),
Spandan Das5e336422024-11-01 22:31:20 +0000528 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000529 switch partitionType {
530 case "system_dlkm":
Spandan Das59ee5d72024-11-18 19:36:32 +0000531 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelModules).Strings()
Spandan Das912d26b2024-11-06 19:35:17 +0000532 props.System_dlkm_specific = proptools.BoolPtr(true)
Spandan Das5b493cd2024-11-07 20:55:56 +0000533 if len(ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelLoadModules) == 0 {
534 // Create empty modules.load file for system
535 // https://source.corp.google.com/h/googleplex-android/platform/build/+/ef55daac9954896161b26db4f3ef1781b5a5694c:core/Makefile;l=695-700;drc=549fe2a5162548bd8b47867d35f907eb22332023;bpv=1;bpt=0
536 props.Load_by_default = proptools.BoolPtr(false)
537 }
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000538 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelBlocklistFile; blocklistFile != "" {
539 props.Blocklist_file = proptools.StringPtr(blocklistFile)
540 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000541 case "vendor_dlkm":
Spandan Das59ee5d72024-11-18 19:36:32 +0000542 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorKernelModules).Strings()
Spandan Das5b493cd2024-11-07 20:55:56 +0000543 if len(ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelModules) > 0 {
544 props.System_deps = []string{":" + generatedModuleName(ctx.Config(), "system_dlkm-kernel-modules") + "{.modules}"}
545 }
546 props.Vendor_dlkm_specific = proptools.BoolPtr(true)
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000547 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorKernelBlocklistFile; blocklistFile != "" {
548 props.Blocklist_file = proptools.StringPtr(blocklistFile)
549 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000550 case "odm_dlkm":
Spandan Das59ee5d72024-11-18 19:36:32 +0000551 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.OdmKernelModules).Strings()
Spandan Das5b493cd2024-11-07 20:55:56 +0000552 props.Odm_dlkm_specific = proptools.BoolPtr(true)
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000553 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.OdmKernelBlocklistFile; blocklistFile != "" {
554 props.Blocklist_file = proptools.StringPtr(blocklistFile)
555 }
Jihoon Kanga8fa0712024-11-26 23:11:07 +0000556 case "vendor_ramdisk":
557 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorRamdiskKernelModules).Strings()
558 props.Vendor_ramdisk = proptools.BoolPtr(true)
559 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorRamdiskKernelBlocklistFile; blocklistFile != "" {
560 props.Blocklist_file = proptools.StringPtr(blocklistFile)
561 }
Jihoon Kang72dd6fc2024-11-27 01:16:39 +0000562 if optionsFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorRamdiskKernelOptionsFile; optionsFile != "" {
563 props.Options_file = proptools.StringPtr(optionsFile)
564 }
565
Spandan Das5b493cd2024-11-07 20:55:56 +0000566 default:
567 ctx.ModuleErrorf("DLKM is not supported for %s\n", partitionType)
Spandan Das912d26b2024-11-06 19:35:17 +0000568 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000569
570 if len(props.Srcs) == 0 {
571 return // do not generate `prebuilt_kernel_modules` if there are no sources
572 }
573
Spandan Das7b25a512024-11-06 20:41:26 +0000574 kernelModule := ctx.CreateModuleInDirectory(
575 kernel.PrebuiltKernelModulesFactory,
576 ".", // create in root directory for now
577 props,
578 )
579 kernelModule.HideFromMake()
580 // Add to deps
581 (*fsGenState.fsDeps[partitionType])[name] = defaultDepCandidateProps(ctx.Config())
Spandan Das5e336422024-11-01 22:31:20 +0000582}
583
Spandan Das4cd93b52024-11-05 23:27:03 +0000584// Create a build_prop and android_info module. This will be used to create /vendor/build.prop
585func (f *filesystemCreator) createVendorBuildProp(ctx android.LoadHookContext) {
586 // Create a android_info for vendor
587 // The board info files might be in a directory outside the root soong namespace, so create
588 // the module in "."
589 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
590 androidInfoProps := &struct {
591 Name *string
592 Board_info_files []string
593 Bootloader_board_name *string
594 }{
595 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "android-info.prop")),
596 Board_info_files: partitionVars.BoardInfoFiles,
597 }
598 if len(androidInfoProps.Board_info_files) == 0 {
599 androidInfoProps.Bootloader_board_name = proptools.StringPtr(partitionVars.BootLoaderBoardName)
600 }
601 androidInfoProp := ctx.CreateModuleInDirectory(
602 android.AndroidInfoFactory,
603 ".",
604 androidInfoProps,
605 )
606 androidInfoProp.HideFromMake()
607 // Create a build prop for vendor
608 vendorBuildProps := &struct {
609 Name *string
610 Vendor *bool
611 Stem *string
612 Product_config *string
613 Android_info *string
614 }{
615 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "vendor-build.prop")),
616 Vendor: proptools.BoolPtr(true),
617 Stem: proptools.StringPtr("build.prop"),
618 Product_config: proptools.StringPtr(":product_config"),
619 Android_info: proptools.StringPtr(":" + androidInfoProp.Name()),
620 }
621 vendorBuildProp := ctx.CreateModule(
622 android.BuildPropFactory,
623 vendorBuildProps,
624 )
625 vendorBuildProp.HideFromMake()
626}
627
Spandan Das8fe68dc2024-10-29 18:20:11 +0000628// createLinkerConfigSourceFilegroups creates filegroup modules to generate linker.config.pb for the following partitions
629// 1. vendor: Using PRODUCT_VENDOR_LINKER_CONFIG_FRAGMENTS (space separated file list)
630// 1. product: Using PRODUCT_PRODUCT_LINKER_CONFIG_FRAGMENTS (space separated file list)
631// It creates a filegroup for each file in the fragment list
Spandan Das312cc412024-10-29 18:20:11 +0000632// The filegroup modules are then added to `linker_config_srcs` of the autogenerated vendor `android_filesystem`.
Spandan Das8fe68dc2024-10-29 18:20:11 +0000633func (f *filesystemCreator) createLinkerConfigSourceFilegroups(ctx android.LoadHookContext, partitionType string) []string {
Spandan Das312cc412024-10-29 18:20:11 +0000634 ret := []string{}
635 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Spandan Das8fe68dc2024-10-29 18:20:11 +0000636 var linkerConfigSrcs []string
637 if partitionType == "vendor" {
638 linkerConfigSrcs = android.FirstUniqueStrings(partitionVars.VendorLinkerConfigSrcs)
639 } else if partitionType == "product" {
640 linkerConfigSrcs = android.FirstUniqueStrings(partitionVars.ProductLinkerConfigSrcs)
641 } else {
642 ctx.ModuleErrorf("linker.config.pb is only supported for vendor and product partitions. For system partition, use `android_system_image`")
643 }
644
645 if len(linkerConfigSrcs) > 0 {
Spandan Das312cc412024-10-29 18:20:11 +0000646 // Create a filegroup, and add `:<filegroup_name>` to ret.
647 for index, linkerConfigSrc := range linkerConfigSrcs {
648 dir := filepath.Dir(linkerConfigSrc)
649 base := filepath.Base(linkerConfigSrc)
Spandan Das8fe68dc2024-10-29 18:20:11 +0000650 fgName := generatedModuleName(ctx.Config(), fmt.Sprintf("%s-linker-config-src%s", partitionType, strconv.Itoa(index)))
Spandan Das312cc412024-10-29 18:20:11 +0000651 srcs := []string{base}
652 fgProps := &struct {
653 Name *string
654 Srcs proptools.Configurable[[]string]
655 }{
656 Name: proptools.StringPtr(fgName),
657 Srcs: proptools.NewSimpleConfigurable(srcs),
658 }
659 ctx.CreateModuleInDirectory(
660 android.FileGroupFactory,
661 dir,
662 fgProps,
663 )
664 ret = append(ret, ":"+fgName)
665 }
666 }
667 return ret
668}
669
mrziwang4b0ca972024-10-17 14:56:19 -0700670type filesystemBaseProperty struct {
671 Name *string
672 Compile_multilib *string
Cole Faust3552eb62024-11-06 18:07:26 -0800673 Visibility []string
mrziwang4b0ca972024-10-17 14:56:19 -0700674}
675
676func generateBaseProps(namePtr *string) *filesystemBaseProperty {
677 return &filesystemBaseProperty{
678 Name: namePtr,
679 Compile_multilib: proptools.StringPtr("both"),
Cole Faust3552eb62024-11-06 18:07:26 -0800680 // The vbmeta modules are currently in the root directory and depend on the partitions
681 Visibility: []string{"//.", "//build/soong:__subpackages__"},
mrziwang4b0ca972024-10-17 14:56:19 -0700682 }
683}
684
685func generateFsProps(ctx android.EarlyModuleContext, partitionType string) (*filesystem.FilesystemProperties, bool) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700686 fsProps := &filesystem.FilesystemProperties{}
687
mrziwang4b0ca972024-10-17 14:56:19 -0700688 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Cole Faust0c4b4152024-11-20 16:42:53 -0800689 var avbInfo avbInfo
Cole Faust76a6e952024-11-07 16:56:45 -0800690 var fsType string
691 if strings.Contains(partitionType, "ramdisk") {
692 fsType = "compressed_cpio"
693 } else {
Cole Faust953476f2024-11-14 14:11:29 -0800694 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
Cole Faust76a6e952024-11-07 16:56:45 -0800695 fsType = specificPartitionVars.BoardFileSystemType
Cole Faust0c4b4152024-11-20 16:42:53 -0800696 avbInfo = getAvbInfo(ctx.Config(), partitionType)
Cole Faust953476f2024-11-14 14:11:29 -0800697 if fsType == "" {
698 fsType = "ext4" //default
699 }
Cole Faust76a6e952024-11-07 16:56:45 -0800700 }
Cole Faust76a6e952024-11-07 16:56:45 -0800701
mrziwang4b0ca972024-10-17 14:56:19 -0700702 fsProps.Type = proptools.StringPtr(fsType)
703 if filesystem.GetFsTypeFromString(ctx, *fsProps.Type).IsUnknown() {
704 // Currently the android_filesystem module type only supports a handful of FS types like ext4, erofs
705 return nil, false
706 }
707
Cole Faust92ccbe22024-10-03 14:38:37 -0700708 // Don't build this module on checkbuilds, the soong-built partitions are still in-progress
709 // and sometimes don't build.
710 fsProps.Unchecked_module = proptools.BoolPtr(true)
711
Jihoon Kang98047cf2024-10-02 17:13:54 +0000712 // BOARD_AVB_ENABLE
Cole Faust0c4b4152024-11-20 16:42:53 -0800713 fsProps.Use_avb = avbInfo.avbEnable
Jihoon Kang98047cf2024-10-02 17:13:54 +0000714 // BOARD_AVB_KEY_PATH
Cole Faust0c4b4152024-11-20 16:42:53 -0800715 fsProps.Avb_private_key = avbInfo.avbkeyFilegroup
Jihoon Kang98047cf2024-10-02 17:13:54 +0000716 // BOARD_AVB_ALGORITHM
Cole Faust0c4b4152024-11-20 16:42:53 -0800717 fsProps.Avb_algorithm = avbInfo.avbAlgorithm
Jihoon Kang98047cf2024-10-02 17:13:54 +0000718 // BOARD_AVB_SYSTEM_ROLLBACK_INDEX
Cole Faust0c4b4152024-11-20 16:42:53 -0800719 fsProps.Rollback_index = avbInfo.avbRollbackIndex
Jihoon Kang98047cf2024-10-02 17:13:54 +0000720
Cole Faust92ccbe22024-10-03 14:38:37 -0700721 fsProps.Partition_name = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000722
Cole Faust68382192024-11-19 10:36:03 -0800723 if !strings.Contains(partitionType, "ramdisk") {
724 fsProps.Base_dir = proptools.StringPtr(partitionType)
725 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000726
Jihoon Kang0d545b82024-10-11 00:21:57 +0000727 fsProps.Is_auto_generated = proptools.BoolPtr(true)
728
Spandan Das71be42d2024-11-20 18:34:16 +0000729 partitionSpecificFsProps(ctx, fsProps, partitionVars, partitionType)
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000730
Jihoon Kang98047cf2024-10-02 17:13:54 +0000731 // system_image properties that are not set:
732 // - filesystemProperties.Avb_hash_algorithm
733 // - filesystemProperties.File_contexts
734 // - filesystemProperties.Dirs
735 // - filesystemProperties.Symlinks
736 // - filesystemProperties.Fake_timestamp
737 // - filesystemProperties.Uuid
738 // - filesystemProperties.Mount_point
739 // - filesystemProperties.Include_make_built_files
740 // - filesystemProperties.Build_logtags
Jihoon Kang98047cf2024-10-02 17:13:54 +0000741 // - systemImageProperties.Linker_config_src
mrziwang4b0ca972024-10-17 14:56:19 -0700742
743 return fsProps, true
Cole Faust92ccbe22024-10-03 14:38:37 -0700744}
745
Cole Faust0c4b4152024-11-20 16:42:53 -0800746type avbInfo struct {
747 avbEnable *bool
748 avbKeyPath *string
749 avbkeyFilegroup *string
750 avbAlgorithm *string
751 avbRollbackIndex *int64
752 avbMode *string
753}
754
755func getAvbInfo(config android.Config, partitionType string) avbInfo {
756 partitionVars := config.ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
757 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
758 var result avbInfo
759 boardAvbEnable := partitionVars.BoardAvbEnable
760 if boardAvbEnable {
761 result.avbEnable = proptools.BoolPtr(true)
762 if specificPartitionVars.BoardAvbKeyPath != "" {
763 result.avbKeyPath = proptools.StringPtr(specificPartitionVars.BoardAvbKeyPath)
764 } else if partitionVars.BoardAvbKeyPath != "" {
765 result.avbKeyPath = proptools.StringPtr(partitionVars.BoardAvbKeyPath)
766 }
767 if specificPartitionVars.BoardAvbAlgorithm != "" {
768 result.avbAlgorithm = proptools.StringPtr(specificPartitionVars.BoardAvbAlgorithm)
769 } else if partitionVars.BoardAvbAlgorithm != "" {
770 result.avbAlgorithm = proptools.StringPtr(partitionVars.BoardAvbAlgorithm)
771 }
772 if specificPartitionVars.BoardAvbRollbackIndex != "" {
773 parsed, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64)
774 if err != nil {
775 panic(fmt.Sprintf("Rollback index must be an int, got %s", specificPartitionVars.BoardAvbRollbackIndex))
776 }
777 result.avbRollbackIndex = &parsed
778 } else if partitionVars.BoardAvbRollbackIndex != "" {
779 parsed, err := strconv.ParseInt(partitionVars.BoardAvbRollbackIndex, 10, 64)
780 if err != nil {
781 panic(fmt.Sprintf("Rollback index must be an int, got %s", partitionVars.BoardAvbRollbackIndex))
782 }
783 result.avbRollbackIndex = &parsed
784 }
785 result.avbMode = proptools.StringPtr("make_legacy")
786 }
787 if result.avbKeyPath != nil {
788 fsGenState := config.Get(fsGenStateOnceKey).(*FsGenState)
789 filegroup := fsGenState.avbKeyFilegroups[*result.avbKeyPath]
790 result.avbkeyFilegroup = proptools.StringPtr(":" + filegroup)
791 }
792 return result
793}
794
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800795func (f *filesystemCreator) createFileListDiffTest(ctx android.ModuleContext, partitionType string) android.Path {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000796 partitionModuleName := generatedModuleNameForPartition(ctx.Config(), partitionType)
Cole Faust92ccbe22024-10-03 14:38:37 -0700797 systemImage := ctx.GetDirectDepWithTag(partitionModuleName, generatedFilesystemDepTag)
798 filesystemInfo, ok := android.OtherModuleProvider(ctx, systemImage, filesystem.FilesystemProvider)
799 if !ok {
800 ctx.ModuleErrorf("Expected module %s to provide FileysystemInfo", partitionModuleName)
801 }
802 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 +0000803 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700804
805 builder := android.NewRuleBuilder(pctx, ctx)
806 builder.Command().BuiltTool("file_list_diff").
807 Input(makeFileList).
808 Input(filesystemInfo.FileListFile).
Cole Faust56301572024-11-07 15:22:42 -0800809 Text(partitionModuleName)
Cole Faust92ccbe22024-10-03 14:38:37 -0700810 builder.Command().Text("touch").Output(diffTestResultFile)
811 builder.Build(partitionModuleName+" diff test", partitionModuleName+" diff test")
812 return diffTestResultFile
813}
814
815func createFailingCommand(ctx android.ModuleContext, message string) android.Path {
816 hasher := sha256.New()
817 hasher.Write([]byte(message))
818 filename := fmt.Sprintf("failing_command_%x.txt", hasher.Sum(nil))
819 file := android.PathForModuleOut(ctx, filename)
820 builder := android.NewRuleBuilder(pctx, ctx)
821 builder.Command().Textf("echo %s", proptools.NinjaAndShellEscape(message))
822 builder.Command().Text("exit 1 #").Output(file)
823 builder.Build("failing command "+filename, "failing command "+filename)
824 return file
825}
826
Cole Faust3552eb62024-11-06 18:07:26 -0800827func createVbmetaDiff(ctx android.ModuleContext, vbmetaModuleName string, vbmetaPartitionName string) android.Path {
828 vbmetaModule := ctx.GetDirectDepWithTag(vbmetaModuleName, generatedVbmetaPartitionDepTag)
829 outputFilesProvider, ok := android.OtherModuleProvider(ctx, vbmetaModule, android.OutputFilesProvider)
830 if !ok {
831 ctx.ModuleErrorf("Expected module %s to provide OutputFiles", vbmetaModule)
832 }
833 if len(outputFilesProvider.DefaultOutputFiles) != 1 {
834 ctx.ModuleErrorf("Expected 1 output file from module %s", vbmetaModule)
835 }
836 soongVbMetaFile := outputFilesProvider.DefaultOutputFiles[0]
837 makeVbmetaFile := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/%s.img", ctx.Config().DeviceName(), vbmetaPartitionName))
838
839 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", vbmetaModuleName))
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800840 createDiffTest(ctx, diffTestResultFile, soongVbMetaFile, makeVbmetaFile)
841 return diffTestResultFile
842}
843
844func createDiffTest(ctx android.ModuleContext, diffTestResultFile android.WritablePath, file1 android.Path, file2 android.Path) {
Cole Faust3552eb62024-11-06 18:07:26 -0800845 builder := android.NewRuleBuilder(pctx, ctx)
846 builder.Command().Text("diff").
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800847 Input(file1).
848 Input(file2)
Cole Faust3552eb62024-11-06 18:07:26 -0800849 builder.Command().Text("touch").Output(diffTestResultFile)
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800850 builder.Build("diff test "+diffTestResultFile.String(), "diff test")
Cole Faust3552eb62024-11-06 18:07:26 -0800851}
852
Cole Faust92ccbe22024-10-03 14:38:37 -0700853type systemImageDepTagType struct {
854 blueprint.BaseDependencyTag
855}
856
857var generatedFilesystemDepTag systemImageDepTagType
Cole Faust3552eb62024-11-06 18:07:26 -0800858var generatedVbmetaPartitionDepTag systemImageDepTagType
Cole Faust92ccbe22024-10-03 14:38:37 -0700859
860func (f *filesystemCreator) DepsMutator(ctx android.BottomUpMutatorContext) {
861 for _, partitionType := range f.properties.Generated_partition_types {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000862 ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, generatedModuleNameForPartition(ctx.Config(), partitionType))
Cole Faust92ccbe22024-10-03 14:38:37 -0700863 }
Cole Faust3552eb62024-11-06 18:07:26 -0800864 for _, vbmetaModule := range f.properties.Vbmeta_module_names {
865 ctx.AddDependency(ctx.Module(), generatedVbmetaPartitionDepTag, vbmetaModule)
866 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000867}
868
869func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700870 if ctx.ModuleDir() != "build/soong/fsgen" {
871 ctx.ModuleErrorf("There can only be one soong_filesystem_creator in build/soong/fsgen")
872 }
873 f.HideFromMake()
Jihoon Kang98047cf2024-10-02 17:13:54 +0000874
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000875 var content strings.Builder
876 generatedBp := android.PathForModuleOut(ctx, "soong_generated_product_config.bp")
877 for _, partition := range ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions {
878 content.WriteString(generateBpContent(ctx, partition))
879 content.WriteString("\n")
880 }
881 android.WriteFileRule(ctx, generatedBp, content.String())
882
mrziwang8f86c882024-10-03 12:34:33 -0700883 ctx.Phony("product_config_to_bp", generatedBp)
884
Cole Faust92ccbe22024-10-03 14:38:37 -0700885 var diffTestFiles []android.Path
886 for _, partitionType := range f.properties.Generated_partition_types {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800887 diffTestFile := f.createFileListDiffTest(ctx, partitionType)
Jihoon Kang72f812f2024-10-17 18:46:24 +0000888 diffTestFiles = append(diffTestFiles, diffTestFile)
889 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700890 }
891 for _, partitionType := range f.properties.Unsupported_partition_types {
Jihoon Kang72f812f2024-10-17 18:46:24 +0000892 diffTestFile := createFailingCommand(ctx, fmt.Sprintf("Couldn't build %s partition", partitionType))
893 diffTestFiles = append(diffTestFiles, diffTestFile)
894 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700895 }
Cole Faust3552eb62024-11-06 18:07:26 -0800896 for i, vbmetaModule := range f.properties.Vbmeta_module_names {
897 diffTestFile := createVbmetaDiff(ctx, vbmetaModule, f.properties.Vbmeta_partition_names[i])
898 diffTestFiles = append(diffTestFiles, diffTestFile)
899 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", f.properties.Vbmeta_partition_names[i]), diffTestFile)
900 }
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800901 if f.properties.Boot_image != "" {
902 diffTestFile := android.PathForModuleOut(ctx, "boot_diff_test.txt")
903 soongBootImg := android.PathForModuleSrc(ctx, f.properties.Boot_image)
904 makeBootImage := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/boot.img", ctx.Config().DeviceName()))
905 createDiffTest(ctx, diffTestFile, soongBootImg, makeBootImage)
906 diffTestFiles = append(diffTestFiles, diffTestFile)
907 ctx.Phony("soong_generated_boot_filesystem_test", diffTestFile)
908 }
Cole Faust24938e22024-11-18 14:01:58 -0800909 if f.properties.Vendor_boot_image != "" {
910 diffTestFile := android.PathForModuleOut(ctx, "vendor_boot_diff_test.txt")
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000911 soongBootImg := android.PathForModuleSrc(ctx, f.properties.Vendor_boot_image)
Cole Faust24938e22024-11-18 14:01:58 -0800912 makeBootImage := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/vendor_boot.img", ctx.Config().DeviceName()))
913 createDiffTest(ctx, diffTestFile, soongBootImg, makeBootImage)
914 diffTestFiles = append(diffTestFiles, diffTestFile)
915 ctx.Phony("soong_generated_vendor_boot_filesystem_test", diffTestFile)
916 }
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000917 if f.properties.Init_boot_image != "" {
918 diffTestFile := android.PathForModuleOut(ctx, "init_boot_diff_test.txt")
919 soongBootImg := android.PathForModuleSrc(ctx, f.properties.Init_boot_image)
920 makeBootImage := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/init_boot.img", ctx.Config().DeviceName()))
921 createDiffTest(ctx, diffTestFile, soongBootImg, makeBootImage)
922 diffTestFiles = append(diffTestFiles, diffTestFile)
923 ctx.Phony("soong_generated_init_boot_filesystem_test", diffTestFile)
924 }
Cole Faust92ccbe22024-10-03 14:38:37 -0700925 ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000926}
mrziwang8f86c882024-10-03 12:34:33 -0700927
mrziwang8f86c882024-10-03 12:34:33 -0700928func generateBpContent(ctx android.EarlyModuleContext, partitionType string) string {
mrziwang4b0ca972024-10-17 14:56:19 -0700929 fsProps, fsTypeSupported := generateFsProps(ctx, partitionType)
930 if !fsTypeSupported {
931 return ""
mrziwang8f86c882024-10-03 12:34:33 -0700932 }
933
mrziwang4b0ca972024-10-17 14:56:19 -0700934 baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)))
Jihoon Kang0d7b0112024-11-13 20:44:05 +0000935 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
936 deps := fsGenState.fsDeps[partitionType]
937 highPriorityDeps := fsGenState.generatedPrebuiltEtcModuleNames
938 depProps := generateDepStruct(*deps, highPriorityDeps)
mrziwang8f86c882024-10-03 12:34:33 -0700939
mrziwang4b0ca972024-10-17 14:56:19 -0700940 result, err := proptools.RepackProperties([]interface{}{baseProps, fsProps, depProps})
mrziwang8f86c882024-10-03 12:34:33 -0700941 if err != nil {
Cole Faustae3e1d32024-11-05 13:22:50 -0800942 ctx.ModuleErrorf("%s", err.Error())
943 return ""
mrziwang8f86c882024-10-03 12:34:33 -0700944 }
945
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000946 moduleType := "android_filesystem"
947 if partitionType == "system" {
948 moduleType = "android_system_image"
949 }
950
mrziwang8f86c882024-10-03 12:34:33 -0700951 file := &parser.File{
952 Defs: []parser.Definition{
953 &parser.Module{
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000954 Type: moduleType,
mrziwang8f86c882024-10-03 12:34:33 -0700955 Map: *result,
956 },
957 },
958 }
959 bytes, err := parser.Print(file)
960 if err != nil {
961 ctx.ModuleErrorf(err.Error())
962 }
963 return strings.TrimSpace(string(bytes))
964}