blob: fac71281f81bcaf52f4782bfcf126edb3880da7d [file] [log] [blame]
Dmitry Grinberg77044852016-02-03 16:08:28 -08001/*
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
Dmitry Grinberg77044852016-02-03 16:08:28 -080017#include <stdbool.h>
Dmitry Grinberg77044852016-02-03 16:08:28 -080018#include <stdint.h>
Alexey Polyudovbf34a2b2016-07-22 01:00:28 -070019#include <string.h>
20
21#include <bl.h>
Dmitry Grinberg77044852016-02-03 16:08:28 -080022#include <eeData.h>
23
Dmitry Grinberg77044852016-02-03 16:08:28 -080024extern uint32_t __eedata_start[], __eedata_end[];
25
26//STM32F4xx eedata stores data in 4-byte aligned chunks
27
Alexey Polyudov6215cd42016-05-13 08:31:40 -070028static void* eeFind(uint32_t nameToFind, uint32_t *offset, bool findFirst, uint32_t *szP)
Dmitry Grinberg77044852016-02-03 16:08:28 -080029{
Alexey Polyudov6215cd42016-05-13 08:31:40 -070030 uint32_t *p = __eedata_start + (offset ? *offset : 0);
Dmitry Grinberg77044852016-02-03 16:08:28 -080031 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
Alexey Polyudov6215cd42016-05-13 08:31:40 -070057 if (offset)
58 *offset = p - __eedata_start;
59
Dmitry Grinberg77044852016-02-03 16:08:28 -080060 return foundData;
61}
62
63static bool eeIsValidName(uint32_t name)
64{
65 return name && name < EE_DATA_NAME_MAX;
66}
67
Alexey Polyudovb62d65a2016-05-13 11:58:44 -070068static void *eeDataGetEx(uint32_t name, uint32_t *offsetP, bool first, void *buf, uint32_t *szP)
Dmitry Grinberg77044852016-02-03 16:08:28 -080069{
70 uint32_t sz = 0;
71 void *data;
72
73 if (!eeIsValidName(name))
74 return false;
75
76 //find the data item
Alexey Polyudov6215cd42016-05-13 08:31:40 -070077 data = eeFind(name, offsetP, first, &sz);
Dmitry Grinberg77044852016-02-03 16:08:28 -080078 if (!data)
Alexey Polyudovb62d65a2016-05-13 11:58:44 -070079 return NULL;
Dmitry Grinberg77044852016-02-03 16:08:28 -080080
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
Alexey Polyudovb62d65a2016-05-13 11:58:44 -070090 return (uint32_t*)data - 1;
Dmitry Grinberg77044852016-02-03 16:08:28 -080091}
92
Ben Fennemae4c235d2018-02-16 16:50:22 -080093uint32_t eeDataGetSize()
94{
95 return __eedata_end - __eedata_start;
96}
97
98uint32_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
Dmitry Grinberg77044852016-02-03 16:08:28 -0800121bool eeDataGet(uint32_t name, void *buf, uint32_t *szP)
122{
123 uint32_t offset = 0;
124
Alexey Polyudovb62d65a2016-05-13 11:58:44 -0700125 return eeDataGetEx(name, &offset, false, buf, szP) != NULL;
Dmitry Grinberg77044852016-02-03 16:08:28 -0800126}
127
Alexey Polyudovb62d65a2016-05-13 11:58:44 -0700128void *eeDataGetAllVersions(uint32_t name, void *buf, uint32_t *szP, void **stateP)
Dmitry Grinberg77044852016-02-03 16:08:28 -0800129{
130 uint32_t offset = *(uint32_t*)stateP;
Alexey Polyudovb62d65a2016-05-13 11:58:44 -0700131 void *addr = eeDataGetEx(name, &offset, true, buf, szP);
Dmitry Grinberg77044852016-02-03 16:08:28 -0800132 *(uint32_t*)stateP = offset;
Alexey Polyudovb62d65a2016-05-13 11:58:44 -0700133 return addr;
Dmitry Grinberg77044852016-02-03 16:08:28 -0800134}
135
136static 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
141bool 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)
Alexey Polyudov6215cd42016-05-13 08:31:40 -0700151 space = eeFind(EE_DATA_NAME_MAX, NULL, false, &sz);
Dmitry Grinberg77044852016-02-03 16:08:28 -0800152 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
Alexey Polyudovb62d65a2016-05-13 11:58:44 -0700169bool eeDataEraseOldVersion(uint32_t name, void *vaddr)
Dmitry Grinberg77044852016-02-03 16:08:28 -0800170{
Alexey Polyudovb62d65a2016-05-13 11:58:44 -0700171 uint32_t *addr = (uint32_t*)vaddr;
172 uint32_t v;
Dmitry Grinberg77044852016-02-03 16:08:28 -0800173
Sayak Dutta8f1907a2021-07-18 15:28:16 +0000174 // validate name and address
Alexey Polyudovb62d65a2016-05-13 11:58:44 -0700175 if (!eeIsValidName(name) || addr < __eedata_start || addr >= (__eedata_end - 1))
Dmitry Grinberg77044852016-02-03 16:08:28 -0800176 return false;
177
Alexey Polyudovb62d65a2016-05-13 11:58:44 -0700178 v = *addr;
179
Dmitry Grinberg77044852016-02-03 16:08:28 -0800180 //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
Alexey Polyudovb62d65a2016-05-13 11:58:44 -0700188 return eeWrite(addr, &v, sizeof(v));
Dmitry Grinberg77044852016-02-03 16:08:28 -0800189}