]>
Commit | Line | Data |
---|---|---|
690a1d69 RK |
1 | // SPDX-License-Identifier: GPL-2.0+ |
2 | /* | |
3 | * Copyright 2022 | |
4 | * Roger Knecht <[email protected]> | |
5 | */ | |
6 | ||
d678a59d | 7 | #include <common.h> |
690a1d69 RK |
8 | #include <command.h> |
9 | #include <fs.h> | |
10 | #include <malloc.h> | |
11 | #include <mapmem.h> | |
12 | ||
13 | static int do_cat(struct cmd_tbl *cmdtp, int flag, int argc, | |
14 | char *const argv[]) | |
15 | { | |
16 | char *ifname; | |
17 | char *dev; | |
18 | char *file; | |
19 | char *buffer; | |
822f7a45 SG |
20 | ulong file_size; |
21 | int ret; | |
690a1d69 RK |
22 | |
23 | if (argc < 4) | |
24 | return CMD_RET_USAGE; | |
25 | ||
26 | ifname = argv[1]; | |
27 | dev = argv[2]; | |
28 | file = argv[3]; | |
29 | ||
822f7a45 SG |
30 | ret = fs_load_alloc(ifname, dev, file, 0, 0, (void **)&buffer, |
31 | &file_size); | |
32 | ||
690a1d69 | 33 | // check file exists |
822f7a45 SG |
34 | switch (ret) { |
35 | case 0: | |
36 | break; | |
37 | case -ENOMEDIUM: | |
690a1d69 | 38 | return CMD_RET_FAILURE; |
822f7a45 | 39 | case -ENOENT: |
690a1d69 RK |
40 | log_err("File does not exist: ifname=%s dev=%s file=%s\n", ifname, dev, file); |
41 | return CMD_RET_FAILURE; | |
822f7a45 SG |
42 | case -E2BIG: |
43 | log_err("File is too large: ifname=%s dev=%s file=%s\n", ifname, dev, file); | |
690a1d69 | 44 | return CMD_RET_FAILURE; |
822f7a45 SG |
45 | case -ENOMEM: |
46 | log_err("Not enough memory: ifname=%s dev=%s file=%s\n", ifname, dev, file); | |
690a1d69 | 47 | return CMD_RET_FAILURE; |
822f7a45 SG |
48 | default: |
49 | case -EIO: | |
50 | log_err("File-read failed: ifname=%s dev=%s file=%s\n", ifname, dev, file); | |
690a1d69 RK |
51 | return CMD_RET_FAILURE; |
52 | } | |
53 | ||
54 | // print file content | |
55 | buffer[file_size] = '\0'; | |
56 | puts(buffer); | |
57 | ||
58 | free(buffer); | |
59 | ||
60 | return 0; | |
61 | } | |
62 | ||
3616218b | 63 | U_BOOT_LONGHELP(cat, |
690a1d69 | 64 | "<interface> <dev[:part]> <file>\n" |
3616218b | 65 | " - Print file from 'dev' on 'interface' to standard output\n"); |
690a1d69 RK |
66 | |
67 | U_BOOT_CMD(cat, 4, 1, do_cat, | |
68 | "Print file to standard output", | |
69 | cat_help_text | |
70 | ); |