2022-09-03 11:34:53 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
|
|
|
/*
|
|
|
|
* Copyright 2022
|
|
|
|
* Roger Knecht <rknecht@pm.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <command.h>
|
|
|
|
#include <fs.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <mapmem.h>
|
|
|
|
|
|
|
|
static int do_cat(struct cmd_tbl *cmdtp, int flag, int argc,
|
|
|
|
char *const argv[])
|
|
|
|
{
|
|
|
|
char *ifname;
|
|
|
|
char *dev;
|
|
|
|
char *file;
|
|
|
|
char *buffer;
|
2023-06-01 16:22:39 +00:00
|
|
|
ulong file_size;
|
|
|
|
int ret;
|
2022-09-03 11:34:53 +00:00
|
|
|
|
|
|
|
if (argc < 4)
|
|
|
|
return CMD_RET_USAGE;
|
|
|
|
|
|
|
|
ifname = argv[1];
|
|
|
|
dev = argv[2];
|
|
|
|
file = argv[3];
|
|
|
|
|
2023-06-01 16:22:39 +00:00
|
|
|
ret = fs_load_alloc(ifname, dev, file, 0, 0, (void **)&buffer,
|
|
|
|
&file_size);
|
|
|
|
|
2022-09-03 11:34:53 +00:00
|
|
|
// check file exists
|
2023-06-01 16:22:39 +00:00
|
|
|
switch (ret) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case -ENOMEDIUM:
|
2022-09-03 11:34:53 +00:00
|
|
|
return CMD_RET_FAILURE;
|
2023-06-01 16:22:39 +00:00
|
|
|
case -ENOENT:
|
2022-09-03 11:34:53 +00:00
|
|
|
log_err("File does not exist: ifname=%s dev=%s file=%s\n", ifname, dev, file);
|
|
|
|
return CMD_RET_FAILURE;
|
2023-06-01 16:22:39 +00:00
|
|
|
case -E2BIG:
|
|
|
|
log_err("File is too large: ifname=%s dev=%s file=%s\n", ifname, dev, file);
|
2022-09-03 11:34:53 +00:00
|
|
|
return CMD_RET_FAILURE;
|
2023-06-01 16:22:39 +00:00
|
|
|
case -ENOMEM:
|
|
|
|
log_err("Not enough memory: ifname=%s dev=%s file=%s\n", ifname, dev, file);
|
2022-09-03 11:34:53 +00:00
|
|
|
return CMD_RET_FAILURE;
|
2023-06-01 16:22:39 +00:00
|
|
|
default:
|
|
|
|
case -EIO:
|
|
|
|
log_err("File-read failed: ifname=%s dev=%s file=%s\n", ifname, dev, file);
|
2022-09-03 11:34:53 +00:00
|
|
|
return CMD_RET_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// print file content
|
|
|
|
buffer[file_size] = '\0';
|
|
|
|
puts(buffer);
|
|
|
|
|
|
|
|
free(buffer);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-10-07 19:13:08 +00:00
|
|
|
U_BOOT_LONGHELP(cat,
|
2022-09-03 11:34:53 +00:00
|
|
|
"<interface> <dev[:part]> <file>\n"
|
2023-10-07 19:13:08 +00:00
|
|
|
" - Print file from 'dev' on 'interface' to standard output\n");
|
2022-09-03 11:34:53 +00:00
|
|
|
|
|
|
|
U_BOOT_CMD(cat, 4, 1, do_cat,
|
|
|
|
"Print file to standard output",
|
|
|
|
cat_help_text
|
|
|
|
);
|