blob: 468cddf99b999aef17af6d9d1c984f6a3b6bbee2 [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
Hung-Te Lin08dc5f32010-06-03 14:35:47 -070021 PROP_RCVKEY, // recovery key
Hung-Te Lin21ef1a32010-06-02 18:13:47 -070022 PROP_RANGE, // indicator of valid property range
23 };
24
Hung-Te Lin262668f2010-05-28 10:32:02 -070025 GoogleBinaryBlockUtil();
26 ~GoogleBinaryBlockUtil();
27
28 // load GBB from a BIOS image file.
29 // return true if a valid GBB was retrieved.
30 bool load_from_file(const char *filename);
31
32 // save loaded (and modified) GBB with BIOS image to new file
33 // return true on success.
34 bool save_to_file(const char *filename);
35
Hung-Te Lin21ef1a32010-06-02 18:13:47 -070036 // retrieve the value of a property from GBB data.
37 // return the property value.
38 std::string get_property(PROPINDEX i) const;
39
40 // overwrite a property in GBB data.
41 // return true on success.
42 bool set_property(PROPINDEX i, const std::string &value);
43
44 // get a readable name by a property index.
45 // return the name for valid properties, otherwise unexpected empty string.
46 std::string get_property_name(PROPINDEX i) const;
47
48 // quick getters and setters of known properties in GBB
49 bool set_hwid(const char *hwid); // NOTE: hwid is NUL-terminated.
Hung-Te Lin262668f2010-05-28 10:32:02 -070050 bool set_rootkey(const std::string &value);
51 bool set_bmpfv(const std::string &value);
Hung-Te Lin08dc5f32010-06-03 14:35:47 -070052 bool set_recovery_key(const std::string &value);
Hung-Te Lin21ef1a32010-06-02 18:13:47 -070053 std::string get_hwid() const { return get_property(PROP_HWID); }
Hung-Te Lin262668f2010-05-28 10:32:02 -070054 std::string get_rootkey() const { return get_property(PROP_ROOTKEY); }
Hung-Te Lin21ef1a32010-06-02 18:13:47 -070055 std::string get_bmpfv() const { return get_property(PROP_BMPFV); }
Hung-Te Lin08dc5f32010-06-03 14:35:47 -070056 std::string get_recovery_key() const { return get_property(PROP_RCVKEY); }
Hung-Te Lin262668f2010-05-28 10:32:02 -070057
58 private:
Hung-Te Lin262668f2010-05-28 10:32:02 -070059 // clear all cached data and initialize to original state
60 void initialize();
61
62 // search and count for GBB signatures in loaded image.
63 // return the number of signatures found.
64 int search_header_signatures(const std::string &image, long *poffset) const;
65
66 // load and check header structure from image by given offset.
67 // return true if a valid GBB header is loaded into *phdr.
68 bool load_gbb_header(const std::string &image, long offset,
69 GoogleBinaryBlockHeader *phdr) const;
70
Hung-Te Lin21ef1a32010-06-02 18:13:47 -070071 // find the size, offset, and name information for given property.
72 // return true if the offset and size are assign to *poffset and *psize;
73 // if pname is not NULL, *pname will hold a pointer to a readable name.
74 // return false if the property index is invalid.
75 bool find_property(PROPINDEX i, uint32_t *poffset, uint32_t *psize,
76 const char **pname) const;
Hung-Te Lin262668f2010-05-28 10:32:02 -070077
78 GoogleBinaryBlockHeader header_; // copy of GBB header from image
79 std::string file_content_; // complete image file content
80 long header_offset_; // offset to GBB header in file_content_
81 bool is_valid_gbb; // if we are holding a valid GBB
82
83 bool verbose; // provide verbose messages
84};
85
86} // namespace vboot_reference
87
88#endif // VBOOT_REFERENCE_GBB_UTILITY_H_
89