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 | |
Roger Knecht | 690a1d6 | 2022-09-03 11:34:53 +0000 | [diff] [blame] | 7 | #include <command.h> |
| 8 | #include <fs.h> |
| 9 | #include <malloc.h> |
| 10 | #include <mapmem.h> |
| 11 | |
| 12 | static int do_cat(struct cmd_tbl *cmdtp, int flag, int argc, |
| 13 | char *const argv[]) |
| 14 | { |
| 15 | char *ifname; |
| 16 | char *dev; |
| 17 | char *file; |
| 18 | char *buffer; |
Simon Glass | 822f7a4 | 2023-06-01 10:22:39 -0600 | [diff] [blame] | 19 | ulong file_size; |
| 20 | int ret; |
Roger Knecht | 690a1d6 | 2022-09-03 11:34:53 +0000 | [diff] [blame] | 21 | |
| 22 | if (argc < 4) |
| 23 | return CMD_RET_USAGE; |
| 24 | |
| 25 | ifname = argv[1]; |
| 26 | dev = argv[2]; |
| 27 | file = argv[3]; |
| 28 | |
Simon Glass | 822f7a4 | 2023-06-01 10:22:39 -0600 | [diff] [blame] | 29 | ret = fs_load_alloc(ifname, dev, file, 0, 0, (void **)&buffer, |
| 30 | &file_size); |
Roger Knecht | 690a1d6 | 2022-09-03 11:34:53 +0000 | [diff] [blame] | 31 | |
Simon Glass | 822f7a4 | 2023-06-01 10:22:39 -0600 | [diff] [blame] | 32 | // check file exists |
| 33 | switch (ret) { |
| 34 | case 0: |
| 35 | break; |
| 36 | case -ENOMEDIUM: |
| 37 | return CMD_RET_FAILURE; |
| 38 | case -ENOENT: |
Roger Knecht | 690a1d6 | 2022-09-03 11:34:53 +0000 | [diff] [blame] | 39 | log_err("File does not exist: ifname=%s dev=%s file=%s\n", ifname, dev, file); |
| 40 | return CMD_RET_FAILURE; |
Simon Glass | 822f7a4 | 2023-06-01 10:22:39 -0600 | [diff] [blame] | 41 | case -E2BIG: |
| 42 | 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] | 43 | return CMD_RET_FAILURE; |
Simon Glass | 822f7a4 | 2023-06-01 10:22:39 -0600 | [diff] [blame] | 44 | case -ENOMEM: |
| 45 | 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] | 46 | return CMD_RET_FAILURE; |
Simon Glass | 822f7a4 | 2023-06-01 10:22:39 -0600 | [diff] [blame] | 47 | default: |
| 48 | case -EIO: |
| 49 | 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] | 50 | return CMD_RET_FAILURE; |
| 51 | } |
| 52 | |
| 53 | // print file content |
| 54 | buffer[file_size] = '\0'; |
| 55 | puts(buffer); |
| 56 | |
| 57 | free(buffer); |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
Tom Rini | 3616218 | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 62 | U_BOOT_LONGHELP(cat, |
Roger Knecht | 690a1d6 | 2022-09-03 11:34:53 +0000 | [diff] [blame] | 63 | "<interface> <dev[:part]> <file>\n" |
Tom Rini | 3616218 | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 64 | " - Print file from 'dev' on 'interface' to standard output\n"); |
Roger Knecht | 690a1d6 | 2022-09-03 11:34:53 +0000 | [diff] [blame] | 65 | |
| 66 | U_BOOT_CMD(cat, 4, 1, do_cat, |
| 67 | "Print file to standard output", |
| 68 | cat_help_text |
| 69 | ); |