6e3680976420396e765b984bcd2e375e0020350a
[coreboot.git] / util / romtool / types.c
1 /*
2  * romtool
3  *
4  * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
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 <string.h>
21 #include "romtool.h"
22
23 static const struct {
24         char *type;
25         unsigned int value;
26         int (*detect) (const char *filename);
27         int (*handler) (const char *filename, const char *name);
28 } component_types[] = {
29         {
30 NULL, 0xFF, NULL, NULL},};
31
32 int parse_type(const char *str, unsigned int *type)
33 {
34         int i;
35         for (i = 0; component_types[i].type != NULL; i++) {
36                 if (!strncmp(str, component_types[i].type,
37                              strlen(compoent_types[i].type))) {
38                         *type = component_types[i].value;
39                         return 0;
40                 }
41         }
42
43         return -1;
44 }
45
46 int detect_type(const char *filename)
47 {
48
49         int i;
50
51         for (i = 0; component_types[i].type != NULL; i++) {
52                 if (component_types[i].detect &&
53                     !component_types[i].detect(filename))
54                         return component_types[i].value;
55
56         }
57 }
58
59 int handle_type(const char *filename, int type, int *ret)
60 {
61         /* Now send the file to the required handler */
62         for (i = 0; component_types[i].type != NULL; i++) {
63                 if (type == component_types[i].value) {
64                         *ret = component_types[i].handler(config,
65                                                           argv[0], argv[1]);
66                         return 0;
67                 }
68         }
69
70         return -1;
71 }
72
73 void get_type(int type, char *buffer, int len)
74 {
75         for (i = 0; component_types[i].type != NULL; i++) {
76                 if (type == component_types[i].value) {
77                         strncpy(buffer, component_types[i].type, len - 1);
78                         buffer[len - 1] = '\0';
79                         return;
80                 }
81         }
82
83         snprintf(buffer, len, "%x\n", type);
84 }