Dmitry Grinberg | 7704485 | 2016-02-03 16:08:28 -0800 | [diff] [blame] | 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 | |
Dmitry Grinberg | 7704485 | 2016-02-03 16:08:28 -0800 | [diff] [blame] | 17 | #include <stdbool.h> |
Dmitry Grinberg | 7704485 | 2016-02-03 16:08:28 -0800 | [diff] [blame] | 18 | #include <stdint.h> |
Alexey Polyudov | bf34a2b | 2016-07-22 01:00:28 -0700 | [diff] [blame] | 19 | #include <string.h> |
| 20 | |
| 21 | #include <bl.h> |
Dmitry Grinberg | 7704485 | 2016-02-03 16:08:28 -0800 | [diff] [blame] | 22 | #include <eeData.h> |
| 23 | |
Dmitry Grinberg | 7704485 | 2016-02-03 16:08:28 -0800 | [diff] [blame] | 24 | extern uint32_t __eedata_start[], __eedata_end[]; |
| 25 | |
| 26 | //STM32F4xx eedata stores data in 4-byte aligned chunks |
| 27 | |
Alexey Polyudov | 6215cd4 | 2016-05-13 08:31:40 -0700 | [diff] [blame] | 28 | static void* eeFind(uint32_t nameToFind, uint32_t *offset, bool findFirst, uint32_t *szP) |
Dmitry Grinberg | 7704485 | 2016-02-03 16:08:28 -0800 | [diff] [blame] | 29 | { |
Alexey Polyudov | 6215cd4 | 2016-05-13 08:31:40 -0700 | [diff] [blame] | 30 | uint32_t *p = __eedata_start + (offset ? *offset : 0); |
Dmitry Grinberg | 7704485 | 2016-02-03 16:08:28 -0800 | [diff] [blame] | 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 | |
Alexey Polyudov | 6215cd4 | 2016-05-13 08:31:40 -0700 | [diff] [blame] | 57 | if (offset) |
| 58 | *offset = p - __eedata_start; |
| 59 | |
Dmitry Grinberg | 7704485 | 2016-02-03 16:08:28 -0800 | [diff] [blame] | 60 | return foundData; |
| 61 | } |
| 62 | |
| 63 | static bool eeIsValidName(uint32_t name) |
| 64 | { |
| 65 | return name && name < EE_DATA_NAME_MAX; |
| 66 | } |
| 67 | |
Alexey Polyudov | b62d65a | 2016-05-13 11:58:44 -0700 | [diff] [blame] | 68 | static void *eeDataGetEx(uint32_t name, uint32_t *offsetP, bool first, void *buf, uint32_t *szP) |
Dmitry Grinberg | 7704485 | 2016-02-03 16:08:28 -0800 | [diff] [blame] | 69 | { |
| 70 | uint32_t sz = 0; |
| 71 | void *data; |
| 72 | |
| 73 | if (!eeIsValidName(name)) |
| 74 | return false; |
| 75 | |
| 76 | //find the data item |
Alexey Polyudov | 6215cd4 | 2016-05-13 08:31:40 -0700 | [diff] [blame] | 77 | data = eeFind(name, offsetP, first, &sz); |
Dmitry Grinberg | 7704485 | 2016-02-03 16:08:28 -0800 | [diff] [blame] | 78 | if (!data) |
Alexey Polyudov | b62d65a | 2016-05-13 11:58:44 -0700 | [diff] [blame] | 79 | return NULL; |
Dmitry Grinberg | 7704485 | 2016-02-03 16:08:28 -0800 | [diff] [blame] | 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 | |
Alexey Polyudov | b62d65a | 2016-05-13 11:58:44 -0700 | [diff] [blame] | 90 | return (uint32_t*)data - 1; |
Dmitry Grinberg | 7704485 | 2016-02-03 16:08:28 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Ben Fennema | e4c235d | 2018-02-16 16:50:22 -0800 | [diff] [blame] | 93 | uint32_t eeDataGetSize() |
| 94 | { |
| 95 | return __eedata_end - __eedata_start; |
| 96 | } |
| 97 | |
| 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 | |
Dmitry Grinberg | 7704485 | 2016-02-03 16:08:28 -0800 | [diff] [blame] | 121 | bool eeDataGet(uint32_t name, void *buf, uint32_t *szP) |
| 122 | { |
| 123 | uint32_t offset = 0; |
| 124 | |
Alexey Polyudov | b62d65a | 2016-05-13 11:58:44 -0700 | [diff] [blame] | 125 | return eeDataGetEx(name, &offset, false, buf, szP) != NULL; |
Dmitry Grinberg | 7704485 | 2016-02-03 16:08:28 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Alexey Polyudov | b62d65a | 2016-05-13 11:58:44 -0700 | [diff] [blame] | 128 | void *eeDataGetAllVersions(uint32_t name, void *buf, uint32_t *szP, void **stateP) |
Dmitry Grinberg | 7704485 | 2016-02-03 16:08:28 -0800 | [diff] [blame] | 129 | { |
| 130 | uint32_t offset = *(uint32_t*)stateP; |
Alexey Polyudov | b62d65a | 2016-05-13 11:58:44 -0700 | [diff] [blame] | 131 | void *addr = eeDataGetEx(name, &offset, true, buf, szP); |
Dmitry Grinberg | 7704485 | 2016-02-03 16:08:28 -0800 | [diff] [blame] | 132 | *(uint32_t*)stateP = offset; |
Alexey Polyudov | b62d65a | 2016-05-13 11:58:44 -0700 | [diff] [blame] | 133 | return addr; |
Dmitry Grinberg | 7704485 | 2016-02-03 16:08:28 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 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 | |
| 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) |
Alexey Polyudov | 6215cd4 | 2016-05-13 08:31:40 -0700 | [diff] [blame] | 151 | space = eeFind(EE_DATA_NAME_MAX, NULL, false, &sz); |
Dmitry Grinberg | 7704485 | 2016-02-03 16:08:28 -0800 | [diff] [blame] | 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 | |
Alexey Polyudov | b62d65a | 2016-05-13 11:58:44 -0700 | [diff] [blame] | 169 | bool eeDataEraseOldVersion(uint32_t name, void *vaddr) |
Dmitry Grinberg | 7704485 | 2016-02-03 16:08:28 -0800 | [diff] [blame] | 170 | { |
Alexey Polyudov | b62d65a | 2016-05-13 11:58:44 -0700 | [diff] [blame] | 171 | uint32_t *addr = (uint32_t*)vaddr; |
| 172 | uint32_t v; |
Dmitry Grinberg | 7704485 | 2016-02-03 16:08:28 -0800 | [diff] [blame] | 173 | |
Sayak Dutta | 8f1907a | 2021-07-18 15:28:16 +0000 | [diff] [blame] | 174 | // validate name and address |
Alexey Polyudov | b62d65a | 2016-05-13 11:58:44 -0700 | [diff] [blame] | 175 | if (!eeIsValidName(name) || addr < __eedata_start || addr >= (__eedata_end - 1)) |
Dmitry Grinberg | 7704485 | 2016-02-03 16:08:28 -0800 | [diff] [blame] | 176 | return false; |
| 177 | |
Alexey Polyudov | b62d65a | 2016-05-13 11:58:44 -0700 | [diff] [blame] | 178 | v = *addr; |
| 179 | |
Dmitry Grinberg | 7704485 | 2016-02-03 16:08:28 -0800 | [diff] [blame] | 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 |
Alexey Polyudov | b62d65a | 2016-05-13 11:58:44 -0700 | [diff] [blame] | 188 | return eeWrite(addr, &v, sizeof(v)); |
Dmitry Grinberg | 7704485 | 2016-02-03 16:08:28 -0800 | [diff] [blame] | 189 | } |