blob: 5282416c67b969e4dbc837155f1afd7e44ecd36d [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"
19 "io"
20
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)
30}
31
32type apexKey struct {
33 android.ModuleBase
34
35 properties apexKeyProperties
36
37 public_key_file android.Path
38 private_key_file android.Path
39
40 keyName string
41}
42
43type apexKeyProperties struct {
44 // Path to the public key file in avbpubkey format. Installed to the device.
45 // Base name of the file is used as the ID for the key.
46 Public_key *string
47 // Path to the private key file in pem format. Used to sign APEXs.
48 Private_key *string
Jiyong Park50d99202018-12-27 13:32:34 +090049
50 // Whether this key is installable to one of the partitions. Defualt: true.
51 Installable *bool
Jiyong Parkff1458f2018-10-12 21:49:38 +090052}
53
54func apexKeyFactory() android.Module {
55 module := &apexKey{}
56 module.AddProperties(&module.properties)
57 android.InitAndroidModule(module)
58 return module
59}
60
Jiyong Park50d99202018-12-27 13:32:34 +090061func (m *apexKey) installable() bool {
62 return m.properties.Installable == nil || proptools.Bool(m.properties.Installable)
63}
64
Jiyong Parkff1458f2018-10-12 21:49:38 +090065func (m *apexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park9335a262018-12-24 11:31:58 +090066 m.public_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Public_key))
67 m.private_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Private_key))
68
69 // If not found, fall back to the local key pairs
70 if !android.ExistentPathForSource(ctx, m.public_key_file.String()).Valid() {
71 m.public_key_file = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
72 }
73 if !android.ExistentPathForSource(ctx, m.private_key_file.String()).Valid() {
74 m.private_key_file = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
75 }
Jiyong Parkff1458f2018-10-12 21:49:38 +090076
77 pubKeyName := m.public_key_file.Base()[0 : len(m.public_key_file.Base())-len(m.public_key_file.Ext())]
78 privKeyName := m.private_key_file.Base()[0 : len(m.private_key_file.Base())-len(m.private_key_file.Ext())]
79
80 if pubKeyName != privKeyName {
81 ctx.ModuleErrorf("public_key %q (keyname:%q) and private_key %q (keyname:%q) do not have same keyname",
82 m.public_key_file.String(), pubKeyName, m.private_key_file, privKeyName)
83 return
84 }
85 m.keyName = pubKeyName
86
Jiyong Park50d99202018-12-27 13:32:34 +090087 if m.installable() {
88 ctx.InstallFile(android.PathForModuleInstall(ctx, "etc/security/apex"), m.keyName, m.public_key_file)
89 }
Jiyong Parkff1458f2018-10-12 21:49:38 +090090}
91
92func (m *apexKey) AndroidMk() android.AndroidMkData {
93 return android.AndroidMkData{
94 Class: "ETC",
95 OutputFile: android.OptionalPathForPath(m.public_key_file),
96 Extra: []android.AndroidMkExtraFunc{
97 func(w io.Writer, outputFile android.Path) {
98 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", "$(TARGET_OUT)/etc/security/apex")
99 fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", m.keyName)
Jiyong Park50d99202018-12-27 13:32:34 +0900100 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !m.installable())
Jiyong Parkff1458f2018-10-12 21:49:38 +0900101 },
102 },
103 }
104}