1*8617a60dSAndroid Build Coastguard Worker /* Copyright 2022 The ChromiumOS Authors 2*8617a60dSAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license that can be 3*8617a60dSAndroid Build Coastguard Worker * found in the LICENSE file. 4*8617a60dSAndroid Build Coastguard Worker */ 5*8617a60dSAndroid Build Coastguard Worker 6*8617a60dSAndroid Build Coastguard Worker #include <stdbool.h> 7*8617a60dSAndroid Build Coastguard Worker 8*8617a60dSAndroid Build Coastguard Worker #include "2return_codes.h" 9*8617a60dSAndroid Build Coastguard Worker #include "2sha.h" 10*8617a60dSAndroid Build Coastguard Worker 11*8617a60dSAndroid Build Coastguard Worker #define ENV_CBFSTOOL "CBFSTOOL" 12*8617a60dSAndroid Build Coastguard Worker #define DEFAULT_CBFSTOOL "cbfstool" 13*8617a60dSAndroid Build Coastguard Worker 14*8617a60dSAndroid Build Coastguard Worker /* 15*8617a60dSAndroid Build Coastguard Worker * Check the existence of a CBFS file. 16*8617a60dSAndroid Build Coastguard Worker * 17*8617a60dSAndroid Build Coastguard Worker * @param image_file Firmware image file. 18*8617a60dSAndroid Build Coastguard Worker * @param region FMAP region (section). If `region` is NULL, then the 19*8617a60dSAndroid Build Coastguard Worker * region option will not be passed to cbfstool, and hence 20*8617a60dSAndroid Build Coastguard Worker * opterations will be performed on the default "COREBOOT" 21*8617a60dSAndroid Build Coastguard Worker * region. 22*8617a60dSAndroid Build Coastguard Worker * @param name CBFS file name. 23*8617a60dSAndroid Build Coastguard Worker * @return true if the CBFS file exists; false otherwise. 24*8617a60dSAndroid Build Coastguard Worker */ 25*8617a60dSAndroid Build Coastguard Worker bool cbfstool_file_exists(const char *image_file, const char *region, 26*8617a60dSAndroid Build Coastguard Worker const char *name); 27*8617a60dSAndroid Build Coastguard Worker 28*8617a60dSAndroid Build Coastguard Worker /* 29*8617a60dSAndroid Build Coastguard Worker * Extract a CBFS file from a firmware image file. 30*8617a60dSAndroid Build Coastguard Worker * 31*8617a60dSAndroid Build Coastguard Worker * @param image_file Firmware image file. 32*8617a60dSAndroid Build Coastguard Worker * @param region FMAP region (section). If `region` is NULL, then the 33*8617a60dSAndroid Build Coastguard Worker * region option will not be passed to cbfstool, and hence 34*8617a60dSAndroid Build Coastguard Worker * opterations will be performed on the default "COREBOOT" 35*8617a60dSAndroid Build Coastguard Worker * region. 36*8617a60dSAndroid Build Coastguard Worker * @param name CBFS file name to extract. 37*8617a60dSAndroid Build Coastguard Worker * @param file File path to store the extracted file to. 38*8617a60dSAndroid Build Coastguard Worker * @return 0 on success; non-zero on failure. 39*8617a60dSAndroid Build Coastguard Worker */ 40*8617a60dSAndroid Build Coastguard Worker int cbfstool_extract(const char *image_file, const char *region, 41*8617a60dSAndroid Build Coastguard Worker const char *name, const char *file); 42*8617a60dSAndroid Build Coastguard Worker 43*8617a60dSAndroid Build Coastguard Worker /* Truncate CBFS region and store the new CBFS size to `new_size`. */ 44*8617a60dSAndroid Build Coastguard Worker vb2_error_t cbfstool_truncate(const char *file, const char *region, 45*8617a60dSAndroid Build Coastguard Worker size_t *new_size); 46*8617a60dSAndroid Build Coastguard Worker 47*8617a60dSAndroid Build Coastguard Worker /* 48*8617a60dSAndroid Build Coastguard Worker * Check whether image under `file` path supports CBFS_VERIFICATION, 49*8617a60dSAndroid Build Coastguard Worker * and contains metadata hash. Hash found is available under *hash. If it was 50*8617a60dSAndroid Build Coastguard Worker * not found, then hash type will be set to VB2_HASH_INVALID. 51*8617a60dSAndroid Build Coastguard Worker * 52*8617a60dSAndroid Build Coastguard Worker * If `region` is NULL, then region option will not be passed to cbfstool. 53*8617a60dSAndroid Build Coastguard Worker * Operations will be performed on default `COREBOOT` region. 54*8617a60dSAndroid Build Coastguard Worker */ 55*8617a60dSAndroid Build Coastguard Worker vb2_error_t cbfstool_get_metadata_hash(const char *file, const char *region, 56*8617a60dSAndroid Build Coastguard Worker struct vb2_hash *hash); 57*8617a60dSAndroid Build Coastguard Worker 58*8617a60dSAndroid Build Coastguard Worker /* 59*8617a60dSAndroid Build Coastguard Worker * Get value of a bool Kconfig option from "config" file in CBFS. 60*8617a60dSAndroid Build Coastguard Worker * 61*8617a60dSAndroid Build Coastguard Worker * This function extracts "config" file from selected region, parses it to find 62*8617a60dSAndroid Build Coastguard Worker * value of `config_field`, and stores it in `value`. On failure, `value` will 63*8617a60dSAndroid Build Coastguard Worker * be false. 64*8617a60dSAndroid Build Coastguard Worker * 65*8617a60dSAndroid Build Coastguard Worker * If `region` is NULL, then region option will not be passed to cbfstool. 66*8617a60dSAndroid Build Coastguard Worker * Operations will be performed on default `COREBOOT` region. 67*8617a60dSAndroid Build Coastguard Worker */ 68*8617a60dSAndroid Build Coastguard Worker vb2_error_t cbfstool_get_config_bool(const char *file, const char *region, 69*8617a60dSAndroid Build Coastguard Worker const char *config_field, bool *value); 70*8617a60dSAndroid Build Coastguard Worker 71*8617a60dSAndroid Build Coastguard Worker /* 72*8617a60dSAndroid Build Coastguard Worker * Get value of a str Kconfig option from "config" file in CBFS. 73*8617a60dSAndroid Build Coastguard Worker * 74*8617a60dSAndroid Build Coastguard Worker * This is similar to cbfstool_get_config_bool(). On success, the extracted 75*8617a60dSAndroid Build Coastguard Worker * value is stored in `value` as an allocated string (which has to be freed by 76*8617a60dSAndroid Build Coastguard Worker * the caller). If the value is not found, an error will be returned, and 77*8617a60dSAndroid Build Coastguard Worker * `value` will be NULL. 78*8617a60dSAndroid Build Coastguard Worker */ 79*8617a60dSAndroid Build Coastguard Worker vb2_error_t cbfstool_get_config_string(const char *file, const char *region, 80*8617a60dSAndroid Build Coastguard Worker const char *config_field, char **value); 81