-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFlashBlock.cpp
More file actions
executable file
·130 lines (109 loc) · 4.68 KB
/
FlashBlock.cpp
File metadata and controls
executable file
·130 lines (109 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA *
* *
***************************************************************************
* *
* (c) Copyright, 1997-2012, ANSR *
* *
***************************************************************************
* *
* Filename: FlashBlock.cpp *
* *
***************************************************************************/
#include "armlib.h"
/**
* Object that represents the contents of the flash buffer block.
*/
class FlashBuffer
{
public:
/// Constructor.
FlashBuffer() { std::memset (this, 0xff, sizeof(FlashBuffer)); };
/// Calculate the CRC-32 of the flash block header and contents.
uint32_t CRC() { return CRC32::Calculate(this, sizeof(FlashBuffer) - sizeof(uint32_t)); };
/// Unique 32-bit number to identify the start of the flash block.
uint32_t magicNumber;
/// Number of data bytes in the block.
uint32_t length;
/// Storage area of the flash data not include the magic number, length, and CRC-32.
uint8_t data[FlashBlock::FlashBlockSize - (3 * sizeof(uint32_t))];
/// Calculated CRC-32 of the flash block.
uint32_t crc32;
};
/**
* Constructor.
*
* @param magicNumber unique 32-bit number used to identify the start of the flash block
* @param flashAddress address of flash memory block to read/write
*/
FlashBlock::FlashBlock(uint32_t magicNumber, uint32_t flashAddress)
{
// Save the values for later use.
this->magicNumber = magicNumber;
this->flashAddress = flashAddress;
}
/**
* Get a pointer to the data stored in the flash block area.
*
* @return void pointer that may be cast to the desired data block
*/
const void *FlashBlock::Get()
{
return reinterpret_cast<const void *> (this->flashAddress + (2 * sizeof(uint32_t)));
}
/**
* Validate the contents of the flash block by checking the magic number and CRC-32.
*
* @return true if block is valid; otherwise false
*/
bool_t FlashBlock::IsValid()
{
FlashBuffer *flash = reinterpret_cast<FlashBuffer *> (this->flashAddress);
if (flash->magicNumber != this->magicNumber)
return false;
if (flash->crc32 != flash->CRC())
return false;
return true;
}
/**
* Write the contents of the buffer in flash memory.
*
* @param buffer pointer to data buffer
* @param length number of bytes in the data buffer
*/
void FlashBlock::Write(const void *buffer, uint32_t length)
{
FlashBuffer flash;
// Fill in the header information.
flash.magicNumber = this->magicNumber;
flash.length = length;
// Clear the flash data buffer in case our data block isn't as big as required.
std::memset(flash.data, 0xff, sizeof(flash.data));
// Copy the data to the flash buffer.
std::memcpy(flash.data, buffer, length);
// Calculate the CRC-32.
flash.crc32 = flash.CRC();
// Finally erase and write flash.
IAP::Erase (this->flashAddress, FlashBlockSize);
IAP::Write (this->flashAddress, FlashBlockSize, &flash);
}
/**
* Erase the flash block contents.
*/
void FlashBlock::Erase()
{
IAP::Erase (this->flashAddress, FlashBlockSize);
}