blob: 6828b7b364e5a2ad4269d347d034aed915d015da [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
Roger Knecht690a1d62022-09-03 11:34:53 +00007#include <command.h>
8#include <fs.h>
9#include <malloc.h>
10#include <mapmem.h>
11
12static 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 Glass822f7a42023-06-01 10:22:39 -060019 ulong file_size;
20 int ret;
Roger Knecht690a1d62022-09-03 11:34:53 +000021
22 if (argc < 4)
23 return CMD_RET_USAGE;
24
25 ifname = argv[1];
26 dev = argv[2];
27 file = argv[3];
28
Simon Glass822f7a42023-06-01 10:22:39 -060029 ret = fs_load_alloc(ifname, dev, file, 0, 0, (void **)&buffer,
30 &file_size);
Roger Knecht690a1d62022-09-03 11:34:53 +000031
Simon Glass822f7a42023-06-01 10:22:39 -060032 // check file exists
33 switch (ret) {
34 case 0:
35 break;
36 case -ENOMEDIUM:
37 return CMD_RET_FAILURE;
38 case -ENOENT:
Roger Knecht690a1d62022-09-03 11:34:53 +000039 log_err("File does not exist: ifname=%s dev=%s file=%s\n", ifname, dev, file);
40 return CMD_RET_FAILURE;
Simon Glass822f7a42023-06-01 10:22:39 -060041 case -E2BIG:
42 log_err("File is too large: ifname=%s dev=%s file=%s\n", ifname, dev, file);
Roger Knecht690a1d62022-09-03 11:34:53 +000043 return CMD_RET_FAILURE;
Simon Glass822f7a42023-06-01 10:22:39 -060044 case -ENOMEM:
45 log_err("Not enough memory: ifname=%s dev=%s file=%s\n", ifname, dev, file);
Roger Knecht690a1d62022-09-03 11:34:53 +000046 return CMD_RET_FAILURE;
Simon Glass822f7a42023-06-01 10:22:39 -060047 default:
48 case -EIO:
49 log_err("File-read failed: ifname=%s dev=%s file=%s\n", ifname, dev, file);
Roger Knecht690a1d62022-09-03 11:34:53 +000050 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 Rini36162182023-10-07 15:13:08 -040062U_BOOT_LONGHELP(cat,
Roger Knecht690a1d62022-09-03 11:34:53 +000063 "<interface> <dev[:part]> <file>\n"
Tom Rini36162182023-10-07 15:13:08 -040064 " - Print file from 'dev' on 'interface' to standard output\n");
Roger Knecht690a1d62022-09-03 11:34:53 +000065
66U_BOOT_CMD(cat, 4, 1, do_cat,
67 "Print file to standard output",
68 cat_help_text
69);