blob: 6aec90a16c4c6532fb89cd301aa98ae0a6d56e04 [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 },
mrziwang9afc2982024-11-05 14:29:48 -0800325 }
Cole Faust1d4e76c2024-11-26 14:15:29 -0800326 fsProps.Dirs = proptools.NewSimpleConfigurable([]string{
327 // From generic_rootdirs in build/make/target/product/generic/Android.bp
328 "acct",
329 "apex",
330 "bootstrap-apex",
331 "config",
332 "data",
333 "data_mirror",
334 "debug_ramdisk",
335 "dev",
336 "linkerconfig",
337 "metadata",
338 "mnt",
339 "odm",
340 "odm_dlkm",
341 "oem",
342 "postinstall",
343 "proc",
344 "second_stage_resources",
345 "storage",
346 "sys",
347 "system",
348 "system_dlkm",
349 "tmp",
350 "vendor",
351 "vendor_dlkm",
352
353 // from android_rootdirs in build/make/target/product/generic/Android.bp
354 "system_ext",
355 "product",
356 })
Spandan Dasa8fa6b42024-10-23 00:45:29 +0000357 case "system_ext":
Spandan Das2b4bf4c2024-12-02 19:41:04 +0000358 if partitionVars.ProductFsverityGenerateMetadata {
359 fsProps.Fsverity.Inputs = []string{
360 "framework/*",
361 "framework/*/*", // framework/{arch}
362 "framework/oat/*/*", // framework/oat/{arch}
363 }
364 fsProps.Fsverity.Libs = []string{":framework-res{.export-package.apk}"}
Spandan Dasa8fa6b42024-10-23 00:45:29 +0000365 }
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000366 case "product":
367 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
Spandan Das71be42d2024-11-20 18:34:16 +0000368 fsProps.Android_filesystem_deps.System = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system"))
369 if ctx.DeviceConfig().SystemExtPath() == "system_ext" {
370 fsProps.Android_filesystem_deps.System_ext = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_ext"))
371 }
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000372 case "vendor":
373 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
Spandan Das69464c32024-10-25 20:08:06 +0000374 fsProps.Symlinks = []filesystem.SymlinkDefinition{
375 filesystem.SymlinkDefinition{
376 Target: proptools.StringPtr("/odm"),
377 Name: proptools.StringPtr("vendor/odm"),
378 },
379 filesystem.SymlinkDefinition{
380 Target: proptools.StringPtr("/vendor_dlkm/lib/modules"),
381 Name: proptools.StringPtr("vendor/lib/modules"),
382 },
383 }
Spandan Das71be42d2024-11-20 18:34:16 +0000384 fsProps.Android_filesystem_deps.System = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system"))
385 if ctx.DeviceConfig().SystemExtPath() == "system_ext" {
386 fsProps.Android_filesystem_deps.System_ext = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_ext"))
387 }
Spandan Dasc5717162024-11-01 18:33:57 +0000388 case "odm":
389 fsProps.Symlinks = []filesystem.SymlinkDefinition{
390 filesystem.SymlinkDefinition{
391 Target: proptools.StringPtr("/odm_dlkm/lib/modules"),
392 Name: proptools.StringPtr("odm/lib/modules"),
393 },
394 }
mrziwang23ba8762024-11-07 16:21:53 -0800395 case "userdata":
396 fsProps.Base_dir = proptools.StringPtr("data")
Jihoon Kangd098d442024-11-19 00:03:22 +0000397 case "ramdisk":
398 // Following the logic in https://cs.android.com/android/platform/superproject/main/+/c3c5063df32748a8806ce5da5dd0db158eab9ad9:build/make/core/Makefile;l=1307
399 fsProps.Dirs = android.NewSimpleConfigurable([]string{
400 "debug_ramdisk",
401 "dev",
402 "metadata",
403 "mnt",
404 "proc",
405 "second_stage_resources",
406 "sys",
407 })
408 if partitionVars.BoardUsesGenericKernelImage {
409 fsProps.Dirs.AppendSimpleValue([]string{
410 "first_stage_ramdisk/debug_ramdisk",
411 "first_stage_ramdisk/dev",
412 "first_stage_ramdisk/metadata",
413 "first_stage_ramdisk/mnt",
414 "first_stage_ramdisk/proc",
415 "first_stage_ramdisk/second_stage_resources",
416 "first_stage_ramdisk/sys",
417 })
418 }
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000419 }
420}
Spandan Dascbe641a2024-10-14 21:07:34 +0000421
Spandan Das5b493cd2024-11-07 20:55:56 +0000422var (
423 dlkmPartitions = []string{
424 "system_dlkm",
425 "vendor_dlkm",
426 "odm_dlkm",
427 }
428)
429
Cole Faust92ccbe22024-10-03 14:38:37 -0700430// Creates a soong module to build the given partition. Returns false if we can't support building
431// it.
432func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partitionType string) bool {
mrziwang4b0ca972024-10-17 14:56:19 -0700433 baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)))
434
435 fsProps, supported := generateFsProps(ctx, partitionType)
436 if !supported {
437 return false
mrziwanga077b942024-10-16 16:00:06 -0700438 }
mrziwanga077b942024-10-16 16:00:06 -0700439
Cole Faust7db05752024-11-21 13:30:41 -0800440 if partitionType == "vendor" || partitionType == "product" || partitionType == "system" {
Spandan Das2047a4c2024-11-11 21:24:58 +0000441 fsProps.Linker_config.Gen_linker_config = proptools.BoolPtr(true)
Cole Faust7db05752024-11-21 13:30:41 -0800442 if partitionType != "system" {
443 fsProps.Linker_config.Linker_config_srcs = f.createLinkerConfigSourceFilegroups(ctx, partitionType)
444 }
Spandan Das312cc412024-10-29 18:20:11 +0000445 }
446
Jihoon Kanga8fa0712024-11-26 23:11:07 +0000447 if android.InList(partitionType, append(dlkmPartitions, "vendor_ramdisk")) {
Spandan Das5b493cd2024-11-07 20:55:56 +0000448 f.createPrebuiltKernelModules(ctx, partitionType)
Spandan Das5e336422024-11-01 22:31:20 +0000449 }
450
mrziwang4b0ca972024-10-17 14:56:19 -0700451 var module android.Module
452 if partitionType == "system" {
453 module = ctx.CreateModule(filesystem.SystemImageFactory, baseProps, fsProps)
454 } else {
455 // Explicitly set the partition.
456 fsProps.Partition_type = proptools.StringPtr(partitionType)
457 module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps)
458 }
459 module.HideFromMake()
Spandan Das168098c2024-10-28 19:44:34 +0000460 if partitionType == "vendor" {
Spandan Das4cd93b52024-11-05 23:27:03 +0000461 f.createVendorBuildProp(ctx)
Spandan Das168098c2024-10-28 19:44:34 +0000462 }
mrziwang4b0ca972024-10-17 14:56:19 -0700463 return true
464}
465
Cole Faust953476f2024-11-14 14:11:29 -0800466// Creates filegroups for the files specified in BOARD_(partition_)AVB_KEY_PATH
467func (f *filesystemCreator) createAvbKeyFilegroups(ctx android.LoadHookContext) {
468 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
469 var files []string
470
471 if len(partitionVars.BoardAvbKeyPath) > 0 {
472 files = append(files, partitionVars.BoardAvbKeyPath)
473 }
474 for _, partition := range android.SortedKeys(partitionVars.PartitionQualifiedVariables) {
475 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partition]
476 if len(specificPartitionVars.BoardAvbKeyPath) > 0 {
477 files = append(files, specificPartitionVars.BoardAvbKeyPath)
478 }
479 }
480
481 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
482 for _, file := range files {
483 if _, ok := fsGenState.avbKeyFilegroups[file]; ok {
484 continue
485 }
486 if file == "external/avb/test/data/testkey_rsa4096.pem" {
487 // There already exists a checked-in filegroup for this commonly-used key, just use that
488 fsGenState.avbKeyFilegroups[file] = "avb_testkey_rsa4096"
489 continue
490 }
491 dir := filepath.Dir(file)
492 base := filepath.Base(file)
493 name := fmt.Sprintf("avb_key_%x", strings.ReplaceAll(file, "/", "_"))
494 ctx.CreateModuleInDirectory(
495 android.FileGroupFactory,
496 dir,
497 &struct {
498 Name *string
499 Srcs []string
500 Visibility []string
501 }{
502 Name: proptools.StringPtr(name),
503 Srcs: []string{base},
504 Visibility: []string{"//visibility:public"},
505 },
506 )
507 fsGenState.avbKeyFilegroups[file] = name
508 }
509}
510
Cole Faust3e730972024-12-03 13:12:08 -0800511// Creates filegroups for miscellaneous other files
512func (f *filesystemCreator) createMiscFilegroups(ctx android.LoadHookContext) {
513 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
514
515 if partitionVars.BoardErofsCompressorHints != "" {
516 dir := filepath.Dir(partitionVars.BoardErofsCompressorHints)
517 base := filepath.Base(partitionVars.BoardErofsCompressorHints)
518 ctx.CreateModuleInDirectory(
519 android.FileGroupFactory,
520 dir,
521 &struct {
522 Name *string
523 Srcs []string
524 Visibility []string
525 }{
526 Name: proptools.StringPtr("soong_generated_board_erofs_compress_hints_filegroup"),
527 Srcs: []string{base},
528 Visibility: []string{"//visibility:public"},
529 },
530 )
531 }
532}
533
Spandan Das5e336422024-11-01 22:31:20 +0000534// createPrebuiltKernelModules creates `prebuilt_kernel_modules`. These modules will be added to deps of the
Spandan Das7b25a512024-11-06 20:41:26 +0000535// autogenerated *_dlkm filsystem modules. Each _dlkm partition should have a single prebuilt_kernel_modules dependency.
536// This ensures that the depmod artifacts (modules.* installed in /lib/modules/) are generated with a complete view.
Spandan Das5b493cd2024-11-07 20:55:56 +0000537func (f *filesystemCreator) createPrebuiltKernelModules(ctx android.LoadHookContext, partitionType string) {
Spandan Das5e336422024-11-01 22:31:20 +0000538 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
Spandan Das7b25a512024-11-06 20:41:26 +0000539 name := generatedModuleName(ctx.Config(), fmt.Sprintf("%s-kernel-modules", partitionType))
540 props := &struct {
Spandan Das912d26b2024-11-06 19:35:17 +0000541 Name *string
542 Srcs []string
Spandan Das5b493cd2024-11-07 20:55:56 +0000543 System_deps []string
Spandan Das912d26b2024-11-06 19:35:17 +0000544 System_dlkm_specific *bool
Spandan Das5b493cd2024-11-07 20:55:56 +0000545 Vendor_dlkm_specific *bool
546 Odm_dlkm_specific *bool
Jihoon Kanga8fa0712024-11-26 23:11:07 +0000547 Vendor_ramdisk *bool
Spandan Das5b493cd2024-11-07 20:55:56 +0000548 Load_by_default *bool
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000549 Blocklist_file *string
Jihoon Kang72dd6fc2024-11-27 01:16:39 +0000550 Options_file *string
Spandan Das7b25a512024-11-06 20:41:26 +0000551 }{
552 Name: proptools.StringPtr(name),
Spandan Das5e336422024-11-01 22:31:20 +0000553 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000554 switch partitionType {
555 case "system_dlkm":
Spandan Das59ee5d72024-11-18 19:36:32 +0000556 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelModules).Strings()
Spandan Das912d26b2024-11-06 19:35:17 +0000557 props.System_dlkm_specific = proptools.BoolPtr(true)
Spandan Das5b493cd2024-11-07 20:55:56 +0000558 if len(ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelLoadModules) == 0 {
559 // Create empty modules.load file for system
560 // https://source.corp.google.com/h/googleplex-android/platform/build/+/ef55daac9954896161b26db4f3ef1781b5a5694c:core/Makefile;l=695-700;drc=549fe2a5162548bd8b47867d35f907eb22332023;bpv=1;bpt=0
561 props.Load_by_default = proptools.BoolPtr(false)
562 }
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000563 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelBlocklistFile; blocklistFile != "" {
564 props.Blocklist_file = proptools.StringPtr(blocklistFile)
565 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000566 case "vendor_dlkm":
Spandan Das59ee5d72024-11-18 19:36:32 +0000567 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorKernelModules).Strings()
Spandan Das5b493cd2024-11-07 20:55:56 +0000568 if len(ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelModules) > 0 {
569 props.System_deps = []string{":" + generatedModuleName(ctx.Config(), "system_dlkm-kernel-modules") + "{.modules}"}
570 }
571 props.Vendor_dlkm_specific = proptools.BoolPtr(true)
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000572 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorKernelBlocklistFile; blocklistFile != "" {
573 props.Blocklist_file = proptools.StringPtr(blocklistFile)
574 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000575 case "odm_dlkm":
Spandan Das59ee5d72024-11-18 19:36:32 +0000576 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.OdmKernelModules).Strings()
Spandan Das5b493cd2024-11-07 20:55:56 +0000577 props.Odm_dlkm_specific = proptools.BoolPtr(true)
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000578 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.OdmKernelBlocklistFile; blocklistFile != "" {
579 props.Blocklist_file = proptools.StringPtr(blocklistFile)
580 }
Jihoon Kanga8fa0712024-11-26 23:11:07 +0000581 case "vendor_ramdisk":
582 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorRamdiskKernelModules).Strings()
583 props.Vendor_ramdisk = proptools.BoolPtr(true)
584 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorRamdiskKernelBlocklistFile; blocklistFile != "" {
585 props.Blocklist_file = proptools.StringPtr(blocklistFile)
586 }
Jihoon Kang72dd6fc2024-11-27 01:16:39 +0000587 if optionsFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorRamdiskKernelOptionsFile; optionsFile != "" {
588 props.Options_file = proptools.StringPtr(optionsFile)
589 }
590
Spandan Das5b493cd2024-11-07 20:55:56 +0000591 default:
592 ctx.ModuleErrorf("DLKM is not supported for %s\n", partitionType)
Spandan Das912d26b2024-11-06 19:35:17 +0000593 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000594
595 if len(props.Srcs) == 0 {
596 return // do not generate `prebuilt_kernel_modules` if there are no sources
597 }
598
Spandan Das7b25a512024-11-06 20:41:26 +0000599 kernelModule := ctx.CreateModuleInDirectory(
600 kernel.PrebuiltKernelModulesFactory,
601 ".", // create in root directory for now
602 props,
603 )
604 kernelModule.HideFromMake()
605 // Add to deps
606 (*fsGenState.fsDeps[partitionType])[name] = defaultDepCandidateProps(ctx.Config())
Spandan Das5e336422024-11-01 22:31:20 +0000607}
608
Spandan Das4cd93b52024-11-05 23:27:03 +0000609// Create a build_prop and android_info module. This will be used to create /vendor/build.prop
610func (f *filesystemCreator) createVendorBuildProp(ctx android.LoadHookContext) {
611 // Create a android_info for vendor
612 // The board info files might be in a directory outside the root soong namespace, so create
613 // the module in "."
614 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
615 androidInfoProps := &struct {
616 Name *string
617 Board_info_files []string
618 Bootloader_board_name *string
619 }{
620 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "android-info.prop")),
621 Board_info_files: partitionVars.BoardInfoFiles,
622 }
623 if len(androidInfoProps.Board_info_files) == 0 {
624 androidInfoProps.Bootloader_board_name = proptools.StringPtr(partitionVars.BootLoaderBoardName)
625 }
626 androidInfoProp := ctx.CreateModuleInDirectory(
627 android.AndroidInfoFactory,
628 ".",
629 androidInfoProps,
630 )
631 androidInfoProp.HideFromMake()
632 // Create a build prop for vendor
633 vendorBuildProps := &struct {
634 Name *string
635 Vendor *bool
636 Stem *string
637 Product_config *string
638 Android_info *string
639 }{
640 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "vendor-build.prop")),
641 Vendor: proptools.BoolPtr(true),
642 Stem: proptools.StringPtr("build.prop"),
643 Product_config: proptools.StringPtr(":product_config"),
644 Android_info: proptools.StringPtr(":" + androidInfoProp.Name()),
645 }
646 vendorBuildProp := ctx.CreateModule(
647 android.BuildPropFactory,
648 vendorBuildProps,
649 )
650 vendorBuildProp.HideFromMake()
651}
652
Spandan Das8fe68dc2024-10-29 18:20:11 +0000653// createLinkerConfigSourceFilegroups creates filegroup modules to generate linker.config.pb for the following partitions
654// 1. vendor: Using PRODUCT_VENDOR_LINKER_CONFIG_FRAGMENTS (space separated file list)
655// 1. product: Using PRODUCT_PRODUCT_LINKER_CONFIG_FRAGMENTS (space separated file list)
656// It creates a filegroup for each file in the fragment list
Spandan Das312cc412024-10-29 18:20:11 +0000657// The filegroup modules are then added to `linker_config_srcs` of the autogenerated vendor `android_filesystem`.
Spandan Das8fe68dc2024-10-29 18:20:11 +0000658func (f *filesystemCreator) createLinkerConfigSourceFilegroups(ctx android.LoadHookContext, partitionType string) []string {
Spandan Das312cc412024-10-29 18:20:11 +0000659 ret := []string{}
660 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Spandan Das8fe68dc2024-10-29 18:20:11 +0000661 var linkerConfigSrcs []string
662 if partitionType == "vendor" {
663 linkerConfigSrcs = android.FirstUniqueStrings(partitionVars.VendorLinkerConfigSrcs)
664 } else if partitionType == "product" {
665 linkerConfigSrcs = android.FirstUniqueStrings(partitionVars.ProductLinkerConfigSrcs)
666 } else {
667 ctx.ModuleErrorf("linker.config.pb is only supported for vendor and product partitions. For system partition, use `android_system_image`")
668 }
669
670 if len(linkerConfigSrcs) > 0 {
Spandan Das312cc412024-10-29 18:20:11 +0000671 // Create a filegroup, and add `:<filegroup_name>` to ret.
672 for index, linkerConfigSrc := range linkerConfigSrcs {
673 dir := filepath.Dir(linkerConfigSrc)
674 base := filepath.Base(linkerConfigSrc)
Spandan Das8fe68dc2024-10-29 18:20:11 +0000675 fgName := generatedModuleName(ctx.Config(), fmt.Sprintf("%s-linker-config-src%s", partitionType, strconv.Itoa(index)))
Spandan Das312cc412024-10-29 18:20:11 +0000676 srcs := []string{base}
677 fgProps := &struct {
678 Name *string
679 Srcs proptools.Configurable[[]string]
680 }{
681 Name: proptools.StringPtr(fgName),
682 Srcs: proptools.NewSimpleConfigurable(srcs),
683 }
684 ctx.CreateModuleInDirectory(
685 android.FileGroupFactory,
686 dir,
687 fgProps,
688 )
689 ret = append(ret, ":"+fgName)
690 }
691 }
692 return ret
693}
694
mrziwang4b0ca972024-10-17 14:56:19 -0700695type filesystemBaseProperty struct {
696 Name *string
697 Compile_multilib *string
Cole Faust3552eb62024-11-06 18:07:26 -0800698 Visibility []string
mrziwang4b0ca972024-10-17 14:56:19 -0700699}
700
701func generateBaseProps(namePtr *string) *filesystemBaseProperty {
702 return &filesystemBaseProperty{
703 Name: namePtr,
704 Compile_multilib: proptools.StringPtr("both"),
Cole Faust3552eb62024-11-06 18:07:26 -0800705 // The vbmeta modules are currently in the root directory and depend on the partitions
706 Visibility: []string{"//.", "//build/soong:__subpackages__"},
mrziwang4b0ca972024-10-17 14:56:19 -0700707 }
708}
709
710func generateFsProps(ctx android.EarlyModuleContext, partitionType string) (*filesystem.FilesystemProperties, bool) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700711 fsProps := &filesystem.FilesystemProperties{}
712
mrziwang4b0ca972024-10-17 14:56:19 -0700713 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Cole Faust0c4b4152024-11-20 16:42:53 -0800714 var avbInfo avbInfo
Cole Faust76a6e952024-11-07 16:56:45 -0800715 var fsType string
716 if strings.Contains(partitionType, "ramdisk") {
717 fsType = "compressed_cpio"
718 } else {
Cole Faust953476f2024-11-14 14:11:29 -0800719 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
Cole Faust76a6e952024-11-07 16:56:45 -0800720 fsType = specificPartitionVars.BoardFileSystemType
Cole Faust0c4b4152024-11-20 16:42:53 -0800721 avbInfo = getAvbInfo(ctx.Config(), partitionType)
Cole Faust953476f2024-11-14 14:11:29 -0800722 if fsType == "" {
723 fsType = "ext4" //default
724 }
Cole Faust76a6e952024-11-07 16:56:45 -0800725 }
Cole Faust76a6e952024-11-07 16:56:45 -0800726
mrziwang4b0ca972024-10-17 14:56:19 -0700727 fsProps.Type = proptools.StringPtr(fsType)
728 if filesystem.GetFsTypeFromString(ctx, *fsProps.Type).IsUnknown() {
729 // Currently the android_filesystem module type only supports a handful of FS types like ext4, erofs
730 return nil, false
731 }
732
Cole Faust3e730972024-12-03 13:12:08 -0800733 if *fsProps.Type == "erofs" {
734 if partitionVars.BoardErofsCompressor != "" {
735 fsProps.Erofs.Compressor = proptools.StringPtr(partitionVars.BoardErofsCompressor)
736 }
737 if partitionVars.BoardErofsCompressorHints != "" {
738 fsProps.Erofs.Compress_hints = proptools.StringPtr(":soong_generated_board_erofs_compress_hints_filegroup")
739 }
740 }
741
Cole Faust92ccbe22024-10-03 14:38:37 -0700742 // Don't build this module on checkbuilds, the soong-built partitions are still in-progress
743 // and sometimes don't build.
744 fsProps.Unchecked_module = proptools.BoolPtr(true)
745
Jihoon Kang98047cf2024-10-02 17:13:54 +0000746 // BOARD_AVB_ENABLE
Cole Faust0c4b4152024-11-20 16:42:53 -0800747 fsProps.Use_avb = avbInfo.avbEnable
Jihoon Kang98047cf2024-10-02 17:13:54 +0000748 // BOARD_AVB_KEY_PATH
Cole Faust0c4b4152024-11-20 16:42:53 -0800749 fsProps.Avb_private_key = avbInfo.avbkeyFilegroup
Jihoon Kang98047cf2024-10-02 17:13:54 +0000750 // BOARD_AVB_ALGORITHM
Cole Faust0c4b4152024-11-20 16:42:53 -0800751 fsProps.Avb_algorithm = avbInfo.avbAlgorithm
Jihoon Kang98047cf2024-10-02 17:13:54 +0000752 // BOARD_AVB_SYSTEM_ROLLBACK_INDEX
Cole Faust0c4b4152024-11-20 16:42:53 -0800753 fsProps.Rollback_index = avbInfo.avbRollbackIndex
Cole Fauste1676122024-12-03 17:32:25 -0800754 fsProps.Avb_hash_algorithm = avbInfo.avbHashAlgorithm
Jihoon Kang98047cf2024-10-02 17:13:54 +0000755
Cole Faust92ccbe22024-10-03 14:38:37 -0700756 fsProps.Partition_name = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000757
Cole Faust68382192024-11-19 10:36:03 -0800758 if !strings.Contains(partitionType, "ramdisk") {
759 fsProps.Base_dir = proptools.StringPtr(partitionType)
760 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000761
Jihoon Kang0d545b82024-10-11 00:21:57 +0000762 fsProps.Is_auto_generated = proptools.BoolPtr(true)
763
Spandan Das71be42d2024-11-20 18:34:16 +0000764 partitionSpecificFsProps(ctx, fsProps, partitionVars, partitionType)
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000765
Jihoon Kang98047cf2024-10-02 17:13:54 +0000766 // system_image properties that are not set:
767 // - filesystemProperties.Avb_hash_algorithm
768 // - filesystemProperties.File_contexts
769 // - filesystemProperties.Dirs
770 // - filesystemProperties.Symlinks
771 // - filesystemProperties.Fake_timestamp
772 // - filesystemProperties.Uuid
773 // - filesystemProperties.Mount_point
774 // - filesystemProperties.Include_make_built_files
775 // - filesystemProperties.Build_logtags
Jihoon Kang98047cf2024-10-02 17:13:54 +0000776 // - systemImageProperties.Linker_config_src
mrziwang4b0ca972024-10-17 14:56:19 -0700777
778 return fsProps, true
Cole Faust92ccbe22024-10-03 14:38:37 -0700779}
780
Cole Faust0c4b4152024-11-20 16:42:53 -0800781type avbInfo struct {
782 avbEnable *bool
783 avbKeyPath *string
784 avbkeyFilegroup *string
785 avbAlgorithm *string
786 avbRollbackIndex *int64
787 avbMode *string
Cole Fauste1676122024-12-03 17:32:25 -0800788 avbHashAlgorithm *string
Cole Faust0c4b4152024-11-20 16:42:53 -0800789}
790
791func getAvbInfo(config android.Config, partitionType string) avbInfo {
792 partitionVars := config.ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
793 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
794 var result avbInfo
795 boardAvbEnable := partitionVars.BoardAvbEnable
796 if boardAvbEnable {
797 result.avbEnable = proptools.BoolPtr(true)
Cole Fauste1676122024-12-03 17:32:25 -0800798 // There are "global" and "specific" copies of a lot of these variables. Sometimes they
799 // choose the specific and then fall back to the global one if it's not set, other times
800 // the global one actually only applies to the vbmeta partition.
801 if partitionType == "vbmeta" {
802 if partitionVars.BoardAvbKeyPath != "" {
803 result.avbKeyPath = proptools.StringPtr(partitionVars.BoardAvbKeyPath)
804 }
805 if partitionVars.BoardAvbRollbackIndex != "" {
806 parsed, err := strconv.ParseInt(partitionVars.BoardAvbRollbackIndex, 10, 64)
807 if err != nil {
808 panic(fmt.Sprintf("Rollback index must be an int, got %s", partitionVars.BoardAvbRollbackIndex))
809 }
810 result.avbRollbackIndex = &parsed
811 }
812 }
Cole Faust0c4b4152024-11-20 16:42:53 -0800813 if specificPartitionVars.BoardAvbKeyPath != "" {
814 result.avbKeyPath = proptools.StringPtr(specificPartitionVars.BoardAvbKeyPath)
Cole Faust0c4b4152024-11-20 16:42:53 -0800815 }
816 if specificPartitionVars.BoardAvbAlgorithm != "" {
817 result.avbAlgorithm = proptools.StringPtr(specificPartitionVars.BoardAvbAlgorithm)
818 } else if partitionVars.BoardAvbAlgorithm != "" {
819 result.avbAlgorithm = proptools.StringPtr(partitionVars.BoardAvbAlgorithm)
820 }
821 if specificPartitionVars.BoardAvbRollbackIndex != "" {
822 parsed, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64)
823 if err != nil {
824 panic(fmt.Sprintf("Rollback index must be an int, got %s", specificPartitionVars.BoardAvbRollbackIndex))
825 }
826 result.avbRollbackIndex = &parsed
Cole Fauste1676122024-12-03 17:32:25 -0800827 }
828 if specificPartitionVars.BoardAvbRollbackIndex != "" {
829 parsed, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64)
Cole Faust0c4b4152024-11-20 16:42:53 -0800830 if err != nil {
Cole Fauste1676122024-12-03 17:32:25 -0800831 panic(fmt.Sprintf("Rollback index must be an int, got %s", specificPartitionVars.BoardAvbRollbackIndex))
Cole Faust0c4b4152024-11-20 16:42:53 -0800832 }
833 result.avbRollbackIndex = &parsed
834 }
Cole Fauste1676122024-12-03 17:32:25 -0800835
836 // Make allows you to pass arbitrary arguments to avbtool via this variable, but in practice
837 // it's only used for --hash_algorithm. The soong module has a dedicated property for the
838 // hashtree algorithm, and doesn't allow custom arguments, so just extract the hashtree
839 // algorithm out of the arbitrary arguments.
840 addHashtreeFooterArgs := strings.Split(specificPartitionVars.BoardAvbAddHashtreeFooterArgs, " ")
841 if i := slices.Index(addHashtreeFooterArgs, "--hash_algorithm"); i >= 0 {
842 result.avbHashAlgorithm = &addHashtreeFooterArgs[i+1]
843 }
844
Cole Faust0c4b4152024-11-20 16:42:53 -0800845 result.avbMode = proptools.StringPtr("make_legacy")
846 }
847 if result.avbKeyPath != nil {
848 fsGenState := config.Get(fsGenStateOnceKey).(*FsGenState)
849 filegroup := fsGenState.avbKeyFilegroups[*result.avbKeyPath]
850 result.avbkeyFilegroup = proptools.StringPtr(":" + filegroup)
851 }
852 return result
853}
854
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800855func (f *filesystemCreator) createFileListDiffTest(ctx android.ModuleContext, partitionType string) android.Path {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000856 partitionModuleName := generatedModuleNameForPartition(ctx.Config(), partitionType)
Cole Faust92ccbe22024-10-03 14:38:37 -0700857 systemImage := ctx.GetDirectDepWithTag(partitionModuleName, generatedFilesystemDepTag)
858 filesystemInfo, ok := android.OtherModuleProvider(ctx, systemImage, filesystem.FilesystemProvider)
859 if !ok {
860 ctx.ModuleErrorf("Expected module %s to provide FileysystemInfo", partitionModuleName)
861 }
862 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 +0000863 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700864
865 builder := android.NewRuleBuilder(pctx, ctx)
866 builder.Command().BuiltTool("file_list_diff").
867 Input(makeFileList).
868 Input(filesystemInfo.FileListFile).
Cole Faust56301572024-11-07 15:22:42 -0800869 Text(partitionModuleName)
Cole Faust92ccbe22024-10-03 14:38:37 -0700870 builder.Command().Text("touch").Output(diffTestResultFile)
871 builder.Build(partitionModuleName+" diff test", partitionModuleName+" diff test")
872 return diffTestResultFile
873}
874
875func createFailingCommand(ctx android.ModuleContext, message string) android.Path {
876 hasher := sha256.New()
877 hasher.Write([]byte(message))
878 filename := fmt.Sprintf("failing_command_%x.txt", hasher.Sum(nil))
879 file := android.PathForModuleOut(ctx, filename)
880 builder := android.NewRuleBuilder(pctx, ctx)
881 builder.Command().Textf("echo %s", proptools.NinjaAndShellEscape(message))
882 builder.Command().Text("exit 1 #").Output(file)
883 builder.Build("failing command "+filename, "failing command "+filename)
884 return file
885}
886
Cole Faust3552eb62024-11-06 18:07:26 -0800887func createVbmetaDiff(ctx android.ModuleContext, vbmetaModuleName string, vbmetaPartitionName string) android.Path {
888 vbmetaModule := ctx.GetDirectDepWithTag(vbmetaModuleName, generatedVbmetaPartitionDepTag)
889 outputFilesProvider, ok := android.OtherModuleProvider(ctx, vbmetaModule, android.OutputFilesProvider)
890 if !ok {
891 ctx.ModuleErrorf("Expected module %s to provide OutputFiles", vbmetaModule)
892 }
893 if len(outputFilesProvider.DefaultOutputFiles) != 1 {
894 ctx.ModuleErrorf("Expected 1 output file from module %s", vbmetaModule)
895 }
896 soongVbMetaFile := outputFilesProvider.DefaultOutputFiles[0]
897 makeVbmetaFile := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/%s.img", ctx.Config().DeviceName(), vbmetaPartitionName))
898
899 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", vbmetaModuleName))
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800900 createDiffTest(ctx, diffTestResultFile, soongVbMetaFile, makeVbmetaFile)
901 return diffTestResultFile
902}
903
904func createDiffTest(ctx android.ModuleContext, diffTestResultFile android.WritablePath, file1 android.Path, file2 android.Path) {
Cole Faust3552eb62024-11-06 18:07:26 -0800905 builder := android.NewRuleBuilder(pctx, ctx)
906 builder.Command().Text("diff").
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800907 Input(file1).
908 Input(file2)
Cole Faust3552eb62024-11-06 18:07:26 -0800909 builder.Command().Text("touch").Output(diffTestResultFile)
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800910 builder.Build("diff test "+diffTestResultFile.String(), "diff test")
Cole Faust3552eb62024-11-06 18:07:26 -0800911}
912
Cole Faust92ccbe22024-10-03 14:38:37 -0700913type systemImageDepTagType struct {
914 blueprint.BaseDependencyTag
915}
916
917var generatedFilesystemDepTag systemImageDepTagType
Cole Faust3552eb62024-11-06 18:07:26 -0800918var generatedVbmetaPartitionDepTag systemImageDepTagType
Cole Faust92ccbe22024-10-03 14:38:37 -0700919
920func (f *filesystemCreator) DepsMutator(ctx android.BottomUpMutatorContext) {
921 for _, partitionType := range f.properties.Generated_partition_types {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000922 ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, generatedModuleNameForPartition(ctx.Config(), partitionType))
Cole Faust92ccbe22024-10-03 14:38:37 -0700923 }
Cole Faust3552eb62024-11-06 18:07:26 -0800924 for _, vbmetaModule := range f.properties.Vbmeta_module_names {
925 ctx.AddDependency(ctx.Module(), generatedVbmetaPartitionDepTag, vbmetaModule)
926 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000927}
928
929func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700930 if ctx.ModuleDir() != "build/soong/fsgen" {
931 ctx.ModuleErrorf("There can only be one soong_filesystem_creator in build/soong/fsgen")
932 }
933 f.HideFromMake()
Jihoon Kang98047cf2024-10-02 17:13:54 +0000934
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000935 var content strings.Builder
936 generatedBp := android.PathForModuleOut(ctx, "soong_generated_product_config.bp")
937 for _, partition := range ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions {
938 content.WriteString(generateBpContent(ctx, partition))
939 content.WriteString("\n")
940 }
941 android.WriteFileRule(ctx, generatedBp, content.String())
942
mrziwang8f86c882024-10-03 12:34:33 -0700943 ctx.Phony("product_config_to_bp", generatedBp)
944
Cole Faust92ccbe22024-10-03 14:38:37 -0700945 var diffTestFiles []android.Path
946 for _, partitionType := range f.properties.Generated_partition_types {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800947 diffTestFile := f.createFileListDiffTest(ctx, partitionType)
Jihoon Kang72f812f2024-10-17 18:46:24 +0000948 diffTestFiles = append(diffTestFiles, diffTestFile)
949 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700950 }
951 for _, partitionType := range f.properties.Unsupported_partition_types {
Jihoon Kang72f812f2024-10-17 18:46:24 +0000952 diffTestFile := createFailingCommand(ctx, fmt.Sprintf("Couldn't build %s partition", partitionType))
953 diffTestFiles = append(diffTestFiles, diffTestFile)
954 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700955 }
Cole Faust3552eb62024-11-06 18:07:26 -0800956 for i, vbmetaModule := range f.properties.Vbmeta_module_names {
957 diffTestFile := createVbmetaDiff(ctx, vbmetaModule, f.properties.Vbmeta_partition_names[i])
958 diffTestFiles = append(diffTestFiles, diffTestFile)
959 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", f.properties.Vbmeta_partition_names[i]), diffTestFile)
960 }
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800961 if f.properties.Boot_image != "" {
962 diffTestFile := android.PathForModuleOut(ctx, "boot_diff_test.txt")
963 soongBootImg := android.PathForModuleSrc(ctx, f.properties.Boot_image)
964 makeBootImage := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/boot.img", ctx.Config().DeviceName()))
965 createDiffTest(ctx, diffTestFile, soongBootImg, makeBootImage)
966 diffTestFiles = append(diffTestFiles, diffTestFile)
967 ctx.Phony("soong_generated_boot_filesystem_test", diffTestFile)
968 }
Cole Faust24938e22024-11-18 14:01:58 -0800969 if f.properties.Vendor_boot_image != "" {
970 diffTestFile := android.PathForModuleOut(ctx, "vendor_boot_diff_test.txt")
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000971 soongBootImg := android.PathForModuleSrc(ctx, f.properties.Vendor_boot_image)
Cole Faust24938e22024-11-18 14:01:58 -0800972 makeBootImage := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/vendor_boot.img", ctx.Config().DeviceName()))
973 createDiffTest(ctx, diffTestFile, soongBootImg, makeBootImage)
974 diffTestFiles = append(diffTestFiles, diffTestFile)
975 ctx.Phony("soong_generated_vendor_boot_filesystem_test", diffTestFile)
976 }
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000977 if f.properties.Init_boot_image != "" {
978 diffTestFile := android.PathForModuleOut(ctx, "init_boot_diff_test.txt")
979 soongBootImg := android.PathForModuleSrc(ctx, f.properties.Init_boot_image)
980 makeBootImage := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/init_boot.img", ctx.Config().DeviceName()))
981 createDiffTest(ctx, diffTestFile, soongBootImg, makeBootImage)
982 diffTestFiles = append(diffTestFiles, diffTestFile)
983 ctx.Phony("soong_generated_init_boot_filesystem_test", diffTestFile)
984 }
Cole Faust92ccbe22024-10-03 14:38:37 -0700985 ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000986}
mrziwang8f86c882024-10-03 12:34:33 -0700987
mrziwang8f86c882024-10-03 12:34:33 -0700988func generateBpContent(ctx android.EarlyModuleContext, partitionType string) string {
mrziwang4b0ca972024-10-17 14:56:19 -0700989 fsProps, fsTypeSupported := generateFsProps(ctx, partitionType)
990 if !fsTypeSupported {
991 return ""
mrziwang8f86c882024-10-03 12:34:33 -0700992 }
993
mrziwang4b0ca972024-10-17 14:56:19 -0700994 baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)))
Jihoon Kang0d7b0112024-11-13 20:44:05 +0000995 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
996 deps := fsGenState.fsDeps[partitionType]
997 highPriorityDeps := fsGenState.generatedPrebuiltEtcModuleNames
998 depProps := generateDepStruct(*deps, highPriorityDeps)
mrziwang8f86c882024-10-03 12:34:33 -0700999
mrziwang4b0ca972024-10-17 14:56:19 -07001000 result, err := proptools.RepackProperties([]interface{}{baseProps, fsProps, depProps})
mrziwang8f86c882024-10-03 12:34:33 -07001001 if err != nil {
Cole Faustae3e1d32024-11-05 13:22:50 -08001002 ctx.ModuleErrorf("%s", err.Error())
1003 return ""
mrziwang8f86c882024-10-03 12:34:33 -07001004 }
1005
Jihoon Kang4e5d8de2024-10-19 01:59:58 +00001006 moduleType := "android_filesystem"
1007 if partitionType == "system" {
1008 moduleType = "android_system_image"
1009 }
1010
mrziwang8f86c882024-10-03 12:34:33 -07001011 file := &parser.File{
1012 Defs: []parser.Definition{
1013 &parser.Module{
Jihoon Kang4e5d8de2024-10-19 01:59:58 +00001014 Type: moduleType,
mrziwang8f86c882024-10-03 12:34:33 -07001015 Map: *result,
1016 },
1017 },
1018 }
1019 bytes, err := parser.Print(file)
1020 if err != nil {
1021 ctx.ModuleErrorf(err.Error())
1022 }
1023 return strings.TrimSpace(string(bytes))
1024}