soc/intel/common/pch: Add pch lockdown code
[coreboot.git] / util / archive / archive.c
blob36c0ab1a87cacfd06c21e161d5ad090494dbfa05
1 /*
2 * Copyright (C) 2015 The ChromiumOS Authors. All rights reserved.
3 * written by Daisuke Nojiri <dnojiri@chromium.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include "archive.h"
16 #include <endian.h>
17 #include <errno.h>
18 #include <libgen.h>
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
24 static struct directory *archive;
26 static void usage(void)
28 printf("Name:\n");
29 printf(" archive - concatenate files and create an archive\n");
30 printf("Usage:\n");
31 printf(" archive archive_name create file0 file1 ...\n");
34 static int get_file_size(const char *file)
36 FILE *fp = fopen(file, "rb");
37 int size;
39 if (!fp) {
40 fprintf(stderr, "Error: failed to open %s\n", file);
41 return -1;
43 fseek(fp, 0, SEEK_END);
44 size = ftell(fp);
45 fclose(fp);
46 if (size < 0) {
47 fprintf(stderr, "Error: failed to get file size\n");
48 return -1;
51 return size;
54 static int set_file_name(const char *path, struct dentry *dest)
56 struct dentry *entry;
57 char *name, *copy;
58 int i;
60 copy = strdup(path);
61 name = basename(copy);
63 /* check name length */
64 if (strlen(name) > NAME_LENGTH) {
65 fprintf(stderr, "Error: file name '%s' exceeds %d chars\n",
66 name, NAME_LENGTH);
67 free(copy);
68 return -1;
71 /* check if there is a duplicate name */
72 entry = get_first_dentry(archive);
73 for (i = 0; i < archive->count && &entry[i] != dest; i++) {
74 if (!strncmp(entry[i].name, name, NAME_LENGTH)) {
75 fprintf(stderr, "Error: duplicate name '%s'\n", name);
76 free(copy);
77 return -1;
81 /* copy the name to the entry */
82 strncpy(dest->name, name, NAME_LENGTH);
83 free(copy);
85 return 0;
89 * Add a file to the archive in RAM
91 * path: path to the file to be added
92 * entry: pointer to struct dentry where file header is created
93 * offset: offset of the file contents from the archive header
95 * return: 0 on success or -1 on error
97 static int add_file(const char *path, struct dentry *entry, uint32_t offset)
99 FILE *fp;
100 int size;
102 if (!path || !*path || !entry) {
103 fprintf(stderr, "Error: invalid path or entry\n");
104 return -1;
107 size = get_file_size(path);
108 if (size < 0)
109 return -1;
110 if (offset + size > archive->size) {
111 fprintf(stderr, "Error: invalid offset or size\n");
112 return -1;
115 fp = fopen(path, "rb");
116 if (!fp) {
117 fprintf(stderr, "Error: failed to open %s (%d: %s)\n",
118 path, errno, strerror(errno));
119 return -1;
121 if (fread((char *)archive + offset, sizeof(char), size, fp) != size) {
122 fprintf(stderr, "Error: failed to read %s\n", path);
123 fclose(fp);
124 return -1;
126 fclose(fp);
128 /* set file name*/
129 if (set_file_name(path, entry))
130 return -1;
132 entry->offset = offset;
133 entry->size = size;
135 return 0;
139 * Allocate memory for archive
141 * count: number of files to add
142 * files: pointer to the array of file names
144 * return: 0 on success or -1 on error
146 static int setup_archive(int count, const char **files)
148 uint32_t size;
149 int i, s;
151 size = sizeof(*archive);
152 for (i = 0; i < count; i++) {
153 s = get_file_size(files[i]);
154 if (s < 0)
155 return -1;
156 size += sizeof(struct dentry);
157 size += s;
160 archive = calloc(size, 1);
161 if (!archive) {
162 fprintf(stderr, "Error: failed to allocate memory\n");
163 return -1;
166 /* install magic string */
167 memcpy(archive->magic, CBAR_MAGIC, sizeof(archive->magic));
168 archive->version = VERSION;
169 archive->size = size;
170 archive->count = count;
172 printf("Set up archive: size=%d count=%d\n", size, count);
174 return 0;
178 * Store files in archive
180 static int archive_files(const char **files)
182 struct dentry *entry;
183 uint32_t offset;
184 int i;
186 entry = get_first_dentry(archive);
187 offset = get_first_offset(archive);
188 for (i = 0; i < archive->count; i++) {
189 if (add_file(files[i], entry, offset))
190 return -1;
191 offset += entry->size;
192 entry++;
195 return 0;
198 static void convert_endian(void)
200 struct dentry *entry;
201 int i;
203 entry = get_first_dentry(archive);
204 for (i = 0; i < archive->count; i++) {
205 entry[i].offset = htole32(entry[i].offset);
206 entry[i].size = htole32(entry[i].size);
209 archive->version = htole32(archive->version);
210 archive->size = htole32(archive->size);
211 archive->count = htole32(archive->count);
215 * Write archive to file
217 static int output_archive(const char *path)
219 FILE *fp;
221 convert_endian();
223 fp = fopen(path, "wb");
224 if (!fp) {
225 fprintf(stderr, "Error: failed to open %s\n", path);
226 fclose(fp);
227 return -1;
229 if (fwrite(archive, sizeof(char), archive->size, fp) != archive->size) {
230 fprintf(stderr, "Error: failed to write to %s\n", path);
231 fclose(fp);
232 return -1;
234 fclose(fp);
235 printf("Wrote archive to %s\n", path);
237 return 0;
240 static int cmd_create(const char *archive_path, int count, const char **files)
242 if (count < 1 || !files) {
243 fprintf(stderr, "Error: no input files specified\n");
244 return -1;
247 if (setup_archive(count, files))
248 return -1;
250 if (archive_files(files))
251 return -1;
253 if (output_archive(archive_path))
254 return -1;
256 return 0;
259 int main(int argc, const char *argv[])
261 const char *command;
263 if (argc < 3) {
264 fprintf(stderr, "Error: invalid number of arguments\n");
265 usage();
266 return -1;
269 command = argv[2];
271 /* branch by command name */
272 if (!strncmp(command, "create", sizeof("create"))) {
273 if (cmd_create(argv[1], argc - 3, &argv[3]))
274 return -1;
275 } else {
276 fprintf(stderr, "Error: invalid command: %s\n", command);
277 return -1;
280 return 0;