1262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin// Copyright (c) 2010 The Chromium OS Authors. All rights reserved. 2262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin// Use of this source code is governed by a BSD-style license that can be 3262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin// found in the LICENSE file. 4262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin 5262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin 6262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin#ifndef VBOOT_REFERENCE_GBB_UTILITY_H_ 7262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin#define VBOOT_REFERENCE_GBB_UTILITY_H_ 8262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin 9262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin#include <string> 10a59a0293bd854ec40481052d4ae61950373ddd1dHung-Te Lin#include <vector> 11262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin#include "gbb_header.h" 12262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin 13262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Linnamespace vboot_reference { 14262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin 15262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Linclass GoogleBinaryBlockUtil { 16262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin public: 1721ef1a3678854cf1a0ff63a6548305a3df2ecec3Hung-Te Lin // enumerate of available data fields 1821ef1a3678854cf1a0ff63a6548305a3df2ecec3Hung-Te Lin enum PROPINDEX { 19fec47595ce655c580749f9a3f47e1fd6bd4d8ee3Hung-Te Lin PROP_FLAGS = -1,// flags (virtual property) 2021ef1a3678854cf1a0ff63a6548305a3df2ecec3Hung-Te Lin PROP_HWID, // hardware id 2121ef1a3678854cf1a0ff63a6548305a3df2ecec3Hung-Te Lin PROP_ROOTKEY, // root key 2221ef1a3678854cf1a0ff63a6548305a3df2ecec3Hung-Te Lin PROP_BMPFV, // bitmap FV 2308dc5f381d2e144428bfc25b0881a435fd1ca155Hung-Te Lin PROP_RCVKEY, // recovery key 2421ef1a3678854cf1a0ff63a6548305a3df2ecec3Hung-Te Lin PROP_RANGE, // indicator of valid property range 2521ef1a3678854cf1a0ff63a6548305a3df2ecec3Hung-Te Lin }; 2621ef1a3678854cf1a0ff63a6548305a3df2ecec3Hung-Te Lin 27262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin GoogleBinaryBlockUtil(); 28262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin ~GoogleBinaryBlockUtil(); 29262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin 30262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin // load GBB from a BIOS image file. 31262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin // return true if a valid GBB was retrieved. 32262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin bool load_from_file(const char *filename); 33262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin 34262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin // save loaded (and modified) GBB with BIOS image to new file 35262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin // return true on success. 36262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin bool save_to_file(const char *filename); 37262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin 38a59a0293bd854ec40481052d4ae61950373ddd1dHung-Te Lin // create a new GBB blob by providing a list of reserved data size for each 39a59a0293bd854ec40481052d4ae61950373ddd1dHung-Te Lin // properties, following the order described in GoogleBinaryBlockHeader. 40a59a0293bd854ec40481052d4ae61950373ddd1dHung-Te Lin // return true on success. 41a59a0293bd854ec40481052d4ae61950373ddd1dHung-Te Lin bool create_new(const std::vector<uint32_t> &create_param); 42a59a0293bd854ec40481052d4ae61950373ddd1dHung-Te Lin 43fec47595ce655c580749f9a3f47e1fd6bd4d8ee3Hung-Te Lin // retrieve the value of GBB header flags. 44fec47595ce655c580749f9a3f47e1fd6bd4d8ee3Hung-Te Lin // return the flags value. 45fec47595ce655c580749f9a3f47e1fd6bd4d8ee3Hung-Te Lin uint32_t get_flags() const; 46fec47595ce655c580749f9a3f47e1fd6bd4d8ee3Hung-Te Lin 47fec47595ce655c580749f9a3f47e1fd6bd4d8ee3Hung-Te Lin // overwrite GBB header flags. 48fec47595ce655c580749f9a3f47e1fd6bd4d8ee3Hung-Te Lin // return true on success. 49fec47595ce655c580749f9a3f47e1fd6bd4d8ee3Hung-Te Lin bool set_flags(const uint32_t flags); 50fec47595ce655c580749f9a3f47e1fd6bd4d8ee3Hung-Te Lin 5121ef1a3678854cf1a0ff63a6548305a3df2ecec3Hung-Te Lin // retrieve the value of a property from GBB data. 5221ef1a3678854cf1a0ff63a6548305a3df2ecec3Hung-Te Lin // return the property value. 5321ef1a3678854cf1a0ff63a6548305a3df2ecec3Hung-Te Lin std::string get_property(PROPINDEX i) const; 5421ef1a3678854cf1a0ff63a6548305a3df2ecec3Hung-Te Lin 5521ef1a3678854cf1a0ff63a6548305a3df2ecec3Hung-Te Lin // overwrite a property in GBB data. 5621ef1a3678854cf1a0ff63a6548305a3df2ecec3Hung-Te Lin // return true on success. 5721ef1a3678854cf1a0ff63a6548305a3df2ecec3Hung-Te Lin bool set_property(PROPINDEX i, const std::string &value); 5821ef1a3678854cf1a0ff63a6548305a3df2ecec3Hung-Te Lin 5921ef1a3678854cf1a0ff63a6548305a3df2ecec3Hung-Te Lin // get a readable name by a property index. 6021ef1a3678854cf1a0ff63a6548305a3df2ecec3Hung-Te Lin // return the name for valid properties, otherwise unexpected empty string. 6121ef1a3678854cf1a0ff63a6548305a3df2ecec3Hung-Te Lin std::string get_property_name(PROPINDEX i) const; 6221ef1a3678854cf1a0ff63a6548305a3df2ecec3Hung-Te Lin 6321ef1a3678854cf1a0ff63a6548305a3df2ecec3Hung-Te Lin // quick getters and setters of known properties in GBB 6421ef1a3678854cf1a0ff63a6548305a3df2ecec3Hung-Te Lin bool set_hwid(const char *hwid); // NOTE: hwid is NUL-terminated. 65262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin bool set_rootkey(const std::string &value); 66262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin bool set_bmpfv(const std::string &value); 6708dc5f381d2e144428bfc25b0881a435fd1ca155Hung-Te Lin bool set_recovery_key(const std::string &value); 6821ef1a3678854cf1a0ff63a6548305a3df2ecec3Hung-Te Lin std::string get_hwid() const { return get_property(PROP_HWID); } 69262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin std::string get_rootkey() const { return get_property(PROP_ROOTKEY); } 7021ef1a3678854cf1a0ff63a6548305a3df2ecec3Hung-Te Lin std::string get_bmpfv() const { return get_property(PROP_BMPFV); } 7108dc5f381d2e144428bfc25b0881a435fd1ca155Hung-Te Lin std::string get_recovery_key() const { return get_property(PROP_RCVKEY); } 72262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin 73262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin private: 74262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin // clear all cached data and initialize to original state 75262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin void initialize(); 76262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin 77262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin // search and count for GBB signatures in loaded image. 78262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin // return the number of signatures found. 79262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin int search_header_signatures(const std::string &image, long *poffset) const; 80262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin 81262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin // load and check header structure from image by given offset. 82262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin // return true if a valid GBB header is loaded into *phdr. 83262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin bool load_gbb_header(const std::string &image, long offset, 84262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin GoogleBinaryBlockHeader *phdr) const; 85262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin 8621ef1a3678854cf1a0ff63a6548305a3df2ecec3Hung-Te Lin // find the size, offset, and name information for given property. 8721ef1a3678854cf1a0ff63a6548305a3df2ecec3Hung-Te Lin // return true if the offset and size are assign to *poffset and *psize; 8821ef1a3678854cf1a0ff63a6548305a3df2ecec3Hung-Te Lin // if pname is not NULL, *pname will hold a pointer to a readable name. 8921ef1a3678854cf1a0ff63a6548305a3df2ecec3Hung-Te Lin // return false if the property index is invalid. 9021ef1a3678854cf1a0ff63a6548305a3df2ecec3Hung-Te Lin bool find_property(PROPINDEX i, uint32_t *poffset, uint32_t *psize, 9121ef1a3678854cf1a0ff63a6548305a3df2ecec3Hung-Te Lin const char **pname) const; 92262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin 93262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin GoogleBinaryBlockHeader header_; // copy of GBB header from image 94262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin std::string file_content_; // complete image file content 95262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin long header_offset_; // offset to GBB header in file_content_ 96262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin bool is_valid_gbb; // if we are holding a valid GBB 97262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin 98262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin bool verbose; // provide verbose messages 99262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin}; 100262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin 101262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin} // namespace vboot_reference 102262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin 103262668fcf3d66595947527d0bb34c1b4e0bc250eHung-Te Lin#endif // VBOOT_REFERENCE_GBB_UTILITY_H_ 104