blob: 259060f3b011cc4f2bb8f3e49e235a47c4f1987d [file] [log] [blame]
Jiyong Parkff1458f2018-10-12 21:49:38 +09001// Copyright (C) 2018 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 apex
16
17import (
18 "fmt"
Jiyong Park4d277042019-04-23 18:00:10 +090019 "sort"
Jiyong Park0ca3ce82019-02-18 15:25:04 +090020 "strings"
Jiyong Parkff1458f2018-10-12 21:49:38 +090021
22 "android/soong/android"
Rupert Shuttlewortheb8c85a2021-07-27 07:10:32 -040023 "android/soong/bazel"
Colin Cross5f692ec2019-02-01 16:53:07 -080024
Jiyong Parkff1458f2018-10-12 21:49:38 +090025 "github.com/google/blueprint/proptools"
26)
27
28var String = proptools.String
29
30func init() {
Paul Duffin667893c2021-03-09 22:34:13 +000031 registerApexKeyBuildComponents(android.InitRegistrationContext)
32}
33
34func registerApexKeyBuildComponents(ctx android.RegistrationContext) {
35 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
36 ctx.RegisterSingletonType("apex_keys_text", apexKeysTextFactory)
Rupert Shuttlewortheb8c85a2021-07-27 07:10:32 -040037
38 android.RegisterBp2BuildMutator("apex_key", ApexKeyBp2Build)
Jiyong Parkff1458f2018-10-12 21:49:38 +090039}
40
41type apexKey struct {
42 android.ModuleBase
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -040043 android.BazelModuleBase
Jiyong Parkff1458f2018-10-12 21:49:38 +090044
45 properties apexKeyProperties
46
Jaewoong Jung18aefc12020-12-21 09:11:10 -080047 publicKeyFile android.Path
48 privateKeyFile android.Path
Jiyong Parkff1458f2018-10-12 21:49:38 +090049
50 keyName string
51}
52
53type apexKeyProperties struct {
Jiyong Park67882562019-03-21 01:11:21 +090054 // Path or module to the public key file in avbpubkey format. Installed to the device.
Jiyong Parkff1458f2018-10-12 21:49:38 +090055 // Base name of the file is used as the ID for the key.
Jiyong Park67882562019-03-21 01:11:21 +090056 Public_key *string `android:"path"`
57 // Path or module to the private key file in pem format. Used to sign APEXs.
58 Private_key *string `android:"path"`
Jiyong Park50d99202018-12-27 13:32:34 +090059
60 // Whether this key is installable to one of the partitions. Defualt: true.
61 Installable *bool
Jiyong Parkff1458f2018-10-12 21:49:38 +090062}
63
Jiyong Parkd1063c12019-07-17 20:08:41 +090064func ApexKeyFactory() android.Module {
Jiyong Parkff1458f2018-10-12 21:49:38 +090065 module := &apexKey{}
66 module.AddProperties(&module.properties)
dimitryf8071672019-04-11 17:27:11 +020067 android.InitAndroidArchModule(module, android.HostAndDeviceDefault, android.MultilibCommon)
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -040068 android.InitBazelModule(module)
Jiyong Parkff1458f2018-10-12 21:49:38 +090069 return module
70}
71
Jiyong Park50d99202018-12-27 13:32:34 +090072func (m *apexKey) installable() bool {
Jiyong Park42cca6c2019-04-01 11:15:50 +090073 return false
Jiyong Park50d99202018-12-27 13:32:34 +090074}
75
Jiyong Parkff1458f2018-10-12 21:49:38 +090076func (m *apexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park67882562019-03-21 01:11:21 +090077 // If the keys are from other modules (i.e. :module syntax) respect it.
78 // Otherwise, try to locate the key files in the default cert dir or
79 // in the local module dir
80 if android.SrcIsModule(String(m.properties.Public_key)) != "" {
Jaewoong Jung18aefc12020-12-21 09:11:10 -080081 m.publicKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
Jiyong Park67882562019-03-21 01:11:21 +090082 } else {
Jaewoong Jung18aefc12020-12-21 09:11:10 -080083 m.publicKeyFile = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Public_key))
Jiyong Park67882562019-03-21 01:11:21 +090084 // If not found, fall back to the local key pairs
Jaewoong Jung18aefc12020-12-21 09:11:10 -080085 if !android.ExistentPathForSource(ctx, m.publicKeyFile.String()).Valid() {
86 m.publicKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
Jiyong Park67882562019-03-21 01:11:21 +090087 }
Jiyong Park9335a262018-12-24 11:31:58 +090088 }
Jiyong Park67882562019-03-21 01:11:21 +090089
90 if android.SrcIsModule(String(m.properties.Private_key)) != "" {
Jaewoong Jung18aefc12020-12-21 09:11:10 -080091 m.privateKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
Jiyong Park67882562019-03-21 01:11:21 +090092 } else {
Jaewoong Jung18aefc12020-12-21 09:11:10 -080093 m.privateKeyFile = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Private_key))
94 if !android.ExistentPathForSource(ctx, m.privateKeyFile.String()).Valid() {
95 m.privateKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
Jiyong Park67882562019-03-21 01:11:21 +090096 }
Jiyong Park9335a262018-12-24 11:31:58 +090097 }
Jiyong Parkff1458f2018-10-12 21:49:38 +090098
Jaewoong Jung18aefc12020-12-21 09:11:10 -080099 pubKeyName := m.publicKeyFile.Base()[0 : len(m.publicKeyFile.Base())-len(m.publicKeyFile.Ext())]
100 privKeyName := m.privateKeyFile.Base()[0 : len(m.privateKeyFile.Base())-len(m.privateKeyFile.Ext())]
Jiyong Parkff1458f2018-10-12 21:49:38 +0900101
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700102 if m.properties.Public_key != nil && m.properties.Private_key != nil && pubKeyName != privKeyName {
Jiyong Parkff1458f2018-10-12 21:49:38 +0900103 ctx.ModuleErrorf("public_key %q (keyname:%q) and private_key %q (keyname:%q) do not have same keyname",
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800104 m.publicKeyFile.String(), pubKeyName, m.privateKeyFile, privKeyName)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900105 return
106 }
107 m.keyName = pubKeyName
Jiyong Parkff1458f2018-10-12 21:49:38 +0900108}
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900109
110////////////////////////////////////////////////////////////////////////
111// apex_keys_text
Jiyong Park37eb8bb2019-02-20 22:23:29 +0900112type apexKeysText struct {
113 output android.OutputPath
114}
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900115
116func (s *apexKeysText) GenerateBuildActions(ctx android.SingletonContext) {
Jiyong Park37eb8bb2019-02-20 22:23:29 +0900117 s.output = android.PathForOutput(ctx, "apexkeys.txt")
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900118 type apexKeyEntry struct {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800119 name string
120 presigned bool
121 publicKey string
122 privateKey string
123 containerCertificate string
124 containerPrivateKey string
125 partition string
Jooyung Han09c11ad2021-10-27 03:45:31 +0900126 signTool string
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900127 }
128 toString := func(e apexKeyEntry) string {
Jooyung Han09c11ad2021-10-27 03:45:31 +0900129 signTool := ""
130 if e.signTool != "" {
131 signTool = fmt.Sprintf(" sign_tool=%q", e.signTool)
132 }
133 format := "name=%q public_key=%q private_key=%q container_certificate=%q container_private_key=%q partition=%q%s\n"
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900134 if e.presigned {
Jooyung Han09c11ad2021-10-27 03:45:31 +0900135 return fmt.Sprintf(format, e.name, "PRESIGNED", "PRESIGNED", "PRESIGNED", "PRESIGNED", e.partition, signTool)
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900136 } else {
Jooyung Han09c11ad2021-10-27 03:45:31 +0900137 return fmt.Sprintf(format, e.name, e.publicKey, e.privateKey, e.containerCertificate, e.containerPrivateKey, e.partition, signTool)
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900138 }
139 }
140
141 apexKeyMap := make(map[string]apexKeyEntry)
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900142 ctx.VisitAllModules(func(module android.Module) {
Jiyong Park4d277042019-04-23 18:00:10 +0900143 if m, ok := module.(*apexBundle); ok && m.Enabled() && m.installable() {
Jiyong Parkb81b9902020-11-24 19:51:18 +0900144 pem, key := m.getCertificateAndPrivateKey(ctx)
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900145 apexKeyMap[m.Name()] = apexKeyEntry{
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800146 name: m.Name() + ".apex",
147 presigned: false,
148 publicKey: m.publicKeyFile.String(),
149 privateKey: m.privateKeyFile.String(),
150 containerCertificate: pem.String(),
151 containerPrivateKey: key.String(),
152 partition: m.PartitionTag(ctx.DeviceConfig()),
Jooyung Han09c11ad2021-10-27 03:45:31 +0900153 signTool: proptools.String(m.properties.Custom_sign_tool),
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900154 }
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900155 }
Jiyong Park4d277042019-04-23 18:00:10 +0900156 })
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900157
Jiyong Park4d277042019-04-23 18:00:10 +0900158 // Find prebuilts and let them override apexBundle if they are preferred
159 ctx.VisitAllModules(func(module android.Module) {
160 if m, ok := module.(*Prebuilt); ok && m.Enabled() && m.installable() &&
161 m.Prebuilt().UsePrebuilt() {
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900162 apexKeyMap[m.BaseModuleName()] = apexKeyEntry{
163 name: m.InstallFilename(),
164 presigned: true,
165 partition: m.PartitionTag(ctx.DeviceConfig()),
166 }
167 }
168 })
169
170 // Find apex_set and let them override apexBundle or prebuilts. This is done in a separate pass
171 // so that apex_set are not overridden by prebuilts.
172 ctx.VisitAllModules(func(module android.Module) {
173 if m, ok := module.(*ApexSet); ok && m.Enabled() {
174 entry := apexKeyEntry{
175 name: m.InstallFilename(),
176 presigned: true,
177 partition: m.PartitionTag(ctx.DeviceConfig()),
178 }
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900179 apexKeyMap[m.BaseModuleName()] = entry
Jiyong Park4d277042019-04-23 18:00:10 +0900180 }
181 })
182
183 // iterating over map does not give consistent ordering in golang
184 var moduleNames []string
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900185 for key, _ := range apexKeyMap {
Jiyong Park4d277042019-04-23 18:00:10 +0900186 moduleNames = append(moduleNames, key)
187 }
188 sort.Strings(moduleNames)
189
190 var filecontent strings.Builder
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900191 for _, name := range moduleNames {
Colin Crosscf371cc2020-11-13 11:48:42 -0800192 filecontent.WriteString(toString(apexKeyMap[name]))
Jiyong Park4d277042019-04-23 18:00:10 +0900193 }
Colin Crosscf371cc2020-11-13 11:48:42 -0800194 android.WriteFileRule(ctx, s.output, filecontent.String())
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900195}
196
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900197func apexKeysTextFactory() android.Singleton {
198 return &apexKeysText{}
199}
200
Jiyong Park37eb8bb2019-02-20 22:23:29 +0900201func (s *apexKeysText) MakeVars(ctx android.MakeVarsContext) {
202 ctx.Strict("SOONG_APEX_KEYS_FILE", s.output.String())
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900203}
Rupert Shuttlewortheb8c85a2021-07-27 07:10:32 -0400204
205// For Bazel / bp2build
206
207type bazelApexKeyAttributes struct {
208 Public_key bazel.LabelAttribute
209 Private_key bazel.LabelAttribute
210}
211
Rupert Shuttlewortheb8c85a2021-07-27 07:10:32 -0400212func ApexKeyBp2Build(ctx android.TopDownMutatorContext) {
213 module, ok := ctx.Module().(*apexKey)
214 if !ok {
215 // Not an APEX key
216 return
217 }
218 if !module.ConvertWithBp2build(ctx) {
219 return
220 }
221 if ctx.ModuleType() != "apex_key" {
222 return
223 }
224
225 apexKeyBp2BuildInternal(ctx, module)
226}
227
228func apexKeyBp2BuildInternal(ctx android.TopDownMutatorContext, module *apexKey) {
229 var privateKeyLabelAttribute bazel.LabelAttribute
230 if module.properties.Private_key != nil {
231 privateKeyLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *module.properties.Private_key))
232 }
233
234 var publicKeyLabelAttribute bazel.LabelAttribute
235 if module.properties.Public_key != nil {
236 publicKeyLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *module.properties.Public_key))
237 }
238
239 attrs := &bazelApexKeyAttributes{
240 Private_key: privateKeyLabelAttribute,
241 Public_key: publicKeyLabelAttribute,
242 }
243
244 props := bazel.BazelTargetModuleProperties{
245 Rule_class: "apex_key",
246 Bzl_load_location: "//build/bazel/rules:apex_key.bzl",
247 }
248
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000249 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, attrs)
Rupert Shuttlewortheb8c85a2021-07-27 07:10:32 -0400250}