Roger Knecht | 11827c4 | 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 | 5101ea4 | 2023-06-01 10:22:39 -0600 | [diff] [blame] | 20 | ulong file_size; |
| 21 | int ret; |
Roger Knecht | 11827c4 | 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 | 5101ea4 | 2023-06-01 10:22:39 -0600 | [diff] [blame] | 30 | ret = fs_load_alloc(ifname, dev, file, 0, 0, (void **)&buffer, |
| 31 | &file_size); |
| 32 | |
Roger Knecht | 11827c4 | 2022-09-03 11:34:53 +0000 | [diff] [blame] | 33 | // check file exists |
Simon Glass | 5101ea4 | 2023-06-01 10:22:39 -0600 | [diff] [blame] | 34 | switch (ret) { |
| 35 | case 0: |
| 36 | break; |
| 37 | case -ENOMEDIUM: |
Roger Knecht | 11827c4 | 2022-09-03 11:34:53 +0000 | [diff] [blame] | 38 | return CMD_RET_FAILURE; |
Simon Glass | 5101ea4 | 2023-06-01 10:22:39 -0600 | [diff] [blame] | 39 | case -ENOENT: |
Roger Knecht | 11827c4 | 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 | 5101ea4 | 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 | 11827c4 | 2022-09-03 11:34:53 +0000 | [diff] [blame] | 44 | return CMD_RET_FAILURE; |
Simon Glass | 5101ea4 | 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 | 11827c4 | 2022-09-03 11:34:53 +0000 | [diff] [blame] | 47 | return CMD_RET_FAILURE; |
Simon Glass | 5101ea4 | 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 | 11827c4 | 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 | |
Tom Rini | 03f146c | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 63 | U_BOOT_LONGHELP(cat, |
Roger Knecht | 11827c4 | 2022-09-03 11:34:53 +0000 | [diff] [blame] | 64 | "<interface> <dev[:part]> <file>\n" |
Tom Rini | 03f146c | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 65 | " - Print file from 'dev' on 'interface' to standard output\n"); |
Roger Knecht | 11827c4 | 2022-09-03 11:34:53 +0000 | [diff] [blame] | 66 | |
| 67 | U_BOOT_CMD(cat, 4, 1, do_cat, |
| 68 | "Print file to standard output", |
| 69 | cat_help_text |
| 70 | ); |