]> git.proxmox.com Git - grub2.git/blob - commands/hexdump.c
2007-11-18 Bean <bean123ch@gmail.com>
[grub2.git] / commands / hexdump.c
1 /* hexdump.c - command to dump the contents of a file or memory */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2007 Free Software Foundation, Inc.
5 *
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <grub/normal.h>
21 #include <grub/dl.h>
22 #include <grub/arg.h>
23 #include <grub/file.h>
24 #include <grub/disk.h>
25 #include <grub/misc.h>
26 #include <grub/gzio.h>
27 #include <grub/hexdump.h>
28
29 static const struct grub_arg_option options[] = {
30 {"skip", 's', 0, "skip offset bytes from the beginning of file.", 0,
31 ARG_TYPE_INT},
32 {"length", 'n', 0, "read only length bytes", 0, ARG_TYPE_INT},
33 {0, 0, 0, 0, 0, 0}
34 };
35
36 void
37 hexdump (unsigned long bse, char *buf, int len)
38 {
39 int pos;
40 char line[80];
41
42 while (len > 0)
43 {
44 int cnt, i;
45
46 pos = grub_sprintf (line, "%08lx ", bse);
47 cnt = 16;
48 if (cnt > len)
49 cnt = len;
50
51 for (i = 0; i < cnt; i++)
52 {
53 pos += grub_sprintf (&line[pos], "%02x ", (unsigned char) buf[i]);
54 if ((i & 7) == 7)
55 line[pos++] = ' ';
56 }
57
58 for (; i < 16; i++)
59 {
60 pos += grub_sprintf (&line[pos], " ");
61 if ((i & 7) == 7)
62 line[pos++] = ' ';
63 }
64
65 line[pos++] = '|';
66
67 for (i = 0; i < cnt; i++)
68 line[pos++] = ((buf[i] >= 32) && (buf[i] < 127)) ? buf[i] : '.';
69
70 line[pos++] = '|';
71
72 line[pos] = 0;
73
74 grub_printf ("%s\n", line);
75
76 bse += 16;
77 buf += 16;
78 len -= cnt;
79 }
80 }
81
82 static grub_err_t
83 grub_cmd_hexdump (struct grub_arg_list *state, int argc, char **args)
84 {
85 grub_file_t file;
86 char buf[GRUB_DISK_SECTOR_SIZE];
87 grub_ssize_t size, length;
88 unsigned long skip;
89 int is_file;
90
91 if (argc != 1)
92 return grub_error (GRUB_ERR_BAD_ARGUMENT, "file name required");
93
94 skip = (state[0].set) ? grub_strtoul (state[0].arg, 0, 0) : 0;
95 length = (state[1].set) ? grub_strtoul (state[1].arg, 0, 0) : 0;
96
97 is_file = (grub_strcmp (args[0], "(mem)"));
98 if ((!is_file) && (!length))
99 length = 256;
100
101 if (is_file)
102 {
103 file = grub_gzfile_open (args[0], 1);
104 if (!file)
105 return 0;
106
107 file->offset = skip;
108
109 while ((size = grub_file_read (file, buf, sizeof (buf))) > 0)
110 {
111 unsigned long len;
112
113 len = ((length) && (size > length)) ? length : size;
114 hexdump (skip, buf, len);
115 skip += len;
116 if (length)
117 {
118 length -= len;
119 if (!length)
120 break;
121 }
122 }
123
124 grub_file_close (file);
125 }
126 else
127 hexdump (skip, (char *) skip, length);
128
129 return 0;
130 }
131 \f
132
133 GRUB_MOD_INIT (hexdump)
134 {
135 (void) mod; /* To stop warning. */
136 grub_register_command ("hexdump", grub_cmd_hexdump, GRUB_COMMAND_FLAG_BOTH,
137 "hexdump [ -s offset ] [-n length] { FILE | (mem) }",
138 "Dump the contents of a file or memory.", options);
139 }
140
141 GRUB_MOD_FINI (hexdump)
142 {
143 grub_unregister_command ("hexdump");
144 }