2 * This file is part of the coreboot project.
4 * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
5 * Copyright (C) 2011 secunet Security Networks AG
6 * (Written by Patrick Georgi <patrick.georgi@secunet.com>)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
22 #include <arpa/inet.h>
23 #include <sys/types.h>
32 #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
33 #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
35 static void *cbfs_mapped
;
36 static void *cbfs_offset
;
37 static void* virt_to_phys(u32 virt
)
39 return cbfs_offset
+ virt
;
43 #define debug(x...) printf(x)
45 #define debug(x...) while(0) {}
48 static int cbfs_check_magic(struct cbfs_file
*file
)
50 return !strcmp(file
->magic
, CBFS_FILE_MAGIC
) ? 1 : 0;
53 static struct cbfs_header
*cbfs_master_header(void)
55 struct cbfs_header
*header
;
57 void *ptr
= virt_to_phys(*((u32
*)virt_to_phys(CBFS_HEADPTR_ADDR
)));
58 debug("Check CBFS header at %p\n", ptr
);
59 header
= (struct cbfs_header
*) ptr
;
61 debug("magic is %08x\n", ntohl(header
->magic
));
62 if (ntohl(header
->magic
) != CBFS_HEADER_MAGIC
) {
63 printf("ERROR: No valid CBFS header found!\n");
67 debug("Found CBFS header at %p\n", ptr
);
71 struct cbfs_file
*cbfs_find(const char *name
)
73 struct cbfs_header
*header
= cbfs_master_header();
78 offset
= virt_to_phys(0 - ntohl(header
->romsize
) + ntohl(header
->offset
));
80 int align
= ntohl(header
->align
);
83 struct cbfs_file
*file
= (struct cbfs_file
*) offset
;
84 if (!cbfs_check_magic(file
)) return NULL
;
85 debug("Check %s\n", CBFS_NAME(file
));
86 if (!strcmp(CBFS_NAME(file
), name
))
89 int flen
= ntohl(file
->len
);
90 int foffset
= ntohl(file
->offset
);
91 debug("CBFS: follow chain: %p + %x + %x + align -> ", offset
, foffset
, flen
);
93 void *oldoffset
= offset
;
94 offset
= (void*)ALIGN((uintptr_t)(offset
+ foffset
+ flen
), align
);
95 debug("%p\n", (void *)offset
);
96 if (offset
<= oldoffset
) return NULL
;
98 if (offset
< virt_to_phys(0xFFFFFFFF - ntohl(header
->romsize
)))
103 void *cbfs_find_file(const char *name
, unsigned int type
, unsigned int *len
)
105 struct cbfs_file
*file
= cbfs_find(name
);
108 printf("CBFS: Could not find file %s\n",
113 if (ntohl(file
->type
) != type
) {
114 printf("CBFS: File %s is of type %x instead of"
115 "type %x\n", name
, file
->type
, type
);
119 if (len
!= NULL
) *len
= file
->len
;
121 return (void *) CBFS_SUBHEADER(file
);
124 void open_cbfs(const char *filename
)
126 struct stat cbfs_stat
;
129 cbfs_fd
= open(filename
, O_RDWR
);
131 printf("Couldn't open '%s'\n", filename
);
134 if (fstat(cbfs_fd
, &cbfs_stat
) == -1) {
135 printf("Couldn't stat '%s'\n", filename
);
138 cbfs_mapped
= mmap(NULL
, cbfs_stat
.st_size
, PROT_READ
| PROT_WRITE
,
139 MAP_SHARED
, cbfs_fd
, 0);
140 if (cbfs_mapped
== MAP_FAILED
) {
141 printf("Couldn't map '%s'\n", filename
);
144 cbfs_offset
= cbfs_mapped
-(0xffffffff-cbfs_stat
.st_size
+1);