memdisk.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* memdisk.c - Access embedded memory disk. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2007,2008 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/disk.h>
  20. #include <grub/dl.h>
  21. #include <grub/kernel.h>
  22. #include <grub/misc.h>
  23. #include <grub/mm.h>
  24. #include <grub/types.h>
  25. #include <grub/safemath.h>
  26. GRUB_MOD_LICENSE ("GPLv3+");
  27. static char *memdisk_addr;
  28. static grub_off_t memdisk_size = 0;
  29. static int
  30. grub_memdisk_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
  31. grub_disk_pull_t pull)
  32. {
  33. if (pull != GRUB_DISK_PULL_NONE)
  34. return 0;
  35. return hook ("memdisk", hook_data);
  36. }
  37. static grub_err_t
  38. grub_memdisk_open (const char *name, grub_disk_t disk)
  39. {
  40. if (grub_strcmp (name, "memdisk"))
  41. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "not a memdisk");
  42. disk->total_sectors = memdisk_size / GRUB_DISK_SECTOR_SIZE;
  43. disk->max_agglomerate = GRUB_DISK_MAX_MAX_AGGLOMERATE;
  44. disk->id = 0;
  45. return GRUB_ERR_NONE;
  46. }
  47. static void
  48. grub_memdisk_close (grub_disk_t disk __attribute((unused)))
  49. {
  50. }
  51. static grub_err_t
  52. grub_memdisk_read (grub_disk_t disk __attribute((unused)), grub_disk_addr_t sector,
  53. grub_size_t size, char *buf)
  54. {
  55. grub_memcpy (buf, memdisk_addr + (sector << GRUB_DISK_SECTOR_BITS), size << GRUB_DISK_SECTOR_BITS);
  56. return 0;
  57. }
  58. static grub_err_t
  59. grub_memdisk_write (grub_disk_t disk __attribute((unused)), grub_disk_addr_t sector,
  60. grub_size_t size, const char *buf)
  61. {
  62. grub_memcpy (memdisk_addr + (sector << GRUB_DISK_SECTOR_BITS), buf, size << GRUB_DISK_SECTOR_BITS);
  63. return 0;
  64. }
  65. static struct grub_disk_dev grub_memdisk_dev =
  66. {
  67. .name = "memdisk",
  68. .id = GRUB_DISK_DEVICE_MEMDISK_ID,
  69. .disk_iterate = grub_memdisk_iterate,
  70. .disk_open = grub_memdisk_open,
  71. .disk_close = grub_memdisk_close,
  72. .disk_read = grub_memdisk_read,
  73. .disk_write = grub_memdisk_write,
  74. .next = 0
  75. };
  76. GRUB_MOD_INIT(memdisk)
  77. {
  78. struct grub_module_header *header;
  79. FOR_MODULES (header)
  80. if (header->type == OBJ_TYPE_MEMDISK)
  81. {
  82. char *memdisk_orig_addr;
  83. memdisk_orig_addr = (char *) header + sizeof (struct grub_module_header);
  84. grub_dprintf ("memdisk", "Found memdisk image at %p\n", memdisk_orig_addr);
  85. if (grub_sub (header->size, sizeof (struct grub_module_header), &memdisk_size))
  86. {
  87. grub_error (GRUB_ERR_OUT_OF_RANGE, "underflow detected while obtaining memdisk size");
  88. return;
  89. }
  90. memdisk_addr = grub_malloc (memdisk_size);
  91. if (memdisk_addr == NULL)
  92. return;
  93. grub_dprintf ("memdisk", "Copying memdisk image to dynamic memory\n");
  94. grub_memmove (memdisk_addr, memdisk_orig_addr, memdisk_size);
  95. grub_disk_dev_register (&grub_memdisk_dev);
  96. break;
  97. }
  98. }
  99. GRUB_MOD_FINI(memdisk)
  100. {
  101. if (! memdisk_size)
  102. return;
  103. grub_free (memdisk_addr);
  104. grub_disk_dev_unregister (&grub_memdisk_dev);
  105. }