blob: 8325b1ed530b55426ff87c3f62a25bb152349f59 [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 }
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800111 return generatedPartitions
112}
113
Jihoon Kang98047cf2024-10-02 17:13:54 +0000114func (f *filesystemCreator) createInternalModules(ctx android.LoadHookContext) {
Cole Faust3552eb62024-11-06 18:07:26 -0800115 soongGeneratedPartitions := generatedPartitions(ctx)
116 finalSoongGeneratedPartitions := make([]string, 0, len(soongGeneratedPartitions))
117 for _, partitionType := range soongGeneratedPartitions {
Cole Faust92ccbe22024-10-03 14:38:37 -0700118 if f.createPartition(ctx, partitionType) {
119 f.properties.Generated_partition_types = append(f.properties.Generated_partition_types, partitionType)
Cole Faust3552eb62024-11-06 18:07:26 -0800120 finalSoongGeneratedPartitions = append(finalSoongGeneratedPartitions, partitionType)
Cole Faust92ccbe22024-10-03 14:38:37 -0700121 } else {
122 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, partitionType)
123 }
124 }
Cole Faust3552eb62024-11-06 18:07:26 -0800125
Cole Faust24938e22024-11-18 14:01:58 -0800126 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Jihoon Kang70c1c682024-11-20 23:58:38 +0000127 dtbImg := createDtbImgFilegroup(ctx)
128
Cole Faust24938e22024-11-18 14:01:58 -0800129 if buildingBootImage(partitionVars) {
Jihoon Kang70c1c682024-11-20 23:58:38 +0000130 if createBootImage(ctx, dtbImg) {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800131 f.properties.Boot_image = ":" + generatedModuleNameForPartition(ctx.Config(), "boot")
132 } else {
133 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, "boot")
134 }
135 }
Cole Faust24938e22024-11-18 14:01:58 -0800136 if buildingVendorBootImage(partitionVars) {
Jihoon Kang70c1c682024-11-20 23:58:38 +0000137 if createVendorBootImage(ctx, dtbImg) {
Cole Faust24938e22024-11-18 14:01:58 -0800138 f.properties.Vendor_boot_image = ":" + generatedModuleNameForPartition(ctx.Config(), "vendor_boot")
139 } else {
140 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, "vendor_boot")
141 }
142 }
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000143 if buildingInitBootImage(partitionVars) {
144 if createInitBootImage(ctx) {
145 f.properties.Init_boot_image = ":" + generatedModuleNameForPartition(ctx.Config(), "init_boot")
146 } else {
147 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, "init_boot")
148 }
149 }
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800150
Cole Faust3552eb62024-11-06 18:07:26 -0800151 for _, x := range createVbmetaPartitions(ctx, finalSoongGeneratedPartitions) {
152 f.properties.Vbmeta_module_names = append(f.properties.Vbmeta_module_names, x.moduleName)
153 f.properties.Vbmeta_partition_names = append(f.properties.Vbmeta_partition_names, x.partitionName)
154 }
155
156 ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions = finalSoongGeneratedPartitions
157 f.createDeviceModule(ctx, finalSoongGeneratedPartitions, f.properties.Vbmeta_module_names)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000158}
159
Jihoon Kang0d545b82024-10-11 00:21:57 +0000160func generatedModuleName(cfg android.Config, suffix string) string {
Cole Faust92ccbe22024-10-03 14:38:37 -0700161 prefix := "soong"
162 if cfg.HasDeviceProduct() {
163 prefix = cfg.DeviceProduct()
164 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000165 return fmt.Sprintf("%s_generated_%s", prefix, suffix)
166}
167
Jihoon Kang0d545b82024-10-11 00:21:57 +0000168func generatedModuleNameForPartition(cfg android.Config, partitionType string) string {
169 return generatedModuleName(cfg, fmt.Sprintf("%s_image", partitionType))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000170}
171
Cole Faust3552eb62024-11-06 18:07:26 -0800172func (f *filesystemCreator) createDeviceModule(
173 ctx android.LoadHookContext,
174 generatedPartitionTypes []string,
175 vbmetaPartitions []string,
176) {
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000177 baseProps := &struct {
178 Name *string
179 }{
Jihoon Kang0d545b82024-10-11 00:21:57 +0000180 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "device")),
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000181 }
182
Priyanka Advani (xWF)dafaa7f2024-10-21 22:55:13 +0000183 // Currently, only the system and system_ext partition module is created.
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000184 partitionProps := &filesystem.PartitionNameProperties{}
Cole Faust3552eb62024-11-06 18:07:26 -0800185 if android.InList("system", generatedPartitionTypes) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000186 partitionProps.System_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system"))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000187 }
Cole Faust3552eb62024-11-06 18:07:26 -0800188 if android.InList("system_ext", generatedPartitionTypes) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000189 partitionProps.System_ext_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_ext"))
Spandan Das7a46f6c2024-10-14 18:41:18 +0000190 }
Cole Faust3552eb62024-11-06 18:07:26 -0800191 if android.InList("vendor", generatedPartitionTypes) {
Spandan Dase3b65312024-10-22 00:27:27 +0000192 partitionProps.Vendor_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "vendor"))
193 }
Cole Faust3552eb62024-11-06 18:07:26 -0800194 if android.InList("product", generatedPartitionTypes) {
Jihoon Kang6dd13b62024-10-22 23:21:02 +0000195 partitionProps.Product_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "product"))
196 }
Cole Faust3552eb62024-11-06 18:07:26 -0800197 if android.InList("odm", generatedPartitionTypes) {
Spandan Dasc5717162024-11-01 18:33:57 +0000198 partitionProps.Odm_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "odm"))
199 }
mrziwang23ba8762024-11-07 16:21:53 -0800200 if android.InList("userdata", f.properties.Generated_partition_types) {
201 partitionProps.Userdata_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "userdata"))
202 }
Cole Faust3552eb62024-11-06 18:07:26 -0800203 partitionProps.Vbmeta_partitions = vbmetaPartitions
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000204
205 ctx.CreateModule(filesystem.AndroidDeviceFactory, baseProps, partitionProps)
Cole Faust92ccbe22024-10-03 14:38:37 -0700206}
207
Jihoon Kangd098d442024-11-19 00:03:22 +0000208func partitionSpecificFsProps(fsProps *filesystem.FilesystemProperties, partitionVars android.PartitionVariables, partitionType string) {
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000209 switch partitionType {
210 case "system":
211 fsProps.Build_logtags = proptools.BoolPtr(true)
212 // https://source.corp.google.com/h/googleplex-android/platform/build//639d79f5012a6542ab1f733b0697db45761ab0f3:core/packaging/flags.mk;l=21;drc=5ba8a8b77507f93aa48cc61c5ba3f31a4d0cbf37;bpv=1;bpt=0
213 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
Justin Yuned3dbce2024-11-15 11:57:24 +0900214 // Identical to that of the aosp_shared_system_image
Spandan Dasa8fa6b42024-10-23 00:45:29 +0000215 fsProps.Fsverity.Inputs = []string{
216 "etc/boot-image.prof",
217 "etc/dirty-image-objects",
218 "etc/preloaded-classes",
219 "etc/classpaths/*.pb",
220 "framework/*",
221 "framework/*/*", // framework/{arch}
222 "framework/oat/*/*", // framework/oat/{arch}
223 }
224 fsProps.Fsverity.Libs = []string{":framework-res{.export-package.apk}"}
Cole Faust1d4e76c2024-11-26 14:15:29 -0800225 // Most of the symlinks and directories listed here originate from create_root_structure.mk,
226 // but the handwritten generic system image also recreates them:
227 // 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 -0800228 // TODO(b/377734331): only generate the symlinks if the relevant partitions exist
229 fsProps.Symlinks = []filesystem.SymlinkDefinition{
230 filesystem.SymlinkDefinition{
Cole Faust1d4e76c2024-11-26 14:15:29 -0800231 Target: proptools.StringPtr("/system/bin/init"),
232 Name: proptools.StringPtr("init"),
233 },
234 filesystem.SymlinkDefinition{
235 Target: proptools.StringPtr("/system/etc"),
236 Name: proptools.StringPtr("etc"),
237 },
238 filesystem.SymlinkDefinition{
239 Target: proptools.StringPtr("/system/bin"),
240 Name: proptools.StringPtr("bin"),
241 },
242 filesystem.SymlinkDefinition{
243 Target: proptools.StringPtr("/data/user_de/0/com.android.shell/files/bugreports"),
244 Name: proptools.StringPtr("bugreports"),
245 },
246 filesystem.SymlinkDefinition{
247 Target: proptools.StringPtr("/sys/kernel/debug"),
248 Name: proptools.StringPtr("d"),
249 },
250 filesystem.SymlinkDefinition{
251 Target: proptools.StringPtr("/storage/self/primary"),
252 Name: proptools.StringPtr("sdcard"),
253 },
254 filesystem.SymlinkDefinition{
255 Target: proptools.StringPtr("/product/etc/security/adb_keys"),
256 Name: proptools.StringPtr("adb_keys"),
257 },
258 filesystem.SymlinkDefinition{
259 Target: proptools.StringPtr("/vendor/odm/app"),
260 Name: proptools.StringPtr("odm/app"),
261 },
262 filesystem.SymlinkDefinition{
263 Target: proptools.StringPtr("/vendor/odm/bin"),
264 Name: proptools.StringPtr("odm/bin"),
265 },
266 filesystem.SymlinkDefinition{
267 Target: proptools.StringPtr("/vendor/odm/etc"),
268 Name: proptools.StringPtr("odm/etc"),
269 },
270 filesystem.SymlinkDefinition{
271 Target: proptools.StringPtr("/vendor/odm/firmware"),
272 Name: proptools.StringPtr("odm/firmware"),
273 },
274 filesystem.SymlinkDefinition{
275 Target: proptools.StringPtr("/vendor/odm/framework"),
276 Name: proptools.StringPtr("odm/framework"),
277 },
278 filesystem.SymlinkDefinition{
279 Target: proptools.StringPtr("/vendor/odm/lib"),
280 Name: proptools.StringPtr("odm/lib"),
281 },
282 filesystem.SymlinkDefinition{
283 Target: proptools.StringPtr("/vendor/odm/lib64"),
284 Name: proptools.StringPtr("odm/lib64"),
285 },
286 filesystem.SymlinkDefinition{
287 Target: proptools.StringPtr("/vendor/odm/overlay"),
288 Name: proptools.StringPtr("odm/overlay"),
289 },
290 filesystem.SymlinkDefinition{
291 Target: proptools.StringPtr("/vendor/odm/priv-app"),
292 Name: proptools.StringPtr("odm/priv-app"),
293 },
294 filesystem.SymlinkDefinition{
295 Target: proptools.StringPtr("/vendor/odm/usr"),
296 Name: proptools.StringPtr("odm/usr"),
297 },
298 filesystem.SymlinkDefinition{
mrziwang9afc2982024-11-05 14:29:48 -0800299 Target: proptools.StringPtr("/product"),
300 Name: proptools.StringPtr("system/product"),
301 },
302 filesystem.SymlinkDefinition{
303 Target: proptools.StringPtr("/system_ext"),
304 Name: proptools.StringPtr("system/system_ext"),
305 },
306 filesystem.SymlinkDefinition{
307 Target: proptools.StringPtr("/vendor"),
308 Name: proptools.StringPtr("system/vendor"),
309 },
310 filesystem.SymlinkDefinition{
311 Target: proptools.StringPtr("/system_dlkm/lib/modules"),
312 Name: proptools.StringPtr("system/lib/modules"),
313 },
Cole Faust1d4e76c2024-11-26 14:15:29 -0800314 filesystem.SymlinkDefinition{
315 Target: proptools.StringPtr("/data/cache"),
316 Name: proptools.StringPtr("cache"),
317 },
mrziwang9afc2982024-11-05 14:29:48 -0800318 }
Cole Faust1d4e76c2024-11-26 14:15:29 -0800319 fsProps.Dirs = proptools.NewSimpleConfigurable([]string{
320 // From generic_rootdirs in build/make/target/product/generic/Android.bp
321 "acct",
322 "apex",
323 "bootstrap-apex",
324 "config",
325 "data",
326 "data_mirror",
327 "debug_ramdisk",
328 "dev",
329 "linkerconfig",
330 "metadata",
331 "mnt",
332 "odm",
333 "odm_dlkm",
334 "oem",
335 "postinstall",
336 "proc",
337 "second_stage_resources",
338 "storage",
339 "sys",
340 "system",
341 "system_dlkm",
342 "tmp",
343 "vendor",
344 "vendor_dlkm",
345
346 // from android_rootdirs in build/make/target/product/generic/Android.bp
347 "system_ext",
348 "product",
349 })
Spandan Dasa8fa6b42024-10-23 00:45:29 +0000350 case "system_ext":
351 fsProps.Fsverity.Inputs = []string{
352 "framework/*",
353 "framework/*/*", // framework/{arch}
354 "framework/oat/*/*", // framework/oat/{arch}
355 }
356 fsProps.Fsverity.Libs = []string{":framework-res{.export-package.apk}"}
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000357 case "product":
358 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
359 case "vendor":
360 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
Spandan Das69464c32024-10-25 20:08:06 +0000361 fsProps.Symlinks = []filesystem.SymlinkDefinition{
362 filesystem.SymlinkDefinition{
363 Target: proptools.StringPtr("/odm"),
364 Name: proptools.StringPtr("vendor/odm"),
365 },
366 filesystem.SymlinkDefinition{
367 Target: proptools.StringPtr("/vendor_dlkm/lib/modules"),
368 Name: proptools.StringPtr("vendor/lib/modules"),
369 },
370 }
Spandan Dasc5717162024-11-01 18:33:57 +0000371 case "odm":
372 fsProps.Symlinks = []filesystem.SymlinkDefinition{
373 filesystem.SymlinkDefinition{
374 Target: proptools.StringPtr("/odm_dlkm/lib/modules"),
375 Name: proptools.StringPtr("odm/lib/modules"),
376 },
377 }
mrziwang23ba8762024-11-07 16:21:53 -0800378 case "userdata":
379 fsProps.Base_dir = proptools.StringPtr("data")
Jihoon Kangd098d442024-11-19 00:03:22 +0000380 case "ramdisk":
381 // Following the logic in https://cs.android.com/android/platform/superproject/main/+/c3c5063df32748a8806ce5da5dd0db158eab9ad9:build/make/core/Makefile;l=1307
382 fsProps.Dirs = android.NewSimpleConfigurable([]string{
383 "debug_ramdisk",
384 "dev",
385 "metadata",
386 "mnt",
387 "proc",
388 "second_stage_resources",
389 "sys",
390 })
391 if partitionVars.BoardUsesGenericKernelImage {
392 fsProps.Dirs.AppendSimpleValue([]string{
393 "first_stage_ramdisk/debug_ramdisk",
394 "first_stage_ramdisk/dev",
395 "first_stage_ramdisk/metadata",
396 "first_stage_ramdisk/mnt",
397 "first_stage_ramdisk/proc",
398 "first_stage_ramdisk/second_stage_resources",
399 "first_stage_ramdisk/sys",
400 })
401 }
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000402 }
403}
Spandan Dascbe641a2024-10-14 21:07:34 +0000404
Spandan Das5b493cd2024-11-07 20:55:56 +0000405var (
406 dlkmPartitions = []string{
407 "system_dlkm",
408 "vendor_dlkm",
409 "odm_dlkm",
410 }
411)
412
Cole Faust92ccbe22024-10-03 14:38:37 -0700413// Creates a soong module to build the given partition. Returns false if we can't support building
414// it.
415func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partitionType string) bool {
mrziwang4b0ca972024-10-17 14:56:19 -0700416 baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)))
417
418 fsProps, supported := generateFsProps(ctx, partitionType)
419 if !supported {
420 return false
mrziwanga077b942024-10-16 16:00:06 -0700421 }
mrziwanga077b942024-10-16 16:00:06 -0700422
Cole Faust7db05752024-11-21 13:30:41 -0800423 if partitionType == "vendor" || partitionType == "product" || partitionType == "system" {
Spandan Das2047a4c2024-11-11 21:24:58 +0000424 fsProps.Linker_config.Gen_linker_config = proptools.BoolPtr(true)
Cole Faust7db05752024-11-21 13:30:41 -0800425 if partitionType != "system" {
426 fsProps.Linker_config.Linker_config_srcs = f.createLinkerConfigSourceFilegroups(ctx, partitionType)
427 }
Spandan Das312cc412024-10-29 18:20:11 +0000428 }
429
Jihoon Kanga8fa0712024-11-26 23:11:07 +0000430 if android.InList(partitionType, append(dlkmPartitions, "vendor_ramdisk")) {
Spandan Das5b493cd2024-11-07 20:55:56 +0000431 f.createPrebuiltKernelModules(ctx, partitionType)
Spandan Das5e336422024-11-01 22:31:20 +0000432 }
433
mrziwang4b0ca972024-10-17 14:56:19 -0700434 var module android.Module
435 if partitionType == "system" {
436 module = ctx.CreateModule(filesystem.SystemImageFactory, baseProps, fsProps)
437 } else {
438 // Explicitly set the partition.
439 fsProps.Partition_type = proptools.StringPtr(partitionType)
440 module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps)
441 }
442 module.HideFromMake()
Spandan Das168098c2024-10-28 19:44:34 +0000443 if partitionType == "vendor" {
Spandan Das4cd93b52024-11-05 23:27:03 +0000444 f.createVendorBuildProp(ctx)
Spandan Das168098c2024-10-28 19:44:34 +0000445 }
mrziwang4b0ca972024-10-17 14:56:19 -0700446 return true
447}
448
Cole Faust953476f2024-11-14 14:11:29 -0800449// Creates filegroups for the files specified in BOARD_(partition_)AVB_KEY_PATH
450func (f *filesystemCreator) createAvbKeyFilegroups(ctx android.LoadHookContext) {
451 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
452 var files []string
453
454 if len(partitionVars.BoardAvbKeyPath) > 0 {
455 files = append(files, partitionVars.BoardAvbKeyPath)
456 }
457 for _, partition := range android.SortedKeys(partitionVars.PartitionQualifiedVariables) {
458 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partition]
459 if len(specificPartitionVars.BoardAvbKeyPath) > 0 {
460 files = append(files, specificPartitionVars.BoardAvbKeyPath)
461 }
462 }
463
464 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
465 for _, file := range files {
466 if _, ok := fsGenState.avbKeyFilegroups[file]; ok {
467 continue
468 }
469 if file == "external/avb/test/data/testkey_rsa4096.pem" {
470 // There already exists a checked-in filegroup for this commonly-used key, just use that
471 fsGenState.avbKeyFilegroups[file] = "avb_testkey_rsa4096"
472 continue
473 }
474 dir := filepath.Dir(file)
475 base := filepath.Base(file)
476 name := fmt.Sprintf("avb_key_%x", strings.ReplaceAll(file, "/", "_"))
477 ctx.CreateModuleInDirectory(
478 android.FileGroupFactory,
479 dir,
480 &struct {
481 Name *string
482 Srcs []string
483 Visibility []string
484 }{
485 Name: proptools.StringPtr(name),
486 Srcs: []string{base},
487 Visibility: []string{"//visibility:public"},
488 },
489 )
490 fsGenState.avbKeyFilegroups[file] = name
491 }
492}
493
Spandan Das5e336422024-11-01 22:31:20 +0000494// createPrebuiltKernelModules creates `prebuilt_kernel_modules`. These modules will be added to deps of the
Spandan Das7b25a512024-11-06 20:41:26 +0000495// autogenerated *_dlkm filsystem modules. Each _dlkm partition should have a single prebuilt_kernel_modules dependency.
496// This ensures that the depmod artifacts (modules.* installed in /lib/modules/) are generated with a complete view.
Spandan Das5b493cd2024-11-07 20:55:56 +0000497func (f *filesystemCreator) createPrebuiltKernelModules(ctx android.LoadHookContext, partitionType string) {
Spandan Das5e336422024-11-01 22:31:20 +0000498 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
Spandan Das7b25a512024-11-06 20:41:26 +0000499 name := generatedModuleName(ctx.Config(), fmt.Sprintf("%s-kernel-modules", partitionType))
500 props := &struct {
Spandan Das912d26b2024-11-06 19:35:17 +0000501 Name *string
502 Srcs []string
Spandan Das5b493cd2024-11-07 20:55:56 +0000503 System_deps []string
Spandan Das912d26b2024-11-06 19:35:17 +0000504 System_dlkm_specific *bool
Spandan Das5b493cd2024-11-07 20:55:56 +0000505 Vendor_dlkm_specific *bool
506 Odm_dlkm_specific *bool
Jihoon Kanga8fa0712024-11-26 23:11:07 +0000507 Vendor_ramdisk *bool
Spandan Das5b493cd2024-11-07 20:55:56 +0000508 Load_by_default *bool
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000509 Blocklist_file *string
Jihoon Kang72dd6fc2024-11-27 01:16:39 +0000510 Options_file *string
Spandan Das7b25a512024-11-06 20:41:26 +0000511 }{
512 Name: proptools.StringPtr(name),
Spandan Das5e336422024-11-01 22:31:20 +0000513 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000514 switch partitionType {
515 case "system_dlkm":
Spandan Das59ee5d72024-11-18 19:36:32 +0000516 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelModules).Strings()
Spandan Das912d26b2024-11-06 19:35:17 +0000517 props.System_dlkm_specific = proptools.BoolPtr(true)
Spandan Das5b493cd2024-11-07 20:55:56 +0000518 if len(ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelLoadModules) == 0 {
519 // Create empty modules.load file for system
520 // https://source.corp.google.com/h/googleplex-android/platform/build/+/ef55daac9954896161b26db4f3ef1781b5a5694c:core/Makefile;l=695-700;drc=549fe2a5162548bd8b47867d35f907eb22332023;bpv=1;bpt=0
521 props.Load_by_default = proptools.BoolPtr(false)
522 }
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000523 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelBlocklistFile; blocklistFile != "" {
524 props.Blocklist_file = proptools.StringPtr(blocklistFile)
525 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000526 case "vendor_dlkm":
Spandan Das59ee5d72024-11-18 19:36:32 +0000527 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorKernelModules).Strings()
Spandan Das5b493cd2024-11-07 20:55:56 +0000528 if len(ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelModules) > 0 {
529 props.System_deps = []string{":" + generatedModuleName(ctx.Config(), "system_dlkm-kernel-modules") + "{.modules}"}
530 }
531 props.Vendor_dlkm_specific = proptools.BoolPtr(true)
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000532 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorKernelBlocklistFile; blocklistFile != "" {
533 props.Blocklist_file = proptools.StringPtr(blocklistFile)
534 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000535 case "odm_dlkm":
Spandan Das59ee5d72024-11-18 19:36:32 +0000536 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.OdmKernelModules).Strings()
Spandan Das5b493cd2024-11-07 20:55:56 +0000537 props.Odm_dlkm_specific = proptools.BoolPtr(true)
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000538 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.OdmKernelBlocklistFile; blocklistFile != "" {
539 props.Blocklist_file = proptools.StringPtr(blocklistFile)
540 }
Jihoon Kanga8fa0712024-11-26 23:11:07 +0000541 case "vendor_ramdisk":
542 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorRamdiskKernelModules).Strings()
543 props.Vendor_ramdisk = proptools.BoolPtr(true)
544 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorRamdiskKernelBlocklistFile; blocklistFile != "" {
545 props.Blocklist_file = proptools.StringPtr(blocklistFile)
546 }
Jihoon Kang72dd6fc2024-11-27 01:16:39 +0000547 if optionsFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorRamdiskKernelOptionsFile; optionsFile != "" {
548 props.Options_file = proptools.StringPtr(optionsFile)
549 }
550
Spandan Das5b493cd2024-11-07 20:55:56 +0000551 default:
552 ctx.ModuleErrorf("DLKM is not supported for %s\n", partitionType)
Spandan Das912d26b2024-11-06 19:35:17 +0000553 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000554
555 if len(props.Srcs) == 0 {
556 return // do not generate `prebuilt_kernel_modules` if there are no sources
557 }
558
Spandan Das7b25a512024-11-06 20:41:26 +0000559 kernelModule := ctx.CreateModuleInDirectory(
560 kernel.PrebuiltKernelModulesFactory,
561 ".", // create in root directory for now
562 props,
563 )
564 kernelModule.HideFromMake()
565 // Add to deps
566 (*fsGenState.fsDeps[partitionType])[name] = defaultDepCandidateProps(ctx.Config())
Spandan Das5e336422024-11-01 22:31:20 +0000567}
568
Spandan Das4cd93b52024-11-05 23:27:03 +0000569// Create a build_prop and android_info module. This will be used to create /vendor/build.prop
570func (f *filesystemCreator) createVendorBuildProp(ctx android.LoadHookContext) {
571 // Create a android_info for vendor
572 // The board info files might be in a directory outside the root soong namespace, so create
573 // the module in "."
574 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
575 androidInfoProps := &struct {
576 Name *string
577 Board_info_files []string
578 Bootloader_board_name *string
579 }{
580 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "android-info.prop")),
581 Board_info_files: partitionVars.BoardInfoFiles,
582 }
583 if len(androidInfoProps.Board_info_files) == 0 {
584 androidInfoProps.Bootloader_board_name = proptools.StringPtr(partitionVars.BootLoaderBoardName)
585 }
586 androidInfoProp := ctx.CreateModuleInDirectory(
587 android.AndroidInfoFactory,
588 ".",
589 androidInfoProps,
590 )
591 androidInfoProp.HideFromMake()
592 // Create a build prop for vendor
593 vendorBuildProps := &struct {
594 Name *string
595 Vendor *bool
596 Stem *string
597 Product_config *string
598 Android_info *string
599 }{
600 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "vendor-build.prop")),
601 Vendor: proptools.BoolPtr(true),
602 Stem: proptools.StringPtr("build.prop"),
603 Product_config: proptools.StringPtr(":product_config"),
604 Android_info: proptools.StringPtr(":" + androidInfoProp.Name()),
605 }
606 vendorBuildProp := ctx.CreateModule(
607 android.BuildPropFactory,
608 vendorBuildProps,
609 )
610 vendorBuildProp.HideFromMake()
611}
612
Spandan Das8fe68dc2024-10-29 18:20:11 +0000613// createLinkerConfigSourceFilegroups creates filegroup modules to generate linker.config.pb for the following partitions
614// 1. vendor: Using PRODUCT_VENDOR_LINKER_CONFIG_FRAGMENTS (space separated file list)
615// 1. product: Using PRODUCT_PRODUCT_LINKER_CONFIG_FRAGMENTS (space separated file list)
616// It creates a filegroup for each file in the fragment list
Spandan Das312cc412024-10-29 18:20:11 +0000617// The filegroup modules are then added to `linker_config_srcs` of the autogenerated vendor `android_filesystem`.
Spandan Das8fe68dc2024-10-29 18:20:11 +0000618func (f *filesystemCreator) createLinkerConfigSourceFilegroups(ctx android.LoadHookContext, partitionType string) []string {
Spandan Das312cc412024-10-29 18:20:11 +0000619 ret := []string{}
620 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Spandan Das8fe68dc2024-10-29 18:20:11 +0000621 var linkerConfigSrcs []string
622 if partitionType == "vendor" {
623 linkerConfigSrcs = android.FirstUniqueStrings(partitionVars.VendorLinkerConfigSrcs)
624 } else if partitionType == "product" {
625 linkerConfigSrcs = android.FirstUniqueStrings(partitionVars.ProductLinkerConfigSrcs)
626 } else {
627 ctx.ModuleErrorf("linker.config.pb is only supported for vendor and product partitions. For system partition, use `android_system_image`")
628 }
629
630 if len(linkerConfigSrcs) > 0 {
Spandan Das312cc412024-10-29 18:20:11 +0000631 // Create a filegroup, and add `:<filegroup_name>` to ret.
632 for index, linkerConfigSrc := range linkerConfigSrcs {
633 dir := filepath.Dir(linkerConfigSrc)
634 base := filepath.Base(linkerConfigSrc)
Spandan Das8fe68dc2024-10-29 18:20:11 +0000635 fgName := generatedModuleName(ctx.Config(), fmt.Sprintf("%s-linker-config-src%s", partitionType, strconv.Itoa(index)))
Spandan Das312cc412024-10-29 18:20:11 +0000636 srcs := []string{base}
637 fgProps := &struct {
638 Name *string
639 Srcs proptools.Configurable[[]string]
640 }{
641 Name: proptools.StringPtr(fgName),
642 Srcs: proptools.NewSimpleConfigurable(srcs),
643 }
644 ctx.CreateModuleInDirectory(
645 android.FileGroupFactory,
646 dir,
647 fgProps,
648 )
649 ret = append(ret, ":"+fgName)
650 }
651 }
652 return ret
653}
654
mrziwang4b0ca972024-10-17 14:56:19 -0700655type filesystemBaseProperty struct {
656 Name *string
657 Compile_multilib *string
Cole Faust3552eb62024-11-06 18:07:26 -0800658 Visibility []string
mrziwang4b0ca972024-10-17 14:56:19 -0700659}
660
661func generateBaseProps(namePtr *string) *filesystemBaseProperty {
662 return &filesystemBaseProperty{
663 Name: namePtr,
664 Compile_multilib: proptools.StringPtr("both"),
Cole Faust3552eb62024-11-06 18:07:26 -0800665 // The vbmeta modules are currently in the root directory and depend on the partitions
666 Visibility: []string{"//.", "//build/soong:__subpackages__"},
mrziwang4b0ca972024-10-17 14:56:19 -0700667 }
668}
669
670func generateFsProps(ctx android.EarlyModuleContext, partitionType string) (*filesystem.FilesystemProperties, bool) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700671 fsProps := &filesystem.FilesystemProperties{}
672
mrziwang4b0ca972024-10-17 14:56:19 -0700673 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Cole Faust0c4b4152024-11-20 16:42:53 -0800674 var avbInfo avbInfo
Cole Faust76a6e952024-11-07 16:56:45 -0800675 var fsType string
676 if strings.Contains(partitionType, "ramdisk") {
677 fsType = "compressed_cpio"
678 } else {
Cole Faust953476f2024-11-14 14:11:29 -0800679 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
Cole Faust76a6e952024-11-07 16:56:45 -0800680 fsType = specificPartitionVars.BoardFileSystemType
Cole Faust0c4b4152024-11-20 16:42:53 -0800681 avbInfo = getAvbInfo(ctx.Config(), partitionType)
Cole Faust953476f2024-11-14 14:11:29 -0800682 if fsType == "" {
683 fsType = "ext4" //default
684 }
Cole Faust76a6e952024-11-07 16:56:45 -0800685 }
Cole Faust76a6e952024-11-07 16:56:45 -0800686
mrziwang4b0ca972024-10-17 14:56:19 -0700687 fsProps.Type = proptools.StringPtr(fsType)
688 if filesystem.GetFsTypeFromString(ctx, *fsProps.Type).IsUnknown() {
689 // Currently the android_filesystem module type only supports a handful of FS types like ext4, erofs
690 return nil, false
691 }
692
Cole Faust92ccbe22024-10-03 14:38:37 -0700693 // Don't build this module on checkbuilds, the soong-built partitions are still in-progress
694 // and sometimes don't build.
695 fsProps.Unchecked_module = proptools.BoolPtr(true)
696
Jihoon Kang98047cf2024-10-02 17:13:54 +0000697 // BOARD_AVB_ENABLE
Cole Faust0c4b4152024-11-20 16:42:53 -0800698 fsProps.Use_avb = avbInfo.avbEnable
Jihoon Kang98047cf2024-10-02 17:13:54 +0000699 // BOARD_AVB_KEY_PATH
Cole Faust0c4b4152024-11-20 16:42:53 -0800700 fsProps.Avb_private_key = avbInfo.avbkeyFilegroup
Jihoon Kang98047cf2024-10-02 17:13:54 +0000701 // BOARD_AVB_ALGORITHM
Cole Faust0c4b4152024-11-20 16:42:53 -0800702 fsProps.Avb_algorithm = avbInfo.avbAlgorithm
Jihoon Kang98047cf2024-10-02 17:13:54 +0000703 // BOARD_AVB_SYSTEM_ROLLBACK_INDEX
Cole Faust0c4b4152024-11-20 16:42:53 -0800704 fsProps.Rollback_index = avbInfo.avbRollbackIndex
Jihoon Kang98047cf2024-10-02 17:13:54 +0000705
Cole Faust92ccbe22024-10-03 14:38:37 -0700706 fsProps.Partition_name = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000707
Cole Faust68382192024-11-19 10:36:03 -0800708 if !strings.Contains(partitionType, "ramdisk") {
709 fsProps.Base_dir = proptools.StringPtr(partitionType)
710 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000711
Jihoon Kang0d545b82024-10-11 00:21:57 +0000712 fsProps.Is_auto_generated = proptools.BoolPtr(true)
713
Jihoon Kangd098d442024-11-19 00:03:22 +0000714 partitionSpecificFsProps(fsProps, partitionVars, partitionType)
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000715
Jihoon Kang98047cf2024-10-02 17:13:54 +0000716 // system_image properties that are not set:
717 // - filesystemProperties.Avb_hash_algorithm
718 // - filesystemProperties.File_contexts
719 // - filesystemProperties.Dirs
720 // - filesystemProperties.Symlinks
721 // - filesystemProperties.Fake_timestamp
722 // - filesystemProperties.Uuid
723 // - filesystemProperties.Mount_point
724 // - filesystemProperties.Include_make_built_files
725 // - filesystemProperties.Build_logtags
Jihoon Kang98047cf2024-10-02 17:13:54 +0000726 // - systemImageProperties.Linker_config_src
mrziwang4b0ca972024-10-17 14:56:19 -0700727
728 return fsProps, true
Cole Faust92ccbe22024-10-03 14:38:37 -0700729}
730
Cole Faust0c4b4152024-11-20 16:42:53 -0800731type avbInfo struct {
732 avbEnable *bool
733 avbKeyPath *string
734 avbkeyFilegroup *string
735 avbAlgorithm *string
736 avbRollbackIndex *int64
737 avbMode *string
738}
739
740func getAvbInfo(config android.Config, partitionType string) avbInfo {
741 partitionVars := config.ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
742 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
743 var result avbInfo
744 boardAvbEnable := partitionVars.BoardAvbEnable
745 if boardAvbEnable {
746 result.avbEnable = proptools.BoolPtr(true)
747 if specificPartitionVars.BoardAvbKeyPath != "" {
748 result.avbKeyPath = proptools.StringPtr(specificPartitionVars.BoardAvbKeyPath)
749 } else if partitionVars.BoardAvbKeyPath != "" {
750 result.avbKeyPath = proptools.StringPtr(partitionVars.BoardAvbKeyPath)
751 }
752 if specificPartitionVars.BoardAvbAlgorithm != "" {
753 result.avbAlgorithm = proptools.StringPtr(specificPartitionVars.BoardAvbAlgorithm)
754 } else if partitionVars.BoardAvbAlgorithm != "" {
755 result.avbAlgorithm = proptools.StringPtr(partitionVars.BoardAvbAlgorithm)
756 }
757 if specificPartitionVars.BoardAvbRollbackIndex != "" {
758 parsed, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64)
759 if err != nil {
760 panic(fmt.Sprintf("Rollback index must be an int, got %s", specificPartitionVars.BoardAvbRollbackIndex))
761 }
762 result.avbRollbackIndex = &parsed
763 } else if partitionVars.BoardAvbRollbackIndex != "" {
764 parsed, err := strconv.ParseInt(partitionVars.BoardAvbRollbackIndex, 10, 64)
765 if err != nil {
766 panic(fmt.Sprintf("Rollback index must be an int, got %s", partitionVars.BoardAvbRollbackIndex))
767 }
768 result.avbRollbackIndex = &parsed
769 }
770 result.avbMode = proptools.StringPtr("make_legacy")
771 }
772 if result.avbKeyPath != nil {
773 fsGenState := config.Get(fsGenStateOnceKey).(*FsGenState)
774 filegroup := fsGenState.avbKeyFilegroups[*result.avbKeyPath]
775 result.avbkeyFilegroup = proptools.StringPtr(":" + filegroup)
776 }
777 return result
778}
779
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800780func (f *filesystemCreator) createFileListDiffTest(ctx android.ModuleContext, partitionType string) android.Path {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000781 partitionModuleName := generatedModuleNameForPartition(ctx.Config(), partitionType)
Cole Faust92ccbe22024-10-03 14:38:37 -0700782 systemImage := ctx.GetDirectDepWithTag(partitionModuleName, generatedFilesystemDepTag)
783 filesystemInfo, ok := android.OtherModuleProvider(ctx, systemImage, filesystem.FilesystemProvider)
784 if !ok {
785 ctx.ModuleErrorf("Expected module %s to provide FileysystemInfo", partitionModuleName)
786 }
787 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 +0000788 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700789
790 builder := android.NewRuleBuilder(pctx, ctx)
791 builder.Command().BuiltTool("file_list_diff").
792 Input(makeFileList).
793 Input(filesystemInfo.FileListFile).
Cole Faust56301572024-11-07 15:22:42 -0800794 Text(partitionModuleName)
Cole Faust92ccbe22024-10-03 14:38:37 -0700795 builder.Command().Text("touch").Output(diffTestResultFile)
796 builder.Build(partitionModuleName+" diff test", partitionModuleName+" diff test")
797 return diffTestResultFile
798}
799
800func createFailingCommand(ctx android.ModuleContext, message string) android.Path {
801 hasher := sha256.New()
802 hasher.Write([]byte(message))
803 filename := fmt.Sprintf("failing_command_%x.txt", hasher.Sum(nil))
804 file := android.PathForModuleOut(ctx, filename)
805 builder := android.NewRuleBuilder(pctx, ctx)
806 builder.Command().Textf("echo %s", proptools.NinjaAndShellEscape(message))
807 builder.Command().Text("exit 1 #").Output(file)
808 builder.Build("failing command "+filename, "failing command "+filename)
809 return file
810}
811
Cole Faust3552eb62024-11-06 18:07:26 -0800812func createVbmetaDiff(ctx android.ModuleContext, vbmetaModuleName string, vbmetaPartitionName string) android.Path {
813 vbmetaModule := ctx.GetDirectDepWithTag(vbmetaModuleName, generatedVbmetaPartitionDepTag)
814 outputFilesProvider, ok := android.OtherModuleProvider(ctx, vbmetaModule, android.OutputFilesProvider)
815 if !ok {
816 ctx.ModuleErrorf("Expected module %s to provide OutputFiles", vbmetaModule)
817 }
818 if len(outputFilesProvider.DefaultOutputFiles) != 1 {
819 ctx.ModuleErrorf("Expected 1 output file from module %s", vbmetaModule)
820 }
821 soongVbMetaFile := outputFilesProvider.DefaultOutputFiles[0]
822 makeVbmetaFile := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/%s.img", ctx.Config().DeviceName(), vbmetaPartitionName))
823
824 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", vbmetaModuleName))
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800825 createDiffTest(ctx, diffTestResultFile, soongVbMetaFile, makeVbmetaFile)
826 return diffTestResultFile
827}
828
829func createDiffTest(ctx android.ModuleContext, diffTestResultFile android.WritablePath, file1 android.Path, file2 android.Path) {
Cole Faust3552eb62024-11-06 18:07:26 -0800830 builder := android.NewRuleBuilder(pctx, ctx)
831 builder.Command().Text("diff").
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800832 Input(file1).
833 Input(file2)
Cole Faust3552eb62024-11-06 18:07:26 -0800834 builder.Command().Text("touch").Output(diffTestResultFile)
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800835 builder.Build("diff test "+diffTestResultFile.String(), "diff test")
Cole Faust3552eb62024-11-06 18:07:26 -0800836}
837
Cole Faust92ccbe22024-10-03 14:38:37 -0700838type systemImageDepTagType struct {
839 blueprint.BaseDependencyTag
840}
841
842var generatedFilesystemDepTag systemImageDepTagType
Cole Faust3552eb62024-11-06 18:07:26 -0800843var generatedVbmetaPartitionDepTag systemImageDepTagType
Cole Faust92ccbe22024-10-03 14:38:37 -0700844
845func (f *filesystemCreator) DepsMutator(ctx android.BottomUpMutatorContext) {
846 for _, partitionType := range f.properties.Generated_partition_types {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000847 ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, generatedModuleNameForPartition(ctx.Config(), partitionType))
Cole Faust92ccbe22024-10-03 14:38:37 -0700848 }
Cole Faust3552eb62024-11-06 18:07:26 -0800849 for _, vbmetaModule := range f.properties.Vbmeta_module_names {
850 ctx.AddDependency(ctx.Module(), generatedVbmetaPartitionDepTag, vbmetaModule)
851 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000852}
853
854func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700855 if ctx.ModuleDir() != "build/soong/fsgen" {
856 ctx.ModuleErrorf("There can only be one soong_filesystem_creator in build/soong/fsgen")
857 }
858 f.HideFromMake()
Jihoon Kang98047cf2024-10-02 17:13:54 +0000859
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000860 var content strings.Builder
861 generatedBp := android.PathForModuleOut(ctx, "soong_generated_product_config.bp")
862 for _, partition := range ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions {
863 content.WriteString(generateBpContent(ctx, partition))
864 content.WriteString("\n")
865 }
866 android.WriteFileRule(ctx, generatedBp, content.String())
867
mrziwang8f86c882024-10-03 12:34:33 -0700868 ctx.Phony("product_config_to_bp", generatedBp)
869
Cole Faust92ccbe22024-10-03 14:38:37 -0700870 var diffTestFiles []android.Path
871 for _, partitionType := range f.properties.Generated_partition_types {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800872 diffTestFile := f.createFileListDiffTest(ctx, partitionType)
Jihoon Kang72f812f2024-10-17 18:46:24 +0000873 diffTestFiles = append(diffTestFiles, diffTestFile)
874 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700875 }
876 for _, partitionType := range f.properties.Unsupported_partition_types {
Jihoon Kang72f812f2024-10-17 18:46:24 +0000877 diffTestFile := createFailingCommand(ctx, fmt.Sprintf("Couldn't build %s partition", partitionType))
878 diffTestFiles = append(diffTestFiles, diffTestFile)
879 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700880 }
Cole Faust3552eb62024-11-06 18:07:26 -0800881 for i, vbmetaModule := range f.properties.Vbmeta_module_names {
882 diffTestFile := createVbmetaDiff(ctx, vbmetaModule, f.properties.Vbmeta_partition_names[i])
883 diffTestFiles = append(diffTestFiles, diffTestFile)
884 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", f.properties.Vbmeta_partition_names[i]), diffTestFile)
885 }
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800886 if f.properties.Boot_image != "" {
887 diffTestFile := android.PathForModuleOut(ctx, "boot_diff_test.txt")
888 soongBootImg := android.PathForModuleSrc(ctx, f.properties.Boot_image)
889 makeBootImage := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/boot.img", ctx.Config().DeviceName()))
890 createDiffTest(ctx, diffTestFile, soongBootImg, makeBootImage)
891 diffTestFiles = append(diffTestFiles, diffTestFile)
892 ctx.Phony("soong_generated_boot_filesystem_test", diffTestFile)
893 }
Cole Faust24938e22024-11-18 14:01:58 -0800894 if f.properties.Vendor_boot_image != "" {
895 diffTestFile := android.PathForModuleOut(ctx, "vendor_boot_diff_test.txt")
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000896 soongBootImg := android.PathForModuleSrc(ctx, f.properties.Vendor_boot_image)
Cole Faust24938e22024-11-18 14:01:58 -0800897 makeBootImage := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/vendor_boot.img", ctx.Config().DeviceName()))
898 createDiffTest(ctx, diffTestFile, soongBootImg, makeBootImage)
899 diffTestFiles = append(diffTestFiles, diffTestFile)
900 ctx.Phony("soong_generated_vendor_boot_filesystem_test", diffTestFile)
901 }
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000902 if f.properties.Init_boot_image != "" {
903 diffTestFile := android.PathForModuleOut(ctx, "init_boot_diff_test.txt")
904 soongBootImg := android.PathForModuleSrc(ctx, f.properties.Init_boot_image)
905 makeBootImage := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/init_boot.img", ctx.Config().DeviceName()))
906 createDiffTest(ctx, diffTestFile, soongBootImg, makeBootImage)
907 diffTestFiles = append(diffTestFiles, diffTestFile)
908 ctx.Phony("soong_generated_init_boot_filesystem_test", diffTestFile)
909 }
Cole Faust92ccbe22024-10-03 14:38:37 -0700910 ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000911}
mrziwang8f86c882024-10-03 12:34:33 -0700912
mrziwang8f86c882024-10-03 12:34:33 -0700913func generateBpContent(ctx android.EarlyModuleContext, partitionType string) string {
mrziwang4b0ca972024-10-17 14:56:19 -0700914 fsProps, fsTypeSupported := generateFsProps(ctx, partitionType)
915 if !fsTypeSupported {
916 return ""
mrziwang8f86c882024-10-03 12:34:33 -0700917 }
918
mrziwang4b0ca972024-10-17 14:56:19 -0700919 baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)))
Jihoon Kang0d7b0112024-11-13 20:44:05 +0000920 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
921 deps := fsGenState.fsDeps[partitionType]
922 highPriorityDeps := fsGenState.generatedPrebuiltEtcModuleNames
923 depProps := generateDepStruct(*deps, highPriorityDeps)
mrziwang8f86c882024-10-03 12:34:33 -0700924
mrziwang4b0ca972024-10-17 14:56:19 -0700925 result, err := proptools.RepackProperties([]interface{}{baseProps, fsProps, depProps})
mrziwang8f86c882024-10-03 12:34:33 -0700926 if err != nil {
Cole Faustae3e1d32024-11-05 13:22:50 -0800927 ctx.ModuleErrorf("%s", err.Error())
928 return ""
mrziwang8f86c882024-10-03 12:34:33 -0700929 }
930
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000931 moduleType := "android_filesystem"
932 if partitionType == "system" {
933 moduleType = "android_system_image"
934 }
935
mrziwang8f86c882024-10-03 12:34:33 -0700936 file := &parser.File{
937 Defs: []parser.Definition{
938 &parser.Module{
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000939 Type: moduleType,
mrziwang8f86c882024-10-03 12:34:33 -0700940 Map: *result,
941 },
942 },
943 }
944 bytes, err := parser.Print(file)
945 if err != nil {
946 ctx.ModuleErrorf(err.Error())
947 }
948 return strings.TrimSpace(string(bytes))
949}