Roger Knecht | 690a1d6 | 2022-09-03 11:34:53 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2022 |
| 4 | * Roger Knecht <rknecht@pm.de> |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 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; |
Simon Glass | 822f7a4 | 2023-06-01 10:22:39 -0600 | [diff] [blame] | 20 | ulong file_size; |
| 21 | int ret; |
Roger Knecht | 690a1d6 | 2022-09-03 11:34:53 +0000 | [diff] [blame] | 22 | |
| 23 | if (argc < 4) |
| 24 | return CMD_RET_USAGE; |
| 25 | |
| 26 | ifname = argv[1]; |
| 27 | dev = argv[2]; |
| 28 | file = argv[3]; |
| 29 | |
Simon Glass | 822f7a4 | 2023-06-01 10:22:39 -0600 | [diff] [blame] | 30 | ret = fs_load_alloc(ifname, dev, file, 0, 0, (void **)&buffer, |
| 31 | &file_size); |
Roger Knecht | 690a1d6 | 2022-09-03 11:34:53 +0000 | [diff] [blame] | 32 | |
Simon Glass | 822f7a4 | 2023-06-01 10:22:39 -0600 | [diff] [blame] | 33 | // check file exists |
| 34 | switch (ret) { |
| 35 | case 0: |
| 36 | break; |
| 37 | case -ENOMEDIUM: |
| 38 | return CMD_RET_FAILURE; |
| 39 | case -ENOENT: |
Roger Knecht | 690a1d6 | 2022-09-03 11:34:53 +0000 | [diff] [blame] | 40 | log_err("File does not exist: ifname=%s dev=%s file=%s\n", ifname, dev, file); |
| 41 | return CMD_RET_FAILURE; |
Simon Glass | 822f7a4 | 2023-06-01 10:22:39 -0600 | [diff] [blame] | 42 | case -E2BIG: |
| 43 | log_err("File is too large: ifname=%s dev=%s file=%s\n", ifname, dev, file); |
Roger Knecht | 690a1d6 | 2022-09-03 11:34:53 +0000 | [diff] [blame] | 44 | return CMD_RET_FAILURE; |
Simon Glass | 822f7a4 | 2023-06-01 10:22:39 -0600 | [diff] [blame] | 45 | case -ENOMEM: |
| 46 | log_err("Not enough memory: ifname=%s dev=%s file=%s\n", ifname, dev, file); |
Roger Knecht | 690a1d6 | 2022-09-03 11:34:53 +0000 | [diff] [blame] | 47 | return CMD_RET_FAILURE; |
Simon Glass | 822f7a4 | 2023-06-01 10:22:39 -0600 | [diff] [blame] | 48 | default: |
| 49 | case -EIO: |
| 50 | log_err("File-read failed: ifname=%s dev=%s file=%s\n", ifname, dev, file); |
Roger Knecht | 690a1d6 | 2022-09-03 11:34:53 +0000 | [diff] [blame] | 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 | |
| 63 | #ifdef CONFIG_SYS_LONGHELP |
| 64 | static char cat_help_text[] = |
| 65 | "<interface> <dev[:part]> <file>\n" |
| 66 | " - Print file from 'dev' on 'interface' to standard output\n"; |
| 67 | #endif |
| 68 | |
| 69 | U_BOOT_CMD(cat, 4, 1, do_cat, |
| 70 | "Print file to standard output", |
| 71 | cat_help_text |
| 72 | ); |