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.
21 #include <arpa/inet.h>
23 #include <sys/types.h>
35 #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
36 #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
38 static void *cbfs_mapped
;
39 static void *cbfs_offset
;
40 static void* virt_to_phys(u32 virt
)
42 return cbfs_offset
+ virt
;
46 #define debug(x...) printf(x)
48 #define debug(x...) while(0) {}
51 static int cbfs_check_magic(struct cbfs_file
*file
)
53 return !strcmp(file
->magic
, CBFS_FILE_MAGIC
) ? 1 : 0;
56 static struct cbfs_header
*cbfs_master_header(void)
58 struct cbfs_header
*header
;
60 void *ptr
= virt_to_phys(*((u32
*)virt_to_phys(CBFS_HEADPTR_ADDR
)));
61 debug("Check CBFS header at %p\n", ptr
);
62 header
= (struct cbfs_header
*) ptr
;
64 debug("magic is %08x\n", ntohl(header
->magic
));
65 if (ntohl(header
->magic
) != CBFS_HEADER_MAGIC
) {
66 printf("ERROR: No valid CBFS header found!\n");
70 debug("Found CBFS header at %p\n", ptr
);
74 struct cbfs_file
*cbfs_find(const char *name
)
76 struct cbfs_header
*header
= cbfs_master_header();
81 offset
= virt_to_phys(0 - ntohl(header
->romsize
) + ntohl(header
->offset
));
83 int align
= ntohl(header
->align
);
86 struct cbfs_file
*file
= (struct cbfs_file
*) offset
;
87 if (!cbfs_check_magic(file
)) return NULL
;
88 debug("Check %s\n", CBFS_NAME(file
));
89 if (!strcmp(CBFS_NAME(file
), name
))
92 int flen
= ntohl(file
->len
);
93 int foffset
= ntohl(file
->offset
);
94 debug("CBFS: follow chain: %p + %x + %x + align -> ", offset
, foffset
, flen
);
96 void *oldoffset
= offset
;
97 offset
= (void*)ALIGN((uintptr_t)(offset
+ foffset
+ flen
), align
);
98 debug("%p\n", (void *)offset
);
99 if (offset
<= oldoffset
) return NULL
;
101 if (offset
< virt_to_phys(0xFFFFFFFF - ntohl(header
->romsize
)))
106 void *cbfs_find_file(const char *name
, unsigned int type
, unsigned int *len
)
108 struct cbfs_file
*file
= cbfs_find(name
);
111 printf("CBFS: Could not find file %s\n",
116 if (ntohl(file
->type
) != type
) {
117 printf("CBFS: File %s is of type %x instead of"
118 "type %x\n", name
, file
->type
, type
);
122 if (len
!= NULL
) *len
= file
->len
;
124 return (void *) CBFS_SUBHEADER(file
);
127 void open_cbfs(const char *filename
)
129 struct stat cbfs_stat
;
132 cbfs_fd
= open(filename
, O_RDWR
);
134 printf("Couldn't open '%s'\n", filename
);
137 if (fstat(cbfs_fd
, &cbfs_stat
) == -1) {
138 printf("Couldn't stat '%s'\n", filename
);
141 cbfs_mapped
= mmap(NULL
, cbfs_stat
.st_size
, PROT_READ
| PROT_WRITE
,
142 MAP_SHARED
, cbfs_fd
, 0);
144 if (cbfs_mapped
== MAP_FAILED
) {
145 printf("Couldn't map '%s'\n", filename
);
148 cbfs_offset
= cbfs_mapped
-(0xffffffff-cbfs_stat
.st_size
+1);