blob: 6828b7b364e5a2ad4269d347d034aed915d015da [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
Roger Knecht11827c42022-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 Glass5101ea42023-06-01 10:22:39 -060019 ulong file_size;
20 int ret;
Roger Knecht11827c42022-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 Glass5101ea42023-06-01 10:22:39 -060029 ret = fs_load_alloc(ifname, dev, file, 0, 0, (void **)&buffer,
30 &file_size);
31
Roger Knecht11827c42022-09-03 11:34:53 +000032 // check file exists
Simon Glass5101ea42023-06-01 10:22:39 -060033 switch (ret) {
34 case 0:
35 break;
36 case -ENOMEDIUM:
Roger Knecht11827c42022-09-03 11:34:53 +000037 return CMD_RET_FAILURE;
Simon Glass5101ea42023-06-01 10:22:39 -060038 case -ENOENT:
Roger Knecht11827c42022-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 Glass5101ea42023-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 Knecht11827c42022-09-03 11:34:53 +000043 return CMD_RET_FAILURE;
Simon Glass5101ea42023-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 Knecht11827c42022-09-03 11:34:53 +000046 return CMD_RET_FAILURE;
Simon Glass5101ea42023-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 Knecht11827c42022-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 Rini03f146c2023-10-07 15:13:08 -040062U_BOOT_LONGHELP(cat,
Roger Knecht11827c42022-09-03 11:34:53 +000063 "<interface> <dev[:part]> <file>\n"
Tom Rini03f146c2023-10-07 15:13:08 -040064 " - Print file from 'dev' on 'interface' to standard output\n");
Roger Knecht11827c42022-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);