blob: 5754453b1cfcc63a21d129033edd8f6071e4ff7c [file] [log] [blame]
Hung-Te Lineab2c812013-01-29 01:56:17 +08001/*
2 * CBFS Image Manipulation
3 *
4 * Copyright (C) 2013 The Chromium OS Authors. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
18 */
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
24#include "common.h"
25#include "cbfs_image.h"
26
27/* The file name align is not defined in CBFS spec -- only a preference by
28 * (old) cbfstool. */
29#define CBFS_FILENAME_ALIGN (16)
30
31/* To make CBFS more friendly to ROM, fill -1 (0xFF) instead of zero. */
32#define CBFS_CONTENT_DEFAULT_VALUE (-1)
33
34static uint32_t align_up(uint32_t value, uint32_t align) {
35 if (value % align)
36 value += align - (value % align);
37 return value;
38}
39
40int cbfs_image_from_file(struct cbfs_image *image, const char *filename) {
41 if (buffer_from_file(&image->buffer, filename) != 0)
42 return -1;
43 DEBUG("read_cbfs_image: %s (%zd bytes)\n", image->buffer.name,
44 image->buffer.size);
45 image->header = cbfs_find_header(image->buffer.data,
46 image->buffer.size);
47 if (!image->header) {
48 ERROR("%s does not have CBFS master header.\n", filename);
49 cbfs_image_delete(image);
50 return -1;
51 }
52
53 return 0;
54}
55
56int cbfs_image_write_file(struct cbfs_image *image, const char *filename) {
57 assert(image && image->buffer.data);
58 return buffer_write_file(&image->buffer, filename);
59}
60
61int cbfs_image_delete(struct cbfs_image *image) {
62 buffer_delete(&image->buffer);
63 image->header = NULL;
64 return 0;
65}
66
67struct cbfs_header *cbfs_find_header(char *data, size_t size) {
68 size_t offset;
69 int found = 0;
70 uint32_t x86sig;
71 struct cbfs_header *header, *result = NULL;
72
73 // Try x86 style (check signature in bottom) header first.
74 x86sig = *(uint32_t *)(data + size - sizeof(uint32_t));
75 offset = (x86sig + (uint32_t)size);
76 DEBUG("x86sig: 0x%x, offset: 0x%zx\n", x86sig, offset);
77 if (offset >= size - sizeof(*header) ||
78 ntohl(((struct cbfs_header *)(data + offset))->magic) !=
79 CBFS_HEADER_MAGIC)
80 offset = 0;
81
82 for (; offset + sizeof(*header) < size; offset++) {
83 header = (struct cbfs_header *)(data + offset);
84 if (ntohl(header->magic) !=(CBFS_HEADER_MAGIC))
85 continue;
86 if (ntohl(header->version) != CBFS_HEADER_VERSION1 &&
87 ntohl(header->version) != CBFS_HEADER_VERSION2) {
88 // Probably not a real CBFS header?
89 continue;
90 }
91 found++;
92 result = header;
93 }
94 if (found > 1) {
95 ERROR("multiple (%d) CBFS headers found!\n",
96 found);
97 result = NULL;
98 }
99 return result;
100}
101
102
103struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image) {
104 assert(image && image->header);
105 return (struct cbfs_file *)(image->buffer.data +
106 ntohl(image->header->offset));
107}
108
109struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
110 struct cbfs_file *entry) {
111 uint32_t addr = cbfs_get_entry_addr(image, entry);
112 int align = ntohl(image->header->align);
113 assert(entry && cbfs_is_valid_entry(entry));
114 addr += ntohl(entry->offset) + ntohl(entry->len);
115 addr = align_up(addr, align);
116 return (struct cbfs_file *)(image->buffer.data + addr);
117}
118
119uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry) {
120 assert(image && image->buffer.data && entry);
121 return (int32_t)((char *)entry - image->buffer.data);
122}
123
124int cbfs_is_valid_entry(struct cbfs_file *entry) {
125 return (entry &&memcmp(entry->magic, CBFS_FILE_MAGIC,
126 sizeof(entry->magic)) == 0);
127}
128