blob: 742a85c7a933566ce7571b18b0048e56fa974bab [file] [log] [blame]
Roger Knecht23c0df62022-09-03 13:15:04 +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 <display_options.h>
10#include <fs.h>
11#include <malloc.h>
12#include <mapmem.h>
13
14static int do_xxd(struct cmd_tbl *cmdtp, int flag, int argc,
15 char *const argv[])
16{
17 char *ifname;
18 char *dev;
19 char *file;
20 char *buffer;
21 phys_addr_t addr;
22 loff_t file_size;
23
24 if (argc < 4)
25 return CMD_RET_USAGE;
26
27 ifname = argv[1];
28 dev = argv[2];
29 file = argv[3];
30
31 // check file exists
32 if (fs_set_blk_dev(ifname, dev, FS_TYPE_ANY))
33 return CMD_RET_FAILURE;
34
35 if (!fs_exists(file)) {
36 log_err("File does not exist: ifname=%s dev=%s file=%s\n", ifname, dev, file);
37 return CMD_RET_FAILURE;
38 }
39
40 // get file size
41 if (fs_set_blk_dev(ifname, dev, FS_TYPE_ANY))
42 return CMD_RET_FAILURE;
43
44 if (fs_size(file, &file_size)) {
45 log_err("Cannot read file size: ifname=%s dev=%s file=%s\n", ifname, dev, file);
46 return CMD_RET_FAILURE;
47 }
48
49 // allocate memory for file content
50 buffer = calloc(sizeof(char), file_size);
51 if (!buffer) {
52 log_err("Out of memory\n");
53 return CMD_RET_FAILURE;
54 }
55
56 // map pointer to system memory
57 addr = map_to_sysmem(buffer);
58
59 // read file to memory
60 if (fs_set_blk_dev(ifname, dev, FS_TYPE_ANY))
61 return CMD_RET_FAILURE;
62
63 if (fs_read(file, addr, 0, 0, &file_size)) {
64 log_err("Cannot read file: ifname=%s dev=%s file=%s\n", ifname, dev, file);
65 return CMD_RET_FAILURE;
66 }
67
68 // print file content
69 print_buffer(0, buffer, sizeof(char), file_size, 0);
70
71 free(buffer);
72
73 return 0;
74}
75
76#ifdef CONFIG_SYS_LONGHELP
77static char xxd_help_text[] =
78 "<interface> <dev[:part]> <file>\n"
79 " - Print file from 'dev' on 'interface' as hexdump to standard output\n";
80#endif
81
82U_BOOT_CMD(xxd, 4, 1, do_xxd,
83 "Print file as hexdump to standard output",
84 xxd_help_text
85);