Kinetis Bootloader Host  2.0.0
Host Tools for Kinetis devices
src/blfwk/DataSource.h
1 /*
2  * Copyright (c) 2013-14, Freescale Semiconductor, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  * of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  * list of conditions and the following disclaimer in the documentation and/or
13  * other materials provided with the distribution.
14  *
15  * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from this
17  * software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #if !defined(_DataSource_h_)
31 #define _DataSource_h_
32 
33 #include <vector>
34 #include "Value.h"
35 #include "smart_ptr.h"
36 #include "StExecutableImage.h"
37 
38 namespace blfwk
39 {
40 // Forward declaration
41 class DataTarget;
42 
57 {
58 public:
65  class Segment
66  {
67  public:
70  : m_source(source)
71  {
72  }
73 
75  virtual ~Segment() {}
86  virtual unsigned getData(unsigned offset, unsigned maxBytes, uint8_t *buffer) = 0;
87 
89  virtual unsigned getLength() = 0;
90 
92  virtual bool hasNaturalLocation() = 0;
93 
95  virtual uint32_t getBaseAddress() { return 0; }
96  protected:
98  };
99 
110  class PatternSegment : public Segment
111  {
112  public:
114  PatternSegment(DataSource &source);
115 
117  PatternSegment(DataSource &source, const SizedIntegerValue &pattern);
118 
120  PatternSegment(DataSource &source, uint8_t pattern);
121 
123  PatternSegment(DataSource &source, uint16_t pattern);
124 
126  PatternSegment(DataSource &source, uint32_t pattern);
127 
129 
130  virtual bool hasNaturalLocation() { return false; }
133  virtual unsigned getData(unsigned offset, unsigned maxBytes, uint8_t *buffer);
134 
136  virtual unsigned getLength();
138 
140 
141  inline void setPattern(const SizedIntegerValue &newPattern) { m_pattern = newPattern; }
144  inline SizedIntegerValue &getPattern() { return m_pattern; }
147  {
148  m_pattern = value;
149  return *this;
150  }
152 
153  protected:
155  };
156 
157 public:
160  : m_target(0)
161  {
162  }
163 
165  virtual ~DataSource() {}
167 
168  inline void setTarget(DataTarget *target) { m_target = target; }
171  inline DataTarget *getTarget() const { return m_target; }
173 
175 
176  virtual unsigned getSegmentCount() = 0;
178 
180  virtual Segment *getSegmentAt(unsigned index) = 0;
182 
183 protected:
185 };
186 
197 {
198 public:
200  PatternSource();
201 
203  PatternSource(const SizedIntegerValue &value);
204 
206  virtual unsigned getSegmentCount() { return 1; }
208  virtual DataSource::Segment *getSegmentAt(unsigned index) { return this; }
211  {
212  setPattern(value);
213  return *this;
214  }
215 };
216 
224 {
225 public:
228 
230  UnmappedDataSource(const uint8_t *data, unsigned length);
231 
233  void setData(const uint8_t *data, unsigned length);
234 
236  virtual unsigned getSegmentCount() { return 1; }
238  virtual DataSource::Segment *getSegmentAt(unsigned index) { return this; }
240 
241  virtual bool hasNaturalLocation() { return false; }
244  virtual unsigned getData(unsigned offset, unsigned maxBytes, uint8_t *buffer);
245 
247  virtual unsigned getLength() { return m_length; }
249  virtual uint32_t getBaseAddress();
250 
252 
253 protected:
255  unsigned m_length;
256 };
257 
264 {
265 public:
268 
270  virtual ~MemoryImageDataSource();
271 
273  virtual unsigned getSegmentCount();
274 
276  virtual DataSource::Segment *getSegmentAt(unsigned index);
277 
278 protected:
283  {
284  public:
286  TextSegment(MemoryImageDataSource &source, StExecutableImage *image, unsigned index);
287 
288  virtual unsigned getData(unsigned offset, unsigned maxBytes, uint8_t *buffer);
289  virtual unsigned getLength();
290 
291  virtual bool hasNaturalLocation() { return true; }
292  virtual uint32_t getBaseAddress();
293 
294  protected:
296  unsigned m_index;
297  };
298 
303  {
304  public:
305  FillSegment(MemoryImageDataSource &source, StExecutableImage *image, unsigned index);
306 
307  virtual unsigned getLength();
308 
309  virtual bool hasNaturalLocation() { return true; }
310  virtual uint32_t getBaseAddress();
311 
312  protected:
314  unsigned m_index;
315  };
316 
317 protected:
319 
320  typedef std::vector<DataSource::Segment *> segment_array_t;
321  segment_array_t m_segments;
322 };
323 
324 }; // namespace blfwk
325 
326 #endif // _DataSource_h_
DataTarget * getTarget() const
Gets the associated data target.
Definition: src/blfwk/DataSource.h:171
unsigned m_index
Record index.
Definition: src/blfwk/DataSource.h:314
virtual unsigned getLength()
Returns the number of bytes of data managed by the source.
Definition: src/blfwk/DataSource.h:247
virtual unsigned getData(unsigned offset, unsigned maxBytes, uint8_t *buffer)=0
Gets all or a portion of the segment&#39;s data.
Definition: BlfwkErrors.h:16
Data source for a repeating pattern.
Definition: src/blfwk/DataSource.h:196
segment_array_t m_segments
The array of Segment instances.
Definition: src/blfwk/DataSource.h:321
virtual ~Segment()
Destructor.
Definition: src/blfwk/DataSource.h:75
void setTarget(DataTarget *target)
Sets the associated data target.
Definition: src/blfwk/DataSource.h:169
virtual unsigned getSegmentCount()=0
Returns the number of segments in this data source.
unsigned m_length
Byte count of the data.
Definition: src/blfwk/DataSource.h:255
virtual uint32_t getBaseAddress()
Returns the address associated with the segment.
Definition: src/blfwk/DataSource.h:95
DataTarget * m_target
Corresponding target for this source.
Definition: src/blfwk/DataSource.h:184
PatternSource & operator=(const SizedIntegerValue &value)
Assignment operator, sets the pattern value and length.
Definition: src/blfwk/DataSource.h:210
PatternSegment & operator=(const SizedIntegerValue &value)
Assignment operator, sets the pattern value and length.
Definition: src/blfwk/DataSource.h:146
StExecutableImage * m_image
Coalesced image of the file.
Definition: src/blfwk/DataSource.h:295
unsigned m_index
Record index.
Definition: src/blfwk/DataSource.h:296
Abstract base class for the target address or range of data.
Definition: src/blfwk/DataTarget.h:57
Used to build a representation of memory regions.
Definition: apps/elftosb/common/StExecutableImage.h:27
virtual Segment * getSegmentAt(unsigned index)=0
Returns segment number index of the data source.
virtual bool hasNaturalLocation()
Returns whether the segment has an associated address.
Definition: src/blfwk/DataSource.h:291
SizedIntegerValue m_pattern
The fill pattern.
Definition: src/blfwk/DataSource.h:154
virtual bool hasNaturalLocation()=0
Returns whether the segment has an associated address.
Adds a word size attribute to IntegerValue.
Definition: src/blfwk/Value.h:95
Segment(DataSource &source)
Default constructor.
Definition: src/blfwk/DataSource.h:69
Segment corresponding to a text region of the executable image.
Definition: src/blfwk/DataSource.h:282
StExecutableImage * m_image
The memory image that is the data source.
Definition: src/blfwk/DataSource.h:318
Data source for data that is not memory mapped (has no natural address).
Definition: src/blfwk/DataSource.h:223
virtual unsigned getSegmentCount()
There is only one segment.
Definition: src/blfwk/DataSource.h:206
DataSource()
Default constructor.
Definition: src/blfwk/DataSource.h:159
virtual DataSource::Segment * getSegmentAt(unsigned index)
Returns this object, as it is its own segment.
Definition: src/blfwk/DataSource.h:208
Segment corresponding to a fill region of the executable image.
Definition: src/blfwk/DataSource.h:302
Discrete, contiguous part of the source&#39;s data.
Definition: src/blfwk/DataSource.h:65
std::vector< DataSource::Segment * > segment_array_t
An array of segments.
Definition: src/blfwk/DataSource.h:320
virtual unsigned getSegmentCount()
There is only one segment.
Definition: src/blfwk/DataSource.h:236
Data source that takes its data from an executable image.
Definition: src/blfwk/DataSource.h:263
StExecutableImage * m_image
Coalesced image of the file.
Definition: src/blfwk/DataSource.h:313
SizedIntegerValue & getPattern()
Return the fill pattern for the segment.
Definition: src/blfwk/DataSource.h:144
DataSource & m_source
The data source to which this segment belongs.
Definition: src/blfwk/DataSource.h:97
virtual ~DataSource()
Destructor.
Definition: src/blfwk/DataSource.h:165
virtual DataSource::Segment * getSegmentAt(unsigned index)
Returns this object, as it is its own segment.
Definition: src/blfwk/DataSource.h:238
Abstract base class for data sources.
Definition: src/blfwk/DataSource.h:56
virtual bool hasNaturalLocation()
Pattern segments have no natural address.
Definition: src/blfwk/DataSource.h:309
smart_array_ptr< uint8_t > m_data
The data.
Definition: src/blfwk/DataSource.h:254
virtual unsigned getLength()=0
Gets the length of the segment&#39;s data.
This is a special type of segment containing a repeating pattern.
Definition: src/blfwk/DataSource.h:110