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