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