1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <stdbool.h>
18 #include <stdint.h>
19 #include <string.h>
20
21 #include <bl.h>
22 #include <eeData.h>
23
24 extern uint32_t __eedata_start[], __eedata_end[];
25
26 //STM32F4xx eedata stores data in 4-byte aligned chunks
27
eeFind(uint32_t nameToFind,uint32_t * offset,bool findFirst,uint32_t * szP)28 static void* eeFind(uint32_t nameToFind, uint32_t *offset, bool findFirst, uint32_t *szP)
29 {
30 uint32_t *p = __eedata_start + (offset ? *offset : 0);
31 void *foundData = NULL;
32
33 //find the last incarnation of "name" in flash area
34 while (p < __eedata_end) {
35 uint32_t info = *p++;
36 uint32_t name = info & EE_DATA_NAME_MAX;
37 uint32_t sz = info / (EE_DATA_NAME_MAX + 1);
38 void *data = p;
39
40 //skip over to next data chunk header
41 p += (sz + 3) / 4;
42
43 //check for a match
44 if (nameToFind == name) {
45 *szP = sz;
46 foundData = data;
47
48 if (findFirst)
49 break;
50 }
51
52 //check for ending condition (name == max)
53 if (name == EE_DATA_NAME_MAX)
54 break;
55 }
56
57 if (offset)
58 *offset = p - __eedata_start;
59
60 return foundData;
61 }
62
eeIsValidName(uint32_t name)63 static bool eeIsValidName(uint32_t name)
64 {
65 return name && name < EE_DATA_NAME_MAX;
66 }
67
eeDataGetEx(uint32_t name,uint32_t * offsetP,bool first,void * buf,uint32_t * szP)68 static void *eeDataGetEx(uint32_t name, uint32_t *offsetP, bool first, void *buf, uint32_t *szP)
69 {
70 uint32_t sz = 0;
71 void *data;
72
73 if (!eeIsValidName(name))
74 return false;
75
76 //find the data item
77 data = eeFind(name, offsetP, first, &sz);
78 if (!data)
79 return NULL;
80
81 if (buf && szP) { //get the data
82 if (sz > *szP)
83 sz = *szP;
84 *szP = sz;
85 memcpy(buf, data, sz);
86 }
87 else if (szP) //get size
88 *szP = sz;
89
90 return (uint32_t*)data - 1;
91 }
92
eeDataGetSize()93 uint32_t eeDataGetSize()
94 {
95 return __eedata_end - __eedata_start;
96 }
97
eeDataGetFree()98 uint32_t eeDataGetFree()
99 {
100 uint32_t *p = __eedata_start;
101
102 //find the last incarnation of "name" in flash area
103 while (p < __eedata_end) {
104 uint32_t info = *p;
105 uint32_t name = info & EE_DATA_NAME_MAX;
106 uint32_t sz = info / (EE_DATA_NAME_MAX + 1);
107
108 //check for ending condition (name == max)
109 if (name == EE_DATA_NAME_MAX)
110 break;
111
112 p++;
113
114 //skip over to next data chunk header
115 p += (sz + 3) / 4;
116 }
117
118 return __eedata_end - p;
119 }
120
eeDataGet(uint32_t name,void * buf,uint32_t * szP)121 bool eeDataGet(uint32_t name, void *buf, uint32_t *szP)
122 {
123 uint32_t offset = 0;
124
125 return eeDataGetEx(name, &offset, false, buf, szP) != NULL;
126 }
127
eeDataGetAllVersions(uint32_t name,void * buf,uint32_t * szP,void ** stateP)128 void *eeDataGetAllVersions(uint32_t name, void *buf, uint32_t *szP, void **stateP)
129 {
130 uint32_t offset = *(uint32_t*)stateP;
131 void *addr = eeDataGetEx(name, &offset, true, buf, szP);
132 *(uint32_t*)stateP = offset;
133 return addr;
134 }
135
eeWrite(void * dst,const void * src,uint32_t len)136 static bool eeWrite(void *dst, const void *src, uint32_t len)
137 {
138 return BL.blProgramEe(dst, src, len, BL_FLASH_KEY1, BL_FLASH_KEY2);
139 }
140
eeDataSet(uint32_t name,const void * buf,uint32_t len)141 bool eeDataSet(uint32_t name, const void *buf, uint32_t len)
142 {
143 uint32_t sz, effectiveSz, info = name + len * (EE_DATA_NAME_MAX + 1);
144 bool ret = true;
145 void *space;
146
147 if (!eeIsValidName(name))
148 return false;
149
150 //find the empty space at the end of everything and make sure it is really empty (size == EE_DATA_LEN_MAX)
151 space = eeFind(EE_DATA_NAME_MAX, NULL, false, &sz);
152 if (!space || sz != EE_DATA_LEN_MAX)
153 return false;
154
155 //calculate effective size
156 effectiveSz = (len + 3) &~ 3;
157
158 //verify we have the space
159 if ((uint8_t*)__eedata_end - (uint8_t*)space < effectiveSz)
160 return false;
161
162 //write it in
163 ret = eeWrite(((uint32_t*)space) - 1, &info, sizeof(info)) && ret;
164 ret = eeWrite(space, buf, len) && ret;
165
166 return ret;
167 }
168
eeDataEraseOldVersion(uint32_t name,void * vaddr)169 bool eeDataEraseOldVersion(uint32_t name, void *vaddr)
170 {
171 uint32_t *addr = (uint32_t*)vaddr;
172 uint32_t v;
173
174 // sanity check
175 if (!eeIsValidName(name) || addr < __eedata_start || addr >= (__eedata_end - 1))
176 return false;
177
178 v = *addr;
179
180 //verify name
181 if ((v & EE_DATA_NAME_MAX) != name)
182 return false;
183
184 //clear name
185 v &=~ EE_DATA_NAME_MAX;
186
187 //store result
188 return eeWrite(addr, &v, sizeof(v));
189 }
190