offset.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2013 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/file.h>
  19. #include <grub/dl.h>
  20. GRUB_MOD_LICENSE ("GPLv3+");
  21. struct grub_offset_file
  22. {
  23. grub_file_t parent;
  24. grub_off_t off;
  25. };
  26. static grub_ssize_t
  27. grub_offset_read (grub_file_t file, char *buf, grub_size_t len)
  28. {
  29. struct grub_offset_file *data = file->data;
  30. if (grub_file_seek (data->parent, data->off + file->offset) == (grub_off_t) -1)
  31. return -1;
  32. return grub_file_read (data->parent, buf, len);
  33. }
  34. static grub_err_t
  35. grub_offset_close (grub_file_t file)
  36. {
  37. struct grub_offset_file *data = file->data;
  38. if (data->parent)
  39. grub_file_close (data->parent);
  40. /* No need to close the same device twice. */
  41. file->device = 0;
  42. return 0;
  43. }
  44. static struct grub_fs grub_offset_fs = {
  45. .name = "offset",
  46. .dir = 0,
  47. .open = 0,
  48. .read = grub_offset_read,
  49. .close = grub_offset_close,
  50. .label = 0,
  51. .next = 0
  52. };
  53. void
  54. grub_file_offset_close (grub_file_t file)
  55. {
  56. struct grub_offset_file *off_data = file->data;
  57. off_data->parent = NULL;
  58. grub_file_close (file);
  59. }
  60. grub_file_t
  61. grub_file_offset_open (grub_file_t parent, grub_off_t start, grub_off_t size)
  62. {
  63. struct grub_offset_file *off_data;
  64. grub_file_t off_file, last_off_file;
  65. grub_file_filter_id_t filter;
  66. off_file = grub_zalloc (sizeof (*off_file));
  67. off_data = grub_zalloc (sizeof (*off_data));
  68. if (!off_file || !off_data)
  69. {
  70. grub_free (off_file);
  71. grub_free (off_data);
  72. return 0;
  73. }
  74. off_data->off = start;
  75. off_data->parent = parent;
  76. off_file->device = parent->device;
  77. off_file->data = off_data;
  78. off_file->fs = &grub_offset_fs;
  79. off_file->size = size;
  80. last_off_file = NULL;
  81. for (filter = GRUB_FILE_FILTER_COMPRESSION_FIRST;
  82. off_file && filter <= GRUB_FILE_FILTER_COMPRESSION_LAST; filter++)
  83. if (grub_file_filters_enabled[filter])
  84. {
  85. last_off_file = off_file;
  86. off_file = grub_file_filters_enabled[filter] (off_file, parent->name);
  87. }
  88. if (!off_file)
  89. {
  90. off_data->parent = NULL;
  91. grub_file_close (last_off_file);
  92. return 0;
  93. }
  94. return off_file;
  95. }