| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2003-2010, Broadcom Corporation |
|---|
| 3 | * All Rights Reserved |
|---|
| 4 | * Confidential Property of Broadcom Corporation |
|---|
| 5 | * |
|---|
| 6 | * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 7 | * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 8 | * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 9 | * |
|---|
| 10 | * $brcm_Workfile: $ |
|---|
| 11 | * $brcm_Revision: $ |
|---|
| 12 | * $brcm_Date: $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: simple log structured flash database |
|---|
| 15 | * |
|---|
| 16 | * Revision History: |
|---|
| 17 | * |
|---|
| 18 | * $brcm_Log: $ |
|---|
| 19 | * |
|---|
| 20 | * |
|---|
| 21 | ***************************************************************************/ |
|---|
| 22 | #if !defined(__BFDB_H__) |
|---|
| 23 | #define __BFDB_H__ |
|---|
| 24 | |
|---|
| 25 | /** |
|---|
| 26 | Database consists of tuples with each tuple containing up to 256 bytes of |
|---|
| 27 | data. Each tuple is identified with ID which can be anything from 1 to 0xfd. |
|---|
| 28 | ID 0 is reserved for erased tuple, ID 0xff idicates empty flash and ID 0xfe |
|---|
| 29 | is reserved for house keeping data. Database is very simple, cursor based. |
|---|
| 30 | To operate on any record caller must first position the cursor on the record |
|---|
| 31 | and then perform desired operation. |
|---|
| 32 | |
|---|
| 33 | To position cursor on any given record: |
|---|
| 34 | 1. Position cursor to the beginning of the database by calling |
|---|
| 35 | bfdb_rewind . |
|---|
| 36 | 2. Call bfdb_next to advance cursor to the next record with given ID. |
|---|
| 37 | |
|---|
| 38 | Database records can be accessed by: |
|---|
| 39 | 1. Positioning cursor to the desired record |
|---|
| 40 | 2. Calling bfdb_get to retreive record pointed by cursor. |
|---|
| 41 | |
|---|
| 42 | Database record can be deleted by: |
|---|
| 43 | 1. Positioning cursor on the desired record |
|---|
| 44 | 2. Calling bfdb_delete to mark record as deleted. |
|---|
| 45 | |
|---|
| 46 | Database record can be added by: |
|---|
| 47 | 1. Calling bfdb_add with record ID and record data. |
|---|
| 48 | |
|---|
| 49 | Since the data base is designed operate with flash there is no way to modify |
|---|
| 50 | a record. Caller must add new record and erase old one to achieve the same |
|---|
| 51 | effect. |
|---|
| 52 | |
|---|
| 53 | In case of database corruption or code lockup due to database structure, |
|---|
| 54 | content of database must be dumped in to a file for analisys. To dump |
|---|
| 55 | content use gdb "dump" command: |
|---|
| 56 | dump binary memory flash.raw <start address> <end address> |
|---|
| 57 | flash.raw is the name of the file and can be changes as desired |
|---|
| 58 | <start address> is the starting address of the database and for 1MB flash |
|---|
| 59 | where last 64K are occupied by database it is calculated as following: |
|---|
| 60 | 0xbec00000 + 0x100000 - 0x10000 |
|---|
| 61 | <end address> is the end address of the database and for the same conditions |
|---|
| 62 | as above is calculated as following: |
|---|
| 63 | 0xbec00000 + 0x100000 |
|---|
| 64 | Direct calculations can be used on gdb input line since gdb will understand |
|---|
| 65 | and process these expressions. |
|---|
| 66 | */ |
|---|
| 67 | |
|---|
| 68 | #include "bfds.h" |
|---|
| 69 | |
|---|
| 70 | typedef int bfdb_err; |
|---|
| 71 | |
|---|
| 72 | #define BFDB_OK 0 |
|---|
| 73 | #define BFDB_INVALID_PARAMETER 1 |
|---|
| 74 | #define BFDB_STORE_ERROR 2 |
|---|
| 75 | #define BFDB_RECORD_NOT_FOUND 3 |
|---|
| 76 | #define BFDB_INTERNAL_ERROR 4 |
|---|
| 77 | #define BFDB_LOG_ERROR 5 |
|---|
| 78 | #define BFDB_LOG_FULL 6 |
|---|
| 79 | #define BFDB_ALREADY_COMPACT 7 |
|---|
| 80 | |
|---|
| 81 | /* Reserved database ids */ |
|---|
| 82 | #define BFDB_TUPLE_ERASED 0x0 |
|---|
| 83 | #define BFDB_TUPLE_BLANK 0xff |
|---|
| 84 | #define BFDB_TUPLE_LOG 0xfe |
|---|
| 85 | |
|---|
| 86 | /* Maximum size of tuple allowed */ |
|---|
| 87 | #define BFDB_DATA_MAX 0xff |
|---|
| 88 | |
|---|
| 89 | /* Record structure */ |
|---|
| 90 | struct bfdb_tuple { |
|---|
| 91 | uint8_t id; |
|---|
| 92 | uint8_t size; |
|---|
| 93 | uint8_t data[2]; |
|---|
| 94 | } __attribute__((packed)); |
|---|
| 95 | |
|---|
| 96 | /** |
|---|
| 97 | bfdb_settings structure defines configuration of database and memory |
|---|
| 98 | allocated to the database. There are some restrictions on memory - |
|---|
| 99 | db_offset must be aligned on sector_size |
|---|
| 100 | db_size must be aligned on sector_sise so the database fits in whole |
|---|
| 101 | number of sectors. |
|---|
| 102 | page_size is dependent on flash part and can be obtained from datasheet |
|---|
| 103 | sector_size is also dependent on flash part. |
|---|
| 104 | */ |
|---|
| 105 | struct bfdb_settings { |
|---|
| 106 | uint32_t page_size; /* size of writeable page */ |
|---|
| 107 | uint32_t sector_size; /* size of minimal eraseable sector */ |
|---|
| 108 | uint32_t db_offset; /* starting offset of the database in flash */ |
|---|
| 109 | uint32_t db_size; /* size of flash allocated to the database */ |
|---|
| 110 | }; |
|---|
| 111 | |
|---|
| 112 | typedef struct bfdb_state * bfdb_handle; |
|---|
| 113 | |
|---|
| 114 | /** |
|---|
| 115 | Private structure for use by internal functions. |
|---|
| 116 | fl_start - offset of first byte of flash allocated to db |
|---|
| 117 | fl_end - offset of fiirst free byte of flash after db |
|---|
| 118 | db_head - offset off start of database log. |
|---|
| 119 | db_tail - offset off first free byte where the next write should start. |
|---|
| 120 | */ |
|---|
| 121 | struct bfdb_state { |
|---|
| 122 | uint32_t magic; |
|---|
| 123 | uint32_t page_size; |
|---|
| 124 | uint32_t sector_size; |
|---|
| 125 | uint32_t fl_start; |
|---|
| 126 | uint32_t fl_end; |
|---|
| 127 | uint32_t db_head; |
|---|
| 128 | uint32_t db_tail; |
|---|
| 129 | bfds_handle store; |
|---|
| 130 | uint8_t * page_buffer; |
|---|
| 131 | uint8_t * page_buffer2; |
|---|
| 132 | uint32_t pb_size; |
|---|
| 133 | uint32_t cursor; |
|---|
| 134 | uint32_t sector_sum32; |
|---|
| 135 | }; |
|---|
| 136 | |
|---|
| 137 | /*************************************************************************** |
|---|
| 138 | Summary: |
|---|
| 139 | Open database and initialize in memory data structures. |
|---|
| 140 | Description: |
|---|
| 141 | Database will be positioned on the first valid record; |
|---|
| 142 | Input: |
|---|
| 143 | Output: |
|---|
| 144 | Returns: |
|---|
| 145 | SeeAlso: |
|---|
| 146 | None |
|---|
| 147 | ***************************************************************************/ |
|---|
| 148 | bfdb_err bfdb_open(struct bfdb_settings * settings, bfdb_handle * handle); |
|---|
| 149 | |
|---|
| 150 | /*************************************************************************** |
|---|
| 151 | Summary: |
|---|
| 152 | Move database cursor to the first record with given id in the database. |
|---|
| 153 | Description: |
|---|
| 154 | Function will move cursor to the first record in the database, then it will |
|---|
| 155 | advance one record at the time until it finds first valid record with |
|---|
| 156 | specified id. If no record with desired id is found function will return an |
|---|
| 157 | error. |
|---|
| 158 | Input: |
|---|
| 159 | Output: |
|---|
| 160 | Returns: |
|---|
| 161 | SeeAlso: |
|---|
| 162 | None |
|---|
| 163 | ***************************************************************************/ |
|---|
| 164 | bfdb_err bfdb_rewind(bfdb_handle handle, uint8_t id); |
|---|
| 165 | |
|---|
| 166 | /*************************************************************************** |
|---|
| 167 | Summary: |
|---|
| 168 | Move database cursor to the next record with given id in the database. |
|---|
| 169 | Description: |
|---|
| 170 | Function will move cursor from current record to the next record with |
|---|
| 171 | the specified id. If no record with such id is found function will return |
|---|
| 172 | an error. |
|---|
| 173 | Input: |
|---|
| 174 | Output: |
|---|
| 175 | Returns: |
|---|
| 176 | SeeAlso: |
|---|
| 177 | None |
|---|
| 178 | ***************************************************************************/ |
|---|
| 179 | bfdb_err bfdb_next(bfdb_handle handle, uint8_t id); |
|---|
| 180 | |
|---|
| 181 | /*************************************************************************** |
|---|
| 182 | Summary: |
|---|
| 183 | Get data from the database record pointed by the cursor. |
|---|
| 184 | Description: |
|---|
| 185 | Function will read specified amount of data from record that is pointed by |
|---|
| 186 | the cursor. Data will be placed in to memory pointed by given pointer. No |
|---|
| 187 | more data than is specified by the size will be copied. If specified size |
|---|
| 188 | is larger than amount of data in the record only data from the record will |
|---|
| 189 | be copied. |
|---|
| 190 | Input: |
|---|
| 191 | Output: |
|---|
| 192 | Returns: |
|---|
| 193 | SeeAlso: |
|---|
| 194 | None |
|---|
| 195 | ***************************************************************************/ |
|---|
| 196 | bfdb_err bfdb_get(bfdb_handle handle, uint8_t * data, size_t size); |
|---|
| 197 | |
|---|
| 198 | /*************************************************************************** |
|---|
| 199 | Summary: |
|---|
| 200 | Add record to the end of the database. |
|---|
| 201 | Description: |
|---|
| 202 | Function will add a record to the end of the database. The record will have |
|---|
| 203 | specified id and will contain data of given size pointed by given pointer. |
|---|
| 204 | If provided size is larger than single record can contain, call will return |
|---|
| 205 | an error. Depending on state of database buffers, data may be commited to |
|---|
| 206 | flash. To make certain that data is commited caller should use bfdb_commit |
|---|
| 207 | call. Normally data will be commited when database page buffer will become |
|---|
| 208 | full to increase flash performace by combining smaller writes. |
|---|
| 209 | Input: |
|---|
| 210 | Output: |
|---|
| 211 | Returns: |
|---|
| 212 | SeeAlso: |
|---|
| 213 | None |
|---|
| 214 | ***************************************************************************/ |
|---|
| 215 | bfdb_err bfdb_add(bfdb_handle handle, uint8_t id, uint8_t * data, size_t size); |
|---|
| 216 | |
|---|
| 217 | /*************************************************************************** |
|---|
| 218 | Summary: |
|---|
| 219 | Delete record pointed by the cursor. |
|---|
| 220 | Description: |
|---|
| 221 | The record pointed by the cursor is marked as deleted. It will not come up |
|---|
| 222 | during the record iteration. The record data will still be present and |
|---|
| 223 | the occupied space will be reclaimed later. |
|---|
| 224 | Input: |
|---|
| 225 | Output: |
|---|
| 226 | Returns: |
|---|
| 227 | SeeAlso: |
|---|
| 228 | None |
|---|
| 229 | ***************************************************************************/ |
|---|
| 230 | bfdb_err bfdb_delete(bfdb_handle handle); |
|---|
| 231 | |
|---|
| 232 | /*************************************************************************** |
|---|
| 233 | Summary: |
|---|
| 234 | Compact database. |
|---|
| 235 | Description: |
|---|
| 236 | bfdb_compact will move valid data from the first sector to the end of the |
|---|
| 237 | database and after all of the data moved out it will erase empty sector. |
|---|
| 238 | Head of the log will advance in to the next sector. |
|---|
| 239 | Input: |
|---|
| 240 | Output: |
|---|
| 241 | Returns: |
|---|
| 242 | SeeAlso: |
|---|
| 243 | None |
|---|
| 244 | ***************************************************************************/ |
|---|
| 245 | bfdb_err bfdb_compact(bfdb_handle handle); |
|---|
| 246 | |
|---|
| 247 | /*************************************************************************** |
|---|
| 248 | Summary: |
|---|
| 249 | Erase database. |
|---|
| 250 | Description: |
|---|
| 251 | bfdb_erase will completely erase flash store for the database, setting all |
|---|
| 252 | bytes in the store to 0xff. Only sectors that are within the database |
|---|
| 253 | boundaries will be erased. This is completely destructive operation. |
|---|
| 254 | This call should only be used if database is corrupted. |
|---|
| 255 | Input: |
|---|
| 256 | Output: |
|---|
| 257 | Returns: |
|---|
| 258 | SeeAlso: |
|---|
| 259 | None |
|---|
| 260 | ***************************************************************************/ |
|---|
| 261 | bfdb_err bfdb_erase(bfdb_handle handle); |
|---|
| 262 | |
|---|
| 263 | #endif |
|---|