Kinetis Bootloader Host  2.0.0
Host Tools for Kinetis devices
EncoreBootImageReader.h
1 /*
2  * File: EncoreBootImageReader.h
3  *
4  * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5  * See included license file for license details.
6  */
7 #if !defined(_EncoreBootImageReader_h_)
8 #define _EncoreBootImageReader_h_
9 
10 #include "EncoreBootImage.h"
11 
12 namespace elftosb
13 {
18 {
19 public:
23  class read_error : public std::runtime_error
24  {
25  public:
27  read_error(const std::string &msg)
28  : std::runtime_error(msg)
29  {
30  }
31  };
32 
34  typedef std::vector<EncoreBootImage::section_header_t> section_array_t;
35 
37  typedef std::vector<EncoreBootImage::boot_command_t> boot_tag_array_t;
38 
39 public:
41  EncoreBootImageReader(std::istream &stream)
42  : m_stream(stream)
43  {
44  }
45 
51 
52  inline void setKey(const AESKey<128> &key) { m_dek = key; }
53  inline const AESKey<128> &getKey() const { return m_dek; }
55 
59 
60  void readImageHeader();
62 
64  void computeHeaderDigest(sha1_digest_t &digest);
65 
67  void readImageDigest();
68 
70  void computeImageDigest(sha1_digest_t &digest);
71 
73  void readSectionTable();
74 
76  bool readKeyDictionary(const AESKey<128> &kek);
77 
79  void readBootTags();
80 
82  EncoreBootImage::Section *readSection(unsigned index);
84 
88 
89  inline bool isEncrypted() const { return m_header.m_keyCount > 0; }
95  const sha1_digest_t &getDigest() const { return m_digest; }
97  inline const section_array_t &getSections() const { return m_sections; }
99  inline const boot_tag_array_t &getBootTags() const { return m_bootTags; }
101 
102 protected:
103  std::istream &m_stream;
106  sha1_digest_t m_digest;
107  section_array_t m_sections;
108  boot_tag_array_t m_bootTags;
109 
110 protected:
113 };
114 
115 }; // namespace elftosb
116 
117 #endif // _EncoreBootImageReader_h_
void readImageHeader()
Reads the header from the image.
Definition: EncoreBootImageReader.cpp:20
section_array_t m_sections
The section table.
Definition: EncoreBootImageReader.h:107
Base class for a section of an Encore boot image.
Definition: EncoreBootImage.h:989
void computeImageDigest(sha1_digest_t &digest)
Run a SHA-1 digest over the entire image.
Definition: EncoreBootImageReader.cpp:107
STL namespace.
Reads a Piano/Encore boot image from an input stream.
Definition: EncoreBootImageReader.h:17
std::vector< EncoreBootImage::section_header_t > section_array_t
An array of section headers.
Definition: EncoreBootImageReader.h:34
Exception class used for error found while reading a boot image.
Definition: EncoreBootImageReader.h:23
std::istream & m_stream
The input stream to read the image from.
Definition: EncoreBootImageReader.h:103
virtual ~EncoreBootImageReader()
Destructor.
Definition: EncoreBootImageReader.h:47
const sha1_digest_t & getDigest() const
Returns a reference to the SHA-1 digest read from the image.
Definition: EncoreBootImageReader.h:95
bool isEncrypted() const
Returns whether the image is encrypted or not.
Definition: EncoreBootImageReader.h:91
EncoreBootImage::Section * readSection(unsigned index)
Definition: EncoreBootImageReader.cpp:312
EncoreBootImage::boot_image_header_t m_header
Header from the boot image.
Definition: EncoreBootImageReader.h:105
bool readKeyDictionary(const AESKey< 128 > &kek)
Reads the key dictionary, if the image is encrypted.
Definition: EncoreBootImageReader.cpp:169
void computeHeaderDigest(sha1_digest_t &digest)
Computes the actual SHA-1 digest of the image header.
Definition: EncoreBootImageReader.cpp:67
void readImageDigest()
Reads the digest at the end of the image.
Definition: EncoreBootImageReader.cpp:79
void readBootTags()
Definition: EncoreBootImageReader.cpp:237
Definition: BootImage.h:13
boot_tag_array_t m_bootTags
The array of boot tags read from the image.
Definition: EncoreBootImageReader.h:108
sha1_digest_t m_digest
SHA-1 digest as read from the image.
Definition: EncoreBootImageReader.h:106
Structure for a Piano bootloader command.
Definition: EncoreBootImage.h:249
const boot_tag_array_t & getBootTags() const
Returns a reference to the STL container holding the boot tags.
Definition: EncoreBootImageReader.h:99
const section_array_t & getSections() const
Returns a reference to the STL container holding the section headers.
Definition: EncoreBootImageReader.h:97
uint16_t m_keyCount
Number of entries in DEK dictionary.
Definition: EncoreBootImage.h:168
void readSectionTable()
Read the plaintext section table entries.
Definition: EncoreBootImageReader.cpp:131
std::vector< EncoreBootImage::boot_command_t > boot_tag_array_t
An array of boot tags.
Definition: EncoreBootImageReader.h:37
uint8_t calculateCommandChecksum(EncoreBootImage::boot_command_t &header)
Calculates the 8-bit checksum on a boot command header.
Definition: EncoreBootImageReader.cpp:291
const EncoreBootImage::boot_image_header_t & getHeader() const
Returns a reference to the image&#39;s header.
Definition: EncoreBootImageReader.h:93
AESKey< 128 > m_dek
DEK (data encryption key) read from the key dictionary.
Definition: EncoreBootImageReader.h:104
Header for the entire boot image.
Definition: EncoreBootImage.h:150
EncoreBootImageReader(std::istream &stream)
Default constructor.
Definition: EncoreBootImageReader.h:41
read_error(const std::string &msg)
Constructor.
Definition: EncoreBootImageReader.h:27