blob: abd40eec1293d43299a1f351b49147b36ce27fea [file] [log] [blame]
Patrick Georgi269e9322011-01-21 07:24:08 +00001/*
2 * This file is part of the coreboot project.
3 *
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>)
7 *
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.
11 *
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.
16 *
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
20 */
21
Zheng Bao54516722012-10-22 16:41:42 +080022#ifdef __MINGW32__
23#include <winsock.h>
24#else
Patrick Georgi269e9322011-01-21 07:24:08 +000025#include <arpa/inet.h>
Zheng Bao54516722012-10-22 16:41:42 +080026#endif
Patrick Georgi269e9322011-01-21 07:24:08 +000027#include <sys/types.h>
28#include <sys/stat.h>
Zheng Bao54516722012-10-22 16:41:42 +080029#ifndef __MINGW32__
Patrick Georgi269e9322011-01-21 07:24:08 +000030#include <sys/mman.h>
Zheng Bao54516722012-10-22 16:41:42 +080031#endif
Patrick Georgi269e9322011-01-21 07:24:08 +000032#include <stdlib.h>
33#include <fcntl.h>
34#include <string.h>
35#include <stdio.h>
36#include "cbfs.h"
Zheng Bao54516722012-10-22 16:41:42 +080037#include "common.h"
Patrick Georgi269e9322011-01-21 07:24:08 +000038
39#define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
40#define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
41
42static void *cbfs_mapped;
Patrick Georgi4a2daf62012-03-09 12:54:03 +010043static void *cbfs_offset;
Patrick Georgi269e9322011-01-21 07:24:08 +000044static void* virt_to_phys(u32 virt)
45{
Patrick Georgi4a2daf62012-03-09 12:54:03 +010046 return cbfs_offset + virt;
Patrick Georgi269e9322011-01-21 07:24:08 +000047}
48
49#ifdef DEBUG
50#define debug(x...) printf(x)
51#else
52#define debug(x...) while(0) {}
53#endif
54
55static int cbfs_check_magic(struct cbfs_file *file)
56{
57 return !strcmp(file->magic, CBFS_FILE_MAGIC) ? 1 : 0;
58}
59
60static struct cbfs_header *cbfs_master_header(void)
61{
62 struct cbfs_header *header;
63
64 void *ptr = virt_to_phys(*((u32*)virt_to_phys(CBFS_HEADPTR_ADDR)));
65 debug("Check CBFS header at %p\n", ptr);
66 header = (struct cbfs_header *) ptr;
67
68 debug("magic is %08x\n", ntohl(header->magic));
69 if (ntohl(header->magic) != CBFS_HEADER_MAGIC) {
70 printf("ERROR: No valid CBFS header found!\n");
71 return NULL;
72 }
73
74 debug("Found CBFS header at %p\n", ptr);
75 return header;
76}
77
78struct cbfs_file *cbfs_find(const char *name)
79{
80 struct cbfs_header *header = cbfs_master_header();
Patrick Georgi4a2daf62012-03-09 12:54:03 +010081 void *offset;
Patrick Georgi269e9322011-01-21 07:24:08 +000082
83 if (header == NULL)
84 return NULL;
Patrick Georgi4a2daf62012-03-09 12:54:03 +010085 offset = virt_to_phys(0 - ntohl(header->romsize) + ntohl(header->offset));
Patrick Georgi269e9322011-01-21 07:24:08 +000086
87 int align= ntohl(header->align);
88
89 while(1) {
90 struct cbfs_file *file = (struct cbfs_file *) offset;
91 if (!cbfs_check_magic(file)) return NULL;
92 debug("Check %s\n", CBFS_NAME(file));
93 if (!strcmp(CBFS_NAME(file), name))
94 return file;
95
96 int flen = ntohl(file->len);
97 int foffset = ntohl(file->offset);
Patrick Georgi4a2daf62012-03-09 12:54:03 +010098 debug("CBFS: follow chain: %p + %x + %x + align -> ", offset, foffset, flen);
Patrick Georgi269e9322011-01-21 07:24:08 +000099
Patrick Georgi4a2daf62012-03-09 12:54:03 +0100100 void *oldoffset = offset;
101 offset = (void*)ALIGN((uintptr_t)(offset + foffset + flen), align);
Patrick Georgi269e9322011-01-21 07:24:08 +0000102 debug("%p\n", (void *)offset);
103 if (offset <= oldoffset) return NULL;
104
Patrick Georgi4a2daf62012-03-09 12:54:03 +0100105 if (offset < virt_to_phys(0xFFFFFFFF - ntohl(header->romsize)))
Patrick Georgi269e9322011-01-21 07:24:08 +0000106 return NULL;
107 }
108}
109
110void *cbfs_find_file(const char *name, unsigned int type, unsigned int *len)
111{
112 struct cbfs_file *file = cbfs_find(name);
113
114 if (file == NULL) {
115 printf("CBFS: Could not find file %s\n",
116 name);
117 return NULL;
118 }
119
120 if (ntohl(file->type) != type) {
121 printf("CBFS: File %s is of type %x instead of"
122 "type %x\n", name, file->type, type);
123
124 return NULL;
125 }
126 if (len != NULL) *len = file->len;
127
128 return (void *) CBFS_SUBHEADER(file);
129}
130
131void open_cbfs(const char *filename)
132{
133 struct stat cbfs_stat;
134 int cbfs_fd;
135
136 cbfs_fd = open(filename, O_RDWR);
137 if (cbfs_fd == -1) {
138 printf("Couldn't open '%s'\n", filename);
139 exit(-1);
140 }
141 if (fstat(cbfs_fd, &cbfs_stat) == -1) {
142 printf("Couldn't stat '%s'\n", filename);
143 exit(-1);
144 }
145 cbfs_mapped = mmap(NULL, cbfs_stat.st_size, PROT_READ | PROT_WRITE,
146 MAP_SHARED, cbfs_fd, 0);
Patrick Georgiedb0a612014-08-03 12:10:53 +0200147 close(cbfs_fd);
Patrick Georgi269e9322011-01-21 07:24:08 +0000148 if (cbfs_mapped == MAP_FAILED) {
149 printf("Couldn't map '%s'\n", filename);
150 exit(-1);
151 }
Patrick Georgi4a2daf62012-03-09 12:54:03 +0100152 cbfs_offset = cbfs_mapped-(0xffffffff-cbfs_stat.st_size+1);
Patrick Georgi269e9322011-01-21 07:24:08 +0000153}
154