edid: Library of EDID decode and print
[platform/kernel/u-boot.git] / common / cmd_cbfs.c
1 /*
2  * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
3  *
4  * See file CREDITS for list of people who contributed to this
5  * project.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
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.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20  * MA 02111-1307 USA
21  */
22
23 /*
24  * CBFS commands
25  */
26 #include <common.h>
27 #include <command.h>
28 #include <cbfs.h>
29
30 int do_cbfs_init(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
31 {
32         uintptr_t end_of_rom = 0xffffffff;
33         char *ep;
34
35         if (argc > 2) {
36                 printf("usage: cbfsls [end of rom]>\n");
37                 return 0;
38         }
39         if (argc == 2) {
40                 end_of_rom = (int)simple_strtoul(argv[1], &ep, 16);
41                 if (*ep) {
42                         puts("\n** Invalid end of ROM **\n");
43                         return 1;
44                 }
45         }
46         file_cbfs_init(end_of_rom);
47         if (file_cbfs_result != CBFS_SUCCESS) {
48                 printf("%s.\n", file_cbfs_error());
49                 return 1;
50         }
51         return 0;
52 }
53
54 U_BOOT_CMD(
55         cbfsinit,       2,      0,      do_cbfs_init,
56         "initialize the cbfs driver",
57         "[end of rom]\n"
58         "    - Initialize the cbfs driver. The optional 'end of rom'\n"
59         "      parameter specifies where the end of the ROM is that the\n"
60         "      CBFS is in. It defaults to 0xFFFFFFFF\n"
61 );
62
63 int do_cbfs_fsload(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
64 {
65         const struct cbfs_cachenode *file;
66         unsigned long offset;
67         unsigned long count;
68         char buf[12];
69         long size;
70
71         if (argc < 3) {
72                 printf("usage: cbfsload <addr> <filename> [bytes]\n");
73                 return 1;
74         }
75
76         /* parse offset and count */
77         offset = simple_strtoul(argv[1], NULL, 16);
78         if (argc == 4)
79                 count = simple_strtoul(argv[3], NULL, 16);
80         else
81                 count = 0;
82
83         file = file_cbfs_find(argv[2]);
84         if (!file) {
85                 if (file_cbfs_result == CBFS_FILE_NOT_FOUND)
86                         printf("%s: %s\n", file_cbfs_error(), argv[2]);
87                 else
88                         printf("%s.\n", file_cbfs_error());
89                 return 1;
90         }
91
92         printf("reading %s\n", file_cbfs_name(file));
93
94         size = file_cbfs_read(file, (void *)offset, count);
95
96         printf("\n%ld bytes read\n", size);
97
98         sprintf(buf, "%lX", size);
99         setenv("filesize", buf);
100
101         return 0;
102 }
103
104 U_BOOT_CMD(
105         cbfsload,       4,      0,      do_cbfs_fsload,
106         "load binary file from a cbfs filesystem",
107         "<addr> <filename> [bytes]\n"
108         "    - load binary file 'filename' from the cbfs to address 'addr'\n"
109 );
110
111 int do_cbfs_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
112 {
113         const struct cbfs_cachenode *file = file_cbfs_get_first();
114         int files = 0;
115
116         if (!file) {
117                 printf("%s.\n", file_cbfs_error());
118                 return 1;
119         }
120
121         printf("     size              type  name\n");
122         printf("------------------------------------------\n");
123         while (file) {
124                 u32 type = file_cbfs_type(file);
125                 char *type_name = NULL;
126                 const char *filename = file_cbfs_name(file);
127
128                 printf(" %8d", file_cbfs_size(file));
129
130                 switch (type) {
131                 case CBFS_TYPE_STAGE:
132                         type_name = "stage";
133                         break;
134                 case CBFS_TYPE_PAYLOAD:
135                         type_name = "payload";
136                         break;
137                 case CBFS_TYPE_OPTIONROM:
138                         type_name = "option rom";
139                         break;
140                 case CBFS_TYPE_BOOTSPLASH:
141                         type_name = "boot splash";
142                         break;
143                 case CBFS_TYPE_RAW:
144                         type_name = "raw";
145                         break;
146                 case CBFS_TYPE_VSA:
147                         type_name = "vsa";
148                         break;
149                 case CBFS_TYPE_MBI:
150                         type_name = "mbi";
151                         break;
152                 case CBFS_TYPE_MICROCODE:
153                         type_name = "microcode";
154                         break;
155                 case CBFS_COMPONENT_CMOS_DEFAULT:
156                         type_name = "cmos default";
157                         break;
158                 case CBFS_COMPONENT_CMOS_LAYOUT:
159                         type_name = "cmos layout";
160                         break;
161                 case -1UL:
162                         type_name = "null";
163                         break;
164                 }
165                 if (type_name)
166                         printf("  %16s", type_name);
167                 else
168                         printf("  %16d", type);
169
170                 if (filename[0])
171                         printf("  %s\n", filename);
172                 else
173                         printf("  %s\n", "(empty)");
174                 file_cbfs_get_next(&file);
175                 files++;
176         }
177
178         printf("\n%d file(s)\n\n", files);
179         return 0;
180 }
181
182 U_BOOT_CMD(
183         cbfsls, 1,      1,      do_cbfs_ls,
184         "list files",
185         "    - list the files in the cbfs\n"
186 );
187
188 int do_cbfs_fsinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
189 {
190         const struct cbfs_header *header = file_cbfs_get_header();
191
192         if (!header) {
193                 printf("%s.\n", file_cbfs_error());
194                 return 1;
195         }
196
197         printf("\n");
198         printf("CBFS version: %#x\n", header->version);
199         printf("ROM size: %#x\n", header->rom_size);
200         printf("Boot block size: %#x\n", header->boot_block_size);
201         printf("CBFS size: %#x\n",
202                 header->rom_size - header->boot_block_size - header->offset);
203         printf("Alignment: %d\n", header->align);
204         printf("Offset: %#x\n", header->offset);
205         printf("\n");
206
207         return 0;
208 }
209
210 U_BOOT_CMD(
211         cbfsinfo,       1,      1,      do_cbfs_fsinfo,
212         "print information about filesystem",
213         "    - print information about the cbfs filesystem\n"
214 );