testload.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* testload.c - load the same file in multiple ways */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2003,2005,2006,2007,2009,2010 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/mm.h>
  21. #include <grub/err.h>
  22. #include <grub/env.h>
  23. #include <grub/misc.h>
  24. #include <grub/file.h>
  25. #include <grub/disk.h>
  26. #include <grub/term.h>
  27. #include <grub/loader.h>
  28. #include <grub/command.h>
  29. #include <grub/i18n.h>
  30. GRUB_MOD_LICENSE ("GPLv3+");
  31. static grub_err_t
  32. grub_cmd_testload (struct grub_command *cmd __attribute__ ((unused)),
  33. int argc, char *argv[])
  34. {
  35. grub_file_t file;
  36. char *buf;
  37. grub_size_t size;
  38. grub_off_t pos;
  39. auto void NESTED_FUNC_ATTR read_func (grub_disk_addr_t sector, unsigned offset, unsigned len);
  40. void NESTED_FUNC_ATTR read_func (grub_disk_addr_t sector __attribute__ ((unused)),
  41. unsigned offset __attribute__ ((unused)),
  42. unsigned len __attribute__ ((unused)))
  43. {
  44. grub_xputs (".");
  45. grub_refresh ();
  46. }
  47. if (argc < 1)
  48. return grub_error (GRUB_ERR_BAD_ARGUMENT, "no file specified");
  49. file = grub_file_open (argv[0]);
  50. if (! file)
  51. return grub_errno;
  52. size = grub_file_size (file) & ~(GRUB_DISK_SECTOR_SIZE - 1);
  53. if (size == 0)
  54. {
  55. grub_file_close (file);
  56. return GRUB_ERR_NONE;
  57. }
  58. buf = grub_malloc (size);
  59. if (! buf)
  60. goto fail;
  61. grub_printf ("Reading %s sequentially", argv[0]);
  62. file->read_hook = read_func;
  63. if (grub_file_read (file, buf, size) != (grub_ssize_t) size)
  64. goto fail;
  65. grub_printf (" Done.\n");
  66. /* Read sequentially again. */
  67. grub_printf ("Reading %s sequentially again", argv[0]);
  68. grub_file_seek (file, 0);
  69. for (pos = 0; pos < size; pos += GRUB_DISK_SECTOR_SIZE)
  70. {
  71. char sector[GRUB_DISK_SECTOR_SIZE];
  72. if (grub_file_read (file, sector, GRUB_DISK_SECTOR_SIZE)
  73. != GRUB_DISK_SECTOR_SIZE)
  74. goto fail;
  75. if (grub_memcmp (sector, buf + pos, GRUB_DISK_SECTOR_SIZE) != 0)
  76. {
  77. grub_printf ("\nDiffers in %lld\n", (unsigned long long) pos);
  78. goto fail;
  79. }
  80. }
  81. grub_printf (" Done.\n");
  82. /* Read backwards and compare. */
  83. grub_printf ("Reading %s backwards", argv[0]);
  84. pos = size;
  85. while (pos > 0)
  86. {
  87. char sector[GRUB_DISK_SECTOR_SIZE];
  88. pos -= GRUB_DISK_SECTOR_SIZE;
  89. grub_file_seek (file, pos);
  90. if (grub_file_read (file, sector, GRUB_DISK_SECTOR_SIZE)
  91. != GRUB_DISK_SECTOR_SIZE)
  92. goto fail;
  93. if (grub_memcmp (sector, buf + pos, GRUB_DISK_SECTOR_SIZE) != 0)
  94. {
  95. int i;
  96. grub_printf ("\nDiffers in %lld\n", (unsigned long long) pos);
  97. for (i = 0; i < GRUB_DISK_SECTOR_SIZE; i++)
  98. {
  99. grub_printf ("%02x ", buf[pos + i]);
  100. if ((i & 15) == 15)
  101. grub_printf ("\n");
  102. }
  103. if (i)
  104. grub_refresh ();
  105. goto fail;
  106. }
  107. }
  108. grub_printf (" Done.\n");
  109. return GRUB_ERR_NONE;
  110. fail:
  111. grub_file_close (file);
  112. grub_free (buf);
  113. if (!grub_errno)
  114. grub_error (GRUB_ERR_IO, "bad read");
  115. return grub_errno;
  116. }
  117. static grub_command_t cmd;
  118. GRUB_MOD_INIT(testload)
  119. {
  120. cmd =
  121. grub_register_command ("testload", grub_cmd_testload,
  122. N_("FILE"),
  123. N_("Load the same file in multiple ways."));
  124. }
  125. GRUB_MOD_FINI(testload)
  126. {
  127. grub_unregister_command (cmd);
  128. }