bufio.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* bufio.c - buffered io access */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 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/err.h>
  20. #include <grub/types.h>
  21. #include <grub/mm.h>
  22. #include <grub/misc.h>
  23. #include <grub/fs.h>
  24. #include <grub/bufio.h>
  25. #include <grub/dl.h>
  26. GRUB_MOD_LICENSE ("GPLv3+");
  27. #define GRUB_BUFIO_DEF_SIZE 8192
  28. #define GRUB_BUFIO_MAX_SIZE 1048576
  29. struct grub_bufio
  30. {
  31. grub_file_t file;
  32. grub_size_t block_size;
  33. grub_size_t buffer_len;
  34. grub_off_t buffer_at;
  35. char buffer[0];
  36. };
  37. typedef struct grub_bufio *grub_bufio_t;
  38. static struct grub_fs grub_bufio_fs;
  39. grub_file_t
  40. grub_bufio_open (grub_file_t io, grub_size_t size)
  41. {
  42. grub_file_t file;
  43. grub_bufio_t bufio = 0;
  44. file = (grub_file_t) grub_zalloc (sizeof (*file));
  45. if (! file)
  46. return 0;
  47. if (size == 0)
  48. size = GRUB_BUFIO_DEF_SIZE;
  49. else if (size > GRUB_BUFIO_MAX_SIZE)
  50. size = GRUB_BUFIO_MAX_SIZE;
  51. if (size > io->size)
  52. size = ((io->size > GRUB_BUFIO_MAX_SIZE) ? GRUB_BUFIO_MAX_SIZE :
  53. io->size);
  54. /*
  55. * Round up size to power of 2 which the binary math to
  56. * calculate next_buf in grub_bufio_read() requires.
  57. */
  58. while (size & (size - 1))
  59. size = (size | (size - 1)) + 1;
  60. bufio = grub_zalloc (sizeof (struct grub_bufio) + size);
  61. if (! bufio)
  62. {
  63. grub_free (file);
  64. return 0;
  65. }
  66. bufio->file = io;
  67. bufio->block_size = size;
  68. file->device = io->device;
  69. file->size = io->size;
  70. file->data = bufio;
  71. file->fs = &grub_bufio_fs;
  72. file->not_easily_seekable = io->not_easily_seekable;
  73. return file;
  74. }
  75. grub_file_t
  76. grub_buffile_open (const char *name, enum grub_file_type type, grub_size_t size)
  77. {
  78. grub_file_t io, file;
  79. io = grub_file_open (name, type);
  80. if (! io)
  81. return 0;
  82. file = grub_bufio_open (io, size);
  83. if (! file)
  84. {
  85. grub_file_close (io);
  86. return 0;
  87. }
  88. return file;
  89. }
  90. static grub_ssize_t
  91. grub_bufio_read (grub_file_t file, char *buf, grub_size_t len)
  92. {
  93. grub_size_t res = 0;
  94. grub_off_t next_buf;
  95. grub_bufio_t bufio = file->data;
  96. grub_ssize_t really_read;
  97. if (file->size == GRUB_FILE_SIZE_UNKNOWN)
  98. file->size = bufio->file->size;
  99. /* First part: use whatever we already have in the buffer. */
  100. if ((file->offset >= bufio->buffer_at) &&
  101. (file->offset < bufio->buffer_at + bufio->buffer_len))
  102. {
  103. grub_size_t n;
  104. grub_uint64_t pos;
  105. pos = file->offset - bufio->buffer_at;
  106. n = bufio->buffer_len - pos;
  107. if (n > len)
  108. n = len;
  109. grub_memcpy (buf, &bufio->buffer[pos], n);
  110. len -= n;
  111. res += n;
  112. buf += n;
  113. }
  114. if (len == 0)
  115. return res;
  116. /* Need to read some more. */
  117. next_buf = (file->offset + res + len - 1) & ~((grub_off_t) bufio->block_size - 1);
  118. /* Now read between file->offset + res and bufio->buffer_at. */
  119. if (file->offset + res < next_buf)
  120. {
  121. grub_size_t read_now;
  122. read_now = next_buf - (file->offset + res);
  123. grub_file_seek (bufio->file, file->offset + res);
  124. really_read = grub_file_read (bufio->file, buf, read_now);
  125. if (really_read < 0)
  126. return -1;
  127. if (file->size == GRUB_FILE_SIZE_UNKNOWN)
  128. file->size = bufio->file->size;
  129. len -= really_read;
  130. buf += really_read;
  131. res += really_read;
  132. /* Partial read. File ended unexpectedly. Save the last chunk in buffer.
  133. */
  134. if (really_read != (grub_ssize_t) read_now)
  135. {
  136. bufio->buffer_len = really_read;
  137. if (bufio->buffer_len > bufio->block_size)
  138. bufio->buffer_len = bufio->block_size;
  139. bufio->buffer_at = file->offset + res - bufio->buffer_len;
  140. grub_memcpy (&bufio->buffer[0], buf - bufio->buffer_len,
  141. bufio->buffer_len);
  142. return res;
  143. }
  144. }
  145. /* Read into buffer. */
  146. grub_file_seek (bufio->file, next_buf);
  147. really_read = grub_file_read (bufio->file, bufio->buffer,
  148. bufio->block_size);
  149. if (really_read < 0)
  150. return -1;
  151. bufio->buffer_at = next_buf;
  152. bufio->buffer_len = really_read;
  153. if (file->size == GRUB_FILE_SIZE_UNKNOWN)
  154. file->size = bufio->file->size;
  155. if (len > bufio->buffer_len)
  156. len = bufio->buffer_len;
  157. grub_memcpy (buf, &bufio->buffer[file->offset + res - next_buf], len);
  158. res += len;
  159. return res;
  160. }
  161. static grub_err_t
  162. grub_bufio_close (grub_file_t file)
  163. {
  164. grub_bufio_t bufio = file->data;
  165. grub_file_close (bufio->file);
  166. grub_free (bufio);
  167. file->device = 0;
  168. return grub_errno;
  169. }
  170. static struct grub_fs grub_bufio_fs =
  171. {
  172. .name = "bufio",
  173. .fs_dir = 0,
  174. .fs_open = 0,
  175. .fs_read = grub_bufio_read,
  176. .fs_close = grub_bufio_close,
  177. .fs_label = 0,
  178. .next = 0
  179. };