Hung-Te Lin | 262668f | 2010-05-28 10:32:02 -0700 | [diff] [blame^] | 1 | // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | |
| 6 | #ifndef VBOOT_REFERENCE_GBB_UTILITY_H_ |
| 7 | #define VBOOT_REFERENCE_GBB_UTILITY_H_ |
| 8 | |
| 9 | #include <string> |
| 10 | #include "gbb_header.h" |
| 11 | |
| 12 | namespace vboot_reference { |
| 13 | |
| 14 | class GoogleBinaryBlockUtil { |
| 15 | public: |
| 16 | GoogleBinaryBlockUtil(); |
| 17 | ~GoogleBinaryBlockUtil(); |
| 18 | |
| 19 | // load GBB from a BIOS image file. |
| 20 | // return true if a valid GBB was retrieved. |
| 21 | bool load_from_file(const char *filename); |
| 22 | |
| 23 | // save loaded (and modified) GBB with BIOS image to new file |
| 24 | // return true on success. |
| 25 | bool save_to_file(const char *filename); |
| 26 | |
| 27 | // getters and setters of properties in GBB |
| 28 | bool set_hwid(const char *hwid); // hwid is NUL-terminated. |
| 29 | bool set_rootkey(const std::string &value); |
| 30 | bool set_bmpfv(const std::string &value); |
| 31 | std::string get_hwid() const { return get_property(PROP_HWID); } |
| 32 | std::string get_rootkey() const { return get_property(PROP_ROOTKEY); } |
| 33 | std::string get_bmpfv() const { return get_property(PROP_BMPFV); } |
| 34 | |
| 35 | private: |
| 36 | // enumerate of available data fields |
| 37 | enum PROPINDEX { |
| 38 | PROP_HWID, // hardware id |
| 39 | PROP_ROOTKEY, // root key |
| 40 | PROP_BMPFV, // bitmap FV |
| 41 | }; |
| 42 | |
| 43 | // clear all cached data and initialize to original state |
| 44 | void initialize(); |
| 45 | |
| 46 | // search and count for GBB signatures in loaded image. |
| 47 | // return the number of signatures found. |
| 48 | int search_header_signatures(const std::string &image, long *poffset) const; |
| 49 | |
| 50 | // load and check header structure from image by given offset. |
| 51 | // return true if a valid GBB header is loaded into *phdr. |
| 52 | bool load_gbb_header(const std::string &image, long offset, |
| 53 | GoogleBinaryBlockHeader *phdr) const; |
| 54 | |
| 55 | // retrieve a property from GBB data. |
| 56 | // return the property value. |
| 57 | std::string get_property(PROPINDEX i) const; |
| 58 | |
| 59 | // overwrite a property in GBB data. |
| 60 | // return true on success. |
| 61 | bool set_property(PROPINDEX i, const std::string &value); |
| 62 | |
| 63 | // find the size and offset information for given property |
| 64 | // return true if the offset and size are assign to *poffset and *psize. |
| 65 | bool find_property(PROPINDEX i, uint32_t *poffset, uint32_t *psize) const; |
| 66 | |
| 67 | GoogleBinaryBlockHeader header_; // copy of GBB header from image |
| 68 | std::string file_content_; // complete image file content |
| 69 | long header_offset_; // offset to GBB header in file_content_ |
| 70 | bool is_valid_gbb; // if we are holding a valid GBB |
| 71 | |
| 72 | bool verbose; // provide verbose messages |
| 73 | }; |
| 74 | |
| 75 | } // namespace vboot_reference |
| 76 | |
| 77 | #endif // VBOOT_REFERENCE_GBB_UTILITY_H_ |
| 78 | |