blob: 5e62fa702088ee5076ecd57cb4f2dbea79103899 [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
mrziwang79730d42024-12-02 22:13:59 -080083}
84
85type PartitionGroupsInfo struct {
86 Name string
87 GroupSize string
88 PartitionList []string
89}
90
91type SuperImagePartitionNameProperties struct {
92 // Name of the System partition filesystem module
93 System_partition *string
94 // Name of the System_ext partition filesystem module
95 System_ext_partition *string
96 // Name of the System_dlkm partition filesystem module
97 System_dlkm_partition *string
98 // Name of the System_other partition filesystem module
99 System_other_partition *string
100 // Name of the Product partition filesystem module
101 Product_partition *string
102 // Name of the Vendor partition filesystem module
103 Vendor_partition *string
104 // Name of the Vendor_dlkm partition filesystem module
105 Vendor_dlkm_partition *string
106 // Name of the Odm partition filesystem module
107 Odm_partition *string
108 // Name of the Odm_dlkm partition filesystem module
109 Odm_dlkm_partition *string
110}
111
Cole Faust2bdc5e52025-01-10 10:29:36 -0800112type SuperImageInfo struct {
113 // The built super.img file, which contains the sub-partitions
114 SuperImage android.Path
115
116 // Mapping from the sub-partition type to its re-exported FileSystemInfo providers from the
117 // sub-partitions.
118 SubImageInfo map[string]FilesystemInfo
Spandan Dasf079f0d2025-02-27 13:13:50 +0000119
120 DynamicPartitionsInfo android.Path
Cole Faust2bdc5e52025-01-10 10:29:36 -0800121}
122
123var SuperImageProvider = blueprint.NewProvider[SuperImageInfo]()
124
mrziwang79730d42024-12-02 22:13:59 -0800125func SuperImageFactory() android.Module {
126 module := &superImage{}
127 module.AddProperties(&module.properties, &module.partitionProps)
128 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
129 return module
130}
131
132type superImageDepTagType struct {
133 blueprint.BaseDependencyTag
134}
135
Cole Faust2bdc5e52025-01-10 10:29:36 -0800136var subImageDepTag superImageDepTagType
mrziwang79730d42024-12-02 22:13:59 -0800137
Cole Faust74ee4e02025-01-16 14:55:35 -0800138type systemOtherDepTagType struct {
139 blueprint.BaseDependencyTag
140}
141
142var systemOtherDepTag systemOtherDepTagType
143
mrziwang79730d42024-12-02 22:13:59 -0800144func (s *superImage) DepsMutator(ctx android.BottomUpMutatorContext) {
145 addDependencyIfDefined := func(dep *string) {
146 if dep != nil {
Cole Faust2bdc5e52025-01-10 10:29:36 -0800147 ctx.AddDependency(ctx.Module(), subImageDepTag, proptools.String(dep))
mrziwang79730d42024-12-02 22:13:59 -0800148 }
149 }
150
151 addDependencyIfDefined(s.partitionProps.System_partition)
152 addDependencyIfDefined(s.partitionProps.System_ext_partition)
153 addDependencyIfDefined(s.partitionProps.System_dlkm_partition)
154 addDependencyIfDefined(s.partitionProps.System_other_partition)
155 addDependencyIfDefined(s.partitionProps.Product_partition)
156 addDependencyIfDefined(s.partitionProps.Vendor_partition)
157 addDependencyIfDefined(s.partitionProps.Vendor_dlkm_partition)
158 addDependencyIfDefined(s.partitionProps.Odm_partition)
159 addDependencyIfDefined(s.partitionProps.Odm_dlkm_partition)
Cole Faust74ee4e02025-01-16 14:55:35 -0800160 if s.properties.System_other_partition != nil {
161 ctx.AddDependency(ctx.Module(), systemOtherDepTag, *s.properties.System_other_partition)
162 }
mrziwang79730d42024-12-02 22:13:59 -0800163}
164
165func (s *superImage) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust2bdc5e52025-01-10 10:29:36 -0800166 miscInfo, deps, subImageInfos := s.buildMiscInfo(ctx)
mrziwang79730d42024-12-02 22:13:59 -0800167 builder := android.NewRuleBuilder(pctx, ctx)
168 output := android.PathForModuleOut(ctx, s.installFileName())
169 lpMake := ctx.Config().HostToolPath(ctx, "lpmake")
170 lpMakeDir := filepath.Dir(lpMake.String())
171 deps = append(deps, lpMake)
172 builder.Command().Textf("PATH=%s:\\$PATH", lpMakeDir).
173 BuiltTool("build_super_image").
174 Text("-v").
175 Input(miscInfo).
176 Implicits(deps).
177 Output(output)
178 builder.Build("build_super_image", fmt.Sprintf("Creating super image %s", s.BaseModuleName()))
Cole Faust2bdc5e52025-01-10 10:29:36 -0800179 android.SetProvider(ctx, SuperImageProvider, SuperImageInfo{
Spandan Dasf079f0d2025-02-27 13:13:50 +0000180 SuperImage: output,
181 SubImageInfo: subImageInfos,
182 DynamicPartitionsInfo: s.generateDynamicPartitionsInfo(ctx),
Cole Faust2bdc5e52025-01-10 10:29:36 -0800183 })
mrziwang79730d42024-12-02 22:13:59 -0800184 ctx.SetOutputFiles([]android.Path{output}, "")
Cole Faust2bdc5e52025-01-10 10:29:36 -0800185 ctx.CheckbuildFile(output)
mrziwang79730d42024-12-02 22:13:59 -0800186}
187
188func (s *superImage) installFileName() string {
Cole Fausta472a6f2025-02-10 16:10:04 -0800189 return "super.img"
mrziwang79730d42024-12-02 22:13:59 -0800190}
191
Cole Faust2bdc5e52025-01-10 10:29:36 -0800192func (s *superImage) buildMiscInfo(ctx android.ModuleContext) (android.Path, android.Paths, map[string]FilesystemInfo) {
mrziwang79730d42024-12-02 22:13:59 -0800193 var miscInfoString strings.Builder
Spandan Dasf079f0d2025-02-27 13:13:50 +0000194 partitionList := s.dumpDynamicPartitionInfo(ctx, &miscInfoString)
mrziwang79730d42024-12-02 22:13:59 -0800195 addStr := func(name string, value string) {
196 miscInfoString.WriteString(name)
197 miscInfoString.WriteRune('=')
198 miscInfoString.WriteString(value)
199 miscInfoString.WriteRune('\n')
200 }
201
Cole Faust2bdc5e52025-01-10 10:29:36 -0800202 subImageInfo := make(map[string]FilesystemInfo)
203 var deps android.Paths
204
205 missingPartitionErrorMessage := ""
206 handleSubPartition := func(partitionType string, name *string) {
207 if proptools.String(name) == "" {
208 missingPartitionErrorMessage += fmt.Sprintf("%s image listed in partition groups, but its module was not specified. ", partitionType)
209 return
mrziwang79730d42024-12-02 22:13:59 -0800210 }
Cole Faust2bdc5e52025-01-10 10:29:36 -0800211 mod := ctx.GetDirectDepWithTag(*name, subImageDepTag)
212 if mod == nil {
213 ctx.ModuleErrorf("Could not get dep %q", *name)
214 return
215 }
216 info, ok := android.OtherModuleProvider(ctx, mod, FilesystemProvider)
217 if !ok {
218 ctx.ModuleErrorf("Expected dep %q to provide FilesystemInfo", *name)
219 return
220 }
221 addStr(partitionType+"_image", info.Output.String())
222 deps = append(deps, info.Output)
223 if _, ok := subImageInfo[partitionType]; ok {
224 ctx.ModuleErrorf("Already set subimageInfo for %q", partitionType)
225 }
226 subImageInfo[partitionType] = info
mrziwang79730d42024-12-02 22:13:59 -0800227 }
228
229 // Build partitionToImagePath, because system partition may need system_other
230 // partition image path
231 for _, p := range partitionList {
mrziwang79730d42024-12-02 22:13:59 -0800232 switch p {
233 case "system":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800234 handleSubPartition("system", s.partitionProps.System_partition)
mrziwang79730d42024-12-02 22:13:59 -0800235 case "system_dlkm":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800236 handleSubPartition("system_dlkm", s.partitionProps.System_dlkm_partition)
mrziwang79730d42024-12-02 22:13:59 -0800237 case "system_ext":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800238 handleSubPartition("system_ext", s.partitionProps.System_ext_partition)
mrziwang79730d42024-12-02 22:13:59 -0800239 case "product":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800240 handleSubPartition("product", s.partitionProps.Product_partition)
mrziwang79730d42024-12-02 22:13:59 -0800241 case "vendor":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800242 handleSubPartition("vendor", s.partitionProps.Vendor_partition)
mrziwang79730d42024-12-02 22:13:59 -0800243 case "vendor_dlkm":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800244 handleSubPartition("vendor_dlkm", s.partitionProps.Vendor_dlkm_partition)
mrziwang79730d42024-12-02 22:13:59 -0800245 case "odm":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800246 handleSubPartition("odm", s.partitionProps.Odm_partition)
mrziwang79730d42024-12-02 22:13:59 -0800247 case "odm_dlkm":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800248 handleSubPartition("odm_dlkm", s.partitionProps.Odm_dlkm_partition)
mrziwang79730d42024-12-02 22:13:59 -0800249 default:
Cole Faust2bdc5e52025-01-10 10:29:36 -0800250 ctx.ModuleErrorf("partition %q is not a super image supported partition", p)
mrziwang79730d42024-12-02 22:13:59 -0800251 }
252 }
253
Cole Faust74ee4e02025-01-16 14:55:35 -0800254 if s.properties.System_other_partition != nil {
255 if !slices.Contains(partitionList, "system") {
256 ctx.PropertyErrorf("system_other_partition", "Must have a system partition to use a system_other partition")
257 }
258 systemOther := ctx.GetDirectDepProxyWithTag(*s.properties.System_other_partition, systemOtherDepTag)
259 systemOtherFiles := android.OutputFilesForModule(ctx, systemOther, "")
260 if len(systemOtherFiles) != 1 {
261 ctx.PropertyErrorf("system_other_partition", "Expected 1 output file from module %q", *&s.properties.System_other_partition)
262 } else {
Spandan Das7a42d1c2025-02-12 01:32:21 +0000263 handleSubPartition("system_other", s.partitionProps.System_other_partition)
Cole Faust74ee4e02025-01-16 14:55:35 -0800264 }
265 }
266
Cole Faust2bdc5e52025-01-10 10:29:36 -0800267 // Delay the error message until execution time because on aosp-main-future-without-vendor,
268 // BUILDING_VENDOR_IMAGE is false so we don't get the vendor image, but it's still listed in
269 // BOARD_GOOGLE_DYNAMIC_PARTITIONS_PARTITION_LIST.
270 missingPartitionErrorMessageFile := android.PathForModuleOut(ctx, "missing_partition_error.txt")
271 if missingPartitionErrorMessage != "" {
272 ctx.Build(pctx, android.BuildParams{
273 Rule: android.ErrorRule,
274 Output: missingPartitionErrorMessageFile,
275 Args: map[string]string{
276 "error": missingPartitionErrorMessage,
277 },
278 })
279 } else {
280 ctx.Build(pctx, android.BuildParams{
281 Rule: android.Touch,
282 Output: missingPartitionErrorMessageFile,
283 })
Naresh Kumar Podishetty (xWF)4173c5b2025-01-09 20:20:32 -0800284 }
285
mrziwang79730d42024-12-02 22:13:59 -0800286 miscInfo := android.PathForModuleOut(ctx, "misc_info.txt")
Cole Faust2bdc5e52025-01-10 10:29:36 -0800287 android.WriteFileRule(ctx, miscInfo, miscInfoString.String(), missingPartitionErrorMessageFile)
288 return miscInfo, deps, subImageInfo
mrziwang79730d42024-12-02 22:13:59 -0800289}
Spandan Dasf079f0d2025-02-27 13:13:50 +0000290
291func (s *superImage) dumpDynamicPartitionInfo(ctx android.ModuleContext, sb *strings.Builder) []string {
292 addStr := func(name string, value string) {
293 sb.WriteString(name)
294 sb.WriteRune('=')
295 sb.WriteString(value)
296 sb.WriteRune('\n')
297 }
298
299 addStr("build_super_partition", "true")
300 addStr("use_dynamic_partitions", strconv.FormatBool(proptools.Bool(s.properties.Use_dynamic_partitions)))
301 if proptools.Bool(s.properties.Retrofit) {
302 addStr("dynamic_partition_retrofit", "true")
303 }
304 addStr("lpmake", "lpmake")
305 addStr("super_metadata_device", proptools.String(s.properties.Metadata_device))
306 if len(s.properties.Block_devices) > 0 {
307 addStr("super_block_devices", strings.Join(s.properties.Block_devices, " "))
308 }
Spandan Das9da8a2d2025-02-28 07:45:30 +0000309 if proptools.Bool(s.properties.Super_image_in_update_package) {
310 addStr("super_image_in_update_package", "true")
311 }
Spandan Dasf079f0d2025-02-27 13:13:50 +0000312 addStr("super_partition_size", strconv.Itoa(proptools.Int(s.properties.Size)))
313 // TODO: In make, there's more complicated logic than just this surrounding super_*_device_size
314 addStr("super_super_device_size", strconv.Itoa(proptools.Int(s.properties.Size)))
315 var groups, partitionList []string
316 for _, groupInfo := range s.properties.Partition_groups {
317 groups = append(groups, groupInfo.Name)
318 partitionList = append(partitionList, groupInfo.PartitionList...)
319 addStr("super_"+groupInfo.Name+"_group_size", groupInfo.GroupSize)
320 addStr("super_"+groupInfo.Name+"_partition_list", strings.Join(groupInfo.PartitionList, " "))
321 }
322 initialPartitionListLen := len(partitionList)
323 partitionList = android.SortedUniqueStrings(partitionList)
324 if len(partitionList) != initialPartitionListLen {
325 ctx.ModuleErrorf("Duplicate partitions found in the partition_groups property")
326 }
327 addStr("super_partition_groups", strings.Join(groups, " "))
328 addStr("dynamic_partition_list", strings.Join(partitionList, " "))
329
330 addStr("ab_update", strconv.FormatBool(proptools.Bool(s.properties.Ab_update)))
331
332 if proptools.Bool(s.properties.Virtual_ab.Enable) {
333 addStr("virtual_ab", "true")
334 if proptools.Bool(s.properties.Virtual_ab.Retrofit) {
335 addStr("virtual_ab_retrofit", "true")
336 }
337 addStr("virtual_ab_compression", strconv.FormatBool(proptools.Bool(s.properties.Virtual_ab.Compression)))
338 if s.properties.Virtual_ab.Compression_method != nil {
339 matched, _ := regexp.MatchString("^[a-zA-Z0-9_-]+$", *s.properties.Virtual_ab.Compression_method)
340 if !matched {
341 ctx.PropertyErrorf("virtual_ab.compression_method", "compression_method cannot have special characters")
342 }
343 addStr("virtual_ab_compression_method", *s.properties.Virtual_ab.Compression_method)
344 }
345 if s.properties.Virtual_ab.Compression_factor != nil {
346 addStr("virtual_ab_compression_factor", strconv.FormatInt(*s.properties.Virtual_ab.Compression_factor, 10))
347 }
348 if s.properties.Virtual_ab.Cow_version != nil {
349 addStr("virtual_ab_cow_version", strconv.FormatInt(*s.properties.Virtual_ab.Cow_version, 10))
350 }
351
352 } else {
353 if s.properties.Virtual_ab.Retrofit != nil {
354 ctx.PropertyErrorf("virtual_ab.retrofit", "This property cannot be set when virtual_ab is disabled")
355 }
356 if s.properties.Virtual_ab.Compression != nil {
357 ctx.PropertyErrorf("virtual_ab.compression", "This property cannot be set when virtual_ab is disabled")
358 }
359 if s.properties.Virtual_ab.Compression_method != nil {
360 ctx.PropertyErrorf("virtual_ab.compression_method", "This property cannot be set when virtual_ab is disabled")
361 }
362 if s.properties.Virtual_ab.Compression_factor != nil {
363 ctx.PropertyErrorf("virtual_ab.compression_factor", "This property cannot be set when virtual_ab is disabled")
364 }
365 }
366
367 return partitionList
368}
369
370func (s *superImage) generateDynamicPartitionsInfo(ctx android.ModuleContext) android.Path {
371 var contents strings.Builder
372 s.dumpDynamicPartitionInfo(ctx, &contents)
373 dynamicPartitionsInfo := android.PathForModuleOut(ctx, "dynamic_partitions_info.txt")
374 android.WriteFileRule(ctx, dynamicPartitionsInfo, contents.String())
375 return dynamicPartitionsInfo
376}