blob: 8e2b532a6abfc69b1d8b01716594404aa7b2b382 [file] [log] [blame]
mrziwang79730d42024-12-02 22:13:59 -08001// 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 filesystem
16
17import (
18 "fmt"
19 "path/filepath"
Cole Faust498ffc12025-01-15 14:19:32 -080020 "regexp"
Cole Faust74ee4e02025-01-16 14:55:35 -080021 "slices"
mrziwang79730d42024-12-02 22:13:59 -080022 "strconv"
23 "strings"
24
25 "android/soong/android"
Cole Faust2bdc5e52025-01-10 10:29:36 -080026
mrziwang79730d42024-12-02 22:13:59 -080027 "github.com/google/blueprint"
28 "github.com/google/blueprint/proptools"
29)
30
31func init() {
32 android.RegisterModuleType("super_image", SuperImageFactory)
33}
34
35type superImage struct {
36 android.ModuleBase
37
38 properties SuperImageProperties
39 partitionProps SuperImagePartitionNameProperties
40
41 installDir android.InstallPath
42}
43
44type SuperImageProperties struct {
45 // the size of the super partition
46 Size *int64
47 // the block device where metadata for dynamic partitions is stored
48 Metadata_device *string
49 // the super partition block device list
mrziwangf3c8ddf2024-12-05 17:15:11 -080050 Block_devices []string
mrziwang79730d42024-12-02 22:13:59 -080051 // whether A/B updater is used
52 Ab_update *bool
53 // whether dynamic partitions is enabled on devices that were launched without this support
54 Retrofit *bool
mrziwang79730d42024-12-02 22:13:59 -080055 // whether the output is a sparse image
56 Sparse *bool
57 // information about how partitions within the super partition are grouped together
58 Partition_groups []PartitionGroupsInfo
Cole Faust74ee4e02025-01-16 14:55:35 -080059 // Name of the system_other partition filesystem module. This module will be installed to
60 // the "b" slot of the system partition in a/b partition builds.
61 System_other_partition *string
mrziwang79730d42024-12-02 22:13:59 -080062 // whether dynamic partitions is used
63 Use_dynamic_partitions *bool
Cole Faust498ffc12025-01-15 14:19:32 -080064 Virtual_ab struct {
65 // whether virtual A/B seamless update is enabled
66 Enable *bool
67 // whether retrofitting virtual A/B seamless update is enabled
68 Retrofit *bool
69 // If set, device uses virtual A/B Compression
70 Compression *bool
71 // This value controls the compression algorithm used for VABC.
72 // Valid options are defined in system/core/fs_mgr/libsnapshot/cow_writer.cpp
73 // e.g. "none", "gz", "brotli"
74 Compression_method *string
75 // Specifies maximum bytes to be compressed at once during ota. Options: 4096, 8192, 16384, 32768, 65536, 131072, 262144.
76 Compression_factor *int64
77 // Specifies COW version to be used by update_engine and libsnapshot. If this value is not
78 // specified we default to COW version 2 in update_engine for backwards compatibility
79 Cow_version *int64
80 }
Spandan Das9da8a2d2025-02-28 07:45:30 +000081 // Whether the super image will be disted in the update package
82 Super_image_in_update_package *bool
Spandan Dasd73441e2025-03-10 22:34:25 +000083 // Whether a super_empty.img should be created
84 Create_super_empty *bool
mrziwang79730d42024-12-02 22:13:59 -080085}
86
87type PartitionGroupsInfo struct {
88 Name string
89 GroupSize string
90 PartitionList []string
91}
92
93type SuperImagePartitionNameProperties struct {
94 // Name of the System partition filesystem module
95 System_partition *string
96 // Name of the System_ext partition filesystem module
97 System_ext_partition *string
98 // Name of the System_dlkm partition filesystem module
99 System_dlkm_partition *string
100 // Name of the System_other partition filesystem module
101 System_other_partition *string
102 // Name of the Product partition filesystem module
103 Product_partition *string
104 // Name of the Vendor partition filesystem module
105 Vendor_partition *string
106 // Name of the Vendor_dlkm partition filesystem module
107 Vendor_dlkm_partition *string
108 // Name of the Odm partition filesystem module
109 Odm_partition *string
110 // Name of the Odm_dlkm partition filesystem module
111 Odm_dlkm_partition *string
112}
113
Cole Faust2bdc5e52025-01-10 10:29:36 -0800114type SuperImageInfo struct {
115 // The built super.img file, which contains the sub-partitions
116 SuperImage android.Path
117
118 // Mapping from the sub-partition type to its re-exported FileSystemInfo providers from the
119 // sub-partitions.
120 SubImageInfo map[string]FilesystemInfo
Spandan Dasf079f0d2025-02-27 13:13:50 +0000121
122 DynamicPartitionsInfo android.Path
Spandan Dasd73441e2025-03-10 22:34:25 +0000123
124 SuperEmptyImage android.Path
Cole Faust2bdc5e52025-01-10 10:29:36 -0800125}
126
127var SuperImageProvider = blueprint.NewProvider[SuperImageInfo]()
128
mrziwang79730d42024-12-02 22:13:59 -0800129func SuperImageFactory() android.Module {
130 module := &superImage{}
131 module.AddProperties(&module.properties, &module.partitionProps)
132 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
133 return module
134}
135
136type superImageDepTagType struct {
137 blueprint.BaseDependencyTag
138}
139
Cole Faust2bdc5e52025-01-10 10:29:36 -0800140var subImageDepTag superImageDepTagType
mrziwang79730d42024-12-02 22:13:59 -0800141
Cole Faust74ee4e02025-01-16 14:55:35 -0800142type systemOtherDepTagType struct {
143 blueprint.BaseDependencyTag
144}
145
146var systemOtherDepTag systemOtherDepTagType
147
mrziwang79730d42024-12-02 22:13:59 -0800148func (s *superImage) DepsMutator(ctx android.BottomUpMutatorContext) {
149 addDependencyIfDefined := func(dep *string) {
150 if dep != nil {
Cole Faust2bdc5e52025-01-10 10:29:36 -0800151 ctx.AddDependency(ctx.Module(), subImageDepTag, proptools.String(dep))
mrziwang79730d42024-12-02 22:13:59 -0800152 }
153 }
154
155 addDependencyIfDefined(s.partitionProps.System_partition)
156 addDependencyIfDefined(s.partitionProps.System_ext_partition)
157 addDependencyIfDefined(s.partitionProps.System_dlkm_partition)
158 addDependencyIfDefined(s.partitionProps.System_other_partition)
159 addDependencyIfDefined(s.partitionProps.Product_partition)
160 addDependencyIfDefined(s.partitionProps.Vendor_partition)
161 addDependencyIfDefined(s.partitionProps.Vendor_dlkm_partition)
162 addDependencyIfDefined(s.partitionProps.Odm_partition)
163 addDependencyIfDefined(s.partitionProps.Odm_dlkm_partition)
Cole Faust74ee4e02025-01-16 14:55:35 -0800164 if s.properties.System_other_partition != nil {
165 ctx.AddDependency(ctx.Module(), systemOtherDepTag, *s.properties.System_other_partition)
166 }
mrziwang79730d42024-12-02 22:13:59 -0800167}
168
169func (s *superImage) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Spandan Dasd73441e2025-03-10 22:34:25 +0000170 miscInfo, deps, subImageInfos := s.buildMiscInfo(ctx, false)
mrziwang79730d42024-12-02 22:13:59 -0800171 builder := android.NewRuleBuilder(pctx, ctx)
172 output := android.PathForModuleOut(ctx, s.installFileName())
173 lpMake := ctx.Config().HostToolPath(ctx, "lpmake")
174 lpMakeDir := filepath.Dir(lpMake.String())
175 deps = append(deps, lpMake)
176 builder.Command().Textf("PATH=%s:\\$PATH", lpMakeDir).
177 BuiltTool("build_super_image").
178 Text("-v").
179 Input(miscInfo).
180 Implicits(deps).
181 Output(output)
182 builder.Build("build_super_image", fmt.Sprintf("Creating super image %s", s.BaseModuleName()))
Spandan Dasd73441e2025-03-10 22:34:25 +0000183 var superEmptyImage android.WritablePath
184 if proptools.Bool(s.properties.Create_super_empty) {
185 superEmptyImageBuilder := android.NewRuleBuilder(pctx, ctx)
186 superEmptyImage = android.PathForModuleOut(ctx, "super_empty.img")
187 superEmptyMiscInfo, superEmptyDeps, _ := s.buildMiscInfo(ctx, true)
188 if superEmptyDeps != nil {
189 ctx.ModuleErrorf("TODO: Handle additional deps when building super_empty.img")
190 }
191 superEmptyImageBuilder.Command().Textf("PATH=%s:\\$PATH", lpMakeDir).
192 BuiltTool("build_super_image").
193 Text("-v").
194 Input(superEmptyMiscInfo).
195 Implicit(lpMake).
196 Output(superEmptyImage)
197 superEmptyImageBuilder.Build("build_super_empty_image", fmt.Sprintf("Creating super empty image %s", s.BaseModuleName()))
198 }
Cole Faust2bdc5e52025-01-10 10:29:36 -0800199 android.SetProvider(ctx, SuperImageProvider, SuperImageInfo{
Spandan Dasf079f0d2025-02-27 13:13:50 +0000200 SuperImage: output,
201 SubImageInfo: subImageInfos,
202 DynamicPartitionsInfo: s.generateDynamicPartitionsInfo(ctx),
Spandan Dasd73441e2025-03-10 22:34:25 +0000203 SuperEmptyImage: superEmptyImage,
Cole Faust2bdc5e52025-01-10 10:29:36 -0800204 })
mrziwang79730d42024-12-02 22:13:59 -0800205 ctx.SetOutputFiles([]android.Path{output}, "")
Cole Faust2bdc5e52025-01-10 10:29:36 -0800206 ctx.CheckbuildFile(output)
Wei Li7b8455f2025-03-05 16:05:51 -0800207
208 buildComplianceMetadata(ctx, subImageDepTag)
mrziwang79730d42024-12-02 22:13:59 -0800209}
210
211func (s *superImage) installFileName() string {
Cole Fausta472a6f2025-02-10 16:10:04 -0800212 return "super.img"
mrziwang79730d42024-12-02 22:13:59 -0800213}
214
Spandan Dasd73441e2025-03-10 22:34:25 +0000215func (s *superImage) buildMiscInfo(ctx android.ModuleContext, superEmpty bool) (android.Path, android.Paths, map[string]FilesystemInfo) {
mrziwang79730d42024-12-02 22:13:59 -0800216 var miscInfoString strings.Builder
Spandan Dasf079f0d2025-02-27 13:13:50 +0000217 partitionList := s.dumpDynamicPartitionInfo(ctx, &miscInfoString)
mrziwang79730d42024-12-02 22:13:59 -0800218 addStr := func(name string, value string) {
219 miscInfoString.WriteString(name)
220 miscInfoString.WriteRune('=')
221 miscInfoString.WriteString(value)
222 miscInfoString.WriteRune('\n')
223 }
Spandan Das9284f672025-03-10 17:29:53 +0000224 addStr("ab_update", strconv.FormatBool(proptools.Bool(s.properties.Ab_update)))
Spandan Dasd73441e2025-03-10 22:34:25 +0000225 if superEmpty {
226 miscInfo := android.PathForModuleOut(ctx, "misc_info_super_empty.txt")
227 android.WriteFileRule(ctx, miscInfo, miscInfoString.String())
228 return miscInfo, nil, nil
229 }
mrziwang79730d42024-12-02 22:13:59 -0800230
Cole Faust2bdc5e52025-01-10 10:29:36 -0800231 subImageInfo := make(map[string]FilesystemInfo)
232 var deps android.Paths
233
234 missingPartitionErrorMessage := ""
235 handleSubPartition := func(partitionType string, name *string) {
236 if proptools.String(name) == "" {
237 missingPartitionErrorMessage += fmt.Sprintf("%s image listed in partition groups, but its module was not specified. ", partitionType)
238 return
mrziwang79730d42024-12-02 22:13:59 -0800239 }
Cole Faust2bdc5e52025-01-10 10:29:36 -0800240 mod := ctx.GetDirectDepWithTag(*name, subImageDepTag)
241 if mod == nil {
242 ctx.ModuleErrorf("Could not get dep %q", *name)
243 return
244 }
245 info, ok := android.OtherModuleProvider(ctx, mod, FilesystemProvider)
246 if !ok {
247 ctx.ModuleErrorf("Expected dep %q to provide FilesystemInfo", *name)
248 return
249 }
250 addStr(partitionType+"_image", info.Output.String())
251 deps = append(deps, info.Output)
252 if _, ok := subImageInfo[partitionType]; ok {
253 ctx.ModuleErrorf("Already set subimageInfo for %q", partitionType)
254 }
255 subImageInfo[partitionType] = info
mrziwang79730d42024-12-02 22:13:59 -0800256 }
257
258 // Build partitionToImagePath, because system partition may need system_other
259 // partition image path
260 for _, p := range partitionList {
mrziwang79730d42024-12-02 22:13:59 -0800261 switch p {
262 case "system":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800263 handleSubPartition("system", s.partitionProps.System_partition)
mrziwang79730d42024-12-02 22:13:59 -0800264 case "system_dlkm":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800265 handleSubPartition("system_dlkm", s.partitionProps.System_dlkm_partition)
mrziwang79730d42024-12-02 22:13:59 -0800266 case "system_ext":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800267 handleSubPartition("system_ext", s.partitionProps.System_ext_partition)
mrziwang79730d42024-12-02 22:13:59 -0800268 case "product":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800269 handleSubPartition("product", s.partitionProps.Product_partition)
mrziwang79730d42024-12-02 22:13:59 -0800270 case "vendor":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800271 handleSubPartition("vendor", s.partitionProps.Vendor_partition)
mrziwang79730d42024-12-02 22:13:59 -0800272 case "vendor_dlkm":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800273 handleSubPartition("vendor_dlkm", s.partitionProps.Vendor_dlkm_partition)
mrziwang79730d42024-12-02 22:13:59 -0800274 case "odm":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800275 handleSubPartition("odm", s.partitionProps.Odm_partition)
mrziwang79730d42024-12-02 22:13:59 -0800276 case "odm_dlkm":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800277 handleSubPartition("odm_dlkm", s.partitionProps.Odm_dlkm_partition)
mrziwang79730d42024-12-02 22:13:59 -0800278 default:
Cole Faust2bdc5e52025-01-10 10:29:36 -0800279 ctx.ModuleErrorf("partition %q is not a super image supported partition", p)
mrziwang79730d42024-12-02 22:13:59 -0800280 }
281 }
282
Cole Faust74ee4e02025-01-16 14:55:35 -0800283 if s.properties.System_other_partition != nil {
284 if !slices.Contains(partitionList, "system") {
285 ctx.PropertyErrorf("system_other_partition", "Must have a system partition to use a system_other partition")
286 }
287 systemOther := ctx.GetDirectDepProxyWithTag(*s.properties.System_other_partition, systemOtherDepTag)
288 systemOtherFiles := android.OutputFilesForModule(ctx, systemOther, "")
289 if len(systemOtherFiles) != 1 {
290 ctx.PropertyErrorf("system_other_partition", "Expected 1 output file from module %q", *&s.properties.System_other_partition)
291 } else {
Spandan Das7a42d1c2025-02-12 01:32:21 +0000292 handleSubPartition("system_other", s.partitionProps.System_other_partition)
Cole Faust74ee4e02025-01-16 14:55:35 -0800293 }
294 }
295
Cole Faust2bdc5e52025-01-10 10:29:36 -0800296 // Delay the error message until execution time because on aosp-main-future-without-vendor,
297 // BUILDING_VENDOR_IMAGE is false so we don't get the vendor image, but it's still listed in
298 // BOARD_GOOGLE_DYNAMIC_PARTITIONS_PARTITION_LIST.
299 missingPartitionErrorMessageFile := android.PathForModuleOut(ctx, "missing_partition_error.txt")
300 if missingPartitionErrorMessage != "" {
301 ctx.Build(pctx, android.BuildParams{
302 Rule: android.ErrorRule,
303 Output: missingPartitionErrorMessageFile,
304 Args: map[string]string{
305 "error": missingPartitionErrorMessage,
306 },
307 })
308 } else {
309 ctx.Build(pctx, android.BuildParams{
310 Rule: android.Touch,
311 Output: missingPartitionErrorMessageFile,
312 })
Naresh Kumar Podishetty (xWF)4173c5b2025-01-09 20:20:32 -0800313 }
314
mrziwang79730d42024-12-02 22:13:59 -0800315 miscInfo := android.PathForModuleOut(ctx, "misc_info.txt")
Cole Faust2bdc5e52025-01-10 10:29:36 -0800316 android.WriteFileRule(ctx, miscInfo, miscInfoString.String(), missingPartitionErrorMessageFile)
317 return miscInfo, deps, subImageInfo
mrziwang79730d42024-12-02 22:13:59 -0800318}
Spandan Dasf079f0d2025-02-27 13:13:50 +0000319
320func (s *superImage) dumpDynamicPartitionInfo(ctx android.ModuleContext, sb *strings.Builder) []string {
321 addStr := func(name string, value string) {
322 sb.WriteString(name)
323 sb.WriteRune('=')
324 sb.WriteString(value)
325 sb.WriteRune('\n')
326 }
327
328 addStr("build_super_partition", "true")
Spandan Dasd73441e2025-03-10 22:34:25 +0000329 if proptools.Bool(s.properties.Create_super_empty) {
330 addStr("build_super_empty_partition", "true")
331 }
Spandan Dasf079f0d2025-02-27 13:13:50 +0000332 addStr("use_dynamic_partitions", strconv.FormatBool(proptools.Bool(s.properties.Use_dynamic_partitions)))
333 if proptools.Bool(s.properties.Retrofit) {
334 addStr("dynamic_partition_retrofit", "true")
335 }
336 addStr("lpmake", "lpmake")
337 addStr("super_metadata_device", proptools.String(s.properties.Metadata_device))
338 if len(s.properties.Block_devices) > 0 {
339 addStr("super_block_devices", strings.Join(s.properties.Block_devices, " "))
340 }
Spandan Das9da8a2d2025-02-28 07:45:30 +0000341 if proptools.Bool(s.properties.Super_image_in_update_package) {
342 addStr("super_image_in_update_package", "true")
343 }
Spandan Dasf079f0d2025-02-27 13:13:50 +0000344 addStr("super_partition_size", strconv.Itoa(proptools.Int(s.properties.Size)))
345 // TODO: In make, there's more complicated logic than just this surrounding super_*_device_size
346 addStr("super_super_device_size", strconv.Itoa(proptools.Int(s.properties.Size)))
347 var groups, partitionList []string
348 for _, groupInfo := range s.properties.Partition_groups {
349 groups = append(groups, groupInfo.Name)
350 partitionList = append(partitionList, groupInfo.PartitionList...)
351 addStr("super_"+groupInfo.Name+"_group_size", groupInfo.GroupSize)
352 addStr("super_"+groupInfo.Name+"_partition_list", strings.Join(groupInfo.PartitionList, " "))
353 }
354 initialPartitionListLen := len(partitionList)
355 partitionList = android.SortedUniqueStrings(partitionList)
356 if len(partitionList) != initialPartitionListLen {
357 ctx.ModuleErrorf("Duplicate partitions found in the partition_groups property")
358 }
359 addStr("super_partition_groups", strings.Join(groups, " "))
360 addStr("dynamic_partition_list", strings.Join(partitionList, " "))
361
Spandan Dasf079f0d2025-02-27 13:13:50 +0000362 if proptools.Bool(s.properties.Virtual_ab.Enable) {
363 addStr("virtual_ab", "true")
364 if proptools.Bool(s.properties.Virtual_ab.Retrofit) {
365 addStr("virtual_ab_retrofit", "true")
366 }
367 addStr("virtual_ab_compression", strconv.FormatBool(proptools.Bool(s.properties.Virtual_ab.Compression)))
368 if s.properties.Virtual_ab.Compression_method != nil {
369 matched, _ := regexp.MatchString("^[a-zA-Z0-9_-]+$", *s.properties.Virtual_ab.Compression_method)
370 if !matched {
371 ctx.PropertyErrorf("virtual_ab.compression_method", "compression_method cannot have special characters")
372 }
373 addStr("virtual_ab_compression_method", *s.properties.Virtual_ab.Compression_method)
374 }
375 if s.properties.Virtual_ab.Compression_factor != nil {
376 addStr("virtual_ab_compression_factor", strconv.FormatInt(*s.properties.Virtual_ab.Compression_factor, 10))
377 }
378 if s.properties.Virtual_ab.Cow_version != nil {
379 addStr("virtual_ab_cow_version", strconv.FormatInt(*s.properties.Virtual_ab.Cow_version, 10))
380 }
381
382 } else {
383 if s.properties.Virtual_ab.Retrofit != nil {
384 ctx.PropertyErrorf("virtual_ab.retrofit", "This property cannot be set when virtual_ab is disabled")
385 }
386 if s.properties.Virtual_ab.Compression != nil {
387 ctx.PropertyErrorf("virtual_ab.compression", "This property cannot be set when virtual_ab is disabled")
388 }
389 if s.properties.Virtual_ab.Compression_method != nil {
390 ctx.PropertyErrorf("virtual_ab.compression_method", "This property cannot be set when virtual_ab is disabled")
391 }
392 if s.properties.Virtual_ab.Compression_factor != nil {
393 ctx.PropertyErrorf("virtual_ab.compression_factor", "This property cannot be set when virtual_ab is disabled")
394 }
395 }
396
397 return partitionList
398}
399
400func (s *superImage) generateDynamicPartitionsInfo(ctx android.ModuleContext) android.Path {
401 var contents strings.Builder
402 s.dumpDynamicPartitionInfo(ctx, &contents)
403 dynamicPartitionsInfo := android.PathForModuleOut(ctx, "dynamic_partitions_info.txt")
404 android.WriteFileRule(ctx, dynamicPartitionsInfo, contents.String())
405 return dynamicPartitionsInfo
406}