2N/A/* hexdump.c - command to dump the contents of a file or memory */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2007,2008,2009 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/dl.h>
2N/A#include <grub/file.h>
2N/A#include <grub/disk.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/lib/hexdump.h>
2N/A#include <grub/extcmd.h>
2N/A#include <grub/i18n.h>
2N/A
2N/AGRUB_MOD_LICENSE ("GPLv3+");
2N/A
2N/Astatic const struct grub_arg_option options[] = {
2N/A {"skip", 's', 0, N_("Skip offset bytes from the beginning of file."), 0,
2N/A ARG_TYPE_INT},
2N/A {"length", 'n', 0, N_("Read only LENGTH bytes."), 0, ARG_TYPE_INT},
2N/A {0, 0, 0, 0, 0, 0}
2N/A};
2N/A
2N/Astatic grub_err_t
2N/Agrub_cmd_hexdump (grub_extcmd_context_t ctxt, int argc, char **args)
2N/A{
2N/A struct grub_arg_list *state = ctxt->state;
2N/A char buf[GRUB_DISK_SECTOR_SIZE * 4];
2N/A grub_ssize_t size, length;
2N/A grub_disk_addr_t skip;
2N/A int namelen;
2N/A
2N/A if (argc != 1)
2N/A return grub_error (GRUB_ERR_BAD_ARGUMENT, "file name required");
2N/A
2N/A namelen = grub_strlen (args[0]);
2N/A skip = (state[0].set) ? grub_strtoull (state[0].arg, 0, 0) : 0;
2N/A length = (state[1].set) ? grub_strtoul (state[1].arg, 0, 0) : 256;
2N/A
2N/A if (!grub_strcmp (args[0], "(mem)"))
2N/A hexdump (skip, (char *) (grub_addr_t) skip, length);
2N/A else if ((args[0][0] == '(') && (args[0][namelen - 1] == ')'))
2N/A {
2N/A grub_disk_t disk;
2N/A grub_disk_addr_t sector;
2N/A grub_size_t ofs;
2N/A
2N/A args[0][namelen - 1] = 0;
2N/A disk = grub_disk_open (&args[0][1]);
2N/A if (! disk)
2N/A return 0;
2N/A
2N/A sector = (skip >> (GRUB_DISK_SECTOR_BITS + 2)) * 4;
2N/A ofs = skip & (GRUB_DISK_SECTOR_SIZE * 4 - 1);
2N/A while (length)
2N/A {
2N/A grub_size_t len;
2N/A
2N/A len = length;
2N/A if (len > sizeof (buf))
2N/A len = sizeof (buf);
2N/A
2N/A if (grub_disk_read (disk, sector, ofs, len, buf))
2N/A break;
2N/A
2N/A hexdump (skip, buf, len);
2N/A
2N/A ofs = 0;
2N/A skip += len;
2N/A length -= len;
2N/A sector += 4;
2N/A }
2N/A
2N/A grub_disk_close (disk);
2N/A }
2N/A else
2N/A {
2N/A grub_file_t file;
2N/A
2N/A file = grub_file_open (args[0]);
2N/A if (! file)
2N/A return 0;
2N/A
2N/A file->offset = skip;
2N/A
2N/A while ((size = grub_file_read (file, buf, sizeof (buf))) > 0)
2N/A {
2N/A unsigned long len;
2N/A
2N/A len = ((length) && (size > length)) ? length : size;
2N/A hexdump (skip, buf, len);
2N/A skip += len;
2N/A if (length)
2N/A {
2N/A length -= len;
2N/A if (!length)
2N/A break;
2N/A }
2N/A }
2N/A
2N/A grub_file_close (file);
2N/A }
2N/A
2N/A return 0;
2N/A}
2N/A
2N/Astatic grub_extcmd_t cmd;
2N/A
2N/AGRUB_MOD_INIT (hexdump)
2N/A{
2N/A cmd = grub_register_extcmd ("hexdump", grub_cmd_hexdump, 0,
2N/A N_("[OPTIONS] FILE_OR_DEVICE"),
2N/A N_("Dump the contents of a file or memory."),
2N/A options);
2N/A}
2N/A
2N/AGRUB_MOD_FINI (hexdump)
2N/A{
2N/A grub_unregister_extcmd (cmd);
2N/A}