blob: 752888da2edf0a17284ade0b32112741f600721e [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"
Colin Cross5f692ec2019-02-01 16:53:07 -080023
Jiyong Parkff1458f2018-10-12 21:49:38 +090024 "github.com/google/blueprint/proptools"
25)
26
27var String = proptools.String
28
29func init() {
Jiyong Parkd1063c12019-07-17 20:08:41 +090030 android.RegisterModuleType("apex_key", ApexKeyFactory)
Jiyong Park0ca3ce82019-02-18 15:25:04 +090031 android.RegisterSingletonType("apex_keys_text", apexKeysTextFactory)
Jiyong Parkff1458f2018-10-12 21:49:38 +090032}
33
34type apexKey struct {
35 android.ModuleBase
36
37 properties apexKeyProperties
38
Jaewoong Jung18aefc12020-12-21 09:11:10 -080039 publicKeyFile android.Path
40 privateKeyFile android.Path
Jiyong Parkff1458f2018-10-12 21:49:38 +090041
42 keyName string
43}
44
45type apexKeyProperties struct {
Jiyong Park67882562019-03-21 01:11:21 +090046 // Path or module to the public key file in avbpubkey format. Installed to the device.
Jiyong Parkff1458f2018-10-12 21:49:38 +090047 // Base name of the file is used as the ID for the key.
Jiyong Park67882562019-03-21 01:11:21 +090048 Public_key *string `android:"path"`
49 // Path or module to the private key file in pem format. Used to sign APEXs.
50 Private_key *string `android:"path"`
Jiyong Park50d99202018-12-27 13:32:34 +090051
52 // Whether this key is installable to one of the partitions. Defualt: true.
53 Installable *bool
Jiyong Parkff1458f2018-10-12 21:49:38 +090054}
55
Jiyong Parkd1063c12019-07-17 20:08:41 +090056func ApexKeyFactory() android.Module {
Jiyong Parkff1458f2018-10-12 21:49:38 +090057 module := &apexKey{}
58 module.AddProperties(&module.properties)
dimitryf8071672019-04-11 17:27:11 +020059 android.InitAndroidArchModule(module, android.HostAndDeviceDefault, android.MultilibCommon)
Jiyong Parkff1458f2018-10-12 21:49:38 +090060 return module
61}
62
Jiyong Park50d99202018-12-27 13:32:34 +090063func (m *apexKey) installable() bool {
Jiyong Park42cca6c2019-04-01 11:15:50 +090064 return false
Jiyong Park50d99202018-12-27 13:32:34 +090065}
66
Jiyong Parkff1458f2018-10-12 21:49:38 +090067func (m *apexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park67882562019-03-21 01:11:21 +090068 // If the keys are from other modules (i.e. :module syntax) respect it.
69 // Otherwise, try to locate the key files in the default cert dir or
70 // in the local module dir
71 if android.SrcIsModule(String(m.properties.Public_key)) != "" {
Jaewoong Jung18aefc12020-12-21 09:11:10 -080072 m.publicKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
Jiyong Park67882562019-03-21 01:11:21 +090073 } else {
Jaewoong Jung18aefc12020-12-21 09:11:10 -080074 m.publicKeyFile = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Public_key))
Jiyong Park67882562019-03-21 01:11:21 +090075 // If not found, fall back to the local key pairs
Jaewoong Jung18aefc12020-12-21 09:11:10 -080076 if !android.ExistentPathForSource(ctx, m.publicKeyFile.String()).Valid() {
77 m.publicKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
Jiyong Park67882562019-03-21 01:11:21 +090078 }
Jiyong Park9335a262018-12-24 11:31:58 +090079 }
Jiyong Park67882562019-03-21 01:11:21 +090080
81 if android.SrcIsModule(String(m.properties.Private_key)) != "" {
Jaewoong Jung18aefc12020-12-21 09:11:10 -080082 m.privateKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
Jiyong Park67882562019-03-21 01:11:21 +090083 } else {
Jaewoong Jung18aefc12020-12-21 09:11:10 -080084 m.privateKeyFile = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Private_key))
85 if !android.ExistentPathForSource(ctx, m.privateKeyFile.String()).Valid() {
86 m.privateKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
Jiyong Park67882562019-03-21 01:11:21 +090087 }
Jiyong Park9335a262018-12-24 11:31:58 +090088 }
Jiyong Parkff1458f2018-10-12 21:49:38 +090089
Jaewoong Jung18aefc12020-12-21 09:11:10 -080090 pubKeyName := m.publicKeyFile.Base()[0 : len(m.publicKeyFile.Base())-len(m.publicKeyFile.Ext())]
91 privKeyName := m.privateKeyFile.Base()[0 : len(m.privateKeyFile.Base())-len(m.privateKeyFile.Ext())]
Jiyong Parkff1458f2018-10-12 21:49:38 +090092
Jaewoong Jung939ebd52019-03-26 15:07:36 -070093 if m.properties.Public_key != nil && m.properties.Private_key != nil && pubKeyName != privKeyName {
Jiyong Parkff1458f2018-10-12 21:49:38 +090094 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 -080095 m.publicKeyFile.String(), pubKeyName, m.privateKeyFile, privKeyName)
Jiyong Parkff1458f2018-10-12 21:49:38 +090096 return
97 }
98 m.keyName = pubKeyName
Jiyong Parkff1458f2018-10-12 21:49:38 +090099}
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900100
101////////////////////////////////////////////////////////////////////////
102// apex_keys_text
Jiyong Park37eb8bb2019-02-20 22:23:29 +0900103type apexKeysText struct {
104 output android.OutputPath
105}
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900106
107func (s *apexKeysText) GenerateBuildActions(ctx android.SingletonContext) {
Jiyong Park37eb8bb2019-02-20 22:23:29 +0900108 s.output = android.PathForOutput(ctx, "apexkeys.txt")
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900109 type apexKeyEntry struct {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800110 name string
111 presigned bool
112 publicKey string
113 privateKey string
114 containerCertificate string
115 containerPrivateKey string
116 partition string
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900117 }
118 toString := func(e apexKeyEntry) string {
Colin Crosscf371cc2020-11-13 11:48:42 -0800119 format := "name=%q public_key=%q private_key=%q container_certificate=%q container_private_key=%q partition=%q\n"
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900120 if e.presigned {
121 return fmt.Sprintf(format, e.name, "PRESIGNED", "PRESIGNED", "PRESIGNED", "PRESIGNED", e.partition)
122 } else {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800123 return fmt.Sprintf(format, e.name, e.publicKey, e.privateKey, e.containerCertificate, e.containerPrivateKey, e.partition)
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900124 }
125 }
126
127 apexKeyMap := make(map[string]apexKeyEntry)
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900128 ctx.VisitAllModules(func(module android.Module) {
Jiyong Park4d277042019-04-23 18:00:10 +0900129 if m, ok := module.(*apexBundle); ok && m.Enabled() && m.installable() {
Jiyong Parkb81b9902020-11-24 19:51:18 +0900130 pem, key := m.getCertificateAndPrivateKey(ctx)
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900131 apexKeyMap[m.Name()] = apexKeyEntry{
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800132 name: m.Name() + ".apex",
133 presigned: false,
134 publicKey: m.publicKeyFile.String(),
135 privateKey: m.privateKeyFile.String(),
136 containerCertificate: pem.String(),
137 containerPrivateKey: key.String(),
138 partition: m.PartitionTag(ctx.DeviceConfig()),
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900139 }
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900140 }
Jiyong Park4d277042019-04-23 18:00:10 +0900141 })
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900142
Jiyong Park4d277042019-04-23 18:00:10 +0900143 // Find prebuilts and let them override apexBundle if they are preferred
144 ctx.VisitAllModules(func(module android.Module) {
145 if m, ok := module.(*Prebuilt); ok && m.Enabled() && m.installable() &&
146 m.Prebuilt().UsePrebuilt() {
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900147 apexKeyMap[m.BaseModuleName()] = apexKeyEntry{
148 name: m.InstallFilename(),
149 presigned: true,
150 partition: m.PartitionTag(ctx.DeviceConfig()),
151 }
152 }
153 })
154
155 // Find apex_set and let them override apexBundle or prebuilts. This is done in a separate pass
156 // so that apex_set are not overridden by prebuilts.
157 ctx.VisitAllModules(func(module android.Module) {
158 if m, ok := module.(*ApexSet); ok && m.Enabled() {
159 entry := apexKeyEntry{
160 name: m.InstallFilename(),
161 presigned: true,
162 partition: m.PartitionTag(ctx.DeviceConfig()),
163 }
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900164 apexKeyMap[m.BaseModuleName()] = entry
Jiyong Park4d277042019-04-23 18:00:10 +0900165 }
166 })
167
168 // iterating over map does not give consistent ordering in golang
169 var moduleNames []string
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900170 for key, _ := range apexKeyMap {
Jiyong Park4d277042019-04-23 18:00:10 +0900171 moduleNames = append(moduleNames, key)
172 }
173 sort.Strings(moduleNames)
174
175 var filecontent strings.Builder
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900176 for _, name := range moduleNames {
Colin Crosscf371cc2020-11-13 11:48:42 -0800177 filecontent.WriteString(toString(apexKeyMap[name]))
Jiyong Park4d277042019-04-23 18:00:10 +0900178 }
Colin Crosscf371cc2020-11-13 11:48:42 -0800179 android.WriteFileRule(ctx, s.output, filecontent.String())
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900180}
181
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900182func apexKeysTextFactory() android.Singleton {
183 return &apexKeysText{}
184}
185
Jiyong Park37eb8bb2019-02-20 22:23:29 +0900186func (s *apexKeysText) MakeVars(ctx android.MakeVarsContext) {
187 ctx.Strict("SOONG_APEX_KEYS_FILE", s.output.String())
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900188}