hexdump.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/hexdump.h>
  24. #include <grub/extcmd.h>
  25. #include <grub/i18n.h>
  26. GRUB_MOD_LICENSE ("GPLv3+");
  27. static const struct grub_arg_option options[] = {
  28. {"skip", 's', 0, N_("Skip offset bytes from the beginning of file."), 0,
  29. ARG_TYPE_INT},
  30. {"length", 'n', 0, N_("Read only LENGTH bytes."), 0, ARG_TYPE_INT},
  31. {0, 0, 0, 0, 0, 0}
  32. };
  33. static grub_err_t
  34. grub_cmd_hexdump (grub_extcmd_context_t ctxt, int argc, char **args)
  35. {
  36. struct grub_arg_list *state = ctxt->state;
  37. char buf[GRUB_DISK_SECTOR_SIZE * 4];
  38. grub_ssize_t size, length;
  39. grub_disk_addr_t skip;
  40. int namelen;
  41. if (argc != 1)
  42. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  43. namelen = grub_strlen (args[0]);
  44. skip = (state[0].set) ? grub_strtoull (state[0].arg, 0, 0) : 0;
  45. length = (state[1].set) ? grub_strtoul (state[1].arg, 0, 0) : 256;
  46. if (!grub_strcmp (args[0], "(mem)"))
  47. hexdump (skip, (char *) (grub_addr_t) skip, length);
  48. else if ((args[0][0] == '(') && (args[0][namelen - 1] == ')'))
  49. {
  50. grub_disk_t disk;
  51. grub_disk_addr_t sector;
  52. grub_size_t ofs;
  53. args[0][namelen - 1] = 0;
  54. disk = grub_disk_open (&args[0][1]);
  55. if (! disk)
  56. return 0;
  57. sector = (skip >> (GRUB_DISK_SECTOR_BITS + 2)) * 4;
  58. ofs = skip & (GRUB_DISK_SECTOR_SIZE * 4 - 1);
  59. while (length)
  60. {
  61. grub_size_t len;
  62. len = length;
  63. if (len > sizeof (buf))
  64. len = sizeof (buf);
  65. if (grub_disk_read (disk, sector, ofs, len, buf))
  66. break;
  67. hexdump (skip, buf, len);
  68. ofs = 0;
  69. skip += len;
  70. length -= len;
  71. sector += 4;
  72. }
  73. grub_disk_close (disk);
  74. }
  75. else
  76. {
  77. grub_file_t file;
  78. file = grub_file_open (args[0]);
  79. if (! file)
  80. return 0;
  81. file->offset = skip;
  82. while ((size = grub_file_read (file, buf, sizeof (buf))) > 0)
  83. {
  84. unsigned long len;
  85. len = ((length) && (size > length)) ? length : size;
  86. hexdump (skip, buf, len);
  87. skip += len;
  88. if (length)
  89. {
  90. length -= len;
  91. if (!length)
  92. break;
  93. }
  94. }
  95. grub_file_close (file);
  96. }
  97. return 0;
  98. }
  99. static grub_extcmd_t cmd;
  100. GRUB_MOD_INIT (hexdump)
  101. {
  102. cmd = grub_register_extcmd ("hexdump", grub_cmd_hexdump, 0,
  103. N_("[OPTIONS] FILE_OR_DEVICE"),
  104. N_("Show raw contents of a file or memory."),
  105. options);
  106. }
  107. GRUB_MOD_FINI (hexdump)
  108. {
  109. grub_unregister_extcmd (cmd);
  110. }