2 * File: EncoreBootImageReader.h
4 * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5 * See included license file for license details.
7 #if !defined(_EncoreBootImageReader_h_)
8 #define _EncoreBootImageReader_h_
10 #include "EncoreBootImage.h"
16 * \brief Reads a Piano/Encore boot image from an input stream.
18 class EncoreBootImageReader
22 * \brief Exception class used for error found while reading a boot image.
24 class read_error : public std::runtime_error
27 //! \brief Constructor.
28 read_error(const std::string & msg) : std::runtime_error(msg) {}
31 //! \brief An array of section headers.
32 typedef std::vector<EncoreBootImage::section_header_t> section_array_t;
34 //! \brief An array of boot tags.
35 typedef std::vector<EncoreBootImage::boot_command_t> boot_tag_array_t;
38 //! \brief Default constructor.
39 EncoreBootImageReader(std::istream & stream) : m_stream(stream) {}
41 //! \brief Destructor.
42 virtual ~EncoreBootImageReader() {}
44 //! \name Decryption key
45 //! These methods provide access to the Data Encryption Key (DEK). Normally
46 //! the DEK is discovered using the readKeyDictionary() method.
48 inline void setKey(const AESKey<128> & key) { m_dek = key; }
49 inline const AESKey<128> & getKey() const { return m_dek; }
53 //! This group of methods is responsible for reading and parsing different
54 //! pieces and parts of the boot image file.
56 //! \brief Reads the header from the image.
57 void readImageHeader();
59 //! \brief Computes the actual SHA-1 digest of the image header.
60 void computeHeaderDigest(sha1_digest_t & digest);
62 //! \brief Reads the digest at the end of the image.
63 void readImageDigest();
65 //! \brief Run a SHA-1 digest over the entire image.
66 void computeImageDigest(sha1_digest_t & digest);
68 //! \brief Read the plaintext section table entries.
69 void readSectionTable();
71 //! \brief Reads the key dictionary, if the image is encrypted.
72 bool readKeyDictionary(const AESKey<128> & kek);
78 EncoreBootImage::Section * readSection(unsigned index);
82 //! Information retrieved with reader methods is accessible through
85 //! \brief Returns whether the image is encrypted or not.
86 //! \pre The header must have been read already.
87 inline bool isEncrypted() const { return m_header.m_keyCount > 0; }
89 //! \brief Returns a reference to the image's header.
90 const EncoreBootImage::boot_image_header_t & getHeader() const { return m_header; }
92 //! \brief Returns a reference to the SHA-1 digest read from the image.
93 const sha1_digest_t & getDigest() const { return m_digest; }
95 //! \brief Returns a reference to the STL container holding the section headers.
96 inline const section_array_t & getSections() const { return m_sections; }
98 //! \brief Returns a reference to the STL container holding the boot tags.
99 inline const boot_tag_array_t & getBootTags() const { return m_bootTags; }
103 std::istream & m_stream; //!< The input stream to read the image from.
104 AESKey<128> m_dek; //!< DEK (data encryption key) read from the key dictionary.
105 EncoreBootImage::boot_image_header_t m_header; //!< Header from the boot image.
106 sha1_digest_t m_digest; //!< SHA-1 digest as read from the image.
107 section_array_t m_sections; //!< The section table.
108 boot_tag_array_t m_bootTags; //!< The array of boot tags read from the image.
111 //! \brief Calculates the 8-bit checksum on a boot command header.
112 uint8_t calculateCommandChecksum(EncoreBootImage::boot_command_t & header);
115 }; // namespace elftosb
117 #endif // _EncoreBootImageReader_h_