]>
git.proxmox.com Git - grub2.git/blob - commands/hexdump.c
1 /* hexdump.c - command to dump the contents of a file or memory */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2007 Free Software Foundation, Inc.
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.
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.
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/>.
20 #include <grub/normal.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>
29 static const struct grub_arg_option options
[] = {
30 {"skip", 's', 0, "skip offset bytes from the beginning of file.", 0,
32 {"length", 'n', 0, "read only length bytes", 0, ARG_TYPE_INT
},
37 hexdump (unsigned long bse
, char *buf
, int len
)
46 pos
= grub_sprintf (line
, "%08lx ", bse
);
51 for (i
= 0; i
< cnt
; i
++)
53 pos
+= grub_sprintf (&line
[pos
], "%02x ", (unsigned char) buf
[i
]);
60 pos
+= grub_sprintf (&line
[pos
], " ");
67 for (i
= 0; i
< cnt
; i
++)
68 line
[pos
++] = ((buf
[i
] >= 32) && (buf
[i
] < 127)) ? buf
[i
] : '.';
74 grub_printf ("%s\n", line
);
83 grub_cmd_hexdump (struct grub_arg_list
*state
, int argc
, char **args
)
86 char buf
[GRUB_DISK_SECTOR_SIZE
];
87 grub_ssize_t size
, length
;
92 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "file name required");
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;
97 is_file
= (grub_strcmp (args
[0], "(mem)"));
98 if ((!is_file
) && (!length
))
103 file
= grub_gzfile_open (args
[0], 1);
109 while ((size
= grub_file_read (file
, buf
, sizeof (buf
))) > 0)
113 len
= ((length
) && (size
> length
)) ? length
: size
;
114 hexdump (skip
, buf
, len
);
124 grub_file_close (file
);
127 hexdump (skip
, (char *) skip
, length
);
133 GRUB_MOD_INIT (hexdump
)
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
);
141 GRUB_MOD_FINI (hexdump
)
143 grub_unregister_command ("hexdump");