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> |
Hung-Te Lin | a59a029 | 2011-01-10 13:37:17 +0800 | [diff] [blame^] | 10 | #include <vector> |
Hung-Te Lin | 262668f | 2010-05-28 10:32:02 -0700 | [diff] [blame] | 11 | #include "gbb_header.h" |
| 12 | |
| 13 | namespace vboot_reference { |
| 14 | |
| 15 | class GoogleBinaryBlockUtil { |
| 16 | public: |
Hung-Te Lin | 21ef1a3 | 2010-06-02 18:13:47 -0700 | [diff] [blame] | 17 | // enumerate of available data fields |
| 18 | enum PROPINDEX { |
| 19 | PROP_HWID, // hardware id |
| 20 | PROP_ROOTKEY, // root key |
| 21 | PROP_BMPFV, // bitmap FV |
Hung-Te Lin | 08dc5f3 | 2010-06-03 14:35:47 -0700 | [diff] [blame] | 22 | PROP_RCVKEY, // recovery key |
Hung-Te Lin | 21ef1a3 | 2010-06-02 18:13:47 -0700 | [diff] [blame] | 23 | PROP_RANGE, // indicator of valid property range |
| 24 | }; |
| 25 | |
Hung-Te Lin | 262668f | 2010-05-28 10:32:02 -0700 | [diff] [blame] | 26 | GoogleBinaryBlockUtil(); |
| 27 | ~GoogleBinaryBlockUtil(); |
| 28 | |
| 29 | // load GBB from a BIOS image file. |
| 30 | // return true if a valid GBB was retrieved. |
| 31 | bool load_from_file(const char *filename); |
| 32 | |
| 33 | // save loaded (and modified) GBB with BIOS image to new file |
| 34 | // return true on success. |
| 35 | bool save_to_file(const char *filename); |
| 36 | |
Hung-Te Lin | a59a029 | 2011-01-10 13:37:17 +0800 | [diff] [blame^] | 37 | // create a new GBB blob by providing a list of reserved data size for each |
| 38 | // properties, following the order described in GoogleBinaryBlockHeader. |
| 39 | // return true on success. |
| 40 | bool create_new(const std::vector<uint32_t> &create_param); |
| 41 | |
Hung-Te Lin | 21ef1a3 | 2010-06-02 18:13:47 -0700 | [diff] [blame] | 42 | // retrieve the value of a property from GBB data. |
| 43 | // return the property value. |
| 44 | std::string get_property(PROPINDEX i) const; |
| 45 | |
| 46 | // overwrite a property in GBB data. |
| 47 | // return true on success. |
| 48 | bool set_property(PROPINDEX i, const std::string &value); |
| 49 | |
| 50 | // get a readable name by a property index. |
| 51 | // return the name for valid properties, otherwise unexpected empty string. |
| 52 | std::string get_property_name(PROPINDEX i) const; |
| 53 | |
| 54 | // quick getters and setters of known properties in GBB |
| 55 | bool set_hwid(const char *hwid); // NOTE: hwid is NUL-terminated. |
Hung-Te Lin | 262668f | 2010-05-28 10:32:02 -0700 | [diff] [blame] | 56 | bool set_rootkey(const std::string &value); |
| 57 | bool set_bmpfv(const std::string &value); |
Hung-Te Lin | 08dc5f3 | 2010-06-03 14:35:47 -0700 | [diff] [blame] | 58 | bool set_recovery_key(const std::string &value); |
Hung-Te Lin | 21ef1a3 | 2010-06-02 18:13:47 -0700 | [diff] [blame] | 59 | std::string get_hwid() const { return get_property(PROP_HWID); } |
Hung-Te Lin | 262668f | 2010-05-28 10:32:02 -0700 | [diff] [blame] | 60 | std::string get_rootkey() const { return get_property(PROP_ROOTKEY); } |
Hung-Te Lin | 21ef1a3 | 2010-06-02 18:13:47 -0700 | [diff] [blame] | 61 | std::string get_bmpfv() const { return get_property(PROP_BMPFV); } |
Hung-Te Lin | 08dc5f3 | 2010-06-03 14:35:47 -0700 | [diff] [blame] | 62 | std::string get_recovery_key() const { return get_property(PROP_RCVKEY); } |
Hung-Te Lin | 262668f | 2010-05-28 10:32:02 -0700 | [diff] [blame] | 63 | |
| 64 | private: |
Hung-Te Lin | 262668f | 2010-05-28 10:32:02 -0700 | [diff] [blame] | 65 | // clear all cached data and initialize to original state |
| 66 | void initialize(); |
| 67 | |
| 68 | // search and count for GBB signatures in loaded image. |
| 69 | // return the number of signatures found. |
| 70 | int search_header_signatures(const std::string &image, long *poffset) const; |
| 71 | |
| 72 | // load and check header structure from image by given offset. |
| 73 | // return true if a valid GBB header is loaded into *phdr. |
| 74 | bool load_gbb_header(const std::string &image, long offset, |
| 75 | GoogleBinaryBlockHeader *phdr) const; |
| 76 | |
Hung-Te Lin | 21ef1a3 | 2010-06-02 18:13:47 -0700 | [diff] [blame] | 77 | // find the size, offset, and name information for given property. |
| 78 | // return true if the offset and size are assign to *poffset and *psize; |
| 79 | // if pname is not NULL, *pname will hold a pointer to a readable name. |
| 80 | // return false if the property index is invalid. |
| 81 | bool find_property(PROPINDEX i, uint32_t *poffset, uint32_t *psize, |
| 82 | const char **pname) const; |
Hung-Te Lin | 262668f | 2010-05-28 10:32:02 -0700 | [diff] [blame] | 83 | |
| 84 | GoogleBinaryBlockHeader header_; // copy of GBB header from image |
| 85 | std::string file_content_; // complete image file content |
| 86 | long header_offset_; // offset to GBB header in file_content_ |
| 87 | bool is_valid_gbb; // if we are holding a valid GBB |
| 88 | |
| 89 | bool verbose; // provide verbose messages |
| 90 | }; |
| 91 | |
| 92 | } // namespace vboot_reference |
| 93 | |
| 94 | #endif // VBOOT_REFERENCE_GBB_UTILITY_H_ |