hexdump.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* hexdump.c - command to dump the contents of a file or memory */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2007,2008,2009 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. #include <grub/dl.h>
  20. #include <grub/file.h>
  21. #include <grub/disk.h>
  22. #include <grub/misc.h>
  23. #include <grub/lib.h>
  24. #include <grub/extcmd.h>
  25. #include <grub/i18n.h>
  26. static const struct grub_arg_option options[] = {
  27. {"skip", 's', 0, N_("Skip offset bytes from the beginning of file."), "NUM",
  28. ARG_TYPE_INT},
  29. {"length", 'n', 0, N_("Read only LENGTH bytes."), "NUM", ARG_TYPE_INT},
  30. {0, 0, 0, 0, 0, 0}
  31. };
  32. static grub_err_t
  33. grub_cmd_hexdump (grub_extcmd_t cmd, int argc, char **args)
  34. {
  35. struct grub_arg_list *state = cmd->state;
  36. char buf[GRUB_DISK_SECTOR_SIZE * 4];
  37. grub_ssize_t size, length;
  38. grub_disk_addr_t skip;
  39. int namelen;
  40. if (argc != 1)
  41. return grub_error (GRUB_ERR_BAD_ARGUMENT, "file name required");
  42. namelen = grub_strlen (args[0]);
  43. skip = (state[0].set) ? grub_strtoull (state[0].arg, 0, 0) : 0;
  44. length = (state[1].set) ? grub_strtoul (state[1].arg, 0, 0) : 256;
  45. if (!grub_strcmp (args[0], "(mem)"))
  46. hexdump (skip, (char *) (grub_addr_t) skip, length);
  47. else
  48. {
  49. grub_file_t file;
  50. file = grub_file_open (args[0]);
  51. if (! file)
  52. return 0;
  53. file->flags = 1;
  54. file->offset = skip;
  55. while ((size = grub_file_read (file, buf, sizeof (buf))) > 0)
  56. {
  57. unsigned long len;
  58. len = ((length) && (size > length)) ? length : size;
  59. hexdump (skip, buf, len);
  60. skip += len;
  61. if (length)
  62. {
  63. length -= len;
  64. if (!length)
  65. break;
  66. }
  67. }
  68. grub_file_close (file);
  69. }
  70. return 0;
  71. }
  72. static grub_extcmd_t cmd;
  73. GRUB_MOD_INIT (hexdump)
  74. {
  75. cmd = grub_register_extcmd ("hexdump", grub_cmd_hexdump,
  76. GRUB_COMMAND_FLAG_BOTH,
  77. N_("[OPTIONS] FILE_OR_DEVICE"),
  78. N_("Dump the contents of a file or memory."),
  79. options);
  80. }
  81. GRUB_MOD_FINI (hexdump)
  82. {
  83. grub_unregister_extcmd (cmd);
  84. }