mb: Set coreboot as DSDT's manufacturer model ID
[coreboot.git] / util / nvramtool / cbfs.c
blob3ce50c55241d10d6ee32d7192d1f689e6cc05e16
1 /*
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.
18 #ifdef __MINGW32__
19 #include <winsock.h>
20 #else
21 #include <arpa/inet.h>
22 #endif
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #ifndef __MINGW32__
26 #include <sys/mman.h>
27 #endif
28 #include <stdlib.h>
29 #include <fcntl.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include "cbfs.h"
33 #include "common.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;
45 #ifdef DEBUG
46 #define debug(x...) printf(x)
47 #else
48 #define debug(x...) while(0) {}
49 #endif
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");
67 return NULL;
70 debug("Found CBFS header at %p\n", ptr);
71 return header;
74 struct cbfs_file *cbfs_find(const char *name)
76 struct cbfs_header *header = cbfs_master_header();
77 void *offset;
79 if (header == NULL)
80 return NULL;
81 offset = virt_to_phys(0 - ntohl(header->romsize) + ntohl(header->offset));
83 int align= ntohl(header->align);
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;
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)))
102 return NULL;
106 void *cbfs_find_file(const char *name, unsigned int type, unsigned int *len)
108 struct cbfs_file *file = cbfs_find(name);
110 if (file == NULL) {
111 printf("CBFS: Could not find file %s\n",
112 name);
113 return NULL;
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);
120 return NULL;
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;
130 int cbfs_fd;
132 cbfs_fd = open(filename, O_RDWR);
133 if (cbfs_fd == -1) {
134 printf("Couldn't open '%s'\n", filename);
135 exit(-1);
137 if (fstat(cbfs_fd, &cbfs_stat) == -1) {
138 printf("Couldn't stat '%s'\n", filename);
139 exit(-1);
141 cbfs_mapped = mmap(NULL, cbfs_stat.st_size, PROT_READ | PROT_WRITE,
142 MAP_SHARED, cbfs_fd, 0);
143 close(cbfs_fd);
144 if (cbfs_mapped == MAP_FAILED) {
145 printf("Couldn't map '%s'\n", filename);
146 exit(-1);
148 cbfs_offset = cbfs_mapped-(0xffffffff-cbfs_stat.st_size+1);