blob: 18aa6ca7aa67f3d2c4fa4af2696dfd723e790cda [file] [log] [blame]
Roger Knecht690a1d62022-09-03 11:34:53 +00001// 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
13static 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 Glass822f7a42023-06-01 10:22:39 -060020 ulong file_size;
21 int ret;
Roger Knecht690a1d62022-09-03 11:34:53 +000022
23 if (argc < 4)
24 return CMD_RET_USAGE;
25
26 ifname = argv[1];
27 dev = argv[2];
28 file = argv[3];
29
Simon Glass822f7a42023-06-01 10:22:39 -060030 ret = fs_load_alloc(ifname, dev, file, 0, 0, (void **)&buffer,
31 &file_size);
Roger Knecht690a1d62022-09-03 11:34:53 +000032
Simon Glass822f7a42023-06-01 10:22:39 -060033 // check file exists
34 switch (ret) {
35 case 0:
36 break;
37 case -ENOMEDIUM:
38 return CMD_RET_FAILURE;
39 case -ENOENT:
Roger Knecht690a1d62022-09-03 11:34:53 +000040 log_err("File does not exist: ifname=%s dev=%s file=%s\n", ifname, dev, file);
41 return CMD_RET_FAILURE;
Simon Glass822f7a42023-06-01 10:22:39 -060042 case -E2BIG:
43 log_err("File is too large: ifname=%s dev=%s file=%s\n", ifname, dev, file);
Roger Knecht690a1d62022-09-03 11:34:53 +000044 return CMD_RET_FAILURE;
Simon Glass822f7a42023-06-01 10:22:39 -060045 case -ENOMEM:
46 log_err("Not enough memory: ifname=%s dev=%s file=%s\n", ifname, dev, file);
Roger Knecht690a1d62022-09-03 11:34:53 +000047 return CMD_RET_FAILURE;
Simon Glass822f7a42023-06-01 10:22:39 -060048 default:
49 case -EIO:
50 log_err("File-read failed: ifname=%s dev=%s file=%s\n", ifname, dev, file);
Roger Knecht690a1d62022-09-03 11:34:53 +000051 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 Rini36162182023-10-07 15:13:08 -040063U_BOOT_LONGHELP(cat,
Roger Knecht690a1d62022-09-03 11:34:53 +000064 "<interface> <dev[:part]> <file>\n"
Tom Rini36162182023-10-07 15:13:08 -040065 " - Print file from 'dev' on 'interface' to standard output\n");
Roger Knecht690a1d62022-09-03 11:34:53 +000066
67U_BOOT_CMD(cat, 4, 1, do_cat,
68 "Print file to standard output",
69 cat_help_text
70);