Kinetis Bootloader Host  2.0.0
Host Tools for Kinetis devices
src/blfwk/StExecutableImage.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(_StExecutableImage_h_)
31 #define _StExecutableImage_h_
32 
33 #include "blfwk/stdafx.h"
34 #include <list>
35 
51 {
52 public:
54  typedef enum
55  {
56  TEXT_REGION,
59 
61  enum
62  {
63  REGION_READ_FLAG = 1,
64  REGION_WRITE_FLAG = 2,
65  REGION_EXEC_FLAG = 4,
66 
68 
71  };
72 
78  struct MemoryRegion
79  {
80  MemoryRegionType m_type;
81  uint32_t m_address;
82  uint32_t m_length;
83  uint8_t *m_data;
84  unsigned m_flags;
85 
87  inline uint32_t endAddress() const { return m_address + m_length - 1; }
89  bool operator==(const MemoryRegion &other) const;
90  };
91 
93  typedef std::list<MemoryRegion> MemoryRegionList;
94 
97  typedef MemoryRegionList::const_iterator const_iterator;
98 
100  typedef enum
101  {
107 
113  struct AddressFilter
114  {
115  AddressFilterAction m_action;
116  uint32_t m_fromAddress;
117  uint32_t m_toAddress;
118  unsigned m_priority;
119 
121  AddressFilter(AddressFilterAction action, uint32_t from, uint32_t to, unsigned priority = 0)
122  : m_action(action)
123  , m_fromAddress(from)
124  , m_toAddress(to)
125  , m_priority(priority)
126  {
127  }
128 
130  bool matchesMemoryRegion(const MemoryRegion &region) const;
131 
133  int compare(const AddressFilter &other) const;
134 
136 
137  inline bool operator<(const AddressFilter &other) const { return compare(other) == -1; }
138  inline bool operator>(const AddressFilter &other) const { return compare(other) == 1; }
139  inline bool operator==(const AddressFilter &other) const { return compare(other) == 0; }
140  inline bool operator<=(const AddressFilter &other) const { return compare(other) != 1; }
141  inline bool operator>=(const AddressFilter &other) const { return compare(other) != -1; }
143  };
144 
146  typedef std::list<AddressFilter> AddressFilterList;
147 
151  {
152  public:
157  address_filter_exception(bool isError, std::string &imageName, const AddressFilter &matchingFilter)
158  : m_isError(isError)
159  , m_imageName(imageName)
160  , m_filter(matchingFilter)
161  {
162  }
163 
166  inline bool isError() const { return m_isError; }
168  inline std::string getImageName() const { return m_imageName; }
170  inline const AddressFilter &getMatchingFilter() const { return m_filter; }
171  protected:
172  bool m_isError;
173  std::string m_imageName;
174  AddressFilter m_filter;
175  };
176 
177 public:
179  StExecutableImage(int inAlignment = 256);
180 
182  StExecutableImage(const StExecutableImage &inOther);
183 
185  virtual ~StExecutableImage();
186 
189 
190  virtual void setName(const std::string &inName);
192 
194  virtual std::string getName() const;
196 
199 
200  virtual void addFillRegion(uint32_t inAddress, unsigned inLength);
202 
204  virtual void addTextRegion(uint32_t inAddress, const uint8_t *inData, unsigned inLength);
205 
210  inline unsigned getRegionCount() const { return static_cast<unsigned>(m_image.size()); }
212  const MemoryRegion &getRegionAtIndex(unsigned inIndex) const;
213 
215  inline const_iterator getRegionBegin() const { return m_image.begin(); }
217  inline const_iterator getRegionEnd() const { return m_image.end(); }
219 
221 
222  inline void setEntryPoint(uint32_t inEntryAddress)
224  {
225  m_entry = inEntryAddress;
226  m_hasEntry = true;
227  }
228 
230  inline bool hasEntryPoint() const { return m_hasEntry; }
232  inline uint32_t getEntryPoint() const { return hasEntryPoint() ? m_entry : 0; }
234 
236 
237  virtual void addAddressFilter(const AddressFilter &filter);
239 
245  template <typename _T>
246  void addAddressFilters(_T from, _T to)
247  {
248  _T it = from;
249  for (; it != to; ++it)
250  {
251  addAddressFilter(*it);
252  }
253  }
254 
256  virtual void clearAddressFilters();
257 
259  virtual void applyAddressFilters();
261 
262 protected:
263  std::string m_name;
264  int m_alignment;
265  bool m_hasEntry;
266  uint32_t m_entry;
267  MemoryRegionList m_image;
268  AddressFilterList m_filters;
269 
271  void cropRegionToFilter(MemoryRegion &region, const AddressFilter &filter);
272 
274  void insertOrMergeRegion(MemoryRegion &inRegion);
275 
277  void mergeRegions(MemoryRegion &inOldRegion, MemoryRegion &inNewRegion);
278 };
279 
280 #endif // _StExecutableImage_h_
MemoryRegionType m_type
Memory region type.
Definition: apps/elftosb/common/StExecutableImage.h:57
const_iterator getRegionBegin() const
Return an iterator to the first region.
Definition: src/blfwk/StExecutableImage.h:215
virtual void addAddressFilter(const AddressFilter &filter)
Add a new address filter.
Definition: apps/elftosb/common/StExecutableImage.cpp:129
virtual void addFillRegion(uint32_t inAddress, unsigned inLength)
Add a region to be filled with zeroes.
Definition: apps/elftosb/common/StExecutableImage.cpp:74
uint32_t m_length
Number of bytes in this region.
Definition: apps/elftosb/common/StExecutableImage.h:59
const_iterator getRegionEnd() const
Return an iterator to the next-after-last region.
Definition: src/blfwk/StExecutableImage.h:217
StExecutableImage(int inAlignment=256)
Constructor.
Definition: apps/elftosb/common/StExecutableImage.cpp:14
unsigned m_flags
Flags for the region.
Definition: apps/elftosb/common/StExecutableImage.h:61
Region is readable.
Definition: apps/elftosb/common/StExecutableImage.h:40
A region containing data or instructions.
Definition: apps/elftosb/common/StExecutableImage.h:33
virtual ~StExecutableImage()
Destructor.
Definition: apps/elftosb/common/StExecutableImage.cpp:46
virtual void clearAddressFilters()
Remove all active filters.
Definition: apps/elftosb/common/StExecutableImage.cpp:136
Definition: apps/elftosb/common/StExecutableImage.h:127
Region to be initialized with zero bytes.
Definition: apps/elftosb/common/StExecutableImage.h:34
virtual void applyAddressFilters()
Process all active filters and perform associated actions.
Definition: apps/elftosb/common/StExecutableImage.cpp:148
int m_alignment
The required address alignment for each memory region.
Definition: apps/elftosb/common/StExecutableImage.h:241
Used to build a representation of memory regions.
Definition: apps/elftosb/common/StExecutableImage.h:27
uint32_t getEntryPoint() const
Returns the entry point address.
Definition: src/blfwk/StExecutableImage.h:232
bool hasEntryPoint() const
Returns true if an entry point has been set.
Definition: src/blfwk/StExecutableImage.h:230
virtual void addTextRegion(uint32_t inAddress, const uint8_t *inData, unsigned inLength)
Add a region containing data to be loaded.
Definition: apps/elftosb/common/StExecutableImage.cpp:93
std::string m_name
The name of the image (can be a file name, for instance).
Definition: apps/elftosb/common/StExecutableImage.h:240
Definition: apps/elftosb/common/StExecutableImage.h:90
const MemoryRegion & getRegionAtIndex(unsigned inIndex) const
Returns a reference to the region specified by inIndex.
Definition: apps/elftosb/common/StExecutableImage.cpp:110
virtual void setName(const std::string &inName)
Sets the image&#39;s name to inName.
Definition: apps/elftosb/common/StExecutableImage.cpp:61
Do nothing.
Definition: apps/elftosb/common/StExecutableImage.h:79
bool m_hasEntry
True if an entry point has been set.
Definition: apps/elftosb/common/StExecutableImage.h:242
void mergeRegions(MemoryRegion &inOldRegion, MemoryRegion &inNewRegion)
Merges two memory regions into one.
Definition: apps/elftosb/common/StExecutableImage.cpp:333
AddressFilter(AddressFilterAction action, uint32_t from, uint32_t to, unsigned priority=0)
Constructor.
Definition: src/blfwk/StExecutableImage.h:121
void addAddressFilters(_T from, _T to)
Add multiple address filters at once.
Definition: src/blfwk/StExecutableImage.h:246
MemoryRegionList m_image
The memory regions.
Definition: apps/elftosb/common/StExecutableImage.h:244
void setEntryPoint(uint32_t inEntryAddress)
Sets the entry point address.
Definition: apps/elftosb/common/StExecutableImage.h:200
AddressFilterList m_filters
List of active address filters.
Definition: apps/elftosb/common/StExecutableImage.h:245
bool operator==(const MemoryRegion &other)
Equality operator.
Definition: apps/elftosb/common/StExecutableImage.cpp:402
uint32_t endAddress() const
Calculates the address of the last byte occupied by this region.
Definition: src/blfwk/StExecutableImage.h:87
uint32_t m_address
The 32-bit start address of this region.
Definition: apps/elftosb/common/StExecutableImage.h:58
Mask to access only permissions flags for a region.
Definition: apps/elftosb/common/StExecutableImage.h:47
void insertOrMergeRegion(MemoryRegion &inRegion)
Inserts the region in sorted order or merges with one already in the image.
Definition: apps/elftosb/common/StExecutableImage.cpp:286
address_filter_exception(bool isError, std::string &imageName, const AddressFilter &matchingFilter)
Constructor.
Definition: src/blfwk/StExecutableImage.h:157
unsigned getRegionCount() const
Returns the total number of regions.
Definition: src/blfwk/StExecutableImage.h:210
MemoryRegionType
Possible types of memory regions.
Definition: apps/elftosb/common/StExecutableImage.h:31
AddressFilterAction
The possible actions for regions matching an address filter range.
Definition: apps/elftosb/common/StExecutableImage.h:77
bool isError() const
Returns true if the exception is an error. Otherwise the exception is for a warning.
Definition: src/blfwk/StExecutableImage.h:166
uint8_t * m_data
Pointer to data. Will be NULL for FILL_REGION type.
Definition: apps/elftosb/common/StExecutableImage.h:60
void cropRegionToFilter(MemoryRegion &region, const AddressFilter &filter)
Deletes the portion region that overlaps filter.
Definition: apps/elftosb/common/StExecutableImage.cpp:205
std::list< MemoryRegion > MemoryRegionList
A list of StExecutableImage::MemoryRegion objects.
Definition: src/blfwk/StExecutableImage.h:93
virtual std::string getName() const
Returns a copy of the image&#39;s name.
Definition: apps/elftosb/common/StExecutableImage.cpp:66
Raise a warning exception.
Definition: apps/elftosb/common/StExecutableImage.h:81
MemoryRegionList::const_iterator const_iterator
Definition: src/blfwk/StExecutableImage.h:97
Don&#39;t include the matching address range in the executable image.
Definition: apps/elftosb/common/StExecutableImage.h:82
Region is writable.
Definition: apps/elftosb/common/StExecutableImage.h:41
Definition: apps/elftosb/common/StExecutableImage.h:55
Region is read-write.
Definition: apps/elftosb/common/StExecutableImage.h:44
Region may contain executable code.
Definition: apps/elftosb/common/StExecutableImage.h:42
std::list< AddressFilter > AddressFilterList
List of StExecutableImage::AddressFilter objects.
Definition: src/blfwk/StExecutableImage.h:146
uint32_t m_entry
Entry point address.
Definition: apps/elftosb/common/StExecutableImage.h:243
Raise an error exception.
Definition: apps/elftosb/common/StExecutableImage.h:80