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 
eeDataGet(uint32_t name,void * buf,uint32_t * szP)93 bool eeDataGet(uint32_t name, void *buf, uint32_t *szP)
94 {
95     uint32_t offset = 0;
96 
97     return eeDataGetEx(name, &offset, false, buf, szP) != NULL;
98 }
99 
eeDataGetAllVersions(uint32_t name,void * buf,uint32_t * szP,void ** stateP)100 void *eeDataGetAllVersions(uint32_t name, void *buf, uint32_t *szP, void **stateP)
101 {
102     uint32_t offset = *(uint32_t*)stateP;
103     void *addr = eeDataGetEx(name, &offset, true, buf, szP);
104     *(uint32_t*)stateP = offset;
105     return addr;
106 }
107 
eeWrite(void * dst,const void * src,uint32_t len)108 static bool eeWrite(void *dst, const void *src, uint32_t len)
109 {
110     return BL.blProgramEe(dst, src, len, BL_FLASH_KEY1, BL_FLASH_KEY2);
111 }
112 
eeDataSet(uint32_t name,const void * buf,uint32_t len)113 bool eeDataSet(uint32_t name, const void *buf, uint32_t len)
114 {
115     uint32_t sz, effectiveSz, info = name + len * (EE_DATA_NAME_MAX + 1);
116     bool ret = true;
117     void *space;
118 
119     if (!eeIsValidName(name))
120         return false;
121 
122     //find the empty space at the end of everything and make sure it is really empty (size == EE_DATA_LEN_MAX)
123     space = eeFind(EE_DATA_NAME_MAX, NULL, false, &sz);
124     if (!space || sz != EE_DATA_LEN_MAX)
125         return false;
126 
127     //calculate effective size
128     effectiveSz = (len + 3) &~ 3;
129 
130     //verify we have the space
131     if ((uint8_t*)__eedata_end - (uint8_t*)space < effectiveSz)
132         return false;
133 
134     //write it in
135     ret = eeWrite(((uint32_t*)space) - 1, &info, sizeof(info)) && ret;
136     ret = eeWrite(space, buf, len) && ret;
137 
138     return ret;
139 }
140 
eeDataEraseOldVersion(uint32_t name,void * vaddr)141 bool eeDataEraseOldVersion(uint32_t name, void *vaddr)
142 {
143     uint32_t *addr = (uint32_t*)vaddr;
144     uint32_t v;
145 
146     // sanity check
147     if (!eeIsValidName(name) || addr < __eedata_start || addr >= (__eedata_end - 1))
148         return false;
149 
150     v = *addr;
151 
152     //verify name
153     if ((v & EE_DATA_NAME_MAX) != name)
154         return false;
155 
156     //clear name
157     v &=~ EE_DATA_NAME_MAX;
158 
159     //store result
160     return eeWrite(addr, &v, sizeof(v));
161 }
162