Merge branch '2024-11-22-assorted-fixes'
[u-boot.git] / cmd / cat.c
blob6828b7b364e5a2ad4269d347d034aed915d015da
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright 2022
4 * Roger Knecht <rknecht@pm.de>
5 */
7 #include <command.h>
8 #include <fs.h>
9 #include <malloc.h>
10 #include <mapmem.h>
12 static int do_cat(struct cmd_tbl *cmdtp, int flag, int argc,
13 char *const argv[])
15 char *ifname;
16 char *dev;
17 char *file;
18 char *buffer;
19 ulong file_size;
20 int ret;
22 if (argc < 4)
23 return CMD_RET_USAGE;
25 ifname = argv[1];
26 dev = argv[2];
27 file = argv[3];
29 ret = fs_load_alloc(ifname, dev, file, 0, 0, (void **)&buffer,
30 &file_size);
32 // check file exists
33 switch (ret) {
34 case 0:
35 break;
36 case -ENOMEDIUM:
37 return CMD_RET_FAILURE;
38 case -ENOENT:
39 log_err("File does not exist: ifname=%s dev=%s file=%s\n", ifname, dev, file);
40 return CMD_RET_FAILURE;
41 case -E2BIG:
42 log_err("File is too large: ifname=%s dev=%s file=%s\n", ifname, dev, file);
43 return CMD_RET_FAILURE;
44 case -ENOMEM:
45 log_err("Not enough memory: ifname=%s dev=%s file=%s\n", ifname, dev, file);
46 return CMD_RET_FAILURE;
47 default:
48 case -EIO:
49 log_err("File-read failed: ifname=%s dev=%s file=%s\n", ifname, dev, file);
50 return CMD_RET_FAILURE;
53 // print file content
54 buffer[file_size] = '\0';
55 puts(buffer);
57 free(buffer);
59 return 0;
62 U_BOOT_LONGHELP(cat,
63 "<interface> <dev[:part]> <file>\n"
64 " - Print file from 'dev' on 'interface' to standard output\n");
66 U_BOOT_CMD(cat, 4, 1, do_cat,
67 "Print file to standard output",
68 cat_help_text