Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * CBFS commands |
| 8 | */ |
| 9 | #include <common.h> |
| 10 | #include <command.h> |
Simon Glass | c7694dd | 2019-08-01 09:46:46 -0600 | [diff] [blame] | 11 | #include <env.h> |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 12 | #include <cbfs.h> |
| 13 | |
Simon Glass | 633fb73 | 2016-03-13 19:07:27 -0600 | [diff] [blame] | 14 | static int do_cbfs_init(cmd_tbl_t *cmdtp, int flag, int argc, |
| 15 | char *const argv[]) |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 16 | { |
| 17 | uintptr_t end_of_rom = 0xffffffff; |
| 18 | char *ep; |
| 19 | |
| 20 | if (argc > 2) { |
| 21 | printf("usage: cbfsls [end of rom]>\n"); |
| 22 | return 0; |
| 23 | } |
| 24 | if (argc == 2) { |
Andre Heider | 33222c8 | 2018-02-15 07:40:10 +0100 | [diff] [blame] | 25 | end_of_rom = simple_strtoul(argv[1], &ep, 16); |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 26 | if (*ep) { |
| 27 | puts("\n** Invalid end of ROM **\n"); |
| 28 | return 1; |
| 29 | } |
| 30 | } |
| 31 | file_cbfs_init(end_of_rom); |
Simon Glass | c7f1693 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 32 | if (cbfs_get_result() != CBFS_SUCCESS) { |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 33 | printf("%s.\n", file_cbfs_error()); |
| 34 | return 1; |
| 35 | } |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | U_BOOT_CMD( |
| 40 | cbfsinit, 2, 0, do_cbfs_init, |
| 41 | "initialize the cbfs driver", |
| 42 | "[end of rom]\n" |
| 43 | " - Initialize the cbfs driver. The optional 'end of rom'\n" |
| 44 | " parameter specifies where the end of the ROM is that the\n" |
| 45 | " CBFS is in. It defaults to 0xFFFFFFFF\n" |
| 46 | ); |
| 47 | |
Simon Glass | 633fb73 | 2016-03-13 19:07:27 -0600 | [diff] [blame] | 48 | static int do_cbfs_fsload(cmd_tbl_t *cmdtp, int flag, int argc, |
| 49 | char *const argv[]) |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 50 | { |
| 51 | const struct cbfs_cachenode *file; |
| 52 | unsigned long offset; |
| 53 | unsigned long count; |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 54 | long size; |
| 55 | |
| 56 | if (argc < 3) { |
| 57 | printf("usage: cbfsload <addr> <filename> [bytes]\n"); |
| 58 | return 1; |
| 59 | } |
| 60 | |
| 61 | /* parse offset and count */ |
| 62 | offset = simple_strtoul(argv[1], NULL, 16); |
| 63 | if (argc == 4) |
| 64 | count = simple_strtoul(argv[3], NULL, 16); |
| 65 | else |
| 66 | count = 0; |
| 67 | |
| 68 | file = file_cbfs_find(argv[2]); |
| 69 | if (!file) { |
Simon Glass | c7f1693 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 70 | if (cbfs_get_result() == CBFS_FILE_NOT_FOUND) |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 71 | printf("%s: %s\n", file_cbfs_error(), argv[2]); |
| 72 | else |
| 73 | printf("%s.\n", file_cbfs_error()); |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | printf("reading %s\n", file_cbfs_name(file)); |
| 78 | |
| 79 | size = file_cbfs_read(file, (void *)offset, count); |
| 80 | |
| 81 | printf("\n%ld bytes read\n", size); |
| 82 | |
Simon Glass | 018f530 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 83 | env_set_hex("filesize", size); |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | U_BOOT_CMD( |
| 89 | cbfsload, 4, 0, do_cbfs_fsload, |
| 90 | "load binary file from a cbfs filesystem", |
| 91 | "<addr> <filename> [bytes]\n" |
| 92 | " - load binary file 'filename' from the cbfs to address 'addr'\n" |
| 93 | ); |
| 94 | |
Simon Glass | 633fb73 | 2016-03-13 19:07:27 -0600 | [diff] [blame] | 95 | static int do_cbfs_ls(cmd_tbl_t *cmdtp, int flag, int argc, |
| 96 | char *const argv[]) |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 97 | { |
| 98 | const struct cbfs_cachenode *file = file_cbfs_get_first(); |
| 99 | int files = 0; |
| 100 | |
| 101 | if (!file) { |
| 102 | printf("%s.\n", file_cbfs_error()); |
| 103 | return 1; |
| 104 | } |
| 105 | |
| 106 | printf(" size type name\n"); |
| 107 | printf("------------------------------------------\n"); |
| 108 | while (file) { |
Simon Glass | a696d76 | 2016-02-29 15:25:49 -0700 | [diff] [blame] | 109 | int type = file_cbfs_type(file); |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 110 | char *type_name = NULL; |
| 111 | const char *filename = file_cbfs_name(file); |
| 112 | |
| 113 | printf(" %8d", file_cbfs_size(file)); |
| 114 | |
| 115 | switch (type) { |
Bin Meng | 881bb9a | 2018-12-22 01:55:51 -0800 | [diff] [blame] | 116 | case CBFS_TYPE_BOOTBLOCK: |
| 117 | type_name = "bootblock"; |
| 118 | break; |
| 119 | case CBFS_TYPE_CBFSHEADER: |
| 120 | type_name = "cbfs header"; |
| 121 | break; |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 122 | case CBFS_TYPE_STAGE: |
| 123 | type_name = "stage"; |
| 124 | break; |
| 125 | case CBFS_TYPE_PAYLOAD: |
| 126 | type_name = "payload"; |
| 127 | break; |
Bin Meng | 881bb9a | 2018-12-22 01:55:51 -0800 | [diff] [blame] | 128 | case CBFS_TYPE_FIT: |
| 129 | type_name = "fit"; |
| 130 | break; |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 131 | case CBFS_TYPE_OPTIONROM: |
| 132 | type_name = "option rom"; |
| 133 | break; |
| 134 | case CBFS_TYPE_BOOTSPLASH: |
| 135 | type_name = "boot splash"; |
| 136 | break; |
| 137 | case CBFS_TYPE_RAW: |
| 138 | type_name = "raw"; |
| 139 | break; |
| 140 | case CBFS_TYPE_VSA: |
| 141 | type_name = "vsa"; |
| 142 | break; |
| 143 | case CBFS_TYPE_MBI: |
| 144 | type_name = "mbi"; |
| 145 | break; |
| 146 | case CBFS_TYPE_MICROCODE: |
| 147 | type_name = "microcode"; |
| 148 | break; |
Bin Meng | 881bb9a | 2018-12-22 01:55:51 -0800 | [diff] [blame] | 149 | case CBFS_TYPE_FSP: |
| 150 | type_name = "fsp"; |
| 151 | break; |
| 152 | case CBFS_TYPE_MRC: |
| 153 | type_name = "mrc"; |
| 154 | break; |
| 155 | case CBFS_TYPE_MMA: |
| 156 | type_name = "mma"; |
| 157 | break; |
| 158 | case CBFS_TYPE_EFI: |
| 159 | type_name = "efi"; |
| 160 | break; |
| 161 | case CBFS_TYPE_STRUCT: |
| 162 | type_name = "struct"; |
| 163 | break; |
Bin Meng | 14fdf91 | 2018-12-22 01:55:50 -0800 | [diff] [blame] | 164 | case CBFS_TYPE_CMOS_DEFAULT: |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 165 | type_name = "cmos default"; |
| 166 | break; |
Bin Meng | 881bb9a | 2018-12-22 01:55:51 -0800 | [diff] [blame] | 167 | case CBFS_TYPE_SPD: |
| 168 | type_name = "spd"; |
| 169 | break; |
| 170 | case CBFS_TYPE_MRC_CACHE: |
| 171 | type_name = "mrc cache"; |
| 172 | break; |
Bin Meng | 14fdf91 | 2018-12-22 01:55:50 -0800 | [diff] [blame] | 173 | case CBFS_TYPE_CMOS_LAYOUT: |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 174 | type_name = "cmos layout"; |
| 175 | break; |
Simon Glass | a696d76 | 2016-02-29 15:25:49 -0700 | [diff] [blame] | 176 | case -1: |
| 177 | case 0: |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 178 | type_name = "null"; |
| 179 | break; |
| 180 | } |
| 181 | if (type_name) |
| 182 | printf(" %16s", type_name); |
| 183 | else |
| 184 | printf(" %16d", type); |
| 185 | |
| 186 | if (filename[0]) |
| 187 | printf(" %s\n", filename); |
| 188 | else |
| 189 | printf(" %s\n", "(empty)"); |
| 190 | file_cbfs_get_next(&file); |
| 191 | files++; |
| 192 | } |
| 193 | |
| 194 | printf("\n%d file(s)\n\n", files); |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | U_BOOT_CMD( |
| 199 | cbfsls, 1, 1, do_cbfs_ls, |
| 200 | "list files", |
| 201 | " - list the files in the cbfs\n" |
| 202 | ); |
| 203 | |
Simon Glass | 633fb73 | 2016-03-13 19:07:27 -0600 | [diff] [blame] | 204 | static int do_cbfs_fsinfo(cmd_tbl_t *cmdtp, int flag, int argc, |
| 205 | char *const argv[]) |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 206 | { |
| 207 | const struct cbfs_header *header = file_cbfs_get_header(); |
| 208 | |
| 209 | if (!header) { |
| 210 | printf("%s.\n", file_cbfs_error()); |
| 211 | return 1; |
| 212 | } |
| 213 | |
| 214 | printf("\n"); |
| 215 | printf("CBFS version: %#x\n", header->version); |
| 216 | printf("ROM size: %#x\n", header->rom_size); |
| 217 | printf("Boot block size: %#x\n", header->boot_block_size); |
| 218 | printf("CBFS size: %#x\n", |
| 219 | header->rom_size - header->boot_block_size - header->offset); |
| 220 | printf("Alignment: %d\n", header->align); |
| 221 | printf("Offset: %#x\n", header->offset); |
| 222 | printf("\n"); |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | U_BOOT_CMD( |
| 228 | cbfsinfo, 1, 1, do_cbfs_fsinfo, |
| 229 | "print information about filesystem", |
| 230 | " - print information about the cbfs filesystem\n" |
| 231 | ); |