blob: 3ce50c55241d10d6ee32d7192d1f689e6cc05e16 [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.
Patrick Georgi269e9322011-01-21 07:24:08 +000016 */
17
Zheng Bao54516722012-10-22 16:41:42 +080018#ifdef __MINGW32__
19#include <winsock.h>
20#else
Patrick Georgi269e9322011-01-21 07:24:08 +000021#include <arpa/inet.h>
Zheng Bao54516722012-10-22 16:41:42 +080022#endif
Patrick Georgi269e9322011-01-21 07:24:08 +000023#include <sys/types.h>
24#include <sys/stat.h>
Zheng Bao54516722012-10-22 16:41:42 +080025#ifndef __MINGW32__
Patrick Georgi269e9322011-01-21 07:24:08 +000026#include <sys/mman.h>
Zheng Bao54516722012-10-22 16:41:42 +080027#endif
Patrick Georgi269e9322011-01-21 07:24:08 +000028#include <stdlib.h>
29#include <fcntl.h>
30#include <string.h>
31#include <stdio.h>
32#include "cbfs.h"
Zheng Bao54516722012-10-22 16:41:42 +080033#include "common.h"
Patrick Georgi269e9322011-01-21 07:24:08 +000034
35#define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
36#define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
37
38static void *cbfs_mapped;
Patrick Georgi4a2daf62012-03-09 12:54:03 +010039static void *cbfs_offset;
Patrick Georgi269e9322011-01-21 07:24:08 +000040static void* virt_to_phys(u32 virt)
41{
Patrick Georgi4a2daf62012-03-09 12:54:03 +010042 return cbfs_offset + virt;
Patrick Georgi269e9322011-01-21 07:24:08 +000043}
44
45#ifdef DEBUG
46#define debug(x...) printf(x)
47#else
48#define debug(x...) while(0) {}
49#endif
50
51static int cbfs_check_magic(struct cbfs_file *file)
52{
53 return !strcmp(file->magic, CBFS_FILE_MAGIC) ? 1 : 0;
54}
55
56static struct cbfs_header *cbfs_master_header(void)
57{
58 struct cbfs_header *header;
59
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;
63
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");
67 return NULL;
68 }
69
70 debug("Found CBFS header at %p\n", ptr);
71 return header;
72}
73
74struct cbfs_file *cbfs_find(const char *name)
75{
76 struct cbfs_header *header = cbfs_master_header();
Patrick Georgi4a2daf62012-03-09 12:54:03 +010077 void *offset;
Patrick Georgi269e9322011-01-21 07:24:08 +000078
79 if (header == NULL)
80 return NULL;
Patrick Georgi4a2daf62012-03-09 12:54:03 +010081 offset = virt_to_phys(0 - ntohl(header->romsize) + ntohl(header->offset));
Patrick Georgi269e9322011-01-21 07:24:08 +000082
83 int align= ntohl(header->align);
84
85 while(1) {
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))
90 return file;
91
92 int flen = ntohl(file->len);
93 int foffset = ntohl(file->offset);
Patrick Georgi4a2daf62012-03-09 12:54:03 +010094 debug("CBFS: follow chain: %p + %x + %x + align -> ", offset, foffset, flen);
Patrick Georgi269e9322011-01-21 07:24:08 +000095
Patrick Georgi4a2daf62012-03-09 12:54:03 +010096 void *oldoffset = offset;
97 offset = (void*)ALIGN((uintptr_t)(offset + foffset + flen), align);
Patrick Georgi269e9322011-01-21 07:24:08 +000098 debug("%p\n", (void *)offset);
99 if (offset <= oldoffset) return NULL;
100
Patrick Georgi4a2daf62012-03-09 12:54:03 +0100101 if (offset < virt_to_phys(0xFFFFFFFF - ntohl(header->romsize)))
Patrick Georgi269e9322011-01-21 07:24:08 +0000102 return NULL;
103 }
104}
105
106void *cbfs_find_file(const char *name, unsigned int type, unsigned int *len)
107{
108 struct cbfs_file *file = cbfs_find(name);
109
110 if (file == NULL) {
111 printf("CBFS: Could not find file %s\n",
112 name);
113 return NULL;
114 }
115
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);
119
120 return NULL;
121 }
122 if (len != NULL) *len = file->len;
123
124 return (void *) CBFS_SUBHEADER(file);
125}
126
127void open_cbfs(const char *filename)
128{
129 struct stat cbfs_stat;
130 int cbfs_fd;
131
132 cbfs_fd = open(filename, O_RDWR);
133 if (cbfs_fd == -1) {
134 printf("Couldn't open '%s'\n", filename);
135 exit(-1);
136 }
137 if (fstat(cbfs_fd, &cbfs_stat) == -1) {
138 printf("Couldn't stat '%s'\n", filename);
139 exit(-1);
140 }
141 cbfs_mapped = mmap(NULL, cbfs_stat.st_size, PROT_READ | PROT_WRITE,
142 MAP_SHARED, cbfs_fd, 0);
Patrick Georgiedb0a612014-08-03 12:10:53 +0200143 close(cbfs_fd);
Patrick Georgi269e9322011-01-21 07:24:08 +0000144 if (cbfs_mapped == MAP_FAILED) {
145 printf("Couldn't map '%s'\n", filename);
146 exit(-1);
147 }
Patrick Georgi4a2daf62012-03-09 12:54:03 +0100148 cbfs_offset = cbfs_mapped-(0xffffffff-cbfs_stat.st_size+1);
Patrick Georgi269e9322011-01-21 07:24:08 +0000149}