memdisk.c 3.2 KB

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