Kinetis Bootloader Host  2.0.0
Host Tools for Kinetis devices
apps/elftosb/common/DataSource.h
1 /*
2  * File: DataSource.h
3  *
4  * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5  * See included license file for license details.
6  */
7 #if !defined(_DataSource_h_)
8 #define _DataSource_h_
9 
10 #include <vector>
11 #include "Value.h"
12 #include "smart_ptr.h"
13 #include "StExecutableImage.h"
14 
15 namespace elftosb
16 {
17 // Forward declaration
18 class DataTarget;
19 
34 {
35 public:
42  class Segment
43  {
44  public:
47  : m_source(source)
48  {
49  }
50 
52  virtual ~Segment() {}
63  virtual unsigned getData(unsigned offset, unsigned maxBytes, uint8_t *buffer) = 0;
64 
66  virtual unsigned getLength() = 0;
67 
69  virtual bool hasNaturalLocation() = 0;
70 
72  virtual uint32_t getBaseAddress() { return 0; }
73  protected:
75  };
76 
87  class PatternSegment : public Segment
88  {
89  public:
91  PatternSegment(DataSource &source);
92 
94  PatternSegment(DataSource &source, const SizedIntegerValue &pattern);
95 
97  PatternSegment(DataSource &source, uint8_t pattern);
98 
100  PatternSegment(DataSource &source, uint16_t pattern);
101 
103  PatternSegment(DataSource &source, uint32_t pattern);
104 
106 
107  virtual bool hasNaturalLocation() { return false; }
110  virtual unsigned getData(unsigned offset, unsigned maxBytes, uint8_t *buffer);
111 
113  virtual unsigned getLength();
115 
117 
118  inline void setPattern(const SizedIntegerValue &newPattern) { m_pattern = newPattern; }
121  inline SizedIntegerValue &getPattern() { return m_pattern; }
124  {
125  m_pattern = value;
126  return *this;
127  }
129 
130  protected:
132  };
133 
134 public:
137  : m_target(0)
138  {
139  }
140 
142  virtual ~DataSource() {}
144 
145  inline void setTarget(DataTarget *target) { m_target = target; }
148  inline DataTarget *getTarget() const { return m_target; }
150 
152 
153  virtual unsigned getSegmentCount() = 0;
155 
157  virtual Segment *getSegmentAt(unsigned index) = 0;
159 
160 protected:
162 };
163 
174 {
175 public:
177  PatternSource();
178 
180  PatternSource(const SizedIntegerValue &value);
181 
183  virtual unsigned getSegmentCount() { return 1; }
185  virtual DataSource::Segment *getSegmentAt(unsigned index) { return this; }
188  {
189  setPattern(value);
190  return *this;
191  }
192 };
193 
201 {
202 public:
205 
207  UnmappedDataSource(const uint8_t *data, unsigned length);
208 
210  void setData(const uint8_t *data, unsigned length);
211 
213  virtual unsigned getSegmentCount() { return 1; }
215  virtual DataSource::Segment *getSegmentAt(unsigned index) { return this; }
217 
218  virtual bool hasNaturalLocation() { return false; }
221  virtual unsigned getData(unsigned offset, unsigned maxBytes, uint8_t *buffer);
222 
224  virtual unsigned getLength() { return m_length; }
226 
227 protected:
229  unsigned m_length;
230 };
231 
238 {
239 public:
242 
244  virtual ~MemoryImageDataSource();
245 
247  virtual unsigned getSegmentCount();
248 
250  virtual DataSource::Segment *getSegmentAt(unsigned index);
251 
252 protected:
257  {
258  public:
260  TextSegment(MemoryImageDataSource &source, StExecutableImage *image, unsigned index);
261 
262  virtual unsigned getData(unsigned offset, unsigned maxBytes, uint8_t *buffer);
263  virtual unsigned getLength();
264 
265  virtual bool hasNaturalLocation() { return true; }
266  virtual uint32_t getBaseAddress();
267 
268  protected:
270  unsigned m_index;
271  };
272 
277  {
278  public:
279  FillSegment(MemoryImageDataSource &source, StExecutableImage *image, unsigned index);
280 
281  virtual unsigned getLength();
282 
283  virtual bool hasNaturalLocation() { return true; }
284  virtual uint32_t getBaseAddress();
285 
286  protected:
288  unsigned m_index;
289  };
290 
291 protected:
293 
294  typedef std::vector<DataSource::Segment *> segment_array_t;
295  segment_array_t m_segments;
296 };
297 
298 }; // namespace elftosb
299 
300 #endif // _DataSource_h_
Abstract base class for data sources.
Definition: apps/elftosb/common/DataSource.h:33
DataSource()
Default constructor.
Definition: apps/elftosb/common/DataSource.h:136
virtual ~DataSource()
Destructor.
Definition: apps/elftosb/common/DataSource.h:142
Data source for data that is not memory mapped (has no natural address).
Definition: apps/elftosb/common/DataSource.h:200
Segment corresponding to a text region of the executable image.
Definition: apps/elftosb/common/DataSource.h:256
SizedIntegerValue m_pattern
The fill pattern.
Definition: apps/elftosb/common/DataSource.h:131
virtual DataSource::Segment * getSegmentAt(unsigned index)
Returns this object, as it is its own segment.
Definition: apps/elftosb/common/DataSource.h:185
virtual DataSource::Segment * getSegmentAt(unsigned index)
Returns this object, as it is its own segment.
Definition: apps/elftosb/common/DataSource.h:215
SizedIntegerValue & getPattern()
Return the fill pattern for the segment.
Definition: apps/elftosb/common/DataSource.h:121
virtual uint32_t getBaseAddress()
Returns the address associated with the segment.
Definition: apps/elftosb/common/DataSource.h:72
DataTarget * m_target
Corresponding target for this source.
Definition: apps/elftosb/common/DataSource.h:161
segment_array_t m_segments
The array of Segment instances.
Definition: apps/elftosb/common/DataSource.h:295
StExecutableImage * m_image
Coalesced image of the file.
Definition: apps/elftosb/common/DataSource.h:287
Used to build a representation of memory regions.
Definition: apps/elftosb/common/StExecutableImage.h:27
PatternSource & operator=(const SizedIntegerValue &value)
Assignment operator, sets the pattern value and length.
Definition: apps/elftosb/common/DataSource.h:187
Data source that takes its data from an executable image.
Definition: apps/elftosb/common/DataSource.h:237
Adds a word size attribute to IntegerValue.
Definition: apps/elftosb/common/Value.h:72
virtual unsigned getData(unsigned offset, unsigned maxBytes, uint8_t *buffer)=0
Gets all or a portion of the segment&#39;s data.
virtual bool hasNaturalLocation()
Returns whether the segment has an associated address.
Definition: apps/elftosb/common/DataSource.h:265
Definition: BootImage.h:13
Data source for a repeating pattern.
Definition: apps/elftosb/common/DataSource.h:173
unsigned m_index
Record index.
Definition: apps/elftosb/common/DataSource.h:270
Abstract base class for the target address or range of data.
Definition: apps/elftosb/common/DataTarget.h:34
This is a special type of segment containing a repeating pattern.
Definition: apps/elftosb/common/DataSource.h:87
std::vector< DataSource::Segment * > segment_array_t
An array of segments.
Definition: apps/elftosb/common/DataSource.h:294
virtual unsigned getLength()=0
Gets the length of the segment&#39;s data.
StExecutableImage * m_image
Coalesced image of the file.
Definition: apps/elftosb/common/DataSource.h:269
Segment corresponding to a fill region of the executable image.
Definition: apps/elftosb/common/DataSource.h:276
smart_array_ptr< uint8_t > m_data
The data.
Definition: apps/elftosb/common/DataSource.h:228
virtual unsigned getLength()
Returns the number of bytes of data managed by the source.
Definition: apps/elftosb/common/DataSource.h:224
virtual bool hasNaturalLocation()=0
Returns whether the segment has an associated address.
StExecutableImage * m_image
The memory image that is the data source.
Definition: apps/elftosb/common/DataSource.h:292
void setTarget(DataTarget *target)
Sets the associated data target.
Definition: apps/elftosb/common/DataSource.h:146
Discrete, contiguous part of the source&#39;s data.
Definition: apps/elftosb/common/DataSource.h:42
virtual unsigned getSegmentCount()
There is only one segment.
Definition: apps/elftosb/common/DataSource.h:183
DataSource & m_source
The data source to which this segment belongs.
Definition: apps/elftosb/common/DataSource.h:74
Segment(DataSource &source)
Default constructor.
Definition: apps/elftosb/common/DataSource.h:46
DataTarget * getTarget() const
Gets the associated data target.
Definition: apps/elftosb/common/DataSource.h:148
virtual bool hasNaturalLocation()
Pattern segments have no natural address.
Definition: apps/elftosb/common/DataSource.h:283
unsigned m_index
Record index.
Definition: apps/elftosb/common/DataSource.h:288
virtual Segment * getSegmentAt(unsigned index)=0
Returns segment number index of the data source.
PatternSegment & operator=(const SizedIntegerValue &value)
Assignment operator, sets the pattern value and length.
Definition: apps/elftosb/common/DataSource.h:123
virtual ~Segment()
Destructor.
Definition: apps/elftosb/common/DataSource.h:52
virtual unsigned getSegmentCount()=0
Returns the number of segments in this data source.
virtual unsigned getSegmentCount()
There is only one segment.
Definition: apps/elftosb/common/DataSource.h:213
unsigned m_length
Byte count of the data.
Definition: apps/elftosb/common/DataSource.h:229