1*84cd9327SGabe Black /* 2*84cd9327SGabe Black * Copyright (c) 2011 The Chromium OS Authors. All rights reserved. 3*84cd9327SGabe Black * 4*84cd9327SGabe Black * See file CREDITS for list of people who contributed to this 5*84cd9327SGabe Black * project. 6*84cd9327SGabe Black * 7*84cd9327SGabe Black * This program is free software; you can redistribute it and/or 8*84cd9327SGabe Black * modify it under the terms of the GNU General Public License as 9*84cd9327SGabe Black * published by the Free Software Foundation; either version 2 of 10*84cd9327SGabe Black * the License, or (at your option) any later version. 11*84cd9327SGabe Black * 12*84cd9327SGabe Black * This program is distributed in the hope that it will be useful, 13*84cd9327SGabe Black * but WITHOUT ANY WARRANTY; without even the implied warranty of 14*84cd9327SGabe Black * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15*84cd9327SGabe Black * GNU General Public License for more details. 16*84cd9327SGabe Black * 17*84cd9327SGabe Black * You should have received a copy of the GNU General Public License 18*84cd9327SGabe Black * along with this program; if not, write to the Free Software 19*84cd9327SGabe Black * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 20*84cd9327SGabe Black * MA 02111-1307 USA 21*84cd9327SGabe Black */ 22*84cd9327SGabe Black 23*84cd9327SGabe Black #ifndef __CBFS_H 24*84cd9327SGabe Black #define __CBFS_H 25*84cd9327SGabe Black 26*84cd9327SGabe Black #include <compiler.h> 27*84cd9327SGabe Black #include <linux/compiler.h> 28*84cd9327SGabe Black 29*84cd9327SGabe Black enum cbfs_result { 30*84cd9327SGabe Black CBFS_SUCCESS = 0, 31*84cd9327SGabe Black CBFS_NOT_INITIALIZED, 32*84cd9327SGabe Black CBFS_BAD_HEADER, 33*84cd9327SGabe Black CBFS_BAD_FILE, 34*84cd9327SGabe Black CBFS_FILE_NOT_FOUND 35*84cd9327SGabe Black }; 36*84cd9327SGabe Black 37*84cd9327SGabe Black enum cbfs_filetype { 38*84cd9327SGabe Black CBFS_TYPE_STAGE = 0x10, 39*84cd9327SGabe Black CBFS_TYPE_PAYLOAD = 0x20, 40*84cd9327SGabe Black CBFS_TYPE_OPTIONROM = 0x30, 41*84cd9327SGabe Black CBFS_TYPE_BOOTSPLASH = 0x40, 42*84cd9327SGabe Black CBFS_TYPE_RAW = 0x50, 43*84cd9327SGabe Black CBFS_TYPE_VSA = 0x51, 44*84cd9327SGabe Black CBFS_TYPE_MBI = 0x52, 45*84cd9327SGabe Black CBFS_TYPE_MICROCODE = 0x53, 46*84cd9327SGabe Black CBFS_COMPONENT_CMOS_DEFAULT = 0xaa, 47*84cd9327SGabe Black CBFS_COMPONENT_CMOS_LAYOUT = 0x01aa 48*84cd9327SGabe Black }; 49*84cd9327SGabe Black 50*84cd9327SGabe Black struct cbfs_header { 51*84cd9327SGabe Black u32 magic; 52*84cd9327SGabe Black u32 version; 53*84cd9327SGabe Black u32 rom_size; 54*84cd9327SGabe Black u32 boot_block_size; 55*84cd9327SGabe Black u32 align; 56*84cd9327SGabe Black u32 offset; 57*84cd9327SGabe Black u32 pad[2]; 58*84cd9327SGabe Black } __packed; 59*84cd9327SGabe Black 60*84cd9327SGabe Black struct cbfs_fileheader { 61*84cd9327SGabe Black u8 magic[8]; 62*84cd9327SGabe Black u32 len; 63*84cd9327SGabe Black u32 type; 64*84cd9327SGabe Black u32 checksum; 65*84cd9327SGabe Black u32 offset; 66*84cd9327SGabe Black } __packed; 67*84cd9327SGabe Black 68*84cd9327SGabe Black struct cbfs_cachenode { 69*84cd9327SGabe Black struct cbfs_cachenode *next; 70*84cd9327SGabe Black u32 type; 71*84cd9327SGabe Black void *data; 72*84cd9327SGabe Black u32 data_length; 73*84cd9327SGabe Black char *name; 74*84cd9327SGabe Black u32 name_length; 75*84cd9327SGabe Black u32 checksum; 76*84cd9327SGabe Black } __packed; 77*84cd9327SGabe Black 78*84cd9327SGabe Black extern enum cbfs_result file_cbfs_result; 79*84cd9327SGabe Black 80*84cd9327SGabe Black /* 81*84cd9327SGabe Black * Return a string describing the most recent error condition. 82*84cd9327SGabe Black * 83*84cd9327SGabe Black * @return A pointer to the constant string. 84*84cd9327SGabe Black */ 85*84cd9327SGabe Black const char *file_cbfs_error(void); 86*84cd9327SGabe Black 87*84cd9327SGabe Black /* 88*84cd9327SGabe Black * Initialize the CBFS driver and load metadata into RAM. 89*84cd9327SGabe Black * 90*84cd9327SGabe Black * @param end_of_rom Points to the end of the ROM the CBFS should be read 91*84cd9327SGabe Black * from. 92*84cd9327SGabe Black */ 93*84cd9327SGabe Black void file_cbfs_init(uintptr_t end_of_rom); 94*84cd9327SGabe Black 95*84cd9327SGabe Black /* 96*84cd9327SGabe Black * Get the header structure for the current CBFS. 97*84cd9327SGabe Black * 98*84cd9327SGabe Black * @return A pointer to the constant structure, or NULL if there is none. 99*84cd9327SGabe Black */ 100*84cd9327SGabe Black const struct cbfs_header *file_cbfs_get_header(void); 101*84cd9327SGabe Black 102*84cd9327SGabe Black /* 103*84cd9327SGabe Black * Get a handle for the first file in CBFS. 104*84cd9327SGabe Black * 105*84cd9327SGabe Black * @return A handle for the first file in CBFS, NULL on error. 106*84cd9327SGabe Black */ 107*84cd9327SGabe Black const struct cbfs_cachenode *file_cbfs_get_first(void); 108*84cd9327SGabe Black 109*84cd9327SGabe Black /* 110*84cd9327SGabe Black * Get a handle to the file after this one in CBFS. 111*84cd9327SGabe Black * 112*84cd9327SGabe Black * @param file A pointer to the handle to advance. 113*84cd9327SGabe Black */ 114*84cd9327SGabe Black void file_cbfs_get_next(const struct cbfs_cachenode **file); 115*84cd9327SGabe Black 116*84cd9327SGabe Black /* 117*84cd9327SGabe Black * Find a file with a particular name in CBFS. 118*84cd9327SGabe Black * 119*84cd9327SGabe Black * @param name The name to search for. 120*84cd9327SGabe Black * 121*84cd9327SGabe Black * @return A handle to the file, or NULL on error. 122*84cd9327SGabe Black */ 123*84cd9327SGabe Black const struct cbfs_cachenode *file_cbfs_find(const char *name); 124*84cd9327SGabe Black 125*84cd9327SGabe Black 126*84cd9327SGabe Black /***************************************************************************/ 127*84cd9327SGabe Black /* All of the functions below can be used without first initializing CBFS. */ 128*84cd9327SGabe Black /***************************************************************************/ 129*84cd9327SGabe Black 130*84cd9327SGabe Black /* 131*84cd9327SGabe Black * Find a file with a particular name in CBFS without using the heap. 132*84cd9327SGabe Black * 133*84cd9327SGabe Black * @param end_of_rom Points to the end of the ROM the CBFS should be read 134*84cd9327SGabe Black * from. 135*84cd9327SGabe Black * @param name The name to search for. 136*84cd9327SGabe Black * 137*84cd9327SGabe Black * @return A handle to the file, or NULL on error. 138*84cd9327SGabe Black */ 139*84cd9327SGabe Black const struct cbfs_cachenode *file_cbfs_find_uncached(uintptr_t end_of_rom, 140*84cd9327SGabe Black const char *name); 141*84cd9327SGabe Black 142*84cd9327SGabe Black /* 143*84cd9327SGabe Black * Get the name of a file in CBFS. 144*84cd9327SGabe Black * 145*84cd9327SGabe Black * @param file The handle to the file. 146*84cd9327SGabe Black * 147*84cd9327SGabe Black * @return The name of the file, NULL on error. 148*84cd9327SGabe Black */ 149*84cd9327SGabe Black const char *file_cbfs_name(const struct cbfs_cachenode *file); 150*84cd9327SGabe Black 151*84cd9327SGabe Black /* 152*84cd9327SGabe Black * Get the size of a file in CBFS. 153*84cd9327SGabe Black * 154*84cd9327SGabe Black * @param file The handle to the file. 155*84cd9327SGabe Black * 156*84cd9327SGabe Black * @return The size of the file, zero on error. 157*84cd9327SGabe Black */ 158*84cd9327SGabe Black u32 file_cbfs_size(const struct cbfs_cachenode *file); 159*84cd9327SGabe Black 160*84cd9327SGabe Black /* 161*84cd9327SGabe Black * Get the type of a file in CBFS. 162*84cd9327SGabe Black * 163*84cd9327SGabe Black * @param file The handle to the file. 164*84cd9327SGabe Black * 165*84cd9327SGabe Black * @return The type of the file, zero on error. 166*84cd9327SGabe Black */ 167*84cd9327SGabe Black u32 file_cbfs_type(const struct cbfs_cachenode *file); 168*84cd9327SGabe Black 169*84cd9327SGabe Black /* 170*84cd9327SGabe Black * Read a file from CBFS into RAM 171*84cd9327SGabe Black * 172*84cd9327SGabe Black * @param file A handle to the file to read. 173*84cd9327SGabe Black * @param buffer Where to read it into memory. 174*84cd9327SGabe Black * 175*84cd9327SGabe Black * @return If positive or zero, the number of characters read. If negative, an 176*84cd9327SGabe Black * error occurred. 177*84cd9327SGabe Black */ 178*84cd9327SGabe Black long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer, 179*84cd9327SGabe Black unsigned long maxsize); 180*84cd9327SGabe Black 181*84cd9327SGabe Black #endif /* __CBFS_H */ 182