disk.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2002,2003,2004,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/disk.h>
  19. #include <grub/err.h>
  20. #include <grub/mm.h>
  21. #include <grub/types.h>
  22. #include <grub/partition.h>
  23. #include <grub/misc.h>
  24. #include <grub/time.h>
  25. #include <grub/file.h>
  26. #include <grub/i18n.h>
  27. GRUB_MOD_LICENSE ("GPLv3+");
  28. #include "../kern/disk_common.c"
  29. static void
  30. grub_disk_cache_invalidate (unsigned long dev_id, unsigned long disk_id,
  31. grub_disk_addr_t sector)
  32. {
  33. unsigned cache_index;
  34. struct grub_disk_cache *cache;
  35. sector &= ~((grub_disk_addr_t) GRUB_DISK_CACHE_SIZE - 1);
  36. cache_index = grub_disk_cache_get_index (dev_id, disk_id, sector);
  37. cache = grub_disk_cache_table + cache_index;
  38. if (cache->dev_id == dev_id && cache->disk_id == disk_id
  39. && cache->sector == sector && cache->data)
  40. {
  41. cache->lock = 1;
  42. grub_free (cache->data);
  43. cache->data = 0;
  44. cache->lock = 0;
  45. }
  46. }
  47. grub_err_t
  48. grub_disk_write (grub_disk_t disk, grub_disk_addr_t sector,
  49. grub_off_t offset, grub_size_t size, const void *buf)
  50. {
  51. unsigned real_offset;
  52. grub_disk_addr_t aligned_sector;
  53. grub_dprintf ("disk", "Writing `%s'...\n", disk->name);
  54. if (grub_disk_adjust_range (disk, &sector, &offset, size) != GRUB_ERR_NONE)
  55. return -1;
  56. aligned_sector = (sector & ~((1ULL << (disk->log_sector_size
  57. - GRUB_DISK_SECTOR_BITS)) - 1));
  58. real_offset = offset + ((sector - aligned_sector) << GRUB_DISK_SECTOR_BITS);
  59. sector = aligned_sector;
  60. while (size)
  61. {
  62. if (real_offset != 0 || (size < (1U << disk->log_sector_size)
  63. && size != 0))
  64. {
  65. char *tmp_buf;
  66. grub_size_t len;
  67. grub_partition_t part;
  68. tmp_buf = grub_malloc (1U << disk->log_sector_size);
  69. if (!tmp_buf)
  70. return grub_errno;
  71. part = disk->partition;
  72. disk->partition = 0;
  73. if (grub_disk_read (disk, sector,
  74. 0, (1U << disk->log_sector_size), tmp_buf)
  75. != GRUB_ERR_NONE)
  76. {
  77. disk->partition = part;
  78. grub_free (tmp_buf);
  79. goto finish;
  80. }
  81. disk->partition = part;
  82. len = (1U << disk->log_sector_size) - real_offset;
  83. if (len > size)
  84. len = size;
  85. grub_memcpy (tmp_buf + real_offset, buf, len);
  86. grub_disk_cache_invalidate (disk->dev->id, disk->id, sector);
  87. if ((disk->dev->disk_write) (disk, grub_disk_to_native_sector (disk, sector),
  88. 1, tmp_buf) != GRUB_ERR_NONE)
  89. {
  90. grub_free (tmp_buf);
  91. goto finish;
  92. }
  93. grub_free (tmp_buf);
  94. sector += (1U << (disk->log_sector_size - GRUB_DISK_SECTOR_BITS));
  95. buf = (const char *) buf + len;
  96. size -= len;
  97. real_offset = 0;
  98. }
  99. else
  100. {
  101. grub_size_t len;
  102. grub_size_t n;
  103. len = size & ~((1ULL << disk->log_sector_size) - 1);
  104. n = size >> disk->log_sector_size;
  105. if (n > (disk->max_agglomerate
  106. << (GRUB_DISK_CACHE_BITS + GRUB_DISK_SECTOR_BITS
  107. - disk->log_sector_size)))
  108. n = (disk->max_agglomerate
  109. << (GRUB_DISK_CACHE_BITS + GRUB_DISK_SECTOR_BITS
  110. - disk->log_sector_size));
  111. if ((disk->dev->disk_write) (disk, grub_disk_to_native_sector (disk, sector),
  112. n, buf) != GRUB_ERR_NONE)
  113. goto finish;
  114. while (n--)
  115. {
  116. grub_disk_cache_invalidate (disk->dev->id, disk->id, sector);
  117. sector += (1U << (disk->log_sector_size - GRUB_DISK_SECTOR_BITS));
  118. }
  119. buf = (const char *) buf + len;
  120. size -= len;
  121. }
  122. }
  123. finish:
  124. return grub_errno;
  125. }
  126. GRUB_MOD_INIT(disk)
  127. {
  128. grub_disk_write_weak = grub_disk_write;
  129. }
  130. GRUB_MOD_FINI(disk)
  131. {
  132. grub_disk_write_weak = NULL;
  133. }