memdisk.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. static char *memdisk_addr;
  26. static grub_off_t memdisk_size = 0;
  27. static int
  28. grub_memdisk_iterate (int (*hook) (const char *name, void *closure),
  29. void *closure)
  30. {
  31. return hook ("memdisk", closure);
  32. }
  33. static grub_err_t
  34. grub_memdisk_open (const char *name, grub_disk_t disk)
  35. {
  36. if (grub_strcmp (name, "memdisk"))
  37. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "not a memdisk");
  38. disk->total_sectors = memdisk_size / GRUB_DISK_SECTOR_SIZE;
  39. disk->id = (unsigned long) "mdsk";
  40. disk->has_partitions = 0;
  41. return GRUB_ERR_NONE;
  42. }
  43. static void
  44. grub_memdisk_close (grub_disk_t disk __attribute((unused)))
  45. {
  46. }
  47. static grub_err_t
  48. grub_memdisk_read (grub_disk_t disk __attribute((unused)), grub_disk_addr_t sector,
  49. grub_size_t size, char *buf)
  50. {
  51. grub_memcpy (buf, memdisk_addr + (sector << GRUB_DISK_SECTOR_BITS), size << GRUB_DISK_SECTOR_BITS);
  52. return 0;
  53. }
  54. static grub_err_t
  55. grub_memdisk_write (grub_disk_t disk __attribute((unused)), grub_disk_addr_t sector,
  56. grub_size_t size, const char *buf)
  57. {
  58. grub_memcpy (memdisk_addr + (sector << GRUB_DISK_SECTOR_BITS), buf, size << GRUB_DISK_SECTOR_BITS);
  59. return 0;
  60. }
  61. static struct grub_disk_dev grub_memdisk_dev =
  62. {
  63. .name = "memdisk",
  64. .id = GRUB_DISK_DEVICE_MEMDISK_ID,
  65. .iterate = grub_memdisk_iterate,
  66. .open = grub_memdisk_open,
  67. .close = grub_memdisk_close,
  68. .read = grub_memdisk_read,
  69. .write = grub_memdisk_write,
  70. .next = 0
  71. };
  72. static int
  73. memdisk_hook (struct grub_module_header *header)
  74. {
  75. if (header->type == OBJ_TYPE_MEMDISK)
  76. {
  77. char *memdisk_orig_addr;
  78. memdisk_orig_addr = (char *) header + sizeof (struct grub_module_header);
  79. grub_dprintf ("memdisk", "Found memdisk image at %p\n", memdisk_orig_addr);
  80. memdisk_size = header->size - sizeof (struct grub_module_header);
  81. memdisk_addr = grub_malloc (memdisk_size);
  82. grub_dprintf ("memdisk", "Copying memdisk image to dynamic memory\n");
  83. grub_memmove (memdisk_addr, memdisk_orig_addr, memdisk_size);
  84. grub_disk_dev_register (&grub_memdisk_dev);
  85. return 1;
  86. }
  87. return 0;
  88. }
  89. GRUB_MOD_INIT(memdisk)
  90. {
  91. grub_module_iterate (memdisk_hook);
  92. }
  93. GRUB_MOD_FINI(memdisk)
  94. {
  95. if (! memdisk_size)
  96. return;
  97. grub_free (memdisk_addr);
  98. grub_disk_dev_unregister (&grub_memdisk_dev);
  99. }