1 /******************************************************************************
2  *
3  *  Copyright (C) 1999-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 /******************************************************************************
20  * Construct a buffer that contains multiple Type-Length-Value contents
21  * that is used by the HAL in a CORE_SET_CONFIG NCI command.
22  ******************************************************************************/
23 
24 #define LOG_TAG "NfcNciHal"
25 #include "OverrideLog.h"
26 #include "StartupConfig.h"
27 
28 
29 const UINT8 StartupConfig::mMaxLength = 255;
30 
31 
32 /*******************************************************************************
33 **
34 ** Function:        initialize
35 **
36 ** Description:     Initialize all member variables.
37 **
38 ** Returns:         None
39 **
40 *******************************************************************************/
StartupConfig()41 StartupConfig::StartupConfig ()
42 {
43     //set first byte to 0, which is length of payload
44     mBuffer.append ((uint8_string::size_type) 1, (uint8_string::value_type) 0);
45 }
46 
47 
48 /*******************************************************************************
49 **
50 ** Function:        initialize
51 **
52 ** Description:     Reset all member variables.
53 **
54 ** Returns:         None
55 **
56 *******************************************************************************/
initialize()57 void StartupConfig::initialize ()
58 {
59     mBuffer.clear ();
60     //set first byte to 0, which is length of payload
61     mBuffer.append ((uint8_string::size_type) 1, (uint8_string::value_type) 0);
62 }
63 
64 
65 /*******************************************************************************
66 **
67 ** Function:        getInternalBuffer
68 **
69 ** Description:     Get the pointer to buffer that contains multiple
70 **                  Type-Length-Value contents.
71 **
72 ** Returns:         Pointer to buffer.
73 **
74 *******************************************************************************/
getInternalBuffer()75 const UINT8* StartupConfig::getInternalBuffer ()
76 {
77     return mBuffer.data ();
78 }
79 
80 
81 /*******************************************************************************
82 **
83 ** Function:        append
84 **
85 ** Description:     Append new config data to internal buffer.
86 **                  newContent: buffer containing new content; newContent[0] is
87 **                          payload length; newContent[1..end] is payload.
88 **                  newContentLen: total length of newContent.
89 **
90 ** Returns:         True if ok.
91 **
92 *******************************************************************************/
append(const UINT8 * newContent,UINT8 newContentLen)93 bool StartupConfig::append (const UINT8* newContent, UINT8 newContentLen)
94 {
95     static const char fn [] = "StartupConfig::append";
96     if ((newContentLen+mBuffer.size()) > mMaxLength)
97     {
98         ALOGE ("%s: exceed max length", fn);
99         return false;
100     }
101 
102     ALOGD ("%s: try append %u bytes", fn, (uint8_string::size_type) (newContentLen));
103     //append new payload into private buffer
104     mBuffer.append (newContent+1, (uint8_string::size_type) (newContentLen-1));
105     //increase size counter of payload in private buffer
106     mBuffer[0] = mBuffer[0] + newContentLen-1;
107     ALOGD ("%s: new size %u bytes", fn, mBuffer[0]);
108     return true;
109 };
110 
111 
112 /*******************************************************************************
113 **
114 ** Function:        disableSecureElement
115 **
116 ** Description:     Adjust a TLV to disable secure element(s).  The TLV's type is 0xC2.
117 **                  bitmask: 0xC0 = do not detect any secure element.
118 **                           0x40 = do not detect secure element in slot 0.
119 **                           0x80 = do not detect secure element in slot 1.
120 **
121 ** Returns:         True if ok.
122 **
123 *******************************************************************************/
disableSecureElement(UINT8 bitmask)124 bool StartupConfig::disableSecureElement (UINT8 bitmask)
125 {
126     const UINT8 maxLen = mBuffer[0];
127     UINT8 index = 1, tlvType = 0, tlvLen = 0;
128     bool found0xC2 = false;
129 
130     while (true)
131     {
132         if (index > maxLen)
133             break;
134         tlvType = mBuffer [index];
135         index++;
136         tlvLen = mBuffer [index];
137         index++;
138         if (tlvType == 0xC2) //this TLV controls secure elements
139         {
140             index++; //index of second byte in TLV's value
141             mBuffer [index] = mBuffer [index] | bitmask; //turn on certain bits
142             found0xC2 = true;
143         }
144         else
145             index += tlvLen;
146     }
147 
148     if (found0xC2 == false)
149     {
150         UINT8 tlv [] = {0x04, 0xC2, 0x02, 0x61, 0x00};
151         tlv [4] = tlv [4] | bitmask;
152         found0xC2 = append (tlv, 5);
153     }
154     return found0xC2;
155 }
156