]> Git Repo - u-boot.git/blob - cmd/xxd.c
Merge branch 'misc' of https://source.denx.de/u-boot/custodians/u-boot-tegra
[u-boot.git] / cmd / xxd.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2022
4  * Roger Knecht <[email protected]>
5  */
6
7 #include <command.h>
8 #include <display_options.h>
9 #include <fs.h>
10 #include <malloc.h>
11 #include <mapmem.h>
12
13 static int do_xxd(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);
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         print_buffer(0, buffer, sizeof(char), file_size, 0);
69
70         free(buffer);
71
72         return 0;
73 }
74
75 U_BOOT_LONGHELP(xxd,
76         "<interface> <dev[:part]> <file>\n"
77         "  - Print file from 'dev' on 'interface' as hexdump to standard output\n");
78
79 U_BOOT_CMD(xxd, 4, 1, do_xxd,
80            "Print file as hexdump to standard output",
81            xxd_help_text
82 );
This page took 0.030964 seconds and 4 git commands to generate.