SPDX: Convert all of our single license tags to Linux Kernel style
[platform/kernel/u-boot.git] / cmd / cbfs.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
4  */
5
6 /*
7  * CBFS commands
8  */
9 #include <common.h>
10 #include <command.h>
11 #include <cbfs.h>
12
13 static int do_cbfs_init(cmd_tbl_t *cmdtp, int flag, int argc,
14                         char *const argv[])
15 {
16         uintptr_t end_of_rom = 0xffffffff;
17         char *ep;
18
19         if (argc > 2) {
20                 printf("usage: cbfsls [end of rom]>\n");
21                 return 0;
22         }
23         if (argc == 2) {
24                 end_of_rom = simple_strtoul(argv[1], &ep, 16);
25                 if (*ep) {
26                         puts("\n** Invalid end of ROM **\n");
27                         return 1;
28                 }
29         }
30         file_cbfs_init(end_of_rom);
31         if (file_cbfs_result != CBFS_SUCCESS) {
32                 printf("%s.\n", file_cbfs_error());
33                 return 1;
34         }
35         return 0;
36 }
37
38 U_BOOT_CMD(
39         cbfsinit,       2,      0,      do_cbfs_init,
40         "initialize the cbfs driver",
41         "[end of rom]\n"
42         "    - Initialize the cbfs driver. The optional 'end of rom'\n"
43         "      parameter specifies where the end of the ROM is that the\n"
44         "      CBFS is in. It defaults to 0xFFFFFFFF\n"
45 );
46
47 static int do_cbfs_fsload(cmd_tbl_t *cmdtp, int flag, int argc,
48                           char *const argv[])
49 {
50         const struct cbfs_cachenode *file;
51         unsigned long offset;
52         unsigned long count;
53         long size;
54
55         if (argc < 3) {
56                 printf("usage: cbfsload <addr> <filename> [bytes]\n");
57                 return 1;
58         }
59
60         /* parse offset and count */
61         offset = simple_strtoul(argv[1], NULL, 16);
62         if (argc == 4)
63                 count = simple_strtoul(argv[3], NULL, 16);
64         else
65                 count = 0;
66
67         file = file_cbfs_find(argv[2]);
68         if (!file) {
69                 if (file_cbfs_result == CBFS_FILE_NOT_FOUND)
70                         printf("%s: %s\n", file_cbfs_error(), argv[2]);
71                 else
72                         printf("%s.\n", file_cbfs_error());
73                 return 1;
74         }
75
76         printf("reading %s\n", file_cbfs_name(file));
77
78         size = file_cbfs_read(file, (void *)offset, count);
79
80         printf("\n%ld bytes read\n", size);
81
82         env_set_hex("filesize", size);
83
84         return 0;
85 }
86
87 U_BOOT_CMD(
88         cbfsload,       4,      0,      do_cbfs_fsload,
89         "load binary file from a cbfs filesystem",
90         "<addr> <filename> [bytes]\n"
91         "    - load binary file 'filename' from the cbfs to address 'addr'\n"
92 );
93
94 static int do_cbfs_ls(cmd_tbl_t *cmdtp, int flag, int argc,
95                       char *const argv[])
96 {
97         const struct cbfs_cachenode *file = file_cbfs_get_first();
98         int files = 0;
99
100         if (!file) {
101                 printf("%s.\n", file_cbfs_error());
102                 return 1;
103         }
104
105         printf("     size              type  name\n");
106         printf("------------------------------------------\n");
107         while (file) {
108                 int type = file_cbfs_type(file);
109                 char *type_name = NULL;
110                 const char *filename = file_cbfs_name(file);
111
112                 printf(" %8d", file_cbfs_size(file));
113
114                 switch (type) {
115                 case CBFS_TYPE_STAGE:
116                         type_name = "stage";
117                         break;
118                 case CBFS_TYPE_PAYLOAD:
119                         type_name = "payload";
120                         break;
121                 case CBFS_TYPE_OPTIONROM:
122                         type_name = "option rom";
123                         break;
124                 case CBFS_TYPE_BOOTSPLASH:
125                         type_name = "boot splash";
126                         break;
127                 case CBFS_TYPE_RAW:
128                         type_name = "raw";
129                         break;
130                 case CBFS_TYPE_VSA:
131                         type_name = "vsa";
132                         break;
133                 case CBFS_TYPE_MBI:
134                         type_name = "mbi";
135                         break;
136                 case CBFS_TYPE_MICROCODE:
137                         type_name = "microcode";
138                         break;
139                 case CBFS_COMPONENT_CMOS_DEFAULT:
140                         type_name = "cmos default";
141                         break;
142                 case CBFS_COMPONENT_CMOS_LAYOUT:
143                         type_name = "cmos layout";
144                         break;
145                 case -1:
146                 case 0:
147                         type_name = "null";
148                         break;
149                 }
150                 if (type_name)
151                         printf("  %16s", type_name);
152                 else
153                         printf("  %16d", type);
154
155                 if (filename[0])
156                         printf("  %s\n", filename);
157                 else
158                         printf("  %s\n", "(empty)");
159                 file_cbfs_get_next(&file);
160                 files++;
161         }
162
163         printf("\n%d file(s)\n\n", files);
164         return 0;
165 }
166
167 U_BOOT_CMD(
168         cbfsls, 1,      1,      do_cbfs_ls,
169         "list files",
170         "    - list the files in the cbfs\n"
171 );
172
173 static int do_cbfs_fsinfo(cmd_tbl_t *cmdtp, int flag, int argc,
174                           char *const argv[])
175 {
176         const struct cbfs_header *header = file_cbfs_get_header();
177
178         if (!header) {
179                 printf("%s.\n", file_cbfs_error());
180                 return 1;
181         }
182
183         printf("\n");
184         printf("CBFS version: %#x\n", header->version);
185         printf("ROM size: %#x\n", header->rom_size);
186         printf("Boot block size: %#x\n", header->boot_block_size);
187         printf("CBFS size: %#x\n",
188                 header->rom_size - header->boot_block_size - header->offset);
189         printf("Alignment: %d\n", header->align);
190         printf("Offset: %#x\n", header->offset);
191         printf("\n");
192
193         return 0;
194 }
195
196 U_BOOT_CMD(
197         cbfsinfo,       1,      1,      do_cbfs_fsinfo,
198         "print information about filesystem",
199         "    - print information about the cbfs filesystem\n"
200 );