testload.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. /* Helper for grub_cmd_testload. */
  32. static void
  33. read_progress (grub_disk_addr_t sector __attribute__ ((unused)),
  34. unsigned offset __attribute__ ((unused)),
  35. unsigned len,
  36. void *data __attribute__ ((unused)))
  37. {
  38. for (; len >= GRUB_DISK_SECTOR_SIZE; len -= GRUB_DISK_SECTOR_SIZE)
  39. grub_xputs (".");
  40. if (len)
  41. grub_xputs (".");
  42. grub_refresh ();
  43. }
  44. static grub_err_t
  45. grub_cmd_testload (struct grub_command *cmd __attribute__ ((unused)),
  46. int argc, char *argv[])
  47. {
  48. grub_file_t file;
  49. char *buf;
  50. grub_size_t size;
  51. grub_off_t pos;
  52. if (argc < 1)
  53. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  54. file = grub_file_open (argv[0]);
  55. if (! file)
  56. return grub_errno;
  57. size = grub_file_size (file) & ~(GRUB_DISK_SECTOR_SIZE - 1);
  58. if (size == 0)
  59. {
  60. grub_file_close (file);
  61. return GRUB_ERR_NONE;
  62. }
  63. buf = grub_malloc (size);
  64. if (! buf)
  65. goto fail;
  66. grub_printf ("Reading %s sequentially", argv[0]);
  67. file->read_hook = read_progress;
  68. if (grub_file_read (file, buf, size) != (grub_ssize_t) size)
  69. goto fail;
  70. grub_printf (" Done.\n");
  71. /* Read sequentially again. */
  72. grub_printf ("Reading %s sequentially again", argv[0]);
  73. grub_file_seek (file, 0);
  74. for (pos = 0; pos < size;)
  75. {
  76. char sector[GRUB_DISK_SECTOR_SIZE];
  77. grub_size_t curlen = GRUB_DISK_SECTOR_SIZE;
  78. if (curlen > size - pos)
  79. curlen = size - pos;
  80. if (grub_file_read (file, sector, curlen)
  81. != (grub_ssize_t) curlen)
  82. goto fail;
  83. if (grub_memcmp (sector, buf + pos, curlen) != 0)
  84. {
  85. grub_printf ("\nDiffers in %lld\n", (unsigned long long) pos);
  86. goto fail;
  87. }
  88. pos += curlen;
  89. }
  90. grub_printf (" Done.\n");
  91. /* Read backwards and compare. */
  92. grub_printf ("Reading %s backwards", argv[0]);
  93. pos = size;
  94. while (pos > 0)
  95. {
  96. char sector[GRUB_DISK_SECTOR_SIZE];
  97. if (pos >= GRUB_DISK_SECTOR_SIZE)
  98. pos -= GRUB_DISK_SECTOR_SIZE;
  99. else
  100. pos = 0;
  101. grub_file_seek (file, pos);
  102. if (grub_file_read (file, sector, GRUB_DISK_SECTOR_SIZE)
  103. != GRUB_DISK_SECTOR_SIZE)
  104. goto fail;
  105. if (grub_memcmp (sector, buf + pos, GRUB_DISK_SECTOR_SIZE) != 0)
  106. {
  107. int i;
  108. grub_printf ("\nDiffers in %lld\n", (unsigned long long) pos);
  109. for (i = 0; i < GRUB_DISK_SECTOR_SIZE; i++)
  110. {
  111. grub_printf ("%02x ", buf[pos + i]);
  112. if ((i & 15) == 15)
  113. grub_printf ("\n");
  114. }
  115. if (i)
  116. grub_refresh ();
  117. goto fail;
  118. }
  119. }
  120. grub_printf (" Done.\n");
  121. return GRUB_ERR_NONE;
  122. fail:
  123. grub_file_close (file);
  124. grub_free (buf);
  125. if (!grub_errno)
  126. grub_error (GRUB_ERR_IO, "bad read");
  127. return grub_errno;
  128. }
  129. static grub_command_t cmd;
  130. GRUB_MOD_INIT(testload)
  131. {
  132. cmd =
  133. grub_register_command ("testload", grub_cmd_testload,
  134. N_("FILE"),
  135. N_("Load the same file in multiple ways."));
  136. }
  137. GRUB_MOD_FINI(testload)
  138. {
  139. grub_unregister_command (cmd);
  140. }