blob: 74c7b659ef5b04d9cc56405f2107f14d7c8e568f [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 }
mrziwang79730d42024-12-02 22:13:59 -080081}
82
83type PartitionGroupsInfo struct {
84 Name string
85 GroupSize string
86 PartitionList []string
87}
88
89type SuperImagePartitionNameProperties struct {
90 // Name of the System partition filesystem module
91 System_partition *string
92 // Name of the System_ext partition filesystem module
93 System_ext_partition *string
94 // Name of the System_dlkm partition filesystem module
95 System_dlkm_partition *string
96 // Name of the System_other partition filesystem module
97 System_other_partition *string
98 // Name of the Product partition filesystem module
99 Product_partition *string
100 // Name of the Vendor partition filesystem module
101 Vendor_partition *string
102 // Name of the Vendor_dlkm partition filesystem module
103 Vendor_dlkm_partition *string
104 // Name of the Odm partition filesystem module
105 Odm_partition *string
106 // Name of the Odm_dlkm partition filesystem module
107 Odm_dlkm_partition *string
108}
109
Cole Faust2bdc5e52025-01-10 10:29:36 -0800110type SuperImageInfo struct {
111 // The built super.img file, which contains the sub-partitions
112 SuperImage android.Path
113
114 // Mapping from the sub-partition type to its re-exported FileSystemInfo providers from the
115 // sub-partitions.
116 SubImageInfo map[string]FilesystemInfo
Spandan Dasf079f0d2025-02-27 13:13:50 +0000117
118 DynamicPartitionsInfo android.Path
Cole Faust2bdc5e52025-01-10 10:29:36 -0800119}
120
121var SuperImageProvider = blueprint.NewProvider[SuperImageInfo]()
122
mrziwang79730d42024-12-02 22:13:59 -0800123func SuperImageFactory() android.Module {
124 module := &superImage{}
125 module.AddProperties(&module.properties, &module.partitionProps)
126 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
127 return module
128}
129
130type superImageDepTagType struct {
131 blueprint.BaseDependencyTag
132}
133
Cole Faust2bdc5e52025-01-10 10:29:36 -0800134var subImageDepTag superImageDepTagType
mrziwang79730d42024-12-02 22:13:59 -0800135
Cole Faust74ee4e02025-01-16 14:55:35 -0800136type systemOtherDepTagType struct {
137 blueprint.BaseDependencyTag
138}
139
140var systemOtherDepTag systemOtherDepTagType
141
mrziwang79730d42024-12-02 22:13:59 -0800142func (s *superImage) DepsMutator(ctx android.BottomUpMutatorContext) {
143 addDependencyIfDefined := func(dep *string) {
144 if dep != nil {
Cole Faust2bdc5e52025-01-10 10:29:36 -0800145 ctx.AddDependency(ctx.Module(), subImageDepTag, proptools.String(dep))
mrziwang79730d42024-12-02 22:13:59 -0800146 }
147 }
148
149 addDependencyIfDefined(s.partitionProps.System_partition)
150 addDependencyIfDefined(s.partitionProps.System_ext_partition)
151 addDependencyIfDefined(s.partitionProps.System_dlkm_partition)
152 addDependencyIfDefined(s.partitionProps.System_other_partition)
153 addDependencyIfDefined(s.partitionProps.Product_partition)
154 addDependencyIfDefined(s.partitionProps.Vendor_partition)
155 addDependencyIfDefined(s.partitionProps.Vendor_dlkm_partition)
156 addDependencyIfDefined(s.partitionProps.Odm_partition)
157 addDependencyIfDefined(s.partitionProps.Odm_dlkm_partition)
Cole Faust74ee4e02025-01-16 14:55:35 -0800158 if s.properties.System_other_partition != nil {
159 ctx.AddDependency(ctx.Module(), systemOtherDepTag, *s.properties.System_other_partition)
160 }
mrziwang79730d42024-12-02 22:13:59 -0800161}
162
163func (s *superImage) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust2bdc5e52025-01-10 10:29:36 -0800164 miscInfo, deps, subImageInfos := s.buildMiscInfo(ctx)
mrziwang79730d42024-12-02 22:13:59 -0800165 builder := android.NewRuleBuilder(pctx, ctx)
166 output := android.PathForModuleOut(ctx, s.installFileName())
167 lpMake := ctx.Config().HostToolPath(ctx, "lpmake")
168 lpMakeDir := filepath.Dir(lpMake.String())
169 deps = append(deps, lpMake)
170 builder.Command().Textf("PATH=%s:\\$PATH", lpMakeDir).
171 BuiltTool("build_super_image").
172 Text("-v").
173 Input(miscInfo).
174 Implicits(deps).
175 Output(output)
176 builder.Build("build_super_image", fmt.Sprintf("Creating super image %s", s.BaseModuleName()))
Cole Faust2bdc5e52025-01-10 10:29:36 -0800177 android.SetProvider(ctx, SuperImageProvider, SuperImageInfo{
Spandan Dasf079f0d2025-02-27 13:13:50 +0000178 SuperImage: output,
179 SubImageInfo: subImageInfos,
180 DynamicPartitionsInfo: s.generateDynamicPartitionsInfo(ctx),
Cole Faust2bdc5e52025-01-10 10:29:36 -0800181 })
mrziwang79730d42024-12-02 22:13:59 -0800182 ctx.SetOutputFiles([]android.Path{output}, "")
Cole Faust2bdc5e52025-01-10 10:29:36 -0800183 ctx.CheckbuildFile(output)
mrziwang79730d42024-12-02 22:13:59 -0800184}
185
186func (s *superImage) installFileName() string {
Cole Fausta472a6f2025-02-10 16:10:04 -0800187 return "super.img"
mrziwang79730d42024-12-02 22:13:59 -0800188}
189
Cole Faust2bdc5e52025-01-10 10:29:36 -0800190func (s *superImage) buildMiscInfo(ctx android.ModuleContext) (android.Path, android.Paths, map[string]FilesystemInfo) {
mrziwang79730d42024-12-02 22:13:59 -0800191 var miscInfoString strings.Builder
Spandan Dasf079f0d2025-02-27 13:13:50 +0000192 partitionList := s.dumpDynamicPartitionInfo(ctx, &miscInfoString)
mrziwang79730d42024-12-02 22:13:59 -0800193 addStr := func(name string, value string) {
194 miscInfoString.WriteString(name)
195 miscInfoString.WriteRune('=')
196 miscInfoString.WriteString(value)
197 miscInfoString.WriteRune('\n')
198 }
199
Cole Faust2bdc5e52025-01-10 10:29:36 -0800200 subImageInfo := make(map[string]FilesystemInfo)
201 var deps android.Paths
202
203 missingPartitionErrorMessage := ""
204 handleSubPartition := func(partitionType string, name *string) {
205 if proptools.String(name) == "" {
206 missingPartitionErrorMessage += fmt.Sprintf("%s image listed in partition groups, but its module was not specified. ", partitionType)
207 return
mrziwang79730d42024-12-02 22:13:59 -0800208 }
Cole Faust2bdc5e52025-01-10 10:29:36 -0800209 mod := ctx.GetDirectDepWithTag(*name, subImageDepTag)
210 if mod == nil {
211 ctx.ModuleErrorf("Could not get dep %q", *name)
212 return
213 }
214 info, ok := android.OtherModuleProvider(ctx, mod, FilesystemProvider)
215 if !ok {
216 ctx.ModuleErrorf("Expected dep %q to provide FilesystemInfo", *name)
217 return
218 }
219 addStr(partitionType+"_image", info.Output.String())
220 deps = append(deps, info.Output)
221 if _, ok := subImageInfo[partitionType]; ok {
222 ctx.ModuleErrorf("Already set subimageInfo for %q", partitionType)
223 }
224 subImageInfo[partitionType] = info
mrziwang79730d42024-12-02 22:13:59 -0800225 }
226
227 // Build partitionToImagePath, because system partition may need system_other
228 // partition image path
229 for _, p := range partitionList {
mrziwang79730d42024-12-02 22:13:59 -0800230 switch p {
231 case "system":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800232 handleSubPartition("system", s.partitionProps.System_partition)
mrziwang79730d42024-12-02 22:13:59 -0800233 case "system_dlkm":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800234 handleSubPartition("system_dlkm", s.partitionProps.System_dlkm_partition)
mrziwang79730d42024-12-02 22:13:59 -0800235 case "system_ext":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800236 handleSubPartition("system_ext", s.partitionProps.System_ext_partition)
mrziwang79730d42024-12-02 22:13:59 -0800237 case "product":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800238 handleSubPartition("product", s.partitionProps.Product_partition)
mrziwang79730d42024-12-02 22:13:59 -0800239 case "vendor":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800240 handleSubPartition("vendor", s.partitionProps.Vendor_partition)
mrziwang79730d42024-12-02 22:13:59 -0800241 case "vendor_dlkm":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800242 handleSubPartition("vendor_dlkm", s.partitionProps.Vendor_dlkm_partition)
mrziwang79730d42024-12-02 22:13:59 -0800243 case "odm":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800244 handleSubPartition("odm", s.partitionProps.Odm_partition)
mrziwang79730d42024-12-02 22:13:59 -0800245 case "odm_dlkm":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800246 handleSubPartition("odm_dlkm", s.partitionProps.Odm_dlkm_partition)
mrziwang79730d42024-12-02 22:13:59 -0800247 default:
Cole Faust2bdc5e52025-01-10 10:29:36 -0800248 ctx.ModuleErrorf("partition %q is not a super image supported partition", p)
mrziwang79730d42024-12-02 22:13:59 -0800249 }
250 }
251
Cole Faust74ee4e02025-01-16 14:55:35 -0800252 if s.properties.System_other_partition != nil {
253 if !slices.Contains(partitionList, "system") {
254 ctx.PropertyErrorf("system_other_partition", "Must have a system partition to use a system_other partition")
255 }
256 systemOther := ctx.GetDirectDepProxyWithTag(*s.properties.System_other_partition, systemOtherDepTag)
257 systemOtherFiles := android.OutputFilesForModule(ctx, systemOther, "")
258 if len(systemOtherFiles) != 1 {
259 ctx.PropertyErrorf("system_other_partition", "Expected 1 output file from module %q", *&s.properties.System_other_partition)
260 } else {
Spandan Das7a42d1c2025-02-12 01:32:21 +0000261 handleSubPartition("system_other", s.partitionProps.System_other_partition)
Cole Faust74ee4e02025-01-16 14:55:35 -0800262 }
263 }
264
Cole Faust2bdc5e52025-01-10 10:29:36 -0800265 // Delay the error message until execution time because on aosp-main-future-without-vendor,
266 // BUILDING_VENDOR_IMAGE is false so we don't get the vendor image, but it's still listed in
267 // BOARD_GOOGLE_DYNAMIC_PARTITIONS_PARTITION_LIST.
268 missingPartitionErrorMessageFile := android.PathForModuleOut(ctx, "missing_partition_error.txt")
269 if missingPartitionErrorMessage != "" {
270 ctx.Build(pctx, android.BuildParams{
271 Rule: android.ErrorRule,
272 Output: missingPartitionErrorMessageFile,
273 Args: map[string]string{
274 "error": missingPartitionErrorMessage,
275 },
276 })
277 } else {
278 ctx.Build(pctx, android.BuildParams{
279 Rule: android.Touch,
280 Output: missingPartitionErrorMessageFile,
281 })
Naresh Kumar Podishetty (xWF)4173c5b2025-01-09 20:20:32 -0800282 }
283
mrziwang79730d42024-12-02 22:13:59 -0800284 miscInfo := android.PathForModuleOut(ctx, "misc_info.txt")
Cole Faust2bdc5e52025-01-10 10:29:36 -0800285 android.WriteFileRule(ctx, miscInfo, miscInfoString.String(), missingPartitionErrorMessageFile)
286 return miscInfo, deps, subImageInfo
mrziwang79730d42024-12-02 22:13:59 -0800287}
Spandan Dasf079f0d2025-02-27 13:13:50 +0000288
289func (s *superImage) dumpDynamicPartitionInfo(ctx android.ModuleContext, sb *strings.Builder) []string {
290 addStr := func(name string, value string) {
291 sb.WriteString(name)
292 sb.WriteRune('=')
293 sb.WriteString(value)
294 sb.WriteRune('\n')
295 }
296
297 addStr("build_super_partition", "true")
298 addStr("use_dynamic_partitions", strconv.FormatBool(proptools.Bool(s.properties.Use_dynamic_partitions)))
299 if proptools.Bool(s.properties.Retrofit) {
300 addStr("dynamic_partition_retrofit", "true")
301 }
302 addStr("lpmake", "lpmake")
303 addStr("super_metadata_device", proptools.String(s.properties.Metadata_device))
304 if len(s.properties.Block_devices) > 0 {
305 addStr("super_block_devices", strings.Join(s.properties.Block_devices, " "))
306 }
307 addStr("super_partition_size", strconv.Itoa(proptools.Int(s.properties.Size)))
308 // TODO: In make, there's more complicated logic than just this surrounding super_*_device_size
309 addStr("super_super_device_size", strconv.Itoa(proptools.Int(s.properties.Size)))
310 var groups, partitionList []string
311 for _, groupInfo := range s.properties.Partition_groups {
312 groups = append(groups, groupInfo.Name)
313 partitionList = append(partitionList, groupInfo.PartitionList...)
314 addStr("super_"+groupInfo.Name+"_group_size", groupInfo.GroupSize)
315 addStr("super_"+groupInfo.Name+"_partition_list", strings.Join(groupInfo.PartitionList, " "))
316 }
317 initialPartitionListLen := len(partitionList)
318 partitionList = android.SortedUniqueStrings(partitionList)
319 if len(partitionList) != initialPartitionListLen {
320 ctx.ModuleErrorf("Duplicate partitions found in the partition_groups property")
321 }
322 addStr("super_partition_groups", strings.Join(groups, " "))
323 addStr("dynamic_partition_list", strings.Join(partitionList, " "))
324
325 addStr("ab_update", strconv.FormatBool(proptools.Bool(s.properties.Ab_update)))
326
327 if proptools.Bool(s.properties.Virtual_ab.Enable) {
328 addStr("virtual_ab", "true")
329 if proptools.Bool(s.properties.Virtual_ab.Retrofit) {
330 addStr("virtual_ab_retrofit", "true")
331 }
332 addStr("virtual_ab_compression", strconv.FormatBool(proptools.Bool(s.properties.Virtual_ab.Compression)))
333 if s.properties.Virtual_ab.Compression_method != nil {
334 matched, _ := regexp.MatchString("^[a-zA-Z0-9_-]+$", *s.properties.Virtual_ab.Compression_method)
335 if !matched {
336 ctx.PropertyErrorf("virtual_ab.compression_method", "compression_method cannot have special characters")
337 }
338 addStr("virtual_ab_compression_method", *s.properties.Virtual_ab.Compression_method)
339 }
340 if s.properties.Virtual_ab.Compression_factor != nil {
341 addStr("virtual_ab_compression_factor", strconv.FormatInt(*s.properties.Virtual_ab.Compression_factor, 10))
342 }
343 if s.properties.Virtual_ab.Cow_version != nil {
344 addStr("virtual_ab_cow_version", strconv.FormatInt(*s.properties.Virtual_ab.Cow_version, 10))
345 }
346
347 } else {
348 if s.properties.Virtual_ab.Retrofit != nil {
349 ctx.PropertyErrorf("virtual_ab.retrofit", "This property cannot be set when virtual_ab is disabled")
350 }
351 if s.properties.Virtual_ab.Compression != nil {
352 ctx.PropertyErrorf("virtual_ab.compression", "This property cannot be set when virtual_ab is disabled")
353 }
354 if s.properties.Virtual_ab.Compression_method != nil {
355 ctx.PropertyErrorf("virtual_ab.compression_method", "This property cannot be set when virtual_ab is disabled")
356 }
357 if s.properties.Virtual_ab.Compression_factor != nil {
358 ctx.PropertyErrorf("virtual_ab.compression_factor", "This property cannot be set when virtual_ab is disabled")
359 }
360 }
361
362 return partitionList
363}
364
365func (s *superImage) generateDynamicPartitionsInfo(ctx android.ModuleContext) android.Path {
366 var contents strings.Builder
367 s.dumpDynamicPartitionInfo(ctx, &contents)
368 dynamicPartitionsInfo := android.PathForModuleOut(ctx, "dynamic_partitions_info.txt")
369 android.WriteFileRule(ctx, dynamicPartitionsInfo, contents.String())
370 return dynamicPartitionsInfo
371}