blob: 229d59358929a9a0fa1f5bd34cc6499b6000f698 [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 Park0ca3ce82019-02-18 15:25:04 +090019 "strings"
Jiyong Parkff1458f2018-10-12 21:49:38 +090020
21 "android/soong/android"
Colin Cross5f692ec2019-02-01 16:53:07 -080022
Jiyong Parkff1458f2018-10-12 21:49:38 +090023 "github.com/google/blueprint/proptools"
24)
25
26var String = proptools.String
27
28func init() {
29 android.RegisterModuleType("apex_key", apexKeyFactory)
Jiyong Park0ca3ce82019-02-18 15:25:04 +090030 android.RegisterSingletonType("apex_keys_text", apexKeysTextFactory)
Jiyong Parkff1458f2018-10-12 21:49:38 +090031}
32
33type apexKey struct {
34 android.ModuleBase
35
36 properties apexKeyProperties
37
38 public_key_file android.Path
39 private_key_file android.Path
40
41 keyName string
42}
43
44type apexKeyProperties struct {
Jiyong Park67882562019-03-21 01:11:21 +090045 // Path or module to the public key file in avbpubkey format. Installed to the device.
Jiyong Parkff1458f2018-10-12 21:49:38 +090046 // Base name of the file is used as the ID for the key.
Jiyong Park67882562019-03-21 01:11:21 +090047 Public_key *string `android:"path"`
48 // Path or module to the private key file in pem format. Used to sign APEXs.
49 Private_key *string `android:"path"`
Jiyong Park50d99202018-12-27 13:32:34 +090050
51 // Whether this key is installable to one of the partitions. Defualt: true.
52 Installable *bool
Jiyong Parkff1458f2018-10-12 21:49:38 +090053}
54
55func apexKeyFactory() android.Module {
56 module := &apexKey{}
57 module.AddProperties(&module.properties)
dimitryf8071672019-04-11 17:27:11 +020058 android.InitAndroidArchModule(module, android.HostAndDeviceDefault, android.MultilibCommon)
Jiyong Parkff1458f2018-10-12 21:49:38 +090059 return module
60}
61
Jiyong Park50d99202018-12-27 13:32:34 +090062func (m *apexKey) installable() bool {
Jiyong Park42cca6c2019-04-01 11:15:50 +090063 return false
Jiyong Park50d99202018-12-27 13:32:34 +090064}
65
Jiyong Parkff1458f2018-10-12 21:49:38 +090066func (m *apexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park67882562019-03-21 01:11:21 +090067 // If the keys are from other modules (i.e. :module syntax) respect it.
68 // Otherwise, try to locate the key files in the default cert dir or
69 // in the local module dir
70 if android.SrcIsModule(String(m.properties.Public_key)) != "" {
Jiyong Park9335a262018-12-24 11:31:58 +090071 m.public_key_file = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
Jiyong Park67882562019-03-21 01:11:21 +090072 } else {
73 m.public_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Public_key))
74 // If not found, fall back to the local key pairs
75 if !android.ExistentPathForSource(ctx, m.public_key_file.String()).Valid() {
76 m.public_key_file = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
77 }
Jiyong Park9335a262018-12-24 11:31:58 +090078 }
Jiyong Park67882562019-03-21 01:11:21 +090079
80 if android.SrcIsModule(String(m.properties.Private_key)) != "" {
Jiyong Park9335a262018-12-24 11:31:58 +090081 m.private_key_file = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
Jiyong Park67882562019-03-21 01:11:21 +090082 } else {
83 m.private_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Private_key))
84 if !android.ExistentPathForSource(ctx, m.private_key_file.String()).Valid() {
85 m.private_key_file = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
86 }
Jiyong Park9335a262018-12-24 11:31:58 +090087 }
Jiyong Parkff1458f2018-10-12 21:49:38 +090088
89 pubKeyName := m.public_key_file.Base()[0 : len(m.public_key_file.Base())-len(m.public_key_file.Ext())]
90 privKeyName := m.private_key_file.Base()[0 : len(m.private_key_file.Base())-len(m.private_key_file.Ext())]
91
Jaewoong Jung939ebd52019-03-26 15:07:36 -070092 if m.properties.Public_key != nil && m.properties.Private_key != nil && pubKeyName != privKeyName {
Jiyong Parkff1458f2018-10-12 21:49:38 +090093 ctx.ModuleErrorf("public_key %q (keyname:%q) and private_key %q (keyname:%q) do not have same keyname",
94 m.public_key_file.String(), pubKeyName, m.private_key_file, privKeyName)
95 return
96 }
97 m.keyName = pubKeyName
Jiyong Parkff1458f2018-10-12 21:49:38 +090098}
Jiyong Park0ca3ce82019-02-18 15:25:04 +090099
100////////////////////////////////////////////////////////////////////////
101// apex_keys_text
Jiyong Park37eb8bb2019-02-20 22:23:29 +0900102type apexKeysText struct {
103 output android.OutputPath
104}
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900105
106func (s *apexKeysText) GenerateBuildActions(ctx android.SingletonContext) {
Jiyong Park37eb8bb2019-02-20 22:23:29 +0900107 s.output = android.PathForOutput(ctx, "apexkeys.txt")
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900108 var filecontent strings.Builder
109 ctx.VisitAllModules(func(module android.Module) {
110 if m, ok := module.(android.Module); ok && !m.Enabled() {
111 return
112 }
113
114 if m, ok := module.(*apexBundle); ok {
115 fmt.Fprintf(&filecontent,
116 "name=%q public_key=%q private_key=%q container_certificate=%q container_private_key=%q\\n",
117 m.Name()+".apex",
118 m.public_key_file.String(),
119 m.private_key_file.String(),
120 m.container_certificate_file.String(),
121 m.container_private_key_file.String())
122 }
123 })
124 ctx.Build(pctx, android.BuildParams{
125 Rule: android.WriteFile,
Jiyong Park37eb8bb2019-02-20 22:23:29 +0900126 Description: "apexkeys.txt",
127 Output: s.output,
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900128 Args: map[string]string{
129 "content": filecontent.String(),
130 },
131 })
132}
133
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900134func apexKeysTextFactory() android.Singleton {
135 return &apexKeysText{}
136}
137
Jiyong Park37eb8bb2019-02-20 22:23:29 +0900138func (s *apexKeysText) MakeVars(ctx android.MakeVarsContext) {
139 ctx.Strict("SOONG_APEX_KEYS_FILE", s.output.String())
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900140}