blob: 53ddc50d39de5c358b41d58d43af7e49d18fae32 [file] [log] [blame]
Justin Yun635e7882024-07-04 14:50:57 +09001// Copyright 2024 Google Inc. All rights reserved.
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 etc
16
17import (
18 "android/soong/android"
19
20 "github.com/google/blueprint/proptools"
21)
22
23func init() {
24 RegisterOtacertsZipBuildComponents(android.InitRegistrationContext)
25}
26
27func RegisterOtacertsZipBuildComponents(ctx android.RegistrationContext) {
28 ctx.RegisterModuleType("otacerts_zip", otacertsZipFactory)
29}
30
31type otacertsZipProperties struct {
32 // Make this module available when building for recovery.
33 // Only the recovery partition is available.
34 Recovery_available *bool
35
36 // Optional subdirectory under which the zip file is installed into.
37 Relative_install_path *string
38
39 // Optional name for the installed file. If unspecified, otacerts.zip is used.
40 Filename *string
41}
42
43type otacertsZipModule struct {
44 android.ModuleBase
45
46 properties otacertsZipProperties
Cole Faust4e9f5922024-11-13 16:09:23 -080047 outputPath android.Path
Justin Yun635e7882024-07-04 14:50:57 +090048}
49
50// otacerts_zip collects key files defined in PRODUCT_DEFAULT_DEV_CERTIFICATE
51// and PRODUCT_EXTRA_OTA_KEYS for system or PRODUCT_EXTRA_RECOVERY_KEYS for
52// recovery image. The output file (otacerts.zip by default) is installed into
53// the relative_install_path directory under the etc directory of the target
54// partition.
55func otacertsZipFactory() android.Module {
56 module := &otacertsZipModule{}
57 module.AddProperties(&module.properties)
58 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
59 return module
60}
61
62var _ android.ImageInterface = (*otacertsZipModule)(nil)
63
Cole Faustfa6e0fd2024-10-15 15:22:57 -070064func (m *otacertsZipModule) ImageMutatorBegin(ctx android.ImageInterfaceContext) {}
Justin Yun635e7882024-07-04 14:50:57 +090065
Cole Faustfa6e0fd2024-10-15 15:22:57 -070066func (m *otacertsZipModule) VendorVariantNeeded(ctx android.ImageInterfaceContext) bool {
Justin Yun635e7882024-07-04 14:50:57 +090067 return false
68}
69
Cole Faustfa6e0fd2024-10-15 15:22:57 -070070func (m *otacertsZipModule) ProductVariantNeeded(ctx android.ImageInterfaceContext) bool {
Justin Yun635e7882024-07-04 14:50:57 +090071 return false
72}
73
Cole Faustfa6e0fd2024-10-15 15:22:57 -070074func (m *otacertsZipModule) CoreVariantNeeded(ctx android.ImageInterfaceContext) bool {
Justin Yun635e7882024-07-04 14:50:57 +090075 return !m.ModuleBase.InstallInRecovery()
76}
77
Cole Faustfa6e0fd2024-10-15 15:22:57 -070078func (m *otacertsZipModule) RamdiskVariantNeeded(ctx android.ImageInterfaceContext) bool {
Justin Yun635e7882024-07-04 14:50:57 +090079 return false
80}
81
Cole Faustfa6e0fd2024-10-15 15:22:57 -070082func (m *otacertsZipModule) VendorRamdiskVariantNeeded(ctx android.ImageInterfaceContext) bool {
Justin Yun635e7882024-07-04 14:50:57 +090083 return false
84}
85
Cole Faustfa6e0fd2024-10-15 15:22:57 -070086func (m *otacertsZipModule) DebugRamdiskVariantNeeded(ctx android.ImageInterfaceContext) bool {
Justin Yun635e7882024-07-04 14:50:57 +090087 return false
88}
89
Cole Faustfa6e0fd2024-10-15 15:22:57 -070090func (m *otacertsZipModule) RecoveryVariantNeeded(ctx android.ImageInterfaceContext) bool {
Justin Yun635e7882024-07-04 14:50:57 +090091 return proptools.Bool(m.properties.Recovery_available) || m.ModuleBase.InstallInRecovery()
92}
93
Cole Faustfa6e0fd2024-10-15 15:22:57 -070094func (m *otacertsZipModule) ExtraImageVariations(ctx android.ImageInterfaceContext) []string {
Justin Yun635e7882024-07-04 14:50:57 +090095 return nil
96}
97
Cole Faustfa6e0fd2024-10-15 15:22:57 -070098func (m *otacertsZipModule) SetImageVariation(ctx android.ImageInterfaceContext, variation string) {
Justin Yun635e7882024-07-04 14:50:57 +090099}
100
101func (m *otacertsZipModule) InRecovery() bool {
102 return m.ModuleBase.InRecovery() || m.ModuleBase.InstallInRecovery()
103}
104
105func (m *otacertsZipModule) InstallInRecovery() bool {
106 return m.InRecovery()
107}
108
109func (m *otacertsZipModule) outputFileName() string {
110 // Use otacerts.zip if not specified.
111 return proptools.StringDefault(m.properties.Filename, "otacerts.zip")
112}
113
Jihoon Kang7b30c762024-12-07 00:13:59 +0000114func (m *otacertsZipModule) onlyInRecovery() bool {
115 return m.ModuleBase.InstallInRecovery()
116}
117
Justin Yun635e7882024-07-04 14:50:57 +0900118func (m *otacertsZipModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
119 // Read .x509.pem file defined in PRODUCT_DEFAULT_DEV_CERTIFICATE or the default test key.
120 pem, _ := ctx.Config().DefaultAppCertificate(ctx)
121 // Read .x509.pem files listed in PRODUCT_EXTRA_OTA_KEYS or PRODUCT_EXTRA_RECOVERY_KEYS.
122 extras := ctx.Config().ExtraOtaKeys(ctx, m.InRecovery())
123 srcPaths := append([]android.SourcePath{pem}, extras...)
Cole Faust4e9f5922024-11-13 16:09:23 -0800124 outputPath := android.PathForModuleOut(ctx, m.outputFileName())
Justin Yun635e7882024-07-04 14:50:57 +0900125
126 rule := android.NewRuleBuilder(pctx, ctx)
127 cmd := rule.Command().BuiltTool("soong_zip").
Cole Faust4e9f5922024-11-13 16:09:23 -0800128 FlagWithOutput("-o ", outputPath).
Justin Yun635e7882024-07-04 14:50:57 +0900129 Flag("-j ").
130 Flag("-symlinks=false ")
131 for _, src := range srcPaths {
132 cmd.FlagWithInput("-f ", src)
133 }
134 rule.Build(ctx.ModuleName(), "Generating the otacerts zip file")
135
136 installPath := android.PathForModuleInstall(ctx, "etc", proptools.String(m.properties.Relative_install_path))
Cole Faust4e9f5922024-11-13 16:09:23 -0800137 ctx.InstallFile(installPath, m.outputFileName(), outputPath)
138 m.outputPath = outputPath
Justin Yun635e7882024-07-04 14:50:57 +0900139}
140
141func (m *otacertsZipModule) AndroidMkEntries() []android.AndroidMkEntries {
142 nameSuffix := ""
Jihoon Kang7b30c762024-12-07 00:13:59 +0000143 if m.InRecovery() && !m.onlyInRecovery() {
Justin Yun635e7882024-07-04 14:50:57 +0900144 nameSuffix = ".recovery"
145 }
146 return []android.AndroidMkEntries{android.AndroidMkEntries{
147 Class: "ETC",
148 SubName: nameSuffix,
149 OutputFile: android.OptionalPathForPath(m.outputPath),
150 }}
151}