soc/intel/alderlake: Update the VccIn Aux Imon IccMax for ADL-M
[coreboot.git] / util / archive / archive.c
blobf877632dcc4ab1d91a9737ea2722c8617818d02e
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include "archive.h"
4 #include <endian.h>
5 #include <errno.h>
6 #include <libgen.h>
7 #include <stdint.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
12 static struct directory *archive;
14 static void usage(void)
16 printf("Name:\n");
17 printf(" archive - concatenate files and create an archive\n");
18 printf("Usage:\n");
19 printf(" archive archive_name create file0 file1 ...\n");
22 static int get_file_size(const char *file)
24 FILE *fp = fopen(file, "rb");
25 int size;
27 if (!fp) {
28 fprintf(stderr, "Error: failed to open %s\n", file);
29 return -1;
31 fseek(fp, 0, SEEK_END);
32 size = ftell(fp);
33 fclose(fp);
34 if (size < 0) {
35 fprintf(stderr, "Error: failed to get file size\n");
36 return -1;
39 return size;
42 static int set_file_name(const char *path, struct dentry *dest)
44 struct dentry *entry;
45 char *name, *copy;
46 uint32_t i;
48 copy = strdup(path);
49 name = basename(copy);
51 /* check name length */
52 if (strlen(name) > NAME_LENGTH) {
53 fprintf(stderr, "Error: file name '%s' exceeds %d chars\n",
54 name, NAME_LENGTH);
55 free(copy);
56 return -1;
59 /* check if there is a duplicate name */
60 entry = get_first_dentry(archive);
61 for (i = 0; i < archive->count && &entry[i] != dest; i++) {
62 if (!strncmp(entry[i].name, name, NAME_LENGTH)) {
63 fprintf(stderr, "Error: duplicate name '%s'\n", name);
64 free(copy);
65 return -1;
69 /* copy the name to the entry */
70 strncpy(dest->name, name, NAME_LENGTH);
71 free(copy);
73 return 0;
77 * Add a file to the archive in RAM
79 * path: path to the file to be added
80 * entry: pointer to struct dentry where file header is created
81 * offset: offset of the file contents from the archive header
83 * return: 0 on success or -1 on error
85 static int add_file(const char *path, struct dentry *entry, uint32_t offset)
87 FILE *fp;
88 int size;
90 if (!path || !*path || !entry) {
91 fprintf(stderr, "Error: invalid path or entry\n");
92 return -1;
95 size = get_file_size(path);
96 if (size < 0)
97 return -1;
98 if (offset + size > archive->size) {
99 fprintf(stderr, "Error: invalid offset or size\n");
100 return -1;
103 fp = fopen(path, "rb");
104 if (!fp) {
105 fprintf(stderr, "Error: failed to open %s (%d: %s)\n",
106 path, errno, strerror(errno));
107 return -1;
109 if (fread((char *)archive + offset, sizeof(char), size, fp) != (size_t)size) {
110 fprintf(stderr, "Error: failed to read %s\n", path);
111 fclose(fp);
112 return -1;
114 fclose(fp);
116 /* set file name*/
117 if (set_file_name(path, entry))
118 return -1;
120 entry->offset = offset;
121 entry->size = size;
123 return 0;
127 * Allocate memory for archive
129 * count: number of files to add
130 * files: pointer to the array of file names
132 * return: 0 on success or -1 on error
134 static int setup_archive(int count, const char **files)
136 uint32_t size;
137 int i, s;
139 size = sizeof(*archive);
140 for (i = 0; i < count; i++) {
141 s = get_file_size(files[i]);
142 if (s < 0)
143 return -1;
144 size += sizeof(struct dentry);
145 size += s;
148 archive = calloc(size, 1);
149 if (!archive) {
150 fprintf(stderr, "Error: failed to allocate memory\n");
151 return -1;
154 /* install magic string */
155 memcpy(archive->magic, CBAR_MAGIC, sizeof(archive->magic));
156 archive->version = VERSION;
157 archive->size = size;
158 archive->count = count;
160 printf("Set up archive: size=%d count=%d\n", size, count);
162 return 0;
166 * Store files in archive
168 static int archive_files(const char **files)
170 struct dentry *entry;
171 uint32_t offset;
172 uint32_t i;
174 entry = get_first_dentry(archive);
175 offset = get_first_offset(archive);
176 for (i = 0; i < archive->count; i++) {
177 if (add_file(files[i], entry, offset))
178 return -1;
179 offset += entry->size;
180 entry++;
183 return 0;
186 static void convert_endian(void)
188 struct dentry *entry;
189 uint32_t i;
191 entry = get_first_dentry(archive);
192 for (i = 0; i < archive->count; i++) {
193 entry[i].offset = htole32(entry[i].offset);
194 entry[i].size = htole32(entry[i].size);
197 archive->version = htole32(archive->version);
198 archive->size = htole32(archive->size);
199 archive->count = htole32(archive->count);
203 * Write archive to file
205 static int output_archive(const char *path)
207 FILE *fp;
209 convert_endian();
211 fp = fopen(path, "wb");
212 if (!fp) {
213 fprintf(stderr, "Error: failed to open %s\n", path);
214 fclose(fp);
215 return -1;
217 if (fwrite(archive, sizeof(char), archive->size, fp) != archive->size) {
218 fprintf(stderr, "Error: failed to write to %s\n", path);
219 fclose(fp);
220 return -1;
222 fclose(fp);
223 printf("Wrote archive to %s\n", path);
225 return 0;
228 static int cmd_create(const char *archive_path, int count, const char **files)
230 if (count < 1 || !files) {
231 fprintf(stderr, "Error: no input files specified\n");
232 return -1;
235 if (setup_archive(count, files))
236 return -1;
238 if (archive_files(files))
239 return -1;
241 if (output_archive(archive_path))
242 return -1;
244 return 0;
247 int main(int argc, const char *argv[])
249 const char *command;
251 if (argc < 3) {
252 fprintf(stderr, "Error: invalid number of arguments\n");
253 usage();
254 return -1;
257 command = argv[2];
259 /* branch by command name */
260 if (!strncmp(command, "create", sizeof("create"))) {
261 if (cmd_create(argv[1], argc - 3, &argv[3]))
262 return -1;
263 } else {
264 fprintf(stderr, "Error: invalid command: %s\n", command);
265 return -1;
268 return 0;