blob: 1273a26b145765a7b0e7f2a0adbcb79fea2b799c [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;
20 phys_addr_t addr;
21 loff_t file_size;
22
23 if (argc < 4)
24 return CMD_RET_USAGE;
25
26 ifname = argv[1];
27 dev = argv[2];
28 file = argv[3];
29
30 // check file exists
31 if (fs_set_blk_dev(ifname, dev, FS_TYPE_ANY))
32 return CMD_RET_FAILURE;
33
34 if (!fs_exists(file)) {
35 log_err("File does not exist: ifname=%s dev=%s file=%s\n", ifname, dev, file);
36 return CMD_RET_FAILURE;
37 }
38
39 // get file size
40 if (fs_set_blk_dev(ifname, dev, FS_TYPE_ANY))
41 return CMD_RET_FAILURE;
42
43 if (fs_size(file, &file_size)) {
44 log_err("Cannot read file size: ifname=%s dev=%s file=%s\n", ifname, dev, file);
45 return CMD_RET_FAILURE;
46 }
47
48 // allocate memory for file content
49 buffer = calloc(sizeof(char), file_size + 1);
50 if (!buffer) {
51 log_err("Out of memory\n");
52 return CMD_RET_FAILURE;
53 }
54
55 // map pointer to system memory
56 addr = map_to_sysmem(buffer);
57
58 // read file to memory
59 if (fs_set_blk_dev(ifname, dev, FS_TYPE_ANY))
60 return CMD_RET_FAILURE;
61
62 if (fs_read(file, addr, 0, 0, &file_size)) {
63 log_err("Cannot read file: ifname=%s dev=%s file=%s\n", ifname, dev, file);
64 return CMD_RET_FAILURE;
65 }
66
67 // print file content
68 buffer[file_size] = '\0';
69 puts(buffer);
70
71 free(buffer);
72
73 return 0;
74}
75
76#ifdef CONFIG_SYS_LONGHELP
77static char cat_help_text[] =
78 "<interface> <dev[:part]> <file>\n"
79 " - Print file from 'dev' on 'interface' to standard output\n";
80#endif
81
82U_BOOT_CMD(cat, 4, 1, do_cat,
83 "Print file to standard output",
84 cat_help_text
85);