blob: 18aa6ca7aa67f3d2c4fa4af2696dfd723e790cda [file] [log] [blame]
Roger Knecht11827c42022-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 Glass5101ea42023-06-01 10:22:39 -060020 ulong file_size;
21 int ret;
Roger Knecht11827c42022-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 Glass5101ea42023-06-01 10:22:39 -060030 ret = fs_load_alloc(ifname, dev, file, 0, 0, (void **)&buffer,
31 &file_size);
32
Roger Knecht11827c42022-09-03 11:34:53 +000033 // check file exists
Simon Glass5101ea42023-06-01 10:22:39 -060034 switch (ret) {
35 case 0:
36 break;
37 case -ENOMEDIUM:
Roger Knecht11827c42022-09-03 11:34:53 +000038 return CMD_RET_FAILURE;
Simon Glass5101ea42023-06-01 10:22:39 -060039 case -ENOENT:
Roger Knecht11827c42022-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 Glass5101ea42023-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 Knecht11827c42022-09-03 11:34:53 +000044 return CMD_RET_FAILURE;
Simon Glass5101ea42023-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 Knecht11827c42022-09-03 11:34:53 +000047 return CMD_RET_FAILURE;
Simon Glass5101ea42023-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 Knecht11827c42022-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 Rini03f146c2023-10-07 15:13:08 -040063U_BOOT_LONGHELP(cat,
Roger Knecht11827c42022-09-03 11:34:53 +000064 "<interface> <dev[:part]> <file>\n"
Tom Rini03f146c2023-10-07 15:13:08 -040065 " - Print file from 'dev' on 'interface' to standard output\n");
Roger Knecht11827c42022-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);