blob: 021eb80fcd8597d1c87d957aa041f3c78752b217 [file] [log] [blame]
Hung-Te Lin262668f2010-05-28 10:32:02 -07001// 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
12namespace vboot_reference {
13
14class GoogleBinaryBlockUtil {
15 public:
Hung-Te Lin21ef1a32010-06-02 18:13:47 -070016 // enumerate of available data fields
17 enum PROPINDEX {
18 PROP_HWID, // hardware id
19 PROP_ROOTKEY, // root key
20 PROP_BMPFV, // bitmap FV
21 PROP_RANGE, // indicator of valid property range
22 };
23
Hung-Te Lin262668f2010-05-28 10:32:02 -070024 GoogleBinaryBlockUtil();
25 ~GoogleBinaryBlockUtil();
26
27 // load GBB from a BIOS image file.
28 // return true if a valid GBB was retrieved.
29 bool load_from_file(const char *filename);
30
31 // save loaded (and modified) GBB with BIOS image to new file
32 // return true on success.
33 bool save_to_file(const char *filename);
34
Hung-Te Lin21ef1a32010-06-02 18:13:47 -070035 // retrieve the value of a property from GBB data.
36 // return the property value.
37 std::string get_property(PROPINDEX i) const;
38
39 // overwrite a property in GBB data.
40 // return true on success.
41 bool set_property(PROPINDEX i, const std::string &value);
42
43 // get a readable name by a property index.
44 // return the name for valid properties, otherwise unexpected empty string.
45 std::string get_property_name(PROPINDEX i) const;
46
47 // quick getters and setters of known properties in GBB
48 bool set_hwid(const char *hwid); // NOTE: hwid is NUL-terminated.
Hung-Te Lin262668f2010-05-28 10:32:02 -070049 bool set_rootkey(const std::string &value);
50 bool set_bmpfv(const std::string &value);
Hung-Te Lin21ef1a32010-06-02 18:13:47 -070051 std::string get_hwid() const { return get_property(PROP_HWID); }
Hung-Te Lin262668f2010-05-28 10:32:02 -070052 std::string get_rootkey() const { return get_property(PROP_ROOTKEY); }
Hung-Te Lin21ef1a32010-06-02 18:13:47 -070053 std::string get_bmpfv() const { return get_property(PROP_BMPFV); }
Hung-Te Lin262668f2010-05-28 10:32:02 -070054
55 private:
Hung-Te Lin262668f2010-05-28 10:32:02 -070056 // clear all cached data and initialize to original state
57 void initialize();
58
59 // search and count for GBB signatures in loaded image.
60 // return the number of signatures found.
61 int search_header_signatures(const std::string &image, long *poffset) const;
62
63 // load and check header structure from image by given offset.
64 // return true if a valid GBB header is loaded into *phdr.
65 bool load_gbb_header(const std::string &image, long offset,
66 GoogleBinaryBlockHeader *phdr) const;
67
Hung-Te Lin21ef1a32010-06-02 18:13:47 -070068 // find the size, offset, and name information for given property.
69 // return true if the offset and size are assign to *poffset and *psize;
70 // if pname is not NULL, *pname will hold a pointer to a readable name.
71 // return false if the property index is invalid.
72 bool find_property(PROPINDEX i, uint32_t *poffset, uint32_t *psize,
73 const char **pname) const;
Hung-Te Lin262668f2010-05-28 10:32:02 -070074
75 GoogleBinaryBlockHeader header_; // copy of GBB header from image
76 std::string file_content_; // complete image file content
77 long header_offset_; // offset to GBB header in file_content_
78 bool is_valid_gbb; // if we are holding a valid GBB
79
80 bool verbose; // provide verbose messages
81};
82
83} // namespace vboot_reference
84
85#endif // VBOOT_REFERENCE_GBB_UTILITY_H_
86